New vectorizer messages; message format change.
[official-gcc.git] / gcc / dwarf2out.c
blobfc1c3f215657bd07d35c092860023400e609d838
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "tree.h"
63 #include "version.h"
64 #include "flags.h"
65 #include "rtl.h"
66 #include "hard-reg-set.h"
67 #include "regs.h"
68 #include "insn-config.h"
69 #include "reload.h"
70 #include "function.h"
71 #include "output.h"
72 #include "expr.h"
73 #include "except.h"
74 #include "dwarf2.h"
75 #include "dwarf2out.h"
76 #include "dwarf2asm.h"
77 #include "toplev.h"
78 #include "ggc.h"
79 #include "md5.h"
80 #include "tm_p.h"
81 #include "diagnostic.h"
82 #include "tree-pretty-print.h"
83 #include "debug.h"
84 #include "target.h"
85 #include "common/common-target.h"
86 #include "langhooks.h"
87 #include "hash-table.h"
88 #include "cgraph.h"
89 #include "input.h"
90 #include "gimple.h"
91 #include "ira.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 /* The size of the target's pointer type. */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
129 /* Array of RTXes referenced by the debugging information, which therefore
130 must be kept around forever. */
131 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
133 /* A pointer to the base of a list of incomplete types which might be
134 completed at some later time. incomplete_types_list needs to be a
135 vec<tree, va_gc> *because we want to tell the garbage collector about
136 it. */
137 static GTY(()) vec<tree, va_gc> *incomplete_types;
139 /* A pointer to the base of a table of references to declaration
140 scopes. This table is a display which tracks the nesting
141 of declaration scopes at the current scope and containing
142 scopes. This table is used to find the proper place to
143 define type declaration DIE's. */
144 static GTY(()) vec<tree, va_gc> *decl_scope_table;
146 /* Pointers to various DWARF2 sections. */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_skeleton_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_skeleton_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_addr_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_skeleton_line_section;
156 static GTY(()) section *debug_loc_section;
157 static GTY(()) section *debug_pubnames_section;
158 static GTY(()) section *debug_pubtypes_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_str_dwo_section;
161 static GTY(()) section *debug_str_offsets_section;
162 static GTY(()) section *debug_ranges_section;
163 static GTY(()) section *debug_frame_section;
165 /* Maximum size (in bytes) of an artificially generated label. */
166 #define MAX_ARTIFICIAL_LABEL_BYTES 30
168 /* According to the (draft) DWARF 3 specification, the initial length
169 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
170 bytes are 0xffffffff, followed by the length stored in the next 8
171 bytes.
173 However, the SGI/MIPS ABI uses an initial length which is equal to
174 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
176 #ifndef DWARF_INITIAL_LENGTH_SIZE
177 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
178 #endif
180 /* Round SIZE up to the nearest BOUNDARY. */
181 #define DWARF_ROUND(SIZE,BOUNDARY) \
182 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
184 /* CIE identifier. */
185 #if HOST_BITS_PER_WIDE_INT >= 64
186 #define DWARF_CIE_ID \
187 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
188 #else
189 #define DWARF_CIE_ID DW_CIE_ID
190 #endif
193 /* A vector for a table that contains frame description
194 information for each routine. */
195 #define NOT_INDEXED (-1U)
196 #define NO_INDEX_ASSIGNED (-2U)
198 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
200 struct GTY(()) indirect_string_node {
201 const char *str;
202 unsigned int refcount;
203 enum dwarf_form form;
204 char *label;
205 unsigned int index;
208 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
210 /* With split_debug_info, both the comp_dir and dwo_name go in the
211 main object file, rather than the dwo, similar to the force_direct
212 parameter elsewhere but with additional complications:
214 1) The string is needed in both the main object file and the dwo.
215 That is, the comp_dir and dwo_name will appear in both places.
217 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
218 DW_FORM_GNU_str_index.
220 3) GCC chooses the form to use late, depending on the size and
221 reference count.
223 Rather than forcing the all debug string handling functions and
224 callers to deal with these complications, simply use a separate,
225 special-cased string table for any attribute that should go in the
226 main object file. This limits the complexity to just the places
227 that need it. */
229 static GTY ((param_is (struct indirect_string_node)))
230 htab_t skeleton_debug_str_hash;
232 static GTY(()) int dw2_string_counter;
234 /* True if the compilation unit places functions in more than one section. */
235 static GTY(()) bool have_multiple_function_sections = false;
237 /* Whether the default text and cold text sections have been used at all. */
239 static GTY(()) bool text_section_used = false;
240 static GTY(()) bool cold_text_section_used = false;
242 /* The default cold text section. */
243 static GTY(()) section *cold_text_section;
245 /* Forward declarations for functions defined in this file. */
247 static char *stripattributes (const char *);
248 static void output_call_frame_info (int);
249 static void dwarf2out_note_section_used (void);
251 /* Personality decl of current unit. Used only when assembler does not support
252 personality CFI. */
253 static GTY(()) rtx current_unit_personality;
255 /* Data and reference forms for relocatable data. */
256 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
257 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
259 #ifndef DEBUG_FRAME_SECTION
260 #define DEBUG_FRAME_SECTION ".debug_frame"
261 #endif
263 #ifndef FUNC_BEGIN_LABEL
264 #define FUNC_BEGIN_LABEL "LFB"
265 #endif
267 #ifndef FUNC_END_LABEL
268 #define FUNC_END_LABEL "LFE"
269 #endif
271 #ifndef PROLOGUE_END_LABEL
272 #define PROLOGUE_END_LABEL "LPE"
273 #endif
275 #ifndef EPILOGUE_BEGIN_LABEL
276 #define EPILOGUE_BEGIN_LABEL "LEB"
277 #endif
279 #ifndef FRAME_BEGIN_LABEL
280 #define FRAME_BEGIN_LABEL "Lframe"
281 #endif
282 #define CIE_AFTER_SIZE_LABEL "LSCIE"
283 #define CIE_END_LABEL "LECIE"
284 #define FDE_LABEL "LSFDE"
285 #define FDE_AFTER_SIZE_LABEL "LASFDE"
286 #define FDE_END_LABEL "LEFDE"
287 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
288 #define LINE_NUMBER_END_LABEL "LELT"
289 #define LN_PROLOG_AS_LABEL "LASLTP"
290 #define LN_PROLOG_END_LABEL "LELTP"
291 #define DIE_LABEL_PREFIX "DW"
293 /* Match the base name of a file to the base name of a compilation unit. */
295 static int
296 matches_main_base (const char *path)
298 /* Cache the last query. */
299 static const char *last_path = NULL;
300 static int last_match = 0;
301 if (path != last_path)
303 const char *base;
304 int length = base_of_path (path, &base);
305 last_path = path;
306 last_match = (length == main_input_baselength
307 && memcmp (base, main_input_basename, length) == 0);
309 return last_match;
312 #ifdef DEBUG_DEBUG_STRUCT
314 static int
315 dump_struct_debug (tree type, enum debug_info_usage usage,
316 enum debug_struct_file criterion, int generic,
317 int matches, int result)
319 /* Find the type name. */
320 tree type_decl = TYPE_STUB_DECL (type);
321 tree t = type_decl;
322 const char *name = 0;
323 if (TREE_CODE (t) == TYPE_DECL)
324 t = DECL_NAME (t);
325 if (t)
326 name = IDENTIFIER_POINTER (t);
328 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
329 criterion,
330 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
331 matches ? "bas" : "hdr",
332 generic ? "gen" : "ord",
333 usage == DINFO_USAGE_DFN ? ";" :
334 usage == DINFO_USAGE_DIR_USE ? "." : "*",
335 result,
336 (void*) type_decl, name);
337 return result;
339 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
340 dump_struct_debug (type, usage, criterion, generic, matches, result)
342 #else
344 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
345 (result)
347 #endif
349 static bool
350 should_emit_struct_debug (tree type, enum debug_info_usage usage)
352 enum debug_struct_file criterion;
353 tree type_decl;
354 bool generic = lang_hooks.types.generic_p (type);
356 if (generic)
357 criterion = debug_struct_generic[usage];
358 else
359 criterion = debug_struct_ordinary[usage];
361 if (criterion == DINFO_STRUCT_FILE_NONE)
362 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
363 if (criterion == DINFO_STRUCT_FILE_ANY)
364 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
366 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
368 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
369 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
371 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
372 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
373 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
376 /* Return a pointer to a copy of the section string name S with all
377 attributes stripped off, and an asterisk prepended (for assemble_name). */
379 static inline char *
380 stripattributes (const char *s)
382 char *stripped = XNEWVEC (char, strlen (s) + 2);
383 char *p = stripped;
385 *p++ = '*';
387 while (*s && *s != ',')
388 *p++ = *s++;
390 *p = '\0';
391 return stripped;
394 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
395 switch to the data section instead, and write out a synthetic start label
396 for collect2 the first time around. */
398 static void
399 switch_to_eh_frame_section (bool back)
401 tree label;
403 #ifdef EH_FRAME_SECTION_NAME
404 if (eh_frame_section == 0)
406 int flags;
408 if (EH_TABLES_CAN_BE_READ_ONLY)
410 int fde_encoding;
411 int per_encoding;
412 int lsda_encoding;
414 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
415 /*global=*/0);
416 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
417 /*global=*/1);
418 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
419 /*global=*/0);
420 flags = ((! flag_pic
421 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
422 && (fde_encoding & 0x70) != DW_EH_PE_aligned
423 && (per_encoding & 0x70) != DW_EH_PE_absptr
424 && (per_encoding & 0x70) != DW_EH_PE_aligned
425 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
426 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
427 ? 0 : SECTION_WRITE);
429 else
430 flags = SECTION_WRITE;
431 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
433 #endif /* EH_FRAME_SECTION_NAME */
435 if (eh_frame_section)
436 switch_to_section (eh_frame_section);
437 else
439 /* We have no special eh_frame section. Put the information in
440 the data section and emit special labels to guide collect2. */
441 switch_to_section (data_section);
443 if (!back)
445 label = get_file_function_name ("F");
446 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
447 targetm.asm_out.globalize_label (asm_out_file,
448 IDENTIFIER_POINTER (label));
449 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
454 /* Switch [BACK] to the eh or debug frame table section, depending on
455 FOR_EH. */
457 static void
458 switch_to_frame_table_section (int for_eh, bool back)
460 if (for_eh)
461 switch_to_eh_frame_section (back);
462 else
464 if (!debug_frame_section)
465 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
466 SECTION_DEBUG, NULL);
467 switch_to_section (debug_frame_section);
471 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
473 enum dw_cfi_oprnd_type
474 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
476 switch (cfi)
478 case DW_CFA_nop:
479 case DW_CFA_GNU_window_save:
480 case DW_CFA_remember_state:
481 case DW_CFA_restore_state:
482 return dw_cfi_oprnd_unused;
484 case DW_CFA_set_loc:
485 case DW_CFA_advance_loc1:
486 case DW_CFA_advance_loc2:
487 case DW_CFA_advance_loc4:
488 case DW_CFA_MIPS_advance_loc8:
489 return dw_cfi_oprnd_addr;
491 case DW_CFA_offset:
492 case DW_CFA_offset_extended:
493 case DW_CFA_def_cfa:
494 case DW_CFA_offset_extended_sf:
495 case DW_CFA_def_cfa_sf:
496 case DW_CFA_restore:
497 case DW_CFA_restore_extended:
498 case DW_CFA_undefined:
499 case DW_CFA_same_value:
500 case DW_CFA_def_cfa_register:
501 case DW_CFA_register:
502 case DW_CFA_expression:
503 return dw_cfi_oprnd_reg_num;
505 case DW_CFA_def_cfa_offset:
506 case DW_CFA_GNU_args_size:
507 case DW_CFA_def_cfa_offset_sf:
508 return dw_cfi_oprnd_offset;
510 case DW_CFA_def_cfa_expression:
511 return dw_cfi_oprnd_loc;
513 default:
514 gcc_unreachable ();
518 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
520 enum dw_cfi_oprnd_type
521 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
523 switch (cfi)
525 case DW_CFA_def_cfa:
526 case DW_CFA_def_cfa_sf:
527 case DW_CFA_offset:
528 case DW_CFA_offset_extended_sf:
529 case DW_CFA_offset_extended:
530 return dw_cfi_oprnd_offset;
532 case DW_CFA_register:
533 return dw_cfi_oprnd_reg_num;
535 case DW_CFA_expression:
536 return dw_cfi_oprnd_loc;
538 default:
539 return dw_cfi_oprnd_unused;
543 /* Output one FDE. */
545 static void
546 output_fde (dw_fde_ref fde, bool for_eh, bool second,
547 char *section_start_label, int fde_encoding, char *augmentation,
548 bool any_lsda_needed, int lsda_encoding)
550 const char *begin, *end;
551 static unsigned int j;
552 char l1[20], l2[20];
554 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
555 /* empty */ 0);
556 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
557 for_eh + j);
558 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
559 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
560 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
561 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
562 " indicating 64-bit DWARF extension");
563 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
564 "FDE Length");
565 ASM_OUTPUT_LABEL (asm_out_file, l1);
567 if (for_eh)
568 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
569 else
570 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
571 debug_frame_section, "FDE CIE offset");
573 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
574 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
576 if (for_eh)
578 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
579 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
580 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
581 "FDE initial location");
582 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
583 end, begin, "FDE address range");
585 else
587 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
588 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
591 if (augmentation[0])
593 if (any_lsda_needed)
595 int size = size_of_encoded_value (lsda_encoding);
597 if (lsda_encoding == DW_EH_PE_aligned)
599 int offset = ( 4 /* Length */
600 + 4 /* CIE offset */
601 + 2 * size_of_encoded_value (fde_encoding)
602 + 1 /* Augmentation size */ );
603 int pad = -offset & (PTR_SIZE - 1);
605 size += pad;
606 gcc_assert (size_of_uleb128 (size) == 1);
609 dw2_asm_output_data_uleb128 (size, "Augmentation size");
611 if (fde->uses_eh_lsda)
613 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
614 fde->funcdef_number);
615 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
616 gen_rtx_SYMBOL_REF (Pmode, l1),
617 false,
618 "Language Specific Data Area");
620 else
622 if (lsda_encoding == DW_EH_PE_aligned)
623 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
624 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
625 "Language Specific Data Area (none)");
628 else
629 dw2_asm_output_data_uleb128 (0, "Augmentation size");
632 /* Loop through the Call Frame Instructions associated with this FDE. */
633 fde->dw_fde_current_label = begin;
635 size_t from, until, i;
637 from = 0;
638 until = vec_safe_length (fde->dw_fde_cfi);
640 if (fde->dw_fde_second_begin == NULL)
642 else if (!second)
643 until = fde->dw_fde_switch_cfi_index;
644 else
645 from = fde->dw_fde_switch_cfi_index;
647 for (i = from; i < until; i++)
648 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
651 /* If we are to emit a ref/link from function bodies to their frame tables,
652 do it now. This is typically performed to make sure that tables
653 associated with functions are dragged with them and not discarded in
654 garbage collecting links. We need to do this on a per function basis to
655 cope with -ffunction-sections. */
657 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
658 /* Switch to the function section, emit the ref to the tables, and
659 switch *back* into the table section. */
660 switch_to_section (function_section (fde->decl));
661 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
662 switch_to_frame_table_section (for_eh, true);
663 #endif
665 /* Pad the FDE out to an address sized boundary. */
666 ASM_OUTPUT_ALIGN (asm_out_file,
667 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
668 ASM_OUTPUT_LABEL (asm_out_file, l2);
670 j += 2;
673 /* Return true if frame description entry FDE is needed for EH. */
675 static bool
676 fde_needed_for_eh_p (dw_fde_ref fde)
678 if (flag_asynchronous_unwind_tables)
679 return true;
681 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
682 return true;
684 if (fde->uses_eh_lsda)
685 return true;
687 /* If exceptions are enabled, we have collected nothrow info. */
688 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
689 return false;
691 return true;
694 /* Output the call frame information used to record information
695 that relates to calculating the frame pointer, and records the
696 location of saved registers. */
698 static void
699 output_call_frame_info (int for_eh)
701 unsigned int i;
702 dw_fde_ref fde;
703 dw_cfi_ref cfi;
704 char l1[20], l2[20], section_start_label[20];
705 bool any_lsda_needed = false;
706 char augmentation[6];
707 int augmentation_size;
708 int fde_encoding = DW_EH_PE_absptr;
709 int per_encoding = DW_EH_PE_absptr;
710 int lsda_encoding = DW_EH_PE_absptr;
711 int return_reg;
712 rtx personality = NULL;
713 int dw_cie_version;
715 /* Don't emit a CIE if there won't be any FDEs. */
716 if (!fde_vec)
717 return;
719 /* Nothing to do if the assembler's doing it all. */
720 if (dwarf2out_do_cfi_asm ())
721 return;
723 /* If we don't have any functions we'll want to unwind out of, don't emit
724 any EH unwind information. If we make FDEs linkonce, we may have to
725 emit an empty label for an FDE that wouldn't otherwise be emitted. We
726 want to avoid having an FDE kept around when the function it refers to
727 is discarded. Example where this matters: a primary function template
728 in C++ requires EH information, an explicit specialization doesn't. */
729 if (for_eh)
731 bool any_eh_needed = false;
733 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
735 if (fde->uses_eh_lsda)
736 any_eh_needed = any_lsda_needed = true;
737 else if (fde_needed_for_eh_p (fde))
738 any_eh_needed = true;
739 else if (TARGET_USES_WEAK_UNWIND_INFO)
740 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
743 if (!any_eh_needed)
744 return;
747 /* We're going to be generating comments, so turn on app. */
748 if (flag_debug_asm)
749 app_enable ();
751 /* Switch to the proper frame section, first time. */
752 switch_to_frame_table_section (for_eh, false);
754 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
755 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
757 /* Output the CIE. */
758 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
759 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
760 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
761 dw2_asm_output_data (4, 0xffffffff,
762 "Initial length escape value indicating 64-bit DWARF extension");
763 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
764 "Length of Common Information Entry");
765 ASM_OUTPUT_LABEL (asm_out_file, l1);
767 /* Now that the CIE pointer is PC-relative for EH,
768 use 0 to identify the CIE. */
769 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
770 (for_eh ? 0 : DWARF_CIE_ID),
771 "CIE Identifier Tag");
773 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
774 use CIE version 1, unless that would produce incorrect results
775 due to overflowing the return register column. */
776 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
777 dw_cie_version = 1;
778 if (return_reg >= 256 || dwarf_version > 2)
779 dw_cie_version = 3;
780 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
782 augmentation[0] = 0;
783 augmentation_size = 0;
785 personality = current_unit_personality;
786 if (for_eh)
788 char *p;
790 /* Augmentation:
791 z Indicates that a uleb128 is present to size the
792 augmentation section.
793 L Indicates the encoding (and thus presence) of
794 an LSDA pointer in the FDE augmentation.
795 R Indicates a non-default pointer encoding for
796 FDE code pointers.
797 P Indicates the presence of an encoding + language
798 personality routine in the CIE augmentation. */
800 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
801 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
802 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
804 p = augmentation + 1;
805 if (personality)
807 *p++ = 'P';
808 augmentation_size += 1 + size_of_encoded_value (per_encoding);
809 assemble_external_libcall (personality);
811 if (any_lsda_needed)
813 *p++ = 'L';
814 augmentation_size += 1;
816 if (fde_encoding != DW_EH_PE_absptr)
818 *p++ = 'R';
819 augmentation_size += 1;
821 if (p > augmentation + 1)
823 augmentation[0] = 'z';
824 *p = '\0';
827 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
828 if (personality && per_encoding == DW_EH_PE_aligned)
830 int offset = ( 4 /* Length */
831 + 4 /* CIE Id */
832 + 1 /* CIE version */
833 + strlen (augmentation) + 1 /* Augmentation */
834 + size_of_uleb128 (1) /* Code alignment */
835 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
836 + 1 /* RA column */
837 + 1 /* Augmentation size */
838 + 1 /* Personality encoding */ );
839 int pad = -offset & (PTR_SIZE - 1);
841 augmentation_size += pad;
843 /* Augmentations should be small, so there's scarce need to
844 iterate for a solution. Die if we exceed one uleb128 byte. */
845 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
849 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
850 if (dw_cie_version >= 4)
852 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
853 dw2_asm_output_data (1, 0, "CIE Segment Size");
855 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
856 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
857 "CIE Data Alignment Factor");
859 if (dw_cie_version == 1)
860 dw2_asm_output_data (1, return_reg, "CIE RA Column");
861 else
862 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
864 if (augmentation[0])
866 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
867 if (personality)
869 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
870 eh_data_format_name (per_encoding));
871 dw2_asm_output_encoded_addr_rtx (per_encoding,
872 personality,
873 true, NULL);
876 if (any_lsda_needed)
877 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
878 eh_data_format_name (lsda_encoding));
880 if (fde_encoding != DW_EH_PE_absptr)
881 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
882 eh_data_format_name (fde_encoding));
885 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
886 output_cfi (cfi, NULL, for_eh);
888 /* Pad the CIE out to an address sized boundary. */
889 ASM_OUTPUT_ALIGN (asm_out_file,
890 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
891 ASM_OUTPUT_LABEL (asm_out_file, l2);
893 /* Loop through all of the FDE's. */
894 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
896 unsigned int k;
898 /* Don't emit EH unwind info for leaf functions that don't need it. */
899 if (for_eh && !fde_needed_for_eh_p (fde))
900 continue;
902 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
903 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
904 augmentation, any_lsda_needed, lsda_encoding);
907 if (for_eh && targetm.terminate_dw2_eh_frame_info)
908 dw2_asm_output_data (4, 0, "End of Table");
910 /* Turn off app to make assembly quicker. */
911 if (flag_debug_asm)
912 app_disable ();
915 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
917 static void
918 dwarf2out_do_cfi_startproc (bool second)
920 int enc;
921 rtx ref;
922 rtx personality = get_personality_function (current_function_decl);
924 fprintf (asm_out_file, "\t.cfi_startproc\n");
926 if (personality)
928 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
929 ref = personality;
931 /* ??? The GAS support isn't entirely consistent. We have to
932 handle indirect support ourselves, but PC-relative is done
933 in the assembler. Further, the assembler can't handle any
934 of the weirder relocation types. */
935 if (enc & DW_EH_PE_indirect)
936 ref = dw2_force_const_mem (ref, true);
938 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
939 output_addr_const (asm_out_file, ref);
940 fputc ('\n', asm_out_file);
943 if (crtl->uses_eh_lsda)
945 char lab[20];
947 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
948 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
949 current_function_funcdef_no);
950 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
951 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
953 if (enc & DW_EH_PE_indirect)
954 ref = dw2_force_const_mem (ref, true);
956 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
957 output_addr_const (asm_out_file, ref);
958 fputc ('\n', asm_out_file);
962 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
963 this allocation may be done before pass_final. */
965 dw_fde_ref
966 dwarf2out_alloc_current_fde (void)
968 dw_fde_ref fde;
970 fde = ggc_alloc_cleared_dw_fde_node ();
971 fde->decl = current_function_decl;
972 fde->funcdef_number = current_function_funcdef_no;
973 fde->fde_index = vec_safe_length (fde_vec);
974 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
975 fde->uses_eh_lsda = crtl->uses_eh_lsda;
976 fde->nothrow = crtl->nothrow;
977 fde->drap_reg = INVALID_REGNUM;
978 fde->vdrap_reg = INVALID_REGNUM;
980 /* Record the FDE associated with this function. */
981 cfun->fde = fde;
982 vec_safe_push (fde_vec, fde);
984 return fde;
987 /* Output a marker (i.e. a label) for the beginning of a function, before
988 the prologue. */
990 void
991 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
992 const char *file ATTRIBUTE_UNUSED)
994 char label[MAX_ARTIFICIAL_LABEL_BYTES];
995 char * dup_label;
996 dw_fde_ref fde;
997 section *fnsec;
998 bool do_frame;
1000 current_function_func_begin_label = NULL;
1002 do_frame = dwarf2out_do_frame ();
1004 /* ??? current_function_func_begin_label is also used by except.c for
1005 call-site information. We must emit this label if it might be used. */
1006 if (!do_frame
1007 && (!flag_exceptions
1008 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1009 return;
1011 fnsec = function_section (current_function_decl);
1012 switch_to_section (fnsec);
1013 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1014 current_function_funcdef_no);
1015 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1016 current_function_funcdef_no);
1017 dup_label = xstrdup (label);
1018 current_function_func_begin_label = dup_label;
1020 /* We can elide the fde allocation if we're not emitting debug info. */
1021 if (!do_frame)
1022 return;
1024 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1025 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1026 would include pass_dwarf2_frame. If we've not created the FDE yet,
1027 do so now. */
1028 fde = cfun->fde;
1029 if (fde == NULL)
1030 fde = dwarf2out_alloc_current_fde ();
1032 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1033 fde->dw_fde_begin = dup_label;
1034 fde->dw_fde_current_label = dup_label;
1035 fde->in_std_section = (fnsec == text_section
1036 || (cold_text_section && fnsec == cold_text_section));
1038 /* We only want to output line number information for the genuine dwarf2
1039 prologue case, not the eh frame case. */
1040 #ifdef DWARF2_DEBUGGING_INFO
1041 if (file)
1042 dwarf2out_source_line (line, file, 0, true);
1043 #endif
1045 if (dwarf2out_do_cfi_asm ())
1046 dwarf2out_do_cfi_startproc (false);
1047 else
1049 rtx personality = get_personality_function (current_function_decl);
1050 if (!current_unit_personality)
1051 current_unit_personality = personality;
1053 /* We cannot keep a current personality per function as without CFI
1054 asm, at the point where we emit the CFI data, there is no current
1055 function anymore. */
1056 if (personality && current_unit_personality != personality)
1057 sorry ("multiple EH personalities are supported only with assemblers "
1058 "supporting .cfi_personality directive");
1062 /* Output a marker (i.e. a label) for the end of the generated code
1063 for a function prologue. This gets called *after* the prologue code has
1064 been generated. */
1066 void
1067 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1068 const char *file ATTRIBUTE_UNUSED)
1070 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1072 /* Output a label to mark the endpoint of the code generated for this
1073 function. */
1074 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1075 current_function_funcdef_no);
1076 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1077 current_function_funcdef_no);
1078 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1081 /* Output a marker (i.e. a label) for the beginning of the generated code
1082 for a function epilogue. This gets called *before* the prologue code has
1083 been generated. */
1085 void
1086 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087 const char *file ATTRIBUTE_UNUSED)
1089 dw_fde_ref fde = cfun->fde;
1090 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092 if (fde->dw_fde_vms_begin_epilogue)
1093 return;
1095 /* Output a label to mark the endpoint of the code generated for this
1096 function. */
1097 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1098 current_function_funcdef_no);
1099 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1100 current_function_funcdef_no);
1101 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1104 /* Output a marker (i.e. a label) for the absolute end of the generated code
1105 for a function definition. This gets called *after* the epilogue code has
1106 been generated. */
1108 void
1109 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 dw_fde_ref fde;
1113 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115 last_var_location_insn = NULL_RTX;
1116 cached_next_real_insn = NULL_RTX;
1118 if (dwarf2out_do_cfi_asm ())
1119 fprintf (asm_out_file, "\t.cfi_endproc\n");
1121 /* Output a label to mark the endpoint of the code generated for this
1122 function. */
1123 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1124 current_function_funcdef_no);
1125 ASM_OUTPUT_LABEL (asm_out_file, label);
1126 fde = cfun->fde;
1127 gcc_assert (fde != NULL);
1128 if (fde->dw_fde_second_begin == NULL)
1129 fde->dw_fde_end = xstrdup (label);
1132 void
1133 dwarf2out_frame_finish (void)
1135 /* Output call frame information. */
1136 if (targetm.debug_unwind_info () == UI_DWARF2)
1137 output_call_frame_info (0);
1139 /* Output another copy for the unwinder. */
1140 if ((flag_unwind_tables || flag_exceptions)
1141 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1142 output_call_frame_info (1);
1145 /* Note that the current function section is being used for code. */
1147 static void
1148 dwarf2out_note_section_used (void)
1150 section *sec = current_function_section ();
1151 if (sec == text_section)
1152 text_section_used = true;
1153 else if (sec == cold_text_section)
1154 cold_text_section_used = true;
1157 static void var_location_switch_text_section (void);
1158 static void set_cur_line_info_table (section *);
1160 void
1161 dwarf2out_switch_text_section (void)
1163 section *sect;
1164 dw_fde_ref fde = cfun->fde;
1166 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1168 if (!in_cold_section_p)
1170 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1171 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1172 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1174 else
1176 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1177 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1178 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1180 have_multiple_function_sections = true;
1182 /* There is no need to mark used sections when not debugging. */
1183 if (cold_text_section != NULL)
1184 dwarf2out_note_section_used ();
1186 if (dwarf2out_do_cfi_asm ())
1187 fprintf (asm_out_file, "\t.cfi_endproc\n");
1189 /* Now do the real section switch. */
1190 sect = current_function_section ();
1191 switch_to_section (sect);
1193 fde->second_in_std_section
1194 = (sect == text_section
1195 || (cold_text_section && sect == cold_text_section));
1197 if (dwarf2out_do_cfi_asm ())
1198 dwarf2out_do_cfi_startproc (true);
1200 var_location_switch_text_section ();
1202 if (cold_text_section != NULL)
1203 set_cur_line_info_table (sect);
1206 /* And now, the subset of the debugging information support code necessary
1207 for emitting location expressions. */
1209 /* Data about a single source file. */
1210 struct GTY(()) dwarf_file_data {
1211 const char * filename;
1212 int emitted_number;
1215 typedef struct GTY(()) deferred_locations_struct
1217 tree variable;
1218 dw_die_ref die;
1219 } deferred_locations;
1222 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1225 /* Describe an entry into the .debug_addr section. */
1227 enum ate_kind {
1228 ate_kind_rtx,
1229 ate_kind_rtx_dtprel,
1230 ate_kind_label
1233 typedef struct GTY(()) addr_table_entry_struct {
1234 enum ate_kind kind;
1235 unsigned int refcount;
1236 unsigned int index;
1237 union addr_table_entry_struct_union
1239 rtx GTY ((tag ("0"))) rtl;
1240 char * GTY ((tag ("1"))) label;
1242 GTY ((desc ("%1.kind"))) addr;
1244 addr_table_entry;
1246 /* Location lists are ranges + location descriptions for that range,
1247 so you can track variables that are in different places over
1248 their entire life. */
1249 typedef struct GTY(()) dw_loc_list_struct {
1250 dw_loc_list_ref dw_loc_next;
1251 const char *begin; /* Label and addr_entry for start of range */
1252 addr_table_entry *begin_entry;
1253 const char *end; /* Label for end of range */
1254 char *ll_symbol; /* Label for beginning of location list.
1255 Only on head of list */
1256 const char *section; /* Section this loclist is relative to */
1257 dw_loc_descr_ref expr;
1258 hashval_t hash;
1259 /* True if all addresses in this and subsequent lists are known to be
1260 resolved. */
1261 bool resolved_addr;
1262 /* True if this list has been replaced by dw_loc_next. */
1263 bool replaced;
1264 bool emitted;
1265 /* True if the range should be emitted even if begin and end
1266 are the same. */
1267 bool force;
1268 } dw_loc_list_node;
1270 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1272 /* Convert a DWARF stack opcode into its string name. */
1274 static const char *
1275 dwarf_stack_op_name (unsigned int op)
1277 const char *name = get_DW_OP_name (op);
1279 if (name != NULL)
1280 return name;
1282 return "OP_<unknown>";
1285 /* Return a pointer to a newly allocated location description. Location
1286 descriptions are simple expression terms that can be strung
1287 together to form more complicated location (address) descriptions. */
1289 static inline dw_loc_descr_ref
1290 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1291 unsigned HOST_WIDE_INT oprnd2)
1293 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1295 descr->dw_loc_opc = op;
1296 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1297 descr->dw_loc_oprnd1.val_entry = NULL;
1298 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1299 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1300 descr->dw_loc_oprnd2.val_entry = NULL;
1301 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1303 return descr;
1306 /* Return a pointer to a newly allocated location description for
1307 REG and OFFSET. */
1309 static inline dw_loc_descr_ref
1310 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1312 if (reg <= 31)
1313 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1314 offset, 0);
1315 else
1316 return new_loc_descr (DW_OP_bregx, reg, offset);
1319 /* Add a location description term to a location description expression. */
1321 static inline void
1322 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1324 dw_loc_descr_ref *d;
1326 /* Find the end of the chain. */
1327 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1330 *d = descr;
1333 /* Compare two location operands for exact equality. */
1335 static bool
1336 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1338 if (a->val_class != b->val_class)
1339 return false;
1340 switch (a->val_class)
1342 case dw_val_class_none:
1343 return true;
1344 case dw_val_class_addr:
1345 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1347 case dw_val_class_offset:
1348 case dw_val_class_unsigned_const:
1349 case dw_val_class_const:
1350 case dw_val_class_range_list:
1351 case dw_val_class_lineptr:
1352 case dw_val_class_macptr:
1353 /* These are all HOST_WIDE_INT, signed or unsigned. */
1354 return a->v.val_unsigned == b->v.val_unsigned;
1356 case dw_val_class_loc:
1357 return a->v.val_loc == b->v.val_loc;
1358 case dw_val_class_loc_list:
1359 return a->v.val_loc_list == b->v.val_loc_list;
1360 case dw_val_class_die_ref:
1361 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1362 case dw_val_class_fde_ref:
1363 return a->v.val_fde_index == b->v.val_fde_index;
1364 case dw_val_class_lbl_id:
1365 case dw_val_class_high_pc:
1366 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1367 case dw_val_class_str:
1368 return a->v.val_str == b->v.val_str;
1369 case dw_val_class_flag:
1370 return a->v.val_flag == b->v.val_flag;
1371 case dw_val_class_file:
1372 return a->v.val_file == b->v.val_file;
1373 case dw_val_class_decl_ref:
1374 return a->v.val_decl_ref == b->v.val_decl_ref;
1376 case dw_val_class_const_double:
1377 return (a->v.val_double.high == b->v.val_double.high
1378 && a->v.val_double.low == b->v.val_double.low);
1380 case dw_val_class_vec:
1382 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1383 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1385 return (a_len == b_len
1386 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1389 case dw_val_class_data8:
1390 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1392 case dw_val_class_vms_delta:
1393 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1394 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1396 gcc_unreachable ();
1399 /* Compare two location atoms for exact equality. */
1401 static bool
1402 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1404 if (a->dw_loc_opc != b->dw_loc_opc)
1405 return false;
1407 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1408 address size, but since we always allocate cleared storage it
1409 should be zero for other types of locations. */
1410 if (a->dtprel != b->dtprel)
1411 return false;
1413 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1414 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1417 /* Compare two complete location expressions for exact equality. */
1419 bool
1420 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1422 while (1)
1424 if (a == b)
1425 return true;
1426 if (a == NULL || b == NULL)
1427 return false;
1428 if (!loc_descr_equal_p_1 (a, b))
1429 return false;
1431 a = a->dw_loc_next;
1432 b = b->dw_loc_next;
1437 /* Add a constant OFFSET to a location expression. */
1439 static void
1440 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1442 dw_loc_descr_ref loc;
1443 HOST_WIDE_INT *p;
1445 gcc_assert (*list_head != NULL);
1447 if (!offset)
1448 return;
1450 /* Find the end of the chain. */
1451 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1454 p = NULL;
1455 if (loc->dw_loc_opc == DW_OP_fbreg
1456 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1457 p = &loc->dw_loc_oprnd1.v.val_int;
1458 else if (loc->dw_loc_opc == DW_OP_bregx)
1459 p = &loc->dw_loc_oprnd2.v.val_int;
1461 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1462 offset. Don't optimize if an signed integer overflow would happen. */
1463 if (p != NULL
1464 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1465 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1466 *p += offset;
1468 else if (offset > 0)
1469 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1471 else
1473 loc->dw_loc_next = int_loc_descriptor (-offset);
1474 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1478 /* Add a constant OFFSET to a location list. */
1480 static void
1481 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1483 dw_loc_list_ref d;
1484 for (d = list_head; d != NULL; d = d->dw_loc_next)
1485 loc_descr_plus_const (&d->expr, offset);
1488 #define DWARF_REF_SIZE \
1489 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1491 static unsigned long int get_base_type_offset (dw_die_ref);
1493 /* Return the size of a location descriptor. */
1495 static unsigned long
1496 size_of_loc_descr (dw_loc_descr_ref loc)
1498 unsigned long size = 1;
1500 switch (loc->dw_loc_opc)
1502 case DW_OP_addr:
1503 size += DWARF2_ADDR_SIZE;
1504 break;
1505 case DW_OP_GNU_addr_index:
1506 case DW_OP_GNU_const_index:
1507 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1508 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1509 break;
1510 case DW_OP_const1u:
1511 case DW_OP_const1s:
1512 size += 1;
1513 break;
1514 case DW_OP_const2u:
1515 case DW_OP_const2s:
1516 size += 2;
1517 break;
1518 case DW_OP_const4u:
1519 case DW_OP_const4s:
1520 size += 4;
1521 break;
1522 case DW_OP_const8u:
1523 case DW_OP_const8s:
1524 size += 8;
1525 break;
1526 case DW_OP_constu:
1527 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1528 break;
1529 case DW_OP_consts:
1530 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1531 break;
1532 case DW_OP_pick:
1533 size += 1;
1534 break;
1535 case DW_OP_plus_uconst:
1536 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1537 break;
1538 case DW_OP_skip:
1539 case DW_OP_bra:
1540 size += 2;
1541 break;
1542 case DW_OP_breg0:
1543 case DW_OP_breg1:
1544 case DW_OP_breg2:
1545 case DW_OP_breg3:
1546 case DW_OP_breg4:
1547 case DW_OP_breg5:
1548 case DW_OP_breg6:
1549 case DW_OP_breg7:
1550 case DW_OP_breg8:
1551 case DW_OP_breg9:
1552 case DW_OP_breg10:
1553 case DW_OP_breg11:
1554 case DW_OP_breg12:
1555 case DW_OP_breg13:
1556 case DW_OP_breg14:
1557 case DW_OP_breg15:
1558 case DW_OP_breg16:
1559 case DW_OP_breg17:
1560 case DW_OP_breg18:
1561 case DW_OP_breg19:
1562 case DW_OP_breg20:
1563 case DW_OP_breg21:
1564 case DW_OP_breg22:
1565 case DW_OP_breg23:
1566 case DW_OP_breg24:
1567 case DW_OP_breg25:
1568 case DW_OP_breg26:
1569 case DW_OP_breg27:
1570 case DW_OP_breg28:
1571 case DW_OP_breg29:
1572 case DW_OP_breg30:
1573 case DW_OP_breg31:
1574 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1575 break;
1576 case DW_OP_regx:
1577 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1578 break;
1579 case DW_OP_fbreg:
1580 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1581 break;
1582 case DW_OP_bregx:
1583 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1584 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1585 break;
1586 case DW_OP_piece:
1587 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1588 break;
1589 case DW_OP_bit_piece:
1590 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1591 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1592 break;
1593 case DW_OP_deref_size:
1594 case DW_OP_xderef_size:
1595 size += 1;
1596 break;
1597 case DW_OP_call2:
1598 size += 2;
1599 break;
1600 case DW_OP_call4:
1601 size += 4;
1602 break;
1603 case DW_OP_call_ref:
1604 size += DWARF_REF_SIZE;
1605 break;
1606 case DW_OP_implicit_value:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1608 + loc->dw_loc_oprnd1.v.val_unsigned;
1609 break;
1610 case DW_OP_GNU_implicit_pointer:
1611 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1612 break;
1613 case DW_OP_GNU_entry_value:
1615 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1616 size += size_of_uleb128 (op_size) + op_size;
1617 break;
1619 case DW_OP_GNU_const_type:
1621 unsigned long o
1622 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1623 size += size_of_uleb128 (o) + 1;
1624 switch (loc->dw_loc_oprnd2.val_class)
1626 case dw_val_class_vec:
1627 size += loc->dw_loc_oprnd2.v.val_vec.length
1628 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1629 break;
1630 case dw_val_class_const:
1631 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1632 break;
1633 case dw_val_class_const_double:
1634 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1635 break;
1636 default:
1637 gcc_unreachable ();
1639 break;
1641 case DW_OP_GNU_regval_type:
1643 unsigned long o
1644 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1645 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1646 + size_of_uleb128 (o);
1648 break;
1649 case DW_OP_GNU_deref_type:
1651 unsigned long o
1652 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1653 size += 1 + size_of_uleb128 (o);
1655 break;
1656 case DW_OP_GNU_convert:
1657 case DW_OP_GNU_reinterpret:
1658 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1659 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1660 else
1662 unsigned long o
1663 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1664 size += size_of_uleb128 (o);
1666 break;
1667 case DW_OP_GNU_parameter_ref:
1668 size += 4;
1669 break;
1670 default:
1671 break;
1674 return size;
1677 /* Return the size of a series of location descriptors. */
1679 unsigned long
1680 size_of_locs (dw_loc_descr_ref loc)
1682 dw_loc_descr_ref l;
1683 unsigned long size;
1685 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1686 field, to avoid writing to a PCH file. */
1687 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1689 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1690 break;
1691 size += size_of_loc_descr (l);
1693 if (! l)
1694 return size;
1696 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1698 l->dw_loc_addr = size;
1699 size += size_of_loc_descr (l);
1702 return size;
1705 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1706 static void get_ref_die_offset_label (char *, dw_die_ref);
1707 static unsigned long int get_ref_die_offset (dw_die_ref);
1709 /* Output location description stack opcode's operands (if any).
1710 The for_eh_or_skip parameter controls whether register numbers are
1711 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1712 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1713 info). This should be suppressed for the cases that have not been converted
1714 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1716 static void
1717 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1719 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1720 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1722 switch (loc->dw_loc_opc)
1724 #ifdef DWARF2_DEBUGGING_INFO
1725 case DW_OP_const2u:
1726 case DW_OP_const2s:
1727 dw2_asm_output_data (2, val1->v.val_int, NULL);
1728 break;
1729 case DW_OP_const4u:
1730 if (loc->dtprel)
1732 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1733 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1734 val1->v.val_addr);
1735 fputc ('\n', asm_out_file);
1736 break;
1738 /* FALLTHRU */
1739 case DW_OP_const4s:
1740 dw2_asm_output_data (4, val1->v.val_int, NULL);
1741 break;
1742 case DW_OP_const8u:
1743 if (loc->dtprel)
1745 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1746 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1747 val1->v.val_addr);
1748 fputc ('\n', asm_out_file);
1749 break;
1751 /* FALLTHRU */
1752 case DW_OP_const8s:
1753 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1754 dw2_asm_output_data (8, val1->v.val_int, NULL);
1755 break;
1756 case DW_OP_skip:
1757 case DW_OP_bra:
1759 int offset;
1761 gcc_assert (val1->val_class == dw_val_class_loc);
1762 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1764 dw2_asm_output_data (2, offset, NULL);
1766 break;
1767 case DW_OP_implicit_value:
1768 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1769 switch (val2->val_class)
1771 case dw_val_class_const:
1772 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1773 break;
1774 case dw_val_class_vec:
1776 unsigned int elt_size = val2->v.val_vec.elt_size;
1777 unsigned int len = val2->v.val_vec.length;
1778 unsigned int i;
1779 unsigned char *p;
1781 if (elt_size > sizeof (HOST_WIDE_INT))
1783 elt_size /= 2;
1784 len *= 2;
1786 for (i = 0, p = val2->v.val_vec.array;
1787 i < len;
1788 i++, p += elt_size)
1789 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1790 "fp or vector constant word %u", i);
1792 break;
1793 case dw_val_class_const_double:
1795 unsigned HOST_WIDE_INT first, second;
1797 if (WORDS_BIG_ENDIAN)
1799 first = val2->v.val_double.high;
1800 second = val2->v.val_double.low;
1802 else
1804 first = val2->v.val_double.low;
1805 second = val2->v.val_double.high;
1807 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1808 first, NULL);
1809 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1810 second, NULL);
1812 break;
1813 case dw_val_class_addr:
1814 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1815 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1816 break;
1817 default:
1818 gcc_unreachable ();
1820 break;
1821 #else
1822 case DW_OP_const2u:
1823 case DW_OP_const2s:
1824 case DW_OP_const4u:
1825 case DW_OP_const4s:
1826 case DW_OP_const8u:
1827 case DW_OP_const8s:
1828 case DW_OP_skip:
1829 case DW_OP_bra:
1830 case DW_OP_implicit_value:
1831 /* We currently don't make any attempt to make sure these are
1832 aligned properly like we do for the main unwind info, so
1833 don't support emitting things larger than a byte if we're
1834 only doing unwinding. */
1835 gcc_unreachable ();
1836 #endif
1837 case DW_OP_const1u:
1838 case DW_OP_const1s:
1839 dw2_asm_output_data (1, val1->v.val_int, NULL);
1840 break;
1841 case DW_OP_constu:
1842 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1843 break;
1844 case DW_OP_consts:
1845 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1846 break;
1847 case DW_OP_pick:
1848 dw2_asm_output_data (1, val1->v.val_int, NULL);
1849 break;
1850 case DW_OP_plus_uconst:
1851 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1852 break;
1853 case DW_OP_breg0:
1854 case DW_OP_breg1:
1855 case DW_OP_breg2:
1856 case DW_OP_breg3:
1857 case DW_OP_breg4:
1858 case DW_OP_breg5:
1859 case DW_OP_breg6:
1860 case DW_OP_breg7:
1861 case DW_OP_breg8:
1862 case DW_OP_breg9:
1863 case DW_OP_breg10:
1864 case DW_OP_breg11:
1865 case DW_OP_breg12:
1866 case DW_OP_breg13:
1867 case DW_OP_breg14:
1868 case DW_OP_breg15:
1869 case DW_OP_breg16:
1870 case DW_OP_breg17:
1871 case DW_OP_breg18:
1872 case DW_OP_breg19:
1873 case DW_OP_breg20:
1874 case DW_OP_breg21:
1875 case DW_OP_breg22:
1876 case DW_OP_breg23:
1877 case DW_OP_breg24:
1878 case DW_OP_breg25:
1879 case DW_OP_breg26:
1880 case DW_OP_breg27:
1881 case DW_OP_breg28:
1882 case DW_OP_breg29:
1883 case DW_OP_breg30:
1884 case DW_OP_breg31:
1885 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1886 break;
1887 case DW_OP_regx:
1889 unsigned r = val1->v.val_unsigned;
1890 if (for_eh_or_skip >= 0)
1891 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1892 gcc_assert (size_of_uleb128 (r)
1893 == size_of_uleb128 (val1->v.val_unsigned));
1894 dw2_asm_output_data_uleb128 (r, NULL);
1896 break;
1897 case DW_OP_fbreg:
1898 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_bregx:
1902 unsigned r = val1->v.val_unsigned;
1903 if (for_eh_or_skip >= 0)
1904 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1905 gcc_assert (size_of_uleb128 (r)
1906 == size_of_uleb128 (val1->v.val_unsigned));
1907 dw2_asm_output_data_uleb128 (r, NULL);
1908 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1910 break;
1911 case DW_OP_piece:
1912 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1913 break;
1914 case DW_OP_bit_piece:
1915 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1916 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1917 break;
1918 case DW_OP_deref_size:
1919 case DW_OP_xderef_size:
1920 dw2_asm_output_data (1, val1->v.val_int, NULL);
1921 break;
1923 case DW_OP_addr:
1924 if (loc->dtprel)
1926 if (targetm.asm_out.output_dwarf_dtprel)
1928 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1929 DWARF2_ADDR_SIZE,
1930 val1->v.val_addr);
1931 fputc ('\n', asm_out_file);
1933 else
1934 gcc_unreachable ();
1936 else
1938 #ifdef DWARF2_DEBUGGING_INFO
1939 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1940 #else
1941 gcc_unreachable ();
1942 #endif
1944 break;
1946 case DW_OP_GNU_addr_index:
1947 case DW_OP_GNU_const_index:
1948 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1949 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1950 "(index into .debug_addr)");
1951 break;
1953 case DW_OP_GNU_implicit_pointer:
1955 char label[MAX_ARTIFICIAL_LABEL_BYTES
1956 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1957 gcc_assert (val1->val_class == dw_val_class_die_ref);
1958 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1959 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1960 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1962 break;
1964 case DW_OP_GNU_entry_value:
1965 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1966 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1967 break;
1969 case DW_OP_GNU_const_type:
1971 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1972 gcc_assert (o);
1973 dw2_asm_output_data_uleb128 (o, NULL);
1974 switch (val2->val_class)
1976 case dw_val_class_const:
1977 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1978 dw2_asm_output_data (1, l, NULL);
1979 dw2_asm_output_data (l, val2->v.val_int, NULL);
1980 break;
1981 case dw_val_class_vec:
1983 unsigned int elt_size = val2->v.val_vec.elt_size;
1984 unsigned int len = val2->v.val_vec.length;
1985 unsigned int i;
1986 unsigned char *p;
1988 l = len * elt_size;
1989 dw2_asm_output_data (1, l, NULL);
1990 if (elt_size > sizeof (HOST_WIDE_INT))
1992 elt_size /= 2;
1993 len *= 2;
1995 for (i = 0, p = val2->v.val_vec.array;
1996 i < len;
1997 i++, p += elt_size)
1998 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1999 "fp or vector constant word %u", i);
2001 break;
2002 case dw_val_class_const_double:
2004 unsigned HOST_WIDE_INT first, second;
2005 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2007 dw2_asm_output_data (1, 2 * l, NULL);
2008 if (WORDS_BIG_ENDIAN)
2010 first = val2->v.val_double.high;
2011 second = val2->v.val_double.low;
2013 else
2015 first = val2->v.val_double.low;
2016 second = val2->v.val_double.high;
2018 dw2_asm_output_data (l, first, NULL);
2019 dw2_asm_output_data (l, second, NULL);
2021 break;
2022 default:
2023 gcc_unreachable ();
2026 break;
2027 case DW_OP_GNU_regval_type:
2029 unsigned r = val1->v.val_unsigned;
2030 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2031 gcc_assert (o);
2032 if (for_eh_or_skip >= 0)
2034 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2035 gcc_assert (size_of_uleb128 (r)
2036 == size_of_uleb128 (val1->v.val_unsigned));
2038 dw2_asm_output_data_uleb128 (r, NULL);
2039 dw2_asm_output_data_uleb128 (o, NULL);
2041 break;
2042 case DW_OP_GNU_deref_type:
2044 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2045 gcc_assert (o);
2046 dw2_asm_output_data (1, val1->v.val_int, NULL);
2047 dw2_asm_output_data_uleb128 (o, NULL);
2049 break;
2050 case DW_OP_GNU_convert:
2051 case DW_OP_GNU_reinterpret:
2052 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2053 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2054 else
2056 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2057 gcc_assert (o);
2058 dw2_asm_output_data_uleb128 (o, NULL);
2060 break;
2062 case DW_OP_GNU_parameter_ref:
2064 unsigned long o;
2065 gcc_assert (val1->val_class == dw_val_class_die_ref);
2066 o = get_ref_die_offset (val1->v.val_die_ref.die);
2067 dw2_asm_output_data (4, o, NULL);
2069 break;
2071 default:
2072 /* Other codes have no operands. */
2073 break;
2077 /* Output a sequence of location operations.
2078 The for_eh_or_skip parameter controls whether register numbers are
2079 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2080 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2081 info). This should be suppressed for the cases that have not been converted
2082 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2084 void
2085 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2087 for (; loc != NULL; loc = loc->dw_loc_next)
2089 enum dwarf_location_atom opc = loc->dw_loc_opc;
2090 /* Output the opcode. */
2091 if (for_eh_or_skip >= 0
2092 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2094 unsigned r = (opc - DW_OP_breg0);
2095 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2096 gcc_assert (r <= 31);
2097 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2099 else if (for_eh_or_skip >= 0
2100 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2102 unsigned r = (opc - DW_OP_reg0);
2103 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2104 gcc_assert (r <= 31);
2105 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2108 dw2_asm_output_data (1, opc,
2109 "%s", dwarf_stack_op_name (opc));
2111 /* Output the operand(s) (if any). */
2112 output_loc_operands (loc, for_eh_or_skip);
2116 /* Output location description stack opcode's operands (if any).
2117 The output is single bytes on a line, suitable for .cfi_escape. */
2119 static void
2120 output_loc_operands_raw (dw_loc_descr_ref loc)
2122 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2123 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2125 switch (loc->dw_loc_opc)
2127 case DW_OP_addr:
2128 case DW_OP_GNU_addr_index:
2129 case DW_OP_GNU_const_index:
2130 case DW_OP_implicit_value:
2131 /* We cannot output addresses in .cfi_escape, only bytes. */
2132 gcc_unreachable ();
2134 case DW_OP_const1u:
2135 case DW_OP_const1s:
2136 case DW_OP_pick:
2137 case DW_OP_deref_size:
2138 case DW_OP_xderef_size:
2139 fputc (',', asm_out_file);
2140 dw2_asm_output_data_raw (1, val1->v.val_int);
2141 break;
2143 case DW_OP_const2u:
2144 case DW_OP_const2s:
2145 fputc (',', asm_out_file);
2146 dw2_asm_output_data_raw (2, val1->v.val_int);
2147 break;
2149 case DW_OP_const4u:
2150 case DW_OP_const4s:
2151 fputc (',', asm_out_file);
2152 dw2_asm_output_data_raw (4, val1->v.val_int);
2153 break;
2155 case DW_OP_const8u:
2156 case DW_OP_const8s:
2157 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2158 fputc (',', asm_out_file);
2159 dw2_asm_output_data_raw (8, val1->v.val_int);
2160 break;
2162 case DW_OP_skip:
2163 case DW_OP_bra:
2165 int offset;
2167 gcc_assert (val1->val_class == dw_val_class_loc);
2168 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_raw (2, offset);
2173 break;
2175 case DW_OP_regx:
2177 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2178 gcc_assert (size_of_uleb128 (r)
2179 == size_of_uleb128 (val1->v.val_unsigned));
2180 fputc (',', asm_out_file);
2181 dw2_asm_output_data_uleb128_raw (r);
2183 break;
2185 case DW_OP_constu:
2186 case DW_OP_plus_uconst:
2187 case DW_OP_piece:
2188 fputc (',', asm_out_file);
2189 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2190 break;
2192 case DW_OP_bit_piece:
2193 fputc (',', asm_out_file);
2194 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2195 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2196 break;
2198 case DW_OP_consts:
2199 case DW_OP_breg0:
2200 case DW_OP_breg1:
2201 case DW_OP_breg2:
2202 case DW_OP_breg3:
2203 case DW_OP_breg4:
2204 case DW_OP_breg5:
2205 case DW_OP_breg6:
2206 case DW_OP_breg7:
2207 case DW_OP_breg8:
2208 case DW_OP_breg9:
2209 case DW_OP_breg10:
2210 case DW_OP_breg11:
2211 case DW_OP_breg12:
2212 case DW_OP_breg13:
2213 case DW_OP_breg14:
2214 case DW_OP_breg15:
2215 case DW_OP_breg16:
2216 case DW_OP_breg17:
2217 case DW_OP_breg18:
2218 case DW_OP_breg19:
2219 case DW_OP_breg20:
2220 case DW_OP_breg21:
2221 case DW_OP_breg22:
2222 case DW_OP_breg23:
2223 case DW_OP_breg24:
2224 case DW_OP_breg25:
2225 case DW_OP_breg26:
2226 case DW_OP_breg27:
2227 case DW_OP_breg28:
2228 case DW_OP_breg29:
2229 case DW_OP_breg30:
2230 case DW_OP_breg31:
2231 case DW_OP_fbreg:
2232 fputc (',', asm_out_file);
2233 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2234 break;
2236 case DW_OP_bregx:
2238 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2239 gcc_assert (size_of_uleb128 (r)
2240 == size_of_uleb128 (val1->v.val_unsigned));
2241 fputc (',', asm_out_file);
2242 dw2_asm_output_data_uleb128_raw (r);
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2246 break;
2248 case DW_OP_GNU_implicit_pointer:
2249 case DW_OP_GNU_entry_value:
2250 case DW_OP_GNU_const_type:
2251 case DW_OP_GNU_regval_type:
2252 case DW_OP_GNU_deref_type:
2253 case DW_OP_GNU_convert:
2254 case DW_OP_GNU_reinterpret:
2255 case DW_OP_GNU_parameter_ref:
2256 gcc_unreachable ();
2257 break;
2259 default:
2260 /* Other codes have no operands. */
2261 break;
2265 void
2266 output_loc_sequence_raw (dw_loc_descr_ref loc)
2268 while (1)
2270 enum dwarf_location_atom opc = loc->dw_loc_opc;
2271 /* Output the opcode. */
2272 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2274 unsigned r = (opc - DW_OP_breg0);
2275 r = DWARF2_FRAME_REG_OUT (r, 1);
2276 gcc_assert (r <= 31);
2277 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2279 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2281 unsigned r = (opc - DW_OP_reg0);
2282 r = DWARF2_FRAME_REG_OUT (r, 1);
2283 gcc_assert (r <= 31);
2284 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2286 /* Output the opcode. */
2287 fprintf (asm_out_file, "%#x", opc);
2288 output_loc_operands_raw (loc);
2290 if (!loc->dw_loc_next)
2291 break;
2292 loc = loc->dw_loc_next;
2294 fputc (',', asm_out_file);
2298 /* This function builds a dwarf location descriptor sequence from a
2299 dw_cfa_location, adding the given OFFSET to the result of the
2300 expression. */
2302 struct dw_loc_descr_struct *
2303 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2305 struct dw_loc_descr_struct *head, *tmp;
2307 offset += cfa->offset;
2309 if (cfa->indirect)
2311 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2312 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2313 head->dw_loc_oprnd1.val_entry = NULL;
2314 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2315 add_loc_descr (&head, tmp);
2316 if (offset != 0)
2318 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2319 add_loc_descr (&head, tmp);
2322 else
2323 head = new_reg_loc_descr (cfa->reg, offset);
2325 return head;
2328 /* This function builds a dwarf location descriptor sequence for
2329 the address at OFFSET from the CFA when stack is aligned to
2330 ALIGNMENT byte. */
2332 struct dw_loc_descr_struct *
2333 build_cfa_aligned_loc (dw_cfa_location *cfa,
2334 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2336 struct dw_loc_descr_struct *head;
2337 unsigned int dwarf_fp
2338 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2340 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2341 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2343 head = new_reg_loc_descr (dwarf_fp, 0);
2344 add_loc_descr (&head, int_loc_descriptor (alignment));
2345 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2346 loc_descr_plus_const (&head, offset);
2348 else
2349 head = new_reg_loc_descr (dwarf_fp, offset);
2350 return head;
2353 /* And now, the support for symbolic debugging information. */
2355 /* .debug_str support. */
2356 static int output_indirect_string (void **, void *);
2358 static void dwarf2out_init (const char *);
2359 static void dwarf2out_finish (const char *);
2360 static void dwarf2out_assembly_start (void);
2361 static void dwarf2out_define (unsigned int, const char *);
2362 static void dwarf2out_undef (unsigned int, const char *);
2363 static void dwarf2out_start_source_file (unsigned, const char *);
2364 static void dwarf2out_end_source_file (unsigned);
2365 static void dwarf2out_function_decl (tree);
2366 static void dwarf2out_begin_block (unsigned, unsigned);
2367 static void dwarf2out_end_block (unsigned, unsigned);
2368 static bool dwarf2out_ignore_block (const_tree);
2369 static void dwarf2out_global_decl (tree);
2370 static void dwarf2out_type_decl (tree, int);
2371 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2372 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2373 dw_die_ref);
2374 static void dwarf2out_abstract_function (tree);
2375 static void dwarf2out_var_location (rtx);
2376 static void dwarf2out_begin_function (tree);
2377 static void dwarf2out_end_function (unsigned int);
2378 static void dwarf2out_set_name (tree, tree);
2380 /* The debug hooks structure. */
2382 const struct gcc_debug_hooks dwarf2_debug_hooks =
2384 dwarf2out_init,
2385 dwarf2out_finish,
2386 dwarf2out_assembly_start,
2387 dwarf2out_define,
2388 dwarf2out_undef,
2389 dwarf2out_start_source_file,
2390 dwarf2out_end_source_file,
2391 dwarf2out_begin_block,
2392 dwarf2out_end_block,
2393 dwarf2out_ignore_block,
2394 dwarf2out_source_line,
2395 dwarf2out_begin_prologue,
2396 #if VMS_DEBUGGING_INFO
2397 dwarf2out_vms_end_prologue,
2398 dwarf2out_vms_begin_epilogue,
2399 #else
2400 debug_nothing_int_charstar,
2401 debug_nothing_int_charstar,
2402 #endif
2403 dwarf2out_end_epilogue,
2404 dwarf2out_begin_function,
2405 dwarf2out_end_function, /* end_function */
2406 dwarf2out_function_decl, /* function_decl */
2407 dwarf2out_global_decl,
2408 dwarf2out_type_decl, /* type_decl */
2409 dwarf2out_imported_module_or_decl,
2410 debug_nothing_tree, /* deferred_inline_function */
2411 /* The DWARF 2 backend tries to reduce debugging bloat by not
2412 emitting the abstract description of inline functions until
2413 something tries to reference them. */
2414 dwarf2out_abstract_function, /* outlining_inline_function */
2415 debug_nothing_rtx, /* label */
2416 debug_nothing_int, /* handle_pch */
2417 dwarf2out_var_location,
2418 dwarf2out_switch_text_section,
2419 dwarf2out_set_name,
2420 1, /* start_end_main_source_file */
2421 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2424 /* NOTE: In the comments in this file, many references are made to
2425 "Debugging Information Entries". This term is abbreviated as `DIE'
2426 throughout the remainder of this file. */
2428 /* An internal representation of the DWARF output is built, and then
2429 walked to generate the DWARF debugging info. The walk of the internal
2430 representation is done after the entire program has been compiled.
2431 The types below are used to describe the internal representation. */
2433 /* Whether to put type DIEs into their own section .debug_types instead
2434 of making them part of the .debug_info section. Only supported for
2435 Dwarf V4 or higher and the user didn't disable them through
2436 -fno-debug-types-section. It is more efficient to put them in a
2437 separate comdat sections since the linker will then be able to
2438 remove duplicates. But not all tools support .debug_types sections
2439 yet. */
2441 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2443 /* Various DIE's use offsets relative to the beginning of the
2444 .debug_info section to refer to each other. */
2446 typedef long int dw_offset;
2448 /* Define typedefs here to avoid circular dependencies. */
2450 typedef struct dw_attr_struct *dw_attr_ref;
2451 typedef struct dw_line_info_struct *dw_line_info_ref;
2452 typedef struct pubname_struct *pubname_ref;
2453 typedef struct dw_ranges_struct *dw_ranges_ref;
2454 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2455 typedef struct comdat_type_struct *comdat_type_node_ref;
2457 /* The entries in the line_info table more-or-less mirror the opcodes
2458 that are used in the real dwarf line table. Arrays of these entries
2459 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2460 supported. */
2462 enum dw_line_info_opcode {
2463 /* Emit DW_LNE_set_address; the operand is the label index. */
2464 LI_set_address,
2466 /* Emit a row to the matrix with the given line. This may be done
2467 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2468 special opcodes. */
2469 LI_set_line,
2471 /* Emit a DW_LNS_set_file. */
2472 LI_set_file,
2474 /* Emit a DW_LNS_set_column. */
2475 LI_set_column,
2477 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2478 LI_negate_stmt,
2480 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2481 LI_set_prologue_end,
2482 LI_set_epilogue_begin,
2484 /* Emit a DW_LNE_set_discriminator. */
2485 LI_set_discriminator
2488 typedef struct GTY(()) dw_line_info_struct {
2489 enum dw_line_info_opcode opcode;
2490 unsigned int val;
2491 } dw_line_info_entry;
2494 typedef struct GTY(()) dw_line_info_table_struct {
2495 /* The label that marks the end of this section. */
2496 const char *end_label;
2498 /* The values for the last row of the matrix, as collected in the table.
2499 These are used to minimize the changes to the next row. */
2500 unsigned int file_num;
2501 unsigned int line_num;
2502 unsigned int column_num;
2503 int discrim_num;
2504 bool is_stmt;
2505 bool in_use;
2507 vec<dw_line_info_entry, va_gc> *entries;
2508 } dw_line_info_table;
2510 typedef dw_line_info_table *dw_line_info_table_p;
2513 /* Each DIE attribute has a field specifying the attribute kind,
2514 a link to the next attribute in the chain, and an attribute value.
2515 Attributes are typically linked below the DIE they modify. */
2517 typedef struct GTY(()) dw_attr_struct {
2518 enum dwarf_attribute dw_attr;
2519 dw_val_node dw_attr_val;
2521 dw_attr_node;
2524 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2525 The children of each node form a circular list linked by
2526 die_sib. die_child points to the node *before* the "first" child node. */
2528 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2529 union die_symbol_or_type_node
2531 const char * GTY ((tag ("0"))) die_symbol;
2532 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2534 GTY ((desc ("%0.comdat_type_p"))) die_id;
2535 vec<dw_attr_node, va_gc> *die_attr;
2536 dw_die_ref die_parent;
2537 dw_die_ref die_child;
2538 dw_die_ref die_sib;
2539 dw_die_ref die_definition; /* ref from a specification to its definition */
2540 dw_offset die_offset;
2541 unsigned long die_abbrev;
2542 int die_mark;
2543 unsigned int decl_id;
2544 enum dwarf_tag die_tag;
2545 /* Die is used and must not be pruned as unused. */
2546 BOOL_BITFIELD die_perennial_p : 1;
2547 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2548 /* Lots of spare bits. */
2550 die_node;
2552 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2553 #define FOR_EACH_CHILD(die, c, expr) do { \
2554 c = die->die_child; \
2555 if (c) do { \
2556 c = c->die_sib; \
2557 expr; \
2558 } while (c != die->die_child); \
2559 } while (0)
2561 /* The pubname structure */
2563 typedef struct GTY(()) pubname_struct {
2564 dw_die_ref die;
2565 const char *name;
2567 pubname_entry;
2570 struct GTY(()) dw_ranges_struct {
2571 /* If this is positive, it's a block number, otherwise it's a
2572 bitwise-negated index into dw_ranges_by_label. */
2573 int num;
2576 /* A structure to hold a macinfo entry. */
2578 typedef struct GTY(()) macinfo_struct {
2579 unsigned char code;
2580 unsigned HOST_WIDE_INT lineno;
2581 const char *info;
2583 macinfo_entry;
2586 struct GTY(()) dw_ranges_by_label_struct {
2587 const char *begin;
2588 const char *end;
2591 /* The comdat type node structure. */
2592 typedef struct GTY(()) comdat_type_struct
2594 dw_die_ref root_die;
2595 dw_die_ref type_die;
2596 dw_die_ref skeleton_die;
2597 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2598 struct comdat_type_struct *next;
2600 comdat_type_node;
2602 /* The limbo die list structure. */
2603 typedef struct GTY(()) limbo_die_struct {
2604 dw_die_ref die;
2605 tree created_for;
2606 struct limbo_die_struct *next;
2608 limbo_die_node;
2610 typedef struct skeleton_chain_struct
2612 dw_die_ref old_die;
2613 dw_die_ref new_die;
2614 struct skeleton_chain_struct *parent;
2616 skeleton_chain_node;
2618 /* Define a macro which returns nonzero for a TYPE_DECL which was
2619 implicitly generated for a type.
2621 Note that, unlike the C front-end (which generates a NULL named
2622 TYPE_DECL node for each complete tagged type, each array type,
2623 and each function type node created) the C++ front-end generates
2624 a _named_ TYPE_DECL node for each tagged type node created.
2625 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2626 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2627 front-end, but for each type, tagged or not. */
2629 #define TYPE_DECL_IS_STUB(decl) \
2630 (DECL_NAME (decl) == NULL_TREE \
2631 || (DECL_ARTIFICIAL (decl) \
2632 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2633 /* This is necessary for stub decls that \
2634 appear in nested inline functions. */ \
2635 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2636 && (decl_ultimate_origin (decl) \
2637 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2639 /* Information concerning the compilation unit's programming
2640 language, and compiler version. */
2642 /* Fixed size portion of the DWARF compilation unit header. */
2643 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2644 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2646 /* Fixed size portion of the DWARF comdat type unit header. */
2647 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2648 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2649 + DWARF_OFFSET_SIZE)
2651 /* Fixed size portion of public names info. */
2652 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2654 /* Fixed size portion of the address range info. */
2655 #define DWARF_ARANGES_HEADER_SIZE \
2656 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2657 DWARF2_ADDR_SIZE * 2) \
2658 - DWARF_INITIAL_LENGTH_SIZE)
2660 /* Size of padding portion in the address range info. It must be
2661 aligned to twice the pointer size. */
2662 #define DWARF_ARANGES_PAD_SIZE \
2663 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2664 DWARF2_ADDR_SIZE * 2) \
2665 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2667 /* Use assembler line directives if available. */
2668 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2669 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2670 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2671 #else
2672 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2673 #endif
2674 #endif
2676 /* Minimum line offset in a special line info. opcode.
2677 This value was chosen to give a reasonable range of values. */
2678 #define DWARF_LINE_BASE -10
2680 /* First special line opcode - leave room for the standard opcodes. */
2681 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2683 /* Range of line offsets in a special line info. opcode. */
2684 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2686 /* Flag that indicates the initial value of the is_stmt_start flag.
2687 In the present implementation, we do not mark any lines as
2688 the beginning of a source statement, because that information
2689 is not made available by the GCC front-end. */
2690 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2692 /* Maximum number of operations per instruction bundle. */
2693 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2694 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2695 #endif
2697 /* This location is used by calc_die_sizes() to keep track
2698 the offset of each DIE within the .debug_info section. */
2699 static unsigned long next_die_offset;
2701 /* Record the root of the DIE's built for the current compilation unit. */
2702 static GTY(()) dw_die_ref single_comp_unit_die;
2704 /* A list of type DIEs that have been separated into comdat sections. */
2705 static GTY(()) comdat_type_node *comdat_type_list;
2707 /* A list of DIEs with a NULL parent waiting to be relocated. */
2708 static GTY(()) limbo_die_node *limbo_die_list;
2710 /* A list of DIEs for which we may have to generate
2711 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2712 static GTY(()) limbo_die_node *deferred_asm_name;
2714 /* Filenames referenced by this compilation unit. */
2715 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2717 /* A hash table of references to DIE's that describe declarations.
2718 The key is a DECL_UID() which is a unique number identifying each decl. */
2719 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2721 /* A hash table of references to DIE's that describe COMMON blocks.
2722 The key is DECL_UID() ^ die_parent. */
2723 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2725 typedef struct GTY(()) die_arg_entry_struct {
2726 dw_die_ref die;
2727 tree arg;
2728 } die_arg_entry;
2731 /* Node of the variable location list. */
2732 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2733 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2734 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2735 in mode of the EXPR_LIST node and first EXPR_LIST operand
2736 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2737 location or NULL for padding. For larger bitsizes,
2738 mode is 0 and first operand is a CONCAT with bitsize
2739 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2740 NULL as second operand. */
2741 rtx GTY (()) loc;
2742 const char * GTY (()) label;
2743 struct var_loc_node * GTY (()) next;
2746 /* Variable location list. */
2747 struct GTY (()) var_loc_list_def {
2748 struct var_loc_node * GTY (()) first;
2750 /* Pointer to the last but one or last element of the
2751 chained list. If the list is empty, both first and
2752 last are NULL, if the list contains just one node
2753 or the last node certainly is not redundant, it points
2754 to the last node, otherwise points to the last but one.
2755 Do not mark it for GC because it is marked through the chain. */
2756 struct var_loc_node * GTY ((skip ("%h"))) last;
2758 /* Pointer to the last element before section switch,
2759 if NULL, either sections weren't switched or first
2760 is after section switch. */
2761 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2763 /* DECL_UID of the variable decl. */
2764 unsigned int decl_id;
2766 typedef struct var_loc_list_def var_loc_list;
2768 /* Call argument location list. */
2769 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2770 rtx GTY (()) call_arg_loc_note;
2771 const char * GTY (()) label;
2772 tree GTY (()) block;
2773 bool tail_call_p;
2774 rtx GTY (()) symbol_ref;
2775 struct call_arg_loc_node * GTY (()) next;
2779 /* Table of decl location linked lists. */
2780 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2782 /* Head and tail of call_arg_loc chain. */
2783 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2784 static struct call_arg_loc_node *call_arg_loc_last;
2786 /* Number of call sites in the current function. */
2787 static int call_site_count = -1;
2788 /* Number of tail call sites in the current function. */
2789 static int tail_call_site_count = -1;
2791 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2792 DIEs. */
2793 static vec<dw_die_ref> block_map;
2795 /* A cached location list. */
2796 struct GTY (()) cached_dw_loc_list_def {
2797 /* The DECL_UID of the decl that this entry describes. */
2798 unsigned int decl_id;
2800 /* The cached location list. */
2801 dw_loc_list_ref loc_list;
2803 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2805 /* Table of cached location lists. */
2806 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2808 /* A pointer to the base of a list of references to DIE's that
2809 are uniquely identified by their tag, presence/absence of
2810 children DIE's, and list of attribute/value pairs. */
2811 static GTY((length ("abbrev_die_table_allocated")))
2812 dw_die_ref *abbrev_die_table;
2814 /* Number of elements currently allocated for abbrev_die_table. */
2815 static GTY(()) unsigned abbrev_die_table_allocated;
2817 /* Number of elements in type_die_table currently in use. */
2818 static GTY(()) unsigned abbrev_die_table_in_use;
2820 /* Size (in elements) of increments by which we may expand the
2821 abbrev_die_table. */
2822 #define ABBREV_DIE_TABLE_INCREMENT 256
2824 /* A global counter for generating labels for line number data. */
2825 static unsigned int line_info_label_num;
2827 /* The current table to which we should emit line number information
2828 for the current function. This will be set up at the beginning of
2829 assembly for the function. */
2830 static dw_line_info_table *cur_line_info_table;
2832 /* The two default tables of line number info. */
2833 static GTY(()) dw_line_info_table *text_section_line_info;
2834 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2836 /* The set of all non-default tables of line number info. */
2837 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2839 /* A flag to tell pubnames/types export if there is an info section to
2840 refer to. */
2841 static bool info_section_emitted;
2843 /* A pointer to the base of a table that contains a list of publicly
2844 accessible names. */
2845 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2847 /* A pointer to the base of a table that contains a list of publicly
2848 accessible types. */
2849 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2851 /* A pointer to the base of a table that contains a list of macro
2852 defines/undefines (and file start/end markers). */
2853 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2855 /* True if .debug_macinfo or .debug_macros section is going to be
2856 emitted. */
2857 #define have_macinfo \
2858 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2859 && !macinfo_table->is_empty ())
2861 /* Array of dies for which we should generate .debug_ranges info. */
2862 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2864 /* Number of elements currently allocated for ranges_table. */
2865 static GTY(()) unsigned ranges_table_allocated;
2867 /* Number of elements in ranges_table currently in use. */
2868 static GTY(()) unsigned ranges_table_in_use;
2870 /* Array of pairs of labels referenced in ranges_table. */
2871 static GTY ((length ("ranges_by_label_allocated")))
2872 dw_ranges_by_label_ref ranges_by_label;
2874 /* Number of elements currently allocated for ranges_by_label. */
2875 static GTY(()) unsigned ranges_by_label_allocated;
2877 /* Number of elements in ranges_by_label currently in use. */
2878 static GTY(()) unsigned ranges_by_label_in_use;
2880 /* Size (in elements) of increments by which we may expand the
2881 ranges_table. */
2882 #define RANGES_TABLE_INCREMENT 64
2884 /* Whether we have location lists that need outputting */
2885 static GTY(()) bool have_location_lists;
2887 /* Unique label counter. */
2888 static GTY(()) unsigned int loclabel_num;
2890 /* Unique label counter for point-of-call tables. */
2891 static GTY(()) unsigned int poc_label_num;
2893 /* Record whether the function being analyzed contains inlined functions. */
2894 static int current_function_has_inlines;
2896 /* The last file entry emitted by maybe_emit_file(). */
2897 static GTY(()) struct dwarf_file_data * last_emitted_file;
2899 /* Number of internal labels generated by gen_internal_sym(). */
2900 static GTY(()) int label_num;
2902 /* Cached result of previous call to lookup_filename. */
2903 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2905 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2907 /* Instances of generic types for which we need to generate debug
2908 info that describe their generic parameters and arguments. That
2909 generation needs to happen once all types are properly laid out so
2910 we do it at the end of compilation. */
2911 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2913 /* Offset from the "steady-state frame pointer" to the frame base,
2914 within the current function. */
2915 static HOST_WIDE_INT frame_pointer_fb_offset;
2916 static bool frame_pointer_fb_offset_valid;
2918 static vec<dw_die_ref> base_types;
2920 /* Forward declarations for functions defined in this file. */
2922 static int is_pseudo_reg (const_rtx);
2923 static tree type_main_variant (tree);
2924 static int is_tagged_type (const_tree);
2925 static const char *dwarf_tag_name (unsigned);
2926 static const char *dwarf_attr_name (unsigned);
2927 static const char *dwarf_form_name (unsigned);
2928 static tree decl_ultimate_origin (const_tree);
2929 static tree decl_class_context (tree);
2930 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2931 static inline enum dw_val_class AT_class (dw_attr_ref);
2932 static inline unsigned int AT_index (dw_attr_ref);
2933 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2934 static inline unsigned AT_flag (dw_attr_ref);
2935 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2936 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2937 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2938 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2939 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2940 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2941 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2942 unsigned int, unsigned char *);
2943 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2944 static hashval_t debug_str_do_hash (const void *);
2945 static int debug_str_eq (const void *, const void *);
2946 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2947 static inline const char *AT_string (dw_attr_ref);
2948 static enum dwarf_form AT_string_form (dw_attr_ref);
2949 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2950 static void add_AT_specification (dw_die_ref, dw_die_ref);
2951 static inline dw_die_ref AT_ref (dw_attr_ref);
2952 static inline int AT_ref_external (dw_attr_ref);
2953 static inline void set_AT_ref_external (dw_attr_ref, int);
2954 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2955 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2956 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2957 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2958 dw_loc_list_ref);
2959 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2960 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2961 static void remove_addr_table_entry (addr_table_entry *);
2962 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2963 static inline rtx AT_addr (dw_attr_ref);
2964 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2965 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2966 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2967 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2968 unsigned HOST_WIDE_INT);
2969 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2970 unsigned long, bool);
2971 static inline const char *AT_lbl (dw_attr_ref);
2972 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2973 static const char *get_AT_low_pc (dw_die_ref);
2974 static const char *get_AT_hi_pc (dw_die_ref);
2975 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2976 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2977 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2978 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2979 static bool is_cxx (void);
2980 static bool is_fortran (void);
2981 static bool is_ada (void);
2982 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2983 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2984 static void add_child_die (dw_die_ref, dw_die_ref);
2985 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2986 static dw_die_ref lookup_type_die (tree);
2987 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2988 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2989 static void equate_type_number_to_die (tree, dw_die_ref);
2990 static hashval_t decl_die_table_hash (const void *);
2991 static int decl_die_table_eq (const void *, const void *);
2992 static dw_die_ref lookup_decl_die (tree);
2993 static hashval_t common_block_die_table_hash (const void *);
2994 static int common_block_die_table_eq (const void *, const void *);
2995 static hashval_t decl_loc_table_hash (const void *);
2996 static int decl_loc_table_eq (const void *, const void *);
2997 static var_loc_list *lookup_decl_loc (const_tree);
2998 static void equate_decl_number_to_die (tree, dw_die_ref);
2999 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3000 static void print_spaces (FILE *);
3001 static void print_die (dw_die_ref, FILE *);
3002 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3003 static dw_die_ref pop_compile_unit (dw_die_ref);
3004 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3005 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3006 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3007 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3008 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3009 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3010 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3011 struct md5_ctx *, int *);
3012 struct checksum_attributes;
3013 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3014 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3015 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3016 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3017 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3018 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3019 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3020 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3021 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3022 static void compute_section_prefix (dw_die_ref);
3023 static int is_type_die (dw_die_ref);
3024 static int is_comdat_die (dw_die_ref);
3025 static int is_symbol_die (dw_die_ref);
3026 static inline bool is_template_instantiation (dw_die_ref);
3027 static void assign_symbol_names (dw_die_ref);
3028 static void break_out_includes (dw_die_ref);
3029 static int is_declaration_die (dw_die_ref);
3030 static int should_move_die_to_comdat (dw_die_ref);
3031 static dw_die_ref clone_as_declaration (dw_die_ref);
3032 static dw_die_ref clone_die (dw_die_ref);
3033 static dw_die_ref clone_tree (dw_die_ref);
3034 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3035 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3036 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3037 static dw_die_ref generate_skeleton (dw_die_ref);
3038 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3039 dw_die_ref,
3040 dw_die_ref);
3041 static void break_out_comdat_types (dw_die_ref);
3042 static void copy_decls_for_unworthy_types (dw_die_ref);
3044 static void add_sibling_attributes (dw_die_ref);
3045 static void output_location_lists (dw_die_ref);
3046 static int constant_size (unsigned HOST_WIDE_INT);
3047 static unsigned long size_of_die (dw_die_ref);
3048 static void calc_die_sizes (dw_die_ref);
3049 static void calc_base_type_die_sizes (void);
3050 static void mark_dies (dw_die_ref);
3051 static void unmark_dies (dw_die_ref);
3052 static void unmark_all_dies (dw_die_ref);
3053 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3054 static unsigned long size_of_aranges (void);
3055 static enum dwarf_form value_format (dw_attr_ref);
3056 static void output_value_format (dw_attr_ref);
3057 static void output_abbrev_section (void);
3058 static void output_die_abbrevs (unsigned long, dw_die_ref);
3059 static void output_die_symbol (dw_die_ref);
3060 static void output_die (dw_die_ref);
3061 static void output_compilation_unit_header (void);
3062 static void output_comp_unit (dw_die_ref, int);
3063 static void output_comdat_type_unit (comdat_type_node *);
3064 static const char *dwarf2_name (tree, int);
3065 static void add_pubname (tree, dw_die_ref);
3066 static void add_enumerator_pubname (const char *, dw_die_ref);
3067 static void add_pubname_string (const char *, dw_die_ref);
3068 static void add_pubtype (tree, dw_die_ref);
3069 static void output_pubnames (vec<pubname_entry, va_gc> *);
3070 static void output_aranges (unsigned long);
3071 static unsigned int add_ranges_num (int);
3072 static unsigned int add_ranges (const_tree);
3073 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3074 bool *, bool);
3075 static void output_ranges (void);
3076 static dw_line_info_table *new_line_info_table (void);
3077 static void output_line_info (bool);
3078 static void output_file_names (void);
3079 static dw_die_ref base_type_die (tree);
3080 static int is_base_type (tree);
3081 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3082 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3083 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3084 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3085 static int type_is_enum (const_tree);
3086 static unsigned int dbx_reg_number (const_rtx);
3087 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3088 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3089 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3090 enum var_init_status);
3091 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3092 enum var_init_status);
3093 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3094 enum var_init_status);
3095 static int is_based_loc (const_rtx);
3096 static int resolve_one_addr (rtx *, void *);
3097 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3098 enum var_init_status);
3099 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3100 enum var_init_status);
3101 static dw_loc_list_ref loc_list_from_tree (tree, int);
3102 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3103 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3104 static tree field_type (const_tree);
3105 static unsigned int simple_type_align_in_bits (const_tree);
3106 static unsigned int simple_decl_align_in_bits (const_tree);
3107 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3108 static HOST_WIDE_INT field_byte_offset (const_tree);
3109 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3110 dw_loc_list_ref);
3111 static void add_data_member_location_attribute (dw_die_ref, tree);
3112 static bool add_const_value_attribute (dw_die_ref, rtx);
3113 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3114 static void insert_double (double_int, unsigned char *);
3115 static void insert_float (const_rtx, unsigned char *);
3116 static rtx rtl_for_decl_location (tree);
3117 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3118 enum dwarf_attribute);
3119 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3120 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3121 static void add_name_attribute (dw_die_ref, const char *);
3122 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3123 static void add_comp_dir_attribute (dw_die_ref);
3124 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3125 static void add_subscript_info (dw_die_ref, tree, bool);
3126 static void add_byte_size_attribute (dw_die_ref, tree);
3127 static void add_bit_offset_attribute (dw_die_ref, tree);
3128 static void add_bit_size_attribute (dw_die_ref, tree);
3129 static void add_prototyped_attribute (dw_die_ref, tree);
3130 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3131 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3132 static void add_src_coords_attributes (dw_die_ref, tree);
3133 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3134 static void push_decl_scope (tree);
3135 static void pop_decl_scope (void);
3136 static dw_die_ref scope_die_for (tree, dw_die_ref);
3137 static inline int local_scope_p (dw_die_ref);
3138 static inline int class_scope_p (dw_die_ref);
3139 static inline int class_or_namespace_scope_p (dw_die_ref);
3140 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3141 static void add_calling_convention_attribute (dw_die_ref, tree);
3142 static const char *type_tag (const_tree);
3143 static tree member_declared_type (const_tree);
3144 #if 0
3145 static const char *decl_start_label (tree);
3146 #endif
3147 static void gen_array_type_die (tree, dw_die_ref);
3148 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3149 #if 0
3150 static void gen_entry_point_die (tree, dw_die_ref);
3151 #endif
3152 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3153 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3154 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3155 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3156 static void gen_formal_types_die (tree, dw_die_ref);
3157 static void gen_subprogram_die (tree, dw_die_ref);
3158 static void gen_variable_die (tree, tree, dw_die_ref);
3159 static void gen_const_die (tree, dw_die_ref);
3160 static void gen_label_die (tree, dw_die_ref);
3161 static void gen_lexical_block_die (tree, dw_die_ref, int);
3162 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3163 static void gen_field_die (tree, dw_die_ref);
3164 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3165 static dw_die_ref gen_compile_unit_die (const char *);
3166 static void gen_inheritance_die (tree, tree, dw_die_ref);
3167 static void gen_member_die (tree, dw_die_ref);
3168 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3169 enum debug_info_usage);
3170 static void gen_subroutine_type_die (tree, dw_die_ref);
3171 static void gen_typedef_die (tree, dw_die_ref);
3172 static void gen_type_die (tree, dw_die_ref);
3173 static void gen_block_die (tree, dw_die_ref, int);
3174 static void decls_for_scope (tree, dw_die_ref, int);
3175 static inline int is_redundant_typedef (const_tree);
3176 static bool is_naming_typedef_decl (const_tree);
3177 static inline dw_die_ref get_context_die (tree);
3178 static void gen_namespace_die (tree, dw_die_ref);
3179 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3180 static dw_die_ref force_decl_die (tree);
3181 static dw_die_ref force_type_die (tree);
3182 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3183 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3184 static struct dwarf_file_data * lookup_filename (const char *);
3185 static void retry_incomplete_types (void);
3186 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3187 static void gen_generic_params_dies (tree);
3188 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3189 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3190 static void splice_child_die (dw_die_ref, dw_die_ref);
3191 static int file_info_cmp (const void *, const void *);
3192 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3193 const char *, const char *);
3194 static void output_loc_list (dw_loc_list_ref);
3195 static char *gen_internal_sym (const char *);
3196 static bool want_pubnames (void);
3198 static void prune_unmark_dies (dw_die_ref);
3199 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3200 static void prune_unused_types_mark (dw_die_ref, int);
3201 static void prune_unused_types_walk (dw_die_ref);
3202 static void prune_unused_types_walk_attribs (dw_die_ref);
3203 static void prune_unused_types_prune (dw_die_ref);
3204 static void prune_unused_types (void);
3205 static int maybe_emit_file (struct dwarf_file_data *fd);
3206 static inline const char *AT_vms_delta1 (dw_attr_ref);
3207 static inline const char *AT_vms_delta2 (dw_attr_ref);
3208 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3209 const char *, const char *);
3210 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3211 static void gen_remaining_tmpl_value_param_die_attribute (void);
3212 static bool generic_type_p (tree);
3213 static void schedule_generic_params_dies_gen (tree t);
3214 static void gen_scheduled_generic_parms_dies (void);
3216 static const char *comp_dir_string (void);
3218 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3220 /* enum for tracking thread-local variables whose address is really an offset
3221 relative to the TLS pointer, which will need link-time relocation, but will
3222 not need relocation by the DWARF consumer. */
3224 enum dtprel_bool
3226 dtprel_false = 0,
3227 dtprel_true = 1
3230 /* Return the operator to use for an address of a variable. For dtprel_true, we
3231 use DW_OP_const*. For regular variables, which need both link-time
3232 relocation and consumer-level relocation (e.g., to account for shared objects
3233 loaded at a random address), we use DW_OP_addr*. */
3235 static inline enum dwarf_location_atom
3236 dw_addr_op (enum dtprel_bool dtprel)
3238 if (dtprel == dtprel_true)
3239 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3240 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3241 else
3242 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3245 /* Return a pointer to a newly allocated address location description. If
3246 dwarf_split_debug_info is true, then record the address with the appropriate
3247 relocation. */
3248 static inline dw_loc_descr_ref
3249 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3251 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3253 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3254 ref->dw_loc_oprnd1.v.val_addr = addr;
3255 ref->dtprel = dtprel;
3256 if (dwarf_split_debug_info)
3257 ref->dw_loc_oprnd1.val_entry
3258 = add_addr_table_entry (addr,
3259 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3260 else
3261 ref->dw_loc_oprnd1.val_entry = NULL;
3263 return ref;
3266 /* Section names used to hold DWARF debugging information. */
3268 #ifndef DEBUG_INFO_SECTION
3269 #define DEBUG_INFO_SECTION ".debug_info"
3270 #endif
3271 #ifndef DEBUG_DWO_INFO_SECTION
3272 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3273 #endif
3274 #ifndef DEBUG_ABBREV_SECTION
3275 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3276 #endif
3277 #ifndef DEBUG_DWO_ABBREV_SECTION
3278 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3279 #endif
3280 #ifndef DEBUG_ARANGES_SECTION
3281 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3282 #endif
3283 #ifndef DEBUG_ADDR_SECTION
3284 #define DEBUG_ADDR_SECTION ".debug_addr"
3285 #endif
3286 #ifndef DEBUG_NORM_MACINFO_SECTION
3287 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3288 #endif
3289 #ifndef DEBUG_DWO_MACINFO_SECTION
3290 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3291 #endif
3292 #ifndef DEBUG_MACINFO_SECTION
3293 #define DEBUG_MACINFO_SECTION \
3294 (!dwarf_split_debug_info \
3295 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3296 #endif
3297 #ifndef DEBUG_NORM_MACRO_SECTION
3298 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3299 #endif
3300 #ifndef DEBUG_DWO_MACRO_SECTION
3301 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3302 #endif
3303 #ifndef DEBUG_MACRO_SECTION
3304 #define DEBUG_MACRO_SECTION \
3305 (!dwarf_split_debug_info \
3306 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3307 #endif
3308 #ifndef DEBUG_LINE_SECTION
3309 #define DEBUG_LINE_SECTION ".debug_line"
3310 #endif
3311 #ifndef DEBUG_DWO_LINE_SECTION
3312 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3313 #endif
3314 #ifndef DEBUG_LOC_SECTION
3315 #define DEBUG_LOC_SECTION ".debug_loc"
3316 #endif
3317 #ifndef DEBUG_DWO_LOC_SECTION
3318 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3319 #endif
3320 #ifndef DEBUG_PUBNAMES_SECTION
3321 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3322 #endif
3323 #ifndef DEBUG_PUBTYPES_SECTION
3324 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3325 #endif
3326 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3327 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3328 #ifndef DEBUG_STR_OFFSETS_SECTION
3329 #define DEBUG_STR_OFFSETS_SECTION \
3330 (!dwarf_split_debug_info \
3331 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3332 #endif
3333 #ifndef DEBUG_STR_DWO_SECTION
3334 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3335 #endif
3336 #ifndef DEBUG_STR_SECTION
3337 #define DEBUG_STR_SECTION ".debug_str"
3338 #endif
3339 #ifndef DEBUG_RANGES_SECTION
3340 #define DEBUG_RANGES_SECTION ".debug_ranges"
3341 #endif
3343 /* Standard ELF section names for compiled code and data. */
3344 #ifndef TEXT_SECTION_NAME
3345 #define TEXT_SECTION_NAME ".text"
3346 #endif
3348 /* Section flags for .debug_macinfo/.debug_macro section. */
3349 #define DEBUG_MACRO_SECTION_FLAGS \
3350 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3352 /* Section flags for .debug_str section. */
3353 #define DEBUG_STR_SECTION_FLAGS \
3354 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3355 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3356 : SECTION_DEBUG)
3358 /* Section flags for .debug_str.dwo section. */
3359 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3361 /* Labels we insert at beginning sections we can reference instead of
3362 the section names themselves. */
3364 #ifndef TEXT_SECTION_LABEL
3365 #define TEXT_SECTION_LABEL "Ltext"
3366 #endif
3367 #ifndef COLD_TEXT_SECTION_LABEL
3368 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3369 #endif
3370 #ifndef DEBUG_LINE_SECTION_LABEL
3371 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3372 #endif
3373 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3374 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3375 #endif
3376 #ifndef DEBUG_INFO_SECTION_LABEL
3377 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3378 #endif
3379 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3380 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3381 #endif
3382 #ifndef DEBUG_ABBREV_SECTION_LABEL
3383 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3384 #endif
3385 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3386 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3387 #endif
3388 #ifndef DEBUG_ADDR_SECTION_LABEL
3389 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3390 #endif
3391 #ifndef DEBUG_LOC_SECTION_LABEL
3392 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3393 #endif
3394 #ifndef DEBUG_RANGES_SECTION_LABEL
3395 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3396 #endif
3397 #ifndef DEBUG_MACINFO_SECTION_LABEL
3398 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3399 #endif
3400 #ifndef DEBUG_MACRO_SECTION_LABEL
3401 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3402 #endif
3403 #define SKELETON_COMP_DIE_ABBREV 1
3404 #define SKELETON_TYPE_DIE_ABBREV 2
3406 /* Definitions of defaults for formats and names of various special
3407 (artificial) labels which may be generated within this file (when the -g
3408 options is used and DWARF2_DEBUGGING_INFO is in effect.
3409 If necessary, these may be overridden from within the tm.h file, but
3410 typically, overriding these defaults is unnecessary. */
3412 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3413 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3414 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3415 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3416 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3417 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3418 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3419 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3420 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3421 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3422 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3423 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3424 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3425 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3427 #ifndef TEXT_END_LABEL
3428 #define TEXT_END_LABEL "Letext"
3429 #endif
3430 #ifndef COLD_END_LABEL
3431 #define COLD_END_LABEL "Letext_cold"
3432 #endif
3433 #ifndef BLOCK_BEGIN_LABEL
3434 #define BLOCK_BEGIN_LABEL "LBB"
3435 #endif
3436 #ifndef BLOCK_END_LABEL
3437 #define BLOCK_END_LABEL "LBE"
3438 #endif
3439 #ifndef LINE_CODE_LABEL
3440 #define LINE_CODE_LABEL "LM"
3441 #endif
3444 /* Return the root of the DIE's built for the current compilation unit. */
3445 static dw_die_ref
3446 comp_unit_die (void)
3448 if (!single_comp_unit_die)
3449 single_comp_unit_die = gen_compile_unit_die (NULL);
3450 return single_comp_unit_die;
3453 /* We allow a language front-end to designate a function that is to be
3454 called to "demangle" any name before it is put into a DIE. */
3456 static const char *(*demangle_name_func) (const char *);
3458 void
3459 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3461 demangle_name_func = func;
3464 /* Test if rtl node points to a pseudo register. */
3466 static inline int
3467 is_pseudo_reg (const_rtx rtl)
3469 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3470 || (GET_CODE (rtl) == SUBREG
3471 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3474 /* Return a reference to a type, with its const and volatile qualifiers
3475 removed. */
3477 static inline tree
3478 type_main_variant (tree type)
3480 type = TYPE_MAIN_VARIANT (type);
3482 /* ??? There really should be only one main variant among any group of
3483 variants of a given type (and all of the MAIN_VARIANT values for all
3484 members of the group should point to that one type) but sometimes the C
3485 front-end messes this up for array types, so we work around that bug
3486 here. */
3487 if (TREE_CODE (type) == ARRAY_TYPE)
3488 while (type != TYPE_MAIN_VARIANT (type))
3489 type = TYPE_MAIN_VARIANT (type);
3491 return type;
3494 /* Return nonzero if the given type node represents a tagged type. */
3496 static inline int
3497 is_tagged_type (const_tree type)
3499 enum tree_code code = TREE_CODE (type);
3501 return (code == RECORD_TYPE || code == UNION_TYPE
3502 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3505 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3507 static void
3508 get_ref_die_offset_label (char *label, dw_die_ref ref)
3510 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3513 /* Return die_offset of a DIE reference to a base type. */
3515 static unsigned long int
3516 get_base_type_offset (dw_die_ref ref)
3518 if (ref->die_offset)
3519 return ref->die_offset;
3520 if (comp_unit_die ()->die_abbrev)
3522 calc_base_type_die_sizes ();
3523 gcc_assert (ref->die_offset);
3525 return ref->die_offset;
3528 /* Return die_offset of a DIE reference other than base type. */
3530 static unsigned long int
3531 get_ref_die_offset (dw_die_ref ref)
3533 gcc_assert (ref->die_offset);
3534 return ref->die_offset;
3537 /* Convert a DIE tag into its string name. */
3539 static const char *
3540 dwarf_tag_name (unsigned int tag)
3542 const char *name = get_DW_TAG_name (tag);
3544 if (name != NULL)
3545 return name;
3547 return "DW_TAG_<unknown>";
3550 /* Convert a DWARF attribute code into its string name. */
3552 static const char *
3553 dwarf_attr_name (unsigned int attr)
3555 const char *name;
3557 switch (attr)
3559 #if VMS_DEBUGGING_INFO
3560 case DW_AT_HP_prologue:
3561 return "DW_AT_HP_prologue";
3562 #else
3563 case DW_AT_MIPS_loop_unroll_factor:
3564 return "DW_AT_MIPS_loop_unroll_factor";
3565 #endif
3567 #if VMS_DEBUGGING_INFO
3568 case DW_AT_HP_epilogue:
3569 return "DW_AT_HP_epilogue";
3570 #else
3571 case DW_AT_MIPS_stride:
3572 return "DW_AT_MIPS_stride";
3573 #endif
3576 name = get_DW_AT_name (attr);
3578 if (name != NULL)
3579 return name;
3581 return "DW_AT_<unknown>";
3584 /* Convert a DWARF value form code into its string name. */
3586 static const char *
3587 dwarf_form_name (unsigned int form)
3589 const char *name = get_DW_FORM_name (form);
3591 if (name != NULL)
3592 return name;
3594 return "DW_FORM_<unknown>";
3597 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3598 instance of an inlined instance of a decl which is local to an inline
3599 function, so we have to trace all of the way back through the origin chain
3600 to find out what sort of node actually served as the original seed for the
3601 given block. */
3603 static tree
3604 decl_ultimate_origin (const_tree decl)
3606 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3607 return NULL_TREE;
3609 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3610 nodes in the function to point to themselves; ignore that if
3611 we're trying to output the abstract instance of this function. */
3612 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3613 return NULL_TREE;
3615 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3616 most distant ancestor, this should never happen. */
3617 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3619 return DECL_ABSTRACT_ORIGIN (decl);
3622 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3623 of a virtual function may refer to a base class, so we check the 'this'
3624 parameter. */
3626 static tree
3627 decl_class_context (tree decl)
3629 tree context = NULL_TREE;
3631 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3632 context = DECL_CONTEXT (decl);
3633 else
3634 context = TYPE_MAIN_VARIANT
3635 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3637 if (context && !TYPE_P (context))
3638 context = NULL_TREE;
3640 return context;
3643 /* Add an attribute/value pair to a DIE. */
3645 static inline void
3646 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3648 /* Maybe this should be an assert? */
3649 if (die == NULL)
3650 return;
3652 vec_safe_reserve (die->die_attr, 1);
3653 vec_safe_push (die->die_attr, *attr);
3656 static inline enum dw_val_class
3657 AT_class (dw_attr_ref a)
3659 return a->dw_attr_val.val_class;
3662 /* Return the index for any attribute that will be referenced with a
3663 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3664 are stored in dw_attr_val.v.val_str for reference counting
3665 pruning. */
3667 static inline unsigned int
3668 AT_index (dw_attr_ref a)
3670 if (AT_class (a) == dw_val_class_str)
3671 return a->dw_attr_val.v.val_str->index;
3672 else if (a->dw_attr_val.val_entry != NULL)
3673 return a->dw_attr_val.val_entry->index;
3674 return NOT_INDEXED;
3677 /* Add a flag value attribute to a DIE. */
3679 static inline void
3680 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3682 dw_attr_node attr;
3684 attr.dw_attr = attr_kind;
3685 attr.dw_attr_val.val_class = dw_val_class_flag;
3686 attr.dw_attr_val.val_entry = NULL;
3687 attr.dw_attr_val.v.val_flag = flag;
3688 add_dwarf_attr (die, &attr);
3691 static inline unsigned
3692 AT_flag (dw_attr_ref a)
3694 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3695 return a->dw_attr_val.v.val_flag;
3698 /* Add a signed integer attribute value to a DIE. */
3700 static inline void
3701 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3703 dw_attr_node attr;
3705 attr.dw_attr = attr_kind;
3706 attr.dw_attr_val.val_class = dw_val_class_const;
3707 attr.dw_attr_val.val_entry = NULL;
3708 attr.dw_attr_val.v.val_int = int_val;
3709 add_dwarf_attr (die, &attr);
3712 static inline HOST_WIDE_INT
3713 AT_int (dw_attr_ref a)
3715 gcc_assert (a && AT_class (a) == dw_val_class_const);
3716 return a->dw_attr_val.v.val_int;
3719 /* Add an unsigned integer attribute value to a DIE. */
3721 static inline void
3722 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3723 unsigned HOST_WIDE_INT unsigned_val)
3725 dw_attr_node attr;
3727 attr.dw_attr = attr_kind;
3728 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3729 attr.dw_attr_val.val_entry = NULL;
3730 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3731 add_dwarf_attr (die, &attr);
3734 static inline unsigned HOST_WIDE_INT
3735 AT_unsigned (dw_attr_ref a)
3737 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3738 return a->dw_attr_val.v.val_unsigned;
3741 /* Add an unsigned double integer attribute value to a DIE. */
3743 static inline void
3744 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3745 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3747 dw_attr_node attr;
3749 attr.dw_attr = attr_kind;
3750 attr.dw_attr_val.val_class = dw_val_class_const_double;
3751 attr.dw_attr_val.val_entry = NULL;
3752 attr.dw_attr_val.v.val_double.high = high;
3753 attr.dw_attr_val.v.val_double.low = low;
3754 add_dwarf_attr (die, &attr);
3757 /* Add a floating point attribute value to a DIE and return it. */
3759 static inline void
3760 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3761 unsigned int length, unsigned int elt_size, unsigned char *array)
3763 dw_attr_node attr;
3765 attr.dw_attr = attr_kind;
3766 attr.dw_attr_val.val_class = dw_val_class_vec;
3767 attr.dw_attr_val.val_entry = NULL;
3768 attr.dw_attr_val.v.val_vec.length = length;
3769 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3770 attr.dw_attr_val.v.val_vec.array = array;
3771 add_dwarf_attr (die, &attr);
3774 /* Add an 8-byte data attribute value to a DIE. */
3776 static inline void
3777 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3778 unsigned char data8[8])
3780 dw_attr_node attr;
3782 attr.dw_attr = attr_kind;
3783 attr.dw_attr_val.val_class = dw_val_class_data8;
3784 attr.dw_attr_val.val_entry = NULL;
3785 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3786 add_dwarf_attr (die, &attr);
3789 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3790 dwarf_split_debug_info, address attributes in dies destined for the
3791 final executable have force_direct set to avoid using indexed
3792 references. */
3794 static inline void
3795 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3796 bool force_direct)
3798 dw_attr_node attr;
3799 char * lbl_id;
3801 lbl_id = xstrdup (lbl_low);
3802 attr.dw_attr = DW_AT_low_pc;
3803 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3804 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3805 if (dwarf_split_debug_info && !force_direct)
3806 attr.dw_attr_val.val_entry
3807 = add_addr_table_entry (lbl_id, ate_kind_label);
3808 else
3809 attr.dw_attr_val.val_entry = NULL;
3810 add_dwarf_attr (die, &attr);
3812 attr.dw_attr = DW_AT_high_pc;
3813 if (dwarf_version < 4)
3814 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3815 else
3816 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3817 lbl_id = xstrdup (lbl_high);
3818 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3819 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3820 && dwarf_split_debug_info && !force_direct)
3821 attr.dw_attr_val.val_entry
3822 = add_addr_table_entry (lbl_id, ate_kind_label);
3823 else
3824 attr.dw_attr_val.val_entry = NULL;
3825 add_dwarf_attr (die, &attr);
3828 /* Hash and equality functions for debug_str_hash. */
3830 static hashval_t
3831 debug_str_do_hash (const void *x)
3833 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3836 static int
3837 debug_str_eq (const void *x1, const void *x2)
3839 return strcmp ((((const struct indirect_string_node *)x1)->str),
3840 (const char *)x2) == 0;
3843 /* Add STR to the given string hash table. */
3845 static struct indirect_string_node *
3846 find_AT_string_in_table (const char *str, htab_t table)
3848 struct indirect_string_node *node;
3849 void **slot;
3851 slot = htab_find_slot_with_hash (table, str,
3852 htab_hash_string (str), INSERT);
3853 if (*slot == NULL)
3855 node = ggc_alloc_cleared_indirect_string_node ();
3856 node->str = ggc_strdup (str);
3857 *slot = node;
3859 else
3860 node = (struct indirect_string_node *) *slot;
3862 node->refcount++;
3863 return node;
3866 /* Add STR to the indirect string hash table. */
3868 static struct indirect_string_node *
3869 find_AT_string (const char *str)
3871 if (! debug_str_hash)
3872 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3873 debug_str_eq, NULL);
3875 return find_AT_string_in_table (str, debug_str_hash);
3878 /* Add a string attribute value to a DIE. */
3880 static inline void
3881 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3883 dw_attr_node attr;
3884 struct indirect_string_node *node;
3886 node = find_AT_string (str);
3888 attr.dw_attr = attr_kind;
3889 attr.dw_attr_val.val_class = dw_val_class_str;
3890 attr.dw_attr_val.val_entry = NULL;
3891 attr.dw_attr_val.v.val_str = node;
3892 add_dwarf_attr (die, &attr);
3895 static inline const char *
3896 AT_string (dw_attr_ref a)
3898 gcc_assert (a && AT_class (a) == dw_val_class_str);
3899 return a->dw_attr_val.v.val_str->str;
3902 /* Call this function directly to bypass AT_string_form's logic to put
3903 the string inline in the die. */
3905 static void
3906 set_indirect_string (struct indirect_string_node *node)
3908 char label[32];
3909 /* Already indirect is a no op. */
3910 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3912 gcc_assert (node->label);
3913 return;
3915 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3916 ++dw2_string_counter;
3917 node->label = xstrdup (label);
3919 if (!dwarf_split_debug_info)
3921 node->form = DW_FORM_strp;
3922 node->index = NOT_INDEXED;
3924 else
3926 node->form = DW_FORM_GNU_str_index;
3927 node->index = NO_INDEX_ASSIGNED;
3931 /* Find out whether a string should be output inline in DIE
3932 or out-of-line in .debug_str section. */
3934 static enum dwarf_form
3935 find_string_form (struct indirect_string_node *node)
3937 unsigned int len;
3939 if (node->form)
3940 return node->form;
3942 len = strlen (node->str) + 1;
3944 /* If the string is shorter or equal to the size of the reference, it is
3945 always better to put it inline. */
3946 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3947 return node->form = DW_FORM_string;
3949 /* If we cannot expect the linker to merge strings in .debug_str
3950 section, only put it into .debug_str if it is worth even in this
3951 single module. */
3952 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3953 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3954 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3955 return node->form = DW_FORM_string;
3957 set_indirect_string (node);
3959 return node->form;
3962 /* Find out whether the string referenced from the attribute should be
3963 output inline in DIE or out-of-line in .debug_str section. */
3965 static enum dwarf_form
3966 AT_string_form (dw_attr_ref a)
3968 gcc_assert (a && AT_class (a) == dw_val_class_str);
3969 return find_string_form (a->dw_attr_val.v.val_str);
3972 /* Add a DIE reference attribute value to a DIE. */
3974 static inline void
3975 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3977 dw_attr_node attr;
3979 #ifdef ENABLE_CHECKING
3980 gcc_assert (targ_die != NULL);
3981 #else
3982 /* With LTO we can end up trying to reference something we didn't create
3983 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3984 if (targ_die == NULL)
3985 return;
3986 #endif
3988 attr.dw_attr = attr_kind;
3989 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3990 attr.dw_attr_val.val_entry = NULL;
3991 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3992 attr.dw_attr_val.v.val_die_ref.external = 0;
3993 add_dwarf_attr (die, &attr);
3996 /* Change DIE reference REF to point to NEW_DIE instead. */
3998 static inline void
3999 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4001 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4002 ref->dw_attr_val.v.val_die_ref.die = new_die;
4003 ref->dw_attr_val.v.val_die_ref.external = 0;
4006 /* Add an AT_specification attribute to a DIE, and also make the back
4007 pointer from the specification to the definition. */
4009 static inline void
4010 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4012 add_AT_die_ref (die, DW_AT_specification, targ_die);
4013 gcc_assert (!targ_die->die_definition);
4014 targ_die->die_definition = die;
4017 static inline dw_die_ref
4018 AT_ref (dw_attr_ref a)
4020 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4021 return a->dw_attr_val.v.val_die_ref.die;
4024 static inline int
4025 AT_ref_external (dw_attr_ref a)
4027 if (a && AT_class (a) == dw_val_class_die_ref)
4028 return a->dw_attr_val.v.val_die_ref.external;
4030 return 0;
4033 static inline void
4034 set_AT_ref_external (dw_attr_ref a, int i)
4036 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4037 a->dw_attr_val.v.val_die_ref.external = i;
4040 /* Add an FDE reference attribute value to a DIE. */
4042 static inline void
4043 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4045 dw_attr_node attr;
4047 attr.dw_attr = attr_kind;
4048 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4049 attr.dw_attr_val.val_entry = NULL;
4050 attr.dw_attr_val.v.val_fde_index = targ_fde;
4051 add_dwarf_attr (die, &attr);
4054 /* Add a location description attribute value to a DIE. */
4056 static inline void
4057 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4059 dw_attr_node attr;
4061 attr.dw_attr = attr_kind;
4062 attr.dw_attr_val.val_class = dw_val_class_loc;
4063 attr.dw_attr_val.val_entry = NULL;
4064 attr.dw_attr_val.v.val_loc = loc;
4065 add_dwarf_attr (die, &attr);
4068 static inline dw_loc_descr_ref
4069 AT_loc (dw_attr_ref a)
4071 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4072 return a->dw_attr_val.v.val_loc;
4075 static inline void
4076 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4078 dw_attr_node attr;
4080 attr.dw_attr = attr_kind;
4081 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4082 attr.dw_attr_val.val_entry = NULL;
4083 attr.dw_attr_val.v.val_loc_list = loc_list;
4084 add_dwarf_attr (die, &attr);
4085 have_location_lists = true;
4088 static inline dw_loc_list_ref
4089 AT_loc_list (dw_attr_ref a)
4091 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4092 return a->dw_attr_val.v.val_loc_list;
4095 static inline dw_loc_list_ref *
4096 AT_loc_list_ptr (dw_attr_ref a)
4098 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4099 return &a->dw_attr_val.v.val_loc_list;
4102 /* Table of entries into the .debug_addr section. */
4104 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4106 /* Hash an address_table_entry. */
4108 static hashval_t
4109 addr_table_entry_do_hash (const void *x)
4111 const addr_table_entry *a = (const addr_table_entry *) x;
4112 switch (a->kind)
4114 case ate_kind_rtx:
4115 return iterative_hash_rtx (a->addr.rtl, 0);
4116 case ate_kind_rtx_dtprel:
4117 return iterative_hash_rtx (a->addr.rtl, 1);
4118 case ate_kind_label:
4119 return htab_hash_string (a->addr.label);
4120 default:
4121 gcc_unreachable ();
4125 /* Determine equality for two address_table_entries. */
4127 static int
4128 addr_table_entry_eq (const void *x1, const void *x2)
4130 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4131 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4133 if (a1->kind != a2->kind)
4134 return 0;
4135 switch (a1->kind)
4137 case ate_kind_rtx:
4138 case ate_kind_rtx_dtprel:
4139 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4140 case ate_kind_label:
4141 return strcmp (a1->addr.label, a2->addr.label) == 0;
4142 default:
4143 gcc_unreachable ();
4147 /* Initialize an addr_table_entry. */
4149 void
4150 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4152 e->kind = kind;
4153 switch (kind)
4155 case ate_kind_rtx:
4156 case ate_kind_rtx_dtprel:
4157 e->addr.rtl = (rtx) addr;
4158 break;
4159 case ate_kind_label:
4160 e->addr.label = (char *) addr;
4161 break;
4163 e->refcount = 0;
4164 e->index = NO_INDEX_ASSIGNED;
4167 /* Add attr to the address table entry to the table. Defer setting an
4168 index until output time. */
4170 static addr_table_entry *
4171 add_addr_table_entry (void *addr, enum ate_kind kind)
4173 addr_table_entry *node;
4174 addr_table_entry finder;
4175 void **slot;
4177 gcc_assert (dwarf_split_debug_info);
4178 if (! addr_index_table)
4179 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4180 addr_table_entry_eq, NULL);
4181 init_addr_table_entry (&finder, kind, addr);
4182 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4184 if (*slot == HTAB_EMPTY_ENTRY)
4186 node = ggc_alloc_cleared_addr_table_entry ();
4187 init_addr_table_entry (node, kind, addr);
4188 *slot = node;
4190 else
4191 node = (addr_table_entry *) *slot;
4193 node->refcount++;
4194 return node;
4197 /* Remove an entry from the addr table by decrementing its refcount.
4198 Strictly, decrementing the refcount would be enough, but the
4199 assertion that the entry is actually in the table has found
4200 bugs. */
4202 static void
4203 remove_addr_table_entry (addr_table_entry *entry)
4205 addr_table_entry *node;
4207 gcc_assert (dwarf_split_debug_info && addr_index_table);
4208 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4209 /* After an index is assigned, the table is frozen. */
4210 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4211 node->refcount--;
4214 /* Given a location list, remove all addresses it refers to from the
4215 address_table. */
4217 static void
4218 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4220 for (; descr; descr = descr->dw_loc_next)
4221 if (descr->dw_loc_oprnd1.val_entry != NULL)
4223 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4224 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4228 /* A helper function for dwarf2out_finish called through
4229 htab_traverse. Assign an addr_table_entry its index. All entries
4230 must be collected into the table when this function is called,
4231 because the indexing code relies on htab_traverse to traverse nodes
4232 in the same order for each run. */
4234 static int
4235 index_addr_table_entry (void **h, void *v)
4237 addr_table_entry *node = (addr_table_entry *) *h;
4238 unsigned int *index = (unsigned int *) v;
4240 /* Don't index unreferenced nodes. */
4241 if (node->refcount == 0)
4242 return 1;
4244 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4245 node->index = *index;
4246 *index += 1;
4248 return 1;
4251 /* Add an address constant attribute value to a DIE. When using
4252 dwarf_split_debug_info, address attributes in dies destined for the
4253 final executable should be direct references--setting the parameter
4254 force_direct ensures this behavior. */
4256 static inline void
4257 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4258 bool force_direct)
4260 dw_attr_node attr;
4262 attr.dw_attr = attr_kind;
4263 attr.dw_attr_val.val_class = dw_val_class_addr;
4264 attr.dw_attr_val.v.val_addr = addr;
4265 if (dwarf_split_debug_info && !force_direct)
4266 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4267 else
4268 attr.dw_attr_val.val_entry = NULL;
4269 add_dwarf_attr (die, &attr);
4272 /* Get the RTX from to an address DIE attribute. */
4274 static inline rtx
4275 AT_addr (dw_attr_ref a)
4277 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4278 return a->dw_attr_val.v.val_addr;
4281 /* Add a file attribute value to a DIE. */
4283 static inline void
4284 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4285 struct dwarf_file_data *fd)
4287 dw_attr_node attr;
4289 attr.dw_attr = attr_kind;
4290 attr.dw_attr_val.val_class = dw_val_class_file;
4291 attr.dw_attr_val.val_entry = NULL;
4292 attr.dw_attr_val.v.val_file = fd;
4293 add_dwarf_attr (die, &attr);
4296 /* Get the dwarf_file_data from a file DIE attribute. */
4298 static inline struct dwarf_file_data *
4299 AT_file (dw_attr_ref a)
4301 gcc_assert (a && AT_class (a) == dw_val_class_file);
4302 return a->dw_attr_val.v.val_file;
4305 /* Add a vms delta attribute value to a DIE. */
4307 static inline void
4308 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4309 const char *lbl1, const char *lbl2)
4311 dw_attr_node attr;
4313 attr.dw_attr = attr_kind;
4314 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4315 attr.dw_attr_val.val_entry = NULL;
4316 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4317 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4318 add_dwarf_attr (die, &attr);
4321 /* Add a label identifier attribute value to a DIE. */
4323 static inline void
4324 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4325 const char *lbl_id)
4327 dw_attr_node attr;
4329 attr.dw_attr = attr_kind;
4330 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4331 attr.dw_attr_val.val_entry = NULL;
4332 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4333 if (dwarf_split_debug_info)
4334 attr.dw_attr_val.val_entry
4335 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4336 ate_kind_label);
4337 add_dwarf_attr (die, &attr);
4340 /* Add a section offset attribute value to a DIE, an offset into the
4341 debug_line section. */
4343 static inline void
4344 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4345 const char *label)
4347 dw_attr_node attr;
4349 attr.dw_attr = attr_kind;
4350 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4351 attr.dw_attr_val.val_entry = NULL;
4352 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4353 add_dwarf_attr (die, &attr);
4356 /* Add a section offset attribute value to a DIE, an offset into the
4357 debug_macinfo section. */
4359 static inline void
4360 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4361 const char *label)
4363 dw_attr_node attr;
4365 attr.dw_attr = attr_kind;
4366 attr.dw_attr_val.val_class = dw_val_class_macptr;
4367 attr.dw_attr_val.val_entry = NULL;
4368 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4369 add_dwarf_attr (die, &attr);
4372 /* Add an offset attribute value to a DIE. */
4374 static inline void
4375 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4376 unsigned HOST_WIDE_INT offset)
4378 dw_attr_node attr;
4380 attr.dw_attr = attr_kind;
4381 attr.dw_attr_val.val_class = dw_val_class_offset;
4382 attr.dw_attr_val.val_entry = NULL;
4383 attr.dw_attr_val.v.val_offset = offset;
4384 add_dwarf_attr (die, &attr);
4387 /* Add a range_list attribute value to a DIE. When using
4388 dwarf_split_debug_info, address attributes in dies destined for the
4389 final executable should be direct references--setting the parameter
4390 force_direct ensures this behavior. */
4392 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4393 #define RELOCATED_OFFSET (NULL)
4395 static void
4396 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4397 long unsigned int offset, bool force_direct)
4399 dw_attr_node attr;
4401 attr.dw_attr = attr_kind;
4402 attr.dw_attr_val.val_class = dw_val_class_range_list;
4403 /* For the range_list attribute, use val_entry to store whether the
4404 offset should follow split-debug-info or normal semantics. This
4405 value is read in output_range_list_offset. */
4406 if (dwarf_split_debug_info && !force_direct)
4407 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4408 else
4409 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4410 attr.dw_attr_val.v.val_offset = offset;
4411 add_dwarf_attr (die, &attr);
4414 /* Return the start label of a delta attribute. */
4416 static inline const char *
4417 AT_vms_delta1 (dw_attr_ref a)
4419 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4420 return a->dw_attr_val.v.val_vms_delta.lbl1;
4423 /* Return the end label of a delta attribute. */
4425 static inline const char *
4426 AT_vms_delta2 (dw_attr_ref a)
4428 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4429 return a->dw_attr_val.v.val_vms_delta.lbl2;
4432 static inline const char *
4433 AT_lbl (dw_attr_ref a)
4435 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4436 || AT_class (a) == dw_val_class_lineptr
4437 || AT_class (a) == dw_val_class_macptr
4438 || AT_class (a) == dw_val_class_high_pc));
4439 return a->dw_attr_val.v.val_lbl_id;
4442 /* Get the attribute of type attr_kind. */
4444 static dw_attr_ref
4445 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4447 dw_attr_ref a;
4448 unsigned ix;
4449 dw_die_ref spec = NULL;
4451 if (! die)
4452 return NULL;
4454 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4455 if (a->dw_attr == attr_kind)
4456 return a;
4457 else if (a->dw_attr == DW_AT_specification
4458 || a->dw_attr == DW_AT_abstract_origin)
4459 spec = AT_ref (a);
4461 if (spec)
4462 return get_AT (spec, attr_kind);
4464 return NULL;
4467 /* Returns the parent of the declaration of DIE. */
4469 static dw_die_ref
4470 get_die_parent (dw_die_ref die)
4472 dw_die_ref t;
4474 if (!die)
4475 return NULL;
4477 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4478 || (t = get_AT_ref (die, DW_AT_specification)))
4479 die = t;
4481 return die->die_parent;
4484 /* Return the "low pc" attribute value, typically associated with a subprogram
4485 DIE. Return null if the "low pc" attribute is either not present, or if it
4486 cannot be represented as an assembler label identifier. */
4488 static inline const char *
4489 get_AT_low_pc (dw_die_ref die)
4491 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4493 return a ? AT_lbl (a) : NULL;
4496 /* Return the "high pc" attribute value, typically associated with a subprogram
4497 DIE. Return null if the "high pc" attribute is either not present, or if it
4498 cannot be represented as an assembler label identifier. */
4500 static inline const char *
4501 get_AT_hi_pc (dw_die_ref die)
4503 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4505 return a ? AT_lbl (a) : NULL;
4508 /* Return the value of the string attribute designated by ATTR_KIND, or
4509 NULL if it is not present. */
4511 static inline const char *
4512 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4514 dw_attr_ref a = get_AT (die, attr_kind);
4516 return a ? AT_string (a) : NULL;
4519 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4520 if it is not present. */
4522 static inline int
4523 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4525 dw_attr_ref a = get_AT (die, attr_kind);
4527 return a ? AT_flag (a) : 0;
4530 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4531 if it is not present. */
4533 static inline unsigned
4534 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4536 dw_attr_ref a = get_AT (die, attr_kind);
4538 return a ? AT_unsigned (a) : 0;
4541 static inline dw_die_ref
4542 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4544 dw_attr_ref a = get_AT (die, attr_kind);
4546 return a ? AT_ref (a) : NULL;
4549 static inline struct dwarf_file_data *
4550 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4552 dw_attr_ref a = get_AT (die, attr_kind);
4554 return a ? AT_file (a) : NULL;
4557 /* Return TRUE if the language is C++. */
4559 static inline bool
4560 is_cxx (void)
4562 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4564 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4567 /* Return TRUE if the language is Fortran. */
4569 static inline bool
4570 is_fortran (void)
4572 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4574 return (lang == DW_LANG_Fortran77
4575 || lang == DW_LANG_Fortran90
4576 || lang == DW_LANG_Fortran95);
4579 /* Return TRUE if the language is Ada. */
4581 static inline bool
4582 is_ada (void)
4584 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4586 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4589 /* Remove the specified attribute if present. */
4591 static void
4592 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4594 dw_attr_ref a;
4595 unsigned ix;
4597 if (! die)
4598 return;
4600 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4601 if (a->dw_attr == attr_kind)
4603 if (AT_class (a) == dw_val_class_str)
4604 if (a->dw_attr_val.v.val_str->refcount)
4605 a->dw_attr_val.v.val_str->refcount--;
4607 /* vec::ordered_remove should help reduce the number of abbrevs
4608 that are needed. */
4609 die->die_attr->ordered_remove (ix);
4610 return;
4614 /* Remove CHILD from its parent. PREV must have the property that
4615 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4617 static void
4618 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4620 gcc_assert (child->die_parent == prev->die_parent);
4621 gcc_assert (prev->die_sib == child);
4622 if (prev == child)
4624 gcc_assert (child->die_parent->die_child == child);
4625 prev = NULL;
4627 else
4628 prev->die_sib = child->die_sib;
4629 if (child->die_parent->die_child == child)
4630 child->die_parent->die_child = prev;
4633 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4634 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4636 static void
4637 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4639 dw_die_ref parent = old_child->die_parent;
4641 gcc_assert (parent == prev->die_parent);
4642 gcc_assert (prev->die_sib == old_child);
4644 new_child->die_parent = parent;
4645 if (prev == old_child)
4647 gcc_assert (parent->die_child == old_child);
4648 new_child->die_sib = new_child;
4650 else
4652 prev->die_sib = new_child;
4653 new_child->die_sib = old_child->die_sib;
4655 if (old_child->die_parent->die_child == old_child)
4656 old_child->die_parent->die_child = new_child;
4659 /* Move all children from OLD_PARENT to NEW_PARENT. */
4661 static void
4662 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4664 dw_die_ref c;
4665 new_parent->die_child = old_parent->die_child;
4666 old_parent->die_child = NULL;
4667 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4670 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4671 matches TAG. */
4673 static void
4674 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4676 dw_die_ref c;
4678 c = die->die_child;
4679 if (c) do {
4680 dw_die_ref prev = c;
4681 c = c->die_sib;
4682 while (c->die_tag == tag)
4684 remove_child_with_prev (c, prev);
4685 /* Might have removed every child. */
4686 if (c == c->die_sib)
4687 return;
4688 c = c->die_sib;
4690 } while (c != die->die_child);
4693 /* Add a CHILD_DIE as the last child of DIE. */
4695 static void
4696 add_child_die (dw_die_ref die, dw_die_ref child_die)
4698 /* FIXME this should probably be an assert. */
4699 if (! die || ! child_die)
4700 return;
4701 gcc_assert (die != child_die);
4703 child_die->die_parent = die;
4704 if (die->die_child)
4706 child_die->die_sib = die->die_child->die_sib;
4707 die->die_child->die_sib = child_die;
4709 else
4710 child_die->die_sib = child_die;
4711 die->die_child = child_die;
4714 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4715 is the specification, to the end of PARENT's list of children.
4716 This is done by removing and re-adding it. */
4718 static void
4719 splice_child_die (dw_die_ref parent, dw_die_ref child)
4721 dw_die_ref p;
4723 /* We want the declaration DIE from inside the class, not the
4724 specification DIE at toplevel. */
4725 if (child->die_parent != parent)
4727 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4729 if (tmp)
4730 child = tmp;
4733 gcc_assert (child->die_parent == parent
4734 || (child->die_parent
4735 == get_AT_ref (parent, DW_AT_specification)));
4737 for (p = child->die_parent->die_child; ; p = p->die_sib)
4738 if (p->die_sib == child)
4740 remove_child_with_prev (child, p);
4741 break;
4744 add_child_die (parent, child);
4747 /* Return a pointer to a newly created DIE node. */
4749 static inline dw_die_ref
4750 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4752 dw_die_ref die = ggc_alloc_cleared_die_node ();
4754 die->die_tag = tag_value;
4756 if (parent_die != NULL)
4757 add_child_die (parent_die, die);
4758 else
4760 limbo_die_node *limbo_node;
4762 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4763 limbo_node->die = die;
4764 limbo_node->created_for = t;
4765 limbo_node->next = limbo_die_list;
4766 limbo_die_list = limbo_node;
4769 return die;
4772 /* Return the DIE associated with the given type specifier. */
4774 static inline dw_die_ref
4775 lookup_type_die (tree type)
4777 return TYPE_SYMTAB_DIE (type);
4780 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4781 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4782 anonymous type instead the one of the naming typedef. */
4784 static inline dw_die_ref
4785 strip_naming_typedef (tree type, dw_die_ref type_die)
4787 if (type
4788 && TREE_CODE (type) == RECORD_TYPE
4789 && type_die
4790 && type_die->die_tag == DW_TAG_typedef
4791 && is_naming_typedef_decl (TYPE_NAME (type)))
4792 type_die = get_AT_ref (type_die, DW_AT_type);
4793 return type_die;
4796 /* Like lookup_type_die, but if type is an anonymous type named by a
4797 typedef[1], return the DIE of the anonymous type instead the one of
4798 the naming typedef. This is because in gen_typedef_die, we did
4799 equate the anonymous struct named by the typedef with the DIE of
4800 the naming typedef. So by default, lookup_type_die on an anonymous
4801 struct yields the DIE of the naming typedef.
4803 [1]: Read the comment of is_naming_typedef_decl to learn about what
4804 a naming typedef is. */
4806 static inline dw_die_ref
4807 lookup_type_die_strip_naming_typedef (tree type)
4809 dw_die_ref die = lookup_type_die (type);
4810 return strip_naming_typedef (type, die);
4813 /* Equate a DIE to a given type specifier. */
4815 static inline void
4816 equate_type_number_to_die (tree type, dw_die_ref type_die)
4818 TYPE_SYMTAB_DIE (type) = type_die;
4821 /* Returns a hash value for X (which really is a die_struct). */
4823 static hashval_t
4824 decl_die_table_hash (const void *x)
4826 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4829 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4831 static int
4832 decl_die_table_eq (const void *x, const void *y)
4834 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4837 /* Return the DIE associated with a given declaration. */
4839 static inline dw_die_ref
4840 lookup_decl_die (tree decl)
4842 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4845 /* Returns a hash value for X (which really is a var_loc_list). */
4847 static hashval_t
4848 decl_loc_table_hash (const void *x)
4850 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4853 /* Return nonzero if decl_id of var_loc_list X is the same as
4854 UID of decl *Y. */
4856 static int
4857 decl_loc_table_eq (const void *x, const void *y)
4859 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4862 /* Return the var_loc list associated with a given declaration. */
4864 static inline var_loc_list *
4865 lookup_decl_loc (const_tree decl)
4867 if (!decl_loc_table)
4868 return NULL;
4869 return (var_loc_list *)
4870 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4873 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4875 static hashval_t
4876 cached_dw_loc_list_table_hash (const void *x)
4878 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4881 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4882 UID of decl *Y. */
4884 static int
4885 cached_dw_loc_list_table_eq (const void *x, const void *y)
4887 return (((const cached_dw_loc_list *) x)->decl_id
4888 == DECL_UID ((const_tree) y));
4891 /* Equate a DIE to a particular declaration. */
4893 static void
4894 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4896 unsigned int decl_id = DECL_UID (decl);
4897 void **slot;
4899 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4900 *slot = decl_die;
4901 decl_die->decl_id = decl_id;
4904 /* Return how many bits covers PIECE EXPR_LIST. */
4906 static int
4907 decl_piece_bitsize (rtx piece)
4909 int ret = (int) GET_MODE (piece);
4910 if (ret)
4911 return ret;
4912 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4913 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4914 return INTVAL (XEXP (XEXP (piece, 0), 0));
4917 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4919 static rtx *
4920 decl_piece_varloc_ptr (rtx piece)
4922 if ((int) GET_MODE (piece))
4923 return &XEXP (piece, 0);
4924 else
4925 return &XEXP (XEXP (piece, 0), 1);
4928 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4929 Next is the chain of following piece nodes. */
4931 static rtx
4932 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4934 if (bitsize <= (int) MAX_MACHINE_MODE)
4935 return alloc_EXPR_LIST (bitsize, loc_note, next);
4936 else
4937 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4938 GEN_INT (bitsize),
4939 loc_note), next);
4942 /* Return rtx that should be stored into loc field for
4943 LOC_NOTE and BITPOS/BITSIZE. */
4945 static rtx
4946 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4947 HOST_WIDE_INT bitsize)
4949 if (bitsize != -1)
4951 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4952 if (bitpos != 0)
4953 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4955 return loc_note;
4958 /* This function either modifies location piece list *DEST in
4959 place (if SRC and INNER is NULL), or copies location piece list
4960 *SRC to *DEST while modifying it. Location BITPOS is modified
4961 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4962 not copied and if needed some padding around it is added.
4963 When modifying in place, DEST should point to EXPR_LIST where
4964 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4965 to the start of the whole list and INNER points to the EXPR_LIST
4966 where earlier pieces cover PIECE_BITPOS bits. */
4968 static void
4969 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4970 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4971 HOST_WIDE_INT bitsize, rtx loc_note)
4973 int diff;
4974 bool copy = inner != NULL;
4976 if (copy)
4978 /* First copy all nodes preceding the current bitpos. */
4979 while (src != inner)
4981 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4982 decl_piece_bitsize (*src), NULL_RTX);
4983 dest = &XEXP (*dest, 1);
4984 src = &XEXP (*src, 1);
4987 /* Add padding if needed. */
4988 if (bitpos != piece_bitpos)
4990 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4991 copy ? NULL_RTX : *dest);
4992 dest = &XEXP (*dest, 1);
4994 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4996 gcc_assert (!copy);
4997 /* A piece with correct bitpos and bitsize already exist,
4998 just update the location for it and return. */
4999 *decl_piece_varloc_ptr (*dest) = loc_note;
5000 return;
5002 /* Add the piece that changed. */
5003 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5004 dest = &XEXP (*dest, 1);
5005 /* Skip over pieces that overlap it. */
5006 diff = bitpos - piece_bitpos + bitsize;
5007 if (!copy)
5008 src = dest;
5009 while (diff > 0 && *src)
5011 rtx piece = *src;
5012 diff -= decl_piece_bitsize (piece);
5013 if (copy)
5014 src = &XEXP (piece, 1);
5015 else
5017 *src = XEXP (piece, 1);
5018 free_EXPR_LIST_node (piece);
5021 /* Add padding if needed. */
5022 if (diff < 0 && *src)
5024 if (!copy)
5025 dest = src;
5026 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5027 dest = &XEXP (*dest, 1);
5029 if (!copy)
5030 return;
5031 /* Finally copy all nodes following it. */
5032 while (*src)
5034 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5035 decl_piece_bitsize (*src), NULL_RTX);
5036 dest = &XEXP (*dest, 1);
5037 src = &XEXP (*src, 1);
5041 /* Add a variable location node to the linked list for DECL. */
5043 static struct var_loc_node *
5044 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5046 unsigned int decl_id;
5047 var_loc_list *temp;
5048 void **slot;
5049 struct var_loc_node *loc = NULL;
5050 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5052 if (TREE_CODE (decl) == VAR_DECL
5053 && DECL_HAS_DEBUG_EXPR_P (decl))
5055 tree realdecl = DECL_DEBUG_EXPR (decl);
5056 if (handled_component_p (realdecl)
5057 || (TREE_CODE (realdecl) == MEM_REF
5058 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5060 HOST_WIDE_INT maxsize;
5061 tree innerdecl;
5062 innerdecl
5063 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5064 if (!DECL_P (innerdecl)
5065 || DECL_IGNORED_P (innerdecl)
5066 || TREE_STATIC (innerdecl)
5067 || bitsize <= 0
5068 || bitpos + bitsize > 256
5069 || bitsize != maxsize)
5070 return NULL;
5071 decl = innerdecl;
5075 decl_id = DECL_UID (decl);
5076 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5077 if (*slot == NULL)
5079 temp = ggc_alloc_cleared_var_loc_list ();
5080 temp->decl_id = decl_id;
5081 *slot = temp;
5083 else
5084 temp = (var_loc_list *) *slot;
5086 /* For PARM_DECLs try to keep around the original incoming value,
5087 even if that means we'll emit a zero-range .debug_loc entry. */
5088 if (temp->last
5089 && temp->first == temp->last
5090 && TREE_CODE (decl) == PARM_DECL
5091 && NOTE_P (temp->first->loc)
5092 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5093 && DECL_INCOMING_RTL (decl)
5094 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5095 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5096 == GET_CODE (DECL_INCOMING_RTL (decl))
5097 && prev_real_insn (temp->first->loc) == NULL_RTX
5098 && (bitsize != -1
5099 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5100 NOTE_VAR_LOCATION_LOC (loc_note))
5101 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5102 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5104 loc = ggc_alloc_cleared_var_loc_node ();
5105 temp->first->next = loc;
5106 temp->last = loc;
5107 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5109 else if (temp->last)
5111 struct var_loc_node *last = temp->last, *unused = NULL;
5112 rtx *piece_loc = NULL, last_loc_note;
5113 int piece_bitpos = 0;
5114 if (last->next)
5116 last = last->next;
5117 gcc_assert (last->next == NULL);
5119 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5121 piece_loc = &last->loc;
5124 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5125 if (piece_bitpos + cur_bitsize > bitpos)
5126 break;
5127 piece_bitpos += cur_bitsize;
5128 piece_loc = &XEXP (*piece_loc, 1);
5130 while (*piece_loc);
5132 /* TEMP->LAST here is either pointer to the last but one or
5133 last element in the chained list, LAST is pointer to the
5134 last element. */
5135 if (label && strcmp (last->label, label) == 0)
5137 /* For SRA optimized variables if there weren't any real
5138 insns since last note, just modify the last node. */
5139 if (piece_loc != NULL)
5141 adjust_piece_list (piece_loc, NULL, NULL,
5142 bitpos, piece_bitpos, bitsize, loc_note);
5143 return NULL;
5145 /* If the last note doesn't cover any instructions, remove it. */
5146 if (temp->last != last)
5148 temp->last->next = NULL;
5149 unused = last;
5150 last = temp->last;
5151 gcc_assert (strcmp (last->label, label) != 0);
5153 else
5155 gcc_assert (temp->first == temp->last
5156 || (temp->first->next == temp->last
5157 && TREE_CODE (decl) == PARM_DECL));
5158 memset (temp->last, '\0', sizeof (*temp->last));
5159 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5160 return temp->last;
5163 if (bitsize == -1 && NOTE_P (last->loc))
5164 last_loc_note = last->loc;
5165 else if (piece_loc != NULL
5166 && *piece_loc != NULL_RTX
5167 && piece_bitpos == bitpos
5168 && decl_piece_bitsize (*piece_loc) == bitsize)
5169 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5170 else
5171 last_loc_note = NULL_RTX;
5172 /* If the current location is the same as the end of the list,
5173 and either both or neither of the locations is uninitialized,
5174 we have nothing to do. */
5175 if (last_loc_note == NULL_RTX
5176 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5177 NOTE_VAR_LOCATION_LOC (loc_note)))
5178 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5179 != NOTE_VAR_LOCATION_STATUS (loc_note))
5180 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5181 == VAR_INIT_STATUS_UNINITIALIZED)
5182 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5183 == VAR_INIT_STATUS_UNINITIALIZED))))
5185 /* Add LOC to the end of list and update LAST. If the last
5186 element of the list has been removed above, reuse its
5187 memory for the new node, otherwise allocate a new one. */
5188 if (unused)
5190 loc = unused;
5191 memset (loc, '\0', sizeof (*loc));
5193 else
5194 loc = ggc_alloc_cleared_var_loc_node ();
5195 if (bitsize == -1 || piece_loc == NULL)
5196 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5197 else
5198 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5199 bitpos, piece_bitpos, bitsize, loc_note);
5200 last->next = loc;
5201 /* Ensure TEMP->LAST will point either to the new last but one
5202 element of the chain, or to the last element in it. */
5203 if (last != temp->last)
5204 temp->last = last;
5206 else if (unused)
5207 ggc_free (unused);
5209 else
5211 loc = ggc_alloc_cleared_var_loc_node ();
5212 temp->first = loc;
5213 temp->last = loc;
5214 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5216 return loc;
5219 /* Keep track of the number of spaces used to indent the
5220 output of the debugging routines that print the structure of
5221 the DIE internal representation. */
5222 static int print_indent;
5224 /* Indent the line the number of spaces given by print_indent. */
5226 static inline void
5227 print_spaces (FILE *outfile)
5229 fprintf (outfile, "%*s", print_indent, "");
5232 /* Print a type signature in hex. */
5234 static inline void
5235 print_signature (FILE *outfile, char *sig)
5237 int i;
5239 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5240 fprintf (outfile, "%02x", sig[i] & 0xff);
5243 /* Print the information associated with a given DIE, and its children.
5244 This routine is a debugging aid only. */
5246 static void
5247 print_die (dw_die_ref die, FILE *outfile)
5249 dw_attr_ref a;
5250 dw_die_ref c;
5251 unsigned ix;
5253 print_spaces (outfile);
5254 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5255 die->die_offset, dwarf_tag_name (die->die_tag),
5256 (void*) die);
5257 print_spaces (outfile);
5258 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5259 fprintf (outfile, " offset: %ld", die->die_offset);
5260 fprintf (outfile, " mark: %d\n", die->die_mark);
5262 if (die->comdat_type_p)
5264 print_spaces (outfile);
5265 fprintf (outfile, " signature: ");
5266 print_signature (outfile, die->die_id.die_type_node->signature);
5267 fprintf (outfile, "\n");
5270 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5272 print_spaces (outfile);
5273 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5275 switch (AT_class (a))
5277 case dw_val_class_addr:
5278 fprintf (outfile, "address");
5279 break;
5280 case dw_val_class_offset:
5281 fprintf (outfile, "offset");
5282 break;
5283 case dw_val_class_loc:
5284 fprintf (outfile, "location descriptor");
5285 break;
5286 case dw_val_class_loc_list:
5287 fprintf (outfile, "location list -> label:%s",
5288 AT_loc_list (a)->ll_symbol);
5289 break;
5290 case dw_val_class_range_list:
5291 fprintf (outfile, "range list");
5292 break;
5293 case dw_val_class_const:
5294 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5295 break;
5296 case dw_val_class_unsigned_const:
5297 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5298 break;
5299 case dw_val_class_const_double:
5300 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5301 HOST_WIDE_INT_PRINT_UNSIGNED")",
5302 a->dw_attr_val.v.val_double.high,
5303 a->dw_attr_val.v.val_double.low);
5304 break;
5305 case dw_val_class_vec:
5306 fprintf (outfile, "floating-point or vector constant");
5307 break;
5308 case dw_val_class_flag:
5309 fprintf (outfile, "%u", AT_flag (a));
5310 break;
5311 case dw_val_class_die_ref:
5312 if (AT_ref (a) != NULL)
5314 if (AT_ref (a)->comdat_type_p)
5316 fprintf (outfile, "die -> signature: ");
5317 print_signature (outfile,
5318 AT_ref (a)->die_id.die_type_node->signature);
5320 else if (AT_ref (a)->die_id.die_symbol)
5321 fprintf (outfile, "die -> label: %s",
5322 AT_ref (a)->die_id.die_symbol);
5323 else
5324 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5325 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5327 else
5328 fprintf (outfile, "die -> <null>");
5329 break;
5330 case dw_val_class_vms_delta:
5331 fprintf (outfile, "delta: @slotcount(%s-%s)",
5332 AT_vms_delta2 (a), AT_vms_delta1 (a));
5333 break;
5334 case dw_val_class_lbl_id:
5335 case dw_val_class_lineptr:
5336 case dw_val_class_macptr:
5337 case dw_val_class_high_pc:
5338 fprintf (outfile, "label: %s", AT_lbl (a));
5339 break;
5340 case dw_val_class_str:
5341 if (AT_string (a) != NULL)
5342 fprintf (outfile, "\"%s\"", AT_string (a));
5343 else
5344 fprintf (outfile, "<null>");
5345 break;
5346 case dw_val_class_file:
5347 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5348 AT_file (a)->emitted_number);
5349 break;
5350 case dw_val_class_data8:
5352 int i;
5354 for (i = 0; i < 8; i++)
5355 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5356 break;
5358 default:
5359 break;
5362 fprintf (outfile, "\n");
5365 if (die->die_child != NULL)
5367 print_indent += 4;
5368 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5369 print_indent -= 4;
5371 if (print_indent == 0)
5372 fprintf (outfile, "\n");
5375 /* Print the information collected for a given DIE. */
5377 DEBUG_FUNCTION void
5378 debug_dwarf_die (dw_die_ref die)
5380 print_die (die, stderr);
5383 DEBUG_FUNCTION void
5384 debug (die_struct &ref)
5386 print_die (&ref, stderr);
5389 DEBUG_FUNCTION void
5390 debug (die_struct *ptr)
5392 if (ptr)
5393 debug (*ptr);
5394 else
5395 fprintf (stderr, "<nil>\n");
5399 /* Print all DWARF information collected for the compilation unit.
5400 This routine is a debugging aid only. */
5402 DEBUG_FUNCTION void
5403 debug_dwarf (void)
5405 print_indent = 0;
5406 print_die (comp_unit_die (), stderr);
5409 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5410 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5411 DIE that marks the start of the DIEs for this include file. */
5413 static dw_die_ref
5414 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5416 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5417 dw_die_ref new_unit = gen_compile_unit_die (filename);
5419 new_unit->die_sib = old_unit;
5420 return new_unit;
5423 /* Close an include-file CU and reopen the enclosing one. */
5425 static dw_die_ref
5426 pop_compile_unit (dw_die_ref old_unit)
5428 dw_die_ref new_unit = old_unit->die_sib;
5430 old_unit->die_sib = NULL;
5431 return new_unit;
5434 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5435 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5436 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5438 /* Calculate the checksum of a location expression. */
5440 static inline void
5441 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5443 int tem;
5444 hashval_t hash = 0;
5446 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5447 CHECKSUM (tem);
5448 hash = hash_loc_operands (loc, hash);
5449 CHECKSUM (hash);
5452 /* Calculate the checksum of an attribute. */
5454 static void
5455 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5457 dw_loc_descr_ref loc;
5458 rtx r;
5460 CHECKSUM (at->dw_attr);
5462 /* We don't care that this was compiled with a different compiler
5463 snapshot; if the output is the same, that's what matters. */
5464 if (at->dw_attr == DW_AT_producer)
5465 return;
5467 switch (AT_class (at))
5469 case dw_val_class_const:
5470 CHECKSUM (at->dw_attr_val.v.val_int);
5471 break;
5472 case dw_val_class_unsigned_const:
5473 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5474 break;
5475 case dw_val_class_const_double:
5476 CHECKSUM (at->dw_attr_val.v.val_double);
5477 break;
5478 case dw_val_class_vec:
5479 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5480 (at->dw_attr_val.v.val_vec.length
5481 * at->dw_attr_val.v.val_vec.elt_size));
5482 break;
5483 case dw_val_class_flag:
5484 CHECKSUM (at->dw_attr_val.v.val_flag);
5485 break;
5486 case dw_val_class_str:
5487 CHECKSUM_STRING (AT_string (at));
5488 break;
5490 case dw_val_class_addr:
5491 r = AT_addr (at);
5492 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5493 CHECKSUM_STRING (XSTR (r, 0));
5494 break;
5496 case dw_val_class_offset:
5497 CHECKSUM (at->dw_attr_val.v.val_offset);
5498 break;
5500 case dw_val_class_loc:
5501 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5502 loc_checksum (loc, ctx);
5503 break;
5505 case dw_val_class_die_ref:
5506 die_checksum (AT_ref (at), ctx, mark);
5507 break;
5509 case dw_val_class_fde_ref:
5510 case dw_val_class_vms_delta:
5511 case dw_val_class_lbl_id:
5512 case dw_val_class_lineptr:
5513 case dw_val_class_macptr:
5514 case dw_val_class_high_pc:
5515 break;
5517 case dw_val_class_file:
5518 CHECKSUM_STRING (AT_file (at)->filename);
5519 break;
5521 case dw_val_class_data8:
5522 CHECKSUM (at->dw_attr_val.v.val_data8);
5523 break;
5525 default:
5526 break;
5530 /* Calculate the checksum of a DIE. */
5532 static void
5533 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5535 dw_die_ref c;
5536 dw_attr_ref a;
5537 unsigned ix;
5539 /* To avoid infinite recursion. */
5540 if (die->die_mark)
5542 CHECKSUM (die->die_mark);
5543 return;
5545 die->die_mark = ++(*mark);
5547 CHECKSUM (die->die_tag);
5549 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5550 attr_checksum (a, ctx, mark);
5552 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5555 #undef CHECKSUM
5556 #undef CHECKSUM_BLOCK
5557 #undef CHECKSUM_STRING
5559 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5560 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5561 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5562 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5563 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5564 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5565 #define CHECKSUM_ATTR(FOO) \
5566 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5568 /* Calculate the checksum of a number in signed LEB128 format. */
5570 static void
5571 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5573 unsigned char byte;
5574 bool more;
5576 while (1)
5578 byte = (value & 0x7f);
5579 value >>= 7;
5580 more = !((value == 0 && (byte & 0x40) == 0)
5581 || (value == -1 && (byte & 0x40) != 0));
5582 if (more)
5583 byte |= 0x80;
5584 CHECKSUM (byte);
5585 if (!more)
5586 break;
5590 /* Calculate the checksum of a number in unsigned LEB128 format. */
5592 static void
5593 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5595 while (1)
5597 unsigned char byte = (value & 0x7f);
5598 value >>= 7;
5599 if (value != 0)
5600 /* More bytes to follow. */
5601 byte |= 0x80;
5602 CHECKSUM (byte);
5603 if (value == 0)
5604 break;
5608 /* Checksum the context of the DIE. This adds the names of any
5609 surrounding namespaces or structures to the checksum. */
5611 static void
5612 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5614 const char *name;
5615 dw_die_ref spec;
5616 int tag = die->die_tag;
5618 if (tag != DW_TAG_namespace
5619 && tag != DW_TAG_structure_type
5620 && tag != DW_TAG_class_type)
5621 return;
5623 name = get_AT_string (die, DW_AT_name);
5625 spec = get_AT_ref (die, DW_AT_specification);
5626 if (spec != NULL)
5627 die = spec;
5629 if (die->die_parent != NULL)
5630 checksum_die_context (die->die_parent, ctx);
5632 CHECKSUM_ULEB128 ('C');
5633 CHECKSUM_ULEB128 (tag);
5634 if (name != NULL)
5635 CHECKSUM_STRING (name);
5638 /* Calculate the checksum of a location expression. */
5640 static inline void
5641 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5643 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5644 were emitted as a DW_FORM_sdata instead of a location expression. */
5645 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5647 CHECKSUM_ULEB128 (DW_FORM_sdata);
5648 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5649 return;
5652 /* Otherwise, just checksum the raw location expression. */
5653 while (loc != NULL)
5655 hashval_t hash = 0;
5657 CHECKSUM_ULEB128 (loc->dtprel);
5658 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5659 hash = hash_loc_operands (loc, hash);
5660 CHECKSUM (hash);
5661 loc = loc->dw_loc_next;
5665 /* Calculate the checksum of an attribute. */
5667 static void
5668 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5669 struct md5_ctx *ctx, int *mark)
5671 dw_loc_descr_ref loc;
5672 rtx r;
5674 if (AT_class (at) == dw_val_class_die_ref)
5676 dw_die_ref target_die = AT_ref (at);
5678 /* For pointer and reference types, we checksum only the (qualified)
5679 name of the target type (if there is a name). For friend entries,
5680 we checksum only the (qualified) name of the target type or function.
5681 This allows the checksum to remain the same whether the target type
5682 is complete or not. */
5683 if ((at->dw_attr == DW_AT_type
5684 && (tag == DW_TAG_pointer_type
5685 || tag == DW_TAG_reference_type
5686 || tag == DW_TAG_rvalue_reference_type
5687 || tag == DW_TAG_ptr_to_member_type))
5688 || (at->dw_attr == DW_AT_friend
5689 && tag == DW_TAG_friend))
5691 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5693 if (name_attr != NULL)
5695 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5697 if (decl == NULL)
5698 decl = target_die;
5699 CHECKSUM_ULEB128 ('N');
5700 CHECKSUM_ULEB128 (at->dw_attr);
5701 if (decl->die_parent != NULL)
5702 checksum_die_context (decl->die_parent, ctx);
5703 CHECKSUM_ULEB128 ('E');
5704 CHECKSUM_STRING (AT_string (name_attr));
5705 return;
5709 /* For all other references to another DIE, we check to see if the
5710 target DIE has already been visited. If it has, we emit a
5711 backward reference; if not, we descend recursively. */
5712 if (target_die->die_mark > 0)
5714 CHECKSUM_ULEB128 ('R');
5715 CHECKSUM_ULEB128 (at->dw_attr);
5716 CHECKSUM_ULEB128 (target_die->die_mark);
5718 else
5720 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5722 if (decl == NULL)
5723 decl = target_die;
5724 target_die->die_mark = ++(*mark);
5725 CHECKSUM_ULEB128 ('T');
5726 CHECKSUM_ULEB128 (at->dw_attr);
5727 if (decl->die_parent != NULL)
5728 checksum_die_context (decl->die_parent, ctx);
5729 die_checksum_ordered (target_die, ctx, mark);
5731 return;
5734 CHECKSUM_ULEB128 ('A');
5735 CHECKSUM_ULEB128 (at->dw_attr);
5737 switch (AT_class (at))
5739 case dw_val_class_const:
5740 CHECKSUM_ULEB128 (DW_FORM_sdata);
5741 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5742 break;
5744 case dw_val_class_unsigned_const:
5745 CHECKSUM_ULEB128 (DW_FORM_sdata);
5746 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5747 break;
5749 case dw_val_class_const_double:
5750 CHECKSUM_ULEB128 (DW_FORM_block);
5751 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5752 CHECKSUM (at->dw_attr_val.v.val_double);
5753 break;
5755 case dw_val_class_vec:
5756 CHECKSUM_ULEB128 (DW_FORM_block);
5757 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5758 * at->dw_attr_val.v.val_vec.elt_size);
5759 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5760 (at->dw_attr_val.v.val_vec.length
5761 * at->dw_attr_val.v.val_vec.elt_size));
5762 break;
5764 case dw_val_class_flag:
5765 CHECKSUM_ULEB128 (DW_FORM_flag);
5766 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5767 break;
5769 case dw_val_class_str:
5770 CHECKSUM_ULEB128 (DW_FORM_string);
5771 CHECKSUM_STRING (AT_string (at));
5772 break;
5774 case dw_val_class_addr:
5775 r = AT_addr (at);
5776 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5777 CHECKSUM_ULEB128 (DW_FORM_string);
5778 CHECKSUM_STRING (XSTR (r, 0));
5779 break;
5781 case dw_val_class_offset:
5782 CHECKSUM_ULEB128 (DW_FORM_sdata);
5783 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5784 break;
5786 case dw_val_class_loc:
5787 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5788 loc_checksum_ordered (loc, ctx);
5789 break;
5791 case dw_val_class_fde_ref:
5792 case dw_val_class_lbl_id:
5793 case dw_val_class_lineptr:
5794 case dw_val_class_macptr:
5795 case dw_val_class_high_pc:
5796 break;
5798 case dw_val_class_file:
5799 CHECKSUM_ULEB128 (DW_FORM_string);
5800 CHECKSUM_STRING (AT_file (at)->filename);
5801 break;
5803 case dw_val_class_data8:
5804 CHECKSUM (at->dw_attr_val.v.val_data8);
5805 break;
5807 default:
5808 break;
5812 struct checksum_attributes
5814 dw_attr_ref at_name;
5815 dw_attr_ref at_type;
5816 dw_attr_ref at_friend;
5817 dw_attr_ref at_accessibility;
5818 dw_attr_ref at_address_class;
5819 dw_attr_ref at_allocated;
5820 dw_attr_ref at_artificial;
5821 dw_attr_ref at_associated;
5822 dw_attr_ref at_binary_scale;
5823 dw_attr_ref at_bit_offset;
5824 dw_attr_ref at_bit_size;
5825 dw_attr_ref at_bit_stride;
5826 dw_attr_ref at_byte_size;
5827 dw_attr_ref at_byte_stride;
5828 dw_attr_ref at_const_value;
5829 dw_attr_ref at_containing_type;
5830 dw_attr_ref at_count;
5831 dw_attr_ref at_data_location;
5832 dw_attr_ref at_data_member_location;
5833 dw_attr_ref at_decimal_scale;
5834 dw_attr_ref at_decimal_sign;
5835 dw_attr_ref at_default_value;
5836 dw_attr_ref at_digit_count;
5837 dw_attr_ref at_discr;
5838 dw_attr_ref at_discr_list;
5839 dw_attr_ref at_discr_value;
5840 dw_attr_ref at_encoding;
5841 dw_attr_ref at_endianity;
5842 dw_attr_ref at_explicit;
5843 dw_attr_ref at_is_optional;
5844 dw_attr_ref at_location;
5845 dw_attr_ref at_lower_bound;
5846 dw_attr_ref at_mutable;
5847 dw_attr_ref at_ordering;
5848 dw_attr_ref at_picture_string;
5849 dw_attr_ref at_prototyped;
5850 dw_attr_ref at_small;
5851 dw_attr_ref at_segment;
5852 dw_attr_ref at_string_length;
5853 dw_attr_ref at_threads_scaled;
5854 dw_attr_ref at_upper_bound;
5855 dw_attr_ref at_use_location;
5856 dw_attr_ref at_use_UTF8;
5857 dw_attr_ref at_variable_parameter;
5858 dw_attr_ref at_virtuality;
5859 dw_attr_ref at_visibility;
5860 dw_attr_ref at_vtable_elem_location;
5863 /* Collect the attributes that we will want to use for the checksum. */
5865 static void
5866 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5868 dw_attr_ref a;
5869 unsigned ix;
5871 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5873 switch (a->dw_attr)
5875 case DW_AT_name:
5876 attrs->at_name = a;
5877 break;
5878 case DW_AT_type:
5879 attrs->at_type = a;
5880 break;
5881 case DW_AT_friend:
5882 attrs->at_friend = a;
5883 break;
5884 case DW_AT_accessibility:
5885 attrs->at_accessibility = a;
5886 break;
5887 case DW_AT_address_class:
5888 attrs->at_address_class = a;
5889 break;
5890 case DW_AT_allocated:
5891 attrs->at_allocated = a;
5892 break;
5893 case DW_AT_artificial:
5894 attrs->at_artificial = a;
5895 break;
5896 case DW_AT_associated:
5897 attrs->at_associated = a;
5898 break;
5899 case DW_AT_binary_scale:
5900 attrs->at_binary_scale = a;
5901 break;
5902 case DW_AT_bit_offset:
5903 attrs->at_bit_offset = a;
5904 break;
5905 case DW_AT_bit_size:
5906 attrs->at_bit_size = a;
5907 break;
5908 case DW_AT_bit_stride:
5909 attrs->at_bit_stride = a;
5910 break;
5911 case DW_AT_byte_size:
5912 attrs->at_byte_size = a;
5913 break;
5914 case DW_AT_byte_stride:
5915 attrs->at_byte_stride = a;
5916 break;
5917 case DW_AT_const_value:
5918 attrs->at_const_value = a;
5919 break;
5920 case DW_AT_containing_type:
5921 attrs->at_containing_type = a;
5922 break;
5923 case DW_AT_count:
5924 attrs->at_count = a;
5925 break;
5926 case DW_AT_data_location:
5927 attrs->at_data_location = a;
5928 break;
5929 case DW_AT_data_member_location:
5930 attrs->at_data_member_location = a;
5931 break;
5932 case DW_AT_decimal_scale:
5933 attrs->at_decimal_scale = a;
5934 break;
5935 case DW_AT_decimal_sign:
5936 attrs->at_decimal_sign = a;
5937 break;
5938 case DW_AT_default_value:
5939 attrs->at_default_value = a;
5940 break;
5941 case DW_AT_digit_count:
5942 attrs->at_digit_count = a;
5943 break;
5944 case DW_AT_discr:
5945 attrs->at_discr = a;
5946 break;
5947 case DW_AT_discr_list:
5948 attrs->at_discr_list = a;
5949 break;
5950 case DW_AT_discr_value:
5951 attrs->at_discr_value = a;
5952 break;
5953 case DW_AT_encoding:
5954 attrs->at_encoding = a;
5955 break;
5956 case DW_AT_endianity:
5957 attrs->at_endianity = a;
5958 break;
5959 case DW_AT_explicit:
5960 attrs->at_explicit = a;
5961 break;
5962 case DW_AT_is_optional:
5963 attrs->at_is_optional = a;
5964 break;
5965 case DW_AT_location:
5966 attrs->at_location = a;
5967 break;
5968 case DW_AT_lower_bound:
5969 attrs->at_lower_bound = a;
5970 break;
5971 case DW_AT_mutable:
5972 attrs->at_mutable = a;
5973 break;
5974 case DW_AT_ordering:
5975 attrs->at_ordering = a;
5976 break;
5977 case DW_AT_picture_string:
5978 attrs->at_picture_string = a;
5979 break;
5980 case DW_AT_prototyped:
5981 attrs->at_prototyped = a;
5982 break;
5983 case DW_AT_small:
5984 attrs->at_small = a;
5985 break;
5986 case DW_AT_segment:
5987 attrs->at_segment = a;
5988 break;
5989 case DW_AT_string_length:
5990 attrs->at_string_length = a;
5991 break;
5992 case DW_AT_threads_scaled:
5993 attrs->at_threads_scaled = a;
5994 break;
5995 case DW_AT_upper_bound:
5996 attrs->at_upper_bound = a;
5997 break;
5998 case DW_AT_use_location:
5999 attrs->at_use_location = a;
6000 break;
6001 case DW_AT_use_UTF8:
6002 attrs->at_use_UTF8 = a;
6003 break;
6004 case DW_AT_variable_parameter:
6005 attrs->at_variable_parameter = a;
6006 break;
6007 case DW_AT_virtuality:
6008 attrs->at_virtuality = a;
6009 break;
6010 case DW_AT_visibility:
6011 attrs->at_visibility = a;
6012 break;
6013 case DW_AT_vtable_elem_location:
6014 attrs->at_vtable_elem_location = a;
6015 break;
6016 default:
6017 break;
6022 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6024 static void
6025 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6027 dw_die_ref c;
6028 dw_die_ref decl;
6029 struct checksum_attributes attrs;
6031 CHECKSUM_ULEB128 ('D');
6032 CHECKSUM_ULEB128 (die->die_tag);
6034 memset (&attrs, 0, sizeof (attrs));
6036 decl = get_AT_ref (die, DW_AT_specification);
6037 if (decl != NULL)
6038 collect_checksum_attributes (&attrs, decl);
6039 collect_checksum_attributes (&attrs, die);
6041 CHECKSUM_ATTR (attrs.at_name);
6042 CHECKSUM_ATTR (attrs.at_accessibility);
6043 CHECKSUM_ATTR (attrs.at_address_class);
6044 CHECKSUM_ATTR (attrs.at_allocated);
6045 CHECKSUM_ATTR (attrs.at_artificial);
6046 CHECKSUM_ATTR (attrs.at_associated);
6047 CHECKSUM_ATTR (attrs.at_binary_scale);
6048 CHECKSUM_ATTR (attrs.at_bit_offset);
6049 CHECKSUM_ATTR (attrs.at_bit_size);
6050 CHECKSUM_ATTR (attrs.at_bit_stride);
6051 CHECKSUM_ATTR (attrs.at_byte_size);
6052 CHECKSUM_ATTR (attrs.at_byte_stride);
6053 CHECKSUM_ATTR (attrs.at_const_value);
6054 CHECKSUM_ATTR (attrs.at_containing_type);
6055 CHECKSUM_ATTR (attrs.at_count);
6056 CHECKSUM_ATTR (attrs.at_data_location);
6057 CHECKSUM_ATTR (attrs.at_data_member_location);
6058 CHECKSUM_ATTR (attrs.at_decimal_scale);
6059 CHECKSUM_ATTR (attrs.at_decimal_sign);
6060 CHECKSUM_ATTR (attrs.at_default_value);
6061 CHECKSUM_ATTR (attrs.at_digit_count);
6062 CHECKSUM_ATTR (attrs.at_discr);
6063 CHECKSUM_ATTR (attrs.at_discr_list);
6064 CHECKSUM_ATTR (attrs.at_discr_value);
6065 CHECKSUM_ATTR (attrs.at_encoding);
6066 CHECKSUM_ATTR (attrs.at_endianity);
6067 CHECKSUM_ATTR (attrs.at_explicit);
6068 CHECKSUM_ATTR (attrs.at_is_optional);
6069 CHECKSUM_ATTR (attrs.at_location);
6070 CHECKSUM_ATTR (attrs.at_lower_bound);
6071 CHECKSUM_ATTR (attrs.at_mutable);
6072 CHECKSUM_ATTR (attrs.at_ordering);
6073 CHECKSUM_ATTR (attrs.at_picture_string);
6074 CHECKSUM_ATTR (attrs.at_prototyped);
6075 CHECKSUM_ATTR (attrs.at_small);
6076 CHECKSUM_ATTR (attrs.at_segment);
6077 CHECKSUM_ATTR (attrs.at_string_length);
6078 CHECKSUM_ATTR (attrs.at_threads_scaled);
6079 CHECKSUM_ATTR (attrs.at_upper_bound);
6080 CHECKSUM_ATTR (attrs.at_use_location);
6081 CHECKSUM_ATTR (attrs.at_use_UTF8);
6082 CHECKSUM_ATTR (attrs.at_variable_parameter);
6083 CHECKSUM_ATTR (attrs.at_virtuality);
6084 CHECKSUM_ATTR (attrs.at_visibility);
6085 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6086 CHECKSUM_ATTR (attrs.at_type);
6087 CHECKSUM_ATTR (attrs.at_friend);
6089 /* Checksum the child DIEs. */
6090 c = die->die_child;
6091 if (c) do {
6092 dw_attr_ref name_attr;
6094 c = c->die_sib;
6095 name_attr = get_AT (c, DW_AT_name);
6096 if (is_template_instantiation (c))
6098 /* Ignore instantiations of member type and function templates. */
6100 else if (name_attr != NULL
6101 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6103 /* Use a shallow checksum for named nested types and member
6104 functions. */
6105 CHECKSUM_ULEB128 ('S');
6106 CHECKSUM_ULEB128 (c->die_tag);
6107 CHECKSUM_STRING (AT_string (name_attr));
6109 else
6111 /* Use a deep checksum for other children. */
6112 /* Mark this DIE so it gets processed when unmarking. */
6113 if (c->die_mark == 0)
6114 c->die_mark = -1;
6115 die_checksum_ordered (c, ctx, mark);
6117 } while (c != die->die_child);
6119 CHECKSUM_ULEB128 (0);
6122 /* Add a type name and tag to a hash. */
6123 static void
6124 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6126 CHECKSUM_ULEB128 (tag);
6127 CHECKSUM_STRING (name);
6130 #undef CHECKSUM
6131 #undef CHECKSUM_STRING
6132 #undef CHECKSUM_ATTR
6133 #undef CHECKSUM_LEB128
6134 #undef CHECKSUM_ULEB128
6136 /* Generate the type signature for DIE. This is computed by generating an
6137 MD5 checksum over the DIE's tag, its relevant attributes, and its
6138 children. Attributes that are references to other DIEs are processed
6139 by recursion, using the MARK field to prevent infinite recursion.
6140 If the DIE is nested inside a namespace or another type, we also
6141 need to include that context in the signature. The lower 64 bits
6142 of the resulting MD5 checksum comprise the signature. */
6144 static void
6145 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6147 int mark;
6148 const char *name;
6149 unsigned char checksum[16];
6150 struct md5_ctx ctx;
6151 dw_die_ref decl;
6152 dw_die_ref parent;
6154 name = get_AT_string (die, DW_AT_name);
6155 decl = get_AT_ref (die, DW_AT_specification);
6156 parent = get_die_parent (die);
6158 /* First, compute a signature for just the type name (and its surrounding
6159 context, if any. This is stored in the type unit DIE for link-time
6160 ODR (one-definition rule) checking. */
6162 if (is_cxx() && name != NULL)
6164 md5_init_ctx (&ctx);
6166 /* Checksum the names of surrounding namespaces and structures. */
6167 if (parent != NULL)
6168 checksum_die_context (parent, &ctx);
6170 /* Checksum the current DIE. */
6171 die_odr_checksum (die->die_tag, name, &ctx);
6172 md5_finish_ctx (&ctx, checksum);
6174 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6177 /* Next, compute the complete type signature. */
6179 md5_init_ctx (&ctx);
6180 mark = 1;
6181 die->die_mark = mark;
6183 /* Checksum the names of surrounding namespaces and structures. */
6184 if (parent != NULL)
6185 checksum_die_context (parent, &ctx);
6187 /* Checksum the DIE and its children. */
6188 die_checksum_ordered (die, &ctx, &mark);
6189 unmark_all_dies (die);
6190 md5_finish_ctx (&ctx, checksum);
6192 /* Store the signature in the type node and link the type DIE and the
6193 type node together. */
6194 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6195 DWARF_TYPE_SIGNATURE_SIZE);
6196 die->comdat_type_p = true;
6197 die->die_id.die_type_node = type_node;
6198 type_node->type_die = die;
6200 /* If the DIE is a specification, link its declaration to the type node
6201 as well. */
6202 if (decl != NULL)
6204 decl->comdat_type_p = true;
6205 decl->die_id.die_type_node = type_node;
6209 /* Do the location expressions look same? */
6210 static inline int
6211 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6213 return loc1->dw_loc_opc == loc2->dw_loc_opc
6214 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6215 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6218 /* Do the values look the same? */
6219 static int
6220 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6222 dw_loc_descr_ref loc1, loc2;
6223 rtx r1, r2;
6225 if (v1->val_class != v2->val_class)
6226 return 0;
6228 switch (v1->val_class)
6230 case dw_val_class_const:
6231 return v1->v.val_int == v2->v.val_int;
6232 case dw_val_class_unsigned_const:
6233 return v1->v.val_unsigned == v2->v.val_unsigned;
6234 case dw_val_class_const_double:
6235 return v1->v.val_double.high == v2->v.val_double.high
6236 && v1->v.val_double.low == v2->v.val_double.low;
6237 case dw_val_class_vec:
6238 if (v1->v.val_vec.length != v2->v.val_vec.length
6239 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6240 return 0;
6241 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6242 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6243 return 0;
6244 return 1;
6245 case dw_val_class_flag:
6246 return v1->v.val_flag == v2->v.val_flag;
6247 case dw_val_class_str:
6248 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6250 case dw_val_class_addr:
6251 r1 = v1->v.val_addr;
6252 r2 = v2->v.val_addr;
6253 if (GET_CODE (r1) != GET_CODE (r2))
6254 return 0;
6255 return !rtx_equal_p (r1, r2);
6257 case dw_val_class_offset:
6258 return v1->v.val_offset == v2->v.val_offset;
6260 case dw_val_class_loc:
6261 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6262 loc1 && loc2;
6263 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6264 if (!same_loc_p (loc1, loc2, mark))
6265 return 0;
6266 return !loc1 && !loc2;
6268 case dw_val_class_die_ref:
6269 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6271 case dw_val_class_fde_ref:
6272 case dw_val_class_vms_delta:
6273 case dw_val_class_lbl_id:
6274 case dw_val_class_lineptr:
6275 case dw_val_class_macptr:
6276 case dw_val_class_high_pc:
6277 return 1;
6279 case dw_val_class_file:
6280 return v1->v.val_file == v2->v.val_file;
6282 case dw_val_class_data8:
6283 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6285 default:
6286 return 1;
6290 /* Do the attributes look the same? */
6292 static int
6293 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6295 if (at1->dw_attr != at2->dw_attr)
6296 return 0;
6298 /* We don't care that this was compiled with a different compiler
6299 snapshot; if the output is the same, that's what matters. */
6300 if (at1->dw_attr == DW_AT_producer)
6301 return 1;
6303 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6306 /* Do the dies look the same? */
6308 static int
6309 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6311 dw_die_ref c1, c2;
6312 dw_attr_ref a1;
6313 unsigned ix;
6315 /* To avoid infinite recursion. */
6316 if (die1->die_mark)
6317 return die1->die_mark == die2->die_mark;
6318 die1->die_mark = die2->die_mark = ++(*mark);
6320 if (die1->die_tag != die2->die_tag)
6321 return 0;
6323 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6324 return 0;
6326 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6327 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6328 return 0;
6330 c1 = die1->die_child;
6331 c2 = die2->die_child;
6332 if (! c1)
6334 if (c2)
6335 return 0;
6337 else
6338 for (;;)
6340 if (!same_die_p (c1, c2, mark))
6341 return 0;
6342 c1 = c1->die_sib;
6343 c2 = c2->die_sib;
6344 if (c1 == die1->die_child)
6346 if (c2 == die2->die_child)
6347 break;
6348 else
6349 return 0;
6353 return 1;
6356 /* Do the dies look the same? Wrapper around same_die_p. */
6358 static int
6359 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6361 int mark = 0;
6362 int ret = same_die_p (die1, die2, &mark);
6364 unmark_all_dies (die1);
6365 unmark_all_dies (die2);
6367 return ret;
6370 /* The prefix to attach to symbols on DIEs in the current comdat debug
6371 info section. */
6372 static const char *comdat_symbol_id;
6374 /* The index of the current symbol within the current comdat CU. */
6375 static unsigned int comdat_symbol_number;
6377 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6378 children, and set comdat_symbol_id accordingly. */
6380 static void
6381 compute_section_prefix (dw_die_ref unit_die)
6383 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6384 const char *base = die_name ? lbasename (die_name) : "anonymous";
6385 char *name = XALLOCAVEC (char, strlen (base) + 64);
6386 char *p;
6387 int i, mark;
6388 unsigned char checksum[16];
6389 struct md5_ctx ctx;
6391 /* Compute the checksum of the DIE, then append part of it as hex digits to
6392 the name filename of the unit. */
6394 md5_init_ctx (&ctx);
6395 mark = 0;
6396 die_checksum (unit_die, &ctx, &mark);
6397 unmark_all_dies (unit_die);
6398 md5_finish_ctx (&ctx, checksum);
6400 sprintf (name, "%s.", base);
6401 clean_symbol_name (name);
6403 p = name + strlen (name);
6404 for (i = 0; i < 4; i++)
6406 sprintf (p, "%.2x", checksum[i]);
6407 p += 2;
6410 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6411 comdat_symbol_number = 0;
6414 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6416 static int
6417 is_type_die (dw_die_ref die)
6419 switch (die->die_tag)
6421 case DW_TAG_array_type:
6422 case DW_TAG_class_type:
6423 case DW_TAG_interface_type:
6424 case DW_TAG_enumeration_type:
6425 case DW_TAG_pointer_type:
6426 case DW_TAG_reference_type:
6427 case DW_TAG_rvalue_reference_type:
6428 case DW_TAG_string_type:
6429 case DW_TAG_structure_type:
6430 case DW_TAG_subroutine_type:
6431 case DW_TAG_union_type:
6432 case DW_TAG_ptr_to_member_type:
6433 case DW_TAG_set_type:
6434 case DW_TAG_subrange_type:
6435 case DW_TAG_base_type:
6436 case DW_TAG_const_type:
6437 case DW_TAG_file_type:
6438 case DW_TAG_packed_type:
6439 case DW_TAG_volatile_type:
6440 case DW_TAG_typedef:
6441 return 1;
6442 default:
6443 return 0;
6447 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6448 Basically, we want to choose the bits that are likely to be shared between
6449 compilations (types) and leave out the bits that are specific to individual
6450 compilations (functions). */
6452 static int
6453 is_comdat_die (dw_die_ref c)
6455 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6456 we do for stabs. The advantage is a greater likelihood of sharing between
6457 objects that don't include headers in the same order (and therefore would
6458 put the base types in a different comdat). jason 8/28/00 */
6460 if (c->die_tag == DW_TAG_base_type)
6461 return 0;
6463 if (c->die_tag == DW_TAG_pointer_type
6464 || c->die_tag == DW_TAG_reference_type
6465 || c->die_tag == DW_TAG_rvalue_reference_type
6466 || c->die_tag == DW_TAG_const_type
6467 || c->die_tag == DW_TAG_volatile_type)
6469 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6471 return t ? is_comdat_die (t) : 0;
6474 return is_type_die (c);
6477 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6478 compilation unit. */
6480 static int
6481 is_symbol_die (dw_die_ref c)
6483 return (is_type_die (c)
6484 || is_declaration_die (c)
6485 || c->die_tag == DW_TAG_namespace
6486 || c->die_tag == DW_TAG_module);
6489 /* Returns true iff C is a compile-unit DIE. */
6491 static inline bool
6492 is_cu_die (dw_die_ref c)
6494 return c && c->die_tag == DW_TAG_compile_unit;
6497 /* Returns true iff C is a unit DIE of some sort. */
6499 static inline bool
6500 is_unit_die (dw_die_ref c)
6502 return c && (c->die_tag == DW_TAG_compile_unit
6503 || c->die_tag == DW_TAG_partial_unit
6504 || c->die_tag == DW_TAG_type_unit);
6507 /* Returns true iff C is a namespace DIE. */
6509 static inline bool
6510 is_namespace_die (dw_die_ref c)
6512 return c && c->die_tag == DW_TAG_namespace;
6515 /* Returns true iff C is a class or structure DIE. */
6517 static inline bool
6518 is_class_die (dw_die_ref c)
6520 return c && (c->die_tag == DW_TAG_class_type
6521 || c->die_tag == DW_TAG_structure_type);
6524 /* Return non-zero if this DIE is a template parameter. */
6526 static inline bool
6527 is_template_parameter (dw_die_ref die)
6529 switch (die->die_tag)
6531 case DW_TAG_template_type_param:
6532 case DW_TAG_template_value_param:
6533 case DW_TAG_GNU_template_template_param:
6534 case DW_TAG_GNU_template_parameter_pack:
6535 return true;
6536 default:
6537 return false;
6541 /* Return non-zero if this DIE represents a template instantiation. */
6543 static inline bool
6544 is_template_instantiation (dw_die_ref die)
6546 dw_die_ref c;
6548 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6549 return false;
6550 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6551 return false;
6554 static char *
6555 gen_internal_sym (const char *prefix)
6557 char buf[256];
6559 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6560 return xstrdup (buf);
6563 /* Assign symbols to all worthy DIEs under DIE. */
6565 static void
6566 assign_symbol_names (dw_die_ref die)
6568 dw_die_ref c;
6570 if (is_symbol_die (die) && !die->comdat_type_p)
6572 if (comdat_symbol_id)
6574 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6576 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6577 comdat_symbol_id, comdat_symbol_number++);
6578 die->die_id.die_symbol = xstrdup (p);
6580 else
6581 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6584 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6587 struct cu_hash_table_entry
6589 dw_die_ref cu;
6590 unsigned min_comdat_num, max_comdat_num;
6591 struct cu_hash_table_entry *next;
6594 /* Helpers to manipulate hash table of CUs. */
6596 struct cu_hash_table_entry_hasher
6598 typedef cu_hash_table_entry value_type;
6599 typedef die_struct compare_type;
6600 static inline hashval_t hash (const value_type *);
6601 static inline bool equal (const value_type *, const compare_type *);
6602 static inline void remove (value_type *);
6605 inline hashval_t
6606 cu_hash_table_entry_hasher::hash (const value_type *entry)
6608 return htab_hash_string (entry->cu->die_id.die_symbol);
6611 inline bool
6612 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6613 const compare_type *entry2)
6615 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6618 inline void
6619 cu_hash_table_entry_hasher::remove (value_type *entry)
6621 struct cu_hash_table_entry *next;
6623 while (entry)
6625 next = entry->next;
6626 free (entry);
6627 entry = next;
6631 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6633 /* Check whether we have already seen this CU and set up SYM_NUM
6634 accordingly. */
6635 static int
6636 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6638 struct cu_hash_table_entry dummy;
6639 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6641 dummy.max_comdat_num = 0;
6643 slot = htable.find_slot_with_hash (cu,
6644 htab_hash_string (cu->die_id.die_symbol),
6645 INSERT);
6646 entry = *slot;
6648 for (; entry; last = entry, entry = entry->next)
6650 if (same_die_p_wrap (cu, entry->cu))
6651 break;
6654 if (entry)
6656 *sym_num = entry->min_comdat_num;
6657 return 1;
6660 entry = XCNEW (struct cu_hash_table_entry);
6661 entry->cu = cu;
6662 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6663 entry->next = *slot;
6664 *slot = entry;
6666 return 0;
6669 /* Record SYM_NUM to record of CU in HTABLE. */
6670 static void
6671 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6672 unsigned int sym_num)
6674 struct cu_hash_table_entry **slot, *entry;
6676 slot = htable.find_slot_with_hash (cu,
6677 htab_hash_string (cu->die_id.die_symbol),
6678 NO_INSERT);
6679 entry = *slot;
6681 entry->max_comdat_num = sym_num;
6684 /* Traverse the DIE (which is always comp_unit_die), and set up
6685 additional compilation units for each of the include files we see
6686 bracketed by BINCL/EINCL. */
6688 static void
6689 break_out_includes (dw_die_ref die)
6691 dw_die_ref c;
6692 dw_die_ref unit = NULL;
6693 limbo_die_node *node, **pnode;
6694 cu_hash_type cu_hash_table;
6696 c = die->die_child;
6697 if (c) do {
6698 dw_die_ref prev = c;
6699 c = c->die_sib;
6700 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6701 || (unit && is_comdat_die (c)))
6703 dw_die_ref next = c->die_sib;
6705 /* This DIE is for a secondary CU; remove it from the main one. */
6706 remove_child_with_prev (c, prev);
6708 if (c->die_tag == DW_TAG_GNU_BINCL)
6709 unit = push_new_compile_unit (unit, c);
6710 else if (c->die_tag == DW_TAG_GNU_EINCL)
6711 unit = pop_compile_unit (unit);
6712 else
6713 add_child_die (unit, c);
6714 c = next;
6715 if (c == die->die_child)
6716 break;
6718 } while (c != die->die_child);
6720 #if 0
6721 /* We can only use this in debugging, since the frontend doesn't check
6722 to make sure that we leave every include file we enter. */
6723 gcc_assert (!unit);
6724 #endif
6726 assign_symbol_names (die);
6727 cu_hash_table.create (10);
6728 for (node = limbo_die_list, pnode = &limbo_die_list;
6729 node;
6730 node = node->next)
6732 int is_dupl;
6734 compute_section_prefix (node->die);
6735 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6736 &comdat_symbol_number);
6737 assign_symbol_names (node->die);
6738 if (is_dupl)
6739 *pnode = node->next;
6740 else
6742 pnode = &node->next;
6743 record_comdat_symbol_number (node->die, cu_hash_table,
6744 comdat_symbol_number);
6747 cu_hash_table.dispose ();
6750 /* Return non-zero if this DIE is a declaration. */
6752 static int
6753 is_declaration_die (dw_die_ref die)
6755 dw_attr_ref a;
6756 unsigned ix;
6758 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6759 if (a->dw_attr == DW_AT_declaration)
6760 return 1;
6762 return 0;
6765 /* Return non-zero if this DIE is nested inside a subprogram. */
6767 static int
6768 is_nested_in_subprogram (dw_die_ref die)
6770 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6772 if (decl == NULL)
6773 decl = die;
6774 return local_scope_p (decl);
6777 /* Return non-zero if this DIE contains a defining declaration of a
6778 subprogram. */
6780 static int
6781 contains_subprogram_definition (dw_die_ref die)
6783 dw_die_ref c;
6785 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6786 return 1;
6787 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6788 return 0;
6791 /* Return non-zero if this is a type DIE that should be moved to a
6792 COMDAT .debug_types section. */
6794 static int
6795 should_move_die_to_comdat (dw_die_ref die)
6797 switch (die->die_tag)
6799 case DW_TAG_class_type:
6800 case DW_TAG_structure_type:
6801 case DW_TAG_enumeration_type:
6802 case DW_TAG_union_type:
6803 /* Don't move declarations, inlined instances, or types nested in a
6804 subprogram. */
6805 if (is_declaration_die (die)
6806 || get_AT (die, DW_AT_abstract_origin)
6807 || is_nested_in_subprogram (die))
6808 return 0;
6809 /* A type definition should never contain a subprogram definition. */
6810 gcc_assert (!contains_subprogram_definition (die));
6811 return 1;
6812 case DW_TAG_array_type:
6813 case DW_TAG_interface_type:
6814 case DW_TAG_pointer_type:
6815 case DW_TAG_reference_type:
6816 case DW_TAG_rvalue_reference_type:
6817 case DW_TAG_string_type:
6818 case DW_TAG_subroutine_type:
6819 case DW_TAG_ptr_to_member_type:
6820 case DW_TAG_set_type:
6821 case DW_TAG_subrange_type:
6822 case DW_TAG_base_type:
6823 case DW_TAG_const_type:
6824 case DW_TAG_file_type:
6825 case DW_TAG_packed_type:
6826 case DW_TAG_volatile_type:
6827 case DW_TAG_typedef:
6828 default:
6829 return 0;
6833 /* Make a clone of DIE. */
6835 static dw_die_ref
6836 clone_die (dw_die_ref die)
6838 dw_die_ref clone;
6839 dw_attr_ref a;
6840 unsigned ix;
6842 clone = ggc_alloc_cleared_die_node ();
6843 clone->die_tag = die->die_tag;
6845 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6846 add_dwarf_attr (clone, a);
6848 return clone;
6851 /* Make a clone of the tree rooted at DIE. */
6853 static dw_die_ref
6854 clone_tree (dw_die_ref die)
6856 dw_die_ref c;
6857 dw_die_ref clone = clone_die (die);
6859 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6861 return clone;
6864 /* Make a clone of DIE as a declaration. */
6866 static dw_die_ref
6867 clone_as_declaration (dw_die_ref die)
6869 dw_die_ref clone;
6870 dw_die_ref decl;
6871 dw_attr_ref a;
6872 unsigned ix;
6874 /* If the DIE is already a declaration, just clone it. */
6875 if (is_declaration_die (die))
6876 return clone_die (die);
6878 /* If the DIE is a specification, just clone its declaration DIE. */
6879 decl = get_AT_ref (die, DW_AT_specification);
6880 if (decl != NULL)
6882 clone = clone_die (decl);
6883 if (die->comdat_type_p)
6884 add_AT_die_ref (clone, DW_AT_signature, die);
6885 return clone;
6888 clone = ggc_alloc_cleared_die_node ();
6889 clone->die_tag = die->die_tag;
6891 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6893 /* We don't want to copy over all attributes.
6894 For example we don't want DW_AT_byte_size because otherwise we will no
6895 longer have a declaration and GDB will treat it as a definition. */
6897 switch (a->dw_attr)
6899 case DW_AT_artificial:
6900 case DW_AT_containing_type:
6901 case DW_AT_external:
6902 case DW_AT_name:
6903 case DW_AT_type:
6904 case DW_AT_virtuality:
6905 case DW_AT_linkage_name:
6906 case DW_AT_MIPS_linkage_name:
6907 add_dwarf_attr (clone, a);
6908 break;
6909 case DW_AT_byte_size:
6910 default:
6911 break;
6915 if (die->comdat_type_p)
6916 add_AT_die_ref (clone, DW_AT_signature, die);
6918 add_AT_flag (clone, DW_AT_declaration, 1);
6919 return clone;
6923 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6925 struct decl_table_entry
6927 dw_die_ref orig;
6928 dw_die_ref copy;
6931 /* Helpers to manipulate hash table of copied declarations. */
6933 /* Hashtable helpers. */
6935 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6937 typedef decl_table_entry value_type;
6938 typedef die_struct compare_type;
6939 static inline hashval_t hash (const value_type *);
6940 static inline bool equal (const value_type *, const compare_type *);
6943 inline hashval_t
6944 decl_table_entry_hasher::hash (const value_type *entry)
6946 return htab_hash_pointer (entry->orig);
6949 inline bool
6950 decl_table_entry_hasher::equal (const value_type *entry1,
6951 const compare_type *entry2)
6953 return entry1->orig == entry2;
6956 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6958 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6959 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6960 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6961 to check if the ancestor has already been copied into UNIT. */
6963 static dw_die_ref
6964 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6966 dw_die_ref parent = die->die_parent;
6967 dw_die_ref new_parent = unit;
6968 dw_die_ref copy;
6969 decl_table_entry **slot = NULL;
6970 struct decl_table_entry *entry = NULL;
6972 if (decl_table.is_created ())
6974 /* Check if the entry has already been copied to UNIT. */
6975 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
6976 INSERT);
6977 if (*slot != HTAB_EMPTY_ENTRY)
6979 entry = *slot;
6980 return entry->copy;
6983 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
6984 entry = XCNEW (struct decl_table_entry);
6985 entry->orig = die;
6986 entry->copy = NULL;
6987 *slot = entry;
6990 if (parent != NULL)
6992 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6993 if (spec != NULL)
6994 parent = spec;
6995 if (!is_unit_die (parent))
6996 new_parent = copy_ancestor_tree (unit, parent, decl_table);
6999 copy = clone_as_declaration (die);
7000 add_child_die (new_parent, copy);
7002 if (decl_table.is_created ())
7004 /* Record the pointer to the copy. */
7005 entry->copy = copy;
7008 return copy;
7010 /* Copy the declaration context to the new type unit DIE. This includes
7011 any surrounding namespace or type declarations. If the DIE has an
7012 AT_specification attribute, it also includes attributes and children
7013 attached to the specification, and returns a pointer to the original
7014 parent of the declaration DIE. Returns NULL otherwise. */
7016 static dw_die_ref
7017 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7019 dw_die_ref decl;
7020 dw_die_ref new_decl;
7021 dw_die_ref orig_parent = NULL;
7023 decl = get_AT_ref (die, DW_AT_specification);
7024 if (decl == NULL)
7025 decl = die;
7026 else
7028 unsigned ix;
7029 dw_die_ref c;
7030 dw_attr_ref a;
7032 /* The original DIE will be changed to a declaration, and must
7033 be moved to be a child of the original declaration DIE. */
7034 orig_parent = decl->die_parent;
7036 /* Copy the type node pointer from the new DIE to the original
7037 declaration DIE so we can forward references later. */
7038 decl->comdat_type_p = true;
7039 decl->die_id.die_type_node = die->die_id.die_type_node;
7041 remove_AT (die, DW_AT_specification);
7043 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7045 if (a->dw_attr != DW_AT_name
7046 && a->dw_attr != DW_AT_declaration
7047 && a->dw_attr != DW_AT_external)
7048 add_dwarf_attr (die, a);
7051 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7054 if (decl->die_parent != NULL
7055 && !is_unit_die (decl->die_parent))
7057 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7058 if (new_decl != NULL)
7060 remove_AT (new_decl, DW_AT_signature);
7061 add_AT_specification (die, new_decl);
7065 return orig_parent;
7068 /* Generate the skeleton ancestor tree for the given NODE, then clone
7069 the DIE and add the clone into the tree. */
7071 static void
7072 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7074 if (node->new_die != NULL)
7075 return;
7077 node->new_die = clone_as_declaration (node->old_die);
7079 if (node->parent != NULL)
7081 generate_skeleton_ancestor_tree (node->parent);
7082 add_child_die (node->parent->new_die, node->new_die);
7086 /* Generate a skeleton tree of DIEs containing any declarations that are
7087 found in the original tree. We traverse the tree looking for declaration
7088 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7090 static void
7091 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7093 skeleton_chain_node node;
7094 dw_die_ref c;
7095 dw_die_ref first;
7096 dw_die_ref prev = NULL;
7097 dw_die_ref next = NULL;
7099 node.parent = parent;
7101 first = c = parent->old_die->die_child;
7102 if (c)
7103 next = c->die_sib;
7104 if (c) do {
7105 if (prev == NULL || prev->die_sib == c)
7106 prev = c;
7107 c = next;
7108 next = (c == first ? NULL : c->die_sib);
7109 node.old_die = c;
7110 node.new_die = NULL;
7111 if (is_declaration_die (c))
7113 if (is_template_instantiation (c))
7115 /* Instantiated templates do not need to be cloned into the
7116 type unit. Just move the DIE and its children back to
7117 the skeleton tree (in the main CU). */
7118 remove_child_with_prev (c, prev);
7119 add_child_die (parent->new_die, c);
7120 c = prev;
7122 else
7124 /* Clone the existing DIE, move the original to the skeleton
7125 tree (which is in the main CU), and put the clone, with
7126 all the original's children, where the original came from
7127 (which is about to be moved to the type unit). */
7128 dw_die_ref clone = clone_die (c);
7129 move_all_children (c, clone);
7131 replace_child (c, clone, prev);
7132 generate_skeleton_ancestor_tree (parent);
7133 add_child_die (parent->new_die, c);
7134 node.new_die = c;
7135 c = clone;
7138 generate_skeleton_bottom_up (&node);
7139 } while (next != NULL);
7142 /* Wrapper function for generate_skeleton_bottom_up. */
7144 static dw_die_ref
7145 generate_skeleton (dw_die_ref die)
7147 skeleton_chain_node node;
7149 node.old_die = die;
7150 node.new_die = NULL;
7151 node.parent = NULL;
7153 /* If this type definition is nested inside another type,
7154 and is not an instantiation of a template, always leave
7155 at least a declaration in its place. */
7156 if (die->die_parent != NULL
7157 && is_type_die (die->die_parent)
7158 && !is_template_instantiation (die))
7159 node.new_die = clone_as_declaration (die);
7161 generate_skeleton_bottom_up (&node);
7162 return node.new_die;
7165 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7166 declaration. The original DIE is moved to a new compile unit so that
7167 existing references to it follow it to the new location. If any of the
7168 original DIE's descendants is a declaration, we need to replace the
7169 original DIE with a skeleton tree and move the declarations back into the
7170 skeleton tree. */
7172 static dw_die_ref
7173 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7174 dw_die_ref prev)
7176 dw_die_ref skeleton, orig_parent;
7178 /* Copy the declaration context to the type unit DIE. If the returned
7179 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7180 that DIE. */
7181 orig_parent = copy_declaration_context (unit, child);
7183 skeleton = generate_skeleton (child);
7184 if (skeleton == NULL)
7185 remove_child_with_prev (child, prev);
7186 else
7188 skeleton->comdat_type_p = true;
7189 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7191 /* If the original DIE was a specification, we need to put
7192 the skeleton under the parent DIE of the declaration.
7193 This leaves the original declaration in the tree, but
7194 it will be pruned later since there are no longer any
7195 references to it. */
7196 if (orig_parent != NULL)
7198 remove_child_with_prev (child, prev);
7199 add_child_die (orig_parent, skeleton);
7201 else
7202 replace_child (child, skeleton, prev);
7205 return skeleton;
7208 /* Traverse the DIE and set up additional .debug_types sections for each
7209 type worthy of being placed in a COMDAT section. */
7211 static void
7212 break_out_comdat_types (dw_die_ref die)
7214 dw_die_ref c;
7215 dw_die_ref first;
7216 dw_die_ref prev = NULL;
7217 dw_die_ref next = NULL;
7218 dw_die_ref unit = NULL;
7220 first = c = die->die_child;
7221 if (c)
7222 next = c->die_sib;
7223 if (c) do {
7224 if (prev == NULL || prev->die_sib == c)
7225 prev = c;
7226 c = next;
7227 next = (c == first ? NULL : c->die_sib);
7228 if (should_move_die_to_comdat (c))
7230 dw_die_ref replacement;
7231 comdat_type_node_ref type_node;
7233 /* Break out nested types into their own type units. */
7234 break_out_comdat_types (c);
7236 /* Create a new type unit DIE as the root for the new tree, and
7237 add it to the list of comdat types. */
7238 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7239 add_AT_unsigned (unit, DW_AT_language,
7240 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7241 type_node = ggc_alloc_cleared_comdat_type_node ();
7242 type_node->root_die = unit;
7243 type_node->next = comdat_type_list;
7244 comdat_type_list = type_node;
7246 /* Generate the type signature. */
7247 generate_type_signature (c, type_node);
7249 /* Copy the declaration context, attributes, and children of the
7250 declaration into the new type unit DIE, then remove this DIE
7251 from the main CU (or replace it with a skeleton if necessary). */
7252 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7253 type_node->skeleton_die = replacement;
7255 /* Add the DIE to the new compunit. */
7256 add_child_die (unit, c);
7258 if (replacement != NULL)
7259 c = replacement;
7261 else if (c->die_tag == DW_TAG_namespace
7262 || c->die_tag == DW_TAG_class_type
7263 || c->die_tag == DW_TAG_structure_type
7264 || c->die_tag == DW_TAG_union_type)
7266 /* Look for nested types that can be broken out. */
7267 break_out_comdat_types (c);
7269 } while (next != NULL);
7272 /* Like clone_tree, but additionally enter all the children into
7273 the hash table decl_table. */
7275 static dw_die_ref
7276 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7278 dw_die_ref c;
7279 dw_die_ref clone = clone_die (die);
7280 struct decl_table_entry *entry;
7281 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7282 htab_hash_pointer (die), INSERT);
7283 /* Assert that DIE isn't in the hash table yet. If it would be there
7284 before, the ancestors would be necessarily there as well, therefore
7285 clone_tree_hash wouldn't be called. */
7286 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7287 entry = XCNEW (struct decl_table_entry);
7288 entry->orig = die;
7289 entry->copy = clone;
7290 *slot = entry;
7292 FOR_EACH_CHILD (die, c,
7293 add_child_die (clone, clone_tree_hash (c, decl_table)));
7295 return clone;
7298 /* Walk the DIE and its children, looking for references to incomplete
7299 or trivial types that are unmarked (i.e., that are not in the current
7300 type_unit). */
7302 static void
7303 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7305 dw_die_ref c;
7306 dw_attr_ref a;
7307 unsigned ix;
7309 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7311 if (AT_class (a) == dw_val_class_die_ref)
7313 dw_die_ref targ = AT_ref (a);
7314 decl_table_entry **slot;
7315 struct decl_table_entry *entry;
7317 if (targ->die_mark != 0 || targ->comdat_type_p)
7318 continue;
7320 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7321 INSERT);
7323 if (*slot != HTAB_EMPTY_ENTRY)
7325 /* TARG has already been copied, so we just need to
7326 modify the reference to point to the copy. */
7327 entry = *slot;
7328 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7330 else
7332 dw_die_ref parent = unit;
7333 dw_die_ref copy = clone_die (targ);
7335 /* Record in DECL_TABLE that TARG has been copied.
7336 Need to do this now, before the recursive call,
7337 because DECL_TABLE may be expanded and SLOT
7338 would no longer be a valid pointer. */
7339 entry = XCNEW (struct decl_table_entry);
7340 entry->orig = targ;
7341 entry->copy = copy;
7342 *slot = entry;
7344 FOR_EACH_CHILD (targ, c,
7345 add_child_die (copy,
7346 clone_tree_hash (c, decl_table)));
7348 /* Make sure the cloned tree is marked as part of the
7349 type unit. */
7350 mark_dies (copy);
7352 /* If TARG has surrounding context, copy its ancestor tree
7353 into the new type unit. */
7354 if (targ->die_parent != NULL
7355 && !is_unit_die (targ->die_parent))
7356 parent = copy_ancestor_tree (unit, targ->die_parent,
7357 decl_table);
7359 add_child_die (parent, copy);
7360 a->dw_attr_val.v.val_die_ref.die = copy;
7362 /* Make sure the newly-copied DIE is walked. If it was
7363 installed in a previously-added context, it won't
7364 get visited otherwise. */
7365 if (parent != unit)
7367 /* Find the highest point of the newly-added tree,
7368 mark each node along the way, and walk from there. */
7369 parent->die_mark = 1;
7370 while (parent->die_parent
7371 && parent->die_parent->die_mark == 0)
7373 parent = parent->die_parent;
7374 parent->die_mark = 1;
7376 copy_decls_walk (unit, parent, decl_table);
7382 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7385 /* Copy declarations for "unworthy" types into the new comdat section.
7386 Incomplete types, modified types, and certain other types aren't broken
7387 out into comdat sections of their own, so they don't have a signature,
7388 and we need to copy the declaration into the same section so that we
7389 don't have an external reference. */
7391 static void
7392 copy_decls_for_unworthy_types (dw_die_ref unit)
7394 decl_hash_type decl_table;
7396 mark_dies (unit);
7397 decl_table.create (10);
7398 copy_decls_walk (unit, unit, decl_table);
7399 decl_table.dispose ();
7400 unmark_dies (unit);
7403 /* Traverse the DIE and add a sibling attribute if it may have the
7404 effect of speeding up access to siblings. To save some space,
7405 avoid generating sibling attributes for DIE's without children. */
7407 static void
7408 add_sibling_attributes (dw_die_ref die)
7410 dw_die_ref c;
7412 if (! die->die_child)
7413 return;
7415 if (die->die_parent && die != die->die_parent->die_child)
7416 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7418 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7421 /* Output all location lists for the DIE and its children. */
7423 static void
7424 output_location_lists (dw_die_ref die)
7426 dw_die_ref c;
7427 dw_attr_ref a;
7428 unsigned ix;
7430 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7431 if (AT_class (a) == dw_val_class_loc_list)
7432 output_loc_list (AT_loc_list (a));
7434 FOR_EACH_CHILD (die, c, output_location_lists (c));
7437 /* We want to limit the number of external references, because they are
7438 larger than local references: a relocation takes multiple words, and
7439 even a sig8 reference is always eight bytes, whereas a local reference
7440 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7441 So if we encounter multiple external references to the same type DIE, we
7442 make a local typedef stub for it and redirect all references there.
7444 This is the element of the hash table for keeping track of these
7445 references. */
7447 struct external_ref
7449 dw_die_ref type;
7450 dw_die_ref stub;
7451 unsigned n_refs;
7454 /* Hashtable helpers. */
7456 struct external_ref_hasher : typed_free_remove <external_ref>
7458 typedef external_ref value_type;
7459 typedef external_ref compare_type;
7460 static inline hashval_t hash (const value_type *);
7461 static inline bool equal (const value_type *, const compare_type *);
7464 inline hashval_t
7465 external_ref_hasher::hash (const value_type *r)
7467 dw_die_ref die = r->type;
7468 hashval_t h = 0;
7470 /* We can't use the address of the DIE for hashing, because
7471 that will make the order of the stub DIEs non-deterministic. */
7472 if (! die->comdat_type_p)
7473 /* We have a symbol; use it to compute a hash. */
7474 h = htab_hash_string (die->die_id.die_symbol);
7475 else
7477 /* We have a type signature; use a subset of the bits as the hash.
7478 The 8-byte signature is at least as large as hashval_t. */
7479 comdat_type_node_ref type_node = die->die_id.die_type_node;
7480 memcpy (&h, type_node->signature, sizeof (h));
7482 return h;
7485 inline bool
7486 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7488 return r1->type == r2->type;
7491 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7493 /* Return a pointer to the external_ref for references to DIE. */
7495 static struct external_ref *
7496 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7498 struct external_ref ref, *ref_p;
7499 external_ref **slot;
7501 ref.type = die;
7502 slot = map.find_slot (&ref, INSERT);
7503 if (*slot != HTAB_EMPTY_ENTRY)
7504 return *slot;
7506 ref_p = XCNEW (struct external_ref);
7507 ref_p->type = die;
7508 *slot = ref_p;
7509 return ref_p;
7512 /* Subroutine of optimize_external_refs, below.
7514 If we see a type skeleton, record it as our stub. If we see external
7515 references, remember how many we've seen. */
7517 static void
7518 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7520 dw_die_ref c;
7521 dw_attr_ref a;
7522 unsigned ix;
7523 struct external_ref *ref_p;
7525 if (is_type_die (die)
7526 && (c = get_AT_ref (die, DW_AT_signature)))
7528 /* This is a local skeleton; use it for local references. */
7529 ref_p = lookup_external_ref (map, c);
7530 ref_p->stub = die;
7533 /* Scan the DIE references, and remember any that refer to DIEs from
7534 other CUs (i.e. those which are not marked). */
7535 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7536 if (AT_class (a) == dw_val_class_die_ref
7537 && (c = AT_ref (a))->die_mark == 0
7538 && is_type_die (c))
7540 ref_p = lookup_external_ref (map, c);
7541 ref_p->n_refs++;
7544 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7547 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7548 points to an external_ref, DATA is the CU we're processing. If we don't
7549 already have a local stub, and we have multiple refs, build a stub. */
7552 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7554 struct external_ref *ref_p = *slot;
7556 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7558 /* We have multiple references to this type, so build a small stub.
7559 Both of these forms are a bit dodgy from the perspective of the
7560 DWARF standard, since technically they should have names. */
7561 dw_die_ref cu = data;
7562 dw_die_ref type = ref_p->type;
7563 dw_die_ref stub = NULL;
7565 if (type->comdat_type_p)
7567 /* If we refer to this type via sig8, use AT_signature. */
7568 stub = new_die (type->die_tag, cu, NULL_TREE);
7569 add_AT_die_ref (stub, DW_AT_signature, type);
7571 else
7573 /* Otherwise, use a typedef with no name. */
7574 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7575 add_AT_die_ref (stub, DW_AT_type, type);
7578 stub->die_mark++;
7579 ref_p->stub = stub;
7581 return 1;
7584 /* DIE is a unit; look through all the DIE references to see if there are
7585 any external references to types, and if so, create local stubs for
7586 them which will be applied in build_abbrev_table. This is useful because
7587 references to local DIEs are smaller. */
7589 static external_ref_hash_type
7590 optimize_external_refs (dw_die_ref die)
7592 external_ref_hash_type map;
7593 map.create (10);
7594 optimize_external_refs_1 (die, map);
7595 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7596 return map;
7599 /* The format of each DIE (and its attribute value pairs) is encoded in an
7600 abbreviation table. This routine builds the abbreviation table and assigns
7601 a unique abbreviation id for each abbreviation entry. The children of each
7602 die are visited recursively. */
7604 static void
7605 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7607 unsigned long abbrev_id;
7608 unsigned int n_alloc;
7609 dw_die_ref c;
7610 dw_attr_ref a;
7611 unsigned ix;
7613 /* Scan the DIE references, and replace any that refer to
7614 DIEs from other CUs (i.e. those which are not marked) with
7615 the local stubs we built in optimize_external_refs. */
7616 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7617 if (AT_class (a) == dw_val_class_die_ref
7618 && (c = AT_ref (a))->die_mark == 0)
7620 struct external_ref *ref_p;
7621 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7623 ref_p = lookup_external_ref (extern_map, c);
7624 if (ref_p->stub && ref_p->stub != die)
7625 change_AT_die_ref (a, ref_p->stub);
7626 else
7627 /* We aren't changing this reference, so mark it external. */
7628 set_AT_ref_external (a, 1);
7631 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7633 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7634 dw_attr_ref die_a, abbrev_a;
7635 unsigned ix;
7636 bool ok = true;
7638 if (abbrev->die_tag != die->die_tag)
7639 continue;
7640 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7641 continue;
7643 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7644 continue;
7646 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7648 abbrev_a = &(*abbrev->die_attr)[ix];
7649 if ((abbrev_a->dw_attr != die_a->dw_attr)
7650 || (value_format (abbrev_a) != value_format (die_a)))
7652 ok = false;
7653 break;
7656 if (ok)
7657 break;
7660 if (abbrev_id >= abbrev_die_table_in_use)
7662 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7664 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7665 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7666 n_alloc);
7668 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7669 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7670 abbrev_die_table_allocated = n_alloc;
7673 ++abbrev_die_table_in_use;
7674 abbrev_die_table[abbrev_id] = die;
7677 die->die_abbrev = abbrev_id;
7678 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7681 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7683 static int
7684 constant_size (unsigned HOST_WIDE_INT value)
7686 int log;
7688 if (value == 0)
7689 log = 0;
7690 else
7691 log = floor_log2 (value);
7693 log = log / 8;
7694 log = 1 << (floor_log2 (log) + 1);
7696 return log;
7699 /* Return the size of a DIE as it is represented in the
7700 .debug_info section. */
7702 static unsigned long
7703 size_of_die (dw_die_ref die)
7705 unsigned long size = 0;
7706 dw_attr_ref a;
7707 unsigned ix;
7708 enum dwarf_form form;
7710 size += size_of_uleb128 (die->die_abbrev);
7711 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7713 switch (AT_class (a))
7715 case dw_val_class_addr:
7716 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7718 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7719 size += size_of_uleb128 (AT_index (a));
7721 else
7722 size += DWARF2_ADDR_SIZE;
7723 break;
7724 case dw_val_class_offset:
7725 size += DWARF_OFFSET_SIZE;
7726 break;
7727 case dw_val_class_loc:
7729 unsigned long lsize = size_of_locs (AT_loc (a));
7731 /* Block length. */
7732 if (dwarf_version >= 4)
7733 size += size_of_uleb128 (lsize);
7734 else
7735 size += constant_size (lsize);
7736 size += lsize;
7738 break;
7739 case dw_val_class_loc_list:
7740 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7742 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7743 size += size_of_uleb128 (AT_index (a));
7745 else
7746 size += DWARF_OFFSET_SIZE;
7747 break;
7748 case dw_val_class_range_list:
7749 size += DWARF_OFFSET_SIZE;
7750 break;
7751 case dw_val_class_const:
7752 size += size_of_sleb128 (AT_int (a));
7753 break;
7754 case dw_val_class_unsigned_const:
7756 int csize = constant_size (AT_unsigned (a));
7757 if (dwarf_version == 3
7758 && a->dw_attr == DW_AT_data_member_location
7759 && csize >= 4)
7760 size += size_of_uleb128 (AT_unsigned (a));
7761 else
7762 size += csize;
7764 break;
7765 case dw_val_class_const_double:
7766 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7767 if (HOST_BITS_PER_WIDE_INT >= 64)
7768 size++; /* block */
7769 break;
7770 case dw_val_class_vec:
7771 size += constant_size (a->dw_attr_val.v.val_vec.length
7772 * a->dw_attr_val.v.val_vec.elt_size)
7773 + a->dw_attr_val.v.val_vec.length
7774 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7775 break;
7776 case dw_val_class_flag:
7777 if (dwarf_version >= 4)
7778 /* Currently all add_AT_flag calls pass in 1 as last argument,
7779 so DW_FORM_flag_present can be used. If that ever changes,
7780 we'll need to use DW_FORM_flag and have some optimization
7781 in build_abbrev_table that will change those to
7782 DW_FORM_flag_present if it is set to 1 in all DIEs using
7783 the same abbrev entry. */
7784 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7785 else
7786 size += 1;
7787 break;
7788 case dw_val_class_die_ref:
7789 if (AT_ref_external (a))
7791 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7792 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7793 is sized by target address length, whereas in DWARF3
7794 it's always sized as an offset. */
7795 if (use_debug_types)
7796 size += DWARF_TYPE_SIGNATURE_SIZE;
7797 else if (dwarf_version == 2)
7798 size += DWARF2_ADDR_SIZE;
7799 else
7800 size += DWARF_OFFSET_SIZE;
7802 else
7803 size += DWARF_OFFSET_SIZE;
7804 break;
7805 case dw_val_class_fde_ref:
7806 size += DWARF_OFFSET_SIZE;
7807 break;
7808 case dw_val_class_lbl_id:
7809 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7811 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7812 size += size_of_uleb128 (AT_index (a));
7814 else
7815 size += DWARF2_ADDR_SIZE;
7816 break;
7817 case dw_val_class_lineptr:
7818 case dw_val_class_macptr:
7819 size += DWARF_OFFSET_SIZE;
7820 break;
7821 case dw_val_class_str:
7822 form = AT_string_form (a);
7823 if (form == DW_FORM_strp)
7824 size += DWARF_OFFSET_SIZE;
7825 else if (form == DW_FORM_GNU_str_index)
7826 size += size_of_uleb128 (AT_index (a));
7827 else
7828 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7829 break;
7830 case dw_val_class_file:
7831 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7832 break;
7833 case dw_val_class_data8:
7834 size += 8;
7835 break;
7836 case dw_val_class_vms_delta:
7837 size += DWARF_OFFSET_SIZE;
7838 break;
7839 case dw_val_class_high_pc:
7840 size += DWARF2_ADDR_SIZE;
7841 break;
7842 default:
7843 gcc_unreachable ();
7847 return size;
7850 /* Size the debugging information associated with a given DIE. Visits the
7851 DIE's children recursively. Updates the global variable next_die_offset, on
7852 each time through. Uses the current value of next_die_offset to update the
7853 die_offset field in each DIE. */
7855 static void
7856 calc_die_sizes (dw_die_ref die)
7858 dw_die_ref c;
7860 gcc_assert (die->die_offset == 0
7861 || (unsigned long int) die->die_offset == next_die_offset);
7862 die->die_offset = next_die_offset;
7863 next_die_offset += size_of_die (die);
7865 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7867 if (die->die_child != NULL)
7868 /* Count the null byte used to terminate sibling lists. */
7869 next_die_offset += 1;
7872 /* Size just the base type children at the start of the CU.
7873 This is needed because build_abbrev needs to size locs
7874 and sizing of type based stack ops needs to know die_offset
7875 values for the base types. */
7877 static void
7878 calc_base_type_die_sizes (void)
7880 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7881 unsigned int i;
7882 dw_die_ref base_type;
7883 #if ENABLE_ASSERT_CHECKING
7884 dw_die_ref prev = comp_unit_die ()->die_child;
7885 #endif
7887 die_offset += size_of_die (comp_unit_die ());
7888 for (i = 0; base_types.iterate (i, &base_type); i++)
7890 #if ENABLE_ASSERT_CHECKING
7891 gcc_assert (base_type->die_offset == 0
7892 && prev->die_sib == base_type
7893 && base_type->die_child == NULL
7894 && base_type->die_abbrev);
7895 prev = base_type;
7896 #endif
7897 base_type->die_offset = die_offset;
7898 die_offset += size_of_die (base_type);
7902 /* Set the marks for a die and its children. We do this so
7903 that we know whether or not a reference needs to use FORM_ref_addr; only
7904 DIEs in the same CU will be marked. We used to clear out the offset
7905 and use that as the flag, but ran into ordering problems. */
7907 static void
7908 mark_dies (dw_die_ref die)
7910 dw_die_ref c;
7912 gcc_assert (!die->die_mark);
7914 die->die_mark = 1;
7915 FOR_EACH_CHILD (die, c, mark_dies (c));
7918 /* Clear the marks for a die and its children. */
7920 static void
7921 unmark_dies (dw_die_ref die)
7923 dw_die_ref c;
7925 if (! use_debug_types)
7926 gcc_assert (die->die_mark);
7928 die->die_mark = 0;
7929 FOR_EACH_CHILD (die, c, unmark_dies (c));
7932 /* Clear the marks for a die, its children and referred dies. */
7934 static void
7935 unmark_all_dies (dw_die_ref die)
7937 dw_die_ref c;
7938 dw_attr_ref a;
7939 unsigned ix;
7941 if (!die->die_mark)
7942 return;
7943 die->die_mark = 0;
7945 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7947 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7948 if (AT_class (a) == dw_val_class_die_ref)
7949 unmark_all_dies (AT_ref (a));
7952 /* Calculate if the entry should appear in the final output file. It may be
7953 from a pruned a type. */
7955 static bool
7956 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7958 if (table == pubname_table)
7960 /* Enumerator names are part of the pubname table, but the
7961 parent DW_TAG_enumeration_type die may have been pruned.
7962 Don't output them if that is the case. */
7963 if (p->die->die_tag == DW_TAG_enumerator &&
7964 (p->die->die_parent == NULL
7965 || !p->die->die_parent->die_perennial_p))
7966 return false;
7968 /* Everything else in the pubname table is included. */
7969 return true;
7972 /* The pubtypes table shouldn't include types that have been
7973 pruned. */
7974 return (p->die->die_offset != 0
7975 || !flag_eliminate_unused_debug_types);
7978 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7979 generated for the compilation unit. */
7981 static unsigned long
7982 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7984 unsigned long size;
7985 unsigned i;
7986 pubname_ref p;
7988 size = DWARF_PUBNAMES_HEADER_SIZE;
7989 FOR_EACH_VEC_ELT (*names, i, p)
7990 if (include_pubname_in_output (names, p))
7991 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7993 size += DWARF_OFFSET_SIZE;
7994 return size;
7997 /* Return the size of the information in the .debug_aranges section. */
7999 static unsigned long
8000 size_of_aranges (void)
8002 unsigned long size;
8004 size = DWARF_ARANGES_HEADER_SIZE;
8006 /* Count the address/length pair for this compilation unit. */
8007 if (text_section_used)
8008 size += 2 * DWARF2_ADDR_SIZE;
8009 if (cold_text_section_used)
8010 size += 2 * DWARF2_ADDR_SIZE;
8011 if (have_multiple_function_sections)
8013 unsigned fde_idx;
8014 dw_fde_ref fde;
8016 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8018 if (DECL_IGNORED_P (fde->decl))
8019 continue;
8020 if (!fde->in_std_section)
8021 size += 2 * DWARF2_ADDR_SIZE;
8022 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8023 size += 2 * DWARF2_ADDR_SIZE;
8027 /* Count the two zero words used to terminated the address range table. */
8028 size += 2 * DWARF2_ADDR_SIZE;
8029 return size;
8032 /* Select the encoding of an attribute value. */
8034 static enum dwarf_form
8035 value_format (dw_attr_ref a)
8037 switch (AT_class (a))
8039 case dw_val_class_addr:
8040 /* Only very few attributes allow DW_FORM_addr. */
8041 switch (a->dw_attr)
8043 case DW_AT_low_pc:
8044 case DW_AT_high_pc:
8045 case DW_AT_entry_pc:
8046 case DW_AT_trampoline:
8047 return (AT_index (a) == NOT_INDEXED
8048 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8049 default:
8050 break;
8052 switch (DWARF2_ADDR_SIZE)
8054 case 1:
8055 return DW_FORM_data1;
8056 case 2:
8057 return DW_FORM_data2;
8058 case 4:
8059 return DW_FORM_data4;
8060 case 8:
8061 return DW_FORM_data8;
8062 default:
8063 gcc_unreachable ();
8065 case dw_val_class_range_list:
8066 case dw_val_class_loc_list:
8067 if (dwarf_version >= 4)
8068 return DW_FORM_sec_offset;
8069 /* FALLTHRU */
8070 case dw_val_class_vms_delta:
8071 case dw_val_class_offset:
8072 switch (DWARF_OFFSET_SIZE)
8074 case 4:
8075 return DW_FORM_data4;
8076 case 8:
8077 return DW_FORM_data8;
8078 default:
8079 gcc_unreachable ();
8081 case dw_val_class_loc:
8082 if (dwarf_version >= 4)
8083 return DW_FORM_exprloc;
8084 switch (constant_size (size_of_locs (AT_loc (a))))
8086 case 1:
8087 return DW_FORM_block1;
8088 case 2:
8089 return DW_FORM_block2;
8090 case 4:
8091 return DW_FORM_block4;
8092 default:
8093 gcc_unreachable ();
8095 case dw_val_class_const:
8096 return DW_FORM_sdata;
8097 case dw_val_class_unsigned_const:
8098 switch (constant_size (AT_unsigned (a)))
8100 case 1:
8101 return DW_FORM_data1;
8102 case 2:
8103 return DW_FORM_data2;
8104 case 4:
8105 /* In DWARF3 DW_AT_data_member_location with
8106 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8107 constant, so we need to use DW_FORM_udata if we need
8108 a large constant. */
8109 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8110 return DW_FORM_udata;
8111 return DW_FORM_data4;
8112 case 8:
8113 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8114 return DW_FORM_udata;
8115 return DW_FORM_data8;
8116 default:
8117 gcc_unreachable ();
8119 case dw_val_class_const_double:
8120 switch (HOST_BITS_PER_WIDE_INT)
8122 case 8:
8123 return DW_FORM_data2;
8124 case 16:
8125 return DW_FORM_data4;
8126 case 32:
8127 return DW_FORM_data8;
8128 case 64:
8129 default:
8130 return DW_FORM_block1;
8132 case dw_val_class_vec:
8133 switch (constant_size (a->dw_attr_val.v.val_vec.length
8134 * a->dw_attr_val.v.val_vec.elt_size))
8136 case 1:
8137 return DW_FORM_block1;
8138 case 2:
8139 return DW_FORM_block2;
8140 case 4:
8141 return DW_FORM_block4;
8142 default:
8143 gcc_unreachable ();
8145 case dw_val_class_flag:
8146 if (dwarf_version >= 4)
8148 /* Currently all add_AT_flag calls pass in 1 as last argument,
8149 so DW_FORM_flag_present can be used. If that ever changes,
8150 we'll need to use DW_FORM_flag and have some optimization
8151 in build_abbrev_table that will change those to
8152 DW_FORM_flag_present if it is set to 1 in all DIEs using
8153 the same abbrev entry. */
8154 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8155 return DW_FORM_flag_present;
8157 return DW_FORM_flag;
8158 case dw_val_class_die_ref:
8159 if (AT_ref_external (a))
8160 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8161 else
8162 return DW_FORM_ref;
8163 case dw_val_class_fde_ref:
8164 return DW_FORM_data;
8165 case dw_val_class_lbl_id:
8166 return (AT_index (a) == NOT_INDEXED
8167 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8168 case dw_val_class_lineptr:
8169 case dw_val_class_macptr:
8170 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8171 case dw_val_class_str:
8172 return AT_string_form (a);
8173 case dw_val_class_file:
8174 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8176 case 1:
8177 return DW_FORM_data1;
8178 case 2:
8179 return DW_FORM_data2;
8180 case 4:
8181 return DW_FORM_data4;
8182 default:
8183 gcc_unreachable ();
8186 case dw_val_class_data8:
8187 return DW_FORM_data8;
8189 case dw_val_class_high_pc:
8190 switch (DWARF2_ADDR_SIZE)
8192 case 1:
8193 return DW_FORM_data1;
8194 case 2:
8195 return DW_FORM_data2;
8196 case 4:
8197 return DW_FORM_data4;
8198 case 8:
8199 return DW_FORM_data8;
8200 default:
8201 gcc_unreachable ();
8204 default:
8205 gcc_unreachable ();
8209 /* Output the encoding of an attribute value. */
8211 static void
8212 output_value_format (dw_attr_ref a)
8214 enum dwarf_form form = value_format (a);
8216 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8219 /* Given a die and id, produce the appropriate abbreviations. */
8221 static void
8222 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8224 unsigned ix;
8225 dw_attr_ref a_attr;
8227 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8228 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8229 dwarf_tag_name (abbrev->die_tag));
8231 if (abbrev->die_child != NULL)
8232 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8233 else
8234 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8236 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8238 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8239 dwarf_attr_name (a_attr->dw_attr));
8240 output_value_format (a_attr);
8243 dw2_asm_output_data (1, 0, NULL);
8244 dw2_asm_output_data (1, 0, NULL);
8248 /* Output the .debug_abbrev section which defines the DIE abbreviation
8249 table. */
8251 static void
8252 output_abbrev_section (void)
8254 unsigned long abbrev_id;
8256 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8257 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8259 /* Terminate the table. */
8260 dw2_asm_output_data (1, 0, NULL);
8263 /* Output a symbol we can use to refer to this DIE from another CU. */
8265 static inline void
8266 output_die_symbol (dw_die_ref die)
8268 const char *sym = die->die_id.die_symbol;
8270 gcc_assert (!die->comdat_type_p);
8272 if (sym == 0)
8273 return;
8275 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8276 /* We make these global, not weak; if the target doesn't support
8277 .linkonce, it doesn't support combining the sections, so debugging
8278 will break. */
8279 targetm.asm_out.globalize_label (asm_out_file, sym);
8281 ASM_OUTPUT_LABEL (asm_out_file, sym);
8284 /* Return a new location list, given the begin and end range, and the
8285 expression. */
8287 static inline dw_loc_list_ref
8288 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8289 const char *section)
8291 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8293 retlist->begin = begin;
8294 retlist->begin_entry = NULL;
8295 retlist->end = end;
8296 retlist->expr = expr;
8297 retlist->section = section;
8299 return retlist;
8302 /* Generate a new internal symbol for this location list node, if it
8303 hasn't got one yet. */
8305 static inline void
8306 gen_llsym (dw_loc_list_ref list)
8308 gcc_assert (!list->ll_symbol);
8309 list->ll_symbol = gen_internal_sym ("LLST");
8312 /* Output the location list given to us. */
8314 static void
8315 output_loc_list (dw_loc_list_ref list_head)
8317 dw_loc_list_ref curr = list_head;
8319 if (list_head->emitted)
8320 return;
8321 list_head->emitted = true;
8323 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8325 /* Walk the location list, and output each range + expression. */
8326 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8328 unsigned long size;
8329 /* Don't output an entry that starts and ends at the same address. */
8330 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8331 continue;
8332 size = size_of_locs (curr->expr);
8333 /* If the expression is too large, drop it on the floor. We could
8334 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8335 in the expression, but >= 64KB expressions for a single value
8336 in a single range are unlikely very useful. */
8337 if (size > 0xffff)
8338 continue;
8339 if (dwarf_split_debug_info)
8341 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8342 "Location list start/length entry (%s)",
8343 list_head->ll_symbol);
8344 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8345 "Location list range start index (%s)",
8346 curr->begin);
8347 /* The length field is 4 bytes. If we ever need to support
8348 an 8-byte length, we can add a new DW_LLE code or fall back
8349 to DW_LLE_GNU_start_end_entry. */
8350 dw2_asm_output_delta (4, curr->end, curr->begin,
8351 "Location list range length (%s)",
8352 list_head->ll_symbol);
8354 else if (!have_multiple_function_sections)
8356 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8357 "Location list begin address (%s)",
8358 list_head->ll_symbol);
8359 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8360 "Location list end address (%s)",
8361 list_head->ll_symbol);
8363 else
8365 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8366 "Location list begin address (%s)",
8367 list_head->ll_symbol);
8368 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8369 "Location list end address (%s)",
8370 list_head->ll_symbol);
8373 /* Output the block length for this list of location operations. */
8374 gcc_assert (size <= 0xffff);
8375 dw2_asm_output_data (2, size, "%s", "Location expression size");
8377 output_loc_sequence (curr->expr, -1);
8380 if (dwarf_split_debug_info)
8381 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8382 "Location list terminator (%s)",
8383 list_head->ll_symbol);
8384 else
8386 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8387 "Location list terminator begin (%s)",
8388 list_head->ll_symbol);
8389 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8390 "Location list terminator end (%s)",
8391 list_head->ll_symbol);
8395 /* Output a range_list offset into the debug_range section. Emit a
8396 relocated reference if val_entry is NULL, otherwise, emit an
8397 indirect reference. */
8399 static void
8400 output_range_list_offset (dw_attr_ref a)
8402 const char *name = dwarf_attr_name (a->dw_attr);
8404 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8406 char *p = strchr (ranges_section_label, '\0');
8407 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8408 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8409 debug_ranges_section, "%s", name);
8410 *p = '\0';
8412 else
8413 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8414 "%s (offset from %s)", name, ranges_section_label);
8417 /* Output the offset into the debug_loc section. */
8419 static void
8420 output_loc_list_offset (dw_attr_ref a)
8422 char *sym = AT_loc_list (a)->ll_symbol;
8424 gcc_assert (sym);
8425 if (dwarf_split_debug_info)
8426 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8427 "%s", dwarf_attr_name (a->dw_attr));
8428 else
8429 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8430 "%s", dwarf_attr_name (a->dw_attr));
8433 /* Output an attribute's index or value appropriately. */
8435 static void
8436 output_attr_index_or_value (dw_attr_ref a)
8438 const char *name = dwarf_attr_name (a->dw_attr);
8440 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8442 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8443 return;
8445 switch (AT_class (a))
8447 case dw_val_class_addr:
8448 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8449 break;
8450 case dw_val_class_high_pc:
8451 case dw_val_class_lbl_id:
8452 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8453 break;
8454 case dw_val_class_loc_list:
8455 output_loc_list_offset (a);
8456 break;
8457 default:
8458 gcc_unreachable ();
8462 /* Output a type signature. */
8464 static inline void
8465 output_signature (const char *sig, const char *name)
8467 int i;
8469 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8470 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8473 /* Output the DIE and its attributes. Called recursively to generate
8474 the definitions of each child DIE. */
8476 static void
8477 output_die (dw_die_ref die)
8479 dw_attr_ref a;
8480 dw_die_ref c;
8481 unsigned long size;
8482 unsigned ix;
8484 /* If someone in another CU might refer to us, set up a symbol for
8485 them to point to. */
8486 if (! die->comdat_type_p && die->die_id.die_symbol)
8487 output_die_symbol (die);
8489 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8490 (unsigned long)die->die_offset,
8491 dwarf_tag_name (die->die_tag));
8493 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8495 const char *name = dwarf_attr_name (a->dw_attr);
8497 switch (AT_class (a))
8499 case dw_val_class_addr:
8500 output_attr_index_or_value (a);
8501 break;
8503 case dw_val_class_offset:
8504 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8505 "%s", name);
8506 break;
8508 case dw_val_class_range_list:
8509 output_range_list_offset (a);
8510 break;
8512 case dw_val_class_loc:
8513 size = size_of_locs (AT_loc (a));
8515 /* Output the block length for this list of location operations. */
8516 if (dwarf_version >= 4)
8517 dw2_asm_output_data_uleb128 (size, "%s", name);
8518 else
8519 dw2_asm_output_data (constant_size (size), size, "%s", name);
8521 output_loc_sequence (AT_loc (a), -1);
8522 break;
8524 case dw_val_class_const:
8525 /* ??? It would be slightly more efficient to use a scheme like is
8526 used for unsigned constants below, but gdb 4.x does not sign
8527 extend. Gdb 5.x does sign extend. */
8528 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8529 break;
8531 case dw_val_class_unsigned_const:
8533 int csize = constant_size (AT_unsigned (a));
8534 if (dwarf_version == 3
8535 && a->dw_attr == DW_AT_data_member_location
8536 && csize >= 4)
8537 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8538 else
8539 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8541 break;
8543 case dw_val_class_const_double:
8545 unsigned HOST_WIDE_INT first, second;
8547 if (HOST_BITS_PER_WIDE_INT >= 64)
8548 dw2_asm_output_data (1,
8549 HOST_BITS_PER_DOUBLE_INT
8550 / HOST_BITS_PER_CHAR,
8551 NULL);
8553 if (WORDS_BIG_ENDIAN)
8555 first = a->dw_attr_val.v.val_double.high;
8556 second = a->dw_attr_val.v.val_double.low;
8558 else
8560 first = a->dw_attr_val.v.val_double.low;
8561 second = a->dw_attr_val.v.val_double.high;
8564 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8565 first, "%s", name);
8566 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8567 second, NULL);
8569 break;
8571 case dw_val_class_vec:
8573 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8574 unsigned int len = a->dw_attr_val.v.val_vec.length;
8575 unsigned int i;
8576 unsigned char *p;
8578 dw2_asm_output_data (constant_size (len * elt_size),
8579 len * elt_size, "%s", name);
8580 if (elt_size > sizeof (HOST_WIDE_INT))
8582 elt_size /= 2;
8583 len *= 2;
8585 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8586 i < len;
8587 i++, p += elt_size)
8588 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8589 "fp or vector constant word %u", i);
8590 break;
8593 case dw_val_class_flag:
8594 if (dwarf_version >= 4)
8596 /* Currently all add_AT_flag calls pass in 1 as last argument,
8597 so DW_FORM_flag_present can be used. If that ever changes,
8598 we'll need to use DW_FORM_flag and have some optimization
8599 in build_abbrev_table that will change those to
8600 DW_FORM_flag_present if it is set to 1 in all DIEs using
8601 the same abbrev entry. */
8602 gcc_assert (AT_flag (a) == 1);
8603 if (flag_debug_asm)
8604 fprintf (asm_out_file, "\t\t\t%s %s\n",
8605 ASM_COMMENT_START, name);
8606 break;
8608 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8609 break;
8611 case dw_val_class_loc_list:
8612 output_attr_index_or_value (a);
8613 break;
8615 case dw_val_class_die_ref:
8616 if (AT_ref_external (a))
8618 if (AT_ref (a)->comdat_type_p)
8620 comdat_type_node_ref type_node =
8621 AT_ref (a)->die_id.die_type_node;
8623 gcc_assert (type_node);
8624 output_signature (type_node->signature, name);
8626 else
8628 const char *sym = AT_ref (a)->die_id.die_symbol;
8629 int size;
8631 gcc_assert (sym);
8632 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8633 length, whereas in DWARF3 it's always sized as an
8634 offset. */
8635 if (dwarf_version == 2)
8636 size = DWARF2_ADDR_SIZE;
8637 else
8638 size = DWARF_OFFSET_SIZE;
8639 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8640 name);
8643 else
8645 gcc_assert (AT_ref (a)->die_offset);
8646 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8647 "%s", name);
8649 break;
8651 case dw_val_class_fde_ref:
8653 char l1[20];
8655 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8656 a->dw_attr_val.v.val_fde_index * 2);
8657 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8658 "%s", name);
8660 break;
8662 case dw_val_class_vms_delta:
8663 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8664 AT_vms_delta2 (a), AT_vms_delta1 (a),
8665 "%s", name);
8666 break;
8668 case dw_val_class_lbl_id:
8669 output_attr_index_or_value (a);
8670 break;
8672 case dw_val_class_lineptr:
8673 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8674 debug_line_section, "%s", name);
8675 break;
8677 case dw_val_class_macptr:
8678 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8679 debug_macinfo_section, "%s", name);
8680 break;
8682 case dw_val_class_str:
8683 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8684 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8685 a->dw_attr_val.v.val_str->label,
8686 debug_str_section,
8687 "%s: \"%s\"", name, AT_string (a));
8688 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8689 dw2_asm_output_data_uleb128 (AT_index (a),
8690 "%s: \"%s\"", name, AT_string (a));
8691 else
8692 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8693 break;
8695 case dw_val_class_file:
8697 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8699 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8700 a->dw_attr_val.v.val_file->filename);
8701 break;
8704 case dw_val_class_data8:
8706 int i;
8708 for (i = 0; i < 8; i++)
8709 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8710 i == 0 ? "%s" : NULL, name);
8711 break;
8714 case dw_val_class_high_pc:
8715 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8716 get_AT_low_pc (die), "DW_AT_high_pc");
8717 break;
8719 default:
8720 gcc_unreachable ();
8724 FOR_EACH_CHILD (die, c, output_die (c));
8726 /* Add null byte to terminate sibling list. */
8727 if (die->die_child != NULL)
8728 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8729 (unsigned long) die->die_offset);
8732 /* Output the compilation unit that appears at the beginning of the
8733 .debug_info section, and precedes the DIE descriptions. */
8735 static void
8736 output_compilation_unit_header (void)
8738 int ver = dwarf_version;
8740 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8741 dw2_asm_output_data (4, 0xffffffff,
8742 "Initial length escape value indicating 64-bit DWARF extension");
8743 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8744 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8745 "Length of Compilation Unit Info");
8746 dw2_asm_output_data (2, ver, "DWARF version number");
8747 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8748 debug_abbrev_section,
8749 "Offset Into Abbrev. Section");
8750 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8753 /* Output the compilation unit DIE and its children. */
8755 static void
8756 output_comp_unit (dw_die_ref die, int output_if_empty)
8758 const char *secname, *oldsym;
8759 char *tmp;
8760 external_ref_hash_type extern_map;
8762 /* Unless we are outputting main CU, we may throw away empty ones. */
8763 if (!output_if_empty && die->die_child == NULL)
8764 return;
8766 /* Even if there are no children of this DIE, we must output the information
8767 about the compilation unit. Otherwise, on an empty translation unit, we
8768 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8769 will then complain when examining the file. First mark all the DIEs in
8770 this CU so we know which get local refs. */
8771 mark_dies (die);
8773 extern_map = optimize_external_refs (die);
8775 build_abbrev_table (die, extern_map);
8777 extern_map.dispose ();
8779 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8780 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8781 calc_die_sizes (die);
8783 oldsym = die->die_id.die_symbol;
8784 if (oldsym)
8786 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8788 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8789 secname = tmp;
8790 die->die_id.die_symbol = NULL;
8791 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8793 else
8795 switch_to_section (debug_info_section);
8796 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8797 info_section_emitted = true;
8800 /* Output debugging information. */
8801 output_compilation_unit_header ();
8802 output_die (die);
8804 /* Leave the marks on the main CU, so we can check them in
8805 output_pubnames. */
8806 if (oldsym)
8808 unmark_dies (die);
8809 die->die_id.die_symbol = oldsym;
8813 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8814 and .debug_pubtypes. This is configured per-target, but can be
8815 overridden by the -gpubnames or -gno-pubnames options. */
8817 static inline bool
8818 want_pubnames (void)
8820 if (debug_generate_pub_sections != -1)
8821 return debug_generate_pub_sections;
8822 return targetm.want_debug_pub_sections;
8825 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8827 static void
8828 add_AT_pubnames (dw_die_ref die)
8830 if (want_pubnames ())
8831 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8834 /* Add a string attribute value to a skeleton DIE. */
8836 static inline void
8837 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8838 const char *str)
8840 dw_attr_node attr;
8841 struct indirect_string_node *node;
8843 if (! skeleton_debug_str_hash)
8844 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8845 debug_str_eq, NULL);
8847 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8848 find_string_form (node);
8849 if (node->form == DW_FORM_GNU_str_index)
8850 node->form = DW_FORM_strp;
8852 attr.dw_attr = attr_kind;
8853 attr.dw_attr_val.val_class = dw_val_class_str;
8854 attr.dw_attr_val.val_entry = NULL;
8855 attr.dw_attr_val.v.val_str = node;
8856 add_dwarf_attr (die, &attr);
8859 /* Helper function to generate top-level dies for skeleton debug_info and
8860 debug_types. */
8862 static void
8863 add_top_level_skeleton_die_attrs (dw_die_ref die)
8865 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8866 const char *comp_dir = comp_dir_string ();
8868 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8869 if (comp_dir != NULL)
8870 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8871 add_AT_pubnames (die);
8872 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8875 /* Return the single type-unit die for skeleton type units. */
8877 static dw_die_ref
8878 get_skeleton_type_unit (void)
8880 /* For dwarf_split_debug_sections with use_type info, all type units in the
8881 skeleton sections have identical dies (but different headers). This
8882 single die will be output many times. */
8884 static dw_die_ref skeleton_type_unit = NULL;
8886 if (skeleton_type_unit == NULL)
8888 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8889 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8890 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8892 return skeleton_type_unit;
8895 /* Output skeleton debug sections that point to the dwo file. */
8897 static void
8898 output_skeleton_debug_sections (dw_die_ref comp_unit)
8900 /* These attributes will be found in the full debug_info section. */
8901 remove_AT (comp_unit, DW_AT_producer);
8902 remove_AT (comp_unit, DW_AT_language);
8904 switch_to_section (debug_skeleton_info_section);
8905 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8907 /* Produce the skeleton compilation-unit header. This one differs enough from
8908 a normal CU header that it's better not to call output_compilation_unit
8909 header. */
8910 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8911 dw2_asm_output_data (4, 0xffffffff,
8912 "Initial length escape value indicating 64-bit DWARF extension");
8914 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8915 DWARF_COMPILE_UNIT_HEADER_SIZE
8916 - DWARF_INITIAL_LENGTH_SIZE
8917 + size_of_die (comp_unit),
8918 "Length of Compilation Unit Info");
8919 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8920 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8921 debug_abbrev_section,
8922 "Offset Into Abbrev. Section");
8923 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8925 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8926 output_die (comp_unit);
8928 /* Build the skeleton debug_abbrev section. */
8929 switch_to_section (debug_skeleton_abbrev_section);
8930 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8932 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8933 if (use_debug_types)
8934 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8936 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8939 /* Output a comdat type unit DIE and its children. */
8941 static void
8942 output_comdat_type_unit (comdat_type_node *node)
8944 const char *secname;
8945 char *tmp;
8946 int i;
8947 #if defined (OBJECT_FORMAT_ELF)
8948 tree comdat_key;
8949 #endif
8950 external_ref_hash_type extern_map;
8952 /* First mark all the DIEs in this CU so we know which get local refs. */
8953 mark_dies (node->root_die);
8955 extern_map = optimize_external_refs (node->root_die);
8957 build_abbrev_table (node->root_die, extern_map);
8959 extern_map.dispose ();
8961 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8962 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8963 calc_die_sizes (node->root_die);
8965 #if defined (OBJECT_FORMAT_ELF)
8966 if (!dwarf_split_debug_info)
8967 secname = ".debug_types";
8968 else
8969 secname = ".debug_types.dwo";
8971 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8972 sprintf (tmp, "wt.");
8973 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8974 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8975 comdat_key = get_identifier (tmp);
8976 targetm.asm_out.named_section (secname,
8977 SECTION_DEBUG | SECTION_LINKONCE,
8978 comdat_key);
8979 #else
8980 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8981 sprintf (tmp, ".gnu.linkonce.wt.");
8982 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8983 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8984 secname = tmp;
8985 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8986 #endif
8988 /* Output debugging information. */
8989 output_compilation_unit_header ();
8990 output_signature (node->signature, "Type Signature");
8991 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8992 "Offset to Type DIE");
8993 output_die (node->root_die);
8995 unmark_dies (node->root_die);
8997 #if defined (OBJECT_FORMAT_ELF)
8998 if (dwarf_split_debug_info)
9000 /* Produce the skeleton type-unit header. */
9001 const char *secname = ".debug_types";
9003 targetm.asm_out.named_section (secname,
9004 SECTION_DEBUG | SECTION_LINKONCE,
9005 comdat_key);
9006 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9007 dw2_asm_output_data (4, 0xffffffff,
9008 "Initial length escape value indicating 64-bit DWARF extension");
9010 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9011 DWARF_COMPILE_UNIT_HEADER_SIZE
9012 - DWARF_INITIAL_LENGTH_SIZE
9013 + size_of_die (get_skeleton_type_unit ())
9014 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9015 "Length of Type Unit Info");
9016 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9017 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9018 debug_skeleton_abbrev_section_label,
9019 debug_abbrev_section,
9020 "Offset Into Abbrev. Section");
9021 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9022 output_signature (node->signature, "Type Signature");
9023 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9025 output_die (get_skeleton_type_unit ());
9027 #endif
9030 /* Return the DWARF2/3 pubname associated with a decl. */
9032 static const char *
9033 dwarf2_name (tree decl, int scope)
9035 if (DECL_NAMELESS (decl))
9036 return NULL;
9037 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9040 /* Add a new entry to .debug_pubnames if appropriate. */
9042 static void
9043 add_pubname_string (const char *str, dw_die_ref die)
9045 pubname_entry e;
9047 e.die = die;
9048 e.name = xstrdup (str);
9049 vec_safe_push (pubname_table, e);
9052 static void
9053 add_pubname (tree decl, dw_die_ref die)
9055 if (!want_pubnames ())
9056 return;
9058 /* Don't add items to the table when we expect that the consumer will have
9059 just read the enclosing die. For example, if the consumer is looking at a
9060 class_member, it will either be inside the class already, or will have just
9061 looked up the class to find the member. Either way, searching the class is
9062 faster than searching the index. */
9063 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9064 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9066 const char *name = dwarf2_name (decl, 1);
9068 if (name)
9069 add_pubname_string (name, die);
9073 /* Add an enumerator to the pubnames section. */
9075 static void
9076 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9078 pubname_entry e;
9080 gcc_assert (scope_name);
9081 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9082 e.die = die;
9083 vec_safe_push (pubname_table, e);
9086 /* Add a new entry to .debug_pubtypes if appropriate. */
9088 static void
9089 add_pubtype (tree decl, dw_die_ref die)
9091 pubname_entry e;
9093 if (!want_pubnames ())
9094 return;
9096 if ((TREE_PUBLIC (decl)
9097 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9098 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9100 tree scope = NULL;
9101 const char *scope_name = "";
9102 const char *sep = is_cxx () ? "::" : ".";
9103 const char *name;
9105 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9106 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9108 scope_name = lang_hooks.dwarf_name (scope, 1);
9109 if (scope_name != NULL && scope_name[0] != '\0')
9110 scope_name = concat (scope_name, sep, NULL);
9111 else
9112 scope_name = "";
9115 if (TYPE_P (decl))
9116 name = type_tag (decl);
9117 else
9118 name = lang_hooks.dwarf_name (decl, 1);
9120 /* If we don't have a name for the type, there's no point in adding
9121 it to the table. */
9122 if (name != NULL && name[0] != '\0')
9124 e.die = die;
9125 e.name = concat (scope_name, name, NULL);
9126 vec_safe_push (pubtype_table, e);
9129 /* Although it might be more consistent to add the pubinfo for the
9130 enumerators as their dies are created, they should only be added if the
9131 enum type meets the criteria above. So rather than re-check the parent
9132 enum type whenever an enumerator die is created, just output them all
9133 here. This isn't protected by the name conditional because anonymous
9134 enums don't have names. */
9135 if (die->die_tag == DW_TAG_enumeration_type)
9137 dw_die_ref c;
9139 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9144 /* Output the public names table used to speed up access to externally
9145 visible names; or the public types table used to find type definitions. */
9147 static void
9148 output_pubnames (vec<pubname_entry, va_gc> *names)
9150 unsigned i;
9151 unsigned long pubnames_length = size_of_pubnames (names);
9152 pubname_ref pub;
9154 if (!want_pubnames () || !info_section_emitted)
9155 return;
9156 if (names == pubname_table)
9157 switch_to_section (debug_pubnames_section);
9158 else
9159 switch_to_section (debug_pubtypes_section);
9160 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9161 dw2_asm_output_data (4, 0xffffffff,
9162 "Initial length escape value indicating 64-bit DWARF extension");
9163 if (names == pubname_table)
9164 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9165 "Length of Public Names Info");
9166 else
9167 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9168 "Length of Public Type Names Info");
9169 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
9170 dw2_asm_output_data (2, 2, "DWARF Version");
9171 if (dwarf_split_debug_info)
9172 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9173 debug_skeleton_info_section,
9174 "Offset of Compilation Unit Info");
9175 else
9176 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9177 debug_info_section,
9178 "Offset of Compilation Unit Info");
9179 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9180 "Compilation Unit Length");
9182 FOR_EACH_VEC_ELT (*names, i, pub)
9184 if (include_pubname_in_output (names, pub))
9186 dw_offset die_offset = pub->die->die_offset;
9188 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9189 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9190 gcc_assert (pub->die->die_mark);
9192 /* If we're putting types in their own .debug_types sections,
9193 the .debug_pubtypes table will still point to the compile
9194 unit (not the type unit), so we want to use the offset of
9195 the skeleton DIE (if there is one). */
9196 if (pub->die->comdat_type_p && names == pubtype_table)
9198 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9200 if (type_node != NULL)
9201 die_offset = (type_node->skeleton_die != NULL
9202 ? type_node->skeleton_die->die_offset
9203 : 0);
9206 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9208 dw2_asm_output_nstring (pub->name, -1, "external name");
9212 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9215 /* Output the information that goes into the .debug_aranges table.
9216 Namely, define the beginning and ending address range of the
9217 text section generated for this compilation unit. */
9219 static void
9220 output_aranges (unsigned long aranges_length)
9222 unsigned i;
9224 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9225 dw2_asm_output_data (4, 0xffffffff,
9226 "Initial length escape value indicating 64-bit DWARF extension");
9227 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9228 "Length of Address Ranges Info");
9229 /* Version number for aranges is still 2, even in DWARF3. */
9230 dw2_asm_output_data (2, 2, "DWARF Version");
9231 if (dwarf_split_debug_info)
9232 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9233 debug_skeleton_info_section,
9234 "Offset of Compilation Unit Info");
9235 else
9236 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9237 debug_info_section,
9238 "Offset of Compilation Unit Info");
9239 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9240 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9242 /* We need to align to twice the pointer size here. */
9243 if (DWARF_ARANGES_PAD_SIZE)
9245 /* Pad using a 2 byte words so that padding is correct for any
9246 pointer size. */
9247 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9248 2 * DWARF2_ADDR_SIZE);
9249 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9250 dw2_asm_output_data (2, 0, NULL);
9253 /* It is necessary not to output these entries if the sections were
9254 not used; if the sections were not used, the length will be 0 and
9255 the address may end up as 0 if the section is discarded by ld
9256 --gc-sections, leaving an invalid (0, 0) entry that can be
9257 confused with the terminator. */
9258 if (text_section_used)
9260 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9261 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9262 text_section_label, "Length");
9264 if (cold_text_section_used)
9266 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9267 "Address");
9268 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9269 cold_text_section_label, "Length");
9272 if (have_multiple_function_sections)
9274 unsigned fde_idx;
9275 dw_fde_ref fde;
9277 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9279 if (DECL_IGNORED_P (fde->decl))
9280 continue;
9281 if (!fde->in_std_section)
9283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9284 "Address");
9285 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9286 fde->dw_fde_begin, "Length");
9288 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9290 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9291 "Address");
9292 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9293 fde->dw_fde_second_begin, "Length");
9298 /* Output the terminator words. */
9299 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9300 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9303 /* Add a new entry to .debug_ranges. Return the offset at which it
9304 was placed. */
9306 static unsigned int
9307 add_ranges_num (int num)
9309 unsigned int in_use = ranges_table_in_use;
9311 if (in_use == ranges_table_allocated)
9313 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9314 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9315 ranges_table_allocated);
9316 memset (ranges_table + ranges_table_in_use, 0,
9317 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9320 ranges_table[in_use].num = num;
9321 ranges_table_in_use = in_use + 1;
9323 return in_use * 2 * DWARF2_ADDR_SIZE;
9326 /* Add a new entry to .debug_ranges corresponding to a block, or a
9327 range terminator if BLOCK is NULL. */
9329 static unsigned int
9330 add_ranges (const_tree block)
9332 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9335 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9336 When using dwarf_split_debug_info, address attributes in dies destined
9337 for the final executable should be direct references--setting the
9338 parameter force_direct ensures this behavior. */
9340 static void
9341 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9342 bool *added, bool force_direct)
9344 unsigned int in_use = ranges_by_label_in_use;
9345 unsigned int offset;
9347 if (in_use == ranges_by_label_allocated)
9349 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9350 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9351 ranges_by_label,
9352 ranges_by_label_allocated);
9353 memset (ranges_by_label + ranges_by_label_in_use, 0,
9354 RANGES_TABLE_INCREMENT
9355 * sizeof (struct dw_ranges_by_label_struct));
9358 ranges_by_label[in_use].begin = begin;
9359 ranges_by_label[in_use].end = end;
9360 ranges_by_label_in_use = in_use + 1;
9362 offset = add_ranges_num (-(int)in_use - 1);
9363 if (!*added)
9365 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9366 *added = true;
9370 static void
9371 output_ranges (void)
9373 unsigned i;
9374 static const char *const start_fmt = "Offset %#x";
9375 const char *fmt = start_fmt;
9377 for (i = 0; i < ranges_table_in_use; i++)
9379 int block_num = ranges_table[i].num;
9381 if (block_num > 0)
9383 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9384 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9386 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9387 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9389 /* If all code is in the text section, then the compilation
9390 unit base address defaults to DW_AT_low_pc, which is the
9391 base of the text section. */
9392 if (!have_multiple_function_sections)
9394 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9395 text_section_label,
9396 fmt, i * 2 * DWARF2_ADDR_SIZE);
9397 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9398 text_section_label, NULL);
9401 /* Otherwise, the compilation unit base address is zero,
9402 which allows us to use absolute addresses, and not worry
9403 about whether the target supports cross-section
9404 arithmetic. */
9405 else
9407 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9408 fmt, i * 2 * DWARF2_ADDR_SIZE);
9409 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9412 fmt = NULL;
9415 /* Negative block_num stands for an index into ranges_by_label. */
9416 else if (block_num < 0)
9418 int lab_idx = - block_num - 1;
9420 if (!have_multiple_function_sections)
9422 gcc_unreachable ();
9423 #if 0
9424 /* If we ever use add_ranges_by_labels () for a single
9425 function section, all we have to do is to take out
9426 the #if 0 above. */
9427 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9428 ranges_by_label[lab_idx].begin,
9429 text_section_label,
9430 fmt, i * 2 * DWARF2_ADDR_SIZE);
9431 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9432 ranges_by_label[lab_idx].end,
9433 text_section_label, NULL);
9434 #endif
9436 else
9438 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9439 ranges_by_label[lab_idx].begin,
9440 fmt, i * 2 * DWARF2_ADDR_SIZE);
9441 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9442 ranges_by_label[lab_idx].end,
9443 NULL);
9446 else
9448 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9449 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9450 fmt = start_fmt;
9455 /* Data structure containing information about input files. */
9456 struct file_info
9458 const char *path; /* Complete file name. */
9459 const char *fname; /* File name part. */
9460 int length; /* Length of entire string. */
9461 struct dwarf_file_data * file_idx; /* Index in input file table. */
9462 int dir_idx; /* Index in directory table. */
9465 /* Data structure containing information about directories with source
9466 files. */
9467 struct dir_info
9469 const char *path; /* Path including directory name. */
9470 int length; /* Path length. */
9471 int prefix; /* Index of directory entry which is a prefix. */
9472 int count; /* Number of files in this directory. */
9473 int dir_idx; /* Index of directory used as base. */
9476 /* Callback function for file_info comparison. We sort by looking at
9477 the directories in the path. */
9479 static int
9480 file_info_cmp (const void *p1, const void *p2)
9482 const struct file_info *const s1 = (const struct file_info *) p1;
9483 const struct file_info *const s2 = (const struct file_info *) p2;
9484 const unsigned char *cp1;
9485 const unsigned char *cp2;
9487 /* Take care of file names without directories. We need to make sure that
9488 we return consistent values to qsort since some will get confused if
9489 we return the same value when identical operands are passed in opposite
9490 orders. So if neither has a directory, return 0 and otherwise return
9491 1 or -1 depending on which one has the directory. */
9492 if ((s1->path == s1->fname || s2->path == s2->fname))
9493 return (s2->path == s2->fname) - (s1->path == s1->fname);
9495 cp1 = (const unsigned char *) s1->path;
9496 cp2 = (const unsigned char *) s2->path;
9498 while (1)
9500 ++cp1;
9501 ++cp2;
9502 /* Reached the end of the first path? If so, handle like above. */
9503 if ((cp1 == (const unsigned char *) s1->fname)
9504 || (cp2 == (const unsigned char *) s2->fname))
9505 return ((cp2 == (const unsigned char *) s2->fname)
9506 - (cp1 == (const unsigned char *) s1->fname));
9508 /* Character of current path component the same? */
9509 else if (*cp1 != *cp2)
9510 return *cp1 - *cp2;
9514 struct file_name_acquire_data
9516 struct file_info *files;
9517 int used_files;
9518 int max_files;
9521 /* Traversal function for the hash table. */
9523 static int
9524 file_name_acquire (void ** slot, void *data)
9526 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9527 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9528 struct file_info *fi;
9529 const char *f;
9531 gcc_assert (fnad->max_files >= d->emitted_number);
9533 if (! d->emitted_number)
9534 return 1;
9536 gcc_assert (fnad->max_files != fnad->used_files);
9538 fi = fnad->files + fnad->used_files++;
9540 /* Skip all leading "./". */
9541 f = d->filename;
9542 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9543 f += 2;
9545 /* Create a new array entry. */
9546 fi->path = f;
9547 fi->length = strlen (f);
9548 fi->file_idx = d;
9550 /* Search for the file name part. */
9551 f = strrchr (f, DIR_SEPARATOR);
9552 #if defined (DIR_SEPARATOR_2)
9554 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9556 if (g != NULL)
9558 if (f == NULL || f < g)
9559 f = g;
9562 #endif
9564 fi->fname = f == NULL ? fi->path : f + 1;
9565 return 1;
9568 /* Output the directory table and the file name table. We try to minimize
9569 the total amount of memory needed. A heuristic is used to avoid large
9570 slowdowns with many input files. */
9572 static void
9573 output_file_names (void)
9575 struct file_name_acquire_data fnad;
9576 int numfiles;
9577 struct file_info *files;
9578 struct dir_info *dirs;
9579 int *saved;
9580 int *savehere;
9581 int *backmap;
9582 int ndirs;
9583 int idx_offset;
9584 int i;
9586 if (!last_emitted_file)
9588 dw2_asm_output_data (1, 0, "End directory table");
9589 dw2_asm_output_data (1, 0, "End file name table");
9590 return;
9593 numfiles = last_emitted_file->emitted_number;
9595 /* Allocate the various arrays we need. */
9596 files = XALLOCAVEC (struct file_info, numfiles);
9597 dirs = XALLOCAVEC (struct dir_info, numfiles);
9599 fnad.files = files;
9600 fnad.used_files = 0;
9601 fnad.max_files = numfiles;
9602 htab_traverse (file_table, file_name_acquire, &fnad);
9603 gcc_assert (fnad.used_files == fnad.max_files);
9605 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9607 /* Find all the different directories used. */
9608 dirs[0].path = files[0].path;
9609 dirs[0].length = files[0].fname - files[0].path;
9610 dirs[0].prefix = -1;
9611 dirs[0].count = 1;
9612 dirs[0].dir_idx = 0;
9613 files[0].dir_idx = 0;
9614 ndirs = 1;
9616 for (i = 1; i < numfiles; i++)
9617 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9618 && memcmp (dirs[ndirs - 1].path, files[i].path,
9619 dirs[ndirs - 1].length) == 0)
9621 /* Same directory as last entry. */
9622 files[i].dir_idx = ndirs - 1;
9623 ++dirs[ndirs - 1].count;
9625 else
9627 int j;
9629 /* This is a new directory. */
9630 dirs[ndirs].path = files[i].path;
9631 dirs[ndirs].length = files[i].fname - files[i].path;
9632 dirs[ndirs].count = 1;
9633 dirs[ndirs].dir_idx = ndirs;
9634 files[i].dir_idx = ndirs;
9636 /* Search for a prefix. */
9637 dirs[ndirs].prefix = -1;
9638 for (j = 0; j < ndirs; j++)
9639 if (dirs[j].length < dirs[ndirs].length
9640 && dirs[j].length > 1
9641 && (dirs[ndirs].prefix == -1
9642 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9643 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9644 dirs[ndirs].prefix = j;
9646 ++ndirs;
9649 /* Now to the actual work. We have to find a subset of the directories which
9650 allow expressing the file name using references to the directory table
9651 with the least amount of characters. We do not do an exhaustive search
9652 where we would have to check out every combination of every single
9653 possible prefix. Instead we use a heuristic which provides nearly optimal
9654 results in most cases and never is much off. */
9655 saved = XALLOCAVEC (int, ndirs);
9656 savehere = XALLOCAVEC (int, ndirs);
9658 memset (saved, '\0', ndirs * sizeof (saved[0]));
9659 for (i = 0; i < ndirs; i++)
9661 int j;
9662 int total;
9664 /* We can always save some space for the current directory. But this
9665 does not mean it will be enough to justify adding the directory. */
9666 savehere[i] = dirs[i].length;
9667 total = (savehere[i] - saved[i]) * dirs[i].count;
9669 for (j = i + 1; j < ndirs; j++)
9671 savehere[j] = 0;
9672 if (saved[j] < dirs[i].length)
9674 /* Determine whether the dirs[i] path is a prefix of the
9675 dirs[j] path. */
9676 int k;
9678 k = dirs[j].prefix;
9679 while (k != -1 && k != (int) i)
9680 k = dirs[k].prefix;
9682 if (k == (int) i)
9684 /* Yes it is. We can possibly save some memory by
9685 writing the filenames in dirs[j] relative to
9686 dirs[i]. */
9687 savehere[j] = dirs[i].length;
9688 total += (savehere[j] - saved[j]) * dirs[j].count;
9693 /* Check whether we can save enough to justify adding the dirs[i]
9694 directory. */
9695 if (total > dirs[i].length + 1)
9697 /* It's worthwhile adding. */
9698 for (j = i; j < ndirs; j++)
9699 if (savehere[j] > 0)
9701 /* Remember how much we saved for this directory so far. */
9702 saved[j] = savehere[j];
9704 /* Remember the prefix directory. */
9705 dirs[j].dir_idx = i;
9710 /* Emit the directory name table. */
9711 idx_offset = dirs[0].length > 0 ? 1 : 0;
9712 for (i = 1 - idx_offset; i < ndirs; i++)
9713 dw2_asm_output_nstring (dirs[i].path,
9714 dirs[i].length
9715 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9716 "Directory Entry: %#x", i + idx_offset);
9718 dw2_asm_output_data (1, 0, "End directory table");
9720 /* We have to emit them in the order of emitted_number since that's
9721 used in the debug info generation. To do this efficiently we
9722 generate a back-mapping of the indices first. */
9723 backmap = XALLOCAVEC (int, numfiles);
9724 for (i = 0; i < numfiles; i++)
9725 backmap[files[i].file_idx->emitted_number - 1] = i;
9727 /* Now write all the file names. */
9728 for (i = 0; i < numfiles; i++)
9730 int file_idx = backmap[i];
9731 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9733 #ifdef VMS_DEBUGGING_INFO
9734 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9736 /* Setting these fields can lead to debugger miscomparisons,
9737 but VMS Debug requires them to be set correctly. */
9739 int ver;
9740 long long cdt;
9741 long siz;
9742 int maxfilelen = strlen (files[file_idx].path)
9743 + dirs[dir_idx].length
9744 + MAX_VMS_VERSION_LEN + 1;
9745 char *filebuf = XALLOCAVEC (char, maxfilelen);
9747 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9748 snprintf (filebuf, maxfilelen, "%s;%d",
9749 files[file_idx].path + dirs[dir_idx].length, ver);
9751 dw2_asm_output_nstring
9752 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9754 /* Include directory index. */
9755 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9757 /* Modification time. */
9758 dw2_asm_output_data_uleb128
9759 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9760 ? cdt : 0,
9761 NULL);
9763 /* File length in bytes. */
9764 dw2_asm_output_data_uleb128
9765 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9766 ? siz : 0,
9767 NULL);
9768 #else
9769 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9770 "File Entry: %#x", (unsigned) i + 1);
9772 /* Include directory index. */
9773 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9775 /* Modification time. */
9776 dw2_asm_output_data_uleb128 (0, NULL);
9778 /* File length in bytes. */
9779 dw2_asm_output_data_uleb128 (0, NULL);
9780 #endif /* VMS_DEBUGGING_INFO */
9783 dw2_asm_output_data (1, 0, "End file name table");
9787 /* Output one line number table into the .debug_line section. */
9789 static void
9790 output_one_line_info_table (dw_line_info_table *table)
9792 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9793 unsigned int current_line = 1;
9794 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9795 dw_line_info_entry *ent;
9796 size_t i;
9798 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9800 switch (ent->opcode)
9802 case LI_set_address:
9803 /* ??? Unfortunately, we have little choice here currently, and
9804 must always use the most general form. GCC does not know the
9805 address delta itself, so we can't use DW_LNS_advance_pc. Many
9806 ports do have length attributes which will give an upper bound
9807 on the address range. We could perhaps use length attributes
9808 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9809 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9811 /* This can handle any delta. This takes
9812 4+DWARF2_ADDR_SIZE bytes. */
9813 dw2_asm_output_data (1, 0, "set address %s", line_label);
9814 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9815 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9816 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9817 break;
9819 case LI_set_line:
9820 if (ent->val == current_line)
9822 /* We still need to start a new row, so output a copy insn. */
9823 dw2_asm_output_data (1, DW_LNS_copy,
9824 "copy line %u", current_line);
9826 else
9828 int line_offset = ent->val - current_line;
9829 int line_delta = line_offset - DWARF_LINE_BASE;
9831 current_line = ent->val;
9832 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9834 /* This can handle deltas from -10 to 234, using the current
9835 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9836 This takes 1 byte. */
9837 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9838 "line %u", current_line);
9840 else
9842 /* This can handle any delta. This takes at least 4 bytes,
9843 depending on the value being encoded. */
9844 dw2_asm_output_data (1, DW_LNS_advance_line,
9845 "advance to line %u", current_line);
9846 dw2_asm_output_data_sleb128 (line_offset, NULL);
9847 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9850 break;
9852 case LI_set_file:
9853 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9854 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9855 break;
9857 case LI_set_column:
9858 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9859 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9860 break;
9862 case LI_negate_stmt:
9863 current_is_stmt = !current_is_stmt;
9864 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9865 "is_stmt %d", current_is_stmt);
9866 break;
9868 case LI_set_prologue_end:
9869 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9870 "set prologue end");
9871 break;
9873 case LI_set_epilogue_begin:
9874 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9875 "set epilogue begin");
9876 break;
9878 case LI_set_discriminator:
9879 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9880 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9881 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9882 dw2_asm_output_data_uleb128 (ent->val, NULL);
9883 break;
9887 /* Emit debug info for the address of the end of the table. */
9888 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9889 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9890 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9891 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9893 dw2_asm_output_data (1, 0, "end sequence");
9894 dw2_asm_output_data_uleb128 (1, NULL);
9895 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9898 /* Output the source line number correspondence information. This
9899 information goes into the .debug_line section. */
9901 static void
9902 output_line_info (bool prologue_only)
9904 char l1[20], l2[20], p1[20], p2[20];
9905 int ver = dwarf_version;
9906 bool saw_one = false;
9907 int opc;
9909 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9910 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9911 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9912 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9914 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9915 dw2_asm_output_data (4, 0xffffffff,
9916 "Initial length escape value indicating 64-bit DWARF extension");
9917 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9918 "Length of Source Line Info");
9919 ASM_OUTPUT_LABEL (asm_out_file, l1);
9921 dw2_asm_output_data (2, ver, "DWARF Version");
9922 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9923 ASM_OUTPUT_LABEL (asm_out_file, p1);
9925 /* Define the architecture-dependent minimum instruction length (in bytes).
9926 In this implementation of DWARF, this field is used for information
9927 purposes only. Since GCC generates assembly language, we have no
9928 a priori knowledge of how many instruction bytes are generated for each
9929 source line, and therefore can use only the DW_LNE_set_address and
9930 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9931 this as '1', which is "correct enough" for all architectures,
9932 and don't let the target override. */
9933 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9935 if (ver >= 4)
9936 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9937 "Maximum Operations Per Instruction");
9938 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9939 "Default is_stmt_start flag");
9940 dw2_asm_output_data (1, DWARF_LINE_BASE,
9941 "Line Base Value (Special Opcodes)");
9942 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9943 "Line Range Value (Special Opcodes)");
9944 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9945 "Special Opcode Base");
9947 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9949 int n_op_args;
9950 switch (opc)
9952 case DW_LNS_advance_pc:
9953 case DW_LNS_advance_line:
9954 case DW_LNS_set_file:
9955 case DW_LNS_set_column:
9956 case DW_LNS_fixed_advance_pc:
9957 case DW_LNS_set_isa:
9958 n_op_args = 1;
9959 break;
9960 default:
9961 n_op_args = 0;
9962 break;
9965 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9966 opc, n_op_args);
9969 /* Write out the information about the files we use. */
9970 output_file_names ();
9971 ASM_OUTPUT_LABEL (asm_out_file, p2);
9972 if (prologue_only)
9974 /* Output the marker for the end of the line number info. */
9975 ASM_OUTPUT_LABEL (asm_out_file, l2);
9976 return;
9979 if (separate_line_info)
9981 dw_line_info_table *table;
9982 size_t i;
9984 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9985 if (table->in_use)
9987 output_one_line_info_table (table);
9988 saw_one = true;
9991 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9993 output_one_line_info_table (cold_text_section_line_info);
9994 saw_one = true;
9997 /* ??? Some Darwin linkers crash on a .debug_line section with no
9998 sequences. Further, merely a DW_LNE_end_sequence entry is not
9999 sufficient -- the address column must also be initialized.
10000 Make sure to output at least one set_address/end_sequence pair,
10001 choosing .text since that section is always present. */
10002 if (text_section_line_info->in_use || !saw_one)
10003 output_one_line_info_table (text_section_line_info);
10005 /* Output the marker for the end of the line number info. */
10006 ASM_OUTPUT_LABEL (asm_out_file, l2);
10009 /* Given a pointer to a tree node for some base type, return a pointer to
10010 a DIE that describes the given type.
10012 This routine must only be called for GCC type nodes that correspond to
10013 Dwarf base (fundamental) types. */
10015 static dw_die_ref
10016 base_type_die (tree type)
10018 dw_die_ref base_type_result;
10019 enum dwarf_type encoding;
10021 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10022 return 0;
10024 /* If this is a subtype that should not be emitted as a subrange type,
10025 use the base type. See subrange_type_for_debug_p. */
10026 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10027 type = TREE_TYPE (type);
10029 switch (TREE_CODE (type))
10031 case INTEGER_TYPE:
10032 if ((dwarf_version >= 4 || !dwarf_strict)
10033 && TYPE_NAME (type)
10034 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10035 && DECL_IS_BUILTIN (TYPE_NAME (type))
10036 && DECL_NAME (TYPE_NAME (type)))
10038 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10039 if (strcmp (name, "char16_t") == 0
10040 || strcmp (name, "char32_t") == 0)
10042 encoding = DW_ATE_UTF;
10043 break;
10046 if (TYPE_STRING_FLAG (type))
10048 if (TYPE_UNSIGNED (type))
10049 encoding = DW_ATE_unsigned_char;
10050 else
10051 encoding = DW_ATE_signed_char;
10053 else if (TYPE_UNSIGNED (type))
10054 encoding = DW_ATE_unsigned;
10055 else
10056 encoding = DW_ATE_signed;
10057 break;
10059 case REAL_TYPE:
10060 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10062 if (dwarf_version >= 3 || !dwarf_strict)
10063 encoding = DW_ATE_decimal_float;
10064 else
10065 encoding = DW_ATE_lo_user;
10067 else
10068 encoding = DW_ATE_float;
10069 break;
10071 case FIXED_POINT_TYPE:
10072 if (!(dwarf_version >= 3 || !dwarf_strict))
10073 encoding = DW_ATE_lo_user;
10074 else if (TYPE_UNSIGNED (type))
10075 encoding = DW_ATE_unsigned_fixed;
10076 else
10077 encoding = DW_ATE_signed_fixed;
10078 break;
10080 /* Dwarf2 doesn't know anything about complex ints, so use
10081 a user defined type for it. */
10082 case COMPLEX_TYPE:
10083 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10084 encoding = DW_ATE_complex_float;
10085 else
10086 encoding = DW_ATE_lo_user;
10087 break;
10089 case BOOLEAN_TYPE:
10090 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10091 encoding = DW_ATE_boolean;
10092 break;
10094 default:
10095 /* No other TREE_CODEs are Dwarf fundamental types. */
10096 gcc_unreachable ();
10099 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10101 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10102 int_size_in_bytes (type));
10103 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10104 add_pubtype (type, base_type_result);
10106 return base_type_result;
10109 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10110 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10112 static inline int
10113 is_base_type (tree type)
10115 switch (TREE_CODE (type))
10117 case ERROR_MARK:
10118 case VOID_TYPE:
10119 case INTEGER_TYPE:
10120 case REAL_TYPE:
10121 case FIXED_POINT_TYPE:
10122 case COMPLEX_TYPE:
10123 case BOOLEAN_TYPE:
10124 return 1;
10126 case ARRAY_TYPE:
10127 case RECORD_TYPE:
10128 case UNION_TYPE:
10129 case QUAL_UNION_TYPE:
10130 case ENUMERAL_TYPE:
10131 case FUNCTION_TYPE:
10132 case METHOD_TYPE:
10133 case POINTER_TYPE:
10134 case REFERENCE_TYPE:
10135 case NULLPTR_TYPE:
10136 case OFFSET_TYPE:
10137 case LANG_TYPE:
10138 case VECTOR_TYPE:
10139 return 0;
10141 default:
10142 gcc_unreachable ();
10145 return 0;
10148 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10149 node, return the size in bits for the type if it is a constant, or else
10150 return the alignment for the type if the type's size is not constant, or
10151 else return BITS_PER_WORD if the type actually turns out to be an
10152 ERROR_MARK node. */
10154 static inline unsigned HOST_WIDE_INT
10155 simple_type_size_in_bits (const_tree type)
10157 if (TREE_CODE (type) == ERROR_MARK)
10158 return BITS_PER_WORD;
10159 else if (TYPE_SIZE (type) == NULL_TREE)
10160 return 0;
10161 else if (host_integerp (TYPE_SIZE (type), 1))
10162 return tree_low_cst (TYPE_SIZE (type), 1);
10163 else
10164 return TYPE_ALIGN (type);
10167 /* Similarly, but return a double_int instead of UHWI. */
10169 static inline double_int
10170 double_int_type_size_in_bits (const_tree type)
10172 if (TREE_CODE (type) == ERROR_MARK)
10173 return double_int::from_uhwi (BITS_PER_WORD);
10174 else if (TYPE_SIZE (type) == NULL_TREE)
10175 return double_int_zero;
10176 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10177 return tree_to_double_int (TYPE_SIZE (type));
10178 else
10179 return double_int::from_uhwi (TYPE_ALIGN (type));
10182 /* Given a pointer to a tree node for a subrange type, return a pointer
10183 to a DIE that describes the given type. */
10185 static dw_die_ref
10186 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10188 dw_die_ref subrange_die;
10189 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10191 if (context_die == NULL)
10192 context_die = comp_unit_die ();
10194 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10196 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10198 /* The size of the subrange type and its base type do not match,
10199 so we need to generate a size attribute for the subrange type. */
10200 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10203 if (low)
10204 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10205 if (high)
10206 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10208 return subrange_die;
10211 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10212 entry that chains various modifiers in front of the given type. */
10214 static dw_die_ref
10215 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10216 dw_die_ref context_die)
10218 enum tree_code code = TREE_CODE (type);
10219 dw_die_ref mod_type_die;
10220 dw_die_ref sub_die = NULL;
10221 tree item_type = NULL;
10222 tree qualified_type;
10223 tree name, low, high;
10224 dw_die_ref mod_scope;
10226 if (code == ERROR_MARK)
10227 return NULL;
10229 /* See if we already have the appropriately qualified variant of
10230 this type. */
10231 qualified_type
10232 = get_qualified_type (type,
10233 ((is_const_type ? TYPE_QUAL_CONST : 0)
10234 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10236 if (qualified_type == sizetype
10237 && TYPE_NAME (qualified_type)
10238 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10240 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10242 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10243 && TYPE_PRECISION (t)
10244 == TYPE_PRECISION (qualified_type)
10245 && TYPE_UNSIGNED (t)
10246 == TYPE_UNSIGNED (qualified_type));
10247 qualified_type = t;
10250 /* If we do, then we can just use its DIE, if it exists. */
10251 if (qualified_type)
10253 mod_type_die = lookup_type_die (qualified_type);
10254 if (mod_type_die)
10255 return mod_type_die;
10258 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10260 /* Handle C typedef types. */
10261 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10262 && !DECL_ARTIFICIAL (name))
10264 tree dtype = TREE_TYPE (name);
10266 if (qualified_type == dtype)
10268 /* For a named type, use the typedef. */
10269 gen_type_die (qualified_type, context_die);
10270 return lookup_type_die (qualified_type);
10272 else if (is_const_type < TYPE_READONLY (dtype)
10273 || is_volatile_type < TYPE_VOLATILE (dtype)
10274 || (is_const_type <= TYPE_READONLY (dtype)
10275 && is_volatile_type <= TYPE_VOLATILE (dtype)
10276 && DECL_ORIGINAL_TYPE (name) != type))
10277 /* cv-unqualified version of named type. Just use the unnamed
10278 type to which it refers. */
10279 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10280 is_const_type, is_volatile_type,
10281 context_die);
10282 /* Else cv-qualified version of named type; fall through. */
10285 mod_scope = scope_die_for (type, context_die);
10287 if (is_const_type
10288 /* If both is_const_type and is_volatile_type, prefer the path
10289 which leads to a qualified type. */
10290 && (!is_volatile_type
10291 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10292 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10294 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10295 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10297 else if (is_volatile_type)
10299 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10300 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10302 else if (code == POINTER_TYPE)
10304 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10305 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10306 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10307 item_type = TREE_TYPE (type);
10308 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10309 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10310 TYPE_ADDR_SPACE (item_type));
10312 else if (code == REFERENCE_TYPE)
10314 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10315 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10316 type);
10317 else
10318 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10319 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10320 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10321 item_type = TREE_TYPE (type);
10322 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10323 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10324 TYPE_ADDR_SPACE (item_type));
10326 else if (code == INTEGER_TYPE
10327 && TREE_TYPE (type) != NULL_TREE
10328 && subrange_type_for_debug_p (type, &low, &high))
10330 mod_type_die = subrange_type_die (type, low, high, context_die);
10331 item_type = TREE_TYPE (type);
10333 else if (is_base_type (type))
10334 mod_type_die = base_type_die (type);
10335 else
10337 gen_type_die (type, context_die);
10339 /* We have to get the type_main_variant here (and pass that to the
10340 `lookup_type_die' routine) because the ..._TYPE node we have
10341 might simply be a *copy* of some original type node (where the
10342 copy was created to help us keep track of typedef names) and
10343 that copy might have a different TYPE_UID from the original
10344 ..._TYPE node. */
10345 if (TREE_CODE (type) != VECTOR_TYPE)
10346 return lookup_type_die (type_main_variant (type));
10347 else
10348 /* Vectors have the debugging information in the type,
10349 not the main variant. */
10350 return lookup_type_die (type);
10353 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10354 don't output a DW_TAG_typedef, since there isn't one in the
10355 user's program; just attach a DW_AT_name to the type.
10356 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10357 if the base type already has the same name. */
10358 if (name
10359 && ((TREE_CODE (name) != TYPE_DECL
10360 && (qualified_type == TYPE_MAIN_VARIANT (type)
10361 || (!is_const_type && !is_volatile_type)))
10362 || (TREE_CODE (name) == TYPE_DECL
10363 && TREE_TYPE (name) == qualified_type
10364 && DECL_NAME (name))))
10366 if (TREE_CODE (name) == TYPE_DECL)
10367 /* Could just call add_name_and_src_coords_attributes here,
10368 but since this is a builtin type it doesn't have any
10369 useful source coordinates anyway. */
10370 name = DECL_NAME (name);
10371 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10373 /* This probably indicates a bug. */
10374 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10376 name = TYPE_NAME (type);
10377 if (name
10378 && TREE_CODE (name) == TYPE_DECL)
10379 name = DECL_NAME (name);
10380 add_name_attribute (mod_type_die,
10381 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10384 if (qualified_type)
10385 equate_type_number_to_die (qualified_type, mod_type_die);
10387 if (item_type)
10388 /* We must do this after the equate_type_number_to_die call, in case
10389 this is a recursive type. This ensures that the modified_type_die
10390 recursion will terminate even if the type is recursive. Recursive
10391 types are possible in Ada. */
10392 sub_die = modified_type_die (item_type,
10393 TYPE_READONLY (item_type),
10394 TYPE_VOLATILE (item_type),
10395 context_die);
10397 if (sub_die != NULL)
10398 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10400 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10401 if (TYPE_ARTIFICIAL (type))
10402 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10404 return mod_type_die;
10407 /* Generate DIEs for the generic parameters of T.
10408 T must be either a generic type or a generic function.
10409 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10411 static void
10412 gen_generic_params_dies (tree t)
10414 tree parms, args;
10415 int parms_num, i;
10416 dw_die_ref die = NULL;
10417 int non_default;
10419 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10420 return;
10422 if (TYPE_P (t))
10423 die = lookup_type_die (t);
10424 else if (DECL_P (t))
10425 die = lookup_decl_die (t);
10427 gcc_assert (die);
10429 parms = lang_hooks.get_innermost_generic_parms (t);
10430 if (!parms)
10431 /* T has no generic parameter. It means T is neither a generic type
10432 or function. End of story. */
10433 return;
10435 parms_num = TREE_VEC_LENGTH (parms);
10436 args = lang_hooks.get_innermost_generic_args (t);
10437 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10438 non_default = int_cst_value (TREE_CHAIN (args));
10439 else
10440 non_default = TREE_VEC_LENGTH (args);
10441 for (i = 0; i < parms_num; i++)
10443 tree parm, arg, arg_pack_elems;
10444 dw_die_ref parm_die;
10446 parm = TREE_VEC_ELT (parms, i);
10447 arg = TREE_VEC_ELT (args, i);
10448 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10449 gcc_assert (parm && TREE_VALUE (parm) && arg);
10451 if (parm && TREE_VALUE (parm) && arg)
10453 /* If PARM represents a template parameter pack,
10454 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10455 by DW_TAG_template_*_parameter DIEs for the argument
10456 pack elements of ARG. Note that ARG would then be
10457 an argument pack. */
10458 if (arg_pack_elems)
10459 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10460 arg_pack_elems,
10461 die);
10462 else
10463 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10464 true /* emit name */, die);
10465 if (i >= non_default)
10466 add_AT_flag (parm_die, DW_AT_default_value, 1);
10471 /* Create and return a DIE for PARM which should be
10472 the representation of a generic type parameter.
10473 For instance, in the C++ front end, PARM would be a template parameter.
10474 ARG is the argument to PARM.
10475 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10476 name of the PARM.
10477 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10478 as a child node. */
10480 static dw_die_ref
10481 generic_parameter_die (tree parm, tree arg,
10482 bool emit_name_p,
10483 dw_die_ref parent_die)
10485 dw_die_ref tmpl_die = NULL;
10486 const char *name = NULL;
10488 if (!parm || !DECL_NAME (parm) || !arg)
10489 return NULL;
10491 /* We support non-type generic parameters and arguments,
10492 type generic parameters and arguments, as well as
10493 generic generic parameters (a.k.a. template template parameters in C++)
10494 and arguments. */
10495 if (TREE_CODE (parm) == PARM_DECL)
10496 /* PARM is a nontype generic parameter */
10497 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10498 else if (TREE_CODE (parm) == TYPE_DECL)
10499 /* PARM is a type generic parameter. */
10500 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10501 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10502 /* PARM is a generic generic parameter.
10503 Its DIE is a GNU extension. It shall have a
10504 DW_AT_name attribute to represent the name of the template template
10505 parameter, and a DW_AT_GNU_template_name attribute to represent the
10506 name of the template template argument. */
10507 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10508 parent_die, parm);
10509 else
10510 gcc_unreachable ();
10512 if (tmpl_die)
10514 tree tmpl_type;
10516 /* If PARM is a generic parameter pack, it means we are
10517 emitting debug info for a template argument pack element.
10518 In other terms, ARG is a template argument pack element.
10519 In that case, we don't emit any DW_AT_name attribute for
10520 the die. */
10521 if (emit_name_p)
10523 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10524 gcc_assert (name);
10525 add_AT_string (tmpl_die, DW_AT_name, name);
10528 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10530 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10531 TMPL_DIE should have a child DW_AT_type attribute that is set
10532 to the type of the argument to PARM, which is ARG.
10533 If PARM is a type generic parameter, TMPL_DIE should have a
10534 child DW_AT_type that is set to ARG. */
10535 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10536 add_type_attribute (tmpl_die, tmpl_type, 0,
10537 TREE_THIS_VOLATILE (tmpl_type),
10538 parent_die);
10540 else
10542 /* So TMPL_DIE is a DIE representing a
10543 a generic generic template parameter, a.k.a template template
10544 parameter in C++ and arg is a template. */
10546 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10547 to the name of the argument. */
10548 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10549 if (name)
10550 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10553 if (TREE_CODE (parm) == PARM_DECL)
10554 /* So PARM is a non-type generic parameter.
10555 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10556 attribute of TMPL_DIE which value represents the value
10557 of ARG.
10558 We must be careful here:
10559 The value of ARG might reference some function decls.
10560 We might currently be emitting debug info for a generic
10561 type and types are emitted before function decls, we don't
10562 know if the function decls referenced by ARG will actually be
10563 emitted after cgraph computations.
10564 So must defer the generation of the DW_AT_const_value to
10565 after cgraph is ready. */
10566 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10569 return tmpl_die;
10572 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10573 PARM_PACK must be a template parameter pack. The returned DIE
10574 will be child DIE of PARENT_DIE. */
10576 static dw_die_ref
10577 template_parameter_pack_die (tree parm_pack,
10578 tree parm_pack_args,
10579 dw_die_ref parent_die)
10581 dw_die_ref die;
10582 int j;
10584 gcc_assert (parent_die && parm_pack);
10586 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10587 add_name_and_src_coords_attributes (die, parm_pack);
10588 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10589 generic_parameter_die (parm_pack,
10590 TREE_VEC_ELT (parm_pack_args, j),
10591 false /* Don't emit DW_AT_name */,
10592 die);
10593 return die;
10596 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10597 an enumerated type. */
10599 static inline int
10600 type_is_enum (const_tree type)
10602 return TREE_CODE (type) == ENUMERAL_TYPE;
10605 /* Return the DBX register number described by a given RTL node. */
10607 static unsigned int
10608 dbx_reg_number (const_rtx rtl)
10610 unsigned regno = REGNO (rtl);
10612 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10614 #ifdef LEAF_REG_REMAP
10615 if (crtl->uses_only_leaf_regs)
10617 int leaf_reg = LEAF_REG_REMAP (regno);
10618 if (leaf_reg != -1)
10619 regno = (unsigned) leaf_reg;
10621 #endif
10623 regno = DBX_REGISTER_NUMBER (regno);
10624 gcc_assert (regno != INVALID_REGNUM);
10625 return regno;
10628 /* Optionally add a DW_OP_piece term to a location description expression.
10629 DW_OP_piece is only added if the location description expression already
10630 doesn't end with DW_OP_piece. */
10632 static void
10633 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10635 dw_loc_descr_ref loc;
10637 if (*list_head != NULL)
10639 /* Find the end of the chain. */
10640 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10643 if (loc->dw_loc_opc != DW_OP_piece)
10644 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10648 /* Return a location descriptor that designates a machine register or
10649 zero if there is none. */
10651 static dw_loc_descr_ref
10652 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10654 rtx regs;
10656 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10657 return 0;
10659 /* We only use "frame base" when we're sure we're talking about the
10660 post-prologue local stack frame. We do this by *not* running
10661 register elimination until this point, and recognizing the special
10662 argument pointer and soft frame pointer rtx's.
10663 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10664 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10665 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10667 dw_loc_descr_ref result = NULL;
10669 if (dwarf_version >= 4 || !dwarf_strict)
10671 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10672 initialized);
10673 if (result)
10674 add_loc_descr (&result,
10675 new_loc_descr (DW_OP_stack_value, 0, 0));
10677 return result;
10680 regs = targetm.dwarf_register_span (rtl);
10682 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10683 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10684 else
10686 unsigned int dbx_regnum = dbx_reg_number (rtl);
10687 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10688 return 0;
10689 return one_reg_loc_descriptor (dbx_regnum, initialized);
10693 /* Return a location descriptor that designates a machine register for
10694 a given hard register number. */
10696 static dw_loc_descr_ref
10697 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10699 dw_loc_descr_ref reg_loc_descr;
10701 if (regno <= 31)
10702 reg_loc_descr
10703 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10704 else
10705 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10707 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10708 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10710 return reg_loc_descr;
10713 /* Given an RTL of a register, return a location descriptor that
10714 designates a value that spans more than one register. */
10716 static dw_loc_descr_ref
10717 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10718 enum var_init_status initialized)
10720 int size, i;
10721 dw_loc_descr_ref loc_result = NULL;
10723 /* Simple, contiguous registers. */
10724 if (regs == NULL_RTX)
10726 unsigned reg = REGNO (rtl);
10727 int nregs;
10729 #ifdef LEAF_REG_REMAP
10730 if (crtl->uses_only_leaf_regs)
10732 int leaf_reg = LEAF_REG_REMAP (reg);
10733 if (leaf_reg != -1)
10734 reg = (unsigned) leaf_reg;
10736 #endif
10738 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10739 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10741 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10743 loc_result = NULL;
10744 while (nregs--)
10746 dw_loc_descr_ref t;
10748 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10749 VAR_INIT_STATUS_INITIALIZED);
10750 add_loc_descr (&loc_result, t);
10751 add_loc_descr_op_piece (&loc_result, size);
10752 ++reg;
10754 return loc_result;
10757 /* Now onto stupid register sets in non contiguous locations. */
10759 gcc_assert (GET_CODE (regs) == PARALLEL);
10761 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10762 loc_result = NULL;
10764 for (i = 0; i < XVECLEN (regs, 0); ++i)
10766 dw_loc_descr_ref t;
10768 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10769 VAR_INIT_STATUS_INITIALIZED);
10770 add_loc_descr (&loc_result, t);
10771 add_loc_descr_op_piece (&loc_result, size);
10774 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10775 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10776 return loc_result;
10779 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10781 /* Return a location descriptor that designates a constant i,
10782 as a compound operation from constant (i >> shift), constant shift
10783 and DW_OP_shl. */
10785 static dw_loc_descr_ref
10786 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10788 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10789 add_loc_descr (&ret, int_loc_descriptor (shift));
10790 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10791 return ret;
10794 /* Return a location descriptor that designates a constant. */
10796 static dw_loc_descr_ref
10797 int_loc_descriptor (HOST_WIDE_INT i)
10799 enum dwarf_location_atom op;
10801 /* Pick the smallest representation of a constant, rather than just
10802 defaulting to the LEB encoding. */
10803 if (i >= 0)
10805 int clz = clz_hwi (i);
10806 int ctz = ctz_hwi (i);
10807 if (i <= 31)
10808 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10809 else if (i <= 0xff)
10810 op = DW_OP_const1u;
10811 else if (i <= 0xffff)
10812 op = DW_OP_const2u;
10813 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10814 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10815 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10816 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10817 while DW_OP_const4u is 5 bytes. */
10818 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10819 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10820 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10821 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10822 while DW_OP_const4u is 5 bytes. */
10823 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10824 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10825 op = DW_OP_const4u;
10826 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10827 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10828 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10829 while DW_OP_constu of constant >= 0x100000000 takes at least
10830 6 bytes. */
10831 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10832 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10833 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10834 >= HOST_BITS_PER_WIDE_INT)
10835 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10836 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10837 while DW_OP_constu takes in this case at least 6 bytes. */
10838 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10839 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10840 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10841 && size_of_uleb128 (i) > 6)
10842 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10843 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10844 else
10845 op = DW_OP_constu;
10847 else
10849 if (i >= -0x80)
10850 op = DW_OP_const1s;
10851 else if (i >= -0x8000)
10852 op = DW_OP_const2s;
10853 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10855 if (size_of_int_loc_descriptor (i) < 5)
10857 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10858 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10859 return ret;
10861 op = DW_OP_const4s;
10863 else
10865 if (size_of_int_loc_descriptor (i)
10866 < (unsigned long) 1 + size_of_sleb128 (i))
10868 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10869 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10870 return ret;
10872 op = DW_OP_consts;
10876 return new_loc_descr (op, i, 0);
10879 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10880 without actually allocating it. */
10882 static unsigned long
10883 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10885 return size_of_int_loc_descriptor (i >> shift)
10886 + size_of_int_loc_descriptor (shift)
10887 + 1;
10890 /* Return size_of_locs (int_loc_descriptor (i)) without
10891 actually allocating it. */
10893 static unsigned long
10894 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10896 unsigned long s;
10898 if (i >= 0)
10900 int clz, ctz;
10901 if (i <= 31)
10902 return 1;
10903 else if (i <= 0xff)
10904 return 2;
10905 else if (i <= 0xffff)
10906 return 3;
10907 clz = clz_hwi (i);
10908 ctz = ctz_hwi (i);
10909 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10910 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10911 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10912 - clz - 5);
10913 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10914 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10915 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10916 - clz - 8);
10917 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10918 return 5;
10919 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10920 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10921 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10922 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10923 - clz - 8);
10924 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10925 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10926 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10927 - clz - 16);
10928 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10929 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10930 && s > 6)
10931 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10932 - clz - 32);
10933 else
10934 return 1 + s;
10936 else
10938 if (i >= -0x80)
10939 return 2;
10940 else if (i >= -0x8000)
10941 return 3;
10942 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10944 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10946 s = size_of_int_loc_descriptor (-i) + 1;
10947 if (s < 5)
10948 return s;
10950 return 5;
10952 else
10954 unsigned long r = 1 + size_of_sleb128 (i);
10955 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10957 s = size_of_int_loc_descriptor (-i) + 1;
10958 if (s < r)
10959 return s;
10961 return r;
10966 /* Return loc description representing "address" of integer value.
10967 This can appear only as toplevel expression. */
10969 static dw_loc_descr_ref
10970 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10972 int litsize;
10973 dw_loc_descr_ref loc_result = NULL;
10975 if (!(dwarf_version >= 4 || !dwarf_strict))
10976 return NULL;
10978 litsize = size_of_int_loc_descriptor (i);
10979 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10980 is more compact. For DW_OP_stack_value we need:
10981 litsize + 1 (DW_OP_stack_value)
10982 and for DW_OP_implicit_value:
10983 1 (DW_OP_implicit_value) + 1 (length) + size. */
10984 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10986 loc_result = int_loc_descriptor (i);
10987 add_loc_descr (&loc_result,
10988 new_loc_descr (DW_OP_stack_value, 0, 0));
10989 return loc_result;
10992 loc_result = new_loc_descr (DW_OP_implicit_value,
10993 size, 0);
10994 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10995 loc_result->dw_loc_oprnd2.v.val_int = i;
10996 return loc_result;
10999 /* Return a location descriptor that designates a base+offset location. */
11001 static dw_loc_descr_ref
11002 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11003 enum var_init_status initialized)
11005 unsigned int regno;
11006 dw_loc_descr_ref result;
11007 dw_fde_ref fde = cfun->fde;
11009 /* We only use "frame base" when we're sure we're talking about the
11010 post-prologue local stack frame. We do this by *not* running
11011 register elimination until this point, and recognizing the special
11012 argument pointer and soft frame pointer rtx's. */
11013 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11015 rtx elim = (ira_use_lra_p
11016 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11017 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11019 if (elim != reg)
11021 if (GET_CODE (elim) == PLUS)
11023 offset += INTVAL (XEXP (elim, 1));
11024 elim = XEXP (elim, 0);
11026 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11027 && (elim == hard_frame_pointer_rtx
11028 || elim == stack_pointer_rtx))
11029 || elim == (frame_pointer_needed
11030 ? hard_frame_pointer_rtx
11031 : stack_pointer_rtx));
11033 /* If drap register is used to align stack, use frame
11034 pointer + offset to access stack variables. If stack
11035 is aligned without drap, use stack pointer + offset to
11036 access stack variables. */
11037 if (crtl->stack_realign_tried
11038 && reg == frame_pointer_rtx)
11040 int base_reg
11041 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11042 ? HARD_FRAME_POINTER_REGNUM
11043 : REGNO (elim));
11044 return new_reg_loc_descr (base_reg, offset);
11047 gcc_assert (frame_pointer_fb_offset_valid);
11048 offset += frame_pointer_fb_offset;
11049 return new_loc_descr (DW_OP_fbreg, offset, 0);
11053 regno = REGNO (reg);
11054 #ifdef LEAF_REG_REMAP
11055 if (crtl->uses_only_leaf_regs)
11057 int leaf_reg = LEAF_REG_REMAP (regno);
11058 if (leaf_reg != -1)
11059 regno = (unsigned) leaf_reg;
11061 #endif
11062 regno = DWARF_FRAME_REGNUM (regno);
11064 if (!optimize && fde
11065 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11067 /* Use cfa+offset to represent the location of arguments passed
11068 on the stack when drap is used to align stack.
11069 Only do this when not optimizing, for optimized code var-tracking
11070 is supposed to track where the arguments live and the register
11071 used as vdrap or drap in some spot might be used for something
11072 else in other part of the routine. */
11073 return new_loc_descr (DW_OP_fbreg, offset, 0);
11076 if (regno <= 31)
11077 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11078 offset, 0);
11079 else
11080 result = new_loc_descr (DW_OP_bregx, regno, offset);
11082 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11083 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11085 return result;
11088 /* Return true if this RTL expression describes a base+offset calculation. */
11090 static inline int
11091 is_based_loc (const_rtx rtl)
11093 return (GET_CODE (rtl) == PLUS
11094 && ((REG_P (XEXP (rtl, 0))
11095 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11096 && CONST_INT_P (XEXP (rtl, 1)))));
11099 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11100 failed. */
11102 static dw_loc_descr_ref
11103 tls_mem_loc_descriptor (rtx mem)
11105 tree base;
11106 dw_loc_descr_ref loc_result;
11108 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11109 return NULL;
11111 base = get_base_address (MEM_EXPR (mem));
11112 if (base == NULL
11113 || TREE_CODE (base) != VAR_DECL
11114 || !DECL_THREAD_LOCAL_P (base))
11115 return NULL;
11117 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11118 if (loc_result == NULL)
11119 return NULL;
11121 if (MEM_OFFSET (mem))
11122 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11124 return loc_result;
11127 /* Output debug info about reason why we failed to expand expression as dwarf
11128 expression. */
11130 static void
11131 expansion_failed (tree expr, rtx rtl, char const *reason)
11133 if (dump_file && (dump_flags & TDF_DETAILS))
11135 fprintf (dump_file, "Failed to expand as dwarf: ");
11136 if (expr)
11137 print_generic_expr (dump_file, expr, dump_flags);
11138 if (rtl)
11140 fprintf (dump_file, "\n");
11141 print_rtl (dump_file, rtl);
11143 fprintf (dump_file, "\nReason: %s\n", reason);
11147 /* Helper function for const_ok_for_output, called either directly
11148 or via for_each_rtx. */
11150 static int
11151 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11153 rtx rtl = *rtlp;
11155 if (GET_CODE (rtl) == UNSPEC)
11157 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11158 we can't express it in the debug info. */
11159 #ifdef ENABLE_CHECKING
11160 /* Don't complain about TLS UNSPECs, those are just too hard to
11161 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11162 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11163 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11164 if (XVECLEN (rtl, 0) == 0
11165 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11166 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11167 inform (current_function_decl
11168 ? DECL_SOURCE_LOCATION (current_function_decl)
11169 : UNKNOWN_LOCATION,
11170 #if NUM_UNSPEC_VALUES > 0
11171 "non-delegitimized UNSPEC %s (%d) found in variable location",
11172 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11173 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11174 XINT (rtl, 1));
11175 #else
11176 "non-delegitimized UNSPEC %d found in variable location",
11177 XINT (rtl, 1));
11178 #endif
11179 #endif
11180 expansion_failed (NULL_TREE, rtl,
11181 "UNSPEC hasn't been delegitimized.\n");
11182 return 1;
11185 if (targetm.const_not_ok_for_debug_p (rtl))
11187 expansion_failed (NULL_TREE, rtl,
11188 "Expression rejected for debug by the backend.\n");
11189 return 1;
11192 if (GET_CODE (rtl) != SYMBOL_REF)
11193 return 0;
11195 if (CONSTANT_POOL_ADDRESS_P (rtl))
11197 bool marked;
11198 get_pool_constant_mark (rtl, &marked);
11199 /* If all references to this pool constant were optimized away,
11200 it was not output and thus we can't represent it. */
11201 if (!marked)
11203 expansion_failed (NULL_TREE, rtl,
11204 "Constant was removed from constant pool.\n");
11205 return 1;
11209 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11210 return 1;
11212 /* Avoid references to external symbols in debug info, on several targets
11213 the linker might even refuse to link when linking a shared library,
11214 and in many other cases the relocations for .debug_info/.debug_loc are
11215 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11216 to be defined within the same shared library or executable are fine. */
11217 if (SYMBOL_REF_EXTERNAL_P (rtl))
11219 tree decl = SYMBOL_REF_DECL (rtl);
11221 if (decl == NULL || !targetm.binds_local_p (decl))
11223 expansion_failed (NULL_TREE, rtl,
11224 "Symbol not defined in current TU.\n");
11225 return 1;
11229 return 0;
11232 /* Return true if constant RTL can be emitted in DW_OP_addr or
11233 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11234 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11236 static bool
11237 const_ok_for_output (rtx rtl)
11239 if (GET_CODE (rtl) == SYMBOL_REF)
11240 return const_ok_for_output_1 (&rtl, NULL) == 0;
11242 if (GET_CODE (rtl) == CONST)
11243 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11245 return true;
11248 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11249 if possible, NULL otherwise. */
11251 static dw_die_ref
11252 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11254 dw_die_ref type_die;
11255 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11257 if (type == NULL)
11258 return NULL;
11259 switch (TREE_CODE (type))
11261 case INTEGER_TYPE:
11262 case REAL_TYPE:
11263 break;
11264 default:
11265 return NULL;
11267 type_die = lookup_type_die (type);
11268 if (!type_die)
11269 type_die = modified_type_die (type, false, false, comp_unit_die ());
11270 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11271 return NULL;
11272 return type_die;
11275 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11276 type matching MODE, or, if MODE is narrower than or as wide as
11277 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11278 possible. */
11280 static dw_loc_descr_ref
11281 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11283 enum machine_mode outer_mode = mode;
11284 dw_die_ref type_die;
11285 dw_loc_descr_ref cvt;
11287 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11289 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11290 return op;
11292 type_die = base_type_for_mode (outer_mode, 1);
11293 if (type_die == NULL)
11294 return NULL;
11295 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11296 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11297 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11298 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11299 add_loc_descr (&op, cvt);
11300 return op;
11303 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11305 static dw_loc_descr_ref
11306 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11307 dw_loc_descr_ref op1)
11309 dw_loc_descr_ref ret = op0;
11310 add_loc_descr (&ret, op1);
11311 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11312 if (STORE_FLAG_VALUE != 1)
11314 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11315 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11317 return ret;
11320 /* Return location descriptor for signed comparison OP RTL. */
11322 static dw_loc_descr_ref
11323 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11324 enum machine_mode mem_mode)
11326 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11327 dw_loc_descr_ref op0, op1;
11328 int shift;
11330 if (op_mode == VOIDmode)
11331 op_mode = GET_MODE (XEXP (rtl, 1));
11332 if (op_mode == VOIDmode)
11333 return NULL;
11335 if (dwarf_strict
11336 && (GET_MODE_CLASS (op_mode) != MODE_INT
11337 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11338 return NULL;
11340 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11341 VAR_INIT_STATUS_INITIALIZED);
11342 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11343 VAR_INIT_STATUS_INITIALIZED);
11345 if (op0 == NULL || op1 == NULL)
11346 return NULL;
11348 if (GET_MODE_CLASS (op_mode) != MODE_INT
11349 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11350 return compare_loc_descriptor (op, op0, op1);
11352 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11354 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11355 dw_loc_descr_ref cvt;
11357 if (type_die == NULL)
11358 return NULL;
11359 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11360 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11361 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11362 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11363 add_loc_descr (&op0, cvt);
11364 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11365 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11366 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11367 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11368 add_loc_descr (&op1, cvt);
11369 return compare_loc_descriptor (op, op0, op1);
11372 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11373 /* For eq/ne, if the operands are known to be zero-extended,
11374 there is no need to do the fancy shifting up. */
11375 if (op == DW_OP_eq || op == DW_OP_ne)
11377 dw_loc_descr_ref last0, last1;
11378 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11380 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11382 /* deref_size zero extends, and for constants we can check
11383 whether they are zero extended or not. */
11384 if (((last0->dw_loc_opc == DW_OP_deref_size
11385 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11386 || (CONST_INT_P (XEXP (rtl, 0))
11387 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11388 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11389 && ((last1->dw_loc_opc == DW_OP_deref_size
11390 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11391 || (CONST_INT_P (XEXP (rtl, 1))
11392 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11393 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11394 return compare_loc_descriptor (op, op0, op1);
11396 /* EQ/NE comparison against constant in narrower type than
11397 DWARF2_ADDR_SIZE can be performed either as
11398 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11399 DW_OP_{eq,ne}
11401 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11402 DW_OP_{eq,ne}. Pick whatever is shorter. */
11403 if (CONST_INT_P (XEXP (rtl, 1))
11404 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11405 && (size_of_int_loc_descriptor (shift) + 1
11406 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11407 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11408 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11409 & GET_MODE_MASK (op_mode))))
11411 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11412 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11413 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11414 & GET_MODE_MASK (op_mode));
11415 return compare_loc_descriptor (op, op0, op1);
11418 add_loc_descr (&op0, int_loc_descriptor (shift));
11419 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11420 if (CONST_INT_P (XEXP (rtl, 1)))
11421 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11422 else
11424 add_loc_descr (&op1, int_loc_descriptor (shift));
11425 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11427 return compare_loc_descriptor (op, op0, op1);
11430 /* Return location descriptor for unsigned comparison OP RTL. */
11432 static dw_loc_descr_ref
11433 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11434 enum machine_mode mem_mode)
11436 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11437 dw_loc_descr_ref op0, op1;
11439 if (op_mode == VOIDmode)
11440 op_mode = GET_MODE (XEXP (rtl, 1));
11441 if (op_mode == VOIDmode)
11442 return NULL;
11443 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11444 return NULL;
11446 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11447 return NULL;
11449 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11450 VAR_INIT_STATUS_INITIALIZED);
11451 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11452 VAR_INIT_STATUS_INITIALIZED);
11454 if (op0 == NULL || op1 == NULL)
11455 return NULL;
11457 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11459 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11460 dw_loc_descr_ref last0, last1;
11461 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11463 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11465 if (CONST_INT_P (XEXP (rtl, 0)))
11466 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11467 /* deref_size zero extends, so no need to mask it again. */
11468 else if (last0->dw_loc_opc != DW_OP_deref_size
11469 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11471 add_loc_descr (&op0, int_loc_descriptor (mask));
11472 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11474 if (CONST_INT_P (XEXP (rtl, 1)))
11475 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11476 /* deref_size zero extends, so no need to mask it again. */
11477 else if (last1->dw_loc_opc != DW_OP_deref_size
11478 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11480 add_loc_descr (&op1, int_loc_descriptor (mask));
11481 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11484 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11486 HOST_WIDE_INT bias = 1;
11487 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11488 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11489 if (CONST_INT_P (XEXP (rtl, 1)))
11490 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11491 + INTVAL (XEXP (rtl, 1)));
11492 else
11493 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11494 bias, 0));
11496 return compare_loc_descriptor (op, op0, op1);
11499 /* Return location descriptor for {U,S}{MIN,MAX}. */
11501 static dw_loc_descr_ref
11502 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11503 enum machine_mode mem_mode)
11505 enum dwarf_location_atom op;
11506 dw_loc_descr_ref op0, op1, ret;
11507 dw_loc_descr_ref bra_node, drop_node;
11509 if (dwarf_strict
11510 && (GET_MODE_CLASS (mode) != MODE_INT
11511 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11512 return NULL;
11514 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11515 VAR_INIT_STATUS_INITIALIZED);
11516 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11517 VAR_INIT_STATUS_INITIALIZED);
11519 if (op0 == NULL || op1 == NULL)
11520 return NULL;
11522 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11523 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11524 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11525 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11527 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11529 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11530 add_loc_descr (&op0, int_loc_descriptor (mask));
11531 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11532 add_loc_descr (&op1, int_loc_descriptor (mask));
11533 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11535 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11537 HOST_WIDE_INT bias = 1;
11538 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11539 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11540 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11543 else if (GET_MODE_CLASS (mode) == MODE_INT
11544 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11546 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11547 add_loc_descr (&op0, int_loc_descriptor (shift));
11548 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11549 add_loc_descr (&op1, int_loc_descriptor (shift));
11550 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11552 else if (GET_MODE_CLASS (mode) == MODE_INT
11553 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11555 dw_die_ref type_die = base_type_for_mode (mode, 0);
11556 dw_loc_descr_ref cvt;
11557 if (type_die == NULL)
11558 return NULL;
11559 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11560 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11561 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11562 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11563 add_loc_descr (&op0, cvt);
11564 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11565 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11566 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11567 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11568 add_loc_descr (&op1, cvt);
11571 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11572 op = DW_OP_lt;
11573 else
11574 op = DW_OP_gt;
11575 ret = op0;
11576 add_loc_descr (&ret, op1);
11577 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11578 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11579 add_loc_descr (&ret, bra_node);
11580 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11581 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11582 add_loc_descr (&ret, drop_node);
11583 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11584 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11585 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11586 && GET_MODE_CLASS (mode) == MODE_INT
11587 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11588 ret = convert_descriptor_to_mode (mode, ret);
11589 return ret;
11592 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11593 but after converting arguments to type_die, afterwards
11594 convert back to unsigned. */
11596 static dw_loc_descr_ref
11597 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11598 enum machine_mode mode, enum machine_mode mem_mode)
11600 dw_loc_descr_ref cvt, op0, op1;
11602 if (type_die == NULL)
11603 return NULL;
11604 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11605 VAR_INIT_STATUS_INITIALIZED);
11606 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11607 VAR_INIT_STATUS_INITIALIZED);
11608 if (op0 == NULL || op1 == NULL)
11609 return NULL;
11610 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11611 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11612 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11613 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11614 add_loc_descr (&op0, cvt);
11615 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11616 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11617 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11618 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11619 add_loc_descr (&op1, cvt);
11620 add_loc_descr (&op0, op1);
11621 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11622 return convert_descriptor_to_mode (mode, op0);
11625 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11626 const0 is DW_OP_lit0 or corresponding typed constant,
11627 const1 is DW_OP_lit1 or corresponding typed constant
11628 and constMSB is constant with just the MSB bit set
11629 for the mode):
11630 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11631 L1: const0 DW_OP_swap
11632 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11633 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11634 L3: DW_OP_drop
11635 L4: DW_OP_nop
11637 CTZ is similar:
11638 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11639 L1: const0 DW_OP_swap
11640 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11641 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11642 L3: DW_OP_drop
11643 L4: DW_OP_nop
11645 FFS is similar:
11646 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11647 L1: const1 DW_OP_swap
11648 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11649 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11650 L3: DW_OP_drop
11651 L4: DW_OP_nop */
11653 static dw_loc_descr_ref
11654 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11655 enum machine_mode mem_mode)
11657 dw_loc_descr_ref op0, ret, tmp;
11658 HOST_WIDE_INT valv;
11659 dw_loc_descr_ref l1jump, l1label;
11660 dw_loc_descr_ref l2jump, l2label;
11661 dw_loc_descr_ref l3jump, l3label;
11662 dw_loc_descr_ref l4jump, l4label;
11663 rtx msb;
11665 if (GET_MODE_CLASS (mode) != MODE_INT
11666 || GET_MODE (XEXP (rtl, 0)) != mode
11667 || (GET_CODE (rtl) == CLZ
11668 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11669 return NULL;
11671 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11672 VAR_INIT_STATUS_INITIALIZED);
11673 if (op0 == NULL)
11674 return NULL;
11675 ret = op0;
11676 if (GET_CODE (rtl) == CLZ)
11678 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11679 valv = GET_MODE_BITSIZE (mode);
11681 else if (GET_CODE (rtl) == FFS)
11682 valv = 0;
11683 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11684 valv = GET_MODE_BITSIZE (mode);
11685 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11686 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11687 add_loc_descr (&ret, l1jump);
11688 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11689 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11690 VAR_INIT_STATUS_INITIALIZED);
11691 if (tmp == NULL)
11692 return NULL;
11693 add_loc_descr (&ret, tmp);
11694 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11695 add_loc_descr (&ret, l4jump);
11696 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11697 ? const1_rtx : const0_rtx,
11698 mode, mem_mode,
11699 VAR_INIT_STATUS_INITIALIZED);
11700 if (l1label == NULL)
11701 return NULL;
11702 add_loc_descr (&ret, l1label);
11703 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11704 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11705 add_loc_descr (&ret, l2label);
11706 if (GET_CODE (rtl) != CLZ)
11707 msb = const1_rtx;
11708 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11709 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11710 << (GET_MODE_BITSIZE (mode) - 1));
11711 else
11712 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11713 << (GET_MODE_BITSIZE (mode)
11714 - HOST_BITS_PER_WIDE_INT - 1), mode);
11715 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11716 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11717 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11718 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11719 else
11720 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11721 VAR_INIT_STATUS_INITIALIZED);
11722 if (tmp == NULL)
11723 return NULL;
11724 add_loc_descr (&ret, tmp);
11725 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11726 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11727 add_loc_descr (&ret, l3jump);
11728 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11729 VAR_INIT_STATUS_INITIALIZED);
11730 if (tmp == NULL)
11731 return NULL;
11732 add_loc_descr (&ret, tmp);
11733 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11734 ? DW_OP_shl : DW_OP_shr, 0, 0));
11735 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11736 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11737 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11738 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11739 add_loc_descr (&ret, l2jump);
11740 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11741 add_loc_descr (&ret, l3label);
11742 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11743 add_loc_descr (&ret, l4label);
11744 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11745 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11746 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11747 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11748 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11749 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11750 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11751 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11752 return ret;
11755 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11756 const1 is DW_OP_lit1 or corresponding typed constant):
11757 const0 DW_OP_swap
11758 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11759 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11760 L2: DW_OP_drop
11762 PARITY is similar:
11763 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11764 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11765 L2: DW_OP_drop */
11767 static dw_loc_descr_ref
11768 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11769 enum machine_mode mem_mode)
11771 dw_loc_descr_ref op0, ret, tmp;
11772 dw_loc_descr_ref l1jump, l1label;
11773 dw_loc_descr_ref l2jump, l2label;
11775 if (GET_MODE_CLASS (mode) != MODE_INT
11776 || GET_MODE (XEXP (rtl, 0)) != mode)
11777 return NULL;
11779 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11780 VAR_INIT_STATUS_INITIALIZED);
11781 if (op0 == NULL)
11782 return NULL;
11783 ret = op0;
11784 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11785 VAR_INIT_STATUS_INITIALIZED);
11786 if (tmp == NULL)
11787 return NULL;
11788 add_loc_descr (&ret, tmp);
11789 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11790 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11791 add_loc_descr (&ret, l1label);
11792 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11793 add_loc_descr (&ret, l2jump);
11794 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11795 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11796 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11797 VAR_INIT_STATUS_INITIALIZED);
11798 if (tmp == NULL)
11799 return NULL;
11800 add_loc_descr (&ret, tmp);
11801 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11802 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11803 ? DW_OP_plus : DW_OP_xor, 0, 0));
11804 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11805 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11806 VAR_INIT_STATUS_INITIALIZED);
11807 add_loc_descr (&ret, tmp);
11808 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11809 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11810 add_loc_descr (&ret, l1jump);
11811 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11812 add_loc_descr (&ret, l2label);
11813 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11814 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11815 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11816 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11817 return ret;
11820 /* BSWAP (constS is initial shift count, either 56 or 24):
11821 constS const0
11822 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11823 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11824 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11825 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11826 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11828 static dw_loc_descr_ref
11829 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11830 enum machine_mode mem_mode)
11832 dw_loc_descr_ref op0, ret, tmp;
11833 dw_loc_descr_ref l1jump, l1label;
11834 dw_loc_descr_ref l2jump, l2label;
11836 if (GET_MODE_CLASS (mode) != MODE_INT
11837 || BITS_PER_UNIT != 8
11838 || (GET_MODE_BITSIZE (mode) != 32
11839 && GET_MODE_BITSIZE (mode) != 64))
11840 return NULL;
11842 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11843 VAR_INIT_STATUS_INITIALIZED);
11844 if (op0 == NULL)
11845 return NULL;
11847 ret = op0;
11848 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11849 mode, mem_mode,
11850 VAR_INIT_STATUS_INITIALIZED);
11851 if (tmp == NULL)
11852 return NULL;
11853 add_loc_descr (&ret, tmp);
11854 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11855 VAR_INIT_STATUS_INITIALIZED);
11856 if (tmp == NULL)
11857 return NULL;
11858 add_loc_descr (&ret, tmp);
11859 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11860 add_loc_descr (&ret, l1label);
11861 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11862 mode, mem_mode,
11863 VAR_INIT_STATUS_INITIALIZED);
11864 add_loc_descr (&ret, tmp);
11865 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11866 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11867 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11868 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11869 VAR_INIT_STATUS_INITIALIZED);
11870 if (tmp == NULL)
11871 return NULL;
11872 add_loc_descr (&ret, tmp);
11873 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11874 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11875 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11876 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11877 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11878 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11879 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11880 VAR_INIT_STATUS_INITIALIZED);
11881 add_loc_descr (&ret, tmp);
11882 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11883 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11884 add_loc_descr (&ret, l2jump);
11885 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11886 VAR_INIT_STATUS_INITIALIZED);
11887 add_loc_descr (&ret, tmp);
11888 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11889 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11890 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11891 add_loc_descr (&ret, l1jump);
11892 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11893 add_loc_descr (&ret, l2label);
11894 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11895 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11896 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11897 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11898 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11899 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11900 return ret;
11903 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11904 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11905 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11906 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11908 ROTATERT is similar:
11909 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11910 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11911 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11913 static dw_loc_descr_ref
11914 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11915 enum machine_mode mem_mode)
11917 rtx rtlop1 = XEXP (rtl, 1);
11918 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11919 int i;
11921 if (GET_MODE_CLASS (mode) != MODE_INT)
11922 return NULL;
11924 if (GET_MODE (rtlop1) != VOIDmode
11925 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11926 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11927 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11928 VAR_INIT_STATUS_INITIALIZED);
11929 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11930 VAR_INIT_STATUS_INITIALIZED);
11931 if (op0 == NULL || op1 == NULL)
11932 return NULL;
11933 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11934 for (i = 0; i < 2; i++)
11936 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11937 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11938 mode, mem_mode,
11939 VAR_INIT_STATUS_INITIALIZED);
11940 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11941 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11942 ? DW_OP_const4u
11943 : HOST_BITS_PER_WIDE_INT == 64
11944 ? DW_OP_const8u : DW_OP_constu,
11945 GET_MODE_MASK (mode), 0);
11946 else
11947 mask[i] = NULL;
11948 if (mask[i] == NULL)
11949 return NULL;
11950 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11952 ret = op0;
11953 add_loc_descr (&ret, op1);
11954 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11955 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11956 if (GET_CODE (rtl) == ROTATERT)
11958 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11959 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11960 GET_MODE_BITSIZE (mode), 0));
11962 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11963 if (mask[0] != NULL)
11964 add_loc_descr (&ret, mask[0]);
11965 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11966 if (mask[1] != NULL)
11968 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11969 add_loc_descr (&ret, mask[1]);
11970 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11972 if (GET_CODE (rtl) == ROTATE)
11974 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11975 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11976 GET_MODE_BITSIZE (mode), 0));
11978 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11979 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11980 return ret;
11983 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11984 for DEBUG_PARAMETER_REF RTL. */
11986 static dw_loc_descr_ref
11987 parameter_ref_descriptor (rtx rtl)
11989 dw_loc_descr_ref ret;
11990 dw_die_ref ref;
11992 if (dwarf_strict)
11993 return NULL;
11994 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11995 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11996 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11997 if (ref)
11999 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12000 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12001 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12003 else
12005 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12006 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12008 return ret;
12011 /* The following routine converts the RTL for a variable or parameter
12012 (resident in memory) into an equivalent Dwarf representation of a
12013 mechanism for getting the address of that same variable onto the top of a
12014 hypothetical "address evaluation" stack.
12016 When creating memory location descriptors, we are effectively transforming
12017 the RTL for a memory-resident object into its Dwarf postfix expression
12018 equivalent. This routine recursively descends an RTL tree, turning
12019 it into Dwarf postfix code as it goes.
12021 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12023 MEM_MODE is the mode of the memory reference, needed to handle some
12024 autoincrement addressing modes.
12026 Return 0 if we can't represent the location. */
12028 dw_loc_descr_ref
12029 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12030 enum machine_mode mem_mode,
12031 enum var_init_status initialized)
12033 dw_loc_descr_ref mem_loc_result = NULL;
12034 enum dwarf_location_atom op;
12035 dw_loc_descr_ref op0, op1;
12036 rtx inner = NULL_RTX;
12038 if (mode == VOIDmode)
12039 mode = GET_MODE (rtl);
12041 /* Note that for a dynamically sized array, the location we will generate a
12042 description of here will be the lowest numbered location which is
12043 actually within the array. That's *not* necessarily the same as the
12044 zeroth element of the array. */
12046 rtl = targetm.delegitimize_address (rtl);
12048 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12049 return NULL;
12051 switch (GET_CODE (rtl))
12053 case POST_INC:
12054 case POST_DEC:
12055 case POST_MODIFY:
12056 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12058 case SUBREG:
12059 /* The case of a subreg may arise when we have a local (register)
12060 variable or a formal (register) parameter which doesn't quite fill
12061 up an entire register. For now, just assume that it is
12062 legitimate to make the Dwarf info refer to the whole register which
12063 contains the given subreg. */
12064 if (!subreg_lowpart_p (rtl))
12065 break;
12066 inner = SUBREG_REG (rtl);
12067 case TRUNCATE:
12068 if (inner == NULL_RTX)
12069 inner = XEXP (rtl, 0);
12070 if (GET_MODE_CLASS (mode) == MODE_INT
12071 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12072 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12073 #ifdef POINTERS_EXTEND_UNSIGNED
12074 || (mode == Pmode && mem_mode != VOIDmode)
12075 #endif
12077 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12079 mem_loc_result = mem_loc_descriptor (inner,
12080 GET_MODE (inner),
12081 mem_mode, initialized);
12082 break;
12084 if (dwarf_strict)
12085 break;
12086 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12087 break;
12088 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12089 && (GET_MODE_CLASS (mode) != MODE_INT
12090 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12091 break;
12092 else
12094 dw_die_ref type_die;
12095 dw_loc_descr_ref cvt;
12097 mem_loc_result = mem_loc_descriptor (inner,
12098 GET_MODE (inner),
12099 mem_mode, initialized);
12100 if (mem_loc_result == NULL)
12101 break;
12102 type_die = base_type_for_mode (mode,
12103 GET_MODE_CLASS (mode) == MODE_INT);
12104 if (type_die == NULL)
12106 mem_loc_result = NULL;
12107 break;
12109 if (GET_MODE_SIZE (mode)
12110 != GET_MODE_SIZE (GET_MODE (inner)))
12111 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12112 else
12113 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12114 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12115 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12116 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12117 add_loc_descr (&mem_loc_result, cvt);
12119 break;
12121 case REG:
12122 if (GET_MODE_CLASS (mode) != MODE_INT
12123 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12124 && rtl != arg_pointer_rtx
12125 && rtl != frame_pointer_rtx
12126 #ifdef POINTERS_EXTEND_UNSIGNED
12127 && (mode != Pmode || mem_mode == VOIDmode)
12128 #endif
12131 dw_die_ref type_die;
12132 unsigned int dbx_regnum;
12134 if (dwarf_strict)
12135 break;
12136 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12137 break;
12138 type_die = base_type_for_mode (mode,
12139 GET_MODE_CLASS (mode) == MODE_INT);
12140 if (type_die == NULL)
12141 break;
12143 dbx_regnum = dbx_reg_number (rtl);
12144 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12145 break;
12146 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12147 dbx_regnum, 0);
12148 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12149 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12150 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12151 break;
12153 /* Whenever a register number forms a part of the description of the
12154 method for calculating the (dynamic) address of a memory resident
12155 object, DWARF rules require the register number be referred to as
12156 a "base register". This distinction is not based in any way upon
12157 what category of register the hardware believes the given register
12158 belongs to. This is strictly DWARF terminology we're dealing with
12159 here. Note that in cases where the location of a memory-resident
12160 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12161 OP_CONST (0)) the actual DWARF location descriptor that we generate
12162 may just be OP_BASEREG (basereg). This may look deceptively like
12163 the object in question was allocated to a register (rather than in
12164 memory) so DWARF consumers need to be aware of the subtle
12165 distinction between OP_REG and OP_BASEREG. */
12166 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12167 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12168 else if (stack_realign_drap
12169 && crtl->drap_reg
12170 && crtl->args.internal_arg_pointer == rtl
12171 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12173 /* If RTL is internal_arg_pointer, which has been optimized
12174 out, use DRAP instead. */
12175 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12176 VAR_INIT_STATUS_INITIALIZED);
12178 break;
12180 case SIGN_EXTEND:
12181 case ZERO_EXTEND:
12182 if (GET_MODE_CLASS (mode) != MODE_INT)
12183 break;
12184 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12185 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12186 if (op0 == 0)
12187 break;
12188 else if (GET_CODE (rtl) == ZERO_EXTEND
12189 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12190 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12191 < HOST_BITS_PER_WIDE_INT
12192 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12193 to expand zero extend as two shifts instead of
12194 masking. */
12195 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12197 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12198 mem_loc_result = op0;
12199 add_loc_descr (&mem_loc_result,
12200 int_loc_descriptor (GET_MODE_MASK (imode)));
12201 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12203 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12205 int shift = DWARF2_ADDR_SIZE
12206 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12207 shift *= BITS_PER_UNIT;
12208 if (GET_CODE (rtl) == SIGN_EXTEND)
12209 op = DW_OP_shra;
12210 else
12211 op = DW_OP_shr;
12212 mem_loc_result = op0;
12213 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12214 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12215 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12216 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12218 else if (!dwarf_strict)
12220 dw_die_ref type_die1, type_die2;
12221 dw_loc_descr_ref cvt;
12223 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12224 GET_CODE (rtl) == ZERO_EXTEND);
12225 if (type_die1 == NULL)
12226 break;
12227 type_die2 = base_type_for_mode (mode, 1);
12228 if (type_die2 == NULL)
12229 break;
12230 mem_loc_result = op0;
12231 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12232 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12233 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12234 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12235 add_loc_descr (&mem_loc_result, cvt);
12236 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12237 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12238 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12239 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12240 add_loc_descr (&mem_loc_result, cvt);
12242 break;
12244 case MEM:
12246 rtx new_rtl = avoid_constant_pool_reference (rtl);
12247 if (new_rtl != rtl)
12249 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12250 initialized);
12251 if (mem_loc_result != NULL)
12252 return mem_loc_result;
12255 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12256 get_address_mode (rtl), mode,
12257 VAR_INIT_STATUS_INITIALIZED);
12258 if (mem_loc_result == NULL)
12259 mem_loc_result = tls_mem_loc_descriptor (rtl);
12260 if (mem_loc_result != NULL)
12262 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12263 || GET_MODE_CLASS (mode) != MODE_INT)
12265 dw_die_ref type_die;
12266 dw_loc_descr_ref deref;
12268 if (dwarf_strict)
12269 return NULL;
12270 type_die
12271 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12272 if (type_die == NULL)
12273 return NULL;
12274 deref = new_loc_descr (DW_OP_GNU_deref_type,
12275 GET_MODE_SIZE (mode), 0);
12276 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12277 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12278 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12279 add_loc_descr (&mem_loc_result, deref);
12281 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12282 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12283 else
12284 add_loc_descr (&mem_loc_result,
12285 new_loc_descr (DW_OP_deref_size,
12286 GET_MODE_SIZE (mode), 0));
12288 break;
12290 case LO_SUM:
12291 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12293 case LABEL_REF:
12294 /* Some ports can transform a symbol ref into a label ref, because
12295 the symbol ref is too far away and has to be dumped into a constant
12296 pool. */
12297 case CONST:
12298 case SYMBOL_REF:
12299 if (GET_MODE_CLASS (mode) != MODE_INT
12300 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12301 #ifdef POINTERS_EXTEND_UNSIGNED
12302 && (mode != Pmode || mem_mode == VOIDmode)
12303 #endif
12305 break;
12306 if (GET_CODE (rtl) == SYMBOL_REF
12307 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12309 dw_loc_descr_ref temp;
12311 /* If this is not defined, we have no way to emit the data. */
12312 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12313 break;
12315 temp = new_addr_loc_descr (rtl, dtprel_true);
12317 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12318 add_loc_descr (&mem_loc_result, temp);
12320 break;
12323 if (!const_ok_for_output (rtl))
12324 break;
12326 symref:
12327 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12328 vec_safe_push (used_rtx_array, rtl);
12329 break;
12331 case CONCAT:
12332 case CONCATN:
12333 case VAR_LOCATION:
12334 case DEBUG_IMPLICIT_PTR:
12335 expansion_failed (NULL_TREE, rtl,
12336 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12337 return 0;
12339 case ENTRY_VALUE:
12340 if (dwarf_strict)
12341 return NULL;
12342 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12344 if (GET_MODE_CLASS (mode) != MODE_INT
12345 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12346 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12347 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12348 else
12350 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12351 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12352 return NULL;
12353 op0 = one_reg_loc_descriptor (dbx_regnum,
12354 VAR_INIT_STATUS_INITIALIZED);
12357 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12358 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12360 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12361 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12362 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12363 return NULL;
12365 else
12366 gcc_unreachable ();
12367 if (op0 == NULL)
12368 return NULL;
12369 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12370 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12371 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12372 break;
12374 case DEBUG_PARAMETER_REF:
12375 mem_loc_result = parameter_ref_descriptor (rtl);
12376 break;
12378 case PRE_MODIFY:
12379 /* Extract the PLUS expression nested inside and fall into
12380 PLUS code below. */
12381 rtl = XEXP (rtl, 1);
12382 goto plus;
12384 case PRE_INC:
12385 case PRE_DEC:
12386 /* Turn these into a PLUS expression and fall into the PLUS code
12387 below. */
12388 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12389 GEN_INT (GET_CODE (rtl) == PRE_INC
12390 ? GET_MODE_UNIT_SIZE (mem_mode)
12391 : -GET_MODE_UNIT_SIZE (mem_mode)));
12393 /* ... fall through ... */
12395 case PLUS:
12396 plus:
12397 if (is_based_loc (rtl)
12398 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12399 || XEXP (rtl, 0) == arg_pointer_rtx
12400 || XEXP (rtl, 0) == frame_pointer_rtx)
12401 && GET_MODE_CLASS (mode) == MODE_INT)
12402 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12403 INTVAL (XEXP (rtl, 1)),
12404 VAR_INIT_STATUS_INITIALIZED);
12405 else
12407 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12408 VAR_INIT_STATUS_INITIALIZED);
12409 if (mem_loc_result == 0)
12410 break;
12412 if (CONST_INT_P (XEXP (rtl, 1))
12413 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12414 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12415 else
12417 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12418 VAR_INIT_STATUS_INITIALIZED);
12419 if (op1 == 0)
12420 break;
12421 add_loc_descr (&mem_loc_result, op1);
12422 add_loc_descr (&mem_loc_result,
12423 new_loc_descr (DW_OP_plus, 0, 0));
12426 break;
12428 /* If a pseudo-reg is optimized away, it is possible for it to
12429 be replaced with a MEM containing a multiply or shift. */
12430 case MINUS:
12431 op = DW_OP_minus;
12432 goto do_binop;
12434 case MULT:
12435 op = DW_OP_mul;
12436 goto do_binop;
12438 case DIV:
12439 if (!dwarf_strict
12440 && GET_MODE_CLASS (mode) == MODE_INT
12441 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12443 mem_loc_result = typed_binop (DW_OP_div, rtl,
12444 base_type_for_mode (mode, 0),
12445 mode, mem_mode);
12446 break;
12448 op = DW_OP_div;
12449 goto do_binop;
12451 case UMOD:
12452 op = DW_OP_mod;
12453 goto do_binop;
12455 case ASHIFT:
12456 op = DW_OP_shl;
12457 goto do_shift;
12459 case ASHIFTRT:
12460 op = DW_OP_shra;
12461 goto do_shift;
12463 case LSHIFTRT:
12464 op = DW_OP_shr;
12465 goto do_shift;
12467 do_shift:
12468 if (GET_MODE_CLASS (mode) != MODE_INT)
12469 break;
12470 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12471 VAR_INIT_STATUS_INITIALIZED);
12473 rtx rtlop1 = XEXP (rtl, 1);
12474 if (GET_MODE (rtlop1) != VOIDmode
12475 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12476 < GET_MODE_BITSIZE (mode))
12477 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12478 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12479 VAR_INIT_STATUS_INITIALIZED);
12482 if (op0 == 0 || op1 == 0)
12483 break;
12485 mem_loc_result = op0;
12486 add_loc_descr (&mem_loc_result, op1);
12487 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12488 break;
12490 case AND:
12491 op = DW_OP_and;
12492 goto do_binop;
12494 case IOR:
12495 op = DW_OP_or;
12496 goto do_binop;
12498 case XOR:
12499 op = DW_OP_xor;
12500 goto do_binop;
12502 do_binop:
12503 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12504 VAR_INIT_STATUS_INITIALIZED);
12505 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12506 VAR_INIT_STATUS_INITIALIZED);
12508 if (op0 == 0 || op1 == 0)
12509 break;
12511 mem_loc_result = op0;
12512 add_loc_descr (&mem_loc_result, op1);
12513 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12514 break;
12516 case MOD:
12517 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12519 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12520 base_type_for_mode (mode, 0),
12521 mode, mem_mode);
12522 break;
12525 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12526 VAR_INIT_STATUS_INITIALIZED);
12527 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12528 VAR_INIT_STATUS_INITIALIZED);
12530 if (op0 == 0 || op1 == 0)
12531 break;
12533 mem_loc_result = op0;
12534 add_loc_descr (&mem_loc_result, op1);
12535 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12536 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12537 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12538 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12539 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12540 break;
12542 case UDIV:
12543 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12545 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12547 op = DW_OP_div;
12548 goto do_binop;
12550 mem_loc_result = typed_binop (DW_OP_div, rtl,
12551 base_type_for_mode (mode, 1),
12552 mode, mem_mode);
12554 break;
12556 case NOT:
12557 op = DW_OP_not;
12558 goto do_unop;
12560 case ABS:
12561 op = DW_OP_abs;
12562 goto do_unop;
12564 case NEG:
12565 op = DW_OP_neg;
12566 goto do_unop;
12568 do_unop:
12569 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12570 VAR_INIT_STATUS_INITIALIZED);
12572 if (op0 == 0)
12573 break;
12575 mem_loc_result = op0;
12576 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12577 break;
12579 case CONST_INT:
12580 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12581 #ifdef POINTERS_EXTEND_UNSIGNED
12582 || (mode == Pmode
12583 && mem_mode != VOIDmode
12584 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12585 #endif
12588 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12589 break;
12591 if (!dwarf_strict
12592 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12593 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12595 dw_die_ref type_die = base_type_for_mode (mode, 1);
12596 enum machine_mode amode;
12597 if (type_die == NULL)
12598 return NULL;
12599 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12600 MODE_INT, 0);
12601 if (INTVAL (rtl) >= 0
12602 && amode != BLKmode
12603 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12604 /* const DW_OP_GNU_convert <XXX> vs.
12605 DW_OP_GNU_const_type <XXX, 1, const>. */
12606 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12607 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12609 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12610 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12611 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12612 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12613 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12614 add_loc_descr (&mem_loc_result, op0);
12615 return mem_loc_result;
12617 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12618 INTVAL (rtl));
12619 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12620 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12621 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12622 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12623 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12624 else
12626 mem_loc_result->dw_loc_oprnd2.val_class
12627 = dw_val_class_const_double;
12628 mem_loc_result->dw_loc_oprnd2.v.val_double
12629 = double_int::from_shwi (INTVAL (rtl));
12632 break;
12634 case CONST_DOUBLE:
12635 if (!dwarf_strict)
12637 dw_die_ref type_die;
12639 /* Note that a CONST_DOUBLE rtx could represent either an integer
12640 or a floating-point constant. A CONST_DOUBLE is used whenever
12641 the constant requires more than one word in order to be
12642 adequately represented. We output CONST_DOUBLEs as blocks. */
12643 if (mode == VOIDmode
12644 || (GET_MODE (rtl) == VOIDmode
12645 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12646 break;
12647 type_die = base_type_for_mode (mode,
12648 GET_MODE_CLASS (mode) == MODE_INT);
12649 if (type_die == NULL)
12650 return NULL;
12651 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12652 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12653 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12654 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12655 if (SCALAR_FLOAT_MODE_P (mode))
12657 unsigned int length = GET_MODE_SIZE (mode);
12658 unsigned char *array
12659 = (unsigned char*) ggc_alloc_atomic (length);
12661 insert_float (rtl, array);
12662 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12663 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12664 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12665 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12667 else
12669 mem_loc_result->dw_loc_oprnd2.val_class
12670 = dw_val_class_const_double;
12671 mem_loc_result->dw_loc_oprnd2.v.val_double
12672 = rtx_to_double_int (rtl);
12675 break;
12677 case EQ:
12678 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12679 break;
12681 case GE:
12682 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12683 break;
12685 case GT:
12686 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12687 break;
12689 case LE:
12690 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12691 break;
12693 case LT:
12694 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12695 break;
12697 case NE:
12698 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12699 break;
12701 case GEU:
12702 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12703 break;
12705 case GTU:
12706 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12707 break;
12709 case LEU:
12710 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12711 break;
12713 case LTU:
12714 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12715 break;
12717 case UMIN:
12718 case UMAX:
12719 if (GET_MODE_CLASS (mode) != MODE_INT)
12720 break;
12721 /* FALLTHRU */
12722 case SMIN:
12723 case SMAX:
12724 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12725 break;
12727 case ZERO_EXTRACT:
12728 case SIGN_EXTRACT:
12729 if (CONST_INT_P (XEXP (rtl, 1))
12730 && CONST_INT_P (XEXP (rtl, 2))
12731 && ((unsigned) INTVAL (XEXP (rtl, 1))
12732 + (unsigned) INTVAL (XEXP (rtl, 2))
12733 <= GET_MODE_BITSIZE (mode))
12734 && GET_MODE_CLASS (mode) == MODE_INT
12735 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12736 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12738 int shift, size;
12739 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12740 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12741 if (op0 == 0)
12742 break;
12743 if (GET_CODE (rtl) == SIGN_EXTRACT)
12744 op = DW_OP_shra;
12745 else
12746 op = DW_OP_shr;
12747 mem_loc_result = op0;
12748 size = INTVAL (XEXP (rtl, 1));
12749 shift = INTVAL (XEXP (rtl, 2));
12750 if (BITS_BIG_ENDIAN)
12751 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12752 - shift - size;
12753 if (shift + size != (int) DWARF2_ADDR_SIZE)
12755 add_loc_descr (&mem_loc_result,
12756 int_loc_descriptor (DWARF2_ADDR_SIZE
12757 - shift - size));
12758 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12760 if (size != (int) DWARF2_ADDR_SIZE)
12762 add_loc_descr (&mem_loc_result,
12763 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12764 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12767 break;
12769 case IF_THEN_ELSE:
12771 dw_loc_descr_ref op2, bra_node, drop_node;
12772 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12773 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12774 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12775 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12776 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12777 VAR_INIT_STATUS_INITIALIZED);
12778 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12779 VAR_INIT_STATUS_INITIALIZED);
12780 if (op0 == NULL || op1 == NULL || op2 == NULL)
12781 break;
12783 mem_loc_result = op1;
12784 add_loc_descr (&mem_loc_result, op2);
12785 add_loc_descr (&mem_loc_result, op0);
12786 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12787 add_loc_descr (&mem_loc_result, bra_node);
12788 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12789 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12790 add_loc_descr (&mem_loc_result, drop_node);
12791 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12792 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12794 break;
12796 case FLOAT_EXTEND:
12797 case FLOAT_TRUNCATE:
12798 case FLOAT:
12799 case UNSIGNED_FLOAT:
12800 case FIX:
12801 case UNSIGNED_FIX:
12802 if (!dwarf_strict)
12804 dw_die_ref type_die;
12805 dw_loc_descr_ref cvt;
12807 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12808 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12809 if (op0 == NULL)
12810 break;
12811 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12812 && (GET_CODE (rtl) == FLOAT
12813 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12814 <= DWARF2_ADDR_SIZE))
12816 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12817 GET_CODE (rtl) == UNSIGNED_FLOAT);
12818 if (type_die == NULL)
12819 break;
12820 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12821 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12822 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12823 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12824 add_loc_descr (&op0, cvt);
12826 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12827 if (type_die == NULL)
12828 break;
12829 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12830 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12831 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12832 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12833 add_loc_descr (&op0, cvt);
12834 if (GET_MODE_CLASS (mode) == MODE_INT
12835 && (GET_CODE (rtl) == FIX
12836 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12838 op0 = convert_descriptor_to_mode (mode, op0);
12839 if (op0 == NULL)
12840 break;
12842 mem_loc_result = op0;
12844 break;
12846 case CLZ:
12847 case CTZ:
12848 case FFS:
12849 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12850 break;
12852 case POPCOUNT:
12853 case PARITY:
12854 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12855 break;
12857 case BSWAP:
12858 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12859 break;
12861 case ROTATE:
12862 case ROTATERT:
12863 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12864 break;
12866 case COMPARE:
12867 /* In theory, we could implement the above. */
12868 /* DWARF cannot represent the unsigned compare operations
12869 natively. */
12870 case SS_MULT:
12871 case US_MULT:
12872 case SS_DIV:
12873 case US_DIV:
12874 case SS_PLUS:
12875 case US_PLUS:
12876 case SS_MINUS:
12877 case US_MINUS:
12878 case SS_NEG:
12879 case US_NEG:
12880 case SS_ABS:
12881 case SS_ASHIFT:
12882 case US_ASHIFT:
12883 case SS_TRUNCATE:
12884 case US_TRUNCATE:
12885 case UNORDERED:
12886 case ORDERED:
12887 case UNEQ:
12888 case UNGE:
12889 case UNGT:
12890 case UNLE:
12891 case UNLT:
12892 case LTGT:
12893 case FRACT_CONVERT:
12894 case UNSIGNED_FRACT_CONVERT:
12895 case SAT_FRACT:
12896 case UNSIGNED_SAT_FRACT:
12897 case SQRT:
12898 case ASM_OPERANDS:
12899 case VEC_MERGE:
12900 case VEC_SELECT:
12901 case VEC_CONCAT:
12902 case VEC_DUPLICATE:
12903 case UNSPEC:
12904 case HIGH:
12905 case FMA:
12906 case STRICT_LOW_PART:
12907 case CONST_VECTOR:
12908 case CONST_FIXED:
12909 case CLRSB:
12910 case CLOBBER:
12911 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12912 can't express it in the debug info. This can happen e.g. with some
12913 TLS UNSPECs. */
12914 break;
12916 case CONST_STRING:
12917 resolve_one_addr (&rtl, NULL);
12918 goto symref;
12920 default:
12921 #ifdef ENABLE_CHECKING
12922 print_rtl (stderr, rtl);
12923 gcc_unreachable ();
12924 #else
12925 break;
12926 #endif
12929 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12930 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12932 return mem_loc_result;
12935 /* Return a descriptor that describes the concatenation of two locations.
12936 This is typically a complex variable. */
12938 static dw_loc_descr_ref
12939 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12941 dw_loc_descr_ref cc_loc_result = NULL;
12942 dw_loc_descr_ref x0_ref
12943 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12944 dw_loc_descr_ref x1_ref
12945 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12947 if (x0_ref == 0 || x1_ref == 0)
12948 return 0;
12950 cc_loc_result = x0_ref;
12951 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12953 add_loc_descr (&cc_loc_result, x1_ref);
12954 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12956 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12957 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12959 return cc_loc_result;
12962 /* Return a descriptor that describes the concatenation of N
12963 locations. */
12965 static dw_loc_descr_ref
12966 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12968 unsigned int i;
12969 dw_loc_descr_ref cc_loc_result = NULL;
12970 unsigned int n = XVECLEN (concatn, 0);
12972 for (i = 0; i < n; ++i)
12974 dw_loc_descr_ref ref;
12975 rtx x = XVECEXP (concatn, 0, i);
12977 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12978 if (ref == NULL)
12979 return NULL;
12981 add_loc_descr (&cc_loc_result, ref);
12982 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12985 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12986 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12988 return cc_loc_result;
12991 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12992 for DEBUG_IMPLICIT_PTR RTL. */
12994 static dw_loc_descr_ref
12995 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12997 dw_loc_descr_ref ret;
12998 dw_die_ref ref;
13000 if (dwarf_strict)
13001 return NULL;
13002 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13003 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13004 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13005 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13006 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13007 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13008 if (ref)
13010 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13011 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13012 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13014 else
13016 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13017 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13019 return ret;
13022 /* Output a proper Dwarf location descriptor for a variable or parameter
13023 which is either allocated in a register or in a memory location. For a
13024 register, we just generate an OP_REG and the register number. For a
13025 memory location we provide a Dwarf postfix expression describing how to
13026 generate the (dynamic) address of the object onto the address stack.
13028 MODE is mode of the decl if this loc_descriptor is going to be used in
13029 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13030 allowed, VOIDmode otherwise.
13032 If we don't know how to describe it, return 0. */
13034 static dw_loc_descr_ref
13035 loc_descriptor (rtx rtl, enum machine_mode mode,
13036 enum var_init_status initialized)
13038 dw_loc_descr_ref loc_result = NULL;
13040 switch (GET_CODE (rtl))
13042 case SUBREG:
13043 /* The case of a subreg may arise when we have a local (register)
13044 variable or a formal (register) parameter which doesn't quite fill
13045 up an entire register. For now, just assume that it is
13046 legitimate to make the Dwarf info refer to the whole register which
13047 contains the given subreg. */
13048 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13049 loc_result = loc_descriptor (SUBREG_REG (rtl),
13050 GET_MODE (SUBREG_REG (rtl)), initialized);
13051 else
13052 goto do_default;
13053 break;
13055 case REG:
13056 loc_result = reg_loc_descriptor (rtl, initialized);
13057 break;
13059 case MEM:
13060 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13061 GET_MODE (rtl), initialized);
13062 if (loc_result == NULL)
13063 loc_result = tls_mem_loc_descriptor (rtl);
13064 if (loc_result == NULL)
13066 rtx new_rtl = avoid_constant_pool_reference (rtl);
13067 if (new_rtl != rtl)
13068 loc_result = loc_descriptor (new_rtl, mode, initialized);
13070 break;
13072 case CONCAT:
13073 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13074 initialized);
13075 break;
13077 case CONCATN:
13078 loc_result = concatn_loc_descriptor (rtl, initialized);
13079 break;
13081 case VAR_LOCATION:
13082 /* Single part. */
13083 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13085 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13086 if (GET_CODE (loc) == EXPR_LIST)
13087 loc = XEXP (loc, 0);
13088 loc_result = loc_descriptor (loc, mode, initialized);
13089 break;
13092 rtl = XEXP (rtl, 1);
13093 /* FALLTHRU */
13095 case PARALLEL:
13097 rtvec par_elems = XVEC (rtl, 0);
13098 int num_elem = GET_NUM_ELEM (par_elems);
13099 enum machine_mode mode;
13100 int i;
13102 /* Create the first one, so we have something to add to. */
13103 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13104 VOIDmode, initialized);
13105 if (loc_result == NULL)
13106 return NULL;
13107 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13108 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13109 for (i = 1; i < num_elem; i++)
13111 dw_loc_descr_ref temp;
13113 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13114 VOIDmode, initialized);
13115 if (temp == NULL)
13116 return NULL;
13117 add_loc_descr (&loc_result, temp);
13118 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13119 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13122 break;
13124 case CONST_INT:
13125 if (mode != VOIDmode && mode != BLKmode)
13126 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13127 INTVAL (rtl));
13128 break;
13130 case CONST_DOUBLE:
13131 if (mode == VOIDmode)
13132 mode = GET_MODE (rtl);
13134 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13136 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13138 /* Note that a CONST_DOUBLE rtx could represent either an integer
13139 or a floating-point constant. A CONST_DOUBLE is used whenever
13140 the constant requires more than one word in order to be
13141 adequately represented. We output CONST_DOUBLEs as blocks. */
13142 loc_result = new_loc_descr (DW_OP_implicit_value,
13143 GET_MODE_SIZE (mode), 0);
13144 if (SCALAR_FLOAT_MODE_P (mode))
13146 unsigned int length = GET_MODE_SIZE (mode);
13147 unsigned char *array
13148 = (unsigned char*) ggc_alloc_atomic (length);
13150 insert_float (rtl, array);
13151 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13152 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13153 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13154 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13156 else
13158 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13159 loc_result->dw_loc_oprnd2.v.val_double
13160 = rtx_to_double_int (rtl);
13163 break;
13165 case CONST_VECTOR:
13166 if (mode == VOIDmode)
13167 mode = GET_MODE (rtl);
13169 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13171 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13172 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13173 unsigned char *array = (unsigned char *)
13174 ggc_alloc_atomic (length * elt_size);
13175 unsigned int i;
13176 unsigned char *p;
13178 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13179 switch (GET_MODE_CLASS (mode))
13181 case MODE_VECTOR_INT:
13182 for (i = 0, p = array; i < length; i++, p += elt_size)
13184 rtx elt = CONST_VECTOR_ELT (rtl, i);
13185 double_int val = rtx_to_double_int (elt);
13187 if (elt_size <= sizeof (HOST_WIDE_INT))
13188 insert_int (val.to_shwi (), elt_size, p);
13189 else
13191 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13192 insert_double (val, p);
13195 break;
13197 case MODE_VECTOR_FLOAT:
13198 for (i = 0, p = array; i < length; i++, p += elt_size)
13200 rtx elt = CONST_VECTOR_ELT (rtl, i);
13201 insert_float (elt, p);
13203 break;
13205 default:
13206 gcc_unreachable ();
13209 loc_result = new_loc_descr (DW_OP_implicit_value,
13210 length * elt_size, 0);
13211 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13212 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13213 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13214 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13216 break;
13218 case CONST:
13219 if (mode == VOIDmode
13220 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13221 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13222 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13224 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13225 break;
13227 /* FALLTHROUGH */
13228 case SYMBOL_REF:
13229 if (!const_ok_for_output (rtl))
13230 break;
13231 case LABEL_REF:
13232 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13233 && (dwarf_version >= 4 || !dwarf_strict))
13235 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13236 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13237 vec_safe_push (used_rtx_array, rtl);
13239 break;
13241 case DEBUG_IMPLICIT_PTR:
13242 loc_result = implicit_ptr_descriptor (rtl, 0);
13243 break;
13245 case PLUS:
13246 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13247 && CONST_INT_P (XEXP (rtl, 1)))
13249 loc_result
13250 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13251 break;
13253 /* FALLTHRU */
13254 do_default:
13255 default:
13256 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13257 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13258 && dwarf_version >= 4)
13259 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13261 /* Value expression. */
13262 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13263 if (loc_result)
13264 add_loc_descr (&loc_result,
13265 new_loc_descr (DW_OP_stack_value, 0, 0));
13267 break;
13270 return loc_result;
13273 /* We need to figure out what section we should use as the base for the
13274 address ranges where a given location is valid.
13275 1. If this particular DECL has a section associated with it, use that.
13276 2. If this function has a section associated with it, use that.
13277 3. Otherwise, use the text section.
13278 XXX: If you split a variable across multiple sections, we won't notice. */
13280 static const char *
13281 secname_for_decl (const_tree decl)
13283 const char *secname;
13285 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13287 tree sectree = DECL_SECTION_NAME (decl);
13288 secname = TREE_STRING_POINTER (sectree);
13290 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13292 tree sectree = DECL_SECTION_NAME (current_function_decl);
13293 secname = TREE_STRING_POINTER (sectree);
13295 else if (cfun && in_cold_section_p)
13296 secname = crtl->subsections.cold_section_label;
13297 else
13298 secname = text_section_label;
13300 return secname;
13303 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13305 static bool
13306 decl_by_reference_p (tree decl)
13308 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13309 || TREE_CODE (decl) == VAR_DECL)
13310 && DECL_BY_REFERENCE (decl));
13313 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13314 for VARLOC. */
13316 static dw_loc_descr_ref
13317 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13318 enum var_init_status initialized)
13320 int have_address = 0;
13321 dw_loc_descr_ref descr;
13322 enum machine_mode mode;
13324 if (want_address != 2)
13326 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13327 /* Single part. */
13328 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13330 varloc = PAT_VAR_LOCATION_LOC (varloc);
13331 if (GET_CODE (varloc) == EXPR_LIST)
13332 varloc = XEXP (varloc, 0);
13333 mode = GET_MODE (varloc);
13334 if (MEM_P (varloc))
13336 rtx addr = XEXP (varloc, 0);
13337 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13338 mode, initialized);
13339 if (descr)
13340 have_address = 1;
13341 else
13343 rtx x = avoid_constant_pool_reference (varloc);
13344 if (x != varloc)
13345 descr = mem_loc_descriptor (x, mode, VOIDmode,
13346 initialized);
13349 else
13350 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13352 else
13353 return 0;
13355 else
13357 if (GET_CODE (varloc) == VAR_LOCATION)
13358 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13359 else
13360 mode = DECL_MODE (loc);
13361 descr = loc_descriptor (varloc, mode, initialized);
13362 have_address = 1;
13365 if (!descr)
13366 return 0;
13368 if (want_address == 2 && !have_address
13369 && (dwarf_version >= 4 || !dwarf_strict))
13371 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13373 expansion_failed (loc, NULL_RTX,
13374 "DWARF address size mismatch");
13375 return 0;
13377 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13378 have_address = 1;
13380 /* Show if we can't fill the request for an address. */
13381 if (want_address && !have_address)
13383 expansion_failed (loc, NULL_RTX,
13384 "Want address and only have value");
13385 return 0;
13388 /* If we've got an address and don't want one, dereference. */
13389 if (!want_address && have_address)
13391 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13392 enum dwarf_location_atom op;
13394 if (size > DWARF2_ADDR_SIZE || size == -1)
13396 expansion_failed (loc, NULL_RTX,
13397 "DWARF address size mismatch");
13398 return 0;
13400 else if (size == DWARF2_ADDR_SIZE)
13401 op = DW_OP_deref;
13402 else
13403 op = DW_OP_deref_size;
13405 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13408 return descr;
13411 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13412 if it is not possible. */
13414 static dw_loc_descr_ref
13415 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13417 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13418 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13419 else if (dwarf_version >= 3 || !dwarf_strict)
13420 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13421 else
13422 return NULL;
13425 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13426 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13428 static dw_loc_descr_ref
13429 dw_sra_loc_expr (tree decl, rtx loc)
13431 rtx p;
13432 unsigned int padsize = 0;
13433 dw_loc_descr_ref descr, *descr_tail;
13434 unsigned HOST_WIDE_INT decl_size;
13435 rtx varloc;
13436 enum var_init_status initialized;
13438 if (DECL_SIZE (decl) == NULL
13439 || !host_integerp (DECL_SIZE (decl), 1))
13440 return NULL;
13442 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13443 descr = NULL;
13444 descr_tail = &descr;
13446 for (p = loc; p; p = XEXP (p, 1))
13448 unsigned int bitsize = decl_piece_bitsize (p);
13449 rtx loc_note = *decl_piece_varloc_ptr (p);
13450 dw_loc_descr_ref cur_descr;
13451 dw_loc_descr_ref *tail, last = NULL;
13452 unsigned int opsize = 0;
13454 if (loc_note == NULL_RTX
13455 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13457 padsize += bitsize;
13458 continue;
13460 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13461 varloc = NOTE_VAR_LOCATION (loc_note);
13462 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13463 if (cur_descr == NULL)
13465 padsize += bitsize;
13466 continue;
13469 /* Check that cur_descr either doesn't use
13470 DW_OP_*piece operations, or their sum is equal
13471 to bitsize. Otherwise we can't embed it. */
13472 for (tail = &cur_descr; *tail != NULL;
13473 tail = &(*tail)->dw_loc_next)
13474 if ((*tail)->dw_loc_opc == DW_OP_piece)
13476 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13477 * BITS_PER_UNIT;
13478 last = *tail;
13480 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13482 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13483 last = *tail;
13486 if (last != NULL && opsize != bitsize)
13488 padsize += bitsize;
13489 continue;
13492 /* If there is a hole, add DW_OP_*piece after empty DWARF
13493 expression, which means that those bits are optimized out. */
13494 if (padsize)
13496 if (padsize > decl_size)
13497 return NULL;
13498 decl_size -= padsize;
13499 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13500 if (*descr_tail == NULL)
13501 return NULL;
13502 descr_tail = &(*descr_tail)->dw_loc_next;
13503 padsize = 0;
13505 *descr_tail = cur_descr;
13506 descr_tail = tail;
13507 if (bitsize > decl_size)
13508 return NULL;
13509 decl_size -= bitsize;
13510 if (last == NULL)
13512 HOST_WIDE_INT offset = 0;
13513 if (GET_CODE (varloc) == VAR_LOCATION
13514 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13516 varloc = PAT_VAR_LOCATION_LOC (varloc);
13517 if (GET_CODE (varloc) == EXPR_LIST)
13518 varloc = XEXP (varloc, 0);
13522 if (GET_CODE (varloc) == CONST
13523 || GET_CODE (varloc) == SIGN_EXTEND
13524 || GET_CODE (varloc) == ZERO_EXTEND)
13525 varloc = XEXP (varloc, 0);
13526 else if (GET_CODE (varloc) == SUBREG)
13527 varloc = SUBREG_REG (varloc);
13528 else
13529 break;
13531 while (1);
13532 /* DW_OP_bit_size offset should be zero for register
13533 or implicit location descriptions and empty location
13534 descriptions, but for memory addresses needs big endian
13535 adjustment. */
13536 if (MEM_P (varloc))
13538 unsigned HOST_WIDE_INT memsize
13539 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13540 if (memsize != bitsize)
13542 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13543 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13544 return NULL;
13545 if (memsize < bitsize)
13546 return NULL;
13547 if (BITS_BIG_ENDIAN)
13548 offset = memsize - bitsize;
13552 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13553 if (*descr_tail == NULL)
13554 return NULL;
13555 descr_tail = &(*descr_tail)->dw_loc_next;
13559 /* If there were any non-empty expressions, add padding till the end of
13560 the decl. */
13561 if (descr != NULL && decl_size != 0)
13563 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13564 if (*descr_tail == NULL)
13565 return NULL;
13567 return descr;
13570 /* Return the dwarf representation of the location list LOC_LIST of
13571 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13572 function. */
13574 static dw_loc_list_ref
13575 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13577 const char *endname, *secname;
13578 rtx varloc;
13579 enum var_init_status initialized;
13580 struct var_loc_node *node;
13581 dw_loc_descr_ref descr;
13582 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13583 dw_loc_list_ref list = NULL;
13584 dw_loc_list_ref *listp = &list;
13586 /* Now that we know what section we are using for a base,
13587 actually construct the list of locations.
13588 The first location information is what is passed to the
13589 function that creates the location list, and the remaining
13590 locations just get added on to that list.
13591 Note that we only know the start address for a location
13592 (IE location changes), so to build the range, we use
13593 the range [current location start, next location start].
13594 This means we have to special case the last node, and generate
13595 a range of [last location start, end of function label]. */
13597 secname = secname_for_decl (decl);
13599 for (node = loc_list->first; node; node = node->next)
13600 if (GET_CODE (node->loc) == EXPR_LIST
13601 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13603 if (GET_CODE (node->loc) == EXPR_LIST)
13605 /* This requires DW_OP_{,bit_}piece, which is not usable
13606 inside DWARF expressions. */
13607 if (want_address != 2)
13608 continue;
13609 descr = dw_sra_loc_expr (decl, node->loc);
13610 if (descr == NULL)
13611 continue;
13613 else
13615 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13616 varloc = NOTE_VAR_LOCATION (node->loc);
13617 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13619 if (descr)
13621 bool range_across_switch = false;
13622 /* If section switch happens in between node->label
13623 and node->next->label (or end of function) and
13624 we can't emit it as a single entry list,
13625 emit two ranges, first one ending at the end
13626 of first partition and second one starting at the
13627 beginning of second partition. */
13628 if (node == loc_list->last_before_switch
13629 && (node != loc_list->first || loc_list->first->next)
13630 && current_function_decl)
13632 endname = cfun->fde->dw_fde_end;
13633 range_across_switch = true;
13635 /* The variable has a location between NODE->LABEL and
13636 NODE->NEXT->LABEL. */
13637 else if (node->next)
13638 endname = node->next->label;
13639 /* If the variable has a location at the last label
13640 it keeps its location until the end of function. */
13641 else if (!current_function_decl)
13642 endname = text_end_label;
13643 else
13645 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13646 current_function_funcdef_no);
13647 endname = ggc_strdup (label_id);
13650 *listp = new_loc_list (descr, node->label, endname, secname);
13651 if (TREE_CODE (decl) == PARM_DECL
13652 && node == loc_list->first
13653 && NOTE_P (node->loc)
13654 && strcmp (node->label, endname) == 0)
13655 (*listp)->force = true;
13656 listp = &(*listp)->dw_loc_next;
13658 if (range_across_switch)
13660 if (GET_CODE (node->loc) == EXPR_LIST)
13661 descr = dw_sra_loc_expr (decl, node->loc);
13662 else
13664 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13665 varloc = NOTE_VAR_LOCATION (node->loc);
13666 descr = dw_loc_list_1 (decl, varloc, want_address,
13667 initialized);
13669 gcc_assert (descr);
13670 /* The variable has a location between NODE->LABEL and
13671 NODE->NEXT->LABEL. */
13672 if (node->next)
13673 endname = node->next->label;
13674 else
13675 endname = cfun->fde->dw_fde_second_end;
13676 *listp = new_loc_list (descr,
13677 cfun->fde->dw_fde_second_begin,
13678 endname, secname);
13679 listp = &(*listp)->dw_loc_next;
13684 /* Try to avoid the overhead of a location list emitting a location
13685 expression instead, but only if we didn't have more than one
13686 location entry in the first place. If some entries were not
13687 representable, we don't want to pretend a single entry that was
13688 applies to the entire scope in which the variable is
13689 available. */
13690 if (list && loc_list->first->next)
13691 gen_llsym (list);
13693 return list;
13696 /* Return if the loc_list has only single element and thus can be represented
13697 as location description. */
13699 static bool
13700 single_element_loc_list_p (dw_loc_list_ref list)
13702 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13703 return !list->ll_symbol;
13706 /* To each location in list LIST add loc descr REF. */
13708 static void
13709 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13711 dw_loc_descr_ref copy;
13712 add_loc_descr (&list->expr, ref);
13713 list = list->dw_loc_next;
13714 while (list)
13716 copy = ggc_alloc_dw_loc_descr_node ();
13717 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13718 add_loc_descr (&list->expr, copy);
13719 while (copy->dw_loc_next)
13721 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13722 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13723 copy->dw_loc_next = new_copy;
13724 copy = new_copy;
13726 list = list->dw_loc_next;
13730 /* Given two lists RET and LIST
13731 produce location list that is result of adding expression in LIST
13732 to expression in RET on each position in program.
13733 Might be destructive on both RET and LIST.
13735 TODO: We handle only simple cases of RET or LIST having at most one
13736 element. General case would inolve sorting the lists in program order
13737 and merging them that will need some additional work.
13738 Adding that will improve quality of debug info especially for SRA-ed
13739 structures. */
13741 static void
13742 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13744 if (!list)
13745 return;
13746 if (!*ret)
13748 *ret = list;
13749 return;
13751 if (!list->dw_loc_next)
13753 add_loc_descr_to_each (*ret, list->expr);
13754 return;
13756 if (!(*ret)->dw_loc_next)
13758 add_loc_descr_to_each (list, (*ret)->expr);
13759 *ret = list;
13760 return;
13762 expansion_failed (NULL_TREE, NULL_RTX,
13763 "Don't know how to merge two non-trivial"
13764 " location lists.\n");
13765 *ret = NULL;
13766 return;
13769 /* LOC is constant expression. Try a luck, look it up in constant
13770 pool and return its loc_descr of its address. */
13772 static dw_loc_descr_ref
13773 cst_pool_loc_descr (tree loc)
13775 /* Get an RTL for this, if something has been emitted. */
13776 rtx rtl = lookup_constant_def (loc);
13778 if (!rtl || !MEM_P (rtl))
13780 gcc_assert (!rtl);
13781 return 0;
13783 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13785 /* TODO: We might get more coverage if we was actually delaying expansion
13786 of all expressions till end of compilation when constant pools are fully
13787 populated. */
13788 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13790 expansion_failed (loc, NULL_RTX,
13791 "CST value in contant pool but not marked.");
13792 return 0;
13794 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13795 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13798 /* Return dw_loc_list representing address of addr_expr LOC
13799 by looking for inner INDIRECT_REF expression and turning
13800 it into simple arithmetics. */
13802 static dw_loc_list_ref
13803 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13805 tree obj, offset;
13806 HOST_WIDE_INT bitsize, bitpos, bytepos;
13807 enum machine_mode mode;
13808 int unsignedp, volatilep = 0;
13809 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13811 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13812 &bitsize, &bitpos, &offset, &mode,
13813 &unsignedp, &volatilep, false);
13814 STRIP_NOPS (obj);
13815 if (bitpos % BITS_PER_UNIT)
13817 expansion_failed (loc, NULL_RTX, "bitfield access");
13818 return 0;
13820 if (!INDIRECT_REF_P (obj))
13822 expansion_failed (obj,
13823 NULL_RTX, "no indirect ref in inner refrence");
13824 return 0;
13826 if (!offset && !bitpos)
13827 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13828 else if (toplev
13829 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13830 && (dwarf_version >= 4 || !dwarf_strict))
13832 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13833 if (!list_ret)
13834 return 0;
13835 if (offset)
13837 /* Variable offset. */
13838 list_ret1 = loc_list_from_tree (offset, 0);
13839 if (list_ret1 == 0)
13840 return 0;
13841 add_loc_list (&list_ret, list_ret1);
13842 if (!list_ret)
13843 return 0;
13844 add_loc_descr_to_each (list_ret,
13845 new_loc_descr (DW_OP_plus, 0, 0));
13847 bytepos = bitpos / BITS_PER_UNIT;
13848 if (bytepos > 0)
13849 add_loc_descr_to_each (list_ret,
13850 new_loc_descr (DW_OP_plus_uconst,
13851 bytepos, 0));
13852 else if (bytepos < 0)
13853 loc_list_plus_const (list_ret, bytepos);
13854 add_loc_descr_to_each (list_ret,
13855 new_loc_descr (DW_OP_stack_value, 0, 0));
13857 return list_ret;
13861 /* Generate Dwarf location list representing LOC.
13862 If WANT_ADDRESS is false, expression computing LOC will be computed
13863 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13864 if WANT_ADDRESS is 2, expression computing address useable in location
13865 will be returned (i.e. DW_OP_reg can be used
13866 to refer to register values). */
13868 static dw_loc_list_ref
13869 loc_list_from_tree (tree loc, int want_address)
13871 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13872 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13873 int have_address = 0;
13874 enum dwarf_location_atom op;
13876 /* ??? Most of the time we do not take proper care for sign/zero
13877 extending the values properly. Hopefully this won't be a real
13878 problem... */
13880 switch (TREE_CODE (loc))
13882 case ERROR_MARK:
13883 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13884 return 0;
13886 case PLACEHOLDER_EXPR:
13887 /* This case involves extracting fields from an object to determine the
13888 position of other fields. We don't try to encode this here. The
13889 only user of this is Ada, which encodes the needed information using
13890 the names of types. */
13891 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13892 return 0;
13894 case CALL_EXPR:
13895 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13896 /* There are no opcodes for these operations. */
13897 return 0;
13899 case PREINCREMENT_EXPR:
13900 case PREDECREMENT_EXPR:
13901 case POSTINCREMENT_EXPR:
13902 case POSTDECREMENT_EXPR:
13903 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13904 /* There are no opcodes for these operations. */
13905 return 0;
13907 case ADDR_EXPR:
13908 /* If we already want an address, see if there is INDIRECT_REF inside
13909 e.g. for &this->field. */
13910 if (want_address)
13912 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13913 (loc, want_address == 2);
13914 if (list_ret)
13915 have_address = 1;
13916 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13917 && (ret = cst_pool_loc_descr (loc)))
13918 have_address = 1;
13920 /* Otherwise, process the argument and look for the address. */
13921 if (!list_ret && !ret)
13922 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13923 else
13925 if (want_address)
13926 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13927 return NULL;
13929 break;
13931 case VAR_DECL:
13932 if (DECL_THREAD_LOCAL_P (loc))
13934 rtx rtl;
13935 enum dwarf_location_atom tls_op;
13936 enum dtprel_bool dtprel = dtprel_false;
13938 if (targetm.have_tls)
13940 /* If this is not defined, we have no way to emit the
13941 data. */
13942 if (!targetm.asm_out.output_dwarf_dtprel)
13943 return 0;
13945 /* The way DW_OP_GNU_push_tls_address is specified, we
13946 can only look up addresses of objects in the current
13947 module. We used DW_OP_addr as first op, but that's
13948 wrong, because DW_OP_addr is relocated by the debug
13949 info consumer, while DW_OP_GNU_push_tls_address
13950 operand shouldn't be. */
13951 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13952 return 0;
13953 dtprel = dtprel_true;
13954 tls_op = DW_OP_GNU_push_tls_address;
13956 else
13958 if (!targetm.emutls.debug_form_tls_address
13959 || !(dwarf_version >= 3 || !dwarf_strict))
13960 return 0;
13961 /* We stuffed the control variable into the DECL_VALUE_EXPR
13962 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13963 no longer appear in gimple code. We used the control
13964 variable in specific so that we could pick it up here. */
13965 loc = DECL_VALUE_EXPR (loc);
13966 tls_op = DW_OP_form_tls_address;
13969 rtl = rtl_for_decl_location (loc);
13970 if (rtl == NULL_RTX)
13971 return 0;
13973 if (!MEM_P (rtl))
13974 return 0;
13975 rtl = XEXP (rtl, 0);
13976 if (! CONSTANT_P (rtl))
13977 return 0;
13979 ret = new_addr_loc_descr (rtl, dtprel);
13980 ret1 = new_loc_descr (tls_op, 0, 0);
13981 add_loc_descr (&ret, ret1);
13983 have_address = 1;
13984 break;
13986 /* FALLTHRU */
13988 case PARM_DECL:
13989 case RESULT_DECL:
13990 if (DECL_HAS_VALUE_EXPR_P (loc))
13991 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13992 want_address);
13993 /* FALLTHRU */
13995 case FUNCTION_DECL:
13997 rtx rtl;
13998 var_loc_list *loc_list = lookup_decl_loc (loc);
14000 if (loc_list && loc_list->first)
14002 list_ret = dw_loc_list (loc_list, loc, want_address);
14003 have_address = want_address != 0;
14004 break;
14006 rtl = rtl_for_decl_location (loc);
14007 if (rtl == NULL_RTX)
14009 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14010 return 0;
14012 else if (CONST_INT_P (rtl))
14014 HOST_WIDE_INT val = INTVAL (rtl);
14015 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14016 val &= GET_MODE_MASK (DECL_MODE (loc));
14017 ret = int_loc_descriptor (val);
14019 else if (GET_CODE (rtl) == CONST_STRING)
14021 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14022 return 0;
14024 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14025 ret = new_addr_loc_descr (rtl, dtprel_false);
14026 else
14028 enum machine_mode mode, mem_mode;
14030 /* Certain constructs can only be represented at top-level. */
14031 if (want_address == 2)
14033 ret = loc_descriptor (rtl, VOIDmode,
14034 VAR_INIT_STATUS_INITIALIZED);
14035 have_address = 1;
14037 else
14039 mode = GET_MODE (rtl);
14040 mem_mode = VOIDmode;
14041 if (MEM_P (rtl))
14043 mem_mode = mode;
14044 mode = get_address_mode (rtl);
14045 rtl = XEXP (rtl, 0);
14046 have_address = 1;
14048 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14049 VAR_INIT_STATUS_INITIALIZED);
14051 if (!ret)
14052 expansion_failed (loc, rtl,
14053 "failed to produce loc descriptor for rtl");
14056 break;
14058 case MEM_REF:
14059 /* ??? FIXME. */
14060 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14061 return 0;
14062 /* Fallthru. */
14063 case INDIRECT_REF:
14064 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14065 have_address = 1;
14066 break;
14068 case COMPOUND_EXPR:
14069 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14071 CASE_CONVERT:
14072 case VIEW_CONVERT_EXPR:
14073 case SAVE_EXPR:
14074 case MODIFY_EXPR:
14075 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14077 case COMPONENT_REF:
14078 case BIT_FIELD_REF:
14079 case ARRAY_REF:
14080 case ARRAY_RANGE_REF:
14081 case REALPART_EXPR:
14082 case IMAGPART_EXPR:
14084 tree obj, offset;
14085 HOST_WIDE_INT bitsize, bitpos, bytepos;
14086 enum machine_mode mode;
14087 int unsignedp, volatilep = 0;
14089 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14090 &unsignedp, &volatilep, false);
14092 gcc_assert (obj != loc);
14094 list_ret = loc_list_from_tree (obj,
14095 want_address == 2
14096 && !bitpos && !offset ? 2 : 1);
14097 /* TODO: We can extract value of the small expression via shifting even
14098 for nonzero bitpos. */
14099 if (list_ret == 0)
14100 return 0;
14101 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14103 expansion_failed (loc, NULL_RTX,
14104 "bitfield access");
14105 return 0;
14108 if (offset != NULL_TREE)
14110 /* Variable offset. */
14111 list_ret1 = loc_list_from_tree (offset, 0);
14112 if (list_ret1 == 0)
14113 return 0;
14114 add_loc_list (&list_ret, list_ret1);
14115 if (!list_ret)
14116 return 0;
14117 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14120 bytepos = bitpos / BITS_PER_UNIT;
14121 if (bytepos > 0)
14122 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14123 else if (bytepos < 0)
14124 loc_list_plus_const (list_ret, bytepos);
14126 have_address = 1;
14127 break;
14130 case INTEGER_CST:
14131 if ((want_address || !host_integerp (loc, 0))
14132 && (ret = cst_pool_loc_descr (loc)))
14133 have_address = 1;
14134 else if (want_address == 2
14135 && host_integerp (loc, 0)
14136 && (ret = address_of_int_loc_descriptor
14137 (int_size_in_bytes (TREE_TYPE (loc)),
14138 tree_low_cst (loc, 0))))
14139 have_address = 1;
14140 else if (host_integerp (loc, 0))
14141 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14142 else
14144 expansion_failed (loc, NULL_RTX,
14145 "Integer operand is not host integer");
14146 return 0;
14148 break;
14150 case CONSTRUCTOR:
14151 case REAL_CST:
14152 case STRING_CST:
14153 case COMPLEX_CST:
14154 if ((ret = cst_pool_loc_descr (loc)))
14155 have_address = 1;
14156 else
14157 /* We can construct small constants here using int_loc_descriptor. */
14158 expansion_failed (loc, NULL_RTX,
14159 "constructor or constant not in constant pool");
14160 break;
14162 case TRUTH_AND_EXPR:
14163 case TRUTH_ANDIF_EXPR:
14164 case BIT_AND_EXPR:
14165 op = DW_OP_and;
14166 goto do_binop;
14168 case TRUTH_XOR_EXPR:
14169 case BIT_XOR_EXPR:
14170 op = DW_OP_xor;
14171 goto do_binop;
14173 case TRUTH_OR_EXPR:
14174 case TRUTH_ORIF_EXPR:
14175 case BIT_IOR_EXPR:
14176 op = DW_OP_or;
14177 goto do_binop;
14179 case FLOOR_DIV_EXPR:
14180 case CEIL_DIV_EXPR:
14181 case ROUND_DIV_EXPR:
14182 case TRUNC_DIV_EXPR:
14183 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14184 return 0;
14185 op = DW_OP_div;
14186 goto do_binop;
14188 case MINUS_EXPR:
14189 op = DW_OP_minus;
14190 goto do_binop;
14192 case FLOOR_MOD_EXPR:
14193 case CEIL_MOD_EXPR:
14194 case ROUND_MOD_EXPR:
14195 case TRUNC_MOD_EXPR:
14196 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14198 op = DW_OP_mod;
14199 goto do_binop;
14201 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14202 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14203 if (list_ret == 0 || list_ret1 == 0)
14204 return 0;
14206 add_loc_list (&list_ret, list_ret1);
14207 if (list_ret == 0)
14208 return 0;
14209 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14210 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14211 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14212 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14213 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14214 break;
14216 case MULT_EXPR:
14217 op = DW_OP_mul;
14218 goto do_binop;
14220 case LSHIFT_EXPR:
14221 op = DW_OP_shl;
14222 goto do_binop;
14224 case RSHIFT_EXPR:
14225 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14226 goto do_binop;
14228 case POINTER_PLUS_EXPR:
14229 case PLUS_EXPR:
14230 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14232 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14233 if (list_ret == 0)
14234 return 0;
14236 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14237 break;
14240 op = DW_OP_plus;
14241 goto do_binop;
14243 case LE_EXPR:
14244 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14245 return 0;
14247 op = DW_OP_le;
14248 goto do_binop;
14250 case GE_EXPR:
14251 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14252 return 0;
14254 op = DW_OP_ge;
14255 goto do_binop;
14257 case LT_EXPR:
14258 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14259 return 0;
14261 op = DW_OP_lt;
14262 goto do_binop;
14264 case GT_EXPR:
14265 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14266 return 0;
14268 op = DW_OP_gt;
14269 goto do_binop;
14271 case EQ_EXPR:
14272 op = DW_OP_eq;
14273 goto do_binop;
14275 case NE_EXPR:
14276 op = DW_OP_ne;
14277 goto do_binop;
14279 do_binop:
14280 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14281 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14282 if (list_ret == 0 || list_ret1 == 0)
14283 return 0;
14285 add_loc_list (&list_ret, list_ret1);
14286 if (list_ret == 0)
14287 return 0;
14288 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14289 break;
14291 case TRUTH_NOT_EXPR:
14292 case BIT_NOT_EXPR:
14293 op = DW_OP_not;
14294 goto do_unop;
14296 case ABS_EXPR:
14297 op = DW_OP_abs;
14298 goto do_unop;
14300 case NEGATE_EXPR:
14301 op = DW_OP_neg;
14302 goto do_unop;
14304 do_unop:
14305 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14306 if (list_ret == 0)
14307 return 0;
14309 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14310 break;
14312 case MIN_EXPR:
14313 case MAX_EXPR:
14315 const enum tree_code code =
14316 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14318 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14319 build2 (code, integer_type_node,
14320 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14321 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14324 /* ... fall through ... */
14326 case COND_EXPR:
14328 dw_loc_descr_ref lhs
14329 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14330 dw_loc_list_ref rhs
14331 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14332 dw_loc_descr_ref bra_node, jump_node, tmp;
14334 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14335 if (list_ret == 0 || lhs == 0 || rhs == 0)
14336 return 0;
14338 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14339 add_loc_descr_to_each (list_ret, bra_node);
14341 add_loc_list (&list_ret, rhs);
14342 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14343 add_loc_descr_to_each (list_ret, jump_node);
14345 add_loc_descr_to_each (list_ret, lhs);
14346 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14347 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14349 /* ??? Need a node to point the skip at. Use a nop. */
14350 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14351 add_loc_descr_to_each (list_ret, tmp);
14352 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14353 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14355 break;
14357 case FIX_TRUNC_EXPR:
14358 return 0;
14360 default:
14361 /* Leave front-end specific codes as simply unknown. This comes
14362 up, for instance, with the C STMT_EXPR. */
14363 if ((unsigned int) TREE_CODE (loc)
14364 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14366 expansion_failed (loc, NULL_RTX,
14367 "language specific tree node");
14368 return 0;
14371 #ifdef ENABLE_CHECKING
14372 /* Otherwise this is a generic code; we should just lists all of
14373 these explicitly. We forgot one. */
14374 gcc_unreachable ();
14375 #else
14376 /* In a release build, we want to degrade gracefully: better to
14377 generate incomplete debugging information than to crash. */
14378 return NULL;
14379 #endif
14382 if (!ret && !list_ret)
14383 return 0;
14385 if (want_address == 2 && !have_address
14386 && (dwarf_version >= 4 || !dwarf_strict))
14388 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14390 expansion_failed (loc, NULL_RTX,
14391 "DWARF address size mismatch");
14392 return 0;
14394 if (ret)
14395 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14396 else
14397 add_loc_descr_to_each (list_ret,
14398 new_loc_descr (DW_OP_stack_value, 0, 0));
14399 have_address = 1;
14401 /* Show if we can't fill the request for an address. */
14402 if (want_address && !have_address)
14404 expansion_failed (loc, NULL_RTX,
14405 "Want address and only have value");
14406 return 0;
14409 gcc_assert (!ret || !list_ret);
14411 /* If we've got an address and don't want one, dereference. */
14412 if (!want_address && have_address)
14414 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14416 if (size > DWARF2_ADDR_SIZE || size == -1)
14418 expansion_failed (loc, NULL_RTX,
14419 "DWARF address size mismatch");
14420 return 0;
14422 else if (size == DWARF2_ADDR_SIZE)
14423 op = DW_OP_deref;
14424 else
14425 op = DW_OP_deref_size;
14427 if (ret)
14428 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14429 else
14430 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14432 if (ret)
14433 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14435 return list_ret;
14438 /* Same as above but return only single location expression. */
14439 static dw_loc_descr_ref
14440 loc_descriptor_from_tree (tree loc, int want_address)
14442 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14443 if (!ret)
14444 return NULL;
14445 if (ret->dw_loc_next)
14447 expansion_failed (loc, NULL_RTX,
14448 "Location list where only loc descriptor needed");
14449 return NULL;
14451 return ret->expr;
14454 /* Given a value, round it up to the lowest multiple of `boundary'
14455 which is not less than the value itself. */
14457 static inline HOST_WIDE_INT
14458 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14460 return (((value + boundary - 1) / boundary) * boundary);
14463 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14464 pointer to the declared type for the relevant field variable, or return
14465 `integer_type_node' if the given node turns out to be an
14466 ERROR_MARK node. */
14468 static inline tree
14469 field_type (const_tree decl)
14471 tree type;
14473 if (TREE_CODE (decl) == ERROR_MARK)
14474 return integer_type_node;
14476 type = DECL_BIT_FIELD_TYPE (decl);
14477 if (type == NULL_TREE)
14478 type = TREE_TYPE (decl);
14480 return type;
14483 /* Given a pointer to a tree node, return the alignment in bits for
14484 it, or else return BITS_PER_WORD if the node actually turns out to
14485 be an ERROR_MARK node. */
14487 static inline unsigned
14488 simple_type_align_in_bits (const_tree type)
14490 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14493 static inline unsigned
14494 simple_decl_align_in_bits (const_tree decl)
14496 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14499 /* Return the result of rounding T up to ALIGN. */
14501 static inline double_int
14502 round_up_to_align (double_int t, unsigned int align)
14504 double_int alignd = double_int::from_uhwi (align);
14505 t += alignd;
14506 t += double_int_minus_one;
14507 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14508 t *= alignd;
14509 return t;
14512 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14513 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14514 or return 0 if we are unable to determine what that offset is, either
14515 because the argument turns out to be a pointer to an ERROR_MARK node, or
14516 because the offset is actually variable. (We can't handle the latter case
14517 just yet). */
14519 static HOST_WIDE_INT
14520 field_byte_offset (const_tree decl)
14522 double_int object_offset_in_bits;
14523 double_int object_offset_in_bytes;
14524 double_int bitpos_int;
14526 if (TREE_CODE (decl) == ERROR_MARK)
14527 return 0;
14529 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14531 /* We cannot yet cope with fields whose positions are variable, so
14532 for now, when we see such things, we simply return 0. Someday, we may
14533 be able to handle such cases, but it will be damn difficult. */
14534 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14535 return 0;
14537 bitpos_int = tree_to_double_int (bit_position (decl));
14539 #ifdef PCC_BITFIELD_TYPE_MATTERS
14540 if (PCC_BITFIELD_TYPE_MATTERS)
14542 tree type;
14543 tree field_size_tree;
14544 double_int deepest_bitpos;
14545 double_int field_size_in_bits;
14546 unsigned int type_align_in_bits;
14547 unsigned int decl_align_in_bits;
14548 double_int type_size_in_bits;
14550 type = field_type (decl);
14551 type_size_in_bits = double_int_type_size_in_bits (type);
14552 type_align_in_bits = simple_type_align_in_bits (type);
14554 field_size_tree = DECL_SIZE (decl);
14556 /* The size could be unspecified if there was an error, or for
14557 a flexible array member. */
14558 if (!field_size_tree)
14559 field_size_tree = bitsize_zero_node;
14561 /* If the size of the field is not constant, use the type size. */
14562 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14563 field_size_in_bits = tree_to_double_int (field_size_tree);
14564 else
14565 field_size_in_bits = type_size_in_bits;
14567 decl_align_in_bits = simple_decl_align_in_bits (decl);
14569 /* The GCC front-end doesn't make any attempt to keep track of the
14570 starting bit offset (relative to the start of the containing
14571 structure type) of the hypothetical "containing object" for a
14572 bit-field. Thus, when computing the byte offset value for the
14573 start of the "containing object" of a bit-field, we must deduce
14574 this information on our own. This can be rather tricky to do in
14575 some cases. For example, handling the following structure type
14576 definition when compiling for an i386/i486 target (which only
14577 aligns long long's to 32-bit boundaries) can be very tricky:
14579 struct S { int field1; long long field2:31; };
14581 Fortunately, there is a simple rule-of-thumb which can be used
14582 in such cases. When compiling for an i386/i486, GCC will
14583 allocate 8 bytes for the structure shown above. It decides to
14584 do this based upon one simple rule for bit-field allocation.
14585 GCC allocates each "containing object" for each bit-field at
14586 the first (i.e. lowest addressed) legitimate alignment boundary
14587 (based upon the required minimum alignment for the declared
14588 type of the field) which it can possibly use, subject to the
14589 condition that there is still enough available space remaining
14590 in the containing object (when allocated at the selected point)
14591 to fully accommodate all of the bits of the bit-field itself.
14593 This simple rule makes it obvious why GCC allocates 8 bytes for
14594 each object of the structure type shown above. When looking
14595 for a place to allocate the "containing object" for `field2',
14596 the compiler simply tries to allocate a 64-bit "containing
14597 object" at each successive 32-bit boundary (starting at zero)
14598 until it finds a place to allocate that 64- bit field such that
14599 at least 31 contiguous (and previously unallocated) bits remain
14600 within that selected 64 bit field. (As it turns out, for the
14601 example above, the compiler finds it is OK to allocate the
14602 "containing object" 64-bit field at bit-offset zero within the
14603 structure type.)
14605 Here we attempt to work backwards from the limited set of facts
14606 we're given, and we try to deduce from those facts, where GCC
14607 must have believed that the containing object started (within
14608 the structure type). The value we deduce is then used (by the
14609 callers of this routine) to generate DW_AT_location and
14610 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14611 the case of DW_AT_location, regular fields as well). */
14613 /* Figure out the bit-distance from the start of the structure to
14614 the "deepest" bit of the bit-field. */
14615 deepest_bitpos = bitpos_int + field_size_in_bits;
14617 /* This is the tricky part. Use some fancy footwork to deduce
14618 where the lowest addressed bit of the containing object must
14619 be. */
14620 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14622 /* Round up to type_align by default. This works best for
14623 bitfields. */
14624 object_offset_in_bits
14625 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14627 if (object_offset_in_bits.ugt (bitpos_int))
14629 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14631 /* Round up to decl_align instead. */
14632 object_offset_in_bits
14633 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14636 else
14637 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14638 object_offset_in_bits = bitpos_int;
14640 object_offset_in_bytes
14641 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14642 true, TRUNC_DIV_EXPR);
14643 return object_offset_in_bytes.to_shwi ();
14646 /* The following routines define various Dwarf attributes and any data
14647 associated with them. */
14649 /* Add a location description attribute value to a DIE.
14651 This emits location attributes suitable for whole variables and
14652 whole parameters. Note that the location attributes for struct fields are
14653 generated by the routine `data_member_location_attribute' below. */
14655 static inline void
14656 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14657 dw_loc_list_ref descr)
14659 if (descr == 0)
14660 return;
14661 if (single_element_loc_list_p (descr))
14662 add_AT_loc (die, attr_kind, descr->expr);
14663 else
14664 add_AT_loc_list (die, attr_kind, descr);
14667 /* Add DW_AT_accessibility attribute to DIE if needed. */
14669 static void
14670 add_accessibility_attribute (dw_die_ref die, tree decl)
14672 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14673 children, otherwise the default is DW_ACCESS_public. In DWARF2
14674 the default has always been DW_ACCESS_public. */
14675 if (TREE_PROTECTED (decl))
14676 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14677 else if (TREE_PRIVATE (decl))
14679 if (dwarf_version == 2
14680 || die->die_parent == NULL
14681 || die->die_parent->die_tag != DW_TAG_class_type)
14682 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14684 else if (dwarf_version > 2
14685 && die->die_parent
14686 && die->die_parent->die_tag == DW_TAG_class_type)
14687 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14690 /* Attach the specialized form of location attribute used for data members of
14691 struct and union types. In the special case of a FIELD_DECL node which
14692 represents a bit-field, the "offset" part of this special location
14693 descriptor must indicate the distance in bytes from the lowest-addressed
14694 byte of the containing struct or union type to the lowest-addressed byte of
14695 the "containing object" for the bit-field. (See the `field_byte_offset'
14696 function above).
14698 For any given bit-field, the "containing object" is a hypothetical object
14699 (of some integral or enum type) within which the given bit-field lives. The
14700 type of this hypothetical "containing object" is always the same as the
14701 declared type of the individual bit-field itself (for GCC anyway... the
14702 DWARF spec doesn't actually mandate this). Note that it is the size (in
14703 bytes) of the hypothetical "containing object" which will be given in the
14704 DW_AT_byte_size attribute for this bit-field. (See the
14705 `byte_size_attribute' function below.) It is also used when calculating the
14706 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14707 function below.) */
14709 static void
14710 add_data_member_location_attribute (dw_die_ref die, tree decl)
14712 HOST_WIDE_INT offset;
14713 dw_loc_descr_ref loc_descr = 0;
14715 if (TREE_CODE (decl) == TREE_BINFO)
14717 /* We're working on the TAG_inheritance for a base class. */
14718 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14720 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14721 aren't at a fixed offset from all (sub)objects of the same
14722 type. We need to extract the appropriate offset from our
14723 vtable. The following dwarf expression means
14725 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14727 This is specific to the V3 ABI, of course. */
14729 dw_loc_descr_ref tmp;
14731 /* Make a copy of the object address. */
14732 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14733 add_loc_descr (&loc_descr, tmp);
14735 /* Extract the vtable address. */
14736 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14737 add_loc_descr (&loc_descr, tmp);
14739 /* Calculate the address of the offset. */
14740 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14741 gcc_assert (offset < 0);
14743 tmp = int_loc_descriptor (-offset);
14744 add_loc_descr (&loc_descr, tmp);
14745 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14746 add_loc_descr (&loc_descr, tmp);
14748 /* Extract the offset. */
14749 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14750 add_loc_descr (&loc_descr, tmp);
14752 /* Add it to the object address. */
14753 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14754 add_loc_descr (&loc_descr, tmp);
14756 else
14757 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14759 else
14760 offset = field_byte_offset (decl);
14762 if (! loc_descr)
14764 if (dwarf_version > 2)
14766 /* Don't need to output a location expression, just the constant. */
14767 if (offset < 0)
14768 add_AT_int (die, DW_AT_data_member_location, offset);
14769 else
14770 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14771 return;
14773 else
14775 enum dwarf_location_atom op;
14777 /* The DWARF2 standard says that we should assume that the structure
14778 address is already on the stack, so we can specify a structure
14779 field address by using DW_OP_plus_uconst. */
14780 op = DW_OP_plus_uconst;
14781 loc_descr = new_loc_descr (op, offset, 0);
14785 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14788 /* Writes integer values to dw_vec_const array. */
14790 static void
14791 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14793 while (size != 0)
14795 *dest++ = val & 0xff;
14796 val >>= 8;
14797 --size;
14801 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14803 static HOST_WIDE_INT
14804 extract_int (const unsigned char *src, unsigned int size)
14806 HOST_WIDE_INT val = 0;
14808 src += size;
14809 while (size != 0)
14811 val <<= 8;
14812 val |= *--src & 0xff;
14813 --size;
14815 return val;
14818 /* Writes double_int values to dw_vec_const array. */
14820 static void
14821 insert_double (double_int val, unsigned char *dest)
14823 unsigned char *p0 = dest;
14824 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14826 if (WORDS_BIG_ENDIAN)
14828 p0 = p1;
14829 p1 = dest;
14832 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14833 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14836 /* Writes floating point values to dw_vec_const array. */
14838 static void
14839 insert_float (const_rtx rtl, unsigned char *array)
14841 REAL_VALUE_TYPE rv;
14842 long val[4];
14843 int i;
14845 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14846 real_to_target (val, &rv, GET_MODE (rtl));
14848 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14849 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14851 insert_int (val[i], 4, array);
14852 array += 4;
14856 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14857 does not have a "location" either in memory or in a register. These
14858 things can arise in GNU C when a constant is passed as an actual parameter
14859 to an inlined function. They can also arise in C++ where declared
14860 constants do not necessarily get memory "homes". */
14862 static bool
14863 add_const_value_attribute (dw_die_ref die, rtx rtl)
14865 switch (GET_CODE (rtl))
14867 case CONST_INT:
14869 HOST_WIDE_INT val = INTVAL (rtl);
14871 if (val < 0)
14872 add_AT_int (die, DW_AT_const_value, val);
14873 else
14874 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14876 return true;
14878 case CONST_DOUBLE:
14879 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14880 floating-point constant. A CONST_DOUBLE is used whenever the
14881 constant requires more than one word in order to be adequately
14882 represented. */
14884 enum machine_mode mode = GET_MODE (rtl);
14886 if (SCALAR_FLOAT_MODE_P (mode))
14888 unsigned int length = GET_MODE_SIZE (mode);
14889 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14891 insert_float (rtl, array);
14892 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14894 else
14895 add_AT_double (die, DW_AT_const_value,
14896 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14898 return true;
14900 case CONST_VECTOR:
14902 enum machine_mode mode = GET_MODE (rtl);
14903 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14904 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14905 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14906 (length * elt_size);
14907 unsigned int i;
14908 unsigned char *p;
14910 switch (GET_MODE_CLASS (mode))
14912 case MODE_VECTOR_INT:
14913 for (i = 0, p = array; i < length; i++, p += elt_size)
14915 rtx elt = CONST_VECTOR_ELT (rtl, i);
14916 double_int val = rtx_to_double_int (elt);
14918 if (elt_size <= sizeof (HOST_WIDE_INT))
14919 insert_int (val.to_shwi (), elt_size, p);
14920 else
14922 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14923 insert_double (val, p);
14926 break;
14928 case MODE_VECTOR_FLOAT:
14929 for (i = 0, p = array; i < length; i++, p += elt_size)
14931 rtx elt = CONST_VECTOR_ELT (rtl, i);
14932 insert_float (elt, p);
14934 break;
14936 default:
14937 gcc_unreachable ();
14940 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14942 return true;
14944 case CONST_STRING:
14945 if (dwarf_version >= 4 || !dwarf_strict)
14947 dw_loc_descr_ref loc_result;
14948 resolve_one_addr (&rtl, NULL);
14949 rtl_addr:
14950 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14951 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14952 add_AT_loc (die, DW_AT_location, loc_result);
14953 vec_safe_push (used_rtx_array, rtl);
14954 return true;
14956 return false;
14958 case CONST:
14959 if (CONSTANT_P (XEXP (rtl, 0)))
14960 return add_const_value_attribute (die, XEXP (rtl, 0));
14961 /* FALLTHROUGH */
14962 case SYMBOL_REF:
14963 if (!const_ok_for_output (rtl))
14964 return false;
14965 case LABEL_REF:
14966 if (dwarf_version >= 4 || !dwarf_strict)
14967 goto rtl_addr;
14968 return false;
14970 case PLUS:
14971 /* In cases where an inlined instance of an inline function is passed
14972 the address of an `auto' variable (which is local to the caller) we
14973 can get a situation where the DECL_RTL of the artificial local
14974 variable (for the inlining) which acts as a stand-in for the
14975 corresponding formal parameter (of the inline function) will look
14976 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14977 exactly a compile-time constant expression, but it isn't the address
14978 of the (artificial) local variable either. Rather, it represents the
14979 *value* which the artificial local variable always has during its
14980 lifetime. We currently have no way to represent such quasi-constant
14981 values in Dwarf, so for now we just punt and generate nothing. */
14982 return false;
14984 case HIGH:
14985 case CONST_FIXED:
14986 return false;
14988 case MEM:
14989 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14990 && MEM_READONLY_P (rtl)
14991 && GET_MODE (rtl) == BLKmode)
14993 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14994 return true;
14996 return false;
14998 default:
14999 /* No other kinds of rtx should be possible here. */
15000 gcc_unreachable ();
15002 return false;
15005 /* Determine whether the evaluation of EXPR references any variables
15006 or functions which aren't otherwise used (and therefore may not be
15007 output). */
15008 static tree
15009 reference_to_unused (tree * tp, int * walk_subtrees,
15010 void * data ATTRIBUTE_UNUSED)
15012 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15013 *walk_subtrees = 0;
15015 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15016 && ! TREE_ASM_WRITTEN (*tp))
15017 return *tp;
15018 /* ??? The C++ FE emits debug information for using decls, so
15019 putting gcc_unreachable here falls over. See PR31899. For now
15020 be conservative. */
15021 else if (!cgraph_global_info_ready
15022 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15023 return *tp;
15024 else if (TREE_CODE (*tp) == VAR_DECL)
15026 struct varpool_node *node = varpool_get_node (*tp);
15027 if (!node || !node->symbol.definition)
15028 return *tp;
15030 else if (TREE_CODE (*tp) == FUNCTION_DECL
15031 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15033 /* The call graph machinery must have finished analyzing,
15034 optimizing and gimplifying the CU by now.
15035 So if *TP has no call graph node associated
15036 to it, it means *TP will not be emitted. */
15037 if (!cgraph_get_node (*tp))
15038 return *tp;
15040 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15041 return *tp;
15043 return NULL_TREE;
15046 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15047 for use in a later add_const_value_attribute call. */
15049 static rtx
15050 rtl_for_decl_init (tree init, tree type)
15052 rtx rtl = NULL_RTX;
15054 STRIP_NOPS (init);
15056 /* If a variable is initialized with a string constant without embedded
15057 zeros, build CONST_STRING. */
15058 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15060 tree enttype = TREE_TYPE (type);
15061 tree domain = TYPE_DOMAIN (type);
15062 enum machine_mode mode = TYPE_MODE (enttype);
15064 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15065 && domain
15066 && integer_zerop (TYPE_MIN_VALUE (domain))
15067 && compare_tree_int (TYPE_MAX_VALUE (domain),
15068 TREE_STRING_LENGTH (init) - 1) == 0
15069 && ((size_t) TREE_STRING_LENGTH (init)
15070 == strlen (TREE_STRING_POINTER (init)) + 1))
15072 rtl = gen_rtx_CONST_STRING (VOIDmode,
15073 ggc_strdup (TREE_STRING_POINTER (init)));
15074 rtl = gen_rtx_MEM (BLKmode, rtl);
15075 MEM_READONLY_P (rtl) = 1;
15078 /* Other aggregates, and complex values, could be represented using
15079 CONCAT: FIXME! */
15080 else if (AGGREGATE_TYPE_P (type)
15081 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15082 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15083 || TREE_CODE (type) == COMPLEX_TYPE)
15085 /* Vectors only work if their mode is supported by the target.
15086 FIXME: generic vectors ought to work too. */
15087 else if (TREE_CODE (type) == VECTOR_TYPE
15088 && !VECTOR_MODE_P (TYPE_MODE (type)))
15090 /* If the initializer is something that we know will expand into an
15091 immediate RTL constant, expand it now. We must be careful not to
15092 reference variables which won't be output. */
15093 else if (initializer_constant_valid_p (init, type)
15094 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15096 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15097 possible. */
15098 if (TREE_CODE (type) == VECTOR_TYPE)
15099 switch (TREE_CODE (init))
15101 case VECTOR_CST:
15102 break;
15103 case CONSTRUCTOR:
15104 if (TREE_CONSTANT (init))
15106 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15107 bool constant_p = true;
15108 tree value;
15109 unsigned HOST_WIDE_INT ix;
15111 /* Even when ctor is constant, it might contain non-*_CST
15112 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15113 belong into VECTOR_CST nodes. */
15114 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15115 if (!CONSTANT_CLASS_P (value))
15117 constant_p = false;
15118 break;
15121 if (constant_p)
15123 init = build_vector_from_ctor (type, elts);
15124 break;
15127 /* FALLTHRU */
15129 default:
15130 return NULL;
15133 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15135 /* If expand_expr returns a MEM, it wasn't immediate. */
15136 gcc_assert (!rtl || !MEM_P (rtl));
15139 return rtl;
15142 /* Generate RTL for the variable DECL to represent its location. */
15144 static rtx
15145 rtl_for_decl_location (tree decl)
15147 rtx rtl;
15149 /* Here we have to decide where we are going to say the parameter "lives"
15150 (as far as the debugger is concerned). We only have a couple of
15151 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15153 DECL_RTL normally indicates where the parameter lives during most of the
15154 activation of the function. If optimization is enabled however, this
15155 could be either NULL or else a pseudo-reg. Both of those cases indicate
15156 that the parameter doesn't really live anywhere (as far as the code
15157 generation parts of GCC are concerned) during most of the function's
15158 activation. That will happen (for example) if the parameter is never
15159 referenced within the function.
15161 We could just generate a location descriptor here for all non-NULL
15162 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15163 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15164 where DECL_RTL is NULL or is a pseudo-reg.
15166 Note however that we can only get away with using DECL_INCOMING_RTL as
15167 a backup substitute for DECL_RTL in certain limited cases. In cases
15168 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15169 we can be sure that the parameter was passed using the same type as it is
15170 declared to have within the function, and that its DECL_INCOMING_RTL
15171 points us to a place where a value of that type is passed.
15173 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15174 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15175 because in these cases DECL_INCOMING_RTL points us to a value of some
15176 type which is *different* from the type of the parameter itself. Thus,
15177 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15178 such cases, the debugger would end up (for example) trying to fetch a
15179 `float' from a place which actually contains the first part of a
15180 `double'. That would lead to really incorrect and confusing
15181 output at debug-time.
15183 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15184 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15185 are a couple of exceptions however. On little-endian machines we can
15186 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15187 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15188 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15189 when (on a little-endian machine) a non-prototyped function has a
15190 parameter declared to be of type `short' or `char'. In such cases,
15191 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15192 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15193 passed `int' value. If the debugger then uses that address to fetch
15194 a `short' or a `char' (on a little-endian machine) the result will be
15195 the correct data, so we allow for such exceptional cases below.
15197 Note that our goal here is to describe the place where the given formal
15198 parameter lives during most of the function's activation (i.e. between the
15199 end of the prologue and the start of the epilogue). We'll do that as best
15200 as we can. Note however that if the given formal parameter is modified
15201 sometime during the execution of the function, then a stack backtrace (at
15202 debug-time) will show the function as having been called with the *new*
15203 value rather than the value which was originally passed in. This happens
15204 rarely enough that it is not a major problem, but it *is* a problem, and
15205 I'd like to fix it.
15207 A future version of dwarf2out.c may generate two additional attributes for
15208 any given DW_TAG_formal_parameter DIE which will describe the "passed
15209 type" and the "passed location" for the given formal parameter in addition
15210 to the attributes we now generate to indicate the "declared type" and the
15211 "active location" for each parameter. This additional set of attributes
15212 could be used by debuggers for stack backtraces. Separately, note that
15213 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15214 This happens (for example) for inlined-instances of inline function formal
15215 parameters which are never referenced. This really shouldn't be
15216 happening. All PARM_DECL nodes should get valid non-NULL
15217 DECL_INCOMING_RTL values. FIXME. */
15219 /* Use DECL_RTL as the "location" unless we find something better. */
15220 rtl = DECL_RTL_IF_SET (decl);
15222 /* When generating abstract instances, ignore everything except
15223 constants, symbols living in memory, and symbols living in
15224 fixed registers. */
15225 if (! reload_completed)
15227 if (rtl
15228 && (CONSTANT_P (rtl)
15229 || (MEM_P (rtl)
15230 && CONSTANT_P (XEXP (rtl, 0)))
15231 || (REG_P (rtl)
15232 && TREE_CODE (decl) == VAR_DECL
15233 && TREE_STATIC (decl))))
15235 rtl = targetm.delegitimize_address (rtl);
15236 return rtl;
15238 rtl = NULL_RTX;
15240 else if (TREE_CODE (decl) == PARM_DECL)
15242 if (rtl == NULL_RTX
15243 || is_pseudo_reg (rtl)
15244 || (MEM_P (rtl)
15245 && is_pseudo_reg (XEXP (rtl, 0))
15246 && DECL_INCOMING_RTL (decl)
15247 && MEM_P (DECL_INCOMING_RTL (decl))
15248 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15250 tree declared_type = TREE_TYPE (decl);
15251 tree passed_type = DECL_ARG_TYPE (decl);
15252 enum machine_mode dmode = TYPE_MODE (declared_type);
15253 enum machine_mode pmode = TYPE_MODE (passed_type);
15255 /* This decl represents a formal parameter which was optimized out.
15256 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15257 all cases where (rtl == NULL_RTX) just below. */
15258 if (dmode == pmode)
15259 rtl = DECL_INCOMING_RTL (decl);
15260 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15261 && SCALAR_INT_MODE_P (dmode)
15262 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15263 && DECL_INCOMING_RTL (decl))
15265 rtx inc = DECL_INCOMING_RTL (decl);
15266 if (REG_P (inc))
15267 rtl = inc;
15268 else if (MEM_P (inc))
15270 if (BYTES_BIG_ENDIAN)
15271 rtl = adjust_address_nv (inc, dmode,
15272 GET_MODE_SIZE (pmode)
15273 - GET_MODE_SIZE (dmode));
15274 else
15275 rtl = inc;
15280 /* If the parm was passed in registers, but lives on the stack, then
15281 make a big endian correction if the mode of the type of the
15282 parameter is not the same as the mode of the rtl. */
15283 /* ??? This is the same series of checks that are made in dbxout.c before
15284 we reach the big endian correction code there. It isn't clear if all
15285 of these checks are necessary here, but keeping them all is the safe
15286 thing to do. */
15287 else if (MEM_P (rtl)
15288 && XEXP (rtl, 0) != const0_rtx
15289 && ! CONSTANT_P (XEXP (rtl, 0))
15290 /* Not passed in memory. */
15291 && !MEM_P (DECL_INCOMING_RTL (decl))
15292 /* Not passed by invisible reference. */
15293 && (!REG_P (XEXP (rtl, 0))
15294 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15295 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15296 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15297 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15298 #endif
15300 /* Big endian correction check. */
15301 && BYTES_BIG_ENDIAN
15302 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15303 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15304 < UNITS_PER_WORD))
15306 enum machine_mode addr_mode = get_address_mode (rtl);
15307 int offset = (UNITS_PER_WORD
15308 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15310 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15311 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15314 else if (TREE_CODE (decl) == VAR_DECL
15315 && rtl
15316 && MEM_P (rtl)
15317 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15318 && BYTES_BIG_ENDIAN)
15320 enum machine_mode addr_mode = get_address_mode (rtl);
15321 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15322 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15324 /* If a variable is declared "register" yet is smaller than
15325 a register, then if we store the variable to memory, it
15326 looks like we're storing a register-sized value, when in
15327 fact we are not. We need to adjust the offset of the
15328 storage location to reflect the actual value's bytes,
15329 else gdb will not be able to display it. */
15330 if (rsize > dsize)
15331 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15332 plus_constant (addr_mode, XEXP (rtl, 0),
15333 rsize - dsize));
15336 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15337 and will have been substituted directly into all expressions that use it.
15338 C does not have such a concept, but C++ and other languages do. */
15339 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15340 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15342 if (rtl)
15343 rtl = targetm.delegitimize_address (rtl);
15345 /* If we don't look past the constant pool, we risk emitting a
15346 reference to a constant pool entry that isn't referenced from
15347 code, and thus is not emitted. */
15348 if (rtl)
15349 rtl = avoid_constant_pool_reference (rtl);
15351 /* Try harder to get a rtl. If this symbol ends up not being emitted
15352 in the current CU, resolve_addr will remove the expression referencing
15353 it. */
15354 if (rtl == NULL_RTX
15355 && TREE_CODE (decl) == VAR_DECL
15356 && !DECL_EXTERNAL (decl)
15357 && TREE_STATIC (decl)
15358 && DECL_NAME (decl)
15359 && !DECL_HARD_REGISTER (decl)
15360 && DECL_MODE (decl) != VOIDmode)
15362 rtl = make_decl_rtl_for_debug (decl);
15363 if (!MEM_P (rtl)
15364 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15365 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15366 rtl = NULL_RTX;
15369 return rtl;
15372 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15373 returned. If so, the decl for the COMMON block is returned, and the
15374 value is the offset into the common block for the symbol. */
15376 static tree
15377 fortran_common (tree decl, HOST_WIDE_INT *value)
15379 tree val_expr, cvar;
15380 enum machine_mode mode;
15381 HOST_WIDE_INT bitsize, bitpos;
15382 tree offset;
15383 int unsignedp, volatilep = 0;
15385 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15386 it does not have a value (the offset into the common area), or if it
15387 is thread local (as opposed to global) then it isn't common, and shouldn't
15388 be handled as such. */
15389 if (TREE_CODE (decl) != VAR_DECL
15390 || !TREE_STATIC (decl)
15391 || !DECL_HAS_VALUE_EXPR_P (decl)
15392 || !is_fortran ())
15393 return NULL_TREE;
15395 val_expr = DECL_VALUE_EXPR (decl);
15396 if (TREE_CODE (val_expr) != COMPONENT_REF)
15397 return NULL_TREE;
15399 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15400 &mode, &unsignedp, &volatilep, true);
15402 if (cvar == NULL_TREE
15403 || TREE_CODE (cvar) != VAR_DECL
15404 || DECL_ARTIFICIAL (cvar)
15405 || !TREE_PUBLIC (cvar))
15406 return NULL_TREE;
15408 *value = 0;
15409 if (offset != NULL)
15411 if (!host_integerp (offset, 0))
15412 return NULL_TREE;
15413 *value = tree_low_cst (offset, 0);
15415 if (bitpos != 0)
15416 *value += bitpos / BITS_PER_UNIT;
15418 return cvar;
15421 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15422 data attribute for a variable or a parameter. We generate the
15423 DW_AT_const_value attribute only in those cases where the given variable
15424 or parameter does not have a true "location" either in memory or in a
15425 register. This can happen (for example) when a constant is passed as an
15426 actual argument in a call to an inline function. (It's possible that
15427 these things can crop up in other ways also.) Note that one type of
15428 constant value which can be passed into an inlined function is a constant
15429 pointer. This can happen for example if an actual argument in an inlined
15430 function call evaluates to a compile-time constant address.
15432 CACHE_P is true if it is worth caching the location list for DECL,
15433 so that future calls can reuse it rather than regenerate it from scratch.
15434 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15435 since we will need to refer to them each time the function is inlined. */
15437 static bool
15438 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15439 enum dwarf_attribute attr)
15441 rtx rtl;
15442 dw_loc_list_ref list;
15443 var_loc_list *loc_list;
15444 cached_dw_loc_list *cache;
15445 void **slot;
15447 if (TREE_CODE (decl) == ERROR_MARK)
15448 return false;
15450 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15451 || TREE_CODE (decl) == RESULT_DECL);
15453 /* Try to get some constant RTL for this decl, and use that as the value of
15454 the location. */
15456 rtl = rtl_for_decl_location (decl);
15457 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15458 && add_const_value_attribute (die, rtl))
15459 return true;
15461 /* See if we have single element location list that is equivalent to
15462 a constant value. That way we are better to use add_const_value_attribute
15463 rather than expanding constant value equivalent. */
15464 loc_list = lookup_decl_loc (decl);
15465 if (loc_list
15466 && loc_list->first
15467 && loc_list->first->next == NULL
15468 && NOTE_P (loc_list->first->loc)
15469 && NOTE_VAR_LOCATION (loc_list->first->loc)
15470 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15472 struct var_loc_node *node;
15474 node = loc_list->first;
15475 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15476 if (GET_CODE (rtl) == EXPR_LIST)
15477 rtl = XEXP (rtl, 0);
15478 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15479 && add_const_value_attribute (die, rtl))
15480 return true;
15482 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15483 list several times. See if we've already cached the contents. */
15484 list = NULL;
15485 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15486 cache_p = false;
15487 if (cache_p)
15489 cache = (cached_dw_loc_list *)
15490 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15491 if (cache)
15492 list = cache->loc_list;
15494 if (list == NULL)
15496 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15497 /* It is usually worth caching this result if the decl is from
15498 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15499 if (cache_p && list && list->dw_loc_next)
15501 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15502 DECL_UID (decl), INSERT);
15503 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15504 cache->decl_id = DECL_UID (decl);
15505 cache->loc_list = list;
15506 *slot = cache;
15509 if (list)
15511 add_AT_location_description (die, attr, list);
15512 return true;
15514 /* None of that worked, so it must not really have a location;
15515 try adding a constant value attribute from the DECL_INITIAL. */
15516 return tree_add_const_value_attribute_for_decl (die, decl);
15519 /* Add VARIABLE and DIE into deferred locations list. */
15521 static void
15522 defer_location (tree variable, dw_die_ref die)
15524 deferred_locations entry;
15525 entry.variable = variable;
15526 entry.die = die;
15527 vec_safe_push (deferred_locations_list, entry);
15530 /* Helper function for tree_add_const_value_attribute. Natively encode
15531 initializer INIT into an array. Return true if successful. */
15533 static bool
15534 native_encode_initializer (tree init, unsigned char *array, int size)
15536 tree type;
15538 if (init == NULL_TREE)
15539 return false;
15541 STRIP_NOPS (init);
15542 switch (TREE_CODE (init))
15544 case STRING_CST:
15545 type = TREE_TYPE (init);
15546 if (TREE_CODE (type) == ARRAY_TYPE)
15548 tree enttype = TREE_TYPE (type);
15549 enum machine_mode mode = TYPE_MODE (enttype);
15551 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15552 return false;
15553 if (int_size_in_bytes (type) != size)
15554 return false;
15555 if (size > TREE_STRING_LENGTH (init))
15557 memcpy (array, TREE_STRING_POINTER (init),
15558 TREE_STRING_LENGTH (init));
15559 memset (array + TREE_STRING_LENGTH (init),
15560 '\0', size - TREE_STRING_LENGTH (init));
15562 else
15563 memcpy (array, TREE_STRING_POINTER (init), size);
15564 return true;
15566 return false;
15567 case CONSTRUCTOR:
15568 type = TREE_TYPE (init);
15569 if (int_size_in_bytes (type) != size)
15570 return false;
15571 if (TREE_CODE (type) == ARRAY_TYPE)
15573 HOST_WIDE_INT min_index;
15574 unsigned HOST_WIDE_INT cnt;
15575 int curpos = 0, fieldsize;
15576 constructor_elt *ce;
15578 if (TYPE_DOMAIN (type) == NULL_TREE
15579 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15580 return false;
15582 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15583 if (fieldsize <= 0)
15584 return false;
15586 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15587 memset (array, '\0', size);
15588 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15590 tree val = ce->value;
15591 tree index = ce->index;
15592 int pos = curpos;
15593 if (index && TREE_CODE (index) == RANGE_EXPR)
15594 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15595 * fieldsize;
15596 else if (index)
15597 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15599 if (val)
15601 STRIP_NOPS (val);
15602 if (!native_encode_initializer (val, array + pos, fieldsize))
15603 return false;
15605 curpos = pos + fieldsize;
15606 if (index && TREE_CODE (index) == RANGE_EXPR)
15608 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15609 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15610 while (count-- > 0)
15612 if (val)
15613 memcpy (array + curpos, array + pos, fieldsize);
15614 curpos += fieldsize;
15617 gcc_assert (curpos <= size);
15619 return true;
15621 else if (TREE_CODE (type) == RECORD_TYPE
15622 || TREE_CODE (type) == UNION_TYPE)
15624 tree field = NULL_TREE;
15625 unsigned HOST_WIDE_INT cnt;
15626 constructor_elt *ce;
15628 if (int_size_in_bytes (type) != size)
15629 return false;
15631 if (TREE_CODE (type) == RECORD_TYPE)
15632 field = TYPE_FIELDS (type);
15634 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15636 tree val = ce->value;
15637 int pos, fieldsize;
15639 if (ce->index != 0)
15640 field = ce->index;
15642 if (val)
15643 STRIP_NOPS (val);
15645 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15646 return false;
15648 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15649 && TYPE_DOMAIN (TREE_TYPE (field))
15650 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15651 return false;
15652 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15653 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15654 return false;
15655 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15656 pos = int_byte_position (field);
15657 gcc_assert (pos + fieldsize <= size);
15658 if (val
15659 && !native_encode_initializer (val, array + pos, fieldsize))
15660 return false;
15662 return true;
15664 return false;
15665 case VIEW_CONVERT_EXPR:
15666 case NON_LVALUE_EXPR:
15667 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15668 default:
15669 return native_encode_expr (init, array, size) == size;
15673 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15674 attribute is the const value T. */
15676 static bool
15677 tree_add_const_value_attribute (dw_die_ref die, tree t)
15679 tree init;
15680 tree type = TREE_TYPE (t);
15681 rtx rtl;
15683 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15684 return false;
15686 init = t;
15687 gcc_assert (!DECL_P (init));
15689 rtl = rtl_for_decl_init (init, type);
15690 if (rtl)
15691 return add_const_value_attribute (die, rtl);
15692 /* If the host and target are sane, try harder. */
15693 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15694 && initializer_constant_valid_p (init, type))
15696 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15697 if (size > 0 && (int) size == size)
15699 unsigned char *array = (unsigned char *)
15700 ggc_alloc_cleared_atomic (size);
15702 if (native_encode_initializer (init, array, size))
15704 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15705 return true;
15707 ggc_free (array);
15710 return false;
15713 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15714 attribute is the const value of T, where T is an integral constant
15715 variable with static storage duration
15716 (so it can't be a PARM_DECL or a RESULT_DECL). */
15718 static bool
15719 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15722 if (!decl
15723 || (TREE_CODE (decl) != VAR_DECL
15724 && TREE_CODE (decl) != CONST_DECL)
15725 || (TREE_CODE (decl) == VAR_DECL
15726 && !TREE_STATIC (decl)))
15727 return false;
15729 if (TREE_READONLY (decl)
15730 && ! TREE_THIS_VOLATILE (decl)
15731 && DECL_INITIAL (decl))
15732 /* OK */;
15733 else
15734 return false;
15736 /* Don't add DW_AT_const_value if abstract origin already has one. */
15737 if (get_AT (var_die, DW_AT_const_value))
15738 return false;
15740 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15743 /* Convert the CFI instructions for the current function into a
15744 location list. This is used for DW_AT_frame_base when we targeting
15745 a dwarf2 consumer that does not support the dwarf3
15746 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15747 expressions. */
15749 static dw_loc_list_ref
15750 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15752 int ix;
15753 dw_fde_ref fde;
15754 dw_loc_list_ref list, *list_tail;
15755 dw_cfi_ref cfi;
15756 dw_cfa_location last_cfa, next_cfa;
15757 const char *start_label, *last_label, *section;
15758 dw_cfa_location remember;
15760 fde = cfun->fde;
15761 gcc_assert (fde != NULL);
15763 section = secname_for_decl (current_function_decl);
15764 list_tail = &list;
15765 list = NULL;
15767 memset (&next_cfa, 0, sizeof (next_cfa));
15768 next_cfa.reg = INVALID_REGNUM;
15769 remember = next_cfa;
15771 start_label = fde->dw_fde_begin;
15773 /* ??? Bald assumption that the CIE opcode list does not contain
15774 advance opcodes. */
15775 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15776 lookup_cfa_1 (cfi, &next_cfa, &remember);
15778 last_cfa = next_cfa;
15779 last_label = start_label;
15781 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15783 /* If the first partition contained no CFI adjustments, the
15784 CIE opcodes apply to the whole first partition. */
15785 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15786 fde->dw_fde_begin, fde->dw_fde_end, section);
15787 list_tail =&(*list_tail)->dw_loc_next;
15788 start_label = last_label = fde->dw_fde_second_begin;
15791 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15793 switch (cfi->dw_cfi_opc)
15795 case DW_CFA_set_loc:
15796 case DW_CFA_advance_loc1:
15797 case DW_CFA_advance_loc2:
15798 case DW_CFA_advance_loc4:
15799 if (!cfa_equal_p (&last_cfa, &next_cfa))
15801 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15802 start_label, last_label, section);
15804 list_tail = &(*list_tail)->dw_loc_next;
15805 last_cfa = next_cfa;
15806 start_label = last_label;
15808 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15809 break;
15811 case DW_CFA_advance_loc:
15812 /* The encoding is complex enough that we should never emit this. */
15813 gcc_unreachable ();
15815 default:
15816 lookup_cfa_1 (cfi, &next_cfa, &remember);
15817 break;
15819 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15821 if (!cfa_equal_p (&last_cfa, &next_cfa))
15823 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15824 start_label, last_label, section);
15826 list_tail = &(*list_tail)->dw_loc_next;
15827 last_cfa = next_cfa;
15828 start_label = last_label;
15830 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15831 start_label, fde->dw_fde_end, section);
15832 list_tail = &(*list_tail)->dw_loc_next;
15833 start_label = last_label = fde->dw_fde_second_begin;
15837 if (!cfa_equal_p (&last_cfa, &next_cfa))
15839 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15840 start_label, last_label, section);
15841 list_tail = &(*list_tail)->dw_loc_next;
15842 start_label = last_label;
15845 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15846 start_label,
15847 fde->dw_fde_second_begin
15848 ? fde->dw_fde_second_end : fde->dw_fde_end,
15849 section);
15851 if (list && list->dw_loc_next)
15852 gen_llsym (list);
15854 return list;
15857 /* Compute a displacement from the "steady-state frame pointer" to the
15858 frame base (often the same as the CFA), and store it in
15859 frame_pointer_fb_offset. OFFSET is added to the displacement
15860 before the latter is negated. */
15862 static void
15863 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15865 rtx reg, elim;
15867 #ifdef FRAME_POINTER_CFA_OFFSET
15868 reg = frame_pointer_rtx;
15869 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15870 #else
15871 reg = arg_pointer_rtx;
15872 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15873 #endif
15875 elim = (ira_use_lra_p
15876 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15877 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15878 if (GET_CODE (elim) == PLUS)
15880 offset += INTVAL (XEXP (elim, 1));
15881 elim = XEXP (elim, 0);
15884 frame_pointer_fb_offset = -offset;
15886 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15887 in which to eliminate. This is because it's stack pointer isn't
15888 directly accessible as a register within the ISA. To work around
15889 this, assume that while we cannot provide a proper value for
15890 frame_pointer_fb_offset, we won't need one either. */
15891 frame_pointer_fb_offset_valid
15892 = ((SUPPORTS_STACK_ALIGNMENT
15893 && (elim == hard_frame_pointer_rtx
15894 || elim == stack_pointer_rtx))
15895 || elim == (frame_pointer_needed
15896 ? hard_frame_pointer_rtx
15897 : stack_pointer_rtx));
15900 /* Generate a DW_AT_name attribute given some string value to be included as
15901 the value of the attribute. */
15903 static void
15904 add_name_attribute (dw_die_ref die, const char *name_string)
15906 if (name_string != NULL && *name_string != 0)
15908 if (demangle_name_func)
15909 name_string = (*demangle_name_func) (name_string);
15911 add_AT_string (die, DW_AT_name, name_string);
15915 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15916 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15917 of TYPE accordingly.
15919 ??? This is a temporary measure until after we're able to generate
15920 regular DWARF for the complex Ada type system. */
15922 static void
15923 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15924 dw_die_ref context_die)
15926 tree dtype;
15927 dw_die_ref dtype_die;
15929 if (!lang_hooks.types.descriptive_type)
15930 return;
15932 dtype = lang_hooks.types.descriptive_type (type);
15933 if (!dtype)
15934 return;
15936 dtype_die = lookup_type_die (dtype);
15937 if (!dtype_die)
15939 gen_type_die (dtype, context_die);
15940 dtype_die = lookup_type_die (dtype);
15941 gcc_assert (dtype_die);
15944 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15947 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
15949 static const char *
15950 comp_dir_string (void)
15952 const char *wd;
15953 char *wd1;
15954 static const char *cached_wd = NULL;
15956 if (cached_wd != NULL)
15957 return cached_wd;
15959 wd = get_src_pwd ();
15960 if (wd == NULL)
15961 return NULL;
15963 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15965 int wdlen;
15967 wdlen = strlen (wd);
15968 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15969 strcpy (wd1, wd);
15970 wd1 [wdlen] = DIR_SEPARATOR;
15971 wd1 [wdlen + 1] = 0;
15972 wd = wd1;
15975 cached_wd = remap_debug_filename (wd);
15976 return cached_wd;
15979 /* Generate a DW_AT_comp_dir attribute for DIE. */
15981 static void
15982 add_comp_dir_attribute (dw_die_ref die)
15984 const char * wd = comp_dir_string ();
15985 if (wd != NULL)
15986 add_AT_string (die, DW_AT_comp_dir, wd);
15989 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15990 default. */
15992 static int
15993 lower_bound_default (void)
15995 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15997 case DW_LANG_C:
15998 case DW_LANG_C89:
15999 case DW_LANG_C99:
16000 case DW_LANG_C_plus_plus:
16001 case DW_LANG_ObjC:
16002 case DW_LANG_ObjC_plus_plus:
16003 case DW_LANG_Java:
16004 return 0;
16005 case DW_LANG_Fortran77:
16006 case DW_LANG_Fortran90:
16007 case DW_LANG_Fortran95:
16008 return 1;
16009 case DW_LANG_UPC:
16010 case DW_LANG_D:
16011 case DW_LANG_Python:
16012 return dwarf_version >= 4 ? 0 : -1;
16013 case DW_LANG_Ada95:
16014 case DW_LANG_Ada83:
16015 case DW_LANG_Cobol74:
16016 case DW_LANG_Cobol85:
16017 case DW_LANG_Pascal83:
16018 case DW_LANG_Modula2:
16019 case DW_LANG_PLI:
16020 return dwarf_version >= 4 ? 1 : -1;
16021 default:
16022 return -1;
16026 /* Given a tree node describing an array bound (either lower or upper) output
16027 a representation for that bound. */
16029 static void
16030 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16032 switch (TREE_CODE (bound))
16034 case ERROR_MARK:
16035 return;
16037 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16038 case INTEGER_CST:
16040 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16041 int dflt;
16043 /* Use the default if possible. */
16044 if (bound_attr == DW_AT_lower_bound
16045 && host_integerp (bound, 0)
16046 && (dflt = lower_bound_default ()) != -1
16047 && tree_low_cst (bound, 0) == dflt)
16050 /* Otherwise represent the bound as an unsigned value with the
16051 precision of its type. The precision and signedness of the
16052 type will be necessary to re-interpret it unambiguously. */
16053 else if (prec < HOST_BITS_PER_WIDE_INT)
16055 unsigned HOST_WIDE_INT mask
16056 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16057 add_AT_unsigned (subrange_die, bound_attr,
16058 TREE_INT_CST_LOW (bound) & mask);
16060 else if (prec == HOST_BITS_PER_WIDE_INT
16061 || TREE_INT_CST_HIGH (bound) == 0)
16062 add_AT_unsigned (subrange_die, bound_attr,
16063 TREE_INT_CST_LOW (bound));
16064 else
16065 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16066 TREE_INT_CST_LOW (bound));
16068 break;
16070 CASE_CONVERT:
16071 case VIEW_CONVERT_EXPR:
16072 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16073 break;
16075 case SAVE_EXPR:
16076 break;
16078 case VAR_DECL:
16079 case PARM_DECL:
16080 case RESULT_DECL:
16082 dw_die_ref decl_die = lookup_decl_die (bound);
16084 /* ??? Can this happen, or should the variable have been bound
16085 first? Probably it can, since I imagine that we try to create
16086 the types of parameters in the order in which they exist in
16087 the list, and won't have created a forward reference to a
16088 later parameter. */
16089 if (decl_die != NULL)
16091 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16092 break;
16095 /* FALLTHRU */
16097 default:
16099 /* Otherwise try to create a stack operation procedure to
16100 evaluate the value of the array bound. */
16102 dw_die_ref ctx, decl_die;
16103 dw_loc_list_ref list;
16105 list = loc_list_from_tree (bound, 2);
16106 if (list == NULL || single_element_loc_list_p (list))
16108 /* If DW_AT_*bound is not a reference nor constant, it is
16109 a DWARF expression rather than location description.
16110 For that loc_list_from_tree (bound, 0) is needed.
16111 If that fails to give a single element list,
16112 fall back to outputting this as a reference anyway. */
16113 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16114 if (list2 && single_element_loc_list_p (list2))
16116 add_AT_loc (subrange_die, bound_attr, list2->expr);
16117 break;
16120 if (list == NULL)
16121 break;
16123 if (current_function_decl == 0)
16124 ctx = comp_unit_die ();
16125 else
16126 ctx = lookup_decl_die (current_function_decl);
16128 decl_die = new_die (DW_TAG_variable, ctx, bound);
16129 add_AT_flag (decl_die, DW_AT_artificial, 1);
16130 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16131 add_AT_location_description (decl_die, DW_AT_location, list);
16132 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16133 break;
16138 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16139 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16140 Note that the block of subscript information for an array type also
16141 includes information about the element type of the given array type. */
16143 static void
16144 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16146 unsigned dimension_number;
16147 tree lower, upper;
16148 dw_die_ref subrange_die;
16150 for (dimension_number = 0;
16151 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16152 type = TREE_TYPE (type), dimension_number++)
16154 tree domain = TYPE_DOMAIN (type);
16156 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16157 break;
16159 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16160 and (in GNU C only) variable bounds. Handle all three forms
16161 here. */
16162 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16163 if (domain)
16165 /* We have an array type with specified bounds. */
16166 lower = TYPE_MIN_VALUE (domain);
16167 upper = TYPE_MAX_VALUE (domain);
16169 /* Define the index type. */
16170 if (TREE_TYPE (domain))
16172 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16173 TREE_TYPE field. We can't emit debug info for this
16174 because it is an unnamed integral type. */
16175 if (TREE_CODE (domain) == INTEGER_TYPE
16176 && TYPE_NAME (domain) == NULL_TREE
16177 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16178 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16180 else
16181 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16182 type_die);
16185 /* ??? If upper is NULL, the array has unspecified length,
16186 but it does have a lower bound. This happens with Fortran
16187 dimension arr(N:*)
16188 Since the debugger is definitely going to need to know N
16189 to produce useful results, go ahead and output the lower
16190 bound solo, and hope the debugger can cope. */
16192 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16193 if (upper)
16194 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16197 /* Otherwise we have an array type with an unspecified length. The
16198 DWARF-2 spec does not say how to handle this; let's just leave out the
16199 bounds. */
16203 static void
16204 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16206 dw_die_ref decl_die;
16207 unsigned size;
16209 switch (TREE_CODE (tree_node))
16211 case ERROR_MARK:
16212 size = 0;
16213 break;
16214 case ENUMERAL_TYPE:
16215 case RECORD_TYPE:
16216 case UNION_TYPE:
16217 case QUAL_UNION_TYPE:
16218 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16219 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16221 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16222 return;
16224 size = int_size_in_bytes (tree_node);
16225 break;
16226 case FIELD_DECL:
16227 /* For a data member of a struct or union, the DW_AT_byte_size is
16228 generally given as the number of bytes normally allocated for an
16229 object of the *declared* type of the member itself. This is true
16230 even for bit-fields. */
16231 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16232 break;
16233 default:
16234 gcc_unreachable ();
16237 /* Note that `size' might be -1 when we get to this point. If it is, that
16238 indicates that the byte size of the entity in question is variable. We
16239 have no good way of expressing this fact in Dwarf at the present time,
16240 so just let the -1 pass on through. */
16241 add_AT_unsigned (die, DW_AT_byte_size, size);
16244 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16245 which specifies the distance in bits from the highest order bit of the
16246 "containing object" for the bit-field to the highest order bit of the
16247 bit-field itself.
16249 For any given bit-field, the "containing object" is a hypothetical object
16250 (of some integral or enum type) within which the given bit-field lives. The
16251 type of this hypothetical "containing object" is always the same as the
16252 declared type of the individual bit-field itself. The determination of the
16253 exact location of the "containing object" for a bit-field is rather
16254 complicated. It's handled by the `field_byte_offset' function (above).
16256 Note that it is the size (in bytes) of the hypothetical "containing object"
16257 which will be given in the DW_AT_byte_size attribute for this bit-field.
16258 (See `byte_size_attribute' above). */
16260 static inline void
16261 add_bit_offset_attribute (dw_die_ref die, tree decl)
16263 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16264 tree type = DECL_BIT_FIELD_TYPE (decl);
16265 HOST_WIDE_INT bitpos_int;
16266 HOST_WIDE_INT highest_order_object_bit_offset;
16267 HOST_WIDE_INT highest_order_field_bit_offset;
16268 HOST_WIDE_INT bit_offset;
16270 /* Must be a field and a bit field. */
16271 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16273 /* We can't yet handle bit-fields whose offsets are variable, so if we
16274 encounter such things, just return without generating any attribute
16275 whatsoever. Likewise for variable or too large size. */
16276 if (! host_integerp (bit_position (decl), 0)
16277 || ! host_integerp (DECL_SIZE (decl), 1))
16278 return;
16280 bitpos_int = int_bit_position (decl);
16282 /* Note that the bit offset is always the distance (in bits) from the
16283 highest-order bit of the "containing object" to the highest-order bit of
16284 the bit-field itself. Since the "high-order end" of any object or field
16285 is different on big-endian and little-endian machines, the computation
16286 below must take account of these differences. */
16287 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16288 highest_order_field_bit_offset = bitpos_int;
16290 if (! BYTES_BIG_ENDIAN)
16292 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16293 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16296 bit_offset
16297 = (! BYTES_BIG_ENDIAN
16298 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16299 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16301 if (bit_offset < 0)
16302 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16303 else
16304 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16307 /* For a FIELD_DECL node which represents a bit field, output an attribute
16308 which specifies the length in bits of the given field. */
16310 static inline void
16311 add_bit_size_attribute (dw_die_ref die, tree decl)
16313 /* Must be a field and a bit field. */
16314 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16315 && DECL_BIT_FIELD_TYPE (decl));
16317 if (host_integerp (DECL_SIZE (decl), 1))
16318 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16321 /* If the compiled language is ANSI C, then add a 'prototyped'
16322 attribute, if arg types are given for the parameters of a function. */
16324 static inline void
16325 add_prototyped_attribute (dw_die_ref die, tree func_type)
16327 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16328 && prototype_p (func_type))
16329 add_AT_flag (die, DW_AT_prototyped, 1);
16332 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16333 by looking in either the type declaration or object declaration
16334 equate table. */
16336 static inline dw_die_ref
16337 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16339 dw_die_ref origin_die = NULL;
16341 if (TREE_CODE (origin) != FUNCTION_DECL)
16343 /* We may have gotten separated from the block for the inlined
16344 function, if we're in an exception handler or some such; make
16345 sure that the abstract function has been written out.
16347 Doing this for nested functions is wrong, however; functions are
16348 distinct units, and our context might not even be inline. */
16349 tree fn = origin;
16351 if (TYPE_P (fn))
16352 fn = TYPE_STUB_DECL (fn);
16354 fn = decl_function_context (fn);
16355 if (fn)
16356 dwarf2out_abstract_function (fn);
16359 if (DECL_P (origin))
16360 origin_die = lookup_decl_die (origin);
16361 else if (TYPE_P (origin))
16362 origin_die = lookup_type_die (origin);
16364 /* XXX: Functions that are never lowered don't always have correct block
16365 trees (in the case of java, they simply have no block tree, in some other
16366 languages). For these functions, there is nothing we can really do to
16367 output correct debug info for inlined functions in all cases. Rather
16368 than die, we'll just produce deficient debug info now, in that we will
16369 have variables without a proper abstract origin. In the future, when all
16370 functions are lowered, we should re-add a gcc_assert (origin_die)
16371 here. */
16373 if (origin_die)
16374 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16375 return origin_die;
16378 /* We do not currently support the pure_virtual attribute. */
16380 static inline void
16381 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16383 if (DECL_VINDEX (func_decl))
16385 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16387 if (host_integerp (DECL_VINDEX (func_decl), 0))
16388 add_AT_loc (die, DW_AT_vtable_elem_location,
16389 new_loc_descr (DW_OP_constu,
16390 tree_low_cst (DECL_VINDEX (func_decl), 0),
16391 0));
16393 /* GNU extension: Record what type this method came from originally. */
16394 if (debug_info_level > DINFO_LEVEL_TERSE
16395 && DECL_CONTEXT (func_decl))
16396 add_AT_die_ref (die, DW_AT_containing_type,
16397 lookup_type_die (DECL_CONTEXT (func_decl)));
16401 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16402 given decl. This used to be a vendor extension until after DWARF 4
16403 standardized it. */
16405 static void
16406 add_linkage_attr (dw_die_ref die, tree decl)
16408 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16410 /* Mimic what assemble_name_raw does with a leading '*'. */
16411 if (name[0] == '*')
16412 name = &name[1];
16414 if (dwarf_version >= 4)
16415 add_AT_string (die, DW_AT_linkage_name, name);
16416 else
16417 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16420 /* Add source coordinate attributes for the given decl. */
16422 static void
16423 add_src_coords_attributes (dw_die_ref die, tree decl)
16425 expanded_location s;
16427 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16428 return;
16429 s = expand_location (DECL_SOURCE_LOCATION (decl));
16430 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16431 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16434 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16436 static void
16437 add_linkage_name (dw_die_ref die, tree decl)
16439 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16440 && TREE_PUBLIC (decl)
16441 && !DECL_ABSTRACT (decl)
16442 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16443 && die->die_tag != DW_TAG_member)
16445 /* Defer until we have an assembler name set. */
16446 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16448 limbo_die_node *asm_name;
16450 asm_name = ggc_alloc_cleared_limbo_die_node ();
16451 asm_name->die = die;
16452 asm_name->created_for = decl;
16453 asm_name->next = deferred_asm_name;
16454 deferred_asm_name = asm_name;
16456 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16457 add_linkage_attr (die, decl);
16461 /* Add a DW_AT_name attribute and source coordinate attribute for the
16462 given decl, but only if it actually has a name. */
16464 static void
16465 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16467 tree decl_name;
16469 decl_name = DECL_NAME (decl);
16470 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16472 const char *name = dwarf2_name (decl, 0);
16473 if (name)
16474 add_name_attribute (die, name);
16475 if (! DECL_ARTIFICIAL (decl))
16476 add_src_coords_attributes (die, decl);
16478 add_linkage_name (die, decl);
16481 #ifdef VMS_DEBUGGING_INFO
16482 /* Get the function's name, as described by its RTL. This may be different
16483 from the DECL_NAME name used in the source file. */
16484 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16486 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16487 XEXP (DECL_RTL (decl), 0), false);
16488 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16490 #endif /* VMS_DEBUGGING_INFO */
16493 #ifdef VMS_DEBUGGING_INFO
16494 /* Output the debug main pointer die for VMS */
16496 void
16497 dwarf2out_vms_debug_main_pointer (void)
16499 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16500 dw_die_ref die;
16502 /* Allocate the VMS debug main subprogram die. */
16503 die = ggc_alloc_cleared_die_node ();
16504 die->die_tag = DW_TAG_subprogram;
16505 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16506 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16507 current_function_funcdef_no);
16508 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16510 /* Make it the first child of comp_unit_die (). */
16511 die->die_parent = comp_unit_die ();
16512 if (comp_unit_die ()->die_child)
16514 die->die_sib = comp_unit_die ()->die_child->die_sib;
16515 comp_unit_die ()->die_child->die_sib = die;
16517 else
16519 die->die_sib = die;
16520 comp_unit_die ()->die_child = die;
16523 #endif /* VMS_DEBUGGING_INFO */
16525 /* Push a new declaration scope. */
16527 static void
16528 push_decl_scope (tree scope)
16530 vec_safe_push (decl_scope_table, scope);
16533 /* Pop a declaration scope. */
16535 static inline void
16536 pop_decl_scope (void)
16538 decl_scope_table->pop ();
16541 /* walk_tree helper function for uses_local_type, below. */
16543 static tree
16544 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16546 if (!TYPE_P (*tp))
16547 *walk_subtrees = 0;
16548 else
16550 tree name = TYPE_NAME (*tp);
16551 if (name && DECL_P (name) && decl_function_context (name))
16552 return *tp;
16554 return NULL_TREE;
16557 /* If TYPE involves a function-local type (including a local typedef to a
16558 non-local type), returns that type; otherwise returns NULL_TREE. */
16560 static tree
16561 uses_local_type (tree type)
16563 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16564 return used;
16567 /* Return the DIE for the scope that immediately contains this type.
16568 Non-named types that do not involve a function-local type get global
16569 scope. Named types nested in namespaces or other types get their
16570 containing scope. All other types (i.e. function-local named types) get
16571 the current active scope. */
16573 static dw_die_ref
16574 scope_die_for (tree t, dw_die_ref context_die)
16576 dw_die_ref scope_die = NULL;
16577 tree containing_scope;
16579 /* Non-types always go in the current scope. */
16580 gcc_assert (TYPE_P (t));
16582 /* Use the scope of the typedef, rather than the scope of the type
16583 it refers to. */
16584 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16585 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16586 else
16587 containing_scope = TYPE_CONTEXT (t);
16589 /* Use the containing namespace if there is one. */
16590 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16592 if (context_die == lookup_decl_die (containing_scope))
16593 /* OK */;
16594 else if (debug_info_level > DINFO_LEVEL_TERSE)
16595 context_die = get_context_die (containing_scope);
16596 else
16597 containing_scope = NULL_TREE;
16600 /* Ignore function type "scopes" from the C frontend. They mean that
16601 a tagged type is local to a parmlist of a function declarator, but
16602 that isn't useful to DWARF. */
16603 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16604 containing_scope = NULL_TREE;
16606 if (SCOPE_FILE_SCOPE_P (containing_scope))
16608 /* If T uses a local type keep it local as well, to avoid references
16609 to function-local DIEs from outside the function. */
16610 if (current_function_decl && uses_local_type (t))
16611 scope_die = context_die;
16612 else
16613 scope_die = comp_unit_die ();
16615 else if (TYPE_P (containing_scope))
16617 /* For types, we can just look up the appropriate DIE. */
16618 if (debug_info_level > DINFO_LEVEL_TERSE)
16619 scope_die = get_context_die (containing_scope);
16620 else
16622 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16623 if (scope_die == NULL)
16624 scope_die = comp_unit_die ();
16627 else
16628 scope_die = context_die;
16630 return scope_die;
16633 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16635 static inline int
16636 local_scope_p (dw_die_ref context_die)
16638 for (; context_die; context_die = context_die->die_parent)
16639 if (context_die->die_tag == DW_TAG_inlined_subroutine
16640 || context_die->die_tag == DW_TAG_subprogram)
16641 return 1;
16643 return 0;
16646 /* Returns nonzero if CONTEXT_DIE is a class. */
16648 static inline int
16649 class_scope_p (dw_die_ref context_die)
16651 return (context_die
16652 && (context_die->die_tag == DW_TAG_structure_type
16653 || context_die->die_tag == DW_TAG_class_type
16654 || context_die->die_tag == DW_TAG_interface_type
16655 || context_die->die_tag == DW_TAG_union_type));
16658 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16659 whether or not to treat a DIE in this context as a declaration. */
16661 static inline int
16662 class_or_namespace_scope_p (dw_die_ref context_die)
16664 return (class_scope_p (context_die)
16665 || (context_die && context_die->die_tag == DW_TAG_namespace));
16668 /* Many forms of DIEs require a "type description" attribute. This
16669 routine locates the proper "type descriptor" die for the type given
16670 by 'type', and adds a DW_AT_type attribute below the given die. */
16672 static void
16673 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16674 int decl_volatile, dw_die_ref context_die)
16676 enum tree_code code = TREE_CODE (type);
16677 dw_die_ref type_die = NULL;
16679 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16680 or fixed-point type, use the inner type. This is because we have no
16681 support for unnamed types in base_type_die. This can happen if this is
16682 an Ada subrange type. Correct solution is emit a subrange type die. */
16683 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16684 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16685 type = TREE_TYPE (type), code = TREE_CODE (type);
16687 if (code == ERROR_MARK
16688 /* Handle a special case. For functions whose return type is void, we
16689 generate *no* type attribute. (Note that no object may have type
16690 `void', so this only applies to function return types). */
16691 || code == VOID_TYPE)
16692 return;
16694 type_die = modified_type_die (type,
16695 decl_const || TYPE_READONLY (type),
16696 decl_volatile || TYPE_VOLATILE (type),
16697 context_die);
16699 if (type_die != NULL)
16700 add_AT_die_ref (object_die, DW_AT_type, type_die);
16703 /* Given an object die, add the calling convention attribute for the
16704 function call type. */
16705 static void
16706 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16708 enum dwarf_calling_convention value = DW_CC_normal;
16710 value = ((enum dwarf_calling_convention)
16711 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16713 if (is_fortran ()
16714 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16716 /* DWARF 2 doesn't provide a way to identify a program's source-level
16717 entry point. DW_AT_calling_convention attributes are only meant
16718 to describe functions' calling conventions. However, lacking a
16719 better way to signal the Fortran main program, we used this for
16720 a long time, following existing custom. Now, DWARF 4 has
16721 DW_AT_main_subprogram, which we add below, but some tools still
16722 rely on the old way, which we thus keep. */
16723 value = DW_CC_program;
16725 if (dwarf_version >= 4 || !dwarf_strict)
16726 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16729 /* Only add the attribute if the backend requests it, and
16730 is not DW_CC_normal. */
16731 if (value && (value != DW_CC_normal))
16732 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16735 /* Given a tree pointer to a struct, class, union, or enum type node, return
16736 a pointer to the (string) tag name for the given type, or zero if the type
16737 was declared without a tag. */
16739 static const char *
16740 type_tag (const_tree type)
16742 const char *name = 0;
16744 if (TYPE_NAME (type) != 0)
16746 tree t = 0;
16748 /* Find the IDENTIFIER_NODE for the type name. */
16749 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16750 && !TYPE_NAMELESS (type))
16751 t = TYPE_NAME (type);
16753 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16754 a TYPE_DECL node, regardless of whether or not a `typedef' was
16755 involved. */
16756 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16757 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16759 /* We want to be extra verbose. Don't call dwarf_name if
16760 DECL_NAME isn't set. The default hook for decl_printable_name
16761 doesn't like that, and in this context it's correct to return
16762 0, instead of "<anonymous>" or the like. */
16763 if (DECL_NAME (TYPE_NAME (type))
16764 && !DECL_NAMELESS (TYPE_NAME (type)))
16765 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16768 /* Now get the name as a string, or invent one. */
16769 if (!name && t != 0)
16770 name = IDENTIFIER_POINTER (t);
16773 return (name == 0 || *name == '\0') ? 0 : name;
16776 /* Return the type associated with a data member, make a special check
16777 for bit field types. */
16779 static inline tree
16780 member_declared_type (const_tree member)
16782 return (DECL_BIT_FIELD_TYPE (member)
16783 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16786 /* Get the decl's label, as described by its RTL. This may be different
16787 from the DECL_NAME name used in the source file. */
16789 #if 0
16790 static const char *
16791 decl_start_label (tree decl)
16793 rtx x;
16794 const char *fnname;
16796 x = DECL_RTL (decl);
16797 gcc_assert (MEM_P (x));
16799 x = XEXP (x, 0);
16800 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16802 fnname = XSTR (x, 0);
16803 return fnname;
16805 #endif
16807 /* These routines generate the internal representation of the DIE's for
16808 the compilation unit. Debugging information is collected by walking
16809 the declaration trees passed in from dwarf2out_decl(). */
16811 static void
16812 gen_array_type_die (tree type, dw_die_ref context_die)
16814 dw_die_ref scope_die = scope_die_for (type, context_die);
16815 dw_die_ref array_die;
16817 /* GNU compilers represent multidimensional array types as sequences of one
16818 dimensional array types whose element types are themselves array types.
16819 We sometimes squish that down to a single array_type DIE with multiple
16820 subscripts in the Dwarf debugging info. The draft Dwarf specification
16821 say that we are allowed to do this kind of compression in C, because
16822 there is no difference between an array of arrays and a multidimensional
16823 array. We don't do this for Ada to remain as close as possible to the
16824 actual representation, which is especially important against the language
16825 flexibilty wrt arrays of variable size. */
16827 bool collapse_nested_arrays = !is_ada ();
16828 tree element_type;
16830 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16831 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16832 if (TYPE_STRING_FLAG (type)
16833 && TREE_CODE (type) == ARRAY_TYPE
16834 && is_fortran ()
16835 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16837 HOST_WIDE_INT size;
16839 array_die = new_die (DW_TAG_string_type, scope_die, type);
16840 add_name_attribute (array_die, type_tag (type));
16841 equate_type_number_to_die (type, array_die);
16842 size = int_size_in_bytes (type);
16843 if (size >= 0)
16844 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16845 else if (TYPE_DOMAIN (type) != NULL_TREE
16846 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16847 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16849 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16850 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16852 size = int_size_in_bytes (TREE_TYPE (szdecl));
16853 if (loc && size > 0)
16855 add_AT_location_description (array_die, DW_AT_string_length, loc);
16856 if (size != DWARF2_ADDR_SIZE)
16857 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16860 return;
16863 array_die = new_die (DW_TAG_array_type, scope_die, type);
16864 add_name_attribute (array_die, type_tag (type));
16865 equate_type_number_to_die (type, array_die);
16867 if (TREE_CODE (type) == VECTOR_TYPE)
16868 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16870 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16871 if (is_fortran ()
16872 && TREE_CODE (type) == ARRAY_TYPE
16873 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16874 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16875 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16877 #if 0
16878 /* We default the array ordering. SDB will probably do
16879 the right things even if DW_AT_ordering is not present. It's not even
16880 an issue until we start to get into multidimensional arrays anyway. If
16881 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16882 then we'll have to put the DW_AT_ordering attribute back in. (But if
16883 and when we find out that we need to put these in, we will only do so
16884 for multidimensional arrays. */
16885 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16886 #endif
16888 if (TREE_CODE (type) == VECTOR_TYPE)
16890 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16891 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16892 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16893 add_bound_info (subrange_die, DW_AT_upper_bound,
16894 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16896 else
16897 add_subscript_info (array_die, type, collapse_nested_arrays);
16899 /* Add representation of the type of the elements of this array type and
16900 emit the corresponding DIE if we haven't done it already. */
16901 element_type = TREE_TYPE (type);
16902 if (collapse_nested_arrays)
16903 while (TREE_CODE (element_type) == ARRAY_TYPE)
16905 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16906 break;
16907 element_type = TREE_TYPE (element_type);
16910 add_type_attribute (array_die, element_type, 0, 0, context_die);
16912 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16913 if (TYPE_ARTIFICIAL (type))
16914 add_AT_flag (array_die, DW_AT_artificial, 1);
16916 if (get_AT (array_die, DW_AT_name))
16917 add_pubtype (type, array_die);
16920 static dw_loc_descr_ref
16921 descr_info_loc (tree val, tree base_decl)
16923 HOST_WIDE_INT size;
16924 dw_loc_descr_ref loc, loc2;
16925 enum dwarf_location_atom op;
16927 if (val == base_decl)
16928 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16930 switch (TREE_CODE (val))
16932 CASE_CONVERT:
16933 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16934 case VAR_DECL:
16935 return loc_descriptor_from_tree (val, 0);
16936 case INTEGER_CST:
16937 if (host_integerp (val, 0))
16938 return int_loc_descriptor (tree_low_cst (val, 0));
16939 break;
16940 case INDIRECT_REF:
16941 size = int_size_in_bytes (TREE_TYPE (val));
16942 if (size < 0)
16943 break;
16944 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16945 if (!loc)
16946 break;
16947 if (size == DWARF2_ADDR_SIZE)
16948 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16949 else
16950 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16951 return loc;
16952 case POINTER_PLUS_EXPR:
16953 case PLUS_EXPR:
16954 if (host_integerp (TREE_OPERAND (val, 1), 1)
16955 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16956 < 16384)
16958 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16959 if (!loc)
16960 break;
16961 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16963 else
16965 op = DW_OP_plus;
16966 do_binop:
16967 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16968 if (!loc)
16969 break;
16970 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16971 if (!loc2)
16972 break;
16973 add_loc_descr (&loc, loc2);
16974 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16976 return loc;
16977 case MINUS_EXPR:
16978 op = DW_OP_minus;
16979 goto do_binop;
16980 case MULT_EXPR:
16981 op = DW_OP_mul;
16982 goto do_binop;
16983 case EQ_EXPR:
16984 op = DW_OP_eq;
16985 goto do_binop;
16986 case NE_EXPR:
16987 op = DW_OP_ne;
16988 goto do_binop;
16989 default:
16990 break;
16992 return NULL;
16995 static void
16996 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16997 tree val, tree base_decl)
16999 dw_loc_descr_ref loc;
17001 if (host_integerp (val, 0))
17003 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17004 return;
17007 loc = descr_info_loc (val, base_decl);
17008 if (!loc)
17009 return;
17011 add_AT_loc (die, attr, loc);
17014 /* This routine generates DIE for array with hidden descriptor, details
17015 are filled into *info by a langhook. */
17017 static void
17018 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17019 dw_die_ref context_die)
17021 dw_die_ref scope_die = scope_die_for (type, context_die);
17022 dw_die_ref array_die;
17023 int dim;
17025 array_die = new_die (DW_TAG_array_type, scope_die, type);
17026 add_name_attribute (array_die, type_tag (type));
17027 equate_type_number_to_die (type, array_die);
17029 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17030 if (is_fortran ()
17031 && info->ndimensions >= 2)
17032 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17034 if (info->data_location)
17035 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17036 info->base_decl);
17037 if (info->associated)
17038 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17039 info->base_decl);
17040 if (info->allocated)
17041 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17042 info->base_decl);
17044 for (dim = 0; dim < info->ndimensions; dim++)
17046 dw_die_ref subrange_die
17047 = new_die (DW_TAG_subrange_type, array_die, NULL);
17049 if (info->dimen[dim].lower_bound)
17051 /* If it is the default value, omit it. */
17052 int dflt;
17054 if (host_integerp (info->dimen[dim].lower_bound, 0)
17055 && (dflt = lower_bound_default ()) != -1
17056 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
17058 else
17059 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17060 info->dimen[dim].lower_bound,
17061 info->base_decl);
17063 if (info->dimen[dim].upper_bound)
17064 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17065 info->dimen[dim].upper_bound,
17066 info->base_decl);
17067 if (info->dimen[dim].stride)
17068 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17069 info->dimen[dim].stride,
17070 info->base_decl);
17073 gen_type_die (info->element_type, context_die);
17074 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17076 if (get_AT (array_die, DW_AT_name))
17077 add_pubtype (type, array_die);
17080 #if 0
17081 static void
17082 gen_entry_point_die (tree decl, dw_die_ref context_die)
17084 tree origin = decl_ultimate_origin (decl);
17085 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17087 if (origin != NULL)
17088 add_abstract_origin_attribute (decl_die, origin);
17089 else
17091 add_name_and_src_coords_attributes (decl_die, decl);
17092 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17093 0, 0, context_die);
17096 if (DECL_ABSTRACT (decl))
17097 equate_decl_number_to_die (decl, decl_die);
17098 else
17099 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17101 #endif
17103 /* Walk through the list of incomplete types again, trying once more to
17104 emit full debugging info for them. */
17106 static void
17107 retry_incomplete_types (void)
17109 int i;
17111 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17112 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17113 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17116 /* Determine what tag to use for a record type. */
17118 static enum dwarf_tag
17119 record_type_tag (tree type)
17121 if (! lang_hooks.types.classify_record)
17122 return DW_TAG_structure_type;
17124 switch (lang_hooks.types.classify_record (type))
17126 case RECORD_IS_STRUCT:
17127 return DW_TAG_structure_type;
17129 case RECORD_IS_CLASS:
17130 return DW_TAG_class_type;
17132 case RECORD_IS_INTERFACE:
17133 if (dwarf_version >= 3 || !dwarf_strict)
17134 return DW_TAG_interface_type;
17135 return DW_TAG_structure_type;
17137 default:
17138 gcc_unreachable ();
17142 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17143 include all of the information about the enumeration values also. Each
17144 enumerated type name/value is listed as a child of the enumerated type
17145 DIE. */
17147 static dw_die_ref
17148 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17150 dw_die_ref type_die = lookup_type_die (type);
17152 if (type_die == NULL)
17154 type_die = new_die (DW_TAG_enumeration_type,
17155 scope_die_for (type, context_die), type);
17156 equate_type_number_to_die (type, type_die);
17157 add_name_attribute (type_die, type_tag (type));
17158 if (dwarf_version >= 4 || !dwarf_strict)
17160 if (ENUM_IS_SCOPED (type))
17161 add_AT_flag (type_die, DW_AT_enum_class, 1);
17162 if (ENUM_IS_OPAQUE (type))
17163 add_AT_flag (type_die, DW_AT_declaration, 1);
17166 else if (! TYPE_SIZE (type))
17167 return type_die;
17168 else
17169 remove_AT (type_die, DW_AT_declaration);
17171 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17172 given enum type is incomplete, do not generate the DW_AT_byte_size
17173 attribute or the DW_AT_element_list attribute. */
17174 if (TYPE_SIZE (type))
17176 tree link;
17178 TREE_ASM_WRITTEN (type) = 1;
17179 add_byte_size_attribute (type_die, type);
17180 if (TYPE_STUB_DECL (type) != NULL_TREE)
17182 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17183 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17186 /* If the first reference to this type was as the return type of an
17187 inline function, then it may not have a parent. Fix this now. */
17188 if (type_die->die_parent == NULL)
17189 add_child_die (scope_die_for (type, context_die), type_die);
17191 for (link = TYPE_VALUES (type);
17192 link != NULL; link = TREE_CHAIN (link))
17194 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17195 tree value = TREE_VALUE (link);
17197 add_name_attribute (enum_die,
17198 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17200 if (TREE_CODE (value) == CONST_DECL)
17201 value = DECL_INITIAL (value);
17203 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value)))
17204 && (simple_type_size_in_bits (TREE_TYPE (value))
17205 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0)))
17206 /* DWARF2 does not provide a way of indicating whether or
17207 not enumeration constants are signed or unsigned. GDB
17208 always assumes the values are signed, so we output all
17209 values as if they were signed. That means that
17210 enumeration constants with very large unsigned values
17211 will appear to have negative values in the debugger.
17213 TODO: the above comment is wrong, DWARF2 does provide
17214 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17215 This should be re-worked to use correct signed/unsigned
17216 int/double tags for all cases, instead of always treating as
17217 signed. */
17218 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17219 else
17220 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17221 that here. */
17222 add_AT_double (enum_die, DW_AT_const_value,
17223 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17226 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17227 if (TYPE_ARTIFICIAL (type))
17228 add_AT_flag (type_die, DW_AT_artificial, 1);
17230 else
17231 add_AT_flag (type_die, DW_AT_declaration, 1);
17233 add_pubtype (type, type_die);
17235 return type_die;
17238 /* Generate a DIE to represent either a real live formal parameter decl or to
17239 represent just the type of some formal parameter position in some function
17240 type.
17242 Note that this routine is a bit unusual because its argument may be a
17243 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17244 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17245 node. If it's the former then this function is being called to output a
17246 DIE to represent a formal parameter object (or some inlining thereof). If
17247 it's the latter, then this function is only being called to output a
17248 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17249 argument type of some subprogram type.
17250 If EMIT_NAME_P is true, name and source coordinate attributes
17251 are emitted. */
17253 static dw_die_ref
17254 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17255 dw_die_ref context_die)
17257 tree node_or_origin = node ? node : origin;
17258 tree ultimate_origin;
17259 dw_die_ref parm_die
17260 = new_die (DW_TAG_formal_parameter, context_die, node);
17262 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17264 case tcc_declaration:
17265 ultimate_origin = decl_ultimate_origin (node_or_origin);
17266 if (node || ultimate_origin)
17267 origin = ultimate_origin;
17268 if (origin != NULL)
17269 add_abstract_origin_attribute (parm_die, origin);
17270 else if (emit_name_p)
17271 add_name_and_src_coords_attributes (parm_die, node);
17272 if (origin == NULL
17273 || (! DECL_ABSTRACT (node_or_origin)
17274 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17275 decl_function_context
17276 (node_or_origin))))
17278 tree type = TREE_TYPE (node_or_origin);
17279 if (decl_by_reference_p (node_or_origin))
17280 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17281 context_die);
17282 else
17283 add_type_attribute (parm_die, type,
17284 TREE_READONLY (node_or_origin),
17285 TREE_THIS_VOLATILE (node_or_origin),
17286 context_die);
17288 if (origin == NULL && DECL_ARTIFICIAL (node))
17289 add_AT_flag (parm_die, DW_AT_artificial, 1);
17291 if (node && node != origin)
17292 equate_decl_number_to_die (node, parm_die);
17293 if (! DECL_ABSTRACT (node_or_origin))
17294 add_location_or_const_value_attribute (parm_die, node_or_origin,
17295 node == NULL, DW_AT_location);
17297 break;
17299 case tcc_type:
17300 /* We were called with some kind of a ..._TYPE node. */
17301 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17302 break;
17304 default:
17305 gcc_unreachable ();
17308 return parm_die;
17311 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17312 children DW_TAG_formal_parameter DIEs representing the arguments of the
17313 parameter pack.
17315 PARM_PACK must be a function parameter pack.
17316 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17317 must point to the subsequent arguments of the function PACK_ARG belongs to.
17318 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17319 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17320 following the last one for which a DIE was generated. */
17322 static dw_die_ref
17323 gen_formal_parameter_pack_die (tree parm_pack,
17324 tree pack_arg,
17325 dw_die_ref subr_die,
17326 tree *next_arg)
17328 tree arg;
17329 dw_die_ref parm_pack_die;
17331 gcc_assert (parm_pack
17332 && lang_hooks.function_parameter_pack_p (parm_pack)
17333 && subr_die);
17335 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17336 add_src_coords_attributes (parm_pack_die, parm_pack);
17338 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17340 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17341 parm_pack))
17342 break;
17343 gen_formal_parameter_die (arg, NULL,
17344 false /* Don't emit name attribute. */,
17345 parm_pack_die);
17347 if (next_arg)
17348 *next_arg = arg;
17349 return parm_pack_die;
17352 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17353 at the end of an (ANSI prototyped) formal parameters list. */
17355 static void
17356 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17358 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17361 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17362 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17363 parameters as specified in some function type specification (except for
17364 those which appear as part of a function *definition*). */
17366 static void
17367 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17369 tree link;
17370 tree formal_type = NULL;
17371 tree first_parm_type;
17372 tree arg;
17374 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17376 arg = DECL_ARGUMENTS (function_or_method_type);
17377 function_or_method_type = TREE_TYPE (function_or_method_type);
17379 else
17380 arg = NULL_TREE;
17382 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17384 /* Make our first pass over the list of formal parameter types and output a
17385 DW_TAG_formal_parameter DIE for each one. */
17386 for (link = first_parm_type; link; )
17388 dw_die_ref parm_die;
17390 formal_type = TREE_VALUE (link);
17391 if (formal_type == void_type_node)
17392 break;
17394 /* Output a (nameless) DIE to represent the formal parameter itself. */
17395 parm_die = gen_formal_parameter_die (formal_type, NULL,
17396 true /* Emit name attribute. */,
17397 context_die);
17398 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17399 && link == first_parm_type)
17401 add_AT_flag (parm_die, DW_AT_artificial, 1);
17402 if (dwarf_version >= 3 || !dwarf_strict)
17403 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17405 else if (arg && DECL_ARTIFICIAL (arg))
17406 add_AT_flag (parm_die, DW_AT_artificial, 1);
17408 link = TREE_CHAIN (link);
17409 if (arg)
17410 arg = DECL_CHAIN (arg);
17413 /* If this function type has an ellipsis, add a
17414 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17415 if (formal_type != void_type_node)
17416 gen_unspecified_parameters_die (function_or_method_type, context_die);
17418 /* Make our second (and final) pass over the list of formal parameter types
17419 and output DIEs to represent those types (as necessary). */
17420 for (link = TYPE_ARG_TYPES (function_or_method_type);
17421 link && TREE_VALUE (link);
17422 link = TREE_CHAIN (link))
17423 gen_type_die (TREE_VALUE (link), context_die);
17426 /* We want to generate the DIE for TYPE so that we can generate the
17427 die for MEMBER, which has been defined; we will need to refer back
17428 to the member declaration nested within TYPE. If we're trying to
17429 generate minimal debug info for TYPE, processing TYPE won't do the
17430 trick; we need to attach the member declaration by hand. */
17432 static void
17433 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17435 gen_type_die (type, context_die);
17437 /* If we're trying to avoid duplicate debug info, we may not have
17438 emitted the member decl for this function. Emit it now. */
17439 if (TYPE_STUB_DECL (type)
17440 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17441 && ! lookup_decl_die (member))
17443 dw_die_ref type_die;
17444 gcc_assert (!decl_ultimate_origin (member));
17446 push_decl_scope (type);
17447 type_die = lookup_type_die_strip_naming_typedef (type);
17448 if (TREE_CODE (member) == FUNCTION_DECL)
17449 gen_subprogram_die (member, type_die);
17450 else if (TREE_CODE (member) == FIELD_DECL)
17452 /* Ignore the nameless fields that are used to skip bits but handle
17453 C++ anonymous unions and structs. */
17454 if (DECL_NAME (member) != NULL_TREE
17455 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17456 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17458 gen_type_die (member_declared_type (member), type_die);
17459 gen_field_die (member, type_die);
17462 else
17463 gen_variable_die (member, NULL_TREE, type_die);
17465 pop_decl_scope ();
17469 /* Forward declare these functions, because they are mutually recursive
17470 with their set_block_* pairing functions. */
17471 static void set_decl_origin_self (tree);
17472 static void set_decl_abstract_flags (tree, int);
17474 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17475 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17476 that it points to the node itself, thus indicating that the node is its
17477 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17478 the given node is NULL, recursively descend the decl/block tree which
17479 it is the root of, and for each other ..._DECL or BLOCK node contained
17480 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17481 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17482 values to point to themselves. */
17484 static void
17485 set_block_origin_self (tree stmt)
17487 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17489 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17492 tree local_decl;
17494 for (local_decl = BLOCK_VARS (stmt);
17495 local_decl != NULL_TREE;
17496 local_decl = DECL_CHAIN (local_decl))
17497 if (! DECL_EXTERNAL (local_decl))
17498 set_decl_origin_self (local_decl); /* Potential recursion. */
17502 tree subblock;
17504 for (subblock = BLOCK_SUBBLOCKS (stmt);
17505 subblock != NULL_TREE;
17506 subblock = BLOCK_CHAIN (subblock))
17507 set_block_origin_self (subblock); /* Recurse. */
17512 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17513 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17514 node to so that it points to the node itself, thus indicating that the
17515 node represents its own (abstract) origin. Additionally, if the
17516 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17517 the decl/block tree of which the given node is the root of, and for
17518 each other ..._DECL or BLOCK node contained therein whose
17519 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17520 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17521 point to themselves. */
17523 static void
17524 set_decl_origin_self (tree decl)
17526 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17528 DECL_ABSTRACT_ORIGIN (decl) = decl;
17529 if (TREE_CODE (decl) == FUNCTION_DECL)
17531 tree arg;
17533 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17534 DECL_ABSTRACT_ORIGIN (arg) = arg;
17535 if (DECL_INITIAL (decl) != NULL_TREE
17536 && DECL_INITIAL (decl) != error_mark_node)
17537 set_block_origin_self (DECL_INITIAL (decl));
17542 /* Given a pointer to some BLOCK node, and a boolean value to set the
17543 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17544 the given block, and for all local decls and all local sub-blocks
17545 (recursively) which are contained therein. */
17547 static void
17548 set_block_abstract_flags (tree stmt, int setting)
17550 tree local_decl;
17551 tree subblock;
17552 unsigned int i;
17554 BLOCK_ABSTRACT (stmt) = setting;
17556 for (local_decl = BLOCK_VARS (stmt);
17557 local_decl != NULL_TREE;
17558 local_decl = DECL_CHAIN (local_decl))
17559 if (! DECL_EXTERNAL (local_decl))
17560 set_decl_abstract_flags (local_decl, setting);
17562 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17564 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17565 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17566 || TREE_CODE (local_decl) == PARM_DECL)
17567 set_decl_abstract_flags (local_decl, setting);
17570 for (subblock = BLOCK_SUBBLOCKS (stmt);
17571 subblock != NULL_TREE;
17572 subblock = BLOCK_CHAIN (subblock))
17573 set_block_abstract_flags (subblock, setting);
17576 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17577 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17578 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17579 set the abstract flags for all of the parameters, local vars, local
17580 blocks and sub-blocks (recursively) to the same setting. */
17582 static void
17583 set_decl_abstract_flags (tree decl, int setting)
17585 DECL_ABSTRACT (decl) = setting;
17586 if (TREE_CODE (decl) == FUNCTION_DECL)
17588 tree arg;
17590 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17591 DECL_ABSTRACT (arg) = setting;
17592 if (DECL_INITIAL (decl) != NULL_TREE
17593 && DECL_INITIAL (decl) != error_mark_node)
17594 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17598 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17599 may later generate inlined and/or out-of-line instances of. */
17601 static void
17602 dwarf2out_abstract_function (tree decl)
17604 dw_die_ref old_die;
17605 tree save_fn;
17606 tree context;
17607 int was_abstract;
17608 htab_t old_decl_loc_table;
17609 htab_t old_cached_dw_loc_list_table;
17610 int old_call_site_count, old_tail_call_site_count;
17611 struct call_arg_loc_node *old_call_arg_locations;
17613 /* Make sure we have the actual abstract inline, not a clone. */
17614 decl = DECL_ORIGIN (decl);
17616 old_die = lookup_decl_die (decl);
17617 if (old_die && get_AT (old_die, DW_AT_inline))
17618 /* We've already generated the abstract instance. */
17619 return;
17621 /* We can be called while recursively when seeing block defining inlined subroutine
17622 DIE. Be sure to not clobber the outer location table nor use it or we would
17623 get locations in abstract instantces. */
17624 old_decl_loc_table = decl_loc_table;
17625 decl_loc_table = NULL;
17626 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17627 cached_dw_loc_list_table = NULL;
17628 old_call_arg_locations = call_arg_locations;
17629 call_arg_locations = NULL;
17630 old_call_site_count = call_site_count;
17631 call_site_count = -1;
17632 old_tail_call_site_count = tail_call_site_count;
17633 tail_call_site_count = -1;
17635 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17636 we don't get confused by DECL_ABSTRACT. */
17637 if (debug_info_level > DINFO_LEVEL_TERSE)
17639 context = decl_class_context (decl);
17640 if (context)
17641 gen_type_die_for_member
17642 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17645 /* Pretend we've just finished compiling this function. */
17646 save_fn = current_function_decl;
17647 current_function_decl = decl;
17649 was_abstract = DECL_ABSTRACT (decl);
17650 set_decl_abstract_flags (decl, 1);
17651 dwarf2out_decl (decl);
17652 if (! was_abstract)
17653 set_decl_abstract_flags (decl, 0);
17655 current_function_decl = save_fn;
17656 decl_loc_table = old_decl_loc_table;
17657 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17658 call_arg_locations = old_call_arg_locations;
17659 call_site_count = old_call_site_count;
17660 tail_call_site_count = old_tail_call_site_count;
17663 /* Helper function of premark_used_types() which gets called through
17664 htab_traverse.
17666 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17667 marked as unused by prune_unused_types. */
17669 static int
17670 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17672 tree type;
17673 dw_die_ref die;
17675 type = (tree) *slot;
17676 die = lookup_type_die (type);
17677 if (die != NULL)
17678 die->die_perennial_p = 1;
17679 return 1;
17682 /* Helper function of premark_types_used_by_global_vars which gets called
17683 through htab_traverse.
17685 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17686 marked as unused by prune_unused_types. The DIE of the type is marked
17687 only if the global variable using the type will actually be emitted. */
17689 static int
17690 premark_types_used_by_global_vars_helper (void **slot,
17691 void *data ATTRIBUTE_UNUSED)
17693 struct types_used_by_vars_entry *entry;
17694 dw_die_ref die;
17696 entry = (struct types_used_by_vars_entry *) *slot;
17697 gcc_assert (entry->type != NULL
17698 && entry->var_decl != NULL);
17699 die = lookup_type_die (entry->type);
17700 if (die)
17702 /* Ask cgraph if the global variable really is to be emitted.
17703 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17704 struct varpool_node *node = varpool_get_node (entry->var_decl);
17705 if (node && node->symbol.definition)
17707 die->die_perennial_p = 1;
17708 /* Keep the parent DIEs as well. */
17709 while ((die = die->die_parent) && die->die_perennial_p == 0)
17710 die->die_perennial_p = 1;
17713 return 1;
17716 /* Mark all members of used_types_hash as perennial. */
17718 static void
17719 premark_used_types (struct function *fun)
17721 if (fun && fun->used_types_hash)
17722 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17725 /* Mark all members of types_used_by_vars_entry as perennial. */
17727 static void
17728 premark_types_used_by_global_vars (void)
17730 if (types_used_by_vars_hash)
17731 htab_traverse (types_used_by_vars_hash,
17732 premark_types_used_by_global_vars_helper, NULL);
17735 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17736 for CA_LOC call arg loc node. */
17738 static dw_die_ref
17739 gen_call_site_die (tree decl, dw_die_ref subr_die,
17740 struct call_arg_loc_node *ca_loc)
17742 dw_die_ref stmt_die = NULL, die;
17743 tree block = ca_loc->block;
17745 while (block
17746 && block != DECL_INITIAL (decl)
17747 && TREE_CODE (block) == BLOCK)
17749 if (block_map.length () > BLOCK_NUMBER (block))
17750 stmt_die = block_map[BLOCK_NUMBER (block)];
17751 if (stmt_die)
17752 break;
17753 block = BLOCK_SUPERCONTEXT (block);
17755 if (stmt_die == NULL)
17756 stmt_die = subr_die;
17757 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17758 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17759 if (ca_loc->tail_call_p)
17760 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17761 if (ca_loc->symbol_ref)
17763 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17764 if (tdie)
17765 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17766 else
17767 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17769 return die;
17772 /* Generate a DIE to represent a declared function (either file-scope or
17773 block-local). */
17775 static void
17776 gen_subprogram_die (tree decl, dw_die_ref context_die)
17778 tree origin = decl_ultimate_origin (decl);
17779 dw_die_ref subr_die;
17780 tree outer_scope;
17781 dw_die_ref old_die = lookup_decl_die (decl);
17782 int declaration = (current_function_decl != decl
17783 || class_or_namespace_scope_p (context_die));
17785 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17787 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17788 started to generate the abstract instance of an inline, decided to output
17789 its containing class, and proceeded to emit the declaration of the inline
17790 from the member list for the class. If so, DECLARATION takes priority;
17791 we'll get back to the abstract instance when done with the class. */
17793 /* The class-scope declaration DIE must be the primary DIE. */
17794 if (origin && declaration && class_or_namespace_scope_p (context_die))
17796 origin = NULL;
17797 gcc_assert (!old_die);
17800 /* Now that the C++ front end lazily declares artificial member fns, we
17801 might need to retrofit the declaration into its class. */
17802 if (!declaration && !origin && !old_die
17803 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17804 && !class_or_namespace_scope_p (context_die)
17805 && debug_info_level > DINFO_LEVEL_TERSE)
17806 old_die = force_decl_die (decl);
17808 if (origin != NULL)
17810 gcc_assert (!declaration || local_scope_p (context_die));
17812 /* Fixup die_parent for the abstract instance of a nested
17813 inline function. */
17814 if (old_die && old_die->die_parent == NULL)
17815 add_child_die (context_die, old_die);
17817 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17818 add_abstract_origin_attribute (subr_die, origin);
17819 /* This is where the actual code for a cloned function is.
17820 Let's emit linkage name attribute for it. This helps
17821 debuggers to e.g, set breakpoints into
17822 constructors/destructors when the user asks "break
17823 K::K". */
17824 add_linkage_name (subr_die, decl);
17826 else if (old_die)
17828 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17829 struct dwarf_file_data * file_index = lookup_filename (s.file);
17831 if (!get_AT_flag (old_die, DW_AT_declaration)
17832 /* We can have a normal definition following an inline one in the
17833 case of redefinition of GNU C extern inlines.
17834 It seems reasonable to use AT_specification in this case. */
17835 && !get_AT (old_die, DW_AT_inline))
17837 /* Detect and ignore this case, where we are trying to output
17838 something we have already output. */
17839 return;
17842 /* If the definition comes from the same place as the declaration,
17843 maybe use the old DIE. We always want the DIE for this function
17844 that has the *_pc attributes to be under comp_unit_die so the
17845 debugger can find it. We also need to do this for abstract
17846 instances of inlines, since the spec requires the out-of-line copy
17847 to have the same parent. For local class methods, this doesn't
17848 apply; we just use the old DIE. */
17849 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17850 && (DECL_ARTIFICIAL (decl)
17851 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17852 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17853 == (unsigned) s.line))))
17855 subr_die = old_die;
17857 /* Clear out the declaration attribute and the formal parameters.
17858 Do not remove all children, because it is possible that this
17859 declaration die was forced using force_decl_die(). In such
17860 cases die that forced declaration die (e.g. TAG_imported_module)
17861 is one of the children that we do not want to remove. */
17862 remove_AT (subr_die, DW_AT_declaration);
17863 remove_AT (subr_die, DW_AT_object_pointer);
17864 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17866 else
17868 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17869 add_AT_specification (subr_die, old_die);
17870 add_pubname (decl, subr_die);
17871 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17872 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17873 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17874 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17877 else
17879 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17881 if (TREE_PUBLIC (decl))
17882 add_AT_flag (subr_die, DW_AT_external, 1);
17884 add_name_and_src_coords_attributes (subr_die, decl);
17885 add_pubname (decl, subr_die);
17886 if (debug_info_level > DINFO_LEVEL_TERSE)
17888 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17889 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17890 0, 0, context_die);
17893 add_pure_or_virtual_attribute (subr_die, decl);
17894 if (DECL_ARTIFICIAL (decl))
17895 add_AT_flag (subr_die, DW_AT_artificial, 1);
17897 add_accessibility_attribute (subr_die, decl);
17900 if (declaration)
17902 if (!old_die || !get_AT (old_die, DW_AT_inline))
17904 add_AT_flag (subr_die, DW_AT_declaration, 1);
17906 /* If this is an explicit function declaration then generate
17907 a DW_AT_explicit attribute. */
17908 if (lang_hooks.decls.function_decl_explicit_p (decl)
17909 && (dwarf_version >= 3 || !dwarf_strict))
17910 add_AT_flag (subr_die, DW_AT_explicit, 1);
17912 /* The first time we see a member function, it is in the context of
17913 the class to which it belongs. We make sure of this by emitting
17914 the class first. The next time is the definition, which is
17915 handled above. The two may come from the same source text.
17917 Note that force_decl_die() forces function declaration die. It is
17918 later reused to represent definition. */
17919 equate_decl_number_to_die (decl, subr_die);
17922 else if (DECL_ABSTRACT (decl))
17924 if (DECL_DECLARED_INLINE_P (decl))
17926 if (cgraph_function_possibly_inlined_p (decl))
17927 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17928 else
17929 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17931 else
17933 if (cgraph_function_possibly_inlined_p (decl))
17934 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17935 else
17936 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17939 if (DECL_DECLARED_INLINE_P (decl)
17940 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17941 add_AT_flag (subr_die, DW_AT_artificial, 1);
17943 equate_decl_number_to_die (decl, subr_die);
17945 else if (!DECL_EXTERNAL (decl))
17947 HOST_WIDE_INT cfa_fb_offset;
17948 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17950 if (!old_die || !get_AT (old_die, DW_AT_inline))
17951 equate_decl_number_to_die (decl, subr_die);
17953 gcc_checking_assert (fun);
17954 if (!flag_reorder_blocks_and_partition)
17956 dw_fde_ref fde = fun->fde;
17957 if (fde->dw_fde_begin)
17959 /* We have already generated the labels. */
17960 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17961 fde->dw_fde_end, false);
17963 else
17965 /* Create start/end labels and add the range. */
17966 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17967 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17968 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17969 current_function_funcdef_no);
17970 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17971 current_function_funcdef_no);
17972 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17973 false);
17976 #if VMS_DEBUGGING_INFO
17977 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17978 Section 2.3 Prologue and Epilogue Attributes:
17979 When a breakpoint is set on entry to a function, it is generally
17980 desirable for execution to be suspended, not on the very first
17981 instruction of the function, but rather at a point after the
17982 function's frame has been set up, after any language defined local
17983 declaration processing has been completed, and before execution of
17984 the first statement of the function begins. Debuggers generally
17985 cannot properly determine where this point is. Similarly for a
17986 breakpoint set on exit from a function. The prologue and epilogue
17987 attributes allow a compiler to communicate the location(s) to use. */
17990 if (fde->dw_fde_vms_end_prologue)
17991 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17992 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17994 if (fde->dw_fde_vms_begin_epilogue)
17995 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17996 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17998 #endif
18001 else
18003 /* Generate pubnames entries for the split function code ranges. */
18004 dw_fde_ref fde = fun->fde;
18006 if (fde->dw_fde_second_begin)
18008 if (dwarf_version >= 3 || !dwarf_strict)
18010 /* We should use ranges for non-contiguous code section
18011 addresses. Use the actual code range for the initial
18012 section, since the HOT/COLD labels might precede an
18013 alignment offset. */
18014 bool range_list_added = false;
18015 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18016 fde->dw_fde_end, &range_list_added,
18017 false);
18018 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18019 fde->dw_fde_second_end,
18020 &range_list_added, false);
18021 if (range_list_added)
18022 add_ranges (NULL);
18024 else
18026 /* There is no real support in DW2 for this .. so we make
18027 a work-around. First, emit the pub name for the segment
18028 containing the function label. Then make and emit a
18029 simplified subprogram DIE for the second segment with the
18030 name pre-fixed by __hot/cold_sect_of_. We use the same
18031 linkage name for the second die so that gdb will find both
18032 sections when given "b foo". */
18033 const char *name = NULL;
18034 tree decl_name = DECL_NAME (decl);
18035 dw_die_ref seg_die;
18037 /* Do the 'primary' section. */
18038 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18039 fde->dw_fde_end, false);
18041 /* Build a minimal DIE for the secondary section. */
18042 seg_die = new_die (DW_TAG_subprogram,
18043 subr_die->die_parent, decl);
18045 if (TREE_PUBLIC (decl))
18046 add_AT_flag (seg_die, DW_AT_external, 1);
18048 if (decl_name != NULL
18049 && IDENTIFIER_POINTER (decl_name) != NULL)
18051 name = dwarf2_name (decl, 1);
18052 if (! DECL_ARTIFICIAL (decl))
18053 add_src_coords_attributes (seg_die, decl);
18055 add_linkage_name (seg_die, decl);
18057 gcc_assert (name != NULL);
18058 add_pure_or_virtual_attribute (seg_die, decl);
18059 if (DECL_ARTIFICIAL (decl))
18060 add_AT_flag (seg_die, DW_AT_artificial, 1);
18062 name = concat ("__second_sect_of_", name, NULL);
18063 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18064 fde->dw_fde_second_end, false);
18065 add_name_attribute (seg_die, name);
18066 if (want_pubnames ())
18067 add_pubname_string (name, seg_die);
18070 else
18071 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18072 false);
18075 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18077 /* We define the "frame base" as the function's CFA. This is more
18078 convenient for several reasons: (1) It's stable across the prologue
18079 and epilogue, which makes it better than just a frame pointer,
18080 (2) With dwarf3, there exists a one-byte encoding that allows us
18081 to reference the .debug_frame data by proxy, but failing that,
18082 (3) We can at least reuse the code inspection and interpretation
18083 code that determines the CFA position at various points in the
18084 function. */
18085 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18087 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18088 add_AT_loc (subr_die, DW_AT_frame_base, op);
18090 else
18092 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18093 if (list->dw_loc_next)
18094 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18095 else
18096 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18099 /* Compute a displacement from the "steady-state frame pointer" to
18100 the CFA. The former is what all stack slots and argument slots
18101 will reference in the rtl; the latter is what we've told the
18102 debugger about. We'll need to adjust all frame_base references
18103 by this displacement. */
18104 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18106 if (fun->static_chain_decl)
18107 add_AT_location_description (subr_die, DW_AT_static_link,
18108 loc_list_from_tree (fun->static_chain_decl, 2));
18111 /* Generate child dies for template paramaters. */
18112 if (debug_info_level > DINFO_LEVEL_TERSE)
18113 gen_generic_params_dies (decl);
18115 /* Now output descriptions of the arguments for this function. This gets
18116 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18117 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18118 `...' at the end of the formal parameter list. In order to find out if
18119 there was a trailing ellipsis or not, we must instead look at the type
18120 associated with the FUNCTION_DECL. This will be a node of type
18121 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18122 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18123 an ellipsis at the end. */
18125 /* In the case where we are describing a mere function declaration, all we
18126 need to do here (and all we *can* do here) is to describe the *types* of
18127 its formal parameters. */
18128 if (debug_info_level <= DINFO_LEVEL_TERSE)
18130 else if (declaration)
18131 gen_formal_types_die (decl, subr_die);
18132 else
18134 /* Generate DIEs to represent all known formal parameters. */
18135 tree parm = DECL_ARGUMENTS (decl);
18136 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18137 tree generic_decl_parm = generic_decl
18138 ? DECL_ARGUMENTS (generic_decl)
18139 : NULL;
18141 /* Now we want to walk the list of parameters of the function and
18142 emit their relevant DIEs.
18144 We consider the case of DECL being an instance of a generic function
18145 as well as it being a normal function.
18147 If DECL is an instance of a generic function we walk the
18148 parameters of the generic function declaration _and_ the parameters of
18149 DECL itself. This is useful because we want to emit specific DIEs for
18150 function parameter packs and those are declared as part of the
18151 generic function declaration. In that particular case,
18152 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18153 That DIE has children DIEs representing the set of arguments
18154 of the pack. Note that the set of pack arguments can be empty.
18155 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18156 children DIE.
18158 Otherwise, we just consider the parameters of DECL. */
18159 while (generic_decl_parm || parm)
18161 if (generic_decl_parm
18162 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18163 gen_formal_parameter_pack_die (generic_decl_parm,
18164 parm, subr_die,
18165 &parm);
18166 else if (parm)
18168 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18170 if (parm == DECL_ARGUMENTS (decl)
18171 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18172 && parm_die
18173 && (dwarf_version >= 3 || !dwarf_strict))
18174 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18176 parm = DECL_CHAIN (parm);
18179 if (generic_decl_parm)
18180 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18183 /* Decide whether we need an unspecified_parameters DIE at the end.
18184 There are 2 more cases to do this for: 1) the ansi ... declaration -
18185 this is detectable when the end of the arg list is not a
18186 void_type_node 2) an unprototyped function declaration (not a
18187 definition). This just means that we have no info about the
18188 parameters at all. */
18189 if (prototype_p (TREE_TYPE (decl)))
18191 /* This is the prototyped case, check for.... */
18192 if (stdarg_p (TREE_TYPE (decl)))
18193 gen_unspecified_parameters_die (decl, subr_die);
18195 else if (DECL_INITIAL (decl) == NULL_TREE)
18196 gen_unspecified_parameters_die (decl, subr_die);
18199 /* Output Dwarf info for all of the stuff within the body of the function
18200 (if it has one - it may be just a declaration). */
18201 outer_scope = DECL_INITIAL (decl);
18203 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18204 a function. This BLOCK actually represents the outermost binding contour
18205 for the function, i.e. the contour in which the function's formal
18206 parameters and labels get declared. Curiously, it appears that the front
18207 end doesn't actually put the PARM_DECL nodes for the current function onto
18208 the BLOCK_VARS list for this outer scope, but are strung off of the
18209 DECL_ARGUMENTS list for the function instead.
18211 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18212 the LABEL_DECL nodes for the function however, and we output DWARF info
18213 for those in decls_for_scope. Just within the `outer_scope' there will be
18214 a BLOCK node representing the function's outermost pair of curly braces,
18215 and any blocks used for the base and member initializers of a C++
18216 constructor function. */
18217 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18219 int call_site_note_count = 0;
18220 int tail_call_site_note_count = 0;
18222 /* Emit a DW_TAG_variable DIE for a named return value. */
18223 if (DECL_NAME (DECL_RESULT (decl)))
18224 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18226 current_function_has_inlines = 0;
18227 decls_for_scope (outer_scope, subr_die, 0);
18229 if (call_arg_locations && !dwarf_strict)
18231 struct call_arg_loc_node *ca_loc;
18232 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18234 dw_die_ref die = NULL;
18235 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18236 rtx arg, next_arg;
18238 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18239 arg; arg = next_arg)
18241 dw_loc_descr_ref reg, val;
18242 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18243 dw_die_ref cdie, tdie = NULL;
18245 next_arg = XEXP (arg, 1);
18246 if (REG_P (XEXP (XEXP (arg, 0), 0))
18247 && next_arg
18248 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18249 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18250 && REGNO (XEXP (XEXP (arg, 0), 0))
18251 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18252 next_arg = XEXP (next_arg, 1);
18253 if (mode == VOIDmode)
18255 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18256 if (mode == VOIDmode)
18257 mode = GET_MODE (XEXP (arg, 0));
18259 if (mode == VOIDmode || mode == BLKmode)
18260 continue;
18261 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18263 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18264 tloc = XEXP (XEXP (arg, 0), 1);
18265 continue;
18267 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18268 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18270 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18271 tlocc = XEXP (XEXP (arg, 0), 1);
18272 continue;
18274 reg = NULL;
18275 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18276 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18277 VAR_INIT_STATUS_INITIALIZED);
18278 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18280 rtx mem = XEXP (XEXP (arg, 0), 0);
18281 reg = mem_loc_descriptor (XEXP (mem, 0),
18282 get_address_mode (mem),
18283 GET_MODE (mem),
18284 VAR_INIT_STATUS_INITIALIZED);
18286 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18287 == DEBUG_PARAMETER_REF)
18289 tree tdecl
18290 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18291 tdie = lookup_decl_die (tdecl);
18292 if (tdie == NULL)
18293 continue;
18295 else
18296 continue;
18297 if (reg == NULL
18298 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18299 != DEBUG_PARAMETER_REF)
18300 continue;
18301 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18302 VOIDmode,
18303 VAR_INIT_STATUS_INITIALIZED);
18304 if (val == NULL)
18305 continue;
18306 if (die == NULL)
18307 die = gen_call_site_die (decl, subr_die, ca_loc);
18308 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18309 NULL_TREE);
18310 if (reg != NULL)
18311 add_AT_loc (cdie, DW_AT_location, reg);
18312 else if (tdie != NULL)
18313 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18314 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18315 if (next_arg != XEXP (arg, 1))
18317 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18318 if (mode == VOIDmode)
18319 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18320 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18321 0), 1),
18322 mode, VOIDmode,
18323 VAR_INIT_STATUS_INITIALIZED);
18324 if (val != NULL)
18325 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18328 if (die == NULL
18329 && (ca_loc->symbol_ref || tloc))
18330 die = gen_call_site_die (decl, subr_die, ca_loc);
18331 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18333 dw_loc_descr_ref tval = NULL;
18335 if (tloc != NULL_RTX)
18336 tval = mem_loc_descriptor (tloc,
18337 GET_MODE (tloc) == VOIDmode
18338 ? Pmode : GET_MODE (tloc),
18339 VOIDmode,
18340 VAR_INIT_STATUS_INITIALIZED);
18341 if (tval)
18342 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18343 else if (tlocc != NULL_RTX)
18345 tval = mem_loc_descriptor (tlocc,
18346 GET_MODE (tlocc) == VOIDmode
18347 ? Pmode : GET_MODE (tlocc),
18348 VOIDmode,
18349 VAR_INIT_STATUS_INITIALIZED);
18350 if (tval)
18351 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18352 tval);
18355 if (die != NULL)
18357 call_site_note_count++;
18358 if (ca_loc->tail_call_p)
18359 tail_call_site_note_count++;
18363 call_arg_locations = NULL;
18364 call_arg_loc_last = NULL;
18365 if (tail_call_site_count >= 0
18366 && tail_call_site_count == tail_call_site_note_count
18367 && !dwarf_strict)
18369 if (call_site_count >= 0
18370 && call_site_count == call_site_note_count)
18371 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18372 else
18373 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18375 call_site_count = -1;
18376 tail_call_site_count = -1;
18378 /* Add the calling convention attribute if requested. */
18379 add_calling_convention_attribute (subr_die, decl);
18383 /* Returns a hash value for X (which really is a die_struct). */
18385 static hashval_t
18386 common_block_die_table_hash (const void *x)
18388 const_dw_die_ref d = (const_dw_die_ref) x;
18389 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18392 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18393 as decl_id and die_parent of die_struct Y. */
18395 static int
18396 common_block_die_table_eq (const void *x, const void *y)
18398 const_dw_die_ref d = (const_dw_die_ref) x;
18399 const_dw_die_ref e = (const_dw_die_ref) y;
18400 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18403 /* Generate a DIE to represent a declared data object.
18404 Either DECL or ORIGIN must be non-null. */
18406 static void
18407 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18409 HOST_WIDE_INT off = 0;
18410 tree com_decl;
18411 tree decl_or_origin = decl ? decl : origin;
18412 tree ultimate_origin;
18413 dw_die_ref var_die;
18414 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18415 dw_die_ref origin_die;
18416 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18417 || class_or_namespace_scope_p (context_die));
18418 bool specialization_p = false;
18420 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18421 if (decl || ultimate_origin)
18422 origin = ultimate_origin;
18423 com_decl = fortran_common (decl_or_origin, &off);
18425 /* Symbol in common gets emitted as a child of the common block, in the form
18426 of a data member. */
18427 if (com_decl)
18429 dw_die_ref com_die;
18430 dw_loc_list_ref loc;
18431 die_node com_die_arg;
18433 var_die = lookup_decl_die (decl_or_origin);
18434 if (var_die)
18436 if (get_AT (var_die, DW_AT_location) == NULL)
18438 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18439 if (loc)
18441 if (off)
18443 /* Optimize the common case. */
18444 if (single_element_loc_list_p (loc)
18445 && loc->expr->dw_loc_opc == DW_OP_addr
18446 && loc->expr->dw_loc_next == NULL
18447 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18448 == SYMBOL_REF)
18450 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18451 loc->expr->dw_loc_oprnd1.v.val_addr
18452 = plus_constant (GET_MODE (x), x , off);
18454 else
18455 loc_list_plus_const (loc, off);
18457 add_AT_location_description (var_die, DW_AT_location, loc);
18458 remove_AT (var_die, DW_AT_declaration);
18461 return;
18464 if (common_block_die_table == NULL)
18465 common_block_die_table
18466 = htab_create_ggc (10, common_block_die_table_hash,
18467 common_block_die_table_eq, NULL);
18469 com_die_arg.decl_id = DECL_UID (com_decl);
18470 com_die_arg.die_parent = context_die;
18471 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18472 loc = loc_list_from_tree (com_decl, 2);
18473 if (com_die == NULL)
18475 const char *cnam
18476 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18477 void **slot;
18479 com_die = new_die (DW_TAG_common_block, context_die, decl);
18480 add_name_and_src_coords_attributes (com_die, com_decl);
18481 if (loc)
18483 add_AT_location_description (com_die, DW_AT_location, loc);
18484 /* Avoid sharing the same loc descriptor between
18485 DW_TAG_common_block and DW_TAG_variable. */
18486 loc = loc_list_from_tree (com_decl, 2);
18488 else if (DECL_EXTERNAL (decl))
18489 add_AT_flag (com_die, DW_AT_declaration, 1);
18490 if (want_pubnames ())
18491 add_pubname_string (cnam, com_die); /* ??? needed? */
18492 com_die->decl_id = DECL_UID (com_decl);
18493 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18494 *slot = (void *) com_die;
18496 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18498 add_AT_location_description (com_die, DW_AT_location, loc);
18499 loc = loc_list_from_tree (com_decl, 2);
18500 remove_AT (com_die, DW_AT_declaration);
18502 var_die = new_die (DW_TAG_variable, com_die, decl);
18503 add_name_and_src_coords_attributes (var_die, decl);
18504 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18505 TREE_THIS_VOLATILE (decl), context_die);
18506 add_AT_flag (var_die, DW_AT_external, 1);
18507 if (loc)
18509 if (off)
18511 /* Optimize the common case. */
18512 if (single_element_loc_list_p (loc)
18513 && loc->expr->dw_loc_opc == DW_OP_addr
18514 && loc->expr->dw_loc_next == NULL
18515 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18517 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18518 loc->expr->dw_loc_oprnd1.v.val_addr
18519 = plus_constant (GET_MODE (x), x, off);
18521 else
18522 loc_list_plus_const (loc, off);
18524 add_AT_location_description (var_die, DW_AT_location, loc);
18526 else if (DECL_EXTERNAL (decl))
18527 add_AT_flag (var_die, DW_AT_declaration, 1);
18528 equate_decl_number_to_die (decl, var_die);
18529 return;
18532 /* If the compiler emitted a definition for the DECL declaration
18533 and if we already emitted a DIE for it, don't emit a second
18534 DIE for it again. Allow re-declarations of DECLs that are
18535 inside functions, though. */
18536 if (old_die && declaration && !local_scope_p (context_die))
18537 return;
18539 /* For static data members, the declaration in the class is supposed
18540 to have DW_TAG_member tag; the specification should still be
18541 DW_TAG_variable referencing the DW_TAG_member DIE. */
18542 if (declaration && class_scope_p (context_die))
18543 var_die = new_die (DW_TAG_member, context_die, decl);
18544 else
18545 var_die = new_die (DW_TAG_variable, context_die, decl);
18547 origin_die = NULL;
18548 if (origin != NULL)
18549 origin_die = add_abstract_origin_attribute (var_die, origin);
18551 /* Loop unrolling can create multiple blocks that refer to the same
18552 static variable, so we must test for the DW_AT_declaration flag.
18554 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18555 copy decls and set the DECL_ABSTRACT flag on them instead of
18556 sharing them.
18558 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18560 ??? The declare_in_namespace support causes us to get two DIEs for one
18561 variable, both of which are declarations. We want to avoid considering
18562 one to be a specification, so we must test that this DIE is not a
18563 declaration. */
18564 else if (old_die && TREE_STATIC (decl) && ! declaration
18565 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18567 /* This is a definition of a C++ class level static. */
18568 add_AT_specification (var_die, old_die);
18569 specialization_p = true;
18570 if (DECL_NAME (decl))
18572 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18573 struct dwarf_file_data * file_index = lookup_filename (s.file);
18575 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18576 add_AT_file (var_die, DW_AT_decl_file, file_index);
18578 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18579 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18581 if (old_die->die_tag == DW_TAG_member)
18582 add_linkage_name (var_die, decl);
18585 else
18586 add_name_and_src_coords_attributes (var_die, decl);
18588 if ((origin == NULL && !specialization_p)
18589 || (origin != NULL
18590 && !DECL_ABSTRACT (decl_or_origin)
18591 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18592 decl_function_context
18593 (decl_or_origin))))
18595 tree type = TREE_TYPE (decl_or_origin);
18597 if (decl_by_reference_p (decl_or_origin))
18598 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18599 else
18600 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18601 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18604 if (origin == NULL && !specialization_p)
18606 if (TREE_PUBLIC (decl))
18607 add_AT_flag (var_die, DW_AT_external, 1);
18609 if (DECL_ARTIFICIAL (decl))
18610 add_AT_flag (var_die, DW_AT_artificial, 1);
18612 add_accessibility_attribute (var_die, decl);
18615 if (declaration)
18616 add_AT_flag (var_die, DW_AT_declaration, 1);
18618 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18619 equate_decl_number_to_die (decl, var_die);
18621 if (! declaration
18622 && (! DECL_ABSTRACT (decl_or_origin)
18623 /* Local static vars are shared between all clones/inlines,
18624 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18625 already set. */
18626 || (TREE_CODE (decl_or_origin) == VAR_DECL
18627 && TREE_STATIC (decl_or_origin)
18628 && DECL_RTL_SET_P (decl_or_origin)))
18629 /* When abstract origin already has DW_AT_location attribute, no need
18630 to add it again. */
18631 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18633 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18634 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18635 defer_location (decl_or_origin, var_die);
18636 else
18637 add_location_or_const_value_attribute (var_die, decl_or_origin,
18638 decl == NULL, DW_AT_location);
18639 add_pubname (decl_or_origin, var_die);
18641 else
18642 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18645 /* Generate a DIE to represent a named constant. */
18647 static void
18648 gen_const_die (tree decl, dw_die_ref context_die)
18650 dw_die_ref const_die;
18651 tree type = TREE_TYPE (decl);
18653 const_die = new_die (DW_TAG_constant, context_die, decl);
18654 add_name_and_src_coords_attributes (const_die, decl);
18655 add_type_attribute (const_die, type, 1, 0, context_die);
18656 if (TREE_PUBLIC (decl))
18657 add_AT_flag (const_die, DW_AT_external, 1);
18658 if (DECL_ARTIFICIAL (decl))
18659 add_AT_flag (const_die, DW_AT_artificial, 1);
18660 tree_add_const_value_attribute_for_decl (const_die, decl);
18663 /* Generate a DIE to represent a label identifier. */
18665 static void
18666 gen_label_die (tree decl, dw_die_ref context_die)
18668 tree origin = decl_ultimate_origin (decl);
18669 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18670 rtx insn;
18671 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18673 if (origin != NULL)
18674 add_abstract_origin_attribute (lbl_die, origin);
18675 else
18676 add_name_and_src_coords_attributes (lbl_die, decl);
18678 if (DECL_ABSTRACT (decl))
18679 equate_decl_number_to_die (decl, lbl_die);
18680 else
18682 insn = DECL_RTL_IF_SET (decl);
18684 /* Deleted labels are programmer specified labels which have been
18685 eliminated because of various optimizations. We still emit them
18686 here so that it is possible to put breakpoints on them. */
18687 if (insn
18688 && (LABEL_P (insn)
18689 || ((NOTE_P (insn)
18690 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18692 /* When optimization is enabled (via -O) some parts of the compiler
18693 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18694 represent source-level labels which were explicitly declared by
18695 the user. This really shouldn't be happening though, so catch
18696 it if it ever does happen. */
18697 gcc_assert (!INSN_DELETED_P (insn));
18699 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18700 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18702 else if (insn
18703 && NOTE_P (insn)
18704 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18705 && CODE_LABEL_NUMBER (insn) != -1)
18707 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18708 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18713 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18714 attributes to the DIE for a block STMT, to describe where the inlined
18715 function was called from. This is similar to add_src_coords_attributes. */
18717 static inline void
18718 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18720 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18722 if (dwarf_version >= 3 || !dwarf_strict)
18724 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18725 add_AT_unsigned (die, DW_AT_call_line, s.line);
18730 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18731 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18733 static inline void
18734 add_high_low_attributes (tree stmt, dw_die_ref die)
18736 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18738 if (BLOCK_FRAGMENT_CHAIN (stmt)
18739 && (dwarf_version >= 3 || !dwarf_strict))
18741 tree chain, superblock = NULL_TREE;
18742 dw_die_ref pdie;
18743 dw_attr_ref attr = NULL;
18745 if (inlined_function_outer_scope_p (stmt))
18747 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18748 BLOCK_NUMBER (stmt));
18749 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18752 /* Optimize duplicate .debug_ranges lists or even tails of
18753 lists. If this BLOCK has same ranges as its supercontext,
18754 lookup DW_AT_ranges attribute in the supercontext (and
18755 recursively so), verify that the ranges_table contains the
18756 right values and use it instead of adding a new .debug_range. */
18757 for (chain = stmt, pdie = die;
18758 BLOCK_SAME_RANGE (chain);
18759 chain = BLOCK_SUPERCONTEXT (chain))
18761 dw_attr_ref new_attr;
18763 pdie = pdie->die_parent;
18764 if (pdie == NULL)
18765 break;
18766 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18767 break;
18768 new_attr = get_AT (pdie, DW_AT_ranges);
18769 if (new_attr == NULL
18770 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18771 break;
18772 attr = new_attr;
18773 superblock = BLOCK_SUPERCONTEXT (chain);
18775 if (attr != NULL
18776 && (ranges_table[attr->dw_attr_val.v.val_offset
18777 / 2 / DWARF2_ADDR_SIZE].num
18778 == BLOCK_NUMBER (superblock))
18779 && BLOCK_FRAGMENT_CHAIN (superblock))
18781 unsigned long off = attr->dw_attr_val.v.val_offset
18782 / 2 / DWARF2_ADDR_SIZE;
18783 unsigned long supercnt = 0, thiscnt = 0;
18784 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18785 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18787 ++supercnt;
18788 gcc_checking_assert (ranges_table[off + supercnt].num
18789 == BLOCK_NUMBER (chain));
18791 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18792 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18793 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18794 ++thiscnt;
18795 gcc_assert (supercnt >= thiscnt);
18796 add_AT_range_list (die, DW_AT_ranges,
18797 ((off + supercnt - thiscnt)
18798 * 2 * DWARF2_ADDR_SIZE),
18799 false);
18800 return;
18803 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18805 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18808 add_ranges (chain);
18809 chain = BLOCK_FRAGMENT_CHAIN (chain);
18811 while (chain);
18812 add_ranges (NULL);
18814 else
18816 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18817 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18818 BLOCK_NUMBER (stmt));
18819 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18820 BLOCK_NUMBER (stmt));
18821 add_AT_low_high_pc (die, label, label_high, false);
18825 /* Generate a DIE for a lexical block. */
18827 static void
18828 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18830 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18832 if (call_arg_locations)
18834 if (block_map.length () <= BLOCK_NUMBER (stmt))
18835 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18836 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18839 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18840 add_high_low_attributes (stmt, stmt_die);
18842 decls_for_scope (stmt, stmt_die, depth);
18845 /* Generate a DIE for an inlined subprogram. */
18847 static void
18848 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18850 tree decl;
18852 /* The instance of function that is effectively being inlined shall not
18853 be abstract. */
18854 gcc_assert (! BLOCK_ABSTRACT (stmt));
18856 decl = block_ultimate_origin (stmt);
18858 /* Emit info for the abstract instance first, if we haven't yet. We
18859 must emit this even if the block is abstract, otherwise when we
18860 emit the block below (or elsewhere), we may end up trying to emit
18861 a die whose origin die hasn't been emitted, and crashing. */
18862 dwarf2out_abstract_function (decl);
18864 if (! BLOCK_ABSTRACT (stmt))
18866 dw_die_ref subr_die
18867 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18869 if (call_arg_locations)
18871 if (block_map.length () <= BLOCK_NUMBER (stmt))
18872 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18873 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18875 add_abstract_origin_attribute (subr_die, decl);
18876 if (TREE_ASM_WRITTEN (stmt))
18877 add_high_low_attributes (stmt, subr_die);
18878 add_call_src_coords_attributes (stmt, subr_die);
18880 decls_for_scope (stmt, subr_die, depth);
18881 current_function_has_inlines = 1;
18885 /* Generate a DIE for a field in a record, or structure. */
18887 static void
18888 gen_field_die (tree decl, dw_die_ref context_die)
18890 dw_die_ref decl_die;
18892 if (TREE_TYPE (decl) == error_mark_node)
18893 return;
18895 decl_die = new_die (DW_TAG_member, context_die, decl);
18896 add_name_and_src_coords_attributes (decl_die, decl);
18897 add_type_attribute (decl_die, member_declared_type (decl),
18898 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18899 context_die);
18901 if (DECL_BIT_FIELD_TYPE (decl))
18903 add_byte_size_attribute (decl_die, decl);
18904 add_bit_size_attribute (decl_die, decl);
18905 add_bit_offset_attribute (decl_die, decl);
18908 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18909 add_data_member_location_attribute (decl_die, decl);
18911 if (DECL_ARTIFICIAL (decl))
18912 add_AT_flag (decl_die, DW_AT_artificial, 1);
18914 add_accessibility_attribute (decl_die, decl);
18916 /* Equate decl number to die, so that we can look up this decl later on. */
18917 equate_decl_number_to_die (decl, decl_die);
18920 #if 0
18921 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18922 Use modified_type_die instead.
18923 We keep this code here just in case these types of DIEs may be needed to
18924 represent certain things in other languages (e.g. Pascal) someday. */
18926 static void
18927 gen_pointer_type_die (tree type, dw_die_ref context_die)
18929 dw_die_ref ptr_die
18930 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18932 equate_type_number_to_die (type, ptr_die);
18933 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18934 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18937 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18938 Use modified_type_die instead.
18939 We keep this code here just in case these types of DIEs may be needed to
18940 represent certain things in other languages (e.g. Pascal) someday. */
18942 static void
18943 gen_reference_type_die (tree type, dw_die_ref context_die)
18945 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18947 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18948 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18949 else
18950 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18952 equate_type_number_to_die (type, ref_die);
18953 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18954 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18956 #endif
18958 /* Generate a DIE for a pointer to a member type. */
18960 static void
18961 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18963 dw_die_ref ptr_die
18964 = new_die (DW_TAG_ptr_to_member_type,
18965 scope_die_for (type, context_die), type);
18967 equate_type_number_to_die (type, ptr_die);
18968 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18969 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18970 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18973 typedef const char *dchar_p; /* For DEF_VEC_P. */
18975 static char *producer_string;
18977 /* Return a heap allocated producer string including command line options
18978 if -grecord-gcc-switches. */
18980 static char *
18981 gen_producer_string (void)
18983 size_t j;
18984 vec<dchar_p> switches = vNULL;
18985 const char *language_string = lang_hooks.name;
18986 char *producer, *tail;
18987 const char *p;
18988 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18989 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18991 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18992 switch (save_decoded_options[j].opt_index)
18994 case OPT_o:
18995 case OPT_d:
18996 case OPT_dumpbase:
18997 case OPT_dumpdir:
18998 case OPT_auxbase:
18999 case OPT_auxbase_strip:
19000 case OPT_quiet:
19001 case OPT_version:
19002 case OPT_v:
19003 case OPT_w:
19004 case OPT_L:
19005 case OPT_D:
19006 case OPT_I:
19007 case OPT_U:
19008 case OPT_SPECIAL_unknown:
19009 case OPT_SPECIAL_ignore:
19010 case OPT_SPECIAL_program_name:
19011 case OPT_SPECIAL_input_file:
19012 case OPT_grecord_gcc_switches:
19013 case OPT_gno_record_gcc_switches:
19014 case OPT__output_pch_:
19015 case OPT_fdiagnostics_show_location_:
19016 case OPT_fdiagnostics_show_option:
19017 case OPT_fdiagnostics_show_caret:
19018 case OPT_fdiagnostics_color_:
19019 case OPT_fverbose_asm:
19020 case OPT____:
19021 case OPT__sysroot_:
19022 case OPT_nostdinc:
19023 case OPT_nostdinc__:
19024 /* Ignore these. */
19025 continue;
19026 default:
19027 if (cl_options[save_decoded_options[j].opt_index].flags
19028 & CL_NO_DWARF_RECORD)
19029 continue;
19030 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19031 == '-');
19032 switch (save_decoded_options[j].canonical_option[0][1])
19034 case 'M':
19035 case 'i':
19036 case 'W':
19037 continue;
19038 case 'f':
19039 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19040 "dump", 4) == 0)
19041 continue;
19042 break;
19043 default:
19044 break;
19046 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19047 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19048 break;
19051 producer = XNEWVEC (char, plen + 1 + len + 1);
19052 tail = producer;
19053 sprintf (tail, "%s %s", language_string, version_string);
19054 tail += plen;
19056 FOR_EACH_VEC_ELT (switches, j, p)
19058 len = strlen (p);
19059 *tail = ' ';
19060 memcpy (tail + 1, p, len);
19061 tail += len + 1;
19064 *tail = '\0';
19065 switches.release ();
19066 return producer;
19069 /* Generate the DIE for the compilation unit. */
19071 static dw_die_ref
19072 gen_compile_unit_die (const char *filename)
19074 dw_die_ref die;
19075 const char *language_string = lang_hooks.name;
19076 int language;
19078 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19080 if (filename)
19082 add_name_attribute (die, filename);
19083 /* Don't add cwd for <built-in>. */
19084 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19085 add_comp_dir_attribute (die);
19088 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19090 /* If our producer is LTO try to figure out a common language to use
19091 from the global list of translation units. */
19092 if (strcmp (language_string, "GNU GIMPLE") == 0)
19094 unsigned i;
19095 tree t;
19096 const char *common_lang = NULL;
19098 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19100 if (!TRANSLATION_UNIT_LANGUAGE (t))
19101 continue;
19102 if (!common_lang)
19103 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19104 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19106 else if (strncmp (common_lang, "GNU C", 5) == 0
19107 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19108 /* Mixing C and C++ is ok, use C++ in that case. */
19109 common_lang = "GNU C++";
19110 else
19112 /* Fall back to C. */
19113 common_lang = NULL;
19114 break;
19118 if (common_lang)
19119 language_string = common_lang;
19122 language = DW_LANG_C89;
19123 if (strcmp (language_string, "GNU C++") == 0)
19124 language = DW_LANG_C_plus_plus;
19125 else if (strcmp (language_string, "GNU F77") == 0)
19126 language = DW_LANG_Fortran77;
19127 else if (strcmp (language_string, "GNU Pascal") == 0)
19128 language = DW_LANG_Pascal83;
19129 else if (dwarf_version >= 3 || !dwarf_strict)
19131 if (strcmp (language_string, "GNU Ada") == 0)
19132 language = DW_LANG_Ada95;
19133 else if (strcmp (language_string, "GNU Fortran") == 0)
19134 language = DW_LANG_Fortran95;
19135 else if (strcmp (language_string, "GNU Java") == 0)
19136 language = DW_LANG_Java;
19137 else if (strcmp (language_string, "GNU Objective-C") == 0)
19138 language = DW_LANG_ObjC;
19139 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19140 language = DW_LANG_ObjC_plus_plus;
19141 else if (dwarf_version >= 5 || !dwarf_strict)
19143 if (strcmp (language_string, "GNU Go") == 0)
19144 language = DW_LANG_Go;
19147 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19148 else if (strcmp (language_string, "GNU Fortran") == 0)
19149 language = DW_LANG_Fortran90;
19151 add_AT_unsigned (die, DW_AT_language, language);
19153 switch (language)
19155 case DW_LANG_Fortran77:
19156 case DW_LANG_Fortran90:
19157 case DW_LANG_Fortran95:
19158 /* Fortran has case insensitive identifiers and the front-end
19159 lowercases everything. */
19160 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19161 break;
19162 default:
19163 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19164 break;
19166 return die;
19169 /* Generate the DIE for a base class. */
19171 static void
19172 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19174 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19176 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19177 add_data_member_location_attribute (die, binfo);
19179 if (BINFO_VIRTUAL_P (binfo))
19180 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19182 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19183 children, otherwise the default is DW_ACCESS_public. In DWARF2
19184 the default has always been DW_ACCESS_private. */
19185 if (access == access_public_node)
19187 if (dwarf_version == 2
19188 || context_die->die_tag == DW_TAG_class_type)
19189 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19191 else if (access == access_protected_node)
19192 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19193 else if (dwarf_version > 2
19194 && context_die->die_tag != DW_TAG_class_type)
19195 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19198 /* Generate a DIE for a class member. */
19200 static void
19201 gen_member_die (tree type, dw_die_ref context_die)
19203 tree member;
19204 tree binfo = TYPE_BINFO (type);
19205 dw_die_ref child;
19207 /* If this is not an incomplete type, output descriptions of each of its
19208 members. Note that as we output the DIEs necessary to represent the
19209 members of this record or union type, we will also be trying to output
19210 DIEs to represent the *types* of those members. However the `type'
19211 function (above) will specifically avoid generating type DIEs for member
19212 types *within* the list of member DIEs for this (containing) type except
19213 for those types (of members) which are explicitly marked as also being
19214 members of this (containing) type themselves. The g++ front- end can
19215 force any given type to be treated as a member of some other (containing)
19216 type by setting the TYPE_CONTEXT of the given (member) type to point to
19217 the TREE node representing the appropriate (containing) type. */
19219 /* First output info about the base classes. */
19220 if (binfo)
19222 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19223 int i;
19224 tree base;
19226 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19227 gen_inheritance_die (base,
19228 (accesses ? (*accesses)[i] : access_public_node),
19229 context_die);
19232 /* Now output info about the data members and type members. */
19233 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19235 /* If we thought we were generating minimal debug info for TYPE
19236 and then changed our minds, some of the member declarations
19237 may have already been defined. Don't define them again, but
19238 do put them in the right order. */
19240 child = lookup_decl_die (member);
19241 if (child)
19242 splice_child_die (context_die, child);
19243 else
19244 gen_decl_die (member, NULL, context_die);
19247 /* Now output info about the function members (if any). */
19248 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19250 /* Don't include clones in the member list. */
19251 if (DECL_ABSTRACT_ORIGIN (member))
19252 continue;
19254 child = lookup_decl_die (member);
19255 if (child)
19256 splice_child_die (context_die, child);
19257 else
19258 gen_decl_die (member, NULL, context_die);
19262 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19263 is set, we pretend that the type was never defined, so we only get the
19264 member DIEs needed by later specification DIEs. */
19266 static void
19267 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19268 enum debug_info_usage usage)
19270 dw_die_ref type_die = lookup_type_die (type);
19271 dw_die_ref scope_die = 0;
19272 int nested = 0;
19273 int complete = (TYPE_SIZE (type)
19274 && (! TYPE_STUB_DECL (type)
19275 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19276 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19277 complete = complete && should_emit_struct_debug (type, usage);
19279 if (type_die && ! complete)
19280 return;
19282 if (TYPE_CONTEXT (type) != NULL_TREE
19283 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19284 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19285 nested = 1;
19287 scope_die = scope_die_for (type, context_die);
19289 /* Generate child dies for template paramaters. */
19290 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19291 schedule_generic_params_dies_gen (type);
19293 if (! type_die || (nested && is_cu_die (scope_die)))
19294 /* First occurrence of type or toplevel definition of nested class. */
19296 dw_die_ref old_die = type_die;
19298 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19299 ? record_type_tag (type) : DW_TAG_union_type,
19300 scope_die, type);
19301 equate_type_number_to_die (type, type_die);
19302 if (old_die)
19303 add_AT_specification (type_die, old_die);
19304 else
19305 add_name_attribute (type_die, type_tag (type));
19307 else
19308 remove_AT (type_die, DW_AT_declaration);
19310 /* If this type has been completed, then give it a byte_size attribute and
19311 then give a list of members. */
19312 if (complete && !ns_decl)
19314 /* Prevent infinite recursion in cases where the type of some member of
19315 this type is expressed in terms of this type itself. */
19316 TREE_ASM_WRITTEN (type) = 1;
19317 add_byte_size_attribute (type_die, type);
19318 if (TYPE_STUB_DECL (type) != NULL_TREE)
19320 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19321 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19324 /* If the first reference to this type was as the return type of an
19325 inline function, then it may not have a parent. Fix this now. */
19326 if (type_die->die_parent == NULL)
19327 add_child_die (scope_die, type_die);
19329 push_decl_scope (type);
19330 gen_member_die (type, type_die);
19331 pop_decl_scope ();
19333 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19334 if (TYPE_ARTIFICIAL (type))
19335 add_AT_flag (type_die, DW_AT_artificial, 1);
19337 /* GNU extension: Record what type our vtable lives in. */
19338 if (TYPE_VFIELD (type))
19340 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19342 gen_type_die (vtype, context_die);
19343 add_AT_die_ref (type_die, DW_AT_containing_type,
19344 lookup_type_die (vtype));
19347 else
19349 add_AT_flag (type_die, DW_AT_declaration, 1);
19351 /* We don't need to do this for function-local types. */
19352 if (TYPE_STUB_DECL (type)
19353 && ! decl_function_context (TYPE_STUB_DECL (type)))
19354 vec_safe_push (incomplete_types, type);
19357 if (get_AT (type_die, DW_AT_name))
19358 add_pubtype (type, type_die);
19361 /* Generate a DIE for a subroutine _type_. */
19363 static void
19364 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19366 tree return_type = TREE_TYPE (type);
19367 dw_die_ref subr_die
19368 = new_die (DW_TAG_subroutine_type,
19369 scope_die_for (type, context_die), type);
19371 equate_type_number_to_die (type, subr_die);
19372 add_prototyped_attribute (subr_die, type);
19373 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19374 gen_formal_types_die (type, subr_die);
19376 if (get_AT (subr_die, DW_AT_name))
19377 add_pubtype (type, subr_die);
19380 /* Generate a DIE for a type definition. */
19382 static void
19383 gen_typedef_die (tree decl, dw_die_ref context_die)
19385 dw_die_ref type_die;
19386 tree origin;
19388 if (TREE_ASM_WRITTEN (decl))
19389 return;
19391 TREE_ASM_WRITTEN (decl) = 1;
19392 type_die = new_die (DW_TAG_typedef, context_die, decl);
19393 origin = decl_ultimate_origin (decl);
19394 if (origin != NULL)
19395 add_abstract_origin_attribute (type_die, origin);
19396 else
19398 tree type;
19400 add_name_and_src_coords_attributes (type_die, decl);
19401 if (DECL_ORIGINAL_TYPE (decl))
19403 type = DECL_ORIGINAL_TYPE (decl);
19405 gcc_assert (type != TREE_TYPE (decl));
19406 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19408 else
19410 type = TREE_TYPE (decl);
19412 if (is_naming_typedef_decl (TYPE_NAME (type)))
19414 /* Here, we are in the case of decl being a typedef naming
19415 an anonymous type, e.g:
19416 typedef struct {...} foo;
19417 In that case TREE_TYPE (decl) is not a typedef variant
19418 type and TYPE_NAME of the anonymous type is set to the
19419 TYPE_DECL of the typedef. This construct is emitted by
19420 the C++ FE.
19422 TYPE is the anonymous struct named by the typedef
19423 DECL. As we need the DW_AT_type attribute of the
19424 DW_TAG_typedef to point to the DIE of TYPE, let's
19425 generate that DIE right away. add_type_attribute
19426 called below will then pick (via lookup_type_die) that
19427 anonymous struct DIE. */
19428 if (!TREE_ASM_WRITTEN (type))
19429 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19431 /* This is a GNU Extension. We are adding a
19432 DW_AT_linkage_name attribute to the DIE of the
19433 anonymous struct TYPE. The value of that attribute
19434 is the name of the typedef decl naming the anonymous
19435 struct. This greatly eases the work of consumers of
19436 this debug info. */
19437 add_linkage_attr (lookup_type_die (type), decl);
19441 add_type_attribute (type_die, type, TREE_READONLY (decl),
19442 TREE_THIS_VOLATILE (decl), context_die);
19444 if (is_naming_typedef_decl (decl))
19445 /* We want that all subsequent calls to lookup_type_die with
19446 TYPE in argument yield the DW_TAG_typedef we have just
19447 created. */
19448 equate_type_number_to_die (type, type_die);
19450 add_accessibility_attribute (type_die, decl);
19453 if (DECL_ABSTRACT (decl))
19454 equate_decl_number_to_die (decl, type_die);
19456 if (get_AT (type_die, DW_AT_name))
19457 add_pubtype (decl, type_die);
19460 /* Generate a DIE for a struct, class, enum or union type. */
19462 static void
19463 gen_tagged_type_die (tree type,
19464 dw_die_ref context_die,
19465 enum debug_info_usage usage)
19467 int need_pop;
19469 if (type == NULL_TREE
19470 || !is_tagged_type (type))
19471 return;
19473 /* If this is a nested type whose containing class hasn't been written
19474 out yet, writing it out will cover this one, too. This does not apply
19475 to instantiations of member class templates; they need to be added to
19476 the containing class as they are generated. FIXME: This hurts the
19477 idea of combining type decls from multiple TUs, since we can't predict
19478 what set of template instantiations we'll get. */
19479 if (TYPE_CONTEXT (type)
19480 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19481 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19483 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19485 if (TREE_ASM_WRITTEN (type))
19486 return;
19488 /* If that failed, attach ourselves to the stub. */
19489 push_decl_scope (TYPE_CONTEXT (type));
19490 context_die = lookup_type_die (TYPE_CONTEXT (type));
19491 need_pop = 1;
19493 else if (TYPE_CONTEXT (type) != NULL_TREE
19494 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19496 /* If this type is local to a function that hasn't been written
19497 out yet, use a NULL context for now; it will be fixed up in
19498 decls_for_scope. */
19499 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19500 /* A declaration DIE doesn't count; nested types need to go in the
19501 specification. */
19502 if (context_die && is_declaration_die (context_die))
19503 context_die = NULL;
19504 need_pop = 0;
19506 else
19508 context_die = declare_in_namespace (type, context_die);
19509 need_pop = 0;
19512 if (TREE_CODE (type) == ENUMERAL_TYPE)
19514 /* This might have been written out by the call to
19515 declare_in_namespace. */
19516 if (!TREE_ASM_WRITTEN (type))
19517 gen_enumeration_type_die (type, context_die);
19519 else
19520 gen_struct_or_union_type_die (type, context_die, usage);
19522 if (need_pop)
19523 pop_decl_scope ();
19525 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19526 it up if it is ever completed. gen_*_type_die will set it for us
19527 when appropriate. */
19530 /* Generate a type description DIE. */
19532 static void
19533 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19534 enum debug_info_usage usage)
19536 struct array_descr_info info;
19538 if (type == NULL_TREE || type == error_mark_node)
19539 return;
19541 if (TYPE_NAME (type) != NULL_TREE
19542 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19543 && is_redundant_typedef (TYPE_NAME (type))
19544 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19545 /* The DECL of this type is a typedef we don't want to emit debug
19546 info for but we want debug info for its underlying typedef.
19547 This can happen for e.g, the injected-class-name of a C++
19548 type. */
19549 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19551 /* If TYPE is a typedef type variant, let's generate debug info
19552 for the parent typedef which TYPE is a type of. */
19553 if (typedef_variant_p (type))
19555 if (TREE_ASM_WRITTEN (type))
19556 return;
19558 /* Prevent broken recursion; we can't hand off to the same type. */
19559 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19561 /* Give typedefs the right scope. */
19562 context_die = scope_die_for (type, context_die);
19564 TREE_ASM_WRITTEN (type) = 1;
19566 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19567 return;
19570 /* If type is an anonymous tagged type named by a typedef, let's
19571 generate debug info for the typedef. */
19572 if (is_naming_typedef_decl (TYPE_NAME (type)))
19574 /* Use the DIE of the containing namespace as the parent DIE of
19575 the type description DIE we want to generate. */
19576 if (DECL_CONTEXT (TYPE_NAME (type))
19577 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19578 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19580 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19581 return;
19584 /* If this is an array type with hidden descriptor, handle it first. */
19585 if (!TREE_ASM_WRITTEN (type)
19586 && lang_hooks.types.get_array_descr_info
19587 && lang_hooks.types.get_array_descr_info (type, &info)
19588 && (dwarf_version >= 3 || !dwarf_strict))
19590 gen_descr_array_type_die (type, &info, context_die);
19591 TREE_ASM_WRITTEN (type) = 1;
19592 return;
19595 /* We are going to output a DIE to represent the unqualified version
19596 of this type (i.e. without any const or volatile qualifiers) so
19597 get the main variant (i.e. the unqualified version) of this type
19598 now. (Vectors are special because the debugging info is in the
19599 cloned type itself). */
19600 if (TREE_CODE (type) != VECTOR_TYPE)
19601 type = type_main_variant (type);
19603 if (TREE_ASM_WRITTEN (type))
19604 return;
19606 switch (TREE_CODE (type))
19608 case ERROR_MARK:
19609 break;
19611 case POINTER_TYPE:
19612 case REFERENCE_TYPE:
19613 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19614 ensures that the gen_type_die recursion will terminate even if the
19615 type is recursive. Recursive types are possible in Ada. */
19616 /* ??? We could perhaps do this for all types before the switch
19617 statement. */
19618 TREE_ASM_WRITTEN (type) = 1;
19620 /* For these types, all that is required is that we output a DIE (or a
19621 set of DIEs) to represent the "basis" type. */
19622 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19623 DINFO_USAGE_IND_USE);
19624 break;
19626 case OFFSET_TYPE:
19627 /* This code is used for C++ pointer-to-data-member types.
19628 Output a description of the relevant class type. */
19629 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19630 DINFO_USAGE_IND_USE);
19632 /* Output a description of the type of the object pointed to. */
19633 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19634 DINFO_USAGE_IND_USE);
19636 /* Now output a DIE to represent this pointer-to-data-member type
19637 itself. */
19638 gen_ptr_to_mbr_type_die (type, context_die);
19639 break;
19641 case FUNCTION_TYPE:
19642 /* Force out return type (in case it wasn't forced out already). */
19643 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19644 DINFO_USAGE_DIR_USE);
19645 gen_subroutine_type_die (type, context_die);
19646 break;
19648 case METHOD_TYPE:
19649 /* Force out return type (in case it wasn't forced out already). */
19650 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19651 DINFO_USAGE_DIR_USE);
19652 gen_subroutine_type_die (type, context_die);
19653 break;
19655 case ARRAY_TYPE:
19656 gen_array_type_die (type, context_die);
19657 break;
19659 case VECTOR_TYPE:
19660 gen_array_type_die (type, context_die);
19661 break;
19663 case ENUMERAL_TYPE:
19664 case RECORD_TYPE:
19665 case UNION_TYPE:
19666 case QUAL_UNION_TYPE:
19667 gen_tagged_type_die (type, context_die, usage);
19668 return;
19670 case VOID_TYPE:
19671 case INTEGER_TYPE:
19672 case REAL_TYPE:
19673 case FIXED_POINT_TYPE:
19674 case COMPLEX_TYPE:
19675 case BOOLEAN_TYPE:
19676 /* No DIEs needed for fundamental types. */
19677 break;
19679 case NULLPTR_TYPE:
19680 case LANG_TYPE:
19681 /* Just use DW_TAG_unspecified_type. */
19683 dw_die_ref type_die = lookup_type_die (type);
19684 if (type_die == NULL)
19686 tree name = TYPE_NAME (type);
19687 if (TREE_CODE (name) == TYPE_DECL)
19688 name = DECL_NAME (name);
19689 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19690 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19691 equate_type_number_to_die (type, type_die);
19694 break;
19696 default:
19697 gcc_unreachable ();
19700 TREE_ASM_WRITTEN (type) = 1;
19703 static void
19704 gen_type_die (tree type, dw_die_ref context_die)
19706 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19709 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19710 things which are local to the given block. */
19712 static void
19713 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19715 int must_output_die = 0;
19716 bool inlined_func;
19718 /* Ignore blocks that are NULL. */
19719 if (stmt == NULL_TREE)
19720 return;
19722 inlined_func = inlined_function_outer_scope_p (stmt);
19724 /* If the block is one fragment of a non-contiguous block, do not
19725 process the variables, since they will have been done by the
19726 origin block. Do process subblocks. */
19727 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19729 tree sub;
19731 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19732 gen_block_die (sub, context_die, depth + 1);
19734 return;
19737 /* Determine if we need to output any Dwarf DIEs at all to represent this
19738 block. */
19739 if (inlined_func)
19740 /* The outer scopes for inlinings *must* always be represented. We
19741 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19742 must_output_die = 1;
19743 else
19745 /* Determine if this block directly contains any "significant"
19746 local declarations which we will need to output DIEs for. */
19747 if (debug_info_level > DINFO_LEVEL_TERSE)
19748 /* We are not in terse mode so *any* local declaration counts
19749 as being a "significant" one. */
19750 must_output_die = ((BLOCK_VARS (stmt) != NULL
19751 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19752 && (TREE_USED (stmt)
19753 || TREE_ASM_WRITTEN (stmt)
19754 || BLOCK_ABSTRACT (stmt)));
19755 else if ((TREE_USED (stmt)
19756 || TREE_ASM_WRITTEN (stmt)
19757 || BLOCK_ABSTRACT (stmt))
19758 && !dwarf2out_ignore_block (stmt))
19759 must_output_die = 1;
19762 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19763 DIE for any block which contains no significant local declarations at
19764 all. Rather, in such cases we just call `decls_for_scope' so that any
19765 needed Dwarf info for any sub-blocks will get properly generated. Note
19766 that in terse mode, our definition of what constitutes a "significant"
19767 local declaration gets restricted to include only inlined function
19768 instances and local (nested) function definitions. */
19769 if (must_output_die)
19771 if (inlined_func)
19773 /* If STMT block is abstract, that means we have been called
19774 indirectly from dwarf2out_abstract_function.
19775 That function rightfully marks the descendent blocks (of
19776 the abstract function it is dealing with) as being abstract,
19777 precisely to prevent us from emitting any
19778 DW_TAG_inlined_subroutine DIE as a descendent
19779 of an abstract function instance. So in that case, we should
19780 not call gen_inlined_subroutine_die.
19782 Later though, when cgraph asks dwarf2out to emit info
19783 for the concrete instance of the function decl into which
19784 the concrete instance of STMT got inlined, the later will lead
19785 to the generation of a DW_TAG_inlined_subroutine DIE. */
19786 if (! BLOCK_ABSTRACT (stmt))
19787 gen_inlined_subroutine_die (stmt, context_die, depth);
19789 else
19790 gen_lexical_block_die (stmt, context_die, depth);
19792 else
19793 decls_for_scope (stmt, context_die, depth);
19796 /* Process variable DECL (or variable with origin ORIGIN) within
19797 block STMT and add it to CONTEXT_DIE. */
19798 static void
19799 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19801 dw_die_ref die;
19802 tree decl_or_origin = decl ? decl : origin;
19804 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19805 die = lookup_decl_die (decl_or_origin);
19806 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19807 && TYPE_DECL_IS_STUB (decl_or_origin))
19808 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19809 else
19810 die = NULL;
19812 if (die != NULL && die->die_parent == NULL)
19813 add_child_die (context_die, die);
19814 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19815 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19816 stmt, context_die);
19817 else
19818 gen_decl_die (decl, origin, context_die);
19821 /* Generate all of the decls declared within a given scope and (recursively)
19822 all of its sub-blocks. */
19824 static void
19825 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19827 tree decl;
19828 unsigned int i;
19829 tree subblocks;
19831 /* Ignore NULL blocks. */
19832 if (stmt == NULL_TREE)
19833 return;
19835 /* Output the DIEs to represent all of the data objects and typedefs
19836 declared directly within this block but not within any nested
19837 sub-blocks. Also, nested function and tag DIEs have been
19838 generated with a parent of NULL; fix that up now. */
19839 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19840 process_scope_var (stmt, decl, NULL_TREE, context_die);
19841 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19842 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19843 context_die);
19845 /* If we're at -g1, we're not interested in subblocks. */
19846 if (debug_info_level <= DINFO_LEVEL_TERSE)
19847 return;
19849 /* Output the DIEs to represent all sub-blocks (and the items declared
19850 therein) of this block. */
19851 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19852 subblocks != NULL;
19853 subblocks = BLOCK_CHAIN (subblocks))
19854 gen_block_die (subblocks, context_die, depth + 1);
19857 /* Is this a typedef we can avoid emitting? */
19859 static inline int
19860 is_redundant_typedef (const_tree decl)
19862 if (TYPE_DECL_IS_STUB (decl))
19863 return 1;
19865 if (DECL_ARTIFICIAL (decl)
19866 && DECL_CONTEXT (decl)
19867 && is_tagged_type (DECL_CONTEXT (decl))
19868 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19869 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19870 /* Also ignore the artificial member typedef for the class name. */
19871 return 1;
19873 return 0;
19876 /* Return TRUE if TYPE is a typedef that names a type for linkage
19877 purposes. This kind of typedefs is produced by the C++ FE for
19878 constructs like:
19880 typedef struct {...} foo;
19882 In that case, there is no typedef variant type produced for foo.
19883 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19884 struct type. */
19886 static bool
19887 is_naming_typedef_decl (const_tree decl)
19889 if (decl == NULL_TREE
19890 || TREE_CODE (decl) != TYPE_DECL
19891 || !is_tagged_type (TREE_TYPE (decl))
19892 || DECL_IS_BUILTIN (decl)
19893 || is_redundant_typedef (decl)
19894 /* It looks like Ada produces TYPE_DECLs that are very similar
19895 to C++ naming typedefs but that have different
19896 semantics. Let's be specific to c++ for now. */
19897 || !is_cxx ())
19898 return FALSE;
19900 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19901 && TYPE_NAME (TREE_TYPE (decl)) == decl
19902 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19903 != TYPE_NAME (TREE_TYPE (decl))));
19906 /* Returns the DIE for a context. */
19908 static inline dw_die_ref
19909 get_context_die (tree context)
19911 if (context)
19913 /* Find die that represents this context. */
19914 if (TYPE_P (context))
19916 context = TYPE_MAIN_VARIANT (context);
19917 return strip_naming_typedef (context, force_type_die (context));
19919 else
19920 return force_decl_die (context);
19922 return comp_unit_die ();
19925 /* Returns the DIE for decl. A DIE will always be returned. */
19927 static dw_die_ref
19928 force_decl_die (tree decl)
19930 dw_die_ref decl_die;
19931 unsigned saved_external_flag;
19932 tree save_fn = NULL_TREE;
19933 decl_die = lookup_decl_die (decl);
19934 if (!decl_die)
19936 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19938 decl_die = lookup_decl_die (decl);
19939 if (decl_die)
19940 return decl_die;
19942 switch (TREE_CODE (decl))
19944 case FUNCTION_DECL:
19945 /* Clear current_function_decl, so that gen_subprogram_die thinks
19946 that this is a declaration. At this point, we just want to force
19947 declaration die. */
19948 save_fn = current_function_decl;
19949 current_function_decl = NULL_TREE;
19950 gen_subprogram_die (decl, context_die);
19951 current_function_decl = save_fn;
19952 break;
19954 case VAR_DECL:
19955 /* Set external flag to force declaration die. Restore it after
19956 gen_decl_die() call. */
19957 saved_external_flag = DECL_EXTERNAL (decl);
19958 DECL_EXTERNAL (decl) = 1;
19959 gen_decl_die (decl, NULL, context_die);
19960 DECL_EXTERNAL (decl) = saved_external_flag;
19961 break;
19963 case NAMESPACE_DECL:
19964 if (dwarf_version >= 3 || !dwarf_strict)
19965 dwarf2out_decl (decl);
19966 else
19967 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19968 decl_die = comp_unit_die ();
19969 break;
19971 case TRANSLATION_UNIT_DECL:
19972 decl_die = comp_unit_die ();
19973 break;
19975 default:
19976 gcc_unreachable ();
19979 /* We should be able to find the DIE now. */
19980 if (!decl_die)
19981 decl_die = lookup_decl_die (decl);
19982 gcc_assert (decl_die);
19985 return decl_die;
19988 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19989 always returned. */
19991 static dw_die_ref
19992 force_type_die (tree type)
19994 dw_die_ref type_die;
19996 type_die = lookup_type_die (type);
19997 if (!type_die)
19999 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20001 type_die = modified_type_die (type, TYPE_READONLY (type),
20002 TYPE_VOLATILE (type), context_die);
20003 gcc_assert (type_die);
20005 return type_die;
20008 /* Force out any required namespaces to be able to output DECL,
20009 and return the new context_die for it, if it's changed. */
20011 static dw_die_ref
20012 setup_namespace_context (tree thing, dw_die_ref context_die)
20014 tree context = (DECL_P (thing)
20015 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20016 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20017 /* Force out the namespace. */
20018 context_die = force_decl_die (context);
20020 return context_die;
20023 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20024 type) within its namespace, if appropriate.
20026 For compatibility with older debuggers, namespace DIEs only contain
20027 declarations; all definitions are emitted at CU scope. */
20029 static dw_die_ref
20030 declare_in_namespace (tree thing, dw_die_ref context_die)
20032 dw_die_ref ns_context;
20034 if (debug_info_level <= DINFO_LEVEL_TERSE)
20035 return context_die;
20037 /* If this decl is from an inlined function, then don't try to emit it in its
20038 namespace, as we will get confused. It would have already been emitted
20039 when the abstract instance of the inline function was emitted anyways. */
20040 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20041 return context_die;
20043 ns_context = setup_namespace_context (thing, context_die);
20045 if (ns_context != context_die)
20047 if (is_fortran ())
20048 return ns_context;
20049 if (DECL_P (thing))
20050 gen_decl_die (thing, NULL, ns_context);
20051 else
20052 gen_type_die (thing, ns_context);
20054 return context_die;
20057 /* Generate a DIE for a namespace or namespace alias. */
20059 static void
20060 gen_namespace_die (tree decl, dw_die_ref context_die)
20062 dw_die_ref namespace_die;
20064 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20065 they are an alias of. */
20066 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20068 /* Output a real namespace or module. */
20069 context_die = setup_namespace_context (decl, comp_unit_die ());
20070 namespace_die = new_die (is_fortran ()
20071 ? DW_TAG_module : DW_TAG_namespace,
20072 context_die, decl);
20073 /* For Fortran modules defined in different CU don't add src coords. */
20074 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20076 const char *name = dwarf2_name (decl, 0);
20077 if (name)
20078 add_name_attribute (namespace_die, name);
20080 else
20081 add_name_and_src_coords_attributes (namespace_die, decl);
20082 if (DECL_EXTERNAL (decl))
20083 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20084 equate_decl_number_to_die (decl, namespace_die);
20086 else
20088 /* Output a namespace alias. */
20090 /* Force out the namespace we are an alias of, if necessary. */
20091 dw_die_ref origin_die
20092 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20094 if (DECL_FILE_SCOPE_P (decl)
20095 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20096 context_die = setup_namespace_context (decl, comp_unit_die ());
20097 /* Now create the namespace alias DIE. */
20098 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20099 add_name_and_src_coords_attributes (namespace_die, decl);
20100 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20101 equate_decl_number_to_die (decl, namespace_die);
20103 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20104 if (want_pubnames ())
20105 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20108 /* Generate Dwarf debug information for a decl described by DECL.
20109 The return value is currently only meaningful for PARM_DECLs,
20110 for all other decls it returns NULL. */
20112 static dw_die_ref
20113 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20115 tree decl_or_origin = decl ? decl : origin;
20116 tree class_origin = NULL, ultimate_origin;
20118 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20119 return NULL;
20121 switch (TREE_CODE (decl_or_origin))
20123 case ERROR_MARK:
20124 break;
20126 case CONST_DECL:
20127 if (!is_fortran () && !is_ada ())
20129 /* The individual enumerators of an enum type get output when we output
20130 the Dwarf representation of the relevant enum type itself. */
20131 break;
20134 /* Emit its type. */
20135 gen_type_die (TREE_TYPE (decl), context_die);
20137 /* And its containing namespace. */
20138 context_die = declare_in_namespace (decl, context_die);
20140 gen_const_die (decl, context_die);
20141 break;
20143 case FUNCTION_DECL:
20144 /* Don't output any DIEs to represent mere function declarations,
20145 unless they are class members or explicit block externs. */
20146 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20147 && DECL_FILE_SCOPE_P (decl_or_origin)
20148 && (current_function_decl == NULL_TREE
20149 || DECL_ARTIFICIAL (decl_or_origin)))
20150 break;
20152 #if 0
20153 /* FIXME */
20154 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20155 on local redeclarations of global functions. That seems broken. */
20156 if (current_function_decl != decl)
20157 /* This is only a declaration. */;
20158 #endif
20160 /* If we're emitting a clone, emit info for the abstract instance. */
20161 if (origin || DECL_ORIGIN (decl) != decl)
20162 dwarf2out_abstract_function (origin
20163 ? DECL_ORIGIN (origin)
20164 : DECL_ABSTRACT_ORIGIN (decl));
20166 /* If we're emitting an out-of-line copy of an inline function,
20167 emit info for the abstract instance and set up to refer to it. */
20168 else if (cgraph_function_possibly_inlined_p (decl)
20169 && ! DECL_ABSTRACT (decl)
20170 && ! class_or_namespace_scope_p (context_die)
20171 /* dwarf2out_abstract_function won't emit a die if this is just
20172 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20173 that case, because that works only if we have a die. */
20174 && DECL_INITIAL (decl) != NULL_TREE)
20176 dwarf2out_abstract_function (decl);
20177 set_decl_origin_self (decl);
20180 /* Otherwise we're emitting the primary DIE for this decl. */
20181 else if (debug_info_level > DINFO_LEVEL_TERSE)
20183 /* Before we describe the FUNCTION_DECL itself, make sure that we
20184 have its containing type. */
20185 if (!origin)
20186 origin = decl_class_context (decl);
20187 if (origin != NULL_TREE)
20188 gen_type_die (origin, context_die);
20190 /* And its return type. */
20191 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20193 /* And its virtual context. */
20194 if (DECL_VINDEX (decl) != NULL_TREE)
20195 gen_type_die (DECL_CONTEXT (decl), context_die);
20197 /* Make sure we have a member DIE for decl. */
20198 if (origin != NULL_TREE)
20199 gen_type_die_for_member (origin, decl, context_die);
20201 /* And its containing namespace. */
20202 context_die = declare_in_namespace (decl, context_die);
20205 /* Now output a DIE to represent the function itself. */
20206 if (decl)
20207 gen_subprogram_die (decl, context_die);
20208 break;
20210 case TYPE_DECL:
20211 /* If we are in terse mode, don't generate any DIEs to represent any
20212 actual typedefs. */
20213 if (debug_info_level <= DINFO_LEVEL_TERSE)
20214 break;
20216 /* In the special case of a TYPE_DECL node representing the declaration
20217 of some type tag, if the given TYPE_DECL is marked as having been
20218 instantiated from some other (original) TYPE_DECL node (e.g. one which
20219 was generated within the original definition of an inline function) we
20220 used to generate a special (abbreviated) DW_TAG_structure_type,
20221 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20222 should be actually referencing those DIEs, as variable DIEs with that
20223 type would be emitted already in the abstract origin, so it was always
20224 removed during unused type prunning. Don't add anything in this
20225 case. */
20226 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20227 break;
20229 if (is_redundant_typedef (decl))
20230 gen_type_die (TREE_TYPE (decl), context_die);
20231 else
20232 /* Output a DIE to represent the typedef itself. */
20233 gen_typedef_die (decl, context_die);
20234 break;
20236 case LABEL_DECL:
20237 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20238 gen_label_die (decl, context_die);
20239 break;
20241 case VAR_DECL:
20242 case RESULT_DECL:
20243 /* If we are in terse mode, don't generate any DIEs to represent any
20244 variable declarations or definitions. */
20245 if (debug_info_level <= DINFO_LEVEL_TERSE)
20246 break;
20248 /* Output any DIEs that are needed to specify the type of this data
20249 object. */
20250 if (decl_by_reference_p (decl_or_origin))
20251 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20252 else
20253 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20255 /* And its containing type. */
20256 class_origin = decl_class_context (decl_or_origin);
20257 if (class_origin != NULL_TREE)
20258 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20260 /* And its containing namespace. */
20261 context_die = declare_in_namespace (decl_or_origin, context_die);
20263 /* Now output the DIE to represent the data object itself. This gets
20264 complicated because of the possibility that the VAR_DECL really
20265 represents an inlined instance of a formal parameter for an inline
20266 function. */
20267 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20268 if (ultimate_origin != NULL_TREE
20269 && TREE_CODE (ultimate_origin) == PARM_DECL)
20270 gen_formal_parameter_die (decl, origin,
20271 true /* Emit name attribute. */,
20272 context_die);
20273 else
20274 gen_variable_die (decl, origin, context_die);
20275 break;
20277 case FIELD_DECL:
20278 /* Ignore the nameless fields that are used to skip bits but handle C++
20279 anonymous unions and structs. */
20280 if (DECL_NAME (decl) != NULL_TREE
20281 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20282 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20284 gen_type_die (member_declared_type (decl), context_die);
20285 gen_field_die (decl, context_die);
20287 break;
20289 case PARM_DECL:
20290 if (DECL_BY_REFERENCE (decl_or_origin))
20291 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20292 else
20293 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20294 return gen_formal_parameter_die (decl, origin,
20295 true /* Emit name attribute. */,
20296 context_die);
20298 case NAMESPACE_DECL:
20299 case IMPORTED_DECL:
20300 if (dwarf_version >= 3 || !dwarf_strict)
20301 gen_namespace_die (decl, context_die);
20302 break;
20304 default:
20305 /* Probably some frontend-internal decl. Assume we don't care. */
20306 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20307 break;
20310 return NULL;
20313 /* Output debug information for global decl DECL. Called from toplev.c after
20314 compilation proper has finished. */
20316 static void
20317 dwarf2out_global_decl (tree decl)
20319 /* Output DWARF2 information for file-scope tentative data object
20320 declarations, file-scope (extern) function declarations (which
20321 had no corresponding body) and file-scope tagged type declarations
20322 and definitions which have not yet been forced out. */
20323 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20324 dwarf2out_decl (decl);
20327 /* Output debug information for type decl DECL. Called from toplev.c
20328 and from language front ends (to record built-in types). */
20329 static void
20330 dwarf2out_type_decl (tree decl, int local)
20332 if (!local)
20333 dwarf2out_decl (decl);
20336 /* Output debug information for imported module or decl DECL.
20337 NAME is non-NULL name in the lexical block if the decl has been renamed.
20338 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20339 that DECL belongs to.
20340 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20341 static void
20342 dwarf2out_imported_module_or_decl_1 (tree decl,
20343 tree name,
20344 tree lexical_block,
20345 dw_die_ref lexical_block_die)
20347 expanded_location xloc;
20348 dw_die_ref imported_die = NULL;
20349 dw_die_ref at_import_die;
20351 if (TREE_CODE (decl) == IMPORTED_DECL)
20353 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20354 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20355 gcc_assert (decl);
20357 else
20358 xloc = expand_location (input_location);
20360 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20362 at_import_die = force_type_die (TREE_TYPE (decl));
20363 /* For namespace N { typedef void T; } using N::T; base_type_die
20364 returns NULL, but DW_TAG_imported_declaration requires
20365 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20366 if (!at_import_die)
20368 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20369 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20370 at_import_die = lookup_type_die (TREE_TYPE (decl));
20371 gcc_assert (at_import_die);
20374 else
20376 at_import_die = lookup_decl_die (decl);
20377 if (!at_import_die)
20379 /* If we're trying to avoid duplicate debug info, we may not have
20380 emitted the member decl for this field. Emit it now. */
20381 if (TREE_CODE (decl) == FIELD_DECL)
20383 tree type = DECL_CONTEXT (decl);
20385 if (TYPE_CONTEXT (type)
20386 && TYPE_P (TYPE_CONTEXT (type))
20387 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20388 DINFO_USAGE_DIR_USE))
20389 return;
20390 gen_type_die_for_member (type, decl,
20391 get_context_die (TYPE_CONTEXT (type)));
20393 at_import_die = force_decl_die (decl);
20397 if (TREE_CODE (decl) == NAMESPACE_DECL)
20399 if (dwarf_version >= 3 || !dwarf_strict)
20400 imported_die = new_die (DW_TAG_imported_module,
20401 lexical_block_die,
20402 lexical_block);
20403 else
20404 return;
20406 else
20407 imported_die = new_die (DW_TAG_imported_declaration,
20408 lexical_block_die,
20409 lexical_block);
20411 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20412 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20413 if (name)
20414 add_AT_string (imported_die, DW_AT_name,
20415 IDENTIFIER_POINTER (name));
20416 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20419 /* Output debug information for imported module or decl DECL.
20420 NAME is non-NULL name in context if the decl has been renamed.
20421 CHILD is true if decl is one of the renamed decls as part of
20422 importing whole module. */
20424 static void
20425 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20426 bool child)
20428 /* dw_die_ref at_import_die; */
20429 dw_die_ref scope_die;
20431 if (debug_info_level <= DINFO_LEVEL_TERSE)
20432 return;
20434 gcc_assert (decl);
20436 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20437 We need decl DIE for reference and scope die. First, get DIE for the decl
20438 itself. */
20440 /* Get the scope die for decl context. Use comp_unit_die for global module
20441 or decl. If die is not found for non globals, force new die. */
20442 if (context
20443 && TYPE_P (context)
20444 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20445 return;
20447 if (!(dwarf_version >= 3 || !dwarf_strict))
20448 return;
20450 scope_die = get_context_die (context);
20452 if (child)
20454 gcc_assert (scope_die->die_child);
20455 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20456 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20457 scope_die = scope_die->die_child;
20460 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20461 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20465 /* Write the debugging output for DECL. */
20467 void
20468 dwarf2out_decl (tree decl)
20470 dw_die_ref context_die = comp_unit_die ();
20472 switch (TREE_CODE (decl))
20474 case ERROR_MARK:
20475 return;
20477 case FUNCTION_DECL:
20478 /* What we would really like to do here is to filter out all mere
20479 file-scope declarations of file-scope functions which are never
20480 referenced later within this translation unit (and keep all of ones
20481 that *are* referenced later on) but we aren't clairvoyant, so we have
20482 no idea which functions will be referenced in the future (i.e. later
20483 on within the current translation unit). So here we just ignore all
20484 file-scope function declarations which are not also definitions. If
20485 and when the debugger needs to know something about these functions,
20486 it will have to hunt around and find the DWARF information associated
20487 with the definition of the function.
20489 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20490 nodes represent definitions and which ones represent mere
20491 declarations. We have to check DECL_INITIAL instead. That's because
20492 the C front-end supports some weird semantics for "extern inline"
20493 function definitions. These can get inlined within the current
20494 translation unit (and thus, we need to generate Dwarf info for their
20495 abstract instances so that the Dwarf info for the concrete inlined
20496 instances can have something to refer to) but the compiler never
20497 generates any out-of-lines instances of such things (despite the fact
20498 that they *are* definitions).
20500 The important point is that the C front-end marks these "extern
20501 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20502 them anyway. Note that the C++ front-end also plays some similar games
20503 for inline function definitions appearing within include files which
20504 also contain `#pragma interface' pragmas.
20506 If we are called from dwarf2out_abstract_function output a DIE
20507 anyway. We can end up here this way with early inlining and LTO
20508 where the inlined function is output in a different LTRANS unit
20509 or not at all. */
20510 if (DECL_INITIAL (decl) == NULL_TREE
20511 && ! DECL_ABSTRACT (decl))
20512 return;
20514 /* If we're a nested function, initially use a parent of NULL; if we're
20515 a plain function, this will be fixed up in decls_for_scope. If
20516 we're a method, it will be ignored, since we already have a DIE. */
20517 if (decl_function_context (decl)
20518 /* But if we're in terse mode, we don't care about scope. */
20519 && debug_info_level > DINFO_LEVEL_TERSE)
20520 context_die = NULL;
20521 break;
20523 case VAR_DECL:
20524 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20525 declaration and if the declaration was never even referenced from
20526 within this entire compilation unit. We suppress these DIEs in
20527 order to save space in the .debug section (by eliminating entries
20528 which are probably useless). Note that we must not suppress
20529 block-local extern declarations (whether used or not) because that
20530 would screw-up the debugger's name lookup mechanism and cause it to
20531 miss things which really ought to be in scope at a given point. */
20532 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20533 return;
20535 /* For local statics lookup proper context die. */
20536 if (TREE_STATIC (decl)
20537 && DECL_CONTEXT (decl)
20538 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20539 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20541 /* If we are in terse mode, don't generate any DIEs to represent any
20542 variable declarations or definitions. */
20543 if (debug_info_level <= DINFO_LEVEL_TERSE)
20544 return;
20545 break;
20547 case CONST_DECL:
20548 if (debug_info_level <= DINFO_LEVEL_TERSE)
20549 return;
20550 if (!is_fortran () && !is_ada ())
20551 return;
20552 if (TREE_STATIC (decl) && decl_function_context (decl))
20553 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20554 break;
20556 case NAMESPACE_DECL:
20557 case IMPORTED_DECL:
20558 if (debug_info_level <= DINFO_LEVEL_TERSE)
20559 return;
20560 if (lookup_decl_die (decl) != NULL)
20561 return;
20562 break;
20564 case TYPE_DECL:
20565 /* Don't emit stubs for types unless they are needed by other DIEs. */
20566 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20567 return;
20569 /* Don't bother trying to generate any DIEs to represent any of the
20570 normal built-in types for the language we are compiling. */
20571 if (DECL_IS_BUILTIN (decl))
20572 return;
20574 /* If we are in terse mode, don't generate any DIEs for types. */
20575 if (debug_info_level <= DINFO_LEVEL_TERSE)
20576 return;
20578 /* If we're a function-scope tag, initially use a parent of NULL;
20579 this will be fixed up in decls_for_scope. */
20580 if (decl_function_context (decl))
20581 context_die = NULL;
20583 break;
20585 default:
20586 return;
20589 gen_decl_die (decl, NULL, context_die);
20592 /* Write the debugging output for DECL. */
20594 static void
20595 dwarf2out_function_decl (tree decl)
20597 dwarf2out_decl (decl);
20598 call_arg_locations = NULL;
20599 call_arg_loc_last = NULL;
20600 call_site_count = -1;
20601 tail_call_site_count = -1;
20602 block_map.release ();
20603 htab_empty (decl_loc_table);
20604 htab_empty (cached_dw_loc_list_table);
20607 /* Output a marker (i.e. a label) for the beginning of the generated code for
20608 a lexical block. */
20610 static void
20611 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20612 unsigned int blocknum)
20614 switch_to_section (current_function_section ());
20615 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20618 /* Output a marker (i.e. a label) for the end of the generated code for a
20619 lexical block. */
20621 static void
20622 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20624 switch_to_section (current_function_section ());
20625 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20628 /* Returns nonzero if it is appropriate not to emit any debugging
20629 information for BLOCK, because it doesn't contain any instructions.
20631 Don't allow this for blocks with nested functions or local classes
20632 as we would end up with orphans, and in the presence of scheduling
20633 we may end up calling them anyway. */
20635 static bool
20636 dwarf2out_ignore_block (const_tree block)
20638 tree decl;
20639 unsigned int i;
20641 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20642 if (TREE_CODE (decl) == FUNCTION_DECL
20643 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20644 return 0;
20645 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20647 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20648 if (TREE_CODE (decl) == FUNCTION_DECL
20649 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20650 return 0;
20653 return 1;
20656 /* Hash table routines for file_hash. */
20658 static int
20659 file_table_eq (const void *p1_p, const void *p2_p)
20661 const struct dwarf_file_data *const p1 =
20662 (const struct dwarf_file_data *) p1_p;
20663 const char *const p2 = (const char *) p2_p;
20664 return filename_cmp (p1->filename, p2) == 0;
20667 static hashval_t
20668 file_table_hash (const void *p_p)
20670 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20671 return htab_hash_string (p->filename);
20674 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20675 dwarf2out.c) and return its "index". The index of each (known) filename is
20676 just a unique number which is associated with only that one filename. We
20677 need such numbers for the sake of generating labels (in the .debug_sfnames
20678 section) and references to those files numbers (in the .debug_srcinfo
20679 and.debug_macinfo sections). If the filename given as an argument is not
20680 found in our current list, add it to the list and assign it the next
20681 available unique index number. In order to speed up searches, we remember
20682 the index of the filename was looked up last. This handles the majority of
20683 all searches. */
20685 static struct dwarf_file_data *
20686 lookup_filename (const char *file_name)
20688 void ** slot;
20689 struct dwarf_file_data * created;
20691 /* Check to see if the file name that was searched on the previous
20692 call matches this file name. If so, return the index. */
20693 if (file_table_last_lookup
20694 && (file_name == file_table_last_lookup->filename
20695 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20696 return file_table_last_lookup;
20698 /* Didn't match the previous lookup, search the table. */
20699 slot = htab_find_slot_with_hash (file_table, file_name,
20700 htab_hash_string (file_name), INSERT);
20701 if (*slot)
20702 return (struct dwarf_file_data *) *slot;
20704 created = ggc_alloc_dwarf_file_data ();
20705 created->filename = file_name;
20706 created->emitted_number = 0;
20707 *slot = created;
20708 return created;
20711 /* If the assembler will construct the file table, then translate the compiler
20712 internal file table number into the assembler file table number, and emit
20713 a .file directive if we haven't already emitted one yet. The file table
20714 numbers are different because we prune debug info for unused variables and
20715 types, which may include filenames. */
20717 static int
20718 maybe_emit_file (struct dwarf_file_data * fd)
20720 if (! fd->emitted_number)
20722 if (last_emitted_file)
20723 fd->emitted_number = last_emitted_file->emitted_number + 1;
20724 else
20725 fd->emitted_number = 1;
20726 last_emitted_file = fd;
20728 if (DWARF2_ASM_LINE_DEBUG_INFO)
20730 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20731 output_quoted_string (asm_out_file,
20732 remap_debug_filename (fd->filename));
20733 fputc ('\n', asm_out_file);
20737 return fd->emitted_number;
20740 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20741 That generation should happen after function debug info has been
20742 generated. The value of the attribute is the constant value of ARG. */
20744 static void
20745 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20747 die_arg_entry entry;
20749 if (!die || !arg)
20750 return;
20752 if (!tmpl_value_parm_die_table)
20753 vec_alloc (tmpl_value_parm_die_table, 32);
20755 entry.die = die;
20756 entry.arg = arg;
20757 vec_safe_push (tmpl_value_parm_die_table, entry);
20760 /* Return TRUE if T is an instance of generic type, FALSE
20761 otherwise. */
20763 static bool
20764 generic_type_p (tree t)
20766 if (t == NULL_TREE || !TYPE_P (t))
20767 return false;
20768 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20771 /* Schedule the generation of the generic parameter dies for the
20772 instance of generic type T. The proper generation itself is later
20773 done by gen_scheduled_generic_parms_dies. */
20775 static void
20776 schedule_generic_params_dies_gen (tree t)
20778 if (!generic_type_p (t))
20779 return;
20781 if (!generic_type_instances)
20782 vec_alloc (generic_type_instances, 256);
20784 vec_safe_push (generic_type_instances, t);
20787 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20788 by append_entry_to_tmpl_value_parm_die_table. This function must
20789 be called after function DIEs have been generated. */
20791 static void
20792 gen_remaining_tmpl_value_param_die_attribute (void)
20794 if (tmpl_value_parm_die_table)
20796 unsigned i;
20797 die_arg_entry *e;
20799 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20800 tree_add_const_value_attribute (e->die, e->arg);
20804 /* Generate generic parameters DIEs for instances of generic types
20805 that have been previously scheduled by
20806 schedule_generic_params_dies_gen. This function must be called
20807 after all the types of the CU have been laid out. */
20809 static void
20810 gen_scheduled_generic_parms_dies (void)
20812 unsigned i;
20813 tree t;
20815 if (!generic_type_instances)
20816 return;
20818 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20819 if (COMPLETE_TYPE_P (t))
20820 gen_generic_params_dies (t);
20824 /* Replace DW_AT_name for the decl with name. */
20826 static void
20827 dwarf2out_set_name (tree decl, tree name)
20829 dw_die_ref die;
20830 dw_attr_ref attr;
20831 const char *dname;
20833 die = TYPE_SYMTAB_DIE (decl);
20834 if (!die)
20835 return;
20837 dname = dwarf2_name (name, 0);
20838 if (!dname)
20839 return;
20841 attr = get_AT (die, DW_AT_name);
20842 if (attr)
20844 struct indirect_string_node *node;
20846 node = find_AT_string (dname);
20847 /* replace the string. */
20848 attr->dw_attr_val.v.val_str = node;
20851 else
20852 add_name_attribute (die, dname);
20855 /* True if before or during processing of the first function being emitted. */
20856 static bool in_first_function_p = true;
20857 /* True if loc_note during dwarf2out_var_location call might still be
20858 before first real instruction at address equal to .Ltext0. */
20859 static bool maybe_at_text_label_p = true;
20860 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20861 static unsigned int first_loclabel_num_not_at_text_label;
20863 /* Called by the final INSN scan whenever we see a var location. We
20864 use it to drop labels in the right places, and throw the location in
20865 our lookup table. */
20867 static void
20868 dwarf2out_var_location (rtx loc_note)
20870 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20871 struct var_loc_node *newloc;
20872 rtx next_real, next_note;
20873 static const char *last_label;
20874 static const char *last_postcall_label;
20875 static bool last_in_cold_section_p;
20876 static rtx expected_next_loc_note;
20877 tree decl;
20878 bool var_loc_p;
20880 if (!NOTE_P (loc_note))
20882 if (CALL_P (loc_note))
20884 call_site_count++;
20885 if (SIBLING_CALL_P (loc_note))
20886 tail_call_site_count++;
20888 return;
20891 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20892 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20893 return;
20895 /* Optimize processing a large consecutive sequence of location
20896 notes so we don't spend too much time in next_real_insn. If the
20897 next insn is another location note, remember the next_real_insn
20898 calculation for next time. */
20899 next_real = cached_next_real_insn;
20900 if (next_real)
20902 if (expected_next_loc_note != loc_note)
20903 next_real = NULL_RTX;
20906 next_note = NEXT_INSN (loc_note);
20907 if (! next_note
20908 || INSN_DELETED_P (next_note)
20909 || ! NOTE_P (next_note)
20910 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20911 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20912 next_note = NULL_RTX;
20914 if (! next_real)
20915 next_real = next_real_insn (loc_note);
20917 if (next_note)
20919 expected_next_loc_note = next_note;
20920 cached_next_real_insn = next_real;
20922 else
20923 cached_next_real_insn = NULL_RTX;
20925 /* If there are no instructions which would be affected by this note,
20926 don't do anything. */
20927 if (var_loc_p
20928 && next_real == NULL_RTX
20929 && !NOTE_DURING_CALL_P (loc_note))
20930 return;
20932 if (next_real == NULL_RTX)
20933 next_real = get_last_insn ();
20935 /* If there were any real insns between note we processed last time
20936 and this note (or if it is the first note), clear
20937 last_{,postcall_}label so that they are not reused this time. */
20938 if (last_var_location_insn == NULL_RTX
20939 || last_var_location_insn != next_real
20940 || last_in_cold_section_p != in_cold_section_p)
20942 last_label = NULL;
20943 last_postcall_label = NULL;
20946 if (var_loc_p)
20948 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20949 newloc = add_var_loc_to_decl (decl, loc_note,
20950 NOTE_DURING_CALL_P (loc_note)
20951 ? last_postcall_label : last_label);
20952 if (newloc == NULL)
20953 return;
20955 else
20957 decl = NULL_TREE;
20958 newloc = NULL;
20961 /* If there were no real insns between note we processed last time
20962 and this note, use the label we emitted last time. Otherwise
20963 create a new label and emit it. */
20964 if (last_label == NULL)
20966 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20967 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20968 loclabel_num++;
20969 last_label = ggc_strdup (loclabel);
20970 /* See if loclabel might be equal to .Ltext0. If yes,
20971 bump first_loclabel_num_not_at_text_label. */
20972 if (!have_multiple_function_sections
20973 && in_first_function_p
20974 && maybe_at_text_label_p)
20976 static rtx last_start;
20977 rtx insn;
20978 for (insn = loc_note; insn; insn = previous_insn (insn))
20979 if (insn == last_start)
20980 break;
20981 else if (!NONDEBUG_INSN_P (insn))
20982 continue;
20983 else
20985 rtx body = PATTERN (insn);
20986 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20987 continue;
20988 /* Inline asm could occupy zero bytes. */
20989 else if (GET_CODE (body) == ASM_INPUT
20990 || asm_noperands (body) >= 0)
20991 continue;
20992 #ifdef HAVE_attr_length
20993 else if (get_attr_min_length (insn) == 0)
20994 continue;
20995 #endif
20996 else
20998 /* Assume insn has non-zero length. */
20999 maybe_at_text_label_p = false;
21000 break;
21003 if (maybe_at_text_label_p)
21005 last_start = loc_note;
21006 first_loclabel_num_not_at_text_label = loclabel_num;
21011 if (!var_loc_p)
21013 struct call_arg_loc_node *ca_loc
21014 = ggc_alloc_cleared_call_arg_loc_node ();
21015 rtx prev = prev_real_insn (loc_note), x;
21016 ca_loc->call_arg_loc_note = loc_note;
21017 ca_loc->next = NULL;
21018 ca_loc->label = last_label;
21019 gcc_assert (prev
21020 && (CALL_P (prev)
21021 || (NONJUMP_INSN_P (prev)
21022 && GET_CODE (PATTERN (prev)) == SEQUENCE
21023 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21024 if (!CALL_P (prev))
21025 prev = XVECEXP (PATTERN (prev), 0, 0);
21026 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21027 x = get_call_rtx_from (PATTERN (prev));
21028 if (x)
21030 x = XEXP (XEXP (x, 0), 0);
21031 if (GET_CODE (x) == SYMBOL_REF
21032 && SYMBOL_REF_DECL (x)
21033 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21034 ca_loc->symbol_ref = x;
21036 ca_loc->block = insn_scope (prev);
21037 if (call_arg_locations)
21038 call_arg_loc_last->next = ca_loc;
21039 else
21040 call_arg_locations = ca_loc;
21041 call_arg_loc_last = ca_loc;
21043 else if (!NOTE_DURING_CALL_P (loc_note))
21044 newloc->label = last_label;
21045 else
21047 if (!last_postcall_label)
21049 sprintf (loclabel, "%s-1", last_label);
21050 last_postcall_label = ggc_strdup (loclabel);
21052 newloc->label = last_postcall_label;
21055 last_var_location_insn = next_real;
21056 last_in_cold_section_p = in_cold_section_p;
21059 /* Note in one location list that text section has changed. */
21061 static int
21062 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21064 var_loc_list *list = (var_loc_list *) *slot;
21065 if (list->first)
21066 list->last_before_switch
21067 = list->last->next ? list->last->next : list->last;
21068 return 1;
21071 /* Note in all location lists that text section has changed. */
21073 static void
21074 var_location_switch_text_section (void)
21076 if (decl_loc_table == NULL)
21077 return;
21079 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21082 /* Create a new line number table. */
21084 static dw_line_info_table *
21085 new_line_info_table (void)
21087 dw_line_info_table *table;
21089 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21090 table->file_num = 1;
21091 table->line_num = 1;
21092 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21094 return table;
21097 /* Lookup the "current" table into which we emit line info, so
21098 that we don't have to do it for every source line. */
21100 static void
21101 set_cur_line_info_table (section *sec)
21103 dw_line_info_table *table;
21105 if (sec == text_section)
21106 table = text_section_line_info;
21107 else if (sec == cold_text_section)
21109 table = cold_text_section_line_info;
21110 if (!table)
21112 cold_text_section_line_info = table = new_line_info_table ();
21113 table->end_label = cold_end_label;
21116 else
21118 const char *end_label;
21120 if (flag_reorder_blocks_and_partition)
21122 if (in_cold_section_p)
21123 end_label = crtl->subsections.cold_section_end_label;
21124 else
21125 end_label = crtl->subsections.hot_section_end_label;
21127 else
21129 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21130 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21131 current_function_funcdef_no);
21132 end_label = ggc_strdup (label);
21135 table = new_line_info_table ();
21136 table->end_label = end_label;
21138 vec_safe_push (separate_line_info, table);
21141 if (DWARF2_ASM_LINE_DEBUG_INFO)
21142 table->is_stmt = (cur_line_info_table
21143 ? cur_line_info_table->is_stmt
21144 : DWARF_LINE_DEFAULT_IS_STMT_START);
21145 cur_line_info_table = table;
21149 /* We need to reset the locations at the beginning of each
21150 function. We can't do this in the end_function hook, because the
21151 declarations that use the locations won't have been output when
21152 that hook is called. Also compute have_multiple_function_sections here. */
21154 static void
21155 dwarf2out_begin_function (tree fun)
21157 section *sec = function_section (fun);
21159 if (sec != text_section)
21160 have_multiple_function_sections = true;
21162 if (flag_reorder_blocks_and_partition && !cold_text_section)
21164 gcc_assert (current_function_decl == fun);
21165 cold_text_section = unlikely_text_section ();
21166 switch_to_section (cold_text_section);
21167 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21168 switch_to_section (sec);
21171 dwarf2out_note_section_used ();
21172 call_site_count = 0;
21173 tail_call_site_count = 0;
21175 set_cur_line_info_table (sec);
21178 /* Helper function of dwarf2out_end_function, called only after emitting
21179 the very first function into assembly. Check if some .debug_loc range
21180 might end with a .LVL* label that could be equal to .Ltext0.
21181 In that case we must force using absolute addresses in .debug_loc ranges,
21182 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21183 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21184 list terminator.
21185 Set have_multiple_function_sections to true in that case and
21186 terminate htab traversal. */
21188 static int
21189 find_empty_loc_ranges_at_text_label (void **slot, void *)
21191 var_loc_list *entry;
21192 struct var_loc_node *node;
21194 entry = (var_loc_list *) *slot;
21195 node = entry->first;
21196 if (node && node->next && node->next->label)
21198 unsigned int i;
21199 const char *label = node->next->label;
21200 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21202 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21204 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21205 if (strcmp (label, loclabel) == 0)
21207 have_multiple_function_sections = true;
21208 return 0;
21212 return 1;
21215 /* Hook called after emitting a function into assembly.
21216 This does something only for the very first function emitted. */
21218 static void
21219 dwarf2out_end_function (unsigned int)
21221 if (in_first_function_p
21222 && !have_multiple_function_sections
21223 && first_loclabel_num_not_at_text_label
21224 && decl_loc_table)
21225 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21226 NULL);
21227 in_first_function_p = false;
21228 maybe_at_text_label_p = false;
21231 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21233 static void
21234 push_dw_line_info_entry (dw_line_info_table *table,
21235 enum dw_line_info_opcode opcode, unsigned int val)
21237 dw_line_info_entry e;
21238 e.opcode = opcode;
21239 e.val = val;
21240 vec_safe_push (table->entries, e);
21243 /* Output a label to mark the beginning of a source code line entry
21244 and record information relating to this source line, in
21245 'line_info_table' for later output of the .debug_line section. */
21246 /* ??? The discriminator parameter ought to be unsigned. */
21248 static void
21249 dwarf2out_source_line (unsigned int line, const char *filename,
21250 int discriminator, bool is_stmt)
21252 unsigned int file_num;
21253 dw_line_info_table *table;
21255 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21256 return;
21258 /* The discriminator column was added in dwarf4. Simplify the below
21259 by simply removing it if we're not supposed to output it. */
21260 if (dwarf_version < 4 && dwarf_strict)
21261 discriminator = 0;
21263 table = cur_line_info_table;
21264 file_num = maybe_emit_file (lookup_filename (filename));
21266 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21267 the debugger has used the second (possibly duplicate) line number
21268 at the beginning of the function to mark the end of the prologue.
21269 We could eliminate any other duplicates within the function. For
21270 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21271 that second line number entry. */
21272 /* Recall that this end-of-prologue indication is *not* the same thing
21273 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21274 to which the hook corresponds, follows the last insn that was
21275 emitted by gen_prologue. What we need is to precede the first insn
21276 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21277 insn that corresponds to something the user wrote. These may be
21278 very different locations once scheduling is enabled. */
21280 if (0 && file_num == table->file_num
21281 && line == table->line_num
21282 && discriminator == table->discrim_num
21283 && is_stmt == table->is_stmt)
21284 return;
21286 switch_to_section (current_function_section ());
21288 /* If requested, emit something human-readable. */
21289 if (flag_debug_asm)
21290 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21292 if (DWARF2_ASM_LINE_DEBUG_INFO)
21294 /* Emit the .loc directive understood by GNU as. */
21295 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21296 file_num, line, is_stmt, discriminator */
21297 fputs ("\t.loc ", asm_out_file);
21298 fprint_ul (asm_out_file, file_num);
21299 putc (' ', asm_out_file);
21300 fprint_ul (asm_out_file, line);
21301 putc (' ', asm_out_file);
21302 putc ('0', asm_out_file);
21304 if (is_stmt != table->is_stmt)
21306 fputs (" is_stmt ", asm_out_file);
21307 putc (is_stmt ? '1' : '0', asm_out_file);
21309 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21311 gcc_assert (discriminator > 0);
21312 fputs (" discriminator ", asm_out_file);
21313 fprint_ul (asm_out_file, (unsigned long) discriminator);
21315 putc ('\n', asm_out_file);
21317 else
21319 unsigned int label_num = ++line_info_label_num;
21321 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21323 push_dw_line_info_entry (table, LI_set_address, label_num);
21324 if (file_num != table->file_num)
21325 push_dw_line_info_entry (table, LI_set_file, file_num);
21326 if (discriminator != table->discrim_num)
21327 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21328 if (is_stmt != table->is_stmt)
21329 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21330 push_dw_line_info_entry (table, LI_set_line, line);
21333 table->file_num = file_num;
21334 table->line_num = line;
21335 table->discrim_num = discriminator;
21336 table->is_stmt = is_stmt;
21337 table->in_use = true;
21340 /* Record the beginning of a new source file. */
21342 static void
21343 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21345 if (flag_eliminate_dwarf2_dups)
21347 /* Record the beginning of the file for break_out_includes. */
21348 dw_die_ref bincl_die;
21350 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21351 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21354 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21356 macinfo_entry e;
21357 e.code = DW_MACINFO_start_file;
21358 e.lineno = lineno;
21359 e.info = ggc_strdup (filename);
21360 vec_safe_push (macinfo_table, e);
21364 /* Record the end of a source file. */
21366 static void
21367 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21369 if (flag_eliminate_dwarf2_dups)
21370 /* Record the end of the file for break_out_includes. */
21371 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21373 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21375 macinfo_entry e;
21376 e.code = DW_MACINFO_end_file;
21377 e.lineno = lineno;
21378 e.info = NULL;
21379 vec_safe_push (macinfo_table, e);
21383 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21384 the tail part of the directive line, i.e. the part which is past the
21385 initial whitespace, #, whitespace, directive-name, whitespace part. */
21387 static void
21388 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21389 const char *buffer ATTRIBUTE_UNUSED)
21391 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21393 macinfo_entry e;
21394 /* Insert a dummy first entry to be able to optimize the whole
21395 predefined macro block using DW_MACRO_GNU_transparent_include. */
21396 if (macinfo_table->is_empty () && lineno <= 1)
21398 e.code = 0;
21399 e.lineno = 0;
21400 e.info = NULL;
21401 vec_safe_push (macinfo_table, e);
21403 e.code = DW_MACINFO_define;
21404 e.lineno = lineno;
21405 e.info = ggc_strdup (buffer);
21406 vec_safe_push (macinfo_table, e);
21410 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21411 the tail part of the directive line, i.e. the part which is past the
21412 initial whitespace, #, whitespace, directive-name, whitespace part. */
21414 static void
21415 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21416 const char *buffer ATTRIBUTE_UNUSED)
21418 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21420 macinfo_entry e;
21421 /* Insert a dummy first entry to be able to optimize the whole
21422 predefined macro block using DW_MACRO_GNU_transparent_include. */
21423 if (macinfo_table->is_empty () && lineno <= 1)
21425 e.code = 0;
21426 e.lineno = 0;
21427 e.info = NULL;
21428 vec_safe_push (macinfo_table, e);
21430 e.code = DW_MACINFO_undef;
21431 e.lineno = lineno;
21432 e.info = ggc_strdup (buffer);
21433 vec_safe_push (macinfo_table, e);
21437 /* Helpers to manipulate hash table of CUs. */
21439 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21441 typedef macinfo_entry value_type;
21442 typedef macinfo_entry compare_type;
21443 static inline hashval_t hash (const value_type *);
21444 static inline bool equal (const value_type *, const compare_type *);
21447 inline hashval_t
21448 macinfo_entry_hasher::hash (const value_type *entry)
21450 return htab_hash_string (entry->info);
21453 inline bool
21454 macinfo_entry_hasher::equal (const value_type *entry1,
21455 const compare_type *entry2)
21457 return !strcmp (entry1->info, entry2->info);
21460 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21462 /* Output a single .debug_macinfo entry. */
21464 static void
21465 output_macinfo_op (macinfo_entry *ref)
21467 int file_num;
21468 size_t len;
21469 struct indirect_string_node *node;
21470 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21471 struct dwarf_file_data *fd;
21473 switch (ref->code)
21475 case DW_MACINFO_start_file:
21476 fd = lookup_filename (ref->info);
21477 file_num = maybe_emit_file (fd);
21478 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21479 dw2_asm_output_data_uleb128 (ref->lineno,
21480 "Included from line number %lu",
21481 (unsigned long) ref->lineno);
21482 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21483 break;
21484 case DW_MACINFO_end_file:
21485 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21486 break;
21487 case DW_MACINFO_define:
21488 case DW_MACINFO_undef:
21489 len = strlen (ref->info) + 1;
21490 if (!dwarf_strict
21491 && len > DWARF_OFFSET_SIZE
21492 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21493 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21495 ref->code = ref->code == DW_MACINFO_define
21496 ? DW_MACRO_GNU_define_indirect
21497 : DW_MACRO_GNU_undef_indirect;
21498 output_macinfo_op (ref);
21499 return;
21501 dw2_asm_output_data (1, ref->code,
21502 ref->code == DW_MACINFO_define
21503 ? "Define macro" : "Undefine macro");
21504 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21505 (unsigned long) ref->lineno);
21506 dw2_asm_output_nstring (ref->info, -1, "The macro");
21507 break;
21508 case DW_MACRO_GNU_define_indirect:
21509 case DW_MACRO_GNU_undef_indirect:
21510 node = find_AT_string (ref->info);
21511 gcc_assert (node
21512 && ((node->form == DW_FORM_strp)
21513 || (node->form == DW_FORM_GNU_str_index)));
21514 dw2_asm_output_data (1, ref->code,
21515 ref->code == DW_MACRO_GNU_define_indirect
21516 ? "Define macro indirect"
21517 : "Undefine macro indirect");
21518 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21519 (unsigned long) ref->lineno);
21520 if (node->form == DW_FORM_strp)
21521 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21522 debug_str_section, "The macro: \"%s\"",
21523 ref->info);
21524 else
21525 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21526 ref->info);
21527 break;
21528 case DW_MACRO_GNU_transparent_include:
21529 dw2_asm_output_data (1, ref->code, "Transparent include");
21530 ASM_GENERATE_INTERNAL_LABEL (label,
21531 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21532 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21533 break;
21534 default:
21535 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21536 ASM_COMMENT_START, (unsigned long) ref->code);
21537 break;
21541 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21542 other compilation unit .debug_macinfo sections. IDX is the first
21543 index of a define/undef, return the number of ops that should be
21544 emitted in a comdat .debug_macinfo section and emit
21545 a DW_MACRO_GNU_transparent_include entry referencing it.
21546 If the define/undef entry should be emitted normally, return 0. */
21548 static unsigned
21549 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21550 macinfo_hash_type *macinfo_htab)
21552 macinfo_entry *first, *second, *cur, *inc;
21553 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21554 unsigned char checksum[16];
21555 struct md5_ctx ctx;
21556 char *grp_name, *tail;
21557 const char *base;
21558 unsigned int i, count, encoded_filename_len, linebuf_len;
21559 macinfo_entry **slot;
21561 first = &(*macinfo_table)[idx];
21562 second = &(*macinfo_table)[idx + 1];
21564 /* Optimize only if there are at least two consecutive define/undef ops,
21565 and either all of them are before first DW_MACINFO_start_file
21566 with lineno {0,1} (i.e. predefined macro block), or all of them are
21567 in some included header file. */
21568 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21569 return 0;
21570 if (vec_safe_is_empty (files))
21572 if (first->lineno > 1 || second->lineno > 1)
21573 return 0;
21575 else if (first->lineno == 0)
21576 return 0;
21578 /* Find the last define/undef entry that can be grouped together
21579 with first and at the same time compute md5 checksum of their
21580 codes, linenumbers and strings. */
21581 md5_init_ctx (&ctx);
21582 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21583 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21584 break;
21585 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21586 break;
21587 else
21589 unsigned char code = cur->code;
21590 md5_process_bytes (&code, 1, &ctx);
21591 checksum_uleb128 (cur->lineno, &ctx);
21592 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21594 md5_finish_ctx (&ctx, checksum);
21595 count = i - idx;
21597 /* From the containing include filename (if any) pick up just
21598 usable characters from its basename. */
21599 if (vec_safe_is_empty (files))
21600 base = "";
21601 else
21602 base = lbasename (files->last ().info);
21603 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21604 if (ISIDNUM (base[i]) || base[i] == '.')
21605 encoded_filename_len++;
21606 /* Count . at the end. */
21607 if (encoded_filename_len)
21608 encoded_filename_len++;
21610 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21611 linebuf_len = strlen (linebuf);
21613 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21614 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21615 + 16 * 2 + 1);
21616 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21617 tail = grp_name + 4;
21618 if (encoded_filename_len)
21620 for (i = 0; base[i]; i++)
21621 if (ISIDNUM (base[i]) || base[i] == '.')
21622 *tail++ = base[i];
21623 *tail++ = '.';
21625 memcpy (tail, linebuf, linebuf_len);
21626 tail += linebuf_len;
21627 *tail++ = '.';
21628 for (i = 0; i < 16; i++)
21629 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21631 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21632 in the empty vector entry before the first define/undef. */
21633 inc = &(*macinfo_table)[idx - 1];
21634 inc->code = DW_MACRO_GNU_transparent_include;
21635 inc->lineno = 0;
21636 inc->info = ggc_strdup (grp_name);
21637 if (!macinfo_htab->is_created ())
21638 macinfo_htab->create (10);
21639 /* Avoid emitting duplicates. */
21640 slot = macinfo_htab->find_slot (inc, INSERT);
21641 if (*slot != NULL)
21643 inc->code = 0;
21644 inc->info = NULL;
21645 /* If such an entry has been used before, just emit
21646 a DW_MACRO_GNU_transparent_include op. */
21647 inc = *slot;
21648 output_macinfo_op (inc);
21649 /* And clear all macinfo_entry in the range to avoid emitting them
21650 in the second pass. */
21651 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21653 cur->code = 0;
21654 cur->info = NULL;
21657 else
21659 *slot = inc;
21660 inc->lineno = macinfo_htab->elements ();
21661 output_macinfo_op (inc);
21663 return count;
21666 /* Save any strings needed by the macinfo table in the debug str
21667 table. All strings must be collected into the table by the time
21668 index_string is called. */
21670 static void
21671 save_macinfo_strings (void)
21673 unsigned len;
21674 unsigned i;
21675 macinfo_entry *ref;
21677 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21679 switch (ref->code)
21681 /* Match the logic in output_macinfo_op to decide on
21682 indirect strings. */
21683 case DW_MACINFO_define:
21684 case DW_MACINFO_undef:
21685 len = strlen (ref->info) + 1;
21686 if (!dwarf_strict
21687 && len > DWARF_OFFSET_SIZE
21688 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21689 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21690 set_indirect_string (find_AT_string (ref->info));
21691 break;
21692 case DW_MACRO_GNU_define_indirect:
21693 case DW_MACRO_GNU_undef_indirect:
21694 set_indirect_string (find_AT_string (ref->info));
21695 break;
21696 default:
21697 break;
21702 /* Output macinfo section(s). */
21704 static void
21705 output_macinfo (void)
21707 unsigned i;
21708 unsigned long length = vec_safe_length (macinfo_table);
21709 macinfo_entry *ref;
21710 vec<macinfo_entry, va_gc> *files = NULL;
21711 macinfo_hash_type macinfo_htab;
21713 if (! length)
21714 return;
21716 /* output_macinfo* uses these interchangeably. */
21717 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21718 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21719 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21720 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21722 /* For .debug_macro emit the section header. */
21723 if (!dwarf_strict)
21725 dw2_asm_output_data (2, 4, "DWARF macro version number");
21726 if (DWARF_OFFSET_SIZE == 8)
21727 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21728 else
21729 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21730 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21731 (!dwarf_split_debug_info ? debug_line_section_label
21732 : debug_skeleton_line_section_label),
21733 debug_line_section, NULL);
21736 /* In the first loop, it emits the primary .debug_macinfo section
21737 and after each emitted op the macinfo_entry is cleared.
21738 If a longer range of define/undef ops can be optimized using
21739 DW_MACRO_GNU_transparent_include, the
21740 DW_MACRO_GNU_transparent_include op is emitted and kept in
21741 the vector before the first define/undef in the range and the
21742 whole range of define/undef ops is not emitted and kept. */
21743 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21745 switch (ref->code)
21747 case DW_MACINFO_start_file:
21748 vec_safe_push (files, *ref);
21749 break;
21750 case DW_MACINFO_end_file:
21751 if (!vec_safe_is_empty (files))
21752 files->pop ();
21753 break;
21754 case DW_MACINFO_define:
21755 case DW_MACINFO_undef:
21756 if (!dwarf_strict
21757 && HAVE_COMDAT_GROUP
21758 && vec_safe_length (files) != 1
21759 && i > 0
21760 && i + 1 < length
21761 && (*macinfo_table)[i - 1].code == 0)
21763 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21764 if (count)
21766 i += count - 1;
21767 continue;
21770 break;
21771 case 0:
21772 /* A dummy entry may be inserted at the beginning to be able
21773 to optimize the whole block of predefined macros. */
21774 if (i == 0)
21775 continue;
21776 default:
21777 break;
21779 output_macinfo_op (ref);
21780 ref->info = NULL;
21781 ref->code = 0;
21784 if (!macinfo_htab.is_created ())
21785 return;
21787 macinfo_htab.dispose ();
21789 /* If any DW_MACRO_GNU_transparent_include were used, on those
21790 DW_MACRO_GNU_transparent_include entries terminate the
21791 current chain and switch to a new comdat .debug_macinfo
21792 section and emit the define/undef entries within it. */
21793 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21794 switch (ref->code)
21796 case 0:
21797 continue;
21798 case DW_MACRO_GNU_transparent_include:
21800 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21801 tree comdat_key = get_identifier (ref->info);
21802 /* Terminate the previous .debug_macinfo section. */
21803 dw2_asm_output_data (1, 0, "End compilation unit");
21804 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21805 SECTION_DEBUG
21806 | SECTION_LINKONCE,
21807 comdat_key);
21808 ASM_GENERATE_INTERNAL_LABEL (label,
21809 DEBUG_MACRO_SECTION_LABEL,
21810 ref->lineno);
21811 ASM_OUTPUT_LABEL (asm_out_file, label);
21812 ref->code = 0;
21813 ref->info = NULL;
21814 dw2_asm_output_data (2, 4, "DWARF macro version number");
21815 if (DWARF_OFFSET_SIZE == 8)
21816 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21817 else
21818 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21820 break;
21821 case DW_MACINFO_define:
21822 case DW_MACINFO_undef:
21823 output_macinfo_op (ref);
21824 ref->code = 0;
21825 ref->info = NULL;
21826 break;
21827 default:
21828 gcc_unreachable ();
21832 /* Set up for Dwarf output at the start of compilation. */
21834 static void
21835 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21837 /* Allocate the file_table. */
21838 file_table = htab_create_ggc (50, file_table_hash,
21839 file_table_eq, NULL);
21841 /* Allocate the decl_die_table. */
21842 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21843 decl_die_table_eq, NULL);
21845 /* Allocate the decl_loc_table. */
21846 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21847 decl_loc_table_eq, NULL);
21849 /* Allocate the cached_dw_loc_list_table. */
21850 cached_dw_loc_list_table
21851 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21852 cached_dw_loc_list_table_eq, NULL);
21854 /* Allocate the initial hunk of the decl_scope_table. */
21855 vec_alloc (decl_scope_table, 256);
21857 /* Allocate the initial hunk of the abbrev_die_table. */
21858 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21859 (ABBREV_DIE_TABLE_INCREMENT);
21860 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21861 /* Zero-th entry is allocated, but unused. */
21862 abbrev_die_table_in_use = 1;
21864 /* Allocate the pubtypes and pubnames vectors. */
21865 vec_alloc (pubname_table, 32);
21866 vec_alloc (pubtype_table, 32);
21868 vec_alloc (incomplete_types, 64);
21870 vec_alloc (used_rtx_array, 32);
21872 if (!dwarf_split_debug_info)
21874 debug_info_section = get_section (DEBUG_INFO_SECTION,
21875 SECTION_DEBUG, NULL);
21876 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21877 SECTION_DEBUG, NULL);
21878 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21879 SECTION_DEBUG, NULL);
21881 else
21883 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21884 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21885 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21886 SECTION_DEBUG | SECTION_EXCLUDE,
21887 NULL);
21888 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21889 SECTION_DEBUG, NULL);
21890 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21891 SECTION_DEBUG, NULL);
21892 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21893 SECTION_DEBUG, NULL);
21894 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21895 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21897 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21898 the main .o, but the skeleton_line goes into the split off dwo. */
21899 debug_skeleton_line_section
21900 = get_section (DEBUG_DWO_LINE_SECTION,
21901 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21902 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21903 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21904 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21905 SECTION_DEBUG | SECTION_EXCLUDE,
21906 NULL);
21907 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21908 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21909 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21910 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21911 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
21912 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
21914 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21915 SECTION_DEBUG, NULL);
21916 debug_macinfo_section = get_section (dwarf_strict
21917 ? DEBUG_MACINFO_SECTION
21918 : DEBUG_MACRO_SECTION,
21919 DEBUG_MACRO_SECTION_FLAGS, NULL);
21920 debug_line_section = get_section (DEBUG_LINE_SECTION,
21921 SECTION_DEBUG, NULL);
21922 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21923 SECTION_DEBUG, NULL);
21924 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21925 SECTION_DEBUG, NULL);
21926 debug_str_section = get_section (DEBUG_STR_SECTION,
21927 DEBUG_STR_SECTION_FLAGS, NULL);
21928 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21929 SECTION_DEBUG, NULL);
21930 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21931 SECTION_DEBUG, NULL);
21933 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21934 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21935 DEBUG_ABBREV_SECTION_LABEL, 0);
21936 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21937 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21938 COLD_TEXT_SECTION_LABEL, 0);
21939 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21941 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21942 DEBUG_INFO_SECTION_LABEL, 0);
21943 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21944 DEBUG_LINE_SECTION_LABEL, 0);
21945 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21946 DEBUG_RANGES_SECTION_LABEL, 0);
21947 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21948 DEBUG_ADDR_SECTION_LABEL, 0);
21949 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21950 dwarf_strict
21951 ? DEBUG_MACINFO_SECTION_LABEL
21952 : DEBUG_MACRO_SECTION_LABEL, 0);
21953 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21955 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21956 vec_alloc (macinfo_table, 64);
21958 switch_to_section (text_section);
21959 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21961 /* Make sure the line number table for .text always exists. */
21962 text_section_line_info = new_line_info_table ();
21963 text_section_line_info->end_label = text_end_label;
21966 /* Called before compile () starts outputtting functions, variables
21967 and toplevel asms into assembly. */
21969 static void
21970 dwarf2out_assembly_start (void)
21972 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21973 && dwarf2out_do_cfi_asm ()
21974 && (!(flag_unwind_tables || flag_exceptions)
21975 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21976 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21979 /* A helper function for dwarf2out_finish called through
21980 htab_traverse. Assign a string its index. All strings must be
21981 collected into the table by the time index_string is called,
21982 because the indexing code relies on htab_traverse to traverse nodes
21983 in the same order for each run. */
21985 static int
21986 index_string (void **h, void *v)
21988 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21989 unsigned int *index = (unsigned int *) v;
21991 find_string_form (node);
21992 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21994 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21995 node->index = *index;
21996 *index += 1;
21998 return 1;
22001 /* A helper function for output_indirect_strings called through
22002 htab_traverse. Output the offset to a string and update the
22003 current offset. */
22005 static int
22006 output_index_string_offset (void **h, void *v)
22008 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22009 unsigned int *offset = (unsigned int *) v;
22011 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22013 /* Assert that this node has been assigned an index. */
22014 gcc_assert (node->index != NO_INDEX_ASSIGNED
22015 && node->index != NOT_INDEXED);
22016 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22017 "indexed string 0x%x: %s", node->index, node->str);
22018 *offset += strlen (node->str) + 1;
22020 return 1;
22023 /* A helper function for dwarf2out_finish called through
22024 htab_traverse. Output the indexed string. */
22026 static int
22027 output_index_string (void **h, void *v)
22029 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22030 unsigned int *cur_idx = (unsigned int *) v;
22032 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22034 /* Assert that the strings are output in the same order as their
22035 indexes were assigned. */
22036 gcc_assert (*cur_idx == node->index);
22037 assemble_string (node->str, strlen (node->str) + 1);
22038 *cur_idx += 1;
22040 return 1;
22043 /* A helper function for dwarf2out_finish called through
22044 htab_traverse. Emit one queued .debug_str string. */
22046 static int
22047 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22049 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22051 node->form = find_string_form (node);
22052 if (node->form == DW_FORM_strp && node->refcount > 0)
22054 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22055 assemble_string (node->str, strlen (node->str) + 1);
22058 return 1;
22061 /* Output the indexed string table. */
22063 static void
22064 output_indirect_strings (void)
22066 switch_to_section (debug_str_section);
22067 if (!dwarf_split_debug_info)
22068 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22069 else
22071 unsigned int offset = 0;
22072 unsigned int cur_idx = 0;
22074 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22076 switch_to_section (debug_str_offsets_section);
22077 htab_traverse_noresize (debug_str_hash,
22078 output_index_string_offset,
22079 &offset);
22080 switch_to_section (debug_str_dwo_section);
22081 htab_traverse_noresize (debug_str_hash,
22082 output_index_string,
22083 &cur_idx);
22087 /* Callback for htab_traverse to assign an index to an entry in the
22088 table, and to write that entry to the .debug_addr section. */
22090 static int
22091 output_addr_table_entry (void **slot, void *data)
22093 addr_table_entry *entry = (addr_table_entry *) *slot;
22094 unsigned int *cur_index = (unsigned int *)data;
22096 if (entry->refcount == 0)
22098 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22099 || entry->index == NOT_INDEXED);
22100 return 1;
22103 gcc_assert (entry->index == *cur_index);
22104 (*cur_index)++;
22106 switch (entry->kind)
22108 case ate_kind_rtx:
22109 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22110 "0x%x", entry->index);
22111 break;
22112 case ate_kind_rtx_dtprel:
22113 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22114 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22115 DWARF2_ADDR_SIZE,
22116 entry->addr.rtl);
22117 fputc ('\n', asm_out_file);
22118 break;
22119 case ate_kind_label:
22120 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22121 "0x%x", entry->index);
22122 break;
22123 default:
22124 gcc_unreachable ();
22126 return 1;
22129 /* Produce the .debug_addr section. */
22131 static void
22132 output_addr_table (void)
22134 unsigned int index = 0;
22135 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22136 return;
22138 switch_to_section (debug_addr_section);
22139 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22142 #if ENABLE_ASSERT_CHECKING
22143 /* Verify that all marks are clear. */
22145 static void
22146 verify_marks_clear (dw_die_ref die)
22148 dw_die_ref c;
22150 gcc_assert (! die->die_mark);
22151 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22153 #endif /* ENABLE_ASSERT_CHECKING */
22155 /* Clear the marks for a die and its children.
22156 Be cool if the mark isn't set. */
22158 static void
22159 prune_unmark_dies (dw_die_ref die)
22161 dw_die_ref c;
22163 if (die->die_mark)
22164 die->die_mark = 0;
22165 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22168 /* Given DIE that we're marking as used, find any other dies
22169 it references as attributes and mark them as used. */
22171 static void
22172 prune_unused_types_walk_attribs (dw_die_ref die)
22174 dw_attr_ref a;
22175 unsigned ix;
22177 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22179 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22181 /* A reference to another DIE.
22182 Make sure that it will get emitted.
22183 If it was broken out into a comdat group, don't follow it. */
22184 if (! AT_ref (a)->comdat_type_p
22185 || a->dw_attr == DW_AT_specification)
22186 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22188 /* Set the string's refcount to 0 so that prune_unused_types_mark
22189 accounts properly for it. */
22190 if (AT_class (a) == dw_val_class_str)
22191 a->dw_attr_val.v.val_str->refcount = 0;
22195 /* Mark the generic parameters and arguments children DIEs of DIE. */
22197 static void
22198 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22200 dw_die_ref c;
22202 if (die == NULL || die->die_child == NULL)
22203 return;
22204 c = die->die_child;
22207 if (is_template_parameter (c))
22208 prune_unused_types_mark (c, 1);
22209 c = c->die_sib;
22210 } while (c && c != die->die_child);
22213 /* Mark DIE as being used. If DOKIDS is true, then walk down
22214 to DIE's children. */
22216 static void
22217 prune_unused_types_mark (dw_die_ref die, int dokids)
22219 dw_die_ref c;
22221 if (die->die_mark == 0)
22223 /* We haven't done this node yet. Mark it as used. */
22224 die->die_mark = 1;
22225 /* If this is the DIE of a generic type instantiation,
22226 mark the children DIEs that describe its generic parms and
22227 args. */
22228 prune_unused_types_mark_generic_parms_dies (die);
22230 /* We also have to mark its parents as used.
22231 (But we don't want to mark our parent's kids due to this,
22232 unless it is a class.) */
22233 if (die->die_parent)
22234 prune_unused_types_mark (die->die_parent,
22235 class_scope_p (die->die_parent));
22237 /* Mark any referenced nodes. */
22238 prune_unused_types_walk_attribs (die);
22240 /* If this node is a specification,
22241 also mark the definition, if it exists. */
22242 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22243 prune_unused_types_mark (die->die_definition, 1);
22246 if (dokids && die->die_mark != 2)
22248 /* We need to walk the children, but haven't done so yet.
22249 Remember that we've walked the kids. */
22250 die->die_mark = 2;
22252 /* If this is an array type, we need to make sure our
22253 kids get marked, even if they're types. If we're
22254 breaking out types into comdat sections, do this
22255 for all type definitions. */
22256 if (die->die_tag == DW_TAG_array_type
22257 || (use_debug_types
22258 && is_type_die (die) && ! is_declaration_die (die)))
22259 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22260 else
22261 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22265 /* For local classes, look if any static member functions were emitted
22266 and if so, mark them. */
22268 static void
22269 prune_unused_types_walk_local_classes (dw_die_ref die)
22271 dw_die_ref c;
22273 if (die->die_mark == 2)
22274 return;
22276 switch (die->die_tag)
22278 case DW_TAG_structure_type:
22279 case DW_TAG_union_type:
22280 case DW_TAG_class_type:
22281 break;
22283 case DW_TAG_subprogram:
22284 if (!get_AT_flag (die, DW_AT_declaration)
22285 || die->die_definition != NULL)
22286 prune_unused_types_mark (die, 1);
22287 return;
22289 default:
22290 return;
22293 /* Mark children. */
22294 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22297 /* Walk the tree DIE and mark types that we actually use. */
22299 static void
22300 prune_unused_types_walk (dw_die_ref die)
22302 dw_die_ref c;
22304 /* Don't do anything if this node is already marked and
22305 children have been marked as well. */
22306 if (die->die_mark == 2)
22307 return;
22309 switch (die->die_tag)
22311 case DW_TAG_structure_type:
22312 case DW_TAG_union_type:
22313 case DW_TAG_class_type:
22314 if (die->die_perennial_p)
22315 break;
22317 for (c = die->die_parent; c; c = c->die_parent)
22318 if (c->die_tag == DW_TAG_subprogram)
22319 break;
22321 /* Finding used static member functions inside of classes
22322 is needed just for local classes, because for other classes
22323 static member function DIEs with DW_AT_specification
22324 are emitted outside of the DW_TAG_*_type. If we ever change
22325 it, we'd need to call this even for non-local classes. */
22326 if (c)
22327 prune_unused_types_walk_local_classes (die);
22329 /* It's a type node --- don't mark it. */
22330 return;
22332 case DW_TAG_const_type:
22333 case DW_TAG_packed_type:
22334 case DW_TAG_pointer_type:
22335 case DW_TAG_reference_type:
22336 case DW_TAG_rvalue_reference_type:
22337 case DW_TAG_volatile_type:
22338 case DW_TAG_typedef:
22339 case DW_TAG_array_type:
22340 case DW_TAG_interface_type:
22341 case DW_TAG_friend:
22342 case DW_TAG_variant_part:
22343 case DW_TAG_enumeration_type:
22344 case DW_TAG_subroutine_type:
22345 case DW_TAG_string_type:
22346 case DW_TAG_set_type:
22347 case DW_TAG_subrange_type:
22348 case DW_TAG_ptr_to_member_type:
22349 case DW_TAG_file_type:
22350 if (die->die_perennial_p)
22351 break;
22353 /* It's a type node --- don't mark it. */
22354 return;
22356 default:
22357 /* Mark everything else. */
22358 break;
22361 if (die->die_mark == 0)
22363 die->die_mark = 1;
22365 /* Now, mark any dies referenced from here. */
22366 prune_unused_types_walk_attribs (die);
22369 die->die_mark = 2;
22371 /* Mark children. */
22372 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22375 /* Increment the string counts on strings referred to from DIE's
22376 attributes. */
22378 static void
22379 prune_unused_types_update_strings (dw_die_ref die)
22381 dw_attr_ref a;
22382 unsigned ix;
22384 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22385 if (AT_class (a) == dw_val_class_str)
22387 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22388 s->refcount++;
22389 /* Avoid unnecessarily putting strings that are used less than
22390 twice in the hash table. */
22391 if (s->refcount
22392 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22394 void ** slot;
22395 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22396 htab_hash_string (s->str),
22397 INSERT);
22398 gcc_assert (*slot == NULL);
22399 *slot = s;
22404 /* Remove from the tree DIE any dies that aren't marked. */
22406 static void
22407 prune_unused_types_prune (dw_die_ref die)
22409 dw_die_ref c;
22411 gcc_assert (die->die_mark);
22412 prune_unused_types_update_strings (die);
22414 if (! die->die_child)
22415 return;
22417 c = die->die_child;
22418 do {
22419 dw_die_ref prev = c;
22420 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22421 if (c == die->die_child)
22423 /* No marked children between 'prev' and the end of the list. */
22424 if (prev == c)
22425 /* No marked children at all. */
22426 die->die_child = NULL;
22427 else
22429 prev->die_sib = c->die_sib;
22430 die->die_child = prev;
22432 return;
22435 if (c != prev->die_sib)
22436 prev->die_sib = c;
22437 prune_unused_types_prune (c);
22438 } while (c != die->die_child);
22441 /* Remove dies representing declarations that we never use. */
22443 static void
22444 prune_unused_types (void)
22446 unsigned int i;
22447 limbo_die_node *node;
22448 comdat_type_node *ctnode;
22449 pubname_ref pub;
22450 dw_die_ref base_type;
22452 #if ENABLE_ASSERT_CHECKING
22453 /* All the marks should already be clear. */
22454 verify_marks_clear (comp_unit_die ());
22455 for (node = limbo_die_list; node; node = node->next)
22456 verify_marks_clear (node->die);
22457 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22458 verify_marks_clear (ctnode->root_die);
22459 #endif /* ENABLE_ASSERT_CHECKING */
22461 /* Mark types that are used in global variables. */
22462 premark_types_used_by_global_vars ();
22464 /* Set the mark on nodes that are actually used. */
22465 prune_unused_types_walk (comp_unit_die ());
22466 for (node = limbo_die_list; node; node = node->next)
22467 prune_unused_types_walk (node->die);
22468 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22470 prune_unused_types_walk (ctnode->root_die);
22471 prune_unused_types_mark (ctnode->type_die, 1);
22474 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22475 are unusual in that they are pubnames that are the children of pubtypes.
22476 They should only be marked via their parent DW_TAG_enumeration_type die,
22477 not as roots in themselves. */
22478 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22479 if (pub->die->die_tag != DW_TAG_enumerator)
22480 prune_unused_types_mark (pub->die, 1);
22481 for (i = 0; base_types.iterate (i, &base_type); i++)
22482 prune_unused_types_mark (base_type, 1);
22484 if (debug_str_hash)
22485 htab_empty (debug_str_hash);
22486 if (skeleton_debug_str_hash)
22487 htab_empty (skeleton_debug_str_hash);
22488 prune_unused_types_prune (comp_unit_die ());
22489 for (node = limbo_die_list; node; node = node->next)
22490 prune_unused_types_prune (node->die);
22491 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22492 prune_unused_types_prune (ctnode->root_die);
22494 /* Leave the marks clear. */
22495 prune_unmark_dies (comp_unit_die ());
22496 for (node = limbo_die_list; node; node = node->next)
22497 prune_unmark_dies (node->die);
22498 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22499 prune_unmark_dies (ctnode->root_die);
22502 /* Set the parameter to true if there are any relative pathnames in
22503 the file table. */
22504 static int
22505 file_table_relative_p (void ** slot, void *param)
22507 bool *p = (bool *) param;
22508 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22509 if (!IS_ABSOLUTE_PATH (d->filename))
22511 *p = true;
22512 return 0;
22514 return 1;
22517 /* Helpers to manipulate hash table of comdat type units. */
22519 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22521 typedef comdat_type_node value_type;
22522 typedef comdat_type_node compare_type;
22523 static inline hashval_t hash (const value_type *);
22524 static inline bool equal (const value_type *, const compare_type *);
22527 inline hashval_t
22528 comdat_type_hasher::hash (const value_type *type_node)
22530 hashval_t h;
22531 memcpy (&h, type_node->signature, sizeof (h));
22532 return h;
22535 inline bool
22536 comdat_type_hasher::equal (const value_type *type_node_1,
22537 const compare_type *type_node_2)
22539 return (! memcmp (type_node_1->signature, type_node_2->signature,
22540 DWARF_TYPE_SIGNATURE_SIZE));
22543 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22544 to the location it would have been added, should we know its
22545 DECL_ASSEMBLER_NAME when we added other attributes. This will
22546 probably improve compactness of debug info, removing equivalent
22547 abbrevs, and hide any differences caused by deferring the
22548 computation of the assembler name, triggered by e.g. PCH. */
22550 static inline void
22551 move_linkage_attr (dw_die_ref die)
22553 unsigned ix = vec_safe_length (die->die_attr);
22554 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22556 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22557 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22559 while (--ix > 0)
22561 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22563 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22564 break;
22567 if (ix != vec_safe_length (die->die_attr) - 1)
22569 die->die_attr->pop ();
22570 die->die_attr->quick_insert (ix, linkage);
22574 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22575 referenced from typed stack ops and count how often they are used. */
22577 static void
22578 mark_base_types (dw_loc_descr_ref loc)
22580 dw_die_ref base_type = NULL;
22582 for (; loc; loc = loc->dw_loc_next)
22584 switch (loc->dw_loc_opc)
22586 case DW_OP_GNU_regval_type:
22587 case DW_OP_GNU_deref_type:
22588 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22589 break;
22590 case DW_OP_GNU_convert:
22591 case DW_OP_GNU_reinterpret:
22592 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22593 continue;
22594 /* FALLTHRU */
22595 case DW_OP_GNU_const_type:
22596 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22597 break;
22598 case DW_OP_GNU_entry_value:
22599 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22600 continue;
22601 default:
22602 continue;
22604 gcc_assert (base_type->die_parent == comp_unit_die ());
22605 if (base_type->die_mark)
22606 base_type->die_mark++;
22607 else
22609 base_types.safe_push (base_type);
22610 base_type->die_mark = 1;
22615 /* Comparison function for sorting marked base types. */
22617 static int
22618 base_type_cmp (const void *x, const void *y)
22620 dw_die_ref dx = *(const dw_die_ref *) x;
22621 dw_die_ref dy = *(const dw_die_ref *) y;
22622 unsigned int byte_size1, byte_size2;
22623 unsigned int encoding1, encoding2;
22624 if (dx->die_mark > dy->die_mark)
22625 return -1;
22626 if (dx->die_mark < dy->die_mark)
22627 return 1;
22628 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22629 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22630 if (byte_size1 < byte_size2)
22631 return 1;
22632 if (byte_size1 > byte_size2)
22633 return -1;
22634 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22635 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22636 if (encoding1 < encoding2)
22637 return 1;
22638 if (encoding1 > encoding2)
22639 return -1;
22640 return 0;
22643 /* Move base types marked by mark_base_types as early as possible
22644 in the CU, sorted by decreasing usage count both to make the
22645 uleb128 references as small as possible and to make sure they
22646 will have die_offset already computed by calc_die_sizes when
22647 sizes of typed stack loc ops is computed. */
22649 static void
22650 move_marked_base_types (void)
22652 unsigned int i;
22653 dw_die_ref base_type, die, c;
22655 if (base_types.is_empty ())
22656 return;
22658 /* Sort by decreasing usage count, they will be added again in that
22659 order later on. */
22660 base_types.qsort (base_type_cmp);
22661 die = comp_unit_die ();
22662 c = die->die_child;
22665 dw_die_ref prev = c;
22666 c = c->die_sib;
22667 while (c->die_mark)
22669 remove_child_with_prev (c, prev);
22670 /* As base types got marked, there must be at least
22671 one node other than DW_TAG_base_type. */
22672 gcc_assert (c != c->die_sib);
22673 c = c->die_sib;
22676 while (c != die->die_child);
22677 gcc_assert (die->die_child);
22678 c = die->die_child;
22679 for (i = 0; base_types.iterate (i, &base_type); i++)
22681 base_type->die_mark = 0;
22682 base_type->die_sib = c->die_sib;
22683 c->die_sib = base_type;
22684 c = base_type;
22688 /* Helper function for resolve_addr, attempt to resolve
22689 one CONST_STRING, return non-zero if not successful. Similarly verify that
22690 SYMBOL_REFs refer to variables emitted in the current CU. */
22692 static int
22693 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22695 rtx rtl = *addr;
22697 if (GET_CODE (rtl) == CONST_STRING)
22699 size_t len = strlen (XSTR (rtl, 0)) + 1;
22700 tree t = build_string (len, XSTR (rtl, 0));
22701 tree tlen = size_int (len - 1);
22702 TREE_TYPE (t)
22703 = build_array_type (char_type_node, build_index_type (tlen));
22704 rtl = lookup_constant_def (t);
22705 if (!rtl || !MEM_P (rtl))
22706 return 1;
22707 rtl = XEXP (rtl, 0);
22708 if (GET_CODE (rtl) == SYMBOL_REF
22709 && SYMBOL_REF_DECL (rtl)
22710 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22711 return 1;
22712 vec_safe_push (used_rtx_array, rtl);
22713 *addr = rtl;
22714 return 0;
22717 if (GET_CODE (rtl) == SYMBOL_REF
22718 && SYMBOL_REF_DECL (rtl))
22720 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22722 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22723 return 1;
22725 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22726 return 1;
22729 if (GET_CODE (rtl) == CONST
22730 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22731 return 1;
22733 return 0;
22736 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22737 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22738 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22740 static rtx
22741 string_cst_pool_decl (tree t)
22743 rtx rtl = output_constant_def (t, 1);
22744 unsigned char *array;
22745 dw_loc_descr_ref l;
22746 tree decl;
22747 size_t len;
22748 dw_die_ref ref;
22750 if (!rtl || !MEM_P (rtl))
22751 return NULL_RTX;
22752 rtl = XEXP (rtl, 0);
22753 if (GET_CODE (rtl) != SYMBOL_REF
22754 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22755 return NULL_RTX;
22757 decl = SYMBOL_REF_DECL (rtl);
22758 if (!lookup_decl_die (decl))
22760 len = TREE_STRING_LENGTH (t);
22761 vec_safe_push (used_rtx_array, rtl);
22762 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22763 array = (unsigned char *) ggc_alloc_atomic (len);
22764 memcpy (array, TREE_STRING_POINTER (t), len);
22765 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22766 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22767 l->dw_loc_oprnd2.v.val_vec.length = len;
22768 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22769 l->dw_loc_oprnd2.v.val_vec.array = array;
22770 add_AT_loc (ref, DW_AT_location, l);
22771 equate_decl_number_to_die (decl, ref);
22773 return rtl;
22776 /* Helper function of resolve_addr_in_expr. LOC is
22777 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22778 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22779 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22780 with DW_OP_GNU_implicit_pointer if possible
22781 and return true, if unsuccessful, return false. */
22783 static bool
22784 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22786 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22787 HOST_WIDE_INT offset = 0;
22788 dw_die_ref ref = NULL;
22789 tree decl;
22791 if (GET_CODE (rtl) == CONST
22792 && GET_CODE (XEXP (rtl, 0)) == PLUS
22793 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22795 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22796 rtl = XEXP (XEXP (rtl, 0), 0);
22798 if (GET_CODE (rtl) == CONST_STRING)
22800 size_t len = strlen (XSTR (rtl, 0)) + 1;
22801 tree t = build_string (len, XSTR (rtl, 0));
22802 tree tlen = size_int (len - 1);
22804 TREE_TYPE (t)
22805 = build_array_type (char_type_node, build_index_type (tlen));
22806 rtl = string_cst_pool_decl (t);
22807 if (!rtl)
22808 return false;
22810 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22812 decl = SYMBOL_REF_DECL (rtl);
22813 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22815 ref = lookup_decl_die (decl);
22816 if (ref && (get_AT (ref, DW_AT_location)
22817 || get_AT (ref, DW_AT_const_value)))
22819 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22820 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22821 loc->dw_loc_oprnd1.val_entry = NULL;
22822 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22823 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22824 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22825 loc->dw_loc_oprnd2.v.val_int = offset;
22826 return true;
22830 return false;
22833 /* Helper function for resolve_addr, handle one location
22834 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22835 the location list couldn't be resolved. */
22837 static bool
22838 resolve_addr_in_expr (dw_loc_descr_ref loc)
22840 dw_loc_descr_ref keep = NULL;
22841 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22842 switch (loc->dw_loc_opc)
22844 case DW_OP_addr:
22845 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22847 if ((prev == NULL
22848 || prev->dw_loc_opc == DW_OP_piece
22849 || prev->dw_loc_opc == DW_OP_bit_piece)
22850 && loc->dw_loc_next
22851 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22852 && !dwarf_strict
22853 && optimize_one_addr_into_implicit_ptr (loc))
22854 break;
22855 return false;
22857 break;
22858 case DW_OP_GNU_addr_index:
22859 case DW_OP_GNU_const_index:
22860 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22861 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22862 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22863 NULL))
22864 return false;
22865 break;
22866 case DW_OP_const4u:
22867 case DW_OP_const8u:
22868 if (loc->dtprel
22869 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22870 return false;
22871 break;
22872 case DW_OP_plus_uconst:
22873 if (size_of_loc_descr (loc)
22874 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22876 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22878 dw_loc_descr_ref repl
22879 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22880 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22881 add_loc_descr (&repl, loc->dw_loc_next);
22882 *loc = *repl;
22884 break;
22885 case DW_OP_implicit_value:
22886 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22887 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22888 return false;
22889 break;
22890 case DW_OP_GNU_implicit_pointer:
22891 case DW_OP_GNU_parameter_ref:
22892 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22894 dw_die_ref ref
22895 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22896 if (ref == NULL)
22897 return false;
22898 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22899 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22900 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22902 break;
22903 case DW_OP_GNU_const_type:
22904 case DW_OP_GNU_regval_type:
22905 case DW_OP_GNU_deref_type:
22906 case DW_OP_GNU_convert:
22907 case DW_OP_GNU_reinterpret:
22908 while (loc->dw_loc_next
22909 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22911 dw_die_ref base1, base2;
22912 unsigned enc1, enc2, size1, size2;
22913 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22914 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22915 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22916 else if (loc->dw_loc_oprnd1.val_class
22917 == dw_val_class_unsigned_const)
22918 break;
22919 else
22920 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22921 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22922 == dw_val_class_unsigned_const)
22923 break;
22924 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22925 gcc_assert (base1->die_tag == DW_TAG_base_type
22926 && base2->die_tag == DW_TAG_base_type);
22927 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22928 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22929 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22930 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22931 if (size1 == size2
22932 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22933 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22934 && loc != keep)
22935 || enc1 == enc2))
22937 /* Optimize away next DW_OP_GNU_convert after
22938 adjusting LOC's base type die reference. */
22939 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22940 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22941 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22942 else
22943 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22944 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22945 continue;
22947 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22948 point typed stack entry. */
22949 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22950 keep = loc->dw_loc_next;
22951 break;
22953 break;
22954 default:
22955 break;
22957 return true;
22960 /* Helper function of resolve_addr. DIE had DW_AT_location of
22961 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
22962 and DW_OP_addr couldn't be resolved. resolve_addr has already
22963 removed the DW_AT_location attribute. This function attempts to
22964 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
22965 to it or DW_AT_const_value attribute, if possible. */
22967 static void
22968 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
22970 if (TREE_CODE (decl) != VAR_DECL
22971 || lookup_decl_die (decl) != die
22972 || DECL_EXTERNAL (decl)
22973 || !TREE_STATIC (decl)
22974 || DECL_INITIAL (decl) == NULL_TREE
22975 || DECL_P (DECL_INITIAL (decl))
22976 || get_AT (die, DW_AT_const_value))
22977 return;
22979 tree init = DECL_INITIAL (decl);
22980 HOST_WIDE_INT offset = 0;
22981 /* For variables that have been optimized away and thus
22982 don't have a memory location, see if we can emit
22983 DW_AT_const_value instead. */
22984 if (tree_add_const_value_attribute (die, init))
22985 return;
22986 if (dwarf_strict)
22987 return;
22988 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
22989 and ADDR_EXPR refers to a decl that has DW_AT_location or
22990 DW_AT_const_value (but isn't addressable, otherwise
22991 resolving the original DW_OP_addr wouldn't fail), see if
22992 we can add DW_OP_GNU_implicit_pointer. */
22993 STRIP_NOPS (init);
22994 if (TREE_CODE (init) == POINTER_PLUS_EXPR
22995 && host_integerp (TREE_OPERAND (init, 1), 0))
22997 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
22998 init = TREE_OPERAND (init, 0);
22999 STRIP_NOPS (init);
23001 if (TREE_CODE (init) != ADDR_EXPR)
23002 return;
23003 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23004 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23005 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23006 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23007 && TREE_OPERAND (init, 0) != decl))
23009 dw_die_ref ref;
23010 dw_loc_descr_ref l;
23012 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23014 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23015 if (!rtl)
23016 return;
23017 decl = SYMBOL_REF_DECL (rtl);
23019 else
23020 decl = TREE_OPERAND (init, 0);
23021 ref = lookup_decl_die (decl);
23022 if (ref == NULL
23023 || (!get_AT (ref, DW_AT_location)
23024 && !get_AT (ref, DW_AT_const_value)))
23025 return;
23026 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23027 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23028 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23029 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23030 add_AT_loc (die, DW_AT_location, l);
23034 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23035 an address in .rodata section if the string literal is emitted there,
23036 or remove the containing location list or replace DW_AT_const_value
23037 with DW_AT_location and empty location expression, if it isn't found
23038 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23039 to something that has been emitted in the current CU. */
23041 static void
23042 resolve_addr (dw_die_ref die)
23044 dw_die_ref c;
23045 dw_attr_ref a;
23046 dw_loc_list_ref *curr, *start, loc;
23047 unsigned ix;
23049 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23050 switch (AT_class (a))
23052 case dw_val_class_loc_list:
23053 start = curr = AT_loc_list_ptr (a);
23054 loc = *curr;
23055 gcc_assert (loc);
23056 /* The same list can be referenced more than once. See if we have
23057 already recorded the result from a previous pass. */
23058 if (loc->replaced)
23059 *curr = loc->dw_loc_next;
23060 else if (!loc->resolved_addr)
23062 /* As things stand, we do not expect or allow one die to
23063 reference a suffix of another die's location list chain.
23064 References must be identical or completely separate.
23065 There is therefore no need to cache the result of this
23066 pass on any list other than the first; doing so
23067 would lead to unnecessary writes. */
23068 while (*curr)
23070 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23071 if (!resolve_addr_in_expr ((*curr)->expr))
23073 dw_loc_list_ref next = (*curr)->dw_loc_next;
23074 dw_loc_descr_ref l = (*curr)->expr;
23076 if (next && (*curr)->ll_symbol)
23078 gcc_assert (!next->ll_symbol);
23079 next->ll_symbol = (*curr)->ll_symbol;
23081 if (dwarf_split_debug_info)
23082 remove_loc_list_addr_table_entries (l);
23083 *curr = next;
23085 else
23087 mark_base_types ((*curr)->expr);
23088 curr = &(*curr)->dw_loc_next;
23091 if (loc == *start)
23092 loc->resolved_addr = 1;
23093 else
23095 loc->replaced = 1;
23096 loc->dw_loc_next = *start;
23099 if (!*start)
23101 remove_AT (die, a->dw_attr);
23102 ix--;
23104 break;
23105 case dw_val_class_loc:
23107 dw_loc_descr_ref l = AT_loc (a);
23108 /* For -gdwarf-2 don't attempt to optimize
23109 DW_AT_data_member_location containing
23110 DW_OP_plus_uconst - older consumers might
23111 rely on it being that op instead of a more complex,
23112 but shorter, location description. */
23113 if ((dwarf_version > 2
23114 || a->dw_attr != DW_AT_data_member_location
23115 || l == NULL
23116 || l->dw_loc_opc != DW_OP_plus_uconst
23117 || l->dw_loc_next != NULL)
23118 && !resolve_addr_in_expr (l))
23120 if (dwarf_split_debug_info)
23121 remove_loc_list_addr_table_entries (l);
23122 if (l != NULL
23123 && l->dw_loc_next == NULL
23124 && l->dw_loc_opc == DW_OP_addr
23125 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23126 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23127 && a->dw_attr == DW_AT_location)
23129 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23130 remove_AT (die, a->dw_attr);
23131 ix--;
23132 optimize_location_into_implicit_ptr (die, decl);
23133 break;
23135 remove_AT (die, a->dw_attr);
23136 ix--;
23138 else
23139 mark_base_types (l);
23141 break;
23142 case dw_val_class_addr:
23143 if (a->dw_attr == DW_AT_const_value
23144 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23146 if (AT_index (a) != NOT_INDEXED)
23147 remove_addr_table_entry (a->dw_attr_val.val_entry);
23148 remove_AT (die, a->dw_attr);
23149 ix--;
23151 if (die->die_tag == DW_TAG_GNU_call_site
23152 && a->dw_attr == DW_AT_abstract_origin)
23154 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23155 dw_die_ref tdie = lookup_decl_die (tdecl);
23156 if (tdie == NULL
23157 && DECL_EXTERNAL (tdecl)
23158 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23160 force_decl_die (tdecl);
23161 tdie = lookup_decl_die (tdecl);
23163 if (tdie)
23165 a->dw_attr_val.val_class = dw_val_class_die_ref;
23166 a->dw_attr_val.v.val_die_ref.die = tdie;
23167 a->dw_attr_val.v.val_die_ref.external = 0;
23169 else
23171 if (AT_index (a) != NOT_INDEXED)
23172 remove_addr_table_entry (a->dw_attr_val.val_entry);
23173 remove_AT (die, a->dw_attr);
23174 ix--;
23177 break;
23178 default:
23179 break;
23182 FOR_EACH_CHILD (die, c, resolve_addr (c));
23185 /* Helper routines for optimize_location_lists.
23186 This pass tries to share identical local lists in .debug_loc
23187 section. */
23189 /* Iteratively hash operands of LOC opcode. */
23191 static hashval_t
23192 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23194 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23195 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23197 switch (loc->dw_loc_opc)
23199 case DW_OP_const4u:
23200 case DW_OP_const8u:
23201 if (loc->dtprel)
23202 goto hash_addr;
23203 /* FALLTHRU */
23204 case DW_OP_const1u:
23205 case DW_OP_const1s:
23206 case DW_OP_const2u:
23207 case DW_OP_const2s:
23208 case DW_OP_const4s:
23209 case DW_OP_const8s:
23210 case DW_OP_constu:
23211 case DW_OP_consts:
23212 case DW_OP_pick:
23213 case DW_OP_plus_uconst:
23214 case DW_OP_breg0:
23215 case DW_OP_breg1:
23216 case DW_OP_breg2:
23217 case DW_OP_breg3:
23218 case DW_OP_breg4:
23219 case DW_OP_breg5:
23220 case DW_OP_breg6:
23221 case DW_OP_breg7:
23222 case DW_OP_breg8:
23223 case DW_OP_breg9:
23224 case DW_OP_breg10:
23225 case DW_OP_breg11:
23226 case DW_OP_breg12:
23227 case DW_OP_breg13:
23228 case DW_OP_breg14:
23229 case DW_OP_breg15:
23230 case DW_OP_breg16:
23231 case DW_OP_breg17:
23232 case DW_OP_breg18:
23233 case DW_OP_breg19:
23234 case DW_OP_breg20:
23235 case DW_OP_breg21:
23236 case DW_OP_breg22:
23237 case DW_OP_breg23:
23238 case DW_OP_breg24:
23239 case DW_OP_breg25:
23240 case DW_OP_breg26:
23241 case DW_OP_breg27:
23242 case DW_OP_breg28:
23243 case DW_OP_breg29:
23244 case DW_OP_breg30:
23245 case DW_OP_breg31:
23246 case DW_OP_regx:
23247 case DW_OP_fbreg:
23248 case DW_OP_piece:
23249 case DW_OP_deref_size:
23250 case DW_OP_xderef_size:
23251 hash = iterative_hash_object (val1->v.val_int, hash);
23252 break;
23253 case DW_OP_skip:
23254 case DW_OP_bra:
23256 int offset;
23258 gcc_assert (val1->val_class == dw_val_class_loc);
23259 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23260 hash = iterative_hash_object (offset, hash);
23262 break;
23263 case DW_OP_implicit_value:
23264 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23265 switch (val2->val_class)
23267 case dw_val_class_const:
23268 hash = iterative_hash_object (val2->v.val_int, hash);
23269 break;
23270 case dw_val_class_vec:
23272 unsigned int elt_size = val2->v.val_vec.elt_size;
23273 unsigned int len = val2->v.val_vec.length;
23275 hash = iterative_hash_object (elt_size, hash);
23276 hash = iterative_hash_object (len, hash);
23277 hash = iterative_hash (val2->v.val_vec.array,
23278 len * elt_size, hash);
23280 break;
23281 case dw_val_class_const_double:
23282 hash = iterative_hash_object (val2->v.val_double.low, hash);
23283 hash = iterative_hash_object (val2->v.val_double.high, hash);
23284 break;
23285 case dw_val_class_addr:
23286 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23287 break;
23288 default:
23289 gcc_unreachable ();
23291 break;
23292 case DW_OP_bregx:
23293 case DW_OP_bit_piece:
23294 hash = iterative_hash_object (val1->v.val_int, hash);
23295 hash = iterative_hash_object (val2->v.val_int, hash);
23296 break;
23297 case DW_OP_addr:
23298 hash_addr:
23299 if (loc->dtprel)
23301 unsigned char dtprel = 0xd1;
23302 hash = iterative_hash_object (dtprel, hash);
23304 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23305 break;
23306 case DW_OP_GNU_addr_index:
23307 case DW_OP_GNU_const_index:
23309 if (loc->dtprel)
23311 unsigned char dtprel = 0xd1;
23312 hash = iterative_hash_object (dtprel, hash);
23314 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23316 break;
23317 case DW_OP_GNU_implicit_pointer:
23318 hash = iterative_hash_object (val2->v.val_int, hash);
23319 break;
23320 case DW_OP_GNU_entry_value:
23321 hash = hash_loc_operands (val1->v.val_loc, hash);
23322 break;
23323 case DW_OP_GNU_regval_type:
23324 case DW_OP_GNU_deref_type:
23326 unsigned int byte_size
23327 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23328 unsigned int encoding
23329 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23330 hash = iterative_hash_object (val1->v.val_int, hash);
23331 hash = iterative_hash_object (byte_size, hash);
23332 hash = iterative_hash_object (encoding, hash);
23334 break;
23335 case DW_OP_GNU_convert:
23336 case DW_OP_GNU_reinterpret:
23337 if (val1->val_class == dw_val_class_unsigned_const)
23339 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23340 break;
23342 /* FALLTHRU */
23343 case DW_OP_GNU_const_type:
23345 unsigned int byte_size
23346 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23347 unsigned int encoding
23348 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23349 hash = iterative_hash_object (byte_size, hash);
23350 hash = iterative_hash_object (encoding, hash);
23351 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23352 break;
23353 hash = iterative_hash_object (val2->val_class, hash);
23354 switch (val2->val_class)
23356 case dw_val_class_const:
23357 hash = iterative_hash_object (val2->v.val_int, hash);
23358 break;
23359 case dw_val_class_vec:
23361 unsigned int elt_size = val2->v.val_vec.elt_size;
23362 unsigned int len = val2->v.val_vec.length;
23364 hash = iterative_hash_object (elt_size, hash);
23365 hash = iterative_hash_object (len, hash);
23366 hash = iterative_hash (val2->v.val_vec.array,
23367 len * elt_size, hash);
23369 break;
23370 case dw_val_class_const_double:
23371 hash = iterative_hash_object (val2->v.val_double.low, hash);
23372 hash = iterative_hash_object (val2->v.val_double.high, hash);
23373 break;
23374 default:
23375 gcc_unreachable ();
23378 break;
23380 default:
23381 /* Other codes have no operands. */
23382 break;
23384 return hash;
23387 /* Iteratively hash the whole DWARF location expression LOC. */
23389 static inline hashval_t
23390 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23392 dw_loc_descr_ref l;
23393 bool sizes_computed = false;
23394 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23395 size_of_locs (loc);
23397 for (l = loc; l != NULL; l = l->dw_loc_next)
23399 enum dwarf_location_atom opc = l->dw_loc_opc;
23400 hash = iterative_hash_object (opc, hash);
23401 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23403 size_of_locs (loc);
23404 sizes_computed = true;
23406 hash = hash_loc_operands (l, hash);
23408 return hash;
23411 /* Compute hash of the whole location list LIST_HEAD. */
23413 static inline void
23414 hash_loc_list (dw_loc_list_ref list_head)
23416 dw_loc_list_ref curr = list_head;
23417 hashval_t hash = 0;
23419 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23421 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23422 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23423 if (curr->section)
23424 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23425 hash);
23426 hash = hash_locs (curr->expr, hash);
23428 list_head->hash = hash;
23431 /* Return true if X and Y opcodes have the same operands. */
23433 static inline bool
23434 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23436 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23437 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23438 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23439 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23441 switch (x->dw_loc_opc)
23443 case DW_OP_const4u:
23444 case DW_OP_const8u:
23445 if (x->dtprel)
23446 goto hash_addr;
23447 /* FALLTHRU */
23448 case DW_OP_const1u:
23449 case DW_OP_const1s:
23450 case DW_OP_const2u:
23451 case DW_OP_const2s:
23452 case DW_OP_const4s:
23453 case DW_OP_const8s:
23454 case DW_OP_constu:
23455 case DW_OP_consts:
23456 case DW_OP_pick:
23457 case DW_OP_plus_uconst:
23458 case DW_OP_breg0:
23459 case DW_OP_breg1:
23460 case DW_OP_breg2:
23461 case DW_OP_breg3:
23462 case DW_OP_breg4:
23463 case DW_OP_breg5:
23464 case DW_OP_breg6:
23465 case DW_OP_breg7:
23466 case DW_OP_breg8:
23467 case DW_OP_breg9:
23468 case DW_OP_breg10:
23469 case DW_OP_breg11:
23470 case DW_OP_breg12:
23471 case DW_OP_breg13:
23472 case DW_OP_breg14:
23473 case DW_OP_breg15:
23474 case DW_OP_breg16:
23475 case DW_OP_breg17:
23476 case DW_OP_breg18:
23477 case DW_OP_breg19:
23478 case DW_OP_breg20:
23479 case DW_OP_breg21:
23480 case DW_OP_breg22:
23481 case DW_OP_breg23:
23482 case DW_OP_breg24:
23483 case DW_OP_breg25:
23484 case DW_OP_breg26:
23485 case DW_OP_breg27:
23486 case DW_OP_breg28:
23487 case DW_OP_breg29:
23488 case DW_OP_breg30:
23489 case DW_OP_breg31:
23490 case DW_OP_regx:
23491 case DW_OP_fbreg:
23492 case DW_OP_piece:
23493 case DW_OP_deref_size:
23494 case DW_OP_xderef_size:
23495 return valx1->v.val_int == valy1->v.val_int;
23496 case DW_OP_skip:
23497 case DW_OP_bra:
23498 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23499 can cause irrelevant differences in dw_loc_addr. */
23500 gcc_assert (valx1->val_class == dw_val_class_loc
23501 && valy1->val_class == dw_val_class_loc
23502 && (dwarf_split_debug_info
23503 || x->dw_loc_addr == y->dw_loc_addr));
23504 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23505 case DW_OP_implicit_value:
23506 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23507 || valx2->val_class != valy2->val_class)
23508 return false;
23509 switch (valx2->val_class)
23511 case dw_val_class_const:
23512 return valx2->v.val_int == valy2->v.val_int;
23513 case dw_val_class_vec:
23514 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23515 && valx2->v.val_vec.length == valy2->v.val_vec.length
23516 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23517 valx2->v.val_vec.elt_size
23518 * valx2->v.val_vec.length) == 0;
23519 case dw_val_class_const_double:
23520 return valx2->v.val_double.low == valy2->v.val_double.low
23521 && valx2->v.val_double.high == valy2->v.val_double.high;
23522 case dw_val_class_addr:
23523 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23524 default:
23525 gcc_unreachable ();
23527 case DW_OP_bregx:
23528 case DW_OP_bit_piece:
23529 return valx1->v.val_int == valy1->v.val_int
23530 && valx2->v.val_int == valy2->v.val_int;
23531 case DW_OP_addr:
23532 hash_addr:
23533 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23534 case DW_OP_GNU_addr_index:
23535 case DW_OP_GNU_const_index:
23537 rtx ax1 = valx1->val_entry->addr.rtl;
23538 rtx ay1 = valy1->val_entry->addr.rtl;
23539 return rtx_equal_p (ax1, ay1);
23541 case DW_OP_GNU_implicit_pointer:
23542 return valx1->val_class == dw_val_class_die_ref
23543 && valx1->val_class == valy1->val_class
23544 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23545 && valx2->v.val_int == valy2->v.val_int;
23546 case DW_OP_GNU_entry_value:
23547 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23548 case DW_OP_GNU_const_type:
23549 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23550 || valx2->val_class != valy2->val_class)
23551 return false;
23552 switch (valx2->val_class)
23554 case dw_val_class_const:
23555 return valx2->v.val_int == valy2->v.val_int;
23556 case dw_val_class_vec:
23557 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23558 && valx2->v.val_vec.length == valy2->v.val_vec.length
23559 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23560 valx2->v.val_vec.elt_size
23561 * valx2->v.val_vec.length) == 0;
23562 case dw_val_class_const_double:
23563 return valx2->v.val_double.low == valy2->v.val_double.low
23564 && valx2->v.val_double.high == valy2->v.val_double.high;
23565 default:
23566 gcc_unreachable ();
23568 case DW_OP_GNU_regval_type:
23569 case DW_OP_GNU_deref_type:
23570 return valx1->v.val_int == valy1->v.val_int
23571 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23572 case DW_OP_GNU_convert:
23573 case DW_OP_GNU_reinterpret:
23574 if (valx1->val_class != valy1->val_class)
23575 return false;
23576 if (valx1->val_class == dw_val_class_unsigned_const)
23577 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23578 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23579 case DW_OP_GNU_parameter_ref:
23580 return valx1->val_class == dw_val_class_die_ref
23581 && valx1->val_class == valy1->val_class
23582 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23583 default:
23584 /* Other codes have no operands. */
23585 return true;
23589 /* Return true if DWARF location expressions X and Y are the same. */
23591 static inline bool
23592 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23594 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23595 if (x->dw_loc_opc != y->dw_loc_opc
23596 || x->dtprel != y->dtprel
23597 || !compare_loc_operands (x, y))
23598 break;
23599 return x == NULL && y == NULL;
23602 /* Hashtable helpers. */
23604 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23606 typedef dw_loc_list_struct value_type;
23607 typedef dw_loc_list_struct compare_type;
23608 static inline hashval_t hash (const value_type *);
23609 static inline bool equal (const value_type *, const compare_type *);
23612 /* Return precomputed hash of location list X. */
23614 inline hashval_t
23615 loc_list_hasher::hash (const value_type *x)
23617 return x->hash;
23620 /* Return true if location lists A and B are the same. */
23622 inline bool
23623 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23625 if (a == b)
23626 return 1;
23627 if (a->hash != b->hash)
23628 return 0;
23629 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23630 if (strcmp (a->begin, b->begin) != 0
23631 || strcmp (a->end, b->end) != 0
23632 || (a->section == NULL) != (b->section == NULL)
23633 || (a->section && strcmp (a->section, b->section) != 0)
23634 || !compare_locs (a->expr, b->expr))
23635 break;
23636 return a == NULL && b == NULL;
23639 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23642 /* Recursively optimize location lists referenced from DIE
23643 children and share them whenever possible. */
23645 static void
23646 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23648 dw_die_ref c;
23649 dw_attr_ref a;
23650 unsigned ix;
23651 dw_loc_list_struct **slot;
23653 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23654 if (AT_class (a) == dw_val_class_loc_list)
23656 dw_loc_list_ref list = AT_loc_list (a);
23657 /* TODO: perform some optimizations here, before hashing
23658 it and storing into the hash table. */
23659 hash_loc_list (list);
23660 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23661 if (*slot == NULL)
23662 *slot = list;
23663 else
23664 a->dw_attr_val.v.val_loc_list = *slot;
23667 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23671 /* Recursively assign each location list a unique index into the debug_addr
23672 section. */
23674 static void
23675 index_location_lists (dw_die_ref die)
23677 dw_die_ref c;
23678 dw_attr_ref a;
23679 unsigned ix;
23681 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23682 if (AT_class (a) == dw_val_class_loc_list)
23684 dw_loc_list_ref list = AT_loc_list (a);
23685 dw_loc_list_ref curr;
23686 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23688 /* Don't index an entry that has already been indexed
23689 or won't be output. */
23690 if (curr->begin_entry != NULL
23691 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23692 continue;
23694 curr->begin_entry
23695 = add_addr_table_entry (xstrdup (curr->begin),
23696 ate_kind_label);
23700 FOR_EACH_CHILD (die, c, index_location_lists (c));
23703 /* Optimize location lists referenced from DIE
23704 children and share them whenever possible. */
23706 static void
23707 optimize_location_lists (dw_die_ref die)
23709 loc_list_hash_type htab;
23710 htab.create (500);
23711 optimize_location_lists_1 (die, htab);
23712 htab.dispose ();
23715 /* Output stuff that dwarf requires at the end of every file,
23716 and generate the DWARF-2 debugging info. */
23718 static void
23719 dwarf2out_finish (const char *filename)
23721 limbo_die_node *node, *next_node;
23722 comdat_type_node *ctnode;
23723 hash_table <comdat_type_hasher> comdat_type_table;
23724 unsigned int i;
23725 dw_die_ref main_comp_unit_die;
23727 /* PCH might result in DW_AT_producer string being restored from the
23728 header compilation, so always fill it with empty string initially
23729 and overwrite only here. */
23730 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23731 producer_string = gen_producer_string ();
23732 producer->dw_attr_val.v.val_str->refcount--;
23733 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23735 gen_scheduled_generic_parms_dies ();
23736 gen_remaining_tmpl_value_param_die_attribute ();
23738 /* Add the name for the main input file now. We delayed this from
23739 dwarf2out_init to avoid complications with PCH. */
23740 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23741 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23742 add_comp_dir_attribute (comp_unit_die ());
23743 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23745 bool p = false;
23746 htab_traverse (file_table, file_table_relative_p, &p);
23747 if (p)
23748 add_comp_dir_attribute (comp_unit_die ());
23751 if (deferred_locations_list)
23752 for (i = 0; i < deferred_locations_list->length (); i++)
23754 add_location_or_const_value_attribute (
23755 (*deferred_locations_list)[i].die,
23756 (*deferred_locations_list)[i].variable,
23757 false,
23758 DW_AT_location);
23761 /* Traverse the limbo die list, and add parent/child links. The only
23762 dies without parents that should be here are concrete instances of
23763 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23764 For concrete instances, we can get the parent die from the abstract
23765 instance. */
23766 for (node = limbo_die_list; node; node = next_node)
23768 dw_die_ref die = node->die;
23769 next_node = node->next;
23771 if (die->die_parent == NULL)
23773 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23775 if (origin && origin->die_parent)
23776 add_child_die (origin->die_parent, die);
23777 else if (is_cu_die (die))
23779 else if (seen_error ())
23780 /* It's OK to be confused by errors in the input. */
23781 add_child_die (comp_unit_die (), die);
23782 else
23784 /* In certain situations, the lexical block containing a
23785 nested function can be optimized away, which results
23786 in the nested function die being orphaned. Likewise
23787 with the return type of that nested function. Force
23788 this to be a child of the containing function.
23790 It may happen that even the containing function got fully
23791 inlined and optimized out. In that case we are lost and
23792 assign the empty child. This should not be big issue as
23793 the function is likely unreachable too. */
23794 gcc_assert (node->created_for);
23796 if (DECL_P (node->created_for))
23797 origin = get_context_die (DECL_CONTEXT (node->created_for));
23798 else if (TYPE_P (node->created_for))
23799 origin = scope_die_for (node->created_for, comp_unit_die ());
23800 else
23801 origin = comp_unit_die ();
23803 add_child_die (origin, die);
23808 limbo_die_list = NULL;
23810 #if ENABLE_ASSERT_CHECKING
23812 dw_die_ref die = comp_unit_die (), c;
23813 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23815 #endif
23816 resolve_addr (comp_unit_die ());
23817 move_marked_base_types ();
23819 for (node = deferred_asm_name; node; node = node->next)
23821 tree decl = node->created_for;
23822 /* When generating LTO bytecode we can not generate new assembler
23823 names at this point and all important decls got theirs via
23824 free-lang-data. */
23825 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23826 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23828 add_linkage_attr (node->die, decl);
23829 move_linkage_attr (node->die);
23833 deferred_asm_name = NULL;
23835 /* Walk through the list of incomplete types again, trying once more to
23836 emit full debugging info for them. */
23837 retry_incomplete_types ();
23839 if (flag_eliminate_unused_debug_types)
23840 prune_unused_types ();
23842 /* Generate separate COMDAT sections for type DIEs. */
23843 if (use_debug_types)
23845 break_out_comdat_types (comp_unit_die ());
23847 /* Each new type_unit DIE was added to the limbo die list when created.
23848 Since these have all been added to comdat_type_list, clear the
23849 limbo die list. */
23850 limbo_die_list = NULL;
23852 /* For each new comdat type unit, copy declarations for incomplete
23853 types to make the new unit self-contained (i.e., no direct
23854 references to the main compile unit). */
23855 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23856 copy_decls_for_unworthy_types (ctnode->root_die);
23857 copy_decls_for_unworthy_types (comp_unit_die ());
23859 /* In the process of copying declarations from one unit to another,
23860 we may have left some declarations behind that are no longer
23861 referenced. Prune them. */
23862 prune_unused_types ();
23865 /* Generate separate CUs for each of the include files we've seen.
23866 They will go into limbo_die_list. */
23867 if (flag_eliminate_dwarf2_dups)
23868 break_out_includes (comp_unit_die ());
23870 /* Traverse the DIE's and add add sibling attributes to those DIE's
23871 that have children. */
23872 add_sibling_attributes (comp_unit_die ());
23873 for (node = limbo_die_list; node; node = node->next)
23874 add_sibling_attributes (node->die);
23875 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23876 add_sibling_attributes (ctnode->root_die);
23878 /* When splitting DWARF info, we put some attributes in the
23879 skeleton compile_unit DIE that remains in the .o, while
23880 most attributes go in the DWO compile_unit_die. */
23881 if (dwarf_split_debug_info)
23882 main_comp_unit_die = gen_compile_unit_die (NULL);
23883 else
23884 main_comp_unit_die = comp_unit_die ();
23886 /* Output a terminator label for the .text section. */
23887 switch_to_section (text_section);
23888 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23889 if (cold_text_section)
23891 switch_to_section (cold_text_section);
23892 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23895 /* We can only use the low/high_pc attributes if all of the code was
23896 in .text. */
23897 if (!have_multiple_function_sections
23898 || (dwarf_version < 3 && dwarf_strict))
23900 /* Don't add if the CU has no associated code. */
23901 if (text_section_used)
23902 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23903 text_end_label, true);
23905 else
23907 unsigned fde_idx;
23908 dw_fde_ref fde;
23909 bool range_list_added = false;
23911 if (text_section_used)
23912 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23913 text_end_label, &range_list_added, true);
23914 if (cold_text_section_used)
23915 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23916 cold_end_label, &range_list_added, true);
23918 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23920 if (DECL_IGNORED_P (fde->decl))
23921 continue;
23922 if (!fde->in_std_section)
23923 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23924 fde->dw_fde_end, &range_list_added,
23925 true);
23926 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23927 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23928 fde->dw_fde_second_end, &range_list_added,
23929 true);
23932 if (range_list_added)
23934 /* We need to give .debug_loc and .debug_ranges an appropriate
23935 "base address". Use zero so that these addresses become
23936 absolute. Historically, we've emitted the unexpected
23937 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23938 Emit both to give time for other tools to adapt. */
23939 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23940 if (! dwarf_strict && dwarf_version < 4)
23941 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23943 add_ranges (NULL);
23947 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23948 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23949 debug_line_section_label);
23951 if (have_macinfo)
23952 add_AT_macptr (comp_unit_die (),
23953 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23954 macinfo_section_label);
23956 if (dwarf_split_debug_info && addr_index_table != NULL)
23958 /* optimize_location_lists calculates the size of the lists,
23959 so index them first, and assign indices to the entries.
23960 Although optimize_location_lists will remove entries from
23961 the table, it only does so for duplicates, and therefore
23962 only reduces ref_counts to 1. */
23963 unsigned int index = 0;
23964 index_location_lists (comp_unit_die ());
23965 htab_traverse_noresize (addr_index_table,
23966 index_addr_table_entry, &index);
23968 if (have_location_lists)
23969 optimize_location_lists (comp_unit_die ());
23971 save_macinfo_strings ();
23973 if (dwarf_split_debug_info)
23975 unsigned int index = 0;
23977 /* Add attributes common to skeleton compile_units and
23978 type_units. Because these attributes include strings, it
23979 must be done before freezing the string table. Top-level
23980 skeleton die attrs are added when the skeleton type unit is
23981 created, so ensure it is created by this point. */
23982 add_top_level_skeleton_die_attrs (main_comp_unit_die);
23983 (void) get_skeleton_type_unit ();
23984 htab_traverse_noresize (debug_str_hash, index_string, &index);
23987 /* Output all of the compilation units. We put the main one last so that
23988 the offsets are available to output_pubnames. */
23989 for (node = limbo_die_list; node; node = node->next)
23990 output_comp_unit (node->die, 0);
23992 comdat_type_table.create (100);
23993 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23995 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
23997 /* Don't output duplicate types. */
23998 if (*slot != HTAB_EMPTY_ENTRY)
23999 continue;
24001 /* Add a pointer to the line table for the main compilation unit
24002 so that the debugger can make sense of DW_AT_decl_file
24003 attributes. */
24004 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24005 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24006 (!dwarf_split_debug_info
24007 ? debug_line_section_label
24008 : debug_skeleton_line_section_label));
24010 output_comdat_type_unit (ctnode);
24011 *slot = ctnode;
24013 comdat_type_table.dispose ();
24015 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24016 both the main_cu and all skeleton TUs. Making this call unconditional
24017 would end up either adding a second copy of the AT_pubnames attribute, or
24018 requiring a special case in add_top_level_skeleton_die_attrs. */
24019 if (!dwarf_split_debug_info)
24020 add_AT_pubnames (comp_unit_die ());
24022 if (dwarf_split_debug_info)
24024 int mark;
24025 unsigned char checksum[16];
24026 struct md5_ctx ctx;
24028 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24029 md5_init_ctx (&ctx);
24030 mark = 0;
24031 die_checksum (comp_unit_die (), &ctx, &mark);
24032 unmark_all_dies (comp_unit_die ());
24033 md5_finish_ctx (&ctx, checksum);
24035 /* Use the first 8 bytes of the checksum as the dwo_id,
24036 and add it to both comp-unit DIEs. */
24037 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24038 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24040 /* Add the base offset of the ranges table to the skeleton
24041 comp-unit DIE. */
24042 if (ranges_table_in_use)
24043 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24044 ranges_section_label);
24046 switch_to_section (debug_addr_section);
24047 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24048 output_addr_table ();
24051 /* Output the main compilation unit if non-empty or if .debug_macinfo
24052 or .debug_macro will be emitted. */
24053 output_comp_unit (comp_unit_die (), have_macinfo);
24055 if (dwarf_split_debug_info && info_section_emitted)
24056 output_skeleton_debug_sections (main_comp_unit_die);
24058 /* Output the abbreviation table. */
24059 if (abbrev_die_table_in_use != 1)
24061 switch_to_section (debug_abbrev_section);
24062 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24063 output_abbrev_section ();
24066 /* Output location list section if necessary. */
24067 if (have_location_lists)
24069 /* Output the location lists info. */
24070 switch_to_section (debug_loc_section);
24071 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24072 output_location_lists (comp_unit_die ());
24075 /* Output public names and types tables if necessary. */
24076 output_pubnames (pubname_table);
24077 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
24078 It shouldn't hurt to emit it always, since pure DWARF2 consumers
24079 simply won't look for the section. */
24080 output_pubnames (pubtype_table);
24082 /* Output the address range information if a CU (.debug_info section)
24083 was emitted. We output an empty table even if we had no functions
24084 to put in it. This because the consumer has no way to tell the
24085 difference between an empty table that we omitted and failure to
24086 generate a table that would have contained data. */
24087 if (info_section_emitted)
24089 unsigned long aranges_length = size_of_aranges ();
24091 switch_to_section (debug_aranges_section);
24092 output_aranges (aranges_length);
24095 /* Output ranges section if necessary. */
24096 if (ranges_table_in_use)
24098 switch_to_section (debug_ranges_section);
24099 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24100 output_ranges ();
24103 /* Have to end the macro section. */
24104 if (have_macinfo)
24106 switch_to_section (debug_macinfo_section);
24107 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24108 output_macinfo ();
24109 dw2_asm_output_data (1, 0, "End compilation unit");
24112 /* Output the source line correspondence table. We must do this
24113 even if there is no line information. Otherwise, on an empty
24114 translation unit, we will generate a present, but empty,
24115 .debug_info section. IRIX 6.5 `nm' will then complain when
24116 examining the file. This is done late so that any filenames
24117 used by the debug_info section are marked as 'used'. */
24118 switch_to_section (debug_line_section);
24119 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24120 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24121 output_line_info (false);
24123 if (dwarf_split_debug_info && info_section_emitted)
24125 switch_to_section (debug_skeleton_line_section);
24126 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24127 output_line_info (true);
24130 /* If we emitted any indirect strings, output the string table too. */
24131 if (debug_str_hash || skeleton_debug_str_hash)
24132 output_indirect_strings ();
24135 #include "gt-dwarf2out.h"