Merge trunk version 207818 into gupc branch.
[official-gcc.git] / gcc / dwarf2out.c
blobea86a18bad49981f5112178bf65e386455b0815a
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 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 "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "tree-dfa.h"
99 #include "gdb/gdb-index.h"
101 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
102 static rtx last_var_location_insn;
103 static rtx cached_next_real_insn;
105 #ifdef VMS_DEBUGGING_INFO
106 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
108 /* Define this macro to be a nonzero value if the directory specifications
109 which are output in the debug info should end with a separator. */
110 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
111 /* Define this macro to evaluate to a nonzero value if GCC should refrain
112 from generating indirect strings in DWARF2 debug information, for instance
113 if your target is stuck with an old version of GDB that is unable to
114 process them properly or uses VMS Debug. */
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
116 #else
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
119 #endif
121 /* ??? Poison these here until it can be done generically. They've been
122 totally replaced in this file; make sure it stays that way. */
123 #undef DWARF2_UNWIND_INFO
124 #undef DWARF2_FRAME_INFO
125 #if (GCC_VERSION >= 3000)
126 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
127 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 vec<tree, va_gc> *because we want to tell the garbage collector about
141 it. */
142 static GTY(()) vec<tree, va_gc> *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) vec<tree, va_gc> *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_skeleton_info_section;
154 static GTY(()) section *debug_abbrev_section;
155 static GTY(()) section *debug_skeleton_abbrev_section;
156 static GTY(()) section *debug_aranges_section;
157 static GTY(()) section *debug_addr_section;
158 static GTY(()) section *debug_macinfo_section;
159 static GTY(()) section *debug_line_section;
160 static GTY(()) section *debug_skeleton_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_str_dwo_section;
166 static GTY(()) section *debug_str_offsets_section;
167 static GTY(()) section *debug_ranges_section;
168 static GTY(()) section *debug_frame_section;
170 /* Maximum size (in bytes) of an artificially generated label. */
171 #define MAX_ARTIFICIAL_LABEL_BYTES 30
173 /* According to the (draft) DWARF 3 specification, the initial length
174 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
175 bytes are 0xffffffff, followed by the length stored in the next 8
176 bytes.
178 However, the SGI/MIPS ABI uses an initial length which is equal to
179 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
181 #ifndef DWARF_INITIAL_LENGTH_SIZE
182 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
183 #endif
185 /* Round SIZE up to the nearest BOUNDARY. */
186 #define DWARF_ROUND(SIZE,BOUNDARY) \
187 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
189 /* CIE identifier. */
190 #if HOST_BITS_PER_WIDE_INT >= 64
191 #define DWARF_CIE_ID \
192 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
193 #else
194 #define DWARF_CIE_ID DW_CIE_ID
195 #endif
198 /* A vector for a table that contains frame description
199 information for each routine. */
200 #define NOT_INDEXED (-1U)
201 #define NO_INDEX_ASSIGNED (-2U)
203 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
205 struct GTY(()) indirect_string_node {
206 const char *str;
207 unsigned int refcount;
208 enum dwarf_form form;
209 char *label;
210 unsigned int index;
213 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
215 /* With split_debug_info, both the comp_dir and dwo_name go in the
216 main object file, rather than the dwo, similar to the force_direct
217 parameter elsewhere but with additional complications:
219 1) The string is needed in both the main object file and the dwo.
220 That is, the comp_dir and dwo_name will appear in both places.
222 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
223 DW_FORM_GNU_str_index.
225 3) GCC chooses the form to use late, depending on the size and
226 reference count.
228 Rather than forcing the all debug string handling functions and
229 callers to deal with these complications, simply use a separate,
230 special-cased string table for any attribute that should go in the
231 main object file. This limits the complexity to just the places
232 that need it. */
234 static GTY ((param_is (struct indirect_string_node)))
235 htab_t skeleton_debug_str_hash;
237 static GTY(()) int dw2_string_counter;
239 /* True if the compilation unit places functions in more than one section. */
240 static GTY(()) bool have_multiple_function_sections = false;
242 /* Whether the default text and cold text sections have been used at all. */
244 static GTY(()) bool text_section_used = false;
245 static GTY(()) bool cold_text_section_used = false;
247 /* The default cold text section. */
248 static GTY(()) section *cold_text_section;
250 /* The DIE for C++1y 'auto' in a function return type. */
251 static GTY(()) dw_die_ref auto_die;
253 /* Forward declarations for functions defined in this file. */
255 static char *stripattributes (const char *);
256 static void output_call_frame_info (int);
257 static void dwarf2out_note_section_used (void);
259 /* Personality decl of current unit. Used only when assembler does not support
260 personality CFI. */
261 static GTY(()) rtx current_unit_personality;
263 /* Data and reference forms for relocatable data. */
264 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
265 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
267 #ifndef DEBUG_FRAME_SECTION
268 #define DEBUG_FRAME_SECTION ".debug_frame"
269 #endif
271 #ifndef FUNC_BEGIN_LABEL
272 #define FUNC_BEGIN_LABEL "LFB"
273 #endif
275 #ifndef FUNC_END_LABEL
276 #define FUNC_END_LABEL "LFE"
277 #endif
279 #ifndef PROLOGUE_END_LABEL
280 #define PROLOGUE_END_LABEL "LPE"
281 #endif
283 #ifndef EPILOGUE_BEGIN_LABEL
284 #define EPILOGUE_BEGIN_LABEL "LEB"
285 #endif
287 #ifndef FRAME_BEGIN_LABEL
288 #define FRAME_BEGIN_LABEL "Lframe"
289 #endif
290 #define CIE_AFTER_SIZE_LABEL "LSCIE"
291 #define CIE_END_LABEL "LECIE"
292 #define FDE_LABEL "LSFDE"
293 #define FDE_AFTER_SIZE_LABEL "LASFDE"
294 #define FDE_END_LABEL "LEFDE"
295 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
296 #define LINE_NUMBER_END_LABEL "LELT"
297 #define LN_PROLOG_AS_LABEL "LASLTP"
298 #define LN_PROLOG_END_LABEL "LELTP"
299 #define DIE_LABEL_PREFIX "DW"
301 /* Match the base name of a file to the base name of a compilation unit. */
303 static int
304 matches_main_base (const char *path)
306 /* Cache the last query. */
307 static const char *last_path = NULL;
308 static int last_match = 0;
309 if (path != last_path)
311 const char *base;
312 int length = base_of_path (path, &base);
313 last_path = path;
314 last_match = (length == main_input_baselength
315 && memcmp (base, main_input_basename, length) == 0);
317 return last_match;
320 #ifdef DEBUG_DEBUG_STRUCT
322 static int
323 dump_struct_debug (tree type, enum debug_info_usage usage,
324 enum debug_struct_file criterion, int generic,
325 int matches, int result)
327 /* Find the type name. */
328 tree type_decl = TYPE_STUB_DECL (type);
329 tree t = type_decl;
330 const char *name = 0;
331 if (TREE_CODE (t) == TYPE_DECL)
332 t = DECL_NAME (t);
333 if (t)
334 name = IDENTIFIER_POINTER (t);
336 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
337 criterion,
338 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
339 matches ? "bas" : "hdr",
340 generic ? "gen" : "ord",
341 usage == DINFO_USAGE_DFN ? ";" :
342 usage == DINFO_USAGE_DIR_USE ? "." : "*",
343 result,
344 (void*) type_decl, name);
345 return result;
347 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
348 dump_struct_debug (type, usage, criterion, generic, matches, result)
350 #else
352 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
353 (result)
355 #endif
357 static bool
358 should_emit_struct_debug (tree type, enum debug_info_usage usage)
360 enum debug_struct_file criterion;
361 tree type_decl;
362 bool generic = lang_hooks.types.generic_p (type);
364 if (generic)
365 criterion = debug_struct_generic[usage];
366 else
367 criterion = debug_struct_ordinary[usage];
369 if (criterion == DINFO_STRUCT_FILE_NONE)
370 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
371 if (criterion == DINFO_STRUCT_FILE_ANY)
372 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
374 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
376 if (type_decl != NULL)
378 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
379 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
381 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
382 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
385 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
388 /* Return a pointer to a copy of the section string name S with all
389 attributes stripped off, and an asterisk prepended (for assemble_name). */
391 static inline char *
392 stripattributes (const char *s)
394 char *stripped = XNEWVEC (char, strlen (s) + 2);
395 char *p = stripped;
397 *p++ = '*';
399 while (*s && *s != ',')
400 *p++ = *s++;
402 *p = '\0';
403 return stripped;
406 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
407 switch to the data section instead, and write out a synthetic start label
408 for collect2 the first time around. */
410 static void
411 switch_to_eh_frame_section (bool back)
413 tree label;
415 #ifdef EH_FRAME_SECTION_NAME
416 if (eh_frame_section == 0)
418 int flags;
420 if (EH_TABLES_CAN_BE_READ_ONLY)
422 int fde_encoding;
423 int per_encoding;
424 int lsda_encoding;
426 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
427 /*global=*/0);
428 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
429 /*global=*/1);
430 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
431 /*global=*/0);
432 flags = ((! flag_pic
433 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
434 && (fde_encoding & 0x70) != DW_EH_PE_aligned
435 && (per_encoding & 0x70) != DW_EH_PE_absptr
436 && (per_encoding & 0x70) != DW_EH_PE_aligned
437 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
438 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
439 ? 0 : SECTION_WRITE);
441 else
442 flags = SECTION_WRITE;
443 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
445 #endif /* EH_FRAME_SECTION_NAME */
447 if (eh_frame_section)
448 switch_to_section (eh_frame_section);
449 else
451 /* We have no special eh_frame section. Put the information in
452 the data section and emit special labels to guide collect2. */
453 switch_to_section (data_section);
455 if (!back)
457 label = get_file_function_name ("F");
458 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
459 targetm.asm_out.globalize_label (asm_out_file,
460 IDENTIFIER_POINTER (label));
461 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
466 /* Switch [BACK] to the eh or debug frame table section, depending on
467 FOR_EH. */
469 static void
470 switch_to_frame_table_section (int for_eh, bool back)
472 if (for_eh)
473 switch_to_eh_frame_section (back);
474 else
476 if (!debug_frame_section)
477 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
478 SECTION_DEBUG, NULL);
479 switch_to_section (debug_frame_section);
483 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
485 enum dw_cfi_oprnd_type
486 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
488 switch (cfi)
490 case DW_CFA_nop:
491 case DW_CFA_GNU_window_save:
492 case DW_CFA_remember_state:
493 case DW_CFA_restore_state:
494 return dw_cfi_oprnd_unused;
496 case DW_CFA_set_loc:
497 case DW_CFA_advance_loc1:
498 case DW_CFA_advance_loc2:
499 case DW_CFA_advance_loc4:
500 case DW_CFA_MIPS_advance_loc8:
501 return dw_cfi_oprnd_addr;
503 case DW_CFA_offset:
504 case DW_CFA_offset_extended:
505 case DW_CFA_def_cfa:
506 case DW_CFA_offset_extended_sf:
507 case DW_CFA_def_cfa_sf:
508 case DW_CFA_restore:
509 case DW_CFA_restore_extended:
510 case DW_CFA_undefined:
511 case DW_CFA_same_value:
512 case DW_CFA_def_cfa_register:
513 case DW_CFA_register:
514 case DW_CFA_expression:
515 return dw_cfi_oprnd_reg_num;
517 case DW_CFA_def_cfa_offset:
518 case DW_CFA_GNU_args_size:
519 case DW_CFA_def_cfa_offset_sf:
520 return dw_cfi_oprnd_offset;
522 case DW_CFA_def_cfa_expression:
523 return dw_cfi_oprnd_loc;
525 default:
526 gcc_unreachable ();
530 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
532 enum dw_cfi_oprnd_type
533 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
535 switch (cfi)
537 case DW_CFA_def_cfa:
538 case DW_CFA_def_cfa_sf:
539 case DW_CFA_offset:
540 case DW_CFA_offset_extended_sf:
541 case DW_CFA_offset_extended:
542 return dw_cfi_oprnd_offset;
544 case DW_CFA_register:
545 return dw_cfi_oprnd_reg_num;
547 case DW_CFA_expression:
548 return dw_cfi_oprnd_loc;
550 default:
551 return dw_cfi_oprnd_unused;
555 /* Output one FDE. */
557 static void
558 output_fde (dw_fde_ref fde, bool for_eh, bool second,
559 char *section_start_label, int fde_encoding, char *augmentation,
560 bool any_lsda_needed, int lsda_encoding)
562 const char *begin, *end;
563 static unsigned int j;
564 char l1[20], l2[20];
566 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
567 /* empty */ 0);
568 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
569 for_eh + j);
570 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
571 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
572 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
573 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
574 " indicating 64-bit DWARF extension");
575 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
576 "FDE Length");
577 ASM_OUTPUT_LABEL (asm_out_file, l1);
579 if (for_eh)
580 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
581 else
582 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
583 debug_frame_section, "FDE CIE offset");
585 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
586 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
588 if (for_eh)
590 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
591 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
592 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
593 "FDE initial location");
594 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
595 end, begin, "FDE address range");
597 else
599 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
600 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
603 if (augmentation[0])
605 if (any_lsda_needed)
607 int size = size_of_encoded_value (lsda_encoding);
609 if (lsda_encoding == DW_EH_PE_aligned)
611 int offset = ( 4 /* Length */
612 + 4 /* CIE offset */
613 + 2 * size_of_encoded_value (fde_encoding)
614 + 1 /* Augmentation size */ );
615 int pad = -offset & (PTR_SIZE - 1);
617 size += pad;
618 gcc_assert (size_of_uleb128 (size) == 1);
621 dw2_asm_output_data_uleb128 (size, "Augmentation size");
623 if (fde->uses_eh_lsda)
625 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
626 fde->funcdef_number);
627 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
628 gen_rtx_SYMBOL_REF (Pmode, l1),
629 false,
630 "Language Specific Data Area");
632 else
634 if (lsda_encoding == DW_EH_PE_aligned)
635 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
636 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
637 "Language Specific Data Area (none)");
640 else
641 dw2_asm_output_data_uleb128 (0, "Augmentation size");
644 /* Loop through the Call Frame Instructions associated with this FDE. */
645 fde->dw_fde_current_label = begin;
647 size_t from, until, i;
649 from = 0;
650 until = vec_safe_length (fde->dw_fde_cfi);
652 if (fde->dw_fde_second_begin == NULL)
654 else if (!second)
655 until = fde->dw_fde_switch_cfi_index;
656 else
657 from = fde->dw_fde_switch_cfi_index;
659 for (i = from; i < until; i++)
660 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
663 /* If we are to emit a ref/link from function bodies to their frame tables,
664 do it now. This is typically performed to make sure that tables
665 associated with functions are dragged with them and not discarded in
666 garbage collecting links. We need to do this on a per function basis to
667 cope with -ffunction-sections. */
669 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
670 /* Switch to the function section, emit the ref to the tables, and
671 switch *back* into the table section. */
672 switch_to_section (function_section (fde->decl));
673 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
674 switch_to_frame_table_section (for_eh, true);
675 #endif
677 /* Pad the FDE out to an address sized boundary. */
678 ASM_OUTPUT_ALIGN (asm_out_file,
679 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
680 ASM_OUTPUT_LABEL (asm_out_file, l2);
682 j += 2;
685 /* Return true if frame description entry FDE is needed for EH. */
687 static bool
688 fde_needed_for_eh_p (dw_fde_ref fde)
690 if (flag_asynchronous_unwind_tables)
691 return true;
693 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
694 return true;
696 if (fde->uses_eh_lsda)
697 return true;
699 /* If exceptions are enabled, we have collected nothrow info. */
700 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
701 return false;
703 return true;
706 /* Output the call frame information used to record information
707 that relates to calculating the frame pointer, and records the
708 location of saved registers. */
710 static void
711 output_call_frame_info (int for_eh)
713 unsigned int i;
714 dw_fde_ref fde;
715 dw_cfi_ref cfi;
716 char l1[20], l2[20], section_start_label[20];
717 bool any_lsda_needed = false;
718 char augmentation[6];
719 int augmentation_size;
720 int fde_encoding = DW_EH_PE_absptr;
721 int per_encoding = DW_EH_PE_absptr;
722 int lsda_encoding = DW_EH_PE_absptr;
723 int return_reg;
724 rtx personality = NULL;
725 int dw_cie_version;
727 /* Don't emit a CIE if there won't be any FDEs. */
728 if (!fde_vec)
729 return;
731 /* Nothing to do if the assembler's doing it all. */
732 if (dwarf2out_do_cfi_asm ())
733 return;
735 /* If we don't have any functions we'll want to unwind out of, don't emit
736 any EH unwind information. If we make FDEs linkonce, we may have to
737 emit an empty label for an FDE that wouldn't otherwise be emitted. We
738 want to avoid having an FDE kept around when the function it refers to
739 is discarded. Example where this matters: a primary function template
740 in C++ requires EH information, an explicit specialization doesn't. */
741 if (for_eh)
743 bool any_eh_needed = false;
745 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
747 if (fde->uses_eh_lsda)
748 any_eh_needed = any_lsda_needed = true;
749 else if (fde_needed_for_eh_p (fde))
750 any_eh_needed = true;
751 else if (TARGET_USES_WEAK_UNWIND_INFO)
752 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
755 if (!any_eh_needed)
756 return;
759 /* We're going to be generating comments, so turn on app. */
760 if (flag_debug_asm)
761 app_enable ();
763 /* Switch to the proper frame section, first time. */
764 switch_to_frame_table_section (for_eh, false);
766 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
767 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
769 /* Output the CIE. */
770 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
771 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
772 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
773 dw2_asm_output_data (4, 0xffffffff,
774 "Initial length escape value indicating 64-bit DWARF extension");
775 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
776 "Length of Common Information Entry");
777 ASM_OUTPUT_LABEL (asm_out_file, l1);
779 /* Now that the CIE pointer is PC-relative for EH,
780 use 0 to identify the CIE. */
781 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
782 (for_eh ? 0 : DWARF_CIE_ID),
783 "CIE Identifier Tag");
785 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
786 use CIE version 1, unless that would produce incorrect results
787 due to overflowing the return register column. */
788 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
789 dw_cie_version = 1;
790 if (return_reg >= 256 || dwarf_version > 2)
791 dw_cie_version = 3;
792 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
794 augmentation[0] = 0;
795 augmentation_size = 0;
797 personality = current_unit_personality;
798 if (for_eh)
800 char *p;
802 /* Augmentation:
803 z Indicates that a uleb128 is present to size the
804 augmentation section.
805 L Indicates the encoding (and thus presence) of
806 an LSDA pointer in the FDE augmentation.
807 R Indicates a non-default pointer encoding for
808 FDE code pointers.
809 P Indicates the presence of an encoding + language
810 personality routine in the CIE augmentation. */
812 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
813 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
814 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
816 p = augmentation + 1;
817 if (personality)
819 *p++ = 'P';
820 augmentation_size += 1 + size_of_encoded_value (per_encoding);
821 assemble_external_libcall (personality);
823 if (any_lsda_needed)
825 *p++ = 'L';
826 augmentation_size += 1;
828 if (fde_encoding != DW_EH_PE_absptr)
830 *p++ = 'R';
831 augmentation_size += 1;
833 if (p > augmentation + 1)
835 augmentation[0] = 'z';
836 *p = '\0';
839 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
840 if (personality && per_encoding == DW_EH_PE_aligned)
842 int offset = ( 4 /* Length */
843 + 4 /* CIE Id */
844 + 1 /* CIE version */
845 + strlen (augmentation) + 1 /* Augmentation */
846 + size_of_uleb128 (1) /* Code alignment */
847 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
848 + 1 /* RA column */
849 + 1 /* Augmentation size */
850 + 1 /* Personality encoding */ );
851 int pad = -offset & (PTR_SIZE - 1);
853 augmentation_size += pad;
855 /* Augmentations should be small, so there's scarce need to
856 iterate for a solution. Die if we exceed one uleb128 byte. */
857 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
861 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
862 if (dw_cie_version >= 4)
864 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
865 dw2_asm_output_data (1, 0, "CIE Segment Size");
867 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
868 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
869 "CIE Data Alignment Factor");
871 if (dw_cie_version == 1)
872 dw2_asm_output_data (1, return_reg, "CIE RA Column");
873 else
874 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
876 if (augmentation[0])
878 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
879 if (personality)
881 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
882 eh_data_format_name (per_encoding));
883 dw2_asm_output_encoded_addr_rtx (per_encoding,
884 personality,
885 true, NULL);
888 if (any_lsda_needed)
889 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
890 eh_data_format_name (lsda_encoding));
892 if (fde_encoding != DW_EH_PE_absptr)
893 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
894 eh_data_format_name (fde_encoding));
897 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
898 output_cfi (cfi, NULL, for_eh);
900 /* Pad the CIE out to an address sized boundary. */
901 ASM_OUTPUT_ALIGN (asm_out_file,
902 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
903 ASM_OUTPUT_LABEL (asm_out_file, l2);
905 /* Loop through all of the FDE's. */
906 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
908 unsigned int k;
910 /* Don't emit EH unwind info for leaf functions that don't need it. */
911 if (for_eh && !fde_needed_for_eh_p (fde))
912 continue;
914 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
915 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
916 augmentation, any_lsda_needed, lsda_encoding);
919 if (for_eh && targetm.terminate_dw2_eh_frame_info)
920 dw2_asm_output_data (4, 0, "End of Table");
922 /* Turn off app to make assembly quicker. */
923 if (flag_debug_asm)
924 app_disable ();
927 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
929 static void
930 dwarf2out_do_cfi_startproc (bool second)
932 int enc;
933 rtx ref;
934 rtx personality = get_personality_function (current_function_decl);
936 fprintf (asm_out_file, "\t.cfi_startproc\n");
938 if (personality)
940 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
941 ref = personality;
943 /* ??? The GAS support isn't entirely consistent. We have to
944 handle indirect support ourselves, but PC-relative is done
945 in the assembler. Further, the assembler can't handle any
946 of the weirder relocation types. */
947 if (enc & DW_EH_PE_indirect)
948 ref = dw2_force_const_mem (ref, true);
950 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
951 output_addr_const (asm_out_file, ref);
952 fputc ('\n', asm_out_file);
955 if (crtl->uses_eh_lsda)
957 char lab[20];
959 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
960 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
961 current_function_funcdef_no);
962 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
963 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
965 if (enc & DW_EH_PE_indirect)
966 ref = dw2_force_const_mem (ref, true);
968 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
969 output_addr_const (asm_out_file, ref);
970 fputc ('\n', asm_out_file);
974 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
975 this allocation may be done before pass_final. */
977 dw_fde_ref
978 dwarf2out_alloc_current_fde (void)
980 dw_fde_ref fde;
982 fde = ggc_alloc_cleared_dw_fde_node ();
983 fde->decl = current_function_decl;
984 fde->funcdef_number = current_function_funcdef_no;
985 fde->fde_index = vec_safe_length (fde_vec);
986 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
987 fde->uses_eh_lsda = crtl->uses_eh_lsda;
988 fde->nothrow = crtl->nothrow;
989 fde->drap_reg = INVALID_REGNUM;
990 fde->vdrap_reg = INVALID_REGNUM;
992 /* Record the FDE associated with this function. */
993 cfun->fde = fde;
994 vec_safe_push (fde_vec, fde);
996 return fde;
999 /* Output a marker (i.e. a label) for the beginning of a function, before
1000 the prologue. */
1002 void
1003 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1004 const char *file ATTRIBUTE_UNUSED)
1006 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1007 char * dup_label;
1008 dw_fde_ref fde;
1009 section *fnsec;
1010 bool do_frame;
1012 current_function_func_begin_label = NULL;
1014 do_frame = dwarf2out_do_frame ();
1016 /* ??? current_function_func_begin_label is also used by except.c for
1017 call-site information. We must emit this label if it might be used. */
1018 if (!do_frame
1019 && (!flag_exceptions
1020 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1021 return;
1023 fnsec = function_section (current_function_decl);
1024 switch_to_section (fnsec);
1025 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1026 current_function_funcdef_no);
1027 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1028 current_function_funcdef_no);
1029 dup_label = xstrdup (label);
1030 current_function_func_begin_label = dup_label;
1032 /* We can elide the fde allocation if we're not emitting debug info. */
1033 if (!do_frame)
1034 return;
1036 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1037 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1038 would include pass_dwarf2_frame. If we've not created the FDE yet,
1039 do so now. */
1040 fde = cfun->fde;
1041 if (fde == NULL)
1042 fde = dwarf2out_alloc_current_fde ();
1044 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1045 fde->dw_fde_begin = dup_label;
1046 fde->dw_fde_current_label = dup_label;
1047 fde->in_std_section = (fnsec == text_section
1048 || (cold_text_section && fnsec == cold_text_section));
1050 /* We only want to output line number information for the genuine dwarf2
1051 prologue case, not the eh frame case. */
1052 #ifdef DWARF2_DEBUGGING_INFO
1053 if (file)
1054 dwarf2out_source_line (line, file, 0, true);
1055 #endif
1057 if (dwarf2out_do_cfi_asm ())
1058 dwarf2out_do_cfi_startproc (false);
1059 else
1061 rtx personality = get_personality_function (current_function_decl);
1062 if (!current_unit_personality)
1063 current_unit_personality = personality;
1065 /* We cannot keep a current personality per function as without CFI
1066 asm, at the point where we emit the CFI data, there is no current
1067 function anymore. */
1068 if (personality && current_unit_personality != personality)
1069 sorry ("multiple EH personalities are supported only with assemblers "
1070 "supporting .cfi_personality directive");
1074 /* Output a marker (i.e. a label) for the end of the generated code
1075 for a function prologue. This gets called *after* the prologue code has
1076 been generated. */
1078 void
1079 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1080 const char *file ATTRIBUTE_UNUSED)
1082 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1084 /* Output a label to mark the endpoint of the code generated for this
1085 function. */
1086 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1087 current_function_funcdef_no);
1088 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1089 current_function_funcdef_no);
1090 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1093 /* Output a marker (i.e. a label) for the beginning of the generated code
1094 for a function epilogue. This gets called *before* the prologue code has
1095 been generated. */
1097 void
1098 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1099 const char *file ATTRIBUTE_UNUSED)
1101 dw_fde_ref fde = cfun->fde;
1102 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1104 if (fde->dw_fde_vms_begin_epilogue)
1105 return;
1107 /* Output a label to mark the endpoint of the code generated for this
1108 function. */
1109 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1110 current_function_funcdef_no);
1111 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1112 current_function_funcdef_no);
1113 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1116 /* Output a marker (i.e. a label) for the absolute end of the generated code
1117 for a function definition. This gets called *after* the epilogue code has
1118 been generated. */
1120 void
1121 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1122 const char *file ATTRIBUTE_UNUSED)
1124 dw_fde_ref fde;
1125 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1127 last_var_location_insn = NULL_RTX;
1128 cached_next_real_insn = NULL_RTX;
1130 if (dwarf2out_do_cfi_asm ())
1131 fprintf (asm_out_file, "\t.cfi_endproc\n");
1133 /* Output a label to mark the endpoint of the code generated for this
1134 function. */
1135 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1136 current_function_funcdef_no);
1137 ASM_OUTPUT_LABEL (asm_out_file, label);
1138 fde = cfun->fde;
1139 gcc_assert (fde != NULL);
1140 if (fde->dw_fde_second_begin == NULL)
1141 fde->dw_fde_end = xstrdup (label);
1144 void
1145 dwarf2out_frame_finish (void)
1147 /* Output call frame information. */
1148 if (targetm.debug_unwind_info () == UI_DWARF2)
1149 output_call_frame_info (0);
1151 /* Output another copy for the unwinder. */
1152 if ((flag_unwind_tables || flag_exceptions)
1153 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1154 output_call_frame_info (1);
1157 /* Note that the current function section is being used for code. */
1159 static void
1160 dwarf2out_note_section_used (void)
1162 section *sec = current_function_section ();
1163 if (sec == text_section)
1164 text_section_used = true;
1165 else if (sec == cold_text_section)
1166 cold_text_section_used = true;
1169 static void var_location_switch_text_section (void);
1170 static void set_cur_line_info_table (section *);
1172 void
1173 dwarf2out_switch_text_section (void)
1175 section *sect;
1176 dw_fde_ref fde = cfun->fde;
1178 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1180 if (!in_cold_section_p)
1182 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1183 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1184 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1186 else
1188 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1189 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1190 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1192 have_multiple_function_sections = true;
1194 /* There is no need to mark used sections when not debugging. */
1195 if (cold_text_section != NULL)
1196 dwarf2out_note_section_used ();
1198 if (dwarf2out_do_cfi_asm ())
1199 fprintf (asm_out_file, "\t.cfi_endproc\n");
1201 /* Now do the real section switch. */
1202 sect = current_function_section ();
1203 switch_to_section (sect);
1205 fde->second_in_std_section
1206 = (sect == text_section
1207 || (cold_text_section && sect == cold_text_section));
1209 if (dwarf2out_do_cfi_asm ())
1210 dwarf2out_do_cfi_startproc (true);
1212 var_location_switch_text_section ();
1214 if (cold_text_section != NULL)
1215 set_cur_line_info_table (sect);
1218 /* And now, the subset of the debugging information support code necessary
1219 for emitting location expressions. */
1221 /* Data about a single source file. */
1222 struct GTY(()) dwarf_file_data {
1223 const char * filename;
1224 int emitted_number;
1227 typedef struct GTY(()) deferred_locations_struct
1229 tree variable;
1230 dw_die_ref die;
1231 } deferred_locations;
1234 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1237 /* Describe an entry into the .debug_addr section. */
1239 enum ate_kind {
1240 ate_kind_rtx,
1241 ate_kind_rtx_dtprel,
1242 ate_kind_label
1245 typedef struct GTY(()) addr_table_entry_struct {
1246 enum ate_kind kind;
1247 unsigned int refcount;
1248 unsigned int index;
1249 union addr_table_entry_struct_union
1251 rtx GTY ((tag ("0"))) rtl;
1252 char * GTY ((tag ("1"))) label;
1254 GTY ((desc ("%1.kind"))) addr;
1256 addr_table_entry;
1258 /* Location lists are ranges + location descriptions for that range,
1259 so you can track variables that are in different places over
1260 their entire life. */
1261 typedef struct GTY(()) dw_loc_list_struct {
1262 dw_loc_list_ref dw_loc_next;
1263 const char *begin; /* Label and addr_entry for start of range */
1264 addr_table_entry *begin_entry;
1265 const char *end; /* Label for end of range */
1266 char *ll_symbol; /* Label for beginning of location list.
1267 Only on head of list */
1268 const char *section; /* Section this loclist is relative to */
1269 dw_loc_descr_ref expr;
1270 hashval_t hash;
1271 /* True if all addresses in this and subsequent lists are known to be
1272 resolved. */
1273 bool resolved_addr;
1274 /* True if this list has been replaced by dw_loc_next. */
1275 bool replaced;
1276 bool emitted;
1277 /* True if the range should be emitted even if begin and end
1278 are the same. */
1279 bool force;
1280 } dw_loc_list_node;
1282 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1284 /* Convert a DWARF stack opcode into its string name. */
1286 static const char *
1287 dwarf_stack_op_name (unsigned int op)
1289 const char *name = get_DW_OP_name (op);
1291 if (name != NULL)
1292 return name;
1294 return "OP_<unknown>";
1297 /* Return a pointer to a newly allocated location description. Location
1298 descriptions are simple expression terms that can be strung
1299 together to form more complicated location (address) descriptions. */
1301 static inline dw_loc_descr_ref
1302 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1303 unsigned HOST_WIDE_INT oprnd2)
1305 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1307 descr->dw_loc_opc = op;
1308 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1309 descr->dw_loc_oprnd1.val_entry = NULL;
1310 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1311 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1312 descr->dw_loc_oprnd2.val_entry = NULL;
1313 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1315 return descr;
1318 /* Return a pointer to a newly allocated location description for
1319 REG and OFFSET. */
1321 static inline dw_loc_descr_ref
1322 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1324 if (reg <= 31)
1325 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1326 offset, 0);
1327 else
1328 return new_loc_descr (DW_OP_bregx, reg, offset);
1331 /* Add a location description term to a location description expression. */
1333 static inline void
1334 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1336 dw_loc_descr_ref *d;
1338 /* Find the end of the chain. */
1339 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1342 *d = descr;
1345 /* Compare two location operands for exact equality. */
1347 static bool
1348 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1350 if (a->val_class != b->val_class)
1351 return false;
1352 switch (a->val_class)
1354 case dw_val_class_none:
1355 return true;
1356 case dw_val_class_addr:
1357 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1359 case dw_val_class_offset:
1360 case dw_val_class_unsigned_const:
1361 case dw_val_class_const:
1362 case dw_val_class_range_list:
1363 case dw_val_class_lineptr:
1364 case dw_val_class_macptr:
1365 /* These are all HOST_WIDE_INT, signed or unsigned. */
1366 return a->v.val_unsigned == b->v.val_unsigned;
1368 case dw_val_class_loc:
1369 return a->v.val_loc == b->v.val_loc;
1370 case dw_val_class_loc_list:
1371 return a->v.val_loc_list == b->v.val_loc_list;
1372 case dw_val_class_die_ref:
1373 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1374 case dw_val_class_fde_ref:
1375 return a->v.val_fde_index == b->v.val_fde_index;
1376 case dw_val_class_lbl_id:
1377 case dw_val_class_high_pc:
1378 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1379 case dw_val_class_str:
1380 return a->v.val_str == b->v.val_str;
1381 case dw_val_class_flag:
1382 return a->v.val_flag == b->v.val_flag;
1383 case dw_val_class_file:
1384 return a->v.val_file == b->v.val_file;
1385 case dw_val_class_decl_ref:
1386 return a->v.val_decl_ref == b->v.val_decl_ref;
1388 case dw_val_class_const_double:
1389 return (a->v.val_double.high == b->v.val_double.high
1390 && a->v.val_double.low == b->v.val_double.low);
1392 case dw_val_class_vec:
1394 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1395 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1397 return (a_len == b_len
1398 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1401 case dw_val_class_data8:
1402 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1404 case dw_val_class_vms_delta:
1405 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1406 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1408 gcc_unreachable ();
1411 /* Compare two location atoms for exact equality. */
1413 static bool
1414 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1416 if (a->dw_loc_opc != b->dw_loc_opc)
1417 return false;
1419 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1420 address size, but since we always allocate cleared storage it
1421 should be zero for other types of locations. */
1422 if (a->dtprel != b->dtprel)
1423 return false;
1425 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1426 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1429 /* Compare two complete location expressions for exact equality. */
1431 bool
1432 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1434 while (1)
1436 if (a == b)
1437 return true;
1438 if (a == NULL || b == NULL)
1439 return false;
1440 if (!loc_descr_equal_p_1 (a, b))
1441 return false;
1443 a = a->dw_loc_next;
1444 b = b->dw_loc_next;
1449 /* Add a constant OFFSET to a location expression. */
1451 static void
1452 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1454 dw_loc_descr_ref loc;
1455 HOST_WIDE_INT *p;
1457 gcc_assert (*list_head != NULL);
1459 if (!offset)
1460 return;
1462 /* Find the end of the chain. */
1463 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1466 p = NULL;
1467 if (loc->dw_loc_opc == DW_OP_fbreg
1468 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1469 p = &loc->dw_loc_oprnd1.v.val_int;
1470 else if (loc->dw_loc_opc == DW_OP_bregx)
1471 p = &loc->dw_loc_oprnd2.v.val_int;
1473 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1474 offset. Don't optimize if an signed integer overflow would happen. */
1475 if (p != NULL
1476 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1477 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1478 *p += offset;
1480 else if (offset > 0)
1481 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1483 else
1485 loc->dw_loc_next = int_loc_descriptor (-offset);
1486 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1490 /* Add a constant OFFSET to a location list. */
1492 static void
1493 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1495 dw_loc_list_ref d;
1496 for (d = list_head; d != NULL; d = d->dw_loc_next)
1497 loc_descr_plus_const (&d->expr, offset);
1500 #define DWARF_REF_SIZE \
1501 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1503 static unsigned long int get_base_type_offset (dw_die_ref);
1505 /* Return the size of a location descriptor. */
1507 static unsigned long
1508 size_of_loc_descr (dw_loc_descr_ref loc)
1510 unsigned long size = 1;
1512 switch (loc->dw_loc_opc)
1514 case DW_OP_addr:
1515 size += DWARF2_ADDR_SIZE;
1516 break;
1517 case DW_OP_GNU_addr_index:
1518 case DW_OP_GNU_const_index:
1519 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1520 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1521 break;
1522 case DW_OP_const1u:
1523 case DW_OP_const1s:
1524 size += 1;
1525 break;
1526 case DW_OP_const2u:
1527 case DW_OP_const2s:
1528 size += 2;
1529 break;
1530 case DW_OP_const4u:
1531 case DW_OP_const4s:
1532 size += 4;
1533 break;
1534 case DW_OP_const8u:
1535 case DW_OP_const8s:
1536 size += 8;
1537 break;
1538 case DW_OP_constu:
1539 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1540 break;
1541 case DW_OP_consts:
1542 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1543 break;
1544 case DW_OP_pick:
1545 size += 1;
1546 break;
1547 case DW_OP_plus_uconst:
1548 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1549 break;
1550 case DW_OP_skip:
1551 case DW_OP_bra:
1552 size += 2;
1553 break;
1554 case DW_OP_breg0:
1555 case DW_OP_breg1:
1556 case DW_OP_breg2:
1557 case DW_OP_breg3:
1558 case DW_OP_breg4:
1559 case DW_OP_breg5:
1560 case DW_OP_breg6:
1561 case DW_OP_breg7:
1562 case DW_OP_breg8:
1563 case DW_OP_breg9:
1564 case DW_OP_breg10:
1565 case DW_OP_breg11:
1566 case DW_OP_breg12:
1567 case DW_OP_breg13:
1568 case DW_OP_breg14:
1569 case DW_OP_breg15:
1570 case DW_OP_breg16:
1571 case DW_OP_breg17:
1572 case DW_OP_breg18:
1573 case DW_OP_breg19:
1574 case DW_OP_breg20:
1575 case DW_OP_breg21:
1576 case DW_OP_breg22:
1577 case DW_OP_breg23:
1578 case DW_OP_breg24:
1579 case DW_OP_breg25:
1580 case DW_OP_breg26:
1581 case DW_OP_breg27:
1582 case DW_OP_breg28:
1583 case DW_OP_breg29:
1584 case DW_OP_breg30:
1585 case DW_OP_breg31:
1586 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1587 break;
1588 case DW_OP_regx:
1589 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1590 break;
1591 case DW_OP_fbreg:
1592 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1593 break;
1594 case DW_OP_bregx:
1595 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1597 break;
1598 case DW_OP_piece:
1599 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1600 break;
1601 case DW_OP_bit_piece:
1602 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1603 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1604 break;
1605 case DW_OP_deref_size:
1606 case DW_OP_xderef_size:
1607 size += 1;
1608 break;
1609 case DW_OP_call2:
1610 size += 2;
1611 break;
1612 case DW_OP_call4:
1613 size += 4;
1614 break;
1615 case DW_OP_call_ref:
1616 size += DWARF_REF_SIZE;
1617 break;
1618 case DW_OP_implicit_value:
1619 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1620 + loc->dw_loc_oprnd1.v.val_unsigned;
1621 break;
1622 case DW_OP_GNU_implicit_pointer:
1623 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1624 break;
1625 case DW_OP_GNU_entry_value:
1627 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1628 size += size_of_uleb128 (op_size) + op_size;
1629 break;
1631 case DW_OP_GNU_const_type:
1633 unsigned long o
1634 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1635 size += size_of_uleb128 (o) + 1;
1636 switch (loc->dw_loc_oprnd2.val_class)
1638 case dw_val_class_vec:
1639 size += loc->dw_loc_oprnd2.v.val_vec.length
1640 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1641 break;
1642 case dw_val_class_const:
1643 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1644 break;
1645 case dw_val_class_const_double:
1646 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1647 break;
1648 default:
1649 gcc_unreachable ();
1651 break;
1653 case DW_OP_GNU_regval_type:
1655 unsigned long o
1656 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1657 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1658 + size_of_uleb128 (o);
1660 break;
1661 case DW_OP_GNU_deref_type:
1663 unsigned long o
1664 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1665 size += 1 + size_of_uleb128 (o);
1667 break;
1668 case DW_OP_GNU_convert:
1669 case DW_OP_GNU_reinterpret:
1670 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1671 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1672 else
1674 unsigned long o
1675 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1676 size += size_of_uleb128 (o);
1678 break;
1679 case DW_OP_GNU_parameter_ref:
1680 size += 4;
1681 break;
1682 default:
1683 break;
1686 return size;
1689 /* Return the size of a series of location descriptors. */
1691 unsigned long
1692 size_of_locs (dw_loc_descr_ref loc)
1694 dw_loc_descr_ref l;
1695 unsigned long size;
1697 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1698 field, to avoid writing to a PCH file. */
1699 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1701 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1702 break;
1703 size += size_of_loc_descr (l);
1705 if (! l)
1706 return size;
1708 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1710 l->dw_loc_addr = size;
1711 size += size_of_loc_descr (l);
1714 return size;
1717 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1718 static void get_ref_die_offset_label (char *, dw_die_ref);
1719 static unsigned long int get_ref_die_offset (dw_die_ref);
1721 /* Output location description stack opcode's operands (if any).
1722 The for_eh_or_skip parameter controls whether register numbers are
1723 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1724 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1725 info). This should be suppressed for the cases that have not been converted
1726 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1728 static void
1729 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1731 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1732 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1734 switch (loc->dw_loc_opc)
1736 #ifdef DWARF2_DEBUGGING_INFO
1737 case DW_OP_const2u:
1738 case DW_OP_const2s:
1739 dw2_asm_output_data (2, val1->v.val_int, NULL);
1740 break;
1741 case DW_OP_const4u:
1742 if (loc->dtprel)
1744 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1745 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1746 val1->v.val_addr);
1747 fputc ('\n', asm_out_file);
1748 break;
1750 /* FALLTHRU */
1751 case DW_OP_const4s:
1752 dw2_asm_output_data (4, val1->v.val_int, NULL);
1753 break;
1754 case DW_OP_const8u:
1755 if (loc->dtprel)
1757 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1758 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1759 val1->v.val_addr);
1760 fputc ('\n', asm_out_file);
1761 break;
1763 /* FALLTHRU */
1764 case DW_OP_const8s:
1765 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1766 dw2_asm_output_data (8, val1->v.val_int, NULL);
1767 break;
1768 case DW_OP_skip:
1769 case DW_OP_bra:
1771 int offset;
1773 gcc_assert (val1->val_class == dw_val_class_loc);
1774 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1776 dw2_asm_output_data (2, offset, NULL);
1778 break;
1779 case DW_OP_implicit_value:
1780 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1781 switch (val2->val_class)
1783 case dw_val_class_const:
1784 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1785 break;
1786 case dw_val_class_vec:
1788 unsigned int elt_size = val2->v.val_vec.elt_size;
1789 unsigned int len = val2->v.val_vec.length;
1790 unsigned int i;
1791 unsigned char *p;
1793 if (elt_size > sizeof (HOST_WIDE_INT))
1795 elt_size /= 2;
1796 len *= 2;
1798 for (i = 0, p = val2->v.val_vec.array;
1799 i < len;
1800 i++, p += elt_size)
1801 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1802 "fp or vector constant word %u", i);
1804 break;
1805 case dw_val_class_const_double:
1807 unsigned HOST_WIDE_INT first, second;
1809 if (WORDS_BIG_ENDIAN)
1811 first = val2->v.val_double.high;
1812 second = val2->v.val_double.low;
1814 else
1816 first = val2->v.val_double.low;
1817 second = val2->v.val_double.high;
1819 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1820 first, NULL);
1821 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1822 second, NULL);
1824 break;
1825 case dw_val_class_addr:
1826 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1827 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1828 break;
1829 default:
1830 gcc_unreachable ();
1832 break;
1833 #else
1834 case DW_OP_const2u:
1835 case DW_OP_const2s:
1836 case DW_OP_const4u:
1837 case DW_OP_const4s:
1838 case DW_OP_const8u:
1839 case DW_OP_const8s:
1840 case DW_OP_skip:
1841 case DW_OP_bra:
1842 case DW_OP_implicit_value:
1843 /* We currently don't make any attempt to make sure these are
1844 aligned properly like we do for the main unwind info, so
1845 don't support emitting things larger than a byte if we're
1846 only doing unwinding. */
1847 gcc_unreachable ();
1848 #endif
1849 case DW_OP_const1u:
1850 case DW_OP_const1s:
1851 dw2_asm_output_data (1, val1->v.val_int, NULL);
1852 break;
1853 case DW_OP_constu:
1854 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1855 break;
1856 case DW_OP_consts:
1857 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1858 break;
1859 case DW_OP_pick:
1860 dw2_asm_output_data (1, val1->v.val_int, NULL);
1861 break;
1862 case DW_OP_plus_uconst:
1863 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1864 break;
1865 case DW_OP_breg0:
1866 case DW_OP_breg1:
1867 case DW_OP_breg2:
1868 case DW_OP_breg3:
1869 case DW_OP_breg4:
1870 case DW_OP_breg5:
1871 case DW_OP_breg6:
1872 case DW_OP_breg7:
1873 case DW_OP_breg8:
1874 case DW_OP_breg9:
1875 case DW_OP_breg10:
1876 case DW_OP_breg11:
1877 case DW_OP_breg12:
1878 case DW_OP_breg13:
1879 case DW_OP_breg14:
1880 case DW_OP_breg15:
1881 case DW_OP_breg16:
1882 case DW_OP_breg17:
1883 case DW_OP_breg18:
1884 case DW_OP_breg19:
1885 case DW_OP_breg20:
1886 case DW_OP_breg21:
1887 case DW_OP_breg22:
1888 case DW_OP_breg23:
1889 case DW_OP_breg24:
1890 case DW_OP_breg25:
1891 case DW_OP_breg26:
1892 case DW_OP_breg27:
1893 case DW_OP_breg28:
1894 case DW_OP_breg29:
1895 case DW_OP_breg30:
1896 case DW_OP_breg31:
1897 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1898 break;
1899 case DW_OP_regx:
1901 unsigned r = val1->v.val_unsigned;
1902 if (for_eh_or_skip >= 0)
1903 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1904 gcc_assert (size_of_uleb128 (r)
1905 == size_of_uleb128 (val1->v.val_unsigned));
1906 dw2_asm_output_data_uleb128 (r, NULL);
1908 break;
1909 case DW_OP_fbreg:
1910 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1911 break;
1912 case DW_OP_bregx:
1914 unsigned r = val1->v.val_unsigned;
1915 if (for_eh_or_skip >= 0)
1916 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1917 gcc_assert (size_of_uleb128 (r)
1918 == size_of_uleb128 (val1->v.val_unsigned));
1919 dw2_asm_output_data_uleb128 (r, NULL);
1920 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1922 break;
1923 case DW_OP_piece:
1924 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1925 break;
1926 case DW_OP_bit_piece:
1927 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1928 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1929 break;
1930 case DW_OP_deref_size:
1931 case DW_OP_xderef_size:
1932 dw2_asm_output_data (1, val1->v.val_int, NULL);
1933 break;
1935 case DW_OP_addr:
1936 if (loc->dtprel)
1938 if (targetm.asm_out.output_dwarf_dtprel)
1940 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1941 DWARF2_ADDR_SIZE,
1942 val1->v.val_addr);
1943 fputc ('\n', asm_out_file);
1945 else
1946 gcc_unreachable ();
1948 else
1950 #ifdef DWARF2_DEBUGGING_INFO
1951 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1952 #else
1953 gcc_unreachable ();
1954 #endif
1956 break;
1958 case DW_OP_GNU_addr_index:
1959 case DW_OP_GNU_const_index:
1960 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1961 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1962 "(index into .debug_addr)");
1963 break;
1965 case DW_OP_GNU_implicit_pointer:
1967 char label[MAX_ARTIFICIAL_LABEL_BYTES
1968 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1969 gcc_assert (val1->val_class == dw_val_class_die_ref);
1970 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1971 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1972 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1974 break;
1976 case DW_OP_GNU_entry_value:
1977 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1978 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1979 break;
1981 case DW_OP_GNU_const_type:
1983 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1984 gcc_assert (o);
1985 dw2_asm_output_data_uleb128 (o, NULL);
1986 switch (val2->val_class)
1988 case dw_val_class_const:
1989 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1990 dw2_asm_output_data (1, l, NULL);
1991 dw2_asm_output_data (l, val2->v.val_int, NULL);
1992 break;
1993 case dw_val_class_vec:
1995 unsigned int elt_size = val2->v.val_vec.elt_size;
1996 unsigned int len = val2->v.val_vec.length;
1997 unsigned int i;
1998 unsigned char *p;
2000 l = len * elt_size;
2001 dw2_asm_output_data (1, l, NULL);
2002 if (elt_size > sizeof (HOST_WIDE_INT))
2004 elt_size /= 2;
2005 len *= 2;
2007 for (i = 0, p = val2->v.val_vec.array;
2008 i < len;
2009 i++, p += elt_size)
2010 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2011 "fp or vector constant word %u", i);
2013 break;
2014 case dw_val_class_const_double:
2016 unsigned HOST_WIDE_INT first, second;
2017 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2019 dw2_asm_output_data (1, 2 * l, NULL);
2020 if (WORDS_BIG_ENDIAN)
2022 first = val2->v.val_double.high;
2023 second = val2->v.val_double.low;
2025 else
2027 first = val2->v.val_double.low;
2028 second = val2->v.val_double.high;
2030 dw2_asm_output_data (l, first, NULL);
2031 dw2_asm_output_data (l, second, NULL);
2033 break;
2034 default:
2035 gcc_unreachable ();
2038 break;
2039 case DW_OP_GNU_regval_type:
2041 unsigned r = val1->v.val_unsigned;
2042 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2043 gcc_assert (o);
2044 if (for_eh_or_skip >= 0)
2046 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2047 gcc_assert (size_of_uleb128 (r)
2048 == size_of_uleb128 (val1->v.val_unsigned));
2050 dw2_asm_output_data_uleb128 (r, NULL);
2051 dw2_asm_output_data_uleb128 (o, NULL);
2053 break;
2054 case DW_OP_GNU_deref_type:
2056 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2057 gcc_assert (o);
2058 dw2_asm_output_data (1, val1->v.val_int, NULL);
2059 dw2_asm_output_data_uleb128 (o, NULL);
2061 break;
2062 case DW_OP_GNU_convert:
2063 case DW_OP_GNU_reinterpret:
2064 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2065 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2066 else
2068 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2069 gcc_assert (o);
2070 dw2_asm_output_data_uleb128 (o, NULL);
2072 break;
2074 case DW_OP_GNU_parameter_ref:
2076 unsigned long o;
2077 gcc_assert (val1->val_class == dw_val_class_die_ref);
2078 o = get_ref_die_offset (val1->v.val_die_ref.die);
2079 dw2_asm_output_data (4, o, NULL);
2081 break;
2083 default:
2084 /* Other codes have no operands. */
2085 break;
2089 /* Output a sequence of location operations.
2090 The for_eh_or_skip parameter controls whether register numbers are
2091 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2092 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2093 info). This should be suppressed for the cases that have not been converted
2094 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2096 void
2097 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2099 for (; loc != NULL; loc = loc->dw_loc_next)
2101 enum dwarf_location_atom opc = loc->dw_loc_opc;
2102 /* Output the opcode. */
2103 if (for_eh_or_skip >= 0
2104 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2106 unsigned r = (opc - DW_OP_breg0);
2107 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2108 gcc_assert (r <= 31);
2109 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2111 else if (for_eh_or_skip >= 0
2112 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2114 unsigned r = (opc - DW_OP_reg0);
2115 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2116 gcc_assert (r <= 31);
2117 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2120 dw2_asm_output_data (1, opc,
2121 "%s", dwarf_stack_op_name (opc));
2123 /* Output the operand(s) (if any). */
2124 output_loc_operands (loc, for_eh_or_skip);
2128 /* Output location description stack opcode's operands (if any).
2129 The output is single bytes on a line, suitable for .cfi_escape. */
2131 static void
2132 output_loc_operands_raw (dw_loc_descr_ref loc)
2134 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2135 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2137 switch (loc->dw_loc_opc)
2139 case DW_OP_addr:
2140 case DW_OP_GNU_addr_index:
2141 case DW_OP_GNU_const_index:
2142 case DW_OP_implicit_value:
2143 /* We cannot output addresses in .cfi_escape, only bytes. */
2144 gcc_unreachable ();
2146 case DW_OP_const1u:
2147 case DW_OP_const1s:
2148 case DW_OP_pick:
2149 case DW_OP_deref_size:
2150 case DW_OP_xderef_size:
2151 fputc (',', asm_out_file);
2152 dw2_asm_output_data_raw (1, val1->v.val_int);
2153 break;
2155 case DW_OP_const2u:
2156 case DW_OP_const2s:
2157 fputc (',', asm_out_file);
2158 dw2_asm_output_data_raw (2, val1->v.val_int);
2159 break;
2161 case DW_OP_const4u:
2162 case DW_OP_const4s:
2163 fputc (',', asm_out_file);
2164 dw2_asm_output_data_raw (4, val1->v.val_int);
2165 break;
2167 case DW_OP_const8u:
2168 case DW_OP_const8s:
2169 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_raw (8, val1->v.val_int);
2172 break;
2174 case DW_OP_skip:
2175 case DW_OP_bra:
2177 int offset;
2179 gcc_assert (val1->val_class == dw_val_class_loc);
2180 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2182 fputc (',', asm_out_file);
2183 dw2_asm_output_data_raw (2, offset);
2185 break;
2187 case DW_OP_regx:
2189 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2190 gcc_assert (size_of_uleb128 (r)
2191 == size_of_uleb128 (val1->v.val_unsigned));
2192 fputc (',', asm_out_file);
2193 dw2_asm_output_data_uleb128_raw (r);
2195 break;
2197 case DW_OP_constu:
2198 case DW_OP_plus_uconst:
2199 case DW_OP_piece:
2200 fputc (',', asm_out_file);
2201 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2202 break;
2204 case DW_OP_bit_piece:
2205 fputc (',', asm_out_file);
2206 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2207 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2208 break;
2210 case DW_OP_consts:
2211 case DW_OP_breg0:
2212 case DW_OP_breg1:
2213 case DW_OP_breg2:
2214 case DW_OP_breg3:
2215 case DW_OP_breg4:
2216 case DW_OP_breg5:
2217 case DW_OP_breg6:
2218 case DW_OP_breg7:
2219 case DW_OP_breg8:
2220 case DW_OP_breg9:
2221 case DW_OP_breg10:
2222 case DW_OP_breg11:
2223 case DW_OP_breg12:
2224 case DW_OP_breg13:
2225 case DW_OP_breg14:
2226 case DW_OP_breg15:
2227 case DW_OP_breg16:
2228 case DW_OP_breg17:
2229 case DW_OP_breg18:
2230 case DW_OP_breg19:
2231 case DW_OP_breg20:
2232 case DW_OP_breg21:
2233 case DW_OP_breg22:
2234 case DW_OP_breg23:
2235 case DW_OP_breg24:
2236 case DW_OP_breg25:
2237 case DW_OP_breg26:
2238 case DW_OP_breg27:
2239 case DW_OP_breg28:
2240 case DW_OP_breg29:
2241 case DW_OP_breg30:
2242 case DW_OP_breg31:
2243 case DW_OP_fbreg:
2244 fputc (',', asm_out_file);
2245 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2246 break;
2248 case DW_OP_bregx:
2250 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2251 gcc_assert (size_of_uleb128 (r)
2252 == size_of_uleb128 (val1->v.val_unsigned));
2253 fputc (',', asm_out_file);
2254 dw2_asm_output_data_uleb128_raw (r);
2255 fputc (',', asm_out_file);
2256 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2258 break;
2260 case DW_OP_GNU_implicit_pointer:
2261 case DW_OP_GNU_entry_value:
2262 case DW_OP_GNU_const_type:
2263 case DW_OP_GNU_regval_type:
2264 case DW_OP_GNU_deref_type:
2265 case DW_OP_GNU_convert:
2266 case DW_OP_GNU_reinterpret:
2267 case DW_OP_GNU_parameter_ref:
2268 gcc_unreachable ();
2269 break;
2271 default:
2272 /* Other codes have no operands. */
2273 break;
2277 void
2278 output_loc_sequence_raw (dw_loc_descr_ref loc)
2280 while (1)
2282 enum dwarf_location_atom opc = loc->dw_loc_opc;
2283 /* Output the opcode. */
2284 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2286 unsigned r = (opc - DW_OP_breg0);
2287 r = DWARF2_FRAME_REG_OUT (r, 1);
2288 gcc_assert (r <= 31);
2289 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2291 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2293 unsigned r = (opc - DW_OP_reg0);
2294 r = DWARF2_FRAME_REG_OUT (r, 1);
2295 gcc_assert (r <= 31);
2296 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2298 /* Output the opcode. */
2299 fprintf (asm_out_file, "%#x", opc);
2300 output_loc_operands_raw (loc);
2302 if (!loc->dw_loc_next)
2303 break;
2304 loc = loc->dw_loc_next;
2306 fputc (',', asm_out_file);
2310 /* This function builds a dwarf location descriptor sequence from a
2311 dw_cfa_location, adding the given OFFSET to the result of the
2312 expression. */
2314 struct dw_loc_descr_node *
2315 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2317 struct dw_loc_descr_node *head, *tmp;
2319 offset += cfa->offset;
2321 if (cfa->indirect)
2323 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2324 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2325 head->dw_loc_oprnd1.val_entry = NULL;
2326 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2327 add_loc_descr (&head, tmp);
2328 if (offset != 0)
2330 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2331 add_loc_descr (&head, tmp);
2334 else
2335 head = new_reg_loc_descr (cfa->reg, offset);
2337 return head;
2340 /* This function builds a dwarf location descriptor sequence for
2341 the address at OFFSET from the CFA when stack is aligned to
2342 ALIGNMENT byte. */
2344 struct dw_loc_descr_node *
2345 build_cfa_aligned_loc (dw_cfa_location *cfa,
2346 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2348 struct dw_loc_descr_node *head;
2349 unsigned int dwarf_fp
2350 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2352 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2353 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2355 head = new_reg_loc_descr (dwarf_fp, 0);
2356 add_loc_descr (&head, int_loc_descriptor (alignment));
2357 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2358 loc_descr_plus_const (&head, offset);
2360 else
2361 head = new_reg_loc_descr (dwarf_fp, offset);
2362 return head;
2365 /* And now, the support for symbolic debugging information. */
2367 /* .debug_str support. */
2368 static int output_indirect_string (void **, void *);
2370 static void dwarf2out_init (const char *);
2371 static void dwarf2out_finish (const char *);
2372 static void dwarf2out_assembly_start (void);
2373 static void dwarf2out_define (unsigned int, const char *);
2374 static void dwarf2out_undef (unsigned int, const char *);
2375 static void dwarf2out_start_source_file (unsigned, const char *);
2376 static void dwarf2out_end_source_file (unsigned);
2377 static void dwarf2out_function_decl (tree);
2378 static void dwarf2out_begin_block (unsigned, unsigned);
2379 static void dwarf2out_end_block (unsigned, unsigned);
2380 static bool dwarf2out_ignore_block (const_tree);
2381 static void dwarf2out_global_decl (tree);
2382 static void dwarf2out_type_decl (tree, int);
2383 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2384 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2385 dw_die_ref);
2386 static void dwarf2out_abstract_function (tree);
2387 static void dwarf2out_var_location (rtx);
2388 static void dwarf2out_begin_function (tree);
2389 static void dwarf2out_end_function (unsigned int);
2390 static void dwarf2out_set_name (tree, tree);
2392 /* The debug hooks structure. */
2394 const struct gcc_debug_hooks dwarf2_debug_hooks =
2396 dwarf2out_init,
2397 dwarf2out_finish,
2398 dwarf2out_assembly_start,
2399 dwarf2out_define,
2400 dwarf2out_undef,
2401 dwarf2out_start_source_file,
2402 dwarf2out_end_source_file,
2403 dwarf2out_begin_block,
2404 dwarf2out_end_block,
2405 dwarf2out_ignore_block,
2406 dwarf2out_source_line,
2407 dwarf2out_begin_prologue,
2408 #if VMS_DEBUGGING_INFO
2409 dwarf2out_vms_end_prologue,
2410 dwarf2out_vms_begin_epilogue,
2411 #else
2412 debug_nothing_int_charstar,
2413 debug_nothing_int_charstar,
2414 #endif
2415 dwarf2out_end_epilogue,
2416 dwarf2out_begin_function,
2417 dwarf2out_end_function, /* end_function */
2418 dwarf2out_function_decl, /* function_decl */
2419 dwarf2out_global_decl,
2420 dwarf2out_type_decl, /* type_decl */
2421 dwarf2out_imported_module_or_decl,
2422 debug_nothing_tree, /* deferred_inline_function */
2423 /* The DWARF 2 backend tries to reduce debugging bloat by not
2424 emitting the abstract description of inline functions until
2425 something tries to reference them. */
2426 dwarf2out_abstract_function, /* outlining_inline_function */
2427 debug_nothing_rtx, /* label */
2428 debug_nothing_int, /* handle_pch */
2429 dwarf2out_var_location,
2430 dwarf2out_switch_text_section,
2431 dwarf2out_set_name,
2432 1, /* start_end_main_source_file */
2433 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2436 /* NOTE: In the comments in this file, many references are made to
2437 "Debugging Information Entries". This term is abbreviated as `DIE'
2438 throughout the remainder of this file. */
2440 /* An internal representation of the DWARF output is built, and then
2441 walked to generate the DWARF debugging info. The walk of the internal
2442 representation is done after the entire program has been compiled.
2443 The types below are used to describe the internal representation. */
2445 /* Whether to put type DIEs into their own section .debug_types instead
2446 of making them part of the .debug_info section. Only supported for
2447 Dwarf V4 or higher and the user didn't disable them through
2448 -fno-debug-types-section. It is more efficient to put them in a
2449 separate comdat sections since the linker will then be able to
2450 remove duplicates. But not all tools support .debug_types sections
2451 yet. */
2453 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2455 /* Various DIE's use offsets relative to the beginning of the
2456 .debug_info section to refer to each other. */
2458 typedef long int dw_offset;
2460 /* Define typedefs here to avoid circular dependencies. */
2462 typedef struct dw_attr_struct *dw_attr_ref;
2463 typedef struct dw_line_info_struct *dw_line_info_ref;
2464 typedef struct pubname_struct *pubname_ref;
2465 typedef struct dw_ranges_struct *dw_ranges_ref;
2466 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2467 typedef struct comdat_type_struct *comdat_type_node_ref;
2469 /* The entries in the line_info table more-or-less mirror the opcodes
2470 that are used in the real dwarf line table. Arrays of these entries
2471 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2472 supported. */
2474 enum dw_line_info_opcode {
2475 /* Emit DW_LNE_set_address; the operand is the label index. */
2476 LI_set_address,
2478 /* Emit a row to the matrix with the given line. This may be done
2479 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2480 special opcodes. */
2481 LI_set_line,
2483 /* Emit a DW_LNS_set_file. */
2484 LI_set_file,
2486 /* Emit a DW_LNS_set_column. */
2487 LI_set_column,
2489 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2490 LI_negate_stmt,
2492 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2493 LI_set_prologue_end,
2494 LI_set_epilogue_begin,
2496 /* Emit a DW_LNE_set_discriminator. */
2497 LI_set_discriminator
2500 typedef struct GTY(()) dw_line_info_struct {
2501 enum dw_line_info_opcode opcode;
2502 unsigned int val;
2503 } dw_line_info_entry;
2506 typedef struct GTY(()) dw_line_info_table_struct {
2507 /* The label that marks the end of this section. */
2508 const char *end_label;
2510 /* The values for the last row of the matrix, as collected in the table.
2511 These are used to minimize the changes to the next row. */
2512 unsigned int file_num;
2513 unsigned int line_num;
2514 unsigned int column_num;
2515 int discrim_num;
2516 bool is_stmt;
2517 bool in_use;
2519 vec<dw_line_info_entry, va_gc> *entries;
2520 } dw_line_info_table;
2522 typedef dw_line_info_table *dw_line_info_table_p;
2525 /* Each DIE attribute has a field specifying the attribute kind,
2526 a link to the next attribute in the chain, and an attribute value.
2527 Attributes are typically linked below the DIE they modify. */
2529 typedef struct GTY(()) dw_attr_struct {
2530 enum dwarf_attribute dw_attr;
2531 dw_val_node dw_attr_val;
2533 dw_attr_node;
2536 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2537 The children of each node form a circular list linked by
2538 die_sib. die_child points to the node *before* the "first" child node. */
2540 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2541 union die_symbol_or_type_node
2543 const char * GTY ((tag ("0"))) die_symbol;
2544 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2546 GTY ((desc ("%0.comdat_type_p"))) die_id;
2547 vec<dw_attr_node, va_gc> *die_attr;
2548 dw_die_ref die_parent;
2549 dw_die_ref die_child;
2550 dw_die_ref die_sib;
2551 dw_die_ref die_definition; /* ref from a specification to its definition */
2552 dw_offset die_offset;
2553 unsigned long die_abbrev;
2554 int die_mark;
2555 unsigned int decl_id;
2556 enum dwarf_tag die_tag;
2557 /* Die is used and must not be pruned as unused. */
2558 BOOL_BITFIELD die_perennial_p : 1;
2559 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2560 /* Lots of spare bits. */
2562 die_node;
2564 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2565 #define FOR_EACH_CHILD(die, c, expr) do { \
2566 c = die->die_child; \
2567 if (c) do { \
2568 c = c->die_sib; \
2569 expr; \
2570 } while (c != die->die_child); \
2571 } while (0)
2573 /* The pubname structure */
2575 typedef struct GTY(()) pubname_struct {
2576 dw_die_ref die;
2577 const char *name;
2579 pubname_entry;
2582 struct GTY(()) dw_ranges_struct {
2583 /* If this is positive, it's a block number, otherwise it's a
2584 bitwise-negated index into dw_ranges_by_label. */
2585 int num;
2588 /* A structure to hold a macinfo entry. */
2590 typedef struct GTY(()) macinfo_struct {
2591 unsigned char code;
2592 unsigned HOST_WIDE_INT lineno;
2593 const char *info;
2595 macinfo_entry;
2598 struct GTY(()) dw_ranges_by_label_struct {
2599 const char *begin;
2600 const char *end;
2603 /* The comdat type node structure. */
2604 typedef struct GTY(()) comdat_type_struct
2606 dw_die_ref root_die;
2607 dw_die_ref type_die;
2608 dw_die_ref skeleton_die;
2609 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2610 struct comdat_type_struct *next;
2612 comdat_type_node;
2614 /* The limbo die list structure. */
2615 typedef struct GTY(()) limbo_die_struct {
2616 dw_die_ref die;
2617 tree created_for;
2618 struct limbo_die_struct *next;
2620 limbo_die_node;
2622 typedef struct skeleton_chain_struct
2624 dw_die_ref old_die;
2625 dw_die_ref new_die;
2626 struct skeleton_chain_struct *parent;
2628 skeleton_chain_node;
2630 /* Define a macro which returns nonzero for a TYPE_DECL which was
2631 implicitly generated for a type.
2633 Note that, unlike the C front-end (which generates a NULL named
2634 TYPE_DECL node for each complete tagged type, each array type,
2635 and each function type node created) the C++ front-end generates
2636 a _named_ TYPE_DECL node for each tagged type node created.
2637 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2638 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2639 front-end, but for each type, tagged or not. */
2641 #define TYPE_DECL_IS_STUB(decl) \
2642 (DECL_NAME (decl) == NULL_TREE \
2643 || (DECL_ARTIFICIAL (decl) \
2644 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2645 /* This is necessary for stub decls that \
2646 appear in nested inline functions. */ \
2647 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2648 && (decl_ultimate_origin (decl) \
2649 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2651 /* Information concerning the compilation unit's programming
2652 language, and compiler version. */
2654 /* Fixed size portion of the DWARF compilation unit header. */
2655 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2656 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2658 /* Fixed size portion of the DWARF comdat type unit header. */
2659 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2660 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2661 + DWARF_OFFSET_SIZE)
2663 /* Fixed size portion of public names info. */
2664 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2666 /* Fixed size portion of the address range info. */
2667 #define DWARF_ARANGES_HEADER_SIZE \
2668 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2669 DWARF2_ADDR_SIZE * 2) \
2670 - DWARF_INITIAL_LENGTH_SIZE)
2672 /* Size of padding portion in the address range info. It must be
2673 aligned to twice the pointer size. */
2674 #define DWARF_ARANGES_PAD_SIZE \
2675 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2676 DWARF2_ADDR_SIZE * 2) \
2677 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2679 /* Use assembler line directives if available. */
2680 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2681 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2682 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2683 #else
2684 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2685 #endif
2686 #endif
2688 /* Minimum line offset in a special line info. opcode.
2689 This value was chosen to give a reasonable range of values. */
2690 #define DWARF_LINE_BASE -10
2692 /* First special line opcode - leave room for the standard opcodes. */
2693 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2695 /* Range of line offsets in a special line info. opcode. */
2696 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2698 /* Flag that indicates the initial value of the is_stmt_start flag.
2699 In the present implementation, we do not mark any lines as
2700 the beginning of a source statement, because that information
2701 is not made available by the GCC front-end. */
2702 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2704 /* Maximum number of operations per instruction bundle. */
2705 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2706 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2707 #endif
2709 /* This location is used by calc_die_sizes() to keep track
2710 the offset of each DIE within the .debug_info section. */
2711 static unsigned long next_die_offset;
2713 /* Record the root of the DIE's built for the current compilation unit. */
2714 static GTY(()) dw_die_ref single_comp_unit_die;
2716 /* A list of type DIEs that have been separated into comdat sections. */
2717 static GTY(()) comdat_type_node *comdat_type_list;
2719 /* A list of DIEs with a NULL parent waiting to be relocated. */
2720 static GTY(()) limbo_die_node *limbo_die_list;
2722 /* A list of DIEs for which we may have to generate
2723 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2724 static GTY(()) limbo_die_node *deferred_asm_name;
2726 /* Filenames referenced by this compilation unit. */
2727 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2729 /* A hash table of references to DIE's that describe declarations.
2730 The key is a DECL_UID() which is a unique number identifying each decl. */
2731 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2733 /* A hash table of references to DIE's that describe COMMON blocks.
2734 The key is DECL_UID() ^ die_parent. */
2735 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2737 typedef struct GTY(()) die_arg_entry_struct {
2738 dw_die_ref die;
2739 tree arg;
2740 } die_arg_entry;
2743 /* Node of the variable location list. */
2744 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2745 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2746 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2747 in mode of the EXPR_LIST node and first EXPR_LIST operand
2748 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2749 location or NULL for padding. For larger bitsizes,
2750 mode is 0 and first operand is a CONCAT with bitsize
2751 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2752 NULL as second operand. */
2753 rtx GTY (()) loc;
2754 const char * GTY (()) label;
2755 struct var_loc_node * GTY (()) next;
2758 /* Variable location list. */
2759 struct GTY (()) var_loc_list_def {
2760 struct var_loc_node * GTY (()) first;
2762 /* Pointer to the last but one or last element of the
2763 chained list. If the list is empty, both first and
2764 last are NULL, if the list contains just one node
2765 or the last node certainly is not redundant, it points
2766 to the last node, otherwise points to the last but one.
2767 Do not mark it for GC because it is marked through the chain. */
2768 struct var_loc_node * GTY ((skip ("%h"))) last;
2770 /* Pointer to the last element before section switch,
2771 if NULL, either sections weren't switched or first
2772 is after section switch. */
2773 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2775 /* DECL_UID of the variable decl. */
2776 unsigned int decl_id;
2778 typedef struct var_loc_list_def var_loc_list;
2780 /* Call argument location list. */
2781 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2782 rtx GTY (()) call_arg_loc_note;
2783 const char * GTY (()) label;
2784 tree GTY (()) block;
2785 bool tail_call_p;
2786 rtx GTY (()) symbol_ref;
2787 struct call_arg_loc_node * GTY (()) next;
2791 /* Table of decl location linked lists. */
2792 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2794 /* Head and tail of call_arg_loc chain. */
2795 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2796 static struct call_arg_loc_node *call_arg_loc_last;
2798 /* Number of call sites in the current function. */
2799 static int call_site_count = -1;
2800 /* Number of tail call sites in the current function. */
2801 static int tail_call_site_count = -1;
2803 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2804 DIEs. */
2805 static vec<dw_die_ref> block_map;
2807 /* A cached location list. */
2808 struct GTY (()) cached_dw_loc_list_def {
2809 /* The DECL_UID of the decl that this entry describes. */
2810 unsigned int decl_id;
2812 /* The cached location list. */
2813 dw_loc_list_ref loc_list;
2815 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2817 /* Table of cached location lists. */
2818 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2820 /* A pointer to the base of a list of references to DIE's that
2821 are uniquely identified by their tag, presence/absence of
2822 children DIE's, and list of attribute/value pairs. */
2823 static GTY((length ("abbrev_die_table_allocated")))
2824 dw_die_ref *abbrev_die_table;
2826 /* Number of elements currently allocated for abbrev_die_table. */
2827 static GTY(()) unsigned abbrev_die_table_allocated;
2829 /* Number of elements in type_die_table currently in use. */
2830 static GTY(()) unsigned abbrev_die_table_in_use;
2832 /* Size (in elements) of increments by which we may expand the
2833 abbrev_die_table. */
2834 #define ABBREV_DIE_TABLE_INCREMENT 256
2836 /* A global counter for generating labels for line number data. */
2837 static unsigned int line_info_label_num;
2839 /* The current table to which we should emit line number information
2840 for the current function. This will be set up at the beginning of
2841 assembly for the function. */
2842 static dw_line_info_table *cur_line_info_table;
2844 /* The two default tables of line number info. */
2845 static GTY(()) dw_line_info_table *text_section_line_info;
2846 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2848 /* The set of all non-default tables of line number info. */
2849 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2851 /* A flag to tell pubnames/types export if there is an info section to
2852 refer to. */
2853 static bool info_section_emitted;
2855 /* A pointer to the base of a table that contains a list of publicly
2856 accessible names. */
2857 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2859 /* A pointer to the base of a table that contains a list of publicly
2860 accessible types. */
2861 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2863 /* A pointer to the base of a table that contains a list of macro
2864 defines/undefines (and file start/end markers). */
2865 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2867 /* True if .debug_macinfo or .debug_macros section is going to be
2868 emitted. */
2869 #define have_macinfo \
2870 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2871 && !macinfo_table->is_empty ())
2873 /* Array of dies for which we should generate .debug_ranges info. */
2874 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2876 /* Number of elements currently allocated for ranges_table. */
2877 static GTY(()) unsigned ranges_table_allocated;
2879 /* Number of elements in ranges_table currently in use. */
2880 static GTY(()) unsigned ranges_table_in_use;
2882 /* Array of pairs of labels referenced in ranges_table. */
2883 static GTY ((length ("ranges_by_label_allocated")))
2884 dw_ranges_by_label_ref ranges_by_label;
2886 /* Number of elements currently allocated for ranges_by_label. */
2887 static GTY(()) unsigned ranges_by_label_allocated;
2889 /* Number of elements in ranges_by_label currently in use. */
2890 static GTY(()) unsigned ranges_by_label_in_use;
2892 /* Size (in elements) of increments by which we may expand the
2893 ranges_table. */
2894 #define RANGES_TABLE_INCREMENT 64
2896 /* Whether we have location lists that need outputting */
2897 static GTY(()) bool have_location_lists;
2899 /* Unique label counter. */
2900 static GTY(()) unsigned int loclabel_num;
2902 /* Unique label counter for point-of-call tables. */
2903 static GTY(()) unsigned int poc_label_num;
2905 /* Record whether the function being analyzed contains inlined functions. */
2906 static int current_function_has_inlines;
2908 /* The last file entry emitted by maybe_emit_file(). */
2909 static GTY(()) struct dwarf_file_data * last_emitted_file;
2911 /* Number of internal labels generated by gen_internal_sym(). */
2912 static GTY(()) int label_num;
2914 /* Cached result of previous call to lookup_filename. */
2915 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2917 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2919 /* Instances of generic types for which we need to generate debug
2920 info that describe their generic parameters and arguments. That
2921 generation needs to happen once all types are properly laid out so
2922 we do it at the end of compilation. */
2923 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2925 /* Offset from the "steady-state frame pointer" to the frame base,
2926 within the current function. */
2927 static HOST_WIDE_INT frame_pointer_fb_offset;
2928 static bool frame_pointer_fb_offset_valid;
2930 static vec<dw_die_ref> base_types;
2932 /* Forward declarations for functions defined in this file. */
2934 static int is_pseudo_reg (const_rtx);
2935 static int is_tagged_type (const_tree);
2936 static const char *dwarf_tag_name (unsigned);
2937 static const char *dwarf_attr_name (unsigned);
2938 static const char *dwarf_form_name (unsigned);
2939 static tree decl_ultimate_origin (const_tree);
2940 static tree decl_class_context (tree);
2941 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2942 static inline enum dw_val_class AT_class (dw_attr_ref);
2943 static inline unsigned int AT_index (dw_attr_ref);
2944 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2945 static inline unsigned AT_flag (dw_attr_ref);
2946 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2947 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2948 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2949 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2950 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2951 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2952 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2953 unsigned int, unsigned char *);
2954 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2955 static hashval_t debug_str_do_hash (const void *);
2956 static int debug_str_eq (const void *, const void *);
2957 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2958 static inline const char *AT_string (dw_attr_ref);
2959 static enum dwarf_form AT_string_form (dw_attr_ref);
2960 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2961 static void add_AT_specification (dw_die_ref, dw_die_ref);
2962 static inline dw_die_ref AT_ref (dw_attr_ref);
2963 static inline int AT_ref_external (dw_attr_ref);
2964 static inline void set_AT_ref_external (dw_attr_ref, int);
2965 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2966 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2967 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2968 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2969 dw_loc_list_ref);
2970 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2971 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2972 static void remove_addr_table_entry (addr_table_entry *);
2973 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2974 static inline rtx AT_addr (dw_attr_ref);
2975 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2976 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2977 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2978 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2979 unsigned HOST_WIDE_INT);
2980 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2981 unsigned long, bool);
2982 static inline const char *AT_lbl (dw_attr_ref);
2983 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2984 static const char *get_AT_low_pc (dw_die_ref);
2985 static const char *get_AT_hi_pc (dw_die_ref);
2986 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2987 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2988 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2989 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2990 static bool is_cxx (void);
2991 static bool is_fortran (void);
2992 static bool is_ada (void);
2993 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2994 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2995 static void add_child_die (dw_die_ref, dw_die_ref);
2996 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2997 static dw_die_ref lookup_type_die (tree);
2998 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2999 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3000 static void equate_type_number_to_die (tree, dw_die_ref);
3001 static hashval_t decl_die_table_hash (const void *);
3002 static int decl_die_table_eq (const void *, const void *);
3003 static dw_die_ref lookup_decl_die (tree);
3004 static hashval_t common_block_die_table_hash (const void *);
3005 static int common_block_die_table_eq (const void *, const void *);
3006 static hashval_t decl_loc_table_hash (const void *);
3007 static int decl_loc_table_eq (const void *, const void *);
3008 static var_loc_list *lookup_decl_loc (const_tree);
3009 static void equate_decl_number_to_die (tree, dw_die_ref);
3010 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3011 static void print_spaces (FILE *);
3012 static void print_die (dw_die_ref, FILE *);
3013 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3014 static dw_die_ref pop_compile_unit (dw_die_ref);
3015 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3016 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3017 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3018 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3019 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3020 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3021 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3022 struct md5_ctx *, int *);
3023 struct checksum_attributes;
3024 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3025 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3026 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3027 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3028 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3029 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3030 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3031 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3032 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3033 static void compute_section_prefix (dw_die_ref);
3034 static int is_type_die (dw_die_ref);
3035 static int is_comdat_die (dw_die_ref);
3036 static int is_symbol_die (dw_die_ref);
3037 static inline bool is_template_instantiation (dw_die_ref);
3038 static void assign_symbol_names (dw_die_ref);
3039 static void break_out_includes (dw_die_ref);
3040 static int is_declaration_die (dw_die_ref);
3041 static int should_move_die_to_comdat (dw_die_ref);
3042 static dw_die_ref clone_as_declaration (dw_die_ref);
3043 static dw_die_ref clone_die (dw_die_ref);
3044 static dw_die_ref clone_tree (dw_die_ref);
3045 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3046 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3047 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3048 static dw_die_ref generate_skeleton (dw_die_ref);
3049 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3050 dw_die_ref,
3051 dw_die_ref);
3052 static void break_out_comdat_types (dw_die_ref);
3053 static void copy_decls_for_unworthy_types (dw_die_ref);
3055 static void add_sibling_attributes (dw_die_ref);
3056 static void output_location_lists (dw_die_ref);
3057 static int constant_size (unsigned HOST_WIDE_INT);
3058 static unsigned long size_of_die (dw_die_ref);
3059 static void calc_die_sizes (dw_die_ref);
3060 static void calc_base_type_die_sizes (void);
3061 static void mark_dies (dw_die_ref);
3062 static void unmark_dies (dw_die_ref);
3063 static void unmark_all_dies (dw_die_ref);
3064 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3065 static unsigned long size_of_aranges (void);
3066 static enum dwarf_form value_format (dw_attr_ref);
3067 static void output_value_format (dw_attr_ref);
3068 static void output_abbrev_section (void);
3069 static void output_die_abbrevs (unsigned long, dw_die_ref);
3070 static void output_die_symbol (dw_die_ref);
3071 static void output_die (dw_die_ref);
3072 static void output_compilation_unit_header (void);
3073 static void output_comp_unit (dw_die_ref, int);
3074 static void output_comdat_type_unit (comdat_type_node *);
3075 static const char *dwarf2_name (tree, int);
3076 static void add_pubname (tree, dw_die_ref);
3077 static void add_enumerator_pubname (const char *, dw_die_ref);
3078 static void add_pubname_string (const char *, dw_die_ref);
3079 static void add_pubtype (tree, dw_die_ref);
3080 static void output_pubnames (vec<pubname_entry, va_gc> *);
3081 static void output_aranges (unsigned long);
3082 static unsigned int add_ranges_num (int);
3083 static unsigned int add_ranges (const_tree);
3084 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3085 bool *, bool);
3086 static void output_ranges (void);
3087 static dw_line_info_table *new_line_info_table (void);
3088 static void output_line_info (bool);
3089 static void output_file_names (void);
3090 static dw_die_ref base_type_die (tree);
3091 static int is_base_type (tree);
3092 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3093 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3094 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3095 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3096 static int type_is_enum (const_tree);
3097 static unsigned int dbx_reg_number (const_rtx);
3098 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3099 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3100 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3101 enum var_init_status);
3102 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3103 enum var_init_status);
3104 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3105 enum var_init_status);
3106 static int is_based_loc (const_rtx);
3107 static int resolve_one_addr (rtx *, void *);
3108 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3109 enum var_init_status);
3110 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3111 enum var_init_status);
3112 static dw_loc_list_ref loc_list_from_tree (tree, int);
3113 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3114 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3115 static tree field_type (const_tree);
3116 static unsigned int simple_type_align_in_bits (const_tree);
3117 static unsigned int simple_decl_align_in_bits (const_tree);
3118 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3119 static HOST_WIDE_INT field_byte_offset (const_tree);
3120 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3121 dw_loc_list_ref);
3122 static void add_data_member_location_attribute (dw_die_ref, tree);
3123 static bool add_const_value_attribute (dw_die_ref, rtx);
3124 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3125 static void insert_double (double_int, unsigned char *);
3126 static void insert_float (const_rtx, unsigned char *);
3127 static rtx rtl_for_decl_location (tree);
3128 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3129 enum dwarf_attribute);
3130 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3131 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3132 static void add_name_attribute (dw_die_ref, const char *);
3133 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3134 static void add_comp_dir_attribute (dw_die_ref);
3135 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3136 static void add_subscript_info (dw_die_ref, tree, bool);
3137 static void add_byte_size_attribute (dw_die_ref, tree);
3138 static void add_bit_offset_attribute (dw_die_ref, tree);
3139 static void add_bit_size_attribute (dw_die_ref, tree);
3140 static void add_prototyped_attribute (dw_die_ref, tree);
3141 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3142 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3143 static void add_src_coords_attributes (dw_die_ref, tree);
3144 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3145 static void push_decl_scope (tree);
3146 static void pop_decl_scope (void);
3147 static dw_die_ref scope_die_for (tree, dw_die_ref);
3148 static inline int local_scope_p (dw_die_ref);
3149 static inline int class_scope_p (dw_die_ref);
3150 static inline int class_or_namespace_scope_p (dw_die_ref);
3151 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3152 static void add_calling_convention_attribute (dw_die_ref, tree);
3153 static const char *type_tag (const_tree);
3154 static tree member_declared_type (const_tree);
3155 #if 0
3156 static const char *decl_start_label (tree);
3157 #endif
3158 static void gen_array_type_die (tree, dw_die_ref);
3159 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3160 #if 0
3161 static void gen_entry_point_die (tree, dw_die_ref);
3162 #endif
3163 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3164 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3165 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3166 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3167 static void gen_formal_types_die (tree, dw_die_ref);
3168 static void gen_subprogram_die (tree, dw_die_ref);
3169 static void gen_variable_die (tree, tree, dw_die_ref);
3170 static void gen_const_die (tree, dw_die_ref);
3171 static void gen_label_die (tree, dw_die_ref);
3172 static void gen_lexical_block_die (tree, dw_die_ref, int);
3173 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3174 static void gen_field_die (tree, dw_die_ref);
3175 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3176 static dw_die_ref gen_compile_unit_die (const char *);
3177 static void gen_inheritance_die (tree, tree, dw_die_ref);
3178 static void gen_member_die (tree, dw_die_ref);
3179 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3180 enum debug_info_usage);
3181 static void gen_subroutine_type_die (tree, dw_die_ref);
3182 static void gen_typedef_die (tree, dw_die_ref);
3183 static void gen_type_die (tree, dw_die_ref);
3184 static void gen_block_die (tree, dw_die_ref, int);
3185 static void decls_for_scope (tree, dw_die_ref, int);
3186 static inline int is_redundant_typedef (const_tree);
3187 static bool is_naming_typedef_decl (const_tree);
3188 static inline dw_die_ref get_context_die (tree);
3189 static void gen_namespace_die (tree, dw_die_ref);
3190 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3191 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3192 static dw_die_ref force_decl_die (tree);
3193 static dw_die_ref force_type_die (tree);
3194 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3195 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3196 static struct dwarf_file_data * lookup_filename (const char *);
3197 static void retry_incomplete_types (void);
3198 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3199 static void gen_generic_params_dies (tree);
3200 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3201 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3202 static void splice_child_die (dw_die_ref, dw_die_ref);
3203 static int file_info_cmp (const void *, const void *);
3204 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3205 const char *, const char *);
3206 static void output_loc_list (dw_loc_list_ref);
3207 static char *gen_internal_sym (const char *);
3208 static bool want_pubnames (void);
3210 static void prune_unmark_dies (dw_die_ref);
3211 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3212 static void prune_unused_types_mark (dw_die_ref, int);
3213 static void prune_unused_types_walk (dw_die_ref);
3214 static void prune_unused_types_walk_attribs (dw_die_ref);
3215 static void prune_unused_types_prune (dw_die_ref);
3216 static void prune_unused_types (void);
3217 static int maybe_emit_file (struct dwarf_file_data *fd);
3218 static inline const char *AT_vms_delta1 (dw_attr_ref);
3219 static inline const char *AT_vms_delta2 (dw_attr_ref);
3220 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3221 const char *, const char *);
3222 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3223 static void gen_remaining_tmpl_value_param_die_attribute (void);
3224 static bool generic_type_p (tree);
3225 static void schedule_generic_params_dies_gen (tree t);
3226 static void gen_scheduled_generic_parms_dies (void);
3228 static const char *comp_dir_string (void);
3230 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3232 /* enum for tracking thread-local variables whose address is really an offset
3233 relative to the TLS pointer, which will need link-time relocation, but will
3234 not need relocation by the DWARF consumer. */
3236 enum dtprel_bool
3238 dtprel_false = 0,
3239 dtprel_true = 1
3242 /* Return the operator to use for an address of a variable. For dtprel_true, we
3243 use DW_OP_const*. For regular variables, which need both link-time
3244 relocation and consumer-level relocation (e.g., to account for shared objects
3245 loaded at a random address), we use DW_OP_addr*. */
3247 static inline enum dwarf_location_atom
3248 dw_addr_op (enum dtprel_bool dtprel)
3250 if (dtprel == dtprel_true)
3251 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3252 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3253 else
3254 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3257 /* Return a pointer to a newly allocated address location description. If
3258 dwarf_split_debug_info is true, then record the address with the appropriate
3259 relocation. */
3260 static inline dw_loc_descr_ref
3261 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3263 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3265 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3266 ref->dw_loc_oprnd1.v.val_addr = addr;
3267 ref->dtprel = dtprel;
3268 if (dwarf_split_debug_info)
3269 ref->dw_loc_oprnd1.val_entry
3270 = add_addr_table_entry (addr,
3271 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3272 else
3273 ref->dw_loc_oprnd1.val_entry = NULL;
3275 return ref;
3278 /* Section names used to hold DWARF debugging information. */
3280 #ifndef DEBUG_INFO_SECTION
3281 #define DEBUG_INFO_SECTION ".debug_info"
3282 #endif
3283 #ifndef DEBUG_DWO_INFO_SECTION
3284 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3285 #endif
3286 #ifndef DEBUG_ABBREV_SECTION
3287 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3288 #endif
3289 #ifndef DEBUG_DWO_ABBREV_SECTION
3290 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3291 #endif
3292 #ifndef DEBUG_ARANGES_SECTION
3293 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3294 #endif
3295 #ifndef DEBUG_ADDR_SECTION
3296 #define DEBUG_ADDR_SECTION ".debug_addr"
3297 #endif
3298 #ifndef DEBUG_NORM_MACINFO_SECTION
3299 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3300 #endif
3301 #ifndef DEBUG_DWO_MACINFO_SECTION
3302 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3303 #endif
3304 #ifndef DEBUG_MACINFO_SECTION
3305 #define DEBUG_MACINFO_SECTION \
3306 (!dwarf_split_debug_info \
3307 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3308 #endif
3309 #ifndef DEBUG_NORM_MACRO_SECTION
3310 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3311 #endif
3312 #ifndef DEBUG_DWO_MACRO_SECTION
3313 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3314 #endif
3315 #ifndef DEBUG_MACRO_SECTION
3316 #define DEBUG_MACRO_SECTION \
3317 (!dwarf_split_debug_info \
3318 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3319 #endif
3320 #ifndef DEBUG_LINE_SECTION
3321 #define DEBUG_LINE_SECTION ".debug_line"
3322 #endif
3323 #ifndef DEBUG_DWO_LINE_SECTION
3324 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3325 #endif
3326 #ifndef DEBUG_LOC_SECTION
3327 #define DEBUG_LOC_SECTION ".debug_loc"
3328 #endif
3329 #ifndef DEBUG_DWO_LOC_SECTION
3330 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3331 #endif
3332 #ifndef DEBUG_PUBNAMES_SECTION
3333 #define DEBUG_PUBNAMES_SECTION \
3334 ((debug_generate_pub_sections == 2) \
3335 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3336 #endif
3337 #ifndef DEBUG_PUBTYPES_SECTION
3338 #define DEBUG_PUBTYPES_SECTION \
3339 ((debug_generate_pub_sections == 2) \
3340 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3341 #endif
3342 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3343 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3344 #ifndef DEBUG_STR_OFFSETS_SECTION
3345 #define DEBUG_STR_OFFSETS_SECTION \
3346 (!dwarf_split_debug_info \
3347 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3348 #endif
3349 #ifndef DEBUG_STR_DWO_SECTION
3350 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3351 #endif
3352 #ifndef DEBUG_STR_SECTION
3353 #define DEBUG_STR_SECTION ".debug_str"
3354 #endif
3355 #ifndef DEBUG_RANGES_SECTION
3356 #define DEBUG_RANGES_SECTION ".debug_ranges"
3357 #endif
3359 /* Standard ELF section names for compiled code and data. */
3360 #ifndef TEXT_SECTION_NAME
3361 #define TEXT_SECTION_NAME ".text"
3362 #endif
3364 /* Section flags for .debug_macinfo/.debug_macro section. */
3365 #define DEBUG_MACRO_SECTION_FLAGS \
3366 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3368 /* Section flags for .debug_str section. */
3369 #define DEBUG_STR_SECTION_FLAGS \
3370 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3371 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3372 : SECTION_DEBUG)
3374 /* Section flags for .debug_str.dwo section. */
3375 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3377 /* Labels we insert at beginning sections we can reference instead of
3378 the section names themselves. */
3380 #ifndef TEXT_SECTION_LABEL
3381 #define TEXT_SECTION_LABEL "Ltext"
3382 #endif
3383 #ifndef COLD_TEXT_SECTION_LABEL
3384 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3385 #endif
3386 #ifndef DEBUG_LINE_SECTION_LABEL
3387 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3388 #endif
3389 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3390 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3391 #endif
3392 #ifndef DEBUG_INFO_SECTION_LABEL
3393 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3394 #endif
3395 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3396 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3397 #endif
3398 #ifndef DEBUG_ABBREV_SECTION_LABEL
3399 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3400 #endif
3401 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3402 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3403 #endif
3404 #ifndef DEBUG_ADDR_SECTION_LABEL
3405 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3406 #endif
3407 #ifndef DEBUG_LOC_SECTION_LABEL
3408 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3409 #endif
3410 #ifndef DEBUG_RANGES_SECTION_LABEL
3411 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3412 #endif
3413 #ifndef DEBUG_MACINFO_SECTION_LABEL
3414 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3415 #endif
3416 #ifndef DEBUG_MACRO_SECTION_LABEL
3417 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3418 #endif
3419 #define SKELETON_COMP_DIE_ABBREV 1
3420 #define SKELETON_TYPE_DIE_ABBREV 2
3422 /* Definitions of defaults for formats and names of various special
3423 (artificial) labels which may be generated within this file (when the -g
3424 options is used and DWARF2_DEBUGGING_INFO is in effect.
3425 If necessary, these may be overridden from within the tm.h file, but
3426 typically, overriding these defaults is unnecessary. */
3428 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3429 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3430 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3431 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3432 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3440 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3441 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3443 #ifndef TEXT_END_LABEL
3444 #define TEXT_END_LABEL "Letext"
3445 #endif
3446 #ifndef COLD_END_LABEL
3447 #define COLD_END_LABEL "Letext_cold"
3448 #endif
3449 #ifndef BLOCK_BEGIN_LABEL
3450 #define BLOCK_BEGIN_LABEL "LBB"
3451 #endif
3452 #ifndef BLOCK_END_LABEL
3453 #define BLOCK_END_LABEL "LBE"
3454 #endif
3455 #ifndef LINE_CODE_LABEL
3456 #define LINE_CODE_LABEL "LM"
3457 #endif
3460 /* Return the root of the DIE's built for the current compilation unit. */
3461 static dw_die_ref
3462 comp_unit_die (void)
3464 if (!single_comp_unit_die)
3465 single_comp_unit_die = gen_compile_unit_die (NULL);
3466 return single_comp_unit_die;
3469 /* We allow a language front-end to designate a function that is to be
3470 called to "demangle" any name before it is put into a DIE. */
3472 static const char *(*demangle_name_func) (const char *);
3474 void
3475 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3477 demangle_name_func = func;
3480 /* Test if rtl node points to a pseudo register. */
3482 static inline int
3483 is_pseudo_reg (const_rtx rtl)
3485 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3486 || (GET_CODE (rtl) == SUBREG
3487 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3490 /* Return a reference to a type, with its const and volatile qualifiers
3491 removed. */
3493 static inline tree
3494 type_main_variant (tree type)
3496 type = TYPE_MAIN_VARIANT (type);
3498 /* ??? There really should be only one main variant among any group of
3499 variants of a given type (and all of the MAIN_VARIANT values for all
3500 members of the group should point to that one type) but sometimes the C
3501 front-end messes this up for array types, so we work around that bug
3502 here. */
3503 if (TREE_CODE (type) == ARRAY_TYPE)
3504 while (type != TYPE_MAIN_VARIANT (type))
3505 type = TYPE_MAIN_VARIANT (type);
3507 return type;
3510 /* Return nonzero if the given type node represents a tagged type. */
3512 static inline int
3513 is_tagged_type (const_tree type)
3515 enum tree_code code = TREE_CODE (type);
3517 return (code == RECORD_TYPE || code == UNION_TYPE
3518 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3521 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3523 static void
3524 get_ref_die_offset_label (char *label, dw_die_ref ref)
3526 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3529 /* Return die_offset of a DIE reference to a base type. */
3531 static unsigned long int
3532 get_base_type_offset (dw_die_ref ref)
3534 if (ref->die_offset)
3535 return ref->die_offset;
3536 if (comp_unit_die ()->die_abbrev)
3538 calc_base_type_die_sizes ();
3539 gcc_assert (ref->die_offset);
3541 return ref->die_offset;
3544 /* Return die_offset of a DIE reference other than base type. */
3546 static unsigned long int
3547 get_ref_die_offset (dw_die_ref ref)
3549 gcc_assert (ref->die_offset);
3550 return ref->die_offset;
3553 /* Convert a DIE tag into its string name. */
3555 static const char *
3556 dwarf_tag_name (unsigned int tag)
3558 const char *name = get_DW_TAG_name (tag);
3560 if (name != NULL)
3561 return name;
3563 return "DW_TAG_<unknown>";
3566 /* Convert a DWARF attribute code into its string name. */
3568 static const char *
3569 dwarf_attr_name (unsigned int attr)
3571 const char *name;
3573 switch (attr)
3575 #if VMS_DEBUGGING_INFO
3576 case DW_AT_HP_prologue:
3577 return "DW_AT_HP_prologue";
3578 #else
3579 case DW_AT_MIPS_loop_unroll_factor:
3580 return "DW_AT_MIPS_loop_unroll_factor";
3581 #endif
3583 #if VMS_DEBUGGING_INFO
3584 case DW_AT_HP_epilogue:
3585 return "DW_AT_HP_epilogue";
3586 #else
3587 case DW_AT_MIPS_stride:
3588 return "DW_AT_MIPS_stride";
3589 #endif
3592 name = get_DW_AT_name (attr);
3594 if (name != NULL)
3595 return name;
3597 return "DW_AT_<unknown>";
3600 /* Convert a DWARF value form code into its string name. */
3602 static const char *
3603 dwarf_form_name (unsigned int form)
3605 const char *name = get_DW_FORM_name (form);
3607 if (name != NULL)
3608 return name;
3610 return "DW_FORM_<unknown>";
3613 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3614 instance of an inlined instance of a decl which is local to an inline
3615 function, so we have to trace all of the way back through the origin chain
3616 to find out what sort of node actually served as the original seed for the
3617 given block. */
3619 static tree
3620 decl_ultimate_origin (const_tree decl)
3622 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3623 return NULL_TREE;
3625 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3626 nodes in the function to point to themselves; ignore that if
3627 we're trying to output the abstract instance of this function. */
3628 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3629 return NULL_TREE;
3631 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3632 most distant ancestor, this should never happen. */
3633 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3635 return DECL_ABSTRACT_ORIGIN (decl);
3638 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3639 of a virtual function may refer to a base class, so we check the 'this'
3640 parameter. */
3642 static tree
3643 decl_class_context (tree decl)
3645 tree context = NULL_TREE;
3647 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3648 context = DECL_CONTEXT (decl);
3649 else
3650 context = TYPE_MAIN_VARIANT
3651 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3653 if (context && !TYPE_P (context))
3654 context = NULL_TREE;
3656 return context;
3659 /* Add an attribute/value pair to a DIE. */
3661 static inline void
3662 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3664 /* Maybe this should be an assert? */
3665 if (die == NULL)
3666 return;
3668 vec_safe_reserve (die->die_attr, 1);
3669 vec_safe_push (die->die_attr, *attr);
3672 static inline enum dw_val_class
3673 AT_class (dw_attr_ref a)
3675 return a->dw_attr_val.val_class;
3678 /* Return the index for any attribute that will be referenced with a
3679 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3680 are stored in dw_attr_val.v.val_str for reference counting
3681 pruning. */
3683 static inline unsigned int
3684 AT_index (dw_attr_ref a)
3686 if (AT_class (a) == dw_val_class_str)
3687 return a->dw_attr_val.v.val_str->index;
3688 else if (a->dw_attr_val.val_entry != NULL)
3689 return a->dw_attr_val.val_entry->index;
3690 return NOT_INDEXED;
3693 /* Add a flag value attribute to a DIE. */
3695 static inline void
3696 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3698 dw_attr_node attr;
3700 attr.dw_attr = attr_kind;
3701 attr.dw_attr_val.val_class = dw_val_class_flag;
3702 attr.dw_attr_val.val_entry = NULL;
3703 attr.dw_attr_val.v.val_flag = flag;
3704 add_dwarf_attr (die, &attr);
3707 static inline unsigned
3708 AT_flag (dw_attr_ref a)
3710 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3711 return a->dw_attr_val.v.val_flag;
3714 /* Add a signed integer attribute value to a DIE. */
3716 static inline void
3717 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3719 dw_attr_node attr;
3721 attr.dw_attr = attr_kind;
3722 attr.dw_attr_val.val_class = dw_val_class_const;
3723 attr.dw_attr_val.val_entry = NULL;
3724 attr.dw_attr_val.v.val_int = int_val;
3725 add_dwarf_attr (die, &attr);
3728 static inline HOST_WIDE_INT
3729 AT_int (dw_attr_ref a)
3731 gcc_assert (a && AT_class (a) == dw_val_class_const);
3732 return a->dw_attr_val.v.val_int;
3735 /* Add an unsigned integer attribute value to a DIE. */
3737 static inline void
3738 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3739 unsigned HOST_WIDE_INT unsigned_val)
3741 dw_attr_node attr;
3743 attr.dw_attr = attr_kind;
3744 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3745 attr.dw_attr_val.val_entry = NULL;
3746 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3747 add_dwarf_attr (die, &attr);
3750 static inline unsigned HOST_WIDE_INT
3751 AT_unsigned (dw_attr_ref a)
3753 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3754 return a->dw_attr_val.v.val_unsigned;
3757 /* Add an unsigned double integer attribute value to a DIE. */
3759 static inline void
3760 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3761 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3763 dw_attr_node attr;
3765 attr.dw_attr = attr_kind;
3766 attr.dw_attr_val.val_class = dw_val_class_const_double;
3767 attr.dw_attr_val.val_entry = NULL;
3768 attr.dw_attr_val.v.val_double.high = high;
3769 attr.dw_attr_val.v.val_double.low = low;
3770 add_dwarf_attr (die, &attr);
3773 /* Add a floating point attribute value to a DIE and return it. */
3775 static inline void
3776 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3777 unsigned int length, unsigned int elt_size, unsigned char *array)
3779 dw_attr_node attr;
3781 attr.dw_attr = attr_kind;
3782 attr.dw_attr_val.val_class = dw_val_class_vec;
3783 attr.dw_attr_val.val_entry = NULL;
3784 attr.dw_attr_val.v.val_vec.length = length;
3785 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3786 attr.dw_attr_val.v.val_vec.array = array;
3787 add_dwarf_attr (die, &attr);
3790 /* Add an 8-byte data attribute value to a DIE. */
3792 static inline void
3793 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3794 unsigned char data8[8])
3796 dw_attr_node attr;
3798 attr.dw_attr = attr_kind;
3799 attr.dw_attr_val.val_class = dw_val_class_data8;
3800 attr.dw_attr_val.val_entry = NULL;
3801 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3802 add_dwarf_attr (die, &attr);
3805 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3806 dwarf_split_debug_info, address attributes in dies destined for the
3807 final executable have force_direct set to avoid using indexed
3808 references. */
3810 static inline void
3811 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3812 bool force_direct)
3814 dw_attr_node attr;
3815 char * lbl_id;
3817 lbl_id = xstrdup (lbl_low);
3818 attr.dw_attr = DW_AT_low_pc;
3819 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3820 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3821 if (dwarf_split_debug_info && !force_direct)
3822 attr.dw_attr_val.val_entry
3823 = add_addr_table_entry (lbl_id, ate_kind_label);
3824 else
3825 attr.dw_attr_val.val_entry = NULL;
3826 add_dwarf_attr (die, &attr);
3828 attr.dw_attr = DW_AT_high_pc;
3829 if (dwarf_version < 4)
3830 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3831 else
3832 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3833 lbl_id = xstrdup (lbl_high);
3834 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3835 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3836 && dwarf_split_debug_info && !force_direct)
3837 attr.dw_attr_val.val_entry
3838 = add_addr_table_entry (lbl_id, ate_kind_label);
3839 else
3840 attr.dw_attr_val.val_entry = NULL;
3841 add_dwarf_attr (die, &attr);
3844 /* Hash and equality functions for debug_str_hash. */
3846 static hashval_t
3847 debug_str_do_hash (const void *x)
3849 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3852 static int
3853 debug_str_eq (const void *x1, const void *x2)
3855 return strcmp ((((const struct indirect_string_node *)x1)->str),
3856 (const char *)x2) == 0;
3859 /* Add STR to the given string hash table. */
3861 static struct indirect_string_node *
3862 find_AT_string_in_table (const char *str, htab_t table)
3864 struct indirect_string_node *node;
3865 void **slot;
3867 slot = htab_find_slot_with_hash (table, str,
3868 htab_hash_string (str), INSERT);
3869 if (*slot == NULL)
3871 node = ggc_alloc_cleared_indirect_string_node ();
3872 node->str = ggc_strdup (str);
3873 *slot = node;
3875 else
3876 node = (struct indirect_string_node *) *slot;
3878 node->refcount++;
3879 return node;
3882 /* Add STR to the indirect string hash table. */
3884 static struct indirect_string_node *
3885 find_AT_string (const char *str)
3887 if (! debug_str_hash)
3888 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3889 debug_str_eq, NULL);
3891 return find_AT_string_in_table (str, debug_str_hash);
3894 /* Add a string attribute value to a DIE. */
3896 static inline void
3897 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3899 dw_attr_node attr;
3900 struct indirect_string_node *node;
3902 node = find_AT_string (str);
3904 attr.dw_attr = attr_kind;
3905 attr.dw_attr_val.val_class = dw_val_class_str;
3906 attr.dw_attr_val.val_entry = NULL;
3907 attr.dw_attr_val.v.val_str = node;
3908 add_dwarf_attr (die, &attr);
3911 static inline const char *
3912 AT_string (dw_attr_ref a)
3914 gcc_assert (a && AT_class (a) == dw_val_class_str);
3915 return a->dw_attr_val.v.val_str->str;
3918 /* Call this function directly to bypass AT_string_form's logic to put
3919 the string inline in the die. */
3921 static void
3922 set_indirect_string (struct indirect_string_node *node)
3924 char label[32];
3925 /* Already indirect is a no op. */
3926 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3928 gcc_assert (node->label);
3929 return;
3931 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3932 ++dw2_string_counter;
3933 node->label = xstrdup (label);
3935 if (!dwarf_split_debug_info)
3937 node->form = DW_FORM_strp;
3938 node->index = NOT_INDEXED;
3940 else
3942 node->form = DW_FORM_GNU_str_index;
3943 node->index = NO_INDEX_ASSIGNED;
3947 /* Find out whether a string should be output inline in DIE
3948 or out-of-line in .debug_str section. */
3950 static enum dwarf_form
3951 find_string_form (struct indirect_string_node *node)
3953 unsigned int len;
3955 if (node->form)
3956 return node->form;
3958 len = strlen (node->str) + 1;
3960 /* If the string is shorter or equal to the size of the reference, it is
3961 always better to put it inline. */
3962 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3963 return node->form = DW_FORM_string;
3965 /* If we cannot expect the linker to merge strings in .debug_str
3966 section, only put it into .debug_str if it is worth even in this
3967 single module. */
3968 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3969 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3970 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3971 return node->form = DW_FORM_string;
3973 set_indirect_string (node);
3975 return node->form;
3978 /* Find out whether the string referenced from the attribute should be
3979 output inline in DIE or out-of-line in .debug_str section. */
3981 static enum dwarf_form
3982 AT_string_form (dw_attr_ref a)
3984 gcc_assert (a && AT_class (a) == dw_val_class_str);
3985 return find_string_form (a->dw_attr_val.v.val_str);
3988 /* Add a DIE reference attribute value to a DIE. */
3990 static inline void
3991 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3993 dw_attr_node attr;
3995 #ifdef ENABLE_CHECKING
3996 gcc_assert (targ_die != NULL);
3997 #else
3998 /* With LTO we can end up trying to reference something we didn't create
3999 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4000 if (targ_die == NULL)
4001 return;
4002 #endif
4004 attr.dw_attr = attr_kind;
4005 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4006 attr.dw_attr_val.val_entry = NULL;
4007 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4008 attr.dw_attr_val.v.val_die_ref.external = 0;
4009 add_dwarf_attr (die, &attr);
4012 /* Change DIE reference REF to point to NEW_DIE instead. */
4014 static inline void
4015 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4017 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4018 ref->dw_attr_val.v.val_die_ref.die = new_die;
4019 ref->dw_attr_val.v.val_die_ref.external = 0;
4022 /* Add an AT_specification attribute to a DIE, and also make the back
4023 pointer from the specification to the definition. */
4025 static inline void
4026 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4028 add_AT_die_ref (die, DW_AT_specification, targ_die);
4029 gcc_assert (!targ_die->die_definition);
4030 targ_die->die_definition = die;
4033 static inline dw_die_ref
4034 AT_ref (dw_attr_ref a)
4036 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4037 return a->dw_attr_val.v.val_die_ref.die;
4040 static inline int
4041 AT_ref_external (dw_attr_ref a)
4043 if (a && AT_class (a) == dw_val_class_die_ref)
4044 return a->dw_attr_val.v.val_die_ref.external;
4046 return 0;
4049 static inline void
4050 set_AT_ref_external (dw_attr_ref a, int i)
4052 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4053 a->dw_attr_val.v.val_die_ref.external = i;
4056 /* Add an FDE reference attribute value to a DIE. */
4058 static inline void
4059 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4061 dw_attr_node attr;
4063 attr.dw_attr = attr_kind;
4064 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4065 attr.dw_attr_val.val_entry = NULL;
4066 attr.dw_attr_val.v.val_fde_index = targ_fde;
4067 add_dwarf_attr (die, &attr);
4070 /* Add a location description attribute value to a DIE. */
4072 static inline void
4073 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4075 dw_attr_node attr;
4077 attr.dw_attr = attr_kind;
4078 attr.dw_attr_val.val_class = dw_val_class_loc;
4079 attr.dw_attr_val.val_entry = NULL;
4080 attr.dw_attr_val.v.val_loc = loc;
4081 add_dwarf_attr (die, &attr);
4084 static inline dw_loc_descr_ref
4085 AT_loc (dw_attr_ref a)
4087 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4088 return a->dw_attr_val.v.val_loc;
4091 static inline void
4092 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4094 dw_attr_node attr;
4096 attr.dw_attr = attr_kind;
4097 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4098 attr.dw_attr_val.val_entry = NULL;
4099 attr.dw_attr_val.v.val_loc_list = loc_list;
4100 add_dwarf_attr (die, &attr);
4101 have_location_lists = true;
4104 static inline dw_loc_list_ref
4105 AT_loc_list (dw_attr_ref a)
4107 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4108 return a->dw_attr_val.v.val_loc_list;
4111 static inline dw_loc_list_ref *
4112 AT_loc_list_ptr (dw_attr_ref a)
4114 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4115 return &a->dw_attr_val.v.val_loc_list;
4118 /* Table of entries into the .debug_addr section. */
4120 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4122 /* Hash an address_table_entry. */
4124 static hashval_t
4125 addr_table_entry_do_hash (const void *x)
4127 const addr_table_entry *a = (const addr_table_entry *) x;
4128 switch (a->kind)
4130 case ate_kind_rtx:
4131 return iterative_hash_rtx (a->addr.rtl, 0);
4132 case ate_kind_rtx_dtprel:
4133 return iterative_hash_rtx (a->addr.rtl, 1);
4134 case ate_kind_label:
4135 return htab_hash_string (a->addr.label);
4136 default:
4137 gcc_unreachable ();
4141 /* Determine equality for two address_table_entries. */
4143 static int
4144 addr_table_entry_eq (const void *x1, const void *x2)
4146 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4147 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4149 if (a1->kind != a2->kind)
4150 return 0;
4151 switch (a1->kind)
4153 case ate_kind_rtx:
4154 case ate_kind_rtx_dtprel:
4155 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4156 case ate_kind_label:
4157 return strcmp (a1->addr.label, a2->addr.label) == 0;
4158 default:
4159 gcc_unreachable ();
4163 /* Initialize an addr_table_entry. */
4165 void
4166 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4168 e->kind = kind;
4169 switch (kind)
4171 case ate_kind_rtx:
4172 case ate_kind_rtx_dtprel:
4173 e->addr.rtl = (rtx) addr;
4174 break;
4175 case ate_kind_label:
4176 e->addr.label = (char *) addr;
4177 break;
4179 e->refcount = 0;
4180 e->index = NO_INDEX_ASSIGNED;
4183 /* Add attr to the address table entry to the table. Defer setting an
4184 index until output time. */
4186 static addr_table_entry *
4187 add_addr_table_entry (void *addr, enum ate_kind kind)
4189 addr_table_entry *node;
4190 addr_table_entry finder;
4191 void **slot;
4193 gcc_assert (dwarf_split_debug_info);
4194 if (! addr_index_table)
4195 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4196 addr_table_entry_eq, NULL);
4197 init_addr_table_entry (&finder, kind, addr);
4198 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4200 if (*slot == HTAB_EMPTY_ENTRY)
4202 node = ggc_alloc_cleared_addr_table_entry ();
4203 init_addr_table_entry (node, kind, addr);
4204 *slot = node;
4206 else
4207 node = (addr_table_entry *) *slot;
4209 node->refcount++;
4210 return node;
4213 /* Remove an entry from the addr table by decrementing its refcount.
4214 Strictly, decrementing the refcount would be enough, but the
4215 assertion that the entry is actually in the table has found
4216 bugs. */
4218 static void
4219 remove_addr_table_entry (addr_table_entry *entry)
4221 addr_table_entry *node;
4223 gcc_assert (dwarf_split_debug_info && addr_index_table);
4224 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4225 /* After an index is assigned, the table is frozen. */
4226 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4227 node->refcount--;
4230 /* Given a location list, remove all addresses it refers to from the
4231 address_table. */
4233 static void
4234 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4236 for (; descr; descr = descr->dw_loc_next)
4237 if (descr->dw_loc_oprnd1.val_entry != NULL)
4239 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4240 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4244 /* A helper function for dwarf2out_finish called through
4245 htab_traverse. Assign an addr_table_entry its index. All entries
4246 must be collected into the table when this function is called,
4247 because the indexing code relies on htab_traverse to traverse nodes
4248 in the same order for each run. */
4250 static int
4251 index_addr_table_entry (void **h, void *v)
4253 addr_table_entry *node = (addr_table_entry *) *h;
4254 unsigned int *index = (unsigned int *) v;
4256 /* Don't index unreferenced nodes. */
4257 if (node->refcount == 0)
4258 return 1;
4260 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4261 node->index = *index;
4262 *index += 1;
4264 return 1;
4267 /* Add an address constant attribute value to a DIE. When using
4268 dwarf_split_debug_info, address attributes in dies destined for the
4269 final executable should be direct references--setting the parameter
4270 force_direct ensures this behavior. */
4272 static inline void
4273 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4274 bool force_direct)
4276 dw_attr_node attr;
4278 attr.dw_attr = attr_kind;
4279 attr.dw_attr_val.val_class = dw_val_class_addr;
4280 attr.dw_attr_val.v.val_addr = addr;
4281 if (dwarf_split_debug_info && !force_direct)
4282 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4283 else
4284 attr.dw_attr_val.val_entry = NULL;
4285 add_dwarf_attr (die, &attr);
4288 /* Get the RTX from to an address DIE attribute. */
4290 static inline rtx
4291 AT_addr (dw_attr_ref a)
4293 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4294 return a->dw_attr_val.v.val_addr;
4297 /* Add a file attribute value to a DIE. */
4299 static inline void
4300 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4301 struct dwarf_file_data *fd)
4303 dw_attr_node attr;
4305 attr.dw_attr = attr_kind;
4306 attr.dw_attr_val.val_class = dw_val_class_file;
4307 attr.dw_attr_val.val_entry = NULL;
4308 attr.dw_attr_val.v.val_file = fd;
4309 add_dwarf_attr (die, &attr);
4312 /* Get the dwarf_file_data from a file DIE attribute. */
4314 static inline struct dwarf_file_data *
4315 AT_file (dw_attr_ref a)
4317 gcc_assert (a && AT_class (a) == dw_val_class_file);
4318 return a->dw_attr_val.v.val_file;
4321 /* Add a vms delta attribute value to a DIE. */
4323 static inline void
4324 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4325 const char *lbl1, const char *lbl2)
4327 dw_attr_node attr;
4329 attr.dw_attr = attr_kind;
4330 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4331 attr.dw_attr_val.val_entry = NULL;
4332 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4333 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4334 add_dwarf_attr (die, &attr);
4337 /* Add a label identifier attribute value to a DIE. */
4339 static inline void
4340 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4341 const char *lbl_id)
4343 dw_attr_node attr;
4345 attr.dw_attr = attr_kind;
4346 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4347 attr.dw_attr_val.val_entry = NULL;
4348 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4349 if (dwarf_split_debug_info)
4350 attr.dw_attr_val.val_entry
4351 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4352 ate_kind_label);
4353 add_dwarf_attr (die, &attr);
4356 /* Add a section offset attribute value to a DIE, an offset into the
4357 debug_line section. */
4359 static inline void
4360 add_AT_lineptr (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_lineptr;
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 a section offset attribute value to a DIE, an offset into the
4373 debug_macinfo section. */
4375 static inline void
4376 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4377 const char *label)
4379 dw_attr_node attr;
4381 attr.dw_attr = attr_kind;
4382 attr.dw_attr_val.val_class = dw_val_class_macptr;
4383 attr.dw_attr_val.val_entry = NULL;
4384 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4385 add_dwarf_attr (die, &attr);
4388 /* Add an offset attribute value to a DIE. */
4390 static inline void
4391 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4392 unsigned HOST_WIDE_INT offset)
4394 dw_attr_node attr;
4396 attr.dw_attr = attr_kind;
4397 attr.dw_attr_val.val_class = dw_val_class_offset;
4398 attr.dw_attr_val.val_entry = NULL;
4399 attr.dw_attr_val.v.val_offset = offset;
4400 add_dwarf_attr (die, &attr);
4403 /* Add a range_list attribute value to a DIE. When using
4404 dwarf_split_debug_info, address attributes in dies destined for the
4405 final executable should be direct references--setting the parameter
4406 force_direct ensures this behavior. */
4408 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4409 #define RELOCATED_OFFSET (NULL)
4411 static void
4412 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4413 long unsigned int offset, bool force_direct)
4415 dw_attr_node attr;
4417 attr.dw_attr = attr_kind;
4418 attr.dw_attr_val.val_class = dw_val_class_range_list;
4419 /* For the range_list attribute, use val_entry to store whether the
4420 offset should follow split-debug-info or normal semantics. This
4421 value is read in output_range_list_offset. */
4422 if (dwarf_split_debug_info && !force_direct)
4423 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4424 else
4425 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4426 attr.dw_attr_val.v.val_offset = offset;
4427 add_dwarf_attr (die, &attr);
4430 /* Return the start label of a delta attribute. */
4432 static inline const char *
4433 AT_vms_delta1 (dw_attr_ref a)
4435 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4436 return a->dw_attr_val.v.val_vms_delta.lbl1;
4439 /* Return the end label of a delta attribute. */
4441 static inline const char *
4442 AT_vms_delta2 (dw_attr_ref a)
4444 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4445 return a->dw_attr_val.v.val_vms_delta.lbl2;
4448 static inline const char *
4449 AT_lbl (dw_attr_ref a)
4451 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4452 || AT_class (a) == dw_val_class_lineptr
4453 || AT_class (a) == dw_val_class_macptr
4454 || AT_class (a) == dw_val_class_high_pc));
4455 return a->dw_attr_val.v.val_lbl_id;
4458 /* Get the attribute of type attr_kind. */
4460 static dw_attr_ref
4461 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4463 dw_attr_ref a;
4464 unsigned ix;
4465 dw_die_ref spec = NULL;
4467 if (! die)
4468 return NULL;
4470 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4471 if (a->dw_attr == attr_kind)
4472 return a;
4473 else if (a->dw_attr == DW_AT_specification
4474 || a->dw_attr == DW_AT_abstract_origin)
4475 spec = AT_ref (a);
4477 if (spec)
4478 return get_AT (spec, attr_kind);
4480 return NULL;
4483 /* Returns the parent of the declaration of DIE. */
4485 static dw_die_ref
4486 get_die_parent (dw_die_ref die)
4488 dw_die_ref t;
4490 if (!die)
4491 return NULL;
4493 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4494 || (t = get_AT_ref (die, DW_AT_specification)))
4495 die = t;
4497 return die->die_parent;
4500 /* Return the "low pc" attribute value, typically associated with a subprogram
4501 DIE. Return null if the "low pc" attribute is either not present, or if it
4502 cannot be represented as an assembler label identifier. */
4504 static inline const char *
4505 get_AT_low_pc (dw_die_ref die)
4507 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4509 return a ? AT_lbl (a) : NULL;
4512 /* Return the "high pc" attribute value, typically associated with a subprogram
4513 DIE. Return null if the "high pc" attribute is either not present, or if it
4514 cannot be represented as an assembler label identifier. */
4516 static inline const char *
4517 get_AT_hi_pc (dw_die_ref die)
4519 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4521 return a ? AT_lbl (a) : NULL;
4524 /* Return the value of the string attribute designated by ATTR_KIND, or
4525 NULL if it is not present. */
4527 static inline const char *
4528 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4530 dw_attr_ref a = get_AT (die, attr_kind);
4532 return a ? AT_string (a) : NULL;
4535 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4536 if it is not present. */
4538 static inline int
4539 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4541 dw_attr_ref a = get_AT (die, attr_kind);
4543 return a ? AT_flag (a) : 0;
4546 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4547 if it is not present. */
4549 static inline unsigned
4550 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4552 dw_attr_ref a = get_AT (die, attr_kind);
4554 return a ? AT_unsigned (a) : 0;
4557 static inline dw_die_ref
4558 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4560 dw_attr_ref a = get_AT (die, attr_kind);
4562 return a ? AT_ref (a) : NULL;
4565 static inline struct dwarf_file_data *
4566 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4568 dw_attr_ref a = get_AT (die, attr_kind);
4570 return a ? AT_file (a) : NULL;
4573 /* Return TRUE if the language is C++. */
4575 static inline bool
4576 is_cxx (void)
4578 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4580 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4583 /* Return TRUE if the language is Java. */
4585 static inline bool
4586 is_java (void)
4588 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4590 return lang == DW_LANG_Java;
4593 /* Return TRUE if the language is Fortran. */
4595 static inline bool
4596 is_fortran (void)
4598 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4600 return (lang == DW_LANG_Fortran77
4601 || lang == DW_LANG_Fortran90
4602 || lang == DW_LANG_Fortran95);
4605 /* Return TRUE if the language is Ada. */
4607 static inline bool
4608 is_ada (void)
4610 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4612 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4615 /* Remove the specified attribute if present. */
4617 static void
4618 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4620 dw_attr_ref a;
4621 unsigned ix;
4623 if (! die)
4624 return;
4626 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4627 if (a->dw_attr == attr_kind)
4629 if (AT_class (a) == dw_val_class_str)
4630 if (a->dw_attr_val.v.val_str->refcount)
4631 a->dw_attr_val.v.val_str->refcount--;
4633 /* vec::ordered_remove should help reduce the number of abbrevs
4634 that are needed. */
4635 die->die_attr->ordered_remove (ix);
4636 return;
4640 /* Remove CHILD from its parent. PREV must have the property that
4641 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4643 static void
4644 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4646 gcc_assert (child->die_parent == prev->die_parent);
4647 gcc_assert (prev->die_sib == child);
4648 if (prev == child)
4650 gcc_assert (child->die_parent->die_child == child);
4651 prev = NULL;
4653 else
4654 prev->die_sib = child->die_sib;
4655 if (child->die_parent->die_child == child)
4656 child->die_parent->die_child = prev;
4659 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4660 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4662 static void
4663 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4665 dw_die_ref parent = old_child->die_parent;
4667 gcc_assert (parent == prev->die_parent);
4668 gcc_assert (prev->die_sib == old_child);
4670 new_child->die_parent = parent;
4671 if (prev == old_child)
4673 gcc_assert (parent->die_child == old_child);
4674 new_child->die_sib = new_child;
4676 else
4678 prev->die_sib = new_child;
4679 new_child->die_sib = old_child->die_sib;
4681 if (old_child->die_parent->die_child == old_child)
4682 old_child->die_parent->die_child = new_child;
4685 /* Move all children from OLD_PARENT to NEW_PARENT. */
4687 static void
4688 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4690 dw_die_ref c;
4691 new_parent->die_child = old_parent->die_child;
4692 old_parent->die_child = NULL;
4693 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4696 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4697 matches TAG. */
4699 static void
4700 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4702 dw_die_ref c;
4704 c = die->die_child;
4705 if (c) do {
4706 dw_die_ref prev = c;
4707 c = c->die_sib;
4708 while (c->die_tag == tag)
4710 remove_child_with_prev (c, prev);
4711 /* Might have removed every child. */
4712 if (c == c->die_sib)
4713 return;
4714 c = c->die_sib;
4716 } while (c != die->die_child);
4719 /* Add a CHILD_DIE as the last child of DIE. */
4721 static void
4722 add_child_die (dw_die_ref die, dw_die_ref child_die)
4724 /* FIXME this should probably be an assert. */
4725 if (! die || ! child_die)
4726 return;
4727 gcc_assert (die != child_die);
4729 child_die->die_parent = die;
4730 if (die->die_child)
4732 child_die->die_sib = die->die_child->die_sib;
4733 die->die_child->die_sib = child_die;
4735 else
4736 child_die->die_sib = child_die;
4737 die->die_child = child_die;
4740 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4741 is the specification, to the end of PARENT's list of children.
4742 This is done by removing and re-adding it. */
4744 static void
4745 splice_child_die (dw_die_ref parent, dw_die_ref child)
4747 dw_die_ref p;
4749 /* We want the declaration DIE from inside the class, not the
4750 specification DIE at toplevel. */
4751 if (child->die_parent != parent)
4753 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4755 if (tmp)
4756 child = tmp;
4759 gcc_assert (child->die_parent == parent
4760 || (child->die_parent
4761 == get_AT_ref (parent, DW_AT_specification)));
4763 for (p = child->die_parent->die_child; ; p = p->die_sib)
4764 if (p->die_sib == child)
4766 remove_child_with_prev (child, p);
4767 break;
4770 add_child_die (parent, child);
4773 /* Return a pointer to a newly created DIE node. */
4775 static inline dw_die_ref
4776 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4778 dw_die_ref die = ggc_alloc_cleared_die_node ();
4780 die->die_tag = tag_value;
4782 if (parent_die != NULL)
4783 add_child_die (parent_die, die);
4784 else
4786 limbo_die_node *limbo_node;
4788 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4789 limbo_node->die = die;
4790 limbo_node->created_for = t;
4791 limbo_node->next = limbo_die_list;
4792 limbo_die_list = limbo_node;
4795 return die;
4798 /* Return the DIE associated with the given type specifier. */
4800 static inline dw_die_ref
4801 lookup_type_die (tree type)
4803 return TYPE_SYMTAB_DIE (type);
4806 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4807 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4808 anonymous type instead the one of the naming typedef. */
4810 static inline dw_die_ref
4811 strip_naming_typedef (tree type, dw_die_ref type_die)
4813 if (type
4814 && TREE_CODE (type) == RECORD_TYPE
4815 && type_die
4816 && type_die->die_tag == DW_TAG_typedef
4817 && is_naming_typedef_decl (TYPE_NAME (type)))
4818 type_die = get_AT_ref (type_die, DW_AT_type);
4819 return type_die;
4822 /* Like lookup_type_die, but if type is an anonymous type named by a
4823 typedef[1], return the DIE of the anonymous type instead the one of
4824 the naming typedef. This is because in gen_typedef_die, we did
4825 equate the anonymous struct named by the typedef with the DIE of
4826 the naming typedef. So by default, lookup_type_die on an anonymous
4827 struct yields the DIE of the naming typedef.
4829 [1]: Read the comment of is_naming_typedef_decl to learn about what
4830 a naming typedef is. */
4832 static inline dw_die_ref
4833 lookup_type_die_strip_naming_typedef (tree type)
4835 dw_die_ref die = lookup_type_die (type);
4836 return strip_naming_typedef (type, die);
4839 /* Equate a DIE to a given type specifier. */
4841 static inline void
4842 equate_type_number_to_die (tree type, dw_die_ref type_die)
4844 TYPE_SYMTAB_DIE (type) = type_die;
4847 /* Returns a hash value for X (which really is a die_struct). */
4849 static hashval_t
4850 decl_die_table_hash (const void *x)
4852 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4855 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4857 static int
4858 decl_die_table_eq (const void *x, const void *y)
4860 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4863 /* Return the DIE associated with a given declaration. */
4865 static inline dw_die_ref
4866 lookup_decl_die (tree decl)
4868 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4871 /* Returns a hash value for X (which really is a var_loc_list). */
4873 static hashval_t
4874 decl_loc_table_hash (const void *x)
4876 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4879 /* Return nonzero if decl_id of var_loc_list X is the same as
4880 UID of decl *Y. */
4882 static int
4883 decl_loc_table_eq (const void *x, const void *y)
4885 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4888 /* Return the var_loc list associated with a given declaration. */
4890 static inline var_loc_list *
4891 lookup_decl_loc (const_tree decl)
4893 if (!decl_loc_table)
4894 return NULL;
4895 return (var_loc_list *)
4896 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4899 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4901 static hashval_t
4902 cached_dw_loc_list_table_hash (const void *x)
4904 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4907 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4908 UID of decl *Y. */
4910 static int
4911 cached_dw_loc_list_table_eq (const void *x, const void *y)
4913 return (((const cached_dw_loc_list *) x)->decl_id
4914 == DECL_UID ((const_tree) y));
4917 /* Equate a DIE to a particular declaration. */
4919 static void
4920 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4922 unsigned int decl_id = DECL_UID (decl);
4923 void **slot;
4925 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4926 *slot = decl_die;
4927 decl_die->decl_id = decl_id;
4930 /* Return how many bits covers PIECE EXPR_LIST. */
4932 static int
4933 decl_piece_bitsize (rtx piece)
4935 int ret = (int) GET_MODE (piece);
4936 if (ret)
4937 return ret;
4938 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4939 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4940 return INTVAL (XEXP (XEXP (piece, 0), 0));
4943 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4945 static rtx *
4946 decl_piece_varloc_ptr (rtx piece)
4948 if ((int) GET_MODE (piece))
4949 return &XEXP (piece, 0);
4950 else
4951 return &XEXP (XEXP (piece, 0), 1);
4954 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4955 Next is the chain of following piece nodes. */
4957 static rtx
4958 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4960 if (bitsize <= (int) MAX_MACHINE_MODE)
4961 return alloc_EXPR_LIST (bitsize, loc_note, next);
4962 else
4963 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4964 GEN_INT (bitsize),
4965 loc_note), next);
4968 /* Return rtx that should be stored into loc field for
4969 LOC_NOTE and BITPOS/BITSIZE. */
4971 static rtx
4972 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4973 HOST_WIDE_INT bitsize)
4975 if (bitsize != -1)
4977 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4978 if (bitpos != 0)
4979 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4981 return loc_note;
4984 /* This function either modifies location piece list *DEST in
4985 place (if SRC and INNER is NULL), or copies location piece list
4986 *SRC to *DEST while modifying it. Location BITPOS is modified
4987 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4988 not copied and if needed some padding around it is added.
4989 When modifying in place, DEST should point to EXPR_LIST where
4990 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4991 to the start of the whole list and INNER points to the EXPR_LIST
4992 where earlier pieces cover PIECE_BITPOS bits. */
4994 static void
4995 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4996 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4997 HOST_WIDE_INT bitsize, rtx loc_note)
4999 int diff;
5000 bool copy = inner != NULL;
5002 if (copy)
5004 /* First copy all nodes preceding the current bitpos. */
5005 while (src != inner)
5007 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5008 decl_piece_bitsize (*src), NULL_RTX);
5009 dest = &XEXP (*dest, 1);
5010 src = &XEXP (*src, 1);
5013 /* Add padding if needed. */
5014 if (bitpos != piece_bitpos)
5016 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5017 copy ? NULL_RTX : *dest);
5018 dest = &XEXP (*dest, 1);
5020 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5022 gcc_assert (!copy);
5023 /* A piece with correct bitpos and bitsize already exist,
5024 just update the location for it and return. */
5025 *decl_piece_varloc_ptr (*dest) = loc_note;
5026 return;
5028 /* Add the piece that changed. */
5029 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5030 dest = &XEXP (*dest, 1);
5031 /* Skip over pieces that overlap it. */
5032 diff = bitpos - piece_bitpos + bitsize;
5033 if (!copy)
5034 src = dest;
5035 while (diff > 0 && *src)
5037 rtx piece = *src;
5038 diff -= decl_piece_bitsize (piece);
5039 if (copy)
5040 src = &XEXP (piece, 1);
5041 else
5043 *src = XEXP (piece, 1);
5044 free_EXPR_LIST_node (piece);
5047 /* Add padding if needed. */
5048 if (diff < 0 && *src)
5050 if (!copy)
5051 dest = src;
5052 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5053 dest = &XEXP (*dest, 1);
5055 if (!copy)
5056 return;
5057 /* Finally copy all nodes following it. */
5058 while (*src)
5060 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5061 decl_piece_bitsize (*src), NULL_RTX);
5062 dest = &XEXP (*dest, 1);
5063 src = &XEXP (*src, 1);
5067 /* Add a variable location node to the linked list for DECL. */
5069 static struct var_loc_node *
5070 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5072 unsigned int decl_id;
5073 var_loc_list *temp;
5074 void **slot;
5075 struct var_loc_node *loc = NULL;
5076 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5078 if (TREE_CODE (decl) == VAR_DECL
5079 && DECL_HAS_DEBUG_EXPR_P (decl))
5081 tree realdecl = DECL_DEBUG_EXPR (decl);
5082 if (handled_component_p (realdecl)
5083 || (TREE_CODE (realdecl) == MEM_REF
5084 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5086 HOST_WIDE_INT maxsize;
5087 tree innerdecl;
5088 innerdecl
5089 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5090 if (!DECL_P (innerdecl)
5091 || DECL_IGNORED_P (innerdecl)
5092 || TREE_STATIC (innerdecl)
5093 || bitsize <= 0
5094 || bitpos + bitsize > 256
5095 || bitsize != maxsize)
5096 return NULL;
5097 decl = innerdecl;
5101 decl_id = DECL_UID (decl);
5102 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5103 if (*slot == NULL)
5105 temp = ggc_alloc_cleared_var_loc_list ();
5106 temp->decl_id = decl_id;
5107 *slot = temp;
5109 else
5110 temp = (var_loc_list *) *slot;
5112 /* For PARM_DECLs try to keep around the original incoming value,
5113 even if that means we'll emit a zero-range .debug_loc entry. */
5114 if (temp->last
5115 && temp->first == temp->last
5116 && TREE_CODE (decl) == PARM_DECL
5117 && NOTE_P (temp->first->loc)
5118 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5119 && DECL_INCOMING_RTL (decl)
5120 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5121 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5122 == GET_CODE (DECL_INCOMING_RTL (decl))
5123 && prev_real_insn (temp->first->loc) == NULL_RTX
5124 && (bitsize != -1
5125 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5126 NOTE_VAR_LOCATION_LOC (loc_note))
5127 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5128 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5130 loc = ggc_alloc_cleared_var_loc_node ();
5131 temp->first->next = loc;
5132 temp->last = loc;
5133 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5135 else if (temp->last)
5137 struct var_loc_node *last = temp->last, *unused = NULL;
5138 rtx *piece_loc = NULL, last_loc_note;
5139 int piece_bitpos = 0;
5140 if (last->next)
5142 last = last->next;
5143 gcc_assert (last->next == NULL);
5145 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5147 piece_loc = &last->loc;
5150 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5151 if (piece_bitpos + cur_bitsize > bitpos)
5152 break;
5153 piece_bitpos += cur_bitsize;
5154 piece_loc = &XEXP (*piece_loc, 1);
5156 while (*piece_loc);
5158 /* TEMP->LAST here is either pointer to the last but one or
5159 last element in the chained list, LAST is pointer to the
5160 last element. */
5161 if (label && strcmp (last->label, label) == 0)
5163 /* For SRA optimized variables if there weren't any real
5164 insns since last note, just modify the last node. */
5165 if (piece_loc != NULL)
5167 adjust_piece_list (piece_loc, NULL, NULL,
5168 bitpos, piece_bitpos, bitsize, loc_note);
5169 return NULL;
5171 /* If the last note doesn't cover any instructions, remove it. */
5172 if (temp->last != last)
5174 temp->last->next = NULL;
5175 unused = last;
5176 last = temp->last;
5177 gcc_assert (strcmp (last->label, label) != 0);
5179 else
5181 gcc_assert (temp->first == temp->last
5182 || (temp->first->next == temp->last
5183 && TREE_CODE (decl) == PARM_DECL));
5184 memset (temp->last, '\0', sizeof (*temp->last));
5185 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5186 return temp->last;
5189 if (bitsize == -1 && NOTE_P (last->loc))
5190 last_loc_note = last->loc;
5191 else if (piece_loc != NULL
5192 && *piece_loc != NULL_RTX
5193 && piece_bitpos == bitpos
5194 && decl_piece_bitsize (*piece_loc) == bitsize)
5195 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5196 else
5197 last_loc_note = NULL_RTX;
5198 /* If the current location is the same as the end of the list,
5199 and either both or neither of the locations is uninitialized,
5200 we have nothing to do. */
5201 if (last_loc_note == NULL_RTX
5202 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5203 NOTE_VAR_LOCATION_LOC (loc_note)))
5204 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5205 != NOTE_VAR_LOCATION_STATUS (loc_note))
5206 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5207 == VAR_INIT_STATUS_UNINITIALIZED)
5208 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5209 == VAR_INIT_STATUS_UNINITIALIZED))))
5211 /* Add LOC to the end of list and update LAST. If the last
5212 element of the list has been removed above, reuse its
5213 memory for the new node, otherwise allocate a new one. */
5214 if (unused)
5216 loc = unused;
5217 memset (loc, '\0', sizeof (*loc));
5219 else
5220 loc = ggc_alloc_cleared_var_loc_node ();
5221 if (bitsize == -1 || piece_loc == NULL)
5222 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5223 else
5224 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5225 bitpos, piece_bitpos, bitsize, loc_note);
5226 last->next = loc;
5227 /* Ensure TEMP->LAST will point either to the new last but one
5228 element of the chain, or to the last element in it. */
5229 if (last != temp->last)
5230 temp->last = last;
5232 else if (unused)
5233 ggc_free (unused);
5235 else
5237 loc = ggc_alloc_cleared_var_loc_node ();
5238 temp->first = loc;
5239 temp->last = loc;
5240 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5242 return loc;
5245 /* Keep track of the number of spaces used to indent the
5246 output of the debugging routines that print the structure of
5247 the DIE internal representation. */
5248 static int print_indent;
5250 /* Indent the line the number of spaces given by print_indent. */
5252 static inline void
5253 print_spaces (FILE *outfile)
5255 fprintf (outfile, "%*s", print_indent, "");
5258 /* Print a type signature in hex. */
5260 static inline void
5261 print_signature (FILE *outfile, char *sig)
5263 int i;
5265 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5266 fprintf (outfile, "%02x", sig[i] & 0xff);
5269 /* Print the information associated with a given DIE, and its children.
5270 This routine is a debugging aid only. */
5272 static void
5273 print_die (dw_die_ref die, FILE *outfile)
5275 dw_attr_ref a;
5276 dw_die_ref c;
5277 unsigned ix;
5279 print_spaces (outfile);
5280 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5281 die->die_offset, dwarf_tag_name (die->die_tag),
5282 (void*) die);
5283 print_spaces (outfile);
5284 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5285 fprintf (outfile, " offset: %ld", die->die_offset);
5286 fprintf (outfile, " mark: %d\n", die->die_mark);
5288 if (die->comdat_type_p)
5290 print_spaces (outfile);
5291 fprintf (outfile, " signature: ");
5292 print_signature (outfile, die->die_id.die_type_node->signature);
5293 fprintf (outfile, "\n");
5296 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5298 print_spaces (outfile);
5299 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5301 switch (AT_class (a))
5303 case dw_val_class_addr:
5304 fprintf (outfile, "address");
5305 break;
5306 case dw_val_class_offset:
5307 fprintf (outfile, "offset");
5308 break;
5309 case dw_val_class_loc:
5310 fprintf (outfile, "location descriptor");
5311 break;
5312 case dw_val_class_loc_list:
5313 fprintf (outfile, "location list -> label:%s",
5314 AT_loc_list (a)->ll_symbol);
5315 break;
5316 case dw_val_class_range_list:
5317 fprintf (outfile, "range list");
5318 break;
5319 case dw_val_class_const:
5320 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5321 break;
5322 case dw_val_class_unsigned_const:
5323 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5324 break;
5325 case dw_val_class_const_double:
5326 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5327 HOST_WIDE_INT_PRINT_UNSIGNED")",
5328 a->dw_attr_val.v.val_double.high,
5329 a->dw_attr_val.v.val_double.low);
5330 break;
5331 case dw_val_class_vec:
5332 fprintf (outfile, "floating-point or vector constant");
5333 break;
5334 case dw_val_class_flag:
5335 fprintf (outfile, "%u", AT_flag (a));
5336 break;
5337 case dw_val_class_die_ref:
5338 if (AT_ref (a) != NULL)
5340 if (AT_ref (a)->comdat_type_p)
5342 fprintf (outfile, "die -> signature: ");
5343 print_signature (outfile,
5344 AT_ref (a)->die_id.die_type_node->signature);
5346 else if (AT_ref (a)->die_id.die_symbol)
5347 fprintf (outfile, "die -> label: %s",
5348 AT_ref (a)->die_id.die_symbol);
5349 else
5350 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5351 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5353 else
5354 fprintf (outfile, "die -> <null>");
5355 break;
5356 case dw_val_class_vms_delta:
5357 fprintf (outfile, "delta: @slotcount(%s-%s)",
5358 AT_vms_delta2 (a), AT_vms_delta1 (a));
5359 break;
5360 case dw_val_class_lbl_id:
5361 case dw_val_class_lineptr:
5362 case dw_val_class_macptr:
5363 case dw_val_class_high_pc:
5364 fprintf (outfile, "label: %s", AT_lbl (a));
5365 break;
5366 case dw_val_class_str:
5367 if (AT_string (a) != NULL)
5368 fprintf (outfile, "\"%s\"", AT_string (a));
5369 else
5370 fprintf (outfile, "<null>");
5371 break;
5372 case dw_val_class_file:
5373 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5374 AT_file (a)->emitted_number);
5375 break;
5376 case dw_val_class_data8:
5378 int i;
5380 for (i = 0; i < 8; i++)
5381 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5382 break;
5384 default:
5385 break;
5388 fprintf (outfile, "\n");
5391 if (die->die_child != NULL)
5393 print_indent += 4;
5394 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5395 print_indent -= 4;
5397 if (print_indent == 0)
5398 fprintf (outfile, "\n");
5401 /* Print the information collected for a given DIE. */
5403 DEBUG_FUNCTION void
5404 debug_dwarf_die (dw_die_ref die)
5406 print_die (die, stderr);
5409 DEBUG_FUNCTION void
5410 debug (die_struct &ref)
5412 print_die (&ref, stderr);
5415 DEBUG_FUNCTION void
5416 debug (die_struct *ptr)
5418 if (ptr)
5419 debug (*ptr);
5420 else
5421 fprintf (stderr, "<nil>\n");
5425 /* Print all DWARF information collected for the compilation unit.
5426 This routine is a debugging aid only. */
5428 DEBUG_FUNCTION void
5429 debug_dwarf (void)
5431 print_indent = 0;
5432 print_die (comp_unit_die (), stderr);
5435 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5436 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5437 DIE that marks the start of the DIEs for this include file. */
5439 static dw_die_ref
5440 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5442 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5443 dw_die_ref new_unit = gen_compile_unit_die (filename);
5445 new_unit->die_sib = old_unit;
5446 return new_unit;
5449 /* Close an include-file CU and reopen the enclosing one. */
5451 static dw_die_ref
5452 pop_compile_unit (dw_die_ref old_unit)
5454 dw_die_ref new_unit = old_unit->die_sib;
5456 old_unit->die_sib = NULL;
5457 return new_unit;
5460 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5461 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5462 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5464 /* Calculate the checksum of a location expression. */
5466 static inline void
5467 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5469 int tem;
5470 hashval_t hash = 0;
5472 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5473 CHECKSUM (tem);
5474 hash = hash_loc_operands (loc, hash);
5475 CHECKSUM (hash);
5478 /* Calculate the checksum of an attribute. */
5480 static void
5481 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5483 dw_loc_descr_ref loc;
5484 rtx r;
5486 CHECKSUM (at->dw_attr);
5488 /* We don't care that this was compiled with a different compiler
5489 snapshot; if the output is the same, that's what matters. */
5490 if (at->dw_attr == DW_AT_producer)
5491 return;
5493 switch (AT_class (at))
5495 case dw_val_class_const:
5496 CHECKSUM (at->dw_attr_val.v.val_int);
5497 break;
5498 case dw_val_class_unsigned_const:
5499 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5500 break;
5501 case dw_val_class_const_double:
5502 CHECKSUM (at->dw_attr_val.v.val_double);
5503 break;
5504 case dw_val_class_vec:
5505 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5506 (at->dw_attr_val.v.val_vec.length
5507 * at->dw_attr_val.v.val_vec.elt_size));
5508 break;
5509 case dw_val_class_flag:
5510 CHECKSUM (at->dw_attr_val.v.val_flag);
5511 break;
5512 case dw_val_class_str:
5513 CHECKSUM_STRING (AT_string (at));
5514 break;
5516 case dw_val_class_addr:
5517 r = AT_addr (at);
5518 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5519 CHECKSUM_STRING (XSTR (r, 0));
5520 break;
5522 case dw_val_class_offset:
5523 CHECKSUM (at->dw_attr_val.v.val_offset);
5524 break;
5526 case dw_val_class_loc:
5527 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5528 loc_checksum (loc, ctx);
5529 break;
5531 case dw_val_class_die_ref:
5532 die_checksum (AT_ref (at), ctx, mark);
5533 break;
5535 case dw_val_class_fde_ref:
5536 case dw_val_class_vms_delta:
5537 case dw_val_class_lbl_id:
5538 case dw_val_class_lineptr:
5539 case dw_val_class_macptr:
5540 case dw_val_class_high_pc:
5541 break;
5543 case dw_val_class_file:
5544 CHECKSUM_STRING (AT_file (at)->filename);
5545 break;
5547 case dw_val_class_data8:
5548 CHECKSUM (at->dw_attr_val.v.val_data8);
5549 break;
5551 default:
5552 break;
5556 /* Calculate the checksum of a DIE. */
5558 static void
5559 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5561 dw_die_ref c;
5562 dw_attr_ref a;
5563 unsigned ix;
5565 /* To avoid infinite recursion. */
5566 if (die->die_mark)
5568 CHECKSUM (die->die_mark);
5569 return;
5571 die->die_mark = ++(*mark);
5573 CHECKSUM (die->die_tag);
5575 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5576 attr_checksum (a, ctx, mark);
5578 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5581 #undef CHECKSUM
5582 #undef CHECKSUM_BLOCK
5583 #undef CHECKSUM_STRING
5585 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5586 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5587 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5588 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5589 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5590 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5591 #define CHECKSUM_ATTR(FOO) \
5592 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5594 /* Calculate the checksum of a number in signed LEB128 format. */
5596 static void
5597 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5599 unsigned char byte;
5600 bool more;
5602 while (1)
5604 byte = (value & 0x7f);
5605 value >>= 7;
5606 more = !((value == 0 && (byte & 0x40) == 0)
5607 || (value == -1 && (byte & 0x40) != 0));
5608 if (more)
5609 byte |= 0x80;
5610 CHECKSUM (byte);
5611 if (!more)
5612 break;
5616 /* Calculate the checksum of a number in unsigned LEB128 format. */
5618 static void
5619 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5621 while (1)
5623 unsigned char byte = (value & 0x7f);
5624 value >>= 7;
5625 if (value != 0)
5626 /* More bytes to follow. */
5627 byte |= 0x80;
5628 CHECKSUM (byte);
5629 if (value == 0)
5630 break;
5634 /* Checksum the context of the DIE. This adds the names of any
5635 surrounding namespaces or structures to the checksum. */
5637 static void
5638 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5640 const char *name;
5641 dw_die_ref spec;
5642 int tag = die->die_tag;
5644 if (tag != DW_TAG_namespace
5645 && tag != DW_TAG_structure_type
5646 && tag != DW_TAG_class_type)
5647 return;
5649 name = get_AT_string (die, DW_AT_name);
5651 spec = get_AT_ref (die, DW_AT_specification);
5652 if (spec != NULL)
5653 die = spec;
5655 if (die->die_parent != NULL)
5656 checksum_die_context (die->die_parent, ctx);
5658 CHECKSUM_ULEB128 ('C');
5659 CHECKSUM_ULEB128 (tag);
5660 if (name != NULL)
5661 CHECKSUM_STRING (name);
5664 /* Calculate the checksum of a location expression. */
5666 static inline void
5667 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5669 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5670 were emitted as a DW_FORM_sdata instead of a location expression. */
5671 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5673 CHECKSUM_ULEB128 (DW_FORM_sdata);
5674 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5675 return;
5678 /* Otherwise, just checksum the raw location expression. */
5679 while (loc != NULL)
5681 hashval_t hash = 0;
5683 CHECKSUM_ULEB128 (loc->dtprel);
5684 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5685 hash = hash_loc_operands (loc, hash);
5686 CHECKSUM (hash);
5687 loc = loc->dw_loc_next;
5691 /* Calculate the checksum of an attribute. */
5693 static void
5694 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5695 struct md5_ctx *ctx, int *mark)
5697 dw_loc_descr_ref loc;
5698 rtx r;
5700 if (AT_class (at) == dw_val_class_die_ref)
5702 dw_die_ref target_die = AT_ref (at);
5704 /* For pointer and reference types, we checksum only the (qualified)
5705 name of the target type (if there is a name). For friend entries,
5706 we checksum only the (qualified) name of the target type or function.
5707 This allows the checksum to remain the same whether the target type
5708 is complete or not. */
5709 if ((at->dw_attr == DW_AT_type
5710 && (tag == DW_TAG_pointer_type
5711 || tag == DW_TAG_reference_type
5712 || tag == DW_TAG_rvalue_reference_type
5713 || tag == DW_TAG_ptr_to_member_type))
5714 || (at->dw_attr == DW_AT_friend
5715 && tag == DW_TAG_friend))
5717 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5719 if (name_attr != NULL)
5721 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5723 if (decl == NULL)
5724 decl = target_die;
5725 CHECKSUM_ULEB128 ('N');
5726 CHECKSUM_ULEB128 (at->dw_attr);
5727 if (decl->die_parent != NULL)
5728 checksum_die_context (decl->die_parent, ctx);
5729 CHECKSUM_ULEB128 ('E');
5730 CHECKSUM_STRING (AT_string (name_attr));
5731 return;
5735 /* For all other references to another DIE, we check to see if the
5736 target DIE has already been visited. If it has, we emit a
5737 backward reference; if not, we descend recursively. */
5738 if (target_die->die_mark > 0)
5740 CHECKSUM_ULEB128 ('R');
5741 CHECKSUM_ULEB128 (at->dw_attr);
5742 CHECKSUM_ULEB128 (target_die->die_mark);
5744 else
5746 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5748 if (decl == NULL)
5749 decl = target_die;
5750 target_die->die_mark = ++(*mark);
5751 CHECKSUM_ULEB128 ('T');
5752 CHECKSUM_ULEB128 (at->dw_attr);
5753 if (decl->die_parent != NULL)
5754 checksum_die_context (decl->die_parent, ctx);
5755 die_checksum_ordered (target_die, ctx, mark);
5757 return;
5760 CHECKSUM_ULEB128 ('A');
5761 CHECKSUM_ULEB128 (at->dw_attr);
5763 switch (AT_class (at))
5765 case dw_val_class_const:
5766 CHECKSUM_ULEB128 (DW_FORM_sdata);
5767 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5768 break;
5770 case dw_val_class_unsigned_const:
5771 CHECKSUM_ULEB128 (DW_FORM_sdata);
5772 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5773 break;
5775 case dw_val_class_const_double:
5776 CHECKSUM_ULEB128 (DW_FORM_block);
5777 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5778 CHECKSUM (at->dw_attr_val.v.val_double);
5779 break;
5781 case dw_val_class_vec:
5782 CHECKSUM_ULEB128 (DW_FORM_block);
5783 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5784 * at->dw_attr_val.v.val_vec.elt_size);
5785 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5786 (at->dw_attr_val.v.val_vec.length
5787 * at->dw_attr_val.v.val_vec.elt_size));
5788 break;
5790 case dw_val_class_flag:
5791 CHECKSUM_ULEB128 (DW_FORM_flag);
5792 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5793 break;
5795 case dw_val_class_str:
5796 CHECKSUM_ULEB128 (DW_FORM_string);
5797 CHECKSUM_STRING (AT_string (at));
5798 break;
5800 case dw_val_class_addr:
5801 r = AT_addr (at);
5802 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5803 CHECKSUM_ULEB128 (DW_FORM_string);
5804 CHECKSUM_STRING (XSTR (r, 0));
5805 break;
5807 case dw_val_class_offset:
5808 CHECKSUM_ULEB128 (DW_FORM_sdata);
5809 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5810 break;
5812 case dw_val_class_loc:
5813 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5814 loc_checksum_ordered (loc, ctx);
5815 break;
5817 case dw_val_class_fde_ref:
5818 case dw_val_class_lbl_id:
5819 case dw_val_class_lineptr:
5820 case dw_val_class_macptr:
5821 case dw_val_class_high_pc:
5822 break;
5824 case dw_val_class_file:
5825 CHECKSUM_ULEB128 (DW_FORM_string);
5826 CHECKSUM_STRING (AT_file (at)->filename);
5827 break;
5829 case dw_val_class_data8:
5830 CHECKSUM (at->dw_attr_val.v.val_data8);
5831 break;
5833 default:
5834 break;
5838 struct checksum_attributes
5840 dw_attr_ref at_name;
5841 dw_attr_ref at_type;
5842 dw_attr_ref at_friend;
5843 dw_attr_ref at_accessibility;
5844 dw_attr_ref at_address_class;
5845 dw_attr_ref at_allocated;
5846 dw_attr_ref at_artificial;
5847 dw_attr_ref at_associated;
5848 dw_attr_ref at_binary_scale;
5849 dw_attr_ref at_bit_offset;
5850 dw_attr_ref at_bit_size;
5851 dw_attr_ref at_bit_stride;
5852 dw_attr_ref at_byte_size;
5853 dw_attr_ref at_byte_stride;
5854 dw_attr_ref at_const_value;
5855 dw_attr_ref at_containing_type;
5856 dw_attr_ref at_count;
5857 dw_attr_ref at_data_location;
5858 dw_attr_ref at_data_member_location;
5859 dw_attr_ref at_decimal_scale;
5860 dw_attr_ref at_decimal_sign;
5861 dw_attr_ref at_default_value;
5862 dw_attr_ref at_digit_count;
5863 dw_attr_ref at_discr;
5864 dw_attr_ref at_discr_list;
5865 dw_attr_ref at_discr_value;
5866 dw_attr_ref at_encoding;
5867 dw_attr_ref at_endianity;
5868 dw_attr_ref at_explicit;
5869 dw_attr_ref at_is_optional;
5870 dw_attr_ref at_location;
5871 dw_attr_ref at_lower_bound;
5872 dw_attr_ref at_mutable;
5873 dw_attr_ref at_ordering;
5874 dw_attr_ref at_picture_string;
5875 dw_attr_ref at_prototyped;
5876 dw_attr_ref at_small;
5877 dw_attr_ref at_segment;
5878 dw_attr_ref at_string_length;
5879 dw_attr_ref at_threads_scaled;
5880 dw_attr_ref at_upper_bound;
5881 dw_attr_ref at_use_location;
5882 dw_attr_ref at_use_UTF8;
5883 dw_attr_ref at_variable_parameter;
5884 dw_attr_ref at_virtuality;
5885 dw_attr_ref at_visibility;
5886 dw_attr_ref at_vtable_elem_location;
5889 /* Collect the attributes that we will want to use for the checksum. */
5891 static void
5892 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5894 dw_attr_ref a;
5895 unsigned ix;
5897 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5899 switch (a->dw_attr)
5901 case DW_AT_name:
5902 attrs->at_name = a;
5903 break;
5904 case DW_AT_type:
5905 attrs->at_type = a;
5906 break;
5907 case DW_AT_friend:
5908 attrs->at_friend = a;
5909 break;
5910 case DW_AT_accessibility:
5911 attrs->at_accessibility = a;
5912 break;
5913 case DW_AT_address_class:
5914 attrs->at_address_class = a;
5915 break;
5916 case DW_AT_allocated:
5917 attrs->at_allocated = a;
5918 break;
5919 case DW_AT_artificial:
5920 attrs->at_artificial = a;
5921 break;
5922 case DW_AT_associated:
5923 attrs->at_associated = a;
5924 break;
5925 case DW_AT_binary_scale:
5926 attrs->at_binary_scale = a;
5927 break;
5928 case DW_AT_bit_offset:
5929 attrs->at_bit_offset = a;
5930 break;
5931 case DW_AT_bit_size:
5932 attrs->at_bit_size = a;
5933 break;
5934 case DW_AT_bit_stride:
5935 attrs->at_bit_stride = a;
5936 break;
5937 case DW_AT_byte_size:
5938 attrs->at_byte_size = a;
5939 break;
5940 case DW_AT_byte_stride:
5941 attrs->at_byte_stride = a;
5942 break;
5943 case DW_AT_const_value:
5944 attrs->at_const_value = a;
5945 break;
5946 case DW_AT_containing_type:
5947 attrs->at_containing_type = a;
5948 break;
5949 case DW_AT_count:
5950 attrs->at_count = a;
5951 break;
5952 case DW_AT_data_location:
5953 attrs->at_data_location = a;
5954 break;
5955 case DW_AT_data_member_location:
5956 attrs->at_data_member_location = a;
5957 break;
5958 case DW_AT_decimal_scale:
5959 attrs->at_decimal_scale = a;
5960 break;
5961 case DW_AT_decimal_sign:
5962 attrs->at_decimal_sign = a;
5963 break;
5964 case DW_AT_default_value:
5965 attrs->at_default_value = a;
5966 break;
5967 case DW_AT_digit_count:
5968 attrs->at_digit_count = a;
5969 break;
5970 case DW_AT_discr:
5971 attrs->at_discr = a;
5972 break;
5973 case DW_AT_discr_list:
5974 attrs->at_discr_list = a;
5975 break;
5976 case DW_AT_discr_value:
5977 attrs->at_discr_value = a;
5978 break;
5979 case DW_AT_encoding:
5980 attrs->at_encoding = a;
5981 break;
5982 case DW_AT_endianity:
5983 attrs->at_endianity = a;
5984 break;
5985 case DW_AT_explicit:
5986 attrs->at_explicit = a;
5987 break;
5988 case DW_AT_is_optional:
5989 attrs->at_is_optional = a;
5990 break;
5991 case DW_AT_location:
5992 attrs->at_location = a;
5993 break;
5994 case DW_AT_lower_bound:
5995 attrs->at_lower_bound = a;
5996 break;
5997 case DW_AT_mutable:
5998 attrs->at_mutable = a;
5999 break;
6000 case DW_AT_ordering:
6001 attrs->at_ordering = a;
6002 break;
6003 case DW_AT_picture_string:
6004 attrs->at_picture_string = a;
6005 break;
6006 case DW_AT_prototyped:
6007 attrs->at_prototyped = a;
6008 break;
6009 case DW_AT_small:
6010 attrs->at_small = a;
6011 break;
6012 case DW_AT_segment:
6013 attrs->at_segment = a;
6014 break;
6015 case DW_AT_string_length:
6016 attrs->at_string_length = a;
6017 break;
6018 case DW_AT_threads_scaled:
6019 attrs->at_threads_scaled = a;
6020 break;
6021 case DW_AT_upper_bound:
6022 attrs->at_upper_bound = a;
6023 break;
6024 case DW_AT_use_location:
6025 attrs->at_use_location = a;
6026 break;
6027 case DW_AT_use_UTF8:
6028 attrs->at_use_UTF8 = a;
6029 break;
6030 case DW_AT_variable_parameter:
6031 attrs->at_variable_parameter = a;
6032 break;
6033 case DW_AT_virtuality:
6034 attrs->at_virtuality = a;
6035 break;
6036 case DW_AT_visibility:
6037 attrs->at_visibility = a;
6038 break;
6039 case DW_AT_vtable_elem_location:
6040 attrs->at_vtable_elem_location = a;
6041 break;
6042 default:
6043 break;
6048 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6050 static void
6051 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6053 dw_die_ref c;
6054 dw_die_ref decl;
6055 struct checksum_attributes attrs;
6057 CHECKSUM_ULEB128 ('D');
6058 CHECKSUM_ULEB128 (die->die_tag);
6060 memset (&attrs, 0, sizeof (attrs));
6062 decl = get_AT_ref (die, DW_AT_specification);
6063 if (decl != NULL)
6064 collect_checksum_attributes (&attrs, decl);
6065 collect_checksum_attributes (&attrs, die);
6067 CHECKSUM_ATTR (attrs.at_name);
6068 CHECKSUM_ATTR (attrs.at_accessibility);
6069 CHECKSUM_ATTR (attrs.at_address_class);
6070 CHECKSUM_ATTR (attrs.at_allocated);
6071 CHECKSUM_ATTR (attrs.at_artificial);
6072 CHECKSUM_ATTR (attrs.at_associated);
6073 CHECKSUM_ATTR (attrs.at_binary_scale);
6074 CHECKSUM_ATTR (attrs.at_bit_offset);
6075 CHECKSUM_ATTR (attrs.at_bit_size);
6076 CHECKSUM_ATTR (attrs.at_bit_stride);
6077 CHECKSUM_ATTR (attrs.at_byte_size);
6078 CHECKSUM_ATTR (attrs.at_byte_stride);
6079 CHECKSUM_ATTR (attrs.at_const_value);
6080 CHECKSUM_ATTR (attrs.at_containing_type);
6081 CHECKSUM_ATTR (attrs.at_count);
6082 CHECKSUM_ATTR (attrs.at_data_location);
6083 CHECKSUM_ATTR (attrs.at_data_member_location);
6084 CHECKSUM_ATTR (attrs.at_decimal_scale);
6085 CHECKSUM_ATTR (attrs.at_decimal_sign);
6086 CHECKSUM_ATTR (attrs.at_default_value);
6087 CHECKSUM_ATTR (attrs.at_digit_count);
6088 CHECKSUM_ATTR (attrs.at_discr);
6089 CHECKSUM_ATTR (attrs.at_discr_list);
6090 CHECKSUM_ATTR (attrs.at_discr_value);
6091 CHECKSUM_ATTR (attrs.at_encoding);
6092 CHECKSUM_ATTR (attrs.at_endianity);
6093 CHECKSUM_ATTR (attrs.at_explicit);
6094 CHECKSUM_ATTR (attrs.at_is_optional);
6095 CHECKSUM_ATTR (attrs.at_location);
6096 CHECKSUM_ATTR (attrs.at_lower_bound);
6097 CHECKSUM_ATTR (attrs.at_mutable);
6098 CHECKSUM_ATTR (attrs.at_ordering);
6099 CHECKSUM_ATTR (attrs.at_picture_string);
6100 CHECKSUM_ATTR (attrs.at_prototyped);
6101 CHECKSUM_ATTR (attrs.at_small);
6102 CHECKSUM_ATTR (attrs.at_segment);
6103 CHECKSUM_ATTR (attrs.at_string_length);
6104 CHECKSUM_ATTR (attrs.at_threads_scaled);
6105 CHECKSUM_ATTR (attrs.at_upper_bound);
6106 CHECKSUM_ATTR (attrs.at_use_location);
6107 CHECKSUM_ATTR (attrs.at_use_UTF8);
6108 CHECKSUM_ATTR (attrs.at_variable_parameter);
6109 CHECKSUM_ATTR (attrs.at_virtuality);
6110 CHECKSUM_ATTR (attrs.at_visibility);
6111 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6112 CHECKSUM_ATTR (attrs.at_type);
6113 CHECKSUM_ATTR (attrs.at_friend);
6115 /* Checksum the child DIEs. */
6116 c = die->die_child;
6117 if (c) do {
6118 dw_attr_ref name_attr;
6120 c = c->die_sib;
6121 name_attr = get_AT (c, DW_AT_name);
6122 if (is_template_instantiation (c))
6124 /* Ignore instantiations of member type and function templates. */
6126 else if (name_attr != NULL
6127 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6129 /* Use a shallow checksum for named nested types and member
6130 functions. */
6131 CHECKSUM_ULEB128 ('S');
6132 CHECKSUM_ULEB128 (c->die_tag);
6133 CHECKSUM_STRING (AT_string (name_attr));
6135 else
6137 /* Use a deep checksum for other children. */
6138 /* Mark this DIE so it gets processed when unmarking. */
6139 if (c->die_mark == 0)
6140 c->die_mark = -1;
6141 die_checksum_ordered (c, ctx, mark);
6143 } while (c != die->die_child);
6145 CHECKSUM_ULEB128 (0);
6148 /* Add a type name and tag to a hash. */
6149 static void
6150 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6152 CHECKSUM_ULEB128 (tag);
6153 CHECKSUM_STRING (name);
6156 #undef CHECKSUM
6157 #undef CHECKSUM_STRING
6158 #undef CHECKSUM_ATTR
6159 #undef CHECKSUM_LEB128
6160 #undef CHECKSUM_ULEB128
6162 /* Generate the type signature for DIE. This is computed by generating an
6163 MD5 checksum over the DIE's tag, its relevant attributes, and its
6164 children. Attributes that are references to other DIEs are processed
6165 by recursion, using the MARK field to prevent infinite recursion.
6166 If the DIE is nested inside a namespace or another type, we also
6167 need to include that context in the signature. The lower 64 bits
6168 of the resulting MD5 checksum comprise the signature. */
6170 static void
6171 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6173 int mark;
6174 const char *name;
6175 unsigned char checksum[16];
6176 struct md5_ctx ctx;
6177 dw_die_ref decl;
6178 dw_die_ref parent;
6180 name = get_AT_string (die, DW_AT_name);
6181 decl = get_AT_ref (die, DW_AT_specification);
6182 parent = get_die_parent (die);
6184 /* First, compute a signature for just the type name (and its surrounding
6185 context, if any. This is stored in the type unit DIE for link-time
6186 ODR (one-definition rule) checking. */
6188 if (is_cxx () && name != NULL)
6190 md5_init_ctx (&ctx);
6192 /* Checksum the names of surrounding namespaces and structures. */
6193 if (parent != NULL)
6194 checksum_die_context (parent, &ctx);
6196 /* Checksum the current DIE. */
6197 die_odr_checksum (die->die_tag, name, &ctx);
6198 md5_finish_ctx (&ctx, checksum);
6200 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6203 /* Next, compute the complete type signature. */
6205 md5_init_ctx (&ctx);
6206 mark = 1;
6207 die->die_mark = mark;
6209 /* Checksum the names of surrounding namespaces and structures. */
6210 if (parent != NULL)
6211 checksum_die_context (parent, &ctx);
6213 /* Checksum the DIE and its children. */
6214 die_checksum_ordered (die, &ctx, &mark);
6215 unmark_all_dies (die);
6216 md5_finish_ctx (&ctx, checksum);
6218 /* Store the signature in the type node and link the type DIE and the
6219 type node together. */
6220 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6221 DWARF_TYPE_SIGNATURE_SIZE);
6222 die->comdat_type_p = true;
6223 die->die_id.die_type_node = type_node;
6224 type_node->type_die = die;
6226 /* If the DIE is a specification, link its declaration to the type node
6227 as well. */
6228 if (decl != NULL)
6230 decl->comdat_type_p = true;
6231 decl->die_id.die_type_node = type_node;
6235 /* Do the location expressions look same? */
6236 static inline int
6237 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6239 return loc1->dw_loc_opc == loc2->dw_loc_opc
6240 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6241 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6244 /* Do the values look the same? */
6245 static int
6246 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6248 dw_loc_descr_ref loc1, loc2;
6249 rtx r1, r2;
6251 if (v1->val_class != v2->val_class)
6252 return 0;
6254 switch (v1->val_class)
6256 case dw_val_class_const:
6257 return v1->v.val_int == v2->v.val_int;
6258 case dw_val_class_unsigned_const:
6259 return v1->v.val_unsigned == v2->v.val_unsigned;
6260 case dw_val_class_const_double:
6261 return v1->v.val_double.high == v2->v.val_double.high
6262 && v1->v.val_double.low == v2->v.val_double.low;
6263 case dw_val_class_vec:
6264 if (v1->v.val_vec.length != v2->v.val_vec.length
6265 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6266 return 0;
6267 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6268 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6269 return 0;
6270 return 1;
6271 case dw_val_class_flag:
6272 return v1->v.val_flag == v2->v.val_flag;
6273 case dw_val_class_str:
6274 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6276 case dw_val_class_addr:
6277 r1 = v1->v.val_addr;
6278 r2 = v2->v.val_addr;
6279 if (GET_CODE (r1) != GET_CODE (r2))
6280 return 0;
6281 return !rtx_equal_p (r1, r2);
6283 case dw_val_class_offset:
6284 return v1->v.val_offset == v2->v.val_offset;
6286 case dw_val_class_loc:
6287 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6288 loc1 && loc2;
6289 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6290 if (!same_loc_p (loc1, loc2, mark))
6291 return 0;
6292 return !loc1 && !loc2;
6294 case dw_val_class_die_ref:
6295 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6297 case dw_val_class_fde_ref:
6298 case dw_val_class_vms_delta:
6299 case dw_val_class_lbl_id:
6300 case dw_val_class_lineptr:
6301 case dw_val_class_macptr:
6302 case dw_val_class_high_pc:
6303 return 1;
6305 case dw_val_class_file:
6306 return v1->v.val_file == v2->v.val_file;
6308 case dw_val_class_data8:
6309 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6311 default:
6312 return 1;
6316 /* Do the attributes look the same? */
6318 static int
6319 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6321 if (at1->dw_attr != at2->dw_attr)
6322 return 0;
6324 /* We don't care that this was compiled with a different compiler
6325 snapshot; if the output is the same, that's what matters. */
6326 if (at1->dw_attr == DW_AT_producer)
6327 return 1;
6329 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6332 /* Do the dies look the same? */
6334 static int
6335 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6337 dw_die_ref c1, c2;
6338 dw_attr_ref a1;
6339 unsigned ix;
6341 /* To avoid infinite recursion. */
6342 if (die1->die_mark)
6343 return die1->die_mark == die2->die_mark;
6344 die1->die_mark = die2->die_mark = ++(*mark);
6346 if (die1->die_tag != die2->die_tag)
6347 return 0;
6349 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6350 return 0;
6352 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6353 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6354 return 0;
6356 c1 = die1->die_child;
6357 c2 = die2->die_child;
6358 if (! c1)
6360 if (c2)
6361 return 0;
6363 else
6364 for (;;)
6366 if (!same_die_p (c1, c2, mark))
6367 return 0;
6368 c1 = c1->die_sib;
6369 c2 = c2->die_sib;
6370 if (c1 == die1->die_child)
6372 if (c2 == die2->die_child)
6373 break;
6374 else
6375 return 0;
6379 return 1;
6382 /* Do the dies look the same? Wrapper around same_die_p. */
6384 static int
6385 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6387 int mark = 0;
6388 int ret = same_die_p (die1, die2, &mark);
6390 unmark_all_dies (die1);
6391 unmark_all_dies (die2);
6393 return ret;
6396 /* The prefix to attach to symbols on DIEs in the current comdat debug
6397 info section. */
6398 static const char *comdat_symbol_id;
6400 /* The index of the current symbol within the current comdat CU. */
6401 static unsigned int comdat_symbol_number;
6403 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6404 children, and set comdat_symbol_id accordingly. */
6406 static void
6407 compute_section_prefix (dw_die_ref unit_die)
6409 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6410 const char *base = die_name ? lbasename (die_name) : "anonymous";
6411 char *name = XALLOCAVEC (char, strlen (base) + 64);
6412 char *p;
6413 int i, mark;
6414 unsigned char checksum[16];
6415 struct md5_ctx ctx;
6417 /* Compute the checksum of the DIE, then append part of it as hex digits to
6418 the name filename of the unit. */
6420 md5_init_ctx (&ctx);
6421 mark = 0;
6422 die_checksum (unit_die, &ctx, &mark);
6423 unmark_all_dies (unit_die);
6424 md5_finish_ctx (&ctx, checksum);
6426 sprintf (name, "%s.", base);
6427 clean_symbol_name (name);
6429 p = name + strlen (name);
6430 for (i = 0; i < 4; i++)
6432 sprintf (p, "%.2x", checksum[i]);
6433 p += 2;
6436 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6437 comdat_symbol_number = 0;
6440 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6442 static int
6443 is_type_die (dw_die_ref die)
6445 switch (die->die_tag)
6447 case DW_TAG_array_type:
6448 case DW_TAG_class_type:
6449 case DW_TAG_interface_type:
6450 case DW_TAG_enumeration_type:
6451 case DW_TAG_pointer_type:
6452 case DW_TAG_reference_type:
6453 case DW_TAG_rvalue_reference_type:
6454 case DW_TAG_string_type:
6455 case DW_TAG_structure_type:
6456 case DW_TAG_subroutine_type:
6457 case DW_TAG_union_type:
6458 case DW_TAG_ptr_to_member_type:
6459 case DW_TAG_set_type:
6460 case DW_TAG_subrange_type:
6461 case DW_TAG_base_type:
6462 case DW_TAG_const_type:
6463 case DW_TAG_file_type:
6464 case DW_TAG_packed_type:
6465 case DW_TAG_volatile_type:
6466 case DW_TAG_typedef:
6467 return 1;
6468 default:
6469 return 0;
6473 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6474 Basically, we want to choose the bits that are likely to be shared between
6475 compilations (types) and leave out the bits that are specific to individual
6476 compilations (functions). */
6478 static int
6479 is_comdat_die (dw_die_ref c)
6481 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6482 we do for stabs. The advantage is a greater likelihood of sharing between
6483 objects that don't include headers in the same order (and therefore would
6484 put the base types in a different comdat). jason 8/28/00 */
6486 if (c->die_tag == DW_TAG_base_type)
6487 return 0;
6489 if (c->die_tag == DW_TAG_pointer_type
6490 || c->die_tag == DW_TAG_reference_type
6491 || c->die_tag == DW_TAG_rvalue_reference_type
6492 || c->die_tag == DW_TAG_const_type
6493 || c->die_tag == DW_TAG_volatile_type)
6495 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6497 return t ? is_comdat_die (t) : 0;
6500 return is_type_die (c);
6503 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6504 compilation unit. */
6506 static int
6507 is_symbol_die (dw_die_ref c)
6509 return (is_type_die (c)
6510 || is_declaration_die (c)
6511 || c->die_tag == DW_TAG_namespace
6512 || c->die_tag == DW_TAG_module);
6515 /* Returns true iff C is a compile-unit DIE. */
6517 static inline bool
6518 is_cu_die (dw_die_ref c)
6520 return c && c->die_tag == DW_TAG_compile_unit;
6523 /* Returns true iff C is a unit DIE of some sort. */
6525 static inline bool
6526 is_unit_die (dw_die_ref c)
6528 return c && (c->die_tag == DW_TAG_compile_unit
6529 || c->die_tag == DW_TAG_partial_unit
6530 || c->die_tag == DW_TAG_type_unit);
6533 /* Returns true iff C is a namespace DIE. */
6535 static inline bool
6536 is_namespace_die (dw_die_ref c)
6538 return c && c->die_tag == DW_TAG_namespace;
6541 /* Returns true iff C is a class or structure DIE. */
6543 static inline bool
6544 is_class_die (dw_die_ref c)
6546 return c && (c->die_tag == DW_TAG_class_type
6547 || c->die_tag == DW_TAG_structure_type);
6550 /* Return non-zero if this DIE is a template parameter. */
6552 static inline bool
6553 is_template_parameter (dw_die_ref die)
6555 switch (die->die_tag)
6557 case DW_TAG_template_type_param:
6558 case DW_TAG_template_value_param:
6559 case DW_TAG_GNU_template_template_param:
6560 case DW_TAG_GNU_template_parameter_pack:
6561 return true;
6562 default:
6563 return false;
6567 /* Return non-zero if this DIE represents a template instantiation. */
6569 static inline bool
6570 is_template_instantiation (dw_die_ref die)
6572 dw_die_ref c;
6574 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6575 return false;
6576 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6577 return false;
6580 static char *
6581 gen_internal_sym (const char *prefix)
6583 char buf[256];
6585 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6586 return xstrdup (buf);
6589 /* Assign symbols to all worthy DIEs under DIE. */
6591 static void
6592 assign_symbol_names (dw_die_ref die)
6594 dw_die_ref c;
6596 if (is_symbol_die (die) && !die->comdat_type_p)
6598 if (comdat_symbol_id)
6600 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6602 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6603 comdat_symbol_id, comdat_symbol_number++);
6604 die->die_id.die_symbol = xstrdup (p);
6606 else
6607 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6610 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6613 struct cu_hash_table_entry
6615 dw_die_ref cu;
6616 unsigned min_comdat_num, max_comdat_num;
6617 struct cu_hash_table_entry *next;
6620 /* Helpers to manipulate hash table of CUs. */
6622 struct cu_hash_table_entry_hasher
6624 typedef cu_hash_table_entry value_type;
6625 typedef die_struct compare_type;
6626 static inline hashval_t hash (const value_type *);
6627 static inline bool equal (const value_type *, const compare_type *);
6628 static inline void remove (value_type *);
6631 inline hashval_t
6632 cu_hash_table_entry_hasher::hash (const value_type *entry)
6634 return htab_hash_string (entry->cu->die_id.die_symbol);
6637 inline bool
6638 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6639 const compare_type *entry2)
6641 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6644 inline void
6645 cu_hash_table_entry_hasher::remove (value_type *entry)
6647 struct cu_hash_table_entry *next;
6649 while (entry)
6651 next = entry->next;
6652 free (entry);
6653 entry = next;
6657 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6659 /* Check whether we have already seen this CU and set up SYM_NUM
6660 accordingly. */
6661 static int
6662 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6664 struct cu_hash_table_entry dummy;
6665 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6667 dummy.max_comdat_num = 0;
6669 slot = htable.find_slot_with_hash (cu,
6670 htab_hash_string (cu->die_id.die_symbol),
6671 INSERT);
6672 entry = *slot;
6674 for (; entry; last = entry, entry = entry->next)
6676 if (same_die_p_wrap (cu, entry->cu))
6677 break;
6680 if (entry)
6682 *sym_num = entry->min_comdat_num;
6683 return 1;
6686 entry = XCNEW (struct cu_hash_table_entry);
6687 entry->cu = cu;
6688 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6689 entry->next = *slot;
6690 *slot = entry;
6692 return 0;
6695 /* Record SYM_NUM to record of CU in HTABLE. */
6696 static void
6697 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6698 unsigned int sym_num)
6700 struct cu_hash_table_entry **slot, *entry;
6702 slot = htable.find_slot_with_hash (cu,
6703 htab_hash_string (cu->die_id.die_symbol),
6704 NO_INSERT);
6705 entry = *slot;
6707 entry->max_comdat_num = sym_num;
6710 /* Traverse the DIE (which is always comp_unit_die), and set up
6711 additional compilation units for each of the include files we see
6712 bracketed by BINCL/EINCL. */
6714 static void
6715 break_out_includes (dw_die_ref die)
6717 dw_die_ref c;
6718 dw_die_ref unit = NULL;
6719 limbo_die_node *node, **pnode;
6720 cu_hash_type cu_hash_table;
6722 c = die->die_child;
6723 if (c) do {
6724 dw_die_ref prev = c;
6725 c = c->die_sib;
6726 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6727 || (unit && is_comdat_die (c)))
6729 dw_die_ref next = c->die_sib;
6731 /* This DIE is for a secondary CU; remove it from the main one. */
6732 remove_child_with_prev (c, prev);
6734 if (c->die_tag == DW_TAG_GNU_BINCL)
6735 unit = push_new_compile_unit (unit, c);
6736 else if (c->die_tag == DW_TAG_GNU_EINCL)
6737 unit = pop_compile_unit (unit);
6738 else
6739 add_child_die (unit, c);
6740 c = next;
6741 if (c == die->die_child)
6742 break;
6744 } while (c != die->die_child);
6746 #if 0
6747 /* We can only use this in debugging, since the frontend doesn't check
6748 to make sure that we leave every include file we enter. */
6749 gcc_assert (!unit);
6750 #endif
6752 assign_symbol_names (die);
6753 cu_hash_table.create (10);
6754 for (node = limbo_die_list, pnode = &limbo_die_list;
6755 node;
6756 node = node->next)
6758 int is_dupl;
6760 compute_section_prefix (node->die);
6761 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6762 &comdat_symbol_number);
6763 assign_symbol_names (node->die);
6764 if (is_dupl)
6765 *pnode = node->next;
6766 else
6768 pnode = &node->next;
6769 record_comdat_symbol_number (node->die, cu_hash_table,
6770 comdat_symbol_number);
6773 cu_hash_table.dispose ();
6776 /* Return non-zero if this DIE is a declaration. */
6778 static int
6779 is_declaration_die (dw_die_ref die)
6781 dw_attr_ref a;
6782 unsigned ix;
6784 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6785 if (a->dw_attr == DW_AT_declaration)
6786 return 1;
6788 return 0;
6791 /* Return non-zero if this DIE is nested inside a subprogram. */
6793 static int
6794 is_nested_in_subprogram (dw_die_ref die)
6796 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6798 if (decl == NULL)
6799 decl = die;
6800 return local_scope_p (decl);
6803 /* Return non-zero if this DIE contains a defining declaration of a
6804 subprogram. */
6806 static int
6807 contains_subprogram_definition (dw_die_ref die)
6809 dw_die_ref c;
6811 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6812 return 1;
6813 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6814 return 0;
6817 /* Return non-zero if this is a type DIE that should be moved to a
6818 COMDAT .debug_types section. */
6820 static int
6821 should_move_die_to_comdat (dw_die_ref die)
6823 switch (die->die_tag)
6825 case DW_TAG_class_type:
6826 case DW_TAG_structure_type:
6827 case DW_TAG_enumeration_type:
6828 case DW_TAG_union_type:
6829 /* Don't move declarations, inlined instances, or types nested in a
6830 subprogram. */
6831 if (is_declaration_die (die)
6832 || get_AT (die, DW_AT_abstract_origin)
6833 || is_nested_in_subprogram (die))
6834 return 0;
6835 /* A type definition should never contain a subprogram definition. */
6836 gcc_assert (!contains_subprogram_definition (die));
6837 return 1;
6838 case DW_TAG_array_type:
6839 case DW_TAG_interface_type:
6840 case DW_TAG_pointer_type:
6841 case DW_TAG_reference_type:
6842 case DW_TAG_rvalue_reference_type:
6843 case DW_TAG_string_type:
6844 case DW_TAG_subroutine_type:
6845 case DW_TAG_ptr_to_member_type:
6846 case DW_TAG_set_type:
6847 case DW_TAG_subrange_type:
6848 case DW_TAG_base_type:
6849 case DW_TAG_const_type:
6850 case DW_TAG_file_type:
6851 case DW_TAG_packed_type:
6852 case DW_TAG_volatile_type:
6853 case DW_TAG_typedef:
6854 default:
6855 return 0;
6859 /* Make a clone of DIE. */
6861 static dw_die_ref
6862 clone_die (dw_die_ref die)
6864 dw_die_ref clone;
6865 dw_attr_ref a;
6866 unsigned ix;
6868 clone = ggc_alloc_cleared_die_node ();
6869 clone->die_tag = die->die_tag;
6871 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6872 add_dwarf_attr (clone, a);
6874 return clone;
6877 /* Make a clone of the tree rooted at DIE. */
6879 static dw_die_ref
6880 clone_tree (dw_die_ref die)
6882 dw_die_ref c;
6883 dw_die_ref clone = clone_die (die);
6885 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6887 return clone;
6890 /* Make a clone of DIE as a declaration. */
6892 static dw_die_ref
6893 clone_as_declaration (dw_die_ref die)
6895 dw_die_ref clone;
6896 dw_die_ref decl;
6897 dw_attr_ref a;
6898 unsigned ix;
6900 /* If the DIE is already a declaration, just clone it. */
6901 if (is_declaration_die (die))
6902 return clone_die (die);
6904 /* If the DIE is a specification, just clone its declaration DIE. */
6905 decl = get_AT_ref (die, DW_AT_specification);
6906 if (decl != NULL)
6908 clone = clone_die (decl);
6909 if (die->comdat_type_p)
6910 add_AT_die_ref (clone, DW_AT_signature, die);
6911 return clone;
6914 clone = ggc_alloc_cleared_die_node ();
6915 clone->die_tag = die->die_tag;
6917 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6919 /* We don't want to copy over all attributes.
6920 For example we don't want DW_AT_byte_size because otherwise we will no
6921 longer have a declaration and GDB will treat it as a definition. */
6923 switch (a->dw_attr)
6925 case DW_AT_artificial:
6926 case DW_AT_containing_type:
6927 case DW_AT_external:
6928 case DW_AT_name:
6929 case DW_AT_type:
6930 case DW_AT_virtuality:
6931 case DW_AT_linkage_name:
6932 case DW_AT_MIPS_linkage_name:
6933 add_dwarf_attr (clone, a);
6934 break;
6935 case DW_AT_byte_size:
6936 default:
6937 break;
6941 if (die->comdat_type_p)
6942 add_AT_die_ref (clone, DW_AT_signature, die);
6944 add_AT_flag (clone, DW_AT_declaration, 1);
6945 return clone;
6949 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6951 struct decl_table_entry
6953 dw_die_ref orig;
6954 dw_die_ref copy;
6957 /* Helpers to manipulate hash table of copied declarations. */
6959 /* Hashtable helpers. */
6961 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6963 typedef decl_table_entry value_type;
6964 typedef die_struct compare_type;
6965 static inline hashval_t hash (const value_type *);
6966 static inline bool equal (const value_type *, const compare_type *);
6969 inline hashval_t
6970 decl_table_entry_hasher::hash (const value_type *entry)
6972 return htab_hash_pointer (entry->orig);
6975 inline bool
6976 decl_table_entry_hasher::equal (const value_type *entry1,
6977 const compare_type *entry2)
6979 return entry1->orig == entry2;
6982 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6984 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6985 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6986 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6987 to check if the ancestor has already been copied into UNIT. */
6989 static dw_die_ref
6990 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6992 dw_die_ref parent = die->die_parent;
6993 dw_die_ref new_parent = unit;
6994 dw_die_ref copy;
6995 decl_table_entry **slot = NULL;
6996 struct decl_table_entry *entry = NULL;
6998 if (decl_table.is_created ())
7000 /* Check if the entry has already been copied to UNIT. */
7001 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7002 INSERT);
7003 if (*slot != HTAB_EMPTY_ENTRY)
7005 entry = *slot;
7006 return entry->copy;
7009 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7010 entry = XCNEW (struct decl_table_entry);
7011 entry->orig = die;
7012 entry->copy = NULL;
7013 *slot = entry;
7016 if (parent != NULL)
7018 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7019 if (spec != NULL)
7020 parent = spec;
7021 if (!is_unit_die (parent))
7022 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7025 copy = clone_as_declaration (die);
7026 add_child_die (new_parent, copy);
7028 if (decl_table.is_created ())
7030 /* Record the pointer to the copy. */
7031 entry->copy = copy;
7034 return copy;
7036 /* Copy the declaration context to the new type unit DIE. This includes
7037 any surrounding namespace or type declarations. If the DIE has an
7038 AT_specification attribute, it also includes attributes and children
7039 attached to the specification, and returns a pointer to the original
7040 parent of the declaration DIE. Returns NULL otherwise. */
7042 static dw_die_ref
7043 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7045 dw_die_ref decl;
7046 dw_die_ref new_decl;
7047 dw_die_ref orig_parent = NULL;
7049 decl = get_AT_ref (die, DW_AT_specification);
7050 if (decl == NULL)
7051 decl = die;
7052 else
7054 unsigned ix;
7055 dw_die_ref c;
7056 dw_attr_ref a;
7058 /* The original DIE will be changed to a declaration, and must
7059 be moved to be a child of the original declaration DIE. */
7060 orig_parent = decl->die_parent;
7062 /* Copy the type node pointer from the new DIE to the original
7063 declaration DIE so we can forward references later. */
7064 decl->comdat_type_p = true;
7065 decl->die_id.die_type_node = die->die_id.die_type_node;
7067 remove_AT (die, DW_AT_specification);
7069 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7071 if (a->dw_attr != DW_AT_name
7072 && a->dw_attr != DW_AT_declaration
7073 && a->dw_attr != DW_AT_external)
7074 add_dwarf_attr (die, a);
7077 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7080 if (decl->die_parent != NULL
7081 && !is_unit_die (decl->die_parent))
7083 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7084 if (new_decl != NULL)
7086 remove_AT (new_decl, DW_AT_signature);
7087 add_AT_specification (die, new_decl);
7091 return orig_parent;
7094 /* Generate the skeleton ancestor tree for the given NODE, then clone
7095 the DIE and add the clone into the tree. */
7097 static void
7098 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7100 if (node->new_die != NULL)
7101 return;
7103 node->new_die = clone_as_declaration (node->old_die);
7105 if (node->parent != NULL)
7107 generate_skeleton_ancestor_tree (node->parent);
7108 add_child_die (node->parent->new_die, node->new_die);
7112 /* Generate a skeleton tree of DIEs containing any declarations that are
7113 found in the original tree. We traverse the tree looking for declaration
7114 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7116 static void
7117 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7119 skeleton_chain_node node;
7120 dw_die_ref c;
7121 dw_die_ref first;
7122 dw_die_ref prev = NULL;
7123 dw_die_ref next = NULL;
7125 node.parent = parent;
7127 first = c = parent->old_die->die_child;
7128 if (c)
7129 next = c->die_sib;
7130 if (c) do {
7131 if (prev == NULL || prev->die_sib == c)
7132 prev = c;
7133 c = next;
7134 next = (c == first ? NULL : c->die_sib);
7135 node.old_die = c;
7136 node.new_die = NULL;
7137 if (is_declaration_die (c))
7139 if (is_template_instantiation (c))
7141 /* Instantiated templates do not need to be cloned into the
7142 type unit. Just move the DIE and its children back to
7143 the skeleton tree (in the main CU). */
7144 remove_child_with_prev (c, prev);
7145 add_child_die (parent->new_die, c);
7146 c = prev;
7148 else
7150 /* Clone the existing DIE, move the original to the skeleton
7151 tree (which is in the main CU), and put the clone, with
7152 all the original's children, where the original came from
7153 (which is about to be moved to the type unit). */
7154 dw_die_ref clone = clone_die (c);
7155 move_all_children (c, clone);
7157 replace_child (c, clone, prev);
7158 generate_skeleton_ancestor_tree (parent);
7159 add_child_die (parent->new_die, c);
7160 node.new_die = c;
7161 c = clone;
7164 generate_skeleton_bottom_up (&node);
7165 } while (next != NULL);
7168 /* Wrapper function for generate_skeleton_bottom_up. */
7170 static dw_die_ref
7171 generate_skeleton (dw_die_ref die)
7173 skeleton_chain_node node;
7175 node.old_die = die;
7176 node.new_die = NULL;
7177 node.parent = NULL;
7179 /* If this type definition is nested inside another type,
7180 and is not an instantiation of a template, always leave
7181 at least a declaration in its place. */
7182 if (die->die_parent != NULL
7183 && is_type_die (die->die_parent)
7184 && !is_template_instantiation (die))
7185 node.new_die = clone_as_declaration (die);
7187 generate_skeleton_bottom_up (&node);
7188 return node.new_die;
7191 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7192 declaration. The original DIE is moved to a new compile unit so that
7193 existing references to it follow it to the new location. If any of the
7194 original DIE's descendants is a declaration, we need to replace the
7195 original DIE with a skeleton tree and move the declarations back into the
7196 skeleton tree. */
7198 static dw_die_ref
7199 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7200 dw_die_ref prev)
7202 dw_die_ref skeleton, orig_parent;
7204 /* Copy the declaration context to the type unit DIE. If the returned
7205 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7206 that DIE. */
7207 orig_parent = copy_declaration_context (unit, child);
7209 skeleton = generate_skeleton (child);
7210 if (skeleton == NULL)
7211 remove_child_with_prev (child, prev);
7212 else
7214 skeleton->comdat_type_p = true;
7215 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7217 /* If the original DIE was a specification, we need to put
7218 the skeleton under the parent DIE of the declaration.
7219 This leaves the original declaration in the tree, but
7220 it will be pruned later since there are no longer any
7221 references to it. */
7222 if (orig_parent != NULL)
7224 remove_child_with_prev (child, prev);
7225 add_child_die (orig_parent, skeleton);
7227 else
7228 replace_child (child, skeleton, prev);
7231 return skeleton;
7234 /* Traverse the DIE and set up additional .debug_types sections for each
7235 type worthy of being placed in a COMDAT section. */
7237 static void
7238 break_out_comdat_types (dw_die_ref die)
7240 dw_die_ref c;
7241 dw_die_ref first;
7242 dw_die_ref prev = NULL;
7243 dw_die_ref next = NULL;
7244 dw_die_ref unit = NULL;
7246 first = c = die->die_child;
7247 if (c)
7248 next = c->die_sib;
7249 if (c) do {
7250 if (prev == NULL || prev->die_sib == c)
7251 prev = c;
7252 c = next;
7253 next = (c == first ? NULL : c->die_sib);
7254 if (should_move_die_to_comdat (c))
7256 dw_die_ref replacement;
7257 comdat_type_node_ref type_node;
7259 /* Break out nested types into their own type units. */
7260 break_out_comdat_types (c);
7262 /* Create a new type unit DIE as the root for the new tree, and
7263 add it to the list of comdat types. */
7264 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7265 add_AT_unsigned (unit, DW_AT_language,
7266 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7267 type_node = ggc_alloc_cleared_comdat_type_node ();
7268 type_node->root_die = unit;
7269 type_node->next = comdat_type_list;
7270 comdat_type_list = type_node;
7272 /* Generate the type signature. */
7273 generate_type_signature (c, type_node);
7275 /* Copy the declaration context, attributes, and children of the
7276 declaration into the new type unit DIE, then remove this DIE
7277 from the main CU (or replace it with a skeleton if necessary). */
7278 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7279 type_node->skeleton_die = replacement;
7281 /* Add the DIE to the new compunit. */
7282 add_child_die (unit, c);
7284 if (replacement != NULL)
7285 c = replacement;
7287 else if (c->die_tag == DW_TAG_namespace
7288 || c->die_tag == DW_TAG_class_type
7289 || c->die_tag == DW_TAG_structure_type
7290 || c->die_tag == DW_TAG_union_type)
7292 /* Look for nested types that can be broken out. */
7293 break_out_comdat_types (c);
7295 } while (next != NULL);
7298 /* Like clone_tree, but additionally enter all the children into
7299 the hash table decl_table. */
7301 static dw_die_ref
7302 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7304 dw_die_ref c;
7305 dw_die_ref clone = clone_die (die);
7306 struct decl_table_entry *entry;
7307 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7308 htab_hash_pointer (die), INSERT);
7309 /* Assert that DIE isn't in the hash table yet. If it would be there
7310 before, the ancestors would be necessarily there as well, therefore
7311 clone_tree_hash wouldn't be called. */
7312 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7313 entry = XCNEW (struct decl_table_entry);
7314 entry->orig = die;
7315 entry->copy = clone;
7316 *slot = entry;
7318 FOR_EACH_CHILD (die, c,
7319 add_child_die (clone, clone_tree_hash (c, decl_table)));
7321 return clone;
7324 /* Walk the DIE and its children, looking for references to incomplete
7325 or trivial types that are unmarked (i.e., that are not in the current
7326 type_unit). */
7328 static void
7329 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7331 dw_die_ref c;
7332 dw_attr_ref a;
7333 unsigned ix;
7335 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7337 if (AT_class (a) == dw_val_class_die_ref)
7339 dw_die_ref targ = AT_ref (a);
7340 decl_table_entry **slot;
7341 struct decl_table_entry *entry;
7343 if (targ->die_mark != 0 || targ->comdat_type_p)
7344 continue;
7346 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7347 INSERT);
7349 if (*slot != HTAB_EMPTY_ENTRY)
7351 /* TARG has already been copied, so we just need to
7352 modify the reference to point to the copy. */
7353 entry = *slot;
7354 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7356 else
7358 dw_die_ref parent = unit;
7359 dw_die_ref copy = clone_die (targ);
7361 /* Record in DECL_TABLE that TARG has been copied.
7362 Need to do this now, before the recursive call,
7363 because DECL_TABLE may be expanded and SLOT
7364 would no longer be a valid pointer. */
7365 entry = XCNEW (struct decl_table_entry);
7366 entry->orig = targ;
7367 entry->copy = copy;
7368 *slot = entry;
7370 FOR_EACH_CHILD (targ, c,
7371 add_child_die (copy,
7372 clone_tree_hash (c, decl_table)));
7374 /* Make sure the cloned tree is marked as part of the
7375 type unit. */
7376 mark_dies (copy);
7378 /* If TARG has surrounding context, copy its ancestor tree
7379 into the new type unit. */
7380 if (targ->die_parent != NULL
7381 && !is_unit_die (targ->die_parent))
7382 parent = copy_ancestor_tree (unit, targ->die_parent,
7383 decl_table);
7385 add_child_die (parent, copy);
7386 a->dw_attr_val.v.val_die_ref.die = copy;
7388 /* Make sure the newly-copied DIE is walked. If it was
7389 installed in a previously-added context, it won't
7390 get visited otherwise. */
7391 if (parent != unit)
7393 /* Find the highest point of the newly-added tree,
7394 mark each node along the way, and walk from there. */
7395 parent->die_mark = 1;
7396 while (parent->die_parent
7397 && parent->die_parent->die_mark == 0)
7399 parent = parent->die_parent;
7400 parent->die_mark = 1;
7402 copy_decls_walk (unit, parent, decl_table);
7408 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7411 /* Copy declarations for "unworthy" types into the new comdat section.
7412 Incomplete types, modified types, and certain other types aren't broken
7413 out into comdat sections of their own, so they don't have a signature,
7414 and we need to copy the declaration into the same section so that we
7415 don't have an external reference. */
7417 static void
7418 copy_decls_for_unworthy_types (dw_die_ref unit)
7420 decl_hash_type decl_table;
7422 mark_dies (unit);
7423 decl_table.create (10);
7424 copy_decls_walk (unit, unit, decl_table);
7425 decl_table.dispose ();
7426 unmark_dies (unit);
7429 /* Traverse the DIE and add a sibling attribute if it may have the
7430 effect of speeding up access to siblings. To save some space,
7431 avoid generating sibling attributes for DIE's without children. */
7433 static void
7434 add_sibling_attributes (dw_die_ref die)
7436 dw_die_ref c;
7438 if (! die->die_child)
7439 return;
7441 if (die->die_parent && die != die->die_parent->die_child)
7442 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7444 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7447 /* Output all location lists for the DIE and its children. */
7449 static void
7450 output_location_lists (dw_die_ref die)
7452 dw_die_ref c;
7453 dw_attr_ref a;
7454 unsigned ix;
7456 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7457 if (AT_class (a) == dw_val_class_loc_list)
7458 output_loc_list (AT_loc_list (a));
7460 FOR_EACH_CHILD (die, c, output_location_lists (c));
7463 /* We want to limit the number of external references, because they are
7464 larger than local references: a relocation takes multiple words, and
7465 even a sig8 reference is always eight bytes, whereas a local reference
7466 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7467 So if we encounter multiple external references to the same type DIE, we
7468 make a local typedef stub for it and redirect all references there.
7470 This is the element of the hash table for keeping track of these
7471 references. */
7473 struct external_ref
7475 dw_die_ref type;
7476 dw_die_ref stub;
7477 unsigned n_refs;
7480 /* Hashtable helpers. */
7482 struct external_ref_hasher : typed_free_remove <external_ref>
7484 typedef external_ref value_type;
7485 typedef external_ref compare_type;
7486 static inline hashval_t hash (const value_type *);
7487 static inline bool equal (const value_type *, const compare_type *);
7490 inline hashval_t
7491 external_ref_hasher::hash (const value_type *r)
7493 dw_die_ref die = r->type;
7494 hashval_t h = 0;
7496 /* We can't use the address of the DIE for hashing, because
7497 that will make the order of the stub DIEs non-deterministic. */
7498 if (! die->comdat_type_p)
7499 /* We have a symbol; use it to compute a hash. */
7500 h = htab_hash_string (die->die_id.die_symbol);
7501 else
7503 /* We have a type signature; use a subset of the bits as the hash.
7504 The 8-byte signature is at least as large as hashval_t. */
7505 comdat_type_node_ref type_node = die->die_id.die_type_node;
7506 memcpy (&h, type_node->signature, sizeof (h));
7508 return h;
7511 inline bool
7512 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7514 return r1->type == r2->type;
7517 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7519 /* Return a pointer to the external_ref for references to DIE. */
7521 static struct external_ref *
7522 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7524 struct external_ref ref, *ref_p;
7525 external_ref **slot;
7527 ref.type = die;
7528 slot = map.find_slot (&ref, INSERT);
7529 if (*slot != HTAB_EMPTY_ENTRY)
7530 return *slot;
7532 ref_p = XCNEW (struct external_ref);
7533 ref_p->type = die;
7534 *slot = ref_p;
7535 return ref_p;
7538 /* Subroutine of optimize_external_refs, below.
7540 If we see a type skeleton, record it as our stub. If we see external
7541 references, remember how many we've seen. */
7543 static void
7544 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7546 dw_die_ref c;
7547 dw_attr_ref a;
7548 unsigned ix;
7549 struct external_ref *ref_p;
7551 if (is_type_die (die)
7552 && (c = get_AT_ref (die, DW_AT_signature)))
7554 /* This is a local skeleton; use it for local references. */
7555 ref_p = lookup_external_ref (map, c);
7556 ref_p->stub = die;
7559 /* Scan the DIE references, and remember any that refer to DIEs from
7560 other CUs (i.e. those which are not marked). */
7561 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7562 if (AT_class (a) == dw_val_class_die_ref
7563 && (c = AT_ref (a))->die_mark == 0
7564 && is_type_die (c))
7566 ref_p = lookup_external_ref (map, c);
7567 ref_p->n_refs++;
7570 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7573 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7574 points to an external_ref, DATA is the CU we're processing. If we don't
7575 already have a local stub, and we have multiple refs, build a stub. */
7578 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7580 struct external_ref *ref_p = *slot;
7582 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7584 /* We have multiple references to this type, so build a small stub.
7585 Both of these forms are a bit dodgy from the perspective of the
7586 DWARF standard, since technically they should have names. */
7587 dw_die_ref cu = data;
7588 dw_die_ref type = ref_p->type;
7589 dw_die_ref stub = NULL;
7591 if (type->comdat_type_p)
7593 /* If we refer to this type via sig8, use AT_signature. */
7594 stub = new_die (type->die_tag, cu, NULL_TREE);
7595 add_AT_die_ref (stub, DW_AT_signature, type);
7597 else
7599 /* Otherwise, use a typedef with no name. */
7600 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7601 add_AT_die_ref (stub, DW_AT_type, type);
7604 stub->die_mark++;
7605 ref_p->stub = stub;
7607 return 1;
7610 /* DIE is a unit; look through all the DIE references to see if there are
7611 any external references to types, and if so, create local stubs for
7612 them which will be applied in build_abbrev_table. This is useful because
7613 references to local DIEs are smaller. */
7615 static external_ref_hash_type
7616 optimize_external_refs (dw_die_ref die)
7618 external_ref_hash_type map;
7619 map.create (10);
7620 optimize_external_refs_1 (die, map);
7621 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7622 return map;
7625 /* The format of each DIE (and its attribute value pairs) is encoded in an
7626 abbreviation table. This routine builds the abbreviation table and assigns
7627 a unique abbreviation id for each abbreviation entry. The children of each
7628 die are visited recursively. */
7630 static void
7631 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7633 unsigned long abbrev_id;
7634 unsigned int n_alloc;
7635 dw_die_ref c;
7636 dw_attr_ref a;
7637 unsigned ix;
7639 /* Scan the DIE references, and replace any that refer to
7640 DIEs from other CUs (i.e. those which are not marked) with
7641 the local stubs we built in optimize_external_refs. */
7642 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7643 if (AT_class (a) == dw_val_class_die_ref
7644 && (c = AT_ref (a))->die_mark == 0)
7646 struct external_ref *ref_p;
7647 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7649 ref_p = lookup_external_ref (extern_map, c);
7650 if (ref_p->stub && ref_p->stub != die)
7651 change_AT_die_ref (a, ref_p->stub);
7652 else
7653 /* We aren't changing this reference, so mark it external. */
7654 set_AT_ref_external (a, 1);
7657 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7659 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7660 dw_attr_ref die_a, abbrev_a;
7661 unsigned ix;
7662 bool ok = true;
7664 if (abbrev->die_tag != die->die_tag)
7665 continue;
7666 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7667 continue;
7669 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7670 continue;
7672 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7674 abbrev_a = &(*abbrev->die_attr)[ix];
7675 if ((abbrev_a->dw_attr != die_a->dw_attr)
7676 || (value_format (abbrev_a) != value_format (die_a)))
7678 ok = false;
7679 break;
7682 if (ok)
7683 break;
7686 if (abbrev_id >= abbrev_die_table_in_use)
7688 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7690 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7691 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7692 n_alloc);
7694 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7695 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7696 abbrev_die_table_allocated = n_alloc;
7699 ++abbrev_die_table_in_use;
7700 abbrev_die_table[abbrev_id] = die;
7703 die->die_abbrev = abbrev_id;
7704 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7707 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7709 static int
7710 constant_size (unsigned HOST_WIDE_INT value)
7712 int log;
7714 if (value == 0)
7715 log = 0;
7716 else
7717 log = floor_log2 (value);
7719 log = log / 8;
7720 log = 1 << (floor_log2 (log) + 1);
7722 return log;
7725 /* Return the size of a DIE as it is represented in the
7726 .debug_info section. */
7728 static unsigned long
7729 size_of_die (dw_die_ref die)
7731 unsigned long size = 0;
7732 dw_attr_ref a;
7733 unsigned ix;
7734 enum dwarf_form form;
7736 size += size_of_uleb128 (die->die_abbrev);
7737 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7739 switch (AT_class (a))
7741 case dw_val_class_addr:
7742 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7744 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7745 size += size_of_uleb128 (AT_index (a));
7747 else
7748 size += DWARF2_ADDR_SIZE;
7749 break;
7750 case dw_val_class_offset:
7751 size += DWARF_OFFSET_SIZE;
7752 break;
7753 case dw_val_class_loc:
7755 unsigned long lsize = size_of_locs (AT_loc (a));
7757 /* Block length. */
7758 if (dwarf_version >= 4)
7759 size += size_of_uleb128 (lsize);
7760 else
7761 size += constant_size (lsize);
7762 size += lsize;
7764 break;
7765 case dw_val_class_loc_list:
7766 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7768 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7769 size += size_of_uleb128 (AT_index (a));
7771 else
7772 size += DWARF_OFFSET_SIZE;
7773 break;
7774 case dw_val_class_range_list:
7775 size += DWARF_OFFSET_SIZE;
7776 break;
7777 case dw_val_class_const:
7778 size += size_of_sleb128 (AT_int (a));
7779 break;
7780 case dw_val_class_unsigned_const:
7782 int csize = constant_size (AT_unsigned (a));
7783 if (dwarf_version == 3
7784 && a->dw_attr == DW_AT_data_member_location
7785 && csize >= 4)
7786 size += size_of_uleb128 (AT_unsigned (a));
7787 else
7788 size += csize;
7790 break;
7791 case dw_val_class_const_double:
7792 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7793 if (HOST_BITS_PER_WIDE_INT >= 64)
7794 size++; /* block */
7795 break;
7796 case dw_val_class_vec:
7797 size += constant_size (a->dw_attr_val.v.val_vec.length
7798 * a->dw_attr_val.v.val_vec.elt_size)
7799 + a->dw_attr_val.v.val_vec.length
7800 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7801 break;
7802 case dw_val_class_flag:
7803 if (dwarf_version >= 4)
7804 /* Currently all add_AT_flag calls pass in 1 as last argument,
7805 so DW_FORM_flag_present can be used. If that ever changes,
7806 we'll need to use DW_FORM_flag and have some optimization
7807 in build_abbrev_table that will change those to
7808 DW_FORM_flag_present if it is set to 1 in all DIEs using
7809 the same abbrev entry. */
7810 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7811 else
7812 size += 1;
7813 break;
7814 case dw_val_class_die_ref:
7815 if (AT_ref_external (a))
7817 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7818 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7819 is sized by target address length, whereas in DWARF3
7820 it's always sized as an offset. */
7821 if (use_debug_types)
7822 size += DWARF_TYPE_SIGNATURE_SIZE;
7823 else if (dwarf_version == 2)
7824 size += DWARF2_ADDR_SIZE;
7825 else
7826 size += DWARF_OFFSET_SIZE;
7828 else
7829 size += DWARF_OFFSET_SIZE;
7830 break;
7831 case dw_val_class_fde_ref:
7832 size += DWARF_OFFSET_SIZE;
7833 break;
7834 case dw_val_class_lbl_id:
7835 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7837 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7838 size += size_of_uleb128 (AT_index (a));
7840 else
7841 size += DWARF2_ADDR_SIZE;
7842 break;
7843 case dw_val_class_lineptr:
7844 case dw_val_class_macptr:
7845 size += DWARF_OFFSET_SIZE;
7846 break;
7847 case dw_val_class_str:
7848 form = AT_string_form (a);
7849 if (form == DW_FORM_strp)
7850 size += DWARF_OFFSET_SIZE;
7851 else if (form == DW_FORM_GNU_str_index)
7852 size += size_of_uleb128 (AT_index (a));
7853 else
7854 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7855 break;
7856 case dw_val_class_file:
7857 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7858 break;
7859 case dw_val_class_data8:
7860 size += 8;
7861 break;
7862 case dw_val_class_vms_delta:
7863 size += DWARF_OFFSET_SIZE;
7864 break;
7865 case dw_val_class_high_pc:
7866 size += DWARF2_ADDR_SIZE;
7867 break;
7868 default:
7869 gcc_unreachable ();
7873 return size;
7876 /* Size the debugging information associated with a given DIE. Visits the
7877 DIE's children recursively. Updates the global variable next_die_offset, on
7878 each time through. Uses the current value of next_die_offset to update the
7879 die_offset field in each DIE. */
7881 static void
7882 calc_die_sizes (dw_die_ref die)
7884 dw_die_ref c;
7886 gcc_assert (die->die_offset == 0
7887 || (unsigned long int) die->die_offset == next_die_offset);
7888 die->die_offset = next_die_offset;
7889 next_die_offset += size_of_die (die);
7891 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7893 if (die->die_child != NULL)
7894 /* Count the null byte used to terminate sibling lists. */
7895 next_die_offset += 1;
7898 /* Size just the base type children at the start of the CU.
7899 This is needed because build_abbrev needs to size locs
7900 and sizing of type based stack ops needs to know die_offset
7901 values for the base types. */
7903 static void
7904 calc_base_type_die_sizes (void)
7906 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7907 unsigned int i;
7908 dw_die_ref base_type;
7909 #if ENABLE_ASSERT_CHECKING
7910 dw_die_ref prev = comp_unit_die ()->die_child;
7911 #endif
7913 die_offset += size_of_die (comp_unit_die ());
7914 for (i = 0; base_types.iterate (i, &base_type); i++)
7916 #if ENABLE_ASSERT_CHECKING
7917 gcc_assert (base_type->die_offset == 0
7918 && prev->die_sib == base_type
7919 && base_type->die_child == NULL
7920 && base_type->die_abbrev);
7921 prev = base_type;
7922 #endif
7923 base_type->die_offset = die_offset;
7924 die_offset += size_of_die (base_type);
7928 /* Set the marks for a die and its children. We do this so
7929 that we know whether or not a reference needs to use FORM_ref_addr; only
7930 DIEs in the same CU will be marked. We used to clear out the offset
7931 and use that as the flag, but ran into ordering problems. */
7933 static void
7934 mark_dies (dw_die_ref die)
7936 dw_die_ref c;
7938 gcc_assert (!die->die_mark);
7940 die->die_mark = 1;
7941 FOR_EACH_CHILD (die, c, mark_dies (c));
7944 /* Clear the marks for a die and its children. */
7946 static void
7947 unmark_dies (dw_die_ref die)
7949 dw_die_ref c;
7951 if (! use_debug_types)
7952 gcc_assert (die->die_mark);
7954 die->die_mark = 0;
7955 FOR_EACH_CHILD (die, c, unmark_dies (c));
7958 /* Clear the marks for a die, its children and referred dies. */
7960 static void
7961 unmark_all_dies (dw_die_ref die)
7963 dw_die_ref c;
7964 dw_attr_ref a;
7965 unsigned ix;
7967 if (!die->die_mark)
7968 return;
7969 die->die_mark = 0;
7971 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7973 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7974 if (AT_class (a) == dw_val_class_die_ref)
7975 unmark_all_dies (AT_ref (a));
7978 /* Calculate if the entry should appear in the final output file. It may be
7979 from a pruned a type. */
7981 static bool
7982 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7984 /* By limiting gnu pubnames to definitions only, gold can generate a
7985 gdb index without entries for declarations, which don't include
7986 enough information to be useful. */
7987 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7988 return false;
7990 if (table == pubname_table)
7992 /* Enumerator names are part of the pubname table, but the
7993 parent DW_TAG_enumeration_type die may have been pruned.
7994 Don't output them if that is the case. */
7995 if (p->die->die_tag == DW_TAG_enumerator &&
7996 (p->die->die_parent == NULL
7997 || !p->die->die_parent->die_perennial_p))
7998 return false;
8000 /* Everything else in the pubname table is included. */
8001 return true;
8004 /* The pubtypes table shouldn't include types that have been
8005 pruned. */
8006 return (p->die->die_offset != 0
8007 || !flag_eliminate_unused_debug_types);
8010 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8011 generated for the compilation unit. */
8013 static unsigned long
8014 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8016 unsigned long size;
8017 unsigned i;
8018 pubname_ref p;
8019 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8021 size = DWARF_PUBNAMES_HEADER_SIZE;
8022 FOR_EACH_VEC_ELT (*names, i, p)
8023 if (include_pubname_in_output (names, p))
8024 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8026 size += DWARF_OFFSET_SIZE;
8027 return size;
8030 /* Return the size of the information in the .debug_aranges section. */
8032 static unsigned long
8033 size_of_aranges (void)
8035 unsigned long size;
8037 size = DWARF_ARANGES_HEADER_SIZE;
8039 /* Count the address/length pair for this compilation unit. */
8040 if (text_section_used)
8041 size += 2 * DWARF2_ADDR_SIZE;
8042 if (cold_text_section_used)
8043 size += 2 * DWARF2_ADDR_SIZE;
8044 if (have_multiple_function_sections)
8046 unsigned fde_idx;
8047 dw_fde_ref fde;
8049 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8051 if (DECL_IGNORED_P (fde->decl))
8052 continue;
8053 if (!fde->in_std_section)
8054 size += 2 * DWARF2_ADDR_SIZE;
8055 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8056 size += 2 * DWARF2_ADDR_SIZE;
8060 /* Count the two zero words used to terminated the address range table. */
8061 size += 2 * DWARF2_ADDR_SIZE;
8062 return size;
8065 /* Select the encoding of an attribute value. */
8067 static enum dwarf_form
8068 value_format (dw_attr_ref a)
8070 switch (AT_class (a))
8072 case dw_val_class_addr:
8073 /* Only very few attributes allow DW_FORM_addr. */
8074 switch (a->dw_attr)
8076 case DW_AT_low_pc:
8077 case DW_AT_high_pc:
8078 case DW_AT_entry_pc:
8079 case DW_AT_trampoline:
8080 return (AT_index (a) == NOT_INDEXED
8081 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8082 default:
8083 break;
8085 switch (DWARF2_ADDR_SIZE)
8087 case 1:
8088 return DW_FORM_data1;
8089 case 2:
8090 return DW_FORM_data2;
8091 case 4:
8092 return DW_FORM_data4;
8093 case 8:
8094 return DW_FORM_data8;
8095 default:
8096 gcc_unreachable ();
8098 case dw_val_class_range_list:
8099 case dw_val_class_loc_list:
8100 if (dwarf_version >= 4)
8101 return DW_FORM_sec_offset;
8102 /* FALLTHRU */
8103 case dw_val_class_vms_delta:
8104 case dw_val_class_offset:
8105 switch (DWARF_OFFSET_SIZE)
8107 case 4:
8108 return DW_FORM_data4;
8109 case 8:
8110 return DW_FORM_data8;
8111 default:
8112 gcc_unreachable ();
8114 case dw_val_class_loc:
8115 if (dwarf_version >= 4)
8116 return DW_FORM_exprloc;
8117 switch (constant_size (size_of_locs (AT_loc (a))))
8119 case 1:
8120 return DW_FORM_block1;
8121 case 2:
8122 return DW_FORM_block2;
8123 case 4:
8124 return DW_FORM_block4;
8125 default:
8126 gcc_unreachable ();
8128 case dw_val_class_const:
8129 return DW_FORM_sdata;
8130 case dw_val_class_unsigned_const:
8131 switch (constant_size (AT_unsigned (a)))
8133 case 1:
8134 return DW_FORM_data1;
8135 case 2:
8136 return DW_FORM_data2;
8137 case 4:
8138 /* In DWARF3 DW_AT_data_member_location with
8139 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8140 constant, so we need to use DW_FORM_udata if we need
8141 a large constant. */
8142 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8143 return DW_FORM_udata;
8144 return DW_FORM_data4;
8145 case 8:
8146 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8147 return DW_FORM_udata;
8148 return DW_FORM_data8;
8149 default:
8150 gcc_unreachable ();
8152 case dw_val_class_const_double:
8153 switch (HOST_BITS_PER_WIDE_INT)
8155 case 8:
8156 return DW_FORM_data2;
8157 case 16:
8158 return DW_FORM_data4;
8159 case 32:
8160 return DW_FORM_data8;
8161 case 64:
8162 default:
8163 return DW_FORM_block1;
8165 case dw_val_class_vec:
8166 switch (constant_size (a->dw_attr_val.v.val_vec.length
8167 * a->dw_attr_val.v.val_vec.elt_size))
8169 case 1:
8170 return DW_FORM_block1;
8171 case 2:
8172 return DW_FORM_block2;
8173 case 4:
8174 return DW_FORM_block4;
8175 default:
8176 gcc_unreachable ();
8178 case dw_val_class_flag:
8179 if (dwarf_version >= 4)
8181 /* Currently all add_AT_flag calls pass in 1 as last argument,
8182 so DW_FORM_flag_present can be used. If that ever changes,
8183 we'll need to use DW_FORM_flag and have some optimization
8184 in build_abbrev_table that will change those to
8185 DW_FORM_flag_present if it is set to 1 in all DIEs using
8186 the same abbrev entry. */
8187 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8188 return DW_FORM_flag_present;
8190 return DW_FORM_flag;
8191 case dw_val_class_die_ref:
8192 if (AT_ref_external (a))
8193 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8194 else
8195 return DW_FORM_ref;
8196 case dw_val_class_fde_ref:
8197 return DW_FORM_data;
8198 case dw_val_class_lbl_id:
8199 return (AT_index (a) == NOT_INDEXED
8200 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8201 case dw_val_class_lineptr:
8202 case dw_val_class_macptr:
8203 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8204 case dw_val_class_str:
8205 return AT_string_form (a);
8206 case dw_val_class_file:
8207 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8209 case 1:
8210 return DW_FORM_data1;
8211 case 2:
8212 return DW_FORM_data2;
8213 case 4:
8214 return DW_FORM_data4;
8215 default:
8216 gcc_unreachable ();
8219 case dw_val_class_data8:
8220 return DW_FORM_data8;
8222 case dw_val_class_high_pc:
8223 switch (DWARF2_ADDR_SIZE)
8225 case 1:
8226 return DW_FORM_data1;
8227 case 2:
8228 return DW_FORM_data2;
8229 case 4:
8230 return DW_FORM_data4;
8231 case 8:
8232 return DW_FORM_data8;
8233 default:
8234 gcc_unreachable ();
8237 default:
8238 gcc_unreachable ();
8242 /* Output the encoding of an attribute value. */
8244 static void
8245 output_value_format (dw_attr_ref a)
8247 enum dwarf_form form = value_format (a);
8249 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8252 /* Given a die and id, produce the appropriate abbreviations. */
8254 static void
8255 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8257 unsigned ix;
8258 dw_attr_ref a_attr;
8260 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8261 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8262 dwarf_tag_name (abbrev->die_tag));
8264 if (abbrev->die_child != NULL)
8265 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8266 else
8267 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8269 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8271 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8272 dwarf_attr_name (a_attr->dw_attr));
8273 output_value_format (a_attr);
8276 dw2_asm_output_data (1, 0, NULL);
8277 dw2_asm_output_data (1, 0, NULL);
8281 /* Output the .debug_abbrev section which defines the DIE abbreviation
8282 table. */
8284 static void
8285 output_abbrev_section (void)
8287 unsigned long abbrev_id;
8289 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8290 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8292 /* Terminate the table. */
8293 dw2_asm_output_data (1, 0, NULL);
8296 /* Output a symbol we can use to refer to this DIE from another CU. */
8298 static inline void
8299 output_die_symbol (dw_die_ref die)
8301 const char *sym = die->die_id.die_symbol;
8303 gcc_assert (!die->comdat_type_p);
8305 if (sym == 0)
8306 return;
8308 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8309 /* We make these global, not weak; if the target doesn't support
8310 .linkonce, it doesn't support combining the sections, so debugging
8311 will break. */
8312 targetm.asm_out.globalize_label (asm_out_file, sym);
8314 ASM_OUTPUT_LABEL (asm_out_file, sym);
8317 /* Return a new location list, given the begin and end range, and the
8318 expression. */
8320 static inline dw_loc_list_ref
8321 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8322 const char *section)
8324 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8326 retlist->begin = begin;
8327 retlist->begin_entry = NULL;
8328 retlist->end = end;
8329 retlist->expr = expr;
8330 retlist->section = section;
8332 return retlist;
8335 /* Generate a new internal symbol for this location list node, if it
8336 hasn't got one yet. */
8338 static inline void
8339 gen_llsym (dw_loc_list_ref list)
8341 gcc_assert (!list->ll_symbol);
8342 list->ll_symbol = gen_internal_sym ("LLST");
8345 /* Output the location list given to us. */
8347 static void
8348 output_loc_list (dw_loc_list_ref list_head)
8350 dw_loc_list_ref curr = list_head;
8352 if (list_head->emitted)
8353 return;
8354 list_head->emitted = true;
8356 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8358 /* Walk the location list, and output each range + expression. */
8359 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8361 unsigned long size;
8362 /* Don't output an entry that starts and ends at the same address. */
8363 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8364 continue;
8365 size = size_of_locs (curr->expr);
8366 /* If the expression is too large, drop it on the floor. We could
8367 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8368 in the expression, but >= 64KB expressions for a single value
8369 in a single range are unlikely very useful. */
8370 if (size > 0xffff)
8371 continue;
8372 if (dwarf_split_debug_info)
8374 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8375 "Location list start/length entry (%s)",
8376 list_head->ll_symbol);
8377 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8378 "Location list range start index (%s)",
8379 curr->begin);
8380 /* The length field is 4 bytes. If we ever need to support
8381 an 8-byte length, we can add a new DW_LLE code or fall back
8382 to DW_LLE_GNU_start_end_entry. */
8383 dw2_asm_output_delta (4, curr->end, curr->begin,
8384 "Location list range length (%s)",
8385 list_head->ll_symbol);
8387 else if (!have_multiple_function_sections)
8389 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8390 "Location list begin address (%s)",
8391 list_head->ll_symbol);
8392 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8393 "Location list end address (%s)",
8394 list_head->ll_symbol);
8396 else
8398 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8399 "Location list begin address (%s)",
8400 list_head->ll_symbol);
8401 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8402 "Location list end address (%s)",
8403 list_head->ll_symbol);
8406 /* Output the block length for this list of location operations. */
8407 gcc_assert (size <= 0xffff);
8408 dw2_asm_output_data (2, size, "%s", "Location expression size");
8410 output_loc_sequence (curr->expr, -1);
8413 if (dwarf_split_debug_info)
8414 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8415 "Location list terminator (%s)",
8416 list_head->ll_symbol);
8417 else
8419 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8420 "Location list terminator begin (%s)",
8421 list_head->ll_symbol);
8422 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8423 "Location list terminator end (%s)",
8424 list_head->ll_symbol);
8428 /* Output a range_list offset into the debug_range section. Emit a
8429 relocated reference if val_entry is NULL, otherwise, emit an
8430 indirect reference. */
8432 static void
8433 output_range_list_offset (dw_attr_ref a)
8435 const char *name = dwarf_attr_name (a->dw_attr);
8437 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8439 char *p = strchr (ranges_section_label, '\0');
8440 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8441 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8442 debug_ranges_section, "%s", name);
8443 *p = '\0';
8445 else
8446 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8447 "%s (offset from %s)", name, ranges_section_label);
8450 /* Output the offset into the debug_loc section. */
8452 static void
8453 output_loc_list_offset (dw_attr_ref a)
8455 char *sym = AT_loc_list (a)->ll_symbol;
8457 gcc_assert (sym);
8458 if (dwarf_split_debug_info)
8459 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8460 "%s", dwarf_attr_name (a->dw_attr));
8461 else
8462 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8463 "%s", dwarf_attr_name (a->dw_attr));
8466 /* Output an attribute's index or value appropriately. */
8468 static void
8469 output_attr_index_or_value (dw_attr_ref a)
8471 const char *name = dwarf_attr_name (a->dw_attr);
8473 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8475 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8476 return;
8478 switch (AT_class (a))
8480 case dw_val_class_addr:
8481 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8482 break;
8483 case dw_val_class_high_pc:
8484 case dw_val_class_lbl_id:
8485 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8486 break;
8487 case dw_val_class_loc_list:
8488 output_loc_list_offset (a);
8489 break;
8490 default:
8491 gcc_unreachable ();
8495 /* Output a type signature. */
8497 static inline void
8498 output_signature (const char *sig, const char *name)
8500 int i;
8502 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8503 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8506 /* Output the DIE and its attributes. Called recursively to generate
8507 the definitions of each child DIE. */
8509 static void
8510 output_die (dw_die_ref die)
8512 dw_attr_ref a;
8513 dw_die_ref c;
8514 unsigned long size;
8515 unsigned ix;
8517 /* If someone in another CU might refer to us, set up a symbol for
8518 them to point to. */
8519 if (! die->comdat_type_p && die->die_id.die_symbol)
8520 output_die_symbol (die);
8522 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8523 (unsigned long)die->die_offset,
8524 dwarf_tag_name (die->die_tag));
8526 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8528 const char *name = dwarf_attr_name (a->dw_attr);
8530 switch (AT_class (a))
8532 case dw_val_class_addr:
8533 output_attr_index_or_value (a);
8534 break;
8536 case dw_val_class_offset:
8537 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8538 "%s", name);
8539 break;
8541 case dw_val_class_range_list:
8542 output_range_list_offset (a);
8543 break;
8545 case dw_val_class_loc:
8546 size = size_of_locs (AT_loc (a));
8548 /* Output the block length for this list of location operations. */
8549 if (dwarf_version >= 4)
8550 dw2_asm_output_data_uleb128 (size, "%s", name);
8551 else
8552 dw2_asm_output_data (constant_size (size), size, "%s", name);
8554 output_loc_sequence (AT_loc (a), -1);
8555 break;
8557 case dw_val_class_const:
8558 /* ??? It would be slightly more efficient to use a scheme like is
8559 used for unsigned constants below, but gdb 4.x does not sign
8560 extend. Gdb 5.x does sign extend. */
8561 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8562 break;
8564 case dw_val_class_unsigned_const:
8566 int csize = constant_size (AT_unsigned (a));
8567 if (dwarf_version == 3
8568 && a->dw_attr == DW_AT_data_member_location
8569 && csize >= 4)
8570 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8571 else
8572 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8574 break;
8576 case dw_val_class_const_double:
8578 unsigned HOST_WIDE_INT first, second;
8580 if (HOST_BITS_PER_WIDE_INT >= 64)
8581 dw2_asm_output_data (1,
8582 HOST_BITS_PER_DOUBLE_INT
8583 / HOST_BITS_PER_CHAR,
8584 NULL);
8586 if (WORDS_BIG_ENDIAN)
8588 first = a->dw_attr_val.v.val_double.high;
8589 second = a->dw_attr_val.v.val_double.low;
8591 else
8593 first = a->dw_attr_val.v.val_double.low;
8594 second = a->dw_attr_val.v.val_double.high;
8597 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8598 first, "%s", name);
8599 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8600 second, NULL);
8602 break;
8604 case dw_val_class_vec:
8606 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8607 unsigned int len = a->dw_attr_val.v.val_vec.length;
8608 unsigned int i;
8609 unsigned char *p;
8611 dw2_asm_output_data (constant_size (len * elt_size),
8612 len * elt_size, "%s", name);
8613 if (elt_size > sizeof (HOST_WIDE_INT))
8615 elt_size /= 2;
8616 len *= 2;
8618 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8619 i < len;
8620 i++, p += elt_size)
8621 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8622 "fp or vector constant word %u", i);
8623 break;
8626 case dw_val_class_flag:
8627 if (dwarf_version >= 4)
8629 /* Currently all add_AT_flag calls pass in 1 as last argument,
8630 so DW_FORM_flag_present can be used. If that ever changes,
8631 we'll need to use DW_FORM_flag and have some optimization
8632 in build_abbrev_table that will change those to
8633 DW_FORM_flag_present if it is set to 1 in all DIEs using
8634 the same abbrev entry. */
8635 gcc_assert (AT_flag (a) == 1);
8636 if (flag_debug_asm)
8637 fprintf (asm_out_file, "\t\t\t%s %s\n",
8638 ASM_COMMENT_START, name);
8639 break;
8641 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8642 break;
8644 case dw_val_class_loc_list:
8645 output_attr_index_or_value (a);
8646 break;
8648 case dw_val_class_die_ref:
8649 if (AT_ref_external (a))
8651 if (AT_ref (a)->comdat_type_p)
8653 comdat_type_node_ref type_node =
8654 AT_ref (a)->die_id.die_type_node;
8656 gcc_assert (type_node);
8657 output_signature (type_node->signature, name);
8659 else
8661 const char *sym = AT_ref (a)->die_id.die_symbol;
8662 int size;
8664 gcc_assert (sym);
8665 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8666 length, whereas in DWARF3 it's always sized as an
8667 offset. */
8668 if (dwarf_version == 2)
8669 size = DWARF2_ADDR_SIZE;
8670 else
8671 size = DWARF_OFFSET_SIZE;
8672 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8673 name);
8676 else
8678 gcc_assert (AT_ref (a)->die_offset);
8679 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8680 "%s", name);
8682 break;
8684 case dw_val_class_fde_ref:
8686 char l1[20];
8688 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8689 a->dw_attr_val.v.val_fde_index * 2);
8690 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8691 "%s", name);
8693 break;
8695 case dw_val_class_vms_delta:
8696 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8697 AT_vms_delta2 (a), AT_vms_delta1 (a),
8698 "%s", name);
8699 break;
8701 case dw_val_class_lbl_id:
8702 output_attr_index_or_value (a);
8703 break;
8705 case dw_val_class_lineptr:
8706 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8707 debug_line_section, "%s", name);
8708 break;
8710 case dw_val_class_macptr:
8711 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8712 debug_macinfo_section, "%s", name);
8713 break;
8715 case dw_val_class_str:
8716 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8717 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8718 a->dw_attr_val.v.val_str->label,
8719 debug_str_section,
8720 "%s: \"%s\"", name, AT_string (a));
8721 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8722 dw2_asm_output_data_uleb128 (AT_index (a),
8723 "%s: \"%s\"", name, AT_string (a));
8724 else
8725 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8726 break;
8728 case dw_val_class_file:
8730 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8732 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8733 a->dw_attr_val.v.val_file->filename);
8734 break;
8737 case dw_val_class_data8:
8739 int i;
8741 for (i = 0; i < 8; i++)
8742 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8743 i == 0 ? "%s" : NULL, name);
8744 break;
8747 case dw_val_class_high_pc:
8748 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8749 get_AT_low_pc (die), "DW_AT_high_pc");
8750 break;
8752 default:
8753 gcc_unreachable ();
8757 FOR_EACH_CHILD (die, c, output_die (c));
8759 /* Add null byte to terminate sibling list. */
8760 if (die->die_child != NULL)
8761 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8762 (unsigned long) die->die_offset);
8765 /* Output the compilation unit that appears at the beginning of the
8766 .debug_info section, and precedes the DIE descriptions. */
8768 static void
8769 output_compilation_unit_header (void)
8771 int ver = dwarf_version;
8773 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8774 dw2_asm_output_data (4, 0xffffffff,
8775 "Initial length escape value indicating 64-bit DWARF extension");
8776 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8777 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8778 "Length of Compilation Unit Info");
8779 dw2_asm_output_data (2, ver, "DWARF version number");
8780 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8781 debug_abbrev_section,
8782 "Offset Into Abbrev. Section");
8783 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8786 /* Output the compilation unit DIE and its children. */
8788 static void
8789 output_comp_unit (dw_die_ref die, int output_if_empty)
8791 const char *secname, *oldsym;
8792 char *tmp;
8793 external_ref_hash_type extern_map;
8795 /* Unless we are outputting main CU, we may throw away empty ones. */
8796 if (!output_if_empty && die->die_child == NULL)
8797 return;
8799 /* Even if there are no children of this DIE, we must output the information
8800 about the compilation unit. Otherwise, on an empty translation unit, we
8801 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8802 will then complain when examining the file. First mark all the DIEs in
8803 this CU so we know which get local refs. */
8804 mark_dies (die);
8806 extern_map = optimize_external_refs (die);
8808 build_abbrev_table (die, extern_map);
8810 extern_map.dispose ();
8812 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8813 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8814 calc_die_sizes (die);
8816 oldsym = die->die_id.die_symbol;
8817 if (oldsym)
8819 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8821 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8822 secname = tmp;
8823 die->die_id.die_symbol = NULL;
8824 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8826 else
8828 switch_to_section (debug_info_section);
8829 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8830 info_section_emitted = true;
8833 /* Output debugging information. */
8834 output_compilation_unit_header ();
8835 output_die (die);
8837 /* Leave the marks on the main CU, so we can check them in
8838 output_pubnames. */
8839 if (oldsym)
8841 unmark_dies (die);
8842 die->die_id.die_symbol = oldsym;
8846 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8847 and .debug_pubtypes. This is configured per-target, but can be
8848 overridden by the -gpubnames or -gno-pubnames options. */
8850 static inline bool
8851 want_pubnames (void)
8853 if (debug_info_level <= DINFO_LEVEL_TERSE)
8854 return false;
8855 if (debug_generate_pub_sections != -1)
8856 return debug_generate_pub_sections;
8857 return targetm.want_debug_pub_sections;
8860 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8862 static void
8863 add_AT_pubnames (dw_die_ref die)
8865 if (want_pubnames ())
8866 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8869 /* Add a string attribute value to a skeleton DIE. */
8871 static inline void
8872 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8873 const char *str)
8875 dw_attr_node attr;
8876 struct indirect_string_node *node;
8878 if (! skeleton_debug_str_hash)
8879 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8880 debug_str_eq, NULL);
8882 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8883 find_string_form (node);
8884 if (node->form == DW_FORM_GNU_str_index)
8885 node->form = DW_FORM_strp;
8887 attr.dw_attr = attr_kind;
8888 attr.dw_attr_val.val_class = dw_val_class_str;
8889 attr.dw_attr_val.val_entry = NULL;
8890 attr.dw_attr_val.v.val_str = node;
8891 add_dwarf_attr (die, &attr);
8894 /* Helper function to generate top-level dies for skeleton debug_info and
8895 debug_types. */
8897 static void
8898 add_top_level_skeleton_die_attrs (dw_die_ref die)
8900 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8901 const char *comp_dir = comp_dir_string ();
8903 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8904 if (comp_dir != NULL)
8905 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8906 add_AT_pubnames (die);
8907 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8910 /* Return the single type-unit die for skeleton type units. */
8912 static dw_die_ref
8913 get_skeleton_type_unit (void)
8915 /* For dwarf_split_debug_sections with use_type info, all type units in the
8916 skeleton sections have identical dies (but different headers). This
8917 single die will be output many times. */
8919 static dw_die_ref skeleton_type_unit = NULL;
8921 if (skeleton_type_unit == NULL)
8923 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8924 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8925 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8927 return skeleton_type_unit;
8930 /* Output skeleton debug sections that point to the dwo file. */
8932 static void
8933 output_skeleton_debug_sections (dw_die_ref comp_unit)
8935 /* These attributes will be found in the full debug_info section. */
8936 remove_AT (comp_unit, DW_AT_producer);
8937 remove_AT (comp_unit, DW_AT_language);
8939 switch_to_section (debug_skeleton_info_section);
8940 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8942 /* Produce the skeleton compilation-unit header. This one differs enough from
8943 a normal CU header that it's better not to call output_compilation_unit
8944 header. */
8945 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8946 dw2_asm_output_data (4, 0xffffffff,
8947 "Initial length escape value indicating 64-bit DWARF extension");
8949 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8950 DWARF_COMPILE_UNIT_HEADER_SIZE
8951 - DWARF_INITIAL_LENGTH_SIZE
8952 + size_of_die (comp_unit),
8953 "Length of Compilation Unit Info");
8954 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8955 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8956 debug_abbrev_section,
8957 "Offset Into Abbrev. Section");
8958 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8960 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8961 output_die (comp_unit);
8963 /* Build the skeleton debug_abbrev section. */
8964 switch_to_section (debug_skeleton_abbrev_section);
8965 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8967 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8968 if (use_debug_types)
8969 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8971 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8974 /* Output a comdat type unit DIE and its children. */
8976 static void
8977 output_comdat_type_unit (comdat_type_node *node)
8979 const char *secname;
8980 char *tmp;
8981 int i;
8982 #if defined (OBJECT_FORMAT_ELF)
8983 tree comdat_key;
8984 #endif
8985 external_ref_hash_type extern_map;
8987 /* First mark all the DIEs in this CU so we know which get local refs. */
8988 mark_dies (node->root_die);
8990 extern_map = optimize_external_refs (node->root_die);
8992 build_abbrev_table (node->root_die, extern_map);
8994 extern_map.dispose ();
8996 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8997 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8998 calc_die_sizes (node->root_die);
9000 #if defined (OBJECT_FORMAT_ELF)
9001 if (!dwarf_split_debug_info)
9002 secname = ".debug_types";
9003 else
9004 secname = ".debug_types.dwo";
9006 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9007 sprintf (tmp, "wt.");
9008 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9009 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9010 comdat_key = get_identifier (tmp);
9011 targetm.asm_out.named_section (secname,
9012 SECTION_DEBUG | SECTION_LINKONCE,
9013 comdat_key);
9014 #else
9015 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9016 sprintf (tmp, ".gnu.linkonce.wt.");
9017 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9018 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9019 secname = tmp;
9020 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9021 #endif
9023 /* Output debugging information. */
9024 output_compilation_unit_header ();
9025 output_signature (node->signature, "Type Signature");
9026 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9027 "Offset to Type DIE");
9028 output_die (node->root_die);
9030 unmark_dies (node->root_die);
9032 #if defined (OBJECT_FORMAT_ELF)
9033 if (dwarf_split_debug_info)
9035 /* Produce the skeleton type-unit header. */
9036 const char *secname = ".debug_types";
9038 targetm.asm_out.named_section (secname,
9039 SECTION_DEBUG | SECTION_LINKONCE,
9040 comdat_key);
9041 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9042 dw2_asm_output_data (4, 0xffffffff,
9043 "Initial length escape value indicating 64-bit DWARF extension");
9045 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9046 DWARF_COMPILE_UNIT_HEADER_SIZE
9047 - DWARF_INITIAL_LENGTH_SIZE
9048 + size_of_die (get_skeleton_type_unit ())
9049 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9050 "Length of Type Unit Info");
9051 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9052 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9053 debug_skeleton_abbrev_section_label,
9054 debug_abbrev_section,
9055 "Offset Into Abbrev. Section");
9056 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9057 output_signature (node->signature, "Type Signature");
9058 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9060 output_die (get_skeleton_type_unit ());
9062 #endif
9065 /* Return the DWARF2/3 pubname associated with a decl. */
9067 static const char *
9068 dwarf2_name (tree decl, int scope)
9070 if (DECL_NAMELESS (decl))
9071 return NULL;
9072 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9075 /* Add a new entry to .debug_pubnames if appropriate. */
9077 static void
9078 add_pubname_string (const char *str, dw_die_ref die)
9080 pubname_entry e;
9082 e.die = die;
9083 e.name = xstrdup (str);
9084 vec_safe_push (pubname_table, e);
9087 static void
9088 add_pubname (tree decl, dw_die_ref die)
9090 if (!want_pubnames ())
9091 return;
9093 /* Don't add items to the table when we expect that the consumer will have
9094 just read the enclosing die. For example, if the consumer is looking at a
9095 class_member, it will either be inside the class already, or will have just
9096 looked up the class to find the member. Either way, searching the class is
9097 faster than searching the index. */
9098 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9099 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9101 const char *name = dwarf2_name (decl, 1);
9103 if (name)
9104 add_pubname_string (name, die);
9108 /* Add an enumerator to the pubnames section. */
9110 static void
9111 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9113 pubname_entry e;
9115 gcc_assert (scope_name);
9116 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9117 e.die = die;
9118 vec_safe_push (pubname_table, e);
9121 /* Add a new entry to .debug_pubtypes if appropriate. */
9123 static void
9124 add_pubtype (tree decl, dw_die_ref die)
9126 pubname_entry e;
9128 if (!want_pubnames ())
9129 return;
9131 if ((TREE_PUBLIC (decl)
9132 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9133 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9135 tree scope = NULL;
9136 const char *scope_name = "";
9137 const char *sep = is_cxx () ? "::" : ".";
9138 const char *name;
9140 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9141 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9143 scope_name = lang_hooks.dwarf_name (scope, 1);
9144 if (scope_name != NULL && scope_name[0] != '\0')
9145 scope_name = concat (scope_name, sep, NULL);
9146 else
9147 scope_name = "";
9150 if (TYPE_P (decl))
9151 name = type_tag (decl);
9152 else
9153 name = lang_hooks.dwarf_name (decl, 1);
9155 /* If we don't have a name for the type, there's no point in adding
9156 it to the table. */
9157 if (name != NULL && name[0] != '\0')
9159 e.die = die;
9160 e.name = concat (scope_name, name, NULL);
9161 vec_safe_push (pubtype_table, e);
9164 /* Although it might be more consistent to add the pubinfo for the
9165 enumerators as their dies are created, they should only be added if the
9166 enum type meets the criteria above. So rather than re-check the parent
9167 enum type whenever an enumerator die is created, just output them all
9168 here. This isn't protected by the name conditional because anonymous
9169 enums don't have names. */
9170 if (die->die_tag == DW_TAG_enumeration_type)
9172 dw_die_ref c;
9174 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9179 /* Output a single entry in the pubnames table. */
9181 static void
9182 output_pubname (dw_offset die_offset, pubname_entry *entry)
9184 dw_die_ref die = entry->die;
9185 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9187 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9189 if (debug_generate_pub_sections == 2)
9191 /* This logic follows gdb's method for determining the value of the flag
9192 byte. */
9193 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9194 switch (die->die_tag)
9196 case DW_TAG_typedef:
9197 case DW_TAG_base_type:
9198 case DW_TAG_subrange_type:
9199 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9200 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9201 break;
9202 case DW_TAG_enumerator:
9203 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9204 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9205 if (!is_cxx () && !is_java ())
9206 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9207 break;
9208 case DW_TAG_subprogram:
9209 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9210 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9211 if (!is_ada ())
9212 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9213 break;
9214 case DW_TAG_constant:
9215 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9216 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9217 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9218 break;
9219 case DW_TAG_variable:
9220 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9221 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9222 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9223 break;
9224 case DW_TAG_namespace:
9225 case DW_TAG_imported_declaration:
9226 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9227 break;
9228 case DW_TAG_class_type:
9229 case DW_TAG_interface_type:
9230 case DW_TAG_structure_type:
9231 case DW_TAG_union_type:
9232 case DW_TAG_enumeration_type:
9233 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9234 if (!is_cxx () && !is_java ())
9235 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9236 break;
9237 default:
9238 /* An unusual tag. Leave the flag-byte empty. */
9239 break;
9241 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9242 "GDB-index flags");
9245 dw2_asm_output_nstring (entry->name, -1, "external name");
9249 /* Output the public names table used to speed up access to externally
9250 visible names; or the public types table used to find type definitions. */
9252 static void
9253 output_pubnames (vec<pubname_entry, va_gc> *names)
9255 unsigned i;
9256 unsigned long pubnames_length = size_of_pubnames (names);
9257 pubname_ref pub;
9259 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9260 dw2_asm_output_data (4, 0xffffffff,
9261 "Initial length escape value indicating 64-bit DWARF extension");
9262 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9264 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9265 dw2_asm_output_data (2, 2, "DWARF Version");
9267 if (dwarf_split_debug_info)
9268 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9269 debug_skeleton_info_section,
9270 "Offset of Compilation Unit Info");
9271 else
9272 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9273 debug_info_section,
9274 "Offset of Compilation Unit Info");
9275 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9276 "Compilation Unit Length");
9278 FOR_EACH_VEC_ELT (*names, i, pub)
9280 if (include_pubname_in_output (names, pub))
9282 dw_offset die_offset = pub->die->die_offset;
9284 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9285 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9286 gcc_assert (pub->die->die_mark);
9288 /* If we're putting types in their own .debug_types sections,
9289 the .debug_pubtypes table will still point to the compile
9290 unit (not the type unit), so we want to use the offset of
9291 the skeleton DIE (if there is one). */
9292 if (pub->die->comdat_type_p && names == pubtype_table)
9294 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9296 if (type_node != NULL)
9297 die_offset = (type_node->skeleton_die != NULL
9298 ? type_node->skeleton_die->die_offset
9299 : comp_unit_die ()->die_offset);
9302 output_pubname (die_offset, pub);
9306 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9309 /* Output public names and types tables if necessary. */
9311 static void
9312 output_pubtables (void)
9314 if (!want_pubnames () || !info_section_emitted)
9315 return;
9317 switch_to_section (debug_pubnames_section);
9318 output_pubnames (pubname_table);
9319 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9320 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9321 simply won't look for the section. */
9322 switch_to_section (debug_pubtypes_section);
9323 output_pubnames (pubtype_table);
9327 /* Output the information that goes into the .debug_aranges table.
9328 Namely, define the beginning and ending address range of the
9329 text section generated for this compilation unit. */
9331 static void
9332 output_aranges (unsigned long aranges_length)
9334 unsigned i;
9336 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9337 dw2_asm_output_data (4, 0xffffffff,
9338 "Initial length escape value indicating 64-bit DWARF extension");
9339 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9340 "Length of Address Ranges Info");
9341 /* Version number for aranges is still 2, even in DWARF3. */
9342 dw2_asm_output_data (2, 2, "DWARF Version");
9343 if (dwarf_split_debug_info)
9344 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9345 debug_skeleton_info_section,
9346 "Offset of Compilation Unit Info");
9347 else
9348 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9349 debug_info_section,
9350 "Offset of Compilation Unit Info");
9351 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9352 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9354 /* We need to align to twice the pointer size here. */
9355 if (DWARF_ARANGES_PAD_SIZE)
9357 /* Pad using a 2 byte words so that padding is correct for any
9358 pointer size. */
9359 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9360 2 * DWARF2_ADDR_SIZE);
9361 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9362 dw2_asm_output_data (2, 0, NULL);
9365 /* It is necessary not to output these entries if the sections were
9366 not used; if the sections were not used, the length will be 0 and
9367 the address may end up as 0 if the section is discarded by ld
9368 --gc-sections, leaving an invalid (0, 0) entry that can be
9369 confused with the terminator. */
9370 if (text_section_used)
9372 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9373 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9374 text_section_label, "Length");
9376 if (cold_text_section_used)
9378 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9379 "Address");
9380 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9381 cold_text_section_label, "Length");
9384 if (have_multiple_function_sections)
9386 unsigned fde_idx;
9387 dw_fde_ref fde;
9389 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9391 if (DECL_IGNORED_P (fde->decl))
9392 continue;
9393 if (!fde->in_std_section)
9395 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9396 "Address");
9397 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9398 fde->dw_fde_begin, "Length");
9400 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9402 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9403 "Address");
9404 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9405 fde->dw_fde_second_begin, "Length");
9410 /* Output the terminator words. */
9411 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9412 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9415 /* Add a new entry to .debug_ranges. Return the offset at which it
9416 was placed. */
9418 static unsigned int
9419 add_ranges_num (int num)
9421 unsigned int in_use = ranges_table_in_use;
9423 if (in_use == ranges_table_allocated)
9425 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9426 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9427 ranges_table_allocated);
9428 memset (ranges_table + ranges_table_in_use, 0,
9429 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9432 ranges_table[in_use].num = num;
9433 ranges_table_in_use = in_use + 1;
9435 return in_use * 2 * DWARF2_ADDR_SIZE;
9438 /* Add a new entry to .debug_ranges corresponding to a block, or a
9439 range terminator if BLOCK is NULL. */
9441 static unsigned int
9442 add_ranges (const_tree block)
9444 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9447 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9448 When using dwarf_split_debug_info, address attributes in dies destined
9449 for the final executable should be direct references--setting the
9450 parameter force_direct ensures this behavior. */
9452 static void
9453 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9454 bool *added, bool force_direct)
9456 unsigned int in_use = ranges_by_label_in_use;
9457 unsigned int offset;
9459 if (in_use == ranges_by_label_allocated)
9461 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9462 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9463 ranges_by_label,
9464 ranges_by_label_allocated);
9465 memset (ranges_by_label + ranges_by_label_in_use, 0,
9466 RANGES_TABLE_INCREMENT
9467 * sizeof (struct dw_ranges_by_label_struct));
9470 ranges_by_label[in_use].begin = begin;
9471 ranges_by_label[in_use].end = end;
9472 ranges_by_label_in_use = in_use + 1;
9474 offset = add_ranges_num (-(int)in_use - 1);
9475 if (!*added)
9477 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9478 *added = true;
9482 static void
9483 output_ranges (void)
9485 unsigned i;
9486 static const char *const start_fmt = "Offset %#x";
9487 const char *fmt = start_fmt;
9489 for (i = 0; i < ranges_table_in_use; i++)
9491 int block_num = ranges_table[i].num;
9493 if (block_num > 0)
9495 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9496 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9498 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9499 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9501 /* If all code is in the text section, then the compilation
9502 unit base address defaults to DW_AT_low_pc, which is the
9503 base of the text section. */
9504 if (!have_multiple_function_sections)
9506 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9507 text_section_label,
9508 fmt, i * 2 * DWARF2_ADDR_SIZE);
9509 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9510 text_section_label, NULL);
9513 /* Otherwise, the compilation unit base address is zero,
9514 which allows us to use absolute addresses, and not worry
9515 about whether the target supports cross-section
9516 arithmetic. */
9517 else
9519 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9520 fmt, i * 2 * DWARF2_ADDR_SIZE);
9521 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9524 fmt = NULL;
9527 /* Negative block_num stands for an index into ranges_by_label. */
9528 else if (block_num < 0)
9530 int lab_idx = - block_num - 1;
9532 if (!have_multiple_function_sections)
9534 gcc_unreachable ();
9535 #if 0
9536 /* If we ever use add_ranges_by_labels () for a single
9537 function section, all we have to do is to take out
9538 the #if 0 above. */
9539 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9540 ranges_by_label[lab_idx].begin,
9541 text_section_label,
9542 fmt, i * 2 * DWARF2_ADDR_SIZE);
9543 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9544 ranges_by_label[lab_idx].end,
9545 text_section_label, NULL);
9546 #endif
9548 else
9550 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9551 ranges_by_label[lab_idx].begin,
9552 fmt, i * 2 * DWARF2_ADDR_SIZE);
9553 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9554 ranges_by_label[lab_idx].end,
9555 NULL);
9558 else
9560 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9561 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9562 fmt = start_fmt;
9567 /* Data structure containing information about input files. */
9568 struct file_info
9570 const char *path; /* Complete file name. */
9571 const char *fname; /* File name part. */
9572 int length; /* Length of entire string. */
9573 struct dwarf_file_data * file_idx; /* Index in input file table. */
9574 int dir_idx; /* Index in directory table. */
9577 /* Data structure containing information about directories with source
9578 files. */
9579 struct dir_info
9581 const char *path; /* Path including directory name. */
9582 int length; /* Path length. */
9583 int prefix; /* Index of directory entry which is a prefix. */
9584 int count; /* Number of files in this directory. */
9585 int dir_idx; /* Index of directory used as base. */
9588 /* Callback function for file_info comparison. We sort by looking at
9589 the directories in the path. */
9591 static int
9592 file_info_cmp (const void *p1, const void *p2)
9594 const struct file_info *const s1 = (const struct file_info *) p1;
9595 const struct file_info *const s2 = (const struct file_info *) p2;
9596 const unsigned char *cp1;
9597 const unsigned char *cp2;
9599 /* Take care of file names without directories. We need to make sure that
9600 we return consistent values to qsort since some will get confused if
9601 we return the same value when identical operands are passed in opposite
9602 orders. So if neither has a directory, return 0 and otherwise return
9603 1 or -1 depending on which one has the directory. */
9604 if ((s1->path == s1->fname || s2->path == s2->fname))
9605 return (s2->path == s2->fname) - (s1->path == s1->fname);
9607 cp1 = (const unsigned char *) s1->path;
9608 cp2 = (const unsigned char *) s2->path;
9610 while (1)
9612 ++cp1;
9613 ++cp2;
9614 /* Reached the end of the first path? If so, handle like above. */
9615 if ((cp1 == (const unsigned char *) s1->fname)
9616 || (cp2 == (const unsigned char *) s2->fname))
9617 return ((cp2 == (const unsigned char *) s2->fname)
9618 - (cp1 == (const unsigned char *) s1->fname));
9620 /* Character of current path component the same? */
9621 else if (*cp1 != *cp2)
9622 return *cp1 - *cp2;
9626 struct file_name_acquire_data
9628 struct file_info *files;
9629 int used_files;
9630 int max_files;
9633 /* Traversal function for the hash table. */
9635 static int
9636 file_name_acquire (void ** slot, void *data)
9638 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9639 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9640 struct file_info *fi;
9641 const char *f;
9643 gcc_assert (fnad->max_files >= d->emitted_number);
9645 if (! d->emitted_number)
9646 return 1;
9648 gcc_assert (fnad->max_files != fnad->used_files);
9650 fi = fnad->files + fnad->used_files++;
9652 /* Skip all leading "./". */
9653 f = d->filename;
9654 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9655 f += 2;
9657 /* Create a new array entry. */
9658 fi->path = f;
9659 fi->length = strlen (f);
9660 fi->file_idx = d;
9662 /* Search for the file name part. */
9663 f = strrchr (f, DIR_SEPARATOR);
9664 #if defined (DIR_SEPARATOR_2)
9666 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9668 if (g != NULL)
9670 if (f == NULL || f < g)
9671 f = g;
9674 #endif
9676 fi->fname = f == NULL ? fi->path : f + 1;
9677 return 1;
9680 /* Output the directory table and the file name table. We try to minimize
9681 the total amount of memory needed. A heuristic is used to avoid large
9682 slowdowns with many input files. */
9684 static void
9685 output_file_names (void)
9687 struct file_name_acquire_data fnad;
9688 int numfiles;
9689 struct file_info *files;
9690 struct dir_info *dirs;
9691 int *saved;
9692 int *savehere;
9693 int *backmap;
9694 int ndirs;
9695 int idx_offset;
9696 int i;
9698 if (!last_emitted_file)
9700 dw2_asm_output_data (1, 0, "End directory table");
9701 dw2_asm_output_data (1, 0, "End file name table");
9702 return;
9705 numfiles = last_emitted_file->emitted_number;
9707 /* Allocate the various arrays we need. */
9708 files = XALLOCAVEC (struct file_info, numfiles);
9709 dirs = XALLOCAVEC (struct dir_info, numfiles);
9711 fnad.files = files;
9712 fnad.used_files = 0;
9713 fnad.max_files = numfiles;
9714 htab_traverse (file_table, file_name_acquire, &fnad);
9715 gcc_assert (fnad.used_files == fnad.max_files);
9717 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9719 /* Find all the different directories used. */
9720 dirs[0].path = files[0].path;
9721 dirs[0].length = files[0].fname - files[0].path;
9722 dirs[0].prefix = -1;
9723 dirs[0].count = 1;
9724 dirs[0].dir_idx = 0;
9725 files[0].dir_idx = 0;
9726 ndirs = 1;
9728 for (i = 1; i < numfiles; i++)
9729 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9730 && memcmp (dirs[ndirs - 1].path, files[i].path,
9731 dirs[ndirs - 1].length) == 0)
9733 /* Same directory as last entry. */
9734 files[i].dir_idx = ndirs - 1;
9735 ++dirs[ndirs - 1].count;
9737 else
9739 int j;
9741 /* This is a new directory. */
9742 dirs[ndirs].path = files[i].path;
9743 dirs[ndirs].length = files[i].fname - files[i].path;
9744 dirs[ndirs].count = 1;
9745 dirs[ndirs].dir_idx = ndirs;
9746 files[i].dir_idx = ndirs;
9748 /* Search for a prefix. */
9749 dirs[ndirs].prefix = -1;
9750 for (j = 0; j < ndirs; j++)
9751 if (dirs[j].length < dirs[ndirs].length
9752 && dirs[j].length > 1
9753 && (dirs[ndirs].prefix == -1
9754 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9755 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9756 dirs[ndirs].prefix = j;
9758 ++ndirs;
9761 /* Now to the actual work. We have to find a subset of the directories which
9762 allow expressing the file name using references to the directory table
9763 with the least amount of characters. We do not do an exhaustive search
9764 where we would have to check out every combination of every single
9765 possible prefix. Instead we use a heuristic which provides nearly optimal
9766 results in most cases and never is much off. */
9767 saved = XALLOCAVEC (int, ndirs);
9768 savehere = XALLOCAVEC (int, ndirs);
9770 memset (saved, '\0', ndirs * sizeof (saved[0]));
9771 for (i = 0; i < ndirs; i++)
9773 int j;
9774 int total;
9776 /* We can always save some space for the current directory. But this
9777 does not mean it will be enough to justify adding the directory. */
9778 savehere[i] = dirs[i].length;
9779 total = (savehere[i] - saved[i]) * dirs[i].count;
9781 for (j = i + 1; j < ndirs; j++)
9783 savehere[j] = 0;
9784 if (saved[j] < dirs[i].length)
9786 /* Determine whether the dirs[i] path is a prefix of the
9787 dirs[j] path. */
9788 int k;
9790 k = dirs[j].prefix;
9791 while (k != -1 && k != (int) i)
9792 k = dirs[k].prefix;
9794 if (k == (int) i)
9796 /* Yes it is. We can possibly save some memory by
9797 writing the filenames in dirs[j] relative to
9798 dirs[i]. */
9799 savehere[j] = dirs[i].length;
9800 total += (savehere[j] - saved[j]) * dirs[j].count;
9805 /* Check whether we can save enough to justify adding the dirs[i]
9806 directory. */
9807 if (total > dirs[i].length + 1)
9809 /* It's worthwhile adding. */
9810 for (j = i; j < ndirs; j++)
9811 if (savehere[j] > 0)
9813 /* Remember how much we saved for this directory so far. */
9814 saved[j] = savehere[j];
9816 /* Remember the prefix directory. */
9817 dirs[j].dir_idx = i;
9822 /* Emit the directory name table. */
9823 idx_offset = dirs[0].length > 0 ? 1 : 0;
9824 for (i = 1 - idx_offset; i < ndirs; i++)
9825 dw2_asm_output_nstring (dirs[i].path,
9826 dirs[i].length
9827 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9828 "Directory Entry: %#x", i + idx_offset);
9830 dw2_asm_output_data (1, 0, "End directory table");
9832 /* We have to emit them in the order of emitted_number since that's
9833 used in the debug info generation. To do this efficiently we
9834 generate a back-mapping of the indices first. */
9835 backmap = XALLOCAVEC (int, numfiles);
9836 for (i = 0; i < numfiles; i++)
9837 backmap[files[i].file_idx->emitted_number - 1] = i;
9839 /* Now write all the file names. */
9840 for (i = 0; i < numfiles; i++)
9842 int file_idx = backmap[i];
9843 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9845 #ifdef VMS_DEBUGGING_INFO
9846 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9848 /* Setting these fields can lead to debugger miscomparisons,
9849 but VMS Debug requires them to be set correctly. */
9851 int ver;
9852 long long cdt;
9853 long siz;
9854 int maxfilelen = strlen (files[file_idx].path)
9855 + dirs[dir_idx].length
9856 + MAX_VMS_VERSION_LEN + 1;
9857 char *filebuf = XALLOCAVEC (char, maxfilelen);
9859 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9860 snprintf (filebuf, maxfilelen, "%s;%d",
9861 files[file_idx].path + dirs[dir_idx].length, ver);
9863 dw2_asm_output_nstring
9864 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9866 /* Include directory index. */
9867 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9869 /* Modification time. */
9870 dw2_asm_output_data_uleb128
9871 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9872 ? cdt : 0,
9873 NULL);
9875 /* File length in bytes. */
9876 dw2_asm_output_data_uleb128
9877 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9878 ? siz : 0,
9879 NULL);
9880 #else
9881 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9882 "File Entry: %#x", (unsigned) i + 1);
9884 /* Include directory index. */
9885 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9887 /* Modification time. */
9888 dw2_asm_output_data_uleb128 (0, NULL);
9890 /* File length in bytes. */
9891 dw2_asm_output_data_uleb128 (0, NULL);
9892 #endif /* VMS_DEBUGGING_INFO */
9895 dw2_asm_output_data (1, 0, "End file name table");
9899 /* Output one line number table into the .debug_line section. */
9901 static void
9902 output_one_line_info_table (dw_line_info_table *table)
9904 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9905 unsigned int current_line = 1;
9906 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9907 dw_line_info_entry *ent;
9908 size_t i;
9910 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9912 switch (ent->opcode)
9914 case LI_set_address:
9915 /* ??? Unfortunately, we have little choice here currently, and
9916 must always use the most general form. GCC does not know the
9917 address delta itself, so we can't use DW_LNS_advance_pc. Many
9918 ports do have length attributes which will give an upper bound
9919 on the address range. We could perhaps use length attributes
9920 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9921 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9923 /* This can handle any delta. This takes
9924 4+DWARF2_ADDR_SIZE bytes. */
9925 dw2_asm_output_data (1, 0, "set address %s", line_label);
9926 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9927 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9928 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9929 break;
9931 case LI_set_line:
9932 if (ent->val == current_line)
9934 /* We still need to start a new row, so output a copy insn. */
9935 dw2_asm_output_data (1, DW_LNS_copy,
9936 "copy line %u", current_line);
9938 else
9940 int line_offset = ent->val - current_line;
9941 int line_delta = line_offset - DWARF_LINE_BASE;
9943 current_line = ent->val;
9944 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9946 /* This can handle deltas from -10 to 234, using the current
9947 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9948 This takes 1 byte. */
9949 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9950 "line %u", current_line);
9952 else
9954 /* This can handle any delta. This takes at least 4 bytes,
9955 depending on the value being encoded. */
9956 dw2_asm_output_data (1, DW_LNS_advance_line,
9957 "advance to line %u", current_line);
9958 dw2_asm_output_data_sleb128 (line_offset, NULL);
9959 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9962 break;
9964 case LI_set_file:
9965 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9966 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9967 break;
9969 case LI_set_column:
9970 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9971 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9972 break;
9974 case LI_negate_stmt:
9975 current_is_stmt = !current_is_stmt;
9976 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9977 "is_stmt %d", current_is_stmt);
9978 break;
9980 case LI_set_prologue_end:
9981 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9982 "set prologue end");
9983 break;
9985 case LI_set_epilogue_begin:
9986 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9987 "set epilogue begin");
9988 break;
9990 case LI_set_discriminator:
9991 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9992 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9993 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9994 dw2_asm_output_data_uleb128 (ent->val, NULL);
9995 break;
9999 /* Emit debug info for the address of the end of the table. */
10000 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10001 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10002 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10003 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10005 dw2_asm_output_data (1, 0, "end sequence");
10006 dw2_asm_output_data_uleb128 (1, NULL);
10007 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10010 /* Output the source line number correspondence information. This
10011 information goes into the .debug_line section. */
10013 static void
10014 output_line_info (bool prologue_only)
10016 char l1[20], l2[20], p1[20], p2[20];
10017 int ver = dwarf_version;
10018 bool saw_one = false;
10019 int opc;
10021 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10022 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10023 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10024 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10026 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10027 dw2_asm_output_data (4, 0xffffffff,
10028 "Initial length escape value indicating 64-bit DWARF extension");
10029 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10030 "Length of Source Line Info");
10031 ASM_OUTPUT_LABEL (asm_out_file, l1);
10033 dw2_asm_output_data (2, ver, "DWARF Version");
10034 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10035 ASM_OUTPUT_LABEL (asm_out_file, p1);
10037 /* Define the architecture-dependent minimum instruction length (in bytes).
10038 In this implementation of DWARF, this field is used for information
10039 purposes only. Since GCC generates assembly language, we have no
10040 a priori knowledge of how many instruction bytes are generated for each
10041 source line, and therefore can use only the DW_LNE_set_address and
10042 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10043 this as '1', which is "correct enough" for all architectures,
10044 and don't let the target override. */
10045 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10047 if (ver >= 4)
10048 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10049 "Maximum Operations Per Instruction");
10050 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10051 "Default is_stmt_start flag");
10052 dw2_asm_output_data (1, DWARF_LINE_BASE,
10053 "Line Base Value (Special Opcodes)");
10054 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10055 "Line Range Value (Special Opcodes)");
10056 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10057 "Special Opcode Base");
10059 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10061 int n_op_args;
10062 switch (opc)
10064 case DW_LNS_advance_pc:
10065 case DW_LNS_advance_line:
10066 case DW_LNS_set_file:
10067 case DW_LNS_set_column:
10068 case DW_LNS_fixed_advance_pc:
10069 case DW_LNS_set_isa:
10070 n_op_args = 1;
10071 break;
10072 default:
10073 n_op_args = 0;
10074 break;
10077 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10078 opc, n_op_args);
10081 /* Write out the information about the files we use. */
10082 output_file_names ();
10083 ASM_OUTPUT_LABEL (asm_out_file, p2);
10084 if (prologue_only)
10086 /* Output the marker for the end of the line number info. */
10087 ASM_OUTPUT_LABEL (asm_out_file, l2);
10088 return;
10091 if (separate_line_info)
10093 dw_line_info_table *table;
10094 size_t i;
10096 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10097 if (table->in_use)
10099 output_one_line_info_table (table);
10100 saw_one = true;
10103 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10105 output_one_line_info_table (cold_text_section_line_info);
10106 saw_one = true;
10109 /* ??? Some Darwin linkers crash on a .debug_line section with no
10110 sequences. Further, merely a DW_LNE_end_sequence entry is not
10111 sufficient -- the address column must also be initialized.
10112 Make sure to output at least one set_address/end_sequence pair,
10113 choosing .text since that section is always present. */
10114 if (text_section_line_info->in_use || !saw_one)
10115 output_one_line_info_table (text_section_line_info);
10117 /* Output the marker for the end of the line number info. */
10118 ASM_OUTPUT_LABEL (asm_out_file, l2);
10121 /* Given a pointer to a tree node for some base type, return a pointer to
10122 a DIE that describes the given type.
10124 This routine must only be called for GCC type nodes that correspond to
10125 Dwarf base (fundamental) types. */
10127 static dw_die_ref
10128 base_type_die (tree type)
10130 dw_die_ref base_type_result;
10131 enum dwarf_type encoding;
10133 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10134 return 0;
10136 /* If this is a subtype that should not be emitted as a subrange type,
10137 use the base type. See subrange_type_for_debug_p. */
10138 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10139 type = TREE_TYPE (type);
10141 switch (TREE_CODE (type))
10143 case INTEGER_TYPE:
10144 if ((dwarf_version >= 4 || !dwarf_strict)
10145 && TYPE_NAME (type)
10146 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10147 && DECL_IS_BUILTIN (TYPE_NAME (type))
10148 && DECL_NAME (TYPE_NAME (type)))
10150 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10151 if (strcmp (name, "char16_t") == 0
10152 || strcmp (name, "char32_t") == 0)
10154 encoding = DW_ATE_UTF;
10155 break;
10158 if (TYPE_STRING_FLAG (type))
10160 if (TYPE_UNSIGNED (type))
10161 encoding = DW_ATE_unsigned_char;
10162 else
10163 encoding = DW_ATE_signed_char;
10165 else if (TYPE_UNSIGNED (type))
10166 encoding = DW_ATE_unsigned;
10167 else
10168 encoding = DW_ATE_signed;
10169 break;
10171 case REAL_TYPE:
10172 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10174 if (dwarf_version >= 3 || !dwarf_strict)
10175 encoding = DW_ATE_decimal_float;
10176 else
10177 encoding = DW_ATE_lo_user;
10179 else
10180 encoding = DW_ATE_float;
10181 break;
10183 case FIXED_POINT_TYPE:
10184 if (!(dwarf_version >= 3 || !dwarf_strict))
10185 encoding = DW_ATE_lo_user;
10186 else if (TYPE_UNSIGNED (type))
10187 encoding = DW_ATE_unsigned_fixed;
10188 else
10189 encoding = DW_ATE_signed_fixed;
10190 break;
10192 /* Dwarf2 doesn't know anything about complex ints, so use
10193 a user defined type for it. */
10194 case COMPLEX_TYPE:
10195 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10196 encoding = DW_ATE_complex_float;
10197 else
10198 encoding = DW_ATE_lo_user;
10199 break;
10201 case BOOLEAN_TYPE:
10202 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10203 encoding = DW_ATE_boolean;
10204 break;
10206 default:
10207 /* No other TREE_CODEs are Dwarf fundamental types. */
10208 gcc_unreachable ();
10211 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10213 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10214 int_size_in_bytes (type));
10215 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10216 add_pubtype (type, base_type_result);
10218 return base_type_result;
10221 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10222 named 'auto' in its type: return true for it, false otherwise. */
10224 static inline bool
10225 is_cxx_auto (tree type)
10227 if (is_cxx ())
10229 tree name = TYPE_NAME (type);
10230 if (TREE_CODE (name) == TYPE_DECL)
10231 name = DECL_NAME (name);
10232 if (name == get_identifier ("auto"))
10233 return true;
10235 return false;
10238 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10239 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10241 static inline int
10242 is_base_type (tree type)
10244 switch (TREE_CODE (type))
10246 case ERROR_MARK:
10247 case VOID_TYPE:
10248 case INTEGER_TYPE:
10249 case REAL_TYPE:
10250 case FIXED_POINT_TYPE:
10251 case COMPLEX_TYPE:
10252 case BOOLEAN_TYPE:
10253 return 1;
10255 case ARRAY_TYPE:
10256 case RECORD_TYPE:
10257 case UNION_TYPE:
10258 case QUAL_UNION_TYPE:
10259 case ENUMERAL_TYPE:
10260 case FUNCTION_TYPE:
10261 case METHOD_TYPE:
10262 case POINTER_TYPE:
10263 case REFERENCE_TYPE:
10264 case NULLPTR_TYPE:
10265 case OFFSET_TYPE:
10266 case LANG_TYPE:
10267 case VECTOR_TYPE:
10268 return 0;
10270 default:
10271 if (is_cxx_auto (type))
10272 return 0;
10273 gcc_unreachable ();
10276 return 0;
10279 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10280 node, return the size in bits for the type if it is a constant, or else
10281 return the alignment for the type if the type's size is not constant, or
10282 else return BITS_PER_WORD if the type actually turns out to be an
10283 ERROR_MARK node. */
10285 static inline unsigned HOST_WIDE_INT
10286 simple_type_size_in_bits (const_tree type)
10288 if (TREE_CODE (type) == ERROR_MARK)
10289 return BITS_PER_WORD;
10290 else if (TYPE_SIZE (type) == NULL_TREE)
10291 return 0;
10292 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10293 return tree_to_uhwi (TYPE_SIZE (type));
10294 else
10295 return TYPE_ALIGN (type);
10298 /* Similarly, but return a double_int instead of UHWI. */
10300 static inline double_int
10301 double_int_type_size_in_bits (const_tree type)
10303 if (TREE_CODE (type) == ERROR_MARK)
10304 return double_int::from_uhwi (BITS_PER_WORD);
10305 else if (TYPE_SIZE (type) == NULL_TREE)
10306 return double_int_zero;
10307 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10308 return tree_to_double_int (TYPE_SIZE (type));
10309 else
10310 return double_int::from_uhwi (TYPE_ALIGN (type));
10313 /* Given a pointer to a tree node for a subrange type, return a pointer
10314 to a DIE that describes the given type. */
10316 static dw_die_ref
10317 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10319 dw_die_ref subrange_die;
10320 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10322 if (context_die == NULL)
10323 context_die = comp_unit_die ();
10325 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10327 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10329 /* The size of the subrange type and its base type do not match,
10330 so we need to generate a size attribute for the subrange type. */
10331 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10334 if (low)
10335 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10336 if (high)
10337 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10339 return subrange_die;
10342 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10343 entry that chains various modifiers in front of the given type. */
10345 static dw_die_ref
10346 modified_type_die (tree type, int type_quals,
10347 dw_die_ref context_die)
10349 const int is_const_type = ((type_quals & TYPE_QUAL_CONST) != 0);
10350 const int is_volatile_type = ((type_quals & TYPE_QUAL_VOLATILE) != 0);
10351 enum tree_code code = TREE_CODE (type);
10352 dw_die_ref mod_type_die;
10353 dw_die_ref sub_die = NULL;
10354 tree item_type = NULL;
10355 tree qualified_type;
10356 tree name, low, high;
10357 dw_die_ref mod_scope;
10359 if (code == ERROR_MARK)
10360 return NULL;
10362 /* See if we already have the appropriately qualified variant of
10363 this type. */
10364 qualified_type
10365 = get_qualified_type (type, type_quals);
10367 if (qualified_type == sizetype
10368 && TYPE_NAME (qualified_type)
10369 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10371 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10373 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10374 && TYPE_PRECISION (t)
10375 == TYPE_PRECISION (qualified_type)
10376 && TYPE_UNSIGNED (t)
10377 == TYPE_UNSIGNED (qualified_type));
10378 qualified_type = t;
10381 /* If we do, then we can just use its DIE, if it exists. */
10382 if (qualified_type)
10384 mod_type_die = lookup_type_die (qualified_type);
10385 if (mod_type_die)
10386 return mod_type_die;
10389 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10391 /* Handle C typedef types. */
10392 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10393 && !DECL_ARTIFICIAL (name))
10395 tree dtype = TREE_TYPE (name);
10397 if (qualified_type == dtype)
10399 /* For a named type, use the typedef. */
10400 gen_type_die (qualified_type, context_die);
10401 return lookup_type_die (qualified_type);
10403 else if (type_quals == TYPE_QUALS (dtype))
10404 /* cv-unqualified version of named type. Just use the unnamed
10405 type to which it refers. */
10406 return modified_type_die (DECL_ORIGINAL_TYPE (name), type_quals, context_die);
10407 /* Else cv-qualified version of named type; fall through. */
10410 mod_scope = scope_die_for (type, context_die);
10412 if ((type_quals & TYPE_QUAL_CONST)
10413 /* If both is_const_type and is_volatile_type, prefer the path
10414 which leads to a qualified type. */
10415 && (!is_volatile_type
10416 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10417 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10419 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10420 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_CONST, context_die);
10422 else if (type_quals & TYPE_QUAL_VOLATILE)
10424 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10425 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_VOLATILE, context_die);
10427 else if (use_upc_dwarf2_extensions
10428 && (type_quals & TYPE_QUAL_SHARED))
10430 HOST_WIDE_INT block_factor = 1;
10432 /* Inside the compiler,
10433 "shared int x;" TYPE_BLOCK_FACTOR is null.
10434 "shared [] int *p;" TYPE_BLOCK_FACTOR is zero.
10435 "shared [10] int x[50];" TYPE_BLOCK_FACTOR is 10 * bitsize(int)
10436 The DWARF2 encoding is as follows:
10437 "shared int x;" DW_AT_count: 1
10438 "shared [] int *p;" <no DW_AT_count attribute>
10439 "shared [10] int x[50];" DW_AT_count: 10
10440 The logic below handles thse various contingencies. */
10442 mod_type_die = new_die (DW_TAG_upc_shared_type,
10443 comp_unit_die (), type);
10445 if (TYPE_HAS_BLOCK_FACTOR (type))
10446 block_factor = TREE_INT_CST_LOW (TYPE_BLOCK_FACTOR (type));
10448 if (block_factor != 0)
10449 add_AT_unsigned (mod_type_die, DW_AT_count, block_factor);
10451 sub_die = modified_type_die (type,
10452 type_quals & ~TYPE_QUAL_SHARED,
10453 context_die);
10455 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_STRICT)
10457 mod_type_die = new_die (DW_TAG_upc_strict_type,
10458 comp_unit_die (), type);
10459 sub_die = modified_type_die (type,
10460 type_quals & ~TYPE_QUAL_STRICT,
10461 context_die);
10463 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_RELAXED)
10465 mod_type_die = new_die (DW_TAG_upc_relaxed_type,
10466 comp_unit_die (), type);
10467 sub_die = modified_type_die (type,
10468 type_quals & ~TYPE_QUAL_RELAXED,
10469 context_die);
10471 else if (code == POINTER_TYPE)
10473 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10474 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10475 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10476 item_type = TREE_TYPE (type);
10477 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10478 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10479 TYPE_ADDR_SPACE (item_type));
10481 else if (code == REFERENCE_TYPE)
10483 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10484 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10485 type);
10486 else
10487 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10488 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10489 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10490 item_type = TREE_TYPE (type);
10491 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10492 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10493 TYPE_ADDR_SPACE (item_type));
10495 else if (code == INTEGER_TYPE
10496 && TREE_TYPE (type) != NULL_TREE
10497 && subrange_type_for_debug_p (type, &low, &high))
10499 mod_type_die = subrange_type_die (type, low, high, context_die);
10500 item_type = TREE_TYPE (type);
10502 else if (is_base_type (type))
10503 mod_type_die = base_type_die (type);
10504 else
10506 gen_type_die (type, context_die);
10508 /* We have to get the type_main_variant here (and pass that to the
10509 `lookup_type_die' routine) because the ..._TYPE node we have
10510 might simply be a *copy* of some original type node (where the
10511 copy was created to help us keep track of typedef names) and
10512 that copy might have a different TYPE_UID from the original
10513 ..._TYPE node. */
10514 if (TREE_CODE (type) != VECTOR_TYPE)
10515 return lookup_type_die (type_main_variant (type));
10516 else
10517 /* Vectors have the debugging information in the type,
10518 not the main variant. */
10519 return lookup_type_die (type);
10522 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10523 don't output a DW_TAG_typedef, since there isn't one in the
10524 user's program; just attach a DW_AT_name to the type.
10525 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10526 if the base type already has the same name. */
10527 if (name
10528 && ((TREE_CODE (name) != TYPE_DECL
10529 && (qualified_type == TYPE_MAIN_VARIANT (type)
10530 || (!is_const_type && !is_volatile_type)))
10531 || (TREE_CODE (name) == TYPE_DECL
10532 && TREE_TYPE (name) == qualified_type
10533 && DECL_NAME (name))))
10535 if (TREE_CODE (name) == TYPE_DECL)
10536 /* Could just call add_name_and_src_coords_attributes here,
10537 but since this is a builtin type it doesn't have any
10538 useful source coordinates anyway. */
10539 name = DECL_NAME (name);
10540 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10542 /* This probably indicates a bug. */
10543 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10545 name = TYPE_NAME (type);
10546 if (name
10547 && TREE_CODE (name) == TYPE_DECL)
10548 name = DECL_NAME (name);
10549 add_name_attribute (mod_type_die,
10550 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10553 if (qualified_type)
10554 equate_type_number_to_die (qualified_type, mod_type_die);
10556 if (item_type)
10557 /* We must do this after the equate_type_number_to_die call, in case
10558 this is a recursive type. This ensures that the modified_type_die
10559 recursion will terminate even if the type is recursive. Recursive
10560 types are possible in Ada. */
10561 sub_die = modified_type_die (item_type, TYPE_QUALS (item_type), context_die);
10563 if (sub_die != NULL)
10564 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10566 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10567 if (TYPE_ARTIFICIAL (type))
10568 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10570 return mod_type_die;
10573 /* Generate DIEs for the generic parameters of T.
10574 T must be either a generic type or a generic function.
10575 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10577 static void
10578 gen_generic_params_dies (tree t)
10580 tree parms, args;
10581 int parms_num, i;
10582 dw_die_ref die = NULL;
10583 int non_default;
10585 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10586 return;
10588 if (TYPE_P (t))
10589 die = lookup_type_die (t);
10590 else if (DECL_P (t))
10591 die = lookup_decl_die (t);
10593 gcc_assert (die);
10595 parms = lang_hooks.get_innermost_generic_parms (t);
10596 if (!parms)
10597 /* T has no generic parameter. It means T is neither a generic type
10598 or function. End of story. */
10599 return;
10601 parms_num = TREE_VEC_LENGTH (parms);
10602 args = lang_hooks.get_innermost_generic_args (t);
10603 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10604 non_default = int_cst_value (TREE_CHAIN (args));
10605 else
10606 non_default = TREE_VEC_LENGTH (args);
10607 for (i = 0; i < parms_num; i++)
10609 tree parm, arg, arg_pack_elems;
10610 dw_die_ref parm_die;
10612 parm = TREE_VEC_ELT (parms, i);
10613 arg = TREE_VEC_ELT (args, i);
10614 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10615 gcc_assert (parm && TREE_VALUE (parm) && arg);
10617 if (parm && TREE_VALUE (parm) && arg)
10619 /* If PARM represents a template parameter pack,
10620 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10621 by DW_TAG_template_*_parameter DIEs for the argument
10622 pack elements of ARG. Note that ARG would then be
10623 an argument pack. */
10624 if (arg_pack_elems)
10625 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10626 arg_pack_elems,
10627 die);
10628 else
10629 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10630 true /* emit name */, die);
10631 if (i >= non_default)
10632 add_AT_flag (parm_die, DW_AT_default_value, 1);
10637 /* Create and return a DIE for PARM which should be
10638 the representation of a generic type parameter.
10639 For instance, in the C++ front end, PARM would be a template parameter.
10640 ARG is the argument to PARM.
10641 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10642 name of the PARM.
10643 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10644 as a child node. */
10646 static dw_die_ref
10647 generic_parameter_die (tree parm, tree arg,
10648 bool emit_name_p,
10649 dw_die_ref parent_die)
10651 dw_die_ref tmpl_die = NULL;
10652 const char *name = NULL;
10654 if (!parm || !DECL_NAME (parm) || !arg)
10655 return NULL;
10657 /* We support non-type generic parameters and arguments,
10658 type generic parameters and arguments, as well as
10659 generic generic parameters (a.k.a. template template parameters in C++)
10660 and arguments. */
10661 if (TREE_CODE (parm) == PARM_DECL)
10662 /* PARM is a nontype generic parameter */
10663 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10664 else if (TREE_CODE (parm) == TYPE_DECL)
10665 /* PARM is a type generic parameter. */
10666 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10667 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10668 /* PARM is a generic generic parameter.
10669 Its DIE is a GNU extension. It shall have a
10670 DW_AT_name attribute to represent the name of the template template
10671 parameter, and a DW_AT_GNU_template_name attribute to represent the
10672 name of the template template argument. */
10673 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10674 parent_die, parm);
10675 else
10676 gcc_unreachable ();
10678 if (tmpl_die)
10680 tree tmpl_type;
10682 /* If PARM is a generic parameter pack, it means we are
10683 emitting debug info for a template argument pack element.
10684 In other terms, ARG is a template argument pack element.
10685 In that case, we don't emit any DW_AT_name attribute for
10686 the die. */
10687 if (emit_name_p)
10689 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10690 gcc_assert (name);
10691 add_AT_string (tmpl_die, DW_AT_name, name);
10694 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10696 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10697 TMPL_DIE should have a child DW_AT_type attribute that is set
10698 to the type of the argument to PARM, which is ARG.
10699 If PARM is a type generic parameter, TMPL_DIE should have a
10700 child DW_AT_type that is set to ARG. */
10701 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10702 add_type_attribute (tmpl_die, tmpl_type, 0,
10703 TREE_THIS_VOLATILE (tmpl_type),
10704 parent_die);
10706 else
10708 /* So TMPL_DIE is a DIE representing a
10709 a generic generic template parameter, a.k.a template template
10710 parameter in C++ and arg is a template. */
10712 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10713 to the name of the argument. */
10714 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10715 if (name)
10716 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10719 if (TREE_CODE (parm) == PARM_DECL)
10720 /* So PARM is a non-type generic parameter.
10721 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10722 attribute of TMPL_DIE which value represents the value
10723 of ARG.
10724 We must be careful here:
10725 The value of ARG might reference some function decls.
10726 We might currently be emitting debug info for a generic
10727 type and types are emitted before function decls, we don't
10728 know if the function decls referenced by ARG will actually be
10729 emitted after cgraph computations.
10730 So must defer the generation of the DW_AT_const_value to
10731 after cgraph is ready. */
10732 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10735 return tmpl_die;
10738 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10739 PARM_PACK must be a template parameter pack. The returned DIE
10740 will be child DIE of PARENT_DIE. */
10742 static dw_die_ref
10743 template_parameter_pack_die (tree parm_pack,
10744 tree parm_pack_args,
10745 dw_die_ref parent_die)
10747 dw_die_ref die;
10748 int j;
10750 gcc_assert (parent_die && parm_pack);
10752 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10753 add_name_and_src_coords_attributes (die, parm_pack);
10754 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10755 generic_parameter_die (parm_pack,
10756 TREE_VEC_ELT (parm_pack_args, j),
10757 false /* Don't emit DW_AT_name */,
10758 die);
10759 return die;
10762 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10763 an enumerated type. */
10765 static inline int
10766 type_is_enum (const_tree type)
10768 return TREE_CODE (type) == ENUMERAL_TYPE;
10771 /* Return the DBX register number described by a given RTL node. */
10773 static unsigned int
10774 dbx_reg_number (const_rtx rtl)
10776 unsigned regno = REGNO (rtl);
10778 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10780 #ifdef LEAF_REG_REMAP
10781 if (crtl->uses_only_leaf_regs)
10783 int leaf_reg = LEAF_REG_REMAP (regno);
10784 if (leaf_reg != -1)
10785 regno = (unsigned) leaf_reg;
10787 #endif
10789 regno = DBX_REGISTER_NUMBER (regno);
10790 gcc_assert (regno != INVALID_REGNUM);
10791 return regno;
10794 /* Optionally add a DW_OP_piece term to a location description expression.
10795 DW_OP_piece is only added if the location description expression already
10796 doesn't end with DW_OP_piece. */
10798 static void
10799 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10801 dw_loc_descr_ref loc;
10803 if (*list_head != NULL)
10805 /* Find the end of the chain. */
10806 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10809 if (loc->dw_loc_opc != DW_OP_piece)
10810 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10814 /* Return a location descriptor that designates a machine register or
10815 zero if there is none. */
10817 static dw_loc_descr_ref
10818 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10820 rtx regs;
10822 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10823 return 0;
10825 /* We only use "frame base" when we're sure we're talking about the
10826 post-prologue local stack frame. We do this by *not* running
10827 register elimination until this point, and recognizing the special
10828 argument pointer and soft frame pointer rtx's.
10829 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10830 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10831 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10833 dw_loc_descr_ref result = NULL;
10835 if (dwarf_version >= 4 || !dwarf_strict)
10837 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10838 initialized);
10839 if (result)
10840 add_loc_descr (&result,
10841 new_loc_descr (DW_OP_stack_value, 0, 0));
10843 return result;
10846 regs = targetm.dwarf_register_span (rtl);
10848 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10849 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10850 else
10852 unsigned int dbx_regnum = dbx_reg_number (rtl);
10853 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10854 return 0;
10855 return one_reg_loc_descriptor (dbx_regnum, initialized);
10859 /* Return a location descriptor that designates a machine register for
10860 a given hard register number. */
10862 static dw_loc_descr_ref
10863 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10865 dw_loc_descr_ref reg_loc_descr;
10867 if (regno <= 31)
10868 reg_loc_descr
10869 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10870 else
10871 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10873 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10874 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10876 return reg_loc_descr;
10879 /* Given an RTL of a register, return a location descriptor that
10880 designates a value that spans more than one register. */
10882 static dw_loc_descr_ref
10883 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10884 enum var_init_status initialized)
10886 int size, i;
10887 dw_loc_descr_ref loc_result = NULL;
10889 /* Simple, contiguous registers. */
10890 if (regs == NULL_RTX)
10892 unsigned reg = REGNO (rtl);
10893 int nregs;
10895 #ifdef LEAF_REG_REMAP
10896 if (crtl->uses_only_leaf_regs)
10898 int leaf_reg = LEAF_REG_REMAP (reg);
10899 if (leaf_reg != -1)
10900 reg = (unsigned) leaf_reg;
10902 #endif
10904 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10905 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10907 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10909 loc_result = NULL;
10910 while (nregs--)
10912 dw_loc_descr_ref t;
10914 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10915 VAR_INIT_STATUS_INITIALIZED);
10916 add_loc_descr (&loc_result, t);
10917 add_loc_descr_op_piece (&loc_result, size);
10918 ++reg;
10920 return loc_result;
10923 /* Now onto stupid register sets in non contiguous locations. */
10925 gcc_assert (GET_CODE (regs) == PARALLEL);
10927 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10928 loc_result = NULL;
10930 for (i = 0; i < XVECLEN (regs, 0); ++i)
10932 dw_loc_descr_ref t;
10934 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10935 VAR_INIT_STATUS_INITIALIZED);
10936 add_loc_descr (&loc_result, t);
10937 add_loc_descr_op_piece (&loc_result, size);
10940 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10941 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10942 return loc_result;
10945 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10947 /* Return a location descriptor that designates a constant i,
10948 as a compound operation from constant (i >> shift), constant shift
10949 and DW_OP_shl. */
10951 static dw_loc_descr_ref
10952 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10954 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10955 add_loc_descr (&ret, int_loc_descriptor (shift));
10956 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10957 return ret;
10960 /* Return a location descriptor that designates a constant. */
10962 static dw_loc_descr_ref
10963 int_loc_descriptor (HOST_WIDE_INT i)
10965 enum dwarf_location_atom op;
10967 /* Pick the smallest representation of a constant, rather than just
10968 defaulting to the LEB encoding. */
10969 if (i >= 0)
10971 int clz = clz_hwi (i);
10972 int ctz = ctz_hwi (i);
10973 if (i <= 31)
10974 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10975 else if (i <= 0xff)
10976 op = DW_OP_const1u;
10977 else if (i <= 0xffff)
10978 op = DW_OP_const2u;
10979 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10980 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10981 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10982 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10983 while DW_OP_const4u is 5 bytes. */
10984 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10985 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10986 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10987 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10988 while DW_OP_const4u is 5 bytes. */
10989 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10990 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10991 op = DW_OP_const4u;
10992 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10993 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10994 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10995 while DW_OP_constu of constant >= 0x100000000 takes at least
10996 6 bytes. */
10997 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10998 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10999 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11000 >= HOST_BITS_PER_WIDE_INT)
11001 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11002 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11003 while DW_OP_constu takes in this case at least 6 bytes. */
11004 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11005 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11006 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11007 && size_of_uleb128 (i) > 6)
11008 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11009 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11010 else
11011 op = DW_OP_constu;
11013 else
11015 if (i >= -0x80)
11016 op = DW_OP_const1s;
11017 else if (i >= -0x8000)
11018 op = DW_OP_const2s;
11019 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11021 if (size_of_int_loc_descriptor (i) < 5)
11023 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11024 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11025 return ret;
11027 op = DW_OP_const4s;
11029 else
11031 if (size_of_int_loc_descriptor (i)
11032 < (unsigned long) 1 + size_of_sleb128 (i))
11034 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11035 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11036 return ret;
11038 op = DW_OP_consts;
11042 return new_loc_descr (op, i, 0);
11045 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11046 without actually allocating it. */
11048 static unsigned long
11049 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11051 return size_of_int_loc_descriptor (i >> shift)
11052 + size_of_int_loc_descriptor (shift)
11053 + 1;
11056 /* Return size_of_locs (int_loc_descriptor (i)) without
11057 actually allocating it. */
11059 static unsigned long
11060 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11062 unsigned long s;
11064 if (i >= 0)
11066 int clz, ctz;
11067 if (i <= 31)
11068 return 1;
11069 else if (i <= 0xff)
11070 return 2;
11071 else if (i <= 0xffff)
11072 return 3;
11073 clz = clz_hwi (i);
11074 ctz = ctz_hwi (i);
11075 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11076 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11077 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11078 - clz - 5);
11079 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11080 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11081 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11082 - clz - 8);
11083 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11084 return 5;
11085 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11086 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11087 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11088 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11089 - clz - 8);
11090 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11091 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11092 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11093 - clz - 16);
11094 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11095 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11096 && s > 6)
11097 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11098 - clz - 32);
11099 else
11100 return 1 + s;
11102 else
11104 if (i >= -0x80)
11105 return 2;
11106 else if (i >= -0x8000)
11107 return 3;
11108 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11110 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11112 s = size_of_int_loc_descriptor (-i) + 1;
11113 if (s < 5)
11114 return s;
11116 return 5;
11118 else
11120 unsigned long r = 1 + size_of_sleb128 (i);
11121 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11123 s = size_of_int_loc_descriptor (-i) + 1;
11124 if (s < r)
11125 return s;
11127 return r;
11132 /* Return loc description representing "address" of integer value.
11133 This can appear only as toplevel expression. */
11135 static dw_loc_descr_ref
11136 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11138 int litsize;
11139 dw_loc_descr_ref loc_result = NULL;
11141 if (!(dwarf_version >= 4 || !dwarf_strict))
11142 return NULL;
11144 litsize = size_of_int_loc_descriptor (i);
11145 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11146 is more compact. For DW_OP_stack_value we need:
11147 litsize + 1 (DW_OP_stack_value)
11148 and for DW_OP_implicit_value:
11149 1 (DW_OP_implicit_value) + 1 (length) + size. */
11150 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11152 loc_result = int_loc_descriptor (i);
11153 add_loc_descr (&loc_result,
11154 new_loc_descr (DW_OP_stack_value, 0, 0));
11155 return loc_result;
11158 loc_result = new_loc_descr (DW_OP_implicit_value,
11159 size, 0);
11160 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11161 loc_result->dw_loc_oprnd2.v.val_int = i;
11162 return loc_result;
11165 /* Return a location descriptor that designates a base+offset location. */
11167 static dw_loc_descr_ref
11168 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11169 enum var_init_status initialized)
11171 unsigned int regno;
11172 dw_loc_descr_ref result;
11173 dw_fde_ref fde = cfun->fde;
11175 /* We only use "frame base" when we're sure we're talking about the
11176 post-prologue local stack frame. We do this by *not* running
11177 register elimination until this point, and recognizing the special
11178 argument pointer and soft frame pointer rtx's. */
11179 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11181 rtx elim = (ira_use_lra_p
11182 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11183 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11185 if (elim != reg)
11187 if (GET_CODE (elim) == PLUS)
11189 offset += INTVAL (XEXP (elim, 1));
11190 elim = XEXP (elim, 0);
11192 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11193 && (elim == hard_frame_pointer_rtx
11194 || elim == stack_pointer_rtx))
11195 || elim == (frame_pointer_needed
11196 ? hard_frame_pointer_rtx
11197 : stack_pointer_rtx));
11199 /* If drap register is used to align stack, use frame
11200 pointer + offset to access stack variables. If stack
11201 is aligned without drap, use stack pointer + offset to
11202 access stack variables. */
11203 if (crtl->stack_realign_tried
11204 && reg == frame_pointer_rtx)
11206 int base_reg
11207 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11208 ? HARD_FRAME_POINTER_REGNUM
11209 : REGNO (elim));
11210 return new_reg_loc_descr (base_reg, offset);
11213 gcc_assert (frame_pointer_fb_offset_valid);
11214 offset += frame_pointer_fb_offset;
11215 return new_loc_descr (DW_OP_fbreg, offset, 0);
11219 regno = REGNO (reg);
11220 #ifdef LEAF_REG_REMAP
11221 if (crtl->uses_only_leaf_regs)
11223 int leaf_reg = LEAF_REG_REMAP (regno);
11224 if (leaf_reg != -1)
11225 regno = (unsigned) leaf_reg;
11227 #endif
11228 regno = DWARF_FRAME_REGNUM (regno);
11230 if (!optimize && fde
11231 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11233 /* Use cfa+offset to represent the location of arguments passed
11234 on the stack when drap is used to align stack.
11235 Only do this when not optimizing, for optimized code var-tracking
11236 is supposed to track where the arguments live and the register
11237 used as vdrap or drap in some spot might be used for something
11238 else in other part of the routine. */
11239 return new_loc_descr (DW_OP_fbreg, offset, 0);
11242 if (regno <= 31)
11243 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11244 offset, 0);
11245 else
11246 result = new_loc_descr (DW_OP_bregx, regno, offset);
11248 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11249 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11251 return result;
11254 /* Return true if this RTL expression describes a base+offset calculation. */
11256 static inline int
11257 is_based_loc (const_rtx rtl)
11259 return (GET_CODE (rtl) == PLUS
11260 && ((REG_P (XEXP (rtl, 0))
11261 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11262 && CONST_INT_P (XEXP (rtl, 1)))));
11265 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11266 failed. */
11268 static dw_loc_descr_ref
11269 tls_mem_loc_descriptor (rtx mem)
11271 tree base;
11272 dw_loc_descr_ref loc_result;
11274 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11275 return NULL;
11277 base = get_base_address (MEM_EXPR (mem));
11278 if (base == NULL
11279 || TREE_CODE (base) != VAR_DECL
11280 || !DECL_THREAD_LOCAL_P (base))
11281 return NULL;
11283 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11284 if (loc_result == NULL)
11285 return NULL;
11287 if (MEM_OFFSET (mem))
11288 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11290 return loc_result;
11293 /* Output debug info about reason why we failed to expand expression as dwarf
11294 expression. */
11296 static void
11297 expansion_failed (tree expr, rtx rtl, char const *reason)
11299 if (dump_file && (dump_flags & TDF_DETAILS))
11301 fprintf (dump_file, "Failed to expand as dwarf: ");
11302 if (expr)
11303 print_generic_expr (dump_file, expr, dump_flags);
11304 if (rtl)
11306 fprintf (dump_file, "\n");
11307 print_rtl (dump_file, rtl);
11309 fprintf (dump_file, "\nReason: %s\n", reason);
11313 /* Helper function for const_ok_for_output, called either directly
11314 or via for_each_rtx. */
11316 static int
11317 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11319 rtx rtl = *rtlp;
11321 if (GET_CODE (rtl) == UNSPEC)
11323 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11324 we can't express it in the debug info. */
11325 #ifdef ENABLE_CHECKING
11326 /* Don't complain about TLS UNSPECs, those are just too hard to
11327 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11328 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11329 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11330 if (XVECLEN (rtl, 0) == 0
11331 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11332 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11333 inform (current_function_decl
11334 ? DECL_SOURCE_LOCATION (current_function_decl)
11335 : UNKNOWN_LOCATION,
11336 #if NUM_UNSPEC_VALUES > 0
11337 "non-delegitimized UNSPEC %s (%d) found in variable location",
11338 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11339 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11340 XINT (rtl, 1));
11341 #else
11342 "non-delegitimized UNSPEC %d found in variable location",
11343 XINT (rtl, 1));
11344 #endif
11345 #endif
11346 expansion_failed (NULL_TREE, rtl,
11347 "UNSPEC hasn't been delegitimized.\n");
11348 return 1;
11351 if (targetm.const_not_ok_for_debug_p (rtl))
11353 expansion_failed (NULL_TREE, rtl,
11354 "Expression rejected for debug by the backend.\n");
11355 return 1;
11358 if (GET_CODE (rtl) != SYMBOL_REF)
11359 return 0;
11361 if (CONSTANT_POOL_ADDRESS_P (rtl))
11363 bool marked;
11364 get_pool_constant_mark (rtl, &marked);
11365 /* If all references to this pool constant were optimized away,
11366 it was not output and thus we can't represent it. */
11367 if (!marked)
11369 expansion_failed (NULL_TREE, rtl,
11370 "Constant was removed from constant pool.\n");
11371 return 1;
11375 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11376 return 1;
11378 /* Avoid references to external symbols in debug info, on several targets
11379 the linker might even refuse to link when linking a shared library,
11380 and in many other cases the relocations for .debug_info/.debug_loc are
11381 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11382 to be defined within the same shared library or executable are fine. */
11383 if (SYMBOL_REF_EXTERNAL_P (rtl))
11385 tree decl = SYMBOL_REF_DECL (rtl);
11387 if (decl == NULL || !targetm.binds_local_p (decl))
11389 expansion_failed (NULL_TREE, rtl,
11390 "Symbol not defined in current TU.\n");
11391 return 1;
11395 return 0;
11398 /* Return true if constant RTL can be emitted in DW_OP_addr or
11399 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11400 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11402 static bool
11403 const_ok_for_output (rtx rtl)
11405 if (GET_CODE (rtl) == SYMBOL_REF)
11406 return const_ok_for_output_1 (&rtl, NULL) == 0;
11408 if (GET_CODE (rtl) == CONST)
11409 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11411 return true;
11414 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11415 if possible, NULL otherwise. */
11417 static dw_die_ref
11418 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11420 dw_die_ref type_die;
11421 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11423 if (type == NULL)
11424 return NULL;
11425 switch (TREE_CODE (type))
11427 case INTEGER_TYPE:
11428 case REAL_TYPE:
11429 break;
11430 default:
11431 return NULL;
11433 type_die = lookup_type_die (type);
11434 if (!type_die)
11435 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11436 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11437 return NULL;
11438 return type_die;
11441 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11442 type matching MODE, or, if MODE is narrower than or as wide as
11443 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11444 possible. */
11446 static dw_loc_descr_ref
11447 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11449 enum machine_mode outer_mode = mode;
11450 dw_die_ref type_die;
11451 dw_loc_descr_ref cvt;
11453 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11455 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11456 return op;
11458 type_die = base_type_for_mode (outer_mode, 1);
11459 if (type_die == NULL)
11460 return NULL;
11461 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11462 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11463 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11464 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11465 add_loc_descr (&op, cvt);
11466 return op;
11469 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11471 static dw_loc_descr_ref
11472 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11473 dw_loc_descr_ref op1)
11475 dw_loc_descr_ref ret = op0;
11476 add_loc_descr (&ret, op1);
11477 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11478 if (STORE_FLAG_VALUE != 1)
11480 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11481 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11483 return ret;
11486 /* Return location descriptor for signed comparison OP RTL. */
11488 static dw_loc_descr_ref
11489 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11490 enum machine_mode mem_mode)
11492 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11493 dw_loc_descr_ref op0, op1;
11494 int shift;
11496 if (op_mode == VOIDmode)
11497 op_mode = GET_MODE (XEXP (rtl, 1));
11498 if (op_mode == VOIDmode)
11499 return NULL;
11501 if (dwarf_strict
11502 && (GET_MODE_CLASS (op_mode) != MODE_INT
11503 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11504 return NULL;
11506 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11507 VAR_INIT_STATUS_INITIALIZED);
11508 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11509 VAR_INIT_STATUS_INITIALIZED);
11511 if (op0 == NULL || op1 == NULL)
11512 return NULL;
11514 if (GET_MODE_CLASS (op_mode) != MODE_INT
11515 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11516 return compare_loc_descriptor (op, op0, op1);
11518 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11520 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11521 dw_loc_descr_ref cvt;
11523 if (type_die == NULL)
11524 return NULL;
11525 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11526 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11527 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11528 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11529 add_loc_descr (&op0, cvt);
11530 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11531 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11532 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11533 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11534 add_loc_descr (&op1, cvt);
11535 return compare_loc_descriptor (op, op0, op1);
11538 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11539 /* For eq/ne, if the operands are known to be zero-extended,
11540 there is no need to do the fancy shifting up. */
11541 if (op == DW_OP_eq || op == DW_OP_ne)
11543 dw_loc_descr_ref last0, last1;
11544 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11546 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11548 /* deref_size zero extends, and for constants we can check
11549 whether they are zero extended or not. */
11550 if (((last0->dw_loc_opc == DW_OP_deref_size
11551 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11552 || (CONST_INT_P (XEXP (rtl, 0))
11553 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11554 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11555 && ((last1->dw_loc_opc == DW_OP_deref_size
11556 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11557 || (CONST_INT_P (XEXP (rtl, 1))
11558 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11559 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11560 return compare_loc_descriptor (op, op0, op1);
11562 /* EQ/NE comparison against constant in narrower type than
11563 DWARF2_ADDR_SIZE can be performed either as
11564 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11565 DW_OP_{eq,ne}
11567 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11568 DW_OP_{eq,ne}. Pick whatever is shorter. */
11569 if (CONST_INT_P (XEXP (rtl, 1))
11570 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11571 && (size_of_int_loc_descriptor (shift) + 1
11572 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11573 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11574 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11575 & GET_MODE_MASK (op_mode))))
11577 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11578 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11579 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11580 & GET_MODE_MASK (op_mode));
11581 return compare_loc_descriptor (op, op0, op1);
11584 add_loc_descr (&op0, int_loc_descriptor (shift));
11585 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11586 if (CONST_INT_P (XEXP (rtl, 1)))
11587 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11588 else
11590 add_loc_descr (&op1, int_loc_descriptor (shift));
11591 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11593 return compare_loc_descriptor (op, op0, op1);
11596 /* Return location descriptor for unsigned comparison OP RTL. */
11598 static dw_loc_descr_ref
11599 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11600 enum machine_mode mem_mode)
11602 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11603 dw_loc_descr_ref op0, op1;
11605 if (op_mode == VOIDmode)
11606 op_mode = GET_MODE (XEXP (rtl, 1));
11607 if (op_mode == VOIDmode)
11608 return NULL;
11609 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11610 return NULL;
11612 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11613 return NULL;
11615 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11616 VAR_INIT_STATUS_INITIALIZED);
11617 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11618 VAR_INIT_STATUS_INITIALIZED);
11620 if (op0 == NULL || op1 == NULL)
11621 return NULL;
11623 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11625 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11626 dw_loc_descr_ref last0, last1;
11627 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11629 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11631 if (CONST_INT_P (XEXP (rtl, 0)))
11632 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11633 /* deref_size zero extends, so no need to mask it again. */
11634 else if (last0->dw_loc_opc != DW_OP_deref_size
11635 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11637 add_loc_descr (&op0, int_loc_descriptor (mask));
11638 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11640 if (CONST_INT_P (XEXP (rtl, 1)))
11641 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11642 /* deref_size zero extends, so no need to mask it again. */
11643 else if (last1->dw_loc_opc != DW_OP_deref_size
11644 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11646 add_loc_descr (&op1, int_loc_descriptor (mask));
11647 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11650 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11652 HOST_WIDE_INT bias = 1;
11653 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11654 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11655 if (CONST_INT_P (XEXP (rtl, 1)))
11656 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11657 + INTVAL (XEXP (rtl, 1)));
11658 else
11659 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11660 bias, 0));
11662 return compare_loc_descriptor (op, op0, op1);
11665 /* Return location descriptor for {U,S}{MIN,MAX}. */
11667 static dw_loc_descr_ref
11668 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11669 enum machine_mode mem_mode)
11671 enum dwarf_location_atom op;
11672 dw_loc_descr_ref op0, op1, ret;
11673 dw_loc_descr_ref bra_node, drop_node;
11675 if (dwarf_strict
11676 && (GET_MODE_CLASS (mode) != MODE_INT
11677 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11678 return NULL;
11680 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11681 VAR_INIT_STATUS_INITIALIZED);
11682 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11683 VAR_INIT_STATUS_INITIALIZED);
11685 if (op0 == NULL || op1 == NULL)
11686 return NULL;
11688 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11689 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11690 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11691 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11693 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11695 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11696 add_loc_descr (&op0, int_loc_descriptor (mask));
11697 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11698 add_loc_descr (&op1, int_loc_descriptor (mask));
11699 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11701 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11703 HOST_WIDE_INT bias = 1;
11704 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11705 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11706 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11709 else if (GET_MODE_CLASS (mode) == MODE_INT
11710 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11712 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11713 add_loc_descr (&op0, int_loc_descriptor (shift));
11714 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11715 add_loc_descr (&op1, int_loc_descriptor (shift));
11716 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11718 else if (GET_MODE_CLASS (mode) == MODE_INT
11719 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11721 dw_die_ref type_die = base_type_for_mode (mode, 0);
11722 dw_loc_descr_ref cvt;
11723 if (type_die == NULL)
11724 return NULL;
11725 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11726 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11727 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11728 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11729 add_loc_descr (&op0, cvt);
11730 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11731 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11732 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11733 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11734 add_loc_descr (&op1, cvt);
11737 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11738 op = DW_OP_lt;
11739 else
11740 op = DW_OP_gt;
11741 ret = op0;
11742 add_loc_descr (&ret, op1);
11743 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11744 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11745 add_loc_descr (&ret, bra_node);
11746 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11747 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11748 add_loc_descr (&ret, drop_node);
11749 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11750 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11751 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11752 && GET_MODE_CLASS (mode) == MODE_INT
11753 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11754 ret = convert_descriptor_to_mode (mode, ret);
11755 return ret;
11758 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11759 but after converting arguments to type_die, afterwards
11760 convert back to unsigned. */
11762 static dw_loc_descr_ref
11763 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11764 enum machine_mode mode, enum machine_mode mem_mode)
11766 dw_loc_descr_ref cvt, op0, op1;
11768 if (type_die == NULL)
11769 return NULL;
11770 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11771 VAR_INIT_STATUS_INITIALIZED);
11772 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11773 VAR_INIT_STATUS_INITIALIZED);
11774 if (op0 == NULL || op1 == NULL)
11775 return NULL;
11776 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11777 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11778 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11779 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11780 add_loc_descr (&op0, cvt);
11781 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11782 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11783 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11784 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11785 add_loc_descr (&op1, cvt);
11786 add_loc_descr (&op0, op1);
11787 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11788 return convert_descriptor_to_mode (mode, op0);
11791 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11792 const0 is DW_OP_lit0 or corresponding typed constant,
11793 const1 is DW_OP_lit1 or corresponding typed constant
11794 and constMSB is constant with just the MSB bit set
11795 for the mode):
11796 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11797 L1: const0 DW_OP_swap
11798 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11799 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11800 L3: DW_OP_drop
11801 L4: DW_OP_nop
11803 CTZ is similar:
11804 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11805 L1: const0 DW_OP_swap
11806 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11807 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11808 L3: DW_OP_drop
11809 L4: DW_OP_nop
11811 FFS is similar:
11812 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11813 L1: const1 DW_OP_swap
11814 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11815 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11816 L3: DW_OP_drop
11817 L4: DW_OP_nop */
11819 static dw_loc_descr_ref
11820 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11821 enum machine_mode mem_mode)
11823 dw_loc_descr_ref op0, ret, tmp;
11824 HOST_WIDE_INT valv;
11825 dw_loc_descr_ref l1jump, l1label;
11826 dw_loc_descr_ref l2jump, l2label;
11827 dw_loc_descr_ref l3jump, l3label;
11828 dw_loc_descr_ref l4jump, l4label;
11829 rtx msb;
11831 if (GET_MODE_CLASS (mode) != MODE_INT
11832 || GET_MODE (XEXP (rtl, 0)) != mode
11833 || (GET_CODE (rtl) == CLZ
11834 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11835 return NULL;
11837 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11838 VAR_INIT_STATUS_INITIALIZED);
11839 if (op0 == NULL)
11840 return NULL;
11841 ret = op0;
11842 if (GET_CODE (rtl) == CLZ)
11844 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11845 valv = GET_MODE_BITSIZE (mode);
11847 else if (GET_CODE (rtl) == FFS)
11848 valv = 0;
11849 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11850 valv = GET_MODE_BITSIZE (mode);
11851 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11852 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11853 add_loc_descr (&ret, l1jump);
11854 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11855 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11856 VAR_INIT_STATUS_INITIALIZED);
11857 if (tmp == NULL)
11858 return NULL;
11859 add_loc_descr (&ret, tmp);
11860 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11861 add_loc_descr (&ret, l4jump);
11862 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11863 ? const1_rtx : const0_rtx,
11864 mode, mem_mode,
11865 VAR_INIT_STATUS_INITIALIZED);
11866 if (l1label == NULL)
11867 return NULL;
11868 add_loc_descr (&ret, l1label);
11869 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11870 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11871 add_loc_descr (&ret, l2label);
11872 if (GET_CODE (rtl) != CLZ)
11873 msb = const1_rtx;
11874 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11875 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11876 << (GET_MODE_BITSIZE (mode) - 1));
11877 else
11878 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11879 << (GET_MODE_BITSIZE (mode)
11880 - HOST_BITS_PER_WIDE_INT - 1), mode);
11881 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11882 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11883 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11884 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11885 else
11886 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11887 VAR_INIT_STATUS_INITIALIZED);
11888 if (tmp == NULL)
11889 return NULL;
11890 add_loc_descr (&ret, tmp);
11891 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11892 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11893 add_loc_descr (&ret, l3jump);
11894 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11895 VAR_INIT_STATUS_INITIALIZED);
11896 if (tmp == NULL)
11897 return NULL;
11898 add_loc_descr (&ret, tmp);
11899 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11900 ? DW_OP_shl : DW_OP_shr, 0, 0));
11901 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11902 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11903 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11904 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11905 add_loc_descr (&ret, l2jump);
11906 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11907 add_loc_descr (&ret, l3label);
11908 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11909 add_loc_descr (&ret, l4label);
11910 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11911 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11912 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11913 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11914 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11915 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11916 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11917 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11918 return ret;
11921 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11922 const1 is DW_OP_lit1 or corresponding typed constant):
11923 const0 DW_OP_swap
11924 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11925 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11926 L2: DW_OP_drop
11928 PARITY is similar:
11929 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11930 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11931 L2: DW_OP_drop */
11933 static dw_loc_descr_ref
11934 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11935 enum machine_mode mem_mode)
11937 dw_loc_descr_ref op0, ret, tmp;
11938 dw_loc_descr_ref l1jump, l1label;
11939 dw_loc_descr_ref l2jump, l2label;
11941 if (GET_MODE_CLASS (mode) != MODE_INT
11942 || GET_MODE (XEXP (rtl, 0)) != mode)
11943 return NULL;
11945 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11946 VAR_INIT_STATUS_INITIALIZED);
11947 if (op0 == NULL)
11948 return NULL;
11949 ret = op0;
11950 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11951 VAR_INIT_STATUS_INITIALIZED);
11952 if (tmp == NULL)
11953 return NULL;
11954 add_loc_descr (&ret, tmp);
11955 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11956 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11957 add_loc_descr (&ret, l1label);
11958 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11959 add_loc_descr (&ret, l2jump);
11960 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11961 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11962 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11963 VAR_INIT_STATUS_INITIALIZED);
11964 if (tmp == NULL)
11965 return NULL;
11966 add_loc_descr (&ret, tmp);
11967 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11968 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11969 ? DW_OP_plus : DW_OP_xor, 0, 0));
11970 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11971 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11972 VAR_INIT_STATUS_INITIALIZED);
11973 add_loc_descr (&ret, tmp);
11974 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11975 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11976 add_loc_descr (&ret, l1jump);
11977 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11978 add_loc_descr (&ret, l2label);
11979 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11980 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11981 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11982 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11983 return ret;
11986 /* BSWAP (constS is initial shift count, either 56 or 24):
11987 constS const0
11988 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11989 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11990 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11991 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11992 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11994 static dw_loc_descr_ref
11995 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11996 enum machine_mode mem_mode)
11998 dw_loc_descr_ref op0, ret, tmp;
11999 dw_loc_descr_ref l1jump, l1label;
12000 dw_loc_descr_ref l2jump, l2label;
12002 if (GET_MODE_CLASS (mode) != MODE_INT
12003 || BITS_PER_UNIT != 8
12004 || (GET_MODE_BITSIZE (mode) != 32
12005 && GET_MODE_BITSIZE (mode) != 64))
12006 return NULL;
12008 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12009 VAR_INIT_STATUS_INITIALIZED);
12010 if (op0 == NULL)
12011 return NULL;
12013 ret = op0;
12014 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12015 mode, mem_mode,
12016 VAR_INIT_STATUS_INITIALIZED);
12017 if (tmp == NULL)
12018 return NULL;
12019 add_loc_descr (&ret, tmp);
12020 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12021 VAR_INIT_STATUS_INITIALIZED);
12022 if (tmp == NULL)
12023 return NULL;
12024 add_loc_descr (&ret, tmp);
12025 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12026 add_loc_descr (&ret, l1label);
12027 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12028 mode, mem_mode,
12029 VAR_INIT_STATUS_INITIALIZED);
12030 add_loc_descr (&ret, tmp);
12031 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12032 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12033 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12034 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12035 VAR_INIT_STATUS_INITIALIZED);
12036 if (tmp == NULL)
12037 return NULL;
12038 add_loc_descr (&ret, tmp);
12039 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12040 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12041 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12042 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12043 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12044 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12045 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12046 VAR_INIT_STATUS_INITIALIZED);
12047 add_loc_descr (&ret, tmp);
12048 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12049 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12050 add_loc_descr (&ret, l2jump);
12051 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12052 VAR_INIT_STATUS_INITIALIZED);
12053 add_loc_descr (&ret, tmp);
12054 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12055 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12056 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12057 add_loc_descr (&ret, l1jump);
12058 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12059 add_loc_descr (&ret, l2label);
12060 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12061 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12062 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12063 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12064 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12065 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12066 return ret;
12069 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12070 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12071 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12072 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12074 ROTATERT is similar:
12075 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12076 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12077 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12079 static dw_loc_descr_ref
12080 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12081 enum machine_mode mem_mode)
12083 rtx rtlop1 = XEXP (rtl, 1);
12084 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12085 int i;
12087 if (GET_MODE_CLASS (mode) != MODE_INT)
12088 return NULL;
12090 if (GET_MODE (rtlop1) != VOIDmode
12091 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12092 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12093 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12094 VAR_INIT_STATUS_INITIALIZED);
12095 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12096 VAR_INIT_STATUS_INITIALIZED);
12097 if (op0 == NULL || op1 == NULL)
12098 return NULL;
12099 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12100 for (i = 0; i < 2; i++)
12102 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12103 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12104 mode, mem_mode,
12105 VAR_INIT_STATUS_INITIALIZED);
12106 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12107 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12108 ? DW_OP_const4u
12109 : HOST_BITS_PER_WIDE_INT == 64
12110 ? DW_OP_const8u : DW_OP_constu,
12111 GET_MODE_MASK (mode), 0);
12112 else
12113 mask[i] = NULL;
12114 if (mask[i] == NULL)
12115 return NULL;
12116 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12118 ret = op0;
12119 add_loc_descr (&ret, op1);
12120 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12121 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12122 if (GET_CODE (rtl) == ROTATERT)
12124 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12125 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12126 GET_MODE_BITSIZE (mode), 0));
12128 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12129 if (mask[0] != NULL)
12130 add_loc_descr (&ret, mask[0]);
12131 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12132 if (mask[1] != NULL)
12134 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12135 add_loc_descr (&ret, mask[1]);
12136 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12138 if (GET_CODE (rtl) == ROTATE)
12140 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12141 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12142 GET_MODE_BITSIZE (mode), 0));
12144 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12145 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12146 return ret;
12149 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12150 for DEBUG_PARAMETER_REF RTL. */
12152 static dw_loc_descr_ref
12153 parameter_ref_descriptor (rtx rtl)
12155 dw_loc_descr_ref ret;
12156 dw_die_ref ref;
12158 if (dwarf_strict)
12159 return NULL;
12160 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12161 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12162 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12163 if (ref)
12165 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12166 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12167 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12169 else
12171 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12172 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12174 return ret;
12177 /* The following routine converts the RTL for a variable or parameter
12178 (resident in memory) into an equivalent Dwarf representation of a
12179 mechanism for getting the address of that same variable onto the top of a
12180 hypothetical "address evaluation" stack.
12182 When creating memory location descriptors, we are effectively transforming
12183 the RTL for a memory-resident object into its Dwarf postfix expression
12184 equivalent. This routine recursively descends an RTL tree, turning
12185 it into Dwarf postfix code as it goes.
12187 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12189 MEM_MODE is the mode of the memory reference, needed to handle some
12190 autoincrement addressing modes.
12192 Return 0 if we can't represent the location. */
12194 dw_loc_descr_ref
12195 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12196 enum machine_mode mem_mode,
12197 enum var_init_status initialized)
12199 dw_loc_descr_ref mem_loc_result = NULL;
12200 enum dwarf_location_atom op;
12201 dw_loc_descr_ref op0, op1;
12202 rtx inner = NULL_RTX;
12204 if (mode == VOIDmode)
12205 mode = GET_MODE (rtl);
12207 /* Note that for a dynamically sized array, the location we will generate a
12208 description of here will be the lowest numbered location which is
12209 actually within the array. That's *not* necessarily the same as the
12210 zeroth element of the array. */
12212 rtl = targetm.delegitimize_address (rtl);
12214 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12215 return NULL;
12217 switch (GET_CODE (rtl))
12219 case POST_INC:
12220 case POST_DEC:
12221 case POST_MODIFY:
12222 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12224 case SUBREG:
12225 /* The case of a subreg may arise when we have a local (register)
12226 variable or a formal (register) parameter which doesn't quite fill
12227 up an entire register. For now, just assume that it is
12228 legitimate to make the Dwarf info refer to the whole register which
12229 contains the given subreg. */
12230 if (!subreg_lowpart_p (rtl))
12231 break;
12232 inner = SUBREG_REG (rtl);
12233 case TRUNCATE:
12234 if (inner == NULL_RTX)
12235 inner = XEXP (rtl, 0);
12236 if (GET_MODE_CLASS (mode) == MODE_INT
12237 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12238 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12239 #ifdef POINTERS_EXTEND_UNSIGNED
12240 || (mode == Pmode && mem_mode != VOIDmode)
12241 #endif
12243 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12245 mem_loc_result = mem_loc_descriptor (inner,
12246 GET_MODE (inner),
12247 mem_mode, initialized);
12248 break;
12250 if (dwarf_strict)
12251 break;
12252 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12253 break;
12254 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12255 && (GET_MODE_CLASS (mode) != MODE_INT
12256 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12257 break;
12258 else
12260 dw_die_ref type_die;
12261 dw_loc_descr_ref cvt;
12263 mem_loc_result = mem_loc_descriptor (inner,
12264 GET_MODE (inner),
12265 mem_mode, initialized);
12266 if (mem_loc_result == NULL)
12267 break;
12268 type_die = base_type_for_mode (mode,
12269 GET_MODE_CLASS (mode) == MODE_INT);
12270 if (type_die == NULL)
12272 mem_loc_result = NULL;
12273 break;
12275 if (GET_MODE_SIZE (mode)
12276 != GET_MODE_SIZE (GET_MODE (inner)))
12277 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12278 else
12279 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12280 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12281 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12282 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12283 add_loc_descr (&mem_loc_result, cvt);
12285 break;
12287 case REG:
12288 if (GET_MODE_CLASS (mode) != MODE_INT
12289 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12290 && rtl != arg_pointer_rtx
12291 && rtl != frame_pointer_rtx
12292 #ifdef POINTERS_EXTEND_UNSIGNED
12293 && (mode != Pmode || mem_mode == VOIDmode)
12294 #endif
12297 dw_die_ref type_die;
12298 unsigned int dbx_regnum;
12300 if (dwarf_strict)
12301 break;
12302 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12303 break;
12304 type_die = base_type_for_mode (mode,
12305 GET_MODE_CLASS (mode) == MODE_INT);
12306 if (type_die == NULL)
12307 break;
12309 dbx_regnum = dbx_reg_number (rtl);
12310 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12311 break;
12312 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12313 dbx_regnum, 0);
12314 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12315 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12316 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12317 break;
12319 /* Whenever a register number forms a part of the description of the
12320 method for calculating the (dynamic) address of a memory resident
12321 object, DWARF rules require the register number be referred to as
12322 a "base register". This distinction is not based in any way upon
12323 what category of register the hardware believes the given register
12324 belongs to. This is strictly DWARF terminology we're dealing with
12325 here. Note that in cases where the location of a memory-resident
12326 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12327 OP_CONST (0)) the actual DWARF location descriptor that we generate
12328 may just be OP_BASEREG (basereg). This may look deceptively like
12329 the object in question was allocated to a register (rather than in
12330 memory) so DWARF consumers need to be aware of the subtle
12331 distinction between OP_REG and OP_BASEREG. */
12332 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12333 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12334 else if (stack_realign_drap
12335 && crtl->drap_reg
12336 && crtl->args.internal_arg_pointer == rtl
12337 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12339 /* If RTL is internal_arg_pointer, which has been optimized
12340 out, use DRAP instead. */
12341 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12342 VAR_INIT_STATUS_INITIALIZED);
12344 break;
12346 case SIGN_EXTEND:
12347 case ZERO_EXTEND:
12348 if (GET_MODE_CLASS (mode) != MODE_INT)
12349 break;
12350 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12351 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12352 if (op0 == 0)
12353 break;
12354 else if (GET_CODE (rtl) == ZERO_EXTEND
12355 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12356 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12357 < HOST_BITS_PER_WIDE_INT
12358 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12359 to expand zero extend as two shifts instead of
12360 masking. */
12361 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12363 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12364 mem_loc_result = op0;
12365 add_loc_descr (&mem_loc_result,
12366 int_loc_descriptor (GET_MODE_MASK (imode)));
12367 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12369 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12371 int shift = DWARF2_ADDR_SIZE
12372 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12373 shift *= BITS_PER_UNIT;
12374 if (GET_CODE (rtl) == SIGN_EXTEND)
12375 op = DW_OP_shra;
12376 else
12377 op = DW_OP_shr;
12378 mem_loc_result = op0;
12379 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12380 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12381 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12382 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12384 else if (!dwarf_strict)
12386 dw_die_ref type_die1, type_die2;
12387 dw_loc_descr_ref cvt;
12389 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12390 GET_CODE (rtl) == ZERO_EXTEND);
12391 if (type_die1 == NULL)
12392 break;
12393 type_die2 = base_type_for_mode (mode, 1);
12394 if (type_die2 == NULL)
12395 break;
12396 mem_loc_result = op0;
12397 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12398 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12399 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12400 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12401 add_loc_descr (&mem_loc_result, cvt);
12402 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12403 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12404 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12405 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12406 add_loc_descr (&mem_loc_result, cvt);
12408 break;
12410 case MEM:
12412 rtx new_rtl = avoid_constant_pool_reference (rtl);
12413 if (new_rtl != rtl)
12415 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12416 initialized);
12417 if (mem_loc_result != NULL)
12418 return mem_loc_result;
12421 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12422 get_address_mode (rtl), mode,
12423 VAR_INIT_STATUS_INITIALIZED);
12424 if (mem_loc_result == NULL)
12425 mem_loc_result = tls_mem_loc_descriptor (rtl);
12426 if (mem_loc_result != NULL)
12428 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12429 || GET_MODE_CLASS (mode) != MODE_INT)
12431 dw_die_ref type_die;
12432 dw_loc_descr_ref deref;
12434 if (dwarf_strict)
12435 return NULL;
12436 type_die
12437 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12438 if (type_die == NULL)
12439 return NULL;
12440 deref = new_loc_descr (DW_OP_GNU_deref_type,
12441 GET_MODE_SIZE (mode), 0);
12442 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12443 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12444 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12445 add_loc_descr (&mem_loc_result, deref);
12447 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12448 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12449 else
12450 add_loc_descr (&mem_loc_result,
12451 new_loc_descr (DW_OP_deref_size,
12452 GET_MODE_SIZE (mode), 0));
12454 break;
12456 case LO_SUM:
12457 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12459 case LABEL_REF:
12460 /* Some ports can transform a symbol ref into a label ref, because
12461 the symbol ref is too far away and has to be dumped into a constant
12462 pool. */
12463 case CONST:
12464 case SYMBOL_REF:
12465 if (GET_MODE_CLASS (mode) != MODE_INT
12466 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12467 #ifdef POINTERS_EXTEND_UNSIGNED
12468 && (mode != Pmode || mem_mode == VOIDmode)
12469 #endif
12471 break;
12472 if (GET_CODE (rtl) == SYMBOL_REF
12473 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12475 dw_loc_descr_ref temp;
12477 /* If this is not defined, we have no way to emit the data. */
12478 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12479 break;
12481 temp = new_addr_loc_descr (rtl, dtprel_true);
12483 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12484 add_loc_descr (&mem_loc_result, temp);
12486 break;
12489 if (!const_ok_for_output (rtl))
12490 break;
12492 symref:
12493 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12494 vec_safe_push (used_rtx_array, rtl);
12495 break;
12497 case CONCAT:
12498 case CONCATN:
12499 case VAR_LOCATION:
12500 case DEBUG_IMPLICIT_PTR:
12501 expansion_failed (NULL_TREE, rtl,
12502 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12503 return 0;
12505 case ENTRY_VALUE:
12506 if (dwarf_strict)
12507 return NULL;
12508 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12510 if (GET_MODE_CLASS (mode) != MODE_INT
12511 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12512 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12513 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12514 else
12516 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12517 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12518 return NULL;
12519 op0 = one_reg_loc_descriptor (dbx_regnum,
12520 VAR_INIT_STATUS_INITIALIZED);
12523 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12524 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12526 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12527 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12528 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12529 return NULL;
12531 else
12532 gcc_unreachable ();
12533 if (op0 == NULL)
12534 return NULL;
12535 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12536 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12537 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12538 break;
12540 case DEBUG_PARAMETER_REF:
12541 mem_loc_result = parameter_ref_descriptor (rtl);
12542 break;
12544 case PRE_MODIFY:
12545 /* Extract the PLUS expression nested inside and fall into
12546 PLUS code below. */
12547 rtl = XEXP (rtl, 1);
12548 goto plus;
12550 case PRE_INC:
12551 case PRE_DEC:
12552 /* Turn these into a PLUS expression and fall into the PLUS code
12553 below. */
12554 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12555 gen_int_mode (GET_CODE (rtl) == PRE_INC
12556 ? GET_MODE_UNIT_SIZE (mem_mode)
12557 : -GET_MODE_UNIT_SIZE (mem_mode),
12558 mode));
12560 /* ... fall through ... */
12562 case PLUS:
12563 plus:
12564 if (is_based_loc (rtl)
12565 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12566 || XEXP (rtl, 0) == arg_pointer_rtx
12567 || XEXP (rtl, 0) == frame_pointer_rtx)
12568 && GET_MODE_CLASS (mode) == MODE_INT)
12569 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12570 INTVAL (XEXP (rtl, 1)),
12571 VAR_INIT_STATUS_INITIALIZED);
12572 else
12574 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12575 VAR_INIT_STATUS_INITIALIZED);
12576 if (mem_loc_result == 0)
12577 break;
12579 if (CONST_INT_P (XEXP (rtl, 1))
12580 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12581 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12582 else
12584 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12585 VAR_INIT_STATUS_INITIALIZED);
12586 if (op1 == 0)
12587 break;
12588 add_loc_descr (&mem_loc_result, op1);
12589 add_loc_descr (&mem_loc_result,
12590 new_loc_descr (DW_OP_plus, 0, 0));
12593 break;
12595 /* If a pseudo-reg is optimized away, it is possible for it to
12596 be replaced with a MEM containing a multiply or shift. */
12597 case MINUS:
12598 op = DW_OP_minus;
12599 goto do_binop;
12601 case MULT:
12602 op = DW_OP_mul;
12603 goto do_binop;
12605 case DIV:
12606 if (!dwarf_strict
12607 && GET_MODE_CLASS (mode) == MODE_INT
12608 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12610 mem_loc_result = typed_binop (DW_OP_div, rtl,
12611 base_type_for_mode (mode, 0),
12612 mode, mem_mode);
12613 break;
12615 op = DW_OP_div;
12616 goto do_binop;
12618 case UMOD:
12619 op = DW_OP_mod;
12620 goto do_binop;
12622 case ASHIFT:
12623 op = DW_OP_shl;
12624 goto do_shift;
12626 case ASHIFTRT:
12627 op = DW_OP_shra;
12628 goto do_shift;
12630 case LSHIFTRT:
12631 op = DW_OP_shr;
12632 goto do_shift;
12634 do_shift:
12635 if (GET_MODE_CLASS (mode) != MODE_INT)
12636 break;
12637 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12638 VAR_INIT_STATUS_INITIALIZED);
12640 rtx rtlop1 = XEXP (rtl, 1);
12641 if (GET_MODE (rtlop1) != VOIDmode
12642 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12643 < GET_MODE_BITSIZE (mode))
12644 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12645 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12646 VAR_INIT_STATUS_INITIALIZED);
12649 if (op0 == 0 || op1 == 0)
12650 break;
12652 mem_loc_result = op0;
12653 add_loc_descr (&mem_loc_result, op1);
12654 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12655 break;
12657 case AND:
12658 op = DW_OP_and;
12659 goto do_binop;
12661 case IOR:
12662 op = DW_OP_or;
12663 goto do_binop;
12665 case XOR:
12666 op = DW_OP_xor;
12667 goto do_binop;
12669 do_binop:
12670 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12671 VAR_INIT_STATUS_INITIALIZED);
12672 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12673 VAR_INIT_STATUS_INITIALIZED);
12675 if (op0 == 0 || op1 == 0)
12676 break;
12678 mem_loc_result = op0;
12679 add_loc_descr (&mem_loc_result, op1);
12680 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12681 break;
12683 case MOD:
12684 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12686 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12687 base_type_for_mode (mode, 0),
12688 mode, mem_mode);
12689 break;
12692 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12693 VAR_INIT_STATUS_INITIALIZED);
12694 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12695 VAR_INIT_STATUS_INITIALIZED);
12697 if (op0 == 0 || op1 == 0)
12698 break;
12700 mem_loc_result = op0;
12701 add_loc_descr (&mem_loc_result, op1);
12702 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12703 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12704 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12705 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12706 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12707 break;
12709 case UDIV:
12710 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12712 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12714 op = DW_OP_div;
12715 goto do_binop;
12717 mem_loc_result = typed_binop (DW_OP_div, rtl,
12718 base_type_for_mode (mode, 1),
12719 mode, mem_mode);
12721 break;
12723 case NOT:
12724 op = DW_OP_not;
12725 goto do_unop;
12727 case ABS:
12728 op = DW_OP_abs;
12729 goto do_unop;
12731 case NEG:
12732 op = DW_OP_neg;
12733 goto do_unop;
12735 do_unop:
12736 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12737 VAR_INIT_STATUS_INITIALIZED);
12739 if (op0 == 0)
12740 break;
12742 mem_loc_result = op0;
12743 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12744 break;
12746 case CONST_INT:
12747 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12748 #ifdef POINTERS_EXTEND_UNSIGNED
12749 || (mode == Pmode
12750 && mem_mode != VOIDmode
12751 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12752 #endif
12755 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12756 break;
12758 if (!dwarf_strict
12759 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12760 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12762 dw_die_ref type_die = base_type_for_mode (mode, 1);
12763 enum machine_mode amode;
12764 if (type_die == NULL)
12765 return NULL;
12766 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12767 MODE_INT, 0);
12768 if (INTVAL (rtl) >= 0
12769 && amode != BLKmode
12770 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12771 /* const DW_OP_GNU_convert <XXX> vs.
12772 DW_OP_GNU_const_type <XXX, 1, const>. */
12773 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12774 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12776 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12777 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12778 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12779 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12780 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12781 add_loc_descr (&mem_loc_result, op0);
12782 return mem_loc_result;
12784 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12785 INTVAL (rtl));
12786 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12787 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12788 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12789 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12790 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12791 else
12793 mem_loc_result->dw_loc_oprnd2.val_class
12794 = dw_val_class_const_double;
12795 mem_loc_result->dw_loc_oprnd2.v.val_double
12796 = double_int::from_shwi (INTVAL (rtl));
12799 break;
12801 case CONST_DOUBLE:
12802 if (!dwarf_strict)
12804 dw_die_ref type_die;
12806 /* Note that a CONST_DOUBLE rtx could represent either an integer
12807 or a floating-point constant. A CONST_DOUBLE is used whenever
12808 the constant requires more than one word in order to be
12809 adequately represented. We output CONST_DOUBLEs as blocks. */
12810 if (mode == VOIDmode
12811 || (GET_MODE (rtl) == VOIDmode
12812 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12813 break;
12814 type_die = base_type_for_mode (mode,
12815 GET_MODE_CLASS (mode) == MODE_INT);
12816 if (type_die == NULL)
12817 return NULL;
12818 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12819 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12820 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12821 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12822 if (SCALAR_FLOAT_MODE_P (mode))
12824 unsigned int length = GET_MODE_SIZE (mode);
12825 unsigned char *array
12826 = (unsigned char*) ggc_alloc_atomic (length);
12828 insert_float (rtl, array);
12829 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12830 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12831 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12832 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12834 else
12836 mem_loc_result->dw_loc_oprnd2.val_class
12837 = dw_val_class_const_double;
12838 mem_loc_result->dw_loc_oprnd2.v.val_double
12839 = rtx_to_double_int (rtl);
12842 break;
12844 case EQ:
12845 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12846 break;
12848 case GE:
12849 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12850 break;
12852 case GT:
12853 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12854 break;
12856 case LE:
12857 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12858 break;
12860 case LT:
12861 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12862 break;
12864 case NE:
12865 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12866 break;
12868 case GEU:
12869 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12870 break;
12872 case GTU:
12873 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12874 break;
12876 case LEU:
12877 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12878 break;
12880 case LTU:
12881 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12882 break;
12884 case UMIN:
12885 case UMAX:
12886 if (GET_MODE_CLASS (mode) != MODE_INT)
12887 break;
12888 /* FALLTHRU */
12889 case SMIN:
12890 case SMAX:
12891 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12892 break;
12894 case ZERO_EXTRACT:
12895 case SIGN_EXTRACT:
12896 if (CONST_INT_P (XEXP (rtl, 1))
12897 && CONST_INT_P (XEXP (rtl, 2))
12898 && ((unsigned) INTVAL (XEXP (rtl, 1))
12899 + (unsigned) INTVAL (XEXP (rtl, 2))
12900 <= GET_MODE_BITSIZE (mode))
12901 && GET_MODE_CLASS (mode) == MODE_INT
12902 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12903 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12905 int shift, size;
12906 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12907 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12908 if (op0 == 0)
12909 break;
12910 if (GET_CODE (rtl) == SIGN_EXTRACT)
12911 op = DW_OP_shra;
12912 else
12913 op = DW_OP_shr;
12914 mem_loc_result = op0;
12915 size = INTVAL (XEXP (rtl, 1));
12916 shift = INTVAL (XEXP (rtl, 2));
12917 if (BITS_BIG_ENDIAN)
12918 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12919 - shift - size;
12920 if (shift + size != (int) DWARF2_ADDR_SIZE)
12922 add_loc_descr (&mem_loc_result,
12923 int_loc_descriptor (DWARF2_ADDR_SIZE
12924 - shift - size));
12925 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12927 if (size != (int) DWARF2_ADDR_SIZE)
12929 add_loc_descr (&mem_loc_result,
12930 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12931 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12934 break;
12936 case IF_THEN_ELSE:
12938 dw_loc_descr_ref op2, bra_node, drop_node;
12939 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12940 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12941 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12942 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12943 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12944 VAR_INIT_STATUS_INITIALIZED);
12945 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12946 VAR_INIT_STATUS_INITIALIZED);
12947 if (op0 == NULL || op1 == NULL || op2 == NULL)
12948 break;
12950 mem_loc_result = op1;
12951 add_loc_descr (&mem_loc_result, op2);
12952 add_loc_descr (&mem_loc_result, op0);
12953 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12954 add_loc_descr (&mem_loc_result, bra_node);
12955 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12956 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12957 add_loc_descr (&mem_loc_result, drop_node);
12958 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12959 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12961 break;
12963 case FLOAT_EXTEND:
12964 case FLOAT_TRUNCATE:
12965 case FLOAT:
12966 case UNSIGNED_FLOAT:
12967 case FIX:
12968 case UNSIGNED_FIX:
12969 if (!dwarf_strict)
12971 dw_die_ref type_die;
12972 dw_loc_descr_ref cvt;
12974 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12975 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12976 if (op0 == NULL)
12977 break;
12978 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12979 && (GET_CODE (rtl) == FLOAT
12980 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12981 <= DWARF2_ADDR_SIZE))
12983 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12984 GET_CODE (rtl) == UNSIGNED_FLOAT);
12985 if (type_die == NULL)
12986 break;
12987 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12988 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12989 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12990 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12991 add_loc_descr (&op0, cvt);
12993 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12994 if (type_die == NULL)
12995 break;
12996 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12997 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12998 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12999 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13000 add_loc_descr (&op0, cvt);
13001 if (GET_MODE_CLASS (mode) == MODE_INT
13002 && (GET_CODE (rtl) == FIX
13003 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13005 op0 = convert_descriptor_to_mode (mode, op0);
13006 if (op0 == NULL)
13007 break;
13009 mem_loc_result = op0;
13011 break;
13013 case CLZ:
13014 case CTZ:
13015 case FFS:
13016 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13017 break;
13019 case POPCOUNT:
13020 case PARITY:
13021 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13022 break;
13024 case BSWAP:
13025 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13026 break;
13028 case ROTATE:
13029 case ROTATERT:
13030 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13031 break;
13033 case COMPARE:
13034 /* In theory, we could implement the above. */
13035 /* DWARF cannot represent the unsigned compare operations
13036 natively. */
13037 case SS_MULT:
13038 case US_MULT:
13039 case SS_DIV:
13040 case US_DIV:
13041 case SS_PLUS:
13042 case US_PLUS:
13043 case SS_MINUS:
13044 case US_MINUS:
13045 case SS_NEG:
13046 case US_NEG:
13047 case SS_ABS:
13048 case SS_ASHIFT:
13049 case US_ASHIFT:
13050 case SS_TRUNCATE:
13051 case US_TRUNCATE:
13052 case UNORDERED:
13053 case ORDERED:
13054 case UNEQ:
13055 case UNGE:
13056 case UNGT:
13057 case UNLE:
13058 case UNLT:
13059 case LTGT:
13060 case FRACT_CONVERT:
13061 case UNSIGNED_FRACT_CONVERT:
13062 case SAT_FRACT:
13063 case UNSIGNED_SAT_FRACT:
13064 case SQRT:
13065 case ASM_OPERANDS:
13066 case VEC_MERGE:
13067 case VEC_SELECT:
13068 case VEC_CONCAT:
13069 case VEC_DUPLICATE:
13070 case UNSPEC:
13071 case HIGH:
13072 case FMA:
13073 case STRICT_LOW_PART:
13074 case CONST_VECTOR:
13075 case CONST_FIXED:
13076 case CLRSB:
13077 case CLOBBER:
13078 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13079 can't express it in the debug info. This can happen e.g. with some
13080 TLS UNSPECs. */
13081 break;
13083 case CONST_STRING:
13084 resolve_one_addr (&rtl, NULL);
13085 goto symref;
13087 default:
13088 #ifdef ENABLE_CHECKING
13089 print_rtl (stderr, rtl);
13090 gcc_unreachable ();
13091 #else
13092 break;
13093 #endif
13096 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13097 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13099 return mem_loc_result;
13102 /* Return a descriptor that describes the concatenation of two locations.
13103 This is typically a complex variable. */
13105 static dw_loc_descr_ref
13106 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13108 dw_loc_descr_ref cc_loc_result = NULL;
13109 dw_loc_descr_ref x0_ref
13110 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13111 dw_loc_descr_ref x1_ref
13112 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13114 if (x0_ref == 0 || x1_ref == 0)
13115 return 0;
13117 cc_loc_result = x0_ref;
13118 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13120 add_loc_descr (&cc_loc_result, x1_ref);
13121 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13123 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13124 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13126 return cc_loc_result;
13129 /* Return a descriptor that describes the concatenation of N
13130 locations. */
13132 static dw_loc_descr_ref
13133 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13135 unsigned int i;
13136 dw_loc_descr_ref cc_loc_result = NULL;
13137 unsigned int n = XVECLEN (concatn, 0);
13139 for (i = 0; i < n; ++i)
13141 dw_loc_descr_ref ref;
13142 rtx x = XVECEXP (concatn, 0, i);
13144 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13145 if (ref == NULL)
13146 return NULL;
13148 add_loc_descr (&cc_loc_result, ref);
13149 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13152 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13153 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13155 return cc_loc_result;
13158 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13159 for DEBUG_IMPLICIT_PTR RTL. */
13161 static dw_loc_descr_ref
13162 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13164 dw_loc_descr_ref ret;
13165 dw_die_ref ref;
13167 if (dwarf_strict)
13168 return NULL;
13169 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13170 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13171 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13172 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13173 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13174 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13175 if (ref)
13177 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13178 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13179 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13181 else
13183 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13184 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13186 return ret;
13189 /* Output a proper Dwarf location descriptor for a variable or parameter
13190 which is either allocated in a register or in a memory location. For a
13191 register, we just generate an OP_REG and the register number. For a
13192 memory location we provide a Dwarf postfix expression describing how to
13193 generate the (dynamic) address of the object onto the address stack.
13195 MODE is mode of the decl if this loc_descriptor is going to be used in
13196 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13197 allowed, VOIDmode otherwise.
13199 If we don't know how to describe it, return 0. */
13201 static dw_loc_descr_ref
13202 loc_descriptor (rtx rtl, enum machine_mode mode,
13203 enum var_init_status initialized)
13205 dw_loc_descr_ref loc_result = NULL;
13207 switch (GET_CODE (rtl))
13209 case SUBREG:
13210 /* The case of a subreg may arise when we have a local (register)
13211 variable or a formal (register) parameter which doesn't quite fill
13212 up an entire register. For now, just assume that it is
13213 legitimate to make the Dwarf info refer to the whole register which
13214 contains the given subreg. */
13215 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13216 loc_result = loc_descriptor (SUBREG_REG (rtl),
13217 GET_MODE (SUBREG_REG (rtl)), initialized);
13218 else
13219 goto do_default;
13220 break;
13222 case REG:
13223 loc_result = reg_loc_descriptor (rtl, initialized);
13224 break;
13226 case MEM:
13227 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13228 GET_MODE (rtl), initialized);
13229 if (loc_result == NULL)
13230 loc_result = tls_mem_loc_descriptor (rtl);
13231 if (loc_result == NULL)
13233 rtx new_rtl = avoid_constant_pool_reference (rtl);
13234 if (new_rtl != rtl)
13235 loc_result = loc_descriptor (new_rtl, mode, initialized);
13237 break;
13239 case CONCAT:
13240 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13241 initialized);
13242 break;
13244 case CONCATN:
13245 loc_result = concatn_loc_descriptor (rtl, initialized);
13246 break;
13248 case VAR_LOCATION:
13249 /* Single part. */
13250 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13252 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13253 if (GET_CODE (loc) == EXPR_LIST)
13254 loc = XEXP (loc, 0);
13255 loc_result = loc_descriptor (loc, mode, initialized);
13256 break;
13259 rtl = XEXP (rtl, 1);
13260 /* FALLTHRU */
13262 case PARALLEL:
13264 rtvec par_elems = XVEC (rtl, 0);
13265 int num_elem = GET_NUM_ELEM (par_elems);
13266 enum machine_mode mode;
13267 int i;
13269 /* Create the first one, so we have something to add to. */
13270 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13271 VOIDmode, initialized);
13272 if (loc_result == NULL)
13273 return NULL;
13274 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13275 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13276 for (i = 1; i < num_elem; i++)
13278 dw_loc_descr_ref temp;
13280 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13281 VOIDmode, initialized);
13282 if (temp == NULL)
13283 return NULL;
13284 add_loc_descr (&loc_result, temp);
13285 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13286 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13289 break;
13291 case CONST_INT:
13292 if (mode != VOIDmode && mode != BLKmode)
13293 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13294 INTVAL (rtl));
13295 break;
13297 case CONST_DOUBLE:
13298 if (mode == VOIDmode)
13299 mode = GET_MODE (rtl);
13301 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13303 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13305 /* Note that a CONST_DOUBLE rtx could represent either an integer
13306 or a floating-point constant. A CONST_DOUBLE is used whenever
13307 the constant requires more than one word in order to be
13308 adequately represented. We output CONST_DOUBLEs as blocks. */
13309 loc_result = new_loc_descr (DW_OP_implicit_value,
13310 GET_MODE_SIZE (mode), 0);
13311 if (SCALAR_FLOAT_MODE_P (mode))
13313 unsigned int length = GET_MODE_SIZE (mode);
13314 unsigned char *array
13315 = (unsigned char*) ggc_alloc_atomic (length);
13317 insert_float (rtl, array);
13318 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13319 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13320 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13321 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13323 else
13325 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13326 loc_result->dw_loc_oprnd2.v.val_double
13327 = rtx_to_double_int (rtl);
13330 break;
13332 case CONST_VECTOR:
13333 if (mode == VOIDmode)
13334 mode = GET_MODE (rtl);
13336 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13338 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13339 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13340 unsigned char *array = (unsigned char *)
13341 ggc_alloc_atomic (length * elt_size);
13342 unsigned int i;
13343 unsigned char *p;
13345 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13346 switch (GET_MODE_CLASS (mode))
13348 case MODE_VECTOR_INT:
13349 for (i = 0, p = array; i < length; i++, p += elt_size)
13351 rtx elt = CONST_VECTOR_ELT (rtl, i);
13352 double_int val = rtx_to_double_int (elt);
13354 if (elt_size <= sizeof (HOST_WIDE_INT))
13355 insert_int (val.to_shwi (), elt_size, p);
13356 else
13358 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13359 insert_double (val, p);
13362 break;
13364 case MODE_VECTOR_FLOAT:
13365 for (i = 0, p = array; i < length; i++, p += elt_size)
13367 rtx elt = CONST_VECTOR_ELT (rtl, i);
13368 insert_float (elt, p);
13370 break;
13372 default:
13373 gcc_unreachable ();
13376 loc_result = new_loc_descr (DW_OP_implicit_value,
13377 length * elt_size, 0);
13378 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13379 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13380 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13381 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13383 break;
13385 case CONST:
13386 if (mode == VOIDmode
13387 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13388 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13389 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13391 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13392 break;
13394 /* FALLTHROUGH */
13395 case SYMBOL_REF:
13396 if (!const_ok_for_output (rtl))
13397 break;
13398 case LABEL_REF:
13399 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13400 && (dwarf_version >= 4 || !dwarf_strict))
13402 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13403 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13404 vec_safe_push (used_rtx_array, rtl);
13406 break;
13408 case DEBUG_IMPLICIT_PTR:
13409 loc_result = implicit_ptr_descriptor (rtl, 0);
13410 break;
13412 case PLUS:
13413 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13414 && CONST_INT_P (XEXP (rtl, 1)))
13416 loc_result
13417 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13418 break;
13420 /* FALLTHRU */
13421 do_default:
13422 default:
13423 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13424 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13425 && dwarf_version >= 4)
13426 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13428 /* Value expression. */
13429 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13430 if (loc_result)
13431 add_loc_descr (&loc_result,
13432 new_loc_descr (DW_OP_stack_value, 0, 0));
13434 break;
13437 return loc_result;
13440 /* We need to figure out what section we should use as the base for the
13441 address ranges where a given location is valid.
13442 1. If this particular DECL has a section associated with it, use that.
13443 2. If this function has a section associated with it, use that.
13444 3. Otherwise, use the text section.
13445 XXX: If you split a variable across multiple sections, we won't notice. */
13447 static const char *
13448 secname_for_decl (const_tree decl)
13450 const char *secname;
13452 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13454 tree sectree = DECL_SECTION_NAME (decl);
13455 secname = TREE_STRING_POINTER (sectree);
13457 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13459 tree sectree = DECL_SECTION_NAME (current_function_decl);
13460 secname = TREE_STRING_POINTER (sectree);
13462 else if (cfun && in_cold_section_p)
13463 secname = crtl->subsections.cold_section_label;
13464 else
13465 secname = text_section_label;
13467 return secname;
13470 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13472 static bool
13473 decl_by_reference_p (tree decl)
13475 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13476 || TREE_CODE (decl) == VAR_DECL)
13477 && DECL_BY_REFERENCE (decl));
13480 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13481 for VARLOC. */
13483 static dw_loc_descr_ref
13484 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13485 enum var_init_status initialized)
13487 int have_address = 0;
13488 dw_loc_descr_ref descr;
13489 enum machine_mode mode;
13491 if (want_address != 2)
13493 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13494 /* Single part. */
13495 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13497 varloc = PAT_VAR_LOCATION_LOC (varloc);
13498 if (GET_CODE (varloc) == EXPR_LIST)
13499 varloc = XEXP (varloc, 0);
13500 mode = GET_MODE (varloc);
13501 if (MEM_P (varloc))
13503 rtx addr = XEXP (varloc, 0);
13504 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13505 mode, initialized);
13506 if (descr)
13507 have_address = 1;
13508 else
13510 rtx x = avoid_constant_pool_reference (varloc);
13511 if (x != varloc)
13512 descr = mem_loc_descriptor (x, mode, VOIDmode,
13513 initialized);
13516 else
13517 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13519 else
13520 return 0;
13522 else
13524 if (GET_CODE (varloc) == VAR_LOCATION)
13525 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13526 else
13527 mode = DECL_MODE (loc);
13528 descr = loc_descriptor (varloc, mode, initialized);
13529 have_address = 1;
13532 if (!descr)
13533 return 0;
13535 if (want_address == 2 && !have_address
13536 && (dwarf_version >= 4 || !dwarf_strict))
13538 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13540 expansion_failed (loc, NULL_RTX,
13541 "DWARF address size mismatch");
13542 return 0;
13544 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13545 have_address = 1;
13547 /* Show if we can't fill the request for an address. */
13548 if (want_address && !have_address)
13550 expansion_failed (loc, NULL_RTX,
13551 "Want address and only have value");
13552 return 0;
13555 /* If we've got an address and don't want one, dereference. */
13556 if (!want_address && have_address)
13558 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13559 enum dwarf_location_atom op;
13561 if (size > DWARF2_ADDR_SIZE || size == -1)
13563 expansion_failed (loc, NULL_RTX,
13564 "DWARF address size mismatch");
13565 return 0;
13567 else if (size == DWARF2_ADDR_SIZE)
13568 op = DW_OP_deref;
13569 else
13570 op = DW_OP_deref_size;
13572 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13575 return descr;
13578 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13579 if it is not possible. */
13581 static dw_loc_descr_ref
13582 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13584 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13585 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13586 else if (dwarf_version >= 3 || !dwarf_strict)
13587 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13588 else
13589 return NULL;
13592 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13593 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13595 static dw_loc_descr_ref
13596 dw_sra_loc_expr (tree decl, rtx loc)
13598 rtx p;
13599 unsigned int padsize = 0;
13600 dw_loc_descr_ref descr, *descr_tail;
13601 unsigned HOST_WIDE_INT decl_size;
13602 rtx varloc;
13603 enum var_init_status initialized;
13605 if (DECL_SIZE (decl) == NULL
13606 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13607 return NULL;
13609 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13610 descr = NULL;
13611 descr_tail = &descr;
13613 for (p = loc; p; p = XEXP (p, 1))
13615 unsigned int bitsize = decl_piece_bitsize (p);
13616 rtx loc_note = *decl_piece_varloc_ptr (p);
13617 dw_loc_descr_ref cur_descr;
13618 dw_loc_descr_ref *tail, last = NULL;
13619 unsigned int opsize = 0;
13621 if (loc_note == NULL_RTX
13622 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13624 padsize += bitsize;
13625 continue;
13627 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13628 varloc = NOTE_VAR_LOCATION (loc_note);
13629 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13630 if (cur_descr == NULL)
13632 padsize += bitsize;
13633 continue;
13636 /* Check that cur_descr either doesn't use
13637 DW_OP_*piece operations, or their sum is equal
13638 to bitsize. Otherwise we can't embed it. */
13639 for (tail = &cur_descr; *tail != NULL;
13640 tail = &(*tail)->dw_loc_next)
13641 if ((*tail)->dw_loc_opc == DW_OP_piece)
13643 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13644 * BITS_PER_UNIT;
13645 last = *tail;
13647 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13649 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13650 last = *tail;
13653 if (last != NULL && opsize != bitsize)
13655 padsize += bitsize;
13656 /* Discard the current piece of the descriptor and release any
13657 addr_table entries it uses. */
13658 remove_loc_list_addr_table_entries (cur_descr);
13659 continue;
13662 /* If there is a hole, add DW_OP_*piece after empty DWARF
13663 expression, which means that those bits are optimized out. */
13664 if (padsize)
13666 if (padsize > decl_size)
13668 remove_loc_list_addr_table_entries (cur_descr);
13669 goto discard_descr;
13671 decl_size -= padsize;
13672 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13673 if (*descr_tail == NULL)
13675 remove_loc_list_addr_table_entries (cur_descr);
13676 goto discard_descr;
13678 descr_tail = &(*descr_tail)->dw_loc_next;
13679 padsize = 0;
13681 *descr_tail = cur_descr;
13682 descr_tail = tail;
13683 if (bitsize > decl_size)
13684 goto discard_descr;
13685 decl_size -= bitsize;
13686 if (last == NULL)
13688 HOST_WIDE_INT offset = 0;
13689 if (GET_CODE (varloc) == VAR_LOCATION
13690 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13692 varloc = PAT_VAR_LOCATION_LOC (varloc);
13693 if (GET_CODE (varloc) == EXPR_LIST)
13694 varloc = XEXP (varloc, 0);
13698 if (GET_CODE (varloc) == CONST
13699 || GET_CODE (varloc) == SIGN_EXTEND
13700 || GET_CODE (varloc) == ZERO_EXTEND)
13701 varloc = XEXP (varloc, 0);
13702 else if (GET_CODE (varloc) == SUBREG)
13703 varloc = SUBREG_REG (varloc);
13704 else
13705 break;
13707 while (1);
13708 /* DW_OP_bit_size offset should be zero for register
13709 or implicit location descriptions and empty location
13710 descriptions, but for memory addresses needs big endian
13711 adjustment. */
13712 if (MEM_P (varloc))
13714 unsigned HOST_WIDE_INT memsize
13715 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13716 if (memsize != bitsize)
13718 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13719 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13720 goto discard_descr;
13721 if (memsize < bitsize)
13722 goto discard_descr;
13723 if (BITS_BIG_ENDIAN)
13724 offset = memsize - bitsize;
13728 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13729 if (*descr_tail == NULL)
13730 goto discard_descr;
13731 descr_tail = &(*descr_tail)->dw_loc_next;
13735 /* If there were any non-empty expressions, add padding till the end of
13736 the decl. */
13737 if (descr != NULL && decl_size != 0)
13739 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13740 if (*descr_tail == NULL)
13741 goto discard_descr;
13743 return descr;
13745 discard_descr:
13746 /* Discard the descriptor and release any addr_table entries it uses. */
13747 remove_loc_list_addr_table_entries (descr);
13748 return NULL;
13751 /* Return the dwarf representation of the location list LOC_LIST of
13752 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13753 function. */
13755 static dw_loc_list_ref
13756 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13758 const char *endname, *secname;
13759 rtx varloc;
13760 enum var_init_status initialized;
13761 struct var_loc_node *node;
13762 dw_loc_descr_ref descr;
13763 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13764 dw_loc_list_ref list = NULL;
13765 dw_loc_list_ref *listp = &list;
13767 /* Now that we know what section we are using for a base,
13768 actually construct the list of locations.
13769 The first location information is what is passed to the
13770 function that creates the location list, and the remaining
13771 locations just get added on to that list.
13772 Note that we only know the start address for a location
13773 (IE location changes), so to build the range, we use
13774 the range [current location start, next location start].
13775 This means we have to special case the last node, and generate
13776 a range of [last location start, end of function label]. */
13778 secname = secname_for_decl (decl);
13780 for (node = loc_list->first; node; node = node->next)
13781 if (GET_CODE (node->loc) == EXPR_LIST
13782 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13784 if (GET_CODE (node->loc) == EXPR_LIST)
13786 /* This requires DW_OP_{,bit_}piece, which is not usable
13787 inside DWARF expressions. */
13788 if (want_address != 2)
13789 continue;
13790 descr = dw_sra_loc_expr (decl, node->loc);
13791 if (descr == NULL)
13792 continue;
13794 else
13796 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13797 varloc = NOTE_VAR_LOCATION (node->loc);
13798 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13800 if (descr)
13802 bool range_across_switch = false;
13803 /* If section switch happens in between node->label
13804 and node->next->label (or end of function) and
13805 we can't emit it as a single entry list,
13806 emit two ranges, first one ending at the end
13807 of first partition and second one starting at the
13808 beginning of second partition. */
13809 if (node == loc_list->last_before_switch
13810 && (node != loc_list->first || loc_list->first->next)
13811 && current_function_decl)
13813 endname = cfun->fde->dw_fde_end;
13814 range_across_switch = true;
13816 /* The variable has a location between NODE->LABEL and
13817 NODE->NEXT->LABEL. */
13818 else if (node->next)
13819 endname = node->next->label;
13820 /* If the variable has a location at the last label
13821 it keeps its location until the end of function. */
13822 else if (!current_function_decl)
13823 endname = text_end_label;
13824 else
13826 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13827 current_function_funcdef_no);
13828 endname = ggc_strdup (label_id);
13831 *listp = new_loc_list (descr, node->label, endname, secname);
13832 if (TREE_CODE (decl) == PARM_DECL
13833 && node == loc_list->first
13834 && NOTE_P (node->loc)
13835 && strcmp (node->label, endname) == 0)
13836 (*listp)->force = true;
13837 listp = &(*listp)->dw_loc_next;
13839 if (range_across_switch)
13841 if (GET_CODE (node->loc) == EXPR_LIST)
13842 descr = dw_sra_loc_expr (decl, node->loc);
13843 else
13845 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13846 varloc = NOTE_VAR_LOCATION (node->loc);
13847 descr = dw_loc_list_1 (decl, varloc, want_address,
13848 initialized);
13850 gcc_assert (descr);
13851 /* The variable has a location between NODE->LABEL and
13852 NODE->NEXT->LABEL. */
13853 if (node->next)
13854 endname = node->next->label;
13855 else
13856 endname = cfun->fde->dw_fde_second_end;
13857 *listp = new_loc_list (descr,
13858 cfun->fde->dw_fde_second_begin,
13859 endname, secname);
13860 listp = &(*listp)->dw_loc_next;
13865 /* Try to avoid the overhead of a location list emitting a location
13866 expression instead, but only if we didn't have more than one
13867 location entry in the first place. If some entries were not
13868 representable, we don't want to pretend a single entry that was
13869 applies to the entire scope in which the variable is
13870 available. */
13871 if (list && loc_list->first->next)
13872 gen_llsym (list);
13874 return list;
13877 /* Return if the loc_list has only single element and thus can be represented
13878 as location description. */
13880 static bool
13881 single_element_loc_list_p (dw_loc_list_ref list)
13883 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13884 return !list->ll_symbol;
13887 /* To each location in list LIST add loc descr REF. */
13889 static void
13890 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13892 dw_loc_descr_ref copy;
13893 add_loc_descr (&list->expr, ref);
13894 list = list->dw_loc_next;
13895 while (list)
13897 copy = ggc_alloc_dw_loc_descr_node ();
13898 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13899 add_loc_descr (&list->expr, copy);
13900 while (copy->dw_loc_next)
13902 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13903 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13904 copy->dw_loc_next = new_copy;
13905 copy = new_copy;
13907 list = list->dw_loc_next;
13911 /* Given two lists RET and LIST
13912 produce location list that is result of adding expression in LIST
13913 to expression in RET on each position in program.
13914 Might be destructive on both RET and LIST.
13916 TODO: We handle only simple cases of RET or LIST having at most one
13917 element. General case would inolve sorting the lists in program order
13918 and merging them that will need some additional work.
13919 Adding that will improve quality of debug info especially for SRA-ed
13920 structures. */
13922 static void
13923 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13925 if (!list)
13926 return;
13927 if (!*ret)
13929 *ret = list;
13930 return;
13932 if (!list->dw_loc_next)
13934 add_loc_descr_to_each (*ret, list->expr);
13935 return;
13937 if (!(*ret)->dw_loc_next)
13939 add_loc_descr_to_each (list, (*ret)->expr);
13940 *ret = list;
13941 return;
13943 expansion_failed (NULL_TREE, NULL_RTX,
13944 "Don't know how to merge two non-trivial"
13945 " location lists.\n");
13946 *ret = NULL;
13947 return;
13950 /* LOC is constant expression. Try a luck, look it up in constant
13951 pool and return its loc_descr of its address. */
13953 static dw_loc_descr_ref
13954 cst_pool_loc_descr (tree loc)
13956 /* Get an RTL for this, if something has been emitted. */
13957 rtx rtl = lookup_constant_def (loc);
13959 if (!rtl || !MEM_P (rtl))
13961 gcc_assert (!rtl);
13962 return 0;
13964 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13966 /* TODO: We might get more coverage if we was actually delaying expansion
13967 of all expressions till end of compilation when constant pools are fully
13968 populated. */
13969 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13971 expansion_failed (loc, NULL_RTX,
13972 "CST value in contant pool but not marked.");
13973 return 0;
13975 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13976 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13979 /* Return dw_loc_list representing address of addr_expr LOC
13980 by looking for inner INDIRECT_REF expression and turning
13981 it into simple arithmetics. */
13983 static dw_loc_list_ref
13984 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13986 tree obj, offset;
13987 HOST_WIDE_INT bitsize, bitpos, bytepos;
13988 enum machine_mode mode;
13989 int unsignedp, volatilep = 0;
13990 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13992 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13993 &bitsize, &bitpos, &offset, &mode,
13994 &unsignedp, &volatilep, false);
13995 STRIP_NOPS (obj);
13996 if (bitpos % BITS_PER_UNIT)
13998 expansion_failed (loc, NULL_RTX, "bitfield access");
13999 return 0;
14001 if (!INDIRECT_REF_P (obj))
14003 expansion_failed (obj,
14004 NULL_RTX, "no indirect ref in inner refrence");
14005 return 0;
14007 if (!offset && !bitpos)
14008 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14009 else if (toplev
14010 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14011 && (dwarf_version >= 4 || !dwarf_strict))
14013 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14014 if (!list_ret)
14015 return 0;
14016 if (offset)
14018 /* Variable offset. */
14019 list_ret1 = loc_list_from_tree (offset, 0);
14020 if (list_ret1 == 0)
14021 return 0;
14022 add_loc_list (&list_ret, list_ret1);
14023 if (!list_ret)
14024 return 0;
14025 add_loc_descr_to_each (list_ret,
14026 new_loc_descr (DW_OP_plus, 0, 0));
14028 bytepos = bitpos / BITS_PER_UNIT;
14029 if (bytepos > 0)
14030 add_loc_descr_to_each (list_ret,
14031 new_loc_descr (DW_OP_plus_uconst,
14032 bytepos, 0));
14033 else if (bytepos < 0)
14034 loc_list_plus_const (list_ret, bytepos);
14035 add_loc_descr_to_each (list_ret,
14036 new_loc_descr (DW_OP_stack_value, 0, 0));
14038 return list_ret;
14042 /* Generate Dwarf location list representing LOC.
14043 If WANT_ADDRESS is false, expression computing LOC will be computed
14044 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14045 if WANT_ADDRESS is 2, expression computing address useable in location
14046 will be returned (i.e. DW_OP_reg can be used
14047 to refer to register values). */
14049 static dw_loc_list_ref
14050 loc_list_from_tree (tree loc, int want_address)
14052 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14053 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14054 int have_address = 0;
14055 enum dwarf_location_atom op;
14057 /* ??? Most of the time we do not take proper care for sign/zero
14058 extending the values properly. Hopefully this won't be a real
14059 problem... */
14061 switch (TREE_CODE (loc))
14063 case ERROR_MARK:
14064 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14065 return 0;
14067 case PLACEHOLDER_EXPR:
14068 /* This case involves extracting fields from an object to determine the
14069 position of other fields. We don't try to encode this here. The
14070 only user of this is Ada, which encodes the needed information using
14071 the names of types. */
14072 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14073 return 0;
14075 case CALL_EXPR:
14076 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14077 /* There are no opcodes for these operations. */
14078 return 0;
14080 case PREINCREMENT_EXPR:
14081 case PREDECREMENT_EXPR:
14082 case POSTINCREMENT_EXPR:
14083 case POSTDECREMENT_EXPR:
14084 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14085 /* There are no opcodes for these operations. */
14086 return 0;
14088 case ADDR_EXPR:
14089 /* If we already want an address, see if there is INDIRECT_REF inside
14090 e.g. for &this->field. */
14091 if (want_address)
14093 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14094 (loc, want_address == 2);
14095 if (list_ret)
14096 have_address = 1;
14097 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14098 && (ret = cst_pool_loc_descr (loc)))
14099 have_address = 1;
14101 /* Otherwise, process the argument and look for the address. */
14102 if (!list_ret && !ret)
14103 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14104 else
14106 if (want_address)
14107 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14108 return NULL;
14110 break;
14112 case VAR_DECL:
14113 if (DECL_THREAD_LOCAL_P (loc))
14115 rtx rtl;
14116 enum dwarf_location_atom tls_op;
14117 enum dtprel_bool dtprel = dtprel_false;
14119 if (targetm.have_tls)
14121 /* If this is not defined, we have no way to emit the
14122 data. */
14123 if (!targetm.asm_out.output_dwarf_dtprel)
14124 return 0;
14126 /* The way DW_OP_GNU_push_tls_address is specified, we
14127 can only look up addresses of objects in the current
14128 module. We used DW_OP_addr as first op, but that's
14129 wrong, because DW_OP_addr is relocated by the debug
14130 info consumer, while DW_OP_GNU_push_tls_address
14131 operand shouldn't be. */
14132 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14133 return 0;
14134 dtprel = dtprel_true;
14135 tls_op = DW_OP_GNU_push_tls_address;
14137 else
14139 if (!targetm.emutls.debug_form_tls_address
14140 || !(dwarf_version >= 3 || !dwarf_strict))
14141 return 0;
14142 /* We stuffed the control variable into the DECL_VALUE_EXPR
14143 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14144 no longer appear in gimple code. We used the control
14145 variable in specific so that we could pick it up here. */
14146 loc = DECL_VALUE_EXPR (loc);
14147 tls_op = DW_OP_form_tls_address;
14150 rtl = rtl_for_decl_location (loc);
14151 if (rtl == NULL_RTX)
14152 return 0;
14154 if (!MEM_P (rtl))
14155 return 0;
14156 rtl = XEXP (rtl, 0);
14157 if (! CONSTANT_P (rtl))
14158 return 0;
14160 ret = new_addr_loc_descr (rtl, dtprel);
14161 ret1 = new_loc_descr (tls_op, 0, 0);
14162 add_loc_descr (&ret, ret1);
14164 have_address = 1;
14165 break;
14167 /* FALLTHRU */
14169 case PARM_DECL:
14170 case RESULT_DECL:
14171 if (DECL_HAS_VALUE_EXPR_P (loc))
14172 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14173 want_address);
14174 /* FALLTHRU */
14176 case FUNCTION_DECL:
14178 rtx rtl;
14179 var_loc_list *loc_list = lookup_decl_loc (loc);
14181 if (loc_list && loc_list->first)
14183 list_ret = dw_loc_list (loc_list, loc, want_address);
14184 have_address = want_address != 0;
14185 break;
14187 rtl = rtl_for_decl_location (loc);
14188 if (rtl == NULL_RTX)
14190 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14191 return 0;
14193 else if (CONST_INT_P (rtl))
14195 HOST_WIDE_INT val = INTVAL (rtl);
14196 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14197 val &= GET_MODE_MASK (DECL_MODE (loc));
14198 ret = int_loc_descriptor (val);
14200 else if (GET_CODE (rtl) == CONST_STRING)
14202 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14203 return 0;
14205 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14206 ret = new_addr_loc_descr (rtl, dtprel_false);
14207 else
14209 enum machine_mode mode, mem_mode;
14211 /* Certain constructs can only be represented at top-level. */
14212 if (want_address == 2)
14214 ret = loc_descriptor (rtl, VOIDmode,
14215 VAR_INIT_STATUS_INITIALIZED);
14216 have_address = 1;
14218 else
14220 mode = GET_MODE (rtl);
14221 mem_mode = VOIDmode;
14222 if (MEM_P (rtl))
14224 mem_mode = mode;
14225 mode = get_address_mode (rtl);
14226 rtl = XEXP (rtl, 0);
14227 have_address = 1;
14229 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14230 VAR_INIT_STATUS_INITIALIZED);
14232 if (!ret)
14233 expansion_failed (loc, rtl,
14234 "failed to produce loc descriptor for rtl");
14237 break;
14239 case MEM_REF:
14240 /* ??? FIXME. */
14241 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14242 return 0;
14243 /* Fallthru. */
14244 case INDIRECT_REF:
14245 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14246 have_address = 1;
14247 break;
14249 case COMPOUND_EXPR:
14250 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14252 CASE_CONVERT:
14253 case VIEW_CONVERT_EXPR:
14254 case SAVE_EXPR:
14255 case MODIFY_EXPR:
14256 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14258 case COMPONENT_REF:
14259 case BIT_FIELD_REF:
14260 case ARRAY_REF:
14261 case ARRAY_RANGE_REF:
14262 case REALPART_EXPR:
14263 case IMAGPART_EXPR:
14265 tree obj, offset;
14266 HOST_WIDE_INT bitsize, bitpos, bytepos;
14267 enum machine_mode mode;
14268 int unsignedp, volatilep = 0;
14270 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14271 &unsignedp, &volatilep, false);
14273 gcc_assert (obj != loc);
14275 list_ret = loc_list_from_tree (obj,
14276 want_address == 2
14277 && !bitpos && !offset ? 2 : 1);
14278 /* TODO: We can extract value of the small expression via shifting even
14279 for nonzero bitpos. */
14280 if (list_ret == 0)
14281 return 0;
14282 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14284 expansion_failed (loc, NULL_RTX,
14285 "bitfield access");
14286 return 0;
14289 if (offset != NULL_TREE)
14291 /* Variable offset. */
14292 list_ret1 = loc_list_from_tree (offset, 0);
14293 if (list_ret1 == 0)
14294 return 0;
14295 add_loc_list (&list_ret, list_ret1);
14296 if (!list_ret)
14297 return 0;
14298 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14301 bytepos = bitpos / BITS_PER_UNIT;
14302 if (bytepos > 0)
14303 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14304 else if (bytepos < 0)
14305 loc_list_plus_const (list_ret, bytepos);
14307 have_address = 1;
14308 break;
14311 case INTEGER_CST:
14312 if ((want_address || !tree_fits_shwi_p (loc))
14313 && (ret = cst_pool_loc_descr (loc)))
14314 have_address = 1;
14315 else if (want_address == 2
14316 && tree_fits_shwi_p (loc)
14317 && (ret = address_of_int_loc_descriptor
14318 (int_size_in_bytes (TREE_TYPE (loc)),
14319 tree_to_shwi (loc))))
14320 have_address = 1;
14321 else if (tree_fits_shwi_p (loc))
14322 ret = int_loc_descriptor (tree_to_shwi (loc));
14323 else
14325 expansion_failed (loc, NULL_RTX,
14326 "Integer operand is not host integer");
14327 return 0;
14329 break;
14331 case CONSTRUCTOR:
14332 case REAL_CST:
14333 case STRING_CST:
14334 case COMPLEX_CST:
14335 if ((ret = cst_pool_loc_descr (loc)))
14336 have_address = 1;
14337 else
14338 /* We can construct small constants here using int_loc_descriptor. */
14339 expansion_failed (loc, NULL_RTX,
14340 "constructor or constant not in constant pool");
14341 break;
14343 case TRUTH_AND_EXPR:
14344 case TRUTH_ANDIF_EXPR:
14345 case BIT_AND_EXPR:
14346 op = DW_OP_and;
14347 goto do_binop;
14349 case TRUTH_XOR_EXPR:
14350 case BIT_XOR_EXPR:
14351 op = DW_OP_xor;
14352 goto do_binop;
14354 case TRUTH_OR_EXPR:
14355 case TRUTH_ORIF_EXPR:
14356 case BIT_IOR_EXPR:
14357 op = DW_OP_or;
14358 goto do_binop;
14360 case FLOOR_DIV_EXPR:
14361 case CEIL_DIV_EXPR:
14362 case ROUND_DIV_EXPR:
14363 case TRUNC_DIV_EXPR:
14364 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14365 return 0;
14366 op = DW_OP_div;
14367 goto do_binop;
14369 case MINUS_EXPR:
14370 op = DW_OP_minus;
14371 goto do_binop;
14373 case FLOOR_MOD_EXPR:
14374 case CEIL_MOD_EXPR:
14375 case ROUND_MOD_EXPR:
14376 case TRUNC_MOD_EXPR:
14377 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14379 op = DW_OP_mod;
14380 goto do_binop;
14382 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14383 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14384 if (list_ret == 0 || list_ret1 == 0)
14385 return 0;
14387 add_loc_list (&list_ret, list_ret1);
14388 if (list_ret == 0)
14389 return 0;
14390 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14391 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14392 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14393 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14394 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14395 break;
14397 case MULT_EXPR:
14398 op = DW_OP_mul;
14399 goto do_binop;
14401 case LSHIFT_EXPR:
14402 op = DW_OP_shl;
14403 goto do_binop;
14405 case RSHIFT_EXPR:
14406 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14407 goto do_binop;
14409 case POINTER_PLUS_EXPR:
14410 case PLUS_EXPR:
14411 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14413 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14414 if (list_ret == 0)
14415 return 0;
14417 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14418 break;
14421 op = DW_OP_plus;
14422 goto do_binop;
14424 case LE_EXPR:
14425 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14426 return 0;
14428 op = DW_OP_le;
14429 goto do_binop;
14431 case GE_EXPR:
14432 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14433 return 0;
14435 op = DW_OP_ge;
14436 goto do_binop;
14438 case LT_EXPR:
14439 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14440 return 0;
14442 op = DW_OP_lt;
14443 goto do_binop;
14445 case GT_EXPR:
14446 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14447 return 0;
14449 op = DW_OP_gt;
14450 goto do_binop;
14452 case EQ_EXPR:
14453 op = DW_OP_eq;
14454 goto do_binop;
14456 case NE_EXPR:
14457 op = DW_OP_ne;
14458 goto do_binop;
14460 do_binop:
14461 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14462 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14463 if (list_ret == 0 || list_ret1 == 0)
14464 return 0;
14466 add_loc_list (&list_ret, list_ret1);
14467 if (list_ret == 0)
14468 return 0;
14469 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14470 break;
14472 case TRUTH_NOT_EXPR:
14473 case BIT_NOT_EXPR:
14474 op = DW_OP_not;
14475 goto do_unop;
14477 case ABS_EXPR:
14478 op = DW_OP_abs;
14479 goto do_unop;
14481 case NEGATE_EXPR:
14482 op = DW_OP_neg;
14483 goto do_unop;
14485 do_unop:
14486 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14487 if (list_ret == 0)
14488 return 0;
14490 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14491 break;
14493 case MIN_EXPR:
14494 case MAX_EXPR:
14496 const enum tree_code code =
14497 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14499 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14500 build2 (code, integer_type_node,
14501 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14502 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14505 /* ... fall through ... */
14507 case COND_EXPR:
14509 dw_loc_descr_ref lhs
14510 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14511 dw_loc_list_ref rhs
14512 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14513 dw_loc_descr_ref bra_node, jump_node, tmp;
14515 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14516 if (list_ret == 0 || lhs == 0 || rhs == 0)
14517 return 0;
14519 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14520 add_loc_descr_to_each (list_ret, bra_node);
14522 add_loc_list (&list_ret, rhs);
14523 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14524 add_loc_descr_to_each (list_ret, jump_node);
14526 add_loc_descr_to_each (list_ret, lhs);
14527 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14528 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14530 /* ??? Need a node to point the skip at. Use a nop. */
14531 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14532 add_loc_descr_to_each (list_ret, tmp);
14533 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14534 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14536 break;
14538 case FIX_TRUNC_EXPR:
14539 return 0;
14541 default:
14542 /* Leave front-end specific codes as simply unknown. This comes
14543 up, for instance, with the C STMT_EXPR. */
14544 if ((unsigned int) TREE_CODE (loc)
14545 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14547 expansion_failed (loc, NULL_RTX,
14548 "language specific tree node");
14549 return 0;
14552 #ifdef ENABLE_CHECKING
14553 /* Otherwise this is a generic code; we should just lists all of
14554 these explicitly. We forgot one. */
14555 gcc_unreachable ();
14556 #else
14557 /* In a release build, we want to degrade gracefully: better to
14558 generate incomplete debugging information than to crash. */
14559 return NULL;
14560 #endif
14563 if (!ret && !list_ret)
14564 return 0;
14566 if (want_address == 2 && !have_address
14567 && (dwarf_version >= 4 || !dwarf_strict))
14569 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14571 expansion_failed (loc, NULL_RTX,
14572 "DWARF address size mismatch");
14573 return 0;
14575 if (ret)
14576 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14577 else
14578 add_loc_descr_to_each (list_ret,
14579 new_loc_descr (DW_OP_stack_value, 0, 0));
14580 have_address = 1;
14582 /* Show if we can't fill the request for an address. */
14583 if (want_address && !have_address)
14585 expansion_failed (loc, NULL_RTX,
14586 "Want address and only have value");
14587 return 0;
14590 gcc_assert (!ret || !list_ret);
14592 /* If we've got an address and don't want one, dereference. */
14593 if (!want_address && have_address)
14595 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14597 if (size > DWARF2_ADDR_SIZE || size == -1)
14599 expansion_failed (loc, NULL_RTX,
14600 "DWARF address size mismatch");
14601 return 0;
14603 else if (size == DWARF2_ADDR_SIZE)
14604 op = DW_OP_deref;
14605 else
14606 op = DW_OP_deref_size;
14608 if (ret)
14609 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14610 else
14611 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14613 if (ret)
14614 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14616 return list_ret;
14619 /* Same as above but return only single location expression. */
14620 static dw_loc_descr_ref
14621 loc_descriptor_from_tree (tree loc, int want_address)
14623 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14624 if (!ret)
14625 return NULL;
14626 if (ret->dw_loc_next)
14628 expansion_failed (loc, NULL_RTX,
14629 "Location list where only loc descriptor needed");
14630 return NULL;
14632 return ret->expr;
14635 /* Given a value, round it up to the lowest multiple of `boundary'
14636 which is not less than the value itself. */
14638 static inline HOST_WIDE_INT
14639 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14641 return (((value + boundary - 1) / boundary) * boundary);
14644 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14645 pointer to the declared type for the relevant field variable, or return
14646 `integer_type_node' if the given node turns out to be an
14647 ERROR_MARK node. */
14649 static inline tree
14650 field_type (const_tree decl)
14652 tree type;
14654 if (TREE_CODE (decl) == ERROR_MARK)
14655 return integer_type_node;
14657 type = DECL_BIT_FIELD_TYPE (decl);
14658 if (type == NULL_TREE)
14659 type = TREE_TYPE (decl);
14661 return type;
14664 /* Given a pointer to a tree node, return the alignment in bits for
14665 it, or else return BITS_PER_WORD if the node actually turns out to
14666 be an ERROR_MARK node. */
14668 static inline unsigned
14669 simple_type_align_in_bits (const_tree type)
14671 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14674 static inline unsigned
14675 simple_decl_align_in_bits (const_tree decl)
14677 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14680 /* Return the result of rounding T up to ALIGN. */
14682 static inline double_int
14683 round_up_to_align (double_int t, unsigned int align)
14685 double_int alignd = double_int::from_uhwi (align);
14686 t += alignd;
14687 t += double_int_minus_one;
14688 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14689 t *= alignd;
14690 return t;
14693 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14694 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14695 or return 0 if we are unable to determine what that offset is, either
14696 because the argument turns out to be a pointer to an ERROR_MARK node, or
14697 because the offset is actually variable. (We can't handle the latter case
14698 just yet). */
14700 static HOST_WIDE_INT
14701 field_byte_offset (const_tree decl)
14703 double_int object_offset_in_bits;
14704 double_int object_offset_in_bytes;
14705 double_int bitpos_int;
14707 if (TREE_CODE (decl) == ERROR_MARK)
14708 return 0;
14710 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14712 /* We cannot yet cope with fields whose positions are variable, so
14713 for now, when we see such things, we simply return 0. Someday, we may
14714 be able to handle such cases, but it will be damn difficult. */
14715 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14716 return 0;
14718 bitpos_int = tree_to_double_int (bit_position (decl));
14720 #ifdef PCC_BITFIELD_TYPE_MATTERS
14721 if (PCC_BITFIELD_TYPE_MATTERS)
14723 tree type;
14724 tree field_size_tree;
14725 double_int deepest_bitpos;
14726 double_int field_size_in_bits;
14727 unsigned int type_align_in_bits;
14728 unsigned int decl_align_in_bits;
14729 double_int type_size_in_bits;
14731 type = field_type (decl);
14732 type_size_in_bits = double_int_type_size_in_bits (type);
14733 type_align_in_bits = simple_type_align_in_bits (type);
14735 field_size_tree = DECL_SIZE (decl);
14737 /* The size could be unspecified if there was an error, or for
14738 a flexible array member. */
14739 if (!field_size_tree)
14740 field_size_tree = bitsize_zero_node;
14742 /* If the size of the field is not constant, use the type size. */
14743 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14744 field_size_in_bits = tree_to_double_int (field_size_tree);
14745 else
14746 field_size_in_bits = type_size_in_bits;
14748 decl_align_in_bits = simple_decl_align_in_bits (decl);
14750 /* The GCC front-end doesn't make any attempt to keep track of the
14751 starting bit offset (relative to the start of the containing
14752 structure type) of the hypothetical "containing object" for a
14753 bit-field. Thus, when computing the byte offset value for the
14754 start of the "containing object" of a bit-field, we must deduce
14755 this information on our own. This can be rather tricky to do in
14756 some cases. For example, handling the following structure type
14757 definition when compiling for an i386/i486 target (which only
14758 aligns long long's to 32-bit boundaries) can be very tricky:
14760 struct S { int field1; long long field2:31; };
14762 Fortunately, there is a simple rule-of-thumb which can be used
14763 in such cases. When compiling for an i386/i486, GCC will
14764 allocate 8 bytes for the structure shown above. It decides to
14765 do this based upon one simple rule for bit-field allocation.
14766 GCC allocates each "containing object" for each bit-field at
14767 the first (i.e. lowest addressed) legitimate alignment boundary
14768 (based upon the required minimum alignment for the declared
14769 type of the field) which it can possibly use, subject to the
14770 condition that there is still enough available space remaining
14771 in the containing object (when allocated at the selected point)
14772 to fully accommodate all of the bits of the bit-field itself.
14774 This simple rule makes it obvious why GCC allocates 8 bytes for
14775 each object of the structure type shown above. When looking
14776 for a place to allocate the "containing object" for `field2',
14777 the compiler simply tries to allocate a 64-bit "containing
14778 object" at each successive 32-bit boundary (starting at zero)
14779 until it finds a place to allocate that 64- bit field such that
14780 at least 31 contiguous (and previously unallocated) bits remain
14781 within that selected 64 bit field. (As it turns out, for the
14782 example above, the compiler finds it is OK to allocate the
14783 "containing object" 64-bit field at bit-offset zero within the
14784 structure type.)
14786 Here we attempt to work backwards from the limited set of facts
14787 we're given, and we try to deduce from those facts, where GCC
14788 must have believed that the containing object started (within
14789 the structure type). The value we deduce is then used (by the
14790 callers of this routine) to generate DW_AT_location and
14791 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14792 the case of DW_AT_location, regular fields as well). */
14794 /* Figure out the bit-distance from the start of the structure to
14795 the "deepest" bit of the bit-field. */
14796 deepest_bitpos = bitpos_int + field_size_in_bits;
14798 /* This is the tricky part. Use some fancy footwork to deduce
14799 where the lowest addressed bit of the containing object must
14800 be. */
14801 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14803 /* Round up to type_align by default. This works best for
14804 bitfields. */
14805 object_offset_in_bits
14806 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14808 if (object_offset_in_bits.ugt (bitpos_int))
14810 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14812 /* Round up to decl_align instead. */
14813 object_offset_in_bits
14814 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14817 else
14818 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14819 object_offset_in_bits = bitpos_int;
14821 object_offset_in_bytes
14822 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14823 true, TRUNC_DIV_EXPR);
14824 return object_offset_in_bytes.to_shwi ();
14827 /* The following routines define various Dwarf attributes and any data
14828 associated with them. */
14830 /* Add a location description attribute value to a DIE.
14832 This emits location attributes suitable for whole variables and
14833 whole parameters. Note that the location attributes for struct fields are
14834 generated by the routine `data_member_location_attribute' below. */
14836 static inline void
14837 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14838 dw_loc_list_ref descr)
14840 if (descr == 0)
14841 return;
14842 if (single_element_loc_list_p (descr))
14843 add_AT_loc (die, attr_kind, descr->expr);
14844 else
14845 add_AT_loc_list (die, attr_kind, descr);
14848 /* Add DW_AT_accessibility attribute to DIE if needed. */
14850 static void
14851 add_accessibility_attribute (dw_die_ref die, tree decl)
14853 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14854 children, otherwise the default is DW_ACCESS_public. In DWARF2
14855 the default has always been DW_ACCESS_public. */
14856 if (TREE_PROTECTED (decl))
14857 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14858 else if (TREE_PRIVATE (decl))
14860 if (dwarf_version == 2
14861 || die->die_parent == NULL
14862 || die->die_parent->die_tag != DW_TAG_class_type)
14863 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14865 else if (dwarf_version > 2
14866 && die->die_parent
14867 && die->die_parent->die_tag == DW_TAG_class_type)
14868 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14871 /* Attach the specialized form of location attribute used for data members of
14872 struct and union types. In the special case of a FIELD_DECL node which
14873 represents a bit-field, the "offset" part of this special location
14874 descriptor must indicate the distance in bytes from the lowest-addressed
14875 byte of the containing struct or union type to the lowest-addressed byte of
14876 the "containing object" for the bit-field. (See the `field_byte_offset'
14877 function above).
14879 For any given bit-field, the "containing object" is a hypothetical object
14880 (of some integral or enum type) within which the given bit-field lives. The
14881 type of this hypothetical "containing object" is always the same as the
14882 declared type of the individual bit-field itself (for GCC anyway... the
14883 DWARF spec doesn't actually mandate this). Note that it is the size (in
14884 bytes) of the hypothetical "containing object" which will be given in the
14885 DW_AT_byte_size attribute for this bit-field. (See the
14886 `byte_size_attribute' function below.) It is also used when calculating the
14887 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14888 function below.) */
14890 static void
14891 add_data_member_location_attribute (dw_die_ref die, tree decl)
14893 HOST_WIDE_INT offset;
14894 dw_loc_descr_ref loc_descr = 0;
14896 if (TREE_CODE (decl) == TREE_BINFO)
14898 /* We're working on the TAG_inheritance for a base class. */
14899 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14901 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14902 aren't at a fixed offset from all (sub)objects of the same
14903 type. We need to extract the appropriate offset from our
14904 vtable. The following dwarf expression means
14906 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14908 This is specific to the V3 ABI, of course. */
14910 dw_loc_descr_ref tmp;
14912 /* Make a copy of the object address. */
14913 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14914 add_loc_descr (&loc_descr, tmp);
14916 /* Extract the vtable address. */
14917 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14918 add_loc_descr (&loc_descr, tmp);
14920 /* Calculate the address of the offset. */
14921 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14922 gcc_assert (offset < 0);
14924 tmp = int_loc_descriptor (-offset);
14925 add_loc_descr (&loc_descr, tmp);
14926 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14927 add_loc_descr (&loc_descr, tmp);
14929 /* Extract the offset. */
14930 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14931 add_loc_descr (&loc_descr, tmp);
14933 /* Add it to the object address. */
14934 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14935 add_loc_descr (&loc_descr, tmp);
14937 else
14938 offset = tree_to_shwi (BINFO_OFFSET (decl));
14940 else
14941 offset = field_byte_offset (decl);
14943 if (! loc_descr)
14945 if (dwarf_version > 2)
14947 /* Don't need to output a location expression, just the constant. */
14948 if (offset < 0)
14949 add_AT_int (die, DW_AT_data_member_location, offset);
14950 else
14951 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14952 return;
14954 else
14956 enum dwarf_location_atom op;
14958 /* The DWARF2 standard says that we should assume that the structure
14959 address is already on the stack, so we can specify a structure
14960 field address by using DW_OP_plus_uconst. */
14961 op = DW_OP_plus_uconst;
14962 loc_descr = new_loc_descr (op, offset, 0);
14966 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14969 /* Writes integer values to dw_vec_const array. */
14971 static void
14972 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14974 while (size != 0)
14976 *dest++ = val & 0xff;
14977 val >>= 8;
14978 --size;
14982 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14984 static HOST_WIDE_INT
14985 extract_int (const unsigned char *src, unsigned int size)
14987 HOST_WIDE_INT val = 0;
14989 src += size;
14990 while (size != 0)
14992 val <<= 8;
14993 val |= *--src & 0xff;
14994 --size;
14996 return val;
14999 /* Writes double_int values to dw_vec_const array. */
15001 static void
15002 insert_double (double_int val, unsigned char *dest)
15004 unsigned char *p0 = dest;
15005 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
15007 if (WORDS_BIG_ENDIAN)
15009 p0 = p1;
15010 p1 = dest;
15013 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
15014 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
15017 /* Writes floating point values to dw_vec_const array. */
15019 static void
15020 insert_float (const_rtx rtl, unsigned char *array)
15022 REAL_VALUE_TYPE rv;
15023 long val[4];
15024 int i;
15026 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15027 real_to_target (val, &rv, GET_MODE (rtl));
15029 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15030 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15032 insert_int (val[i], 4, array);
15033 array += 4;
15037 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15038 does not have a "location" either in memory or in a register. These
15039 things can arise in GNU C when a constant is passed as an actual parameter
15040 to an inlined function. They can also arise in C++ where declared
15041 constants do not necessarily get memory "homes". */
15043 static bool
15044 add_const_value_attribute (dw_die_ref die, rtx rtl)
15046 switch (GET_CODE (rtl))
15048 case CONST_INT:
15050 HOST_WIDE_INT val = INTVAL (rtl);
15052 if (val < 0)
15053 add_AT_int (die, DW_AT_const_value, val);
15054 else
15055 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15057 return true;
15059 case CONST_DOUBLE:
15060 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15061 floating-point constant. A CONST_DOUBLE is used whenever the
15062 constant requires more than one word in order to be adequately
15063 represented. */
15065 enum machine_mode mode = GET_MODE (rtl);
15067 if (SCALAR_FLOAT_MODE_P (mode))
15069 unsigned int length = GET_MODE_SIZE (mode);
15070 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15072 insert_float (rtl, array);
15073 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15075 else
15076 add_AT_double (die, DW_AT_const_value,
15077 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15079 return true;
15081 case CONST_VECTOR:
15083 enum machine_mode mode = GET_MODE (rtl);
15084 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15085 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15086 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15087 (length * elt_size);
15088 unsigned int i;
15089 unsigned char *p;
15091 switch (GET_MODE_CLASS (mode))
15093 case MODE_VECTOR_INT:
15094 for (i = 0, p = array; i < length; i++, p += elt_size)
15096 rtx elt = CONST_VECTOR_ELT (rtl, i);
15097 double_int val = rtx_to_double_int (elt);
15099 if (elt_size <= sizeof (HOST_WIDE_INT))
15100 insert_int (val.to_shwi (), elt_size, p);
15101 else
15103 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15104 insert_double (val, p);
15107 break;
15109 case MODE_VECTOR_FLOAT:
15110 for (i = 0, p = array; i < length; i++, p += elt_size)
15112 rtx elt = CONST_VECTOR_ELT (rtl, i);
15113 insert_float (elt, p);
15115 break;
15117 default:
15118 gcc_unreachable ();
15121 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15123 return true;
15125 case CONST_STRING:
15126 if (dwarf_version >= 4 || !dwarf_strict)
15128 dw_loc_descr_ref loc_result;
15129 resolve_one_addr (&rtl, NULL);
15130 rtl_addr:
15131 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15132 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15133 add_AT_loc (die, DW_AT_location, loc_result);
15134 vec_safe_push (used_rtx_array, rtl);
15135 return true;
15137 return false;
15139 case CONST:
15140 if (CONSTANT_P (XEXP (rtl, 0)))
15141 return add_const_value_attribute (die, XEXP (rtl, 0));
15142 /* FALLTHROUGH */
15143 case SYMBOL_REF:
15144 if (!const_ok_for_output (rtl))
15145 return false;
15146 case LABEL_REF:
15147 if (dwarf_version >= 4 || !dwarf_strict)
15148 goto rtl_addr;
15149 return false;
15151 case PLUS:
15152 /* In cases where an inlined instance of an inline function is passed
15153 the address of an `auto' variable (which is local to the caller) we
15154 can get a situation where the DECL_RTL of the artificial local
15155 variable (for the inlining) which acts as a stand-in for the
15156 corresponding formal parameter (of the inline function) will look
15157 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15158 exactly a compile-time constant expression, but it isn't the address
15159 of the (artificial) local variable either. Rather, it represents the
15160 *value* which the artificial local variable always has during its
15161 lifetime. We currently have no way to represent such quasi-constant
15162 values in Dwarf, so for now we just punt and generate nothing. */
15163 return false;
15165 case HIGH:
15166 case CONST_FIXED:
15167 return false;
15169 case MEM:
15170 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15171 && MEM_READONLY_P (rtl)
15172 && GET_MODE (rtl) == BLKmode)
15174 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15175 return true;
15177 return false;
15179 default:
15180 /* No other kinds of rtx should be possible here. */
15181 gcc_unreachable ();
15183 return false;
15186 /* Determine whether the evaluation of EXPR references any variables
15187 or functions which aren't otherwise used (and therefore may not be
15188 output). */
15189 static tree
15190 reference_to_unused (tree * tp, int * walk_subtrees,
15191 void * data ATTRIBUTE_UNUSED)
15193 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15194 *walk_subtrees = 0;
15196 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15197 && ! TREE_ASM_WRITTEN (*tp))
15198 return *tp;
15199 /* ??? The C++ FE emits debug information for using decls, so
15200 putting gcc_unreachable here falls over. See PR31899. For now
15201 be conservative. */
15202 else if (!cgraph_global_info_ready
15203 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15204 return *tp;
15205 else if (TREE_CODE (*tp) == VAR_DECL)
15207 varpool_node *node = varpool_get_node (*tp);
15208 if (!node || !node->definition)
15209 return *tp;
15211 else if (TREE_CODE (*tp) == FUNCTION_DECL
15212 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15214 /* The call graph machinery must have finished analyzing,
15215 optimizing and gimplifying the CU by now.
15216 So if *TP has no call graph node associated
15217 to it, it means *TP will not be emitted. */
15218 if (!cgraph_get_node (*tp))
15219 return *tp;
15221 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15222 return *tp;
15224 return NULL_TREE;
15227 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15228 for use in a later add_const_value_attribute call. */
15230 static rtx
15231 rtl_for_decl_init (tree init, tree type)
15233 rtx rtl = NULL_RTX;
15235 STRIP_NOPS (init);
15237 /* If a variable is initialized with a string constant without embedded
15238 zeros, build CONST_STRING. */
15239 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15241 tree enttype = TREE_TYPE (type);
15242 tree domain = TYPE_DOMAIN (type);
15243 enum machine_mode mode = TYPE_MODE (enttype);
15245 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15246 && domain
15247 && integer_zerop (TYPE_MIN_VALUE (domain))
15248 && compare_tree_int (TYPE_MAX_VALUE (domain),
15249 TREE_STRING_LENGTH (init) - 1) == 0
15250 && ((size_t) TREE_STRING_LENGTH (init)
15251 == strlen (TREE_STRING_POINTER (init)) + 1))
15253 rtl = gen_rtx_CONST_STRING (VOIDmode,
15254 ggc_strdup (TREE_STRING_POINTER (init)));
15255 rtl = gen_rtx_MEM (BLKmode, rtl);
15256 MEM_READONLY_P (rtl) = 1;
15259 /* Other aggregates, and complex values, could be represented using
15260 CONCAT: FIXME! */
15261 else if (AGGREGATE_TYPE_P (type)
15262 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15263 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15264 || TREE_CODE (type) == COMPLEX_TYPE)
15266 /* Vectors only work if their mode is supported by the target.
15267 FIXME: generic vectors ought to work too. */
15268 else if (TREE_CODE (type) == VECTOR_TYPE
15269 && !VECTOR_MODE_P (TYPE_MODE (type)))
15271 /* If the initializer is something that we know will expand into an
15272 immediate RTL constant, expand it now. We must be careful not to
15273 reference variables which won't be output. */
15274 else if (initializer_constant_valid_p (init, type)
15275 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15277 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15278 possible. */
15279 if (TREE_CODE (type) == VECTOR_TYPE)
15280 switch (TREE_CODE (init))
15282 case VECTOR_CST:
15283 break;
15284 case CONSTRUCTOR:
15285 if (TREE_CONSTANT (init))
15287 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15288 bool constant_p = true;
15289 tree value;
15290 unsigned HOST_WIDE_INT ix;
15292 /* Even when ctor is constant, it might contain non-*_CST
15293 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15294 belong into VECTOR_CST nodes. */
15295 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15296 if (!CONSTANT_CLASS_P (value))
15298 constant_p = false;
15299 break;
15302 if (constant_p)
15304 init = build_vector_from_ctor (type, elts);
15305 break;
15308 /* FALLTHRU */
15310 default:
15311 return NULL;
15314 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15316 /* If expand_expr returns a MEM, it wasn't immediate. */
15317 gcc_assert (!rtl || !MEM_P (rtl));
15320 return rtl;
15323 /* Generate RTL for the variable DECL to represent its location. */
15325 static rtx
15326 rtl_for_decl_location (tree decl)
15328 rtx rtl;
15330 /* Here we have to decide where we are going to say the parameter "lives"
15331 (as far as the debugger is concerned). We only have a couple of
15332 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15334 DECL_RTL normally indicates where the parameter lives during most of the
15335 activation of the function. If optimization is enabled however, this
15336 could be either NULL or else a pseudo-reg. Both of those cases indicate
15337 that the parameter doesn't really live anywhere (as far as the code
15338 generation parts of GCC are concerned) during most of the function's
15339 activation. That will happen (for example) if the parameter is never
15340 referenced within the function.
15342 We could just generate a location descriptor here for all non-NULL
15343 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15344 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15345 where DECL_RTL is NULL or is a pseudo-reg.
15347 Note however that we can only get away with using DECL_INCOMING_RTL as
15348 a backup substitute for DECL_RTL in certain limited cases. In cases
15349 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15350 we can be sure that the parameter was passed using the same type as it is
15351 declared to have within the function, and that its DECL_INCOMING_RTL
15352 points us to a place where a value of that type is passed.
15354 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15355 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15356 because in these cases DECL_INCOMING_RTL points us to a value of some
15357 type which is *different* from the type of the parameter itself. Thus,
15358 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15359 such cases, the debugger would end up (for example) trying to fetch a
15360 `float' from a place which actually contains the first part of a
15361 `double'. That would lead to really incorrect and confusing
15362 output at debug-time.
15364 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15365 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15366 are a couple of exceptions however. On little-endian machines we can
15367 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15368 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15369 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15370 when (on a little-endian machine) a non-prototyped function has a
15371 parameter declared to be of type `short' or `char'. In such cases,
15372 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15373 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15374 passed `int' value. If the debugger then uses that address to fetch
15375 a `short' or a `char' (on a little-endian machine) the result will be
15376 the correct data, so we allow for such exceptional cases below.
15378 Note that our goal here is to describe the place where the given formal
15379 parameter lives during most of the function's activation (i.e. between the
15380 end of the prologue and the start of the epilogue). We'll do that as best
15381 as we can. Note however that if the given formal parameter is modified
15382 sometime during the execution of the function, then a stack backtrace (at
15383 debug-time) will show the function as having been called with the *new*
15384 value rather than the value which was originally passed in. This happens
15385 rarely enough that it is not a major problem, but it *is* a problem, and
15386 I'd like to fix it.
15388 A future version of dwarf2out.c may generate two additional attributes for
15389 any given DW_TAG_formal_parameter DIE which will describe the "passed
15390 type" and the "passed location" for the given formal parameter in addition
15391 to the attributes we now generate to indicate the "declared type" and the
15392 "active location" for each parameter. This additional set of attributes
15393 could be used by debuggers for stack backtraces. Separately, note that
15394 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15395 This happens (for example) for inlined-instances of inline function formal
15396 parameters which are never referenced. This really shouldn't be
15397 happening. All PARM_DECL nodes should get valid non-NULL
15398 DECL_INCOMING_RTL values. FIXME. */
15400 /* Use DECL_RTL as the "location" unless we find something better. */
15401 rtl = DECL_RTL_IF_SET (decl);
15403 /* When generating abstract instances, ignore everything except
15404 constants, symbols living in memory, and symbols living in
15405 fixed registers. */
15406 if (! reload_completed)
15408 if (rtl
15409 && (CONSTANT_P (rtl)
15410 || (MEM_P (rtl)
15411 && CONSTANT_P (XEXP (rtl, 0)))
15412 || (REG_P (rtl)
15413 && TREE_CODE (decl) == VAR_DECL
15414 && TREE_STATIC (decl))))
15416 rtl = targetm.delegitimize_address (rtl);
15417 return rtl;
15419 rtl = NULL_RTX;
15421 else if (TREE_CODE (decl) == PARM_DECL)
15423 if (rtl == NULL_RTX
15424 || is_pseudo_reg (rtl)
15425 || (MEM_P (rtl)
15426 && is_pseudo_reg (XEXP (rtl, 0))
15427 && DECL_INCOMING_RTL (decl)
15428 && MEM_P (DECL_INCOMING_RTL (decl))
15429 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15431 tree declared_type = TREE_TYPE (decl);
15432 tree passed_type = DECL_ARG_TYPE (decl);
15433 enum machine_mode dmode = TYPE_MODE (declared_type);
15434 enum machine_mode pmode = TYPE_MODE (passed_type);
15436 /* This decl represents a formal parameter which was optimized out.
15437 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15438 all cases where (rtl == NULL_RTX) just below. */
15439 if (dmode == pmode)
15440 rtl = DECL_INCOMING_RTL (decl);
15441 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15442 && SCALAR_INT_MODE_P (dmode)
15443 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15444 && DECL_INCOMING_RTL (decl))
15446 rtx inc = DECL_INCOMING_RTL (decl);
15447 if (REG_P (inc))
15448 rtl = inc;
15449 else if (MEM_P (inc))
15451 if (BYTES_BIG_ENDIAN)
15452 rtl = adjust_address_nv (inc, dmode,
15453 GET_MODE_SIZE (pmode)
15454 - GET_MODE_SIZE (dmode));
15455 else
15456 rtl = inc;
15461 /* If the parm was passed in registers, but lives on the stack, then
15462 make a big endian correction if the mode of the type of the
15463 parameter is not the same as the mode of the rtl. */
15464 /* ??? This is the same series of checks that are made in dbxout.c before
15465 we reach the big endian correction code there. It isn't clear if all
15466 of these checks are necessary here, but keeping them all is the safe
15467 thing to do. */
15468 else if (MEM_P (rtl)
15469 && XEXP (rtl, 0) != const0_rtx
15470 && ! CONSTANT_P (XEXP (rtl, 0))
15471 /* Not passed in memory. */
15472 && !MEM_P (DECL_INCOMING_RTL (decl))
15473 /* Not passed by invisible reference. */
15474 && (!REG_P (XEXP (rtl, 0))
15475 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15476 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15477 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15478 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15479 #endif
15481 /* Big endian correction check. */
15482 && BYTES_BIG_ENDIAN
15483 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15484 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15485 < UNITS_PER_WORD))
15487 enum machine_mode addr_mode = get_address_mode (rtl);
15488 int offset = (UNITS_PER_WORD
15489 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15491 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15492 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15495 else if (TREE_CODE (decl) == VAR_DECL
15496 && rtl
15497 && MEM_P (rtl)
15498 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15499 && BYTES_BIG_ENDIAN)
15501 enum machine_mode addr_mode = get_address_mode (rtl);
15502 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15503 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15505 /* If a variable is declared "register" yet is smaller than
15506 a register, then if we store the variable to memory, it
15507 looks like we're storing a register-sized value, when in
15508 fact we are not. We need to adjust the offset of the
15509 storage location to reflect the actual value's bytes,
15510 else gdb will not be able to display it. */
15511 if (rsize > dsize)
15512 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15513 plus_constant (addr_mode, XEXP (rtl, 0),
15514 rsize - dsize));
15517 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15518 and will have been substituted directly into all expressions that use it.
15519 C does not have such a concept, but C++ and other languages do. */
15520 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15521 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15523 if (rtl)
15524 rtl = targetm.delegitimize_address (rtl);
15526 /* If we don't look past the constant pool, we risk emitting a
15527 reference to a constant pool entry that isn't referenced from
15528 code, and thus is not emitted. */
15529 if (rtl)
15530 rtl = avoid_constant_pool_reference (rtl);
15532 /* Try harder to get a rtl. If this symbol ends up not being emitted
15533 in the current CU, resolve_addr will remove the expression referencing
15534 it. */
15535 if (rtl == NULL_RTX
15536 && TREE_CODE (decl) == VAR_DECL
15537 && !DECL_EXTERNAL (decl)
15538 && TREE_STATIC (decl)
15539 && DECL_NAME (decl)
15540 && !DECL_HARD_REGISTER (decl)
15541 && DECL_MODE (decl) != VOIDmode)
15543 rtl = make_decl_rtl_for_debug (decl);
15544 if (!MEM_P (rtl)
15545 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15546 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15547 rtl = NULL_RTX;
15550 return rtl;
15553 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15554 returned. If so, the decl for the COMMON block is returned, and the
15555 value is the offset into the common block for the symbol. */
15557 static tree
15558 fortran_common (tree decl, HOST_WIDE_INT *value)
15560 tree val_expr, cvar;
15561 enum machine_mode mode;
15562 HOST_WIDE_INT bitsize, bitpos;
15563 tree offset;
15564 int unsignedp, volatilep = 0;
15566 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15567 it does not have a value (the offset into the common area), or if it
15568 is thread local (as opposed to global) then it isn't common, and shouldn't
15569 be handled as such. */
15570 if (TREE_CODE (decl) != VAR_DECL
15571 || !TREE_STATIC (decl)
15572 || !DECL_HAS_VALUE_EXPR_P (decl)
15573 || !is_fortran ())
15574 return NULL_TREE;
15576 val_expr = DECL_VALUE_EXPR (decl);
15577 if (TREE_CODE (val_expr) != COMPONENT_REF)
15578 return NULL_TREE;
15580 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15581 &mode, &unsignedp, &volatilep, true);
15583 if (cvar == NULL_TREE
15584 || TREE_CODE (cvar) != VAR_DECL
15585 || DECL_ARTIFICIAL (cvar)
15586 || !TREE_PUBLIC (cvar))
15587 return NULL_TREE;
15589 *value = 0;
15590 if (offset != NULL)
15592 if (!tree_fits_shwi_p (offset))
15593 return NULL_TREE;
15594 *value = tree_to_shwi (offset);
15596 if (bitpos != 0)
15597 *value += bitpos / BITS_PER_UNIT;
15599 return cvar;
15602 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15603 data attribute for a variable or a parameter. We generate the
15604 DW_AT_const_value attribute only in those cases where the given variable
15605 or parameter does not have a true "location" either in memory or in a
15606 register. This can happen (for example) when a constant is passed as an
15607 actual argument in a call to an inline function. (It's possible that
15608 these things can crop up in other ways also.) Note that one type of
15609 constant value which can be passed into an inlined function is a constant
15610 pointer. This can happen for example if an actual argument in an inlined
15611 function call evaluates to a compile-time constant address.
15613 CACHE_P is true if it is worth caching the location list for DECL,
15614 so that future calls can reuse it rather than regenerate it from scratch.
15615 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15616 since we will need to refer to them each time the function is inlined. */
15618 static bool
15619 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15620 enum dwarf_attribute attr)
15622 rtx rtl;
15623 dw_loc_list_ref list;
15624 var_loc_list *loc_list;
15625 cached_dw_loc_list *cache;
15626 void **slot;
15628 if (TREE_CODE (decl) == ERROR_MARK)
15629 return false;
15631 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15632 || TREE_CODE (decl) == RESULT_DECL);
15634 /* Try to get some constant RTL for this decl, and use that as the value of
15635 the location. */
15637 rtl = rtl_for_decl_location (decl);
15638 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15639 && add_const_value_attribute (die, rtl))
15640 return true;
15642 /* See if we have single element location list that is equivalent to
15643 a constant value. That way we are better to use add_const_value_attribute
15644 rather than expanding constant value equivalent. */
15645 loc_list = lookup_decl_loc (decl);
15646 if (loc_list
15647 && loc_list->first
15648 && loc_list->first->next == NULL
15649 && NOTE_P (loc_list->first->loc)
15650 && NOTE_VAR_LOCATION (loc_list->first->loc)
15651 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15653 struct var_loc_node *node;
15655 node = loc_list->first;
15656 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15657 if (GET_CODE (rtl) == EXPR_LIST)
15658 rtl = XEXP (rtl, 0);
15659 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15660 && add_const_value_attribute (die, rtl))
15661 return true;
15663 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15664 list several times. See if we've already cached the contents. */
15665 list = NULL;
15666 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15667 cache_p = false;
15668 if (cache_p)
15670 cache = (cached_dw_loc_list *)
15671 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15672 if (cache)
15673 list = cache->loc_list;
15675 if (list == NULL)
15677 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15678 /* It is usually worth caching this result if the decl is from
15679 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15680 if (cache_p && list && list->dw_loc_next)
15682 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15683 DECL_UID (decl), INSERT);
15684 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15685 cache->decl_id = DECL_UID (decl);
15686 cache->loc_list = list;
15687 *slot = cache;
15690 if (list)
15692 add_AT_location_description (die, attr, list);
15693 return true;
15695 /* None of that worked, so it must not really have a location;
15696 try adding a constant value attribute from the DECL_INITIAL. */
15697 return tree_add_const_value_attribute_for_decl (die, decl);
15700 /* Add VARIABLE and DIE into deferred locations list. */
15702 static void
15703 defer_location (tree variable, dw_die_ref die)
15705 deferred_locations entry;
15706 entry.variable = variable;
15707 entry.die = die;
15708 vec_safe_push (deferred_locations_list, entry);
15711 /* Helper function for tree_add_const_value_attribute. Natively encode
15712 initializer INIT into an array. Return true if successful. */
15714 static bool
15715 native_encode_initializer (tree init, unsigned char *array, int size)
15717 tree type;
15719 if (init == NULL_TREE)
15720 return false;
15722 STRIP_NOPS (init);
15723 switch (TREE_CODE (init))
15725 case STRING_CST:
15726 type = TREE_TYPE (init);
15727 if (TREE_CODE (type) == ARRAY_TYPE)
15729 tree enttype = TREE_TYPE (type);
15730 enum machine_mode mode = TYPE_MODE (enttype);
15732 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15733 return false;
15734 if (int_size_in_bytes (type) != size)
15735 return false;
15736 if (size > TREE_STRING_LENGTH (init))
15738 memcpy (array, TREE_STRING_POINTER (init),
15739 TREE_STRING_LENGTH (init));
15740 memset (array + TREE_STRING_LENGTH (init),
15741 '\0', size - TREE_STRING_LENGTH (init));
15743 else
15744 memcpy (array, TREE_STRING_POINTER (init), size);
15745 return true;
15747 return false;
15748 case CONSTRUCTOR:
15749 type = TREE_TYPE (init);
15750 if (int_size_in_bytes (type) != size)
15751 return false;
15752 if (TREE_CODE (type) == ARRAY_TYPE)
15754 HOST_WIDE_INT min_index;
15755 unsigned HOST_WIDE_INT cnt;
15756 int curpos = 0, fieldsize;
15757 constructor_elt *ce;
15759 if (TYPE_DOMAIN (type) == NULL_TREE
15760 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15761 return false;
15763 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15764 if (fieldsize <= 0)
15765 return false;
15767 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15768 memset (array, '\0', size);
15769 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15771 tree val = ce->value;
15772 tree index = ce->index;
15773 int pos = curpos;
15774 if (index && TREE_CODE (index) == RANGE_EXPR)
15775 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15776 * fieldsize;
15777 else if (index)
15778 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15780 if (val)
15782 STRIP_NOPS (val);
15783 if (!native_encode_initializer (val, array + pos, fieldsize))
15784 return false;
15786 curpos = pos + fieldsize;
15787 if (index && TREE_CODE (index) == RANGE_EXPR)
15789 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15790 - tree_to_shwi (TREE_OPERAND (index, 0));
15791 while (count-- > 0)
15793 if (val)
15794 memcpy (array + curpos, array + pos, fieldsize);
15795 curpos += fieldsize;
15798 gcc_assert (curpos <= size);
15800 return true;
15802 else if (TREE_CODE (type) == RECORD_TYPE
15803 || TREE_CODE (type) == UNION_TYPE)
15805 tree field = NULL_TREE;
15806 unsigned HOST_WIDE_INT cnt;
15807 constructor_elt *ce;
15809 if (int_size_in_bytes (type) != size)
15810 return false;
15812 if (TREE_CODE (type) == RECORD_TYPE)
15813 field = TYPE_FIELDS (type);
15815 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15817 tree val = ce->value;
15818 int pos, fieldsize;
15820 if (ce->index != 0)
15821 field = ce->index;
15823 if (val)
15824 STRIP_NOPS (val);
15826 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15827 return false;
15829 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15830 && TYPE_DOMAIN (TREE_TYPE (field))
15831 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15832 return false;
15833 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15834 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15835 return false;
15836 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15837 pos = int_byte_position (field);
15838 gcc_assert (pos + fieldsize <= size);
15839 if (val
15840 && !native_encode_initializer (val, array + pos, fieldsize))
15841 return false;
15843 return true;
15845 return false;
15846 case VIEW_CONVERT_EXPR:
15847 case NON_LVALUE_EXPR:
15848 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15849 default:
15850 return native_encode_expr (init, array, size) == size;
15854 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15855 attribute is the const value T. */
15857 static bool
15858 tree_add_const_value_attribute (dw_die_ref die, tree t)
15860 tree init;
15861 tree type = TREE_TYPE (t);
15862 rtx rtl;
15864 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15865 return false;
15867 init = t;
15868 gcc_assert (!DECL_P (init));
15870 rtl = rtl_for_decl_init (init, type);
15871 if (rtl)
15872 return add_const_value_attribute (die, rtl);
15873 /* If the host and target are sane, try harder. */
15874 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15875 && initializer_constant_valid_p (init, type))
15877 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15878 if (size > 0 && (int) size == size)
15880 unsigned char *array = (unsigned char *)
15881 ggc_alloc_cleared_atomic (size);
15883 if (native_encode_initializer (init, array, size))
15885 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15886 return true;
15888 ggc_free (array);
15891 return false;
15894 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15895 attribute is the const value of T, where T is an integral constant
15896 variable with static storage duration
15897 (so it can't be a PARM_DECL or a RESULT_DECL). */
15899 static bool
15900 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15903 if (!decl
15904 || (TREE_CODE (decl) != VAR_DECL
15905 && TREE_CODE (decl) != CONST_DECL)
15906 || (TREE_CODE (decl) == VAR_DECL
15907 && !TREE_STATIC (decl)))
15908 return false;
15910 if (TREE_READONLY (decl)
15911 && ! TREE_THIS_VOLATILE (decl)
15912 && DECL_INITIAL (decl))
15913 /* OK */;
15914 else
15915 return false;
15917 /* Don't add DW_AT_const_value if abstract origin already has one. */
15918 if (get_AT (var_die, DW_AT_const_value))
15919 return false;
15921 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15924 /* Convert the CFI instructions for the current function into a
15925 location list. This is used for DW_AT_frame_base when we targeting
15926 a dwarf2 consumer that does not support the dwarf3
15927 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15928 expressions. */
15930 static dw_loc_list_ref
15931 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15933 int ix;
15934 dw_fde_ref fde;
15935 dw_loc_list_ref list, *list_tail;
15936 dw_cfi_ref cfi;
15937 dw_cfa_location last_cfa, next_cfa;
15938 const char *start_label, *last_label, *section;
15939 dw_cfa_location remember;
15941 fde = cfun->fde;
15942 gcc_assert (fde != NULL);
15944 section = secname_for_decl (current_function_decl);
15945 list_tail = &list;
15946 list = NULL;
15948 memset (&next_cfa, 0, sizeof (next_cfa));
15949 next_cfa.reg = INVALID_REGNUM;
15950 remember = next_cfa;
15952 start_label = fde->dw_fde_begin;
15954 /* ??? Bald assumption that the CIE opcode list does not contain
15955 advance opcodes. */
15956 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15957 lookup_cfa_1 (cfi, &next_cfa, &remember);
15959 last_cfa = next_cfa;
15960 last_label = start_label;
15962 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15964 /* If the first partition contained no CFI adjustments, the
15965 CIE opcodes apply to the whole first partition. */
15966 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15967 fde->dw_fde_begin, fde->dw_fde_end, section);
15968 list_tail =&(*list_tail)->dw_loc_next;
15969 start_label = last_label = fde->dw_fde_second_begin;
15972 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15974 switch (cfi->dw_cfi_opc)
15976 case DW_CFA_set_loc:
15977 case DW_CFA_advance_loc1:
15978 case DW_CFA_advance_loc2:
15979 case DW_CFA_advance_loc4:
15980 if (!cfa_equal_p (&last_cfa, &next_cfa))
15982 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15983 start_label, last_label, section);
15985 list_tail = &(*list_tail)->dw_loc_next;
15986 last_cfa = next_cfa;
15987 start_label = last_label;
15989 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15990 break;
15992 case DW_CFA_advance_loc:
15993 /* The encoding is complex enough that we should never emit this. */
15994 gcc_unreachable ();
15996 default:
15997 lookup_cfa_1 (cfi, &next_cfa, &remember);
15998 break;
16000 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16002 if (!cfa_equal_p (&last_cfa, &next_cfa))
16004 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16005 start_label, last_label, section);
16007 list_tail = &(*list_tail)->dw_loc_next;
16008 last_cfa = next_cfa;
16009 start_label = last_label;
16011 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16012 start_label, fde->dw_fde_end, section);
16013 list_tail = &(*list_tail)->dw_loc_next;
16014 start_label = last_label = fde->dw_fde_second_begin;
16018 if (!cfa_equal_p (&last_cfa, &next_cfa))
16020 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16021 start_label, last_label, section);
16022 list_tail = &(*list_tail)->dw_loc_next;
16023 start_label = last_label;
16026 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16027 start_label,
16028 fde->dw_fde_second_begin
16029 ? fde->dw_fde_second_end : fde->dw_fde_end,
16030 section);
16032 if (list && list->dw_loc_next)
16033 gen_llsym (list);
16035 return list;
16038 /* Compute a displacement from the "steady-state frame pointer" to the
16039 frame base (often the same as the CFA), and store it in
16040 frame_pointer_fb_offset. OFFSET is added to the displacement
16041 before the latter is negated. */
16043 static void
16044 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16046 rtx reg, elim;
16048 #ifdef FRAME_POINTER_CFA_OFFSET
16049 reg = frame_pointer_rtx;
16050 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16051 #else
16052 reg = arg_pointer_rtx;
16053 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16054 #endif
16056 elim = (ira_use_lra_p
16057 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16058 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16059 if (GET_CODE (elim) == PLUS)
16061 offset += INTVAL (XEXP (elim, 1));
16062 elim = XEXP (elim, 0);
16065 frame_pointer_fb_offset = -offset;
16067 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16068 in which to eliminate. This is because it's stack pointer isn't
16069 directly accessible as a register within the ISA. To work around
16070 this, assume that while we cannot provide a proper value for
16071 frame_pointer_fb_offset, we won't need one either. */
16072 frame_pointer_fb_offset_valid
16073 = ((SUPPORTS_STACK_ALIGNMENT
16074 && (elim == hard_frame_pointer_rtx
16075 || elim == stack_pointer_rtx))
16076 || elim == (frame_pointer_needed
16077 ? hard_frame_pointer_rtx
16078 : stack_pointer_rtx));
16081 /* Generate a DW_AT_name attribute given some string value to be included as
16082 the value of the attribute. */
16084 static void
16085 add_name_attribute (dw_die_ref die, const char *name_string)
16087 if (name_string != NULL && *name_string != 0)
16089 if (demangle_name_func)
16090 name_string = (*demangle_name_func) (name_string);
16092 add_AT_string (die, DW_AT_name, name_string);
16096 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16097 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16098 of TYPE accordingly.
16100 ??? This is a temporary measure until after we're able to generate
16101 regular DWARF for the complex Ada type system. */
16103 static void
16104 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16105 dw_die_ref context_die)
16107 tree dtype;
16108 dw_die_ref dtype_die;
16110 if (!lang_hooks.types.descriptive_type)
16111 return;
16113 dtype = lang_hooks.types.descriptive_type (type);
16114 if (!dtype)
16115 return;
16117 dtype_die = lookup_type_die (dtype);
16118 if (!dtype_die)
16120 gen_type_die (dtype, context_die);
16121 dtype_die = lookup_type_die (dtype);
16122 gcc_assert (dtype_die);
16125 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16128 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16130 static const char *
16131 comp_dir_string (void)
16133 const char *wd;
16134 char *wd1;
16135 static const char *cached_wd = NULL;
16137 if (cached_wd != NULL)
16138 return cached_wd;
16140 wd = get_src_pwd ();
16141 if (wd == NULL)
16142 return NULL;
16144 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16146 int wdlen;
16148 wdlen = strlen (wd);
16149 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16150 strcpy (wd1, wd);
16151 wd1 [wdlen] = DIR_SEPARATOR;
16152 wd1 [wdlen + 1] = 0;
16153 wd = wd1;
16156 cached_wd = remap_debug_filename (wd);
16157 return cached_wd;
16160 /* Generate a DW_AT_comp_dir attribute for DIE. */
16162 static void
16163 add_comp_dir_attribute (dw_die_ref die)
16165 const char * wd = comp_dir_string ();
16166 if (wd != NULL)
16167 add_AT_string (die, DW_AT_comp_dir, wd);
16170 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16171 default. */
16173 static int
16174 lower_bound_default (void)
16176 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16178 case DW_LANG_C:
16179 case DW_LANG_C89:
16180 case DW_LANG_C99:
16181 case DW_LANG_C_plus_plus:
16182 case DW_LANG_ObjC:
16183 case DW_LANG_ObjC_plus_plus:
16184 case DW_LANG_Java:
16185 return 0;
16186 case DW_LANG_Fortran77:
16187 case DW_LANG_Fortran90:
16188 case DW_LANG_Fortran95:
16189 return 1;
16190 case DW_LANG_UPC:
16191 case DW_LANG_D:
16192 case DW_LANG_Python:
16193 return dwarf_version >= 4 ? 0 : -1;
16194 case DW_LANG_Ada95:
16195 case DW_LANG_Ada83:
16196 case DW_LANG_Cobol74:
16197 case DW_LANG_Cobol85:
16198 case DW_LANG_Pascal83:
16199 case DW_LANG_Modula2:
16200 case DW_LANG_PLI:
16201 return dwarf_version >= 4 ? 1 : -1;
16202 default:
16203 return -1;
16207 /* Given a tree node describing an array bound (either lower or upper) output
16208 a representation for that bound. */
16210 static void
16211 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16213 switch (TREE_CODE (bound))
16215 case ERROR_MARK:
16216 return;
16218 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16219 case INTEGER_CST:
16221 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16222 int dflt;
16224 /* Use the default if possible. */
16225 if (bound_attr == DW_AT_lower_bound
16226 && tree_fits_shwi_p (bound)
16227 && (dflt = lower_bound_default ()) != -1
16228 && tree_to_shwi (bound) == dflt)
16231 /* Otherwise represent the bound as an unsigned value with the
16232 precision of its type. The precision and signedness of the
16233 type will be necessary to re-interpret it unambiguously. */
16234 else if (prec < HOST_BITS_PER_WIDE_INT)
16236 unsigned HOST_WIDE_INT mask
16237 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16238 add_AT_unsigned (subrange_die, bound_attr,
16239 TREE_INT_CST_LOW (bound) & mask);
16241 else if (prec == HOST_BITS_PER_WIDE_INT
16242 || TREE_INT_CST_HIGH (bound) == 0)
16243 add_AT_unsigned (subrange_die, bound_attr,
16244 TREE_INT_CST_LOW (bound));
16245 else
16246 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16247 TREE_INT_CST_LOW (bound));
16249 break;
16251 CASE_CONVERT:
16252 case VIEW_CONVERT_EXPR:
16253 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16254 break;
16256 case SAVE_EXPR:
16257 break;
16259 case VAR_DECL:
16260 case PARM_DECL:
16261 case RESULT_DECL:
16263 dw_die_ref decl_die = lookup_decl_die (bound);
16265 /* ??? Can this happen, or should the variable have been bound
16266 first? Probably it can, since I imagine that we try to create
16267 the types of parameters in the order in which they exist in
16268 the list, and won't have created a forward reference to a
16269 later parameter. */
16270 if (decl_die != NULL)
16272 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16273 break;
16276 /* FALLTHRU */
16278 default:
16280 /* Otherwise try to create a stack operation procedure to
16281 evaluate the value of the array bound. */
16283 dw_die_ref ctx, decl_die;
16284 dw_loc_list_ref list;
16286 list = loc_list_from_tree (bound, 2);
16287 if (list == NULL || single_element_loc_list_p (list))
16289 /* If DW_AT_*bound is not a reference nor constant, it is
16290 a DWARF expression rather than location description.
16291 For that loc_list_from_tree (bound, 0) is needed.
16292 If that fails to give a single element list,
16293 fall back to outputting this as a reference anyway. */
16294 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16295 if (list2 && single_element_loc_list_p (list2))
16297 add_AT_loc (subrange_die, bound_attr, list2->expr);
16298 break;
16301 if (list == NULL)
16302 break;
16304 if (current_function_decl == 0)
16305 ctx = comp_unit_die ();
16306 else
16307 ctx = lookup_decl_die (current_function_decl);
16309 decl_die = new_die (DW_TAG_variable, ctx, bound);
16310 add_AT_flag (decl_die, DW_AT_artificial, 1);
16311 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16312 add_AT_location_description (decl_die, DW_AT_location, list);
16313 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16314 break;
16319 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16320 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16321 Note that the block of subscript information for an array type also
16322 includes information about the element type of the given array type. */
16324 static void
16325 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16327 unsigned dimension_number;
16328 tree lower, upper;
16329 dw_die_ref subrange_die;
16331 for (dimension_number = 0;
16332 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16333 type = TREE_TYPE (type), dimension_number++)
16335 tree domain = TYPE_DOMAIN (type);
16337 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16338 break;
16340 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16341 and (in GNU C only) variable bounds. Handle all three forms
16342 here. */
16343 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16345 if (use_upc_dwarf2_extensions && TYPE_HAS_THREADS_FACTOR (type))
16347 add_AT_flag (subrange_die, DW_AT_upc_threads_scaled, 1);
16350 if (domain)
16352 /* We have an array type with specified bounds. */
16353 lower = TYPE_MIN_VALUE (domain);
16354 upper = TYPE_MAX_VALUE (domain);
16356 /* Define the index type. */
16357 if (TREE_TYPE (domain))
16359 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16360 TREE_TYPE field. We can't emit debug info for this
16361 because it is an unnamed integral type. */
16362 if (TREE_CODE (domain) == INTEGER_TYPE
16363 && TYPE_NAME (domain) == NULL_TREE
16364 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16365 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16367 else
16368 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16369 type_die);
16372 /* ??? If upper is NULL, the array has unspecified length,
16373 but it does have a lower bound. This happens with Fortran
16374 dimension arr(N:*)
16375 Since the debugger is definitely going to need to know N
16376 to produce useful results, go ahead and output the lower
16377 bound solo, and hope the debugger can cope. */
16379 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16380 if (upper)
16381 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16384 /* Otherwise we have an array type with an unspecified length. The
16385 DWARF-2 spec does not say how to handle this; let's just leave out the
16386 bounds. */
16390 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16392 static void
16393 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16395 dw_die_ref decl_die;
16396 HOST_WIDE_INT size;
16398 switch (TREE_CODE (tree_node))
16400 case ERROR_MARK:
16401 size = 0;
16402 break;
16403 case ENUMERAL_TYPE:
16404 case RECORD_TYPE:
16405 case UNION_TYPE:
16406 case QUAL_UNION_TYPE:
16407 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16408 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16410 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16411 return;
16413 size = int_size_in_bytes (tree_node);
16414 break;
16415 case FIELD_DECL:
16416 /* For a data member of a struct or union, the DW_AT_byte_size is
16417 generally given as the number of bytes normally allocated for an
16418 object of the *declared* type of the member itself. This is true
16419 even for bit-fields. */
16420 size = int_size_in_bytes (field_type (tree_node));
16421 break;
16422 default:
16423 gcc_unreachable ();
16426 /* Note that `size' might be -1 when we get to this point. If it is, that
16427 indicates that the byte size of the entity in question is variable. We
16428 have no good way of expressing this fact in Dwarf at the present time,
16429 when location description was not used by the caller code instead. */
16430 if (size >= 0)
16431 add_AT_unsigned (die, DW_AT_byte_size, size);
16434 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16435 which specifies the distance in bits from the highest order bit of the
16436 "containing object" for the bit-field to the highest order bit of the
16437 bit-field itself.
16439 For any given bit-field, the "containing object" is a hypothetical object
16440 (of some integral or enum type) within which the given bit-field lives. The
16441 type of this hypothetical "containing object" is always the same as the
16442 declared type of the individual bit-field itself. The determination of the
16443 exact location of the "containing object" for a bit-field is rather
16444 complicated. It's handled by the `field_byte_offset' function (above).
16446 Note that it is the size (in bytes) of the hypothetical "containing object"
16447 which will be given in the DW_AT_byte_size attribute for this bit-field.
16448 (See `byte_size_attribute' above). */
16450 static inline void
16451 add_bit_offset_attribute (dw_die_ref die, tree decl)
16453 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16454 tree type = DECL_BIT_FIELD_TYPE (decl);
16455 HOST_WIDE_INT bitpos_int;
16456 HOST_WIDE_INT highest_order_object_bit_offset;
16457 HOST_WIDE_INT highest_order_field_bit_offset;
16458 HOST_WIDE_INT bit_offset;
16460 /* Must be a field and a bit field. */
16461 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16463 /* We can't yet handle bit-fields whose offsets are variable, so if we
16464 encounter such things, just return without generating any attribute
16465 whatsoever. Likewise for variable or too large size. */
16466 if (! tree_fits_shwi_p (bit_position (decl))
16467 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16468 return;
16470 bitpos_int = int_bit_position (decl);
16472 /* Note that the bit offset is always the distance (in bits) from the
16473 highest-order bit of the "containing object" to the highest-order bit of
16474 the bit-field itself. Since the "high-order end" of any object or field
16475 is different on big-endian and little-endian machines, the computation
16476 below must take account of these differences. */
16477 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16478 highest_order_field_bit_offset = bitpos_int;
16480 if (! BYTES_BIG_ENDIAN)
16482 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16483 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16486 bit_offset
16487 = (! BYTES_BIG_ENDIAN
16488 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16489 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16491 if (bit_offset < 0)
16492 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16493 else
16494 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16497 /* For a FIELD_DECL node which represents a bit field, output an attribute
16498 which specifies the length in bits of the given field. */
16500 static inline void
16501 add_bit_size_attribute (dw_die_ref die, tree decl)
16503 /* Must be a field and a bit field. */
16504 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16505 && DECL_BIT_FIELD_TYPE (decl));
16507 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16508 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16511 /* If the compiled language is ANSI C, then add a 'prototyped'
16512 attribute, if arg types are given for the parameters of a function. */
16514 static inline void
16515 add_prototyped_attribute (dw_die_ref die, tree func_type)
16517 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16518 && prototype_p (func_type))
16519 add_AT_flag (die, DW_AT_prototyped, 1);
16522 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16523 by looking in either the type declaration or object declaration
16524 equate table. */
16526 static inline dw_die_ref
16527 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16529 dw_die_ref origin_die = NULL;
16531 if (TREE_CODE (origin) != FUNCTION_DECL)
16533 /* We may have gotten separated from the block for the inlined
16534 function, if we're in an exception handler or some such; make
16535 sure that the abstract function has been written out.
16537 Doing this for nested functions is wrong, however; functions are
16538 distinct units, and our context might not even be inline. */
16539 tree fn = origin;
16541 if (TYPE_P (fn))
16542 fn = TYPE_STUB_DECL (fn);
16544 fn = decl_function_context (fn);
16545 if (fn)
16546 dwarf2out_abstract_function (fn);
16549 if (DECL_P (origin))
16550 origin_die = lookup_decl_die (origin);
16551 else if (TYPE_P (origin))
16552 origin_die = lookup_type_die (origin);
16554 /* XXX: Functions that are never lowered don't always have correct block
16555 trees (in the case of java, they simply have no block tree, in some other
16556 languages). For these functions, there is nothing we can really do to
16557 output correct debug info for inlined functions in all cases. Rather
16558 than die, we'll just produce deficient debug info now, in that we will
16559 have variables without a proper abstract origin. In the future, when all
16560 functions are lowered, we should re-add a gcc_assert (origin_die)
16561 here. */
16563 if (origin_die)
16564 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16565 return origin_die;
16568 /* We do not currently support the pure_virtual attribute. */
16570 static inline void
16571 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16573 if (DECL_VINDEX (func_decl))
16575 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16577 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16578 add_AT_loc (die, DW_AT_vtable_elem_location,
16579 new_loc_descr (DW_OP_constu,
16580 tree_to_shwi (DECL_VINDEX (func_decl)),
16581 0));
16583 /* GNU extension: Record what type this method came from originally. */
16584 if (debug_info_level > DINFO_LEVEL_TERSE
16585 && DECL_CONTEXT (func_decl))
16586 add_AT_die_ref (die, DW_AT_containing_type,
16587 lookup_type_die (DECL_CONTEXT (func_decl)));
16591 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16592 given decl. This used to be a vendor extension until after DWARF 4
16593 standardized it. */
16595 static void
16596 add_linkage_attr (dw_die_ref die, tree decl)
16598 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16600 /* Mimic what assemble_name_raw does with a leading '*'. */
16601 if (name[0] == '*')
16602 name = &name[1];
16604 if (dwarf_version >= 4)
16605 add_AT_string (die, DW_AT_linkage_name, name);
16606 else
16607 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16610 /* Add source coordinate attributes for the given decl. */
16612 static void
16613 add_src_coords_attributes (dw_die_ref die, tree decl)
16615 expanded_location s;
16617 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16618 return;
16619 s = expand_location (DECL_SOURCE_LOCATION (decl));
16620 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16621 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16624 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16626 static void
16627 add_linkage_name (dw_die_ref die, tree decl)
16629 if (debug_info_level > DINFO_LEVEL_TERSE
16630 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16631 && TREE_PUBLIC (decl)
16632 && !DECL_ABSTRACT (decl)
16633 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16634 && die->die_tag != DW_TAG_member)
16636 /* Defer until we have an assembler name set. */
16637 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16639 limbo_die_node *asm_name;
16641 asm_name = ggc_alloc_cleared_limbo_die_node ();
16642 asm_name->die = die;
16643 asm_name->created_for = decl;
16644 asm_name->next = deferred_asm_name;
16645 deferred_asm_name = asm_name;
16647 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16648 add_linkage_attr (die, decl);
16652 /* Add a DW_AT_name attribute and source coordinate attribute for the
16653 given decl, but only if it actually has a name. */
16655 static void
16656 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16658 tree decl_name;
16660 decl_name = DECL_NAME (decl);
16661 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16663 const char *name = dwarf2_name (decl, 0);
16664 if (name)
16665 add_name_attribute (die, name);
16666 if (! DECL_ARTIFICIAL (decl))
16667 add_src_coords_attributes (die, decl);
16669 add_linkage_name (die, decl);
16672 #ifdef VMS_DEBUGGING_INFO
16673 /* Get the function's name, as described by its RTL. This may be different
16674 from the DECL_NAME name used in the source file. */
16675 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16677 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16678 XEXP (DECL_RTL (decl), 0), false);
16679 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16681 #endif /* VMS_DEBUGGING_INFO */
16684 #ifdef VMS_DEBUGGING_INFO
16685 /* Output the debug main pointer die for VMS */
16687 void
16688 dwarf2out_vms_debug_main_pointer (void)
16690 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16691 dw_die_ref die;
16693 /* Allocate the VMS debug main subprogram die. */
16694 die = ggc_alloc_cleared_die_node ();
16695 die->die_tag = DW_TAG_subprogram;
16696 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16697 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16698 current_function_funcdef_no);
16699 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16701 /* Make it the first child of comp_unit_die (). */
16702 die->die_parent = comp_unit_die ();
16703 if (comp_unit_die ()->die_child)
16705 die->die_sib = comp_unit_die ()->die_child->die_sib;
16706 comp_unit_die ()->die_child->die_sib = die;
16708 else
16710 die->die_sib = die;
16711 comp_unit_die ()->die_child = die;
16714 #endif /* VMS_DEBUGGING_INFO */
16716 /* Push a new declaration scope. */
16718 static void
16719 push_decl_scope (tree scope)
16721 vec_safe_push (decl_scope_table, scope);
16724 /* Pop a declaration scope. */
16726 static inline void
16727 pop_decl_scope (void)
16729 decl_scope_table->pop ();
16732 /* walk_tree helper function for uses_local_type, below. */
16734 static tree
16735 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16737 if (!TYPE_P (*tp))
16738 *walk_subtrees = 0;
16739 else
16741 tree name = TYPE_NAME (*tp);
16742 if (name && DECL_P (name) && decl_function_context (name))
16743 return *tp;
16745 return NULL_TREE;
16748 /* If TYPE involves a function-local type (including a local typedef to a
16749 non-local type), returns that type; otherwise returns NULL_TREE. */
16751 static tree
16752 uses_local_type (tree type)
16754 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16755 return used;
16758 /* Return the DIE for the scope that immediately contains this type.
16759 Non-named types that do not involve a function-local type get global
16760 scope. Named types nested in namespaces or other types get their
16761 containing scope. All other types (i.e. function-local named types) get
16762 the current active scope. */
16764 static dw_die_ref
16765 scope_die_for (tree t, dw_die_ref context_die)
16767 dw_die_ref scope_die = NULL;
16768 tree containing_scope;
16770 /* Non-types always go in the current scope. */
16771 gcc_assert (TYPE_P (t));
16773 /* Use the scope of the typedef, rather than the scope of the type
16774 it refers to. */
16775 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16776 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16777 else
16778 containing_scope = TYPE_CONTEXT (t);
16780 /* Use the containing namespace if there is one. */
16781 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16783 if (context_die == lookup_decl_die (containing_scope))
16784 /* OK */;
16785 else if (debug_info_level > DINFO_LEVEL_TERSE)
16786 context_die = get_context_die (containing_scope);
16787 else
16788 containing_scope = NULL_TREE;
16791 /* Ignore function type "scopes" from the C frontend. They mean that
16792 a tagged type is local to a parmlist of a function declarator, but
16793 that isn't useful to DWARF. */
16794 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16795 containing_scope = NULL_TREE;
16797 if (SCOPE_FILE_SCOPE_P (containing_scope))
16799 /* If T uses a local type keep it local as well, to avoid references
16800 to function-local DIEs from outside the function. */
16801 if (current_function_decl && uses_local_type (t))
16802 scope_die = context_die;
16803 else
16804 scope_die = comp_unit_die ();
16806 else if (TYPE_P (containing_scope))
16808 /* For types, we can just look up the appropriate DIE. */
16809 if (debug_info_level > DINFO_LEVEL_TERSE)
16810 scope_die = get_context_die (containing_scope);
16811 else
16813 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16814 if (scope_die == NULL)
16815 scope_die = comp_unit_die ();
16818 else
16819 scope_die = context_die;
16821 return scope_die;
16824 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16826 static inline int
16827 local_scope_p (dw_die_ref context_die)
16829 for (; context_die; context_die = context_die->die_parent)
16830 if (context_die->die_tag == DW_TAG_inlined_subroutine
16831 || context_die->die_tag == DW_TAG_subprogram)
16832 return 1;
16834 return 0;
16837 /* Returns nonzero if CONTEXT_DIE is a class. */
16839 static inline int
16840 class_scope_p (dw_die_ref context_die)
16842 return (context_die
16843 && (context_die->die_tag == DW_TAG_structure_type
16844 || context_die->die_tag == DW_TAG_class_type
16845 || context_die->die_tag == DW_TAG_interface_type
16846 || context_die->die_tag == DW_TAG_union_type));
16849 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16850 whether or not to treat a DIE in this context as a declaration. */
16852 static inline int
16853 class_or_namespace_scope_p (dw_die_ref context_die)
16855 return (class_scope_p (context_die)
16856 || (context_die && context_die->die_tag == DW_TAG_namespace));
16859 /* Many forms of DIEs require a "type description" attribute. This
16860 routine locates the proper "type descriptor" die for the type given
16861 by 'type', and adds a DW_AT_type attribute below the given die. */
16863 static void
16864 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16865 int decl_volatile, dw_die_ref context_die)
16867 enum tree_code code = TREE_CODE (type);
16868 dw_die_ref type_die = NULL;
16869 int type_quals;
16871 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16872 or fixed-point type, use the inner type. This is because we have no
16873 support for unnamed types in base_type_die. This can happen if this is
16874 an Ada subrange type. Correct solution is emit a subrange type die. */
16875 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16876 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16877 type = TREE_TYPE (type), code = TREE_CODE (type);
16879 if (code == ERROR_MARK
16880 /* Handle a special case. For functions whose return type is void, we
16881 generate *no* type attribute. (Note that no object may have type
16882 `void', so this only applies to function return types). */
16883 || code == VOID_TYPE)
16884 return;
16886 type_quals = TYPE_QUALS (type)
16887 | (decl_const * TYPE_QUAL_CONST)
16888 | (decl_volatile * TYPE_QUAL_VOLATILE);
16889 type_die = modified_type_die (type, type_quals, context_die);
16891 if (type_die != NULL)
16892 add_AT_die_ref (object_die, DW_AT_type, type_die);
16895 /* Given an object die, add the calling convention attribute for the
16896 function call type. */
16897 static void
16898 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16900 enum dwarf_calling_convention value = DW_CC_normal;
16902 value = ((enum dwarf_calling_convention)
16903 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16905 if (is_fortran ()
16906 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16908 /* DWARF 2 doesn't provide a way to identify a program's source-level
16909 entry point. DW_AT_calling_convention attributes are only meant
16910 to describe functions' calling conventions. However, lacking a
16911 better way to signal the Fortran main program, we used this for
16912 a long time, following existing custom. Now, DWARF 4 has
16913 DW_AT_main_subprogram, which we add below, but some tools still
16914 rely on the old way, which we thus keep. */
16915 value = DW_CC_program;
16917 if (dwarf_version >= 4 || !dwarf_strict)
16918 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16921 /* Only add the attribute if the backend requests it, and
16922 is not DW_CC_normal. */
16923 if (value && (value != DW_CC_normal))
16924 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16927 /* Given a tree pointer to a struct, class, union, or enum type node, return
16928 a pointer to the (string) tag name for the given type, or zero if the type
16929 was declared without a tag. */
16931 static const char *
16932 type_tag (const_tree type)
16934 const char *name = 0;
16936 if (TYPE_NAME (type) != 0)
16938 tree t = 0;
16940 /* Find the IDENTIFIER_NODE for the type name. */
16941 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16942 && !TYPE_NAMELESS (type))
16943 t = TYPE_NAME (type);
16945 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16946 a TYPE_DECL node, regardless of whether or not a `typedef' was
16947 involved. */
16948 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16949 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16951 /* We want to be extra verbose. Don't call dwarf_name if
16952 DECL_NAME isn't set. The default hook for decl_printable_name
16953 doesn't like that, and in this context it's correct to return
16954 0, instead of "<anonymous>" or the like. */
16955 if (DECL_NAME (TYPE_NAME (type))
16956 && !DECL_NAMELESS (TYPE_NAME (type)))
16957 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16960 /* Now get the name as a string, or invent one. */
16961 if (!name && t != 0)
16962 name = IDENTIFIER_POINTER (t);
16965 return (name == 0 || *name == '\0') ? 0 : name;
16968 /* Return the type associated with a data member, make a special check
16969 for bit field types. */
16971 static inline tree
16972 member_declared_type (const_tree member)
16974 return (DECL_BIT_FIELD_TYPE (member)
16975 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16978 /* Get the decl's label, as described by its RTL. This may be different
16979 from the DECL_NAME name used in the source file. */
16981 #if 0
16982 static const char *
16983 decl_start_label (tree decl)
16985 rtx x;
16986 const char *fnname;
16988 x = DECL_RTL (decl);
16989 gcc_assert (MEM_P (x));
16991 x = XEXP (x, 0);
16992 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16994 fnname = XSTR (x, 0);
16995 return fnname;
16997 #endif
16999 /* These routines generate the internal representation of the DIE's for
17000 the compilation unit. Debugging information is collected by walking
17001 the declaration trees passed in from dwarf2out_decl(). */
17003 static void
17004 gen_array_type_die (tree type, dw_die_ref context_die)
17006 dw_die_ref scope_die = scope_die_for (type, context_die);
17007 dw_die_ref array_die;
17009 /* GNU compilers represent multidimensional array types as sequences of one
17010 dimensional array types whose element types are themselves array types.
17011 We sometimes squish that down to a single array_type DIE with multiple
17012 subscripts in the Dwarf debugging info. The draft Dwarf specification
17013 say that we are allowed to do this kind of compression in C, because
17014 there is no difference between an array of arrays and a multidimensional
17015 array. We don't do this for Ada to remain as close as possible to the
17016 actual representation, which is especially important against the language
17017 flexibilty wrt arrays of variable size. */
17019 bool collapse_nested_arrays = !is_ada ();
17020 tree element_type;
17022 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17023 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17024 if (TYPE_STRING_FLAG (type)
17025 && TREE_CODE (type) == ARRAY_TYPE
17026 && is_fortran ()
17027 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17029 HOST_WIDE_INT size;
17031 array_die = new_die (DW_TAG_string_type, scope_die, type);
17032 add_name_attribute (array_die, type_tag (type));
17033 equate_type_number_to_die (type, array_die);
17034 size = int_size_in_bytes (type);
17035 if (size >= 0)
17036 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17037 else if (TYPE_DOMAIN (type) != NULL_TREE
17038 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17039 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17041 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17042 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17044 size = int_size_in_bytes (TREE_TYPE (szdecl));
17045 if (loc && size > 0)
17047 add_AT_location_description (array_die, DW_AT_string_length, loc);
17048 if (size != DWARF2_ADDR_SIZE)
17049 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17052 return;
17055 array_die = new_die (DW_TAG_array_type, scope_die, type);
17056 add_name_attribute (array_die, type_tag (type));
17057 equate_type_number_to_die (type, array_die);
17059 if (TREE_CODE (type) == VECTOR_TYPE)
17060 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17062 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17063 if (is_fortran ()
17064 && TREE_CODE (type) == ARRAY_TYPE
17065 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17066 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17067 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17069 #if 0
17070 /* We default the array ordering. SDB will probably do
17071 the right things even if DW_AT_ordering is not present. It's not even
17072 an issue until we start to get into multidimensional arrays anyway. If
17073 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17074 then we'll have to put the DW_AT_ordering attribute back in. (But if
17075 and when we find out that we need to put these in, we will only do so
17076 for multidimensional arrays. */
17077 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17078 #endif
17080 if (TREE_CODE (type) == VECTOR_TYPE)
17082 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17083 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17084 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17085 add_bound_info (subrange_die, DW_AT_upper_bound,
17086 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17088 else
17089 add_subscript_info (array_die, type, collapse_nested_arrays);
17091 /* Add representation of the type of the elements of this array type and
17092 emit the corresponding DIE if we haven't done it already. */
17093 element_type = TREE_TYPE (type);
17094 if (collapse_nested_arrays)
17095 while (TREE_CODE (element_type) == ARRAY_TYPE)
17097 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17098 break;
17099 element_type = TREE_TYPE (element_type);
17102 add_type_attribute (array_die, element_type, 0, 0, context_die);
17104 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17105 if (TYPE_ARTIFICIAL (type))
17106 add_AT_flag (array_die, DW_AT_artificial, 1);
17108 if (get_AT (array_die, DW_AT_name))
17109 add_pubtype (type, array_die);
17112 static dw_loc_descr_ref
17113 descr_info_loc (tree val, tree base_decl)
17115 HOST_WIDE_INT size;
17116 dw_loc_descr_ref loc, loc2;
17117 enum dwarf_location_atom op;
17119 if (val == base_decl)
17120 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17122 switch (TREE_CODE (val))
17124 CASE_CONVERT:
17125 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17126 case VAR_DECL:
17127 return loc_descriptor_from_tree (val, 0);
17128 case INTEGER_CST:
17129 if (tree_fits_shwi_p (val))
17130 return int_loc_descriptor (tree_to_shwi (val));
17131 break;
17132 case INDIRECT_REF:
17133 size = int_size_in_bytes (TREE_TYPE (val));
17134 if (size < 0)
17135 break;
17136 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17137 if (!loc)
17138 break;
17139 if (size == DWARF2_ADDR_SIZE)
17140 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17141 else
17142 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17143 return loc;
17144 case POINTER_PLUS_EXPR:
17145 case PLUS_EXPR:
17146 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17147 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17149 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17150 if (!loc)
17151 break;
17152 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17154 else
17156 op = DW_OP_plus;
17157 do_binop:
17158 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17159 if (!loc)
17160 break;
17161 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17162 if (!loc2)
17163 break;
17164 add_loc_descr (&loc, loc2);
17165 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17167 return loc;
17168 case MINUS_EXPR:
17169 op = DW_OP_minus;
17170 goto do_binop;
17171 case MULT_EXPR:
17172 op = DW_OP_mul;
17173 goto do_binop;
17174 case EQ_EXPR:
17175 op = DW_OP_eq;
17176 goto do_binop;
17177 case NE_EXPR:
17178 op = DW_OP_ne;
17179 goto do_binop;
17180 default:
17181 break;
17183 return NULL;
17186 static void
17187 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17188 tree val, tree base_decl)
17190 dw_loc_descr_ref loc;
17192 if (tree_fits_shwi_p (val))
17194 add_AT_unsigned (die, attr, tree_to_shwi (val));
17195 return;
17198 loc = descr_info_loc (val, base_decl);
17199 if (!loc)
17200 return;
17202 add_AT_loc (die, attr, loc);
17205 /* This routine generates DIE for array with hidden descriptor, details
17206 are filled into *info by a langhook. */
17208 static void
17209 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17210 dw_die_ref context_die)
17212 dw_die_ref scope_die = scope_die_for (type, context_die);
17213 dw_die_ref array_die;
17214 int dim;
17216 array_die = new_die (DW_TAG_array_type, scope_die, type);
17217 add_name_attribute (array_die, type_tag (type));
17218 equate_type_number_to_die (type, array_die);
17220 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17221 if (is_fortran ()
17222 && info->ndimensions >= 2)
17223 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17225 if (info->data_location)
17226 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17227 info->base_decl);
17228 if (info->associated)
17229 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17230 info->base_decl);
17231 if (info->allocated)
17232 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17233 info->base_decl);
17235 for (dim = 0; dim < info->ndimensions; dim++)
17237 dw_die_ref subrange_die
17238 = new_die (DW_TAG_subrange_type, array_die, NULL);
17240 if (info->dimen[dim].lower_bound)
17242 /* If it is the default value, omit it. */
17243 int dflt;
17245 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17246 && (dflt = lower_bound_default ()) != -1
17247 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17249 else
17250 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17251 info->dimen[dim].lower_bound,
17252 info->base_decl);
17254 if (info->dimen[dim].upper_bound)
17255 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17256 info->dimen[dim].upper_bound,
17257 info->base_decl);
17258 if (info->dimen[dim].stride)
17259 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17260 info->dimen[dim].stride,
17261 info->base_decl);
17264 gen_type_die (info->element_type, context_die);
17265 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17267 if (get_AT (array_die, DW_AT_name))
17268 add_pubtype (type, array_die);
17271 #if 0
17272 static void
17273 gen_entry_point_die (tree decl, dw_die_ref context_die)
17275 tree origin = decl_ultimate_origin (decl);
17276 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17278 if (origin != NULL)
17279 add_abstract_origin_attribute (decl_die, origin);
17280 else
17282 add_name_and_src_coords_attributes (decl_die, decl);
17283 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17284 0, 0, context_die);
17287 if (DECL_ABSTRACT (decl))
17288 equate_decl_number_to_die (decl, decl_die);
17289 else
17290 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17292 #endif
17294 /* Walk through the list of incomplete types again, trying once more to
17295 emit full debugging info for them. */
17297 static void
17298 retry_incomplete_types (void)
17300 int i;
17302 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17303 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17304 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17307 /* Determine what tag to use for a record type. */
17309 static enum dwarf_tag
17310 record_type_tag (tree type)
17312 if (! lang_hooks.types.classify_record)
17313 return DW_TAG_structure_type;
17315 switch (lang_hooks.types.classify_record (type))
17317 case RECORD_IS_STRUCT:
17318 return DW_TAG_structure_type;
17320 case RECORD_IS_CLASS:
17321 return DW_TAG_class_type;
17323 case RECORD_IS_INTERFACE:
17324 if (dwarf_version >= 3 || !dwarf_strict)
17325 return DW_TAG_interface_type;
17326 return DW_TAG_structure_type;
17328 default:
17329 gcc_unreachable ();
17333 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17334 include all of the information about the enumeration values also. Each
17335 enumerated type name/value is listed as a child of the enumerated type
17336 DIE. */
17338 static dw_die_ref
17339 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17341 dw_die_ref type_die = lookup_type_die (type);
17343 if (type_die == NULL)
17345 type_die = new_die (DW_TAG_enumeration_type,
17346 scope_die_for (type, context_die), type);
17347 equate_type_number_to_die (type, type_die);
17348 add_name_attribute (type_die, type_tag (type));
17349 if (dwarf_version >= 4 || !dwarf_strict)
17351 if (ENUM_IS_SCOPED (type))
17352 add_AT_flag (type_die, DW_AT_enum_class, 1);
17353 if (ENUM_IS_OPAQUE (type))
17354 add_AT_flag (type_die, DW_AT_declaration, 1);
17357 else if (! TYPE_SIZE (type))
17358 return type_die;
17359 else
17360 remove_AT (type_die, DW_AT_declaration);
17362 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17363 given enum type is incomplete, do not generate the DW_AT_byte_size
17364 attribute or the DW_AT_element_list attribute. */
17365 if (TYPE_SIZE (type))
17367 tree link;
17369 TREE_ASM_WRITTEN (type) = 1;
17370 add_byte_size_attribute (type_die, type);
17371 if (TYPE_STUB_DECL (type) != NULL_TREE)
17373 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17374 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17377 /* If the first reference to this type was as the return type of an
17378 inline function, then it may not have a parent. Fix this now. */
17379 if (type_die->die_parent == NULL)
17380 add_child_die (scope_die_for (type, context_die), type_die);
17382 for (link = TYPE_VALUES (type);
17383 link != NULL; link = TREE_CHAIN (link))
17385 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17386 tree value = TREE_VALUE (link);
17388 add_name_attribute (enum_die,
17389 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17391 if (TREE_CODE (value) == CONST_DECL)
17392 value = DECL_INITIAL (value);
17394 if (simple_type_size_in_bits (TREE_TYPE (value))
17395 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17396 /* DWARF2 does not provide a way of indicating whether or
17397 not enumeration constants are signed or unsigned. GDB
17398 always assumes the values are signed, so we output all
17399 values as if they were signed. That means that
17400 enumeration constants with very large unsigned values
17401 will appear to have negative values in the debugger.
17403 TODO: the above comment is wrong, DWARF2 does provide
17404 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17405 This should be re-worked to use correct signed/unsigned
17406 int/double tags for all cases, instead of always treating as
17407 signed. */
17408 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17409 else
17410 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17411 that here. */
17412 add_AT_double (enum_die, DW_AT_const_value,
17413 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17416 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17417 if (TYPE_ARTIFICIAL (type))
17418 add_AT_flag (type_die, DW_AT_artificial, 1);
17420 else
17421 add_AT_flag (type_die, DW_AT_declaration, 1);
17423 add_pubtype (type, type_die);
17425 return type_die;
17428 /* Generate a DIE to represent either a real live formal parameter decl or to
17429 represent just the type of some formal parameter position in some function
17430 type.
17432 Note that this routine is a bit unusual because its argument may be a
17433 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17434 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17435 node. If it's the former then this function is being called to output a
17436 DIE to represent a formal parameter object (or some inlining thereof). If
17437 it's the latter, then this function is only being called to output a
17438 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17439 argument type of some subprogram type.
17440 If EMIT_NAME_P is true, name and source coordinate attributes
17441 are emitted. */
17443 static dw_die_ref
17444 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17445 dw_die_ref context_die)
17447 tree node_or_origin = node ? node : origin;
17448 tree ultimate_origin;
17449 dw_die_ref parm_die
17450 = new_die (DW_TAG_formal_parameter, context_die, node);
17452 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17454 case tcc_declaration:
17455 ultimate_origin = decl_ultimate_origin (node_or_origin);
17456 if (node || ultimate_origin)
17457 origin = ultimate_origin;
17458 if (origin != NULL)
17459 add_abstract_origin_attribute (parm_die, origin);
17460 else if (emit_name_p)
17461 add_name_and_src_coords_attributes (parm_die, node);
17462 if (origin == NULL
17463 || (! DECL_ABSTRACT (node_or_origin)
17464 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17465 decl_function_context
17466 (node_or_origin))))
17468 tree type = TREE_TYPE (node_or_origin);
17469 if (decl_by_reference_p (node_or_origin))
17470 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17471 context_die);
17472 else
17473 add_type_attribute (parm_die, type,
17474 TREE_READONLY (node_or_origin),
17475 TREE_THIS_VOLATILE (node_or_origin),
17476 context_die);
17478 if (origin == NULL && DECL_ARTIFICIAL (node))
17479 add_AT_flag (parm_die, DW_AT_artificial, 1);
17481 if (node && node != origin)
17482 equate_decl_number_to_die (node, parm_die);
17483 if (! DECL_ABSTRACT (node_or_origin))
17484 add_location_or_const_value_attribute (parm_die, node_or_origin,
17485 node == NULL, DW_AT_location);
17487 break;
17489 case tcc_type:
17490 /* We were called with some kind of a ..._TYPE node. */
17491 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17492 break;
17494 default:
17495 gcc_unreachable ();
17498 return parm_die;
17501 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17502 children DW_TAG_formal_parameter DIEs representing the arguments of the
17503 parameter pack.
17505 PARM_PACK must be a function parameter pack.
17506 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17507 must point to the subsequent arguments of the function PACK_ARG belongs to.
17508 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17509 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17510 following the last one for which a DIE was generated. */
17512 static dw_die_ref
17513 gen_formal_parameter_pack_die (tree parm_pack,
17514 tree pack_arg,
17515 dw_die_ref subr_die,
17516 tree *next_arg)
17518 tree arg;
17519 dw_die_ref parm_pack_die;
17521 gcc_assert (parm_pack
17522 && lang_hooks.function_parameter_pack_p (parm_pack)
17523 && subr_die);
17525 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17526 add_src_coords_attributes (parm_pack_die, parm_pack);
17528 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17530 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17531 parm_pack))
17532 break;
17533 gen_formal_parameter_die (arg, NULL,
17534 false /* Don't emit name attribute. */,
17535 parm_pack_die);
17537 if (next_arg)
17538 *next_arg = arg;
17539 return parm_pack_die;
17542 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17543 at the end of an (ANSI prototyped) formal parameters list. */
17545 static void
17546 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17548 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17551 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17552 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17553 parameters as specified in some function type specification (except for
17554 those which appear as part of a function *definition*). */
17556 static void
17557 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17559 tree link;
17560 tree formal_type = NULL;
17561 tree first_parm_type;
17562 tree arg;
17564 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17566 arg = DECL_ARGUMENTS (function_or_method_type);
17567 function_or_method_type = TREE_TYPE (function_or_method_type);
17569 else
17570 arg = NULL_TREE;
17572 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17574 /* Make our first pass over the list of formal parameter types and output a
17575 DW_TAG_formal_parameter DIE for each one. */
17576 for (link = first_parm_type; link; )
17578 dw_die_ref parm_die;
17580 formal_type = TREE_VALUE (link);
17581 if (formal_type == void_type_node)
17582 break;
17584 /* Output a (nameless) DIE to represent the formal parameter itself. */
17585 parm_die = gen_formal_parameter_die (formal_type, NULL,
17586 true /* Emit name attribute. */,
17587 context_die);
17588 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17589 && link == first_parm_type)
17591 add_AT_flag (parm_die, DW_AT_artificial, 1);
17592 if (dwarf_version >= 3 || !dwarf_strict)
17593 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17595 else if (arg && DECL_ARTIFICIAL (arg))
17596 add_AT_flag (parm_die, DW_AT_artificial, 1);
17598 link = TREE_CHAIN (link);
17599 if (arg)
17600 arg = DECL_CHAIN (arg);
17603 /* If this function type has an ellipsis, add a
17604 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17605 if (formal_type != void_type_node)
17606 gen_unspecified_parameters_die (function_or_method_type, context_die);
17608 /* Make our second (and final) pass over the list of formal parameter types
17609 and output DIEs to represent those types (as necessary). */
17610 for (link = TYPE_ARG_TYPES (function_or_method_type);
17611 link && TREE_VALUE (link);
17612 link = TREE_CHAIN (link))
17613 gen_type_die (TREE_VALUE (link), context_die);
17616 /* We want to generate the DIE for TYPE so that we can generate the
17617 die for MEMBER, which has been defined; we will need to refer back
17618 to the member declaration nested within TYPE. If we're trying to
17619 generate minimal debug info for TYPE, processing TYPE won't do the
17620 trick; we need to attach the member declaration by hand. */
17622 static void
17623 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17625 gen_type_die (type, context_die);
17627 /* If we're trying to avoid duplicate debug info, we may not have
17628 emitted the member decl for this function. Emit it now. */
17629 if (TYPE_STUB_DECL (type)
17630 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17631 && ! lookup_decl_die (member))
17633 dw_die_ref type_die;
17634 gcc_assert (!decl_ultimate_origin (member));
17636 push_decl_scope (type);
17637 type_die = lookup_type_die_strip_naming_typedef (type);
17638 if (TREE_CODE (member) == FUNCTION_DECL)
17639 gen_subprogram_die (member, type_die);
17640 else if (TREE_CODE (member) == FIELD_DECL)
17642 /* Ignore the nameless fields that are used to skip bits but handle
17643 C++ anonymous unions and structs. */
17644 if (DECL_NAME (member) != NULL_TREE
17645 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17646 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17648 gen_type_die (member_declared_type (member), type_die);
17649 gen_field_die (member, type_die);
17652 else
17653 gen_variable_die (member, NULL_TREE, type_die);
17655 pop_decl_scope ();
17659 /* Forward declare these functions, because they are mutually recursive
17660 with their set_block_* pairing functions. */
17661 static void set_decl_origin_self (tree);
17662 static void set_decl_abstract_flags (tree, int);
17664 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17665 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17666 that it points to the node itself, thus indicating that the node is its
17667 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17668 the given node is NULL, recursively descend the decl/block tree which
17669 it is the root of, and for each other ..._DECL or BLOCK node contained
17670 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17671 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17672 values to point to themselves. */
17674 static void
17675 set_block_origin_self (tree stmt)
17677 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17679 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17682 tree local_decl;
17684 for (local_decl = BLOCK_VARS (stmt);
17685 local_decl != NULL_TREE;
17686 local_decl = DECL_CHAIN (local_decl))
17687 if (! DECL_EXTERNAL (local_decl))
17688 set_decl_origin_self (local_decl); /* Potential recursion. */
17692 tree subblock;
17694 for (subblock = BLOCK_SUBBLOCKS (stmt);
17695 subblock != NULL_TREE;
17696 subblock = BLOCK_CHAIN (subblock))
17697 set_block_origin_self (subblock); /* Recurse. */
17702 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17703 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17704 node to so that it points to the node itself, thus indicating that the
17705 node represents its own (abstract) origin. Additionally, if the
17706 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17707 the decl/block tree of which the given node is the root of, and for
17708 each other ..._DECL or BLOCK node contained therein whose
17709 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17710 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17711 point to themselves. */
17713 static void
17714 set_decl_origin_self (tree decl)
17716 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17718 DECL_ABSTRACT_ORIGIN (decl) = decl;
17719 if (TREE_CODE (decl) == FUNCTION_DECL)
17721 tree arg;
17723 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17724 DECL_ABSTRACT_ORIGIN (arg) = arg;
17725 if (DECL_INITIAL (decl) != NULL_TREE
17726 && DECL_INITIAL (decl) != error_mark_node)
17727 set_block_origin_self (DECL_INITIAL (decl));
17732 /* Given a pointer to some BLOCK node, and a boolean value to set the
17733 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17734 the given block, and for all local decls and all local sub-blocks
17735 (recursively) which are contained therein. */
17737 static void
17738 set_block_abstract_flags (tree stmt, int setting)
17740 tree local_decl;
17741 tree subblock;
17742 unsigned int i;
17744 BLOCK_ABSTRACT (stmt) = setting;
17746 for (local_decl = BLOCK_VARS (stmt);
17747 local_decl != NULL_TREE;
17748 local_decl = DECL_CHAIN (local_decl))
17749 if (! DECL_EXTERNAL (local_decl))
17750 set_decl_abstract_flags (local_decl, setting);
17752 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17754 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17755 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17756 || TREE_CODE (local_decl) == PARM_DECL)
17757 set_decl_abstract_flags (local_decl, setting);
17760 for (subblock = BLOCK_SUBBLOCKS (stmt);
17761 subblock != NULL_TREE;
17762 subblock = BLOCK_CHAIN (subblock))
17763 set_block_abstract_flags (subblock, setting);
17766 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17767 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17768 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17769 set the abstract flags for all of the parameters, local vars, local
17770 blocks and sub-blocks (recursively) to the same setting. */
17772 static void
17773 set_decl_abstract_flags (tree decl, int setting)
17775 DECL_ABSTRACT (decl) = setting;
17776 if (TREE_CODE (decl) == FUNCTION_DECL)
17778 tree arg;
17780 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17781 DECL_ABSTRACT (arg) = setting;
17782 if (DECL_INITIAL (decl) != NULL_TREE
17783 && DECL_INITIAL (decl) != error_mark_node)
17784 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17788 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17789 may later generate inlined and/or out-of-line instances of. */
17791 static void
17792 dwarf2out_abstract_function (tree decl)
17794 dw_die_ref old_die;
17795 tree save_fn;
17796 tree context;
17797 int was_abstract;
17798 htab_t old_decl_loc_table;
17799 htab_t old_cached_dw_loc_list_table;
17800 int old_call_site_count, old_tail_call_site_count;
17801 struct call_arg_loc_node *old_call_arg_locations;
17803 /* Make sure we have the actual abstract inline, not a clone. */
17804 decl = DECL_ORIGIN (decl);
17806 old_die = lookup_decl_die (decl);
17807 if (old_die && get_AT (old_die, DW_AT_inline))
17808 /* We've already generated the abstract instance. */
17809 return;
17811 /* We can be called while recursively when seeing block defining inlined subroutine
17812 DIE. Be sure to not clobber the outer location table nor use it or we would
17813 get locations in abstract instantces. */
17814 old_decl_loc_table = decl_loc_table;
17815 decl_loc_table = NULL;
17816 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17817 cached_dw_loc_list_table = NULL;
17818 old_call_arg_locations = call_arg_locations;
17819 call_arg_locations = NULL;
17820 old_call_site_count = call_site_count;
17821 call_site_count = -1;
17822 old_tail_call_site_count = tail_call_site_count;
17823 tail_call_site_count = -1;
17825 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17826 we don't get confused by DECL_ABSTRACT. */
17827 if (debug_info_level > DINFO_LEVEL_TERSE)
17829 context = decl_class_context (decl);
17830 if (context)
17831 gen_type_die_for_member
17832 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17835 /* Pretend we've just finished compiling this function. */
17836 save_fn = current_function_decl;
17837 current_function_decl = decl;
17839 was_abstract = DECL_ABSTRACT (decl);
17840 set_decl_abstract_flags (decl, 1);
17841 dwarf2out_decl (decl);
17842 if (! was_abstract)
17843 set_decl_abstract_flags (decl, 0);
17845 current_function_decl = save_fn;
17846 decl_loc_table = old_decl_loc_table;
17847 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17848 call_arg_locations = old_call_arg_locations;
17849 call_site_count = old_call_site_count;
17850 tail_call_site_count = old_tail_call_site_count;
17853 /* Helper function of premark_used_types() which gets called through
17854 htab_traverse.
17856 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17857 marked as unused by prune_unused_types. */
17859 static int
17860 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17862 tree type;
17863 dw_die_ref die;
17865 type = (tree) *slot;
17866 die = lookup_type_die (type);
17867 if (die != NULL)
17868 die->die_perennial_p = 1;
17869 return 1;
17872 /* Helper function of premark_types_used_by_global_vars which gets called
17873 through htab_traverse.
17875 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17876 marked as unused by prune_unused_types. The DIE of the type is marked
17877 only if the global variable using the type will actually be emitted. */
17879 static int
17880 premark_types_used_by_global_vars_helper (void **slot,
17881 void *data ATTRIBUTE_UNUSED)
17883 struct types_used_by_vars_entry *entry;
17884 dw_die_ref die;
17886 entry = (struct types_used_by_vars_entry *) *slot;
17887 gcc_assert (entry->type != NULL
17888 && entry->var_decl != NULL);
17889 die = lookup_type_die (entry->type);
17890 if (die)
17892 /* Ask cgraph if the global variable really is to be emitted.
17893 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17894 varpool_node *node = varpool_get_node (entry->var_decl);
17895 if (node && node->definition)
17897 die->die_perennial_p = 1;
17898 /* Keep the parent DIEs as well. */
17899 while ((die = die->die_parent) && die->die_perennial_p == 0)
17900 die->die_perennial_p = 1;
17903 return 1;
17906 /* Mark all members of used_types_hash as perennial. */
17908 static void
17909 premark_used_types (struct function *fun)
17911 if (fun && fun->used_types_hash)
17912 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17915 /* Mark all members of types_used_by_vars_entry as perennial. */
17917 static void
17918 premark_types_used_by_global_vars (void)
17920 if (types_used_by_vars_hash)
17921 htab_traverse (types_used_by_vars_hash,
17922 premark_types_used_by_global_vars_helper, NULL);
17925 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17926 for CA_LOC call arg loc node. */
17928 static dw_die_ref
17929 gen_call_site_die (tree decl, dw_die_ref subr_die,
17930 struct call_arg_loc_node *ca_loc)
17932 dw_die_ref stmt_die = NULL, die;
17933 tree block = ca_loc->block;
17935 while (block
17936 && block != DECL_INITIAL (decl)
17937 && TREE_CODE (block) == BLOCK)
17939 if (block_map.length () > BLOCK_NUMBER (block))
17940 stmt_die = block_map[BLOCK_NUMBER (block)];
17941 if (stmt_die)
17942 break;
17943 block = BLOCK_SUPERCONTEXT (block);
17945 if (stmt_die == NULL)
17946 stmt_die = subr_die;
17947 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17948 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17949 if (ca_loc->tail_call_p)
17950 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17951 if (ca_loc->symbol_ref)
17953 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17954 if (tdie)
17955 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17956 else
17957 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17959 return die;
17962 /* Generate a DIE to represent a declared function (either file-scope or
17963 block-local). */
17965 static void
17966 gen_subprogram_die (tree decl, dw_die_ref context_die)
17968 tree origin = decl_ultimate_origin (decl);
17969 dw_die_ref subr_die;
17970 tree outer_scope;
17971 dw_die_ref old_die = lookup_decl_die (decl);
17972 int declaration = (current_function_decl != decl
17973 || class_or_namespace_scope_p (context_die));
17975 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17977 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17978 started to generate the abstract instance of an inline, decided to output
17979 its containing class, and proceeded to emit the declaration of the inline
17980 from the member list for the class. If so, DECLARATION takes priority;
17981 we'll get back to the abstract instance when done with the class. */
17983 /* The class-scope declaration DIE must be the primary DIE. */
17984 if (origin && declaration && class_or_namespace_scope_p (context_die))
17986 origin = NULL;
17987 gcc_assert (!old_die);
17990 /* Now that the C++ front end lazily declares artificial member fns, we
17991 might need to retrofit the declaration into its class. */
17992 if (!declaration && !origin && !old_die
17993 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17994 && !class_or_namespace_scope_p (context_die)
17995 && debug_info_level > DINFO_LEVEL_TERSE)
17996 old_die = force_decl_die (decl);
17998 if (origin != NULL)
18000 gcc_assert (!declaration || local_scope_p (context_die));
18002 /* Fixup die_parent for the abstract instance of a nested
18003 inline function. */
18004 if (old_die && old_die->die_parent == NULL)
18005 add_child_die (context_die, old_die);
18007 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18008 add_abstract_origin_attribute (subr_die, origin);
18009 /* This is where the actual code for a cloned function is.
18010 Let's emit linkage name attribute for it. This helps
18011 debuggers to e.g, set breakpoints into
18012 constructors/destructors when the user asks "break
18013 K::K". */
18014 add_linkage_name (subr_die, decl);
18016 else if (old_die)
18018 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18019 struct dwarf_file_data * file_index = lookup_filename (s.file);
18021 if (!get_AT_flag (old_die, DW_AT_declaration)
18022 /* We can have a normal definition following an inline one in the
18023 case of redefinition of GNU C extern inlines.
18024 It seems reasonable to use AT_specification in this case. */
18025 && !get_AT (old_die, DW_AT_inline))
18027 /* Detect and ignore this case, where we are trying to output
18028 something we have already output. */
18029 return;
18032 /* If the definition comes from the same place as the declaration,
18033 maybe use the old DIE. We always want the DIE for this function
18034 that has the *_pc attributes to be under comp_unit_die so the
18035 debugger can find it. We also need to do this for abstract
18036 instances of inlines, since the spec requires the out-of-line copy
18037 to have the same parent. For local class methods, this doesn't
18038 apply; we just use the old DIE. */
18039 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18040 && (DECL_ARTIFICIAL (decl)
18041 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18042 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18043 == (unsigned) s.line))))
18045 subr_die = old_die;
18047 /* Clear out the declaration attribute and the formal parameters.
18048 Do not remove all children, because it is possible that this
18049 declaration die was forced using force_decl_die(). In such
18050 cases die that forced declaration die (e.g. TAG_imported_module)
18051 is one of the children that we do not want to remove. */
18052 remove_AT (subr_die, DW_AT_declaration);
18053 remove_AT (subr_die, DW_AT_object_pointer);
18054 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18056 else
18058 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18059 add_AT_specification (subr_die, old_die);
18060 add_pubname (decl, subr_die);
18061 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18062 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18063 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18064 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18066 /* If the prototype had an 'auto' return type, emit the real
18067 type on the definition die. */
18068 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE
18069 && get_AT_ref (old_die, DW_AT_type) == auto_die)
18070 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18071 0, 0, context_die);
18074 else
18076 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18078 if (TREE_PUBLIC (decl))
18079 add_AT_flag (subr_die, DW_AT_external, 1);
18081 add_name_and_src_coords_attributes (subr_die, decl);
18082 add_pubname (decl, subr_die);
18083 if (debug_info_level > DINFO_LEVEL_TERSE)
18085 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18086 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18087 0, 0, context_die);
18090 add_pure_or_virtual_attribute (subr_die, decl);
18091 if (DECL_ARTIFICIAL (decl))
18092 add_AT_flag (subr_die, DW_AT_artificial, 1);
18094 add_accessibility_attribute (subr_die, decl);
18097 if (declaration)
18099 if (!old_die || !get_AT (old_die, DW_AT_inline))
18101 add_AT_flag (subr_die, DW_AT_declaration, 1);
18103 /* If this is an explicit function declaration then generate
18104 a DW_AT_explicit attribute. */
18105 if (lang_hooks.decls.function_decl_explicit_p (decl)
18106 && (dwarf_version >= 3 || !dwarf_strict))
18107 add_AT_flag (subr_die, DW_AT_explicit, 1);
18109 /* The first time we see a member function, it is in the context of
18110 the class to which it belongs. We make sure of this by emitting
18111 the class first. The next time is the definition, which is
18112 handled above. The two may come from the same source text.
18114 Note that force_decl_die() forces function declaration die. It is
18115 later reused to represent definition. */
18116 equate_decl_number_to_die (decl, subr_die);
18119 else if (DECL_ABSTRACT (decl))
18121 if (DECL_DECLARED_INLINE_P (decl))
18123 if (cgraph_function_possibly_inlined_p (decl))
18124 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18125 else
18126 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18128 else
18130 if (cgraph_function_possibly_inlined_p (decl))
18131 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18132 else
18133 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18136 if (DECL_DECLARED_INLINE_P (decl)
18137 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18138 add_AT_flag (subr_die, DW_AT_artificial, 1);
18140 equate_decl_number_to_die (decl, subr_die);
18142 else if (!DECL_EXTERNAL (decl))
18144 HOST_WIDE_INT cfa_fb_offset;
18145 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18147 if (!old_die || !get_AT (old_die, DW_AT_inline))
18148 equate_decl_number_to_die (decl, subr_die);
18150 gcc_checking_assert (fun);
18151 if (!flag_reorder_blocks_and_partition)
18153 dw_fde_ref fde = fun->fde;
18154 if (fde->dw_fde_begin)
18156 /* We have already generated the labels. */
18157 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18158 fde->dw_fde_end, false);
18160 else
18162 /* Create start/end labels and add the range. */
18163 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18164 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18165 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18166 current_function_funcdef_no);
18167 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18168 current_function_funcdef_no);
18169 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18170 false);
18173 #if VMS_DEBUGGING_INFO
18174 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18175 Section 2.3 Prologue and Epilogue Attributes:
18176 When a breakpoint is set on entry to a function, it is generally
18177 desirable for execution to be suspended, not on the very first
18178 instruction of the function, but rather at a point after the
18179 function's frame has been set up, after any language defined local
18180 declaration processing has been completed, and before execution of
18181 the first statement of the function begins. Debuggers generally
18182 cannot properly determine where this point is. Similarly for a
18183 breakpoint set on exit from a function. The prologue and epilogue
18184 attributes allow a compiler to communicate the location(s) to use. */
18187 if (fde->dw_fde_vms_end_prologue)
18188 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18189 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18191 if (fde->dw_fde_vms_begin_epilogue)
18192 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18193 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18195 #endif
18198 else
18200 /* Generate pubnames entries for the split function code ranges. */
18201 dw_fde_ref fde = fun->fde;
18203 if (fde->dw_fde_second_begin)
18205 if (dwarf_version >= 3 || !dwarf_strict)
18207 /* We should use ranges for non-contiguous code section
18208 addresses. Use the actual code range for the initial
18209 section, since the HOT/COLD labels might precede an
18210 alignment offset. */
18211 bool range_list_added = false;
18212 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18213 fde->dw_fde_end, &range_list_added,
18214 false);
18215 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18216 fde->dw_fde_second_end,
18217 &range_list_added, false);
18218 if (range_list_added)
18219 add_ranges (NULL);
18221 else
18223 /* There is no real support in DW2 for this .. so we make
18224 a work-around. First, emit the pub name for the segment
18225 containing the function label. Then make and emit a
18226 simplified subprogram DIE for the second segment with the
18227 name pre-fixed by __hot/cold_sect_of_. We use the same
18228 linkage name for the second die so that gdb will find both
18229 sections when given "b foo". */
18230 const char *name = NULL;
18231 tree decl_name = DECL_NAME (decl);
18232 dw_die_ref seg_die;
18234 /* Do the 'primary' section. */
18235 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18236 fde->dw_fde_end, false);
18238 /* Build a minimal DIE for the secondary section. */
18239 seg_die = new_die (DW_TAG_subprogram,
18240 subr_die->die_parent, decl);
18242 if (TREE_PUBLIC (decl))
18243 add_AT_flag (seg_die, DW_AT_external, 1);
18245 if (decl_name != NULL
18246 && IDENTIFIER_POINTER (decl_name) != NULL)
18248 name = dwarf2_name (decl, 1);
18249 if (! DECL_ARTIFICIAL (decl))
18250 add_src_coords_attributes (seg_die, decl);
18252 add_linkage_name (seg_die, decl);
18254 gcc_assert (name != NULL);
18255 add_pure_or_virtual_attribute (seg_die, decl);
18256 if (DECL_ARTIFICIAL (decl))
18257 add_AT_flag (seg_die, DW_AT_artificial, 1);
18259 name = concat ("__second_sect_of_", name, NULL);
18260 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18261 fde->dw_fde_second_end, false);
18262 add_name_attribute (seg_die, name);
18263 if (want_pubnames ())
18264 add_pubname_string (name, seg_die);
18267 else
18268 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18269 false);
18272 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18274 /* We define the "frame base" as the function's CFA. This is more
18275 convenient for several reasons: (1) It's stable across the prologue
18276 and epilogue, which makes it better than just a frame pointer,
18277 (2) With dwarf3, there exists a one-byte encoding that allows us
18278 to reference the .debug_frame data by proxy, but failing that,
18279 (3) We can at least reuse the code inspection and interpretation
18280 code that determines the CFA position at various points in the
18281 function. */
18282 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18284 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18285 add_AT_loc (subr_die, DW_AT_frame_base, op);
18287 else
18289 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18290 if (list->dw_loc_next)
18291 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18292 else
18293 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18296 /* Compute a displacement from the "steady-state frame pointer" to
18297 the CFA. The former is what all stack slots and argument slots
18298 will reference in the rtl; the latter is what we've told the
18299 debugger about. We'll need to adjust all frame_base references
18300 by this displacement. */
18301 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18303 if (fun->static_chain_decl)
18304 add_AT_location_description (subr_die, DW_AT_static_link,
18305 loc_list_from_tree (fun->static_chain_decl, 2));
18308 /* Generate child dies for template paramaters. */
18309 if (debug_info_level > DINFO_LEVEL_TERSE)
18310 gen_generic_params_dies (decl);
18312 /* Now output descriptions of the arguments for this function. This gets
18313 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18314 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18315 `...' at the end of the formal parameter list. In order to find out if
18316 there was a trailing ellipsis or not, we must instead look at the type
18317 associated with the FUNCTION_DECL. This will be a node of type
18318 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18319 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18320 an ellipsis at the end. */
18322 /* In the case where we are describing a mere function declaration, all we
18323 need to do here (and all we *can* do here) is to describe the *types* of
18324 its formal parameters. */
18325 if (debug_info_level <= DINFO_LEVEL_TERSE)
18327 else if (declaration)
18328 gen_formal_types_die (decl, subr_die);
18329 else
18331 /* Generate DIEs to represent all known formal parameters. */
18332 tree parm = DECL_ARGUMENTS (decl);
18333 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18334 tree generic_decl_parm = generic_decl
18335 ? DECL_ARGUMENTS (generic_decl)
18336 : NULL;
18338 /* Now we want to walk the list of parameters of the function and
18339 emit their relevant DIEs.
18341 We consider the case of DECL being an instance of a generic function
18342 as well as it being a normal function.
18344 If DECL is an instance of a generic function we walk the
18345 parameters of the generic function declaration _and_ the parameters of
18346 DECL itself. This is useful because we want to emit specific DIEs for
18347 function parameter packs and those are declared as part of the
18348 generic function declaration. In that particular case,
18349 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18350 That DIE has children DIEs representing the set of arguments
18351 of the pack. Note that the set of pack arguments can be empty.
18352 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18353 children DIE.
18355 Otherwise, we just consider the parameters of DECL. */
18356 while (generic_decl_parm || parm)
18358 if (generic_decl_parm
18359 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18360 gen_formal_parameter_pack_die (generic_decl_parm,
18361 parm, subr_die,
18362 &parm);
18363 else if (parm)
18365 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18367 if (parm == DECL_ARGUMENTS (decl)
18368 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18369 && parm_die
18370 && (dwarf_version >= 3 || !dwarf_strict))
18371 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18373 parm = DECL_CHAIN (parm);
18376 if (generic_decl_parm)
18377 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18380 /* Decide whether we need an unspecified_parameters DIE at the end.
18381 There are 2 more cases to do this for: 1) the ansi ... declaration -
18382 this is detectable when the end of the arg list is not a
18383 void_type_node 2) an unprototyped function declaration (not a
18384 definition). This just means that we have no info about the
18385 parameters at all. */
18386 if (prototype_p (TREE_TYPE (decl)))
18388 /* This is the prototyped case, check for.... */
18389 if (stdarg_p (TREE_TYPE (decl)))
18390 gen_unspecified_parameters_die (decl, subr_die);
18392 else if (DECL_INITIAL (decl) == NULL_TREE)
18393 gen_unspecified_parameters_die (decl, subr_die);
18396 /* Output Dwarf info for all of the stuff within the body of the function
18397 (if it has one - it may be just a declaration). */
18398 outer_scope = DECL_INITIAL (decl);
18400 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18401 a function. This BLOCK actually represents the outermost binding contour
18402 for the function, i.e. the contour in which the function's formal
18403 parameters and labels get declared. Curiously, it appears that the front
18404 end doesn't actually put the PARM_DECL nodes for the current function onto
18405 the BLOCK_VARS list for this outer scope, but are strung off of the
18406 DECL_ARGUMENTS list for the function instead.
18408 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18409 the LABEL_DECL nodes for the function however, and we output DWARF info
18410 for those in decls_for_scope. Just within the `outer_scope' there will be
18411 a BLOCK node representing the function's outermost pair of curly braces,
18412 and any blocks used for the base and member initializers of a C++
18413 constructor function. */
18414 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18416 int call_site_note_count = 0;
18417 int tail_call_site_note_count = 0;
18419 /* Emit a DW_TAG_variable DIE for a named return value. */
18420 if (DECL_NAME (DECL_RESULT (decl)))
18421 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18423 current_function_has_inlines = 0;
18424 decls_for_scope (outer_scope, subr_die, 0);
18426 if (call_arg_locations && !dwarf_strict)
18428 struct call_arg_loc_node *ca_loc;
18429 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18431 dw_die_ref die = NULL;
18432 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18433 rtx arg, next_arg;
18435 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18436 arg; arg = next_arg)
18438 dw_loc_descr_ref reg, val;
18439 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18440 dw_die_ref cdie, tdie = NULL;
18442 next_arg = XEXP (arg, 1);
18443 if (REG_P (XEXP (XEXP (arg, 0), 0))
18444 && next_arg
18445 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18446 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18447 && REGNO (XEXP (XEXP (arg, 0), 0))
18448 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18449 next_arg = XEXP (next_arg, 1);
18450 if (mode == VOIDmode)
18452 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18453 if (mode == VOIDmode)
18454 mode = GET_MODE (XEXP (arg, 0));
18456 if (mode == VOIDmode || mode == BLKmode)
18457 continue;
18458 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18460 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18461 tloc = XEXP (XEXP (arg, 0), 1);
18462 continue;
18464 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18465 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18467 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18468 tlocc = XEXP (XEXP (arg, 0), 1);
18469 continue;
18471 reg = NULL;
18472 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18473 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18474 VAR_INIT_STATUS_INITIALIZED);
18475 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18477 rtx mem = XEXP (XEXP (arg, 0), 0);
18478 reg = mem_loc_descriptor (XEXP (mem, 0),
18479 get_address_mode (mem),
18480 GET_MODE (mem),
18481 VAR_INIT_STATUS_INITIALIZED);
18483 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18484 == DEBUG_PARAMETER_REF)
18486 tree tdecl
18487 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18488 tdie = lookup_decl_die (tdecl);
18489 if (tdie == NULL)
18490 continue;
18492 else
18493 continue;
18494 if (reg == NULL
18495 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18496 != DEBUG_PARAMETER_REF)
18497 continue;
18498 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18499 VOIDmode,
18500 VAR_INIT_STATUS_INITIALIZED);
18501 if (val == NULL)
18502 continue;
18503 if (die == NULL)
18504 die = gen_call_site_die (decl, subr_die, ca_loc);
18505 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18506 NULL_TREE);
18507 if (reg != NULL)
18508 add_AT_loc (cdie, DW_AT_location, reg);
18509 else if (tdie != NULL)
18510 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18511 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18512 if (next_arg != XEXP (arg, 1))
18514 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18515 if (mode == VOIDmode)
18516 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18517 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18518 0), 1),
18519 mode, VOIDmode,
18520 VAR_INIT_STATUS_INITIALIZED);
18521 if (val != NULL)
18522 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18525 if (die == NULL
18526 && (ca_loc->symbol_ref || tloc))
18527 die = gen_call_site_die (decl, subr_die, ca_loc);
18528 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18530 dw_loc_descr_ref tval = NULL;
18532 if (tloc != NULL_RTX)
18533 tval = mem_loc_descriptor (tloc,
18534 GET_MODE (tloc) == VOIDmode
18535 ? Pmode : GET_MODE (tloc),
18536 VOIDmode,
18537 VAR_INIT_STATUS_INITIALIZED);
18538 if (tval)
18539 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18540 else if (tlocc != NULL_RTX)
18542 tval = mem_loc_descriptor (tlocc,
18543 GET_MODE (tlocc) == VOIDmode
18544 ? Pmode : GET_MODE (tlocc),
18545 VOIDmode,
18546 VAR_INIT_STATUS_INITIALIZED);
18547 if (tval)
18548 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18549 tval);
18552 if (die != NULL)
18554 call_site_note_count++;
18555 if (ca_loc->tail_call_p)
18556 tail_call_site_note_count++;
18560 call_arg_locations = NULL;
18561 call_arg_loc_last = NULL;
18562 if (tail_call_site_count >= 0
18563 && tail_call_site_count == tail_call_site_note_count
18564 && !dwarf_strict)
18566 if (call_site_count >= 0
18567 && call_site_count == call_site_note_count)
18568 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18569 else
18570 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18572 call_site_count = -1;
18573 tail_call_site_count = -1;
18576 if (subr_die != old_die)
18577 /* Add the calling convention attribute if requested. */
18578 add_calling_convention_attribute (subr_die, decl);
18581 /* Returns a hash value for X (which really is a die_struct). */
18583 static hashval_t
18584 common_block_die_table_hash (const void *x)
18586 const_dw_die_ref d = (const_dw_die_ref) x;
18587 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18590 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18591 as decl_id and die_parent of die_struct Y. */
18593 static int
18594 common_block_die_table_eq (const void *x, const void *y)
18596 const_dw_die_ref d = (const_dw_die_ref) x;
18597 const_dw_die_ref e = (const_dw_die_ref) y;
18598 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18601 /* Generate a DIE to represent a declared data object.
18602 Either DECL or ORIGIN must be non-null. */
18604 static void
18605 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18607 HOST_WIDE_INT off = 0;
18608 tree com_decl;
18609 tree decl_or_origin = decl ? decl : origin;
18610 tree ultimate_origin;
18611 dw_die_ref var_die;
18612 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18613 dw_die_ref origin_die;
18614 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18615 || class_or_namespace_scope_p (context_die));
18616 bool specialization_p = false;
18618 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18619 if (decl || ultimate_origin)
18620 origin = ultimate_origin;
18621 com_decl = fortran_common (decl_or_origin, &off);
18623 /* Symbol in common gets emitted as a child of the common block, in the form
18624 of a data member. */
18625 if (com_decl)
18627 dw_die_ref com_die;
18628 dw_loc_list_ref loc;
18629 die_node com_die_arg;
18631 var_die = lookup_decl_die (decl_or_origin);
18632 if (var_die)
18634 if (get_AT (var_die, DW_AT_location) == NULL)
18636 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18637 if (loc)
18639 if (off)
18641 /* Optimize the common case. */
18642 if (single_element_loc_list_p (loc)
18643 && loc->expr->dw_loc_opc == DW_OP_addr
18644 && loc->expr->dw_loc_next == NULL
18645 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18646 == SYMBOL_REF)
18648 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18649 loc->expr->dw_loc_oprnd1.v.val_addr
18650 = plus_constant (GET_MODE (x), x , off);
18652 else
18653 loc_list_plus_const (loc, off);
18655 add_AT_location_description (var_die, DW_AT_location, loc);
18656 remove_AT (var_die, DW_AT_declaration);
18659 return;
18662 if (common_block_die_table == NULL)
18663 common_block_die_table
18664 = htab_create_ggc (10, common_block_die_table_hash,
18665 common_block_die_table_eq, NULL);
18667 com_die_arg.decl_id = DECL_UID (com_decl);
18668 com_die_arg.die_parent = context_die;
18669 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18670 loc = loc_list_from_tree (com_decl, 2);
18671 if (com_die == NULL)
18673 const char *cnam
18674 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18675 void **slot;
18677 com_die = new_die (DW_TAG_common_block, context_die, decl);
18678 add_name_and_src_coords_attributes (com_die, com_decl);
18679 if (loc)
18681 add_AT_location_description (com_die, DW_AT_location, loc);
18682 /* Avoid sharing the same loc descriptor between
18683 DW_TAG_common_block and DW_TAG_variable. */
18684 loc = loc_list_from_tree (com_decl, 2);
18686 else if (DECL_EXTERNAL (decl))
18687 add_AT_flag (com_die, DW_AT_declaration, 1);
18688 if (want_pubnames ())
18689 add_pubname_string (cnam, com_die); /* ??? needed? */
18690 com_die->decl_id = DECL_UID (com_decl);
18691 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18692 *slot = (void *) com_die;
18694 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18696 add_AT_location_description (com_die, DW_AT_location, loc);
18697 loc = loc_list_from_tree (com_decl, 2);
18698 remove_AT (com_die, DW_AT_declaration);
18700 var_die = new_die (DW_TAG_variable, com_die, decl);
18701 add_name_and_src_coords_attributes (var_die, decl);
18702 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18703 TREE_THIS_VOLATILE (decl), context_die);
18704 add_AT_flag (var_die, DW_AT_external, 1);
18705 if (loc)
18707 if (off)
18709 /* Optimize the common case. */
18710 if (single_element_loc_list_p (loc)
18711 && loc->expr->dw_loc_opc == DW_OP_addr
18712 && loc->expr->dw_loc_next == NULL
18713 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18715 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18716 loc->expr->dw_loc_oprnd1.v.val_addr
18717 = plus_constant (GET_MODE (x), x, off);
18719 else
18720 loc_list_plus_const (loc, off);
18722 add_AT_location_description (var_die, DW_AT_location, loc);
18724 else if (DECL_EXTERNAL (decl))
18725 add_AT_flag (var_die, DW_AT_declaration, 1);
18726 equate_decl_number_to_die (decl, var_die);
18727 return;
18730 /* If the compiler emitted a definition for the DECL declaration
18731 and if we already emitted a DIE for it, don't emit a second
18732 DIE for it again. Allow re-declarations of DECLs that are
18733 inside functions, though. */
18734 if (old_die && declaration && !local_scope_p (context_die))
18735 return;
18737 /* For static data members, the declaration in the class is supposed
18738 to have DW_TAG_member tag; the specification should still be
18739 DW_TAG_variable referencing the DW_TAG_member DIE. */
18740 if (declaration && class_scope_p (context_die))
18741 var_die = new_die (DW_TAG_member, context_die, decl);
18742 else
18743 var_die = new_die (DW_TAG_variable, context_die, decl);
18745 origin_die = NULL;
18746 if (origin != NULL)
18747 origin_die = add_abstract_origin_attribute (var_die, origin);
18749 /* Loop unrolling can create multiple blocks that refer to the same
18750 static variable, so we must test for the DW_AT_declaration flag.
18752 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18753 copy decls and set the DECL_ABSTRACT flag on them instead of
18754 sharing them.
18756 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18758 ??? The declare_in_namespace support causes us to get two DIEs for one
18759 variable, both of which are declarations. We want to avoid considering
18760 one to be a specification, so we must test that this DIE is not a
18761 declaration. */
18762 else if (old_die && TREE_STATIC (decl) && ! declaration
18763 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18765 /* This is a definition of a C++ class level static. */
18766 add_AT_specification (var_die, old_die);
18767 specialization_p = true;
18768 if (DECL_NAME (decl))
18770 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18771 struct dwarf_file_data * file_index = lookup_filename (s.file);
18773 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18774 add_AT_file (var_die, DW_AT_decl_file, file_index);
18776 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18777 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18779 if (old_die->die_tag == DW_TAG_member)
18780 add_linkage_name (var_die, decl);
18783 else
18784 add_name_and_src_coords_attributes (var_die, decl);
18786 if ((origin == NULL && !specialization_p)
18787 || (origin != NULL
18788 && !DECL_ABSTRACT (decl_or_origin)
18789 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18790 decl_function_context
18791 (decl_or_origin))))
18793 tree type = TREE_TYPE (decl_or_origin);
18795 if (decl_by_reference_p (decl_or_origin))
18796 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18797 else
18798 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18799 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18802 if (origin == NULL && !specialization_p)
18804 if (TREE_PUBLIC (decl))
18805 add_AT_flag (var_die, DW_AT_external, 1);
18807 if (DECL_ARTIFICIAL (decl))
18808 add_AT_flag (var_die, DW_AT_artificial, 1);
18810 add_accessibility_attribute (var_die, decl);
18813 if (declaration)
18814 add_AT_flag (var_die, DW_AT_declaration, 1);
18816 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18817 equate_decl_number_to_die (decl, var_die);
18819 if (! declaration
18820 && (! DECL_ABSTRACT (decl_or_origin)
18821 /* Local static vars are shared between all clones/inlines,
18822 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18823 already set. */
18824 || (TREE_CODE (decl_or_origin) == VAR_DECL
18825 && TREE_STATIC (decl_or_origin)
18826 && DECL_RTL_SET_P (decl_or_origin)))
18827 /* When abstract origin already has DW_AT_location attribute, no need
18828 to add it again. */
18829 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18831 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18832 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18833 defer_location (decl_or_origin, var_die);
18834 else
18835 add_location_or_const_value_attribute (var_die, decl_or_origin,
18836 decl == NULL, DW_AT_location);
18837 add_pubname (decl_or_origin, var_die);
18839 else
18840 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18843 /* Generate a DIE to represent a named constant. */
18845 static void
18846 gen_const_die (tree decl, dw_die_ref context_die)
18848 dw_die_ref const_die;
18849 tree type = TREE_TYPE (decl);
18851 const_die = new_die (DW_TAG_constant, context_die, decl);
18852 add_name_and_src_coords_attributes (const_die, decl);
18853 add_type_attribute (const_die, type, 1, 0, context_die);
18854 if (TREE_PUBLIC (decl))
18855 add_AT_flag (const_die, DW_AT_external, 1);
18856 if (DECL_ARTIFICIAL (decl))
18857 add_AT_flag (const_die, DW_AT_artificial, 1);
18858 tree_add_const_value_attribute_for_decl (const_die, decl);
18861 /* Generate a DIE to represent a label identifier. */
18863 static void
18864 gen_label_die (tree decl, dw_die_ref context_die)
18866 tree origin = decl_ultimate_origin (decl);
18867 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18868 rtx insn;
18869 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18871 if (origin != NULL)
18872 add_abstract_origin_attribute (lbl_die, origin);
18873 else
18874 add_name_and_src_coords_attributes (lbl_die, decl);
18876 if (DECL_ABSTRACT (decl))
18877 equate_decl_number_to_die (decl, lbl_die);
18878 else
18880 insn = DECL_RTL_IF_SET (decl);
18882 /* Deleted labels are programmer specified labels which have been
18883 eliminated because of various optimizations. We still emit them
18884 here so that it is possible to put breakpoints on them. */
18885 if (insn
18886 && (LABEL_P (insn)
18887 || ((NOTE_P (insn)
18888 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18890 /* When optimization is enabled (via -O) some parts of the compiler
18891 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18892 represent source-level labels which were explicitly declared by
18893 the user. This really shouldn't be happening though, so catch
18894 it if it ever does happen. */
18895 gcc_assert (!INSN_DELETED_P (insn));
18897 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18898 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18900 else if (insn
18901 && NOTE_P (insn)
18902 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18903 && CODE_LABEL_NUMBER (insn) != -1)
18905 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18906 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18911 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18912 attributes to the DIE for a block STMT, to describe where the inlined
18913 function was called from. This is similar to add_src_coords_attributes. */
18915 static inline void
18916 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18918 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18920 if (dwarf_version >= 3 || !dwarf_strict)
18922 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18923 add_AT_unsigned (die, DW_AT_call_line, s.line);
18928 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18929 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18931 static inline void
18932 add_high_low_attributes (tree stmt, dw_die_ref die)
18934 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18936 if (BLOCK_FRAGMENT_CHAIN (stmt)
18937 && (dwarf_version >= 3 || !dwarf_strict))
18939 tree chain, superblock = NULL_TREE;
18940 dw_die_ref pdie;
18941 dw_attr_ref attr = NULL;
18943 if (inlined_function_outer_scope_p (stmt))
18945 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18946 BLOCK_NUMBER (stmt));
18947 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18950 /* Optimize duplicate .debug_ranges lists or even tails of
18951 lists. If this BLOCK has same ranges as its supercontext,
18952 lookup DW_AT_ranges attribute in the supercontext (and
18953 recursively so), verify that the ranges_table contains the
18954 right values and use it instead of adding a new .debug_range. */
18955 for (chain = stmt, pdie = die;
18956 BLOCK_SAME_RANGE (chain);
18957 chain = BLOCK_SUPERCONTEXT (chain))
18959 dw_attr_ref new_attr;
18961 pdie = pdie->die_parent;
18962 if (pdie == NULL)
18963 break;
18964 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18965 break;
18966 new_attr = get_AT (pdie, DW_AT_ranges);
18967 if (new_attr == NULL
18968 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18969 break;
18970 attr = new_attr;
18971 superblock = BLOCK_SUPERCONTEXT (chain);
18973 if (attr != NULL
18974 && (ranges_table[attr->dw_attr_val.v.val_offset
18975 / 2 / DWARF2_ADDR_SIZE].num
18976 == BLOCK_NUMBER (superblock))
18977 && BLOCK_FRAGMENT_CHAIN (superblock))
18979 unsigned long off = attr->dw_attr_val.v.val_offset
18980 / 2 / DWARF2_ADDR_SIZE;
18981 unsigned long supercnt = 0, thiscnt = 0;
18982 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18983 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18985 ++supercnt;
18986 gcc_checking_assert (ranges_table[off + supercnt].num
18987 == BLOCK_NUMBER (chain));
18989 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18990 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18991 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18992 ++thiscnt;
18993 gcc_assert (supercnt >= thiscnt);
18994 add_AT_range_list (die, DW_AT_ranges,
18995 ((off + supercnt - thiscnt)
18996 * 2 * DWARF2_ADDR_SIZE),
18997 false);
18998 return;
19001 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19003 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19006 add_ranges (chain);
19007 chain = BLOCK_FRAGMENT_CHAIN (chain);
19009 while (chain);
19010 add_ranges (NULL);
19012 else
19014 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19015 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19016 BLOCK_NUMBER (stmt));
19017 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19018 BLOCK_NUMBER (stmt));
19019 add_AT_low_high_pc (die, label, label_high, false);
19023 /* Generate a DIE for a lexical block. */
19025 static void
19026 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19028 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19030 if (call_arg_locations)
19032 if (block_map.length () <= BLOCK_NUMBER (stmt))
19033 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19034 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19037 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19038 add_high_low_attributes (stmt, stmt_die);
19040 decls_for_scope (stmt, stmt_die, depth);
19043 /* Generate a DIE for an inlined subprogram. */
19045 static void
19046 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19048 tree decl;
19050 /* The instance of function that is effectively being inlined shall not
19051 be abstract. */
19052 gcc_assert (! BLOCK_ABSTRACT (stmt));
19054 decl = block_ultimate_origin (stmt);
19056 /* Emit info for the abstract instance first, if we haven't yet. We
19057 must emit this even if the block is abstract, otherwise when we
19058 emit the block below (or elsewhere), we may end up trying to emit
19059 a die whose origin die hasn't been emitted, and crashing. */
19060 dwarf2out_abstract_function (decl);
19062 if (! BLOCK_ABSTRACT (stmt))
19064 dw_die_ref subr_die
19065 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19067 if (call_arg_locations)
19069 if (block_map.length () <= BLOCK_NUMBER (stmt))
19070 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19071 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19073 add_abstract_origin_attribute (subr_die, decl);
19074 if (TREE_ASM_WRITTEN (stmt))
19075 add_high_low_attributes (stmt, subr_die);
19076 add_call_src_coords_attributes (stmt, subr_die);
19078 decls_for_scope (stmt, subr_die, depth);
19079 current_function_has_inlines = 1;
19083 /* Generate a DIE for a field in a record, or structure. */
19085 static void
19086 gen_field_die (tree decl, dw_die_ref context_die)
19088 dw_die_ref decl_die;
19090 if (TREE_TYPE (decl) == error_mark_node)
19091 return;
19093 decl_die = new_die (DW_TAG_member, context_die, decl);
19094 add_name_and_src_coords_attributes (decl_die, decl);
19095 add_type_attribute (decl_die, member_declared_type (decl), 0, 0, context_die);
19097 if (DECL_BIT_FIELD_TYPE (decl))
19099 add_byte_size_attribute (decl_die, decl);
19100 add_bit_size_attribute (decl_die, decl);
19101 add_bit_offset_attribute (decl_die, decl);
19104 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19105 add_data_member_location_attribute (decl_die, decl);
19107 if (DECL_ARTIFICIAL (decl))
19108 add_AT_flag (decl_die, DW_AT_artificial, 1);
19110 add_accessibility_attribute (decl_die, decl);
19112 /* Equate decl number to die, so that we can look up this decl later on. */
19113 equate_decl_number_to_die (decl, decl_die);
19116 #if 0
19117 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19118 Use modified_type_die instead.
19119 We keep this code here just in case these types of DIEs may be needed to
19120 represent certain things in other languages (e.g. Pascal) someday. */
19122 static void
19123 gen_pointer_type_die (tree type, dw_die_ref context_die)
19125 dw_die_ref ptr_die
19126 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19128 equate_type_number_to_die (type, ptr_die);
19129 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19130 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19133 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19134 Use modified_type_die instead.
19135 We keep this code here just in case these types of DIEs may be needed to
19136 represent certain things in other languages (e.g. Pascal) someday. */
19138 static void
19139 gen_reference_type_die (tree type, dw_die_ref context_die)
19141 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19143 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19144 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19145 else
19146 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19148 equate_type_number_to_die (type, ref_die);
19149 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19150 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19152 #endif
19154 /* Generate a DIE for a pointer to a member type. */
19156 static void
19157 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19159 dw_die_ref ptr_die
19160 = new_die (DW_TAG_ptr_to_member_type,
19161 scope_die_for (type, context_die), type);
19163 equate_type_number_to_die (type, ptr_die);
19164 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19165 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19166 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19169 typedef const char *dchar_p; /* For DEF_VEC_P. */
19171 static char *producer_string;
19173 /* Return a heap allocated producer string including command line options
19174 if -grecord-gcc-switches. */
19176 static char *
19177 gen_producer_string (void)
19179 size_t j;
19180 auto_vec<dchar_p> switches;
19181 const char *language_string = lang_hooks.name;
19182 char *producer, *tail;
19183 const char *p;
19184 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19185 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19187 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19188 switch (save_decoded_options[j].opt_index)
19190 case OPT_o:
19191 case OPT_d:
19192 case OPT_dumpbase:
19193 case OPT_dumpdir:
19194 case OPT_auxbase:
19195 case OPT_auxbase_strip:
19196 case OPT_quiet:
19197 case OPT_version:
19198 case OPT_v:
19199 case OPT_w:
19200 case OPT_L:
19201 case OPT_D:
19202 case OPT_I:
19203 case OPT_U:
19204 case OPT_SPECIAL_unknown:
19205 case OPT_SPECIAL_ignore:
19206 case OPT_SPECIAL_program_name:
19207 case OPT_SPECIAL_input_file:
19208 case OPT_grecord_gcc_switches:
19209 case OPT_gno_record_gcc_switches:
19210 case OPT__output_pch_:
19211 case OPT_fdiagnostics_show_location_:
19212 case OPT_fdiagnostics_show_option:
19213 case OPT_fdiagnostics_show_caret:
19214 case OPT_fdiagnostics_color_:
19215 case OPT_fverbose_asm:
19216 case OPT____:
19217 case OPT__sysroot_:
19218 case OPT_nostdinc:
19219 case OPT_nostdinc__:
19220 /* Ignore these. */
19221 continue;
19222 default:
19223 if (cl_options[save_decoded_options[j].opt_index].flags
19224 & CL_NO_DWARF_RECORD)
19225 continue;
19226 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19227 == '-');
19228 switch (save_decoded_options[j].canonical_option[0][1])
19230 case 'M':
19231 case 'i':
19232 case 'W':
19233 continue;
19234 case 'f':
19235 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19236 "dump", 4) == 0)
19237 continue;
19238 break;
19239 default:
19240 break;
19242 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19243 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19244 break;
19247 producer = XNEWVEC (char, plen + 1 + len + 1);
19248 tail = producer;
19249 sprintf (tail, "%s %s", language_string, version_string);
19250 tail += plen;
19252 FOR_EACH_VEC_ELT (switches, j, p)
19254 len = strlen (p);
19255 *tail = ' ';
19256 memcpy (tail + 1, p, len);
19257 tail += len + 1;
19260 *tail = '\0';
19261 return producer;
19264 /* Generate the DIE for the compilation unit. */
19266 static dw_die_ref
19267 gen_compile_unit_die (const char *filename)
19269 dw_die_ref die;
19270 const char *language_string = lang_hooks.name;
19271 int language;
19273 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19275 if (filename)
19277 add_name_attribute (die, filename);
19278 /* Don't add cwd for <built-in>. */
19279 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19280 add_comp_dir_attribute (die);
19283 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19285 /* If our producer is LTO try to figure out a common language to use
19286 from the global list of translation units. */
19287 if (strcmp (language_string, "GNU GIMPLE") == 0)
19289 unsigned i;
19290 tree t;
19291 const char *common_lang = NULL;
19293 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19295 if (!TRANSLATION_UNIT_LANGUAGE (t))
19296 continue;
19297 if (!common_lang)
19298 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19299 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19301 else if (strncmp (common_lang, "GNU C", 5) == 0
19302 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19303 /* Mixing C and C++ is ok, use C++ in that case. */
19304 common_lang = "GNU C++";
19305 else
19307 /* Fall back to C. */
19308 common_lang = NULL;
19309 break;
19313 if (common_lang)
19314 language_string = common_lang;
19317 language = DW_LANG_C89;
19318 if (strcmp (language_string, "GNU C++") == 0)
19319 language = DW_LANG_C_plus_plus;
19320 else if (strcmp (language_string, "GNU F77") == 0)
19321 language = DW_LANG_Fortran77;
19322 else if (strcmp (language_string, "GNU Pascal") == 0)
19323 language = DW_LANG_Pascal83;
19324 else if (use_upc_dwarf2_extensions
19325 && (strcmp (language_string, "GNU UPC") == 0))
19326 language = DW_LANG_Upc;
19327 else if (dwarf_version >= 3 || !dwarf_strict)
19329 if (strcmp (language_string, "GNU Ada") == 0)
19330 language = DW_LANG_Ada95;
19331 else if (strcmp (language_string, "GNU Fortran") == 0)
19332 language = DW_LANG_Fortran95;
19333 else if (strcmp (language_string, "GNU Java") == 0)
19334 language = DW_LANG_Java;
19335 else if (strcmp (language_string, "GNU Objective-C") == 0)
19336 language = DW_LANG_ObjC;
19337 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19338 language = DW_LANG_ObjC_plus_plus;
19339 else if (dwarf_version >= 5 || !dwarf_strict)
19341 if (strcmp (language_string, "GNU Go") == 0)
19342 language = DW_LANG_Go;
19345 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19346 else if (strcmp (language_string, "GNU Fortran") == 0)
19347 language = DW_LANG_Fortran90;
19349 add_AT_unsigned (die, DW_AT_language, language);
19351 switch (language)
19353 case DW_LANG_Fortran77:
19354 case DW_LANG_Fortran90:
19355 case DW_LANG_Fortran95:
19356 /* Fortran has case insensitive identifiers and the front-end
19357 lowercases everything. */
19358 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19359 break;
19360 default:
19361 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19362 break;
19364 return die;
19367 /* Generate the DIE for a base class. */
19369 static void
19370 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19372 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19374 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19375 add_data_member_location_attribute (die, binfo);
19377 if (BINFO_VIRTUAL_P (binfo))
19378 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19380 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19381 children, otherwise the default is DW_ACCESS_public. In DWARF2
19382 the default has always been DW_ACCESS_private. */
19383 if (access == access_public_node)
19385 if (dwarf_version == 2
19386 || context_die->die_tag == DW_TAG_class_type)
19387 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19389 else if (access == access_protected_node)
19390 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19391 else if (dwarf_version > 2
19392 && context_die->die_tag != DW_TAG_class_type)
19393 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19396 /* Generate a DIE for a class member. */
19398 static void
19399 gen_member_die (tree type, dw_die_ref context_die)
19401 tree member;
19402 tree binfo = TYPE_BINFO (type);
19403 dw_die_ref child;
19405 /* If this is not an incomplete type, output descriptions of each of its
19406 members. Note that as we output the DIEs necessary to represent the
19407 members of this record or union type, we will also be trying to output
19408 DIEs to represent the *types* of those members. However the `type'
19409 function (above) will specifically avoid generating type DIEs for member
19410 types *within* the list of member DIEs for this (containing) type except
19411 for those types (of members) which are explicitly marked as also being
19412 members of this (containing) type themselves. The g++ front- end can
19413 force any given type to be treated as a member of some other (containing)
19414 type by setting the TYPE_CONTEXT of the given (member) type to point to
19415 the TREE node representing the appropriate (containing) type. */
19417 /* First output info about the base classes. */
19418 if (binfo)
19420 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19421 int i;
19422 tree base;
19424 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19425 gen_inheritance_die (base,
19426 (accesses ? (*accesses)[i] : access_public_node),
19427 context_die);
19430 /* Now output info about the data members and type members. */
19431 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19433 /* If we thought we were generating minimal debug info for TYPE
19434 and then changed our minds, some of the member declarations
19435 may have already been defined. Don't define them again, but
19436 do put them in the right order. */
19438 child = lookup_decl_die (member);
19439 if (child)
19440 splice_child_die (context_die, child);
19441 else
19442 gen_decl_die (member, NULL, context_die);
19445 /* Now output info about the function members (if any). */
19446 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19448 /* Don't include clones in the member list. */
19449 if (DECL_ABSTRACT_ORIGIN (member))
19450 continue;
19452 child = lookup_decl_die (member);
19453 if (child)
19454 splice_child_die (context_die, child);
19455 else
19456 gen_decl_die (member, NULL, context_die);
19460 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19461 is set, we pretend that the type was never defined, so we only get the
19462 member DIEs needed by later specification DIEs. */
19464 static void
19465 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19466 enum debug_info_usage usage)
19468 dw_die_ref type_die = lookup_type_die (type);
19469 dw_die_ref scope_die = 0;
19470 int nested = 0;
19471 int complete = (TYPE_SIZE (type)
19472 && (! TYPE_STUB_DECL (type)
19473 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19474 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19475 complete = complete && should_emit_struct_debug (type, usage);
19477 if (type_die && ! complete)
19478 return;
19480 if (TYPE_CONTEXT (type) != NULL_TREE
19481 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19482 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19483 nested = 1;
19485 scope_die = scope_die_for (type, context_die);
19487 /* Generate child dies for template paramaters. */
19488 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19489 schedule_generic_params_dies_gen (type);
19491 if (! type_die || (nested && is_cu_die (scope_die)))
19492 /* First occurrence of type or toplevel definition of nested class. */
19494 dw_die_ref old_die = type_die;
19496 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19497 ? record_type_tag (type) : DW_TAG_union_type,
19498 scope_die, type);
19499 equate_type_number_to_die (type, type_die);
19500 if (old_die)
19501 add_AT_specification (type_die, old_die);
19502 else
19503 add_name_attribute (type_die, type_tag (type));
19505 else
19506 remove_AT (type_die, DW_AT_declaration);
19508 /* If this type has been completed, then give it a byte_size attribute and
19509 then give a list of members. */
19510 if (complete && !ns_decl)
19512 /* Prevent infinite recursion in cases where the type of some member of
19513 this type is expressed in terms of this type itself. */
19514 TREE_ASM_WRITTEN (type) = 1;
19515 add_byte_size_attribute (type_die, type);
19516 if (TYPE_STUB_DECL (type) != NULL_TREE)
19518 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19519 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19522 /* If the first reference to this type was as the return type of an
19523 inline function, then it may not have a parent. Fix this now. */
19524 if (type_die->die_parent == NULL)
19525 add_child_die (scope_die, type_die);
19527 push_decl_scope (type);
19528 gen_member_die (type, type_die);
19529 pop_decl_scope ();
19531 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19532 if (TYPE_ARTIFICIAL (type))
19533 add_AT_flag (type_die, DW_AT_artificial, 1);
19535 /* GNU extension: Record what type our vtable lives in. */
19536 if (TYPE_VFIELD (type))
19538 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19540 gen_type_die (vtype, context_die);
19541 add_AT_die_ref (type_die, DW_AT_containing_type,
19542 lookup_type_die (vtype));
19545 else
19547 add_AT_flag (type_die, DW_AT_declaration, 1);
19549 /* We don't need to do this for function-local types. */
19550 if (TYPE_STUB_DECL (type)
19551 && ! decl_function_context (TYPE_STUB_DECL (type)))
19552 vec_safe_push (incomplete_types, type);
19555 if (get_AT (type_die, DW_AT_name))
19556 add_pubtype (type, type_die);
19559 /* Generate a DIE for a subroutine _type_. */
19561 static void
19562 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19564 tree return_type = TREE_TYPE (type);
19565 dw_die_ref subr_die
19566 = new_die (DW_TAG_subroutine_type,
19567 scope_die_for (type, context_die), type);
19569 equate_type_number_to_die (type, subr_die);
19570 add_prototyped_attribute (subr_die, type);
19571 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19572 gen_formal_types_die (type, subr_die);
19574 if (get_AT (subr_die, DW_AT_name))
19575 add_pubtype (type, subr_die);
19578 /* Generate a DIE for a type definition. */
19580 static void
19581 gen_typedef_die (tree decl, dw_die_ref context_die)
19583 dw_die_ref type_die;
19584 tree origin;
19586 if (TREE_ASM_WRITTEN (decl))
19587 return;
19589 TREE_ASM_WRITTEN (decl) = 1;
19590 type_die = new_die (DW_TAG_typedef, context_die, decl);
19591 origin = decl_ultimate_origin (decl);
19592 if (origin != NULL)
19593 add_abstract_origin_attribute (type_die, origin);
19594 else
19596 tree type;
19598 add_name_and_src_coords_attributes (type_die, decl);
19599 if (DECL_ORIGINAL_TYPE (decl))
19601 type = DECL_ORIGINAL_TYPE (decl);
19603 gcc_assert (type != TREE_TYPE (decl));
19604 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19606 else
19608 type = TREE_TYPE (decl);
19610 if (is_naming_typedef_decl (TYPE_NAME (type)))
19612 /* Here, we are in the case of decl being a typedef naming
19613 an anonymous type, e.g:
19614 typedef struct {...} foo;
19615 In that case TREE_TYPE (decl) is not a typedef variant
19616 type and TYPE_NAME of the anonymous type is set to the
19617 TYPE_DECL of the typedef. This construct is emitted by
19618 the C++ FE.
19620 TYPE is the anonymous struct named by the typedef
19621 DECL. As we need the DW_AT_type attribute of the
19622 DW_TAG_typedef to point to the DIE of TYPE, let's
19623 generate that DIE right away. add_type_attribute
19624 called below will then pick (via lookup_type_die) that
19625 anonymous struct DIE. */
19626 if (!TREE_ASM_WRITTEN (type))
19627 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19629 /* This is a GNU Extension. We are adding a
19630 DW_AT_linkage_name attribute to the DIE of the
19631 anonymous struct TYPE. The value of that attribute
19632 is the name of the typedef decl naming the anonymous
19633 struct. This greatly eases the work of consumers of
19634 this debug info. */
19635 add_linkage_attr (lookup_type_die (type), decl);
19639 add_type_attribute (type_die, type, TREE_READONLY (decl),
19640 TREE_THIS_VOLATILE (decl), context_die);
19642 if (is_naming_typedef_decl (decl))
19643 /* We want that all subsequent calls to lookup_type_die with
19644 TYPE in argument yield the DW_TAG_typedef we have just
19645 created. */
19646 equate_type_number_to_die (type, type_die);
19648 add_accessibility_attribute (type_die, decl);
19651 if (DECL_ABSTRACT (decl))
19652 equate_decl_number_to_die (decl, type_die);
19654 if (get_AT (type_die, DW_AT_name))
19655 add_pubtype (decl, type_die);
19658 /* Generate a DIE for a struct, class, enum or union type. */
19660 static void
19661 gen_tagged_type_die (tree type,
19662 dw_die_ref context_die,
19663 enum debug_info_usage usage)
19665 int need_pop;
19667 if (type == NULL_TREE
19668 || !is_tagged_type (type))
19669 return;
19671 /* If this is a nested type whose containing class hasn't been written
19672 out yet, writing it out will cover this one, too. This does not apply
19673 to instantiations of member class templates; they need to be added to
19674 the containing class as they are generated. FIXME: This hurts the
19675 idea of combining type decls from multiple TUs, since we can't predict
19676 what set of template instantiations we'll get. */
19677 if (TYPE_CONTEXT (type)
19678 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19679 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19681 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19683 if (TREE_ASM_WRITTEN (type))
19684 return;
19686 /* If that failed, attach ourselves to the stub. */
19687 push_decl_scope (TYPE_CONTEXT (type));
19688 context_die = lookup_type_die (TYPE_CONTEXT (type));
19689 need_pop = 1;
19691 else if (TYPE_CONTEXT (type) != NULL_TREE
19692 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19694 /* If this type is local to a function that hasn't been written
19695 out yet, use a NULL context for now; it will be fixed up in
19696 decls_for_scope. */
19697 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19698 /* A declaration DIE doesn't count; nested types need to go in the
19699 specification. */
19700 if (context_die && is_declaration_die (context_die))
19701 context_die = NULL;
19702 need_pop = 0;
19704 else
19706 context_die = declare_in_namespace (type, context_die);
19707 need_pop = 0;
19710 if (TREE_CODE (type) == ENUMERAL_TYPE)
19712 /* This might have been written out by the call to
19713 declare_in_namespace. */
19714 if (!TREE_ASM_WRITTEN (type))
19715 gen_enumeration_type_die (type, context_die);
19717 else
19718 gen_struct_or_union_type_die (type, context_die, usage);
19720 if (need_pop)
19721 pop_decl_scope ();
19723 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19724 it up if it is ever completed. gen_*_type_die will set it for us
19725 when appropriate. */
19728 /* Generate a type description DIE. */
19730 static void
19731 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19732 enum debug_info_usage usage)
19734 struct array_descr_info info;
19736 if (type == NULL_TREE || type == error_mark_node)
19737 return;
19739 if (TYPE_NAME (type) != NULL_TREE
19740 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19741 && is_redundant_typedef (TYPE_NAME (type))
19742 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19743 /* The DECL of this type is a typedef we don't want to emit debug
19744 info for but we want debug info for its underlying typedef.
19745 This can happen for e.g, the injected-class-name of a C++
19746 type. */
19747 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19749 /* If TYPE is a typedef type variant, let's generate debug info
19750 for the parent typedef which TYPE is a type of. */
19751 if (typedef_variant_p (type))
19753 if (TREE_ASM_WRITTEN (type))
19754 return;
19756 /* Prevent broken recursion; we can't hand off to the same type. */
19757 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19759 /* Give typedefs the right scope. */
19760 context_die = scope_die_for (type, context_die);
19762 TREE_ASM_WRITTEN (type) = 1;
19764 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19765 return;
19768 /* If type is an anonymous tagged type named by a typedef, let's
19769 generate debug info for the typedef. */
19770 if (is_naming_typedef_decl (TYPE_NAME (type)))
19772 /* Use the DIE of the containing namespace as the parent DIE of
19773 the type description DIE we want to generate. */
19774 if (DECL_CONTEXT (TYPE_NAME (type))
19775 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19776 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19778 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19779 return;
19782 /* If this is an array type with hidden descriptor, handle it first. */
19783 if (!TREE_ASM_WRITTEN (type)
19784 && lang_hooks.types.get_array_descr_info
19785 && lang_hooks.types.get_array_descr_info (type, &info)
19786 && (dwarf_version >= 3 || !dwarf_strict))
19788 gen_descr_array_type_die (type, &info, context_die);
19789 TREE_ASM_WRITTEN (type) = 1;
19790 return;
19793 /* We are going to output a DIE to represent the unqualified version
19794 of this type (i.e. without any const or volatile qualifiers) so
19795 get the main variant (i.e. the unqualified version) of this type
19796 now. (Vectors are special because the debugging info is in the
19797 cloned type itself). */
19798 if (TREE_CODE (type) != VECTOR_TYPE)
19799 type = type_main_variant (type);
19801 if (TREE_ASM_WRITTEN (type))
19802 return;
19804 switch (TREE_CODE (type))
19806 case ERROR_MARK:
19807 break;
19809 case POINTER_TYPE:
19810 case REFERENCE_TYPE:
19811 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19812 ensures that the gen_type_die recursion will terminate even if the
19813 type is recursive. Recursive types are possible in Ada. */
19814 /* ??? We could perhaps do this for all types before the switch
19815 statement. */
19816 TREE_ASM_WRITTEN (type) = 1;
19818 /* For these types, all that is required is that we output a DIE (or a
19819 set of DIEs) to represent the "basis" type. */
19820 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19821 DINFO_USAGE_IND_USE);
19822 break;
19824 case OFFSET_TYPE:
19825 /* This code is used for C++ pointer-to-data-member types.
19826 Output a description of the relevant class type. */
19827 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19828 DINFO_USAGE_IND_USE);
19830 /* Output a description of the type of the object pointed to. */
19831 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19832 DINFO_USAGE_IND_USE);
19834 /* Now output a DIE to represent this pointer-to-data-member type
19835 itself. */
19836 gen_ptr_to_mbr_type_die (type, context_die);
19837 break;
19839 case FUNCTION_TYPE:
19840 /* Force out return type (in case it wasn't forced out already). */
19841 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19842 DINFO_USAGE_DIR_USE);
19843 gen_subroutine_type_die (type, context_die);
19844 break;
19846 case METHOD_TYPE:
19847 /* Force out return type (in case it wasn't forced out already). */
19848 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19849 DINFO_USAGE_DIR_USE);
19850 gen_subroutine_type_die (type, context_die);
19851 break;
19853 case ARRAY_TYPE:
19854 gen_array_type_die (type, context_die);
19855 break;
19857 case VECTOR_TYPE:
19858 gen_array_type_die (type, context_die);
19859 break;
19861 case ENUMERAL_TYPE:
19862 case RECORD_TYPE:
19863 case UNION_TYPE:
19864 case QUAL_UNION_TYPE:
19865 gen_tagged_type_die (type, context_die, usage);
19866 return;
19868 case VOID_TYPE:
19869 case INTEGER_TYPE:
19870 case REAL_TYPE:
19871 case FIXED_POINT_TYPE:
19872 case COMPLEX_TYPE:
19873 case BOOLEAN_TYPE:
19874 /* No DIEs needed for fundamental types. */
19875 break;
19877 case NULLPTR_TYPE:
19878 case LANG_TYPE:
19879 /* Just use DW_TAG_unspecified_type. */
19881 dw_die_ref type_die = lookup_type_die (type);
19882 if (type_die == NULL)
19884 tree name = TYPE_NAME (type);
19885 if (TREE_CODE (name) == TYPE_DECL)
19886 name = DECL_NAME (name);
19887 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19888 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19889 equate_type_number_to_die (type, type_die);
19892 break;
19894 default:
19895 if (is_cxx_auto (type))
19897 if (!auto_die)
19899 auto_die = new_die (DW_TAG_unspecified_type,
19900 comp_unit_die (), NULL_TREE);
19901 add_name_attribute (auto_die, "auto");
19903 equate_type_number_to_die (type, auto_die);
19904 break;
19906 gcc_unreachable ();
19909 TREE_ASM_WRITTEN (type) = 1;
19912 static void
19913 gen_type_die (tree type, dw_die_ref context_die)
19915 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19918 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19919 things which are local to the given block. */
19921 static void
19922 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19924 int must_output_die = 0;
19925 bool inlined_func;
19927 /* Ignore blocks that are NULL. */
19928 if (stmt == NULL_TREE)
19929 return;
19931 inlined_func = inlined_function_outer_scope_p (stmt);
19933 /* If the block is one fragment of a non-contiguous block, do not
19934 process the variables, since they will have been done by the
19935 origin block. Do process subblocks. */
19936 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19938 tree sub;
19940 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19941 gen_block_die (sub, context_die, depth + 1);
19943 return;
19946 /* Determine if we need to output any Dwarf DIEs at all to represent this
19947 block. */
19948 if (inlined_func)
19949 /* The outer scopes for inlinings *must* always be represented. We
19950 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19951 must_output_die = 1;
19952 else
19954 /* Determine if this block directly contains any "significant"
19955 local declarations which we will need to output DIEs for. */
19956 if (debug_info_level > DINFO_LEVEL_TERSE)
19957 /* We are not in terse mode so *any* local declaration counts
19958 as being a "significant" one. */
19959 must_output_die = ((BLOCK_VARS (stmt) != NULL
19960 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19961 && (TREE_USED (stmt)
19962 || TREE_ASM_WRITTEN (stmt)
19963 || BLOCK_ABSTRACT (stmt)));
19964 else if ((TREE_USED (stmt)
19965 || TREE_ASM_WRITTEN (stmt)
19966 || BLOCK_ABSTRACT (stmt))
19967 && !dwarf2out_ignore_block (stmt))
19968 must_output_die = 1;
19971 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19972 DIE for any block which contains no significant local declarations at
19973 all. Rather, in such cases we just call `decls_for_scope' so that any
19974 needed Dwarf info for any sub-blocks will get properly generated. Note
19975 that in terse mode, our definition of what constitutes a "significant"
19976 local declaration gets restricted to include only inlined function
19977 instances and local (nested) function definitions. */
19978 if (must_output_die)
19980 if (inlined_func)
19982 /* If STMT block is abstract, that means we have been called
19983 indirectly from dwarf2out_abstract_function.
19984 That function rightfully marks the descendent blocks (of
19985 the abstract function it is dealing with) as being abstract,
19986 precisely to prevent us from emitting any
19987 DW_TAG_inlined_subroutine DIE as a descendent
19988 of an abstract function instance. So in that case, we should
19989 not call gen_inlined_subroutine_die.
19991 Later though, when cgraph asks dwarf2out to emit info
19992 for the concrete instance of the function decl into which
19993 the concrete instance of STMT got inlined, the later will lead
19994 to the generation of a DW_TAG_inlined_subroutine DIE. */
19995 if (! BLOCK_ABSTRACT (stmt))
19996 gen_inlined_subroutine_die (stmt, context_die, depth);
19998 else
19999 gen_lexical_block_die (stmt, context_die, depth);
20001 else
20002 decls_for_scope (stmt, context_die, depth);
20005 /* Process variable DECL (or variable with origin ORIGIN) within
20006 block STMT and add it to CONTEXT_DIE. */
20007 static void
20008 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20010 dw_die_ref die;
20011 tree decl_or_origin = decl ? decl : origin;
20013 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20014 die = lookup_decl_die (decl_or_origin);
20015 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20016 && TYPE_DECL_IS_STUB (decl_or_origin))
20017 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20018 else
20019 die = NULL;
20021 if (die != NULL && die->die_parent == NULL)
20022 add_child_die (context_die, die);
20023 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20024 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20025 stmt, context_die);
20026 else
20027 gen_decl_die (decl, origin, context_die);
20030 /* Generate all of the decls declared within a given scope and (recursively)
20031 all of its sub-blocks. */
20033 static void
20034 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20036 tree decl;
20037 unsigned int i;
20038 tree subblocks;
20040 /* Ignore NULL blocks. */
20041 if (stmt == NULL_TREE)
20042 return;
20044 /* Output the DIEs to represent all of the data objects and typedefs
20045 declared directly within this block but not within any nested
20046 sub-blocks. Also, nested function and tag DIEs have been
20047 generated with a parent of NULL; fix that up now. We don't
20048 have to do this if we're at -g1. */
20049 if (debug_info_level > DINFO_LEVEL_TERSE)
20051 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20052 process_scope_var (stmt, decl, NULL_TREE, context_die);
20053 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20054 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20055 context_die);
20058 /* Even if we're at -g1, we need to process the subblocks in order to get
20059 inlined call information. */
20061 /* Output the DIEs to represent all sub-blocks (and the items declared
20062 therein) of this block. */
20063 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20064 subblocks != NULL;
20065 subblocks = BLOCK_CHAIN (subblocks))
20066 gen_block_die (subblocks, context_die, depth + 1);
20069 /* Is this a typedef we can avoid emitting? */
20071 static inline int
20072 is_redundant_typedef (const_tree decl)
20074 if (TYPE_DECL_IS_STUB (decl))
20075 return 1;
20077 if (DECL_ARTIFICIAL (decl)
20078 && DECL_CONTEXT (decl)
20079 && is_tagged_type (DECL_CONTEXT (decl))
20080 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20081 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20082 /* Also ignore the artificial member typedef for the class name. */
20083 return 1;
20085 return 0;
20088 /* Return TRUE if TYPE is a typedef that names a type for linkage
20089 purposes. This kind of typedefs is produced by the C++ FE for
20090 constructs like:
20092 typedef struct {...} foo;
20094 In that case, there is no typedef variant type produced for foo.
20095 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20096 struct type. */
20098 static bool
20099 is_naming_typedef_decl (const_tree decl)
20101 if (decl == NULL_TREE
20102 || TREE_CODE (decl) != TYPE_DECL
20103 || !is_tagged_type (TREE_TYPE (decl))
20104 || DECL_IS_BUILTIN (decl)
20105 || is_redundant_typedef (decl)
20106 /* It looks like Ada produces TYPE_DECLs that are very similar
20107 to C++ naming typedefs but that have different
20108 semantics. Let's be specific to c++ for now. */
20109 || !is_cxx ())
20110 return FALSE;
20112 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20113 && TYPE_NAME (TREE_TYPE (decl)) == decl
20114 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20115 != TYPE_NAME (TREE_TYPE (decl))));
20118 /* Returns the DIE for a context. */
20120 static inline dw_die_ref
20121 get_context_die (tree context)
20123 if (context)
20125 /* Find die that represents this context. */
20126 if (TYPE_P (context))
20128 context = TYPE_MAIN_VARIANT (context);
20129 return strip_naming_typedef (context, force_type_die (context));
20131 else
20132 return force_decl_die (context);
20134 return comp_unit_die ();
20137 /* Returns the DIE for decl. A DIE will always be returned. */
20139 static dw_die_ref
20140 force_decl_die (tree decl)
20142 dw_die_ref decl_die;
20143 unsigned saved_external_flag;
20144 tree save_fn = NULL_TREE;
20145 decl_die = lookup_decl_die (decl);
20146 if (!decl_die)
20148 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20150 decl_die = lookup_decl_die (decl);
20151 if (decl_die)
20152 return decl_die;
20154 switch (TREE_CODE (decl))
20156 case FUNCTION_DECL:
20157 /* Clear current_function_decl, so that gen_subprogram_die thinks
20158 that this is a declaration. At this point, we just want to force
20159 declaration die. */
20160 save_fn = current_function_decl;
20161 current_function_decl = NULL_TREE;
20162 gen_subprogram_die (decl, context_die);
20163 current_function_decl = save_fn;
20164 break;
20166 case VAR_DECL:
20167 /* Set external flag to force declaration die. Restore it after
20168 gen_decl_die() call. */
20169 saved_external_flag = DECL_EXTERNAL (decl);
20170 DECL_EXTERNAL (decl) = 1;
20171 gen_decl_die (decl, NULL, context_die);
20172 DECL_EXTERNAL (decl) = saved_external_flag;
20173 break;
20175 case NAMESPACE_DECL:
20176 if (dwarf_version >= 3 || !dwarf_strict)
20177 dwarf2out_decl (decl);
20178 else
20179 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20180 decl_die = comp_unit_die ();
20181 break;
20183 case TRANSLATION_UNIT_DECL:
20184 decl_die = comp_unit_die ();
20185 break;
20187 default:
20188 gcc_unreachable ();
20191 /* We should be able to find the DIE now. */
20192 if (!decl_die)
20193 decl_die = lookup_decl_die (decl);
20194 gcc_assert (decl_die);
20197 return decl_die;
20200 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20201 always returned. */
20203 static dw_die_ref
20204 force_type_die (tree type)
20206 dw_die_ref type_die;
20208 type_die = lookup_type_die (type);
20209 if (!type_die)
20211 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20213 type_die = modified_type_die (type, TYPE_QUALS (type), context_die);
20214 gcc_assert (type_die);
20216 return type_die;
20219 /* Force out any required namespaces to be able to output DECL,
20220 and return the new context_die for it, if it's changed. */
20222 static dw_die_ref
20223 setup_namespace_context (tree thing, dw_die_ref context_die)
20225 tree context = (DECL_P (thing)
20226 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20227 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20228 /* Force out the namespace. */
20229 context_die = force_decl_die (context);
20231 return context_die;
20234 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20235 type) within its namespace, if appropriate.
20237 For compatibility with older debuggers, namespace DIEs only contain
20238 declarations; all definitions are emitted at CU scope. */
20240 static dw_die_ref
20241 declare_in_namespace (tree thing, dw_die_ref context_die)
20243 dw_die_ref ns_context;
20245 if (debug_info_level <= DINFO_LEVEL_TERSE)
20246 return context_die;
20248 /* If this decl is from an inlined function, then don't try to emit it in its
20249 namespace, as we will get confused. It would have already been emitted
20250 when the abstract instance of the inline function was emitted anyways. */
20251 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20252 return context_die;
20254 ns_context = setup_namespace_context (thing, context_die);
20256 if (ns_context != context_die)
20258 if (is_fortran ())
20259 return ns_context;
20260 if (DECL_P (thing))
20261 gen_decl_die (thing, NULL, ns_context);
20262 else
20263 gen_type_die (thing, ns_context);
20265 return context_die;
20268 /* Generate a DIE for a namespace or namespace alias. */
20270 static void
20271 gen_namespace_die (tree decl, dw_die_ref context_die)
20273 dw_die_ref namespace_die;
20275 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20276 they are an alias of. */
20277 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20279 /* Output a real namespace or module. */
20280 context_die = setup_namespace_context (decl, comp_unit_die ());
20281 namespace_die = new_die (is_fortran ()
20282 ? DW_TAG_module : DW_TAG_namespace,
20283 context_die, decl);
20284 /* For Fortran modules defined in different CU don't add src coords. */
20285 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20287 const char *name = dwarf2_name (decl, 0);
20288 if (name)
20289 add_name_attribute (namespace_die, name);
20291 else
20292 add_name_and_src_coords_attributes (namespace_die, decl);
20293 if (DECL_EXTERNAL (decl))
20294 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20295 equate_decl_number_to_die (decl, namespace_die);
20297 else
20299 /* Output a namespace alias. */
20301 /* Force out the namespace we are an alias of, if necessary. */
20302 dw_die_ref origin_die
20303 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20305 if (DECL_FILE_SCOPE_P (decl)
20306 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20307 context_die = setup_namespace_context (decl, comp_unit_die ());
20308 /* Now create the namespace alias DIE. */
20309 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20310 add_name_and_src_coords_attributes (namespace_die, decl);
20311 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20312 equate_decl_number_to_die (decl, namespace_die);
20314 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20315 if (want_pubnames ())
20316 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20319 /* Generate Dwarf debug information for a decl described by DECL.
20320 The return value is currently only meaningful for PARM_DECLs,
20321 for all other decls it returns NULL. */
20323 static dw_die_ref
20324 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20326 tree decl_or_origin = decl ? decl : origin;
20327 tree class_origin = NULL, ultimate_origin;
20329 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20330 return NULL;
20332 switch (TREE_CODE (decl_or_origin))
20334 case ERROR_MARK:
20335 break;
20337 case CONST_DECL:
20338 if (!is_fortran () && !is_ada ())
20340 /* The individual enumerators of an enum type get output when we output
20341 the Dwarf representation of the relevant enum type itself. */
20342 break;
20345 /* Emit its type. */
20346 gen_type_die (TREE_TYPE (decl), context_die);
20348 /* And its containing namespace. */
20349 context_die = declare_in_namespace (decl, context_die);
20351 gen_const_die (decl, context_die);
20352 break;
20354 case FUNCTION_DECL:
20355 /* Don't output any DIEs to represent mere function declarations,
20356 unless they are class members or explicit block externs. */
20357 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20358 && DECL_FILE_SCOPE_P (decl_or_origin)
20359 && (current_function_decl == NULL_TREE
20360 || DECL_ARTIFICIAL (decl_or_origin)))
20361 break;
20363 #if 0
20364 /* FIXME */
20365 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20366 on local redeclarations of global functions. That seems broken. */
20367 if (current_function_decl != decl)
20368 /* This is only a declaration. */;
20369 #endif
20371 /* If we're emitting a clone, emit info for the abstract instance. */
20372 if (origin || DECL_ORIGIN (decl) != decl)
20373 dwarf2out_abstract_function (origin
20374 ? DECL_ORIGIN (origin)
20375 : DECL_ABSTRACT_ORIGIN (decl));
20377 /* If we're emitting an out-of-line copy of an inline function,
20378 emit info for the abstract instance and set up to refer to it. */
20379 else if (cgraph_function_possibly_inlined_p (decl)
20380 && ! DECL_ABSTRACT (decl)
20381 && ! class_or_namespace_scope_p (context_die)
20382 /* dwarf2out_abstract_function won't emit a die if this is just
20383 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20384 that case, because that works only if we have a die. */
20385 && DECL_INITIAL (decl) != NULL_TREE)
20387 dwarf2out_abstract_function (decl);
20388 set_decl_origin_self (decl);
20391 /* Otherwise we're emitting the primary DIE for this decl. */
20392 else if (debug_info_level > DINFO_LEVEL_TERSE)
20394 /* Before we describe the FUNCTION_DECL itself, make sure that we
20395 have its containing type. */
20396 if (!origin)
20397 origin = decl_class_context (decl);
20398 if (origin != NULL_TREE)
20399 gen_type_die (origin, context_die);
20401 /* And its return type. */
20402 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20404 /* And its virtual context. */
20405 if (DECL_VINDEX (decl) != NULL_TREE)
20406 gen_type_die (DECL_CONTEXT (decl), context_die);
20408 /* Make sure we have a member DIE for decl. */
20409 if (origin != NULL_TREE)
20410 gen_type_die_for_member (origin, decl, context_die);
20412 /* And its containing namespace. */
20413 context_die = declare_in_namespace (decl, context_die);
20416 /* Now output a DIE to represent the function itself. */
20417 if (decl)
20418 gen_subprogram_die (decl, context_die);
20419 break;
20421 case TYPE_DECL:
20422 /* If we are in terse mode, don't generate any DIEs to represent any
20423 actual typedefs. */
20424 if (debug_info_level <= DINFO_LEVEL_TERSE)
20425 break;
20427 /* In the special case of a TYPE_DECL node representing the declaration
20428 of some type tag, if the given TYPE_DECL is marked as having been
20429 instantiated from some other (original) TYPE_DECL node (e.g. one which
20430 was generated within the original definition of an inline function) we
20431 used to generate a special (abbreviated) DW_TAG_structure_type,
20432 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20433 should be actually referencing those DIEs, as variable DIEs with that
20434 type would be emitted already in the abstract origin, so it was always
20435 removed during unused type prunning. Don't add anything in this
20436 case. */
20437 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20438 break;
20440 if (is_redundant_typedef (decl))
20441 gen_type_die (TREE_TYPE (decl), context_die);
20442 else
20443 /* Output a DIE to represent the typedef itself. */
20444 gen_typedef_die (decl, context_die);
20445 break;
20447 case LABEL_DECL:
20448 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20449 gen_label_die (decl, context_die);
20450 break;
20452 case VAR_DECL:
20453 case RESULT_DECL:
20454 /* If we are in terse mode, don't generate any DIEs to represent any
20455 variable declarations or definitions. */
20456 if (debug_info_level <= DINFO_LEVEL_TERSE)
20457 break;
20459 /* Output any DIEs that are needed to specify the type of this data
20460 object. */
20461 if (decl_by_reference_p (decl_or_origin))
20462 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20463 else
20464 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20466 /* And its containing type. */
20467 class_origin = decl_class_context (decl_or_origin);
20468 if (class_origin != NULL_TREE)
20469 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20471 /* And its containing namespace. */
20472 context_die = declare_in_namespace (decl_or_origin, context_die);
20474 /* Now output the DIE to represent the data object itself. This gets
20475 complicated because of the possibility that the VAR_DECL really
20476 represents an inlined instance of a formal parameter for an inline
20477 function. */
20478 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20479 if (ultimate_origin != NULL_TREE
20480 && TREE_CODE (ultimate_origin) == PARM_DECL)
20481 gen_formal_parameter_die (decl, origin,
20482 true /* Emit name attribute. */,
20483 context_die);
20484 else
20485 gen_variable_die (decl, origin, context_die);
20486 break;
20488 case FIELD_DECL:
20489 /* Ignore the nameless fields that are used to skip bits but handle C++
20490 anonymous unions and structs. */
20491 if (DECL_NAME (decl) != NULL_TREE
20492 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20493 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20495 gen_type_die (member_declared_type (decl), context_die);
20496 gen_field_die (decl, context_die);
20498 break;
20500 case PARM_DECL:
20501 if (DECL_BY_REFERENCE (decl_or_origin))
20502 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20503 else
20504 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20505 return gen_formal_parameter_die (decl, origin,
20506 true /* Emit name attribute. */,
20507 context_die);
20509 case NAMESPACE_DECL:
20510 case IMPORTED_DECL:
20511 if (dwarf_version >= 3 || !dwarf_strict)
20512 gen_namespace_die (decl, context_die);
20513 break;
20515 case NAMELIST_DECL:
20516 gen_namelist_decl (DECL_NAME (decl), context_die,
20517 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20518 break;
20520 default:
20521 /* Probably some frontend-internal decl. Assume we don't care. */
20522 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20523 break;
20526 return NULL;
20529 /* Output debug information for global decl DECL. Called from toplev.c after
20530 compilation proper has finished. */
20532 static void
20533 dwarf2out_global_decl (tree decl)
20535 /* Output DWARF2 information for file-scope tentative data object
20536 declarations, file-scope (extern) function declarations (which
20537 had no corresponding body) and file-scope tagged type declarations
20538 and definitions which have not yet been forced out. */
20539 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20540 dwarf2out_decl (decl);
20543 /* Output debug information for type decl DECL. Called from toplev.c
20544 and from language front ends (to record built-in types). */
20545 static void
20546 dwarf2out_type_decl (tree decl, int local)
20548 if (!local)
20549 dwarf2out_decl (decl);
20552 /* Output debug information for imported module or decl DECL.
20553 NAME is non-NULL name in the lexical block if the decl has been renamed.
20554 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20555 that DECL belongs to.
20556 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20557 static void
20558 dwarf2out_imported_module_or_decl_1 (tree decl,
20559 tree name,
20560 tree lexical_block,
20561 dw_die_ref lexical_block_die)
20563 expanded_location xloc;
20564 dw_die_ref imported_die = NULL;
20565 dw_die_ref at_import_die;
20567 if (TREE_CODE (decl) == IMPORTED_DECL)
20569 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20570 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20571 gcc_assert (decl);
20573 else
20574 xloc = expand_location (input_location);
20576 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20578 at_import_die = force_type_die (TREE_TYPE (decl));
20579 /* For namespace N { typedef void T; } using N::T; base_type_die
20580 returns NULL, but DW_TAG_imported_declaration requires
20581 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20582 if (!at_import_die)
20584 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20585 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20586 at_import_die = lookup_type_die (TREE_TYPE (decl));
20587 gcc_assert (at_import_die);
20590 else
20592 at_import_die = lookup_decl_die (decl);
20593 if (!at_import_die)
20595 /* If we're trying to avoid duplicate debug info, we may not have
20596 emitted the member decl for this field. Emit it now. */
20597 if (TREE_CODE (decl) == FIELD_DECL)
20599 tree type = DECL_CONTEXT (decl);
20601 if (TYPE_CONTEXT (type)
20602 && TYPE_P (TYPE_CONTEXT (type))
20603 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20604 DINFO_USAGE_DIR_USE))
20605 return;
20606 gen_type_die_for_member (type, decl,
20607 get_context_die (TYPE_CONTEXT (type)));
20609 if (TREE_CODE (decl) == NAMELIST_DECL)
20610 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20611 get_context_die (DECL_CONTEXT (decl)),
20612 NULL_TREE);
20613 else
20614 at_import_die = force_decl_die (decl);
20618 if (TREE_CODE (decl) == NAMESPACE_DECL)
20620 if (dwarf_version >= 3 || !dwarf_strict)
20621 imported_die = new_die (DW_TAG_imported_module,
20622 lexical_block_die,
20623 lexical_block);
20624 else
20625 return;
20627 else
20628 imported_die = new_die (DW_TAG_imported_declaration,
20629 lexical_block_die,
20630 lexical_block);
20632 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20633 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20634 if (name)
20635 add_AT_string (imported_die, DW_AT_name,
20636 IDENTIFIER_POINTER (name));
20637 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20640 /* Output debug information for imported module or decl DECL.
20641 NAME is non-NULL name in context if the decl has been renamed.
20642 CHILD is true if decl is one of the renamed decls as part of
20643 importing whole module. */
20645 static void
20646 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20647 bool child)
20649 /* dw_die_ref at_import_die; */
20650 dw_die_ref scope_die;
20652 if (debug_info_level <= DINFO_LEVEL_TERSE)
20653 return;
20655 gcc_assert (decl);
20657 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20658 We need decl DIE for reference and scope die. First, get DIE for the decl
20659 itself. */
20661 /* Get the scope die for decl context. Use comp_unit_die for global module
20662 or decl. If die is not found for non globals, force new die. */
20663 if (context
20664 && TYPE_P (context)
20665 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20666 return;
20668 if (!(dwarf_version >= 3 || !dwarf_strict))
20669 return;
20671 scope_die = get_context_die (context);
20673 if (child)
20675 gcc_assert (scope_die->die_child);
20676 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20677 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20678 scope_die = scope_die->die_child;
20681 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20682 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20686 /* Output debug information for namelists. */
20688 static dw_die_ref
20689 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20691 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20692 tree value;
20693 unsigned i;
20695 if (debug_info_level <= DINFO_LEVEL_TERSE)
20696 return NULL;
20698 gcc_assert (scope_die != NULL);
20699 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20700 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20702 /* If there are no item_decls, we have a nondefining namelist, e.g.
20703 with USE association; hence, set DW_AT_declaration. */
20704 if (item_decls == NULL_TREE)
20706 add_AT_flag (nml_die, DW_AT_declaration, 1);
20707 return nml_die;
20710 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20712 nml_item_ref_die = lookup_decl_die (value);
20713 if (!nml_item_ref_die)
20714 nml_item_ref_die = force_decl_die (value);
20716 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20717 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20719 return nml_die;
20723 /* Write the debugging output for DECL. */
20725 void
20726 dwarf2out_decl (tree decl)
20728 dw_die_ref context_die = comp_unit_die ();
20730 switch (TREE_CODE (decl))
20732 case ERROR_MARK:
20733 return;
20735 case FUNCTION_DECL:
20736 /* What we would really like to do here is to filter out all mere
20737 file-scope declarations of file-scope functions which are never
20738 referenced later within this translation unit (and keep all of ones
20739 that *are* referenced later on) but we aren't clairvoyant, so we have
20740 no idea which functions will be referenced in the future (i.e. later
20741 on within the current translation unit). So here we just ignore all
20742 file-scope function declarations which are not also definitions. If
20743 and when the debugger needs to know something about these functions,
20744 it will have to hunt around and find the DWARF information associated
20745 with the definition of the function.
20747 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20748 nodes represent definitions and which ones represent mere
20749 declarations. We have to check DECL_INITIAL instead. That's because
20750 the C front-end supports some weird semantics for "extern inline"
20751 function definitions. These can get inlined within the current
20752 translation unit (and thus, we need to generate Dwarf info for their
20753 abstract instances so that the Dwarf info for the concrete inlined
20754 instances can have something to refer to) but the compiler never
20755 generates any out-of-lines instances of such things (despite the fact
20756 that they *are* definitions).
20758 The important point is that the C front-end marks these "extern
20759 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20760 them anyway. Note that the C++ front-end also plays some similar games
20761 for inline function definitions appearing within include files which
20762 also contain `#pragma interface' pragmas.
20764 If we are called from dwarf2out_abstract_function output a DIE
20765 anyway. We can end up here this way with early inlining and LTO
20766 where the inlined function is output in a different LTRANS unit
20767 or not at all. */
20768 if (DECL_INITIAL (decl) == NULL_TREE
20769 && ! DECL_ABSTRACT (decl))
20770 return;
20772 /* If we're a nested function, initially use a parent of NULL; if we're
20773 a plain function, this will be fixed up in decls_for_scope. If
20774 we're a method, it will be ignored, since we already have a DIE. */
20775 if (decl_function_context (decl)
20776 /* But if we're in terse mode, we don't care about scope. */
20777 && debug_info_level > DINFO_LEVEL_TERSE)
20778 context_die = NULL;
20779 break;
20781 case VAR_DECL:
20782 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20783 declaration and if the declaration was never even referenced from
20784 within this entire compilation unit. We suppress these DIEs in
20785 order to save space in the .debug section (by eliminating entries
20786 which are probably useless). Note that we must not suppress
20787 block-local extern declarations (whether used or not) because that
20788 would screw-up the debugger's name lookup mechanism and cause it to
20789 miss things which really ought to be in scope at a given point. */
20790 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20791 return;
20793 /* For local statics lookup proper context die. */
20794 if (TREE_STATIC (decl)
20795 && DECL_CONTEXT (decl)
20796 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20797 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20799 /* If we are in terse mode, don't generate any DIEs to represent any
20800 variable declarations or definitions. */
20801 if (debug_info_level <= DINFO_LEVEL_TERSE)
20802 return;
20803 break;
20805 case CONST_DECL:
20806 if (debug_info_level <= DINFO_LEVEL_TERSE)
20807 return;
20808 if (!is_fortran () && !is_ada ())
20809 return;
20810 if (TREE_STATIC (decl) && decl_function_context (decl))
20811 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20812 break;
20814 case NAMESPACE_DECL:
20815 case IMPORTED_DECL:
20816 if (debug_info_level <= DINFO_LEVEL_TERSE)
20817 return;
20818 if (lookup_decl_die (decl) != NULL)
20819 return;
20820 break;
20822 case TYPE_DECL:
20823 /* Don't emit stubs for types unless they are needed by other DIEs. */
20824 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20825 return;
20827 /* Don't bother trying to generate any DIEs to represent any of the
20828 normal built-in types for the language we are compiling. */
20829 if (DECL_IS_BUILTIN (decl))
20830 return;
20832 /* If we are in terse mode, don't generate any DIEs for types. */
20833 if (debug_info_level <= DINFO_LEVEL_TERSE)
20834 return;
20836 /* If we're a function-scope tag, initially use a parent of NULL;
20837 this will be fixed up in decls_for_scope. */
20838 if (decl_function_context (decl))
20839 context_die = NULL;
20841 break;
20843 case NAMELIST_DECL:
20844 break;
20846 default:
20847 return;
20850 gen_decl_die (decl, NULL, context_die);
20853 /* Write the debugging output for DECL. */
20855 static void
20856 dwarf2out_function_decl (tree decl)
20858 dwarf2out_decl (decl);
20859 call_arg_locations = NULL;
20860 call_arg_loc_last = NULL;
20861 call_site_count = -1;
20862 tail_call_site_count = -1;
20863 block_map.release ();
20864 htab_empty (decl_loc_table);
20865 htab_empty (cached_dw_loc_list_table);
20868 /* Output a marker (i.e. a label) for the beginning of the generated code for
20869 a lexical block. */
20871 static void
20872 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20873 unsigned int blocknum)
20875 switch_to_section (current_function_section ());
20876 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20879 /* Output a marker (i.e. a label) for the end of the generated code for a
20880 lexical block. */
20882 static void
20883 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20885 switch_to_section (current_function_section ());
20886 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20889 /* Returns nonzero if it is appropriate not to emit any debugging
20890 information for BLOCK, because it doesn't contain any instructions.
20892 Don't allow this for blocks with nested functions or local classes
20893 as we would end up with orphans, and in the presence of scheduling
20894 we may end up calling them anyway. */
20896 static bool
20897 dwarf2out_ignore_block (const_tree block)
20899 tree decl;
20900 unsigned int i;
20902 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20903 if (TREE_CODE (decl) == FUNCTION_DECL
20904 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20905 return 0;
20906 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20908 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20909 if (TREE_CODE (decl) == FUNCTION_DECL
20910 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20911 return 0;
20914 return 1;
20917 /* Hash table routines for file_hash. */
20919 static int
20920 file_table_eq (const void *p1_p, const void *p2_p)
20922 const struct dwarf_file_data *const p1 =
20923 (const struct dwarf_file_data *) p1_p;
20924 const char *const p2 = (const char *) p2_p;
20925 return filename_cmp (p1->filename, p2) == 0;
20928 static hashval_t
20929 file_table_hash (const void *p_p)
20931 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20932 return htab_hash_string (p->filename);
20935 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20936 dwarf2out.c) and return its "index". The index of each (known) filename is
20937 just a unique number which is associated with only that one filename. We
20938 need such numbers for the sake of generating labels (in the .debug_sfnames
20939 section) and references to those files numbers (in the .debug_srcinfo
20940 and.debug_macinfo sections). If the filename given as an argument is not
20941 found in our current list, add it to the list and assign it the next
20942 available unique index number. In order to speed up searches, we remember
20943 the index of the filename was looked up last. This handles the majority of
20944 all searches. */
20946 static struct dwarf_file_data *
20947 lookup_filename (const char *file_name)
20949 void ** slot;
20950 struct dwarf_file_data * created;
20952 /* Check to see if the file name that was searched on the previous
20953 call matches this file name. If so, return the index. */
20954 if (file_table_last_lookup
20955 && (file_name == file_table_last_lookup->filename
20956 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20957 return file_table_last_lookup;
20959 /* Didn't match the previous lookup, search the table. */
20960 slot = htab_find_slot_with_hash (file_table, file_name,
20961 htab_hash_string (file_name), INSERT);
20962 if (*slot)
20963 return (struct dwarf_file_data *) *slot;
20965 created = ggc_alloc_dwarf_file_data ();
20966 created->filename = file_name;
20967 created->emitted_number = 0;
20968 *slot = created;
20969 return created;
20972 /* If the assembler will construct the file table, then translate the compiler
20973 internal file table number into the assembler file table number, and emit
20974 a .file directive if we haven't already emitted one yet. The file table
20975 numbers are different because we prune debug info for unused variables and
20976 types, which may include filenames. */
20978 static int
20979 maybe_emit_file (struct dwarf_file_data * fd)
20981 if (! fd->emitted_number)
20983 if (last_emitted_file)
20984 fd->emitted_number = last_emitted_file->emitted_number + 1;
20985 else
20986 fd->emitted_number = 1;
20987 last_emitted_file = fd;
20989 if (DWARF2_ASM_LINE_DEBUG_INFO)
20991 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20992 output_quoted_string (asm_out_file,
20993 remap_debug_filename (fd->filename));
20994 fputc ('\n', asm_out_file);
20998 return fd->emitted_number;
21001 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21002 That generation should happen after function debug info has been
21003 generated. The value of the attribute is the constant value of ARG. */
21005 static void
21006 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21008 die_arg_entry entry;
21010 if (!die || !arg)
21011 return;
21013 if (!tmpl_value_parm_die_table)
21014 vec_alloc (tmpl_value_parm_die_table, 32);
21016 entry.die = die;
21017 entry.arg = arg;
21018 vec_safe_push (tmpl_value_parm_die_table, entry);
21021 /* Return TRUE if T is an instance of generic type, FALSE
21022 otherwise. */
21024 static bool
21025 generic_type_p (tree t)
21027 if (t == NULL_TREE || !TYPE_P (t))
21028 return false;
21029 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21032 /* Schedule the generation of the generic parameter dies for the
21033 instance of generic type T. The proper generation itself is later
21034 done by gen_scheduled_generic_parms_dies. */
21036 static void
21037 schedule_generic_params_dies_gen (tree t)
21039 if (!generic_type_p (t))
21040 return;
21042 if (!generic_type_instances)
21043 vec_alloc (generic_type_instances, 256);
21045 vec_safe_push (generic_type_instances, t);
21048 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21049 by append_entry_to_tmpl_value_parm_die_table. This function must
21050 be called after function DIEs have been generated. */
21052 static void
21053 gen_remaining_tmpl_value_param_die_attribute (void)
21055 if (tmpl_value_parm_die_table)
21057 unsigned i;
21058 die_arg_entry *e;
21060 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21061 tree_add_const_value_attribute (e->die, e->arg);
21065 /* Generate generic parameters DIEs for instances of generic types
21066 that have been previously scheduled by
21067 schedule_generic_params_dies_gen. This function must be called
21068 after all the types of the CU have been laid out. */
21070 static void
21071 gen_scheduled_generic_parms_dies (void)
21073 unsigned i;
21074 tree t;
21076 if (!generic_type_instances)
21077 return;
21079 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21080 if (COMPLETE_TYPE_P (t))
21081 gen_generic_params_dies (t);
21085 /* Replace DW_AT_name for the decl with name. */
21087 static void
21088 dwarf2out_set_name (tree decl, tree name)
21090 dw_die_ref die;
21091 dw_attr_ref attr;
21092 const char *dname;
21094 die = TYPE_SYMTAB_DIE (decl);
21095 if (!die)
21096 return;
21098 dname = dwarf2_name (name, 0);
21099 if (!dname)
21100 return;
21102 attr = get_AT (die, DW_AT_name);
21103 if (attr)
21105 struct indirect_string_node *node;
21107 node = find_AT_string (dname);
21108 /* replace the string. */
21109 attr->dw_attr_val.v.val_str = node;
21112 else
21113 add_name_attribute (die, dname);
21116 /* True if before or during processing of the first function being emitted. */
21117 static bool in_first_function_p = true;
21118 /* True if loc_note during dwarf2out_var_location call might still be
21119 before first real instruction at address equal to .Ltext0. */
21120 static bool maybe_at_text_label_p = true;
21121 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21122 static unsigned int first_loclabel_num_not_at_text_label;
21124 /* Called by the final INSN scan whenever we see a var location. We
21125 use it to drop labels in the right places, and throw the location in
21126 our lookup table. */
21128 static void
21129 dwarf2out_var_location (rtx loc_note)
21131 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21132 struct var_loc_node *newloc;
21133 rtx next_real, next_note;
21134 static const char *last_label;
21135 static const char *last_postcall_label;
21136 static bool last_in_cold_section_p;
21137 static rtx expected_next_loc_note;
21138 tree decl;
21139 bool var_loc_p;
21141 if (!NOTE_P (loc_note))
21143 if (CALL_P (loc_note))
21145 call_site_count++;
21146 if (SIBLING_CALL_P (loc_note))
21147 tail_call_site_count++;
21149 return;
21152 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21153 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21154 return;
21156 /* Optimize processing a large consecutive sequence of location
21157 notes so we don't spend too much time in next_real_insn. If the
21158 next insn is another location note, remember the next_real_insn
21159 calculation for next time. */
21160 next_real = cached_next_real_insn;
21161 if (next_real)
21163 if (expected_next_loc_note != loc_note)
21164 next_real = NULL_RTX;
21167 next_note = NEXT_INSN (loc_note);
21168 if (! next_note
21169 || INSN_DELETED_P (next_note)
21170 || ! NOTE_P (next_note)
21171 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21172 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21173 next_note = NULL_RTX;
21175 if (! next_real)
21176 next_real = next_real_insn (loc_note);
21178 if (next_note)
21180 expected_next_loc_note = next_note;
21181 cached_next_real_insn = next_real;
21183 else
21184 cached_next_real_insn = NULL_RTX;
21186 /* If there are no instructions which would be affected by this note,
21187 don't do anything. */
21188 if (var_loc_p
21189 && next_real == NULL_RTX
21190 && !NOTE_DURING_CALL_P (loc_note))
21191 return;
21193 if (next_real == NULL_RTX)
21194 next_real = get_last_insn ();
21196 /* If there were any real insns between note we processed last time
21197 and this note (or if it is the first note), clear
21198 last_{,postcall_}label so that they are not reused this time. */
21199 if (last_var_location_insn == NULL_RTX
21200 || last_var_location_insn != next_real
21201 || last_in_cold_section_p != in_cold_section_p)
21203 last_label = NULL;
21204 last_postcall_label = NULL;
21207 if (var_loc_p)
21209 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21210 newloc = add_var_loc_to_decl (decl, loc_note,
21211 NOTE_DURING_CALL_P (loc_note)
21212 ? last_postcall_label : last_label);
21213 if (newloc == NULL)
21214 return;
21216 else
21218 decl = NULL_TREE;
21219 newloc = NULL;
21222 /* If there were no real insns between note we processed last time
21223 and this note, use the label we emitted last time. Otherwise
21224 create a new label and emit it. */
21225 if (last_label == NULL)
21227 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21228 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21229 loclabel_num++;
21230 last_label = ggc_strdup (loclabel);
21231 /* See if loclabel might be equal to .Ltext0. If yes,
21232 bump first_loclabel_num_not_at_text_label. */
21233 if (!have_multiple_function_sections
21234 && in_first_function_p
21235 && maybe_at_text_label_p)
21237 static rtx last_start;
21238 rtx insn;
21239 for (insn = loc_note; insn; insn = previous_insn (insn))
21240 if (insn == last_start)
21241 break;
21242 else if (!NONDEBUG_INSN_P (insn))
21243 continue;
21244 else
21246 rtx body = PATTERN (insn);
21247 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21248 continue;
21249 /* Inline asm could occupy zero bytes. */
21250 else if (GET_CODE (body) == ASM_INPUT
21251 || asm_noperands (body) >= 0)
21252 continue;
21253 #ifdef HAVE_attr_length
21254 else if (get_attr_min_length (insn) == 0)
21255 continue;
21256 #endif
21257 else
21259 /* Assume insn has non-zero length. */
21260 maybe_at_text_label_p = false;
21261 break;
21264 if (maybe_at_text_label_p)
21266 last_start = loc_note;
21267 first_loclabel_num_not_at_text_label = loclabel_num;
21272 if (!var_loc_p)
21274 struct call_arg_loc_node *ca_loc
21275 = ggc_alloc_cleared_call_arg_loc_node ();
21276 rtx prev = prev_real_insn (loc_note), x;
21277 ca_loc->call_arg_loc_note = loc_note;
21278 ca_loc->next = NULL;
21279 ca_loc->label = last_label;
21280 gcc_assert (prev
21281 && (CALL_P (prev)
21282 || (NONJUMP_INSN_P (prev)
21283 && GET_CODE (PATTERN (prev)) == SEQUENCE
21284 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21285 if (!CALL_P (prev))
21286 prev = XVECEXP (PATTERN (prev), 0, 0);
21287 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21288 x = get_call_rtx_from (PATTERN (prev));
21289 if (x)
21291 x = XEXP (XEXP (x, 0), 0);
21292 if (GET_CODE (x) == SYMBOL_REF
21293 && SYMBOL_REF_DECL (x)
21294 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21295 ca_loc->symbol_ref = x;
21297 ca_loc->block = insn_scope (prev);
21298 if (call_arg_locations)
21299 call_arg_loc_last->next = ca_loc;
21300 else
21301 call_arg_locations = ca_loc;
21302 call_arg_loc_last = ca_loc;
21304 else if (!NOTE_DURING_CALL_P (loc_note))
21305 newloc->label = last_label;
21306 else
21308 if (!last_postcall_label)
21310 sprintf (loclabel, "%s-1", last_label);
21311 last_postcall_label = ggc_strdup (loclabel);
21313 newloc->label = last_postcall_label;
21316 last_var_location_insn = next_real;
21317 last_in_cold_section_p = in_cold_section_p;
21320 /* Note in one location list that text section has changed. */
21322 static int
21323 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21325 var_loc_list *list = (var_loc_list *) *slot;
21326 if (list->first)
21327 list->last_before_switch
21328 = list->last->next ? list->last->next : list->last;
21329 return 1;
21332 /* Note in all location lists that text section has changed. */
21334 static void
21335 var_location_switch_text_section (void)
21337 if (decl_loc_table == NULL)
21338 return;
21340 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21343 /* Create a new line number table. */
21345 static dw_line_info_table *
21346 new_line_info_table (void)
21348 dw_line_info_table *table;
21350 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21351 table->file_num = 1;
21352 table->line_num = 1;
21353 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21355 return table;
21358 /* Lookup the "current" table into which we emit line info, so
21359 that we don't have to do it for every source line. */
21361 static void
21362 set_cur_line_info_table (section *sec)
21364 dw_line_info_table *table;
21366 if (sec == text_section)
21367 table = text_section_line_info;
21368 else if (sec == cold_text_section)
21370 table = cold_text_section_line_info;
21371 if (!table)
21373 cold_text_section_line_info = table = new_line_info_table ();
21374 table->end_label = cold_end_label;
21377 else
21379 const char *end_label;
21381 if (flag_reorder_blocks_and_partition)
21383 if (in_cold_section_p)
21384 end_label = crtl->subsections.cold_section_end_label;
21385 else
21386 end_label = crtl->subsections.hot_section_end_label;
21388 else
21390 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21391 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21392 current_function_funcdef_no);
21393 end_label = ggc_strdup (label);
21396 table = new_line_info_table ();
21397 table->end_label = end_label;
21399 vec_safe_push (separate_line_info, table);
21402 if (DWARF2_ASM_LINE_DEBUG_INFO)
21403 table->is_stmt = (cur_line_info_table
21404 ? cur_line_info_table->is_stmt
21405 : DWARF_LINE_DEFAULT_IS_STMT_START);
21406 cur_line_info_table = table;
21410 /* We need to reset the locations at the beginning of each
21411 function. We can't do this in the end_function hook, because the
21412 declarations that use the locations won't have been output when
21413 that hook is called. Also compute have_multiple_function_sections here. */
21415 static void
21416 dwarf2out_begin_function (tree fun)
21418 section *sec = function_section (fun);
21420 if (sec != text_section)
21421 have_multiple_function_sections = true;
21423 if (flag_reorder_blocks_and_partition && !cold_text_section)
21425 gcc_assert (current_function_decl == fun);
21426 cold_text_section = unlikely_text_section ();
21427 switch_to_section (cold_text_section);
21428 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21429 switch_to_section (sec);
21432 dwarf2out_note_section_used ();
21433 call_site_count = 0;
21434 tail_call_site_count = 0;
21436 set_cur_line_info_table (sec);
21439 /* Helper function of dwarf2out_end_function, called only after emitting
21440 the very first function into assembly. Check if some .debug_loc range
21441 might end with a .LVL* label that could be equal to .Ltext0.
21442 In that case we must force using absolute addresses in .debug_loc ranges,
21443 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21444 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21445 list terminator.
21446 Set have_multiple_function_sections to true in that case and
21447 terminate htab traversal. */
21449 static int
21450 find_empty_loc_ranges_at_text_label (void **slot, void *)
21452 var_loc_list *entry;
21453 struct var_loc_node *node;
21455 entry = (var_loc_list *) *slot;
21456 node = entry->first;
21457 if (node && node->next && node->next->label)
21459 unsigned int i;
21460 const char *label = node->next->label;
21461 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21463 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21465 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21466 if (strcmp (label, loclabel) == 0)
21468 have_multiple_function_sections = true;
21469 return 0;
21473 return 1;
21476 /* Hook called after emitting a function into assembly.
21477 This does something only for the very first function emitted. */
21479 static void
21480 dwarf2out_end_function (unsigned int)
21482 if (in_first_function_p
21483 && !have_multiple_function_sections
21484 && first_loclabel_num_not_at_text_label
21485 && decl_loc_table)
21486 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21487 NULL);
21488 in_first_function_p = false;
21489 maybe_at_text_label_p = false;
21492 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21494 static void
21495 push_dw_line_info_entry (dw_line_info_table *table,
21496 enum dw_line_info_opcode opcode, unsigned int val)
21498 dw_line_info_entry e;
21499 e.opcode = opcode;
21500 e.val = val;
21501 vec_safe_push (table->entries, e);
21504 /* Output a label to mark the beginning of a source code line entry
21505 and record information relating to this source line, in
21506 'line_info_table' for later output of the .debug_line section. */
21507 /* ??? The discriminator parameter ought to be unsigned. */
21509 static void
21510 dwarf2out_source_line (unsigned int line, const char *filename,
21511 int discriminator, bool is_stmt)
21513 unsigned int file_num;
21514 dw_line_info_table *table;
21516 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21517 return;
21519 /* The discriminator column was added in dwarf4. Simplify the below
21520 by simply removing it if we're not supposed to output it. */
21521 if (dwarf_version < 4 && dwarf_strict)
21522 discriminator = 0;
21524 table = cur_line_info_table;
21525 file_num = maybe_emit_file (lookup_filename (filename));
21527 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21528 the debugger has used the second (possibly duplicate) line number
21529 at the beginning of the function to mark the end of the prologue.
21530 We could eliminate any other duplicates within the function. For
21531 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21532 that second line number entry. */
21533 /* Recall that this end-of-prologue indication is *not* the same thing
21534 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21535 to which the hook corresponds, follows the last insn that was
21536 emitted by gen_prologue. What we need is to precede the first insn
21537 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21538 insn that corresponds to something the user wrote. These may be
21539 very different locations once scheduling is enabled. */
21541 if (0 && file_num == table->file_num
21542 && line == table->line_num
21543 && discriminator == table->discrim_num
21544 && is_stmt == table->is_stmt)
21545 return;
21547 switch_to_section (current_function_section ());
21549 /* If requested, emit something human-readable. */
21550 if (flag_debug_asm)
21551 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21553 if (DWARF2_ASM_LINE_DEBUG_INFO)
21555 /* Emit the .loc directive understood by GNU as. */
21556 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21557 file_num, line, is_stmt, discriminator */
21558 fputs ("\t.loc ", asm_out_file);
21559 fprint_ul (asm_out_file, file_num);
21560 putc (' ', asm_out_file);
21561 fprint_ul (asm_out_file, line);
21562 putc (' ', asm_out_file);
21563 putc ('0', asm_out_file);
21565 if (is_stmt != table->is_stmt)
21567 fputs (" is_stmt ", asm_out_file);
21568 putc (is_stmt ? '1' : '0', asm_out_file);
21570 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21572 gcc_assert (discriminator > 0);
21573 fputs (" discriminator ", asm_out_file);
21574 fprint_ul (asm_out_file, (unsigned long) discriminator);
21576 putc ('\n', asm_out_file);
21578 else
21580 unsigned int label_num = ++line_info_label_num;
21582 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21584 push_dw_line_info_entry (table, LI_set_address, label_num);
21585 if (file_num != table->file_num)
21586 push_dw_line_info_entry (table, LI_set_file, file_num);
21587 if (discriminator != table->discrim_num)
21588 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21589 if (is_stmt != table->is_stmt)
21590 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21591 push_dw_line_info_entry (table, LI_set_line, line);
21594 table->file_num = file_num;
21595 table->line_num = line;
21596 table->discrim_num = discriminator;
21597 table->is_stmt = is_stmt;
21598 table->in_use = true;
21601 /* Record the beginning of a new source file. */
21603 static void
21604 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21606 if (flag_eliminate_dwarf2_dups)
21608 /* Record the beginning of the file for break_out_includes. */
21609 dw_die_ref bincl_die;
21611 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21612 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21615 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21617 macinfo_entry e;
21618 e.code = DW_MACINFO_start_file;
21619 e.lineno = lineno;
21620 e.info = ggc_strdup (filename);
21621 vec_safe_push (macinfo_table, e);
21625 /* Record the end of a source file. */
21627 static void
21628 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21630 if (flag_eliminate_dwarf2_dups)
21631 /* Record the end of the file for break_out_includes. */
21632 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21634 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21636 macinfo_entry e;
21637 e.code = DW_MACINFO_end_file;
21638 e.lineno = lineno;
21639 e.info = NULL;
21640 vec_safe_push (macinfo_table, e);
21644 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21645 the tail part of the directive line, i.e. the part which is past the
21646 initial whitespace, #, whitespace, directive-name, whitespace part. */
21648 static void
21649 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21650 const char *buffer ATTRIBUTE_UNUSED)
21652 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21654 macinfo_entry e;
21655 /* Insert a dummy first entry to be able to optimize the whole
21656 predefined macro block using DW_MACRO_GNU_transparent_include. */
21657 if (macinfo_table->is_empty () && lineno <= 1)
21659 e.code = 0;
21660 e.lineno = 0;
21661 e.info = NULL;
21662 vec_safe_push (macinfo_table, e);
21664 e.code = DW_MACINFO_define;
21665 e.lineno = lineno;
21666 e.info = ggc_strdup (buffer);
21667 vec_safe_push (macinfo_table, e);
21671 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21672 the tail part of the directive line, i.e. the part which is past the
21673 initial whitespace, #, whitespace, directive-name, whitespace part. */
21675 static void
21676 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21677 const char *buffer ATTRIBUTE_UNUSED)
21679 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21681 macinfo_entry e;
21682 /* Insert a dummy first entry to be able to optimize the whole
21683 predefined macro block using DW_MACRO_GNU_transparent_include. */
21684 if (macinfo_table->is_empty () && lineno <= 1)
21686 e.code = 0;
21687 e.lineno = 0;
21688 e.info = NULL;
21689 vec_safe_push (macinfo_table, e);
21691 e.code = DW_MACINFO_undef;
21692 e.lineno = lineno;
21693 e.info = ggc_strdup (buffer);
21694 vec_safe_push (macinfo_table, e);
21698 /* Helpers to manipulate hash table of CUs. */
21700 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21702 typedef macinfo_entry value_type;
21703 typedef macinfo_entry compare_type;
21704 static inline hashval_t hash (const value_type *);
21705 static inline bool equal (const value_type *, const compare_type *);
21708 inline hashval_t
21709 macinfo_entry_hasher::hash (const value_type *entry)
21711 return htab_hash_string (entry->info);
21714 inline bool
21715 macinfo_entry_hasher::equal (const value_type *entry1,
21716 const compare_type *entry2)
21718 return !strcmp (entry1->info, entry2->info);
21721 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21723 /* Output a single .debug_macinfo entry. */
21725 static void
21726 output_macinfo_op (macinfo_entry *ref)
21728 int file_num;
21729 size_t len;
21730 struct indirect_string_node *node;
21731 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21732 struct dwarf_file_data *fd;
21734 switch (ref->code)
21736 case DW_MACINFO_start_file:
21737 fd = lookup_filename (ref->info);
21738 file_num = maybe_emit_file (fd);
21739 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21740 dw2_asm_output_data_uleb128 (ref->lineno,
21741 "Included from line number %lu",
21742 (unsigned long) ref->lineno);
21743 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21744 break;
21745 case DW_MACINFO_end_file:
21746 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21747 break;
21748 case DW_MACINFO_define:
21749 case DW_MACINFO_undef:
21750 len = strlen (ref->info) + 1;
21751 if (!dwarf_strict
21752 && len > DWARF_OFFSET_SIZE
21753 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21754 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21756 ref->code = ref->code == DW_MACINFO_define
21757 ? DW_MACRO_GNU_define_indirect
21758 : DW_MACRO_GNU_undef_indirect;
21759 output_macinfo_op (ref);
21760 return;
21762 dw2_asm_output_data (1, ref->code,
21763 ref->code == DW_MACINFO_define
21764 ? "Define macro" : "Undefine macro");
21765 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21766 (unsigned long) ref->lineno);
21767 dw2_asm_output_nstring (ref->info, -1, "The macro");
21768 break;
21769 case DW_MACRO_GNU_define_indirect:
21770 case DW_MACRO_GNU_undef_indirect:
21771 node = find_AT_string (ref->info);
21772 gcc_assert (node
21773 && ((node->form == DW_FORM_strp)
21774 || (node->form == DW_FORM_GNU_str_index)));
21775 dw2_asm_output_data (1, ref->code,
21776 ref->code == DW_MACRO_GNU_define_indirect
21777 ? "Define macro indirect"
21778 : "Undefine macro indirect");
21779 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21780 (unsigned long) ref->lineno);
21781 if (node->form == DW_FORM_strp)
21782 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21783 debug_str_section, "The macro: \"%s\"",
21784 ref->info);
21785 else
21786 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21787 ref->info);
21788 break;
21789 case DW_MACRO_GNU_transparent_include:
21790 dw2_asm_output_data (1, ref->code, "Transparent include");
21791 ASM_GENERATE_INTERNAL_LABEL (label,
21792 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21793 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21794 break;
21795 default:
21796 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21797 ASM_COMMENT_START, (unsigned long) ref->code);
21798 break;
21802 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21803 other compilation unit .debug_macinfo sections. IDX is the first
21804 index of a define/undef, return the number of ops that should be
21805 emitted in a comdat .debug_macinfo section and emit
21806 a DW_MACRO_GNU_transparent_include entry referencing it.
21807 If the define/undef entry should be emitted normally, return 0. */
21809 static unsigned
21810 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21811 macinfo_hash_type *macinfo_htab)
21813 macinfo_entry *first, *second, *cur, *inc;
21814 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21815 unsigned char checksum[16];
21816 struct md5_ctx ctx;
21817 char *grp_name, *tail;
21818 const char *base;
21819 unsigned int i, count, encoded_filename_len, linebuf_len;
21820 macinfo_entry **slot;
21822 first = &(*macinfo_table)[idx];
21823 second = &(*macinfo_table)[idx + 1];
21825 /* Optimize only if there are at least two consecutive define/undef ops,
21826 and either all of them are before first DW_MACINFO_start_file
21827 with lineno {0,1} (i.e. predefined macro block), or all of them are
21828 in some included header file. */
21829 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21830 return 0;
21831 if (vec_safe_is_empty (files))
21833 if (first->lineno > 1 || second->lineno > 1)
21834 return 0;
21836 else if (first->lineno == 0)
21837 return 0;
21839 /* Find the last define/undef entry that can be grouped together
21840 with first and at the same time compute md5 checksum of their
21841 codes, linenumbers and strings. */
21842 md5_init_ctx (&ctx);
21843 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21844 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21845 break;
21846 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21847 break;
21848 else
21850 unsigned char code = cur->code;
21851 md5_process_bytes (&code, 1, &ctx);
21852 checksum_uleb128 (cur->lineno, &ctx);
21853 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21855 md5_finish_ctx (&ctx, checksum);
21856 count = i - idx;
21858 /* From the containing include filename (if any) pick up just
21859 usable characters from its basename. */
21860 if (vec_safe_is_empty (files))
21861 base = "";
21862 else
21863 base = lbasename (files->last ().info);
21864 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21865 if (ISIDNUM (base[i]) || base[i] == '.')
21866 encoded_filename_len++;
21867 /* Count . at the end. */
21868 if (encoded_filename_len)
21869 encoded_filename_len++;
21871 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21872 linebuf_len = strlen (linebuf);
21874 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21875 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21876 + 16 * 2 + 1);
21877 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21878 tail = grp_name + 4;
21879 if (encoded_filename_len)
21881 for (i = 0; base[i]; i++)
21882 if (ISIDNUM (base[i]) || base[i] == '.')
21883 *tail++ = base[i];
21884 *tail++ = '.';
21886 memcpy (tail, linebuf, linebuf_len);
21887 tail += linebuf_len;
21888 *tail++ = '.';
21889 for (i = 0; i < 16; i++)
21890 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21892 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21893 in the empty vector entry before the first define/undef. */
21894 inc = &(*macinfo_table)[idx - 1];
21895 inc->code = DW_MACRO_GNU_transparent_include;
21896 inc->lineno = 0;
21897 inc->info = ggc_strdup (grp_name);
21898 if (!macinfo_htab->is_created ())
21899 macinfo_htab->create (10);
21900 /* Avoid emitting duplicates. */
21901 slot = macinfo_htab->find_slot (inc, INSERT);
21902 if (*slot != NULL)
21904 inc->code = 0;
21905 inc->info = NULL;
21906 /* If such an entry has been used before, just emit
21907 a DW_MACRO_GNU_transparent_include op. */
21908 inc = *slot;
21909 output_macinfo_op (inc);
21910 /* And clear all macinfo_entry in the range to avoid emitting them
21911 in the second pass. */
21912 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21914 cur->code = 0;
21915 cur->info = NULL;
21918 else
21920 *slot = inc;
21921 inc->lineno = macinfo_htab->elements ();
21922 output_macinfo_op (inc);
21924 return count;
21927 /* Save any strings needed by the macinfo table in the debug str
21928 table. All strings must be collected into the table by the time
21929 index_string is called. */
21931 static void
21932 save_macinfo_strings (void)
21934 unsigned len;
21935 unsigned i;
21936 macinfo_entry *ref;
21938 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21940 switch (ref->code)
21942 /* Match the logic in output_macinfo_op to decide on
21943 indirect strings. */
21944 case DW_MACINFO_define:
21945 case DW_MACINFO_undef:
21946 len = strlen (ref->info) + 1;
21947 if (!dwarf_strict
21948 && len > DWARF_OFFSET_SIZE
21949 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21950 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21951 set_indirect_string (find_AT_string (ref->info));
21952 break;
21953 case DW_MACRO_GNU_define_indirect:
21954 case DW_MACRO_GNU_undef_indirect:
21955 set_indirect_string (find_AT_string (ref->info));
21956 break;
21957 default:
21958 break;
21963 /* Output macinfo section(s). */
21965 static void
21966 output_macinfo (void)
21968 unsigned i;
21969 unsigned long length = vec_safe_length (macinfo_table);
21970 macinfo_entry *ref;
21971 vec<macinfo_entry, va_gc> *files = NULL;
21972 macinfo_hash_type macinfo_htab;
21974 if (! length)
21975 return;
21977 /* output_macinfo* uses these interchangeably. */
21978 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21979 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21980 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21981 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21983 /* For .debug_macro emit the section header. */
21984 if (!dwarf_strict)
21986 dw2_asm_output_data (2, 4, "DWARF macro version number");
21987 if (DWARF_OFFSET_SIZE == 8)
21988 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21989 else
21990 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21991 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21992 (!dwarf_split_debug_info ? debug_line_section_label
21993 : debug_skeleton_line_section_label),
21994 debug_line_section, NULL);
21997 /* In the first loop, it emits the primary .debug_macinfo section
21998 and after each emitted op the macinfo_entry is cleared.
21999 If a longer range of define/undef ops can be optimized using
22000 DW_MACRO_GNU_transparent_include, the
22001 DW_MACRO_GNU_transparent_include op is emitted and kept in
22002 the vector before the first define/undef in the range and the
22003 whole range of define/undef ops is not emitted and kept. */
22004 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22006 switch (ref->code)
22008 case DW_MACINFO_start_file:
22009 vec_safe_push (files, *ref);
22010 break;
22011 case DW_MACINFO_end_file:
22012 if (!vec_safe_is_empty (files))
22013 files->pop ();
22014 break;
22015 case DW_MACINFO_define:
22016 case DW_MACINFO_undef:
22017 if (!dwarf_strict
22018 && HAVE_COMDAT_GROUP
22019 && vec_safe_length (files) != 1
22020 && i > 0
22021 && i + 1 < length
22022 && (*macinfo_table)[i - 1].code == 0)
22024 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22025 if (count)
22027 i += count - 1;
22028 continue;
22031 break;
22032 case 0:
22033 /* A dummy entry may be inserted at the beginning to be able
22034 to optimize the whole block of predefined macros. */
22035 if (i == 0)
22036 continue;
22037 default:
22038 break;
22040 output_macinfo_op (ref);
22041 ref->info = NULL;
22042 ref->code = 0;
22045 if (!macinfo_htab.is_created ())
22046 return;
22048 macinfo_htab.dispose ();
22050 /* If any DW_MACRO_GNU_transparent_include were used, on those
22051 DW_MACRO_GNU_transparent_include entries terminate the
22052 current chain and switch to a new comdat .debug_macinfo
22053 section and emit the define/undef entries within it. */
22054 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22055 switch (ref->code)
22057 case 0:
22058 continue;
22059 case DW_MACRO_GNU_transparent_include:
22061 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22062 tree comdat_key = get_identifier (ref->info);
22063 /* Terminate the previous .debug_macinfo section. */
22064 dw2_asm_output_data (1, 0, "End compilation unit");
22065 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22066 SECTION_DEBUG
22067 | SECTION_LINKONCE,
22068 comdat_key);
22069 ASM_GENERATE_INTERNAL_LABEL (label,
22070 DEBUG_MACRO_SECTION_LABEL,
22071 ref->lineno);
22072 ASM_OUTPUT_LABEL (asm_out_file, label);
22073 ref->code = 0;
22074 ref->info = NULL;
22075 dw2_asm_output_data (2, 4, "DWARF macro version number");
22076 if (DWARF_OFFSET_SIZE == 8)
22077 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22078 else
22079 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22081 break;
22082 case DW_MACINFO_define:
22083 case DW_MACINFO_undef:
22084 output_macinfo_op (ref);
22085 ref->code = 0;
22086 ref->info = NULL;
22087 break;
22088 default:
22089 gcc_unreachable ();
22093 /* Set up for Dwarf output at the start of compilation. */
22095 static void
22096 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22098 /* Allocate the file_table. */
22099 file_table = htab_create_ggc (50, file_table_hash,
22100 file_table_eq, NULL);
22102 /* Allocate the decl_die_table. */
22103 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22104 decl_die_table_eq, NULL);
22106 /* Allocate the decl_loc_table. */
22107 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22108 decl_loc_table_eq, NULL);
22110 /* Allocate the cached_dw_loc_list_table. */
22111 cached_dw_loc_list_table
22112 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22113 cached_dw_loc_list_table_eq, NULL);
22115 /* Allocate the initial hunk of the decl_scope_table. */
22116 vec_alloc (decl_scope_table, 256);
22118 /* Allocate the initial hunk of the abbrev_die_table. */
22119 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22120 (ABBREV_DIE_TABLE_INCREMENT);
22121 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22122 /* Zero-th entry is allocated, but unused. */
22123 abbrev_die_table_in_use = 1;
22125 /* Allocate the pubtypes and pubnames vectors. */
22126 vec_alloc (pubname_table, 32);
22127 vec_alloc (pubtype_table, 32);
22129 vec_alloc (incomplete_types, 64);
22131 vec_alloc (used_rtx_array, 32);
22133 if (!dwarf_split_debug_info)
22135 debug_info_section = get_section (DEBUG_INFO_SECTION,
22136 SECTION_DEBUG, NULL);
22137 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22138 SECTION_DEBUG, NULL);
22139 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22140 SECTION_DEBUG, NULL);
22142 else
22144 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22145 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22146 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22147 SECTION_DEBUG | SECTION_EXCLUDE,
22148 NULL);
22149 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22150 SECTION_DEBUG, NULL);
22151 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22152 SECTION_DEBUG, NULL);
22153 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22154 SECTION_DEBUG, NULL);
22155 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22156 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22158 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22159 the main .o, but the skeleton_line goes into the split off dwo. */
22160 debug_skeleton_line_section
22161 = get_section (DEBUG_DWO_LINE_SECTION,
22162 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22163 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22164 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22165 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22166 SECTION_DEBUG | SECTION_EXCLUDE,
22167 NULL);
22168 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22169 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22170 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22171 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22172 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22173 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22175 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22176 SECTION_DEBUG, NULL);
22177 debug_macinfo_section = get_section (dwarf_strict
22178 ? DEBUG_MACINFO_SECTION
22179 : DEBUG_MACRO_SECTION,
22180 DEBUG_MACRO_SECTION_FLAGS, NULL);
22181 debug_line_section = get_section (DEBUG_LINE_SECTION,
22182 SECTION_DEBUG, NULL);
22183 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22184 SECTION_DEBUG, NULL);
22185 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22186 SECTION_DEBUG, NULL);
22187 debug_str_section = get_section (DEBUG_STR_SECTION,
22188 DEBUG_STR_SECTION_FLAGS, NULL);
22189 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22190 SECTION_DEBUG, NULL);
22191 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22192 SECTION_DEBUG, NULL);
22194 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22195 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22196 DEBUG_ABBREV_SECTION_LABEL, 0);
22197 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22198 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22199 COLD_TEXT_SECTION_LABEL, 0);
22200 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22202 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22203 DEBUG_INFO_SECTION_LABEL, 0);
22204 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22205 DEBUG_LINE_SECTION_LABEL, 0);
22206 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22207 DEBUG_RANGES_SECTION_LABEL, 0);
22208 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22209 DEBUG_ADDR_SECTION_LABEL, 0);
22210 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22211 dwarf_strict
22212 ? DEBUG_MACINFO_SECTION_LABEL
22213 : DEBUG_MACRO_SECTION_LABEL, 0);
22214 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22216 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22217 vec_alloc (macinfo_table, 64);
22219 switch_to_section (text_section);
22220 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22222 /* Make sure the line number table for .text always exists. */
22223 text_section_line_info = new_line_info_table ();
22224 text_section_line_info->end_label = text_end_label;
22227 /* Called before compile () starts outputtting functions, variables
22228 and toplevel asms into assembly. */
22230 static void
22231 dwarf2out_assembly_start (void)
22233 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22234 && dwarf2out_do_cfi_asm ()
22235 && (!(flag_unwind_tables || flag_exceptions)
22236 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22237 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22240 /* A helper function for dwarf2out_finish called through
22241 htab_traverse. Assign a string its index. All strings must be
22242 collected into the table by the time index_string is called,
22243 because the indexing code relies on htab_traverse to traverse nodes
22244 in the same order for each run. */
22246 static int
22247 index_string (void **h, void *v)
22249 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22250 unsigned int *index = (unsigned int *) v;
22252 find_string_form (node);
22253 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22255 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22256 node->index = *index;
22257 *index += 1;
22259 return 1;
22262 /* A helper function for output_indirect_strings called through
22263 htab_traverse. Output the offset to a string and update the
22264 current offset. */
22266 static int
22267 output_index_string_offset (void **h, void *v)
22269 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22270 unsigned int *offset = (unsigned int *) v;
22272 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22274 /* Assert that this node has been assigned an index. */
22275 gcc_assert (node->index != NO_INDEX_ASSIGNED
22276 && node->index != NOT_INDEXED);
22277 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22278 "indexed string 0x%x: %s", node->index, node->str);
22279 *offset += strlen (node->str) + 1;
22281 return 1;
22284 /* A helper function for dwarf2out_finish called through
22285 htab_traverse. Output the indexed string. */
22287 static int
22288 output_index_string (void **h, void *v)
22290 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22291 unsigned int *cur_idx = (unsigned int *) v;
22293 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22295 /* Assert that the strings are output in the same order as their
22296 indexes were assigned. */
22297 gcc_assert (*cur_idx == node->index);
22298 assemble_string (node->str, strlen (node->str) + 1);
22299 *cur_idx += 1;
22301 return 1;
22304 /* A helper function for dwarf2out_finish called through
22305 htab_traverse. Emit one queued .debug_str string. */
22307 static int
22308 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22310 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22312 node->form = find_string_form (node);
22313 if (node->form == DW_FORM_strp && node->refcount > 0)
22315 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22316 assemble_string (node->str, strlen (node->str) + 1);
22319 return 1;
22322 /* Output the indexed string table. */
22324 static void
22325 output_indirect_strings (void)
22327 switch_to_section (debug_str_section);
22328 if (!dwarf_split_debug_info)
22329 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22330 else
22332 unsigned int offset = 0;
22333 unsigned int cur_idx = 0;
22335 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22337 switch_to_section (debug_str_offsets_section);
22338 htab_traverse_noresize (debug_str_hash,
22339 output_index_string_offset,
22340 &offset);
22341 switch_to_section (debug_str_dwo_section);
22342 htab_traverse_noresize (debug_str_hash,
22343 output_index_string,
22344 &cur_idx);
22348 /* Callback for htab_traverse to assign an index to an entry in the
22349 table, and to write that entry to the .debug_addr section. */
22351 static int
22352 output_addr_table_entry (void **slot, void *data)
22354 addr_table_entry *entry = (addr_table_entry *) *slot;
22355 unsigned int *cur_index = (unsigned int *)data;
22357 if (entry->refcount == 0)
22359 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22360 || entry->index == NOT_INDEXED);
22361 return 1;
22364 gcc_assert (entry->index == *cur_index);
22365 (*cur_index)++;
22367 switch (entry->kind)
22369 case ate_kind_rtx:
22370 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22371 "0x%x", entry->index);
22372 break;
22373 case ate_kind_rtx_dtprel:
22374 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22375 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22376 DWARF2_ADDR_SIZE,
22377 entry->addr.rtl);
22378 fputc ('\n', asm_out_file);
22379 break;
22380 case ate_kind_label:
22381 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22382 "0x%x", entry->index);
22383 break;
22384 default:
22385 gcc_unreachable ();
22387 return 1;
22390 /* Produce the .debug_addr section. */
22392 static void
22393 output_addr_table (void)
22395 unsigned int index = 0;
22396 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22397 return;
22399 switch_to_section (debug_addr_section);
22400 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22403 #if ENABLE_ASSERT_CHECKING
22404 /* Verify that all marks are clear. */
22406 static void
22407 verify_marks_clear (dw_die_ref die)
22409 dw_die_ref c;
22411 gcc_assert (! die->die_mark);
22412 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22414 #endif /* ENABLE_ASSERT_CHECKING */
22416 /* Clear the marks for a die and its children.
22417 Be cool if the mark isn't set. */
22419 static void
22420 prune_unmark_dies (dw_die_ref die)
22422 dw_die_ref c;
22424 if (die->die_mark)
22425 die->die_mark = 0;
22426 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22429 /* Given DIE that we're marking as used, find any other dies
22430 it references as attributes and mark them as used. */
22432 static void
22433 prune_unused_types_walk_attribs (dw_die_ref die)
22435 dw_attr_ref a;
22436 unsigned ix;
22438 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22440 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22442 /* A reference to another DIE.
22443 Make sure that it will get emitted.
22444 If it was broken out into a comdat group, don't follow it. */
22445 if (! AT_ref (a)->comdat_type_p
22446 || a->dw_attr == DW_AT_specification)
22447 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22449 /* Set the string's refcount to 0 so that prune_unused_types_mark
22450 accounts properly for it. */
22451 if (AT_class (a) == dw_val_class_str)
22452 a->dw_attr_val.v.val_str->refcount = 0;
22456 /* Mark the generic parameters and arguments children DIEs of DIE. */
22458 static void
22459 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22461 dw_die_ref c;
22463 if (die == NULL || die->die_child == NULL)
22464 return;
22465 c = die->die_child;
22468 if (is_template_parameter (c))
22469 prune_unused_types_mark (c, 1);
22470 c = c->die_sib;
22471 } while (c && c != die->die_child);
22474 /* Mark DIE as being used. If DOKIDS is true, then walk down
22475 to DIE's children. */
22477 static void
22478 prune_unused_types_mark (dw_die_ref die, int dokids)
22480 dw_die_ref c;
22482 if (die->die_mark == 0)
22484 /* We haven't done this node yet. Mark it as used. */
22485 die->die_mark = 1;
22486 /* If this is the DIE of a generic type instantiation,
22487 mark the children DIEs that describe its generic parms and
22488 args. */
22489 prune_unused_types_mark_generic_parms_dies (die);
22491 /* We also have to mark its parents as used.
22492 (But we don't want to mark our parent's kids due to this,
22493 unless it is a class.) */
22494 if (die->die_parent)
22495 prune_unused_types_mark (die->die_parent,
22496 class_scope_p (die->die_parent));
22498 /* Mark any referenced nodes. */
22499 prune_unused_types_walk_attribs (die);
22501 /* If this node is a specification,
22502 also mark the definition, if it exists. */
22503 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22504 prune_unused_types_mark (die->die_definition, 1);
22507 if (dokids && die->die_mark != 2)
22509 /* We need to walk the children, but haven't done so yet.
22510 Remember that we've walked the kids. */
22511 die->die_mark = 2;
22513 /* If this is an array type, we need to make sure our
22514 kids get marked, even if they're types. If we're
22515 breaking out types into comdat sections, do this
22516 for all type definitions. */
22517 if (die->die_tag == DW_TAG_array_type
22518 || (use_debug_types
22519 && is_type_die (die) && ! is_declaration_die (die)))
22520 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22521 else
22522 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22526 /* For local classes, look if any static member functions were emitted
22527 and if so, mark them. */
22529 static void
22530 prune_unused_types_walk_local_classes (dw_die_ref die)
22532 dw_die_ref c;
22534 if (die->die_mark == 2)
22535 return;
22537 switch (die->die_tag)
22539 case DW_TAG_structure_type:
22540 case DW_TAG_union_type:
22541 case DW_TAG_class_type:
22542 break;
22544 case DW_TAG_subprogram:
22545 if (!get_AT_flag (die, DW_AT_declaration)
22546 || die->die_definition != NULL)
22547 prune_unused_types_mark (die, 1);
22548 return;
22550 default:
22551 return;
22554 /* Mark children. */
22555 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22558 /* Walk the tree DIE and mark types that we actually use. */
22560 static void
22561 prune_unused_types_walk (dw_die_ref die)
22563 dw_die_ref c;
22565 /* Don't do anything if this node is already marked and
22566 children have been marked as well. */
22567 if (die->die_mark == 2)
22568 return;
22570 switch (die->die_tag)
22572 case DW_TAG_structure_type:
22573 case DW_TAG_union_type:
22574 case DW_TAG_class_type:
22575 if (die->die_perennial_p)
22576 break;
22578 for (c = die->die_parent; c; c = c->die_parent)
22579 if (c->die_tag == DW_TAG_subprogram)
22580 break;
22582 /* Finding used static member functions inside of classes
22583 is needed just for local classes, because for other classes
22584 static member function DIEs with DW_AT_specification
22585 are emitted outside of the DW_TAG_*_type. If we ever change
22586 it, we'd need to call this even for non-local classes. */
22587 if (c)
22588 prune_unused_types_walk_local_classes (die);
22590 /* It's a type node --- don't mark it. */
22591 return;
22593 case DW_TAG_const_type:
22594 case DW_TAG_packed_type:
22595 case DW_TAG_pointer_type:
22596 case DW_TAG_reference_type:
22597 case DW_TAG_rvalue_reference_type:
22598 case DW_TAG_volatile_type:
22599 case DW_TAG_typedef:
22600 case DW_TAG_array_type:
22601 case DW_TAG_interface_type:
22602 case DW_TAG_friend:
22603 case DW_TAG_variant_part:
22604 case DW_TAG_enumeration_type:
22605 case DW_TAG_subroutine_type:
22606 case DW_TAG_string_type:
22607 case DW_TAG_set_type:
22608 case DW_TAG_subrange_type:
22609 case DW_TAG_ptr_to_member_type:
22610 case DW_TAG_file_type:
22611 if (die->die_perennial_p)
22612 break;
22614 /* It's a type node --- don't mark it. */
22615 return;
22617 default:
22618 /* Mark everything else. */
22619 break;
22622 if (die->die_mark == 0)
22624 die->die_mark = 1;
22626 /* Now, mark any dies referenced from here. */
22627 prune_unused_types_walk_attribs (die);
22630 die->die_mark = 2;
22632 /* Mark children. */
22633 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22636 /* Increment the string counts on strings referred to from DIE's
22637 attributes. */
22639 static void
22640 prune_unused_types_update_strings (dw_die_ref die)
22642 dw_attr_ref a;
22643 unsigned ix;
22645 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22646 if (AT_class (a) == dw_val_class_str)
22648 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22649 s->refcount++;
22650 /* Avoid unnecessarily putting strings that are used less than
22651 twice in the hash table. */
22652 if (s->refcount
22653 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22655 void ** slot;
22656 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22657 htab_hash_string (s->str),
22658 INSERT);
22659 gcc_assert (*slot == NULL);
22660 *slot = s;
22665 /* Remove from the tree DIE any dies that aren't marked. */
22667 static void
22668 prune_unused_types_prune (dw_die_ref die)
22670 dw_die_ref c;
22672 gcc_assert (die->die_mark);
22673 prune_unused_types_update_strings (die);
22675 if (! die->die_child)
22676 return;
22678 c = die->die_child;
22679 do {
22680 dw_die_ref prev = c;
22681 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22682 if (c == die->die_child)
22684 /* No marked children between 'prev' and the end of the list. */
22685 if (prev == c)
22686 /* No marked children at all. */
22687 die->die_child = NULL;
22688 else
22690 prev->die_sib = c->die_sib;
22691 die->die_child = prev;
22693 return;
22696 if (c != prev->die_sib)
22697 prev->die_sib = c;
22698 prune_unused_types_prune (c);
22699 } while (c != die->die_child);
22702 /* Remove dies representing declarations that we never use. */
22704 static void
22705 prune_unused_types (void)
22707 unsigned int i;
22708 limbo_die_node *node;
22709 comdat_type_node *ctnode;
22710 pubname_ref pub;
22711 dw_die_ref base_type;
22713 #if ENABLE_ASSERT_CHECKING
22714 /* All the marks should already be clear. */
22715 verify_marks_clear (comp_unit_die ());
22716 for (node = limbo_die_list; node; node = node->next)
22717 verify_marks_clear (node->die);
22718 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22719 verify_marks_clear (ctnode->root_die);
22720 #endif /* ENABLE_ASSERT_CHECKING */
22722 /* Mark types that are used in global variables. */
22723 premark_types_used_by_global_vars ();
22725 /* Set the mark on nodes that are actually used. */
22726 prune_unused_types_walk (comp_unit_die ());
22727 for (node = limbo_die_list; node; node = node->next)
22728 prune_unused_types_walk (node->die);
22729 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22731 prune_unused_types_walk (ctnode->root_die);
22732 prune_unused_types_mark (ctnode->type_die, 1);
22735 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22736 are unusual in that they are pubnames that are the children of pubtypes.
22737 They should only be marked via their parent DW_TAG_enumeration_type die,
22738 not as roots in themselves. */
22739 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22740 if (pub->die->die_tag != DW_TAG_enumerator)
22741 prune_unused_types_mark (pub->die, 1);
22742 for (i = 0; base_types.iterate (i, &base_type); i++)
22743 prune_unused_types_mark (base_type, 1);
22745 if (debug_str_hash)
22746 htab_empty (debug_str_hash);
22747 if (skeleton_debug_str_hash)
22748 htab_empty (skeleton_debug_str_hash);
22749 prune_unused_types_prune (comp_unit_die ());
22750 for (node = limbo_die_list; node; node = node->next)
22751 prune_unused_types_prune (node->die);
22752 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22753 prune_unused_types_prune (ctnode->root_die);
22755 /* Leave the marks clear. */
22756 prune_unmark_dies (comp_unit_die ());
22757 for (node = limbo_die_list; node; node = node->next)
22758 prune_unmark_dies (node->die);
22759 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22760 prune_unmark_dies (ctnode->root_die);
22763 /* Set the parameter to true if there are any relative pathnames in
22764 the file table. */
22765 static int
22766 file_table_relative_p (void ** slot, void *param)
22768 bool *p = (bool *) param;
22769 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22770 if (!IS_ABSOLUTE_PATH (d->filename))
22772 *p = true;
22773 return 0;
22775 return 1;
22778 /* Helpers to manipulate hash table of comdat type units. */
22780 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22782 typedef comdat_type_node value_type;
22783 typedef comdat_type_node compare_type;
22784 static inline hashval_t hash (const value_type *);
22785 static inline bool equal (const value_type *, const compare_type *);
22788 inline hashval_t
22789 comdat_type_hasher::hash (const value_type *type_node)
22791 hashval_t h;
22792 memcpy (&h, type_node->signature, sizeof (h));
22793 return h;
22796 inline bool
22797 comdat_type_hasher::equal (const value_type *type_node_1,
22798 const compare_type *type_node_2)
22800 return (! memcmp (type_node_1->signature, type_node_2->signature,
22801 DWARF_TYPE_SIGNATURE_SIZE));
22804 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22805 to the location it would have been added, should we know its
22806 DECL_ASSEMBLER_NAME when we added other attributes. This will
22807 probably improve compactness of debug info, removing equivalent
22808 abbrevs, and hide any differences caused by deferring the
22809 computation of the assembler name, triggered by e.g. PCH. */
22811 static inline void
22812 move_linkage_attr (dw_die_ref die)
22814 unsigned ix = vec_safe_length (die->die_attr);
22815 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22817 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22818 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22820 while (--ix > 0)
22822 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22824 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22825 break;
22828 if (ix != vec_safe_length (die->die_attr) - 1)
22830 die->die_attr->pop ();
22831 die->die_attr->quick_insert (ix, linkage);
22835 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22836 referenced from typed stack ops and count how often they are used. */
22838 static void
22839 mark_base_types (dw_loc_descr_ref loc)
22841 dw_die_ref base_type = NULL;
22843 for (; loc; loc = loc->dw_loc_next)
22845 switch (loc->dw_loc_opc)
22847 case DW_OP_GNU_regval_type:
22848 case DW_OP_GNU_deref_type:
22849 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22850 break;
22851 case DW_OP_GNU_convert:
22852 case DW_OP_GNU_reinterpret:
22853 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22854 continue;
22855 /* FALLTHRU */
22856 case DW_OP_GNU_const_type:
22857 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22858 break;
22859 case DW_OP_GNU_entry_value:
22860 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22861 continue;
22862 default:
22863 continue;
22865 gcc_assert (base_type->die_parent == comp_unit_die ());
22866 if (base_type->die_mark)
22867 base_type->die_mark++;
22868 else
22870 base_types.safe_push (base_type);
22871 base_type->die_mark = 1;
22876 /* Comparison function for sorting marked base types. */
22878 static int
22879 base_type_cmp (const void *x, const void *y)
22881 dw_die_ref dx = *(const dw_die_ref *) x;
22882 dw_die_ref dy = *(const dw_die_ref *) y;
22883 unsigned int byte_size1, byte_size2;
22884 unsigned int encoding1, encoding2;
22885 if (dx->die_mark > dy->die_mark)
22886 return -1;
22887 if (dx->die_mark < dy->die_mark)
22888 return 1;
22889 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22890 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22891 if (byte_size1 < byte_size2)
22892 return 1;
22893 if (byte_size1 > byte_size2)
22894 return -1;
22895 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22896 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22897 if (encoding1 < encoding2)
22898 return 1;
22899 if (encoding1 > encoding2)
22900 return -1;
22901 return 0;
22904 /* Move base types marked by mark_base_types as early as possible
22905 in the CU, sorted by decreasing usage count both to make the
22906 uleb128 references as small as possible and to make sure they
22907 will have die_offset already computed by calc_die_sizes when
22908 sizes of typed stack loc ops is computed. */
22910 static void
22911 move_marked_base_types (void)
22913 unsigned int i;
22914 dw_die_ref base_type, die, c;
22916 if (base_types.is_empty ())
22917 return;
22919 /* Sort by decreasing usage count, they will be added again in that
22920 order later on. */
22921 base_types.qsort (base_type_cmp);
22922 die = comp_unit_die ();
22923 c = die->die_child;
22926 dw_die_ref prev = c;
22927 c = c->die_sib;
22928 while (c->die_mark)
22930 remove_child_with_prev (c, prev);
22931 /* As base types got marked, there must be at least
22932 one node other than DW_TAG_base_type. */
22933 gcc_assert (c != c->die_sib);
22934 c = c->die_sib;
22937 while (c != die->die_child);
22938 gcc_assert (die->die_child);
22939 c = die->die_child;
22940 for (i = 0; base_types.iterate (i, &base_type); i++)
22942 base_type->die_mark = 0;
22943 base_type->die_sib = c->die_sib;
22944 c->die_sib = base_type;
22945 c = base_type;
22949 /* Helper function for resolve_addr, attempt to resolve
22950 one CONST_STRING, return non-zero if not successful. Similarly verify that
22951 SYMBOL_REFs refer to variables emitted in the current CU. */
22953 static int
22954 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22956 rtx rtl = *addr;
22958 if (GET_CODE (rtl) == CONST_STRING)
22960 size_t len = strlen (XSTR (rtl, 0)) + 1;
22961 tree t = build_string (len, XSTR (rtl, 0));
22962 tree tlen = size_int (len - 1);
22963 TREE_TYPE (t)
22964 = build_array_type (char_type_node, build_index_type (tlen));
22965 rtl = lookup_constant_def (t);
22966 if (!rtl || !MEM_P (rtl))
22967 return 1;
22968 rtl = XEXP (rtl, 0);
22969 if (GET_CODE (rtl) == SYMBOL_REF
22970 && SYMBOL_REF_DECL (rtl)
22971 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22972 return 1;
22973 vec_safe_push (used_rtx_array, rtl);
22974 *addr = rtl;
22975 return 0;
22978 if (GET_CODE (rtl) == SYMBOL_REF
22979 && SYMBOL_REF_DECL (rtl))
22981 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22983 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22984 return 1;
22986 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22987 return 1;
22990 if (GET_CODE (rtl) == CONST
22991 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22992 return 1;
22994 return 0;
22997 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22998 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22999 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23001 static rtx
23002 string_cst_pool_decl (tree t)
23004 rtx rtl = output_constant_def (t, 1);
23005 unsigned char *array;
23006 dw_loc_descr_ref l;
23007 tree decl;
23008 size_t len;
23009 dw_die_ref ref;
23011 if (!rtl || !MEM_P (rtl))
23012 return NULL_RTX;
23013 rtl = XEXP (rtl, 0);
23014 if (GET_CODE (rtl) != SYMBOL_REF
23015 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23016 return NULL_RTX;
23018 decl = SYMBOL_REF_DECL (rtl);
23019 if (!lookup_decl_die (decl))
23021 len = TREE_STRING_LENGTH (t);
23022 vec_safe_push (used_rtx_array, rtl);
23023 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23024 array = (unsigned char *) ggc_alloc_atomic (len);
23025 memcpy (array, TREE_STRING_POINTER (t), len);
23026 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23027 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23028 l->dw_loc_oprnd2.v.val_vec.length = len;
23029 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23030 l->dw_loc_oprnd2.v.val_vec.array = array;
23031 add_AT_loc (ref, DW_AT_location, l);
23032 equate_decl_number_to_die (decl, ref);
23034 return rtl;
23037 /* Helper function of resolve_addr_in_expr. LOC is
23038 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23039 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23040 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23041 with DW_OP_GNU_implicit_pointer if possible
23042 and return true, if unsuccessful, return false. */
23044 static bool
23045 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23047 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23048 HOST_WIDE_INT offset = 0;
23049 dw_die_ref ref = NULL;
23050 tree decl;
23052 if (GET_CODE (rtl) == CONST
23053 && GET_CODE (XEXP (rtl, 0)) == PLUS
23054 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23056 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23057 rtl = XEXP (XEXP (rtl, 0), 0);
23059 if (GET_CODE (rtl) == CONST_STRING)
23061 size_t len = strlen (XSTR (rtl, 0)) + 1;
23062 tree t = build_string (len, XSTR (rtl, 0));
23063 tree tlen = size_int (len - 1);
23065 TREE_TYPE (t)
23066 = build_array_type (char_type_node, build_index_type (tlen));
23067 rtl = string_cst_pool_decl (t);
23068 if (!rtl)
23069 return false;
23071 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23073 decl = SYMBOL_REF_DECL (rtl);
23074 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23076 ref = lookup_decl_die (decl);
23077 if (ref && (get_AT (ref, DW_AT_location)
23078 || get_AT (ref, DW_AT_const_value)))
23080 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23081 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23082 loc->dw_loc_oprnd1.val_entry = NULL;
23083 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23084 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23085 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23086 loc->dw_loc_oprnd2.v.val_int = offset;
23087 return true;
23091 return false;
23094 /* Helper function for resolve_addr, handle one location
23095 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23096 the location list couldn't be resolved. */
23098 static bool
23099 resolve_addr_in_expr (dw_loc_descr_ref loc)
23101 dw_loc_descr_ref keep = NULL;
23102 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23103 switch (loc->dw_loc_opc)
23105 case DW_OP_addr:
23106 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23108 if ((prev == NULL
23109 || prev->dw_loc_opc == DW_OP_piece
23110 || prev->dw_loc_opc == DW_OP_bit_piece)
23111 && loc->dw_loc_next
23112 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23113 && !dwarf_strict
23114 && optimize_one_addr_into_implicit_ptr (loc))
23115 break;
23116 return false;
23118 break;
23119 case DW_OP_GNU_addr_index:
23120 case DW_OP_GNU_const_index:
23121 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23122 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23123 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23124 NULL))
23125 return false;
23126 break;
23127 case DW_OP_const4u:
23128 case DW_OP_const8u:
23129 if (loc->dtprel
23130 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23131 return false;
23132 break;
23133 case DW_OP_plus_uconst:
23134 if (size_of_loc_descr (loc)
23135 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23137 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23139 dw_loc_descr_ref repl
23140 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23141 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23142 add_loc_descr (&repl, loc->dw_loc_next);
23143 *loc = *repl;
23145 break;
23146 case DW_OP_implicit_value:
23147 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23148 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23149 return false;
23150 break;
23151 case DW_OP_GNU_implicit_pointer:
23152 case DW_OP_GNU_parameter_ref:
23153 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23155 dw_die_ref ref
23156 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23157 if (ref == NULL)
23158 return false;
23159 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23160 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23161 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23163 break;
23164 case DW_OP_GNU_const_type:
23165 case DW_OP_GNU_regval_type:
23166 case DW_OP_GNU_deref_type:
23167 case DW_OP_GNU_convert:
23168 case DW_OP_GNU_reinterpret:
23169 while (loc->dw_loc_next
23170 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23172 dw_die_ref base1, base2;
23173 unsigned enc1, enc2, size1, size2;
23174 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23175 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23176 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23177 else if (loc->dw_loc_oprnd1.val_class
23178 == dw_val_class_unsigned_const)
23179 break;
23180 else
23181 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23182 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23183 == dw_val_class_unsigned_const)
23184 break;
23185 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23186 gcc_assert (base1->die_tag == DW_TAG_base_type
23187 && base2->die_tag == DW_TAG_base_type);
23188 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23189 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23190 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23191 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23192 if (size1 == size2
23193 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23194 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23195 && loc != keep)
23196 || enc1 == enc2))
23198 /* Optimize away next DW_OP_GNU_convert after
23199 adjusting LOC's base type die reference. */
23200 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23201 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23202 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23203 else
23204 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23205 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23206 continue;
23208 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23209 point typed stack entry. */
23210 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23211 keep = loc->dw_loc_next;
23212 break;
23214 break;
23215 default:
23216 break;
23218 return true;
23221 /* Helper function of resolve_addr. DIE had DW_AT_location of
23222 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23223 and DW_OP_addr couldn't be resolved. resolve_addr has already
23224 removed the DW_AT_location attribute. This function attempts to
23225 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23226 to it or DW_AT_const_value attribute, if possible. */
23228 static void
23229 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23231 if (TREE_CODE (decl) != VAR_DECL
23232 || lookup_decl_die (decl) != die
23233 || DECL_EXTERNAL (decl)
23234 || !TREE_STATIC (decl)
23235 || DECL_INITIAL (decl) == NULL_TREE
23236 || DECL_P (DECL_INITIAL (decl))
23237 || get_AT (die, DW_AT_const_value))
23238 return;
23240 tree init = DECL_INITIAL (decl);
23241 HOST_WIDE_INT offset = 0;
23242 /* For variables that have been optimized away and thus
23243 don't have a memory location, see if we can emit
23244 DW_AT_const_value instead. */
23245 if (tree_add_const_value_attribute (die, init))
23246 return;
23247 if (dwarf_strict)
23248 return;
23249 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23250 and ADDR_EXPR refers to a decl that has DW_AT_location or
23251 DW_AT_const_value (but isn't addressable, otherwise
23252 resolving the original DW_OP_addr wouldn't fail), see if
23253 we can add DW_OP_GNU_implicit_pointer. */
23254 STRIP_NOPS (init);
23255 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23256 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23258 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23259 init = TREE_OPERAND (init, 0);
23260 STRIP_NOPS (init);
23262 if (TREE_CODE (init) != ADDR_EXPR)
23263 return;
23264 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23265 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23266 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23267 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23268 && TREE_OPERAND (init, 0) != decl))
23270 dw_die_ref ref;
23271 dw_loc_descr_ref l;
23273 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23275 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23276 if (!rtl)
23277 return;
23278 decl = SYMBOL_REF_DECL (rtl);
23280 else
23281 decl = TREE_OPERAND (init, 0);
23282 ref = lookup_decl_die (decl);
23283 if (ref == NULL
23284 || (!get_AT (ref, DW_AT_location)
23285 && !get_AT (ref, DW_AT_const_value)))
23286 return;
23287 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23288 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23289 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23290 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23291 add_AT_loc (die, DW_AT_location, l);
23295 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23296 an address in .rodata section if the string literal is emitted there,
23297 or remove the containing location list or replace DW_AT_const_value
23298 with DW_AT_location and empty location expression, if it isn't found
23299 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23300 to something that has been emitted in the current CU. */
23302 static void
23303 resolve_addr (dw_die_ref die)
23305 dw_die_ref c;
23306 dw_attr_ref a;
23307 dw_loc_list_ref *curr, *start, loc;
23308 unsigned ix;
23310 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23311 switch (AT_class (a))
23313 case dw_val_class_loc_list:
23314 start = curr = AT_loc_list_ptr (a);
23315 loc = *curr;
23316 gcc_assert (loc);
23317 /* The same list can be referenced more than once. See if we have
23318 already recorded the result from a previous pass. */
23319 if (loc->replaced)
23320 *curr = loc->dw_loc_next;
23321 else if (!loc->resolved_addr)
23323 /* As things stand, we do not expect or allow one die to
23324 reference a suffix of another die's location list chain.
23325 References must be identical or completely separate.
23326 There is therefore no need to cache the result of this
23327 pass on any list other than the first; doing so
23328 would lead to unnecessary writes. */
23329 while (*curr)
23331 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23332 if (!resolve_addr_in_expr ((*curr)->expr))
23334 dw_loc_list_ref next = (*curr)->dw_loc_next;
23335 dw_loc_descr_ref l = (*curr)->expr;
23337 if (next && (*curr)->ll_symbol)
23339 gcc_assert (!next->ll_symbol);
23340 next->ll_symbol = (*curr)->ll_symbol;
23342 if (dwarf_split_debug_info)
23343 remove_loc_list_addr_table_entries (l);
23344 *curr = next;
23346 else
23348 mark_base_types ((*curr)->expr);
23349 curr = &(*curr)->dw_loc_next;
23352 if (loc == *start)
23353 loc->resolved_addr = 1;
23354 else
23356 loc->replaced = 1;
23357 loc->dw_loc_next = *start;
23360 if (!*start)
23362 remove_AT (die, a->dw_attr);
23363 ix--;
23365 break;
23366 case dw_val_class_loc:
23368 dw_loc_descr_ref l = AT_loc (a);
23369 /* For -gdwarf-2 don't attempt to optimize
23370 DW_AT_data_member_location containing
23371 DW_OP_plus_uconst - older consumers might
23372 rely on it being that op instead of a more complex,
23373 but shorter, location description. */
23374 if ((dwarf_version > 2
23375 || a->dw_attr != DW_AT_data_member_location
23376 || l == NULL
23377 || l->dw_loc_opc != DW_OP_plus_uconst
23378 || l->dw_loc_next != NULL)
23379 && !resolve_addr_in_expr (l))
23381 if (dwarf_split_debug_info)
23382 remove_loc_list_addr_table_entries (l);
23383 if (l != NULL
23384 && l->dw_loc_next == NULL
23385 && l->dw_loc_opc == DW_OP_addr
23386 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23387 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23388 && a->dw_attr == DW_AT_location)
23390 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23391 remove_AT (die, a->dw_attr);
23392 ix--;
23393 optimize_location_into_implicit_ptr (die, decl);
23394 break;
23396 remove_AT (die, a->dw_attr);
23397 ix--;
23399 else
23400 mark_base_types (l);
23402 break;
23403 case dw_val_class_addr:
23404 if (a->dw_attr == DW_AT_const_value
23405 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23407 if (AT_index (a) != NOT_INDEXED)
23408 remove_addr_table_entry (a->dw_attr_val.val_entry);
23409 remove_AT (die, a->dw_attr);
23410 ix--;
23412 if (die->die_tag == DW_TAG_GNU_call_site
23413 && a->dw_attr == DW_AT_abstract_origin)
23415 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23416 dw_die_ref tdie = lookup_decl_die (tdecl);
23417 if (tdie == NULL
23418 && DECL_EXTERNAL (tdecl)
23419 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23421 force_decl_die (tdecl);
23422 tdie = lookup_decl_die (tdecl);
23424 if (tdie)
23426 a->dw_attr_val.val_class = dw_val_class_die_ref;
23427 a->dw_attr_val.v.val_die_ref.die = tdie;
23428 a->dw_attr_val.v.val_die_ref.external = 0;
23430 else
23432 if (AT_index (a) != NOT_INDEXED)
23433 remove_addr_table_entry (a->dw_attr_val.val_entry);
23434 remove_AT (die, a->dw_attr);
23435 ix--;
23438 break;
23439 default:
23440 break;
23443 FOR_EACH_CHILD (die, c, resolve_addr (c));
23446 /* Helper routines for optimize_location_lists.
23447 This pass tries to share identical local lists in .debug_loc
23448 section. */
23450 /* Iteratively hash operands of LOC opcode. */
23452 static hashval_t
23453 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23455 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23456 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23458 switch (loc->dw_loc_opc)
23460 case DW_OP_const4u:
23461 case DW_OP_const8u:
23462 if (loc->dtprel)
23463 goto hash_addr;
23464 /* FALLTHRU */
23465 case DW_OP_const1u:
23466 case DW_OP_const1s:
23467 case DW_OP_const2u:
23468 case DW_OP_const2s:
23469 case DW_OP_const4s:
23470 case DW_OP_const8s:
23471 case DW_OP_constu:
23472 case DW_OP_consts:
23473 case DW_OP_pick:
23474 case DW_OP_plus_uconst:
23475 case DW_OP_breg0:
23476 case DW_OP_breg1:
23477 case DW_OP_breg2:
23478 case DW_OP_breg3:
23479 case DW_OP_breg4:
23480 case DW_OP_breg5:
23481 case DW_OP_breg6:
23482 case DW_OP_breg7:
23483 case DW_OP_breg8:
23484 case DW_OP_breg9:
23485 case DW_OP_breg10:
23486 case DW_OP_breg11:
23487 case DW_OP_breg12:
23488 case DW_OP_breg13:
23489 case DW_OP_breg14:
23490 case DW_OP_breg15:
23491 case DW_OP_breg16:
23492 case DW_OP_breg17:
23493 case DW_OP_breg18:
23494 case DW_OP_breg19:
23495 case DW_OP_breg20:
23496 case DW_OP_breg21:
23497 case DW_OP_breg22:
23498 case DW_OP_breg23:
23499 case DW_OP_breg24:
23500 case DW_OP_breg25:
23501 case DW_OP_breg26:
23502 case DW_OP_breg27:
23503 case DW_OP_breg28:
23504 case DW_OP_breg29:
23505 case DW_OP_breg30:
23506 case DW_OP_breg31:
23507 case DW_OP_regx:
23508 case DW_OP_fbreg:
23509 case DW_OP_piece:
23510 case DW_OP_deref_size:
23511 case DW_OP_xderef_size:
23512 hash = iterative_hash_object (val1->v.val_int, hash);
23513 break;
23514 case DW_OP_skip:
23515 case DW_OP_bra:
23517 int offset;
23519 gcc_assert (val1->val_class == dw_val_class_loc);
23520 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23521 hash = iterative_hash_object (offset, hash);
23523 break;
23524 case DW_OP_implicit_value:
23525 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23526 switch (val2->val_class)
23528 case dw_val_class_const:
23529 hash = iterative_hash_object (val2->v.val_int, hash);
23530 break;
23531 case dw_val_class_vec:
23533 unsigned int elt_size = val2->v.val_vec.elt_size;
23534 unsigned int len = val2->v.val_vec.length;
23536 hash = iterative_hash_object (elt_size, hash);
23537 hash = iterative_hash_object (len, hash);
23538 hash = iterative_hash (val2->v.val_vec.array,
23539 len * elt_size, hash);
23541 break;
23542 case dw_val_class_const_double:
23543 hash = iterative_hash_object (val2->v.val_double.low, hash);
23544 hash = iterative_hash_object (val2->v.val_double.high, hash);
23545 break;
23546 case dw_val_class_addr:
23547 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23548 break;
23549 default:
23550 gcc_unreachable ();
23552 break;
23553 case DW_OP_bregx:
23554 case DW_OP_bit_piece:
23555 hash = iterative_hash_object (val1->v.val_int, hash);
23556 hash = iterative_hash_object (val2->v.val_int, hash);
23557 break;
23558 case DW_OP_addr:
23559 hash_addr:
23560 if (loc->dtprel)
23562 unsigned char dtprel = 0xd1;
23563 hash = iterative_hash_object (dtprel, hash);
23565 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23566 break;
23567 case DW_OP_GNU_addr_index:
23568 case DW_OP_GNU_const_index:
23570 if (loc->dtprel)
23572 unsigned char dtprel = 0xd1;
23573 hash = iterative_hash_object (dtprel, hash);
23575 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23577 break;
23578 case DW_OP_GNU_implicit_pointer:
23579 hash = iterative_hash_object (val2->v.val_int, hash);
23580 break;
23581 case DW_OP_GNU_entry_value:
23582 hash = hash_loc_operands (val1->v.val_loc, hash);
23583 break;
23584 case DW_OP_GNU_regval_type:
23585 case DW_OP_GNU_deref_type:
23587 unsigned int byte_size
23588 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23589 unsigned int encoding
23590 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23591 hash = iterative_hash_object (val1->v.val_int, hash);
23592 hash = iterative_hash_object (byte_size, hash);
23593 hash = iterative_hash_object (encoding, hash);
23595 break;
23596 case DW_OP_GNU_convert:
23597 case DW_OP_GNU_reinterpret:
23598 if (val1->val_class == dw_val_class_unsigned_const)
23600 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23601 break;
23603 /* FALLTHRU */
23604 case DW_OP_GNU_const_type:
23606 unsigned int byte_size
23607 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23608 unsigned int encoding
23609 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23610 hash = iterative_hash_object (byte_size, hash);
23611 hash = iterative_hash_object (encoding, hash);
23612 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23613 break;
23614 hash = iterative_hash_object (val2->val_class, hash);
23615 switch (val2->val_class)
23617 case dw_val_class_const:
23618 hash = iterative_hash_object (val2->v.val_int, hash);
23619 break;
23620 case dw_val_class_vec:
23622 unsigned int elt_size = val2->v.val_vec.elt_size;
23623 unsigned int len = val2->v.val_vec.length;
23625 hash = iterative_hash_object (elt_size, hash);
23626 hash = iterative_hash_object (len, hash);
23627 hash = iterative_hash (val2->v.val_vec.array,
23628 len * elt_size, hash);
23630 break;
23631 case dw_val_class_const_double:
23632 hash = iterative_hash_object (val2->v.val_double.low, hash);
23633 hash = iterative_hash_object (val2->v.val_double.high, hash);
23634 break;
23635 default:
23636 gcc_unreachable ();
23639 break;
23641 default:
23642 /* Other codes have no operands. */
23643 break;
23645 return hash;
23648 /* Iteratively hash the whole DWARF location expression LOC. */
23650 static inline hashval_t
23651 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23653 dw_loc_descr_ref l;
23654 bool sizes_computed = false;
23655 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23656 size_of_locs (loc);
23658 for (l = loc; l != NULL; l = l->dw_loc_next)
23660 enum dwarf_location_atom opc = l->dw_loc_opc;
23661 hash = iterative_hash_object (opc, hash);
23662 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23664 size_of_locs (loc);
23665 sizes_computed = true;
23667 hash = hash_loc_operands (l, hash);
23669 return hash;
23672 /* Compute hash of the whole location list LIST_HEAD. */
23674 static inline void
23675 hash_loc_list (dw_loc_list_ref list_head)
23677 dw_loc_list_ref curr = list_head;
23678 hashval_t hash = 0;
23680 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23682 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23683 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23684 if (curr->section)
23685 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23686 hash);
23687 hash = hash_locs (curr->expr, hash);
23689 list_head->hash = hash;
23692 /* Return true if X and Y opcodes have the same operands. */
23694 static inline bool
23695 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23697 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23698 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23699 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23700 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23702 switch (x->dw_loc_opc)
23704 case DW_OP_const4u:
23705 case DW_OP_const8u:
23706 if (x->dtprel)
23707 goto hash_addr;
23708 /* FALLTHRU */
23709 case DW_OP_const1u:
23710 case DW_OP_const1s:
23711 case DW_OP_const2u:
23712 case DW_OP_const2s:
23713 case DW_OP_const4s:
23714 case DW_OP_const8s:
23715 case DW_OP_constu:
23716 case DW_OP_consts:
23717 case DW_OP_pick:
23718 case DW_OP_plus_uconst:
23719 case DW_OP_breg0:
23720 case DW_OP_breg1:
23721 case DW_OP_breg2:
23722 case DW_OP_breg3:
23723 case DW_OP_breg4:
23724 case DW_OP_breg5:
23725 case DW_OP_breg6:
23726 case DW_OP_breg7:
23727 case DW_OP_breg8:
23728 case DW_OP_breg9:
23729 case DW_OP_breg10:
23730 case DW_OP_breg11:
23731 case DW_OP_breg12:
23732 case DW_OP_breg13:
23733 case DW_OP_breg14:
23734 case DW_OP_breg15:
23735 case DW_OP_breg16:
23736 case DW_OP_breg17:
23737 case DW_OP_breg18:
23738 case DW_OP_breg19:
23739 case DW_OP_breg20:
23740 case DW_OP_breg21:
23741 case DW_OP_breg22:
23742 case DW_OP_breg23:
23743 case DW_OP_breg24:
23744 case DW_OP_breg25:
23745 case DW_OP_breg26:
23746 case DW_OP_breg27:
23747 case DW_OP_breg28:
23748 case DW_OP_breg29:
23749 case DW_OP_breg30:
23750 case DW_OP_breg31:
23751 case DW_OP_regx:
23752 case DW_OP_fbreg:
23753 case DW_OP_piece:
23754 case DW_OP_deref_size:
23755 case DW_OP_xderef_size:
23756 return valx1->v.val_int == valy1->v.val_int;
23757 case DW_OP_skip:
23758 case DW_OP_bra:
23759 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23760 can cause irrelevant differences in dw_loc_addr. */
23761 gcc_assert (valx1->val_class == dw_val_class_loc
23762 && valy1->val_class == dw_val_class_loc
23763 && (dwarf_split_debug_info
23764 || x->dw_loc_addr == y->dw_loc_addr));
23765 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23766 case DW_OP_implicit_value:
23767 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23768 || valx2->val_class != valy2->val_class)
23769 return false;
23770 switch (valx2->val_class)
23772 case dw_val_class_const:
23773 return valx2->v.val_int == valy2->v.val_int;
23774 case dw_val_class_vec:
23775 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23776 && valx2->v.val_vec.length == valy2->v.val_vec.length
23777 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23778 valx2->v.val_vec.elt_size
23779 * valx2->v.val_vec.length) == 0;
23780 case dw_val_class_const_double:
23781 return valx2->v.val_double.low == valy2->v.val_double.low
23782 && valx2->v.val_double.high == valy2->v.val_double.high;
23783 case dw_val_class_addr:
23784 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23785 default:
23786 gcc_unreachable ();
23788 case DW_OP_bregx:
23789 case DW_OP_bit_piece:
23790 return valx1->v.val_int == valy1->v.val_int
23791 && valx2->v.val_int == valy2->v.val_int;
23792 case DW_OP_addr:
23793 hash_addr:
23794 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23795 case DW_OP_GNU_addr_index:
23796 case DW_OP_GNU_const_index:
23798 rtx ax1 = valx1->val_entry->addr.rtl;
23799 rtx ay1 = valy1->val_entry->addr.rtl;
23800 return rtx_equal_p (ax1, ay1);
23802 case DW_OP_GNU_implicit_pointer:
23803 return valx1->val_class == dw_val_class_die_ref
23804 && valx1->val_class == valy1->val_class
23805 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23806 && valx2->v.val_int == valy2->v.val_int;
23807 case DW_OP_GNU_entry_value:
23808 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23809 case DW_OP_GNU_const_type:
23810 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23811 || valx2->val_class != valy2->val_class)
23812 return false;
23813 switch (valx2->val_class)
23815 case dw_val_class_const:
23816 return valx2->v.val_int == valy2->v.val_int;
23817 case dw_val_class_vec:
23818 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23819 && valx2->v.val_vec.length == valy2->v.val_vec.length
23820 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23821 valx2->v.val_vec.elt_size
23822 * valx2->v.val_vec.length) == 0;
23823 case dw_val_class_const_double:
23824 return valx2->v.val_double.low == valy2->v.val_double.low
23825 && valx2->v.val_double.high == valy2->v.val_double.high;
23826 default:
23827 gcc_unreachable ();
23829 case DW_OP_GNU_regval_type:
23830 case DW_OP_GNU_deref_type:
23831 return valx1->v.val_int == valy1->v.val_int
23832 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23833 case DW_OP_GNU_convert:
23834 case DW_OP_GNU_reinterpret:
23835 if (valx1->val_class != valy1->val_class)
23836 return false;
23837 if (valx1->val_class == dw_val_class_unsigned_const)
23838 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23839 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23840 case DW_OP_GNU_parameter_ref:
23841 return valx1->val_class == dw_val_class_die_ref
23842 && valx1->val_class == valy1->val_class
23843 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23844 default:
23845 /* Other codes have no operands. */
23846 return true;
23850 /* Return true if DWARF location expressions X and Y are the same. */
23852 static inline bool
23853 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23855 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23856 if (x->dw_loc_opc != y->dw_loc_opc
23857 || x->dtprel != y->dtprel
23858 || !compare_loc_operands (x, y))
23859 break;
23860 return x == NULL && y == NULL;
23863 /* Hashtable helpers. */
23865 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23867 typedef dw_loc_list_struct value_type;
23868 typedef dw_loc_list_struct compare_type;
23869 static inline hashval_t hash (const value_type *);
23870 static inline bool equal (const value_type *, const compare_type *);
23873 /* Return precomputed hash of location list X. */
23875 inline hashval_t
23876 loc_list_hasher::hash (const value_type *x)
23878 return x->hash;
23881 /* Return true if location lists A and B are the same. */
23883 inline bool
23884 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23886 if (a == b)
23887 return 1;
23888 if (a->hash != b->hash)
23889 return 0;
23890 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23891 if (strcmp (a->begin, b->begin) != 0
23892 || strcmp (a->end, b->end) != 0
23893 || (a->section == NULL) != (b->section == NULL)
23894 || (a->section && strcmp (a->section, b->section) != 0)
23895 || !compare_locs (a->expr, b->expr))
23896 break;
23897 return a == NULL && b == NULL;
23900 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23903 /* Recursively optimize location lists referenced from DIE
23904 children and share them whenever possible. */
23906 static void
23907 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23909 dw_die_ref c;
23910 dw_attr_ref a;
23911 unsigned ix;
23912 dw_loc_list_struct **slot;
23914 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23915 if (AT_class (a) == dw_val_class_loc_list)
23917 dw_loc_list_ref list = AT_loc_list (a);
23918 /* TODO: perform some optimizations here, before hashing
23919 it and storing into the hash table. */
23920 hash_loc_list (list);
23921 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23922 if (*slot == NULL)
23923 *slot = list;
23924 else
23925 a->dw_attr_val.v.val_loc_list = *slot;
23928 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23932 /* Recursively assign each location list a unique index into the debug_addr
23933 section. */
23935 static void
23936 index_location_lists (dw_die_ref die)
23938 dw_die_ref c;
23939 dw_attr_ref a;
23940 unsigned ix;
23942 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23943 if (AT_class (a) == dw_val_class_loc_list)
23945 dw_loc_list_ref list = AT_loc_list (a);
23946 dw_loc_list_ref curr;
23947 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23949 /* Don't index an entry that has already been indexed
23950 or won't be output. */
23951 if (curr->begin_entry != NULL
23952 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23953 continue;
23955 curr->begin_entry
23956 = add_addr_table_entry (xstrdup (curr->begin),
23957 ate_kind_label);
23961 FOR_EACH_CHILD (die, c, index_location_lists (c));
23964 /* Optimize location lists referenced from DIE
23965 children and share them whenever possible. */
23967 static void
23968 optimize_location_lists (dw_die_ref die)
23970 loc_list_hash_type htab;
23971 htab.create (500);
23972 optimize_location_lists_1 (die, htab);
23973 htab.dispose ();
23976 /* Output stuff that dwarf requires at the end of every file,
23977 and generate the DWARF-2 debugging info. */
23979 static void
23980 dwarf2out_finish (const char *filename)
23982 limbo_die_node *node, *next_node;
23983 comdat_type_node *ctnode;
23984 hash_table <comdat_type_hasher> comdat_type_table;
23985 unsigned int i;
23986 dw_die_ref main_comp_unit_die;
23988 /* PCH might result in DW_AT_producer string being restored from the
23989 header compilation, so always fill it with empty string initially
23990 and overwrite only here. */
23991 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23992 producer_string = gen_producer_string ();
23993 producer->dw_attr_val.v.val_str->refcount--;
23994 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23996 gen_scheduled_generic_parms_dies ();
23997 gen_remaining_tmpl_value_param_die_attribute ();
23999 /* Add the name for the main input file now. We delayed this from
24000 dwarf2out_init to avoid complications with PCH. */
24001 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24002 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24003 add_comp_dir_attribute (comp_unit_die ());
24004 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24006 bool p = false;
24007 htab_traverse (file_table, file_table_relative_p, &p);
24008 if (p)
24009 add_comp_dir_attribute (comp_unit_die ());
24012 if (deferred_locations_list)
24013 for (i = 0; i < deferred_locations_list->length (); i++)
24015 add_location_or_const_value_attribute (
24016 (*deferred_locations_list)[i].die,
24017 (*deferred_locations_list)[i].variable,
24018 false,
24019 DW_AT_location);
24022 /* Traverse the limbo die list, and add parent/child links. The only
24023 dies without parents that should be here are concrete instances of
24024 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24025 For concrete instances, we can get the parent die from the abstract
24026 instance. */
24027 for (node = limbo_die_list; node; node = next_node)
24029 dw_die_ref die = node->die;
24030 next_node = node->next;
24032 if (die->die_parent == NULL)
24034 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24036 if (origin && origin->die_parent)
24037 add_child_die (origin->die_parent, die);
24038 else if (is_cu_die (die))
24040 else if (seen_error ())
24041 /* It's OK to be confused by errors in the input. */
24042 add_child_die (comp_unit_die (), die);
24043 else
24045 /* In certain situations, the lexical block containing a
24046 nested function can be optimized away, which results
24047 in the nested function die being orphaned. Likewise
24048 with the return type of that nested function. Force
24049 this to be a child of the containing function.
24051 It may happen that even the containing function got fully
24052 inlined and optimized out. In that case we are lost and
24053 assign the empty child. This should not be big issue as
24054 the function is likely unreachable too. */
24055 gcc_assert (node->created_for);
24057 if (DECL_P (node->created_for))
24058 origin = get_context_die (DECL_CONTEXT (node->created_for));
24059 else if (TYPE_P (node->created_for))
24060 origin = scope_die_for (node->created_for, comp_unit_die ());
24061 else
24062 origin = comp_unit_die ();
24064 add_child_die (origin, die);
24069 limbo_die_list = NULL;
24071 #if ENABLE_ASSERT_CHECKING
24073 dw_die_ref die = comp_unit_die (), c;
24074 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24076 #endif
24077 resolve_addr (comp_unit_die ());
24078 move_marked_base_types ();
24080 for (node = deferred_asm_name; node; node = node->next)
24082 tree decl = node->created_for;
24083 /* When generating LTO bytecode we can not generate new assembler
24084 names at this point and all important decls got theirs via
24085 free-lang-data. */
24086 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24087 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24089 add_linkage_attr (node->die, decl);
24090 move_linkage_attr (node->die);
24094 deferred_asm_name = NULL;
24096 /* Walk through the list of incomplete types again, trying once more to
24097 emit full debugging info for them. */
24098 retry_incomplete_types ();
24100 if (flag_eliminate_unused_debug_types)
24101 prune_unused_types ();
24103 /* Generate separate COMDAT sections for type DIEs. */
24104 if (use_debug_types)
24106 break_out_comdat_types (comp_unit_die ());
24108 /* Each new type_unit DIE was added to the limbo die list when created.
24109 Since these have all been added to comdat_type_list, clear the
24110 limbo die list. */
24111 limbo_die_list = NULL;
24113 /* For each new comdat type unit, copy declarations for incomplete
24114 types to make the new unit self-contained (i.e., no direct
24115 references to the main compile unit). */
24116 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24117 copy_decls_for_unworthy_types (ctnode->root_die);
24118 copy_decls_for_unworthy_types (comp_unit_die ());
24120 /* In the process of copying declarations from one unit to another,
24121 we may have left some declarations behind that are no longer
24122 referenced. Prune them. */
24123 prune_unused_types ();
24126 /* Generate separate CUs for each of the include files we've seen.
24127 They will go into limbo_die_list. */
24128 if (flag_eliminate_dwarf2_dups)
24129 break_out_includes (comp_unit_die ());
24131 /* Traverse the DIE's and add add sibling attributes to those DIE's
24132 that have children. */
24133 add_sibling_attributes (comp_unit_die ());
24134 for (node = limbo_die_list; node; node = node->next)
24135 add_sibling_attributes (node->die);
24136 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24137 add_sibling_attributes (ctnode->root_die);
24139 /* When splitting DWARF info, we put some attributes in the
24140 skeleton compile_unit DIE that remains in the .o, while
24141 most attributes go in the DWO compile_unit_die. */
24142 if (dwarf_split_debug_info)
24143 main_comp_unit_die = gen_compile_unit_die (NULL);
24144 else
24145 main_comp_unit_die = comp_unit_die ();
24147 /* Output a terminator label for the .text section. */
24148 switch_to_section (text_section);
24149 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24150 if (cold_text_section)
24152 switch_to_section (cold_text_section);
24153 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24156 /* We can only use the low/high_pc attributes if all of the code was
24157 in .text. */
24158 if (!have_multiple_function_sections
24159 || (dwarf_version < 3 && dwarf_strict))
24161 /* Don't add if the CU has no associated code. */
24162 if (text_section_used)
24163 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24164 text_end_label, true);
24166 else
24168 unsigned fde_idx;
24169 dw_fde_ref fde;
24170 bool range_list_added = false;
24172 if (text_section_used)
24173 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24174 text_end_label, &range_list_added, true);
24175 if (cold_text_section_used)
24176 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24177 cold_end_label, &range_list_added, true);
24179 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24181 if (DECL_IGNORED_P (fde->decl))
24182 continue;
24183 if (!fde->in_std_section)
24184 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24185 fde->dw_fde_end, &range_list_added,
24186 true);
24187 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24188 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24189 fde->dw_fde_second_end, &range_list_added,
24190 true);
24193 if (range_list_added)
24195 /* We need to give .debug_loc and .debug_ranges an appropriate
24196 "base address". Use zero so that these addresses become
24197 absolute. Historically, we've emitted the unexpected
24198 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24199 Emit both to give time for other tools to adapt. */
24200 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24201 if (! dwarf_strict && dwarf_version < 4)
24202 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24204 add_ranges (NULL);
24208 if (debug_info_level >= DINFO_LEVEL_TERSE)
24209 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24210 debug_line_section_label);
24212 if (have_macinfo)
24213 add_AT_macptr (comp_unit_die (),
24214 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24215 macinfo_section_label);
24217 if (dwarf_split_debug_info && addr_index_table != NULL)
24219 /* optimize_location_lists calculates the size of the lists,
24220 so index them first, and assign indices to the entries.
24221 Although optimize_location_lists will remove entries from
24222 the table, it only does so for duplicates, and therefore
24223 only reduces ref_counts to 1. */
24224 unsigned int index = 0;
24225 index_location_lists (comp_unit_die ());
24226 htab_traverse_noresize (addr_index_table,
24227 index_addr_table_entry, &index);
24229 if (have_location_lists)
24230 optimize_location_lists (comp_unit_die ());
24232 save_macinfo_strings ();
24234 if (dwarf_split_debug_info)
24236 unsigned int index = 0;
24238 /* Add attributes common to skeleton compile_units and
24239 type_units. Because these attributes include strings, it
24240 must be done before freezing the string table. Top-level
24241 skeleton die attrs are added when the skeleton type unit is
24242 created, so ensure it is created by this point. */
24243 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24244 (void) get_skeleton_type_unit ();
24245 htab_traverse_noresize (debug_str_hash, index_string, &index);
24248 /* Output all of the compilation units. We put the main one last so that
24249 the offsets are available to output_pubnames. */
24250 for (node = limbo_die_list; node; node = node->next)
24251 output_comp_unit (node->die, 0);
24253 comdat_type_table.create (100);
24254 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24256 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24258 /* Don't output duplicate types. */
24259 if (*slot != HTAB_EMPTY_ENTRY)
24260 continue;
24262 /* Add a pointer to the line table for the main compilation unit
24263 so that the debugger can make sense of DW_AT_decl_file
24264 attributes. */
24265 if (debug_info_level >= DINFO_LEVEL_TERSE)
24266 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24267 (!dwarf_split_debug_info
24268 ? debug_line_section_label
24269 : debug_skeleton_line_section_label));
24271 output_comdat_type_unit (ctnode);
24272 *slot = ctnode;
24274 comdat_type_table.dispose ();
24276 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24277 both the main_cu and all skeleton TUs. Making this call unconditional
24278 would end up either adding a second copy of the AT_pubnames attribute, or
24279 requiring a special case in add_top_level_skeleton_die_attrs. */
24280 if (!dwarf_split_debug_info)
24281 add_AT_pubnames (comp_unit_die ());
24283 if (dwarf_split_debug_info)
24285 int mark;
24286 unsigned char checksum[16];
24287 struct md5_ctx ctx;
24289 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24290 md5_init_ctx (&ctx);
24291 mark = 0;
24292 die_checksum (comp_unit_die (), &ctx, &mark);
24293 unmark_all_dies (comp_unit_die ());
24294 md5_finish_ctx (&ctx, checksum);
24296 /* Use the first 8 bytes of the checksum as the dwo_id,
24297 and add it to both comp-unit DIEs. */
24298 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24299 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24301 /* Add the base offset of the ranges table to the skeleton
24302 comp-unit DIE. */
24303 if (ranges_table_in_use)
24304 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24305 ranges_section_label);
24307 switch_to_section (debug_addr_section);
24308 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24309 output_addr_table ();
24312 /* Output the main compilation unit if non-empty or if .debug_macinfo
24313 or .debug_macro will be emitted. */
24314 output_comp_unit (comp_unit_die (), have_macinfo);
24316 if (dwarf_split_debug_info && info_section_emitted)
24317 output_skeleton_debug_sections (main_comp_unit_die);
24319 /* Output the abbreviation table. */
24320 if (abbrev_die_table_in_use != 1)
24322 switch_to_section (debug_abbrev_section);
24323 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24324 output_abbrev_section ();
24327 /* Output location list section if necessary. */
24328 if (have_location_lists)
24330 /* Output the location lists info. */
24331 switch_to_section (debug_loc_section);
24332 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24333 output_location_lists (comp_unit_die ());
24336 output_pubtables ();
24338 /* Output the address range information if a CU (.debug_info section)
24339 was emitted. We output an empty table even if we had no functions
24340 to put in it. This because the consumer has no way to tell the
24341 difference between an empty table that we omitted and failure to
24342 generate a table that would have contained data. */
24343 if (info_section_emitted)
24345 unsigned long aranges_length = size_of_aranges ();
24347 switch_to_section (debug_aranges_section);
24348 output_aranges (aranges_length);
24351 /* Output ranges section if necessary. */
24352 if (ranges_table_in_use)
24354 switch_to_section (debug_ranges_section);
24355 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24356 output_ranges ();
24359 /* Have to end the macro section. */
24360 if (have_macinfo)
24362 switch_to_section (debug_macinfo_section);
24363 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24364 output_macinfo ();
24365 dw2_asm_output_data (1, 0, "End compilation unit");
24368 /* Output the source line correspondence table. We must do this
24369 even if there is no line information. Otherwise, on an empty
24370 translation unit, we will generate a present, but empty,
24371 .debug_info section. IRIX 6.5 `nm' will then complain when
24372 examining the file. This is done late so that any filenames
24373 used by the debug_info section are marked as 'used'. */
24374 switch_to_section (debug_line_section);
24375 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24376 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24377 output_line_info (false);
24379 if (dwarf_split_debug_info && info_section_emitted)
24381 switch_to_section (debug_skeleton_line_section);
24382 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24383 output_line_info (true);
24386 /* If we emitted any indirect strings, output the string table too. */
24387 if (debug_str_hash || skeleton_debug_str_hash)
24388 output_indirect_strings ();
24391 #include "gt-dwarf2out.h"