* fi.po: Update.
[official-gcc.git] / gcc / dwarf2out.c
blobc3ae926ee7a5b8f223357e87461860443edaf968
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "tree.h"
63 #include "version.h"
64 #include "flags.h"
65 #include "rtl.h"
66 #include "hard-reg-set.h"
67 #include "regs.h"
68 #include "insn-config.h"
69 #include "reload.h"
70 #include "function.h"
71 #include "output.h"
72 #include "expr.h"
73 #include "except.h"
74 #include "dwarf2.h"
75 #include "dwarf2out.h"
76 #include "dwarf2asm.h"
77 #include "toplev.h"
78 #include "ggc.h"
79 #include "md5.h"
80 #include "tm_p.h"
81 #include "diagnostic.h"
82 #include "tree-pretty-print.h"
83 #include "debug.h"
84 #include "target.h"
85 #include "common/common-target.h"
86 #include "langhooks.h"
87 #include "hash-table.h"
88 #include "cgraph.h"
89 #include "input.h"
90 #include "gimple.h"
91 #include "ira.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 /* The size of the target's pointer type. */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
129 /* Array of RTXes referenced by the debugging information, which therefore
130 must be kept around forever. */
131 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
133 /* A pointer to the base of a list of incomplete types which might be
134 completed at some later time. incomplete_types_list needs to be a
135 vec<tree, va_gc> *because we want to tell the garbage collector about
136 it. */
137 static GTY(()) vec<tree, va_gc> *incomplete_types;
139 /* A pointer to the base of a table of references to declaration
140 scopes. This table is a display which tracks the nesting
141 of declaration scopes at the current scope and containing
142 scopes. This table is used to find the proper place to
143 define type declaration DIE's. */
144 static GTY(()) vec<tree, va_gc> *decl_scope_table;
146 /* Pointers to various DWARF2 sections. */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_skeleton_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_skeleton_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_addr_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_skeleton_line_section;
156 static GTY(()) section *debug_loc_section;
157 static GTY(()) section *debug_pubnames_section;
158 static GTY(()) section *debug_pubtypes_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_str_dwo_section;
161 static GTY(()) section *debug_str_offsets_section;
162 static GTY(()) section *debug_ranges_section;
163 static GTY(()) section *debug_frame_section;
165 /* Maximum size (in bytes) of an artificially generated label. */
166 #define MAX_ARTIFICIAL_LABEL_BYTES 30
168 /* According to the (draft) DWARF 3 specification, the initial length
169 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
170 bytes are 0xffffffff, followed by the length stored in the next 8
171 bytes.
173 However, the SGI/MIPS ABI uses an initial length which is equal to
174 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
176 #ifndef DWARF_INITIAL_LENGTH_SIZE
177 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
178 #endif
180 /* Round SIZE up to the nearest BOUNDARY. */
181 #define DWARF_ROUND(SIZE,BOUNDARY) \
182 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
184 /* CIE identifier. */
185 #if HOST_BITS_PER_WIDE_INT >= 64
186 #define DWARF_CIE_ID \
187 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
188 #else
189 #define DWARF_CIE_ID DW_CIE_ID
190 #endif
193 /* A vector for a table that contains frame description
194 information for each routine. */
195 #define NOT_INDEXED (-1U)
196 #define NO_INDEX_ASSIGNED (-2U)
198 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
200 struct GTY(()) indirect_string_node {
201 const char *str;
202 unsigned int refcount;
203 enum dwarf_form form;
204 char *label;
205 unsigned int index;
208 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
210 /* With split_debug_info, both the comp_dir and dwo_name go in the
211 main object file, rather than the dwo, similar to the force_direct
212 parameter elsewhere but with additional complications:
214 1) The string is needed in both the main object file and the dwo.
215 That is, the comp_dir and dwo_name will appear in both places.
217 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
218 DW_FORM_GNU_str_index.
220 3) GCC chooses the form to use late, depending on the size and
221 reference count.
223 Rather than forcing the all debug string handling functions and
224 callers to deal with these complications, simply use a separate,
225 special-cased string table for any attribute that should go in the
226 main object file. This limits the complexity to just the places
227 that need it. */
229 static GTY ((param_is (struct indirect_string_node)))
230 htab_t skeleton_debug_str_hash;
232 static GTY(()) int dw2_string_counter;
234 /* True if the compilation unit places functions in more than one section. */
235 static GTY(()) bool have_multiple_function_sections = false;
237 /* Whether the default text and cold text sections have been used at all. */
239 static GTY(()) bool text_section_used = false;
240 static GTY(()) bool cold_text_section_used = false;
242 /* The default cold text section. */
243 static GTY(()) section *cold_text_section;
245 /* Forward declarations for functions defined in this file. */
247 static char *stripattributes (const char *);
248 static void output_call_frame_info (int);
249 static void dwarf2out_note_section_used (void);
251 /* Personality decl of current unit. Used only when assembler does not support
252 personality CFI. */
253 static GTY(()) rtx current_unit_personality;
255 /* Data and reference forms for relocatable data. */
256 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
257 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
259 #ifndef DEBUG_FRAME_SECTION
260 #define DEBUG_FRAME_SECTION ".debug_frame"
261 #endif
263 #ifndef FUNC_BEGIN_LABEL
264 #define FUNC_BEGIN_LABEL "LFB"
265 #endif
267 #ifndef FUNC_END_LABEL
268 #define FUNC_END_LABEL "LFE"
269 #endif
271 #ifndef PROLOGUE_END_LABEL
272 #define PROLOGUE_END_LABEL "LPE"
273 #endif
275 #ifndef EPILOGUE_BEGIN_LABEL
276 #define EPILOGUE_BEGIN_LABEL "LEB"
277 #endif
279 #ifndef FRAME_BEGIN_LABEL
280 #define FRAME_BEGIN_LABEL "Lframe"
281 #endif
282 #define CIE_AFTER_SIZE_LABEL "LSCIE"
283 #define CIE_END_LABEL "LECIE"
284 #define FDE_LABEL "LSFDE"
285 #define FDE_AFTER_SIZE_LABEL "LASFDE"
286 #define FDE_END_LABEL "LEFDE"
287 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
288 #define LINE_NUMBER_END_LABEL "LELT"
289 #define LN_PROLOG_AS_LABEL "LASLTP"
290 #define LN_PROLOG_END_LABEL "LELTP"
291 #define DIE_LABEL_PREFIX "DW"
293 /* Match the base name of a file to the base name of a compilation unit. */
295 static int
296 matches_main_base (const char *path)
298 /* Cache the last query. */
299 static const char *last_path = NULL;
300 static int last_match = 0;
301 if (path != last_path)
303 const char *base;
304 int length = base_of_path (path, &base);
305 last_path = path;
306 last_match = (length == main_input_baselength
307 && memcmp (base, main_input_basename, length) == 0);
309 return last_match;
312 #ifdef DEBUG_DEBUG_STRUCT
314 static int
315 dump_struct_debug (tree type, enum debug_info_usage usage,
316 enum debug_struct_file criterion, int generic,
317 int matches, int result)
319 /* Find the type name. */
320 tree type_decl = TYPE_STUB_DECL (type);
321 tree t = type_decl;
322 const char *name = 0;
323 if (TREE_CODE (t) == TYPE_DECL)
324 t = DECL_NAME (t);
325 if (t)
326 name = IDENTIFIER_POINTER (t);
328 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
329 criterion,
330 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
331 matches ? "bas" : "hdr",
332 generic ? "gen" : "ord",
333 usage == DINFO_USAGE_DFN ? ";" :
334 usage == DINFO_USAGE_DIR_USE ? "." : "*",
335 result,
336 (void*) type_decl, name);
337 return result;
339 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
340 dump_struct_debug (type, usage, criterion, generic, matches, result)
342 #else
344 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
345 (result)
347 #endif
349 static bool
350 should_emit_struct_debug (tree type, enum debug_info_usage usage)
352 enum debug_struct_file criterion;
353 tree type_decl;
354 bool generic = lang_hooks.types.generic_p (type);
356 if (generic)
357 criterion = debug_struct_generic[usage];
358 else
359 criterion = debug_struct_ordinary[usage];
361 if (criterion == DINFO_STRUCT_FILE_NONE)
362 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
363 if (criterion == DINFO_STRUCT_FILE_ANY)
364 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
366 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
368 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
369 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
371 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
372 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
373 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
376 /* Return a pointer to a copy of the section string name S with all
377 attributes stripped off, and an asterisk prepended (for assemble_name). */
379 static inline char *
380 stripattributes (const char *s)
382 char *stripped = XNEWVEC (char, strlen (s) + 2);
383 char *p = stripped;
385 *p++ = '*';
387 while (*s && *s != ',')
388 *p++ = *s++;
390 *p = '\0';
391 return stripped;
394 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
395 switch to the data section instead, and write out a synthetic start label
396 for collect2 the first time around. */
398 static void
399 switch_to_eh_frame_section (bool back)
401 tree label;
403 #ifdef EH_FRAME_SECTION_NAME
404 if (eh_frame_section == 0)
406 int flags;
408 if (EH_TABLES_CAN_BE_READ_ONLY)
410 int fde_encoding;
411 int per_encoding;
412 int lsda_encoding;
414 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
415 /*global=*/0);
416 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
417 /*global=*/1);
418 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
419 /*global=*/0);
420 flags = ((! flag_pic
421 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
422 && (fde_encoding & 0x70) != DW_EH_PE_aligned
423 && (per_encoding & 0x70) != DW_EH_PE_absptr
424 && (per_encoding & 0x70) != DW_EH_PE_aligned
425 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
426 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
427 ? 0 : SECTION_WRITE);
429 else
430 flags = SECTION_WRITE;
431 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
433 #endif /* EH_FRAME_SECTION_NAME */
435 if (eh_frame_section)
436 switch_to_section (eh_frame_section);
437 else
439 /* We have no special eh_frame section. Put the information in
440 the data section and emit special labels to guide collect2. */
441 switch_to_section (data_section);
443 if (!back)
445 label = get_file_function_name ("F");
446 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
447 targetm.asm_out.globalize_label (asm_out_file,
448 IDENTIFIER_POINTER (label));
449 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
454 /* Switch [BACK] to the eh or debug frame table section, depending on
455 FOR_EH. */
457 static void
458 switch_to_frame_table_section (int for_eh, bool back)
460 if (for_eh)
461 switch_to_eh_frame_section (back);
462 else
464 if (!debug_frame_section)
465 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
466 SECTION_DEBUG, NULL);
467 switch_to_section (debug_frame_section);
471 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
473 enum dw_cfi_oprnd_type
474 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
476 switch (cfi)
478 case DW_CFA_nop:
479 case DW_CFA_GNU_window_save:
480 case DW_CFA_remember_state:
481 case DW_CFA_restore_state:
482 return dw_cfi_oprnd_unused;
484 case DW_CFA_set_loc:
485 case DW_CFA_advance_loc1:
486 case DW_CFA_advance_loc2:
487 case DW_CFA_advance_loc4:
488 case DW_CFA_MIPS_advance_loc8:
489 return dw_cfi_oprnd_addr;
491 case DW_CFA_offset:
492 case DW_CFA_offset_extended:
493 case DW_CFA_def_cfa:
494 case DW_CFA_offset_extended_sf:
495 case DW_CFA_def_cfa_sf:
496 case DW_CFA_restore:
497 case DW_CFA_restore_extended:
498 case DW_CFA_undefined:
499 case DW_CFA_same_value:
500 case DW_CFA_def_cfa_register:
501 case DW_CFA_register:
502 case DW_CFA_expression:
503 return dw_cfi_oprnd_reg_num;
505 case DW_CFA_def_cfa_offset:
506 case DW_CFA_GNU_args_size:
507 case DW_CFA_def_cfa_offset_sf:
508 return dw_cfi_oprnd_offset;
510 case DW_CFA_def_cfa_expression:
511 return dw_cfi_oprnd_loc;
513 default:
514 gcc_unreachable ();
518 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
520 enum dw_cfi_oprnd_type
521 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
523 switch (cfi)
525 case DW_CFA_def_cfa:
526 case DW_CFA_def_cfa_sf:
527 case DW_CFA_offset:
528 case DW_CFA_offset_extended_sf:
529 case DW_CFA_offset_extended:
530 return dw_cfi_oprnd_offset;
532 case DW_CFA_register:
533 return dw_cfi_oprnd_reg_num;
535 case DW_CFA_expression:
536 return dw_cfi_oprnd_loc;
538 default:
539 return dw_cfi_oprnd_unused;
543 /* Output one FDE. */
545 static void
546 output_fde (dw_fde_ref fde, bool for_eh, bool second,
547 char *section_start_label, int fde_encoding, char *augmentation,
548 bool any_lsda_needed, int lsda_encoding)
550 const char *begin, *end;
551 static unsigned int j;
552 char l1[20], l2[20];
554 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
555 /* empty */ 0);
556 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
557 for_eh + j);
558 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
559 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
560 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
561 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
562 " indicating 64-bit DWARF extension");
563 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
564 "FDE Length");
565 ASM_OUTPUT_LABEL (asm_out_file, l1);
567 if (for_eh)
568 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
569 else
570 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
571 debug_frame_section, "FDE CIE offset");
573 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
574 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
576 if (for_eh)
578 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
579 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
580 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
581 "FDE initial location");
582 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
583 end, begin, "FDE address range");
585 else
587 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
588 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
591 if (augmentation[0])
593 if (any_lsda_needed)
595 int size = size_of_encoded_value (lsda_encoding);
597 if (lsda_encoding == DW_EH_PE_aligned)
599 int offset = ( 4 /* Length */
600 + 4 /* CIE offset */
601 + 2 * size_of_encoded_value (fde_encoding)
602 + 1 /* Augmentation size */ );
603 int pad = -offset & (PTR_SIZE - 1);
605 size += pad;
606 gcc_assert (size_of_uleb128 (size) == 1);
609 dw2_asm_output_data_uleb128 (size, "Augmentation size");
611 if (fde->uses_eh_lsda)
613 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
614 fde->funcdef_number);
615 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
616 gen_rtx_SYMBOL_REF (Pmode, l1),
617 false,
618 "Language Specific Data Area");
620 else
622 if (lsda_encoding == DW_EH_PE_aligned)
623 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
624 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
625 "Language Specific Data Area (none)");
628 else
629 dw2_asm_output_data_uleb128 (0, "Augmentation size");
632 /* Loop through the Call Frame Instructions associated with this FDE. */
633 fde->dw_fde_current_label = begin;
635 size_t from, until, i;
637 from = 0;
638 until = vec_safe_length (fde->dw_fde_cfi);
640 if (fde->dw_fde_second_begin == NULL)
642 else if (!second)
643 until = fde->dw_fde_switch_cfi_index;
644 else
645 from = fde->dw_fde_switch_cfi_index;
647 for (i = from; i < until; i++)
648 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
651 /* If we are to emit a ref/link from function bodies to their frame tables,
652 do it now. This is typically performed to make sure that tables
653 associated with functions are dragged with them and not discarded in
654 garbage collecting links. We need to do this on a per function basis to
655 cope with -ffunction-sections. */
657 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
658 /* Switch to the function section, emit the ref to the tables, and
659 switch *back* into the table section. */
660 switch_to_section (function_section (fde->decl));
661 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
662 switch_to_frame_table_section (for_eh, true);
663 #endif
665 /* Pad the FDE out to an address sized boundary. */
666 ASM_OUTPUT_ALIGN (asm_out_file,
667 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
668 ASM_OUTPUT_LABEL (asm_out_file, l2);
670 j += 2;
673 /* Return true if frame description entry FDE is needed for EH. */
675 static bool
676 fde_needed_for_eh_p (dw_fde_ref fde)
678 if (flag_asynchronous_unwind_tables)
679 return true;
681 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
682 return true;
684 if (fde->uses_eh_lsda)
685 return true;
687 /* If exceptions are enabled, we have collected nothrow info. */
688 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
689 return false;
691 return true;
694 /* Output the call frame information used to record information
695 that relates to calculating the frame pointer, and records the
696 location of saved registers. */
698 static void
699 output_call_frame_info (int for_eh)
701 unsigned int i;
702 dw_fde_ref fde;
703 dw_cfi_ref cfi;
704 char l1[20], l2[20], section_start_label[20];
705 bool any_lsda_needed = false;
706 char augmentation[6];
707 int augmentation_size;
708 int fde_encoding = DW_EH_PE_absptr;
709 int per_encoding = DW_EH_PE_absptr;
710 int lsda_encoding = DW_EH_PE_absptr;
711 int return_reg;
712 rtx personality = NULL;
713 int dw_cie_version;
715 /* Don't emit a CIE if there won't be any FDEs. */
716 if (!fde_vec)
717 return;
719 /* Nothing to do if the assembler's doing it all. */
720 if (dwarf2out_do_cfi_asm ())
721 return;
723 /* If we don't have any functions we'll want to unwind out of, don't emit
724 any EH unwind information. If we make FDEs linkonce, we may have to
725 emit an empty label for an FDE that wouldn't otherwise be emitted. We
726 want to avoid having an FDE kept around when the function it refers to
727 is discarded. Example where this matters: a primary function template
728 in C++ requires EH information, an explicit specialization doesn't. */
729 if (for_eh)
731 bool any_eh_needed = false;
733 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
735 if (fde->uses_eh_lsda)
736 any_eh_needed = any_lsda_needed = true;
737 else if (fde_needed_for_eh_p (fde))
738 any_eh_needed = true;
739 else if (TARGET_USES_WEAK_UNWIND_INFO)
740 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
743 if (!any_eh_needed)
744 return;
747 /* We're going to be generating comments, so turn on app. */
748 if (flag_debug_asm)
749 app_enable ();
751 /* Switch to the proper frame section, first time. */
752 switch_to_frame_table_section (for_eh, false);
754 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
755 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
757 /* Output the CIE. */
758 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
759 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
760 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
761 dw2_asm_output_data (4, 0xffffffff,
762 "Initial length escape value indicating 64-bit DWARF extension");
763 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
764 "Length of Common Information Entry");
765 ASM_OUTPUT_LABEL (asm_out_file, l1);
767 /* Now that the CIE pointer is PC-relative for EH,
768 use 0 to identify the CIE. */
769 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
770 (for_eh ? 0 : DWARF_CIE_ID),
771 "CIE Identifier Tag");
773 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
774 use CIE version 1, unless that would produce incorrect results
775 due to overflowing the return register column. */
776 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
777 dw_cie_version = 1;
778 if (return_reg >= 256 || dwarf_version > 2)
779 dw_cie_version = 3;
780 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
782 augmentation[0] = 0;
783 augmentation_size = 0;
785 personality = current_unit_personality;
786 if (for_eh)
788 char *p;
790 /* Augmentation:
791 z Indicates that a uleb128 is present to size the
792 augmentation section.
793 L Indicates the encoding (and thus presence) of
794 an LSDA pointer in the FDE augmentation.
795 R Indicates a non-default pointer encoding for
796 FDE code pointers.
797 P Indicates the presence of an encoding + language
798 personality routine in the CIE augmentation. */
800 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
801 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
802 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
804 p = augmentation + 1;
805 if (personality)
807 *p++ = 'P';
808 augmentation_size += 1 + size_of_encoded_value (per_encoding);
809 assemble_external_libcall (personality);
811 if (any_lsda_needed)
813 *p++ = 'L';
814 augmentation_size += 1;
816 if (fde_encoding != DW_EH_PE_absptr)
818 *p++ = 'R';
819 augmentation_size += 1;
821 if (p > augmentation + 1)
823 augmentation[0] = 'z';
824 *p = '\0';
827 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
828 if (personality && per_encoding == DW_EH_PE_aligned)
830 int offset = ( 4 /* Length */
831 + 4 /* CIE Id */
832 + 1 /* CIE version */
833 + strlen (augmentation) + 1 /* Augmentation */
834 + size_of_uleb128 (1) /* Code alignment */
835 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
836 + 1 /* RA column */
837 + 1 /* Augmentation size */
838 + 1 /* Personality encoding */ );
839 int pad = -offset & (PTR_SIZE - 1);
841 augmentation_size += pad;
843 /* Augmentations should be small, so there's scarce need to
844 iterate for a solution. Die if we exceed one uleb128 byte. */
845 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
849 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
850 if (dw_cie_version >= 4)
852 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
853 dw2_asm_output_data (1, 0, "CIE Segment Size");
855 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
856 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
857 "CIE Data Alignment Factor");
859 if (dw_cie_version == 1)
860 dw2_asm_output_data (1, return_reg, "CIE RA Column");
861 else
862 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
864 if (augmentation[0])
866 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
867 if (personality)
869 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
870 eh_data_format_name (per_encoding));
871 dw2_asm_output_encoded_addr_rtx (per_encoding,
872 personality,
873 true, NULL);
876 if (any_lsda_needed)
877 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
878 eh_data_format_name (lsda_encoding));
880 if (fde_encoding != DW_EH_PE_absptr)
881 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
882 eh_data_format_name (fde_encoding));
885 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
886 output_cfi (cfi, NULL, for_eh);
888 /* Pad the CIE out to an address sized boundary. */
889 ASM_OUTPUT_ALIGN (asm_out_file,
890 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
891 ASM_OUTPUT_LABEL (asm_out_file, l2);
893 /* Loop through all of the FDE's. */
894 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
896 unsigned int k;
898 /* Don't emit EH unwind info for leaf functions that don't need it. */
899 if (for_eh && !fde_needed_for_eh_p (fde))
900 continue;
902 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
903 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
904 augmentation, any_lsda_needed, lsda_encoding);
907 if (for_eh && targetm.terminate_dw2_eh_frame_info)
908 dw2_asm_output_data (4, 0, "End of Table");
910 /* Turn off app to make assembly quicker. */
911 if (flag_debug_asm)
912 app_disable ();
915 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
917 static void
918 dwarf2out_do_cfi_startproc (bool second)
920 int enc;
921 rtx ref;
922 rtx personality = get_personality_function (current_function_decl);
924 fprintf (asm_out_file, "\t.cfi_startproc\n");
926 if (personality)
928 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
929 ref = personality;
931 /* ??? The GAS support isn't entirely consistent. We have to
932 handle indirect support ourselves, but PC-relative is done
933 in the assembler. Further, the assembler can't handle any
934 of the weirder relocation types. */
935 if (enc & DW_EH_PE_indirect)
936 ref = dw2_force_const_mem (ref, true);
938 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
939 output_addr_const (asm_out_file, ref);
940 fputc ('\n', asm_out_file);
943 if (crtl->uses_eh_lsda)
945 char lab[20];
947 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
948 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
949 current_function_funcdef_no);
950 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
951 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
953 if (enc & DW_EH_PE_indirect)
954 ref = dw2_force_const_mem (ref, true);
956 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
957 output_addr_const (asm_out_file, ref);
958 fputc ('\n', asm_out_file);
962 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
963 this allocation may be done before pass_final. */
965 dw_fde_ref
966 dwarf2out_alloc_current_fde (void)
968 dw_fde_ref fde;
970 fde = ggc_alloc_cleared_dw_fde_node ();
971 fde->decl = current_function_decl;
972 fde->funcdef_number = current_function_funcdef_no;
973 fde->fde_index = vec_safe_length (fde_vec);
974 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
975 fde->uses_eh_lsda = crtl->uses_eh_lsda;
976 fde->nothrow = crtl->nothrow;
977 fde->drap_reg = INVALID_REGNUM;
978 fde->vdrap_reg = INVALID_REGNUM;
980 /* Record the FDE associated with this function. */
981 cfun->fde = fde;
982 vec_safe_push (fde_vec, fde);
984 return fde;
987 /* Output a marker (i.e. a label) for the beginning of a function, before
988 the prologue. */
990 void
991 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
992 const char *file ATTRIBUTE_UNUSED)
994 char label[MAX_ARTIFICIAL_LABEL_BYTES];
995 char * dup_label;
996 dw_fde_ref fde;
997 section *fnsec;
998 bool do_frame;
1000 current_function_func_begin_label = NULL;
1002 do_frame = dwarf2out_do_frame ();
1004 /* ??? current_function_func_begin_label is also used by except.c for
1005 call-site information. We must emit this label if it might be used. */
1006 if (!do_frame
1007 && (!flag_exceptions
1008 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1009 return;
1011 fnsec = function_section (current_function_decl);
1012 switch_to_section (fnsec);
1013 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1014 current_function_funcdef_no);
1015 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1016 current_function_funcdef_no);
1017 dup_label = xstrdup (label);
1018 current_function_func_begin_label = dup_label;
1020 /* We can elide the fde allocation if we're not emitting debug info. */
1021 if (!do_frame)
1022 return;
1024 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1025 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1026 would include pass_dwarf2_frame. If we've not created the FDE yet,
1027 do so now. */
1028 fde = cfun->fde;
1029 if (fde == NULL)
1030 fde = dwarf2out_alloc_current_fde ();
1032 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1033 fde->dw_fde_begin = dup_label;
1034 fde->dw_fde_current_label = dup_label;
1035 fde->in_std_section = (fnsec == text_section
1036 || (cold_text_section && fnsec == cold_text_section));
1038 /* We only want to output line number information for the genuine dwarf2
1039 prologue case, not the eh frame case. */
1040 #ifdef DWARF2_DEBUGGING_INFO
1041 if (file)
1042 dwarf2out_source_line (line, file, 0, true);
1043 #endif
1045 if (dwarf2out_do_cfi_asm ())
1046 dwarf2out_do_cfi_startproc (false);
1047 else
1049 rtx personality = get_personality_function (current_function_decl);
1050 if (!current_unit_personality)
1051 current_unit_personality = personality;
1053 /* We cannot keep a current personality per function as without CFI
1054 asm, at the point where we emit the CFI data, there is no current
1055 function anymore. */
1056 if (personality && current_unit_personality != personality)
1057 sorry ("multiple EH personalities are supported only with assemblers "
1058 "supporting .cfi_personality directive");
1062 /* Output a marker (i.e. a label) for the end of the generated code
1063 for a function prologue. This gets called *after* the prologue code has
1064 been generated. */
1066 void
1067 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1068 const char *file ATTRIBUTE_UNUSED)
1070 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1072 /* Output a label to mark the endpoint of the code generated for this
1073 function. */
1074 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1075 current_function_funcdef_no);
1076 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1077 current_function_funcdef_no);
1078 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1081 /* Output a marker (i.e. a label) for the beginning of the generated code
1082 for a function epilogue. This gets called *before* the prologue code has
1083 been generated. */
1085 void
1086 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087 const char *file ATTRIBUTE_UNUSED)
1089 dw_fde_ref fde = cfun->fde;
1090 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092 if (fde->dw_fde_vms_begin_epilogue)
1093 return;
1095 /* Output a label to mark the endpoint of the code generated for this
1096 function. */
1097 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1098 current_function_funcdef_no);
1099 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1100 current_function_funcdef_no);
1101 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1104 /* Output a marker (i.e. a label) for the absolute end of the generated code
1105 for a function definition. This gets called *after* the epilogue code has
1106 been generated. */
1108 void
1109 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 dw_fde_ref fde;
1113 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115 last_var_location_insn = NULL_RTX;
1116 cached_next_real_insn = NULL_RTX;
1118 if (dwarf2out_do_cfi_asm ())
1119 fprintf (asm_out_file, "\t.cfi_endproc\n");
1121 /* Output a label to mark the endpoint of the code generated for this
1122 function. */
1123 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1124 current_function_funcdef_no);
1125 ASM_OUTPUT_LABEL (asm_out_file, label);
1126 fde = cfun->fde;
1127 gcc_assert (fde != NULL);
1128 if (fde->dw_fde_second_begin == NULL)
1129 fde->dw_fde_end = xstrdup (label);
1132 void
1133 dwarf2out_frame_finish (void)
1135 /* Output call frame information. */
1136 if (targetm.debug_unwind_info () == UI_DWARF2)
1137 output_call_frame_info (0);
1139 /* Output another copy for the unwinder. */
1140 if ((flag_unwind_tables || flag_exceptions)
1141 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1142 output_call_frame_info (1);
1145 /* Note that the current function section is being used for code. */
1147 static void
1148 dwarf2out_note_section_used (void)
1150 section *sec = current_function_section ();
1151 if (sec == text_section)
1152 text_section_used = true;
1153 else if (sec == cold_text_section)
1154 cold_text_section_used = true;
1157 static void var_location_switch_text_section (void);
1158 static void set_cur_line_info_table (section *);
1160 void
1161 dwarf2out_switch_text_section (void)
1163 section *sect;
1164 dw_fde_ref fde = cfun->fde;
1166 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1168 if (!in_cold_section_p)
1170 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1171 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1172 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1174 else
1176 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1177 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1178 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1180 have_multiple_function_sections = true;
1182 /* There is no need to mark used sections when not debugging. */
1183 if (cold_text_section != NULL)
1184 dwarf2out_note_section_used ();
1186 if (dwarf2out_do_cfi_asm ())
1187 fprintf (asm_out_file, "\t.cfi_endproc\n");
1189 /* Now do the real section switch. */
1190 sect = current_function_section ();
1191 switch_to_section (sect);
1193 fde->second_in_std_section
1194 = (sect == text_section
1195 || (cold_text_section && sect == cold_text_section));
1197 if (dwarf2out_do_cfi_asm ())
1198 dwarf2out_do_cfi_startproc (true);
1200 var_location_switch_text_section ();
1202 if (cold_text_section != NULL)
1203 set_cur_line_info_table (sect);
1206 /* And now, the subset of the debugging information support code necessary
1207 for emitting location expressions. */
1209 /* Data about a single source file. */
1210 struct GTY(()) dwarf_file_data {
1211 const char * filename;
1212 int emitted_number;
1215 typedef struct GTY(()) deferred_locations_struct
1217 tree variable;
1218 dw_die_ref die;
1219 } deferred_locations;
1222 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1225 /* Describe an entry into the .debug_addr section. */
1227 enum ate_kind {
1228 ate_kind_rtx,
1229 ate_kind_rtx_dtprel,
1230 ate_kind_label
1233 typedef struct GTY(()) addr_table_entry_struct {
1234 enum ate_kind kind;
1235 unsigned int refcount;
1236 unsigned int index;
1237 union addr_table_entry_struct_union
1239 rtx GTY ((tag ("0"))) rtl;
1240 char * GTY ((tag ("1"))) label;
1242 GTY ((desc ("%1.kind"))) addr;
1244 addr_table_entry;
1246 /* Location lists are ranges + location descriptions for that range,
1247 so you can track variables that are in different places over
1248 their entire life. */
1249 typedef struct GTY(()) dw_loc_list_struct {
1250 dw_loc_list_ref dw_loc_next;
1251 const char *begin; /* Label and addr_entry for start of range */
1252 addr_table_entry *begin_entry;
1253 const char *end; /* Label for end of range */
1254 char *ll_symbol; /* Label for beginning of location list.
1255 Only on head of list */
1256 const char *section; /* Section this loclist is relative to */
1257 dw_loc_descr_ref expr;
1258 hashval_t hash;
1259 /* True if all addresses in this and subsequent lists are known to be
1260 resolved. */
1261 bool resolved_addr;
1262 /* True if this list has been replaced by dw_loc_next. */
1263 bool replaced;
1264 bool emitted;
1265 /* True if the range should be emitted even if begin and end
1266 are the same. */
1267 bool force;
1268 } dw_loc_list_node;
1270 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1272 /* Convert a DWARF stack opcode into its string name. */
1274 static const char *
1275 dwarf_stack_op_name (unsigned int op)
1277 const char *name = get_DW_OP_name (op);
1279 if (name != NULL)
1280 return name;
1282 return "OP_<unknown>";
1285 /* Return a pointer to a newly allocated location description. Location
1286 descriptions are simple expression terms that can be strung
1287 together to form more complicated location (address) descriptions. */
1289 static inline dw_loc_descr_ref
1290 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1291 unsigned HOST_WIDE_INT oprnd2)
1293 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1295 descr->dw_loc_opc = op;
1296 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1297 descr->dw_loc_oprnd1.val_entry = NULL;
1298 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1299 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1300 descr->dw_loc_oprnd2.val_entry = NULL;
1301 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1303 return descr;
1306 /* Return a pointer to a newly allocated location description for
1307 REG and OFFSET. */
1309 static inline dw_loc_descr_ref
1310 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1312 if (reg <= 31)
1313 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1314 offset, 0);
1315 else
1316 return new_loc_descr (DW_OP_bregx, reg, offset);
1319 /* Add a location description term to a location description expression. */
1321 static inline void
1322 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1324 dw_loc_descr_ref *d;
1326 /* Find the end of the chain. */
1327 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1330 *d = descr;
1333 /* Compare two location operands for exact equality. */
1335 static bool
1336 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1338 if (a->val_class != b->val_class)
1339 return false;
1340 switch (a->val_class)
1342 case dw_val_class_none:
1343 return true;
1344 case dw_val_class_addr:
1345 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1347 case dw_val_class_offset:
1348 case dw_val_class_unsigned_const:
1349 case dw_val_class_const:
1350 case dw_val_class_range_list:
1351 case dw_val_class_lineptr:
1352 case dw_val_class_macptr:
1353 /* These are all HOST_WIDE_INT, signed or unsigned. */
1354 return a->v.val_unsigned == b->v.val_unsigned;
1356 case dw_val_class_loc:
1357 return a->v.val_loc == b->v.val_loc;
1358 case dw_val_class_loc_list:
1359 return a->v.val_loc_list == b->v.val_loc_list;
1360 case dw_val_class_die_ref:
1361 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1362 case dw_val_class_fde_ref:
1363 return a->v.val_fde_index == b->v.val_fde_index;
1364 case dw_val_class_lbl_id:
1365 case dw_val_class_high_pc:
1366 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1367 case dw_val_class_str:
1368 return a->v.val_str == b->v.val_str;
1369 case dw_val_class_flag:
1370 return a->v.val_flag == b->v.val_flag;
1371 case dw_val_class_file:
1372 return a->v.val_file == b->v.val_file;
1373 case dw_val_class_decl_ref:
1374 return a->v.val_decl_ref == b->v.val_decl_ref;
1376 case dw_val_class_const_double:
1377 return (a->v.val_double.high == b->v.val_double.high
1378 && a->v.val_double.low == b->v.val_double.low);
1380 case dw_val_class_vec:
1382 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1383 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1385 return (a_len == b_len
1386 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1389 case dw_val_class_data8:
1390 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1392 case dw_val_class_vms_delta:
1393 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1394 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1396 gcc_unreachable ();
1399 /* Compare two location atoms for exact equality. */
1401 static bool
1402 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1404 if (a->dw_loc_opc != b->dw_loc_opc)
1405 return false;
1407 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1408 address size, but since we always allocate cleared storage it
1409 should be zero for other types of locations. */
1410 if (a->dtprel != b->dtprel)
1411 return false;
1413 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1414 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1417 /* Compare two complete location expressions for exact equality. */
1419 bool
1420 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1422 while (1)
1424 if (a == b)
1425 return true;
1426 if (a == NULL || b == NULL)
1427 return false;
1428 if (!loc_descr_equal_p_1 (a, b))
1429 return false;
1431 a = a->dw_loc_next;
1432 b = b->dw_loc_next;
1437 /* Add a constant OFFSET to a location expression. */
1439 static void
1440 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1442 dw_loc_descr_ref loc;
1443 HOST_WIDE_INT *p;
1445 gcc_assert (*list_head != NULL);
1447 if (!offset)
1448 return;
1450 /* Find the end of the chain. */
1451 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1454 p = NULL;
1455 if (loc->dw_loc_opc == DW_OP_fbreg
1456 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1457 p = &loc->dw_loc_oprnd1.v.val_int;
1458 else if (loc->dw_loc_opc == DW_OP_bregx)
1459 p = &loc->dw_loc_oprnd2.v.val_int;
1461 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1462 offset. Don't optimize if an signed integer overflow would happen. */
1463 if (p != NULL
1464 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1465 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1466 *p += offset;
1468 else if (offset > 0)
1469 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1471 else
1473 loc->dw_loc_next = int_loc_descriptor (-offset);
1474 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1478 /* Add a constant OFFSET to a location list. */
1480 static void
1481 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1483 dw_loc_list_ref d;
1484 for (d = list_head; d != NULL; d = d->dw_loc_next)
1485 loc_descr_plus_const (&d->expr, offset);
1488 #define DWARF_REF_SIZE \
1489 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1491 static unsigned long int get_base_type_offset (dw_die_ref);
1493 /* Return the size of a location descriptor. */
1495 static unsigned long
1496 size_of_loc_descr (dw_loc_descr_ref loc)
1498 unsigned long size = 1;
1500 switch (loc->dw_loc_opc)
1502 case DW_OP_addr:
1503 size += DWARF2_ADDR_SIZE;
1504 break;
1505 case DW_OP_GNU_addr_index:
1506 case DW_OP_GNU_const_index:
1507 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1508 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1509 break;
1510 case DW_OP_const1u:
1511 case DW_OP_const1s:
1512 size += 1;
1513 break;
1514 case DW_OP_const2u:
1515 case DW_OP_const2s:
1516 size += 2;
1517 break;
1518 case DW_OP_const4u:
1519 case DW_OP_const4s:
1520 size += 4;
1521 break;
1522 case DW_OP_const8u:
1523 case DW_OP_const8s:
1524 size += 8;
1525 break;
1526 case DW_OP_constu:
1527 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1528 break;
1529 case DW_OP_consts:
1530 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1531 break;
1532 case DW_OP_pick:
1533 size += 1;
1534 break;
1535 case DW_OP_plus_uconst:
1536 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1537 break;
1538 case DW_OP_skip:
1539 case DW_OP_bra:
1540 size += 2;
1541 break;
1542 case DW_OP_breg0:
1543 case DW_OP_breg1:
1544 case DW_OP_breg2:
1545 case DW_OP_breg3:
1546 case DW_OP_breg4:
1547 case DW_OP_breg5:
1548 case DW_OP_breg6:
1549 case DW_OP_breg7:
1550 case DW_OP_breg8:
1551 case DW_OP_breg9:
1552 case DW_OP_breg10:
1553 case DW_OP_breg11:
1554 case DW_OP_breg12:
1555 case DW_OP_breg13:
1556 case DW_OP_breg14:
1557 case DW_OP_breg15:
1558 case DW_OP_breg16:
1559 case DW_OP_breg17:
1560 case DW_OP_breg18:
1561 case DW_OP_breg19:
1562 case DW_OP_breg20:
1563 case DW_OP_breg21:
1564 case DW_OP_breg22:
1565 case DW_OP_breg23:
1566 case DW_OP_breg24:
1567 case DW_OP_breg25:
1568 case DW_OP_breg26:
1569 case DW_OP_breg27:
1570 case DW_OP_breg28:
1571 case DW_OP_breg29:
1572 case DW_OP_breg30:
1573 case DW_OP_breg31:
1574 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1575 break;
1576 case DW_OP_regx:
1577 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1578 break;
1579 case DW_OP_fbreg:
1580 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1581 break;
1582 case DW_OP_bregx:
1583 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1584 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1585 break;
1586 case DW_OP_piece:
1587 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1588 break;
1589 case DW_OP_bit_piece:
1590 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1591 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1592 break;
1593 case DW_OP_deref_size:
1594 case DW_OP_xderef_size:
1595 size += 1;
1596 break;
1597 case DW_OP_call2:
1598 size += 2;
1599 break;
1600 case DW_OP_call4:
1601 size += 4;
1602 break;
1603 case DW_OP_call_ref:
1604 size += DWARF_REF_SIZE;
1605 break;
1606 case DW_OP_implicit_value:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1608 + loc->dw_loc_oprnd1.v.val_unsigned;
1609 break;
1610 case DW_OP_GNU_implicit_pointer:
1611 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1612 break;
1613 case DW_OP_GNU_entry_value:
1615 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1616 size += size_of_uleb128 (op_size) + op_size;
1617 break;
1619 case DW_OP_GNU_const_type:
1621 unsigned long o
1622 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1623 size += size_of_uleb128 (o) + 1;
1624 switch (loc->dw_loc_oprnd2.val_class)
1626 case dw_val_class_vec:
1627 size += loc->dw_loc_oprnd2.v.val_vec.length
1628 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1629 break;
1630 case dw_val_class_const:
1631 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1632 break;
1633 case dw_val_class_const_double:
1634 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1635 break;
1636 default:
1637 gcc_unreachable ();
1639 break;
1641 case DW_OP_GNU_regval_type:
1643 unsigned long o
1644 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1645 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1646 + size_of_uleb128 (o);
1648 break;
1649 case DW_OP_GNU_deref_type:
1651 unsigned long o
1652 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1653 size += 1 + size_of_uleb128 (o);
1655 break;
1656 case DW_OP_GNU_convert:
1657 case DW_OP_GNU_reinterpret:
1658 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1659 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1660 else
1662 unsigned long o
1663 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1664 size += size_of_uleb128 (o);
1666 break;
1667 case DW_OP_GNU_parameter_ref:
1668 size += 4;
1669 break;
1670 default:
1671 break;
1674 return size;
1677 /* Return the size of a series of location descriptors. */
1679 unsigned long
1680 size_of_locs (dw_loc_descr_ref loc)
1682 dw_loc_descr_ref l;
1683 unsigned long size;
1685 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1686 field, to avoid writing to a PCH file. */
1687 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1689 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1690 break;
1691 size += size_of_loc_descr (l);
1693 if (! l)
1694 return size;
1696 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1698 l->dw_loc_addr = size;
1699 size += size_of_loc_descr (l);
1702 return size;
1705 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1706 static void get_ref_die_offset_label (char *, dw_die_ref);
1707 static unsigned long int get_ref_die_offset (dw_die_ref);
1709 /* Output location description stack opcode's operands (if any).
1710 The for_eh_or_skip parameter controls whether register numbers are
1711 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1712 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1713 info). This should be suppressed for the cases that have not been converted
1714 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1716 static void
1717 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1719 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1720 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1722 switch (loc->dw_loc_opc)
1724 #ifdef DWARF2_DEBUGGING_INFO
1725 case DW_OP_const2u:
1726 case DW_OP_const2s:
1727 dw2_asm_output_data (2, val1->v.val_int, NULL);
1728 break;
1729 case DW_OP_const4u:
1730 if (loc->dtprel)
1732 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1733 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1734 val1->v.val_addr);
1735 fputc ('\n', asm_out_file);
1736 break;
1738 /* FALLTHRU */
1739 case DW_OP_const4s:
1740 dw2_asm_output_data (4, val1->v.val_int, NULL);
1741 break;
1742 case DW_OP_const8u:
1743 if (loc->dtprel)
1745 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1746 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1747 val1->v.val_addr);
1748 fputc ('\n', asm_out_file);
1749 break;
1751 /* FALLTHRU */
1752 case DW_OP_const8s:
1753 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1754 dw2_asm_output_data (8, val1->v.val_int, NULL);
1755 break;
1756 case DW_OP_skip:
1757 case DW_OP_bra:
1759 int offset;
1761 gcc_assert (val1->val_class == dw_val_class_loc);
1762 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1764 dw2_asm_output_data (2, offset, NULL);
1766 break;
1767 case DW_OP_implicit_value:
1768 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1769 switch (val2->val_class)
1771 case dw_val_class_const:
1772 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1773 break;
1774 case dw_val_class_vec:
1776 unsigned int elt_size = val2->v.val_vec.elt_size;
1777 unsigned int len = val2->v.val_vec.length;
1778 unsigned int i;
1779 unsigned char *p;
1781 if (elt_size > sizeof (HOST_WIDE_INT))
1783 elt_size /= 2;
1784 len *= 2;
1786 for (i = 0, p = val2->v.val_vec.array;
1787 i < len;
1788 i++, p += elt_size)
1789 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1790 "fp or vector constant word %u", i);
1792 break;
1793 case dw_val_class_const_double:
1795 unsigned HOST_WIDE_INT first, second;
1797 if (WORDS_BIG_ENDIAN)
1799 first = val2->v.val_double.high;
1800 second = val2->v.val_double.low;
1802 else
1804 first = val2->v.val_double.low;
1805 second = val2->v.val_double.high;
1807 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1808 first, NULL);
1809 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1810 second, NULL);
1812 break;
1813 case dw_val_class_addr:
1814 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1815 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1816 break;
1817 default:
1818 gcc_unreachable ();
1820 break;
1821 #else
1822 case DW_OP_const2u:
1823 case DW_OP_const2s:
1824 case DW_OP_const4u:
1825 case DW_OP_const4s:
1826 case DW_OP_const8u:
1827 case DW_OP_const8s:
1828 case DW_OP_skip:
1829 case DW_OP_bra:
1830 case DW_OP_implicit_value:
1831 /* We currently don't make any attempt to make sure these are
1832 aligned properly like we do for the main unwind info, so
1833 don't support emitting things larger than a byte if we're
1834 only doing unwinding. */
1835 gcc_unreachable ();
1836 #endif
1837 case DW_OP_const1u:
1838 case DW_OP_const1s:
1839 dw2_asm_output_data (1, val1->v.val_int, NULL);
1840 break;
1841 case DW_OP_constu:
1842 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1843 break;
1844 case DW_OP_consts:
1845 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1846 break;
1847 case DW_OP_pick:
1848 dw2_asm_output_data (1, val1->v.val_int, NULL);
1849 break;
1850 case DW_OP_plus_uconst:
1851 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1852 break;
1853 case DW_OP_breg0:
1854 case DW_OP_breg1:
1855 case DW_OP_breg2:
1856 case DW_OP_breg3:
1857 case DW_OP_breg4:
1858 case DW_OP_breg5:
1859 case DW_OP_breg6:
1860 case DW_OP_breg7:
1861 case DW_OP_breg8:
1862 case DW_OP_breg9:
1863 case DW_OP_breg10:
1864 case DW_OP_breg11:
1865 case DW_OP_breg12:
1866 case DW_OP_breg13:
1867 case DW_OP_breg14:
1868 case DW_OP_breg15:
1869 case DW_OP_breg16:
1870 case DW_OP_breg17:
1871 case DW_OP_breg18:
1872 case DW_OP_breg19:
1873 case DW_OP_breg20:
1874 case DW_OP_breg21:
1875 case DW_OP_breg22:
1876 case DW_OP_breg23:
1877 case DW_OP_breg24:
1878 case DW_OP_breg25:
1879 case DW_OP_breg26:
1880 case DW_OP_breg27:
1881 case DW_OP_breg28:
1882 case DW_OP_breg29:
1883 case DW_OP_breg30:
1884 case DW_OP_breg31:
1885 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1886 break;
1887 case DW_OP_regx:
1889 unsigned r = val1->v.val_unsigned;
1890 if (for_eh_or_skip >= 0)
1891 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1892 gcc_assert (size_of_uleb128 (r)
1893 == size_of_uleb128 (val1->v.val_unsigned));
1894 dw2_asm_output_data_uleb128 (r, NULL);
1896 break;
1897 case DW_OP_fbreg:
1898 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_bregx:
1902 unsigned r = val1->v.val_unsigned;
1903 if (for_eh_or_skip >= 0)
1904 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1905 gcc_assert (size_of_uleb128 (r)
1906 == size_of_uleb128 (val1->v.val_unsigned));
1907 dw2_asm_output_data_uleb128 (r, NULL);
1908 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1910 break;
1911 case DW_OP_piece:
1912 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1913 break;
1914 case DW_OP_bit_piece:
1915 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1916 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1917 break;
1918 case DW_OP_deref_size:
1919 case DW_OP_xderef_size:
1920 dw2_asm_output_data (1, val1->v.val_int, NULL);
1921 break;
1923 case DW_OP_addr:
1924 if (loc->dtprel)
1926 if (targetm.asm_out.output_dwarf_dtprel)
1928 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1929 DWARF2_ADDR_SIZE,
1930 val1->v.val_addr);
1931 fputc ('\n', asm_out_file);
1933 else
1934 gcc_unreachable ();
1936 else
1938 #ifdef DWARF2_DEBUGGING_INFO
1939 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1940 #else
1941 gcc_unreachable ();
1942 #endif
1944 break;
1946 case DW_OP_GNU_addr_index:
1947 case DW_OP_GNU_const_index:
1948 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1949 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1950 "(index into .debug_addr)");
1951 break;
1953 case DW_OP_GNU_implicit_pointer:
1955 char label[MAX_ARTIFICIAL_LABEL_BYTES
1956 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1957 gcc_assert (val1->val_class == dw_val_class_die_ref);
1958 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1959 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1960 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1962 break;
1964 case DW_OP_GNU_entry_value:
1965 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1966 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1967 break;
1969 case DW_OP_GNU_const_type:
1971 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1972 gcc_assert (o);
1973 dw2_asm_output_data_uleb128 (o, NULL);
1974 switch (val2->val_class)
1976 case dw_val_class_const:
1977 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1978 dw2_asm_output_data (1, l, NULL);
1979 dw2_asm_output_data (l, val2->v.val_int, NULL);
1980 break;
1981 case dw_val_class_vec:
1983 unsigned int elt_size = val2->v.val_vec.elt_size;
1984 unsigned int len = val2->v.val_vec.length;
1985 unsigned int i;
1986 unsigned char *p;
1988 l = len * elt_size;
1989 dw2_asm_output_data (1, l, NULL);
1990 if (elt_size > sizeof (HOST_WIDE_INT))
1992 elt_size /= 2;
1993 len *= 2;
1995 for (i = 0, p = val2->v.val_vec.array;
1996 i < len;
1997 i++, p += elt_size)
1998 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1999 "fp or vector constant word %u", i);
2001 break;
2002 case dw_val_class_const_double:
2004 unsigned HOST_WIDE_INT first, second;
2005 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2007 dw2_asm_output_data (1, 2 * l, NULL);
2008 if (WORDS_BIG_ENDIAN)
2010 first = val2->v.val_double.high;
2011 second = val2->v.val_double.low;
2013 else
2015 first = val2->v.val_double.low;
2016 second = val2->v.val_double.high;
2018 dw2_asm_output_data (l, first, NULL);
2019 dw2_asm_output_data (l, second, NULL);
2021 break;
2022 default:
2023 gcc_unreachable ();
2026 break;
2027 case DW_OP_GNU_regval_type:
2029 unsigned r = val1->v.val_unsigned;
2030 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2031 gcc_assert (o);
2032 if (for_eh_or_skip >= 0)
2034 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2035 gcc_assert (size_of_uleb128 (r)
2036 == size_of_uleb128 (val1->v.val_unsigned));
2038 dw2_asm_output_data_uleb128 (r, NULL);
2039 dw2_asm_output_data_uleb128 (o, NULL);
2041 break;
2042 case DW_OP_GNU_deref_type:
2044 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2045 gcc_assert (o);
2046 dw2_asm_output_data (1, val1->v.val_int, NULL);
2047 dw2_asm_output_data_uleb128 (o, NULL);
2049 break;
2050 case DW_OP_GNU_convert:
2051 case DW_OP_GNU_reinterpret:
2052 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2053 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2054 else
2056 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2057 gcc_assert (o);
2058 dw2_asm_output_data_uleb128 (o, NULL);
2060 break;
2062 case DW_OP_GNU_parameter_ref:
2064 unsigned long o;
2065 gcc_assert (val1->val_class == dw_val_class_die_ref);
2066 o = get_ref_die_offset (val1->v.val_die_ref.die);
2067 dw2_asm_output_data (4, o, NULL);
2069 break;
2071 default:
2072 /* Other codes have no operands. */
2073 break;
2077 /* Output a sequence of location operations.
2078 The for_eh_or_skip parameter controls whether register numbers are
2079 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2080 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2081 info). This should be suppressed for the cases that have not been converted
2082 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2084 void
2085 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2087 for (; loc != NULL; loc = loc->dw_loc_next)
2089 enum dwarf_location_atom opc = loc->dw_loc_opc;
2090 /* Output the opcode. */
2091 if (for_eh_or_skip >= 0
2092 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2094 unsigned r = (opc - DW_OP_breg0);
2095 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2096 gcc_assert (r <= 31);
2097 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2099 else if (for_eh_or_skip >= 0
2100 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2102 unsigned r = (opc - DW_OP_reg0);
2103 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2104 gcc_assert (r <= 31);
2105 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2108 dw2_asm_output_data (1, opc,
2109 "%s", dwarf_stack_op_name (opc));
2111 /* Output the operand(s) (if any). */
2112 output_loc_operands (loc, for_eh_or_skip);
2116 /* Output location description stack opcode's operands (if any).
2117 The output is single bytes on a line, suitable for .cfi_escape. */
2119 static void
2120 output_loc_operands_raw (dw_loc_descr_ref loc)
2122 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2123 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2125 switch (loc->dw_loc_opc)
2127 case DW_OP_addr:
2128 case DW_OP_GNU_addr_index:
2129 case DW_OP_GNU_const_index:
2130 case DW_OP_implicit_value:
2131 /* We cannot output addresses in .cfi_escape, only bytes. */
2132 gcc_unreachable ();
2134 case DW_OP_const1u:
2135 case DW_OP_const1s:
2136 case DW_OP_pick:
2137 case DW_OP_deref_size:
2138 case DW_OP_xderef_size:
2139 fputc (',', asm_out_file);
2140 dw2_asm_output_data_raw (1, val1->v.val_int);
2141 break;
2143 case DW_OP_const2u:
2144 case DW_OP_const2s:
2145 fputc (',', asm_out_file);
2146 dw2_asm_output_data_raw (2, val1->v.val_int);
2147 break;
2149 case DW_OP_const4u:
2150 case DW_OP_const4s:
2151 fputc (',', asm_out_file);
2152 dw2_asm_output_data_raw (4, val1->v.val_int);
2153 break;
2155 case DW_OP_const8u:
2156 case DW_OP_const8s:
2157 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2158 fputc (',', asm_out_file);
2159 dw2_asm_output_data_raw (8, val1->v.val_int);
2160 break;
2162 case DW_OP_skip:
2163 case DW_OP_bra:
2165 int offset;
2167 gcc_assert (val1->val_class == dw_val_class_loc);
2168 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_raw (2, offset);
2173 break;
2175 case DW_OP_regx:
2177 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2178 gcc_assert (size_of_uleb128 (r)
2179 == size_of_uleb128 (val1->v.val_unsigned));
2180 fputc (',', asm_out_file);
2181 dw2_asm_output_data_uleb128_raw (r);
2183 break;
2185 case DW_OP_constu:
2186 case DW_OP_plus_uconst:
2187 case DW_OP_piece:
2188 fputc (',', asm_out_file);
2189 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2190 break;
2192 case DW_OP_bit_piece:
2193 fputc (',', asm_out_file);
2194 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2195 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2196 break;
2198 case DW_OP_consts:
2199 case DW_OP_breg0:
2200 case DW_OP_breg1:
2201 case DW_OP_breg2:
2202 case DW_OP_breg3:
2203 case DW_OP_breg4:
2204 case DW_OP_breg5:
2205 case DW_OP_breg6:
2206 case DW_OP_breg7:
2207 case DW_OP_breg8:
2208 case DW_OP_breg9:
2209 case DW_OP_breg10:
2210 case DW_OP_breg11:
2211 case DW_OP_breg12:
2212 case DW_OP_breg13:
2213 case DW_OP_breg14:
2214 case DW_OP_breg15:
2215 case DW_OP_breg16:
2216 case DW_OP_breg17:
2217 case DW_OP_breg18:
2218 case DW_OP_breg19:
2219 case DW_OP_breg20:
2220 case DW_OP_breg21:
2221 case DW_OP_breg22:
2222 case DW_OP_breg23:
2223 case DW_OP_breg24:
2224 case DW_OP_breg25:
2225 case DW_OP_breg26:
2226 case DW_OP_breg27:
2227 case DW_OP_breg28:
2228 case DW_OP_breg29:
2229 case DW_OP_breg30:
2230 case DW_OP_breg31:
2231 case DW_OP_fbreg:
2232 fputc (',', asm_out_file);
2233 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2234 break;
2236 case DW_OP_bregx:
2238 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2239 gcc_assert (size_of_uleb128 (r)
2240 == size_of_uleb128 (val1->v.val_unsigned));
2241 fputc (',', asm_out_file);
2242 dw2_asm_output_data_uleb128_raw (r);
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2246 break;
2248 case DW_OP_GNU_implicit_pointer:
2249 case DW_OP_GNU_entry_value:
2250 case DW_OP_GNU_const_type:
2251 case DW_OP_GNU_regval_type:
2252 case DW_OP_GNU_deref_type:
2253 case DW_OP_GNU_convert:
2254 case DW_OP_GNU_reinterpret:
2255 case DW_OP_GNU_parameter_ref:
2256 gcc_unreachable ();
2257 break;
2259 default:
2260 /* Other codes have no operands. */
2261 break;
2265 void
2266 output_loc_sequence_raw (dw_loc_descr_ref loc)
2268 while (1)
2270 enum dwarf_location_atom opc = loc->dw_loc_opc;
2271 /* Output the opcode. */
2272 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2274 unsigned r = (opc - DW_OP_breg0);
2275 r = DWARF2_FRAME_REG_OUT (r, 1);
2276 gcc_assert (r <= 31);
2277 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2279 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2281 unsigned r = (opc - DW_OP_reg0);
2282 r = DWARF2_FRAME_REG_OUT (r, 1);
2283 gcc_assert (r <= 31);
2284 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2286 /* Output the opcode. */
2287 fprintf (asm_out_file, "%#x", opc);
2288 output_loc_operands_raw (loc);
2290 if (!loc->dw_loc_next)
2291 break;
2292 loc = loc->dw_loc_next;
2294 fputc (',', asm_out_file);
2298 /* This function builds a dwarf location descriptor sequence from a
2299 dw_cfa_location, adding the given OFFSET to the result of the
2300 expression. */
2302 struct dw_loc_descr_struct *
2303 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2305 struct dw_loc_descr_struct *head, *tmp;
2307 offset += cfa->offset;
2309 if (cfa->indirect)
2311 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2312 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2313 head->dw_loc_oprnd1.val_entry = NULL;
2314 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2315 add_loc_descr (&head, tmp);
2316 if (offset != 0)
2318 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2319 add_loc_descr (&head, tmp);
2322 else
2323 head = new_reg_loc_descr (cfa->reg, offset);
2325 return head;
2328 /* This function builds a dwarf location descriptor sequence for
2329 the address at OFFSET from the CFA when stack is aligned to
2330 ALIGNMENT byte. */
2332 struct dw_loc_descr_struct *
2333 build_cfa_aligned_loc (dw_cfa_location *cfa,
2334 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2336 struct dw_loc_descr_struct *head;
2337 unsigned int dwarf_fp
2338 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2340 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2341 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2343 head = new_reg_loc_descr (dwarf_fp, 0);
2344 add_loc_descr (&head, int_loc_descriptor (alignment));
2345 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2346 loc_descr_plus_const (&head, offset);
2348 else
2349 head = new_reg_loc_descr (dwarf_fp, offset);
2350 return head;
2353 /* And now, the support for symbolic debugging information. */
2355 /* .debug_str support. */
2356 static int output_indirect_string (void **, void *);
2358 static void dwarf2out_init (const char *);
2359 static void dwarf2out_finish (const char *);
2360 static void dwarf2out_assembly_start (void);
2361 static void dwarf2out_define (unsigned int, const char *);
2362 static void dwarf2out_undef (unsigned int, const char *);
2363 static void dwarf2out_start_source_file (unsigned, const char *);
2364 static void dwarf2out_end_source_file (unsigned);
2365 static void dwarf2out_function_decl (tree);
2366 static void dwarf2out_begin_block (unsigned, unsigned);
2367 static void dwarf2out_end_block (unsigned, unsigned);
2368 static bool dwarf2out_ignore_block (const_tree);
2369 static void dwarf2out_global_decl (tree);
2370 static void dwarf2out_type_decl (tree, int);
2371 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2372 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2373 dw_die_ref);
2374 static void dwarf2out_abstract_function (tree);
2375 static void dwarf2out_var_location (rtx);
2376 static void dwarf2out_begin_function (tree);
2377 static void dwarf2out_end_function (unsigned int);
2378 static void dwarf2out_set_name (tree, tree);
2380 /* The debug hooks structure. */
2382 const struct gcc_debug_hooks dwarf2_debug_hooks =
2384 dwarf2out_init,
2385 dwarf2out_finish,
2386 dwarf2out_assembly_start,
2387 dwarf2out_define,
2388 dwarf2out_undef,
2389 dwarf2out_start_source_file,
2390 dwarf2out_end_source_file,
2391 dwarf2out_begin_block,
2392 dwarf2out_end_block,
2393 dwarf2out_ignore_block,
2394 dwarf2out_source_line,
2395 dwarf2out_begin_prologue,
2396 #if VMS_DEBUGGING_INFO
2397 dwarf2out_vms_end_prologue,
2398 dwarf2out_vms_begin_epilogue,
2399 #else
2400 debug_nothing_int_charstar,
2401 debug_nothing_int_charstar,
2402 #endif
2403 dwarf2out_end_epilogue,
2404 dwarf2out_begin_function,
2405 dwarf2out_end_function, /* end_function */
2406 dwarf2out_function_decl, /* function_decl */
2407 dwarf2out_global_decl,
2408 dwarf2out_type_decl, /* type_decl */
2409 dwarf2out_imported_module_or_decl,
2410 debug_nothing_tree, /* deferred_inline_function */
2411 /* The DWARF 2 backend tries to reduce debugging bloat by not
2412 emitting the abstract description of inline functions until
2413 something tries to reference them. */
2414 dwarf2out_abstract_function, /* outlining_inline_function */
2415 debug_nothing_rtx, /* label */
2416 debug_nothing_int, /* handle_pch */
2417 dwarf2out_var_location,
2418 dwarf2out_switch_text_section,
2419 dwarf2out_set_name,
2420 1, /* start_end_main_source_file */
2421 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2424 /* NOTE: In the comments in this file, many references are made to
2425 "Debugging Information Entries". This term is abbreviated as `DIE'
2426 throughout the remainder of this file. */
2428 /* An internal representation of the DWARF output is built, and then
2429 walked to generate the DWARF debugging info. The walk of the internal
2430 representation is done after the entire program has been compiled.
2431 The types below are used to describe the internal representation. */
2433 /* Whether to put type DIEs into their own section .debug_types instead
2434 of making them part of the .debug_info section. Only supported for
2435 Dwarf V4 or higher and the user didn't disable them through
2436 -fno-debug-types-section. It is more efficient to put them in a
2437 separate comdat sections since the linker will then be able to
2438 remove duplicates. But not all tools support .debug_types sections
2439 yet. */
2441 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2443 /* Various DIE's use offsets relative to the beginning of the
2444 .debug_info section to refer to each other. */
2446 typedef long int dw_offset;
2448 /* Define typedefs here to avoid circular dependencies. */
2450 typedef struct dw_attr_struct *dw_attr_ref;
2451 typedef struct dw_line_info_struct *dw_line_info_ref;
2452 typedef struct pubname_struct *pubname_ref;
2453 typedef struct dw_ranges_struct *dw_ranges_ref;
2454 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2455 typedef struct comdat_type_struct *comdat_type_node_ref;
2457 /* The entries in the line_info table more-or-less mirror the opcodes
2458 that are used in the real dwarf line table. Arrays of these entries
2459 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2460 supported. */
2462 enum dw_line_info_opcode {
2463 /* Emit DW_LNE_set_address; the operand is the label index. */
2464 LI_set_address,
2466 /* Emit a row to the matrix with the given line. This may be done
2467 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2468 special opcodes. */
2469 LI_set_line,
2471 /* Emit a DW_LNS_set_file. */
2472 LI_set_file,
2474 /* Emit a DW_LNS_set_column. */
2475 LI_set_column,
2477 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2478 LI_negate_stmt,
2480 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2481 LI_set_prologue_end,
2482 LI_set_epilogue_begin,
2484 /* Emit a DW_LNE_set_discriminator. */
2485 LI_set_discriminator
2488 typedef struct GTY(()) dw_line_info_struct {
2489 enum dw_line_info_opcode opcode;
2490 unsigned int val;
2491 } dw_line_info_entry;
2494 typedef struct GTY(()) dw_line_info_table_struct {
2495 /* The label that marks the end of this section. */
2496 const char *end_label;
2498 /* The values for the last row of the matrix, as collected in the table.
2499 These are used to minimize the changes to the next row. */
2500 unsigned int file_num;
2501 unsigned int line_num;
2502 unsigned int column_num;
2503 int discrim_num;
2504 bool is_stmt;
2505 bool in_use;
2507 vec<dw_line_info_entry, va_gc> *entries;
2508 } dw_line_info_table;
2510 typedef dw_line_info_table *dw_line_info_table_p;
2513 /* Each DIE attribute has a field specifying the attribute kind,
2514 a link to the next attribute in the chain, and an attribute value.
2515 Attributes are typically linked below the DIE they modify. */
2517 typedef struct GTY(()) dw_attr_struct {
2518 enum dwarf_attribute dw_attr;
2519 dw_val_node dw_attr_val;
2521 dw_attr_node;
2524 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2525 The children of each node form a circular list linked by
2526 die_sib. die_child points to the node *before* the "first" child node. */
2528 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2529 union die_symbol_or_type_node
2531 const char * GTY ((tag ("0"))) die_symbol;
2532 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2534 GTY ((desc ("%0.comdat_type_p"))) die_id;
2535 vec<dw_attr_node, va_gc> *die_attr;
2536 dw_die_ref die_parent;
2537 dw_die_ref die_child;
2538 dw_die_ref die_sib;
2539 dw_die_ref die_definition; /* ref from a specification to its definition */
2540 dw_offset die_offset;
2541 unsigned long die_abbrev;
2542 int die_mark;
2543 unsigned int decl_id;
2544 enum dwarf_tag die_tag;
2545 /* Die is used and must not be pruned as unused. */
2546 BOOL_BITFIELD die_perennial_p : 1;
2547 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2548 /* Lots of spare bits. */
2550 die_node;
2552 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2553 #define FOR_EACH_CHILD(die, c, expr) do { \
2554 c = die->die_child; \
2555 if (c) do { \
2556 c = c->die_sib; \
2557 expr; \
2558 } while (c != die->die_child); \
2559 } while (0)
2561 /* The pubname structure */
2563 typedef struct GTY(()) pubname_struct {
2564 dw_die_ref die;
2565 const char *name;
2567 pubname_entry;
2570 struct GTY(()) dw_ranges_struct {
2571 /* If this is positive, it's a block number, otherwise it's a
2572 bitwise-negated index into dw_ranges_by_label. */
2573 int num;
2576 /* A structure to hold a macinfo entry. */
2578 typedef struct GTY(()) macinfo_struct {
2579 unsigned char code;
2580 unsigned HOST_WIDE_INT lineno;
2581 const char *info;
2583 macinfo_entry;
2586 struct GTY(()) dw_ranges_by_label_struct {
2587 const char *begin;
2588 const char *end;
2591 /* The comdat type node structure. */
2592 typedef struct GTY(()) comdat_type_struct
2594 dw_die_ref root_die;
2595 dw_die_ref type_die;
2596 dw_die_ref skeleton_die;
2597 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2598 struct comdat_type_struct *next;
2600 comdat_type_node;
2602 /* The limbo die list structure. */
2603 typedef struct GTY(()) limbo_die_struct {
2604 dw_die_ref die;
2605 tree created_for;
2606 struct limbo_die_struct *next;
2608 limbo_die_node;
2610 typedef struct skeleton_chain_struct
2612 dw_die_ref old_die;
2613 dw_die_ref new_die;
2614 struct skeleton_chain_struct *parent;
2616 skeleton_chain_node;
2618 /* Define a macro which returns nonzero for a TYPE_DECL which was
2619 implicitly generated for a type.
2621 Note that, unlike the C front-end (which generates a NULL named
2622 TYPE_DECL node for each complete tagged type, each array type,
2623 and each function type node created) the C++ front-end generates
2624 a _named_ TYPE_DECL node for each tagged type node created.
2625 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2626 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2627 front-end, but for each type, tagged or not. */
2629 #define TYPE_DECL_IS_STUB(decl) \
2630 (DECL_NAME (decl) == NULL_TREE \
2631 || (DECL_ARTIFICIAL (decl) \
2632 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2633 /* This is necessary for stub decls that \
2634 appear in nested inline functions. */ \
2635 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2636 && (decl_ultimate_origin (decl) \
2637 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2639 /* Information concerning the compilation unit's programming
2640 language, and compiler version. */
2642 /* Fixed size portion of the DWARF compilation unit header. */
2643 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2644 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2646 /* Fixed size portion of the DWARF comdat type unit header. */
2647 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2648 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2649 + DWARF_OFFSET_SIZE)
2651 /* Fixed size portion of public names info. */
2652 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2654 /* Fixed size portion of the address range info. */
2655 #define DWARF_ARANGES_HEADER_SIZE \
2656 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2657 DWARF2_ADDR_SIZE * 2) \
2658 - DWARF_INITIAL_LENGTH_SIZE)
2660 /* Size of padding portion in the address range info. It must be
2661 aligned to twice the pointer size. */
2662 #define DWARF_ARANGES_PAD_SIZE \
2663 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2664 DWARF2_ADDR_SIZE * 2) \
2665 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2667 /* Use assembler line directives if available. */
2668 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2669 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2670 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2671 #else
2672 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2673 #endif
2674 #endif
2676 /* Minimum line offset in a special line info. opcode.
2677 This value was chosen to give a reasonable range of values. */
2678 #define DWARF_LINE_BASE -10
2680 /* First special line opcode - leave room for the standard opcodes. */
2681 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2683 /* Range of line offsets in a special line info. opcode. */
2684 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2686 /* Flag that indicates the initial value of the is_stmt_start flag.
2687 In the present implementation, we do not mark any lines as
2688 the beginning of a source statement, because that information
2689 is not made available by the GCC front-end. */
2690 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2692 /* Maximum number of operations per instruction bundle. */
2693 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2694 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2695 #endif
2697 /* This location is used by calc_die_sizes() to keep track
2698 the offset of each DIE within the .debug_info section. */
2699 static unsigned long next_die_offset;
2701 /* Record the root of the DIE's built for the current compilation unit. */
2702 static GTY(()) dw_die_ref single_comp_unit_die;
2704 /* A list of type DIEs that have been separated into comdat sections. */
2705 static GTY(()) comdat_type_node *comdat_type_list;
2707 /* A list of DIEs with a NULL parent waiting to be relocated. */
2708 static GTY(()) limbo_die_node *limbo_die_list;
2710 /* A list of DIEs for which we may have to generate
2711 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2712 static GTY(()) limbo_die_node *deferred_asm_name;
2714 /* Filenames referenced by this compilation unit. */
2715 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2717 /* A hash table of references to DIE's that describe declarations.
2718 The key is a DECL_UID() which is a unique number identifying each decl. */
2719 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2721 /* A hash table of references to DIE's that describe COMMON blocks.
2722 The key is DECL_UID() ^ die_parent. */
2723 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2725 typedef struct GTY(()) die_arg_entry_struct {
2726 dw_die_ref die;
2727 tree arg;
2728 } die_arg_entry;
2731 /* Node of the variable location list. */
2732 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2733 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2734 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2735 in mode of the EXPR_LIST node and first EXPR_LIST operand
2736 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2737 location or NULL for padding. For larger bitsizes,
2738 mode is 0 and first operand is a CONCAT with bitsize
2739 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2740 NULL as second operand. */
2741 rtx GTY (()) loc;
2742 const char * GTY (()) label;
2743 struct var_loc_node * GTY (()) next;
2746 /* Variable location list. */
2747 struct GTY (()) var_loc_list_def {
2748 struct var_loc_node * GTY (()) first;
2750 /* Pointer to the last but one or last element of the
2751 chained list. If the list is empty, both first and
2752 last are NULL, if the list contains just one node
2753 or the last node certainly is not redundant, it points
2754 to the last node, otherwise points to the last but one.
2755 Do not mark it for GC because it is marked through the chain. */
2756 struct var_loc_node * GTY ((skip ("%h"))) last;
2758 /* Pointer to the last element before section switch,
2759 if NULL, either sections weren't switched or first
2760 is after section switch. */
2761 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2763 /* DECL_UID of the variable decl. */
2764 unsigned int decl_id;
2766 typedef struct var_loc_list_def var_loc_list;
2768 /* Call argument location list. */
2769 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2770 rtx GTY (()) call_arg_loc_note;
2771 const char * GTY (()) label;
2772 tree GTY (()) block;
2773 bool tail_call_p;
2774 rtx GTY (()) symbol_ref;
2775 struct call_arg_loc_node * GTY (()) next;
2779 /* Table of decl location linked lists. */
2780 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2782 /* Head and tail of call_arg_loc chain. */
2783 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2784 static struct call_arg_loc_node *call_arg_loc_last;
2786 /* Number of call sites in the current function. */
2787 static int call_site_count = -1;
2788 /* Number of tail call sites in the current function. */
2789 static int tail_call_site_count = -1;
2791 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2792 DIEs. */
2793 static vec<dw_die_ref> block_map;
2795 /* A cached location list. */
2796 struct GTY (()) cached_dw_loc_list_def {
2797 /* The DECL_UID of the decl that this entry describes. */
2798 unsigned int decl_id;
2800 /* The cached location list. */
2801 dw_loc_list_ref loc_list;
2803 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2805 /* Table of cached location lists. */
2806 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2808 /* A pointer to the base of a list of references to DIE's that
2809 are uniquely identified by their tag, presence/absence of
2810 children DIE's, and list of attribute/value pairs. */
2811 static GTY((length ("abbrev_die_table_allocated")))
2812 dw_die_ref *abbrev_die_table;
2814 /* Number of elements currently allocated for abbrev_die_table. */
2815 static GTY(()) unsigned abbrev_die_table_allocated;
2817 /* Number of elements in type_die_table currently in use. */
2818 static GTY(()) unsigned abbrev_die_table_in_use;
2820 /* Size (in elements) of increments by which we may expand the
2821 abbrev_die_table. */
2822 #define ABBREV_DIE_TABLE_INCREMENT 256
2824 /* A global counter for generating labels for line number data. */
2825 static unsigned int line_info_label_num;
2827 /* The current table to which we should emit line number information
2828 for the current function. This will be set up at the beginning of
2829 assembly for the function. */
2830 static dw_line_info_table *cur_line_info_table;
2832 /* The two default tables of line number info. */
2833 static GTY(()) dw_line_info_table *text_section_line_info;
2834 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2836 /* The set of all non-default tables of line number info. */
2837 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2839 /* A flag to tell pubnames/types export if there is an info section to
2840 refer to. */
2841 static bool info_section_emitted;
2843 /* A pointer to the base of a table that contains a list of publicly
2844 accessible names. */
2845 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2847 /* A pointer to the base of a table that contains a list of publicly
2848 accessible types. */
2849 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2851 /* A pointer to the base of a table that contains a list of macro
2852 defines/undefines (and file start/end markers). */
2853 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2855 /* True if .debug_macinfo or .debug_macros section is going to be
2856 emitted. */
2857 #define have_macinfo \
2858 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2859 && !macinfo_table->is_empty ())
2861 /* Array of dies for which we should generate .debug_ranges info. */
2862 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2864 /* Number of elements currently allocated for ranges_table. */
2865 static GTY(()) unsigned ranges_table_allocated;
2867 /* Number of elements in ranges_table currently in use. */
2868 static GTY(()) unsigned ranges_table_in_use;
2870 /* Array of pairs of labels referenced in ranges_table. */
2871 static GTY ((length ("ranges_by_label_allocated")))
2872 dw_ranges_by_label_ref ranges_by_label;
2874 /* Number of elements currently allocated for ranges_by_label. */
2875 static GTY(()) unsigned ranges_by_label_allocated;
2877 /* Number of elements in ranges_by_label currently in use. */
2878 static GTY(()) unsigned ranges_by_label_in_use;
2880 /* Size (in elements) of increments by which we may expand the
2881 ranges_table. */
2882 #define RANGES_TABLE_INCREMENT 64
2884 /* Whether we have location lists that need outputting */
2885 static GTY(()) bool have_location_lists;
2887 /* Unique label counter. */
2888 static GTY(()) unsigned int loclabel_num;
2890 /* Unique label counter for point-of-call tables. */
2891 static GTY(()) unsigned int poc_label_num;
2893 /* Record whether the function being analyzed contains inlined functions. */
2894 static int current_function_has_inlines;
2896 /* The last file entry emitted by maybe_emit_file(). */
2897 static GTY(()) struct dwarf_file_data * last_emitted_file;
2899 /* Number of internal labels generated by gen_internal_sym(). */
2900 static GTY(()) int label_num;
2902 /* Cached result of previous call to lookup_filename. */
2903 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2905 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2907 /* Instances of generic types for which we need to generate debug
2908 info that describe their generic parameters and arguments. That
2909 generation needs to happen once all types are properly laid out so
2910 we do it at the end of compilation. */
2911 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2913 /* Offset from the "steady-state frame pointer" to the frame base,
2914 within the current function. */
2915 static HOST_WIDE_INT frame_pointer_fb_offset;
2916 static bool frame_pointer_fb_offset_valid;
2918 static vec<dw_die_ref> base_types;
2920 /* Forward declarations for functions defined in this file. */
2922 static int is_pseudo_reg (const_rtx);
2923 static tree type_main_variant (tree);
2924 static int is_tagged_type (const_tree);
2925 static const char *dwarf_tag_name (unsigned);
2926 static const char *dwarf_attr_name (unsigned);
2927 static const char *dwarf_form_name (unsigned);
2928 static tree decl_ultimate_origin (const_tree);
2929 static tree decl_class_context (tree);
2930 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2931 static inline enum dw_val_class AT_class (dw_attr_ref);
2932 static inline unsigned int AT_index (dw_attr_ref);
2933 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2934 static inline unsigned AT_flag (dw_attr_ref);
2935 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2936 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2937 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2938 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2939 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2940 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2941 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2942 unsigned int, unsigned char *);
2943 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2944 static hashval_t debug_str_do_hash (const void *);
2945 static int debug_str_eq (const void *, const void *);
2946 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2947 static inline const char *AT_string (dw_attr_ref);
2948 static enum dwarf_form AT_string_form (dw_attr_ref);
2949 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2950 static void add_AT_specification (dw_die_ref, dw_die_ref);
2951 static inline dw_die_ref AT_ref (dw_attr_ref);
2952 static inline int AT_ref_external (dw_attr_ref);
2953 static inline void set_AT_ref_external (dw_attr_ref, int);
2954 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2955 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2956 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2957 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2958 dw_loc_list_ref);
2959 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2960 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2961 static void remove_addr_table_entry (addr_table_entry *);
2962 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2963 static inline rtx AT_addr (dw_attr_ref);
2964 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2965 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2966 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2967 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2968 unsigned HOST_WIDE_INT);
2969 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2970 unsigned long, bool);
2971 static inline const char *AT_lbl (dw_attr_ref);
2972 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2973 static const char *get_AT_low_pc (dw_die_ref);
2974 static const char *get_AT_hi_pc (dw_die_ref);
2975 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2976 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2977 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2978 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2979 static bool is_cxx (void);
2980 static bool is_fortran (void);
2981 static bool is_ada (void);
2982 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2983 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2984 static void add_child_die (dw_die_ref, dw_die_ref);
2985 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2986 static dw_die_ref lookup_type_die (tree);
2987 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2988 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2989 static void equate_type_number_to_die (tree, dw_die_ref);
2990 static hashval_t decl_die_table_hash (const void *);
2991 static int decl_die_table_eq (const void *, const void *);
2992 static dw_die_ref lookup_decl_die (tree);
2993 static hashval_t common_block_die_table_hash (const void *);
2994 static int common_block_die_table_eq (const void *, const void *);
2995 static hashval_t decl_loc_table_hash (const void *);
2996 static int decl_loc_table_eq (const void *, const void *);
2997 static var_loc_list *lookup_decl_loc (const_tree);
2998 static void equate_decl_number_to_die (tree, dw_die_ref);
2999 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3000 static void print_spaces (FILE *);
3001 static void print_die (dw_die_ref, FILE *);
3002 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3003 static dw_die_ref pop_compile_unit (dw_die_ref);
3004 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3005 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3006 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3007 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3008 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3009 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3010 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3011 struct md5_ctx *, int *);
3012 struct checksum_attributes;
3013 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3014 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3015 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3016 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3017 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3018 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3019 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3020 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3021 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3022 static void compute_section_prefix (dw_die_ref);
3023 static int is_type_die (dw_die_ref);
3024 static int is_comdat_die (dw_die_ref);
3025 static int is_symbol_die (dw_die_ref);
3026 static void assign_symbol_names (dw_die_ref);
3027 static void break_out_includes (dw_die_ref);
3028 static int is_declaration_die (dw_die_ref);
3029 static int should_move_die_to_comdat (dw_die_ref);
3030 static dw_die_ref clone_as_declaration (dw_die_ref);
3031 static dw_die_ref clone_die (dw_die_ref);
3032 static dw_die_ref clone_tree (dw_die_ref);
3033 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3034 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3035 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3036 static dw_die_ref generate_skeleton (dw_die_ref);
3037 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3038 dw_die_ref,
3039 dw_die_ref);
3040 static void break_out_comdat_types (dw_die_ref);
3041 static void copy_decls_for_unworthy_types (dw_die_ref);
3043 static void add_sibling_attributes (dw_die_ref);
3044 static void output_location_lists (dw_die_ref);
3045 static int constant_size (unsigned HOST_WIDE_INT);
3046 static unsigned long size_of_die (dw_die_ref);
3047 static void calc_die_sizes (dw_die_ref);
3048 static void calc_base_type_die_sizes (void);
3049 static void mark_dies (dw_die_ref);
3050 static void unmark_dies (dw_die_ref);
3051 static void unmark_all_dies (dw_die_ref);
3052 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3053 static unsigned long size_of_aranges (void);
3054 static enum dwarf_form value_format (dw_attr_ref);
3055 static void output_value_format (dw_attr_ref);
3056 static void output_abbrev_section (void);
3057 static void output_die_abbrevs (unsigned long, dw_die_ref);
3058 static void output_die_symbol (dw_die_ref);
3059 static void output_die (dw_die_ref);
3060 static void output_compilation_unit_header (void);
3061 static void output_comp_unit (dw_die_ref, int);
3062 static void output_comdat_type_unit (comdat_type_node *);
3063 static const char *dwarf2_name (tree, int);
3064 static void add_pubname (tree, dw_die_ref);
3065 static void add_enumerator_pubname (const char *, dw_die_ref);
3066 static void add_pubname_string (const char *, dw_die_ref);
3067 static void add_pubtype (tree, dw_die_ref);
3068 static void output_pubnames (vec<pubname_entry, va_gc> *);
3069 static void output_aranges (unsigned long);
3070 static unsigned int add_ranges_num (int);
3071 static unsigned int add_ranges (const_tree);
3072 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3073 bool *, bool);
3074 static void output_ranges (void);
3075 static dw_line_info_table *new_line_info_table (void);
3076 static void output_line_info (bool);
3077 static void output_file_names (void);
3078 static dw_die_ref base_type_die (tree);
3079 static int is_base_type (tree);
3080 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3081 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3082 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3083 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3084 static int type_is_enum (const_tree);
3085 static unsigned int dbx_reg_number (const_rtx);
3086 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3087 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3088 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3089 enum var_init_status);
3090 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3091 enum var_init_status);
3092 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3093 enum var_init_status);
3094 static int is_based_loc (const_rtx);
3095 static int resolve_one_addr (rtx *, void *);
3096 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3097 enum var_init_status);
3098 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3099 enum var_init_status);
3100 static dw_loc_list_ref loc_list_from_tree (tree, int);
3101 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3102 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3103 static tree field_type (const_tree);
3104 static unsigned int simple_type_align_in_bits (const_tree);
3105 static unsigned int simple_decl_align_in_bits (const_tree);
3106 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3107 static HOST_WIDE_INT field_byte_offset (const_tree);
3108 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3109 dw_loc_list_ref);
3110 static void add_data_member_location_attribute (dw_die_ref, tree);
3111 static bool add_const_value_attribute (dw_die_ref, rtx);
3112 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3113 static void insert_double (double_int, unsigned char *);
3114 static void insert_float (const_rtx, unsigned char *);
3115 static rtx rtl_for_decl_location (tree);
3116 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3117 enum dwarf_attribute);
3118 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3119 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3120 static void add_name_attribute (dw_die_ref, const char *);
3121 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3122 static void add_comp_dir_attribute (dw_die_ref);
3123 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3124 static void add_subscript_info (dw_die_ref, tree, bool);
3125 static void add_byte_size_attribute (dw_die_ref, tree);
3126 static void add_bit_offset_attribute (dw_die_ref, tree);
3127 static void add_bit_size_attribute (dw_die_ref, tree);
3128 static void add_prototyped_attribute (dw_die_ref, tree);
3129 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3130 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3131 static void add_src_coords_attributes (dw_die_ref, tree);
3132 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3133 static void push_decl_scope (tree);
3134 static void pop_decl_scope (void);
3135 static dw_die_ref scope_die_for (tree, dw_die_ref);
3136 static inline int local_scope_p (dw_die_ref);
3137 static inline int class_scope_p (dw_die_ref);
3138 static inline int class_or_namespace_scope_p (dw_die_ref);
3139 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3140 static void add_calling_convention_attribute (dw_die_ref, tree);
3141 static const char *type_tag (const_tree);
3142 static tree member_declared_type (const_tree);
3143 #if 0
3144 static const char *decl_start_label (tree);
3145 #endif
3146 static void gen_array_type_die (tree, dw_die_ref);
3147 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3148 #if 0
3149 static void gen_entry_point_die (tree, dw_die_ref);
3150 #endif
3151 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3152 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3153 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3154 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3155 static void gen_formal_types_die (tree, dw_die_ref);
3156 static void gen_subprogram_die (tree, dw_die_ref);
3157 static void gen_variable_die (tree, tree, dw_die_ref);
3158 static void gen_const_die (tree, dw_die_ref);
3159 static void gen_label_die (tree, dw_die_ref);
3160 static void gen_lexical_block_die (tree, dw_die_ref, int);
3161 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3162 static void gen_field_die (tree, dw_die_ref);
3163 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3164 static dw_die_ref gen_compile_unit_die (const char *);
3165 static void gen_inheritance_die (tree, tree, dw_die_ref);
3166 static void gen_member_die (tree, dw_die_ref);
3167 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3168 enum debug_info_usage);
3169 static void gen_subroutine_type_die (tree, dw_die_ref);
3170 static void gen_typedef_die (tree, dw_die_ref);
3171 static void gen_type_die (tree, dw_die_ref);
3172 static void gen_block_die (tree, dw_die_ref, int);
3173 static void decls_for_scope (tree, dw_die_ref, int);
3174 static inline int is_redundant_typedef (const_tree);
3175 static bool is_naming_typedef_decl (const_tree);
3176 static inline dw_die_ref get_context_die (tree);
3177 static void gen_namespace_die (tree, dw_die_ref);
3178 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3179 static dw_die_ref force_decl_die (tree);
3180 static dw_die_ref force_type_die (tree);
3181 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3182 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3183 static struct dwarf_file_data * lookup_filename (const char *);
3184 static void retry_incomplete_types (void);
3185 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3186 static void gen_generic_params_dies (tree);
3187 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3188 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3189 static void splice_child_die (dw_die_ref, dw_die_ref);
3190 static int file_info_cmp (const void *, const void *);
3191 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3192 const char *, const char *);
3193 static void output_loc_list (dw_loc_list_ref);
3194 static char *gen_internal_sym (const char *);
3195 static bool want_pubnames (void);
3197 static void prune_unmark_dies (dw_die_ref);
3198 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3199 static void prune_unused_types_mark (dw_die_ref, int);
3200 static void prune_unused_types_walk (dw_die_ref);
3201 static void prune_unused_types_walk_attribs (dw_die_ref);
3202 static void prune_unused_types_prune (dw_die_ref);
3203 static void prune_unused_types (void);
3204 static int maybe_emit_file (struct dwarf_file_data *fd);
3205 static inline const char *AT_vms_delta1 (dw_attr_ref);
3206 static inline const char *AT_vms_delta2 (dw_attr_ref);
3207 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3208 const char *, const char *);
3209 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3210 static void gen_remaining_tmpl_value_param_die_attribute (void);
3211 static bool generic_type_p (tree);
3212 static void schedule_generic_params_dies_gen (tree t);
3213 static void gen_scheduled_generic_parms_dies (void);
3215 static const char *comp_dir_string (void);
3217 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3219 /* enum for tracking thread-local variables whose address is really an offset
3220 relative to the TLS pointer, which will need link-time relocation, but will
3221 not need relocation by the DWARF consumer. */
3223 enum dtprel_bool
3225 dtprel_false = 0,
3226 dtprel_true = 1
3229 /* Return the operator to use for an address of a variable. For dtprel_true, we
3230 use DW_OP_const*. For regular variables, which need both link-time
3231 relocation and consumer-level relocation (e.g., to account for shared objects
3232 loaded at a random address), we use DW_OP_addr*. */
3234 static inline enum dwarf_location_atom
3235 dw_addr_op (enum dtprel_bool dtprel)
3237 if (dtprel == dtprel_true)
3238 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3239 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3240 else
3241 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3244 /* Return a pointer to a newly allocated address location description. If
3245 dwarf_split_debug_info is true, then record the address with the appropriate
3246 relocation. */
3247 static inline dw_loc_descr_ref
3248 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3250 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3252 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3253 ref->dw_loc_oprnd1.v.val_addr = addr;
3254 ref->dtprel = dtprel;
3255 if (dwarf_split_debug_info)
3256 ref->dw_loc_oprnd1.val_entry
3257 = add_addr_table_entry (addr,
3258 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3259 else
3260 ref->dw_loc_oprnd1.val_entry = NULL;
3262 return ref;
3265 /* Section names used to hold DWARF debugging information. */
3267 #ifndef DEBUG_INFO_SECTION
3268 #define DEBUG_INFO_SECTION ".debug_info"
3269 #endif
3270 #ifndef DEBUG_DWO_INFO_SECTION
3271 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3272 #endif
3273 #ifndef DEBUG_ABBREV_SECTION
3274 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3275 #endif
3276 #ifndef DEBUG_DWO_ABBREV_SECTION
3277 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3278 #endif
3279 #ifndef DEBUG_ARANGES_SECTION
3280 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3281 #endif
3282 #ifndef DEBUG_ADDR_SECTION
3283 #define DEBUG_ADDR_SECTION ".debug_addr"
3284 #endif
3285 #ifndef DEBUG_NORM_MACINFO_SECTION
3286 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3287 #endif
3288 #ifndef DEBUG_DWO_MACINFO_SECTION
3289 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3290 #endif
3291 #ifndef DEBUG_MACINFO_SECTION
3292 #define DEBUG_MACINFO_SECTION \
3293 (!dwarf_split_debug_info \
3294 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3295 #endif
3296 #ifndef DEBUG_NORM_MACRO_SECTION
3297 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3298 #endif
3299 #ifndef DEBUG_DWO_MACRO_SECTION
3300 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3301 #endif
3302 #ifndef DEBUG_MACRO_SECTION
3303 #define DEBUG_MACRO_SECTION \
3304 (!dwarf_split_debug_info \
3305 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3306 #endif
3307 #ifndef DEBUG_LINE_SECTION
3308 #define DEBUG_LINE_SECTION ".debug_line"
3309 #endif
3310 #ifndef DEBUG_DWO_LINE_SECTION
3311 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3312 #endif
3313 #ifndef DEBUG_LOC_SECTION
3314 #define DEBUG_LOC_SECTION ".debug_loc"
3315 #endif
3316 #ifndef DEBUG_DWO_LOC_SECTION
3317 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3318 #endif
3319 #ifndef DEBUG_PUBNAMES_SECTION
3320 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3321 #endif
3322 #ifndef DEBUG_PUBTYPES_SECTION
3323 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3324 #endif
3325 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3326 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3327 #ifndef DEBUG_STR_OFFSETS_SECTION
3328 #define DEBUG_STR_OFFSETS_SECTION \
3329 (!dwarf_split_debug_info \
3330 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3331 #endif
3332 #ifndef DEBUG_STR_DWO_SECTION
3333 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3334 #endif
3335 #ifndef DEBUG_STR_SECTION
3336 #define DEBUG_STR_SECTION ".debug_str"
3337 #endif
3338 #ifndef DEBUG_RANGES_SECTION
3339 #define DEBUG_RANGES_SECTION ".debug_ranges"
3340 #endif
3342 /* Standard ELF section names for compiled code and data. */
3343 #ifndef TEXT_SECTION_NAME
3344 #define TEXT_SECTION_NAME ".text"
3345 #endif
3347 /* Section flags for .debug_macinfo/.debug_macro section. */
3348 #define DEBUG_MACRO_SECTION_FLAGS \
3349 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3351 /* Section flags for .debug_str section. */
3352 #define DEBUG_STR_SECTION_FLAGS \
3353 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3354 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3355 : SECTION_DEBUG)
3357 /* Section flags for .debug_str.dwo section. */
3358 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3360 /* Labels we insert at beginning sections we can reference instead of
3361 the section names themselves. */
3363 #ifndef TEXT_SECTION_LABEL
3364 #define TEXT_SECTION_LABEL "Ltext"
3365 #endif
3366 #ifndef COLD_TEXT_SECTION_LABEL
3367 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3368 #endif
3369 #ifndef DEBUG_LINE_SECTION_LABEL
3370 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3371 #endif
3372 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3373 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3374 #endif
3375 #ifndef DEBUG_INFO_SECTION_LABEL
3376 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3377 #endif
3378 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3379 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3380 #endif
3381 #ifndef DEBUG_ABBREV_SECTION_LABEL
3382 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3383 #endif
3384 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3385 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3386 #endif
3387 #ifndef DEBUG_ADDR_SECTION_LABEL
3388 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3389 #endif
3390 #ifndef DEBUG_LOC_SECTION_LABEL
3391 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3392 #endif
3393 #ifndef DEBUG_RANGES_SECTION_LABEL
3394 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3395 #endif
3396 #ifndef DEBUG_MACINFO_SECTION_LABEL
3397 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3398 #endif
3399 #ifndef DEBUG_MACRO_SECTION_LABEL
3400 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3401 #endif
3402 #define SKELETON_COMP_DIE_ABBREV 1
3403 #define SKELETON_TYPE_DIE_ABBREV 2
3405 /* Definitions of defaults for formats and names of various special
3406 (artificial) labels which may be generated within this file (when the -g
3407 options is used and DWARF2_DEBUGGING_INFO is in effect.
3408 If necessary, these may be overridden from within the tm.h file, but
3409 typically, overriding these defaults is unnecessary. */
3411 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3412 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3413 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3414 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3415 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3416 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3417 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3418 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3419 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3420 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3421 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3422 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3423 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3424 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3426 #ifndef TEXT_END_LABEL
3427 #define TEXT_END_LABEL "Letext"
3428 #endif
3429 #ifndef COLD_END_LABEL
3430 #define COLD_END_LABEL "Letext_cold"
3431 #endif
3432 #ifndef BLOCK_BEGIN_LABEL
3433 #define BLOCK_BEGIN_LABEL "LBB"
3434 #endif
3435 #ifndef BLOCK_END_LABEL
3436 #define BLOCK_END_LABEL "LBE"
3437 #endif
3438 #ifndef LINE_CODE_LABEL
3439 #define LINE_CODE_LABEL "LM"
3440 #endif
3443 /* Return the root of the DIE's built for the current compilation unit. */
3444 static dw_die_ref
3445 comp_unit_die (void)
3447 if (!single_comp_unit_die)
3448 single_comp_unit_die = gen_compile_unit_die (NULL);
3449 return single_comp_unit_die;
3452 /* We allow a language front-end to designate a function that is to be
3453 called to "demangle" any name before it is put into a DIE. */
3455 static const char *(*demangle_name_func) (const char *);
3457 void
3458 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3460 demangle_name_func = func;
3463 /* Test if rtl node points to a pseudo register. */
3465 static inline int
3466 is_pseudo_reg (const_rtx rtl)
3468 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3469 || (GET_CODE (rtl) == SUBREG
3470 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3473 /* Return a reference to a type, with its const and volatile qualifiers
3474 removed. */
3476 static inline tree
3477 type_main_variant (tree type)
3479 type = TYPE_MAIN_VARIANT (type);
3481 /* ??? There really should be only one main variant among any group of
3482 variants of a given type (and all of the MAIN_VARIANT values for all
3483 members of the group should point to that one type) but sometimes the C
3484 front-end messes this up for array types, so we work around that bug
3485 here. */
3486 if (TREE_CODE (type) == ARRAY_TYPE)
3487 while (type != TYPE_MAIN_VARIANT (type))
3488 type = TYPE_MAIN_VARIANT (type);
3490 return type;
3493 /* Return nonzero if the given type node represents a tagged type. */
3495 static inline int
3496 is_tagged_type (const_tree type)
3498 enum tree_code code = TREE_CODE (type);
3500 return (code == RECORD_TYPE || code == UNION_TYPE
3501 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3504 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3506 static void
3507 get_ref_die_offset_label (char *label, dw_die_ref ref)
3509 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3512 /* Return die_offset of a DIE reference to a base type. */
3514 static unsigned long int
3515 get_base_type_offset (dw_die_ref ref)
3517 if (ref->die_offset)
3518 return ref->die_offset;
3519 if (comp_unit_die ()->die_abbrev)
3521 calc_base_type_die_sizes ();
3522 gcc_assert (ref->die_offset);
3524 return ref->die_offset;
3527 /* Return die_offset of a DIE reference other than base type. */
3529 static unsigned long int
3530 get_ref_die_offset (dw_die_ref ref)
3532 gcc_assert (ref->die_offset);
3533 return ref->die_offset;
3536 /* Convert a DIE tag into its string name. */
3538 static const char *
3539 dwarf_tag_name (unsigned int tag)
3541 const char *name = get_DW_TAG_name (tag);
3543 if (name != NULL)
3544 return name;
3546 return "DW_TAG_<unknown>";
3549 /* Convert a DWARF attribute code into its string name. */
3551 static const char *
3552 dwarf_attr_name (unsigned int attr)
3554 const char *name;
3556 switch (attr)
3558 #if VMS_DEBUGGING_INFO
3559 case DW_AT_HP_prologue:
3560 return "DW_AT_HP_prologue";
3561 #else
3562 case DW_AT_MIPS_loop_unroll_factor:
3563 return "DW_AT_MIPS_loop_unroll_factor";
3564 #endif
3566 #if VMS_DEBUGGING_INFO
3567 case DW_AT_HP_epilogue:
3568 return "DW_AT_HP_epilogue";
3569 #else
3570 case DW_AT_MIPS_stride:
3571 return "DW_AT_MIPS_stride";
3572 #endif
3575 name = get_DW_AT_name (attr);
3577 if (name != NULL)
3578 return name;
3580 return "DW_AT_<unknown>";
3583 /* Convert a DWARF value form code into its string name. */
3585 static const char *
3586 dwarf_form_name (unsigned int form)
3588 const char *name = get_DW_FORM_name (form);
3590 if (name != NULL)
3591 return name;
3593 return "DW_FORM_<unknown>";
3596 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3597 instance of an inlined instance of a decl which is local to an inline
3598 function, so we have to trace all of the way back through the origin chain
3599 to find out what sort of node actually served as the original seed for the
3600 given block. */
3602 static tree
3603 decl_ultimate_origin (const_tree decl)
3605 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3606 return NULL_TREE;
3608 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3609 nodes in the function to point to themselves; ignore that if
3610 we're trying to output the abstract instance of this function. */
3611 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3612 return NULL_TREE;
3614 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3615 most distant ancestor, this should never happen. */
3616 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3618 return DECL_ABSTRACT_ORIGIN (decl);
3621 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3622 of a virtual function may refer to a base class, so we check the 'this'
3623 parameter. */
3625 static tree
3626 decl_class_context (tree decl)
3628 tree context = NULL_TREE;
3630 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3631 context = DECL_CONTEXT (decl);
3632 else
3633 context = TYPE_MAIN_VARIANT
3634 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3636 if (context && !TYPE_P (context))
3637 context = NULL_TREE;
3639 return context;
3642 /* Add an attribute/value pair to a DIE. */
3644 static inline void
3645 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3647 /* Maybe this should be an assert? */
3648 if (die == NULL)
3649 return;
3651 vec_safe_reserve (die->die_attr, 1);
3652 vec_safe_push (die->die_attr, *attr);
3655 static inline enum dw_val_class
3656 AT_class (dw_attr_ref a)
3658 return a->dw_attr_val.val_class;
3661 /* Return the index for any attribute that will be referenced with a
3662 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3663 are stored in dw_attr_val.v.val_str for reference counting
3664 pruning. */
3666 static inline unsigned int
3667 AT_index (dw_attr_ref a)
3669 if (AT_class (a) == dw_val_class_str)
3670 return a->dw_attr_val.v.val_str->index;
3671 else if (a->dw_attr_val.val_entry != NULL)
3672 return a->dw_attr_val.val_entry->index;
3673 return NOT_INDEXED;
3676 /* Add a flag value attribute to a DIE. */
3678 static inline void
3679 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3681 dw_attr_node attr;
3683 attr.dw_attr = attr_kind;
3684 attr.dw_attr_val.val_class = dw_val_class_flag;
3685 attr.dw_attr_val.val_entry = NULL;
3686 attr.dw_attr_val.v.val_flag = flag;
3687 add_dwarf_attr (die, &attr);
3690 static inline unsigned
3691 AT_flag (dw_attr_ref a)
3693 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3694 return a->dw_attr_val.v.val_flag;
3697 /* Add a signed integer attribute value to a DIE. */
3699 static inline void
3700 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3702 dw_attr_node attr;
3704 attr.dw_attr = attr_kind;
3705 attr.dw_attr_val.val_class = dw_val_class_const;
3706 attr.dw_attr_val.val_entry = NULL;
3707 attr.dw_attr_val.v.val_int = int_val;
3708 add_dwarf_attr (die, &attr);
3711 static inline HOST_WIDE_INT
3712 AT_int (dw_attr_ref a)
3714 gcc_assert (a && AT_class (a) == dw_val_class_const);
3715 return a->dw_attr_val.v.val_int;
3718 /* Add an unsigned integer attribute value to a DIE. */
3720 static inline void
3721 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3722 unsigned HOST_WIDE_INT unsigned_val)
3724 dw_attr_node attr;
3726 attr.dw_attr = attr_kind;
3727 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3728 attr.dw_attr_val.val_entry = NULL;
3729 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3730 add_dwarf_attr (die, &attr);
3733 static inline unsigned HOST_WIDE_INT
3734 AT_unsigned (dw_attr_ref a)
3736 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3737 return a->dw_attr_val.v.val_unsigned;
3740 /* Add an unsigned double integer attribute value to a DIE. */
3742 static inline void
3743 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3744 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3746 dw_attr_node attr;
3748 attr.dw_attr = attr_kind;
3749 attr.dw_attr_val.val_class = dw_val_class_const_double;
3750 attr.dw_attr_val.val_entry = NULL;
3751 attr.dw_attr_val.v.val_double.high = high;
3752 attr.dw_attr_val.v.val_double.low = low;
3753 add_dwarf_attr (die, &attr);
3756 /* Add a floating point attribute value to a DIE and return it. */
3758 static inline void
3759 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3760 unsigned int length, unsigned int elt_size, unsigned char *array)
3762 dw_attr_node attr;
3764 attr.dw_attr = attr_kind;
3765 attr.dw_attr_val.val_class = dw_val_class_vec;
3766 attr.dw_attr_val.val_entry = NULL;
3767 attr.dw_attr_val.v.val_vec.length = length;
3768 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3769 attr.dw_attr_val.v.val_vec.array = array;
3770 add_dwarf_attr (die, &attr);
3773 /* Add an 8-byte data attribute value to a DIE. */
3775 static inline void
3776 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3777 unsigned char data8[8])
3779 dw_attr_node attr;
3781 attr.dw_attr = attr_kind;
3782 attr.dw_attr_val.val_class = dw_val_class_data8;
3783 attr.dw_attr_val.val_entry = NULL;
3784 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3785 add_dwarf_attr (die, &attr);
3788 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3789 dwarf_split_debug_info, address attributes in dies destined for the
3790 final executable have force_direct set to avoid using indexed
3791 references. */
3793 static inline void
3794 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3795 bool force_direct)
3797 dw_attr_node attr;
3798 char * lbl_id;
3800 lbl_id = xstrdup (lbl_low);
3801 attr.dw_attr = DW_AT_low_pc;
3802 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3803 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3804 if (dwarf_split_debug_info && !force_direct)
3805 attr.dw_attr_val.val_entry
3806 = add_addr_table_entry (lbl_id, ate_kind_label);
3807 else
3808 attr.dw_attr_val.val_entry = NULL;
3809 add_dwarf_attr (die, &attr);
3811 attr.dw_attr = DW_AT_high_pc;
3812 if (dwarf_version < 4)
3813 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3814 else
3815 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3816 lbl_id = xstrdup (lbl_high);
3817 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3818 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3819 && dwarf_split_debug_info && !force_direct)
3820 attr.dw_attr_val.val_entry
3821 = add_addr_table_entry (lbl_id, ate_kind_label);
3822 else
3823 attr.dw_attr_val.val_entry = NULL;
3824 add_dwarf_attr (die, &attr);
3827 /* Hash and equality functions for debug_str_hash. */
3829 static hashval_t
3830 debug_str_do_hash (const void *x)
3832 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3835 static int
3836 debug_str_eq (const void *x1, const void *x2)
3838 return strcmp ((((const struct indirect_string_node *)x1)->str),
3839 (const char *)x2) == 0;
3842 /* Add STR to the given string hash table. */
3844 static struct indirect_string_node *
3845 find_AT_string_in_table (const char *str, htab_t table)
3847 struct indirect_string_node *node;
3848 void **slot;
3850 slot = htab_find_slot_with_hash (table, str,
3851 htab_hash_string (str), INSERT);
3852 if (*slot == NULL)
3854 node = ggc_alloc_cleared_indirect_string_node ();
3855 node->str = ggc_strdup (str);
3856 *slot = node;
3858 else
3859 node = (struct indirect_string_node *) *slot;
3861 node->refcount++;
3862 return node;
3865 /* Add STR to the indirect string hash table. */
3867 static struct indirect_string_node *
3868 find_AT_string (const char *str)
3870 if (! debug_str_hash)
3871 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3872 debug_str_eq, NULL);
3874 return find_AT_string_in_table (str, debug_str_hash);
3877 /* Add a string attribute value to a DIE. */
3879 static inline void
3880 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3882 dw_attr_node attr;
3883 struct indirect_string_node *node;
3885 node = find_AT_string (str);
3887 attr.dw_attr = attr_kind;
3888 attr.dw_attr_val.val_class = dw_val_class_str;
3889 attr.dw_attr_val.val_entry = NULL;
3890 attr.dw_attr_val.v.val_str = node;
3891 add_dwarf_attr (die, &attr);
3894 static inline const char *
3895 AT_string (dw_attr_ref a)
3897 gcc_assert (a && AT_class (a) == dw_val_class_str);
3898 return a->dw_attr_val.v.val_str->str;
3901 /* Call this function directly to bypass AT_string_form's logic to put
3902 the string inline in the die. */
3904 static void
3905 set_indirect_string (struct indirect_string_node *node)
3907 char label[32];
3908 /* Already indirect is a no op. */
3909 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3911 gcc_assert (node->label);
3912 return;
3914 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3915 ++dw2_string_counter;
3916 node->label = xstrdup (label);
3918 if (!dwarf_split_debug_info)
3920 node->form = DW_FORM_strp;
3921 node->index = NOT_INDEXED;
3923 else
3925 node->form = DW_FORM_GNU_str_index;
3926 node->index = NO_INDEX_ASSIGNED;
3930 /* Find out whether a string should be output inline in DIE
3931 or out-of-line in .debug_str section. */
3933 static enum dwarf_form
3934 find_string_form (struct indirect_string_node *node)
3936 unsigned int len;
3938 if (node->form)
3939 return node->form;
3941 len = strlen (node->str) + 1;
3943 /* If the string is shorter or equal to the size of the reference, it is
3944 always better to put it inline. */
3945 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3946 return node->form = DW_FORM_string;
3948 /* If we cannot expect the linker to merge strings in .debug_str
3949 section, only put it into .debug_str if it is worth even in this
3950 single module. */
3951 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3952 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3953 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3954 return node->form = DW_FORM_string;
3956 set_indirect_string (node);
3958 return node->form;
3961 /* Find out whether the string referenced from the attribute should be
3962 output inline in DIE or out-of-line in .debug_str section. */
3964 static enum dwarf_form
3965 AT_string_form (dw_attr_ref a)
3967 gcc_assert (a && AT_class (a) == dw_val_class_str);
3968 return find_string_form (a->dw_attr_val.v.val_str);
3971 /* Add a DIE reference attribute value to a DIE. */
3973 static inline void
3974 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3976 dw_attr_node attr;
3978 #ifdef ENABLE_CHECKING
3979 gcc_assert (targ_die != NULL);
3980 #else
3981 /* With LTO we can end up trying to reference something we didn't create
3982 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3983 if (targ_die == NULL)
3984 return;
3985 #endif
3987 attr.dw_attr = attr_kind;
3988 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3989 attr.dw_attr_val.val_entry = NULL;
3990 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3991 attr.dw_attr_val.v.val_die_ref.external = 0;
3992 add_dwarf_attr (die, &attr);
3995 /* Change DIE reference REF to point to NEW_DIE instead. */
3997 static inline void
3998 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4000 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4001 ref->dw_attr_val.v.val_die_ref.die = new_die;
4002 ref->dw_attr_val.v.val_die_ref.external = 0;
4005 /* Add an AT_specification attribute to a DIE, and also make the back
4006 pointer from the specification to the definition. */
4008 static inline void
4009 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4011 add_AT_die_ref (die, DW_AT_specification, targ_die);
4012 gcc_assert (!targ_die->die_definition);
4013 targ_die->die_definition = die;
4016 static inline dw_die_ref
4017 AT_ref (dw_attr_ref a)
4019 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4020 return a->dw_attr_val.v.val_die_ref.die;
4023 static inline int
4024 AT_ref_external (dw_attr_ref a)
4026 if (a && AT_class (a) == dw_val_class_die_ref)
4027 return a->dw_attr_val.v.val_die_ref.external;
4029 return 0;
4032 static inline void
4033 set_AT_ref_external (dw_attr_ref a, int i)
4035 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4036 a->dw_attr_val.v.val_die_ref.external = i;
4039 /* Add an FDE reference attribute value to a DIE. */
4041 static inline void
4042 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4044 dw_attr_node attr;
4046 attr.dw_attr = attr_kind;
4047 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4048 attr.dw_attr_val.val_entry = NULL;
4049 attr.dw_attr_val.v.val_fde_index = targ_fde;
4050 add_dwarf_attr (die, &attr);
4053 /* Add a location description attribute value to a DIE. */
4055 static inline void
4056 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4058 dw_attr_node attr;
4060 attr.dw_attr = attr_kind;
4061 attr.dw_attr_val.val_class = dw_val_class_loc;
4062 attr.dw_attr_val.val_entry = NULL;
4063 attr.dw_attr_val.v.val_loc = loc;
4064 add_dwarf_attr (die, &attr);
4067 static inline dw_loc_descr_ref
4068 AT_loc (dw_attr_ref a)
4070 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4071 return a->dw_attr_val.v.val_loc;
4074 static inline void
4075 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4077 dw_attr_node attr;
4079 attr.dw_attr = attr_kind;
4080 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4081 attr.dw_attr_val.val_entry = NULL;
4082 attr.dw_attr_val.v.val_loc_list = loc_list;
4083 add_dwarf_attr (die, &attr);
4084 have_location_lists = true;
4087 static inline dw_loc_list_ref
4088 AT_loc_list (dw_attr_ref a)
4090 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4091 return a->dw_attr_val.v.val_loc_list;
4094 static inline dw_loc_list_ref *
4095 AT_loc_list_ptr (dw_attr_ref a)
4097 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4098 return &a->dw_attr_val.v.val_loc_list;
4101 /* Table of entries into the .debug_addr section. */
4103 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4105 /* Hash an address_table_entry. */
4107 static hashval_t
4108 addr_table_entry_do_hash (const void *x)
4110 const addr_table_entry *a = (const addr_table_entry *) x;
4111 switch (a->kind)
4113 case ate_kind_rtx:
4114 return iterative_hash_rtx (a->addr.rtl, 0);
4115 case ate_kind_rtx_dtprel:
4116 return iterative_hash_rtx (a->addr.rtl, 1);
4117 case ate_kind_label:
4118 return htab_hash_string (a->addr.label);
4119 default:
4120 gcc_unreachable ();
4124 /* Determine equality for two address_table_entries. */
4126 static int
4127 addr_table_entry_eq (const void *x1, const void *x2)
4129 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4130 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4132 if (a1->kind != a2->kind)
4133 return 0;
4134 switch (a1->kind)
4136 case ate_kind_rtx:
4137 case ate_kind_rtx_dtprel:
4138 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4139 case ate_kind_label:
4140 return strcmp (a1->addr.label, a2->addr.label) == 0;
4141 default:
4142 gcc_unreachable ();
4146 /* Initialize an addr_table_entry. */
4148 void
4149 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4151 e->kind = kind;
4152 switch (kind)
4154 case ate_kind_rtx:
4155 case ate_kind_rtx_dtprel:
4156 e->addr.rtl = (rtx) addr;
4157 break;
4158 case ate_kind_label:
4159 e->addr.label = (char *) addr;
4160 break;
4162 e->refcount = 0;
4163 e->index = NO_INDEX_ASSIGNED;
4166 /* Add attr to the address table entry to the table. Defer setting an
4167 index until output time. */
4169 static addr_table_entry *
4170 add_addr_table_entry (void *addr, enum ate_kind kind)
4172 addr_table_entry *node;
4173 addr_table_entry finder;
4174 void **slot;
4176 gcc_assert (dwarf_split_debug_info);
4177 if (! addr_index_table)
4178 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4179 addr_table_entry_eq, NULL);
4180 init_addr_table_entry (&finder, kind, addr);
4181 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4183 if (*slot == HTAB_EMPTY_ENTRY)
4185 node = ggc_alloc_cleared_addr_table_entry ();
4186 init_addr_table_entry (node, kind, addr);
4187 *slot = node;
4189 else
4190 node = (addr_table_entry *) *slot;
4192 node->refcount++;
4193 return node;
4196 /* Remove an entry from the addr table by decrementing its refcount.
4197 Strictly, decrementing the refcount would be enough, but the
4198 assertion that the entry is actually in the table has found
4199 bugs. */
4201 static void
4202 remove_addr_table_entry (addr_table_entry *entry)
4204 addr_table_entry *node;
4206 gcc_assert (dwarf_split_debug_info && addr_index_table);
4207 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4208 /* After an index is assigned, the table is frozen. */
4209 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4210 node->refcount--;
4213 /* Given a location list, remove all addresses it refers to from the
4214 address_table. */
4216 static void
4217 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4219 for (; descr; descr = descr->dw_loc_next)
4220 if (descr->dw_loc_oprnd1.val_entry != NULL)
4222 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4223 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4227 /* A helper function for dwarf2out_finish called through
4228 htab_traverse. Assign an addr_table_entry its index. All entries
4229 must be collected into the table when this function is called,
4230 because the indexing code relies on htab_traverse to traverse nodes
4231 in the same order for each run. */
4233 static int
4234 index_addr_table_entry (void **h, void *v)
4236 addr_table_entry *node = (addr_table_entry *) *h;
4237 unsigned int *index = (unsigned int *) v;
4239 /* Don't index unreferenced nodes. */
4240 if (node->refcount == 0)
4241 return 1;
4243 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4244 node->index = *index;
4245 *index += 1;
4247 return 1;
4250 /* Add an address constant attribute value to a DIE. When using
4251 dwarf_split_debug_info, address attributes in dies destined for the
4252 final executable should be direct references--setting the parameter
4253 force_direct ensures this behavior. */
4255 static inline void
4256 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4257 bool force_direct)
4259 dw_attr_node attr;
4261 attr.dw_attr = attr_kind;
4262 attr.dw_attr_val.val_class = dw_val_class_addr;
4263 attr.dw_attr_val.v.val_addr = addr;
4264 if (dwarf_split_debug_info && !force_direct)
4265 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4266 else
4267 attr.dw_attr_val.val_entry = NULL;
4268 add_dwarf_attr (die, &attr);
4271 /* Get the RTX from to an address DIE attribute. */
4273 static inline rtx
4274 AT_addr (dw_attr_ref a)
4276 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4277 return a->dw_attr_val.v.val_addr;
4280 /* Add a file attribute value to a DIE. */
4282 static inline void
4283 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4284 struct dwarf_file_data *fd)
4286 dw_attr_node attr;
4288 attr.dw_attr = attr_kind;
4289 attr.dw_attr_val.val_class = dw_val_class_file;
4290 attr.dw_attr_val.val_entry = NULL;
4291 attr.dw_attr_val.v.val_file = fd;
4292 add_dwarf_attr (die, &attr);
4295 /* Get the dwarf_file_data from a file DIE attribute. */
4297 static inline struct dwarf_file_data *
4298 AT_file (dw_attr_ref a)
4300 gcc_assert (a && AT_class (a) == dw_val_class_file);
4301 return a->dw_attr_val.v.val_file;
4304 /* Add a vms delta attribute value to a DIE. */
4306 static inline void
4307 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4308 const char *lbl1, const char *lbl2)
4310 dw_attr_node attr;
4312 attr.dw_attr = attr_kind;
4313 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4314 attr.dw_attr_val.val_entry = NULL;
4315 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4316 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4317 add_dwarf_attr (die, &attr);
4320 /* Add a label identifier attribute value to a DIE. */
4322 static inline void
4323 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4324 const char *lbl_id)
4326 dw_attr_node attr;
4328 attr.dw_attr = attr_kind;
4329 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4330 attr.dw_attr_val.val_entry = NULL;
4331 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4332 if (dwarf_split_debug_info)
4333 attr.dw_attr_val.val_entry
4334 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4335 ate_kind_label);
4336 add_dwarf_attr (die, &attr);
4339 /* Add a section offset attribute value to a DIE, an offset into the
4340 debug_line section. */
4342 static inline void
4343 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4344 const char *label)
4346 dw_attr_node attr;
4348 attr.dw_attr = attr_kind;
4349 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4350 attr.dw_attr_val.val_entry = NULL;
4351 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4352 add_dwarf_attr (die, &attr);
4355 /* Add a section offset attribute value to a DIE, an offset into the
4356 debug_macinfo section. */
4358 static inline void
4359 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4360 const char *label)
4362 dw_attr_node attr;
4364 attr.dw_attr = attr_kind;
4365 attr.dw_attr_val.val_class = dw_val_class_macptr;
4366 attr.dw_attr_val.val_entry = NULL;
4367 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4368 add_dwarf_attr (die, &attr);
4371 /* Add an offset attribute value to a DIE. */
4373 static inline void
4374 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4375 unsigned HOST_WIDE_INT offset)
4377 dw_attr_node attr;
4379 attr.dw_attr = attr_kind;
4380 attr.dw_attr_val.val_class = dw_val_class_offset;
4381 attr.dw_attr_val.val_entry = NULL;
4382 attr.dw_attr_val.v.val_offset = offset;
4383 add_dwarf_attr (die, &attr);
4386 /* Add a range_list attribute value to a DIE. When using
4387 dwarf_split_debug_info, address attributes in dies destined for the
4388 final executable should be direct references--setting the parameter
4389 force_direct ensures this behavior. */
4391 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4392 #define RELOCATED_OFFSET (NULL)
4394 static void
4395 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4396 long unsigned int offset, bool force_direct)
4398 dw_attr_node attr;
4400 attr.dw_attr = attr_kind;
4401 attr.dw_attr_val.val_class = dw_val_class_range_list;
4402 /* For the range_list attribute, use val_entry to store whether the
4403 offset should follow split-debug-info or normal semantics. This
4404 value is read in output_range_list_offset. */
4405 if (dwarf_split_debug_info && !force_direct)
4406 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4407 else
4408 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4409 attr.dw_attr_val.v.val_offset = offset;
4410 add_dwarf_attr (die, &attr);
4413 /* Return the start label of a delta attribute. */
4415 static inline const char *
4416 AT_vms_delta1 (dw_attr_ref a)
4418 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4419 return a->dw_attr_val.v.val_vms_delta.lbl1;
4422 /* Return the end label of a delta attribute. */
4424 static inline const char *
4425 AT_vms_delta2 (dw_attr_ref a)
4427 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4428 return a->dw_attr_val.v.val_vms_delta.lbl2;
4431 static inline const char *
4432 AT_lbl (dw_attr_ref a)
4434 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4435 || AT_class (a) == dw_val_class_lineptr
4436 || AT_class (a) == dw_val_class_macptr
4437 || AT_class (a) == dw_val_class_high_pc));
4438 return a->dw_attr_val.v.val_lbl_id;
4441 /* Get the attribute of type attr_kind. */
4443 static dw_attr_ref
4444 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4446 dw_attr_ref a;
4447 unsigned ix;
4448 dw_die_ref spec = NULL;
4450 if (! die)
4451 return NULL;
4453 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4454 if (a->dw_attr == attr_kind)
4455 return a;
4456 else if (a->dw_attr == DW_AT_specification
4457 || a->dw_attr == DW_AT_abstract_origin)
4458 spec = AT_ref (a);
4460 if (spec)
4461 return get_AT (spec, attr_kind);
4463 return NULL;
4466 /* Returns the parent of the declaration of DIE. */
4468 static dw_die_ref
4469 get_die_parent (dw_die_ref die)
4471 dw_die_ref t;
4473 if (!die)
4474 return NULL;
4476 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4477 || (t = get_AT_ref (die, DW_AT_specification)))
4478 die = t;
4480 return die->die_parent;
4483 /* Return the "low pc" attribute value, typically associated with a subprogram
4484 DIE. Return null if the "low pc" attribute is either not present, or if it
4485 cannot be represented as an assembler label identifier. */
4487 static inline const char *
4488 get_AT_low_pc (dw_die_ref die)
4490 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4492 return a ? AT_lbl (a) : NULL;
4495 /* Return the "high pc" attribute value, typically associated with a subprogram
4496 DIE. Return null if the "high pc" attribute is either not present, or if it
4497 cannot be represented as an assembler label identifier. */
4499 static inline const char *
4500 get_AT_hi_pc (dw_die_ref die)
4502 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4504 return a ? AT_lbl (a) : NULL;
4507 /* Return the value of the string attribute designated by ATTR_KIND, or
4508 NULL if it is not present. */
4510 static inline const char *
4511 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4513 dw_attr_ref a = get_AT (die, attr_kind);
4515 return a ? AT_string (a) : NULL;
4518 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4519 if it is not present. */
4521 static inline int
4522 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4524 dw_attr_ref a = get_AT (die, attr_kind);
4526 return a ? AT_flag (a) : 0;
4529 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4530 if it is not present. */
4532 static inline unsigned
4533 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4535 dw_attr_ref a = get_AT (die, attr_kind);
4537 return a ? AT_unsigned (a) : 0;
4540 static inline dw_die_ref
4541 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4543 dw_attr_ref a = get_AT (die, attr_kind);
4545 return a ? AT_ref (a) : NULL;
4548 static inline struct dwarf_file_data *
4549 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4551 dw_attr_ref a = get_AT (die, attr_kind);
4553 return a ? AT_file (a) : NULL;
4556 /* Return TRUE if the language is C++. */
4558 static inline bool
4559 is_cxx (void)
4561 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4563 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4566 /* Return TRUE if the language is Fortran. */
4568 static inline bool
4569 is_fortran (void)
4571 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4573 return (lang == DW_LANG_Fortran77
4574 || lang == DW_LANG_Fortran90
4575 || lang == DW_LANG_Fortran95);
4578 /* Return TRUE if the language is Ada. */
4580 static inline bool
4581 is_ada (void)
4583 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4585 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4588 /* Remove the specified attribute if present. */
4590 static void
4591 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4593 dw_attr_ref a;
4594 unsigned ix;
4596 if (! die)
4597 return;
4599 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4600 if (a->dw_attr == attr_kind)
4602 if (AT_class (a) == dw_val_class_str)
4603 if (a->dw_attr_val.v.val_str->refcount)
4604 a->dw_attr_val.v.val_str->refcount--;
4606 /* vec::ordered_remove should help reduce the number of abbrevs
4607 that are needed. */
4608 die->die_attr->ordered_remove (ix);
4609 return;
4613 /* Remove CHILD from its parent. PREV must have the property that
4614 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4616 static void
4617 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4619 gcc_assert (child->die_parent == prev->die_parent);
4620 gcc_assert (prev->die_sib == child);
4621 if (prev == child)
4623 gcc_assert (child->die_parent->die_child == child);
4624 prev = NULL;
4626 else
4627 prev->die_sib = child->die_sib;
4628 if (child->die_parent->die_child == child)
4629 child->die_parent->die_child = prev;
4632 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4633 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4635 static void
4636 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4638 dw_die_ref parent = old_child->die_parent;
4640 gcc_assert (parent == prev->die_parent);
4641 gcc_assert (prev->die_sib == old_child);
4643 new_child->die_parent = parent;
4644 if (prev == old_child)
4646 gcc_assert (parent->die_child == old_child);
4647 new_child->die_sib = new_child;
4649 else
4651 prev->die_sib = new_child;
4652 new_child->die_sib = old_child->die_sib;
4654 if (old_child->die_parent->die_child == old_child)
4655 old_child->die_parent->die_child = new_child;
4658 /* Move all children from OLD_PARENT to NEW_PARENT. */
4660 static void
4661 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4663 dw_die_ref c;
4664 new_parent->die_child = old_parent->die_child;
4665 old_parent->die_child = NULL;
4666 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4669 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4670 matches TAG. */
4672 static void
4673 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4675 dw_die_ref c;
4677 c = die->die_child;
4678 if (c) do {
4679 dw_die_ref prev = c;
4680 c = c->die_sib;
4681 while (c->die_tag == tag)
4683 remove_child_with_prev (c, prev);
4684 /* Might have removed every child. */
4685 if (c == c->die_sib)
4686 return;
4687 c = c->die_sib;
4689 } while (c != die->die_child);
4692 /* Add a CHILD_DIE as the last child of DIE. */
4694 static void
4695 add_child_die (dw_die_ref die, dw_die_ref child_die)
4697 /* FIXME this should probably be an assert. */
4698 if (! die || ! child_die)
4699 return;
4700 gcc_assert (die != child_die);
4702 child_die->die_parent = die;
4703 if (die->die_child)
4705 child_die->die_sib = die->die_child->die_sib;
4706 die->die_child->die_sib = child_die;
4708 else
4709 child_die->die_sib = child_die;
4710 die->die_child = child_die;
4713 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4714 is the specification, to the end of PARENT's list of children.
4715 This is done by removing and re-adding it. */
4717 static void
4718 splice_child_die (dw_die_ref parent, dw_die_ref child)
4720 dw_die_ref p;
4722 /* We want the declaration DIE from inside the class, not the
4723 specification DIE at toplevel. */
4724 if (child->die_parent != parent)
4726 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4728 if (tmp)
4729 child = tmp;
4732 gcc_assert (child->die_parent == parent
4733 || (child->die_parent
4734 == get_AT_ref (parent, DW_AT_specification)));
4736 for (p = child->die_parent->die_child; ; p = p->die_sib)
4737 if (p->die_sib == child)
4739 remove_child_with_prev (child, p);
4740 break;
4743 add_child_die (parent, child);
4746 /* Return a pointer to a newly created DIE node. */
4748 static inline dw_die_ref
4749 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4751 dw_die_ref die = ggc_alloc_cleared_die_node ();
4753 die->die_tag = tag_value;
4755 if (parent_die != NULL)
4756 add_child_die (parent_die, die);
4757 else
4759 limbo_die_node *limbo_node;
4761 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4762 limbo_node->die = die;
4763 limbo_node->created_for = t;
4764 limbo_node->next = limbo_die_list;
4765 limbo_die_list = limbo_node;
4768 return die;
4771 /* Return the DIE associated with the given type specifier. */
4773 static inline dw_die_ref
4774 lookup_type_die (tree type)
4776 return TYPE_SYMTAB_DIE (type);
4779 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4780 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4781 anonymous type instead the one of the naming typedef. */
4783 static inline dw_die_ref
4784 strip_naming_typedef (tree type, dw_die_ref type_die)
4786 if (type
4787 && TREE_CODE (type) == RECORD_TYPE
4788 && type_die
4789 && type_die->die_tag == DW_TAG_typedef
4790 && is_naming_typedef_decl (TYPE_NAME (type)))
4791 type_die = get_AT_ref (type_die, DW_AT_type);
4792 return type_die;
4795 /* Like lookup_type_die, but if type is an anonymous type named by a
4796 typedef[1], return the DIE of the anonymous type instead the one of
4797 the naming typedef. This is because in gen_typedef_die, we did
4798 equate the anonymous struct named by the typedef with the DIE of
4799 the naming typedef. So by default, lookup_type_die on an anonymous
4800 struct yields the DIE of the naming typedef.
4802 [1]: Read the comment of is_naming_typedef_decl to learn about what
4803 a naming typedef is. */
4805 static inline dw_die_ref
4806 lookup_type_die_strip_naming_typedef (tree type)
4808 dw_die_ref die = lookup_type_die (type);
4809 return strip_naming_typedef (type, die);
4812 /* Equate a DIE to a given type specifier. */
4814 static inline void
4815 equate_type_number_to_die (tree type, dw_die_ref type_die)
4817 TYPE_SYMTAB_DIE (type) = type_die;
4820 /* Returns a hash value for X (which really is a die_struct). */
4822 static hashval_t
4823 decl_die_table_hash (const void *x)
4825 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4828 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4830 static int
4831 decl_die_table_eq (const void *x, const void *y)
4833 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4836 /* Return the DIE associated with a given declaration. */
4838 static inline dw_die_ref
4839 lookup_decl_die (tree decl)
4841 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4844 /* Returns a hash value for X (which really is a var_loc_list). */
4846 static hashval_t
4847 decl_loc_table_hash (const void *x)
4849 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4852 /* Return nonzero if decl_id of var_loc_list X is the same as
4853 UID of decl *Y. */
4855 static int
4856 decl_loc_table_eq (const void *x, const void *y)
4858 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4861 /* Return the var_loc list associated with a given declaration. */
4863 static inline var_loc_list *
4864 lookup_decl_loc (const_tree decl)
4866 if (!decl_loc_table)
4867 return NULL;
4868 return (var_loc_list *)
4869 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4872 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4874 static hashval_t
4875 cached_dw_loc_list_table_hash (const void *x)
4877 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4880 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4881 UID of decl *Y. */
4883 static int
4884 cached_dw_loc_list_table_eq (const void *x, const void *y)
4886 return (((const cached_dw_loc_list *) x)->decl_id
4887 == DECL_UID ((const_tree) y));
4890 /* Equate a DIE to a particular declaration. */
4892 static void
4893 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4895 unsigned int decl_id = DECL_UID (decl);
4896 void **slot;
4898 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4899 *slot = decl_die;
4900 decl_die->decl_id = decl_id;
4903 /* Return how many bits covers PIECE EXPR_LIST. */
4905 static int
4906 decl_piece_bitsize (rtx piece)
4908 int ret = (int) GET_MODE (piece);
4909 if (ret)
4910 return ret;
4911 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4912 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4913 return INTVAL (XEXP (XEXP (piece, 0), 0));
4916 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4918 static rtx *
4919 decl_piece_varloc_ptr (rtx piece)
4921 if ((int) GET_MODE (piece))
4922 return &XEXP (piece, 0);
4923 else
4924 return &XEXP (XEXP (piece, 0), 1);
4927 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4928 Next is the chain of following piece nodes. */
4930 static rtx
4931 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4933 if (bitsize <= (int) MAX_MACHINE_MODE)
4934 return alloc_EXPR_LIST (bitsize, loc_note, next);
4935 else
4936 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4937 GEN_INT (bitsize),
4938 loc_note), next);
4941 /* Return rtx that should be stored into loc field for
4942 LOC_NOTE and BITPOS/BITSIZE. */
4944 static rtx
4945 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4946 HOST_WIDE_INT bitsize)
4948 if (bitsize != -1)
4950 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4951 if (bitpos != 0)
4952 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4954 return loc_note;
4957 /* This function either modifies location piece list *DEST in
4958 place (if SRC and INNER is NULL), or copies location piece list
4959 *SRC to *DEST while modifying it. Location BITPOS is modified
4960 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4961 not copied and if needed some padding around it is added.
4962 When modifying in place, DEST should point to EXPR_LIST where
4963 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4964 to the start of the whole list and INNER points to the EXPR_LIST
4965 where earlier pieces cover PIECE_BITPOS bits. */
4967 static void
4968 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4969 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4970 HOST_WIDE_INT bitsize, rtx loc_note)
4972 int diff;
4973 bool copy = inner != NULL;
4975 if (copy)
4977 /* First copy all nodes preceding the current bitpos. */
4978 while (src != inner)
4980 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4981 decl_piece_bitsize (*src), NULL_RTX);
4982 dest = &XEXP (*dest, 1);
4983 src = &XEXP (*src, 1);
4986 /* Add padding if needed. */
4987 if (bitpos != piece_bitpos)
4989 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4990 copy ? NULL_RTX : *dest);
4991 dest = &XEXP (*dest, 1);
4993 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4995 gcc_assert (!copy);
4996 /* A piece with correct bitpos and bitsize already exist,
4997 just update the location for it and return. */
4998 *decl_piece_varloc_ptr (*dest) = loc_note;
4999 return;
5001 /* Add the piece that changed. */
5002 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5003 dest = &XEXP (*dest, 1);
5004 /* Skip over pieces that overlap it. */
5005 diff = bitpos - piece_bitpos + bitsize;
5006 if (!copy)
5007 src = dest;
5008 while (diff > 0 && *src)
5010 rtx piece = *src;
5011 diff -= decl_piece_bitsize (piece);
5012 if (copy)
5013 src = &XEXP (piece, 1);
5014 else
5016 *src = XEXP (piece, 1);
5017 free_EXPR_LIST_node (piece);
5020 /* Add padding if needed. */
5021 if (diff < 0 && *src)
5023 if (!copy)
5024 dest = src;
5025 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5026 dest = &XEXP (*dest, 1);
5028 if (!copy)
5029 return;
5030 /* Finally copy all nodes following it. */
5031 while (*src)
5033 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5034 decl_piece_bitsize (*src), NULL_RTX);
5035 dest = &XEXP (*dest, 1);
5036 src = &XEXP (*src, 1);
5040 /* Add a variable location node to the linked list for DECL. */
5042 static struct var_loc_node *
5043 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5045 unsigned int decl_id;
5046 var_loc_list *temp;
5047 void **slot;
5048 struct var_loc_node *loc = NULL;
5049 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5051 if (TREE_CODE (decl) == VAR_DECL
5052 && DECL_HAS_DEBUG_EXPR_P (decl))
5054 tree realdecl = DECL_DEBUG_EXPR (decl);
5055 if (handled_component_p (realdecl)
5056 || (TREE_CODE (realdecl) == MEM_REF
5057 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5059 HOST_WIDE_INT maxsize;
5060 tree innerdecl;
5061 innerdecl
5062 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5063 if (!DECL_P (innerdecl)
5064 || DECL_IGNORED_P (innerdecl)
5065 || TREE_STATIC (innerdecl)
5066 || bitsize <= 0
5067 || bitpos + bitsize > 256
5068 || bitsize != maxsize)
5069 return NULL;
5070 decl = innerdecl;
5074 decl_id = DECL_UID (decl);
5075 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5076 if (*slot == NULL)
5078 temp = ggc_alloc_cleared_var_loc_list ();
5079 temp->decl_id = decl_id;
5080 *slot = temp;
5082 else
5083 temp = (var_loc_list *) *slot;
5085 /* For PARM_DECLs try to keep around the original incoming value,
5086 even if that means we'll emit a zero-range .debug_loc entry. */
5087 if (temp->last
5088 && temp->first == temp->last
5089 && TREE_CODE (decl) == PARM_DECL
5090 && NOTE_P (temp->first->loc)
5091 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5092 && DECL_INCOMING_RTL (decl)
5093 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5094 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5095 == GET_CODE (DECL_INCOMING_RTL (decl))
5096 && prev_real_insn (temp->first->loc) == NULL_RTX
5097 && (bitsize != -1
5098 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5099 NOTE_VAR_LOCATION_LOC (loc_note))
5100 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5101 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5103 loc = ggc_alloc_cleared_var_loc_node ();
5104 temp->first->next = loc;
5105 temp->last = loc;
5106 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5108 else if (temp->last)
5110 struct var_loc_node *last = temp->last, *unused = NULL;
5111 rtx *piece_loc = NULL, last_loc_note;
5112 int piece_bitpos = 0;
5113 if (last->next)
5115 last = last->next;
5116 gcc_assert (last->next == NULL);
5118 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5120 piece_loc = &last->loc;
5123 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5124 if (piece_bitpos + cur_bitsize > bitpos)
5125 break;
5126 piece_bitpos += cur_bitsize;
5127 piece_loc = &XEXP (*piece_loc, 1);
5129 while (*piece_loc);
5131 /* TEMP->LAST here is either pointer to the last but one or
5132 last element in the chained list, LAST is pointer to the
5133 last element. */
5134 if (label && strcmp (last->label, label) == 0)
5136 /* For SRA optimized variables if there weren't any real
5137 insns since last note, just modify the last node. */
5138 if (piece_loc != NULL)
5140 adjust_piece_list (piece_loc, NULL, NULL,
5141 bitpos, piece_bitpos, bitsize, loc_note);
5142 return NULL;
5144 /* If the last note doesn't cover any instructions, remove it. */
5145 if (temp->last != last)
5147 temp->last->next = NULL;
5148 unused = last;
5149 last = temp->last;
5150 gcc_assert (strcmp (last->label, label) != 0);
5152 else
5154 gcc_assert (temp->first == temp->last
5155 || (temp->first->next == temp->last
5156 && TREE_CODE (decl) == PARM_DECL));
5157 memset (temp->last, '\0', sizeof (*temp->last));
5158 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5159 return temp->last;
5162 if (bitsize == -1 && NOTE_P (last->loc))
5163 last_loc_note = last->loc;
5164 else if (piece_loc != NULL
5165 && *piece_loc != NULL_RTX
5166 && piece_bitpos == bitpos
5167 && decl_piece_bitsize (*piece_loc) == bitsize)
5168 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5169 else
5170 last_loc_note = NULL_RTX;
5171 /* If the current location is the same as the end of the list,
5172 and either both or neither of the locations is uninitialized,
5173 we have nothing to do. */
5174 if (last_loc_note == NULL_RTX
5175 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5176 NOTE_VAR_LOCATION_LOC (loc_note)))
5177 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5178 != NOTE_VAR_LOCATION_STATUS (loc_note))
5179 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5180 == VAR_INIT_STATUS_UNINITIALIZED)
5181 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5182 == VAR_INIT_STATUS_UNINITIALIZED))))
5184 /* Add LOC to the end of list and update LAST. If the last
5185 element of the list has been removed above, reuse its
5186 memory for the new node, otherwise allocate a new one. */
5187 if (unused)
5189 loc = unused;
5190 memset (loc, '\0', sizeof (*loc));
5192 else
5193 loc = ggc_alloc_cleared_var_loc_node ();
5194 if (bitsize == -1 || piece_loc == NULL)
5195 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5196 else
5197 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5198 bitpos, piece_bitpos, bitsize, loc_note);
5199 last->next = loc;
5200 /* Ensure TEMP->LAST will point either to the new last but one
5201 element of the chain, or to the last element in it. */
5202 if (last != temp->last)
5203 temp->last = last;
5205 else if (unused)
5206 ggc_free (unused);
5208 else
5210 loc = ggc_alloc_cleared_var_loc_node ();
5211 temp->first = loc;
5212 temp->last = loc;
5213 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5215 return loc;
5218 /* Keep track of the number of spaces used to indent the
5219 output of the debugging routines that print the structure of
5220 the DIE internal representation. */
5221 static int print_indent;
5223 /* Indent the line the number of spaces given by print_indent. */
5225 static inline void
5226 print_spaces (FILE *outfile)
5228 fprintf (outfile, "%*s", print_indent, "");
5231 /* Print a type signature in hex. */
5233 static inline void
5234 print_signature (FILE *outfile, char *sig)
5236 int i;
5238 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5239 fprintf (outfile, "%02x", sig[i] & 0xff);
5242 /* Print the information associated with a given DIE, and its children.
5243 This routine is a debugging aid only. */
5245 static void
5246 print_die (dw_die_ref die, FILE *outfile)
5248 dw_attr_ref a;
5249 dw_die_ref c;
5250 unsigned ix;
5252 print_spaces (outfile);
5253 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5254 die->die_offset, dwarf_tag_name (die->die_tag),
5255 (void*) die);
5256 print_spaces (outfile);
5257 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5258 fprintf (outfile, " offset: %ld", die->die_offset);
5259 fprintf (outfile, " mark: %d\n", die->die_mark);
5261 if (die->comdat_type_p)
5263 print_spaces (outfile);
5264 fprintf (outfile, " signature: ");
5265 print_signature (outfile, die->die_id.die_type_node->signature);
5266 fprintf (outfile, "\n");
5269 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5271 print_spaces (outfile);
5272 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5274 switch (AT_class (a))
5276 case dw_val_class_addr:
5277 fprintf (outfile, "address");
5278 break;
5279 case dw_val_class_offset:
5280 fprintf (outfile, "offset");
5281 break;
5282 case dw_val_class_loc:
5283 fprintf (outfile, "location descriptor");
5284 break;
5285 case dw_val_class_loc_list:
5286 fprintf (outfile, "location list -> label:%s",
5287 AT_loc_list (a)->ll_symbol);
5288 break;
5289 case dw_val_class_range_list:
5290 fprintf (outfile, "range list");
5291 break;
5292 case dw_val_class_const:
5293 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5294 break;
5295 case dw_val_class_unsigned_const:
5296 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5297 break;
5298 case dw_val_class_const_double:
5299 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5300 HOST_WIDE_INT_PRINT_UNSIGNED")",
5301 a->dw_attr_val.v.val_double.high,
5302 a->dw_attr_val.v.val_double.low);
5303 break;
5304 case dw_val_class_vec:
5305 fprintf (outfile, "floating-point or vector constant");
5306 break;
5307 case dw_val_class_flag:
5308 fprintf (outfile, "%u", AT_flag (a));
5309 break;
5310 case dw_val_class_die_ref:
5311 if (AT_ref (a) != NULL)
5313 if (AT_ref (a)->comdat_type_p)
5315 fprintf (outfile, "die -> signature: ");
5316 print_signature (outfile,
5317 AT_ref (a)->die_id.die_type_node->signature);
5319 else if (AT_ref (a)->die_id.die_symbol)
5320 fprintf (outfile, "die -> label: %s",
5321 AT_ref (a)->die_id.die_symbol);
5322 else
5323 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5324 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5326 else
5327 fprintf (outfile, "die -> <null>");
5328 break;
5329 case dw_val_class_vms_delta:
5330 fprintf (outfile, "delta: @slotcount(%s-%s)",
5331 AT_vms_delta2 (a), AT_vms_delta1 (a));
5332 break;
5333 case dw_val_class_lbl_id:
5334 case dw_val_class_lineptr:
5335 case dw_val_class_macptr:
5336 case dw_val_class_high_pc:
5337 fprintf (outfile, "label: %s", AT_lbl (a));
5338 break;
5339 case dw_val_class_str:
5340 if (AT_string (a) != NULL)
5341 fprintf (outfile, "\"%s\"", AT_string (a));
5342 else
5343 fprintf (outfile, "<null>");
5344 break;
5345 case dw_val_class_file:
5346 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5347 AT_file (a)->emitted_number);
5348 break;
5349 case dw_val_class_data8:
5351 int i;
5353 for (i = 0; i < 8; i++)
5354 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5355 break;
5357 default:
5358 break;
5361 fprintf (outfile, "\n");
5364 if (die->die_child != NULL)
5366 print_indent += 4;
5367 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5368 print_indent -= 4;
5370 if (print_indent == 0)
5371 fprintf (outfile, "\n");
5374 /* Print the information collected for a given DIE. */
5376 DEBUG_FUNCTION void
5377 debug_dwarf_die (dw_die_ref die)
5379 print_die (die, stderr);
5382 DEBUG_FUNCTION void
5383 debug (die_struct &ref)
5385 print_die (&ref, stderr);
5388 DEBUG_FUNCTION void
5389 debug (die_struct *ptr)
5391 if (ptr)
5392 debug (*ptr);
5393 else
5394 fprintf (stderr, "<nil>\n");
5398 /* Print all DWARF information collected for the compilation unit.
5399 This routine is a debugging aid only. */
5401 DEBUG_FUNCTION void
5402 debug_dwarf (void)
5404 print_indent = 0;
5405 print_die (comp_unit_die (), stderr);
5408 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5409 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5410 DIE that marks the start of the DIEs for this include file. */
5412 static dw_die_ref
5413 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5415 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5416 dw_die_ref new_unit = gen_compile_unit_die (filename);
5418 new_unit->die_sib = old_unit;
5419 return new_unit;
5422 /* Close an include-file CU and reopen the enclosing one. */
5424 static dw_die_ref
5425 pop_compile_unit (dw_die_ref old_unit)
5427 dw_die_ref new_unit = old_unit->die_sib;
5429 old_unit->die_sib = NULL;
5430 return new_unit;
5433 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5434 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5436 /* Calculate the checksum of a location expression. */
5438 static inline void
5439 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5441 int tem;
5442 hashval_t hash = 0;
5444 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5445 CHECKSUM (tem);
5446 hash = hash_loc_operands (loc, hash);
5447 CHECKSUM (hash);
5450 /* Calculate the checksum of an attribute. */
5452 static void
5453 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5455 dw_loc_descr_ref loc;
5456 rtx r;
5458 CHECKSUM (at->dw_attr);
5460 /* We don't care that this was compiled with a different compiler
5461 snapshot; if the output is the same, that's what matters. */
5462 if (at->dw_attr == DW_AT_producer)
5463 return;
5465 switch (AT_class (at))
5467 case dw_val_class_const:
5468 CHECKSUM (at->dw_attr_val.v.val_int);
5469 break;
5470 case dw_val_class_unsigned_const:
5471 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5472 break;
5473 case dw_val_class_const_double:
5474 CHECKSUM (at->dw_attr_val.v.val_double);
5475 break;
5476 case dw_val_class_vec:
5477 CHECKSUM (at->dw_attr_val.v.val_vec);
5478 break;
5479 case dw_val_class_flag:
5480 CHECKSUM (at->dw_attr_val.v.val_flag);
5481 break;
5482 case dw_val_class_str:
5483 CHECKSUM_STRING (AT_string (at));
5484 break;
5486 case dw_val_class_addr:
5487 r = AT_addr (at);
5488 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5489 CHECKSUM_STRING (XSTR (r, 0));
5490 break;
5492 case dw_val_class_offset:
5493 CHECKSUM (at->dw_attr_val.v.val_offset);
5494 break;
5496 case dw_val_class_loc:
5497 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5498 loc_checksum (loc, ctx);
5499 break;
5501 case dw_val_class_die_ref:
5502 die_checksum (AT_ref (at), ctx, mark);
5503 break;
5505 case dw_val_class_fde_ref:
5506 case dw_val_class_vms_delta:
5507 case dw_val_class_lbl_id:
5508 case dw_val_class_lineptr:
5509 case dw_val_class_macptr:
5510 case dw_val_class_high_pc:
5511 break;
5513 case dw_val_class_file:
5514 CHECKSUM_STRING (AT_file (at)->filename);
5515 break;
5517 case dw_val_class_data8:
5518 CHECKSUM (at->dw_attr_val.v.val_data8);
5519 break;
5521 default:
5522 break;
5526 /* Calculate the checksum of a DIE. */
5528 static void
5529 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5531 dw_die_ref c;
5532 dw_attr_ref a;
5533 unsigned ix;
5535 /* To avoid infinite recursion. */
5536 if (die->die_mark)
5538 CHECKSUM (die->die_mark);
5539 return;
5541 die->die_mark = ++(*mark);
5543 CHECKSUM (die->die_tag);
5545 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5546 attr_checksum (a, ctx, mark);
5548 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5551 #undef CHECKSUM
5552 #undef CHECKSUM_STRING
5554 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5555 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5556 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5557 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5558 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5559 #define CHECKSUM_ATTR(FOO) \
5560 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5562 /* Calculate the checksum of a number in signed LEB128 format. */
5564 static void
5565 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5567 unsigned char byte;
5568 bool more;
5570 while (1)
5572 byte = (value & 0x7f);
5573 value >>= 7;
5574 more = !((value == 0 && (byte & 0x40) == 0)
5575 || (value == -1 && (byte & 0x40) != 0));
5576 if (more)
5577 byte |= 0x80;
5578 CHECKSUM (byte);
5579 if (!more)
5580 break;
5584 /* Calculate the checksum of a number in unsigned LEB128 format. */
5586 static void
5587 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5589 while (1)
5591 unsigned char byte = (value & 0x7f);
5592 value >>= 7;
5593 if (value != 0)
5594 /* More bytes to follow. */
5595 byte |= 0x80;
5596 CHECKSUM (byte);
5597 if (value == 0)
5598 break;
5602 /* Checksum the context of the DIE. This adds the names of any
5603 surrounding namespaces or structures to the checksum. */
5605 static void
5606 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5608 const char *name;
5609 dw_die_ref spec;
5610 int tag = die->die_tag;
5612 if (tag != DW_TAG_namespace
5613 && tag != DW_TAG_structure_type
5614 && tag != DW_TAG_class_type)
5615 return;
5617 name = get_AT_string (die, DW_AT_name);
5619 spec = get_AT_ref (die, DW_AT_specification);
5620 if (spec != NULL)
5621 die = spec;
5623 if (die->die_parent != NULL)
5624 checksum_die_context (die->die_parent, ctx);
5626 CHECKSUM_ULEB128 ('C');
5627 CHECKSUM_ULEB128 (tag);
5628 if (name != NULL)
5629 CHECKSUM_STRING (name);
5632 /* Calculate the checksum of a location expression. */
5634 static inline void
5635 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5637 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5638 were emitted as a DW_FORM_sdata instead of a location expression. */
5639 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5641 CHECKSUM_ULEB128 (DW_FORM_sdata);
5642 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5643 return;
5646 /* Otherwise, just checksum the raw location expression. */
5647 while (loc != NULL)
5649 hashval_t hash = 0;
5651 CHECKSUM_ULEB128 (loc->dtprel);
5652 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5653 hash = hash_loc_operands (loc, hash);
5654 CHECKSUM (hash);
5655 loc = loc->dw_loc_next;
5659 /* Calculate the checksum of an attribute. */
5661 static void
5662 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5663 struct md5_ctx *ctx, int *mark)
5665 dw_loc_descr_ref loc;
5666 rtx r;
5668 if (AT_class (at) == dw_val_class_die_ref)
5670 dw_die_ref target_die = AT_ref (at);
5672 /* For pointer and reference types, we checksum only the (qualified)
5673 name of the target type (if there is a name). For friend entries,
5674 we checksum only the (qualified) name of the target type or function.
5675 This allows the checksum to remain the same whether the target type
5676 is complete or not. */
5677 if ((at->dw_attr == DW_AT_type
5678 && (tag == DW_TAG_pointer_type
5679 || tag == DW_TAG_reference_type
5680 || tag == DW_TAG_rvalue_reference_type
5681 || tag == DW_TAG_ptr_to_member_type))
5682 || (at->dw_attr == DW_AT_friend
5683 && tag == DW_TAG_friend))
5685 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5687 if (name_attr != NULL)
5689 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5691 if (decl == NULL)
5692 decl = target_die;
5693 CHECKSUM_ULEB128 ('N');
5694 CHECKSUM_ULEB128 (at->dw_attr);
5695 if (decl->die_parent != NULL)
5696 checksum_die_context (decl->die_parent, ctx);
5697 CHECKSUM_ULEB128 ('E');
5698 CHECKSUM_STRING (AT_string (name_attr));
5699 return;
5703 /* For all other references to another DIE, we check to see if the
5704 target DIE has already been visited. If it has, we emit a
5705 backward reference; if not, we descend recursively. */
5706 if (target_die->die_mark > 0)
5708 CHECKSUM_ULEB128 ('R');
5709 CHECKSUM_ULEB128 (at->dw_attr);
5710 CHECKSUM_ULEB128 (target_die->die_mark);
5712 else
5714 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5716 if (decl == NULL)
5717 decl = target_die;
5718 target_die->die_mark = ++(*mark);
5719 CHECKSUM_ULEB128 ('T');
5720 CHECKSUM_ULEB128 (at->dw_attr);
5721 if (decl->die_parent != NULL)
5722 checksum_die_context (decl->die_parent, ctx);
5723 die_checksum_ordered (target_die, ctx, mark);
5725 return;
5728 CHECKSUM_ULEB128 ('A');
5729 CHECKSUM_ULEB128 (at->dw_attr);
5731 switch (AT_class (at))
5733 case dw_val_class_const:
5734 CHECKSUM_ULEB128 (DW_FORM_sdata);
5735 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5736 break;
5738 case dw_val_class_unsigned_const:
5739 CHECKSUM_ULEB128 (DW_FORM_sdata);
5740 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5741 break;
5743 case dw_val_class_const_double:
5744 CHECKSUM_ULEB128 (DW_FORM_block);
5745 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5746 CHECKSUM (at->dw_attr_val.v.val_double);
5747 break;
5749 case dw_val_class_vec:
5750 CHECKSUM_ULEB128 (DW_FORM_block);
5751 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5752 CHECKSUM (at->dw_attr_val.v.val_vec);
5753 break;
5755 case dw_val_class_flag:
5756 CHECKSUM_ULEB128 (DW_FORM_flag);
5757 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5758 break;
5760 case dw_val_class_str:
5761 CHECKSUM_ULEB128 (DW_FORM_string);
5762 CHECKSUM_STRING (AT_string (at));
5763 break;
5765 case dw_val_class_addr:
5766 r = AT_addr (at);
5767 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5768 CHECKSUM_ULEB128 (DW_FORM_string);
5769 CHECKSUM_STRING (XSTR (r, 0));
5770 break;
5772 case dw_val_class_offset:
5773 CHECKSUM_ULEB128 (DW_FORM_sdata);
5774 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5775 break;
5777 case dw_val_class_loc:
5778 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5779 loc_checksum_ordered (loc, ctx);
5780 break;
5782 case dw_val_class_fde_ref:
5783 case dw_val_class_lbl_id:
5784 case dw_val_class_lineptr:
5785 case dw_val_class_macptr:
5786 case dw_val_class_high_pc:
5787 break;
5789 case dw_val_class_file:
5790 CHECKSUM_ULEB128 (DW_FORM_string);
5791 CHECKSUM_STRING (AT_file (at)->filename);
5792 break;
5794 case dw_val_class_data8:
5795 CHECKSUM (at->dw_attr_val.v.val_data8);
5796 break;
5798 default:
5799 break;
5803 struct checksum_attributes
5805 dw_attr_ref at_name;
5806 dw_attr_ref at_type;
5807 dw_attr_ref at_friend;
5808 dw_attr_ref at_accessibility;
5809 dw_attr_ref at_address_class;
5810 dw_attr_ref at_allocated;
5811 dw_attr_ref at_artificial;
5812 dw_attr_ref at_associated;
5813 dw_attr_ref at_binary_scale;
5814 dw_attr_ref at_bit_offset;
5815 dw_attr_ref at_bit_size;
5816 dw_attr_ref at_bit_stride;
5817 dw_attr_ref at_byte_size;
5818 dw_attr_ref at_byte_stride;
5819 dw_attr_ref at_const_value;
5820 dw_attr_ref at_containing_type;
5821 dw_attr_ref at_count;
5822 dw_attr_ref at_data_location;
5823 dw_attr_ref at_data_member_location;
5824 dw_attr_ref at_decimal_scale;
5825 dw_attr_ref at_decimal_sign;
5826 dw_attr_ref at_default_value;
5827 dw_attr_ref at_digit_count;
5828 dw_attr_ref at_discr;
5829 dw_attr_ref at_discr_list;
5830 dw_attr_ref at_discr_value;
5831 dw_attr_ref at_encoding;
5832 dw_attr_ref at_endianity;
5833 dw_attr_ref at_explicit;
5834 dw_attr_ref at_is_optional;
5835 dw_attr_ref at_location;
5836 dw_attr_ref at_lower_bound;
5837 dw_attr_ref at_mutable;
5838 dw_attr_ref at_ordering;
5839 dw_attr_ref at_picture_string;
5840 dw_attr_ref at_prototyped;
5841 dw_attr_ref at_small;
5842 dw_attr_ref at_segment;
5843 dw_attr_ref at_string_length;
5844 dw_attr_ref at_threads_scaled;
5845 dw_attr_ref at_upper_bound;
5846 dw_attr_ref at_use_location;
5847 dw_attr_ref at_use_UTF8;
5848 dw_attr_ref at_variable_parameter;
5849 dw_attr_ref at_virtuality;
5850 dw_attr_ref at_visibility;
5851 dw_attr_ref at_vtable_elem_location;
5854 /* Collect the attributes that we will want to use for the checksum. */
5856 static void
5857 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5859 dw_attr_ref a;
5860 unsigned ix;
5862 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5864 switch (a->dw_attr)
5866 case DW_AT_name:
5867 attrs->at_name = a;
5868 break;
5869 case DW_AT_type:
5870 attrs->at_type = a;
5871 break;
5872 case DW_AT_friend:
5873 attrs->at_friend = a;
5874 break;
5875 case DW_AT_accessibility:
5876 attrs->at_accessibility = a;
5877 break;
5878 case DW_AT_address_class:
5879 attrs->at_address_class = a;
5880 break;
5881 case DW_AT_allocated:
5882 attrs->at_allocated = a;
5883 break;
5884 case DW_AT_artificial:
5885 attrs->at_artificial = a;
5886 break;
5887 case DW_AT_associated:
5888 attrs->at_associated = a;
5889 break;
5890 case DW_AT_binary_scale:
5891 attrs->at_binary_scale = a;
5892 break;
5893 case DW_AT_bit_offset:
5894 attrs->at_bit_offset = a;
5895 break;
5896 case DW_AT_bit_size:
5897 attrs->at_bit_size = a;
5898 break;
5899 case DW_AT_bit_stride:
5900 attrs->at_bit_stride = a;
5901 break;
5902 case DW_AT_byte_size:
5903 attrs->at_byte_size = a;
5904 break;
5905 case DW_AT_byte_stride:
5906 attrs->at_byte_stride = a;
5907 break;
5908 case DW_AT_const_value:
5909 attrs->at_const_value = a;
5910 break;
5911 case DW_AT_containing_type:
5912 attrs->at_containing_type = a;
5913 break;
5914 case DW_AT_count:
5915 attrs->at_count = a;
5916 break;
5917 case DW_AT_data_location:
5918 attrs->at_data_location = a;
5919 break;
5920 case DW_AT_data_member_location:
5921 attrs->at_data_member_location = a;
5922 break;
5923 case DW_AT_decimal_scale:
5924 attrs->at_decimal_scale = a;
5925 break;
5926 case DW_AT_decimal_sign:
5927 attrs->at_decimal_sign = a;
5928 break;
5929 case DW_AT_default_value:
5930 attrs->at_default_value = a;
5931 break;
5932 case DW_AT_digit_count:
5933 attrs->at_digit_count = a;
5934 break;
5935 case DW_AT_discr:
5936 attrs->at_discr = a;
5937 break;
5938 case DW_AT_discr_list:
5939 attrs->at_discr_list = a;
5940 break;
5941 case DW_AT_discr_value:
5942 attrs->at_discr_value = a;
5943 break;
5944 case DW_AT_encoding:
5945 attrs->at_encoding = a;
5946 break;
5947 case DW_AT_endianity:
5948 attrs->at_endianity = a;
5949 break;
5950 case DW_AT_explicit:
5951 attrs->at_explicit = a;
5952 break;
5953 case DW_AT_is_optional:
5954 attrs->at_is_optional = a;
5955 break;
5956 case DW_AT_location:
5957 attrs->at_location = a;
5958 break;
5959 case DW_AT_lower_bound:
5960 attrs->at_lower_bound = a;
5961 break;
5962 case DW_AT_mutable:
5963 attrs->at_mutable = a;
5964 break;
5965 case DW_AT_ordering:
5966 attrs->at_ordering = a;
5967 break;
5968 case DW_AT_picture_string:
5969 attrs->at_picture_string = a;
5970 break;
5971 case DW_AT_prototyped:
5972 attrs->at_prototyped = a;
5973 break;
5974 case DW_AT_small:
5975 attrs->at_small = a;
5976 break;
5977 case DW_AT_segment:
5978 attrs->at_segment = a;
5979 break;
5980 case DW_AT_string_length:
5981 attrs->at_string_length = a;
5982 break;
5983 case DW_AT_threads_scaled:
5984 attrs->at_threads_scaled = a;
5985 break;
5986 case DW_AT_upper_bound:
5987 attrs->at_upper_bound = a;
5988 break;
5989 case DW_AT_use_location:
5990 attrs->at_use_location = a;
5991 break;
5992 case DW_AT_use_UTF8:
5993 attrs->at_use_UTF8 = a;
5994 break;
5995 case DW_AT_variable_parameter:
5996 attrs->at_variable_parameter = a;
5997 break;
5998 case DW_AT_virtuality:
5999 attrs->at_virtuality = a;
6000 break;
6001 case DW_AT_visibility:
6002 attrs->at_visibility = a;
6003 break;
6004 case DW_AT_vtable_elem_location:
6005 attrs->at_vtable_elem_location = a;
6006 break;
6007 default:
6008 break;
6013 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6015 static void
6016 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6018 dw_die_ref c;
6019 dw_die_ref decl;
6020 struct checksum_attributes attrs;
6022 CHECKSUM_ULEB128 ('D');
6023 CHECKSUM_ULEB128 (die->die_tag);
6025 memset (&attrs, 0, sizeof (attrs));
6027 decl = get_AT_ref (die, DW_AT_specification);
6028 if (decl != NULL)
6029 collect_checksum_attributes (&attrs, decl);
6030 collect_checksum_attributes (&attrs, die);
6032 CHECKSUM_ATTR (attrs.at_name);
6033 CHECKSUM_ATTR (attrs.at_accessibility);
6034 CHECKSUM_ATTR (attrs.at_address_class);
6035 CHECKSUM_ATTR (attrs.at_allocated);
6036 CHECKSUM_ATTR (attrs.at_artificial);
6037 CHECKSUM_ATTR (attrs.at_associated);
6038 CHECKSUM_ATTR (attrs.at_binary_scale);
6039 CHECKSUM_ATTR (attrs.at_bit_offset);
6040 CHECKSUM_ATTR (attrs.at_bit_size);
6041 CHECKSUM_ATTR (attrs.at_bit_stride);
6042 CHECKSUM_ATTR (attrs.at_byte_size);
6043 CHECKSUM_ATTR (attrs.at_byte_stride);
6044 CHECKSUM_ATTR (attrs.at_const_value);
6045 CHECKSUM_ATTR (attrs.at_containing_type);
6046 CHECKSUM_ATTR (attrs.at_count);
6047 CHECKSUM_ATTR (attrs.at_data_location);
6048 CHECKSUM_ATTR (attrs.at_data_member_location);
6049 CHECKSUM_ATTR (attrs.at_decimal_scale);
6050 CHECKSUM_ATTR (attrs.at_decimal_sign);
6051 CHECKSUM_ATTR (attrs.at_default_value);
6052 CHECKSUM_ATTR (attrs.at_digit_count);
6053 CHECKSUM_ATTR (attrs.at_discr);
6054 CHECKSUM_ATTR (attrs.at_discr_list);
6055 CHECKSUM_ATTR (attrs.at_discr_value);
6056 CHECKSUM_ATTR (attrs.at_encoding);
6057 CHECKSUM_ATTR (attrs.at_endianity);
6058 CHECKSUM_ATTR (attrs.at_explicit);
6059 CHECKSUM_ATTR (attrs.at_is_optional);
6060 CHECKSUM_ATTR (attrs.at_location);
6061 CHECKSUM_ATTR (attrs.at_lower_bound);
6062 CHECKSUM_ATTR (attrs.at_mutable);
6063 CHECKSUM_ATTR (attrs.at_ordering);
6064 CHECKSUM_ATTR (attrs.at_picture_string);
6065 CHECKSUM_ATTR (attrs.at_prototyped);
6066 CHECKSUM_ATTR (attrs.at_small);
6067 CHECKSUM_ATTR (attrs.at_segment);
6068 CHECKSUM_ATTR (attrs.at_string_length);
6069 CHECKSUM_ATTR (attrs.at_threads_scaled);
6070 CHECKSUM_ATTR (attrs.at_upper_bound);
6071 CHECKSUM_ATTR (attrs.at_use_location);
6072 CHECKSUM_ATTR (attrs.at_use_UTF8);
6073 CHECKSUM_ATTR (attrs.at_variable_parameter);
6074 CHECKSUM_ATTR (attrs.at_virtuality);
6075 CHECKSUM_ATTR (attrs.at_visibility);
6076 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6077 CHECKSUM_ATTR (attrs.at_type);
6078 CHECKSUM_ATTR (attrs.at_friend);
6080 /* Checksum the child DIEs, except for nested types and member functions. */
6081 c = die->die_child;
6082 if (c) do {
6083 dw_attr_ref name_attr;
6085 c = c->die_sib;
6086 name_attr = get_AT (c, DW_AT_name);
6087 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6088 && name_attr != NULL)
6090 CHECKSUM_ULEB128 ('S');
6091 CHECKSUM_ULEB128 (c->die_tag);
6092 CHECKSUM_STRING (AT_string (name_attr));
6094 else
6096 /* Mark this DIE so it gets processed when unmarking. */
6097 if (c->die_mark == 0)
6098 c->die_mark = -1;
6099 die_checksum_ordered (c, ctx, mark);
6101 } while (c != die->die_child);
6103 CHECKSUM_ULEB128 (0);
6106 #undef CHECKSUM
6107 #undef CHECKSUM_STRING
6108 #undef CHECKSUM_ATTR
6109 #undef CHECKSUM_LEB128
6110 #undef CHECKSUM_ULEB128
6112 /* Generate the type signature for DIE. This is computed by generating an
6113 MD5 checksum over the DIE's tag, its relevant attributes, and its
6114 children. Attributes that are references to other DIEs are processed
6115 by recursion, using the MARK field to prevent infinite recursion.
6116 If the DIE is nested inside a namespace or another type, we also
6117 need to include that context in the signature. The lower 64 bits
6118 of the resulting MD5 checksum comprise the signature. */
6120 static void
6121 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6123 int mark;
6124 const char *name;
6125 unsigned char checksum[16];
6126 struct md5_ctx ctx;
6127 dw_die_ref decl;
6128 dw_die_ref parent;
6130 name = get_AT_string (die, DW_AT_name);
6131 decl = get_AT_ref (die, DW_AT_specification);
6132 parent = get_die_parent (die);
6134 /* First, compute a signature for just the type name (and its surrounding
6135 context, if any. This is stored in the type unit DIE for link-time
6136 ODR (one-definition rule) checking. */
6138 if (is_cxx() && name != NULL)
6140 md5_init_ctx (&ctx);
6142 /* Checksum the names of surrounding namespaces and structures. */
6143 if (parent != NULL)
6144 checksum_die_context (parent, &ctx);
6146 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6147 md5_process_bytes (name, strlen (name) + 1, &ctx);
6148 md5_finish_ctx (&ctx, checksum);
6150 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6153 /* Next, compute the complete type signature. */
6155 md5_init_ctx (&ctx);
6156 mark = 1;
6157 die->die_mark = mark;
6159 /* Checksum the names of surrounding namespaces and structures. */
6160 if (parent != NULL)
6161 checksum_die_context (parent, &ctx);
6163 /* Checksum the DIE and its children. */
6164 die_checksum_ordered (die, &ctx, &mark);
6165 unmark_all_dies (die);
6166 md5_finish_ctx (&ctx, checksum);
6168 /* Store the signature in the type node and link the type DIE and the
6169 type node together. */
6170 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6171 DWARF_TYPE_SIGNATURE_SIZE);
6172 die->comdat_type_p = true;
6173 die->die_id.die_type_node = type_node;
6174 type_node->type_die = die;
6176 /* If the DIE is a specification, link its declaration to the type node
6177 as well. */
6178 if (decl != NULL)
6180 decl->comdat_type_p = true;
6181 decl->die_id.die_type_node = type_node;
6185 /* Do the location expressions look same? */
6186 static inline int
6187 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6189 return loc1->dw_loc_opc == loc2->dw_loc_opc
6190 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6191 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6194 /* Do the values look the same? */
6195 static int
6196 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6198 dw_loc_descr_ref loc1, loc2;
6199 rtx r1, r2;
6201 if (v1->val_class != v2->val_class)
6202 return 0;
6204 switch (v1->val_class)
6206 case dw_val_class_const:
6207 return v1->v.val_int == v2->v.val_int;
6208 case dw_val_class_unsigned_const:
6209 return v1->v.val_unsigned == v2->v.val_unsigned;
6210 case dw_val_class_const_double:
6211 return v1->v.val_double.high == v2->v.val_double.high
6212 && v1->v.val_double.low == v2->v.val_double.low;
6213 case dw_val_class_vec:
6214 if (v1->v.val_vec.length != v2->v.val_vec.length
6215 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6216 return 0;
6217 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6218 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6219 return 0;
6220 return 1;
6221 case dw_val_class_flag:
6222 return v1->v.val_flag == v2->v.val_flag;
6223 case dw_val_class_str:
6224 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6226 case dw_val_class_addr:
6227 r1 = v1->v.val_addr;
6228 r2 = v2->v.val_addr;
6229 if (GET_CODE (r1) != GET_CODE (r2))
6230 return 0;
6231 return !rtx_equal_p (r1, r2);
6233 case dw_val_class_offset:
6234 return v1->v.val_offset == v2->v.val_offset;
6236 case dw_val_class_loc:
6237 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6238 loc1 && loc2;
6239 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6240 if (!same_loc_p (loc1, loc2, mark))
6241 return 0;
6242 return !loc1 && !loc2;
6244 case dw_val_class_die_ref:
6245 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6247 case dw_val_class_fde_ref:
6248 case dw_val_class_vms_delta:
6249 case dw_val_class_lbl_id:
6250 case dw_val_class_lineptr:
6251 case dw_val_class_macptr:
6252 case dw_val_class_high_pc:
6253 return 1;
6255 case dw_val_class_file:
6256 return v1->v.val_file == v2->v.val_file;
6258 case dw_val_class_data8:
6259 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6261 default:
6262 return 1;
6266 /* Do the attributes look the same? */
6268 static int
6269 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6271 if (at1->dw_attr != at2->dw_attr)
6272 return 0;
6274 /* We don't care that this was compiled with a different compiler
6275 snapshot; if the output is the same, that's what matters. */
6276 if (at1->dw_attr == DW_AT_producer)
6277 return 1;
6279 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6282 /* Do the dies look the same? */
6284 static int
6285 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6287 dw_die_ref c1, c2;
6288 dw_attr_ref a1;
6289 unsigned ix;
6291 /* To avoid infinite recursion. */
6292 if (die1->die_mark)
6293 return die1->die_mark == die2->die_mark;
6294 die1->die_mark = die2->die_mark = ++(*mark);
6296 if (die1->die_tag != die2->die_tag)
6297 return 0;
6299 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6300 return 0;
6302 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6303 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6304 return 0;
6306 c1 = die1->die_child;
6307 c2 = die2->die_child;
6308 if (! c1)
6310 if (c2)
6311 return 0;
6313 else
6314 for (;;)
6316 if (!same_die_p (c1, c2, mark))
6317 return 0;
6318 c1 = c1->die_sib;
6319 c2 = c2->die_sib;
6320 if (c1 == die1->die_child)
6322 if (c2 == die2->die_child)
6323 break;
6324 else
6325 return 0;
6329 return 1;
6332 /* Do the dies look the same? Wrapper around same_die_p. */
6334 static int
6335 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6337 int mark = 0;
6338 int ret = same_die_p (die1, die2, &mark);
6340 unmark_all_dies (die1);
6341 unmark_all_dies (die2);
6343 return ret;
6346 /* The prefix to attach to symbols on DIEs in the current comdat debug
6347 info section. */
6348 static const char *comdat_symbol_id;
6350 /* The index of the current symbol within the current comdat CU. */
6351 static unsigned int comdat_symbol_number;
6353 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6354 children, and set comdat_symbol_id accordingly. */
6356 static void
6357 compute_section_prefix (dw_die_ref unit_die)
6359 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6360 const char *base = die_name ? lbasename (die_name) : "anonymous";
6361 char *name = XALLOCAVEC (char, strlen (base) + 64);
6362 char *p;
6363 int i, mark;
6364 unsigned char checksum[16];
6365 struct md5_ctx ctx;
6367 /* Compute the checksum of the DIE, then append part of it as hex digits to
6368 the name filename of the unit. */
6370 md5_init_ctx (&ctx);
6371 mark = 0;
6372 die_checksum (unit_die, &ctx, &mark);
6373 unmark_all_dies (unit_die);
6374 md5_finish_ctx (&ctx, checksum);
6376 sprintf (name, "%s.", base);
6377 clean_symbol_name (name);
6379 p = name + strlen (name);
6380 for (i = 0; i < 4; i++)
6382 sprintf (p, "%.2x", checksum[i]);
6383 p += 2;
6386 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6387 comdat_symbol_number = 0;
6390 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6392 static int
6393 is_type_die (dw_die_ref die)
6395 switch (die->die_tag)
6397 case DW_TAG_array_type:
6398 case DW_TAG_class_type:
6399 case DW_TAG_interface_type:
6400 case DW_TAG_enumeration_type:
6401 case DW_TAG_pointer_type:
6402 case DW_TAG_reference_type:
6403 case DW_TAG_rvalue_reference_type:
6404 case DW_TAG_string_type:
6405 case DW_TAG_structure_type:
6406 case DW_TAG_subroutine_type:
6407 case DW_TAG_union_type:
6408 case DW_TAG_ptr_to_member_type:
6409 case DW_TAG_set_type:
6410 case DW_TAG_subrange_type:
6411 case DW_TAG_base_type:
6412 case DW_TAG_const_type:
6413 case DW_TAG_file_type:
6414 case DW_TAG_packed_type:
6415 case DW_TAG_volatile_type:
6416 case DW_TAG_typedef:
6417 return 1;
6418 default:
6419 return 0;
6423 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6424 Basically, we want to choose the bits that are likely to be shared between
6425 compilations (types) and leave out the bits that are specific to individual
6426 compilations (functions). */
6428 static int
6429 is_comdat_die (dw_die_ref c)
6431 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6432 we do for stabs. The advantage is a greater likelihood of sharing between
6433 objects that don't include headers in the same order (and therefore would
6434 put the base types in a different comdat). jason 8/28/00 */
6436 if (c->die_tag == DW_TAG_base_type)
6437 return 0;
6439 if (c->die_tag == DW_TAG_pointer_type
6440 || c->die_tag == DW_TAG_reference_type
6441 || c->die_tag == DW_TAG_rvalue_reference_type
6442 || c->die_tag == DW_TAG_const_type
6443 || c->die_tag == DW_TAG_volatile_type)
6445 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6447 return t ? is_comdat_die (t) : 0;
6450 return is_type_die (c);
6453 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6454 compilation unit. */
6456 static int
6457 is_symbol_die (dw_die_ref c)
6459 return (is_type_die (c)
6460 || is_declaration_die (c)
6461 || c->die_tag == DW_TAG_namespace
6462 || c->die_tag == DW_TAG_module);
6465 /* Returns true iff C is a compile-unit DIE. */
6467 static inline bool
6468 is_cu_die (dw_die_ref c)
6470 return c && c->die_tag == DW_TAG_compile_unit;
6473 /* Returns true iff C is a unit DIE of some sort. */
6475 static inline bool
6476 is_unit_die (dw_die_ref c)
6478 return c && (c->die_tag == DW_TAG_compile_unit
6479 || c->die_tag == DW_TAG_partial_unit
6480 || c->die_tag == DW_TAG_type_unit);
6483 /* Returns true iff C is a namespace DIE. */
6485 static inline bool
6486 is_namespace_die (dw_die_ref c)
6488 return c && c->die_tag == DW_TAG_namespace;
6491 /* Returns true iff C is a class or structure DIE. */
6493 static inline bool
6494 is_class_die (dw_die_ref c)
6496 return c && (c->die_tag == DW_TAG_class_type
6497 || c->die_tag == DW_TAG_structure_type);
6500 static char *
6501 gen_internal_sym (const char *prefix)
6503 char buf[256];
6505 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6506 return xstrdup (buf);
6509 /* Assign symbols to all worthy DIEs under DIE. */
6511 static void
6512 assign_symbol_names (dw_die_ref die)
6514 dw_die_ref c;
6516 if (is_symbol_die (die) && !die->comdat_type_p)
6518 if (comdat_symbol_id)
6520 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6522 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6523 comdat_symbol_id, comdat_symbol_number++);
6524 die->die_id.die_symbol = xstrdup (p);
6526 else
6527 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6530 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6533 struct cu_hash_table_entry
6535 dw_die_ref cu;
6536 unsigned min_comdat_num, max_comdat_num;
6537 struct cu_hash_table_entry *next;
6540 /* Helpers to manipulate hash table of CUs. */
6542 struct cu_hash_table_entry_hasher
6544 typedef cu_hash_table_entry value_type;
6545 typedef die_struct compare_type;
6546 static inline hashval_t hash (const value_type *);
6547 static inline bool equal (const value_type *, const compare_type *);
6548 static inline void remove (value_type *);
6551 inline hashval_t
6552 cu_hash_table_entry_hasher::hash (const value_type *entry)
6554 return htab_hash_string (entry->cu->die_id.die_symbol);
6557 inline bool
6558 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6559 const compare_type *entry2)
6561 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6564 inline void
6565 cu_hash_table_entry_hasher::remove (value_type *entry)
6567 struct cu_hash_table_entry *next;
6569 while (entry)
6571 next = entry->next;
6572 free (entry);
6573 entry = next;
6577 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6579 /* Check whether we have already seen this CU and set up SYM_NUM
6580 accordingly. */
6581 static int
6582 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6584 struct cu_hash_table_entry dummy;
6585 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6587 dummy.max_comdat_num = 0;
6589 slot = htable.find_slot_with_hash (cu,
6590 htab_hash_string (cu->die_id.die_symbol),
6591 INSERT);
6592 entry = *slot;
6594 for (; entry; last = entry, entry = entry->next)
6596 if (same_die_p_wrap (cu, entry->cu))
6597 break;
6600 if (entry)
6602 *sym_num = entry->min_comdat_num;
6603 return 1;
6606 entry = XCNEW (struct cu_hash_table_entry);
6607 entry->cu = cu;
6608 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6609 entry->next = *slot;
6610 *slot = entry;
6612 return 0;
6615 /* Record SYM_NUM to record of CU in HTABLE. */
6616 static void
6617 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6618 unsigned int sym_num)
6620 struct cu_hash_table_entry **slot, *entry;
6622 slot = htable.find_slot_with_hash (cu,
6623 htab_hash_string (cu->die_id.die_symbol),
6624 NO_INSERT);
6625 entry = *slot;
6627 entry->max_comdat_num = sym_num;
6630 /* Traverse the DIE (which is always comp_unit_die), and set up
6631 additional compilation units for each of the include files we see
6632 bracketed by BINCL/EINCL. */
6634 static void
6635 break_out_includes (dw_die_ref die)
6637 dw_die_ref c;
6638 dw_die_ref unit = NULL;
6639 limbo_die_node *node, **pnode;
6640 cu_hash_type cu_hash_table;
6642 c = die->die_child;
6643 if (c) do {
6644 dw_die_ref prev = c;
6645 c = c->die_sib;
6646 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6647 || (unit && is_comdat_die (c)))
6649 dw_die_ref next = c->die_sib;
6651 /* This DIE is for a secondary CU; remove it from the main one. */
6652 remove_child_with_prev (c, prev);
6654 if (c->die_tag == DW_TAG_GNU_BINCL)
6655 unit = push_new_compile_unit (unit, c);
6656 else if (c->die_tag == DW_TAG_GNU_EINCL)
6657 unit = pop_compile_unit (unit);
6658 else
6659 add_child_die (unit, c);
6660 c = next;
6661 if (c == die->die_child)
6662 break;
6664 } while (c != die->die_child);
6666 #if 0
6667 /* We can only use this in debugging, since the frontend doesn't check
6668 to make sure that we leave every include file we enter. */
6669 gcc_assert (!unit);
6670 #endif
6672 assign_symbol_names (die);
6673 cu_hash_table.create (10);
6674 for (node = limbo_die_list, pnode = &limbo_die_list;
6675 node;
6676 node = node->next)
6678 int is_dupl;
6680 compute_section_prefix (node->die);
6681 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6682 &comdat_symbol_number);
6683 assign_symbol_names (node->die);
6684 if (is_dupl)
6685 *pnode = node->next;
6686 else
6688 pnode = &node->next;
6689 record_comdat_symbol_number (node->die, cu_hash_table,
6690 comdat_symbol_number);
6693 cu_hash_table.dispose ();
6696 /* Return non-zero if this DIE is a declaration. */
6698 static int
6699 is_declaration_die (dw_die_ref die)
6701 dw_attr_ref a;
6702 unsigned ix;
6704 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6705 if (a->dw_attr == DW_AT_declaration)
6706 return 1;
6708 return 0;
6711 /* Return non-zero if this DIE is nested inside a subprogram. */
6713 static int
6714 is_nested_in_subprogram (dw_die_ref die)
6716 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6718 if (decl == NULL)
6719 decl = die;
6720 return local_scope_p (decl);
6723 /* Return non-zero if this DIE contains a defining declaration of a
6724 subprogram. */
6726 static int
6727 contains_subprogram_definition (dw_die_ref die)
6729 dw_die_ref c;
6731 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6732 return 1;
6733 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6734 return 0;
6737 /* Return non-zero if this is a type DIE that should be moved to a
6738 COMDAT .debug_types section. */
6740 static int
6741 should_move_die_to_comdat (dw_die_ref die)
6743 switch (die->die_tag)
6745 case DW_TAG_class_type:
6746 case DW_TAG_structure_type:
6747 case DW_TAG_enumeration_type:
6748 case DW_TAG_union_type:
6749 /* Don't move declarations, inlined instances, or types nested in a
6750 subprogram. */
6751 if (is_declaration_die (die)
6752 || get_AT (die, DW_AT_abstract_origin)
6753 || is_nested_in_subprogram (die))
6754 return 0;
6755 /* A type definition should never contain a subprogram definition. */
6756 gcc_assert (!contains_subprogram_definition (die));
6757 return 1;
6758 case DW_TAG_array_type:
6759 case DW_TAG_interface_type:
6760 case DW_TAG_pointer_type:
6761 case DW_TAG_reference_type:
6762 case DW_TAG_rvalue_reference_type:
6763 case DW_TAG_string_type:
6764 case DW_TAG_subroutine_type:
6765 case DW_TAG_ptr_to_member_type:
6766 case DW_TAG_set_type:
6767 case DW_TAG_subrange_type:
6768 case DW_TAG_base_type:
6769 case DW_TAG_const_type:
6770 case DW_TAG_file_type:
6771 case DW_TAG_packed_type:
6772 case DW_TAG_volatile_type:
6773 case DW_TAG_typedef:
6774 default:
6775 return 0;
6779 /* Make a clone of DIE. */
6781 static dw_die_ref
6782 clone_die (dw_die_ref die)
6784 dw_die_ref clone;
6785 dw_attr_ref a;
6786 unsigned ix;
6788 clone = ggc_alloc_cleared_die_node ();
6789 clone->die_tag = die->die_tag;
6791 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6792 add_dwarf_attr (clone, a);
6794 return clone;
6797 /* Make a clone of the tree rooted at DIE. */
6799 static dw_die_ref
6800 clone_tree (dw_die_ref die)
6802 dw_die_ref c;
6803 dw_die_ref clone = clone_die (die);
6805 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6807 return clone;
6810 /* Make a clone of DIE as a declaration. */
6812 static dw_die_ref
6813 clone_as_declaration (dw_die_ref die)
6815 dw_die_ref clone;
6816 dw_die_ref decl;
6817 dw_attr_ref a;
6818 unsigned ix;
6820 /* If the DIE is already a declaration, just clone it. */
6821 if (is_declaration_die (die))
6822 return clone_die (die);
6824 /* If the DIE is a specification, just clone its declaration DIE. */
6825 decl = get_AT_ref (die, DW_AT_specification);
6826 if (decl != NULL)
6828 clone = clone_die (decl);
6829 if (die->comdat_type_p)
6830 add_AT_die_ref (clone, DW_AT_signature, die);
6831 return clone;
6834 clone = ggc_alloc_cleared_die_node ();
6835 clone->die_tag = die->die_tag;
6837 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6839 /* We don't want to copy over all attributes.
6840 For example we don't want DW_AT_byte_size because otherwise we will no
6841 longer have a declaration and GDB will treat it as a definition. */
6843 switch (a->dw_attr)
6845 case DW_AT_artificial:
6846 case DW_AT_containing_type:
6847 case DW_AT_external:
6848 case DW_AT_name:
6849 case DW_AT_type:
6850 case DW_AT_virtuality:
6851 case DW_AT_linkage_name:
6852 case DW_AT_MIPS_linkage_name:
6853 add_dwarf_attr (clone, a);
6854 break;
6855 case DW_AT_byte_size:
6856 default:
6857 break;
6861 if (die->comdat_type_p)
6862 add_AT_die_ref (clone, DW_AT_signature, die);
6864 add_AT_flag (clone, DW_AT_declaration, 1);
6865 return clone;
6869 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6871 struct decl_table_entry
6873 dw_die_ref orig;
6874 dw_die_ref copy;
6877 /* Helpers to manipulate hash table of copied declarations. */
6879 /* Hashtable helpers. */
6881 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6883 typedef decl_table_entry value_type;
6884 typedef die_struct compare_type;
6885 static inline hashval_t hash (const value_type *);
6886 static inline bool equal (const value_type *, const compare_type *);
6889 inline hashval_t
6890 decl_table_entry_hasher::hash (const value_type *entry)
6892 return htab_hash_pointer (entry->orig);
6895 inline bool
6896 decl_table_entry_hasher::equal (const value_type *entry1,
6897 const compare_type *entry2)
6899 return entry1->orig == entry2;
6902 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6904 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6905 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6906 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6907 to check if the ancestor has already been copied into UNIT. */
6909 static dw_die_ref
6910 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6912 dw_die_ref parent = die->die_parent;
6913 dw_die_ref new_parent = unit;
6914 dw_die_ref copy;
6915 decl_table_entry **slot = NULL;
6916 struct decl_table_entry *entry = NULL;
6918 if (decl_table.is_created ())
6920 /* Check if the entry has already been copied to UNIT. */
6921 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
6922 INSERT);
6923 if (*slot != HTAB_EMPTY_ENTRY)
6925 entry = *slot;
6926 return entry->copy;
6929 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
6930 entry = XCNEW (struct decl_table_entry);
6931 entry->orig = die;
6932 entry->copy = NULL;
6933 *slot = entry;
6936 if (parent != NULL)
6938 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6939 if (spec != NULL)
6940 parent = spec;
6941 if (!is_unit_die (parent))
6942 new_parent = copy_ancestor_tree (unit, parent, decl_table);
6945 copy = clone_as_declaration (die);
6946 add_child_die (new_parent, copy);
6948 if (decl_table.is_created ())
6950 /* Record the pointer to the copy. */
6951 entry->copy = copy;
6954 return copy;
6956 /* Copy the declaration context to the new type unit DIE. This includes
6957 any surrounding namespace or type declarations. If the DIE has an
6958 AT_specification attribute, it also includes attributes and children
6959 attached to the specification, and returns a pointer to the original
6960 parent of the declaration DIE. Returns NULL otherwise. */
6962 static dw_die_ref
6963 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6965 dw_die_ref decl;
6966 dw_die_ref new_decl;
6967 dw_die_ref orig_parent = NULL;
6969 decl = get_AT_ref (die, DW_AT_specification);
6970 if (decl == NULL)
6971 decl = die;
6972 else
6974 unsigned ix;
6975 dw_die_ref c;
6976 dw_attr_ref a;
6978 /* The original DIE will be changed to a declaration, and must
6979 be moved to be a child of the original declaration DIE. */
6980 orig_parent = decl->die_parent;
6982 /* Copy the type node pointer from the new DIE to the original
6983 declaration DIE so we can forward references later. */
6984 decl->comdat_type_p = true;
6985 decl->die_id.die_type_node = die->die_id.die_type_node;
6987 remove_AT (die, DW_AT_specification);
6989 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6991 if (a->dw_attr != DW_AT_name
6992 && a->dw_attr != DW_AT_declaration
6993 && a->dw_attr != DW_AT_external)
6994 add_dwarf_attr (die, a);
6997 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7000 if (decl->die_parent != NULL
7001 && !is_unit_die (decl->die_parent))
7003 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7004 if (new_decl != NULL)
7006 remove_AT (new_decl, DW_AT_signature);
7007 add_AT_specification (die, new_decl);
7011 return orig_parent;
7014 /* Generate the skeleton ancestor tree for the given NODE, then clone
7015 the DIE and add the clone into the tree. */
7017 static void
7018 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7020 if (node->new_die != NULL)
7021 return;
7023 node->new_die = clone_as_declaration (node->old_die);
7025 if (node->parent != NULL)
7027 generate_skeleton_ancestor_tree (node->parent);
7028 add_child_die (node->parent->new_die, node->new_die);
7032 /* Generate a skeleton tree of DIEs containing any declarations that are
7033 found in the original tree. We traverse the tree looking for declaration
7034 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7036 static void
7037 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7039 skeleton_chain_node node;
7040 dw_die_ref c;
7041 dw_die_ref first;
7042 dw_die_ref prev = NULL;
7043 dw_die_ref next = NULL;
7045 node.parent = parent;
7047 first = c = parent->old_die->die_child;
7048 if (c)
7049 next = c->die_sib;
7050 if (c) do {
7051 if (prev == NULL || prev->die_sib == c)
7052 prev = c;
7053 c = next;
7054 next = (c == first ? NULL : c->die_sib);
7055 node.old_die = c;
7056 node.new_die = NULL;
7057 if (is_declaration_die (c))
7059 /* Clone the existing DIE, move the original to the skeleton
7060 tree (which is in the main CU), and put the clone, with
7061 all the original's children, where the original came from. */
7062 dw_die_ref clone = clone_die (c);
7063 move_all_children (c, clone);
7065 replace_child (c, clone, prev);
7066 generate_skeleton_ancestor_tree (parent);
7067 add_child_die (parent->new_die, c);
7068 node.new_die = c;
7069 c = clone;
7071 generate_skeleton_bottom_up (&node);
7072 } while (next != NULL);
7075 /* Wrapper function for generate_skeleton_bottom_up. */
7077 static dw_die_ref
7078 generate_skeleton (dw_die_ref die)
7080 skeleton_chain_node node;
7082 node.old_die = die;
7083 node.new_die = NULL;
7084 node.parent = NULL;
7086 /* If this type definition is nested inside another type,
7087 always leave at least a declaration in its place. */
7088 if (die->die_parent != NULL && is_type_die (die->die_parent))
7089 node.new_die = clone_as_declaration (die);
7091 generate_skeleton_bottom_up (&node);
7092 return node.new_die;
7095 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7096 declaration. The original DIE is moved to a new compile unit so that
7097 existing references to it follow it to the new location. If any of the
7098 original DIE's descendants is a declaration, we need to replace the
7099 original DIE with a skeleton tree and move the declarations back into the
7100 skeleton tree. */
7102 static dw_die_ref
7103 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7104 dw_die_ref prev)
7106 dw_die_ref skeleton, orig_parent;
7108 /* Copy the declaration context to the type unit DIE. If the returned
7109 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7110 that DIE. */
7111 orig_parent = copy_declaration_context (unit, child);
7113 skeleton = generate_skeleton (child);
7114 if (skeleton == NULL)
7115 remove_child_with_prev (child, prev);
7116 else
7118 skeleton->comdat_type_p = true;
7119 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7121 /* If the original DIE was a specification, we need to put
7122 the skeleton under the parent DIE of the declaration.
7123 This leaves the original declaration in the tree, but
7124 it will be pruned later since there are no longer any
7125 references to it. */
7126 if (orig_parent != NULL)
7128 remove_child_with_prev (child, prev);
7129 add_child_die (orig_parent, skeleton);
7131 else
7132 replace_child (child, skeleton, prev);
7135 return skeleton;
7138 /* Traverse the DIE and set up additional .debug_types sections for each
7139 type worthy of being placed in a COMDAT section. */
7141 static void
7142 break_out_comdat_types (dw_die_ref die)
7144 dw_die_ref c;
7145 dw_die_ref first;
7146 dw_die_ref prev = NULL;
7147 dw_die_ref next = NULL;
7148 dw_die_ref unit = NULL;
7150 first = c = die->die_child;
7151 if (c)
7152 next = c->die_sib;
7153 if (c) do {
7154 if (prev == NULL || prev->die_sib == c)
7155 prev = c;
7156 c = next;
7157 next = (c == first ? NULL : c->die_sib);
7158 if (should_move_die_to_comdat (c))
7160 dw_die_ref replacement;
7161 comdat_type_node_ref type_node;
7163 /* Create a new type unit DIE as the root for the new tree, and
7164 add it to the list of comdat types. */
7165 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7166 add_AT_unsigned (unit, DW_AT_language,
7167 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7168 type_node = ggc_alloc_cleared_comdat_type_node ();
7169 type_node->root_die = unit;
7170 type_node->next = comdat_type_list;
7171 comdat_type_list = type_node;
7173 /* Generate the type signature. */
7174 generate_type_signature (c, type_node);
7176 /* Copy the declaration context, attributes, and children of the
7177 declaration into the new type unit DIE, then remove this DIE
7178 from the main CU (or replace it with a skeleton if necessary). */
7179 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7180 type_node->skeleton_die = replacement;
7182 /* Break out nested types into their own type units. */
7183 break_out_comdat_types (c);
7185 /* Add the DIE to the new compunit. */
7186 add_child_die (unit, c);
7188 if (replacement != NULL)
7189 c = replacement;
7191 else if (c->die_tag == DW_TAG_namespace
7192 || c->die_tag == DW_TAG_class_type
7193 || c->die_tag == DW_TAG_structure_type
7194 || c->die_tag == DW_TAG_union_type)
7196 /* Look for nested types that can be broken out. */
7197 break_out_comdat_types (c);
7199 } while (next != NULL);
7202 /* Like clone_tree, but additionally enter all the children into
7203 the hash table decl_table. */
7205 static dw_die_ref
7206 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7208 dw_die_ref c;
7209 dw_die_ref clone = clone_die (die);
7210 struct decl_table_entry *entry;
7211 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7212 htab_hash_pointer (die), INSERT);
7213 /* Assert that DIE isn't in the hash table yet. If it would be there
7214 before, the ancestors would be necessarily there as well, therefore
7215 clone_tree_hash wouldn't be called. */
7216 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7217 entry = XCNEW (struct decl_table_entry);
7218 entry->orig = die;
7219 entry->copy = clone;
7220 *slot = entry;
7222 FOR_EACH_CHILD (die, c,
7223 add_child_die (clone, clone_tree_hash (c, decl_table)));
7225 return clone;
7228 /* Walk the DIE and its children, looking for references to incomplete
7229 or trivial types that are unmarked (i.e., that are not in the current
7230 type_unit). */
7232 static void
7233 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7235 dw_die_ref c;
7236 dw_attr_ref a;
7237 unsigned ix;
7239 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7241 if (AT_class (a) == dw_val_class_die_ref)
7243 dw_die_ref targ = AT_ref (a);
7244 decl_table_entry **slot;
7245 struct decl_table_entry *entry;
7247 if (targ->die_mark != 0 || targ->comdat_type_p)
7248 continue;
7250 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7251 INSERT);
7253 if (*slot != HTAB_EMPTY_ENTRY)
7255 /* TARG has already been copied, so we just need to
7256 modify the reference to point to the copy. */
7257 entry = *slot;
7258 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7260 else
7262 dw_die_ref parent = unit;
7263 dw_die_ref copy = clone_die (targ);
7265 /* Record in DECL_TABLE that TARG has been copied.
7266 Need to do this now, before the recursive call,
7267 because DECL_TABLE may be expanded and SLOT
7268 would no longer be a valid pointer. */
7269 entry = XCNEW (struct decl_table_entry);
7270 entry->orig = targ;
7271 entry->copy = copy;
7272 *slot = entry;
7274 FOR_EACH_CHILD (targ, c,
7275 add_child_die (copy,
7276 clone_tree_hash (c, decl_table)));
7278 /* Make sure the cloned tree is marked as part of the
7279 type unit. */
7280 mark_dies (copy);
7282 /* If TARG has surrounding context, copy its ancestor tree
7283 into the new type unit. */
7284 if (targ->die_parent != NULL
7285 && !is_unit_die (targ->die_parent))
7286 parent = copy_ancestor_tree (unit, targ->die_parent,
7287 decl_table);
7289 add_child_die (parent, copy);
7290 a->dw_attr_val.v.val_die_ref.die = copy;
7292 /* Make sure the newly-copied DIE is walked. If it was
7293 installed in a previously-added context, it won't
7294 get visited otherwise. */
7295 if (parent != unit)
7297 /* Find the highest point of the newly-added tree,
7298 mark each node along the way, and walk from there. */
7299 parent->die_mark = 1;
7300 while (parent->die_parent
7301 && parent->die_parent->die_mark == 0)
7303 parent = parent->die_parent;
7304 parent->die_mark = 1;
7306 copy_decls_walk (unit, parent, decl_table);
7312 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7315 /* Copy declarations for "unworthy" types into the new comdat section.
7316 Incomplete types, modified types, and certain other types aren't broken
7317 out into comdat sections of their own, so they don't have a signature,
7318 and we need to copy the declaration into the same section so that we
7319 don't have an external reference. */
7321 static void
7322 copy_decls_for_unworthy_types (dw_die_ref unit)
7324 decl_hash_type decl_table;
7326 mark_dies (unit);
7327 decl_table.create (10);
7328 copy_decls_walk (unit, unit, decl_table);
7329 decl_table.dispose ();
7330 unmark_dies (unit);
7333 /* Traverse the DIE and add a sibling attribute if it may have the
7334 effect of speeding up access to siblings. To save some space,
7335 avoid generating sibling attributes for DIE's without children. */
7337 static void
7338 add_sibling_attributes (dw_die_ref die)
7340 dw_die_ref c;
7342 if (! die->die_child)
7343 return;
7345 if (die->die_parent && die != die->die_parent->die_child)
7346 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7348 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7351 /* Output all location lists for the DIE and its children. */
7353 static void
7354 output_location_lists (dw_die_ref die)
7356 dw_die_ref c;
7357 dw_attr_ref a;
7358 unsigned ix;
7360 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7361 if (AT_class (a) == dw_val_class_loc_list)
7362 output_loc_list (AT_loc_list (a));
7364 FOR_EACH_CHILD (die, c, output_location_lists (c));
7367 /* We want to limit the number of external references, because they are
7368 larger than local references: a relocation takes multiple words, and
7369 even a sig8 reference is always eight bytes, whereas a local reference
7370 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7371 So if we encounter multiple external references to the same type DIE, we
7372 make a local typedef stub for it and redirect all references there.
7374 This is the element of the hash table for keeping track of these
7375 references. */
7377 struct external_ref
7379 dw_die_ref type;
7380 dw_die_ref stub;
7381 unsigned n_refs;
7384 /* Hashtable helpers. */
7386 struct external_ref_hasher : typed_free_remove <external_ref>
7388 typedef external_ref value_type;
7389 typedef external_ref compare_type;
7390 static inline hashval_t hash (const value_type *);
7391 static inline bool equal (const value_type *, const compare_type *);
7394 inline hashval_t
7395 external_ref_hasher::hash (const value_type *r)
7397 dw_die_ref die = r->type;
7398 hashval_t h = 0;
7400 /* We can't use the address of the DIE for hashing, because
7401 that will make the order of the stub DIEs non-deterministic. */
7402 if (! die->comdat_type_p)
7403 /* We have a symbol; use it to compute a hash. */
7404 h = htab_hash_string (die->die_id.die_symbol);
7405 else
7407 /* We have a type signature; use a subset of the bits as the hash.
7408 The 8-byte signature is at least as large as hashval_t. */
7409 comdat_type_node_ref type_node = die->die_id.die_type_node;
7410 memcpy (&h, type_node->signature, sizeof (h));
7412 return h;
7415 inline bool
7416 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7418 return r1->type == r2->type;
7421 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7423 /* Return a pointer to the external_ref for references to DIE. */
7425 static struct external_ref *
7426 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7428 struct external_ref ref, *ref_p;
7429 external_ref **slot;
7431 ref.type = die;
7432 slot = map.find_slot (&ref, INSERT);
7433 if (*slot != HTAB_EMPTY_ENTRY)
7434 return *slot;
7436 ref_p = XCNEW (struct external_ref);
7437 ref_p->type = die;
7438 *slot = ref_p;
7439 return ref_p;
7442 /* Subroutine of optimize_external_refs, below.
7444 If we see a type skeleton, record it as our stub. If we see external
7445 references, remember how many we've seen. */
7447 static void
7448 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7450 dw_die_ref c;
7451 dw_attr_ref a;
7452 unsigned ix;
7453 struct external_ref *ref_p;
7455 if (is_type_die (die)
7456 && (c = get_AT_ref (die, DW_AT_signature)))
7458 /* This is a local skeleton; use it for local references. */
7459 ref_p = lookup_external_ref (map, c);
7460 ref_p->stub = die;
7463 /* Scan the DIE references, and remember any that refer to DIEs from
7464 other CUs (i.e. those which are not marked). */
7465 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7466 if (AT_class (a) == dw_val_class_die_ref
7467 && (c = AT_ref (a))->die_mark == 0
7468 && is_type_die (c))
7470 ref_p = lookup_external_ref (map, c);
7471 ref_p->n_refs++;
7474 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7477 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7478 points to an external_ref, DATA is the CU we're processing. If we don't
7479 already have a local stub, and we have multiple refs, build a stub. */
7482 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7484 struct external_ref *ref_p = *slot;
7486 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7488 /* We have multiple references to this type, so build a small stub.
7489 Both of these forms are a bit dodgy from the perspective of the
7490 DWARF standard, since technically they should have names. */
7491 dw_die_ref cu = data;
7492 dw_die_ref type = ref_p->type;
7493 dw_die_ref stub = NULL;
7495 if (type->comdat_type_p)
7497 /* If we refer to this type via sig8, use AT_signature. */
7498 stub = new_die (type->die_tag, cu, NULL_TREE);
7499 add_AT_die_ref (stub, DW_AT_signature, type);
7501 else
7503 /* Otherwise, use a typedef with no name. */
7504 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7505 add_AT_die_ref (stub, DW_AT_type, type);
7508 stub->die_mark++;
7509 ref_p->stub = stub;
7511 return 1;
7514 /* DIE is a unit; look through all the DIE references to see if there are
7515 any external references to types, and if so, create local stubs for
7516 them which will be applied in build_abbrev_table. This is useful because
7517 references to local DIEs are smaller. */
7519 static external_ref_hash_type
7520 optimize_external_refs (dw_die_ref die)
7522 external_ref_hash_type map;
7523 map.create (10);
7524 optimize_external_refs_1 (die, map);
7525 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7526 return map;
7529 /* The format of each DIE (and its attribute value pairs) is encoded in an
7530 abbreviation table. This routine builds the abbreviation table and assigns
7531 a unique abbreviation id for each abbreviation entry. The children of each
7532 die are visited recursively. */
7534 static void
7535 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7537 unsigned long abbrev_id;
7538 unsigned int n_alloc;
7539 dw_die_ref c;
7540 dw_attr_ref a;
7541 unsigned ix;
7543 /* Scan the DIE references, and replace any that refer to
7544 DIEs from other CUs (i.e. those which are not marked) with
7545 the local stubs we built in optimize_external_refs. */
7546 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7547 if (AT_class (a) == dw_val_class_die_ref
7548 && (c = AT_ref (a))->die_mark == 0)
7550 struct external_ref *ref_p;
7551 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7553 ref_p = lookup_external_ref (extern_map, c);
7554 if (ref_p->stub && ref_p->stub != die)
7555 change_AT_die_ref (a, ref_p->stub);
7556 else
7557 /* We aren't changing this reference, so mark it external. */
7558 set_AT_ref_external (a, 1);
7561 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7563 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7564 dw_attr_ref die_a, abbrev_a;
7565 unsigned ix;
7566 bool ok = true;
7568 if (abbrev->die_tag != die->die_tag)
7569 continue;
7570 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7571 continue;
7573 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7574 continue;
7576 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7578 abbrev_a = &(*abbrev->die_attr)[ix];
7579 if ((abbrev_a->dw_attr != die_a->dw_attr)
7580 || (value_format (abbrev_a) != value_format (die_a)))
7582 ok = false;
7583 break;
7586 if (ok)
7587 break;
7590 if (abbrev_id >= abbrev_die_table_in_use)
7592 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7594 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7595 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7596 n_alloc);
7598 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7599 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7600 abbrev_die_table_allocated = n_alloc;
7603 ++abbrev_die_table_in_use;
7604 abbrev_die_table[abbrev_id] = die;
7607 die->die_abbrev = abbrev_id;
7608 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7611 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7613 static int
7614 constant_size (unsigned HOST_WIDE_INT value)
7616 int log;
7618 if (value == 0)
7619 log = 0;
7620 else
7621 log = floor_log2 (value);
7623 log = log / 8;
7624 log = 1 << (floor_log2 (log) + 1);
7626 return log;
7629 /* Return the size of a DIE as it is represented in the
7630 .debug_info section. */
7632 static unsigned long
7633 size_of_die (dw_die_ref die)
7635 unsigned long size = 0;
7636 dw_attr_ref a;
7637 unsigned ix;
7638 enum dwarf_form form;
7640 size += size_of_uleb128 (die->die_abbrev);
7641 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7643 switch (AT_class (a))
7645 case dw_val_class_addr:
7646 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7648 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7649 size += size_of_uleb128 (AT_index (a));
7651 else
7652 size += DWARF2_ADDR_SIZE;
7653 break;
7654 case dw_val_class_offset:
7655 size += DWARF_OFFSET_SIZE;
7656 break;
7657 case dw_val_class_loc:
7659 unsigned long lsize = size_of_locs (AT_loc (a));
7661 /* Block length. */
7662 if (dwarf_version >= 4)
7663 size += size_of_uleb128 (lsize);
7664 else
7665 size += constant_size (lsize);
7666 size += lsize;
7668 break;
7669 case dw_val_class_loc_list:
7670 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7672 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7673 size += size_of_uleb128 (AT_index (a));
7675 else
7676 size += DWARF_OFFSET_SIZE;
7677 break;
7678 case dw_val_class_range_list:
7679 size += DWARF_OFFSET_SIZE;
7680 break;
7681 case dw_val_class_const:
7682 size += size_of_sleb128 (AT_int (a));
7683 break;
7684 case dw_val_class_unsigned_const:
7686 int csize = constant_size (AT_unsigned (a));
7687 if (dwarf_version == 3
7688 && a->dw_attr == DW_AT_data_member_location
7689 && csize >= 4)
7690 size += size_of_uleb128 (AT_unsigned (a));
7691 else
7692 size += csize;
7694 break;
7695 case dw_val_class_const_double:
7696 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7697 if (HOST_BITS_PER_WIDE_INT >= 64)
7698 size++; /* block */
7699 break;
7700 case dw_val_class_vec:
7701 size += constant_size (a->dw_attr_val.v.val_vec.length
7702 * a->dw_attr_val.v.val_vec.elt_size)
7703 + a->dw_attr_val.v.val_vec.length
7704 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7705 break;
7706 case dw_val_class_flag:
7707 if (dwarf_version >= 4)
7708 /* Currently all add_AT_flag calls pass in 1 as last argument,
7709 so DW_FORM_flag_present can be used. If that ever changes,
7710 we'll need to use DW_FORM_flag and have some optimization
7711 in build_abbrev_table that will change those to
7712 DW_FORM_flag_present if it is set to 1 in all DIEs using
7713 the same abbrev entry. */
7714 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7715 else
7716 size += 1;
7717 break;
7718 case dw_val_class_die_ref:
7719 if (AT_ref_external (a))
7721 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7722 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7723 is sized by target address length, whereas in DWARF3
7724 it's always sized as an offset. */
7725 if (use_debug_types)
7726 size += DWARF_TYPE_SIGNATURE_SIZE;
7727 else if (dwarf_version == 2)
7728 size += DWARF2_ADDR_SIZE;
7729 else
7730 size += DWARF_OFFSET_SIZE;
7732 else
7733 size += DWARF_OFFSET_SIZE;
7734 break;
7735 case dw_val_class_fde_ref:
7736 size += DWARF_OFFSET_SIZE;
7737 break;
7738 case dw_val_class_lbl_id:
7739 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7741 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7742 size += size_of_uleb128 (AT_index (a));
7744 else
7745 size += DWARF2_ADDR_SIZE;
7746 break;
7747 case dw_val_class_lineptr:
7748 case dw_val_class_macptr:
7749 size += DWARF_OFFSET_SIZE;
7750 break;
7751 case dw_val_class_str:
7752 form = AT_string_form (a);
7753 if (form == DW_FORM_strp)
7754 size += DWARF_OFFSET_SIZE;
7755 else if (form == DW_FORM_GNU_str_index)
7756 size += size_of_uleb128 (AT_index (a));
7757 else
7758 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7759 break;
7760 case dw_val_class_file:
7761 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7762 break;
7763 case dw_val_class_data8:
7764 size += 8;
7765 break;
7766 case dw_val_class_vms_delta:
7767 size += DWARF_OFFSET_SIZE;
7768 break;
7769 case dw_val_class_high_pc:
7770 size += DWARF2_ADDR_SIZE;
7771 break;
7772 default:
7773 gcc_unreachable ();
7777 return size;
7780 /* Size the debugging information associated with a given DIE. Visits the
7781 DIE's children recursively. Updates the global variable next_die_offset, on
7782 each time through. Uses the current value of next_die_offset to update the
7783 die_offset field in each DIE. */
7785 static void
7786 calc_die_sizes (dw_die_ref die)
7788 dw_die_ref c;
7790 gcc_assert (die->die_offset == 0
7791 || (unsigned long int) die->die_offset == next_die_offset);
7792 die->die_offset = next_die_offset;
7793 next_die_offset += size_of_die (die);
7795 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7797 if (die->die_child != NULL)
7798 /* Count the null byte used to terminate sibling lists. */
7799 next_die_offset += 1;
7802 /* Size just the base type children at the start of the CU.
7803 This is needed because build_abbrev needs to size locs
7804 and sizing of type based stack ops needs to know die_offset
7805 values for the base types. */
7807 static void
7808 calc_base_type_die_sizes (void)
7810 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7811 unsigned int i;
7812 dw_die_ref base_type;
7813 #if ENABLE_ASSERT_CHECKING
7814 dw_die_ref prev = comp_unit_die ()->die_child;
7815 #endif
7817 die_offset += size_of_die (comp_unit_die ());
7818 for (i = 0; base_types.iterate (i, &base_type); i++)
7820 #if ENABLE_ASSERT_CHECKING
7821 gcc_assert (base_type->die_offset == 0
7822 && prev->die_sib == base_type
7823 && base_type->die_child == NULL
7824 && base_type->die_abbrev);
7825 prev = base_type;
7826 #endif
7827 base_type->die_offset = die_offset;
7828 die_offset += size_of_die (base_type);
7832 /* Set the marks for a die and its children. We do this so
7833 that we know whether or not a reference needs to use FORM_ref_addr; only
7834 DIEs in the same CU will be marked. We used to clear out the offset
7835 and use that as the flag, but ran into ordering problems. */
7837 static void
7838 mark_dies (dw_die_ref die)
7840 dw_die_ref c;
7842 gcc_assert (!die->die_mark);
7844 die->die_mark = 1;
7845 FOR_EACH_CHILD (die, c, mark_dies (c));
7848 /* Clear the marks for a die and its children. */
7850 static void
7851 unmark_dies (dw_die_ref die)
7853 dw_die_ref c;
7855 if (! use_debug_types)
7856 gcc_assert (die->die_mark);
7858 die->die_mark = 0;
7859 FOR_EACH_CHILD (die, c, unmark_dies (c));
7862 /* Clear the marks for a die, its children and referred dies. */
7864 static void
7865 unmark_all_dies (dw_die_ref die)
7867 dw_die_ref c;
7868 dw_attr_ref a;
7869 unsigned ix;
7871 if (!die->die_mark)
7872 return;
7873 die->die_mark = 0;
7875 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7877 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7878 if (AT_class (a) == dw_val_class_die_ref)
7879 unmark_all_dies (AT_ref (a));
7882 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7883 generated for the compilation unit. */
7885 static unsigned long
7886 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7888 unsigned long size;
7889 unsigned i;
7890 pubname_ref p;
7892 size = DWARF_PUBNAMES_HEADER_SIZE;
7893 FOR_EACH_VEC_ELT (*names, i, p)
7894 if (names != pubtype_table
7895 || p->die->die_offset != 0
7896 || !flag_eliminate_unused_debug_types)
7897 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7899 size += DWARF_OFFSET_SIZE;
7900 return size;
7903 /* Return the size of the information in the .debug_aranges section. */
7905 static unsigned long
7906 size_of_aranges (void)
7908 unsigned long size;
7910 size = DWARF_ARANGES_HEADER_SIZE;
7912 /* Count the address/length pair for this compilation unit. */
7913 if (text_section_used)
7914 size += 2 * DWARF2_ADDR_SIZE;
7915 if (cold_text_section_used)
7916 size += 2 * DWARF2_ADDR_SIZE;
7917 if (have_multiple_function_sections)
7919 unsigned fde_idx;
7920 dw_fde_ref fde;
7922 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7924 if (DECL_IGNORED_P (fde->decl))
7925 continue;
7926 if (!fde->in_std_section)
7927 size += 2 * DWARF2_ADDR_SIZE;
7928 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7929 size += 2 * DWARF2_ADDR_SIZE;
7933 /* Count the two zero words used to terminated the address range table. */
7934 size += 2 * DWARF2_ADDR_SIZE;
7935 return size;
7938 /* Select the encoding of an attribute value. */
7940 static enum dwarf_form
7941 value_format (dw_attr_ref a)
7943 switch (AT_class (a))
7945 case dw_val_class_addr:
7946 /* Only very few attributes allow DW_FORM_addr. */
7947 switch (a->dw_attr)
7949 case DW_AT_low_pc:
7950 case DW_AT_high_pc:
7951 case DW_AT_entry_pc:
7952 case DW_AT_trampoline:
7953 return (AT_index (a) == NOT_INDEXED
7954 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7955 default:
7956 break;
7958 switch (DWARF2_ADDR_SIZE)
7960 case 1:
7961 return DW_FORM_data1;
7962 case 2:
7963 return DW_FORM_data2;
7964 case 4:
7965 return DW_FORM_data4;
7966 case 8:
7967 return DW_FORM_data8;
7968 default:
7969 gcc_unreachable ();
7971 case dw_val_class_range_list:
7972 case dw_val_class_loc_list:
7973 if (dwarf_version >= 4)
7974 return DW_FORM_sec_offset;
7975 /* FALLTHRU */
7976 case dw_val_class_vms_delta:
7977 case dw_val_class_offset:
7978 switch (DWARF_OFFSET_SIZE)
7980 case 4:
7981 return DW_FORM_data4;
7982 case 8:
7983 return DW_FORM_data8;
7984 default:
7985 gcc_unreachable ();
7987 case dw_val_class_loc:
7988 if (dwarf_version >= 4)
7989 return DW_FORM_exprloc;
7990 switch (constant_size (size_of_locs (AT_loc (a))))
7992 case 1:
7993 return DW_FORM_block1;
7994 case 2:
7995 return DW_FORM_block2;
7996 case 4:
7997 return DW_FORM_block4;
7998 default:
7999 gcc_unreachable ();
8001 case dw_val_class_const:
8002 return DW_FORM_sdata;
8003 case dw_val_class_unsigned_const:
8004 switch (constant_size (AT_unsigned (a)))
8006 case 1:
8007 return DW_FORM_data1;
8008 case 2:
8009 return DW_FORM_data2;
8010 case 4:
8011 /* In DWARF3 DW_AT_data_member_location with
8012 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8013 constant, so we need to use DW_FORM_udata if we need
8014 a large constant. */
8015 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8016 return DW_FORM_udata;
8017 return DW_FORM_data4;
8018 case 8:
8019 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8020 return DW_FORM_udata;
8021 return DW_FORM_data8;
8022 default:
8023 gcc_unreachable ();
8025 case dw_val_class_const_double:
8026 switch (HOST_BITS_PER_WIDE_INT)
8028 case 8:
8029 return DW_FORM_data2;
8030 case 16:
8031 return DW_FORM_data4;
8032 case 32:
8033 return DW_FORM_data8;
8034 case 64:
8035 default:
8036 return DW_FORM_block1;
8038 case dw_val_class_vec:
8039 switch (constant_size (a->dw_attr_val.v.val_vec.length
8040 * a->dw_attr_val.v.val_vec.elt_size))
8042 case 1:
8043 return DW_FORM_block1;
8044 case 2:
8045 return DW_FORM_block2;
8046 case 4:
8047 return DW_FORM_block4;
8048 default:
8049 gcc_unreachable ();
8051 case dw_val_class_flag:
8052 if (dwarf_version >= 4)
8054 /* Currently all add_AT_flag calls pass in 1 as last argument,
8055 so DW_FORM_flag_present can be used. If that ever changes,
8056 we'll need to use DW_FORM_flag and have some optimization
8057 in build_abbrev_table that will change those to
8058 DW_FORM_flag_present if it is set to 1 in all DIEs using
8059 the same abbrev entry. */
8060 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8061 return DW_FORM_flag_present;
8063 return DW_FORM_flag;
8064 case dw_val_class_die_ref:
8065 if (AT_ref_external (a))
8066 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8067 else
8068 return DW_FORM_ref;
8069 case dw_val_class_fde_ref:
8070 return DW_FORM_data;
8071 case dw_val_class_lbl_id:
8072 return (AT_index (a) == NOT_INDEXED
8073 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8074 case dw_val_class_lineptr:
8075 case dw_val_class_macptr:
8076 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8077 case dw_val_class_str:
8078 return AT_string_form (a);
8079 case dw_val_class_file:
8080 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8082 case 1:
8083 return DW_FORM_data1;
8084 case 2:
8085 return DW_FORM_data2;
8086 case 4:
8087 return DW_FORM_data4;
8088 default:
8089 gcc_unreachable ();
8092 case dw_val_class_data8:
8093 return DW_FORM_data8;
8095 case dw_val_class_high_pc:
8096 switch (DWARF2_ADDR_SIZE)
8098 case 1:
8099 return DW_FORM_data1;
8100 case 2:
8101 return DW_FORM_data2;
8102 case 4:
8103 return DW_FORM_data4;
8104 case 8:
8105 return DW_FORM_data8;
8106 default:
8107 gcc_unreachable ();
8110 default:
8111 gcc_unreachable ();
8115 /* Output the encoding of an attribute value. */
8117 static void
8118 output_value_format (dw_attr_ref a)
8120 enum dwarf_form form = value_format (a);
8122 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8125 /* Given a die and id, produce the appropriate abbreviations. */
8127 static void
8128 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8130 unsigned ix;
8131 dw_attr_ref a_attr;
8133 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8134 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8135 dwarf_tag_name (abbrev->die_tag));
8137 if (abbrev->die_child != NULL)
8138 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8139 else
8140 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8142 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8144 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8145 dwarf_attr_name (a_attr->dw_attr));
8146 output_value_format (a_attr);
8149 dw2_asm_output_data (1, 0, NULL);
8150 dw2_asm_output_data (1, 0, NULL);
8154 /* Output the .debug_abbrev section which defines the DIE abbreviation
8155 table. */
8157 static void
8158 output_abbrev_section (void)
8160 unsigned long abbrev_id;
8162 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8163 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8165 /* Terminate the table. */
8166 dw2_asm_output_data (1, 0, NULL);
8169 /* Output a symbol we can use to refer to this DIE from another CU. */
8171 static inline void
8172 output_die_symbol (dw_die_ref die)
8174 const char *sym = die->die_id.die_symbol;
8176 gcc_assert (!die->comdat_type_p);
8178 if (sym == 0)
8179 return;
8181 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8182 /* We make these global, not weak; if the target doesn't support
8183 .linkonce, it doesn't support combining the sections, so debugging
8184 will break. */
8185 targetm.asm_out.globalize_label (asm_out_file, sym);
8187 ASM_OUTPUT_LABEL (asm_out_file, sym);
8190 /* Return a new location list, given the begin and end range, and the
8191 expression. */
8193 static inline dw_loc_list_ref
8194 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8195 const char *section)
8197 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8199 retlist->begin = begin;
8200 retlist->begin_entry = NULL;
8201 retlist->end = end;
8202 retlist->expr = expr;
8203 retlist->section = section;
8205 return retlist;
8208 /* Generate a new internal symbol for this location list node, if it
8209 hasn't got one yet. */
8211 static inline void
8212 gen_llsym (dw_loc_list_ref list)
8214 gcc_assert (!list->ll_symbol);
8215 list->ll_symbol = gen_internal_sym ("LLST");
8218 /* Output the location list given to us. */
8220 static void
8221 output_loc_list (dw_loc_list_ref list_head)
8223 dw_loc_list_ref curr = list_head;
8225 if (list_head->emitted)
8226 return;
8227 list_head->emitted = true;
8229 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8231 /* Walk the location list, and output each range + expression. */
8232 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8234 unsigned long size;
8235 /* Don't output an entry that starts and ends at the same address. */
8236 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8237 continue;
8238 size = size_of_locs (curr->expr);
8239 /* If the expression is too large, drop it on the floor. We could
8240 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8241 in the expression, but >= 64KB expressions for a single value
8242 in a single range are unlikely very useful. */
8243 if (size > 0xffff)
8244 continue;
8245 if (dwarf_split_debug_info)
8247 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8248 "Location list start/length entry (%s)",
8249 list_head->ll_symbol);
8250 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8251 "Location list range start index (%s)",
8252 curr->begin);
8253 /* The length field is 4 bytes. If we ever need to support
8254 an 8-byte length, we can add a new DW_LLE code or fall back
8255 to DW_LLE_GNU_start_end_entry. */
8256 dw2_asm_output_delta (4, curr->end, curr->begin,
8257 "Location list range length (%s)",
8258 list_head->ll_symbol);
8260 else if (!have_multiple_function_sections)
8262 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8263 "Location list begin address (%s)",
8264 list_head->ll_symbol);
8265 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8266 "Location list end address (%s)",
8267 list_head->ll_symbol);
8269 else
8271 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8272 "Location list begin address (%s)",
8273 list_head->ll_symbol);
8274 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8275 "Location list end address (%s)",
8276 list_head->ll_symbol);
8279 /* Output the block length for this list of location operations. */
8280 gcc_assert (size <= 0xffff);
8281 dw2_asm_output_data (2, size, "%s", "Location expression size");
8283 output_loc_sequence (curr->expr, -1);
8286 if (dwarf_split_debug_info)
8287 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8288 "Location list terminator (%s)",
8289 list_head->ll_symbol);
8290 else
8292 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8293 "Location list terminator begin (%s)",
8294 list_head->ll_symbol);
8295 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8296 "Location list terminator end (%s)",
8297 list_head->ll_symbol);
8301 /* Output a range_list offset into the debug_range section. Emit a
8302 relocated reference if val_entry is NULL, otherwise, emit an
8303 indirect reference. */
8305 static void
8306 output_range_list_offset (dw_attr_ref a)
8308 const char *name = dwarf_attr_name (a->dw_attr);
8310 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8312 char *p = strchr (ranges_section_label, '\0');
8313 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8314 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8315 debug_ranges_section, "%s", name);
8316 *p = '\0';
8318 else
8319 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8320 "%s (offset from %s)", name, ranges_section_label);
8323 /* Output the offset into the debug_loc section. */
8325 static void
8326 output_loc_list_offset (dw_attr_ref a)
8328 char *sym = AT_loc_list (a)->ll_symbol;
8330 gcc_assert (sym);
8331 if (dwarf_split_debug_info)
8332 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8333 "%s", dwarf_attr_name (a->dw_attr));
8334 else
8335 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8336 "%s", dwarf_attr_name (a->dw_attr));
8339 /* Output an attribute's index or value appropriately. */
8341 static void
8342 output_attr_index_or_value (dw_attr_ref a)
8344 const char *name = dwarf_attr_name (a->dw_attr);
8346 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8348 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8349 return;
8351 switch (AT_class (a))
8353 case dw_val_class_addr:
8354 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8355 break;
8356 case dw_val_class_high_pc:
8357 case dw_val_class_lbl_id:
8358 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8359 break;
8360 case dw_val_class_loc_list:
8361 output_loc_list_offset (a);
8362 break;
8363 default:
8364 gcc_unreachable ();
8368 /* Output a type signature. */
8370 static inline void
8371 output_signature (const char *sig, const char *name)
8373 int i;
8375 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8376 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8379 /* Output the DIE and its attributes. Called recursively to generate
8380 the definitions of each child DIE. */
8382 static void
8383 output_die (dw_die_ref die)
8385 dw_attr_ref a;
8386 dw_die_ref c;
8387 unsigned long size;
8388 unsigned ix;
8390 /* If someone in another CU might refer to us, set up a symbol for
8391 them to point to. */
8392 if (! die->comdat_type_p && die->die_id.die_symbol)
8393 output_die_symbol (die);
8395 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8396 (unsigned long)die->die_offset,
8397 dwarf_tag_name (die->die_tag));
8399 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8401 const char *name = dwarf_attr_name (a->dw_attr);
8403 switch (AT_class (a))
8405 case dw_val_class_addr:
8406 output_attr_index_or_value (a);
8407 break;
8409 case dw_val_class_offset:
8410 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8411 "%s", name);
8412 break;
8414 case dw_val_class_range_list:
8415 output_range_list_offset (a);
8416 break;
8418 case dw_val_class_loc:
8419 size = size_of_locs (AT_loc (a));
8421 /* Output the block length for this list of location operations. */
8422 if (dwarf_version >= 4)
8423 dw2_asm_output_data_uleb128 (size, "%s", name);
8424 else
8425 dw2_asm_output_data (constant_size (size), size, "%s", name);
8427 output_loc_sequence (AT_loc (a), -1);
8428 break;
8430 case dw_val_class_const:
8431 /* ??? It would be slightly more efficient to use a scheme like is
8432 used for unsigned constants below, but gdb 4.x does not sign
8433 extend. Gdb 5.x does sign extend. */
8434 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8435 break;
8437 case dw_val_class_unsigned_const:
8439 int csize = constant_size (AT_unsigned (a));
8440 if (dwarf_version == 3
8441 && a->dw_attr == DW_AT_data_member_location
8442 && csize >= 4)
8443 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8444 else
8445 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8447 break;
8449 case dw_val_class_const_double:
8451 unsigned HOST_WIDE_INT first, second;
8453 if (HOST_BITS_PER_WIDE_INT >= 64)
8454 dw2_asm_output_data (1,
8455 HOST_BITS_PER_DOUBLE_INT
8456 / HOST_BITS_PER_CHAR,
8457 NULL);
8459 if (WORDS_BIG_ENDIAN)
8461 first = a->dw_attr_val.v.val_double.high;
8462 second = a->dw_attr_val.v.val_double.low;
8464 else
8466 first = a->dw_attr_val.v.val_double.low;
8467 second = a->dw_attr_val.v.val_double.high;
8470 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8471 first, "%s", name);
8472 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8473 second, NULL);
8475 break;
8477 case dw_val_class_vec:
8479 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8480 unsigned int len = a->dw_attr_val.v.val_vec.length;
8481 unsigned int i;
8482 unsigned char *p;
8484 dw2_asm_output_data (constant_size (len * elt_size),
8485 len * elt_size, "%s", name);
8486 if (elt_size > sizeof (HOST_WIDE_INT))
8488 elt_size /= 2;
8489 len *= 2;
8491 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8492 i < len;
8493 i++, p += elt_size)
8494 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8495 "fp or vector constant word %u", i);
8496 break;
8499 case dw_val_class_flag:
8500 if (dwarf_version >= 4)
8502 /* Currently all add_AT_flag calls pass in 1 as last argument,
8503 so DW_FORM_flag_present can be used. If that ever changes,
8504 we'll need to use DW_FORM_flag and have some optimization
8505 in build_abbrev_table that will change those to
8506 DW_FORM_flag_present if it is set to 1 in all DIEs using
8507 the same abbrev entry. */
8508 gcc_assert (AT_flag (a) == 1);
8509 if (flag_debug_asm)
8510 fprintf (asm_out_file, "\t\t\t%s %s\n",
8511 ASM_COMMENT_START, name);
8512 break;
8514 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8515 break;
8517 case dw_val_class_loc_list:
8518 output_attr_index_or_value (a);
8519 break;
8521 case dw_val_class_die_ref:
8522 if (AT_ref_external (a))
8524 if (AT_ref (a)->comdat_type_p)
8526 comdat_type_node_ref type_node =
8527 AT_ref (a)->die_id.die_type_node;
8529 gcc_assert (type_node);
8530 output_signature (type_node->signature, name);
8532 else
8534 const char *sym = AT_ref (a)->die_id.die_symbol;
8535 int size;
8537 gcc_assert (sym);
8538 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8539 length, whereas in DWARF3 it's always sized as an
8540 offset. */
8541 if (dwarf_version == 2)
8542 size = DWARF2_ADDR_SIZE;
8543 else
8544 size = DWARF_OFFSET_SIZE;
8545 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8546 name);
8549 else
8551 gcc_assert (AT_ref (a)->die_offset);
8552 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8553 "%s", name);
8555 break;
8557 case dw_val_class_fde_ref:
8559 char l1[20];
8561 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8562 a->dw_attr_val.v.val_fde_index * 2);
8563 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8564 "%s", name);
8566 break;
8568 case dw_val_class_vms_delta:
8569 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8570 AT_vms_delta2 (a), AT_vms_delta1 (a),
8571 "%s", name);
8572 break;
8574 case dw_val_class_lbl_id:
8575 output_attr_index_or_value (a);
8576 break;
8578 case dw_val_class_lineptr:
8579 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8580 debug_line_section, "%s", name);
8581 break;
8583 case dw_val_class_macptr:
8584 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8585 debug_macinfo_section, "%s", name);
8586 break;
8588 case dw_val_class_str:
8589 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8590 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8591 a->dw_attr_val.v.val_str->label,
8592 debug_str_section,
8593 "%s: \"%s\"", name, AT_string (a));
8594 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8595 dw2_asm_output_data_uleb128 (AT_index (a),
8596 "%s: \"%s\"", name, AT_string (a));
8597 else
8598 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8599 break;
8601 case dw_val_class_file:
8603 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8605 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8606 a->dw_attr_val.v.val_file->filename);
8607 break;
8610 case dw_val_class_data8:
8612 int i;
8614 for (i = 0; i < 8; i++)
8615 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8616 i == 0 ? "%s" : NULL, name);
8617 break;
8620 case dw_val_class_high_pc:
8621 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8622 get_AT_low_pc (die), "DW_AT_high_pc");
8623 break;
8625 default:
8626 gcc_unreachable ();
8630 FOR_EACH_CHILD (die, c, output_die (c));
8632 /* Add null byte to terminate sibling list. */
8633 if (die->die_child != NULL)
8634 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8635 (unsigned long) die->die_offset);
8638 /* Output the compilation unit that appears at the beginning of the
8639 .debug_info section, and precedes the DIE descriptions. */
8641 static void
8642 output_compilation_unit_header (void)
8644 int ver = dwarf_version;
8646 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8647 dw2_asm_output_data (4, 0xffffffff,
8648 "Initial length escape value indicating 64-bit DWARF extension");
8649 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8650 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8651 "Length of Compilation Unit Info");
8652 dw2_asm_output_data (2, ver, "DWARF version number");
8653 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8654 debug_abbrev_section,
8655 "Offset Into Abbrev. Section");
8656 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8659 /* Output the compilation unit DIE and its children. */
8661 static void
8662 output_comp_unit (dw_die_ref die, int output_if_empty)
8664 const char *secname, *oldsym;
8665 char *tmp;
8666 external_ref_hash_type extern_map;
8668 /* Unless we are outputting main CU, we may throw away empty ones. */
8669 if (!output_if_empty && die->die_child == NULL)
8670 return;
8672 /* Even if there are no children of this DIE, we must output the information
8673 about the compilation unit. Otherwise, on an empty translation unit, we
8674 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8675 will then complain when examining the file. First mark all the DIEs in
8676 this CU so we know which get local refs. */
8677 mark_dies (die);
8679 extern_map = optimize_external_refs (die);
8681 build_abbrev_table (die, extern_map);
8683 extern_map.dispose ();
8685 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8686 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8687 calc_die_sizes (die);
8689 oldsym = die->die_id.die_symbol;
8690 if (oldsym)
8692 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8694 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8695 secname = tmp;
8696 die->die_id.die_symbol = NULL;
8697 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8699 else
8701 switch_to_section (debug_info_section);
8702 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8703 info_section_emitted = true;
8706 /* Output debugging information. */
8707 output_compilation_unit_header ();
8708 output_die (die);
8710 /* Leave the marks on the main CU, so we can check them in
8711 output_pubnames. */
8712 if (oldsym)
8714 unmark_dies (die);
8715 die->die_id.die_symbol = oldsym;
8719 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8720 and .debug_pubtypes. This is configured per-target, but can be
8721 overridden by the -gpubnames or -gno-pubnames options. */
8723 static inline bool
8724 want_pubnames (void)
8726 return (debug_generate_pub_sections != -1
8727 ? debug_generate_pub_sections
8728 : targetm.want_debug_pub_sections);
8731 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8733 static void
8734 add_AT_pubnames (dw_die_ref die)
8736 if (want_pubnames ())
8737 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8740 /* Add a string attribute value to a skeleton DIE. */
8742 static inline void
8743 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8744 const char *str)
8746 dw_attr_node attr;
8747 struct indirect_string_node *node;
8749 if (! skeleton_debug_str_hash)
8750 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8751 debug_str_eq, NULL);
8753 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8754 find_string_form (node);
8755 if (node->form == DW_FORM_GNU_str_index)
8756 node->form = DW_FORM_strp;
8758 attr.dw_attr = attr_kind;
8759 attr.dw_attr_val.val_class = dw_val_class_str;
8760 attr.dw_attr_val.val_entry = NULL;
8761 attr.dw_attr_val.v.val_str = node;
8762 add_dwarf_attr (die, &attr);
8765 /* Helper function to generate top-level dies for skeleton debug_info and
8766 debug_types. */
8768 static void
8769 add_top_level_skeleton_die_attrs (dw_die_ref die)
8771 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8772 const char *comp_dir = comp_dir_string ();
8774 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8775 if (comp_dir != NULL)
8776 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8777 add_AT_pubnames (die);
8778 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8781 /* Return the single type-unit die for skeleton type units. */
8783 static dw_die_ref
8784 get_skeleton_type_unit (void)
8786 /* For dwarf_split_debug_sections with use_type info, all type units in the
8787 skeleton sections have identical dies (but different headers). This
8788 single die will be output many times. */
8790 static dw_die_ref skeleton_type_unit = NULL;
8792 if (skeleton_type_unit == NULL)
8794 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8795 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8796 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8798 return skeleton_type_unit;
8801 /* Output skeleton debug sections that point to the dwo file. */
8803 static void
8804 output_skeleton_debug_sections (dw_die_ref comp_unit)
8806 /* These attributes will be found in the full debug_info section. */
8807 remove_AT (comp_unit, DW_AT_producer);
8808 remove_AT (comp_unit, DW_AT_language);
8810 switch_to_section (debug_skeleton_info_section);
8811 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8813 /* Produce the skeleton compilation-unit header. This one differs enough from
8814 a normal CU header that it's better not to call output_compilation_unit
8815 header. */
8816 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8817 dw2_asm_output_data (4, 0xffffffff,
8818 "Initial length escape value indicating 64-bit DWARF extension");
8820 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8821 DWARF_COMPILE_UNIT_HEADER_SIZE
8822 - DWARF_INITIAL_LENGTH_SIZE
8823 + size_of_die (comp_unit),
8824 "Length of Compilation Unit Info");
8825 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8827 debug_abbrev_section,
8828 "Offset Into Abbrev. Section");
8829 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8831 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8832 output_die (comp_unit);
8834 /* Build the skeleton debug_abbrev section. */
8835 switch_to_section (debug_skeleton_abbrev_section);
8836 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8838 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8839 if (use_debug_types)
8840 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8842 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8845 /* Output a comdat type unit DIE and its children. */
8847 static void
8848 output_comdat_type_unit (comdat_type_node *node)
8850 const char *secname;
8851 char *tmp;
8852 int i;
8853 #if defined (OBJECT_FORMAT_ELF)
8854 tree comdat_key;
8855 #endif
8856 external_ref_hash_type extern_map;
8858 /* First mark all the DIEs in this CU so we know which get local refs. */
8859 mark_dies (node->root_die);
8861 extern_map = optimize_external_refs (node->root_die);
8863 build_abbrev_table (node->root_die, extern_map);
8865 extern_map.dispose ();
8867 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8868 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8869 calc_die_sizes (node->root_die);
8871 #if defined (OBJECT_FORMAT_ELF)
8872 if (!dwarf_split_debug_info)
8873 secname = ".debug_types";
8874 else
8875 secname = ".debug_types.dwo";
8877 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8878 sprintf (tmp, "wt.");
8879 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8880 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8881 comdat_key = get_identifier (tmp);
8882 targetm.asm_out.named_section (secname,
8883 SECTION_DEBUG | SECTION_LINKONCE,
8884 comdat_key);
8885 #else
8886 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8887 sprintf (tmp, ".gnu.linkonce.wt.");
8888 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8889 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8890 secname = tmp;
8891 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8892 #endif
8894 /* Output debugging information. */
8895 output_compilation_unit_header ();
8896 output_signature (node->signature, "Type Signature");
8897 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8898 "Offset to Type DIE");
8899 output_die (node->root_die);
8901 unmark_dies (node->root_die);
8903 #if defined (OBJECT_FORMAT_ELF)
8904 if (dwarf_split_debug_info)
8906 /* Produce the skeleton type-unit header. */
8907 const char *secname = ".debug_types";
8909 targetm.asm_out.named_section (secname,
8910 SECTION_DEBUG | SECTION_LINKONCE,
8911 comdat_key);
8912 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8913 dw2_asm_output_data (4, 0xffffffff,
8914 "Initial length escape value indicating 64-bit DWARF extension");
8916 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8917 DWARF_COMPILE_UNIT_HEADER_SIZE
8918 - DWARF_INITIAL_LENGTH_SIZE
8919 + size_of_die (get_skeleton_type_unit ())
8920 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8921 "Length of Type Unit Info");
8922 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8923 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8924 debug_skeleton_abbrev_section_label,
8925 debug_abbrev_section,
8926 "Offset Into Abbrev. Section");
8927 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8928 output_signature (node->signature, "Type Signature");
8929 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8931 output_die (get_skeleton_type_unit ());
8933 #endif
8936 /* Return the DWARF2/3 pubname associated with a decl. */
8938 static const char *
8939 dwarf2_name (tree decl, int scope)
8941 if (DECL_NAMELESS (decl))
8942 return NULL;
8943 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8946 /* Add a new entry to .debug_pubnames if appropriate. */
8948 static void
8949 add_pubname_string (const char *str, dw_die_ref die)
8951 pubname_entry e;
8953 e.die = die;
8954 e.name = xstrdup (str);
8955 vec_safe_push (pubname_table, e);
8958 static void
8959 add_pubname (tree decl, dw_die_ref die)
8961 if (!want_pubnames ())
8962 return;
8964 /* Don't add items to the table when we expect that the consumer will have
8965 just read the enclosing die. For example, if the consumer is looking at a
8966 class_member, it will either be inside the class already, or will have just
8967 looked up the class to find the member. Either way, searching the class is
8968 faster than searching the index. */
8969 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8970 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8972 const char *name = dwarf2_name (decl, 1);
8974 if (name)
8975 add_pubname_string (name, die);
8979 /* Add an enumerator to the pubnames section. */
8981 static void
8982 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8984 pubname_entry e;
8986 gcc_assert (scope_name);
8987 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8988 e.die = die;
8989 vec_safe_push (pubname_table, e);
8992 /* Add a new entry to .debug_pubtypes if appropriate. */
8994 static void
8995 add_pubtype (tree decl, dw_die_ref die)
8997 pubname_entry e;
8999 if (!want_pubnames ())
9000 return;
9002 if ((TREE_PUBLIC (decl)
9003 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9004 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9006 tree scope = NULL;
9007 const char *scope_name = "";
9008 const char *sep = is_cxx () ? "::" : ".";
9009 const char *name;
9011 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9012 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9014 scope_name = lang_hooks.dwarf_name (scope, 1);
9015 if (scope_name != NULL && scope_name[0] != '\0')
9016 scope_name = concat (scope_name, sep, NULL);
9017 else
9018 scope_name = "";
9021 if (TYPE_P (decl))
9022 name = type_tag (decl);
9023 else
9024 name = lang_hooks.dwarf_name (decl, 1);
9026 /* If we don't have a name for the type, there's no point in adding
9027 it to the table. */
9028 if (name != NULL && name[0] != '\0')
9030 e.die = die;
9031 e.name = concat (scope_name, name, NULL);
9032 vec_safe_push (pubtype_table, e);
9035 /* Although it might be more consistent to add the pubinfo for the
9036 enumerators as their dies are created, they should only be added if the
9037 enum type meets the criteria above. So rather than re-check the parent
9038 enum type whenever an enumerator die is created, just output them all
9039 here. This isn't protected by the name conditional because anonymous
9040 enums don't have names. */
9041 if (die->die_tag == DW_TAG_enumeration_type)
9043 dw_die_ref c;
9045 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9050 /* Output the public names table used to speed up access to externally
9051 visible names; or the public types table used to find type definitions. */
9053 static void
9054 output_pubnames (vec<pubname_entry, va_gc> *names)
9056 unsigned i;
9057 unsigned long pubnames_length = size_of_pubnames (names);
9058 pubname_ref pub;
9060 if (!want_pubnames () || !info_section_emitted)
9061 return;
9062 if (names == pubname_table)
9063 switch_to_section (debug_pubnames_section);
9064 else
9065 switch_to_section (debug_pubtypes_section);
9066 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9067 dw2_asm_output_data (4, 0xffffffff,
9068 "Initial length escape value indicating 64-bit DWARF extension");
9069 if (names == pubname_table)
9070 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9071 "Length of Public Names Info");
9072 else
9073 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9074 "Length of Public Type Names Info");
9075 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
9076 dw2_asm_output_data (2, 2, "DWARF Version");
9077 if (dwarf_split_debug_info)
9078 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9079 debug_skeleton_info_section,
9080 "Offset of Compilation Unit Info");
9081 else
9082 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9083 debug_info_section,
9084 "Offset of Compilation Unit Info");
9085 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9086 "Compilation Unit Length");
9088 FOR_EACH_VEC_ELT (*names, i, pub)
9090 /* Enumerator names are part of the pubname table, but the parent
9091 DW_TAG_enumeration_type die may have been pruned. Don't output
9092 them if that is the case. */
9093 if (pub->die->die_tag == DW_TAG_enumerator &&
9094 (pub->die->die_parent == NULL
9095 || !pub->die->die_parent->die_perennial_p))
9096 continue;
9098 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9099 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9100 gcc_assert (pub->die->die_mark);
9102 if (names != pubtype_table
9103 || pub->die->die_offset != 0
9104 || !flag_eliminate_unused_debug_types)
9106 dw_offset die_offset = pub->die->die_offset;
9108 /* If we're putting types in their own .debug_types sections,
9109 the .debug_pubtypes table will still point to the compile
9110 unit (not the type unit), so we want to use the offset of
9111 the skeleton DIE (if there is one). */
9112 if (pub->die->comdat_type_p && names == pubtype_table)
9114 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9116 if (type_node != NULL)
9117 die_offset = (type_node->skeleton_die != NULL
9118 ? type_node->skeleton_die->die_offset
9119 : 0);
9122 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9124 dw2_asm_output_nstring (pub->name, -1, "external name");
9128 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9131 /* Output the information that goes into the .debug_aranges table.
9132 Namely, define the beginning and ending address range of the
9133 text section generated for this compilation unit. */
9135 static void
9136 output_aranges (unsigned long aranges_length)
9138 unsigned i;
9140 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9141 dw2_asm_output_data (4, 0xffffffff,
9142 "Initial length escape value indicating 64-bit DWARF extension");
9143 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9144 "Length of Address Ranges Info");
9145 /* Version number for aranges is still 2, even in DWARF3. */
9146 dw2_asm_output_data (2, 2, "DWARF Version");
9147 if (dwarf_split_debug_info)
9148 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9149 debug_skeleton_info_section,
9150 "Offset of Compilation Unit Info");
9151 else
9152 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9153 debug_info_section,
9154 "Offset of Compilation Unit Info");
9155 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9156 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9158 /* We need to align to twice the pointer size here. */
9159 if (DWARF_ARANGES_PAD_SIZE)
9161 /* Pad using a 2 byte words so that padding is correct for any
9162 pointer size. */
9163 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9164 2 * DWARF2_ADDR_SIZE);
9165 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9166 dw2_asm_output_data (2, 0, NULL);
9169 /* It is necessary not to output these entries if the sections were
9170 not used; if the sections were not used, the length will be 0 and
9171 the address may end up as 0 if the section is discarded by ld
9172 --gc-sections, leaving an invalid (0, 0) entry that can be
9173 confused with the terminator. */
9174 if (text_section_used)
9176 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9177 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9178 text_section_label, "Length");
9180 if (cold_text_section_used)
9182 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9183 "Address");
9184 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9185 cold_text_section_label, "Length");
9188 if (have_multiple_function_sections)
9190 unsigned fde_idx;
9191 dw_fde_ref fde;
9193 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9195 if (DECL_IGNORED_P (fde->decl))
9196 continue;
9197 if (!fde->in_std_section)
9199 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9200 "Address");
9201 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9202 fde->dw_fde_begin, "Length");
9204 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9206 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9207 "Address");
9208 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9209 fde->dw_fde_second_begin, "Length");
9214 /* Output the terminator words. */
9215 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9216 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9219 /* Add a new entry to .debug_ranges. Return the offset at which it
9220 was placed. */
9222 static unsigned int
9223 add_ranges_num (int num)
9225 unsigned int in_use = ranges_table_in_use;
9227 if (in_use == ranges_table_allocated)
9229 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9230 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9231 ranges_table_allocated);
9232 memset (ranges_table + ranges_table_in_use, 0,
9233 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9236 ranges_table[in_use].num = num;
9237 ranges_table_in_use = in_use + 1;
9239 return in_use * 2 * DWARF2_ADDR_SIZE;
9242 /* Add a new entry to .debug_ranges corresponding to a block, or a
9243 range terminator if BLOCK is NULL. */
9245 static unsigned int
9246 add_ranges (const_tree block)
9248 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9251 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9252 When using dwarf_split_debug_info, address attributes in dies destined
9253 for the final executable should be direct references--setting the
9254 parameter force_direct ensures this behavior. */
9256 static void
9257 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9258 bool *added, bool force_direct)
9260 unsigned int in_use = ranges_by_label_in_use;
9261 unsigned int offset;
9263 if (in_use == ranges_by_label_allocated)
9265 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9266 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9267 ranges_by_label,
9268 ranges_by_label_allocated);
9269 memset (ranges_by_label + ranges_by_label_in_use, 0,
9270 RANGES_TABLE_INCREMENT
9271 * sizeof (struct dw_ranges_by_label_struct));
9274 ranges_by_label[in_use].begin = begin;
9275 ranges_by_label[in_use].end = end;
9276 ranges_by_label_in_use = in_use + 1;
9278 offset = add_ranges_num (-(int)in_use - 1);
9279 if (!*added)
9281 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9282 *added = true;
9286 static void
9287 output_ranges (void)
9289 unsigned i;
9290 static const char *const start_fmt = "Offset %#x";
9291 const char *fmt = start_fmt;
9293 for (i = 0; i < ranges_table_in_use; i++)
9295 int block_num = ranges_table[i].num;
9297 if (block_num > 0)
9299 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9300 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9302 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9303 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9305 /* If all code is in the text section, then the compilation
9306 unit base address defaults to DW_AT_low_pc, which is the
9307 base of the text section. */
9308 if (!have_multiple_function_sections)
9310 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9311 text_section_label,
9312 fmt, i * 2 * DWARF2_ADDR_SIZE);
9313 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9314 text_section_label, NULL);
9317 /* Otherwise, the compilation unit base address is zero,
9318 which allows us to use absolute addresses, and not worry
9319 about whether the target supports cross-section
9320 arithmetic. */
9321 else
9323 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9324 fmt, i * 2 * DWARF2_ADDR_SIZE);
9325 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9328 fmt = NULL;
9331 /* Negative block_num stands for an index into ranges_by_label. */
9332 else if (block_num < 0)
9334 int lab_idx = - block_num - 1;
9336 if (!have_multiple_function_sections)
9338 gcc_unreachable ();
9339 #if 0
9340 /* If we ever use add_ranges_by_labels () for a single
9341 function section, all we have to do is to take out
9342 the #if 0 above. */
9343 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9344 ranges_by_label[lab_idx].begin,
9345 text_section_label,
9346 fmt, i * 2 * DWARF2_ADDR_SIZE);
9347 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9348 ranges_by_label[lab_idx].end,
9349 text_section_label, NULL);
9350 #endif
9352 else
9354 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9355 ranges_by_label[lab_idx].begin,
9356 fmt, i * 2 * DWARF2_ADDR_SIZE);
9357 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9358 ranges_by_label[lab_idx].end,
9359 NULL);
9362 else
9364 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9365 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9366 fmt = start_fmt;
9371 /* Data structure containing information about input files. */
9372 struct file_info
9374 const char *path; /* Complete file name. */
9375 const char *fname; /* File name part. */
9376 int length; /* Length of entire string. */
9377 struct dwarf_file_data * file_idx; /* Index in input file table. */
9378 int dir_idx; /* Index in directory table. */
9381 /* Data structure containing information about directories with source
9382 files. */
9383 struct dir_info
9385 const char *path; /* Path including directory name. */
9386 int length; /* Path length. */
9387 int prefix; /* Index of directory entry which is a prefix. */
9388 int count; /* Number of files in this directory. */
9389 int dir_idx; /* Index of directory used as base. */
9392 /* Callback function for file_info comparison. We sort by looking at
9393 the directories in the path. */
9395 static int
9396 file_info_cmp (const void *p1, const void *p2)
9398 const struct file_info *const s1 = (const struct file_info *) p1;
9399 const struct file_info *const s2 = (const struct file_info *) p2;
9400 const unsigned char *cp1;
9401 const unsigned char *cp2;
9403 /* Take care of file names without directories. We need to make sure that
9404 we return consistent values to qsort since some will get confused if
9405 we return the same value when identical operands are passed in opposite
9406 orders. So if neither has a directory, return 0 and otherwise return
9407 1 or -1 depending on which one has the directory. */
9408 if ((s1->path == s1->fname || s2->path == s2->fname))
9409 return (s2->path == s2->fname) - (s1->path == s1->fname);
9411 cp1 = (const unsigned char *) s1->path;
9412 cp2 = (const unsigned char *) s2->path;
9414 while (1)
9416 ++cp1;
9417 ++cp2;
9418 /* Reached the end of the first path? If so, handle like above. */
9419 if ((cp1 == (const unsigned char *) s1->fname)
9420 || (cp2 == (const unsigned char *) s2->fname))
9421 return ((cp2 == (const unsigned char *) s2->fname)
9422 - (cp1 == (const unsigned char *) s1->fname));
9424 /* Character of current path component the same? */
9425 else if (*cp1 != *cp2)
9426 return *cp1 - *cp2;
9430 struct file_name_acquire_data
9432 struct file_info *files;
9433 int used_files;
9434 int max_files;
9437 /* Traversal function for the hash table. */
9439 static int
9440 file_name_acquire (void ** slot, void *data)
9442 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9443 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9444 struct file_info *fi;
9445 const char *f;
9447 gcc_assert (fnad->max_files >= d->emitted_number);
9449 if (! d->emitted_number)
9450 return 1;
9452 gcc_assert (fnad->max_files != fnad->used_files);
9454 fi = fnad->files + fnad->used_files++;
9456 /* Skip all leading "./". */
9457 f = d->filename;
9458 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9459 f += 2;
9461 /* Create a new array entry. */
9462 fi->path = f;
9463 fi->length = strlen (f);
9464 fi->file_idx = d;
9466 /* Search for the file name part. */
9467 f = strrchr (f, DIR_SEPARATOR);
9468 #if defined (DIR_SEPARATOR_2)
9470 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9472 if (g != NULL)
9474 if (f == NULL || f < g)
9475 f = g;
9478 #endif
9480 fi->fname = f == NULL ? fi->path : f + 1;
9481 return 1;
9484 /* Output the directory table and the file name table. We try to minimize
9485 the total amount of memory needed. A heuristic is used to avoid large
9486 slowdowns with many input files. */
9488 static void
9489 output_file_names (void)
9491 struct file_name_acquire_data fnad;
9492 int numfiles;
9493 struct file_info *files;
9494 struct dir_info *dirs;
9495 int *saved;
9496 int *savehere;
9497 int *backmap;
9498 int ndirs;
9499 int idx_offset;
9500 int i;
9502 if (!last_emitted_file)
9504 dw2_asm_output_data (1, 0, "End directory table");
9505 dw2_asm_output_data (1, 0, "End file name table");
9506 return;
9509 numfiles = last_emitted_file->emitted_number;
9511 /* Allocate the various arrays we need. */
9512 files = XALLOCAVEC (struct file_info, numfiles);
9513 dirs = XALLOCAVEC (struct dir_info, numfiles);
9515 fnad.files = files;
9516 fnad.used_files = 0;
9517 fnad.max_files = numfiles;
9518 htab_traverse (file_table, file_name_acquire, &fnad);
9519 gcc_assert (fnad.used_files == fnad.max_files);
9521 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9523 /* Find all the different directories used. */
9524 dirs[0].path = files[0].path;
9525 dirs[0].length = files[0].fname - files[0].path;
9526 dirs[0].prefix = -1;
9527 dirs[0].count = 1;
9528 dirs[0].dir_idx = 0;
9529 files[0].dir_idx = 0;
9530 ndirs = 1;
9532 for (i = 1; i < numfiles; i++)
9533 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9534 && memcmp (dirs[ndirs - 1].path, files[i].path,
9535 dirs[ndirs - 1].length) == 0)
9537 /* Same directory as last entry. */
9538 files[i].dir_idx = ndirs - 1;
9539 ++dirs[ndirs - 1].count;
9541 else
9543 int j;
9545 /* This is a new directory. */
9546 dirs[ndirs].path = files[i].path;
9547 dirs[ndirs].length = files[i].fname - files[i].path;
9548 dirs[ndirs].count = 1;
9549 dirs[ndirs].dir_idx = ndirs;
9550 files[i].dir_idx = ndirs;
9552 /* Search for a prefix. */
9553 dirs[ndirs].prefix = -1;
9554 for (j = 0; j < ndirs; j++)
9555 if (dirs[j].length < dirs[ndirs].length
9556 && dirs[j].length > 1
9557 && (dirs[ndirs].prefix == -1
9558 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9559 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9560 dirs[ndirs].prefix = j;
9562 ++ndirs;
9565 /* Now to the actual work. We have to find a subset of the directories which
9566 allow expressing the file name using references to the directory table
9567 with the least amount of characters. We do not do an exhaustive search
9568 where we would have to check out every combination of every single
9569 possible prefix. Instead we use a heuristic which provides nearly optimal
9570 results in most cases and never is much off. */
9571 saved = XALLOCAVEC (int, ndirs);
9572 savehere = XALLOCAVEC (int, ndirs);
9574 memset (saved, '\0', ndirs * sizeof (saved[0]));
9575 for (i = 0; i < ndirs; i++)
9577 int j;
9578 int total;
9580 /* We can always save some space for the current directory. But this
9581 does not mean it will be enough to justify adding the directory. */
9582 savehere[i] = dirs[i].length;
9583 total = (savehere[i] - saved[i]) * dirs[i].count;
9585 for (j = i + 1; j < ndirs; j++)
9587 savehere[j] = 0;
9588 if (saved[j] < dirs[i].length)
9590 /* Determine whether the dirs[i] path is a prefix of the
9591 dirs[j] path. */
9592 int k;
9594 k = dirs[j].prefix;
9595 while (k != -1 && k != (int) i)
9596 k = dirs[k].prefix;
9598 if (k == (int) i)
9600 /* Yes it is. We can possibly save some memory by
9601 writing the filenames in dirs[j] relative to
9602 dirs[i]. */
9603 savehere[j] = dirs[i].length;
9604 total += (savehere[j] - saved[j]) * dirs[j].count;
9609 /* Check whether we can save enough to justify adding the dirs[i]
9610 directory. */
9611 if (total > dirs[i].length + 1)
9613 /* It's worthwhile adding. */
9614 for (j = i; j < ndirs; j++)
9615 if (savehere[j] > 0)
9617 /* Remember how much we saved for this directory so far. */
9618 saved[j] = savehere[j];
9620 /* Remember the prefix directory. */
9621 dirs[j].dir_idx = i;
9626 /* Emit the directory name table. */
9627 idx_offset = dirs[0].length > 0 ? 1 : 0;
9628 for (i = 1 - idx_offset; i < ndirs; i++)
9629 dw2_asm_output_nstring (dirs[i].path,
9630 dirs[i].length
9631 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9632 "Directory Entry: %#x", i + idx_offset);
9634 dw2_asm_output_data (1, 0, "End directory table");
9636 /* We have to emit them in the order of emitted_number since that's
9637 used in the debug info generation. To do this efficiently we
9638 generate a back-mapping of the indices first. */
9639 backmap = XALLOCAVEC (int, numfiles);
9640 for (i = 0; i < numfiles; i++)
9641 backmap[files[i].file_idx->emitted_number - 1] = i;
9643 /* Now write all the file names. */
9644 for (i = 0; i < numfiles; i++)
9646 int file_idx = backmap[i];
9647 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9649 #ifdef VMS_DEBUGGING_INFO
9650 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9652 /* Setting these fields can lead to debugger miscomparisons,
9653 but VMS Debug requires them to be set correctly. */
9655 int ver;
9656 long long cdt;
9657 long siz;
9658 int maxfilelen = strlen (files[file_idx].path)
9659 + dirs[dir_idx].length
9660 + MAX_VMS_VERSION_LEN + 1;
9661 char *filebuf = XALLOCAVEC (char, maxfilelen);
9663 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9664 snprintf (filebuf, maxfilelen, "%s;%d",
9665 files[file_idx].path + dirs[dir_idx].length, ver);
9667 dw2_asm_output_nstring
9668 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9670 /* Include directory index. */
9671 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9673 /* Modification time. */
9674 dw2_asm_output_data_uleb128
9675 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9676 ? cdt : 0,
9677 NULL);
9679 /* File length in bytes. */
9680 dw2_asm_output_data_uleb128
9681 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9682 ? siz : 0,
9683 NULL);
9684 #else
9685 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9686 "File Entry: %#x", (unsigned) i + 1);
9688 /* Include directory index. */
9689 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9691 /* Modification time. */
9692 dw2_asm_output_data_uleb128 (0, NULL);
9694 /* File length in bytes. */
9695 dw2_asm_output_data_uleb128 (0, NULL);
9696 #endif /* VMS_DEBUGGING_INFO */
9699 dw2_asm_output_data (1, 0, "End file name table");
9703 /* Output one line number table into the .debug_line section. */
9705 static void
9706 output_one_line_info_table (dw_line_info_table *table)
9708 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9709 unsigned int current_line = 1;
9710 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9711 dw_line_info_entry *ent;
9712 size_t i;
9714 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9716 switch (ent->opcode)
9718 case LI_set_address:
9719 /* ??? Unfortunately, we have little choice here currently, and
9720 must always use the most general form. GCC does not know the
9721 address delta itself, so we can't use DW_LNS_advance_pc. Many
9722 ports do have length attributes which will give an upper bound
9723 on the address range. We could perhaps use length attributes
9724 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9725 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9727 /* This can handle any delta. This takes
9728 4+DWARF2_ADDR_SIZE bytes. */
9729 dw2_asm_output_data (1, 0, "set address %s", line_label);
9730 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9731 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9732 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9733 break;
9735 case LI_set_line:
9736 if (ent->val == current_line)
9738 /* We still need to start a new row, so output a copy insn. */
9739 dw2_asm_output_data (1, DW_LNS_copy,
9740 "copy line %u", current_line);
9742 else
9744 int line_offset = ent->val - current_line;
9745 int line_delta = line_offset - DWARF_LINE_BASE;
9747 current_line = ent->val;
9748 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9750 /* This can handle deltas from -10 to 234, using the current
9751 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9752 This takes 1 byte. */
9753 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9754 "line %u", current_line);
9756 else
9758 /* This can handle any delta. This takes at least 4 bytes,
9759 depending on the value being encoded. */
9760 dw2_asm_output_data (1, DW_LNS_advance_line,
9761 "advance to line %u", current_line);
9762 dw2_asm_output_data_sleb128 (line_offset, NULL);
9763 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9766 break;
9768 case LI_set_file:
9769 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9770 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9771 break;
9773 case LI_set_column:
9774 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9775 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9776 break;
9778 case LI_negate_stmt:
9779 current_is_stmt = !current_is_stmt;
9780 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9781 "is_stmt %d", current_is_stmt);
9782 break;
9784 case LI_set_prologue_end:
9785 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9786 "set prologue end");
9787 break;
9789 case LI_set_epilogue_begin:
9790 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9791 "set epilogue begin");
9792 break;
9794 case LI_set_discriminator:
9795 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9796 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9797 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9798 dw2_asm_output_data_uleb128 (ent->val, NULL);
9799 break;
9803 /* Emit debug info for the address of the end of the table. */
9804 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9805 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9806 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9807 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9809 dw2_asm_output_data (1, 0, "end sequence");
9810 dw2_asm_output_data_uleb128 (1, NULL);
9811 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9814 /* Output the source line number correspondence information. This
9815 information goes into the .debug_line section. */
9817 static void
9818 output_line_info (bool prologue_only)
9820 char l1[20], l2[20], p1[20], p2[20];
9821 int ver = dwarf_version;
9822 bool saw_one = false;
9823 int opc;
9825 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9826 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9827 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9828 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9830 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9831 dw2_asm_output_data (4, 0xffffffff,
9832 "Initial length escape value indicating 64-bit DWARF extension");
9833 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9834 "Length of Source Line Info");
9835 ASM_OUTPUT_LABEL (asm_out_file, l1);
9837 dw2_asm_output_data (2, ver, "DWARF Version");
9838 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9839 ASM_OUTPUT_LABEL (asm_out_file, p1);
9841 /* Define the architecture-dependent minimum instruction length (in bytes).
9842 In this implementation of DWARF, this field is used for information
9843 purposes only. Since GCC generates assembly language, we have no
9844 a priori knowledge of how many instruction bytes are generated for each
9845 source line, and therefore can use only the DW_LNE_set_address and
9846 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9847 this as '1', which is "correct enough" for all architectures,
9848 and don't let the target override. */
9849 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9851 if (ver >= 4)
9852 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9853 "Maximum Operations Per Instruction");
9854 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9855 "Default is_stmt_start flag");
9856 dw2_asm_output_data (1, DWARF_LINE_BASE,
9857 "Line Base Value (Special Opcodes)");
9858 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9859 "Line Range Value (Special Opcodes)");
9860 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9861 "Special Opcode Base");
9863 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9865 int n_op_args;
9866 switch (opc)
9868 case DW_LNS_advance_pc:
9869 case DW_LNS_advance_line:
9870 case DW_LNS_set_file:
9871 case DW_LNS_set_column:
9872 case DW_LNS_fixed_advance_pc:
9873 case DW_LNS_set_isa:
9874 n_op_args = 1;
9875 break;
9876 default:
9877 n_op_args = 0;
9878 break;
9881 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9882 opc, n_op_args);
9885 /* Write out the information about the files we use. */
9886 output_file_names ();
9887 ASM_OUTPUT_LABEL (asm_out_file, p2);
9888 if (prologue_only)
9890 /* Output the marker for the end of the line number info. */
9891 ASM_OUTPUT_LABEL (asm_out_file, l2);
9892 return;
9895 if (separate_line_info)
9897 dw_line_info_table *table;
9898 size_t i;
9900 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9901 if (table->in_use)
9903 output_one_line_info_table (table);
9904 saw_one = true;
9907 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9909 output_one_line_info_table (cold_text_section_line_info);
9910 saw_one = true;
9913 /* ??? Some Darwin linkers crash on a .debug_line section with no
9914 sequences. Further, merely a DW_LNE_end_sequence entry is not
9915 sufficient -- the address column must also be initialized.
9916 Make sure to output at least one set_address/end_sequence pair,
9917 choosing .text since that section is always present. */
9918 if (text_section_line_info->in_use || !saw_one)
9919 output_one_line_info_table (text_section_line_info);
9921 /* Output the marker for the end of the line number info. */
9922 ASM_OUTPUT_LABEL (asm_out_file, l2);
9925 /* Given a pointer to a tree node for some base type, return a pointer to
9926 a DIE that describes the given type.
9928 This routine must only be called for GCC type nodes that correspond to
9929 Dwarf base (fundamental) types. */
9931 static dw_die_ref
9932 base_type_die (tree type)
9934 dw_die_ref base_type_result;
9935 enum dwarf_type encoding;
9937 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9938 return 0;
9940 /* If this is a subtype that should not be emitted as a subrange type,
9941 use the base type. See subrange_type_for_debug_p. */
9942 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9943 type = TREE_TYPE (type);
9945 switch (TREE_CODE (type))
9947 case INTEGER_TYPE:
9948 if ((dwarf_version >= 4 || !dwarf_strict)
9949 && TYPE_NAME (type)
9950 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9951 && DECL_IS_BUILTIN (TYPE_NAME (type))
9952 && DECL_NAME (TYPE_NAME (type)))
9954 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9955 if (strcmp (name, "char16_t") == 0
9956 || strcmp (name, "char32_t") == 0)
9958 encoding = DW_ATE_UTF;
9959 break;
9962 if (TYPE_STRING_FLAG (type))
9964 if (TYPE_UNSIGNED (type))
9965 encoding = DW_ATE_unsigned_char;
9966 else
9967 encoding = DW_ATE_signed_char;
9969 else if (TYPE_UNSIGNED (type))
9970 encoding = DW_ATE_unsigned;
9971 else
9972 encoding = DW_ATE_signed;
9973 break;
9975 case REAL_TYPE:
9976 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9978 if (dwarf_version >= 3 || !dwarf_strict)
9979 encoding = DW_ATE_decimal_float;
9980 else
9981 encoding = DW_ATE_lo_user;
9983 else
9984 encoding = DW_ATE_float;
9985 break;
9987 case FIXED_POINT_TYPE:
9988 if (!(dwarf_version >= 3 || !dwarf_strict))
9989 encoding = DW_ATE_lo_user;
9990 else if (TYPE_UNSIGNED (type))
9991 encoding = DW_ATE_unsigned_fixed;
9992 else
9993 encoding = DW_ATE_signed_fixed;
9994 break;
9996 /* Dwarf2 doesn't know anything about complex ints, so use
9997 a user defined type for it. */
9998 case COMPLEX_TYPE:
9999 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10000 encoding = DW_ATE_complex_float;
10001 else
10002 encoding = DW_ATE_lo_user;
10003 break;
10005 case BOOLEAN_TYPE:
10006 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10007 encoding = DW_ATE_boolean;
10008 break;
10010 default:
10011 /* No other TREE_CODEs are Dwarf fundamental types. */
10012 gcc_unreachable ();
10015 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10017 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10018 int_size_in_bytes (type));
10019 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10020 add_pubtype (type, base_type_result);
10022 return base_type_result;
10025 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10026 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10028 static inline int
10029 is_base_type (tree type)
10031 switch (TREE_CODE (type))
10033 case ERROR_MARK:
10034 case VOID_TYPE:
10035 case INTEGER_TYPE:
10036 case REAL_TYPE:
10037 case FIXED_POINT_TYPE:
10038 case COMPLEX_TYPE:
10039 case BOOLEAN_TYPE:
10040 return 1;
10042 case ARRAY_TYPE:
10043 case RECORD_TYPE:
10044 case UNION_TYPE:
10045 case QUAL_UNION_TYPE:
10046 case ENUMERAL_TYPE:
10047 case FUNCTION_TYPE:
10048 case METHOD_TYPE:
10049 case POINTER_TYPE:
10050 case REFERENCE_TYPE:
10051 case NULLPTR_TYPE:
10052 case OFFSET_TYPE:
10053 case LANG_TYPE:
10054 case VECTOR_TYPE:
10055 return 0;
10057 default:
10058 gcc_unreachable ();
10061 return 0;
10064 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10065 node, return the size in bits for the type if it is a constant, or else
10066 return the alignment for the type if the type's size is not constant, or
10067 else return BITS_PER_WORD if the type actually turns out to be an
10068 ERROR_MARK node. */
10070 static inline unsigned HOST_WIDE_INT
10071 simple_type_size_in_bits (const_tree type)
10073 if (TREE_CODE (type) == ERROR_MARK)
10074 return BITS_PER_WORD;
10075 else if (TYPE_SIZE (type) == NULL_TREE)
10076 return 0;
10077 else if (host_integerp (TYPE_SIZE (type), 1))
10078 return tree_low_cst (TYPE_SIZE (type), 1);
10079 else
10080 return TYPE_ALIGN (type);
10083 /* Similarly, but return a double_int instead of UHWI. */
10085 static inline double_int
10086 double_int_type_size_in_bits (const_tree type)
10088 if (TREE_CODE (type) == ERROR_MARK)
10089 return double_int::from_uhwi (BITS_PER_WORD);
10090 else if (TYPE_SIZE (type) == NULL_TREE)
10091 return double_int_zero;
10092 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10093 return tree_to_double_int (TYPE_SIZE (type));
10094 else
10095 return double_int::from_uhwi (TYPE_ALIGN (type));
10098 /* Given a pointer to a tree node for a subrange type, return a pointer
10099 to a DIE that describes the given type. */
10101 static dw_die_ref
10102 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10104 dw_die_ref subrange_die;
10105 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10107 if (context_die == NULL)
10108 context_die = comp_unit_die ();
10110 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10112 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10114 /* The size of the subrange type and its base type do not match,
10115 so we need to generate a size attribute for the subrange type. */
10116 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10119 if (low)
10120 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10121 if (high)
10122 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10124 return subrange_die;
10127 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10128 entry that chains various modifiers in front of the given type. */
10130 static dw_die_ref
10131 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10132 dw_die_ref context_die)
10134 enum tree_code code = TREE_CODE (type);
10135 dw_die_ref mod_type_die;
10136 dw_die_ref sub_die = NULL;
10137 tree item_type = NULL;
10138 tree qualified_type;
10139 tree name, low, high;
10140 dw_die_ref mod_scope;
10142 if (code == ERROR_MARK)
10143 return NULL;
10145 /* See if we already have the appropriately qualified variant of
10146 this type. */
10147 qualified_type
10148 = get_qualified_type (type,
10149 ((is_const_type ? TYPE_QUAL_CONST : 0)
10150 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10152 if (qualified_type == sizetype
10153 && TYPE_NAME (qualified_type)
10154 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10156 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10158 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10159 && TYPE_PRECISION (t)
10160 == TYPE_PRECISION (qualified_type)
10161 && TYPE_UNSIGNED (t)
10162 == TYPE_UNSIGNED (qualified_type));
10163 qualified_type = t;
10166 /* If we do, then we can just use its DIE, if it exists. */
10167 if (qualified_type)
10169 mod_type_die = lookup_type_die (qualified_type);
10170 if (mod_type_die)
10171 return mod_type_die;
10174 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10176 /* Handle C typedef types. */
10177 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10178 && !DECL_ARTIFICIAL (name))
10180 tree dtype = TREE_TYPE (name);
10182 if (qualified_type == dtype)
10184 /* For a named type, use the typedef. */
10185 gen_type_die (qualified_type, context_die);
10186 return lookup_type_die (qualified_type);
10188 else if (is_const_type < TYPE_READONLY (dtype)
10189 || is_volatile_type < TYPE_VOLATILE (dtype)
10190 || (is_const_type <= TYPE_READONLY (dtype)
10191 && is_volatile_type <= TYPE_VOLATILE (dtype)
10192 && DECL_ORIGINAL_TYPE (name) != type))
10193 /* cv-unqualified version of named type. Just use the unnamed
10194 type to which it refers. */
10195 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10196 is_const_type, is_volatile_type,
10197 context_die);
10198 /* Else cv-qualified version of named type; fall through. */
10201 mod_scope = scope_die_for (type, context_die);
10203 if (is_const_type
10204 /* If both is_const_type and is_volatile_type, prefer the path
10205 which leads to a qualified type. */
10206 && (!is_volatile_type
10207 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10208 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10210 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10211 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10213 else if (is_volatile_type)
10215 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10216 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10218 else if (code == POINTER_TYPE)
10220 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10221 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10222 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10223 item_type = TREE_TYPE (type);
10224 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10225 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10226 TYPE_ADDR_SPACE (item_type));
10228 else if (code == REFERENCE_TYPE)
10230 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10231 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10232 type);
10233 else
10234 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10235 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10236 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10237 item_type = TREE_TYPE (type);
10238 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10239 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10240 TYPE_ADDR_SPACE (item_type));
10242 else if (code == INTEGER_TYPE
10243 && TREE_TYPE (type) != NULL_TREE
10244 && subrange_type_for_debug_p (type, &low, &high))
10246 mod_type_die = subrange_type_die (type, low, high, context_die);
10247 item_type = TREE_TYPE (type);
10249 else if (is_base_type (type))
10250 mod_type_die = base_type_die (type);
10251 else
10253 gen_type_die (type, context_die);
10255 /* We have to get the type_main_variant here (and pass that to the
10256 `lookup_type_die' routine) because the ..._TYPE node we have
10257 might simply be a *copy* of some original type node (where the
10258 copy was created to help us keep track of typedef names) and
10259 that copy might have a different TYPE_UID from the original
10260 ..._TYPE node. */
10261 if (TREE_CODE (type) != VECTOR_TYPE)
10262 return lookup_type_die (type_main_variant (type));
10263 else
10264 /* Vectors have the debugging information in the type,
10265 not the main variant. */
10266 return lookup_type_die (type);
10269 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10270 don't output a DW_TAG_typedef, since there isn't one in the
10271 user's program; just attach a DW_AT_name to the type.
10272 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10273 if the base type already has the same name. */
10274 if (name
10275 && ((TREE_CODE (name) != TYPE_DECL
10276 && (qualified_type == TYPE_MAIN_VARIANT (type)
10277 || (!is_const_type && !is_volatile_type)))
10278 || (TREE_CODE (name) == TYPE_DECL
10279 && TREE_TYPE (name) == qualified_type
10280 && DECL_NAME (name))))
10282 if (TREE_CODE (name) == TYPE_DECL)
10283 /* Could just call add_name_and_src_coords_attributes here,
10284 but since this is a builtin type it doesn't have any
10285 useful source coordinates anyway. */
10286 name = DECL_NAME (name);
10287 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10289 /* This probably indicates a bug. */
10290 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10292 name = TYPE_NAME (type);
10293 if (name
10294 && TREE_CODE (name) == TYPE_DECL)
10295 name = DECL_NAME (name);
10296 add_name_attribute (mod_type_die,
10297 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10300 if (qualified_type)
10301 equate_type_number_to_die (qualified_type, mod_type_die);
10303 if (item_type)
10304 /* We must do this after the equate_type_number_to_die call, in case
10305 this is a recursive type. This ensures that the modified_type_die
10306 recursion will terminate even if the type is recursive. Recursive
10307 types are possible in Ada. */
10308 sub_die = modified_type_die (item_type,
10309 TYPE_READONLY (item_type),
10310 TYPE_VOLATILE (item_type),
10311 context_die);
10313 if (sub_die != NULL)
10314 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10316 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10317 if (TYPE_ARTIFICIAL (type))
10318 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10320 return mod_type_die;
10323 /* Generate DIEs for the generic parameters of T.
10324 T must be either a generic type or a generic function.
10325 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10327 static void
10328 gen_generic_params_dies (tree t)
10330 tree parms, args;
10331 int parms_num, i;
10332 dw_die_ref die = NULL;
10333 int non_default;
10335 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10336 return;
10338 if (TYPE_P (t))
10339 die = lookup_type_die (t);
10340 else if (DECL_P (t))
10341 die = lookup_decl_die (t);
10343 gcc_assert (die);
10345 parms = lang_hooks.get_innermost_generic_parms (t);
10346 if (!parms)
10347 /* T has no generic parameter. It means T is neither a generic type
10348 or function. End of story. */
10349 return;
10351 parms_num = TREE_VEC_LENGTH (parms);
10352 args = lang_hooks.get_innermost_generic_args (t);
10353 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10354 non_default = int_cst_value (TREE_CHAIN (args));
10355 else
10356 non_default = TREE_VEC_LENGTH (args);
10357 for (i = 0; i < parms_num; i++)
10359 tree parm, arg, arg_pack_elems;
10360 dw_die_ref parm_die;
10362 parm = TREE_VEC_ELT (parms, i);
10363 arg = TREE_VEC_ELT (args, i);
10364 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10365 gcc_assert (parm && TREE_VALUE (parm) && arg);
10367 if (parm && TREE_VALUE (parm) && arg)
10369 /* If PARM represents a template parameter pack,
10370 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10371 by DW_TAG_template_*_parameter DIEs for the argument
10372 pack elements of ARG. Note that ARG would then be
10373 an argument pack. */
10374 if (arg_pack_elems)
10375 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10376 arg_pack_elems,
10377 die);
10378 else
10379 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10380 true /* emit name */, die);
10381 if (i >= non_default)
10382 add_AT_flag (parm_die, DW_AT_default_value, 1);
10387 /* Create and return a DIE for PARM which should be
10388 the representation of a generic type parameter.
10389 For instance, in the C++ front end, PARM would be a template parameter.
10390 ARG is the argument to PARM.
10391 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10392 name of the PARM.
10393 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10394 as a child node. */
10396 static dw_die_ref
10397 generic_parameter_die (tree parm, tree arg,
10398 bool emit_name_p,
10399 dw_die_ref parent_die)
10401 dw_die_ref tmpl_die = NULL;
10402 const char *name = NULL;
10404 if (!parm || !DECL_NAME (parm) || !arg)
10405 return NULL;
10407 /* We support non-type generic parameters and arguments,
10408 type generic parameters and arguments, as well as
10409 generic generic parameters (a.k.a. template template parameters in C++)
10410 and arguments. */
10411 if (TREE_CODE (parm) == PARM_DECL)
10412 /* PARM is a nontype generic parameter */
10413 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10414 else if (TREE_CODE (parm) == TYPE_DECL)
10415 /* PARM is a type generic parameter. */
10416 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10417 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10418 /* PARM is a generic generic parameter.
10419 Its DIE is a GNU extension. It shall have a
10420 DW_AT_name attribute to represent the name of the template template
10421 parameter, and a DW_AT_GNU_template_name attribute to represent the
10422 name of the template template argument. */
10423 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10424 parent_die, parm);
10425 else
10426 gcc_unreachable ();
10428 if (tmpl_die)
10430 tree tmpl_type;
10432 /* If PARM is a generic parameter pack, it means we are
10433 emitting debug info for a template argument pack element.
10434 In other terms, ARG is a template argument pack element.
10435 In that case, we don't emit any DW_AT_name attribute for
10436 the die. */
10437 if (emit_name_p)
10439 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10440 gcc_assert (name);
10441 add_AT_string (tmpl_die, DW_AT_name, name);
10444 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10446 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10447 TMPL_DIE should have a child DW_AT_type attribute that is set
10448 to the type of the argument to PARM, which is ARG.
10449 If PARM is a type generic parameter, TMPL_DIE should have a
10450 child DW_AT_type that is set to ARG. */
10451 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10452 add_type_attribute (tmpl_die, tmpl_type, 0,
10453 TREE_THIS_VOLATILE (tmpl_type),
10454 parent_die);
10456 else
10458 /* So TMPL_DIE is a DIE representing a
10459 a generic generic template parameter, a.k.a template template
10460 parameter in C++ and arg is a template. */
10462 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10463 to the name of the argument. */
10464 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10465 if (name)
10466 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10469 if (TREE_CODE (parm) == PARM_DECL)
10470 /* So PARM is a non-type generic parameter.
10471 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10472 attribute of TMPL_DIE which value represents the value
10473 of ARG.
10474 We must be careful here:
10475 The value of ARG might reference some function decls.
10476 We might currently be emitting debug info for a generic
10477 type and types are emitted before function decls, we don't
10478 know if the function decls referenced by ARG will actually be
10479 emitted after cgraph computations.
10480 So must defer the generation of the DW_AT_const_value to
10481 after cgraph is ready. */
10482 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10485 return tmpl_die;
10488 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10489 PARM_PACK must be a template parameter pack. The returned DIE
10490 will be child DIE of PARENT_DIE. */
10492 static dw_die_ref
10493 template_parameter_pack_die (tree parm_pack,
10494 tree parm_pack_args,
10495 dw_die_ref parent_die)
10497 dw_die_ref die;
10498 int j;
10500 gcc_assert (parent_die && parm_pack);
10502 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10503 add_name_and_src_coords_attributes (die, parm_pack);
10504 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10505 generic_parameter_die (parm_pack,
10506 TREE_VEC_ELT (parm_pack_args, j),
10507 false /* Don't emit DW_AT_name */,
10508 die);
10509 return die;
10512 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10513 an enumerated type. */
10515 static inline int
10516 type_is_enum (const_tree type)
10518 return TREE_CODE (type) == ENUMERAL_TYPE;
10521 /* Return the DBX register number described by a given RTL node. */
10523 static unsigned int
10524 dbx_reg_number (const_rtx rtl)
10526 unsigned regno = REGNO (rtl);
10528 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10530 #ifdef LEAF_REG_REMAP
10531 if (crtl->uses_only_leaf_regs)
10533 int leaf_reg = LEAF_REG_REMAP (regno);
10534 if (leaf_reg != -1)
10535 regno = (unsigned) leaf_reg;
10537 #endif
10539 regno = DBX_REGISTER_NUMBER (regno);
10540 gcc_assert (regno != INVALID_REGNUM);
10541 return regno;
10544 /* Optionally add a DW_OP_piece term to a location description expression.
10545 DW_OP_piece is only added if the location description expression already
10546 doesn't end with DW_OP_piece. */
10548 static void
10549 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10551 dw_loc_descr_ref loc;
10553 if (*list_head != NULL)
10555 /* Find the end of the chain. */
10556 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10559 if (loc->dw_loc_opc != DW_OP_piece)
10560 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10564 /* Return a location descriptor that designates a machine register or
10565 zero if there is none. */
10567 static dw_loc_descr_ref
10568 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10570 rtx regs;
10572 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10573 return 0;
10575 /* We only use "frame base" when we're sure we're talking about the
10576 post-prologue local stack frame. We do this by *not* running
10577 register elimination until this point, and recognizing the special
10578 argument pointer and soft frame pointer rtx's.
10579 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10580 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10581 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10583 dw_loc_descr_ref result = NULL;
10585 if (dwarf_version >= 4 || !dwarf_strict)
10587 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10588 initialized);
10589 if (result)
10590 add_loc_descr (&result,
10591 new_loc_descr (DW_OP_stack_value, 0, 0));
10593 return result;
10596 regs = targetm.dwarf_register_span (rtl);
10598 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10599 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10600 else
10602 unsigned int dbx_regnum = dbx_reg_number (rtl);
10603 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10604 return 0;
10605 return one_reg_loc_descriptor (dbx_regnum, initialized);
10609 /* Return a location descriptor that designates a machine register for
10610 a given hard register number. */
10612 static dw_loc_descr_ref
10613 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10615 dw_loc_descr_ref reg_loc_descr;
10617 if (regno <= 31)
10618 reg_loc_descr
10619 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10620 else
10621 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10623 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10624 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10626 return reg_loc_descr;
10629 /* Given an RTL of a register, return a location descriptor that
10630 designates a value that spans more than one register. */
10632 static dw_loc_descr_ref
10633 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10634 enum var_init_status initialized)
10636 int size, i;
10637 dw_loc_descr_ref loc_result = NULL;
10639 /* Simple, contiguous registers. */
10640 if (regs == NULL_RTX)
10642 unsigned reg = REGNO (rtl);
10643 int nregs;
10645 #ifdef LEAF_REG_REMAP
10646 if (crtl->uses_only_leaf_regs)
10648 int leaf_reg = LEAF_REG_REMAP (reg);
10649 if (leaf_reg != -1)
10650 reg = (unsigned) leaf_reg;
10652 #endif
10654 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10655 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10657 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10659 loc_result = NULL;
10660 while (nregs--)
10662 dw_loc_descr_ref t;
10664 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10665 VAR_INIT_STATUS_INITIALIZED);
10666 add_loc_descr (&loc_result, t);
10667 add_loc_descr_op_piece (&loc_result, size);
10668 ++reg;
10670 return loc_result;
10673 /* Now onto stupid register sets in non contiguous locations. */
10675 gcc_assert (GET_CODE (regs) == PARALLEL);
10677 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10678 loc_result = NULL;
10680 for (i = 0; i < XVECLEN (regs, 0); ++i)
10682 dw_loc_descr_ref t;
10684 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10685 VAR_INIT_STATUS_INITIALIZED);
10686 add_loc_descr (&loc_result, t);
10687 add_loc_descr_op_piece (&loc_result, size);
10690 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10691 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10692 return loc_result;
10695 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10697 /* Return a location descriptor that designates a constant i,
10698 as a compound operation from constant (i >> shift), constant shift
10699 and DW_OP_shl. */
10701 static dw_loc_descr_ref
10702 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10704 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10705 add_loc_descr (&ret, int_loc_descriptor (shift));
10706 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10707 return ret;
10710 /* Return a location descriptor that designates a constant. */
10712 static dw_loc_descr_ref
10713 int_loc_descriptor (HOST_WIDE_INT i)
10715 enum dwarf_location_atom op;
10717 /* Pick the smallest representation of a constant, rather than just
10718 defaulting to the LEB encoding. */
10719 if (i >= 0)
10721 int clz = clz_hwi (i);
10722 int ctz = ctz_hwi (i);
10723 if (i <= 31)
10724 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10725 else if (i <= 0xff)
10726 op = DW_OP_const1u;
10727 else if (i <= 0xffff)
10728 op = DW_OP_const2u;
10729 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10730 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10731 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10732 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10733 while DW_OP_const4u is 5 bytes. */
10734 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10735 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10736 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10737 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10738 while DW_OP_const4u is 5 bytes. */
10739 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10740 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10741 op = DW_OP_const4u;
10742 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10743 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10744 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10745 while DW_OP_constu of constant >= 0x100000000 takes at least
10746 6 bytes. */
10747 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10748 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10749 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10750 >= HOST_BITS_PER_WIDE_INT)
10751 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10752 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10753 while DW_OP_constu takes in this case at least 6 bytes. */
10754 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10755 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10756 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10757 && size_of_uleb128 (i) > 6)
10758 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10759 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10760 else
10761 op = DW_OP_constu;
10763 else
10765 if (i >= -0x80)
10766 op = DW_OP_const1s;
10767 else if (i >= -0x8000)
10768 op = DW_OP_const2s;
10769 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10771 if (size_of_int_loc_descriptor (i) < 5)
10773 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10774 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10775 return ret;
10777 op = DW_OP_const4s;
10779 else
10781 if (size_of_int_loc_descriptor (i)
10782 < (unsigned long) 1 + size_of_sleb128 (i))
10784 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10785 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10786 return ret;
10788 op = DW_OP_consts;
10792 return new_loc_descr (op, i, 0);
10795 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10796 without actually allocating it. */
10798 static unsigned long
10799 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10801 return size_of_int_loc_descriptor (i >> shift)
10802 + size_of_int_loc_descriptor (shift)
10803 + 1;
10806 /* Return size_of_locs (int_loc_descriptor (i)) without
10807 actually allocating it. */
10809 static unsigned long
10810 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10812 unsigned long s;
10814 if (i >= 0)
10816 int clz, ctz;
10817 if (i <= 31)
10818 return 1;
10819 else if (i <= 0xff)
10820 return 2;
10821 else if (i <= 0xffff)
10822 return 3;
10823 clz = clz_hwi (i);
10824 ctz = ctz_hwi (i);
10825 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10826 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10827 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10828 - clz - 5);
10829 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10830 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10831 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10832 - clz - 8);
10833 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10834 return 5;
10835 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10836 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10837 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10838 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10839 - clz - 8);
10840 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10841 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10842 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10843 - clz - 16);
10844 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10845 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10846 && s > 6)
10847 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10848 - clz - 32);
10849 else
10850 return 1 + s;
10852 else
10854 if (i >= -0x80)
10855 return 2;
10856 else if (i >= -0x8000)
10857 return 3;
10858 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10860 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10862 s = size_of_int_loc_descriptor (-i) + 1;
10863 if (s < 5)
10864 return s;
10866 return 5;
10868 else
10870 unsigned long r = 1 + size_of_sleb128 (i);
10871 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10873 s = size_of_int_loc_descriptor (-i) + 1;
10874 if (s < r)
10875 return s;
10877 return r;
10882 /* Return loc description representing "address" of integer value.
10883 This can appear only as toplevel expression. */
10885 static dw_loc_descr_ref
10886 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10888 int litsize;
10889 dw_loc_descr_ref loc_result = NULL;
10891 if (!(dwarf_version >= 4 || !dwarf_strict))
10892 return NULL;
10894 litsize = size_of_int_loc_descriptor (i);
10895 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10896 is more compact. For DW_OP_stack_value we need:
10897 litsize + 1 (DW_OP_stack_value)
10898 and for DW_OP_implicit_value:
10899 1 (DW_OP_implicit_value) + 1 (length) + size. */
10900 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10902 loc_result = int_loc_descriptor (i);
10903 add_loc_descr (&loc_result,
10904 new_loc_descr (DW_OP_stack_value, 0, 0));
10905 return loc_result;
10908 loc_result = new_loc_descr (DW_OP_implicit_value,
10909 size, 0);
10910 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10911 loc_result->dw_loc_oprnd2.v.val_int = i;
10912 return loc_result;
10915 /* Return a location descriptor that designates a base+offset location. */
10917 static dw_loc_descr_ref
10918 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10919 enum var_init_status initialized)
10921 unsigned int regno;
10922 dw_loc_descr_ref result;
10923 dw_fde_ref fde = cfun->fde;
10925 /* We only use "frame base" when we're sure we're talking about the
10926 post-prologue local stack frame. We do this by *not* running
10927 register elimination until this point, and recognizing the special
10928 argument pointer and soft frame pointer rtx's. */
10929 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10931 rtx elim = (ira_use_lra_p
10932 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10933 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10935 if (elim != reg)
10937 if (GET_CODE (elim) == PLUS)
10939 offset += INTVAL (XEXP (elim, 1));
10940 elim = XEXP (elim, 0);
10942 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10943 && (elim == hard_frame_pointer_rtx
10944 || elim == stack_pointer_rtx))
10945 || elim == (frame_pointer_needed
10946 ? hard_frame_pointer_rtx
10947 : stack_pointer_rtx));
10949 /* If drap register is used to align stack, use frame
10950 pointer + offset to access stack variables. If stack
10951 is aligned without drap, use stack pointer + offset to
10952 access stack variables. */
10953 if (crtl->stack_realign_tried
10954 && reg == frame_pointer_rtx)
10956 int base_reg
10957 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10958 ? HARD_FRAME_POINTER_REGNUM
10959 : REGNO (elim));
10960 return new_reg_loc_descr (base_reg, offset);
10963 gcc_assert (frame_pointer_fb_offset_valid);
10964 offset += frame_pointer_fb_offset;
10965 return new_loc_descr (DW_OP_fbreg, offset, 0);
10969 regno = REGNO (reg);
10970 #ifdef LEAF_REG_REMAP
10971 if (crtl->uses_only_leaf_regs)
10973 int leaf_reg = LEAF_REG_REMAP (regno);
10974 if (leaf_reg != -1)
10975 regno = (unsigned) leaf_reg;
10977 #endif
10978 regno = DWARF_FRAME_REGNUM (regno);
10980 if (!optimize && fde
10981 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10983 /* Use cfa+offset to represent the location of arguments passed
10984 on the stack when drap is used to align stack.
10985 Only do this when not optimizing, for optimized code var-tracking
10986 is supposed to track where the arguments live and the register
10987 used as vdrap or drap in some spot might be used for something
10988 else in other part of the routine. */
10989 return new_loc_descr (DW_OP_fbreg, offset, 0);
10992 if (regno <= 31)
10993 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10994 offset, 0);
10995 else
10996 result = new_loc_descr (DW_OP_bregx, regno, offset);
10998 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10999 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11001 return result;
11004 /* Return true if this RTL expression describes a base+offset calculation. */
11006 static inline int
11007 is_based_loc (const_rtx rtl)
11009 return (GET_CODE (rtl) == PLUS
11010 && ((REG_P (XEXP (rtl, 0))
11011 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11012 && CONST_INT_P (XEXP (rtl, 1)))));
11015 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11016 failed. */
11018 static dw_loc_descr_ref
11019 tls_mem_loc_descriptor (rtx mem)
11021 tree base;
11022 dw_loc_descr_ref loc_result;
11024 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11025 return NULL;
11027 base = get_base_address (MEM_EXPR (mem));
11028 if (base == NULL
11029 || TREE_CODE (base) != VAR_DECL
11030 || !DECL_THREAD_LOCAL_P (base))
11031 return NULL;
11033 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11034 if (loc_result == NULL)
11035 return NULL;
11037 if (MEM_OFFSET (mem))
11038 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11040 return loc_result;
11043 /* Output debug info about reason why we failed to expand expression as dwarf
11044 expression. */
11046 static void
11047 expansion_failed (tree expr, rtx rtl, char const *reason)
11049 if (dump_file && (dump_flags & TDF_DETAILS))
11051 fprintf (dump_file, "Failed to expand as dwarf: ");
11052 if (expr)
11053 print_generic_expr (dump_file, expr, dump_flags);
11054 if (rtl)
11056 fprintf (dump_file, "\n");
11057 print_rtl (dump_file, rtl);
11059 fprintf (dump_file, "\nReason: %s\n", reason);
11063 /* Helper function for const_ok_for_output, called either directly
11064 or via for_each_rtx. */
11066 static int
11067 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11069 rtx rtl = *rtlp;
11071 if (GET_CODE (rtl) == UNSPEC)
11073 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11074 we can't express it in the debug info. */
11075 #ifdef ENABLE_CHECKING
11076 /* Don't complain about TLS UNSPECs, those are just too hard to
11077 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11078 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11079 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11080 if (XVECLEN (rtl, 0) == 0
11081 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11082 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11083 inform (current_function_decl
11084 ? DECL_SOURCE_LOCATION (current_function_decl)
11085 : UNKNOWN_LOCATION,
11086 #if NUM_UNSPEC_VALUES > 0
11087 "non-delegitimized UNSPEC %s (%d) found in variable location",
11088 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11089 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11090 XINT (rtl, 1));
11091 #else
11092 "non-delegitimized UNSPEC %d found in variable location",
11093 XINT (rtl, 1));
11094 #endif
11095 #endif
11096 expansion_failed (NULL_TREE, rtl,
11097 "UNSPEC hasn't been delegitimized.\n");
11098 return 1;
11101 if (targetm.const_not_ok_for_debug_p (rtl))
11103 expansion_failed (NULL_TREE, rtl,
11104 "Expression rejected for debug by the backend.\n");
11105 return 1;
11108 if (GET_CODE (rtl) != SYMBOL_REF)
11109 return 0;
11111 if (CONSTANT_POOL_ADDRESS_P (rtl))
11113 bool marked;
11114 get_pool_constant_mark (rtl, &marked);
11115 /* If all references to this pool constant were optimized away,
11116 it was not output and thus we can't represent it. */
11117 if (!marked)
11119 expansion_failed (NULL_TREE, rtl,
11120 "Constant was removed from constant pool.\n");
11121 return 1;
11125 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11126 return 1;
11128 /* Avoid references to external symbols in debug info, on several targets
11129 the linker might even refuse to link when linking a shared library,
11130 and in many other cases the relocations for .debug_info/.debug_loc are
11131 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11132 to be defined within the same shared library or executable are fine. */
11133 if (SYMBOL_REF_EXTERNAL_P (rtl))
11135 tree decl = SYMBOL_REF_DECL (rtl);
11137 if (decl == NULL || !targetm.binds_local_p (decl))
11139 expansion_failed (NULL_TREE, rtl,
11140 "Symbol not defined in current TU.\n");
11141 return 1;
11145 return 0;
11148 /* Return true if constant RTL can be emitted in DW_OP_addr or
11149 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11150 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11152 static bool
11153 const_ok_for_output (rtx rtl)
11155 if (GET_CODE (rtl) == SYMBOL_REF)
11156 return const_ok_for_output_1 (&rtl, NULL) == 0;
11158 if (GET_CODE (rtl) == CONST)
11159 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11161 return true;
11164 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11165 if possible, NULL otherwise. */
11167 static dw_die_ref
11168 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11170 dw_die_ref type_die;
11171 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11173 if (type == NULL)
11174 return NULL;
11175 switch (TREE_CODE (type))
11177 case INTEGER_TYPE:
11178 case REAL_TYPE:
11179 break;
11180 default:
11181 return NULL;
11183 type_die = lookup_type_die (type);
11184 if (!type_die)
11185 type_die = modified_type_die (type, false, false, comp_unit_die ());
11186 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11187 return NULL;
11188 return type_die;
11191 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11192 type matching MODE, or, if MODE is narrower than or as wide as
11193 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11194 possible. */
11196 static dw_loc_descr_ref
11197 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11199 enum machine_mode outer_mode = mode;
11200 dw_die_ref type_die;
11201 dw_loc_descr_ref cvt;
11203 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11205 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11206 return op;
11208 type_die = base_type_for_mode (outer_mode, 1);
11209 if (type_die == NULL)
11210 return NULL;
11211 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11212 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11213 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11214 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11215 add_loc_descr (&op, cvt);
11216 return op;
11219 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11221 static dw_loc_descr_ref
11222 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11223 dw_loc_descr_ref op1)
11225 dw_loc_descr_ref ret = op0;
11226 add_loc_descr (&ret, op1);
11227 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11228 if (STORE_FLAG_VALUE != 1)
11230 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11231 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11233 return ret;
11236 /* Return location descriptor for signed comparison OP RTL. */
11238 static dw_loc_descr_ref
11239 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11240 enum machine_mode mem_mode)
11242 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11243 dw_loc_descr_ref op0, op1;
11244 int shift;
11246 if (op_mode == VOIDmode)
11247 op_mode = GET_MODE (XEXP (rtl, 1));
11248 if (op_mode == VOIDmode)
11249 return NULL;
11251 if (dwarf_strict
11252 && (GET_MODE_CLASS (op_mode) != MODE_INT
11253 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11254 return NULL;
11256 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11257 VAR_INIT_STATUS_INITIALIZED);
11258 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11259 VAR_INIT_STATUS_INITIALIZED);
11261 if (op0 == NULL || op1 == NULL)
11262 return NULL;
11264 if (GET_MODE_CLASS (op_mode) != MODE_INT
11265 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11266 return compare_loc_descriptor (op, op0, op1);
11268 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11270 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11271 dw_loc_descr_ref cvt;
11273 if (type_die == NULL)
11274 return NULL;
11275 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11276 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11277 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11278 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11279 add_loc_descr (&op0, cvt);
11280 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11281 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11282 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11283 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11284 add_loc_descr (&op1, cvt);
11285 return compare_loc_descriptor (op, op0, op1);
11288 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11289 /* For eq/ne, if the operands are known to be zero-extended,
11290 there is no need to do the fancy shifting up. */
11291 if (op == DW_OP_eq || op == DW_OP_ne)
11293 dw_loc_descr_ref last0, last1;
11294 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11296 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11298 /* deref_size zero extends, and for constants we can check
11299 whether they are zero extended or not. */
11300 if (((last0->dw_loc_opc == DW_OP_deref_size
11301 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11302 || (CONST_INT_P (XEXP (rtl, 0))
11303 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11304 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11305 && ((last1->dw_loc_opc == DW_OP_deref_size
11306 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11307 || (CONST_INT_P (XEXP (rtl, 1))
11308 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11309 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11310 return compare_loc_descriptor (op, op0, op1);
11312 /* EQ/NE comparison against constant in narrower type than
11313 DWARF2_ADDR_SIZE can be performed either as
11314 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11315 DW_OP_{eq,ne}
11317 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11318 DW_OP_{eq,ne}. Pick whatever is shorter. */
11319 if (CONST_INT_P (XEXP (rtl, 1))
11320 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11321 && (size_of_int_loc_descriptor (shift) + 1
11322 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11323 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11324 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11325 & GET_MODE_MASK (op_mode))))
11327 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11328 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11329 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11330 & GET_MODE_MASK (op_mode));
11331 return compare_loc_descriptor (op, op0, op1);
11334 add_loc_descr (&op0, int_loc_descriptor (shift));
11335 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11336 if (CONST_INT_P (XEXP (rtl, 1)))
11337 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11338 else
11340 add_loc_descr (&op1, int_loc_descriptor (shift));
11341 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11343 return compare_loc_descriptor (op, op0, op1);
11346 /* Return location descriptor for unsigned comparison OP RTL. */
11348 static dw_loc_descr_ref
11349 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11350 enum machine_mode mem_mode)
11352 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11353 dw_loc_descr_ref op0, op1;
11355 if (op_mode == VOIDmode)
11356 op_mode = GET_MODE (XEXP (rtl, 1));
11357 if (op_mode == VOIDmode)
11358 return NULL;
11359 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11360 return NULL;
11362 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11363 return NULL;
11365 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11366 VAR_INIT_STATUS_INITIALIZED);
11367 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11368 VAR_INIT_STATUS_INITIALIZED);
11370 if (op0 == NULL || op1 == NULL)
11371 return NULL;
11373 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11375 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11376 dw_loc_descr_ref last0, last1;
11377 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11379 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11381 if (CONST_INT_P (XEXP (rtl, 0)))
11382 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11383 /* deref_size zero extends, so no need to mask it again. */
11384 else if (last0->dw_loc_opc != DW_OP_deref_size
11385 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11387 add_loc_descr (&op0, int_loc_descriptor (mask));
11388 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11390 if (CONST_INT_P (XEXP (rtl, 1)))
11391 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11392 /* deref_size zero extends, so no need to mask it again. */
11393 else if (last1->dw_loc_opc != DW_OP_deref_size
11394 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11396 add_loc_descr (&op1, int_loc_descriptor (mask));
11397 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11400 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11402 HOST_WIDE_INT bias = 1;
11403 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11404 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11405 if (CONST_INT_P (XEXP (rtl, 1)))
11406 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11407 + INTVAL (XEXP (rtl, 1)));
11408 else
11409 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11410 bias, 0));
11412 return compare_loc_descriptor (op, op0, op1);
11415 /* Return location descriptor for {U,S}{MIN,MAX}. */
11417 static dw_loc_descr_ref
11418 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11419 enum machine_mode mem_mode)
11421 enum dwarf_location_atom op;
11422 dw_loc_descr_ref op0, op1, ret;
11423 dw_loc_descr_ref bra_node, drop_node;
11425 if (dwarf_strict
11426 && (GET_MODE_CLASS (mode) != MODE_INT
11427 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11428 return NULL;
11430 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11431 VAR_INIT_STATUS_INITIALIZED);
11432 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11433 VAR_INIT_STATUS_INITIALIZED);
11435 if (op0 == NULL || op1 == NULL)
11436 return NULL;
11438 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11439 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11440 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11441 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11443 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11445 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11446 add_loc_descr (&op0, int_loc_descriptor (mask));
11447 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11448 add_loc_descr (&op1, int_loc_descriptor (mask));
11449 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11451 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11453 HOST_WIDE_INT bias = 1;
11454 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11455 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11456 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11459 else if (GET_MODE_CLASS (mode) == MODE_INT
11460 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11462 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11463 add_loc_descr (&op0, int_loc_descriptor (shift));
11464 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11465 add_loc_descr (&op1, int_loc_descriptor (shift));
11466 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11468 else if (GET_MODE_CLASS (mode) == MODE_INT
11469 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11471 dw_die_ref type_die = base_type_for_mode (mode, 0);
11472 dw_loc_descr_ref cvt;
11473 if (type_die == NULL)
11474 return NULL;
11475 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11476 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11477 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11478 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11479 add_loc_descr (&op0, cvt);
11480 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11481 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11482 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11483 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11484 add_loc_descr (&op1, cvt);
11487 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11488 op = DW_OP_lt;
11489 else
11490 op = DW_OP_gt;
11491 ret = op0;
11492 add_loc_descr (&ret, op1);
11493 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11494 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11495 add_loc_descr (&ret, bra_node);
11496 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11497 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11498 add_loc_descr (&ret, drop_node);
11499 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11500 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11501 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11502 && GET_MODE_CLASS (mode) == MODE_INT
11503 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11504 ret = convert_descriptor_to_mode (mode, ret);
11505 return ret;
11508 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11509 but after converting arguments to type_die, afterwards
11510 convert back to unsigned. */
11512 static dw_loc_descr_ref
11513 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11514 enum machine_mode mode, enum machine_mode mem_mode)
11516 dw_loc_descr_ref cvt, op0, op1;
11518 if (type_die == NULL)
11519 return NULL;
11520 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11521 VAR_INIT_STATUS_INITIALIZED);
11522 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11523 VAR_INIT_STATUS_INITIALIZED);
11524 if (op0 == NULL || op1 == NULL)
11525 return NULL;
11526 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11527 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11528 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11529 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11530 add_loc_descr (&op0, cvt);
11531 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11532 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11533 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11534 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11535 add_loc_descr (&op1, cvt);
11536 add_loc_descr (&op0, op1);
11537 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11538 return convert_descriptor_to_mode (mode, op0);
11541 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11542 const0 is DW_OP_lit0 or corresponding typed constant,
11543 const1 is DW_OP_lit1 or corresponding typed constant
11544 and constMSB is constant with just the MSB bit set
11545 for the mode):
11546 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11547 L1: const0 DW_OP_swap
11548 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11549 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11550 L3: DW_OP_drop
11551 L4: DW_OP_nop
11553 CTZ is similar:
11554 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11555 L1: const0 DW_OP_swap
11556 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11557 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11558 L3: DW_OP_drop
11559 L4: DW_OP_nop
11561 FFS is similar:
11562 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11563 L1: const1 DW_OP_swap
11564 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11565 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11566 L3: DW_OP_drop
11567 L4: DW_OP_nop */
11569 static dw_loc_descr_ref
11570 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11571 enum machine_mode mem_mode)
11573 dw_loc_descr_ref op0, ret, tmp;
11574 HOST_WIDE_INT valv;
11575 dw_loc_descr_ref l1jump, l1label;
11576 dw_loc_descr_ref l2jump, l2label;
11577 dw_loc_descr_ref l3jump, l3label;
11578 dw_loc_descr_ref l4jump, l4label;
11579 rtx msb;
11581 if (GET_MODE_CLASS (mode) != MODE_INT
11582 || GET_MODE (XEXP (rtl, 0)) != mode
11583 || (GET_CODE (rtl) == CLZ
11584 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11585 return NULL;
11587 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11588 VAR_INIT_STATUS_INITIALIZED);
11589 if (op0 == NULL)
11590 return NULL;
11591 ret = op0;
11592 if (GET_CODE (rtl) == CLZ)
11594 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11595 valv = GET_MODE_BITSIZE (mode);
11597 else if (GET_CODE (rtl) == FFS)
11598 valv = 0;
11599 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11600 valv = GET_MODE_BITSIZE (mode);
11601 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11602 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11603 add_loc_descr (&ret, l1jump);
11604 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11605 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11606 VAR_INIT_STATUS_INITIALIZED);
11607 if (tmp == NULL)
11608 return NULL;
11609 add_loc_descr (&ret, tmp);
11610 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11611 add_loc_descr (&ret, l4jump);
11612 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11613 ? const1_rtx : const0_rtx,
11614 mode, mem_mode,
11615 VAR_INIT_STATUS_INITIALIZED);
11616 if (l1label == NULL)
11617 return NULL;
11618 add_loc_descr (&ret, l1label);
11619 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11620 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11621 add_loc_descr (&ret, l2label);
11622 if (GET_CODE (rtl) != CLZ)
11623 msb = const1_rtx;
11624 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11625 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11626 << (GET_MODE_BITSIZE (mode) - 1));
11627 else
11628 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11629 << (GET_MODE_BITSIZE (mode)
11630 - HOST_BITS_PER_WIDE_INT - 1), mode);
11631 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11632 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11633 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11634 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11635 else
11636 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11637 VAR_INIT_STATUS_INITIALIZED);
11638 if (tmp == NULL)
11639 return NULL;
11640 add_loc_descr (&ret, tmp);
11641 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11642 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11643 add_loc_descr (&ret, l3jump);
11644 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11645 VAR_INIT_STATUS_INITIALIZED);
11646 if (tmp == NULL)
11647 return NULL;
11648 add_loc_descr (&ret, tmp);
11649 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11650 ? DW_OP_shl : DW_OP_shr, 0, 0));
11651 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11652 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11653 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11654 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11655 add_loc_descr (&ret, l2jump);
11656 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11657 add_loc_descr (&ret, l3label);
11658 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11659 add_loc_descr (&ret, l4label);
11660 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11661 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11662 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11663 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11664 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11665 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11666 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11667 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11668 return ret;
11671 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11672 const1 is DW_OP_lit1 or corresponding typed constant):
11673 const0 DW_OP_swap
11674 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11675 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11676 L2: DW_OP_drop
11678 PARITY is similar:
11679 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11680 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11681 L2: DW_OP_drop */
11683 static dw_loc_descr_ref
11684 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11685 enum machine_mode mem_mode)
11687 dw_loc_descr_ref op0, ret, tmp;
11688 dw_loc_descr_ref l1jump, l1label;
11689 dw_loc_descr_ref l2jump, l2label;
11691 if (GET_MODE_CLASS (mode) != MODE_INT
11692 || GET_MODE (XEXP (rtl, 0)) != mode)
11693 return NULL;
11695 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11696 VAR_INIT_STATUS_INITIALIZED);
11697 if (op0 == NULL)
11698 return NULL;
11699 ret = op0;
11700 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11701 VAR_INIT_STATUS_INITIALIZED);
11702 if (tmp == NULL)
11703 return NULL;
11704 add_loc_descr (&ret, tmp);
11705 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11706 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11707 add_loc_descr (&ret, l1label);
11708 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11709 add_loc_descr (&ret, l2jump);
11710 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11711 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11712 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11713 VAR_INIT_STATUS_INITIALIZED);
11714 if (tmp == NULL)
11715 return NULL;
11716 add_loc_descr (&ret, tmp);
11717 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11718 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11719 ? DW_OP_plus : DW_OP_xor, 0, 0));
11720 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11721 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11722 VAR_INIT_STATUS_INITIALIZED);
11723 add_loc_descr (&ret, tmp);
11724 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11725 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11726 add_loc_descr (&ret, l1jump);
11727 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11728 add_loc_descr (&ret, l2label);
11729 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11730 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11731 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11732 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11733 return ret;
11736 /* BSWAP (constS is initial shift count, either 56 or 24):
11737 constS const0
11738 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11739 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11740 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11741 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11742 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11744 static dw_loc_descr_ref
11745 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11746 enum machine_mode mem_mode)
11748 dw_loc_descr_ref op0, ret, tmp;
11749 dw_loc_descr_ref l1jump, l1label;
11750 dw_loc_descr_ref l2jump, l2label;
11752 if (GET_MODE_CLASS (mode) != MODE_INT
11753 || BITS_PER_UNIT != 8
11754 || (GET_MODE_BITSIZE (mode) != 32
11755 && GET_MODE_BITSIZE (mode) != 64))
11756 return NULL;
11758 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11759 VAR_INIT_STATUS_INITIALIZED);
11760 if (op0 == NULL)
11761 return NULL;
11763 ret = op0;
11764 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11765 mode, mem_mode,
11766 VAR_INIT_STATUS_INITIALIZED);
11767 if (tmp == NULL)
11768 return NULL;
11769 add_loc_descr (&ret, tmp);
11770 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11771 VAR_INIT_STATUS_INITIALIZED);
11772 if (tmp == NULL)
11773 return NULL;
11774 add_loc_descr (&ret, tmp);
11775 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11776 add_loc_descr (&ret, l1label);
11777 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11778 mode, mem_mode,
11779 VAR_INIT_STATUS_INITIALIZED);
11780 add_loc_descr (&ret, tmp);
11781 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11782 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11783 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11784 tmp = mem_loc_descriptor (GEN_INT (255), 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_and, 0, 0));
11790 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11791 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11792 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11793 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11794 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11795 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11796 VAR_INIT_STATUS_INITIALIZED);
11797 add_loc_descr (&ret, tmp);
11798 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11799 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11800 add_loc_descr (&ret, l2jump);
11801 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11802 VAR_INIT_STATUS_INITIALIZED);
11803 add_loc_descr (&ret, tmp);
11804 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11805 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11806 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11807 add_loc_descr (&ret, l1jump);
11808 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11809 add_loc_descr (&ret, l2label);
11810 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11811 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11812 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11813 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11814 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11815 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11816 return ret;
11819 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11820 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11821 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11822 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11824 ROTATERT is similar:
11825 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11826 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11827 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11829 static dw_loc_descr_ref
11830 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11831 enum machine_mode mem_mode)
11833 rtx rtlop1 = XEXP (rtl, 1);
11834 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11835 int i;
11837 if (GET_MODE_CLASS (mode) != MODE_INT)
11838 return NULL;
11840 if (GET_MODE (rtlop1) != VOIDmode
11841 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11842 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11843 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11844 VAR_INIT_STATUS_INITIALIZED);
11845 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11846 VAR_INIT_STATUS_INITIALIZED);
11847 if (op0 == NULL || op1 == NULL)
11848 return NULL;
11849 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11850 for (i = 0; i < 2; i++)
11852 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11853 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11854 mode, mem_mode,
11855 VAR_INIT_STATUS_INITIALIZED);
11856 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11857 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11858 ? DW_OP_const4u
11859 : HOST_BITS_PER_WIDE_INT == 64
11860 ? DW_OP_const8u : DW_OP_constu,
11861 GET_MODE_MASK (mode), 0);
11862 else
11863 mask[i] = NULL;
11864 if (mask[i] == NULL)
11865 return NULL;
11866 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11868 ret = op0;
11869 add_loc_descr (&ret, op1);
11870 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11871 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11872 if (GET_CODE (rtl) == ROTATERT)
11874 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11875 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11876 GET_MODE_BITSIZE (mode), 0));
11878 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11879 if (mask[0] != NULL)
11880 add_loc_descr (&ret, mask[0]);
11881 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11882 if (mask[1] != NULL)
11884 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11885 add_loc_descr (&ret, mask[1]);
11886 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11888 if (GET_CODE (rtl) == ROTATE)
11890 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11891 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11892 GET_MODE_BITSIZE (mode), 0));
11894 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11895 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11896 return ret;
11899 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11900 for DEBUG_PARAMETER_REF RTL. */
11902 static dw_loc_descr_ref
11903 parameter_ref_descriptor (rtx rtl)
11905 dw_loc_descr_ref ret;
11906 dw_die_ref ref;
11908 if (dwarf_strict)
11909 return NULL;
11910 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11911 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11912 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11913 if (ref)
11915 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11916 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11917 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11919 else
11921 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11922 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11924 return ret;
11927 /* The following routine converts the RTL for a variable or parameter
11928 (resident in memory) into an equivalent Dwarf representation of a
11929 mechanism for getting the address of that same variable onto the top of a
11930 hypothetical "address evaluation" stack.
11932 When creating memory location descriptors, we are effectively transforming
11933 the RTL for a memory-resident object into its Dwarf postfix expression
11934 equivalent. This routine recursively descends an RTL tree, turning
11935 it into Dwarf postfix code as it goes.
11937 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11939 MEM_MODE is the mode of the memory reference, needed to handle some
11940 autoincrement addressing modes.
11942 Return 0 if we can't represent the location. */
11944 dw_loc_descr_ref
11945 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11946 enum machine_mode mem_mode,
11947 enum var_init_status initialized)
11949 dw_loc_descr_ref mem_loc_result = NULL;
11950 enum dwarf_location_atom op;
11951 dw_loc_descr_ref op0, op1;
11952 rtx inner = NULL_RTX;
11954 if (mode == VOIDmode)
11955 mode = GET_MODE (rtl);
11957 /* Note that for a dynamically sized array, the location we will generate a
11958 description of here will be the lowest numbered location which is
11959 actually within the array. That's *not* necessarily the same as the
11960 zeroth element of the array. */
11962 rtl = targetm.delegitimize_address (rtl);
11964 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11965 return NULL;
11967 switch (GET_CODE (rtl))
11969 case POST_INC:
11970 case POST_DEC:
11971 case POST_MODIFY:
11972 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11974 case SUBREG:
11975 /* The case of a subreg may arise when we have a local (register)
11976 variable or a formal (register) parameter which doesn't quite fill
11977 up an entire register. For now, just assume that it is
11978 legitimate to make the Dwarf info refer to the whole register which
11979 contains the given subreg. */
11980 if (!subreg_lowpart_p (rtl))
11981 break;
11982 inner = SUBREG_REG (rtl);
11983 case TRUNCATE:
11984 if (inner == NULL_RTX)
11985 inner = XEXP (rtl, 0);
11986 if (GET_MODE_CLASS (mode) == MODE_INT
11987 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11988 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11989 #ifdef POINTERS_EXTEND_UNSIGNED
11990 || (mode == Pmode && mem_mode != VOIDmode)
11991 #endif
11993 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11995 mem_loc_result = mem_loc_descriptor (inner,
11996 GET_MODE (inner),
11997 mem_mode, initialized);
11998 break;
12000 if (dwarf_strict)
12001 break;
12002 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12003 break;
12004 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12005 && (GET_MODE_CLASS (mode) != MODE_INT
12006 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12007 break;
12008 else
12010 dw_die_ref type_die;
12011 dw_loc_descr_ref cvt;
12013 mem_loc_result = mem_loc_descriptor (inner,
12014 GET_MODE (inner),
12015 mem_mode, initialized);
12016 if (mem_loc_result == NULL)
12017 break;
12018 type_die = base_type_for_mode (mode,
12019 GET_MODE_CLASS (mode) == MODE_INT);
12020 if (type_die == NULL)
12022 mem_loc_result = NULL;
12023 break;
12025 if (GET_MODE_SIZE (mode)
12026 != GET_MODE_SIZE (GET_MODE (inner)))
12027 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12028 else
12029 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12030 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12031 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12032 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12033 add_loc_descr (&mem_loc_result, cvt);
12035 break;
12037 case REG:
12038 if (GET_MODE_CLASS (mode) != MODE_INT
12039 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12040 && rtl != arg_pointer_rtx
12041 && rtl != frame_pointer_rtx
12042 #ifdef POINTERS_EXTEND_UNSIGNED
12043 && (mode != Pmode || mem_mode == VOIDmode)
12044 #endif
12047 dw_die_ref type_die;
12048 unsigned int dbx_regnum;
12050 if (dwarf_strict)
12051 break;
12052 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12053 break;
12054 type_die = base_type_for_mode (mode,
12055 GET_MODE_CLASS (mode) == MODE_INT);
12056 if (type_die == NULL)
12057 break;
12059 dbx_regnum = dbx_reg_number (rtl);
12060 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12061 break;
12062 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12063 dbx_regnum, 0);
12064 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12065 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12066 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12067 break;
12069 /* Whenever a register number forms a part of the description of the
12070 method for calculating the (dynamic) address of a memory resident
12071 object, DWARF rules require the register number be referred to as
12072 a "base register". This distinction is not based in any way upon
12073 what category of register the hardware believes the given register
12074 belongs to. This is strictly DWARF terminology we're dealing with
12075 here. Note that in cases where the location of a memory-resident
12076 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12077 OP_CONST (0)) the actual DWARF location descriptor that we generate
12078 may just be OP_BASEREG (basereg). This may look deceptively like
12079 the object in question was allocated to a register (rather than in
12080 memory) so DWARF consumers need to be aware of the subtle
12081 distinction between OP_REG and OP_BASEREG. */
12082 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12083 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12084 else if (stack_realign_drap
12085 && crtl->drap_reg
12086 && crtl->args.internal_arg_pointer == rtl
12087 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12089 /* If RTL is internal_arg_pointer, which has been optimized
12090 out, use DRAP instead. */
12091 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12092 VAR_INIT_STATUS_INITIALIZED);
12094 break;
12096 case SIGN_EXTEND:
12097 case ZERO_EXTEND:
12098 if (GET_MODE_CLASS (mode) != MODE_INT)
12099 break;
12100 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12101 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12102 if (op0 == 0)
12103 break;
12104 else if (GET_CODE (rtl) == ZERO_EXTEND
12105 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12106 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12107 < HOST_BITS_PER_WIDE_INT
12108 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12109 to expand zero extend as two shifts instead of
12110 masking. */
12111 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12113 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12114 mem_loc_result = op0;
12115 add_loc_descr (&mem_loc_result,
12116 int_loc_descriptor (GET_MODE_MASK (imode)));
12117 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12119 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12121 int shift = DWARF2_ADDR_SIZE
12122 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12123 shift *= BITS_PER_UNIT;
12124 if (GET_CODE (rtl) == SIGN_EXTEND)
12125 op = DW_OP_shra;
12126 else
12127 op = DW_OP_shr;
12128 mem_loc_result = op0;
12129 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12130 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12131 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12132 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12134 else if (!dwarf_strict)
12136 dw_die_ref type_die1, type_die2;
12137 dw_loc_descr_ref cvt;
12139 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12140 GET_CODE (rtl) == ZERO_EXTEND);
12141 if (type_die1 == NULL)
12142 break;
12143 type_die2 = base_type_for_mode (mode, 1);
12144 if (type_die2 == NULL)
12145 break;
12146 mem_loc_result = op0;
12147 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12148 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12149 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12150 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12151 add_loc_descr (&mem_loc_result, cvt);
12152 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12153 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12154 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12155 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12156 add_loc_descr (&mem_loc_result, cvt);
12158 break;
12160 case MEM:
12162 rtx new_rtl = avoid_constant_pool_reference (rtl);
12163 if (new_rtl != rtl)
12165 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12166 initialized);
12167 if (mem_loc_result != NULL)
12168 return mem_loc_result;
12171 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12172 get_address_mode (rtl), mode,
12173 VAR_INIT_STATUS_INITIALIZED);
12174 if (mem_loc_result == NULL)
12175 mem_loc_result = tls_mem_loc_descriptor (rtl);
12176 if (mem_loc_result != NULL)
12178 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12179 || GET_MODE_CLASS (mode) != MODE_INT)
12181 dw_die_ref type_die;
12182 dw_loc_descr_ref deref;
12184 if (dwarf_strict)
12185 return NULL;
12186 type_die
12187 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12188 if (type_die == NULL)
12189 return NULL;
12190 deref = new_loc_descr (DW_OP_GNU_deref_type,
12191 GET_MODE_SIZE (mode), 0);
12192 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12193 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12194 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12195 add_loc_descr (&mem_loc_result, deref);
12197 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12198 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12199 else
12200 add_loc_descr (&mem_loc_result,
12201 new_loc_descr (DW_OP_deref_size,
12202 GET_MODE_SIZE (mode), 0));
12204 break;
12206 case LO_SUM:
12207 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12209 case LABEL_REF:
12210 /* Some ports can transform a symbol ref into a label ref, because
12211 the symbol ref is too far away and has to be dumped into a constant
12212 pool. */
12213 case CONST:
12214 case SYMBOL_REF:
12215 if (GET_MODE_CLASS (mode) != MODE_INT
12216 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12217 #ifdef POINTERS_EXTEND_UNSIGNED
12218 && (mode != Pmode || mem_mode == VOIDmode)
12219 #endif
12221 break;
12222 if (GET_CODE (rtl) == SYMBOL_REF
12223 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12225 dw_loc_descr_ref temp;
12227 /* If this is not defined, we have no way to emit the data. */
12228 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12229 break;
12231 temp = new_addr_loc_descr (rtl, dtprel_true);
12233 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12234 add_loc_descr (&mem_loc_result, temp);
12236 break;
12239 if (!const_ok_for_output (rtl))
12240 break;
12242 symref:
12243 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12244 vec_safe_push (used_rtx_array, rtl);
12245 break;
12247 case CONCAT:
12248 case CONCATN:
12249 case VAR_LOCATION:
12250 case DEBUG_IMPLICIT_PTR:
12251 expansion_failed (NULL_TREE, rtl,
12252 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12253 return 0;
12255 case ENTRY_VALUE:
12256 if (dwarf_strict)
12257 return NULL;
12258 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12260 if (GET_MODE_CLASS (mode) != MODE_INT
12261 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12262 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12263 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12264 else
12266 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12267 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12268 return NULL;
12269 op0 = one_reg_loc_descriptor (dbx_regnum,
12270 VAR_INIT_STATUS_INITIALIZED);
12273 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12274 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12276 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12277 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12278 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12279 return NULL;
12281 else
12282 gcc_unreachable ();
12283 if (op0 == NULL)
12284 return NULL;
12285 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12286 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12287 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12288 break;
12290 case DEBUG_PARAMETER_REF:
12291 mem_loc_result = parameter_ref_descriptor (rtl);
12292 break;
12294 case PRE_MODIFY:
12295 /* Extract the PLUS expression nested inside and fall into
12296 PLUS code below. */
12297 rtl = XEXP (rtl, 1);
12298 goto plus;
12300 case PRE_INC:
12301 case PRE_DEC:
12302 /* Turn these into a PLUS expression and fall into the PLUS code
12303 below. */
12304 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12305 GEN_INT (GET_CODE (rtl) == PRE_INC
12306 ? GET_MODE_UNIT_SIZE (mem_mode)
12307 : -GET_MODE_UNIT_SIZE (mem_mode)));
12309 /* ... fall through ... */
12311 case PLUS:
12312 plus:
12313 if (is_based_loc (rtl)
12314 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12315 || XEXP (rtl, 0) == arg_pointer_rtx
12316 || XEXP (rtl, 0) == frame_pointer_rtx)
12317 && GET_MODE_CLASS (mode) == MODE_INT)
12318 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12319 INTVAL (XEXP (rtl, 1)),
12320 VAR_INIT_STATUS_INITIALIZED);
12321 else
12323 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12324 VAR_INIT_STATUS_INITIALIZED);
12325 if (mem_loc_result == 0)
12326 break;
12328 if (CONST_INT_P (XEXP (rtl, 1))
12329 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12330 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12331 else
12333 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12334 VAR_INIT_STATUS_INITIALIZED);
12335 if (op1 == 0)
12336 break;
12337 add_loc_descr (&mem_loc_result, op1);
12338 add_loc_descr (&mem_loc_result,
12339 new_loc_descr (DW_OP_plus, 0, 0));
12342 break;
12344 /* If a pseudo-reg is optimized away, it is possible for it to
12345 be replaced with a MEM containing a multiply or shift. */
12346 case MINUS:
12347 op = DW_OP_minus;
12348 goto do_binop;
12350 case MULT:
12351 op = DW_OP_mul;
12352 goto do_binop;
12354 case DIV:
12355 if (!dwarf_strict
12356 && GET_MODE_CLASS (mode) == MODE_INT
12357 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12359 mem_loc_result = typed_binop (DW_OP_div, rtl,
12360 base_type_for_mode (mode, 0),
12361 mode, mem_mode);
12362 break;
12364 op = DW_OP_div;
12365 goto do_binop;
12367 case UMOD:
12368 op = DW_OP_mod;
12369 goto do_binop;
12371 case ASHIFT:
12372 op = DW_OP_shl;
12373 goto do_shift;
12375 case ASHIFTRT:
12376 op = DW_OP_shra;
12377 goto do_shift;
12379 case LSHIFTRT:
12380 op = DW_OP_shr;
12381 goto do_shift;
12383 do_shift:
12384 if (GET_MODE_CLASS (mode) != MODE_INT)
12385 break;
12386 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12387 VAR_INIT_STATUS_INITIALIZED);
12389 rtx rtlop1 = XEXP (rtl, 1);
12390 if (GET_MODE (rtlop1) != VOIDmode
12391 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12392 < GET_MODE_BITSIZE (mode))
12393 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12394 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12395 VAR_INIT_STATUS_INITIALIZED);
12398 if (op0 == 0 || op1 == 0)
12399 break;
12401 mem_loc_result = op0;
12402 add_loc_descr (&mem_loc_result, op1);
12403 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12404 break;
12406 case AND:
12407 op = DW_OP_and;
12408 goto do_binop;
12410 case IOR:
12411 op = DW_OP_or;
12412 goto do_binop;
12414 case XOR:
12415 op = DW_OP_xor;
12416 goto do_binop;
12418 do_binop:
12419 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12420 VAR_INIT_STATUS_INITIALIZED);
12421 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12422 VAR_INIT_STATUS_INITIALIZED);
12424 if (op0 == 0 || op1 == 0)
12425 break;
12427 mem_loc_result = op0;
12428 add_loc_descr (&mem_loc_result, op1);
12429 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12430 break;
12432 case MOD:
12433 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12435 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12436 base_type_for_mode (mode, 0),
12437 mode, mem_mode);
12438 break;
12441 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12442 VAR_INIT_STATUS_INITIALIZED);
12443 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12444 VAR_INIT_STATUS_INITIALIZED);
12446 if (op0 == 0 || op1 == 0)
12447 break;
12449 mem_loc_result = op0;
12450 add_loc_descr (&mem_loc_result, op1);
12451 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12452 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12453 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12454 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12455 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12456 break;
12458 case UDIV:
12459 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12461 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12463 op = DW_OP_div;
12464 goto do_binop;
12466 mem_loc_result = typed_binop (DW_OP_div, rtl,
12467 base_type_for_mode (mode, 1),
12468 mode, mem_mode);
12470 break;
12472 case NOT:
12473 op = DW_OP_not;
12474 goto do_unop;
12476 case ABS:
12477 op = DW_OP_abs;
12478 goto do_unop;
12480 case NEG:
12481 op = DW_OP_neg;
12482 goto do_unop;
12484 do_unop:
12485 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12486 VAR_INIT_STATUS_INITIALIZED);
12488 if (op0 == 0)
12489 break;
12491 mem_loc_result = op0;
12492 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12493 break;
12495 case CONST_INT:
12496 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12497 #ifdef POINTERS_EXTEND_UNSIGNED
12498 || (mode == Pmode
12499 && mem_mode != VOIDmode
12500 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12501 #endif
12504 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12505 break;
12507 if (!dwarf_strict
12508 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12509 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12511 dw_die_ref type_die = base_type_for_mode (mode, 1);
12512 enum machine_mode amode;
12513 if (type_die == NULL)
12514 return NULL;
12515 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12516 MODE_INT, 0);
12517 if (INTVAL (rtl) >= 0
12518 && amode != BLKmode
12519 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12520 /* const DW_OP_GNU_convert <XXX> vs.
12521 DW_OP_GNU_const_type <XXX, 1, const>. */
12522 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12523 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12525 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12526 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12527 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12528 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12529 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12530 add_loc_descr (&mem_loc_result, op0);
12531 return mem_loc_result;
12533 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12534 INTVAL (rtl));
12535 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12536 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12537 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12538 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12539 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12540 else
12542 mem_loc_result->dw_loc_oprnd2.val_class
12543 = dw_val_class_const_double;
12544 mem_loc_result->dw_loc_oprnd2.v.val_double
12545 = double_int::from_shwi (INTVAL (rtl));
12548 break;
12550 case CONST_DOUBLE:
12551 if (!dwarf_strict)
12553 dw_die_ref type_die;
12555 /* Note that a CONST_DOUBLE rtx could represent either an integer
12556 or a floating-point constant. A CONST_DOUBLE is used whenever
12557 the constant requires more than one word in order to be
12558 adequately represented. We output CONST_DOUBLEs as blocks. */
12559 if (mode == VOIDmode
12560 || (GET_MODE (rtl) == VOIDmode
12561 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12562 break;
12563 type_die = base_type_for_mode (mode,
12564 GET_MODE_CLASS (mode) == MODE_INT);
12565 if (type_die == NULL)
12566 return NULL;
12567 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12568 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12569 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12570 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12571 if (SCALAR_FLOAT_MODE_P (mode))
12573 unsigned int length = GET_MODE_SIZE (mode);
12574 unsigned char *array
12575 = (unsigned char*) ggc_alloc_atomic (length);
12577 insert_float (rtl, array);
12578 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12579 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12580 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12581 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12583 else
12585 mem_loc_result->dw_loc_oprnd2.val_class
12586 = dw_val_class_const_double;
12587 mem_loc_result->dw_loc_oprnd2.v.val_double
12588 = rtx_to_double_int (rtl);
12591 break;
12593 case EQ:
12594 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12595 break;
12597 case GE:
12598 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12599 break;
12601 case GT:
12602 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12603 break;
12605 case LE:
12606 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12607 break;
12609 case LT:
12610 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12611 break;
12613 case NE:
12614 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12615 break;
12617 case GEU:
12618 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12619 break;
12621 case GTU:
12622 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12623 break;
12625 case LEU:
12626 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12627 break;
12629 case LTU:
12630 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12631 break;
12633 case UMIN:
12634 case UMAX:
12635 if (GET_MODE_CLASS (mode) != MODE_INT)
12636 break;
12637 /* FALLTHRU */
12638 case SMIN:
12639 case SMAX:
12640 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12641 break;
12643 case ZERO_EXTRACT:
12644 case SIGN_EXTRACT:
12645 if (CONST_INT_P (XEXP (rtl, 1))
12646 && CONST_INT_P (XEXP (rtl, 2))
12647 && ((unsigned) INTVAL (XEXP (rtl, 1))
12648 + (unsigned) INTVAL (XEXP (rtl, 2))
12649 <= GET_MODE_BITSIZE (mode))
12650 && GET_MODE_CLASS (mode) == MODE_INT
12651 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12652 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12654 int shift, size;
12655 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12656 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12657 if (op0 == 0)
12658 break;
12659 if (GET_CODE (rtl) == SIGN_EXTRACT)
12660 op = DW_OP_shra;
12661 else
12662 op = DW_OP_shr;
12663 mem_loc_result = op0;
12664 size = INTVAL (XEXP (rtl, 1));
12665 shift = INTVAL (XEXP (rtl, 2));
12666 if (BITS_BIG_ENDIAN)
12667 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12668 - shift - size;
12669 if (shift + size != (int) DWARF2_ADDR_SIZE)
12671 add_loc_descr (&mem_loc_result,
12672 int_loc_descriptor (DWARF2_ADDR_SIZE
12673 - shift - size));
12674 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12676 if (size != (int) DWARF2_ADDR_SIZE)
12678 add_loc_descr (&mem_loc_result,
12679 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12680 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12683 break;
12685 case IF_THEN_ELSE:
12687 dw_loc_descr_ref op2, bra_node, drop_node;
12688 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12689 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12690 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12691 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12692 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12693 VAR_INIT_STATUS_INITIALIZED);
12694 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12695 VAR_INIT_STATUS_INITIALIZED);
12696 if (op0 == NULL || op1 == NULL || op2 == NULL)
12697 break;
12699 mem_loc_result = op1;
12700 add_loc_descr (&mem_loc_result, op2);
12701 add_loc_descr (&mem_loc_result, op0);
12702 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12703 add_loc_descr (&mem_loc_result, bra_node);
12704 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12705 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12706 add_loc_descr (&mem_loc_result, drop_node);
12707 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12708 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12710 break;
12712 case FLOAT_EXTEND:
12713 case FLOAT_TRUNCATE:
12714 case FLOAT:
12715 case UNSIGNED_FLOAT:
12716 case FIX:
12717 case UNSIGNED_FIX:
12718 if (!dwarf_strict)
12720 dw_die_ref type_die;
12721 dw_loc_descr_ref cvt;
12723 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12724 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12725 if (op0 == NULL)
12726 break;
12727 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12728 && (GET_CODE (rtl) == FLOAT
12729 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12730 <= DWARF2_ADDR_SIZE))
12732 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12733 GET_CODE (rtl) == UNSIGNED_FLOAT);
12734 if (type_die == NULL)
12735 break;
12736 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12737 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12738 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12739 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12740 add_loc_descr (&op0, cvt);
12742 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12743 if (type_die == NULL)
12744 break;
12745 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12746 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12747 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12748 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12749 add_loc_descr (&op0, cvt);
12750 if (GET_MODE_CLASS (mode) == MODE_INT
12751 && (GET_CODE (rtl) == FIX
12752 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12754 op0 = convert_descriptor_to_mode (mode, op0);
12755 if (op0 == NULL)
12756 break;
12758 mem_loc_result = op0;
12760 break;
12762 case CLZ:
12763 case CTZ:
12764 case FFS:
12765 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12766 break;
12768 case POPCOUNT:
12769 case PARITY:
12770 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12771 break;
12773 case BSWAP:
12774 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12775 break;
12777 case ROTATE:
12778 case ROTATERT:
12779 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12780 break;
12782 case COMPARE:
12783 /* In theory, we could implement the above. */
12784 /* DWARF cannot represent the unsigned compare operations
12785 natively. */
12786 case SS_MULT:
12787 case US_MULT:
12788 case SS_DIV:
12789 case US_DIV:
12790 case SS_PLUS:
12791 case US_PLUS:
12792 case SS_MINUS:
12793 case US_MINUS:
12794 case SS_NEG:
12795 case US_NEG:
12796 case SS_ABS:
12797 case SS_ASHIFT:
12798 case US_ASHIFT:
12799 case SS_TRUNCATE:
12800 case US_TRUNCATE:
12801 case UNORDERED:
12802 case ORDERED:
12803 case UNEQ:
12804 case UNGE:
12805 case UNGT:
12806 case UNLE:
12807 case UNLT:
12808 case LTGT:
12809 case FRACT_CONVERT:
12810 case UNSIGNED_FRACT_CONVERT:
12811 case SAT_FRACT:
12812 case UNSIGNED_SAT_FRACT:
12813 case SQRT:
12814 case ASM_OPERANDS:
12815 case VEC_MERGE:
12816 case VEC_SELECT:
12817 case VEC_CONCAT:
12818 case VEC_DUPLICATE:
12819 case UNSPEC:
12820 case HIGH:
12821 case FMA:
12822 case STRICT_LOW_PART:
12823 case CONST_VECTOR:
12824 case CONST_FIXED:
12825 case CLRSB:
12826 case CLOBBER:
12827 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12828 can't express it in the debug info. This can happen e.g. with some
12829 TLS UNSPECs. */
12830 break;
12832 case CONST_STRING:
12833 resolve_one_addr (&rtl, NULL);
12834 goto symref;
12836 default:
12837 #ifdef ENABLE_CHECKING
12838 print_rtl (stderr, rtl);
12839 gcc_unreachable ();
12840 #else
12841 break;
12842 #endif
12845 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12846 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12848 return mem_loc_result;
12851 /* Return a descriptor that describes the concatenation of two locations.
12852 This is typically a complex variable. */
12854 static dw_loc_descr_ref
12855 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12857 dw_loc_descr_ref cc_loc_result = NULL;
12858 dw_loc_descr_ref x0_ref
12859 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12860 dw_loc_descr_ref x1_ref
12861 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12863 if (x0_ref == 0 || x1_ref == 0)
12864 return 0;
12866 cc_loc_result = x0_ref;
12867 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12869 add_loc_descr (&cc_loc_result, x1_ref);
12870 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12872 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12873 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12875 return cc_loc_result;
12878 /* Return a descriptor that describes the concatenation of N
12879 locations. */
12881 static dw_loc_descr_ref
12882 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12884 unsigned int i;
12885 dw_loc_descr_ref cc_loc_result = NULL;
12886 unsigned int n = XVECLEN (concatn, 0);
12888 for (i = 0; i < n; ++i)
12890 dw_loc_descr_ref ref;
12891 rtx x = XVECEXP (concatn, 0, i);
12893 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12894 if (ref == NULL)
12895 return NULL;
12897 add_loc_descr (&cc_loc_result, ref);
12898 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12901 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12902 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12904 return cc_loc_result;
12907 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12908 for DEBUG_IMPLICIT_PTR RTL. */
12910 static dw_loc_descr_ref
12911 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12913 dw_loc_descr_ref ret;
12914 dw_die_ref ref;
12916 if (dwarf_strict)
12917 return NULL;
12918 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12919 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12920 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12921 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12922 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12923 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12924 if (ref)
12926 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12927 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12928 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12930 else
12932 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12933 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12935 return ret;
12938 /* Output a proper Dwarf location descriptor for a variable or parameter
12939 which is either allocated in a register or in a memory location. For a
12940 register, we just generate an OP_REG and the register number. For a
12941 memory location we provide a Dwarf postfix expression describing how to
12942 generate the (dynamic) address of the object onto the address stack.
12944 MODE is mode of the decl if this loc_descriptor is going to be used in
12945 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12946 allowed, VOIDmode otherwise.
12948 If we don't know how to describe it, return 0. */
12950 static dw_loc_descr_ref
12951 loc_descriptor (rtx rtl, enum machine_mode mode,
12952 enum var_init_status initialized)
12954 dw_loc_descr_ref loc_result = NULL;
12956 switch (GET_CODE (rtl))
12958 case SUBREG:
12959 /* The case of a subreg may arise when we have a local (register)
12960 variable or a formal (register) parameter which doesn't quite fill
12961 up an entire register. For now, just assume that it is
12962 legitimate to make the Dwarf info refer to the whole register which
12963 contains the given subreg. */
12964 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12965 loc_result = loc_descriptor (SUBREG_REG (rtl),
12966 GET_MODE (SUBREG_REG (rtl)), initialized);
12967 else
12968 goto do_default;
12969 break;
12971 case REG:
12972 loc_result = reg_loc_descriptor (rtl, initialized);
12973 break;
12975 case MEM:
12976 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12977 GET_MODE (rtl), initialized);
12978 if (loc_result == NULL)
12979 loc_result = tls_mem_loc_descriptor (rtl);
12980 if (loc_result == NULL)
12982 rtx new_rtl = avoid_constant_pool_reference (rtl);
12983 if (new_rtl != rtl)
12984 loc_result = loc_descriptor (new_rtl, mode, initialized);
12986 break;
12988 case CONCAT:
12989 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12990 initialized);
12991 break;
12993 case CONCATN:
12994 loc_result = concatn_loc_descriptor (rtl, initialized);
12995 break;
12997 case VAR_LOCATION:
12998 /* Single part. */
12999 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13001 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13002 if (GET_CODE (loc) == EXPR_LIST)
13003 loc = XEXP (loc, 0);
13004 loc_result = loc_descriptor (loc, mode, initialized);
13005 break;
13008 rtl = XEXP (rtl, 1);
13009 /* FALLTHRU */
13011 case PARALLEL:
13013 rtvec par_elems = XVEC (rtl, 0);
13014 int num_elem = GET_NUM_ELEM (par_elems);
13015 enum machine_mode mode;
13016 int i;
13018 /* Create the first one, so we have something to add to. */
13019 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13020 VOIDmode, initialized);
13021 if (loc_result == NULL)
13022 return NULL;
13023 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13024 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13025 for (i = 1; i < num_elem; i++)
13027 dw_loc_descr_ref temp;
13029 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13030 VOIDmode, initialized);
13031 if (temp == NULL)
13032 return NULL;
13033 add_loc_descr (&loc_result, temp);
13034 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13035 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13038 break;
13040 case CONST_INT:
13041 if (mode != VOIDmode && mode != BLKmode)
13042 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13043 INTVAL (rtl));
13044 break;
13046 case CONST_DOUBLE:
13047 if (mode == VOIDmode)
13048 mode = GET_MODE (rtl);
13050 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13052 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13054 /* Note that a CONST_DOUBLE rtx could represent either an integer
13055 or a floating-point constant. A CONST_DOUBLE is used whenever
13056 the constant requires more than one word in order to be
13057 adequately represented. We output CONST_DOUBLEs as blocks. */
13058 loc_result = new_loc_descr (DW_OP_implicit_value,
13059 GET_MODE_SIZE (mode), 0);
13060 if (SCALAR_FLOAT_MODE_P (mode))
13062 unsigned int length = GET_MODE_SIZE (mode);
13063 unsigned char *array
13064 = (unsigned char*) ggc_alloc_atomic (length);
13066 insert_float (rtl, array);
13067 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13068 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13069 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13070 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13072 else
13074 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13075 loc_result->dw_loc_oprnd2.v.val_double
13076 = rtx_to_double_int (rtl);
13079 break;
13081 case CONST_VECTOR:
13082 if (mode == VOIDmode)
13083 mode = GET_MODE (rtl);
13085 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13087 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13088 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13089 unsigned char *array = (unsigned char *)
13090 ggc_alloc_atomic (length * elt_size);
13091 unsigned int i;
13092 unsigned char *p;
13094 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13095 switch (GET_MODE_CLASS (mode))
13097 case MODE_VECTOR_INT:
13098 for (i = 0, p = array; i < length; i++, p += elt_size)
13100 rtx elt = CONST_VECTOR_ELT (rtl, i);
13101 double_int val = rtx_to_double_int (elt);
13103 if (elt_size <= sizeof (HOST_WIDE_INT))
13104 insert_int (val.to_shwi (), elt_size, p);
13105 else
13107 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13108 insert_double (val, p);
13111 break;
13113 case MODE_VECTOR_FLOAT:
13114 for (i = 0, p = array; i < length; i++, p += elt_size)
13116 rtx elt = CONST_VECTOR_ELT (rtl, i);
13117 insert_float (elt, p);
13119 break;
13121 default:
13122 gcc_unreachable ();
13125 loc_result = new_loc_descr (DW_OP_implicit_value,
13126 length * elt_size, 0);
13127 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13128 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13129 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13130 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13132 break;
13134 case CONST:
13135 if (mode == VOIDmode
13136 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13137 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13138 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13140 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13141 break;
13143 /* FALLTHROUGH */
13144 case SYMBOL_REF:
13145 if (!const_ok_for_output (rtl))
13146 break;
13147 case LABEL_REF:
13148 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13149 && (dwarf_version >= 4 || !dwarf_strict))
13151 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13152 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13153 vec_safe_push (used_rtx_array, rtl);
13155 break;
13157 case DEBUG_IMPLICIT_PTR:
13158 loc_result = implicit_ptr_descriptor (rtl, 0);
13159 break;
13161 case PLUS:
13162 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13163 && CONST_INT_P (XEXP (rtl, 1)))
13165 loc_result
13166 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13167 break;
13169 /* FALLTHRU */
13170 do_default:
13171 default:
13172 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13173 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13174 && dwarf_version >= 4)
13175 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13177 /* Value expression. */
13178 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13179 if (loc_result)
13180 add_loc_descr (&loc_result,
13181 new_loc_descr (DW_OP_stack_value, 0, 0));
13183 break;
13186 return loc_result;
13189 /* We need to figure out what section we should use as the base for the
13190 address ranges where a given location is valid.
13191 1. If this particular DECL has a section associated with it, use that.
13192 2. If this function has a section associated with it, use that.
13193 3. Otherwise, use the text section.
13194 XXX: If you split a variable across multiple sections, we won't notice. */
13196 static const char *
13197 secname_for_decl (const_tree decl)
13199 const char *secname;
13201 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13203 tree sectree = DECL_SECTION_NAME (decl);
13204 secname = TREE_STRING_POINTER (sectree);
13206 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13208 tree sectree = DECL_SECTION_NAME (current_function_decl);
13209 secname = TREE_STRING_POINTER (sectree);
13211 else if (cfun && in_cold_section_p)
13212 secname = crtl->subsections.cold_section_label;
13213 else
13214 secname = text_section_label;
13216 return secname;
13219 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13221 static bool
13222 decl_by_reference_p (tree decl)
13224 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13225 || TREE_CODE (decl) == VAR_DECL)
13226 && DECL_BY_REFERENCE (decl));
13229 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13230 for VARLOC. */
13232 static dw_loc_descr_ref
13233 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13234 enum var_init_status initialized)
13236 int have_address = 0;
13237 dw_loc_descr_ref descr;
13238 enum machine_mode mode;
13240 if (want_address != 2)
13242 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13243 /* Single part. */
13244 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13246 varloc = PAT_VAR_LOCATION_LOC (varloc);
13247 if (GET_CODE (varloc) == EXPR_LIST)
13248 varloc = XEXP (varloc, 0);
13249 mode = GET_MODE (varloc);
13250 if (MEM_P (varloc))
13252 rtx addr = XEXP (varloc, 0);
13253 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13254 mode, initialized);
13255 if (descr)
13256 have_address = 1;
13257 else
13259 rtx x = avoid_constant_pool_reference (varloc);
13260 if (x != varloc)
13261 descr = mem_loc_descriptor (x, mode, VOIDmode,
13262 initialized);
13265 else
13266 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13268 else
13269 return 0;
13271 else
13273 if (GET_CODE (varloc) == VAR_LOCATION)
13274 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13275 else
13276 mode = DECL_MODE (loc);
13277 descr = loc_descriptor (varloc, mode, initialized);
13278 have_address = 1;
13281 if (!descr)
13282 return 0;
13284 if (want_address == 2 && !have_address
13285 && (dwarf_version >= 4 || !dwarf_strict))
13287 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13289 expansion_failed (loc, NULL_RTX,
13290 "DWARF address size mismatch");
13291 return 0;
13293 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13294 have_address = 1;
13296 /* Show if we can't fill the request for an address. */
13297 if (want_address && !have_address)
13299 expansion_failed (loc, NULL_RTX,
13300 "Want address and only have value");
13301 return 0;
13304 /* If we've got an address and don't want one, dereference. */
13305 if (!want_address && have_address)
13307 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13308 enum dwarf_location_atom op;
13310 if (size > DWARF2_ADDR_SIZE || size == -1)
13312 expansion_failed (loc, NULL_RTX,
13313 "DWARF address size mismatch");
13314 return 0;
13316 else if (size == DWARF2_ADDR_SIZE)
13317 op = DW_OP_deref;
13318 else
13319 op = DW_OP_deref_size;
13321 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13324 return descr;
13327 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13328 if it is not possible. */
13330 static dw_loc_descr_ref
13331 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13333 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13334 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13335 else if (dwarf_version >= 3 || !dwarf_strict)
13336 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13337 else
13338 return NULL;
13341 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13342 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13344 static dw_loc_descr_ref
13345 dw_sra_loc_expr (tree decl, rtx loc)
13347 rtx p;
13348 unsigned int padsize = 0;
13349 dw_loc_descr_ref descr, *descr_tail;
13350 unsigned HOST_WIDE_INT decl_size;
13351 rtx varloc;
13352 enum var_init_status initialized;
13354 if (DECL_SIZE (decl) == NULL
13355 || !host_integerp (DECL_SIZE (decl), 1))
13356 return NULL;
13358 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13359 descr = NULL;
13360 descr_tail = &descr;
13362 for (p = loc; p; p = XEXP (p, 1))
13364 unsigned int bitsize = decl_piece_bitsize (p);
13365 rtx loc_note = *decl_piece_varloc_ptr (p);
13366 dw_loc_descr_ref cur_descr;
13367 dw_loc_descr_ref *tail, last = NULL;
13368 unsigned int opsize = 0;
13370 if (loc_note == NULL_RTX
13371 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13373 padsize += bitsize;
13374 continue;
13376 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13377 varloc = NOTE_VAR_LOCATION (loc_note);
13378 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13379 if (cur_descr == NULL)
13381 padsize += bitsize;
13382 continue;
13385 /* Check that cur_descr either doesn't use
13386 DW_OP_*piece operations, or their sum is equal
13387 to bitsize. Otherwise we can't embed it. */
13388 for (tail = &cur_descr; *tail != NULL;
13389 tail = &(*tail)->dw_loc_next)
13390 if ((*tail)->dw_loc_opc == DW_OP_piece)
13392 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13393 * BITS_PER_UNIT;
13394 last = *tail;
13396 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13398 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13399 last = *tail;
13402 if (last != NULL && opsize != bitsize)
13404 padsize += bitsize;
13405 continue;
13408 /* If there is a hole, add DW_OP_*piece after empty DWARF
13409 expression, which means that those bits are optimized out. */
13410 if (padsize)
13412 if (padsize > decl_size)
13413 return NULL;
13414 decl_size -= padsize;
13415 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13416 if (*descr_tail == NULL)
13417 return NULL;
13418 descr_tail = &(*descr_tail)->dw_loc_next;
13419 padsize = 0;
13421 *descr_tail = cur_descr;
13422 descr_tail = tail;
13423 if (bitsize > decl_size)
13424 return NULL;
13425 decl_size -= bitsize;
13426 if (last == NULL)
13428 HOST_WIDE_INT offset = 0;
13429 if (GET_CODE (varloc) == VAR_LOCATION
13430 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13432 varloc = PAT_VAR_LOCATION_LOC (varloc);
13433 if (GET_CODE (varloc) == EXPR_LIST)
13434 varloc = XEXP (varloc, 0);
13438 if (GET_CODE (varloc) == CONST
13439 || GET_CODE (varloc) == SIGN_EXTEND
13440 || GET_CODE (varloc) == ZERO_EXTEND)
13441 varloc = XEXP (varloc, 0);
13442 else if (GET_CODE (varloc) == SUBREG)
13443 varloc = SUBREG_REG (varloc);
13444 else
13445 break;
13447 while (1);
13448 /* DW_OP_bit_size offset should be zero for register
13449 or implicit location descriptions and empty location
13450 descriptions, but for memory addresses needs big endian
13451 adjustment. */
13452 if (MEM_P (varloc))
13454 unsigned HOST_WIDE_INT memsize
13455 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13456 if (memsize != bitsize)
13458 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13459 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13460 return NULL;
13461 if (memsize < bitsize)
13462 return NULL;
13463 if (BITS_BIG_ENDIAN)
13464 offset = memsize - bitsize;
13468 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13469 if (*descr_tail == NULL)
13470 return NULL;
13471 descr_tail = &(*descr_tail)->dw_loc_next;
13475 /* If there were any non-empty expressions, add padding till the end of
13476 the decl. */
13477 if (descr != NULL && decl_size != 0)
13479 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13480 if (*descr_tail == NULL)
13481 return NULL;
13483 return descr;
13486 /* Return the dwarf representation of the location list LOC_LIST of
13487 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13488 function. */
13490 static dw_loc_list_ref
13491 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13493 const char *endname, *secname;
13494 rtx varloc;
13495 enum var_init_status initialized;
13496 struct var_loc_node *node;
13497 dw_loc_descr_ref descr;
13498 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13499 dw_loc_list_ref list = NULL;
13500 dw_loc_list_ref *listp = &list;
13502 /* Now that we know what section we are using for a base,
13503 actually construct the list of locations.
13504 The first location information is what is passed to the
13505 function that creates the location list, and the remaining
13506 locations just get added on to that list.
13507 Note that we only know the start address for a location
13508 (IE location changes), so to build the range, we use
13509 the range [current location start, next location start].
13510 This means we have to special case the last node, and generate
13511 a range of [last location start, end of function label]. */
13513 secname = secname_for_decl (decl);
13515 for (node = loc_list->first; node; node = node->next)
13516 if (GET_CODE (node->loc) == EXPR_LIST
13517 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13519 if (GET_CODE (node->loc) == EXPR_LIST)
13521 /* This requires DW_OP_{,bit_}piece, which is not usable
13522 inside DWARF expressions. */
13523 if (want_address != 2)
13524 continue;
13525 descr = dw_sra_loc_expr (decl, node->loc);
13526 if (descr == NULL)
13527 continue;
13529 else
13531 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13532 varloc = NOTE_VAR_LOCATION (node->loc);
13533 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13535 if (descr)
13537 bool range_across_switch = false;
13538 /* If section switch happens in between node->label
13539 and node->next->label (or end of function) and
13540 we can't emit it as a single entry list,
13541 emit two ranges, first one ending at the end
13542 of first partition and second one starting at the
13543 beginning of second partition. */
13544 if (node == loc_list->last_before_switch
13545 && (node != loc_list->first || loc_list->first->next)
13546 && current_function_decl)
13548 endname = cfun->fde->dw_fde_end;
13549 range_across_switch = true;
13551 /* The variable has a location between NODE->LABEL and
13552 NODE->NEXT->LABEL. */
13553 else if (node->next)
13554 endname = node->next->label;
13555 /* If the variable has a location at the last label
13556 it keeps its location until the end of function. */
13557 else if (!current_function_decl)
13558 endname = text_end_label;
13559 else
13561 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13562 current_function_funcdef_no);
13563 endname = ggc_strdup (label_id);
13566 *listp = new_loc_list (descr, node->label, endname, secname);
13567 if (TREE_CODE (decl) == PARM_DECL
13568 && node == loc_list->first
13569 && NOTE_P (node->loc)
13570 && strcmp (node->label, endname) == 0)
13571 (*listp)->force = true;
13572 listp = &(*listp)->dw_loc_next;
13574 if (range_across_switch)
13576 if (GET_CODE (node->loc) == EXPR_LIST)
13577 descr = dw_sra_loc_expr (decl, node->loc);
13578 else
13580 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13581 varloc = NOTE_VAR_LOCATION (node->loc);
13582 descr = dw_loc_list_1 (decl, varloc, want_address,
13583 initialized);
13585 gcc_assert (descr);
13586 /* The variable has a location between NODE->LABEL and
13587 NODE->NEXT->LABEL. */
13588 if (node->next)
13589 endname = node->next->label;
13590 else
13591 endname = cfun->fde->dw_fde_second_end;
13592 *listp = new_loc_list (descr,
13593 cfun->fde->dw_fde_second_begin,
13594 endname, secname);
13595 listp = &(*listp)->dw_loc_next;
13600 /* Try to avoid the overhead of a location list emitting a location
13601 expression instead, but only if we didn't have more than one
13602 location entry in the first place. If some entries were not
13603 representable, we don't want to pretend a single entry that was
13604 applies to the entire scope in which the variable is
13605 available. */
13606 if (list && loc_list->first->next)
13607 gen_llsym (list);
13609 return list;
13612 /* Return if the loc_list has only single element and thus can be represented
13613 as location description. */
13615 static bool
13616 single_element_loc_list_p (dw_loc_list_ref list)
13618 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13619 return !list->ll_symbol;
13622 /* To each location in list LIST add loc descr REF. */
13624 static void
13625 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13627 dw_loc_descr_ref copy;
13628 add_loc_descr (&list->expr, ref);
13629 list = list->dw_loc_next;
13630 while (list)
13632 copy = ggc_alloc_dw_loc_descr_node ();
13633 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13634 add_loc_descr (&list->expr, copy);
13635 while (copy->dw_loc_next)
13637 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13638 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13639 copy->dw_loc_next = new_copy;
13640 copy = new_copy;
13642 list = list->dw_loc_next;
13646 /* Given two lists RET and LIST
13647 produce location list that is result of adding expression in LIST
13648 to expression in RET on each position in program.
13649 Might be destructive on both RET and LIST.
13651 TODO: We handle only simple cases of RET or LIST having at most one
13652 element. General case would inolve sorting the lists in program order
13653 and merging them that will need some additional work.
13654 Adding that will improve quality of debug info especially for SRA-ed
13655 structures. */
13657 static void
13658 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13660 if (!list)
13661 return;
13662 if (!*ret)
13664 *ret = list;
13665 return;
13667 if (!list->dw_loc_next)
13669 add_loc_descr_to_each (*ret, list->expr);
13670 return;
13672 if (!(*ret)->dw_loc_next)
13674 add_loc_descr_to_each (list, (*ret)->expr);
13675 *ret = list;
13676 return;
13678 expansion_failed (NULL_TREE, NULL_RTX,
13679 "Don't know how to merge two non-trivial"
13680 " location lists.\n");
13681 *ret = NULL;
13682 return;
13685 /* LOC is constant expression. Try a luck, look it up in constant
13686 pool and return its loc_descr of its address. */
13688 static dw_loc_descr_ref
13689 cst_pool_loc_descr (tree loc)
13691 /* Get an RTL for this, if something has been emitted. */
13692 rtx rtl = lookup_constant_def (loc);
13694 if (!rtl || !MEM_P (rtl))
13696 gcc_assert (!rtl);
13697 return 0;
13699 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13701 /* TODO: We might get more coverage if we was actually delaying expansion
13702 of all expressions till end of compilation when constant pools are fully
13703 populated. */
13704 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13706 expansion_failed (loc, NULL_RTX,
13707 "CST value in contant pool but not marked.");
13708 return 0;
13710 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13711 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13714 /* Return dw_loc_list representing address of addr_expr LOC
13715 by looking for inner INDIRECT_REF expression and turning
13716 it into simple arithmetics. */
13718 static dw_loc_list_ref
13719 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13721 tree obj, offset;
13722 HOST_WIDE_INT bitsize, bitpos, bytepos;
13723 enum machine_mode mode;
13724 int unsignedp, volatilep = 0;
13725 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13727 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13728 &bitsize, &bitpos, &offset, &mode,
13729 &unsignedp, &volatilep, false);
13730 STRIP_NOPS (obj);
13731 if (bitpos % BITS_PER_UNIT)
13733 expansion_failed (loc, NULL_RTX, "bitfield access");
13734 return 0;
13736 if (!INDIRECT_REF_P (obj))
13738 expansion_failed (obj,
13739 NULL_RTX, "no indirect ref in inner refrence");
13740 return 0;
13742 if (!offset && !bitpos)
13743 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13744 else if (toplev
13745 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13746 && (dwarf_version >= 4 || !dwarf_strict))
13748 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13749 if (!list_ret)
13750 return 0;
13751 if (offset)
13753 /* Variable offset. */
13754 list_ret1 = loc_list_from_tree (offset, 0);
13755 if (list_ret1 == 0)
13756 return 0;
13757 add_loc_list (&list_ret, list_ret1);
13758 if (!list_ret)
13759 return 0;
13760 add_loc_descr_to_each (list_ret,
13761 new_loc_descr (DW_OP_plus, 0, 0));
13763 bytepos = bitpos / BITS_PER_UNIT;
13764 if (bytepos > 0)
13765 add_loc_descr_to_each (list_ret,
13766 new_loc_descr (DW_OP_plus_uconst,
13767 bytepos, 0));
13768 else if (bytepos < 0)
13769 loc_list_plus_const (list_ret, bytepos);
13770 add_loc_descr_to_each (list_ret,
13771 new_loc_descr (DW_OP_stack_value, 0, 0));
13773 return list_ret;
13777 /* Generate Dwarf location list representing LOC.
13778 If WANT_ADDRESS is false, expression computing LOC will be computed
13779 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13780 if WANT_ADDRESS is 2, expression computing address useable in location
13781 will be returned (i.e. DW_OP_reg can be used
13782 to refer to register values). */
13784 static dw_loc_list_ref
13785 loc_list_from_tree (tree loc, int want_address)
13787 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13788 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13789 int have_address = 0;
13790 enum dwarf_location_atom op;
13792 /* ??? Most of the time we do not take proper care for sign/zero
13793 extending the values properly. Hopefully this won't be a real
13794 problem... */
13796 switch (TREE_CODE (loc))
13798 case ERROR_MARK:
13799 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13800 return 0;
13802 case PLACEHOLDER_EXPR:
13803 /* This case involves extracting fields from an object to determine the
13804 position of other fields. We don't try to encode this here. The
13805 only user of this is Ada, which encodes the needed information using
13806 the names of types. */
13807 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13808 return 0;
13810 case CALL_EXPR:
13811 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13812 /* There are no opcodes for these operations. */
13813 return 0;
13815 case PREINCREMENT_EXPR:
13816 case PREDECREMENT_EXPR:
13817 case POSTINCREMENT_EXPR:
13818 case POSTDECREMENT_EXPR:
13819 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13820 /* There are no opcodes for these operations. */
13821 return 0;
13823 case ADDR_EXPR:
13824 /* If we already want an address, see if there is INDIRECT_REF inside
13825 e.g. for &this->field. */
13826 if (want_address)
13828 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13829 (loc, want_address == 2);
13830 if (list_ret)
13831 have_address = 1;
13832 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13833 && (ret = cst_pool_loc_descr (loc)))
13834 have_address = 1;
13836 /* Otherwise, process the argument and look for the address. */
13837 if (!list_ret && !ret)
13838 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13839 else
13841 if (want_address)
13842 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13843 return NULL;
13845 break;
13847 case VAR_DECL:
13848 if (DECL_THREAD_LOCAL_P (loc))
13850 rtx rtl;
13851 enum dwarf_location_atom tls_op;
13852 enum dtprel_bool dtprel = dtprel_false;
13854 if (targetm.have_tls)
13856 /* If this is not defined, we have no way to emit the
13857 data. */
13858 if (!targetm.asm_out.output_dwarf_dtprel)
13859 return 0;
13861 /* The way DW_OP_GNU_push_tls_address is specified, we
13862 can only look up addresses of objects in the current
13863 module. We used DW_OP_addr as first op, but that's
13864 wrong, because DW_OP_addr is relocated by the debug
13865 info consumer, while DW_OP_GNU_push_tls_address
13866 operand shouldn't be. */
13867 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13868 return 0;
13869 dtprel = dtprel_true;
13870 tls_op = DW_OP_GNU_push_tls_address;
13872 else
13874 if (!targetm.emutls.debug_form_tls_address
13875 || !(dwarf_version >= 3 || !dwarf_strict))
13876 return 0;
13877 /* We stuffed the control variable into the DECL_VALUE_EXPR
13878 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13879 no longer appear in gimple code. We used the control
13880 variable in specific so that we could pick it up here. */
13881 loc = DECL_VALUE_EXPR (loc);
13882 tls_op = DW_OP_form_tls_address;
13885 rtl = rtl_for_decl_location (loc);
13886 if (rtl == NULL_RTX)
13887 return 0;
13889 if (!MEM_P (rtl))
13890 return 0;
13891 rtl = XEXP (rtl, 0);
13892 if (! CONSTANT_P (rtl))
13893 return 0;
13895 ret = new_addr_loc_descr (rtl, dtprel);
13896 ret1 = new_loc_descr (tls_op, 0, 0);
13897 add_loc_descr (&ret, ret1);
13899 have_address = 1;
13900 break;
13902 /* FALLTHRU */
13904 case PARM_DECL:
13905 case RESULT_DECL:
13906 if (DECL_HAS_VALUE_EXPR_P (loc))
13907 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13908 want_address);
13909 /* FALLTHRU */
13911 case FUNCTION_DECL:
13913 rtx rtl;
13914 var_loc_list *loc_list = lookup_decl_loc (loc);
13916 if (loc_list && loc_list->first)
13918 list_ret = dw_loc_list (loc_list, loc, want_address);
13919 have_address = want_address != 0;
13920 break;
13922 rtl = rtl_for_decl_location (loc);
13923 if (rtl == NULL_RTX)
13925 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13926 return 0;
13928 else if (CONST_INT_P (rtl))
13930 HOST_WIDE_INT val = INTVAL (rtl);
13931 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13932 val &= GET_MODE_MASK (DECL_MODE (loc));
13933 ret = int_loc_descriptor (val);
13935 else if (GET_CODE (rtl) == CONST_STRING)
13937 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13938 return 0;
13940 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13941 ret = new_addr_loc_descr (rtl, dtprel_false);
13942 else
13944 enum machine_mode mode, mem_mode;
13946 /* Certain constructs can only be represented at top-level. */
13947 if (want_address == 2)
13949 ret = loc_descriptor (rtl, VOIDmode,
13950 VAR_INIT_STATUS_INITIALIZED);
13951 have_address = 1;
13953 else
13955 mode = GET_MODE (rtl);
13956 mem_mode = VOIDmode;
13957 if (MEM_P (rtl))
13959 mem_mode = mode;
13960 mode = get_address_mode (rtl);
13961 rtl = XEXP (rtl, 0);
13962 have_address = 1;
13964 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13965 VAR_INIT_STATUS_INITIALIZED);
13967 if (!ret)
13968 expansion_failed (loc, rtl,
13969 "failed to produce loc descriptor for rtl");
13972 break;
13974 case MEM_REF:
13975 /* ??? FIXME. */
13976 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13977 return 0;
13978 /* Fallthru. */
13979 case INDIRECT_REF:
13980 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13981 have_address = 1;
13982 break;
13984 case COMPOUND_EXPR:
13985 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13987 CASE_CONVERT:
13988 case VIEW_CONVERT_EXPR:
13989 case SAVE_EXPR:
13990 case MODIFY_EXPR:
13991 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13993 case COMPONENT_REF:
13994 case BIT_FIELD_REF:
13995 case ARRAY_REF:
13996 case ARRAY_RANGE_REF:
13997 case REALPART_EXPR:
13998 case IMAGPART_EXPR:
14000 tree obj, offset;
14001 HOST_WIDE_INT bitsize, bitpos, bytepos;
14002 enum machine_mode mode;
14003 int unsignedp, volatilep = 0;
14005 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14006 &unsignedp, &volatilep, false);
14008 gcc_assert (obj != loc);
14010 list_ret = loc_list_from_tree (obj,
14011 want_address == 2
14012 && !bitpos && !offset ? 2 : 1);
14013 /* TODO: We can extract value of the small expression via shifting even
14014 for nonzero bitpos. */
14015 if (list_ret == 0)
14016 return 0;
14017 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14019 expansion_failed (loc, NULL_RTX,
14020 "bitfield access");
14021 return 0;
14024 if (offset != NULL_TREE)
14026 /* Variable offset. */
14027 list_ret1 = loc_list_from_tree (offset, 0);
14028 if (list_ret1 == 0)
14029 return 0;
14030 add_loc_list (&list_ret, list_ret1);
14031 if (!list_ret)
14032 return 0;
14033 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14036 bytepos = bitpos / BITS_PER_UNIT;
14037 if (bytepos > 0)
14038 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14039 else if (bytepos < 0)
14040 loc_list_plus_const (list_ret, bytepos);
14042 have_address = 1;
14043 break;
14046 case INTEGER_CST:
14047 if ((want_address || !host_integerp (loc, 0))
14048 && (ret = cst_pool_loc_descr (loc)))
14049 have_address = 1;
14050 else if (want_address == 2
14051 && host_integerp (loc, 0)
14052 && (ret = address_of_int_loc_descriptor
14053 (int_size_in_bytes (TREE_TYPE (loc)),
14054 tree_low_cst (loc, 0))))
14055 have_address = 1;
14056 else if (host_integerp (loc, 0))
14057 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14058 else
14060 expansion_failed (loc, NULL_RTX,
14061 "Integer operand is not host integer");
14062 return 0;
14064 break;
14066 case CONSTRUCTOR:
14067 case REAL_CST:
14068 case STRING_CST:
14069 case COMPLEX_CST:
14070 if ((ret = cst_pool_loc_descr (loc)))
14071 have_address = 1;
14072 else
14073 /* We can construct small constants here using int_loc_descriptor. */
14074 expansion_failed (loc, NULL_RTX,
14075 "constructor or constant not in constant pool");
14076 break;
14078 case TRUTH_AND_EXPR:
14079 case TRUTH_ANDIF_EXPR:
14080 case BIT_AND_EXPR:
14081 op = DW_OP_and;
14082 goto do_binop;
14084 case TRUTH_XOR_EXPR:
14085 case BIT_XOR_EXPR:
14086 op = DW_OP_xor;
14087 goto do_binop;
14089 case TRUTH_OR_EXPR:
14090 case TRUTH_ORIF_EXPR:
14091 case BIT_IOR_EXPR:
14092 op = DW_OP_or;
14093 goto do_binop;
14095 case FLOOR_DIV_EXPR:
14096 case CEIL_DIV_EXPR:
14097 case ROUND_DIV_EXPR:
14098 case TRUNC_DIV_EXPR:
14099 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14100 return 0;
14101 op = DW_OP_div;
14102 goto do_binop;
14104 case MINUS_EXPR:
14105 op = DW_OP_minus;
14106 goto do_binop;
14108 case FLOOR_MOD_EXPR:
14109 case CEIL_MOD_EXPR:
14110 case ROUND_MOD_EXPR:
14111 case TRUNC_MOD_EXPR:
14112 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14114 op = DW_OP_mod;
14115 goto do_binop;
14117 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14118 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14119 if (list_ret == 0 || list_ret1 == 0)
14120 return 0;
14122 add_loc_list (&list_ret, list_ret1);
14123 if (list_ret == 0)
14124 return 0;
14125 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14126 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14127 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14128 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14129 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14130 break;
14132 case MULT_EXPR:
14133 op = DW_OP_mul;
14134 goto do_binop;
14136 case LSHIFT_EXPR:
14137 op = DW_OP_shl;
14138 goto do_binop;
14140 case RSHIFT_EXPR:
14141 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14142 goto do_binop;
14144 case POINTER_PLUS_EXPR:
14145 case PLUS_EXPR:
14146 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14148 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14149 if (list_ret == 0)
14150 return 0;
14152 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14153 break;
14156 op = DW_OP_plus;
14157 goto do_binop;
14159 case LE_EXPR:
14160 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14161 return 0;
14163 op = DW_OP_le;
14164 goto do_binop;
14166 case GE_EXPR:
14167 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14168 return 0;
14170 op = DW_OP_ge;
14171 goto do_binop;
14173 case LT_EXPR:
14174 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14175 return 0;
14177 op = DW_OP_lt;
14178 goto do_binop;
14180 case GT_EXPR:
14181 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14182 return 0;
14184 op = DW_OP_gt;
14185 goto do_binop;
14187 case EQ_EXPR:
14188 op = DW_OP_eq;
14189 goto do_binop;
14191 case NE_EXPR:
14192 op = DW_OP_ne;
14193 goto do_binop;
14195 do_binop:
14196 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14197 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14198 if (list_ret == 0 || list_ret1 == 0)
14199 return 0;
14201 add_loc_list (&list_ret, list_ret1);
14202 if (list_ret == 0)
14203 return 0;
14204 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14205 break;
14207 case TRUTH_NOT_EXPR:
14208 case BIT_NOT_EXPR:
14209 op = DW_OP_not;
14210 goto do_unop;
14212 case ABS_EXPR:
14213 op = DW_OP_abs;
14214 goto do_unop;
14216 case NEGATE_EXPR:
14217 op = DW_OP_neg;
14218 goto do_unop;
14220 do_unop:
14221 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14222 if (list_ret == 0)
14223 return 0;
14225 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14226 break;
14228 case MIN_EXPR:
14229 case MAX_EXPR:
14231 const enum tree_code code =
14232 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14234 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14235 build2 (code, integer_type_node,
14236 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14237 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14240 /* ... fall through ... */
14242 case COND_EXPR:
14244 dw_loc_descr_ref lhs
14245 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14246 dw_loc_list_ref rhs
14247 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14248 dw_loc_descr_ref bra_node, jump_node, tmp;
14250 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14251 if (list_ret == 0 || lhs == 0 || rhs == 0)
14252 return 0;
14254 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14255 add_loc_descr_to_each (list_ret, bra_node);
14257 add_loc_list (&list_ret, rhs);
14258 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14259 add_loc_descr_to_each (list_ret, jump_node);
14261 add_loc_descr_to_each (list_ret, lhs);
14262 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14263 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14265 /* ??? Need a node to point the skip at. Use a nop. */
14266 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14267 add_loc_descr_to_each (list_ret, tmp);
14268 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14269 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14271 break;
14273 case FIX_TRUNC_EXPR:
14274 return 0;
14276 default:
14277 /* Leave front-end specific codes as simply unknown. This comes
14278 up, for instance, with the C STMT_EXPR. */
14279 if ((unsigned int) TREE_CODE (loc)
14280 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14282 expansion_failed (loc, NULL_RTX,
14283 "language specific tree node");
14284 return 0;
14287 #ifdef ENABLE_CHECKING
14288 /* Otherwise this is a generic code; we should just lists all of
14289 these explicitly. We forgot one. */
14290 gcc_unreachable ();
14291 #else
14292 /* In a release build, we want to degrade gracefully: better to
14293 generate incomplete debugging information than to crash. */
14294 return NULL;
14295 #endif
14298 if (!ret && !list_ret)
14299 return 0;
14301 if (want_address == 2 && !have_address
14302 && (dwarf_version >= 4 || !dwarf_strict))
14304 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14306 expansion_failed (loc, NULL_RTX,
14307 "DWARF address size mismatch");
14308 return 0;
14310 if (ret)
14311 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14312 else
14313 add_loc_descr_to_each (list_ret,
14314 new_loc_descr (DW_OP_stack_value, 0, 0));
14315 have_address = 1;
14317 /* Show if we can't fill the request for an address. */
14318 if (want_address && !have_address)
14320 expansion_failed (loc, NULL_RTX,
14321 "Want address and only have value");
14322 return 0;
14325 gcc_assert (!ret || !list_ret);
14327 /* If we've got an address and don't want one, dereference. */
14328 if (!want_address && have_address)
14330 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14332 if (size > DWARF2_ADDR_SIZE || size == -1)
14334 expansion_failed (loc, NULL_RTX,
14335 "DWARF address size mismatch");
14336 return 0;
14338 else if (size == DWARF2_ADDR_SIZE)
14339 op = DW_OP_deref;
14340 else
14341 op = DW_OP_deref_size;
14343 if (ret)
14344 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14345 else
14346 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14348 if (ret)
14349 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14351 return list_ret;
14354 /* Same as above but return only single location expression. */
14355 static dw_loc_descr_ref
14356 loc_descriptor_from_tree (tree loc, int want_address)
14358 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14359 if (!ret)
14360 return NULL;
14361 if (ret->dw_loc_next)
14363 expansion_failed (loc, NULL_RTX,
14364 "Location list where only loc descriptor needed");
14365 return NULL;
14367 return ret->expr;
14370 /* Given a value, round it up to the lowest multiple of `boundary'
14371 which is not less than the value itself. */
14373 static inline HOST_WIDE_INT
14374 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14376 return (((value + boundary - 1) / boundary) * boundary);
14379 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14380 pointer to the declared type for the relevant field variable, or return
14381 `integer_type_node' if the given node turns out to be an
14382 ERROR_MARK node. */
14384 static inline tree
14385 field_type (const_tree decl)
14387 tree type;
14389 if (TREE_CODE (decl) == ERROR_MARK)
14390 return integer_type_node;
14392 type = DECL_BIT_FIELD_TYPE (decl);
14393 if (type == NULL_TREE)
14394 type = TREE_TYPE (decl);
14396 return type;
14399 /* Given a pointer to a tree node, return the alignment in bits for
14400 it, or else return BITS_PER_WORD if the node actually turns out to
14401 be an ERROR_MARK node. */
14403 static inline unsigned
14404 simple_type_align_in_bits (const_tree type)
14406 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14409 static inline unsigned
14410 simple_decl_align_in_bits (const_tree decl)
14412 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14415 /* Return the result of rounding T up to ALIGN. */
14417 static inline double_int
14418 round_up_to_align (double_int t, unsigned int align)
14420 double_int alignd = double_int::from_uhwi (align);
14421 t += alignd;
14422 t += double_int_minus_one;
14423 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14424 t *= alignd;
14425 return t;
14428 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14429 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14430 or return 0 if we are unable to determine what that offset is, either
14431 because the argument turns out to be a pointer to an ERROR_MARK node, or
14432 because the offset is actually variable. (We can't handle the latter case
14433 just yet). */
14435 static HOST_WIDE_INT
14436 field_byte_offset (const_tree decl)
14438 double_int object_offset_in_bits;
14439 double_int object_offset_in_bytes;
14440 double_int bitpos_int;
14442 if (TREE_CODE (decl) == ERROR_MARK)
14443 return 0;
14445 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14447 /* We cannot yet cope with fields whose positions are variable, so
14448 for now, when we see such things, we simply return 0. Someday, we may
14449 be able to handle such cases, but it will be damn difficult. */
14450 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14451 return 0;
14453 bitpos_int = tree_to_double_int (bit_position (decl));
14455 #ifdef PCC_BITFIELD_TYPE_MATTERS
14456 if (PCC_BITFIELD_TYPE_MATTERS)
14458 tree type;
14459 tree field_size_tree;
14460 double_int deepest_bitpos;
14461 double_int field_size_in_bits;
14462 unsigned int type_align_in_bits;
14463 unsigned int decl_align_in_bits;
14464 double_int type_size_in_bits;
14466 type = field_type (decl);
14467 type_size_in_bits = double_int_type_size_in_bits (type);
14468 type_align_in_bits = simple_type_align_in_bits (type);
14470 field_size_tree = DECL_SIZE (decl);
14472 /* The size could be unspecified if there was an error, or for
14473 a flexible array member. */
14474 if (!field_size_tree)
14475 field_size_tree = bitsize_zero_node;
14477 /* If the size of the field is not constant, use the type size. */
14478 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14479 field_size_in_bits = tree_to_double_int (field_size_tree);
14480 else
14481 field_size_in_bits = type_size_in_bits;
14483 decl_align_in_bits = simple_decl_align_in_bits (decl);
14485 /* The GCC front-end doesn't make any attempt to keep track of the
14486 starting bit offset (relative to the start of the containing
14487 structure type) of the hypothetical "containing object" for a
14488 bit-field. Thus, when computing the byte offset value for the
14489 start of the "containing object" of a bit-field, we must deduce
14490 this information on our own. This can be rather tricky to do in
14491 some cases. For example, handling the following structure type
14492 definition when compiling for an i386/i486 target (which only
14493 aligns long long's to 32-bit boundaries) can be very tricky:
14495 struct S { int field1; long long field2:31; };
14497 Fortunately, there is a simple rule-of-thumb which can be used
14498 in such cases. When compiling for an i386/i486, GCC will
14499 allocate 8 bytes for the structure shown above. It decides to
14500 do this based upon one simple rule for bit-field allocation.
14501 GCC allocates each "containing object" for each bit-field at
14502 the first (i.e. lowest addressed) legitimate alignment boundary
14503 (based upon the required minimum alignment for the declared
14504 type of the field) which it can possibly use, subject to the
14505 condition that there is still enough available space remaining
14506 in the containing object (when allocated at the selected point)
14507 to fully accommodate all of the bits of the bit-field itself.
14509 This simple rule makes it obvious why GCC allocates 8 bytes for
14510 each object of the structure type shown above. When looking
14511 for a place to allocate the "containing object" for `field2',
14512 the compiler simply tries to allocate a 64-bit "containing
14513 object" at each successive 32-bit boundary (starting at zero)
14514 until it finds a place to allocate that 64- bit field such that
14515 at least 31 contiguous (and previously unallocated) bits remain
14516 within that selected 64 bit field. (As it turns out, for the
14517 example above, the compiler finds it is OK to allocate the
14518 "containing object" 64-bit field at bit-offset zero within the
14519 structure type.)
14521 Here we attempt to work backwards from the limited set of facts
14522 we're given, and we try to deduce from those facts, where GCC
14523 must have believed that the containing object started (within
14524 the structure type). The value we deduce is then used (by the
14525 callers of this routine) to generate DW_AT_location and
14526 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14527 the case of DW_AT_location, regular fields as well). */
14529 /* Figure out the bit-distance from the start of the structure to
14530 the "deepest" bit of the bit-field. */
14531 deepest_bitpos = bitpos_int + field_size_in_bits;
14533 /* This is the tricky part. Use some fancy footwork to deduce
14534 where the lowest addressed bit of the containing object must
14535 be. */
14536 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14538 /* Round up to type_align by default. This works best for
14539 bitfields. */
14540 object_offset_in_bits
14541 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14543 if (object_offset_in_bits.ugt (bitpos_int))
14545 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14547 /* Round up to decl_align instead. */
14548 object_offset_in_bits
14549 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14552 else
14553 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14554 object_offset_in_bits = bitpos_int;
14556 object_offset_in_bytes
14557 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14558 true, TRUNC_DIV_EXPR);
14559 return object_offset_in_bytes.to_shwi ();
14562 /* The following routines define various Dwarf attributes and any data
14563 associated with them. */
14565 /* Add a location description attribute value to a DIE.
14567 This emits location attributes suitable for whole variables and
14568 whole parameters. Note that the location attributes for struct fields are
14569 generated by the routine `data_member_location_attribute' below. */
14571 static inline void
14572 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14573 dw_loc_list_ref descr)
14575 if (descr == 0)
14576 return;
14577 if (single_element_loc_list_p (descr))
14578 add_AT_loc (die, attr_kind, descr->expr);
14579 else
14580 add_AT_loc_list (die, attr_kind, descr);
14583 /* Add DW_AT_accessibility attribute to DIE if needed. */
14585 static void
14586 add_accessibility_attribute (dw_die_ref die, tree decl)
14588 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14589 children, otherwise the default is DW_ACCESS_public. In DWARF2
14590 the default has always been DW_ACCESS_public. */
14591 if (TREE_PROTECTED (decl))
14592 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14593 else if (TREE_PRIVATE (decl))
14595 if (dwarf_version == 2
14596 || die->die_parent == NULL
14597 || die->die_parent->die_tag != DW_TAG_class_type)
14598 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14600 else if (dwarf_version > 2
14601 && die->die_parent
14602 && die->die_parent->die_tag == DW_TAG_class_type)
14603 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14606 /* Attach the specialized form of location attribute used for data members of
14607 struct and union types. In the special case of a FIELD_DECL node which
14608 represents a bit-field, the "offset" part of this special location
14609 descriptor must indicate the distance in bytes from the lowest-addressed
14610 byte of the containing struct or union type to the lowest-addressed byte of
14611 the "containing object" for the bit-field. (See the `field_byte_offset'
14612 function above).
14614 For any given bit-field, the "containing object" is a hypothetical object
14615 (of some integral or enum type) within which the given bit-field lives. The
14616 type of this hypothetical "containing object" is always the same as the
14617 declared type of the individual bit-field itself (for GCC anyway... the
14618 DWARF spec doesn't actually mandate this). Note that it is the size (in
14619 bytes) of the hypothetical "containing object" which will be given in the
14620 DW_AT_byte_size attribute for this bit-field. (See the
14621 `byte_size_attribute' function below.) It is also used when calculating the
14622 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14623 function below.) */
14625 static void
14626 add_data_member_location_attribute (dw_die_ref die, tree decl)
14628 HOST_WIDE_INT offset;
14629 dw_loc_descr_ref loc_descr = 0;
14631 if (TREE_CODE (decl) == TREE_BINFO)
14633 /* We're working on the TAG_inheritance for a base class. */
14634 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14636 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14637 aren't at a fixed offset from all (sub)objects of the same
14638 type. We need to extract the appropriate offset from our
14639 vtable. The following dwarf expression means
14641 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14643 This is specific to the V3 ABI, of course. */
14645 dw_loc_descr_ref tmp;
14647 /* Make a copy of the object address. */
14648 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14649 add_loc_descr (&loc_descr, tmp);
14651 /* Extract the vtable address. */
14652 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14653 add_loc_descr (&loc_descr, tmp);
14655 /* Calculate the address of the offset. */
14656 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14657 gcc_assert (offset < 0);
14659 tmp = int_loc_descriptor (-offset);
14660 add_loc_descr (&loc_descr, tmp);
14661 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14662 add_loc_descr (&loc_descr, tmp);
14664 /* Extract the offset. */
14665 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14666 add_loc_descr (&loc_descr, tmp);
14668 /* Add it to the object address. */
14669 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14670 add_loc_descr (&loc_descr, tmp);
14672 else
14673 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14675 else
14676 offset = field_byte_offset (decl);
14678 if (! loc_descr)
14680 if (dwarf_version > 2)
14682 /* Don't need to output a location expression, just the constant. */
14683 if (offset < 0)
14684 add_AT_int (die, DW_AT_data_member_location, offset);
14685 else
14686 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14687 return;
14689 else
14691 enum dwarf_location_atom op;
14693 /* The DWARF2 standard says that we should assume that the structure
14694 address is already on the stack, so we can specify a structure
14695 field address by using DW_OP_plus_uconst. */
14696 op = DW_OP_plus_uconst;
14697 loc_descr = new_loc_descr (op, offset, 0);
14701 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14704 /* Writes integer values to dw_vec_const array. */
14706 static void
14707 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14709 while (size != 0)
14711 *dest++ = val & 0xff;
14712 val >>= 8;
14713 --size;
14717 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14719 static HOST_WIDE_INT
14720 extract_int (const unsigned char *src, unsigned int size)
14722 HOST_WIDE_INT val = 0;
14724 src += size;
14725 while (size != 0)
14727 val <<= 8;
14728 val |= *--src & 0xff;
14729 --size;
14731 return val;
14734 /* Writes double_int values to dw_vec_const array. */
14736 static void
14737 insert_double (double_int val, unsigned char *dest)
14739 unsigned char *p0 = dest;
14740 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14742 if (WORDS_BIG_ENDIAN)
14744 p0 = p1;
14745 p1 = dest;
14748 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14749 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14752 /* Writes floating point values to dw_vec_const array. */
14754 static void
14755 insert_float (const_rtx rtl, unsigned char *array)
14757 REAL_VALUE_TYPE rv;
14758 long val[4];
14759 int i;
14761 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14762 real_to_target (val, &rv, GET_MODE (rtl));
14764 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14765 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14767 insert_int (val[i], 4, array);
14768 array += 4;
14772 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14773 does not have a "location" either in memory or in a register. These
14774 things can arise in GNU C when a constant is passed as an actual parameter
14775 to an inlined function. They can also arise in C++ where declared
14776 constants do not necessarily get memory "homes". */
14778 static bool
14779 add_const_value_attribute (dw_die_ref die, rtx rtl)
14781 switch (GET_CODE (rtl))
14783 case CONST_INT:
14785 HOST_WIDE_INT val = INTVAL (rtl);
14787 if (val < 0)
14788 add_AT_int (die, DW_AT_const_value, val);
14789 else
14790 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14792 return true;
14794 case CONST_DOUBLE:
14795 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14796 floating-point constant. A CONST_DOUBLE is used whenever the
14797 constant requires more than one word in order to be adequately
14798 represented. */
14800 enum machine_mode mode = GET_MODE (rtl);
14802 if (SCALAR_FLOAT_MODE_P (mode))
14804 unsigned int length = GET_MODE_SIZE (mode);
14805 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14807 insert_float (rtl, array);
14808 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14810 else
14811 add_AT_double (die, DW_AT_const_value,
14812 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14814 return true;
14816 case CONST_VECTOR:
14818 enum machine_mode mode = GET_MODE (rtl);
14819 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14820 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14821 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14822 (length * elt_size);
14823 unsigned int i;
14824 unsigned char *p;
14826 switch (GET_MODE_CLASS (mode))
14828 case MODE_VECTOR_INT:
14829 for (i = 0, p = array; i < length; i++, p += elt_size)
14831 rtx elt = CONST_VECTOR_ELT (rtl, i);
14832 double_int val = rtx_to_double_int (elt);
14834 if (elt_size <= sizeof (HOST_WIDE_INT))
14835 insert_int (val.to_shwi (), elt_size, p);
14836 else
14838 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14839 insert_double (val, p);
14842 break;
14844 case MODE_VECTOR_FLOAT:
14845 for (i = 0, p = array; i < length; i++, p += elt_size)
14847 rtx elt = CONST_VECTOR_ELT (rtl, i);
14848 insert_float (elt, p);
14850 break;
14852 default:
14853 gcc_unreachable ();
14856 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14858 return true;
14860 case CONST_STRING:
14861 if (dwarf_version >= 4 || !dwarf_strict)
14863 dw_loc_descr_ref loc_result;
14864 resolve_one_addr (&rtl, NULL);
14865 rtl_addr:
14866 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14867 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14868 add_AT_loc (die, DW_AT_location, loc_result);
14869 vec_safe_push (used_rtx_array, rtl);
14870 return true;
14872 return false;
14874 case CONST:
14875 if (CONSTANT_P (XEXP (rtl, 0)))
14876 return add_const_value_attribute (die, XEXP (rtl, 0));
14877 /* FALLTHROUGH */
14878 case SYMBOL_REF:
14879 if (!const_ok_for_output (rtl))
14880 return false;
14881 case LABEL_REF:
14882 if (dwarf_version >= 4 || !dwarf_strict)
14883 goto rtl_addr;
14884 return false;
14886 case PLUS:
14887 /* In cases where an inlined instance of an inline function is passed
14888 the address of an `auto' variable (which is local to the caller) we
14889 can get a situation where the DECL_RTL of the artificial local
14890 variable (for the inlining) which acts as a stand-in for the
14891 corresponding formal parameter (of the inline function) will look
14892 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14893 exactly a compile-time constant expression, but it isn't the address
14894 of the (artificial) local variable either. Rather, it represents the
14895 *value* which the artificial local variable always has during its
14896 lifetime. We currently have no way to represent such quasi-constant
14897 values in Dwarf, so for now we just punt and generate nothing. */
14898 return false;
14900 case HIGH:
14901 case CONST_FIXED:
14902 return false;
14904 case MEM:
14905 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14906 && MEM_READONLY_P (rtl)
14907 && GET_MODE (rtl) == BLKmode)
14909 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14910 return true;
14912 return false;
14914 default:
14915 /* No other kinds of rtx should be possible here. */
14916 gcc_unreachable ();
14918 return false;
14921 /* Determine whether the evaluation of EXPR references any variables
14922 or functions which aren't otherwise used (and therefore may not be
14923 output). */
14924 static tree
14925 reference_to_unused (tree * tp, int * walk_subtrees,
14926 void * data ATTRIBUTE_UNUSED)
14928 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14929 *walk_subtrees = 0;
14931 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14932 && ! TREE_ASM_WRITTEN (*tp))
14933 return *tp;
14934 /* ??? The C++ FE emits debug information for using decls, so
14935 putting gcc_unreachable here falls over. See PR31899. For now
14936 be conservative. */
14937 else if (!cgraph_global_info_ready
14938 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14939 return *tp;
14940 else if (TREE_CODE (*tp) == VAR_DECL)
14942 struct varpool_node *node = varpool_get_node (*tp);
14943 if (!node || !node->symbol.definition)
14944 return *tp;
14946 else if (TREE_CODE (*tp) == FUNCTION_DECL
14947 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14949 /* The call graph machinery must have finished analyzing,
14950 optimizing and gimplifying the CU by now.
14951 So if *TP has no call graph node associated
14952 to it, it means *TP will not be emitted. */
14953 if (!cgraph_get_node (*tp))
14954 return *tp;
14956 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14957 return *tp;
14959 return NULL_TREE;
14962 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14963 for use in a later add_const_value_attribute call. */
14965 static rtx
14966 rtl_for_decl_init (tree init, tree type)
14968 rtx rtl = NULL_RTX;
14970 STRIP_NOPS (init);
14972 /* If a variable is initialized with a string constant without embedded
14973 zeros, build CONST_STRING. */
14974 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14976 tree enttype = TREE_TYPE (type);
14977 tree domain = TYPE_DOMAIN (type);
14978 enum machine_mode mode = TYPE_MODE (enttype);
14980 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14981 && domain
14982 && integer_zerop (TYPE_MIN_VALUE (domain))
14983 && compare_tree_int (TYPE_MAX_VALUE (domain),
14984 TREE_STRING_LENGTH (init) - 1) == 0
14985 && ((size_t) TREE_STRING_LENGTH (init)
14986 == strlen (TREE_STRING_POINTER (init)) + 1))
14988 rtl = gen_rtx_CONST_STRING (VOIDmode,
14989 ggc_strdup (TREE_STRING_POINTER (init)));
14990 rtl = gen_rtx_MEM (BLKmode, rtl);
14991 MEM_READONLY_P (rtl) = 1;
14994 /* Other aggregates, and complex values, could be represented using
14995 CONCAT: FIXME! */
14996 else if (AGGREGATE_TYPE_P (type)
14997 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14998 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14999 || TREE_CODE (type) == COMPLEX_TYPE)
15001 /* Vectors only work if their mode is supported by the target.
15002 FIXME: generic vectors ought to work too. */
15003 else if (TREE_CODE (type) == VECTOR_TYPE
15004 && !VECTOR_MODE_P (TYPE_MODE (type)))
15006 /* If the initializer is something that we know will expand into an
15007 immediate RTL constant, expand it now. We must be careful not to
15008 reference variables which won't be output. */
15009 else if (initializer_constant_valid_p (init, type)
15010 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15012 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15013 possible. */
15014 if (TREE_CODE (type) == VECTOR_TYPE)
15015 switch (TREE_CODE (init))
15017 case VECTOR_CST:
15018 break;
15019 case CONSTRUCTOR:
15020 if (TREE_CONSTANT (init))
15022 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15023 bool constant_p = true;
15024 tree value;
15025 unsigned HOST_WIDE_INT ix;
15027 /* Even when ctor is constant, it might contain non-*_CST
15028 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15029 belong into VECTOR_CST nodes. */
15030 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15031 if (!CONSTANT_CLASS_P (value))
15033 constant_p = false;
15034 break;
15037 if (constant_p)
15039 init = build_vector_from_ctor (type, elts);
15040 break;
15043 /* FALLTHRU */
15045 default:
15046 return NULL;
15049 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15051 /* If expand_expr returns a MEM, it wasn't immediate. */
15052 gcc_assert (!rtl || !MEM_P (rtl));
15055 return rtl;
15058 /* Generate RTL for the variable DECL to represent its location. */
15060 static rtx
15061 rtl_for_decl_location (tree decl)
15063 rtx rtl;
15065 /* Here we have to decide where we are going to say the parameter "lives"
15066 (as far as the debugger is concerned). We only have a couple of
15067 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15069 DECL_RTL normally indicates where the parameter lives during most of the
15070 activation of the function. If optimization is enabled however, this
15071 could be either NULL or else a pseudo-reg. Both of those cases indicate
15072 that the parameter doesn't really live anywhere (as far as the code
15073 generation parts of GCC are concerned) during most of the function's
15074 activation. That will happen (for example) if the parameter is never
15075 referenced within the function.
15077 We could just generate a location descriptor here for all non-NULL
15078 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15079 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15080 where DECL_RTL is NULL or is a pseudo-reg.
15082 Note however that we can only get away with using DECL_INCOMING_RTL as
15083 a backup substitute for DECL_RTL in certain limited cases. In cases
15084 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15085 we can be sure that the parameter was passed using the same type as it is
15086 declared to have within the function, and that its DECL_INCOMING_RTL
15087 points us to a place where a value of that type is passed.
15089 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15090 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15091 because in these cases DECL_INCOMING_RTL points us to a value of some
15092 type which is *different* from the type of the parameter itself. Thus,
15093 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15094 such cases, the debugger would end up (for example) trying to fetch a
15095 `float' from a place which actually contains the first part of a
15096 `double'. That would lead to really incorrect and confusing
15097 output at debug-time.
15099 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15100 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15101 are a couple of exceptions however. On little-endian machines we can
15102 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15103 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15104 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15105 when (on a little-endian machine) a non-prototyped function has a
15106 parameter declared to be of type `short' or `char'. In such cases,
15107 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15108 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15109 passed `int' value. If the debugger then uses that address to fetch
15110 a `short' or a `char' (on a little-endian machine) the result will be
15111 the correct data, so we allow for such exceptional cases below.
15113 Note that our goal here is to describe the place where the given formal
15114 parameter lives during most of the function's activation (i.e. between the
15115 end of the prologue and the start of the epilogue). We'll do that as best
15116 as we can. Note however that if the given formal parameter is modified
15117 sometime during the execution of the function, then a stack backtrace (at
15118 debug-time) will show the function as having been called with the *new*
15119 value rather than the value which was originally passed in. This happens
15120 rarely enough that it is not a major problem, but it *is* a problem, and
15121 I'd like to fix it.
15123 A future version of dwarf2out.c may generate two additional attributes for
15124 any given DW_TAG_formal_parameter DIE which will describe the "passed
15125 type" and the "passed location" for the given formal parameter in addition
15126 to the attributes we now generate to indicate the "declared type" and the
15127 "active location" for each parameter. This additional set of attributes
15128 could be used by debuggers for stack backtraces. Separately, note that
15129 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15130 This happens (for example) for inlined-instances of inline function formal
15131 parameters which are never referenced. This really shouldn't be
15132 happening. All PARM_DECL nodes should get valid non-NULL
15133 DECL_INCOMING_RTL values. FIXME. */
15135 /* Use DECL_RTL as the "location" unless we find something better. */
15136 rtl = DECL_RTL_IF_SET (decl);
15138 /* When generating abstract instances, ignore everything except
15139 constants, symbols living in memory, and symbols living in
15140 fixed registers. */
15141 if (! reload_completed)
15143 if (rtl
15144 && (CONSTANT_P (rtl)
15145 || (MEM_P (rtl)
15146 && CONSTANT_P (XEXP (rtl, 0)))
15147 || (REG_P (rtl)
15148 && TREE_CODE (decl) == VAR_DECL
15149 && TREE_STATIC (decl))))
15151 rtl = targetm.delegitimize_address (rtl);
15152 return rtl;
15154 rtl = NULL_RTX;
15156 else if (TREE_CODE (decl) == PARM_DECL)
15158 if (rtl == NULL_RTX
15159 || is_pseudo_reg (rtl)
15160 || (MEM_P (rtl)
15161 && is_pseudo_reg (XEXP (rtl, 0))
15162 && DECL_INCOMING_RTL (decl)
15163 && MEM_P (DECL_INCOMING_RTL (decl))
15164 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15166 tree declared_type = TREE_TYPE (decl);
15167 tree passed_type = DECL_ARG_TYPE (decl);
15168 enum machine_mode dmode = TYPE_MODE (declared_type);
15169 enum machine_mode pmode = TYPE_MODE (passed_type);
15171 /* This decl represents a formal parameter which was optimized out.
15172 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15173 all cases where (rtl == NULL_RTX) just below. */
15174 if (dmode == pmode)
15175 rtl = DECL_INCOMING_RTL (decl);
15176 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15177 && SCALAR_INT_MODE_P (dmode)
15178 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15179 && DECL_INCOMING_RTL (decl))
15181 rtx inc = DECL_INCOMING_RTL (decl);
15182 if (REG_P (inc))
15183 rtl = inc;
15184 else if (MEM_P (inc))
15186 if (BYTES_BIG_ENDIAN)
15187 rtl = adjust_address_nv (inc, dmode,
15188 GET_MODE_SIZE (pmode)
15189 - GET_MODE_SIZE (dmode));
15190 else
15191 rtl = inc;
15196 /* If the parm was passed in registers, but lives on the stack, then
15197 make a big endian correction if the mode of the type of the
15198 parameter is not the same as the mode of the rtl. */
15199 /* ??? This is the same series of checks that are made in dbxout.c before
15200 we reach the big endian correction code there. It isn't clear if all
15201 of these checks are necessary here, but keeping them all is the safe
15202 thing to do. */
15203 else if (MEM_P (rtl)
15204 && XEXP (rtl, 0) != const0_rtx
15205 && ! CONSTANT_P (XEXP (rtl, 0))
15206 /* Not passed in memory. */
15207 && !MEM_P (DECL_INCOMING_RTL (decl))
15208 /* Not passed by invisible reference. */
15209 && (!REG_P (XEXP (rtl, 0))
15210 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15211 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15212 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15213 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15214 #endif
15216 /* Big endian correction check. */
15217 && BYTES_BIG_ENDIAN
15218 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15219 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15220 < UNITS_PER_WORD))
15222 enum machine_mode addr_mode = get_address_mode (rtl);
15223 int offset = (UNITS_PER_WORD
15224 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15226 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15227 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15230 else if (TREE_CODE (decl) == VAR_DECL
15231 && rtl
15232 && MEM_P (rtl)
15233 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15234 && BYTES_BIG_ENDIAN)
15236 enum machine_mode addr_mode = get_address_mode (rtl);
15237 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15238 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15240 /* If a variable is declared "register" yet is smaller than
15241 a register, then if we store the variable to memory, it
15242 looks like we're storing a register-sized value, when in
15243 fact we are not. We need to adjust the offset of the
15244 storage location to reflect the actual value's bytes,
15245 else gdb will not be able to display it. */
15246 if (rsize > dsize)
15247 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15248 plus_constant (addr_mode, XEXP (rtl, 0),
15249 rsize - dsize));
15252 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15253 and will have been substituted directly into all expressions that use it.
15254 C does not have such a concept, but C++ and other languages do. */
15255 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15256 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15258 if (rtl)
15259 rtl = targetm.delegitimize_address (rtl);
15261 /* If we don't look past the constant pool, we risk emitting a
15262 reference to a constant pool entry that isn't referenced from
15263 code, and thus is not emitted. */
15264 if (rtl)
15265 rtl = avoid_constant_pool_reference (rtl);
15267 /* Try harder to get a rtl. If this symbol ends up not being emitted
15268 in the current CU, resolve_addr will remove the expression referencing
15269 it. */
15270 if (rtl == NULL_RTX
15271 && TREE_CODE (decl) == VAR_DECL
15272 && !DECL_EXTERNAL (decl)
15273 && TREE_STATIC (decl)
15274 && DECL_NAME (decl)
15275 && !DECL_HARD_REGISTER (decl)
15276 && DECL_MODE (decl) != VOIDmode)
15278 rtl = make_decl_rtl_for_debug (decl);
15279 if (!MEM_P (rtl)
15280 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15281 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15282 rtl = NULL_RTX;
15285 return rtl;
15288 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15289 returned. If so, the decl for the COMMON block is returned, and the
15290 value is the offset into the common block for the symbol. */
15292 static tree
15293 fortran_common (tree decl, HOST_WIDE_INT *value)
15295 tree val_expr, cvar;
15296 enum machine_mode mode;
15297 HOST_WIDE_INT bitsize, bitpos;
15298 tree offset;
15299 int unsignedp, volatilep = 0;
15301 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15302 it does not have a value (the offset into the common area), or if it
15303 is thread local (as opposed to global) then it isn't common, and shouldn't
15304 be handled as such. */
15305 if (TREE_CODE (decl) != VAR_DECL
15306 || !TREE_STATIC (decl)
15307 || !DECL_HAS_VALUE_EXPR_P (decl)
15308 || !is_fortran ())
15309 return NULL_TREE;
15311 val_expr = DECL_VALUE_EXPR (decl);
15312 if (TREE_CODE (val_expr) != COMPONENT_REF)
15313 return NULL_TREE;
15315 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15316 &mode, &unsignedp, &volatilep, true);
15318 if (cvar == NULL_TREE
15319 || TREE_CODE (cvar) != VAR_DECL
15320 || DECL_ARTIFICIAL (cvar)
15321 || !TREE_PUBLIC (cvar))
15322 return NULL_TREE;
15324 *value = 0;
15325 if (offset != NULL)
15327 if (!host_integerp (offset, 0))
15328 return NULL_TREE;
15329 *value = tree_low_cst (offset, 0);
15331 if (bitpos != 0)
15332 *value += bitpos / BITS_PER_UNIT;
15334 return cvar;
15337 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15338 data attribute for a variable or a parameter. We generate the
15339 DW_AT_const_value attribute only in those cases where the given variable
15340 or parameter does not have a true "location" either in memory or in a
15341 register. This can happen (for example) when a constant is passed as an
15342 actual argument in a call to an inline function. (It's possible that
15343 these things can crop up in other ways also.) Note that one type of
15344 constant value which can be passed into an inlined function is a constant
15345 pointer. This can happen for example if an actual argument in an inlined
15346 function call evaluates to a compile-time constant address.
15348 CACHE_P is true if it is worth caching the location list for DECL,
15349 so that future calls can reuse it rather than regenerate it from scratch.
15350 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15351 since we will need to refer to them each time the function is inlined. */
15353 static bool
15354 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15355 enum dwarf_attribute attr)
15357 rtx rtl;
15358 dw_loc_list_ref list;
15359 var_loc_list *loc_list;
15360 cached_dw_loc_list *cache;
15361 void **slot;
15363 if (TREE_CODE (decl) == ERROR_MARK)
15364 return false;
15366 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15367 || TREE_CODE (decl) == RESULT_DECL);
15369 /* Try to get some constant RTL for this decl, and use that as the value of
15370 the location. */
15372 rtl = rtl_for_decl_location (decl);
15373 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15374 && add_const_value_attribute (die, rtl))
15375 return true;
15377 /* See if we have single element location list that is equivalent to
15378 a constant value. That way we are better to use add_const_value_attribute
15379 rather than expanding constant value equivalent. */
15380 loc_list = lookup_decl_loc (decl);
15381 if (loc_list
15382 && loc_list->first
15383 && loc_list->first->next == NULL
15384 && NOTE_P (loc_list->first->loc)
15385 && NOTE_VAR_LOCATION (loc_list->first->loc)
15386 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15388 struct var_loc_node *node;
15390 node = loc_list->first;
15391 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15392 if (GET_CODE (rtl) == EXPR_LIST)
15393 rtl = XEXP (rtl, 0);
15394 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15395 && add_const_value_attribute (die, rtl))
15396 return true;
15398 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15399 list several times. See if we've already cached the contents. */
15400 list = NULL;
15401 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15402 cache_p = false;
15403 if (cache_p)
15405 cache = (cached_dw_loc_list *)
15406 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15407 if (cache)
15408 list = cache->loc_list;
15410 if (list == NULL)
15412 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15413 /* It is usually worth caching this result if the decl is from
15414 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15415 if (cache_p && list && list->dw_loc_next)
15417 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15418 DECL_UID (decl), INSERT);
15419 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15420 cache->decl_id = DECL_UID (decl);
15421 cache->loc_list = list;
15422 *slot = cache;
15425 if (list)
15427 add_AT_location_description (die, attr, list);
15428 return true;
15430 /* None of that worked, so it must not really have a location;
15431 try adding a constant value attribute from the DECL_INITIAL. */
15432 return tree_add_const_value_attribute_for_decl (die, decl);
15435 /* Add VARIABLE and DIE into deferred locations list. */
15437 static void
15438 defer_location (tree variable, dw_die_ref die)
15440 deferred_locations entry;
15441 entry.variable = variable;
15442 entry.die = die;
15443 vec_safe_push (deferred_locations_list, entry);
15446 /* Helper function for tree_add_const_value_attribute. Natively encode
15447 initializer INIT into an array. Return true if successful. */
15449 static bool
15450 native_encode_initializer (tree init, unsigned char *array, int size)
15452 tree type;
15454 if (init == NULL_TREE)
15455 return false;
15457 STRIP_NOPS (init);
15458 switch (TREE_CODE (init))
15460 case STRING_CST:
15461 type = TREE_TYPE (init);
15462 if (TREE_CODE (type) == ARRAY_TYPE)
15464 tree enttype = TREE_TYPE (type);
15465 enum machine_mode mode = TYPE_MODE (enttype);
15467 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15468 return false;
15469 if (int_size_in_bytes (type) != size)
15470 return false;
15471 if (size > TREE_STRING_LENGTH (init))
15473 memcpy (array, TREE_STRING_POINTER (init),
15474 TREE_STRING_LENGTH (init));
15475 memset (array + TREE_STRING_LENGTH (init),
15476 '\0', size - TREE_STRING_LENGTH (init));
15478 else
15479 memcpy (array, TREE_STRING_POINTER (init), size);
15480 return true;
15482 return false;
15483 case CONSTRUCTOR:
15484 type = TREE_TYPE (init);
15485 if (int_size_in_bytes (type) != size)
15486 return false;
15487 if (TREE_CODE (type) == ARRAY_TYPE)
15489 HOST_WIDE_INT min_index;
15490 unsigned HOST_WIDE_INT cnt;
15491 int curpos = 0, fieldsize;
15492 constructor_elt *ce;
15494 if (TYPE_DOMAIN (type) == NULL_TREE
15495 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15496 return false;
15498 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15499 if (fieldsize <= 0)
15500 return false;
15502 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15503 memset (array, '\0', size);
15504 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15506 tree val = ce->value;
15507 tree index = ce->index;
15508 int pos = curpos;
15509 if (index && TREE_CODE (index) == RANGE_EXPR)
15510 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15511 * fieldsize;
15512 else if (index)
15513 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15515 if (val)
15517 STRIP_NOPS (val);
15518 if (!native_encode_initializer (val, array + pos, fieldsize))
15519 return false;
15521 curpos = pos + fieldsize;
15522 if (index && TREE_CODE (index) == RANGE_EXPR)
15524 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15525 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15526 while (count-- > 0)
15528 if (val)
15529 memcpy (array + curpos, array + pos, fieldsize);
15530 curpos += fieldsize;
15533 gcc_assert (curpos <= size);
15535 return true;
15537 else if (TREE_CODE (type) == RECORD_TYPE
15538 || TREE_CODE (type) == UNION_TYPE)
15540 tree field = NULL_TREE;
15541 unsigned HOST_WIDE_INT cnt;
15542 constructor_elt *ce;
15544 if (int_size_in_bytes (type) != size)
15545 return false;
15547 if (TREE_CODE (type) == RECORD_TYPE)
15548 field = TYPE_FIELDS (type);
15550 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15552 tree val = ce->value;
15553 int pos, fieldsize;
15555 if (ce->index != 0)
15556 field = ce->index;
15558 if (val)
15559 STRIP_NOPS (val);
15561 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15562 return false;
15564 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15565 && TYPE_DOMAIN (TREE_TYPE (field))
15566 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15567 return false;
15568 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15569 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15570 return false;
15571 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15572 pos = int_byte_position (field);
15573 gcc_assert (pos + fieldsize <= size);
15574 if (val
15575 && !native_encode_initializer (val, array + pos, fieldsize))
15576 return false;
15578 return true;
15580 return false;
15581 case VIEW_CONVERT_EXPR:
15582 case NON_LVALUE_EXPR:
15583 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15584 default:
15585 return native_encode_expr (init, array, size) == size;
15589 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15590 attribute is the const value T. */
15592 static bool
15593 tree_add_const_value_attribute (dw_die_ref die, tree t)
15595 tree init;
15596 tree type = TREE_TYPE (t);
15597 rtx rtl;
15599 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15600 return false;
15602 init = t;
15603 gcc_assert (!DECL_P (init));
15605 rtl = rtl_for_decl_init (init, type);
15606 if (rtl)
15607 return add_const_value_attribute (die, rtl);
15608 /* If the host and target are sane, try harder. */
15609 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15610 && initializer_constant_valid_p (init, type))
15612 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15613 if (size > 0 && (int) size == size)
15615 unsigned char *array = (unsigned char *)
15616 ggc_alloc_cleared_atomic (size);
15618 if (native_encode_initializer (init, array, size))
15620 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15621 return true;
15623 ggc_free (array);
15626 return false;
15629 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15630 attribute is the const value of T, where T is an integral constant
15631 variable with static storage duration
15632 (so it can't be a PARM_DECL or a RESULT_DECL). */
15634 static bool
15635 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15638 if (!decl
15639 || (TREE_CODE (decl) != VAR_DECL
15640 && TREE_CODE (decl) != CONST_DECL)
15641 || (TREE_CODE (decl) == VAR_DECL
15642 && !TREE_STATIC (decl)))
15643 return false;
15645 if (TREE_READONLY (decl)
15646 && ! TREE_THIS_VOLATILE (decl)
15647 && DECL_INITIAL (decl))
15648 /* OK */;
15649 else
15650 return false;
15652 /* Don't add DW_AT_const_value if abstract origin already has one. */
15653 if (get_AT (var_die, DW_AT_const_value))
15654 return false;
15656 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15659 /* Convert the CFI instructions for the current function into a
15660 location list. This is used for DW_AT_frame_base when we targeting
15661 a dwarf2 consumer that does not support the dwarf3
15662 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15663 expressions. */
15665 static dw_loc_list_ref
15666 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15668 int ix;
15669 dw_fde_ref fde;
15670 dw_loc_list_ref list, *list_tail;
15671 dw_cfi_ref cfi;
15672 dw_cfa_location last_cfa, next_cfa;
15673 const char *start_label, *last_label, *section;
15674 dw_cfa_location remember;
15676 fde = cfun->fde;
15677 gcc_assert (fde != NULL);
15679 section = secname_for_decl (current_function_decl);
15680 list_tail = &list;
15681 list = NULL;
15683 memset (&next_cfa, 0, sizeof (next_cfa));
15684 next_cfa.reg = INVALID_REGNUM;
15685 remember = next_cfa;
15687 start_label = fde->dw_fde_begin;
15689 /* ??? Bald assumption that the CIE opcode list does not contain
15690 advance opcodes. */
15691 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15692 lookup_cfa_1 (cfi, &next_cfa, &remember);
15694 last_cfa = next_cfa;
15695 last_label = start_label;
15697 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15699 /* If the first partition contained no CFI adjustments, the
15700 CIE opcodes apply to the whole first partition. */
15701 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15702 fde->dw_fde_begin, fde->dw_fde_end, section);
15703 list_tail =&(*list_tail)->dw_loc_next;
15704 start_label = last_label = fde->dw_fde_second_begin;
15707 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15709 switch (cfi->dw_cfi_opc)
15711 case DW_CFA_set_loc:
15712 case DW_CFA_advance_loc1:
15713 case DW_CFA_advance_loc2:
15714 case DW_CFA_advance_loc4:
15715 if (!cfa_equal_p (&last_cfa, &next_cfa))
15717 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15718 start_label, last_label, section);
15720 list_tail = &(*list_tail)->dw_loc_next;
15721 last_cfa = next_cfa;
15722 start_label = last_label;
15724 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15725 break;
15727 case DW_CFA_advance_loc:
15728 /* The encoding is complex enough that we should never emit this. */
15729 gcc_unreachable ();
15731 default:
15732 lookup_cfa_1 (cfi, &next_cfa, &remember);
15733 break;
15735 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15737 if (!cfa_equal_p (&last_cfa, &next_cfa))
15739 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15740 start_label, last_label, section);
15742 list_tail = &(*list_tail)->dw_loc_next;
15743 last_cfa = next_cfa;
15744 start_label = last_label;
15746 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15747 start_label, fde->dw_fde_end, section);
15748 list_tail = &(*list_tail)->dw_loc_next;
15749 start_label = last_label = fde->dw_fde_second_begin;
15753 if (!cfa_equal_p (&last_cfa, &next_cfa))
15755 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15756 start_label, last_label, section);
15757 list_tail = &(*list_tail)->dw_loc_next;
15758 start_label = last_label;
15761 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15762 start_label,
15763 fde->dw_fde_second_begin
15764 ? fde->dw_fde_second_end : fde->dw_fde_end,
15765 section);
15767 if (list && list->dw_loc_next)
15768 gen_llsym (list);
15770 return list;
15773 /* Compute a displacement from the "steady-state frame pointer" to the
15774 frame base (often the same as the CFA), and store it in
15775 frame_pointer_fb_offset. OFFSET is added to the displacement
15776 before the latter is negated. */
15778 static void
15779 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15781 rtx reg, elim;
15783 #ifdef FRAME_POINTER_CFA_OFFSET
15784 reg = frame_pointer_rtx;
15785 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15786 #else
15787 reg = arg_pointer_rtx;
15788 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15789 #endif
15791 elim = (ira_use_lra_p
15792 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15793 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15794 if (GET_CODE (elim) == PLUS)
15796 offset += INTVAL (XEXP (elim, 1));
15797 elim = XEXP (elim, 0);
15800 frame_pointer_fb_offset = -offset;
15802 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15803 in which to eliminate. This is because it's stack pointer isn't
15804 directly accessible as a register within the ISA. To work around
15805 this, assume that while we cannot provide a proper value for
15806 frame_pointer_fb_offset, we won't need one either. */
15807 frame_pointer_fb_offset_valid
15808 = ((SUPPORTS_STACK_ALIGNMENT
15809 && (elim == hard_frame_pointer_rtx
15810 || elim == stack_pointer_rtx))
15811 || elim == (frame_pointer_needed
15812 ? hard_frame_pointer_rtx
15813 : stack_pointer_rtx));
15816 /* Generate a DW_AT_name attribute given some string value to be included as
15817 the value of the attribute. */
15819 static void
15820 add_name_attribute (dw_die_ref die, const char *name_string)
15822 if (name_string != NULL && *name_string != 0)
15824 if (demangle_name_func)
15825 name_string = (*demangle_name_func) (name_string);
15827 add_AT_string (die, DW_AT_name, name_string);
15831 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15832 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15833 of TYPE accordingly.
15835 ??? This is a temporary measure until after we're able to generate
15836 regular DWARF for the complex Ada type system. */
15838 static void
15839 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15840 dw_die_ref context_die)
15842 tree dtype;
15843 dw_die_ref dtype_die;
15845 if (!lang_hooks.types.descriptive_type)
15846 return;
15848 dtype = lang_hooks.types.descriptive_type (type);
15849 if (!dtype)
15850 return;
15852 dtype_die = lookup_type_die (dtype);
15853 if (!dtype_die)
15855 gen_type_die (dtype, context_die);
15856 dtype_die = lookup_type_die (dtype);
15857 gcc_assert (dtype_die);
15860 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15863 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
15865 static const char *
15866 comp_dir_string (void)
15868 const char *wd;
15869 char *wd1;
15870 static const char *cached_wd = NULL;
15872 if (cached_wd != NULL)
15873 return cached_wd;
15875 wd = get_src_pwd ();
15876 if (wd == NULL)
15877 return NULL;
15879 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15881 int wdlen;
15883 wdlen = strlen (wd);
15884 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15885 strcpy (wd1, wd);
15886 wd1 [wdlen] = DIR_SEPARATOR;
15887 wd1 [wdlen + 1] = 0;
15888 wd = wd1;
15891 cached_wd = remap_debug_filename (wd);
15892 return cached_wd;
15895 /* Generate a DW_AT_comp_dir attribute for DIE. */
15897 static void
15898 add_comp_dir_attribute (dw_die_ref die)
15900 const char * wd = comp_dir_string ();
15901 if (wd != NULL)
15902 add_AT_string (die, DW_AT_comp_dir, wd);
15905 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15906 default. */
15908 static int
15909 lower_bound_default (void)
15911 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15913 case DW_LANG_C:
15914 case DW_LANG_C89:
15915 case DW_LANG_C99:
15916 case DW_LANG_C_plus_plus:
15917 case DW_LANG_ObjC:
15918 case DW_LANG_ObjC_plus_plus:
15919 case DW_LANG_Java:
15920 return 0;
15921 case DW_LANG_Fortran77:
15922 case DW_LANG_Fortran90:
15923 case DW_LANG_Fortran95:
15924 return 1;
15925 case DW_LANG_UPC:
15926 case DW_LANG_D:
15927 case DW_LANG_Python:
15928 return dwarf_version >= 4 ? 0 : -1;
15929 case DW_LANG_Ada95:
15930 case DW_LANG_Ada83:
15931 case DW_LANG_Cobol74:
15932 case DW_LANG_Cobol85:
15933 case DW_LANG_Pascal83:
15934 case DW_LANG_Modula2:
15935 case DW_LANG_PLI:
15936 return dwarf_version >= 4 ? 1 : -1;
15937 default:
15938 return -1;
15942 /* Given a tree node describing an array bound (either lower or upper) output
15943 a representation for that bound. */
15945 static void
15946 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15948 switch (TREE_CODE (bound))
15950 case ERROR_MARK:
15951 return;
15953 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15954 case INTEGER_CST:
15956 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15957 int dflt;
15959 /* Use the default if possible. */
15960 if (bound_attr == DW_AT_lower_bound
15961 && host_integerp (bound, 0)
15962 && (dflt = lower_bound_default ()) != -1
15963 && tree_low_cst (bound, 0) == dflt)
15966 /* Otherwise represent the bound as an unsigned value with the
15967 precision of its type. The precision and signedness of the
15968 type will be necessary to re-interpret it unambiguously. */
15969 else if (prec < HOST_BITS_PER_WIDE_INT)
15971 unsigned HOST_WIDE_INT mask
15972 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15973 add_AT_unsigned (subrange_die, bound_attr,
15974 TREE_INT_CST_LOW (bound) & mask);
15976 else if (prec == HOST_BITS_PER_WIDE_INT
15977 || TREE_INT_CST_HIGH (bound) == 0)
15978 add_AT_unsigned (subrange_die, bound_attr,
15979 TREE_INT_CST_LOW (bound));
15980 else
15981 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15982 TREE_INT_CST_LOW (bound));
15984 break;
15986 CASE_CONVERT:
15987 case VIEW_CONVERT_EXPR:
15988 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15989 break;
15991 case SAVE_EXPR:
15992 break;
15994 case VAR_DECL:
15995 case PARM_DECL:
15996 case RESULT_DECL:
15998 dw_die_ref decl_die = lookup_decl_die (bound);
16000 /* ??? Can this happen, or should the variable have been bound
16001 first? Probably it can, since I imagine that we try to create
16002 the types of parameters in the order in which they exist in
16003 the list, and won't have created a forward reference to a
16004 later parameter. */
16005 if (decl_die != NULL)
16007 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16008 break;
16011 /* FALLTHRU */
16013 default:
16015 /* Otherwise try to create a stack operation procedure to
16016 evaluate the value of the array bound. */
16018 dw_die_ref ctx, decl_die;
16019 dw_loc_list_ref list;
16021 list = loc_list_from_tree (bound, 2);
16022 if (list == NULL || single_element_loc_list_p (list))
16024 /* If DW_AT_*bound is not a reference nor constant, it is
16025 a DWARF expression rather than location description.
16026 For that loc_list_from_tree (bound, 0) is needed.
16027 If that fails to give a single element list,
16028 fall back to outputting this as a reference anyway. */
16029 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16030 if (list2 && single_element_loc_list_p (list2))
16032 add_AT_loc (subrange_die, bound_attr, list2->expr);
16033 break;
16036 if (list == NULL)
16037 break;
16039 if (current_function_decl == 0)
16040 ctx = comp_unit_die ();
16041 else
16042 ctx = lookup_decl_die (current_function_decl);
16044 decl_die = new_die (DW_TAG_variable, ctx, bound);
16045 add_AT_flag (decl_die, DW_AT_artificial, 1);
16046 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16047 add_AT_location_description (decl_die, DW_AT_location, list);
16048 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16049 break;
16054 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16055 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16056 Note that the block of subscript information for an array type also
16057 includes information about the element type of the given array type. */
16059 static void
16060 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16062 unsigned dimension_number;
16063 tree lower, upper;
16064 dw_die_ref subrange_die;
16066 for (dimension_number = 0;
16067 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16068 type = TREE_TYPE (type), dimension_number++)
16070 tree domain = TYPE_DOMAIN (type);
16072 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16073 break;
16075 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16076 and (in GNU C only) variable bounds. Handle all three forms
16077 here. */
16078 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16079 if (domain)
16081 /* We have an array type with specified bounds. */
16082 lower = TYPE_MIN_VALUE (domain);
16083 upper = TYPE_MAX_VALUE (domain);
16085 /* Define the index type. */
16086 if (TREE_TYPE (domain))
16088 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16089 TREE_TYPE field. We can't emit debug info for this
16090 because it is an unnamed integral type. */
16091 if (TREE_CODE (domain) == INTEGER_TYPE
16092 && TYPE_NAME (domain) == NULL_TREE
16093 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16094 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16096 else
16097 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16098 type_die);
16101 /* ??? If upper is NULL, the array has unspecified length,
16102 but it does have a lower bound. This happens with Fortran
16103 dimension arr(N:*)
16104 Since the debugger is definitely going to need to know N
16105 to produce useful results, go ahead and output the lower
16106 bound solo, and hope the debugger can cope. */
16108 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16109 if (upper)
16110 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16113 /* Otherwise we have an array type with an unspecified length. The
16114 DWARF-2 spec does not say how to handle this; let's just leave out the
16115 bounds. */
16119 static void
16120 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16122 dw_die_ref decl_die;
16123 unsigned size;
16125 switch (TREE_CODE (tree_node))
16127 case ERROR_MARK:
16128 size = 0;
16129 break;
16130 case ENUMERAL_TYPE:
16131 case RECORD_TYPE:
16132 case UNION_TYPE:
16133 case QUAL_UNION_TYPE:
16134 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16135 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16137 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16138 return;
16140 size = int_size_in_bytes (tree_node);
16141 break;
16142 case FIELD_DECL:
16143 /* For a data member of a struct or union, the DW_AT_byte_size is
16144 generally given as the number of bytes normally allocated for an
16145 object of the *declared* type of the member itself. This is true
16146 even for bit-fields. */
16147 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16148 break;
16149 default:
16150 gcc_unreachable ();
16153 /* Note that `size' might be -1 when we get to this point. If it is, that
16154 indicates that the byte size of the entity in question is variable. We
16155 have no good way of expressing this fact in Dwarf at the present time,
16156 so just let the -1 pass on through. */
16157 add_AT_unsigned (die, DW_AT_byte_size, size);
16160 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16161 which specifies the distance in bits from the highest order bit of the
16162 "containing object" for the bit-field to the highest order bit of the
16163 bit-field itself.
16165 For any given bit-field, the "containing object" is a hypothetical object
16166 (of some integral or enum type) within which the given bit-field lives. The
16167 type of this hypothetical "containing object" is always the same as the
16168 declared type of the individual bit-field itself. The determination of the
16169 exact location of the "containing object" for a bit-field is rather
16170 complicated. It's handled by the `field_byte_offset' function (above).
16172 Note that it is the size (in bytes) of the hypothetical "containing object"
16173 which will be given in the DW_AT_byte_size attribute for this bit-field.
16174 (See `byte_size_attribute' above). */
16176 static inline void
16177 add_bit_offset_attribute (dw_die_ref die, tree decl)
16179 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16180 tree type = DECL_BIT_FIELD_TYPE (decl);
16181 HOST_WIDE_INT bitpos_int;
16182 HOST_WIDE_INT highest_order_object_bit_offset;
16183 HOST_WIDE_INT highest_order_field_bit_offset;
16184 HOST_WIDE_INT bit_offset;
16186 /* Must be a field and a bit field. */
16187 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16189 /* We can't yet handle bit-fields whose offsets are variable, so if we
16190 encounter such things, just return without generating any attribute
16191 whatsoever. Likewise for variable or too large size. */
16192 if (! host_integerp (bit_position (decl), 0)
16193 || ! host_integerp (DECL_SIZE (decl), 1))
16194 return;
16196 bitpos_int = int_bit_position (decl);
16198 /* Note that the bit offset is always the distance (in bits) from the
16199 highest-order bit of the "containing object" to the highest-order bit of
16200 the bit-field itself. Since the "high-order end" of any object or field
16201 is different on big-endian and little-endian machines, the computation
16202 below must take account of these differences. */
16203 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16204 highest_order_field_bit_offset = bitpos_int;
16206 if (! BYTES_BIG_ENDIAN)
16208 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16209 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16212 bit_offset
16213 = (! BYTES_BIG_ENDIAN
16214 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16215 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16217 if (bit_offset < 0)
16218 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16219 else
16220 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16223 /* For a FIELD_DECL node which represents a bit field, output an attribute
16224 which specifies the length in bits of the given field. */
16226 static inline void
16227 add_bit_size_attribute (dw_die_ref die, tree decl)
16229 /* Must be a field and a bit field. */
16230 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16231 && DECL_BIT_FIELD_TYPE (decl));
16233 if (host_integerp (DECL_SIZE (decl), 1))
16234 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16237 /* If the compiled language is ANSI C, then add a 'prototyped'
16238 attribute, if arg types are given for the parameters of a function. */
16240 static inline void
16241 add_prototyped_attribute (dw_die_ref die, tree func_type)
16243 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16244 && prototype_p (func_type))
16245 add_AT_flag (die, DW_AT_prototyped, 1);
16248 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16249 by looking in either the type declaration or object declaration
16250 equate table. */
16252 static inline dw_die_ref
16253 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16255 dw_die_ref origin_die = NULL;
16257 if (TREE_CODE (origin) != FUNCTION_DECL)
16259 /* We may have gotten separated from the block for the inlined
16260 function, if we're in an exception handler or some such; make
16261 sure that the abstract function has been written out.
16263 Doing this for nested functions is wrong, however; functions are
16264 distinct units, and our context might not even be inline. */
16265 tree fn = origin;
16267 if (TYPE_P (fn))
16268 fn = TYPE_STUB_DECL (fn);
16270 fn = decl_function_context (fn);
16271 if (fn)
16272 dwarf2out_abstract_function (fn);
16275 if (DECL_P (origin))
16276 origin_die = lookup_decl_die (origin);
16277 else if (TYPE_P (origin))
16278 origin_die = lookup_type_die (origin);
16280 /* XXX: Functions that are never lowered don't always have correct block
16281 trees (in the case of java, they simply have no block tree, in some other
16282 languages). For these functions, there is nothing we can really do to
16283 output correct debug info for inlined functions in all cases. Rather
16284 than die, we'll just produce deficient debug info now, in that we will
16285 have variables without a proper abstract origin. In the future, when all
16286 functions are lowered, we should re-add a gcc_assert (origin_die)
16287 here. */
16289 if (origin_die)
16290 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16291 return origin_die;
16294 /* We do not currently support the pure_virtual attribute. */
16296 static inline void
16297 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16299 if (DECL_VINDEX (func_decl))
16301 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16303 if (host_integerp (DECL_VINDEX (func_decl), 0))
16304 add_AT_loc (die, DW_AT_vtable_elem_location,
16305 new_loc_descr (DW_OP_constu,
16306 tree_low_cst (DECL_VINDEX (func_decl), 0),
16307 0));
16309 /* GNU extension: Record what type this method came from originally. */
16310 if (debug_info_level > DINFO_LEVEL_TERSE
16311 && DECL_CONTEXT (func_decl))
16312 add_AT_die_ref (die, DW_AT_containing_type,
16313 lookup_type_die (DECL_CONTEXT (func_decl)));
16317 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16318 given decl. This used to be a vendor extension until after DWARF 4
16319 standardized it. */
16321 static void
16322 add_linkage_attr (dw_die_ref die, tree decl)
16324 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16326 /* Mimic what assemble_name_raw does with a leading '*'. */
16327 if (name[0] == '*')
16328 name = &name[1];
16330 if (dwarf_version >= 4)
16331 add_AT_string (die, DW_AT_linkage_name, name);
16332 else
16333 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16336 /* Add source coordinate attributes for the given decl. */
16338 static void
16339 add_src_coords_attributes (dw_die_ref die, tree decl)
16341 expanded_location s;
16343 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16344 return;
16345 s = expand_location (DECL_SOURCE_LOCATION (decl));
16346 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16347 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16350 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16352 static void
16353 add_linkage_name (dw_die_ref die, tree decl)
16355 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16356 && TREE_PUBLIC (decl)
16357 && !DECL_ABSTRACT (decl)
16358 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16359 && die->die_tag != DW_TAG_member)
16361 /* Defer until we have an assembler name set. */
16362 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16364 limbo_die_node *asm_name;
16366 asm_name = ggc_alloc_cleared_limbo_die_node ();
16367 asm_name->die = die;
16368 asm_name->created_for = decl;
16369 asm_name->next = deferred_asm_name;
16370 deferred_asm_name = asm_name;
16372 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16373 add_linkage_attr (die, decl);
16377 /* Add a DW_AT_name attribute and source coordinate attribute for the
16378 given decl, but only if it actually has a name. */
16380 static void
16381 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16383 tree decl_name;
16385 decl_name = DECL_NAME (decl);
16386 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16388 const char *name = dwarf2_name (decl, 0);
16389 if (name)
16390 add_name_attribute (die, name);
16391 if (! DECL_ARTIFICIAL (decl))
16392 add_src_coords_attributes (die, decl);
16394 add_linkage_name (die, decl);
16397 #ifdef VMS_DEBUGGING_INFO
16398 /* Get the function's name, as described by its RTL. This may be different
16399 from the DECL_NAME name used in the source file. */
16400 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16402 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16403 XEXP (DECL_RTL (decl), 0), false);
16404 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16406 #endif /* VMS_DEBUGGING_INFO */
16409 #ifdef VMS_DEBUGGING_INFO
16410 /* Output the debug main pointer die for VMS */
16412 void
16413 dwarf2out_vms_debug_main_pointer (void)
16415 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16416 dw_die_ref die;
16418 /* Allocate the VMS debug main subprogram die. */
16419 die = ggc_alloc_cleared_die_node ();
16420 die->die_tag = DW_TAG_subprogram;
16421 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16422 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16423 current_function_funcdef_no);
16424 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16426 /* Make it the first child of comp_unit_die (). */
16427 die->die_parent = comp_unit_die ();
16428 if (comp_unit_die ()->die_child)
16430 die->die_sib = comp_unit_die ()->die_child->die_sib;
16431 comp_unit_die ()->die_child->die_sib = die;
16433 else
16435 die->die_sib = die;
16436 comp_unit_die ()->die_child = die;
16439 #endif /* VMS_DEBUGGING_INFO */
16441 /* Push a new declaration scope. */
16443 static void
16444 push_decl_scope (tree scope)
16446 vec_safe_push (decl_scope_table, scope);
16449 /* Pop a declaration scope. */
16451 static inline void
16452 pop_decl_scope (void)
16454 decl_scope_table->pop ();
16457 /* walk_tree helper function for uses_local_type, below. */
16459 static tree
16460 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16462 if (!TYPE_P (*tp))
16463 *walk_subtrees = 0;
16464 else
16466 tree name = TYPE_NAME (*tp);
16467 if (name && DECL_P (name) && decl_function_context (name))
16468 return *tp;
16470 return NULL_TREE;
16473 /* If TYPE involves a function-local type (including a local typedef to a
16474 non-local type), returns that type; otherwise returns NULL_TREE. */
16476 static tree
16477 uses_local_type (tree type)
16479 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16480 return used;
16483 /* Return the DIE for the scope that immediately contains this type.
16484 Non-named types that do not involve a function-local type get global
16485 scope. Named types nested in namespaces or other types get their
16486 containing scope. All other types (i.e. function-local named types) get
16487 the current active scope. */
16489 static dw_die_ref
16490 scope_die_for (tree t, dw_die_ref context_die)
16492 dw_die_ref scope_die = NULL;
16493 tree containing_scope;
16495 /* Non-types always go in the current scope. */
16496 gcc_assert (TYPE_P (t));
16498 /* Use the scope of the typedef, rather than the scope of the type
16499 it refers to. */
16500 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16501 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16502 else
16503 containing_scope = TYPE_CONTEXT (t);
16505 /* Use the containing namespace if there is one. */
16506 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16508 if (context_die == lookup_decl_die (containing_scope))
16509 /* OK */;
16510 else if (debug_info_level > DINFO_LEVEL_TERSE)
16511 context_die = get_context_die (containing_scope);
16512 else
16513 containing_scope = NULL_TREE;
16516 /* Ignore function type "scopes" from the C frontend. They mean that
16517 a tagged type is local to a parmlist of a function declarator, but
16518 that isn't useful to DWARF. */
16519 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16520 containing_scope = NULL_TREE;
16522 if (SCOPE_FILE_SCOPE_P (containing_scope))
16524 /* If T uses a local type keep it local as well, to avoid references
16525 to function-local DIEs from outside the function. */
16526 if (current_function_decl && uses_local_type (t))
16527 scope_die = context_die;
16528 else
16529 scope_die = comp_unit_die ();
16531 else if (TYPE_P (containing_scope))
16533 /* For types, we can just look up the appropriate DIE. */
16534 if (debug_info_level > DINFO_LEVEL_TERSE)
16535 scope_die = get_context_die (containing_scope);
16536 else
16538 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16539 if (scope_die == NULL)
16540 scope_die = comp_unit_die ();
16543 else
16544 scope_die = context_die;
16546 return scope_die;
16549 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16551 static inline int
16552 local_scope_p (dw_die_ref context_die)
16554 for (; context_die; context_die = context_die->die_parent)
16555 if (context_die->die_tag == DW_TAG_inlined_subroutine
16556 || context_die->die_tag == DW_TAG_subprogram)
16557 return 1;
16559 return 0;
16562 /* Returns nonzero if CONTEXT_DIE is a class. */
16564 static inline int
16565 class_scope_p (dw_die_ref context_die)
16567 return (context_die
16568 && (context_die->die_tag == DW_TAG_structure_type
16569 || context_die->die_tag == DW_TAG_class_type
16570 || context_die->die_tag == DW_TAG_interface_type
16571 || context_die->die_tag == DW_TAG_union_type));
16574 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16575 whether or not to treat a DIE in this context as a declaration. */
16577 static inline int
16578 class_or_namespace_scope_p (dw_die_ref context_die)
16580 return (class_scope_p (context_die)
16581 || (context_die && context_die->die_tag == DW_TAG_namespace));
16584 /* Many forms of DIEs require a "type description" attribute. This
16585 routine locates the proper "type descriptor" die for the type given
16586 by 'type', and adds a DW_AT_type attribute below the given die. */
16588 static void
16589 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16590 int decl_volatile, dw_die_ref context_die)
16592 enum tree_code code = TREE_CODE (type);
16593 dw_die_ref type_die = NULL;
16595 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16596 or fixed-point type, use the inner type. This is because we have no
16597 support for unnamed types in base_type_die. This can happen if this is
16598 an Ada subrange type. Correct solution is emit a subrange type die. */
16599 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16600 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16601 type = TREE_TYPE (type), code = TREE_CODE (type);
16603 if (code == ERROR_MARK
16604 /* Handle a special case. For functions whose return type is void, we
16605 generate *no* type attribute. (Note that no object may have type
16606 `void', so this only applies to function return types). */
16607 || code == VOID_TYPE)
16608 return;
16610 type_die = modified_type_die (type,
16611 decl_const || TYPE_READONLY (type),
16612 decl_volatile || TYPE_VOLATILE (type),
16613 context_die);
16615 if (type_die != NULL)
16616 add_AT_die_ref (object_die, DW_AT_type, type_die);
16619 /* Given an object die, add the calling convention attribute for the
16620 function call type. */
16621 static void
16622 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16624 enum dwarf_calling_convention value = DW_CC_normal;
16626 value = ((enum dwarf_calling_convention)
16627 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16629 if (is_fortran ()
16630 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16632 /* DWARF 2 doesn't provide a way to identify a program's source-level
16633 entry point. DW_AT_calling_convention attributes are only meant
16634 to describe functions' calling conventions. However, lacking a
16635 better way to signal the Fortran main program, we used this for
16636 a long time, following existing custom. Now, DWARF 4 has
16637 DW_AT_main_subprogram, which we add below, but some tools still
16638 rely on the old way, which we thus keep. */
16639 value = DW_CC_program;
16641 if (dwarf_version >= 4 || !dwarf_strict)
16642 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16645 /* Only add the attribute if the backend requests it, and
16646 is not DW_CC_normal. */
16647 if (value && (value != DW_CC_normal))
16648 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16651 /* Given a tree pointer to a struct, class, union, or enum type node, return
16652 a pointer to the (string) tag name for the given type, or zero if the type
16653 was declared without a tag. */
16655 static const char *
16656 type_tag (const_tree type)
16658 const char *name = 0;
16660 if (TYPE_NAME (type) != 0)
16662 tree t = 0;
16664 /* Find the IDENTIFIER_NODE for the type name. */
16665 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16666 && !TYPE_NAMELESS (type))
16667 t = TYPE_NAME (type);
16669 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16670 a TYPE_DECL node, regardless of whether or not a `typedef' was
16671 involved. */
16672 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16673 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16675 /* We want to be extra verbose. Don't call dwarf_name if
16676 DECL_NAME isn't set. The default hook for decl_printable_name
16677 doesn't like that, and in this context it's correct to return
16678 0, instead of "<anonymous>" or the like. */
16679 if (DECL_NAME (TYPE_NAME (type))
16680 && !DECL_NAMELESS (TYPE_NAME (type)))
16681 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16684 /* Now get the name as a string, or invent one. */
16685 if (!name && t != 0)
16686 name = IDENTIFIER_POINTER (t);
16689 return (name == 0 || *name == '\0') ? 0 : name;
16692 /* Return the type associated with a data member, make a special check
16693 for bit field types. */
16695 static inline tree
16696 member_declared_type (const_tree member)
16698 return (DECL_BIT_FIELD_TYPE (member)
16699 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16702 /* Get the decl's label, as described by its RTL. This may be different
16703 from the DECL_NAME name used in the source file. */
16705 #if 0
16706 static const char *
16707 decl_start_label (tree decl)
16709 rtx x;
16710 const char *fnname;
16712 x = DECL_RTL (decl);
16713 gcc_assert (MEM_P (x));
16715 x = XEXP (x, 0);
16716 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16718 fnname = XSTR (x, 0);
16719 return fnname;
16721 #endif
16723 /* These routines generate the internal representation of the DIE's for
16724 the compilation unit. Debugging information is collected by walking
16725 the declaration trees passed in from dwarf2out_decl(). */
16727 static void
16728 gen_array_type_die (tree type, dw_die_ref context_die)
16730 dw_die_ref scope_die = scope_die_for (type, context_die);
16731 dw_die_ref array_die;
16733 /* GNU compilers represent multidimensional array types as sequences of one
16734 dimensional array types whose element types are themselves array types.
16735 We sometimes squish that down to a single array_type DIE with multiple
16736 subscripts in the Dwarf debugging info. The draft Dwarf specification
16737 say that we are allowed to do this kind of compression in C, because
16738 there is no difference between an array of arrays and a multidimensional
16739 array. We don't do this for Ada to remain as close as possible to the
16740 actual representation, which is especially important against the language
16741 flexibilty wrt arrays of variable size. */
16743 bool collapse_nested_arrays = !is_ada ();
16744 tree element_type;
16746 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16747 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16748 if (TYPE_STRING_FLAG (type)
16749 && TREE_CODE (type) == ARRAY_TYPE
16750 && is_fortran ()
16751 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16753 HOST_WIDE_INT size;
16755 array_die = new_die (DW_TAG_string_type, scope_die, type);
16756 add_name_attribute (array_die, type_tag (type));
16757 equate_type_number_to_die (type, array_die);
16758 size = int_size_in_bytes (type);
16759 if (size >= 0)
16760 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16761 else if (TYPE_DOMAIN (type) != NULL_TREE
16762 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16763 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16765 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16766 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16768 size = int_size_in_bytes (TREE_TYPE (szdecl));
16769 if (loc && size > 0)
16771 add_AT_location_description (array_die, DW_AT_string_length, loc);
16772 if (size != DWARF2_ADDR_SIZE)
16773 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16776 return;
16779 array_die = new_die (DW_TAG_array_type, scope_die, type);
16780 add_name_attribute (array_die, type_tag (type));
16781 equate_type_number_to_die (type, array_die);
16783 if (TREE_CODE (type) == VECTOR_TYPE)
16784 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16786 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16787 if (is_fortran ()
16788 && TREE_CODE (type) == ARRAY_TYPE
16789 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16790 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16791 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16793 #if 0
16794 /* We default the array ordering. SDB will probably do
16795 the right things even if DW_AT_ordering is not present. It's not even
16796 an issue until we start to get into multidimensional arrays anyway. If
16797 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16798 then we'll have to put the DW_AT_ordering attribute back in. (But if
16799 and when we find out that we need to put these in, we will only do so
16800 for multidimensional arrays. */
16801 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16802 #endif
16804 if (TREE_CODE (type) == VECTOR_TYPE)
16806 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16807 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16808 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16809 add_bound_info (subrange_die, DW_AT_upper_bound,
16810 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16812 else
16813 add_subscript_info (array_die, type, collapse_nested_arrays);
16815 /* Add representation of the type of the elements of this array type and
16816 emit the corresponding DIE if we haven't done it already. */
16817 element_type = TREE_TYPE (type);
16818 if (collapse_nested_arrays)
16819 while (TREE_CODE (element_type) == ARRAY_TYPE)
16821 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16822 break;
16823 element_type = TREE_TYPE (element_type);
16826 add_type_attribute (array_die, element_type, 0, 0, context_die);
16828 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16829 if (TYPE_ARTIFICIAL (type))
16830 add_AT_flag (array_die, DW_AT_artificial, 1);
16832 if (get_AT (array_die, DW_AT_name))
16833 add_pubtype (type, array_die);
16836 static dw_loc_descr_ref
16837 descr_info_loc (tree val, tree base_decl)
16839 HOST_WIDE_INT size;
16840 dw_loc_descr_ref loc, loc2;
16841 enum dwarf_location_atom op;
16843 if (val == base_decl)
16844 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16846 switch (TREE_CODE (val))
16848 CASE_CONVERT:
16849 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16850 case VAR_DECL:
16851 return loc_descriptor_from_tree (val, 0);
16852 case INTEGER_CST:
16853 if (host_integerp (val, 0))
16854 return int_loc_descriptor (tree_low_cst (val, 0));
16855 break;
16856 case INDIRECT_REF:
16857 size = int_size_in_bytes (TREE_TYPE (val));
16858 if (size < 0)
16859 break;
16860 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16861 if (!loc)
16862 break;
16863 if (size == DWARF2_ADDR_SIZE)
16864 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16865 else
16866 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16867 return loc;
16868 case POINTER_PLUS_EXPR:
16869 case PLUS_EXPR:
16870 if (host_integerp (TREE_OPERAND (val, 1), 1)
16871 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16872 < 16384)
16874 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16875 if (!loc)
16876 break;
16877 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16879 else
16881 op = DW_OP_plus;
16882 do_binop:
16883 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16884 if (!loc)
16885 break;
16886 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16887 if (!loc2)
16888 break;
16889 add_loc_descr (&loc, loc2);
16890 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16892 return loc;
16893 case MINUS_EXPR:
16894 op = DW_OP_minus;
16895 goto do_binop;
16896 case MULT_EXPR:
16897 op = DW_OP_mul;
16898 goto do_binop;
16899 case EQ_EXPR:
16900 op = DW_OP_eq;
16901 goto do_binop;
16902 case NE_EXPR:
16903 op = DW_OP_ne;
16904 goto do_binop;
16905 default:
16906 break;
16908 return NULL;
16911 static void
16912 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16913 tree val, tree base_decl)
16915 dw_loc_descr_ref loc;
16917 if (host_integerp (val, 0))
16919 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16920 return;
16923 loc = descr_info_loc (val, base_decl);
16924 if (!loc)
16925 return;
16927 add_AT_loc (die, attr, loc);
16930 /* This routine generates DIE for array with hidden descriptor, details
16931 are filled into *info by a langhook. */
16933 static void
16934 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16935 dw_die_ref context_die)
16937 dw_die_ref scope_die = scope_die_for (type, context_die);
16938 dw_die_ref array_die;
16939 int dim;
16941 array_die = new_die (DW_TAG_array_type, scope_die, type);
16942 add_name_attribute (array_die, type_tag (type));
16943 equate_type_number_to_die (type, array_die);
16945 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16946 if (is_fortran ()
16947 && info->ndimensions >= 2)
16948 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16950 if (info->data_location)
16951 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16952 info->base_decl);
16953 if (info->associated)
16954 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16955 info->base_decl);
16956 if (info->allocated)
16957 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16958 info->base_decl);
16960 for (dim = 0; dim < info->ndimensions; dim++)
16962 dw_die_ref subrange_die
16963 = new_die (DW_TAG_subrange_type, array_die, NULL);
16965 if (info->dimen[dim].lower_bound)
16967 /* If it is the default value, omit it. */
16968 int dflt;
16970 if (host_integerp (info->dimen[dim].lower_bound, 0)
16971 && (dflt = lower_bound_default ()) != -1
16972 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16974 else
16975 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16976 info->dimen[dim].lower_bound,
16977 info->base_decl);
16979 if (info->dimen[dim].upper_bound)
16980 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16981 info->dimen[dim].upper_bound,
16982 info->base_decl);
16983 if (info->dimen[dim].stride)
16984 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16985 info->dimen[dim].stride,
16986 info->base_decl);
16989 gen_type_die (info->element_type, context_die);
16990 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16992 if (get_AT (array_die, DW_AT_name))
16993 add_pubtype (type, array_die);
16996 #if 0
16997 static void
16998 gen_entry_point_die (tree decl, dw_die_ref context_die)
17000 tree origin = decl_ultimate_origin (decl);
17001 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17003 if (origin != NULL)
17004 add_abstract_origin_attribute (decl_die, origin);
17005 else
17007 add_name_and_src_coords_attributes (decl_die, decl);
17008 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17009 0, 0, context_die);
17012 if (DECL_ABSTRACT (decl))
17013 equate_decl_number_to_die (decl, decl_die);
17014 else
17015 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17017 #endif
17019 /* Walk through the list of incomplete types again, trying once more to
17020 emit full debugging info for them. */
17022 static void
17023 retry_incomplete_types (void)
17025 int i;
17027 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17028 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17029 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17032 /* Determine what tag to use for a record type. */
17034 static enum dwarf_tag
17035 record_type_tag (tree type)
17037 if (! lang_hooks.types.classify_record)
17038 return DW_TAG_structure_type;
17040 switch (lang_hooks.types.classify_record (type))
17042 case RECORD_IS_STRUCT:
17043 return DW_TAG_structure_type;
17045 case RECORD_IS_CLASS:
17046 return DW_TAG_class_type;
17048 case RECORD_IS_INTERFACE:
17049 if (dwarf_version >= 3 || !dwarf_strict)
17050 return DW_TAG_interface_type;
17051 return DW_TAG_structure_type;
17053 default:
17054 gcc_unreachable ();
17058 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17059 include all of the information about the enumeration values also. Each
17060 enumerated type name/value is listed as a child of the enumerated type
17061 DIE. */
17063 static dw_die_ref
17064 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17066 dw_die_ref type_die = lookup_type_die (type);
17068 if (type_die == NULL)
17070 type_die = new_die (DW_TAG_enumeration_type,
17071 scope_die_for (type, context_die), type);
17072 equate_type_number_to_die (type, type_die);
17073 add_name_attribute (type_die, type_tag (type));
17074 if (dwarf_version >= 4 || !dwarf_strict)
17076 if (ENUM_IS_SCOPED (type))
17077 add_AT_flag (type_die, DW_AT_enum_class, 1);
17078 if (ENUM_IS_OPAQUE (type))
17079 add_AT_flag (type_die, DW_AT_declaration, 1);
17082 else if (! TYPE_SIZE (type))
17083 return type_die;
17084 else
17085 remove_AT (type_die, DW_AT_declaration);
17087 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17088 given enum type is incomplete, do not generate the DW_AT_byte_size
17089 attribute or the DW_AT_element_list attribute. */
17090 if (TYPE_SIZE (type))
17092 tree link;
17094 TREE_ASM_WRITTEN (type) = 1;
17095 add_byte_size_attribute (type_die, type);
17096 if (TYPE_STUB_DECL (type) != NULL_TREE)
17098 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17099 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17102 /* If the first reference to this type was as the return type of an
17103 inline function, then it may not have a parent. Fix this now. */
17104 if (type_die->die_parent == NULL)
17105 add_child_die (scope_die_for (type, context_die), type_die);
17107 for (link = TYPE_VALUES (type);
17108 link != NULL; link = TREE_CHAIN (link))
17110 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17111 tree value = TREE_VALUE (link);
17113 add_name_attribute (enum_die,
17114 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17116 if (TREE_CODE (value) == CONST_DECL)
17117 value = DECL_INITIAL (value);
17119 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value)))
17120 && (simple_type_size_in_bits (TREE_TYPE (value))
17121 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0)))
17122 /* DWARF2 does not provide a way of indicating whether or
17123 not enumeration constants are signed or unsigned. GDB
17124 always assumes the values are signed, so we output all
17125 values as if they were signed. That means that
17126 enumeration constants with very large unsigned values
17127 will appear to have negative values in the debugger.
17129 TODO: the above comment is wrong, DWARF2 does provide
17130 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17131 This should be re-worked to use correct signed/unsigned
17132 int/double tags for all cases, instead of always treating as
17133 signed. */
17134 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17135 else
17136 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17137 that here. */
17138 add_AT_double (enum_die, DW_AT_const_value,
17139 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17142 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17143 if (TYPE_ARTIFICIAL (type))
17144 add_AT_flag (type_die, DW_AT_artificial, 1);
17146 else
17147 add_AT_flag (type_die, DW_AT_declaration, 1);
17149 add_pubtype (type, type_die);
17151 return type_die;
17154 /* Generate a DIE to represent either a real live formal parameter decl or to
17155 represent just the type of some formal parameter position in some function
17156 type.
17158 Note that this routine is a bit unusual because its argument may be a
17159 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17160 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17161 node. If it's the former then this function is being called to output a
17162 DIE to represent a formal parameter object (or some inlining thereof). If
17163 it's the latter, then this function is only being called to output a
17164 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17165 argument type of some subprogram type.
17166 If EMIT_NAME_P is true, name and source coordinate attributes
17167 are emitted. */
17169 static dw_die_ref
17170 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17171 dw_die_ref context_die)
17173 tree node_or_origin = node ? node : origin;
17174 tree ultimate_origin;
17175 dw_die_ref parm_die
17176 = new_die (DW_TAG_formal_parameter, context_die, node);
17178 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17180 case tcc_declaration:
17181 ultimate_origin = decl_ultimate_origin (node_or_origin);
17182 if (node || ultimate_origin)
17183 origin = ultimate_origin;
17184 if (origin != NULL)
17185 add_abstract_origin_attribute (parm_die, origin);
17186 else if (emit_name_p)
17187 add_name_and_src_coords_attributes (parm_die, node);
17188 if (origin == NULL
17189 || (! DECL_ABSTRACT (node_or_origin)
17190 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17191 decl_function_context
17192 (node_or_origin))))
17194 tree type = TREE_TYPE (node_or_origin);
17195 if (decl_by_reference_p (node_or_origin))
17196 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17197 context_die);
17198 else
17199 add_type_attribute (parm_die, type,
17200 TREE_READONLY (node_or_origin),
17201 TREE_THIS_VOLATILE (node_or_origin),
17202 context_die);
17204 if (origin == NULL && DECL_ARTIFICIAL (node))
17205 add_AT_flag (parm_die, DW_AT_artificial, 1);
17207 if (node && node != origin)
17208 equate_decl_number_to_die (node, parm_die);
17209 if (! DECL_ABSTRACT (node_or_origin))
17210 add_location_or_const_value_attribute (parm_die, node_or_origin,
17211 node == NULL, DW_AT_location);
17213 break;
17215 case tcc_type:
17216 /* We were called with some kind of a ..._TYPE node. */
17217 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17218 break;
17220 default:
17221 gcc_unreachable ();
17224 return parm_die;
17227 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17228 children DW_TAG_formal_parameter DIEs representing the arguments of the
17229 parameter pack.
17231 PARM_PACK must be a function parameter pack.
17232 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17233 must point to the subsequent arguments of the function PACK_ARG belongs to.
17234 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17235 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17236 following the last one for which a DIE was generated. */
17238 static dw_die_ref
17239 gen_formal_parameter_pack_die (tree parm_pack,
17240 tree pack_arg,
17241 dw_die_ref subr_die,
17242 tree *next_arg)
17244 tree arg;
17245 dw_die_ref parm_pack_die;
17247 gcc_assert (parm_pack
17248 && lang_hooks.function_parameter_pack_p (parm_pack)
17249 && subr_die);
17251 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17252 add_src_coords_attributes (parm_pack_die, parm_pack);
17254 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17256 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17257 parm_pack))
17258 break;
17259 gen_formal_parameter_die (arg, NULL,
17260 false /* Don't emit name attribute. */,
17261 parm_pack_die);
17263 if (next_arg)
17264 *next_arg = arg;
17265 return parm_pack_die;
17268 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17269 at the end of an (ANSI prototyped) formal parameters list. */
17271 static void
17272 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17274 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17277 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17278 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17279 parameters as specified in some function type specification (except for
17280 those which appear as part of a function *definition*). */
17282 static void
17283 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17285 tree link;
17286 tree formal_type = NULL;
17287 tree first_parm_type;
17288 tree arg;
17290 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17292 arg = DECL_ARGUMENTS (function_or_method_type);
17293 function_or_method_type = TREE_TYPE (function_or_method_type);
17295 else
17296 arg = NULL_TREE;
17298 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17300 /* Make our first pass over the list of formal parameter types and output a
17301 DW_TAG_formal_parameter DIE for each one. */
17302 for (link = first_parm_type; link; )
17304 dw_die_ref parm_die;
17306 formal_type = TREE_VALUE (link);
17307 if (formal_type == void_type_node)
17308 break;
17310 /* Output a (nameless) DIE to represent the formal parameter itself. */
17311 parm_die = gen_formal_parameter_die (formal_type, NULL,
17312 true /* Emit name attribute. */,
17313 context_die);
17314 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17315 && link == first_parm_type)
17317 add_AT_flag (parm_die, DW_AT_artificial, 1);
17318 if (dwarf_version >= 3 || !dwarf_strict)
17319 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17321 else if (arg && DECL_ARTIFICIAL (arg))
17322 add_AT_flag (parm_die, DW_AT_artificial, 1);
17324 link = TREE_CHAIN (link);
17325 if (arg)
17326 arg = DECL_CHAIN (arg);
17329 /* If this function type has an ellipsis, add a
17330 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17331 if (formal_type != void_type_node)
17332 gen_unspecified_parameters_die (function_or_method_type, context_die);
17334 /* Make our second (and final) pass over the list of formal parameter types
17335 and output DIEs to represent those types (as necessary). */
17336 for (link = TYPE_ARG_TYPES (function_or_method_type);
17337 link && TREE_VALUE (link);
17338 link = TREE_CHAIN (link))
17339 gen_type_die (TREE_VALUE (link), context_die);
17342 /* We want to generate the DIE for TYPE so that we can generate the
17343 die for MEMBER, which has been defined; we will need to refer back
17344 to the member declaration nested within TYPE. If we're trying to
17345 generate minimal debug info for TYPE, processing TYPE won't do the
17346 trick; we need to attach the member declaration by hand. */
17348 static void
17349 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17351 gen_type_die (type, context_die);
17353 /* If we're trying to avoid duplicate debug info, we may not have
17354 emitted the member decl for this function. Emit it now. */
17355 if (TYPE_STUB_DECL (type)
17356 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17357 && ! lookup_decl_die (member))
17359 dw_die_ref type_die;
17360 gcc_assert (!decl_ultimate_origin (member));
17362 push_decl_scope (type);
17363 type_die = lookup_type_die_strip_naming_typedef (type);
17364 if (TREE_CODE (member) == FUNCTION_DECL)
17365 gen_subprogram_die (member, type_die);
17366 else if (TREE_CODE (member) == FIELD_DECL)
17368 /* Ignore the nameless fields that are used to skip bits but handle
17369 C++ anonymous unions and structs. */
17370 if (DECL_NAME (member) != NULL_TREE
17371 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17372 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17374 gen_type_die (member_declared_type (member), type_die);
17375 gen_field_die (member, type_die);
17378 else
17379 gen_variable_die (member, NULL_TREE, type_die);
17381 pop_decl_scope ();
17385 /* Forward declare these functions, because they are mutually recursive
17386 with their set_block_* pairing functions. */
17387 static void set_decl_origin_self (tree);
17388 static void set_decl_abstract_flags (tree, int);
17390 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17391 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17392 that it points to the node itself, thus indicating that the node is its
17393 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17394 the given node is NULL, recursively descend the decl/block tree which
17395 it is the root of, and for each other ..._DECL or BLOCK node contained
17396 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17397 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17398 values to point to themselves. */
17400 static void
17401 set_block_origin_self (tree stmt)
17403 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17405 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17408 tree local_decl;
17410 for (local_decl = BLOCK_VARS (stmt);
17411 local_decl != NULL_TREE;
17412 local_decl = DECL_CHAIN (local_decl))
17413 if (! DECL_EXTERNAL (local_decl))
17414 set_decl_origin_self (local_decl); /* Potential recursion. */
17418 tree subblock;
17420 for (subblock = BLOCK_SUBBLOCKS (stmt);
17421 subblock != NULL_TREE;
17422 subblock = BLOCK_CHAIN (subblock))
17423 set_block_origin_self (subblock); /* Recurse. */
17428 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17429 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17430 node to so that it points to the node itself, thus indicating that the
17431 node represents its own (abstract) origin. Additionally, if the
17432 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17433 the decl/block tree of which the given node is the root of, and for
17434 each other ..._DECL or BLOCK node contained therein whose
17435 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17436 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17437 point to themselves. */
17439 static void
17440 set_decl_origin_self (tree decl)
17442 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17444 DECL_ABSTRACT_ORIGIN (decl) = decl;
17445 if (TREE_CODE (decl) == FUNCTION_DECL)
17447 tree arg;
17449 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17450 DECL_ABSTRACT_ORIGIN (arg) = arg;
17451 if (DECL_INITIAL (decl) != NULL_TREE
17452 && DECL_INITIAL (decl) != error_mark_node)
17453 set_block_origin_self (DECL_INITIAL (decl));
17458 /* Given a pointer to some BLOCK node, and a boolean value to set the
17459 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17460 the given block, and for all local decls and all local sub-blocks
17461 (recursively) which are contained therein. */
17463 static void
17464 set_block_abstract_flags (tree stmt, int setting)
17466 tree local_decl;
17467 tree subblock;
17468 unsigned int i;
17470 BLOCK_ABSTRACT (stmt) = setting;
17472 for (local_decl = BLOCK_VARS (stmt);
17473 local_decl != NULL_TREE;
17474 local_decl = DECL_CHAIN (local_decl))
17475 if (! DECL_EXTERNAL (local_decl))
17476 set_decl_abstract_flags (local_decl, setting);
17478 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17480 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17481 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17482 || TREE_CODE (local_decl) == PARM_DECL)
17483 set_decl_abstract_flags (local_decl, setting);
17486 for (subblock = BLOCK_SUBBLOCKS (stmt);
17487 subblock != NULL_TREE;
17488 subblock = BLOCK_CHAIN (subblock))
17489 set_block_abstract_flags (subblock, setting);
17492 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17493 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17494 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17495 set the abstract flags for all of the parameters, local vars, local
17496 blocks and sub-blocks (recursively) to the same setting. */
17498 static void
17499 set_decl_abstract_flags (tree decl, int setting)
17501 DECL_ABSTRACT (decl) = setting;
17502 if (TREE_CODE (decl) == FUNCTION_DECL)
17504 tree arg;
17506 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17507 DECL_ABSTRACT (arg) = setting;
17508 if (DECL_INITIAL (decl) != NULL_TREE
17509 && DECL_INITIAL (decl) != error_mark_node)
17510 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17514 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17515 may later generate inlined and/or out-of-line instances of. */
17517 static void
17518 dwarf2out_abstract_function (tree decl)
17520 dw_die_ref old_die;
17521 tree save_fn;
17522 tree context;
17523 int was_abstract;
17524 htab_t old_decl_loc_table;
17525 htab_t old_cached_dw_loc_list_table;
17526 int old_call_site_count, old_tail_call_site_count;
17527 struct call_arg_loc_node *old_call_arg_locations;
17529 /* Make sure we have the actual abstract inline, not a clone. */
17530 decl = DECL_ORIGIN (decl);
17532 old_die = lookup_decl_die (decl);
17533 if (old_die && get_AT (old_die, DW_AT_inline))
17534 /* We've already generated the abstract instance. */
17535 return;
17537 /* We can be called while recursively when seeing block defining inlined subroutine
17538 DIE. Be sure to not clobber the outer location table nor use it or we would
17539 get locations in abstract instantces. */
17540 old_decl_loc_table = decl_loc_table;
17541 decl_loc_table = NULL;
17542 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17543 cached_dw_loc_list_table = NULL;
17544 old_call_arg_locations = call_arg_locations;
17545 call_arg_locations = NULL;
17546 old_call_site_count = call_site_count;
17547 call_site_count = -1;
17548 old_tail_call_site_count = tail_call_site_count;
17549 tail_call_site_count = -1;
17551 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17552 we don't get confused by DECL_ABSTRACT. */
17553 if (debug_info_level > DINFO_LEVEL_TERSE)
17555 context = decl_class_context (decl);
17556 if (context)
17557 gen_type_die_for_member
17558 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17561 /* Pretend we've just finished compiling this function. */
17562 save_fn = current_function_decl;
17563 current_function_decl = decl;
17565 was_abstract = DECL_ABSTRACT (decl);
17566 set_decl_abstract_flags (decl, 1);
17567 dwarf2out_decl (decl);
17568 if (! was_abstract)
17569 set_decl_abstract_flags (decl, 0);
17571 current_function_decl = save_fn;
17572 decl_loc_table = old_decl_loc_table;
17573 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17574 call_arg_locations = old_call_arg_locations;
17575 call_site_count = old_call_site_count;
17576 tail_call_site_count = old_tail_call_site_count;
17579 /* Helper function of premark_used_types() which gets called through
17580 htab_traverse.
17582 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17583 marked as unused by prune_unused_types. */
17585 static int
17586 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17588 tree type;
17589 dw_die_ref die;
17591 type = (tree) *slot;
17592 die = lookup_type_die (type);
17593 if (die != NULL)
17594 die->die_perennial_p = 1;
17595 return 1;
17598 /* Helper function of premark_types_used_by_global_vars which gets called
17599 through htab_traverse.
17601 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17602 marked as unused by prune_unused_types. The DIE of the type is marked
17603 only if the global variable using the type will actually be emitted. */
17605 static int
17606 premark_types_used_by_global_vars_helper (void **slot,
17607 void *data ATTRIBUTE_UNUSED)
17609 struct types_used_by_vars_entry *entry;
17610 dw_die_ref die;
17612 entry = (struct types_used_by_vars_entry *) *slot;
17613 gcc_assert (entry->type != NULL
17614 && entry->var_decl != NULL);
17615 die = lookup_type_die (entry->type);
17616 if (die)
17618 /* Ask cgraph if the global variable really is to be emitted.
17619 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17620 struct varpool_node *node = varpool_get_node (entry->var_decl);
17621 if (node && node->symbol.definition)
17623 die->die_perennial_p = 1;
17624 /* Keep the parent DIEs as well. */
17625 while ((die = die->die_parent) && die->die_perennial_p == 0)
17626 die->die_perennial_p = 1;
17629 return 1;
17632 /* Mark all members of used_types_hash as perennial. */
17634 static void
17635 premark_used_types (struct function *fun)
17637 if (fun && fun->used_types_hash)
17638 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17641 /* Mark all members of types_used_by_vars_entry as perennial. */
17643 static void
17644 premark_types_used_by_global_vars (void)
17646 if (types_used_by_vars_hash)
17647 htab_traverse (types_used_by_vars_hash,
17648 premark_types_used_by_global_vars_helper, NULL);
17651 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17652 for CA_LOC call arg loc node. */
17654 static dw_die_ref
17655 gen_call_site_die (tree decl, dw_die_ref subr_die,
17656 struct call_arg_loc_node *ca_loc)
17658 dw_die_ref stmt_die = NULL, die;
17659 tree block = ca_loc->block;
17661 while (block
17662 && block != DECL_INITIAL (decl)
17663 && TREE_CODE (block) == BLOCK)
17665 if (block_map.length () > BLOCK_NUMBER (block))
17666 stmt_die = block_map[BLOCK_NUMBER (block)];
17667 if (stmt_die)
17668 break;
17669 block = BLOCK_SUPERCONTEXT (block);
17671 if (stmt_die == NULL)
17672 stmt_die = subr_die;
17673 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17674 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17675 if (ca_loc->tail_call_p)
17676 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17677 if (ca_loc->symbol_ref)
17679 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17680 if (tdie)
17681 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17682 else
17683 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17685 return die;
17688 /* Generate a DIE to represent a declared function (either file-scope or
17689 block-local). */
17691 static void
17692 gen_subprogram_die (tree decl, dw_die_ref context_die)
17694 tree origin = decl_ultimate_origin (decl);
17695 dw_die_ref subr_die;
17696 tree outer_scope;
17697 dw_die_ref old_die = lookup_decl_die (decl);
17698 int declaration = (current_function_decl != decl
17699 || class_or_namespace_scope_p (context_die));
17701 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17703 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17704 started to generate the abstract instance of an inline, decided to output
17705 its containing class, and proceeded to emit the declaration of the inline
17706 from the member list for the class. If so, DECLARATION takes priority;
17707 we'll get back to the abstract instance when done with the class. */
17709 /* The class-scope declaration DIE must be the primary DIE. */
17710 if (origin && declaration && class_or_namespace_scope_p (context_die))
17712 origin = NULL;
17713 gcc_assert (!old_die);
17716 /* Now that the C++ front end lazily declares artificial member fns, we
17717 might need to retrofit the declaration into its class. */
17718 if (!declaration && !origin && !old_die
17719 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17720 && !class_or_namespace_scope_p (context_die)
17721 && debug_info_level > DINFO_LEVEL_TERSE)
17722 old_die = force_decl_die (decl);
17724 if (origin != NULL)
17726 gcc_assert (!declaration || local_scope_p (context_die));
17728 /* Fixup die_parent for the abstract instance of a nested
17729 inline function. */
17730 if (old_die && old_die->die_parent == NULL)
17731 add_child_die (context_die, old_die);
17733 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17734 add_abstract_origin_attribute (subr_die, origin);
17735 /* This is where the actual code for a cloned function is.
17736 Let's emit linkage name attribute for it. This helps
17737 debuggers to e.g, set breakpoints into
17738 constructors/destructors when the user asks "break
17739 K::K". */
17740 add_linkage_name (subr_die, decl);
17742 else if (old_die)
17744 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17745 struct dwarf_file_data * file_index = lookup_filename (s.file);
17747 if (!get_AT_flag (old_die, DW_AT_declaration)
17748 /* We can have a normal definition following an inline one in the
17749 case of redefinition of GNU C extern inlines.
17750 It seems reasonable to use AT_specification in this case. */
17751 && !get_AT (old_die, DW_AT_inline))
17753 /* Detect and ignore this case, where we are trying to output
17754 something we have already output. */
17755 return;
17758 /* If the definition comes from the same place as the declaration,
17759 maybe use the old DIE. We always want the DIE for this function
17760 that has the *_pc attributes to be under comp_unit_die so the
17761 debugger can find it. We also need to do this for abstract
17762 instances of inlines, since the spec requires the out-of-line copy
17763 to have the same parent. For local class methods, this doesn't
17764 apply; we just use the old DIE. */
17765 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17766 && (DECL_ARTIFICIAL (decl)
17767 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17768 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17769 == (unsigned) s.line))))
17771 subr_die = old_die;
17773 /* Clear out the declaration attribute and the formal parameters.
17774 Do not remove all children, because it is possible that this
17775 declaration die was forced using force_decl_die(). In such
17776 cases die that forced declaration die (e.g. TAG_imported_module)
17777 is one of the children that we do not want to remove. */
17778 remove_AT (subr_die, DW_AT_declaration);
17779 remove_AT (subr_die, DW_AT_object_pointer);
17780 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17782 else
17784 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17785 add_AT_specification (subr_die, old_die);
17786 add_pubname (decl, subr_die);
17787 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17788 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17789 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17790 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17793 else
17795 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17797 if (TREE_PUBLIC (decl))
17798 add_AT_flag (subr_die, DW_AT_external, 1);
17800 add_name_and_src_coords_attributes (subr_die, decl);
17801 add_pubname (decl, subr_die);
17802 if (debug_info_level > DINFO_LEVEL_TERSE)
17804 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17805 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17806 0, 0, context_die);
17809 add_pure_or_virtual_attribute (subr_die, decl);
17810 if (DECL_ARTIFICIAL (decl))
17811 add_AT_flag (subr_die, DW_AT_artificial, 1);
17813 add_accessibility_attribute (subr_die, decl);
17816 if (declaration)
17818 if (!old_die || !get_AT (old_die, DW_AT_inline))
17820 add_AT_flag (subr_die, DW_AT_declaration, 1);
17822 /* If this is an explicit function declaration then generate
17823 a DW_AT_explicit attribute. */
17824 if (lang_hooks.decls.function_decl_explicit_p (decl)
17825 && (dwarf_version >= 3 || !dwarf_strict))
17826 add_AT_flag (subr_die, DW_AT_explicit, 1);
17828 /* The first time we see a member function, it is in the context of
17829 the class to which it belongs. We make sure of this by emitting
17830 the class first. The next time is the definition, which is
17831 handled above. The two may come from the same source text.
17833 Note that force_decl_die() forces function declaration die. It is
17834 later reused to represent definition. */
17835 equate_decl_number_to_die (decl, subr_die);
17838 else if (DECL_ABSTRACT (decl))
17840 if (DECL_DECLARED_INLINE_P (decl))
17842 if (cgraph_function_possibly_inlined_p (decl))
17843 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17844 else
17845 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17847 else
17849 if (cgraph_function_possibly_inlined_p (decl))
17850 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17851 else
17852 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17855 if (DECL_DECLARED_INLINE_P (decl)
17856 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17857 add_AT_flag (subr_die, DW_AT_artificial, 1);
17859 equate_decl_number_to_die (decl, subr_die);
17861 else if (!DECL_EXTERNAL (decl))
17863 HOST_WIDE_INT cfa_fb_offset;
17864 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17866 if (!old_die || !get_AT (old_die, DW_AT_inline))
17867 equate_decl_number_to_die (decl, subr_die);
17869 gcc_checking_assert (fun);
17870 if (!flag_reorder_blocks_and_partition)
17872 dw_fde_ref fde = fun->fde;
17873 if (fde->dw_fde_begin)
17875 /* We have already generated the labels. */
17876 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17877 fde->dw_fde_end, false);
17879 else
17881 /* Create start/end labels and add the range. */
17882 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17883 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17884 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17885 current_function_funcdef_no);
17886 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17887 current_function_funcdef_no);
17888 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17889 false);
17892 #if VMS_DEBUGGING_INFO
17893 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17894 Section 2.3 Prologue and Epilogue Attributes:
17895 When a breakpoint is set on entry to a function, it is generally
17896 desirable for execution to be suspended, not on the very first
17897 instruction of the function, but rather at a point after the
17898 function's frame has been set up, after any language defined local
17899 declaration processing has been completed, and before execution of
17900 the first statement of the function begins. Debuggers generally
17901 cannot properly determine where this point is. Similarly for a
17902 breakpoint set on exit from a function. The prologue and epilogue
17903 attributes allow a compiler to communicate the location(s) to use. */
17906 if (fde->dw_fde_vms_end_prologue)
17907 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17908 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17910 if (fde->dw_fde_vms_begin_epilogue)
17911 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17912 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17914 #endif
17917 else
17919 /* Generate pubnames entries for the split function code ranges. */
17920 dw_fde_ref fde = fun->fde;
17922 if (fde->dw_fde_second_begin)
17924 if (dwarf_version >= 3 || !dwarf_strict)
17926 /* We should use ranges for non-contiguous code section
17927 addresses. Use the actual code range for the initial
17928 section, since the HOT/COLD labels might precede an
17929 alignment offset. */
17930 bool range_list_added = false;
17931 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17932 fde->dw_fde_end, &range_list_added,
17933 false);
17934 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17935 fde->dw_fde_second_end,
17936 &range_list_added, false);
17937 if (range_list_added)
17938 add_ranges (NULL);
17940 else
17942 /* There is no real support in DW2 for this .. so we make
17943 a work-around. First, emit the pub name for the segment
17944 containing the function label. Then make and emit a
17945 simplified subprogram DIE for the second segment with the
17946 name pre-fixed by __hot/cold_sect_of_. We use the same
17947 linkage name for the second die so that gdb will find both
17948 sections when given "b foo". */
17949 const char *name = NULL;
17950 tree decl_name = DECL_NAME (decl);
17951 dw_die_ref seg_die;
17953 /* Do the 'primary' section. */
17954 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17955 fde->dw_fde_end, false);
17957 /* Build a minimal DIE for the secondary section. */
17958 seg_die = new_die (DW_TAG_subprogram,
17959 subr_die->die_parent, decl);
17961 if (TREE_PUBLIC (decl))
17962 add_AT_flag (seg_die, DW_AT_external, 1);
17964 if (decl_name != NULL
17965 && IDENTIFIER_POINTER (decl_name) != NULL)
17967 name = dwarf2_name (decl, 1);
17968 if (! DECL_ARTIFICIAL (decl))
17969 add_src_coords_attributes (seg_die, decl);
17971 add_linkage_name (seg_die, decl);
17973 gcc_assert (name != NULL);
17974 add_pure_or_virtual_attribute (seg_die, decl);
17975 if (DECL_ARTIFICIAL (decl))
17976 add_AT_flag (seg_die, DW_AT_artificial, 1);
17978 name = concat ("__second_sect_of_", name, NULL);
17979 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17980 fde->dw_fde_second_end, false);
17981 add_name_attribute (seg_die, name);
17982 if (want_pubnames ())
17983 add_pubname_string (name, seg_die);
17986 else
17987 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17988 false);
17991 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17993 /* We define the "frame base" as the function's CFA. This is more
17994 convenient for several reasons: (1) It's stable across the prologue
17995 and epilogue, which makes it better than just a frame pointer,
17996 (2) With dwarf3, there exists a one-byte encoding that allows us
17997 to reference the .debug_frame data by proxy, but failing that,
17998 (3) We can at least reuse the code inspection and interpretation
17999 code that determines the CFA position at various points in the
18000 function. */
18001 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18003 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18004 add_AT_loc (subr_die, DW_AT_frame_base, op);
18006 else
18008 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18009 if (list->dw_loc_next)
18010 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18011 else
18012 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18015 /* Compute a displacement from the "steady-state frame pointer" to
18016 the CFA. The former is what all stack slots and argument slots
18017 will reference in the rtl; the latter is what we've told the
18018 debugger about. We'll need to adjust all frame_base references
18019 by this displacement. */
18020 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18022 if (fun->static_chain_decl)
18023 add_AT_location_description (subr_die, DW_AT_static_link,
18024 loc_list_from_tree (fun->static_chain_decl, 2));
18027 /* Generate child dies for template paramaters. */
18028 if (debug_info_level > DINFO_LEVEL_TERSE)
18029 gen_generic_params_dies (decl);
18031 /* Now output descriptions of the arguments for this function. This gets
18032 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18033 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18034 `...' at the end of the formal parameter list. In order to find out if
18035 there was a trailing ellipsis or not, we must instead look at the type
18036 associated with the FUNCTION_DECL. This will be a node of type
18037 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18038 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18039 an ellipsis at the end. */
18041 /* In the case where we are describing a mere function declaration, all we
18042 need to do here (and all we *can* do here) is to describe the *types* of
18043 its formal parameters. */
18044 if (debug_info_level <= DINFO_LEVEL_TERSE)
18046 else if (declaration)
18047 gen_formal_types_die (decl, subr_die);
18048 else
18050 /* Generate DIEs to represent all known formal parameters. */
18051 tree parm = DECL_ARGUMENTS (decl);
18052 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18053 tree generic_decl_parm = generic_decl
18054 ? DECL_ARGUMENTS (generic_decl)
18055 : NULL;
18057 /* Now we want to walk the list of parameters of the function and
18058 emit their relevant DIEs.
18060 We consider the case of DECL being an instance of a generic function
18061 as well as it being a normal function.
18063 If DECL is an instance of a generic function we walk the
18064 parameters of the generic function declaration _and_ the parameters of
18065 DECL itself. This is useful because we want to emit specific DIEs for
18066 function parameter packs and those are declared as part of the
18067 generic function declaration. In that particular case,
18068 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18069 That DIE has children DIEs representing the set of arguments
18070 of the pack. Note that the set of pack arguments can be empty.
18071 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18072 children DIE.
18074 Otherwise, we just consider the parameters of DECL. */
18075 while (generic_decl_parm || parm)
18077 if (generic_decl_parm
18078 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18079 gen_formal_parameter_pack_die (generic_decl_parm,
18080 parm, subr_die,
18081 &parm);
18082 else if (parm)
18084 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18086 if (parm == DECL_ARGUMENTS (decl)
18087 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18088 && parm_die
18089 && (dwarf_version >= 3 || !dwarf_strict))
18090 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18092 parm = DECL_CHAIN (parm);
18095 if (generic_decl_parm)
18096 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18099 /* Decide whether we need an unspecified_parameters DIE at the end.
18100 There are 2 more cases to do this for: 1) the ansi ... declaration -
18101 this is detectable when the end of the arg list is not a
18102 void_type_node 2) an unprototyped function declaration (not a
18103 definition). This just means that we have no info about the
18104 parameters at all. */
18105 if (prototype_p (TREE_TYPE (decl)))
18107 /* This is the prototyped case, check for.... */
18108 if (stdarg_p (TREE_TYPE (decl)))
18109 gen_unspecified_parameters_die (decl, subr_die);
18111 else if (DECL_INITIAL (decl) == NULL_TREE)
18112 gen_unspecified_parameters_die (decl, subr_die);
18115 /* Output Dwarf info for all of the stuff within the body of the function
18116 (if it has one - it may be just a declaration). */
18117 outer_scope = DECL_INITIAL (decl);
18119 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18120 a function. This BLOCK actually represents the outermost binding contour
18121 for the function, i.e. the contour in which the function's formal
18122 parameters and labels get declared. Curiously, it appears that the front
18123 end doesn't actually put the PARM_DECL nodes for the current function onto
18124 the BLOCK_VARS list for this outer scope, but are strung off of the
18125 DECL_ARGUMENTS list for the function instead.
18127 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18128 the LABEL_DECL nodes for the function however, and we output DWARF info
18129 for those in decls_for_scope. Just within the `outer_scope' there will be
18130 a BLOCK node representing the function's outermost pair of curly braces,
18131 and any blocks used for the base and member initializers of a C++
18132 constructor function. */
18133 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18135 int call_site_note_count = 0;
18136 int tail_call_site_note_count = 0;
18138 /* Emit a DW_TAG_variable DIE for a named return value. */
18139 if (DECL_NAME (DECL_RESULT (decl)))
18140 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18142 current_function_has_inlines = 0;
18143 decls_for_scope (outer_scope, subr_die, 0);
18145 if (call_arg_locations && !dwarf_strict)
18147 struct call_arg_loc_node *ca_loc;
18148 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18150 dw_die_ref die = NULL;
18151 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18152 rtx arg, next_arg;
18154 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18155 arg; arg = next_arg)
18157 dw_loc_descr_ref reg, val;
18158 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18159 dw_die_ref cdie, tdie = NULL;
18161 next_arg = XEXP (arg, 1);
18162 if (REG_P (XEXP (XEXP (arg, 0), 0))
18163 && next_arg
18164 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18165 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18166 && REGNO (XEXP (XEXP (arg, 0), 0))
18167 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18168 next_arg = XEXP (next_arg, 1);
18169 if (mode == VOIDmode)
18171 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18172 if (mode == VOIDmode)
18173 mode = GET_MODE (XEXP (arg, 0));
18175 if (mode == VOIDmode || mode == BLKmode)
18176 continue;
18177 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18179 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18180 tloc = XEXP (XEXP (arg, 0), 1);
18181 continue;
18183 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18184 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18186 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18187 tlocc = XEXP (XEXP (arg, 0), 1);
18188 continue;
18190 reg = NULL;
18191 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18192 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18193 VAR_INIT_STATUS_INITIALIZED);
18194 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18196 rtx mem = XEXP (XEXP (arg, 0), 0);
18197 reg = mem_loc_descriptor (XEXP (mem, 0),
18198 get_address_mode (mem),
18199 GET_MODE (mem),
18200 VAR_INIT_STATUS_INITIALIZED);
18202 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18203 == DEBUG_PARAMETER_REF)
18205 tree tdecl
18206 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18207 tdie = lookup_decl_die (tdecl);
18208 if (tdie == NULL)
18209 continue;
18211 else
18212 continue;
18213 if (reg == NULL
18214 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18215 != DEBUG_PARAMETER_REF)
18216 continue;
18217 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18218 VOIDmode,
18219 VAR_INIT_STATUS_INITIALIZED);
18220 if (val == NULL)
18221 continue;
18222 if (die == NULL)
18223 die = gen_call_site_die (decl, subr_die, ca_loc);
18224 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18225 NULL_TREE);
18226 if (reg != NULL)
18227 add_AT_loc (cdie, DW_AT_location, reg);
18228 else if (tdie != NULL)
18229 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18230 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18231 if (next_arg != XEXP (arg, 1))
18233 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18234 if (mode == VOIDmode)
18235 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18236 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18237 0), 1),
18238 mode, VOIDmode,
18239 VAR_INIT_STATUS_INITIALIZED);
18240 if (val != NULL)
18241 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18244 if (die == NULL
18245 && (ca_loc->symbol_ref || tloc))
18246 die = gen_call_site_die (decl, subr_die, ca_loc);
18247 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18249 dw_loc_descr_ref tval = NULL;
18251 if (tloc != NULL_RTX)
18252 tval = mem_loc_descriptor (tloc,
18253 GET_MODE (tloc) == VOIDmode
18254 ? Pmode : GET_MODE (tloc),
18255 VOIDmode,
18256 VAR_INIT_STATUS_INITIALIZED);
18257 if (tval)
18258 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18259 else if (tlocc != NULL_RTX)
18261 tval = mem_loc_descriptor (tlocc,
18262 GET_MODE (tlocc) == VOIDmode
18263 ? Pmode : GET_MODE (tlocc),
18264 VOIDmode,
18265 VAR_INIT_STATUS_INITIALIZED);
18266 if (tval)
18267 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18268 tval);
18271 if (die != NULL)
18273 call_site_note_count++;
18274 if (ca_loc->tail_call_p)
18275 tail_call_site_note_count++;
18279 call_arg_locations = NULL;
18280 call_arg_loc_last = NULL;
18281 if (tail_call_site_count >= 0
18282 && tail_call_site_count == tail_call_site_note_count
18283 && !dwarf_strict)
18285 if (call_site_count >= 0
18286 && call_site_count == call_site_note_count)
18287 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18288 else
18289 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18291 call_site_count = -1;
18292 tail_call_site_count = -1;
18294 /* Add the calling convention attribute if requested. */
18295 add_calling_convention_attribute (subr_die, decl);
18299 /* Returns a hash value for X (which really is a die_struct). */
18301 static hashval_t
18302 common_block_die_table_hash (const void *x)
18304 const_dw_die_ref d = (const_dw_die_ref) x;
18305 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18308 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18309 as decl_id and die_parent of die_struct Y. */
18311 static int
18312 common_block_die_table_eq (const void *x, const void *y)
18314 const_dw_die_ref d = (const_dw_die_ref) x;
18315 const_dw_die_ref e = (const_dw_die_ref) y;
18316 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18319 /* Generate a DIE to represent a declared data object.
18320 Either DECL or ORIGIN must be non-null. */
18322 static void
18323 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18325 HOST_WIDE_INT off = 0;
18326 tree com_decl;
18327 tree decl_or_origin = decl ? decl : origin;
18328 tree ultimate_origin;
18329 dw_die_ref var_die;
18330 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18331 dw_die_ref origin_die;
18332 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18333 || class_or_namespace_scope_p (context_die));
18334 bool specialization_p = false;
18336 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18337 if (decl || ultimate_origin)
18338 origin = ultimate_origin;
18339 com_decl = fortran_common (decl_or_origin, &off);
18341 /* Symbol in common gets emitted as a child of the common block, in the form
18342 of a data member. */
18343 if (com_decl)
18345 dw_die_ref com_die;
18346 dw_loc_list_ref loc;
18347 die_node com_die_arg;
18349 var_die = lookup_decl_die (decl_or_origin);
18350 if (var_die)
18352 if (get_AT (var_die, DW_AT_location) == NULL)
18354 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18355 if (loc)
18357 if (off)
18359 /* Optimize the common case. */
18360 if (single_element_loc_list_p (loc)
18361 && loc->expr->dw_loc_opc == DW_OP_addr
18362 && loc->expr->dw_loc_next == NULL
18363 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18364 == SYMBOL_REF)
18366 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18367 loc->expr->dw_loc_oprnd1.v.val_addr
18368 = plus_constant (GET_MODE (x), x , off);
18370 else
18371 loc_list_plus_const (loc, off);
18373 add_AT_location_description (var_die, DW_AT_location, loc);
18374 remove_AT (var_die, DW_AT_declaration);
18377 return;
18380 if (common_block_die_table == NULL)
18381 common_block_die_table
18382 = htab_create_ggc (10, common_block_die_table_hash,
18383 common_block_die_table_eq, NULL);
18385 com_die_arg.decl_id = DECL_UID (com_decl);
18386 com_die_arg.die_parent = context_die;
18387 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18388 loc = loc_list_from_tree (com_decl, 2);
18389 if (com_die == NULL)
18391 const char *cnam
18392 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18393 void **slot;
18395 com_die = new_die (DW_TAG_common_block, context_die, decl);
18396 add_name_and_src_coords_attributes (com_die, com_decl);
18397 if (loc)
18399 add_AT_location_description (com_die, DW_AT_location, loc);
18400 /* Avoid sharing the same loc descriptor between
18401 DW_TAG_common_block and DW_TAG_variable. */
18402 loc = loc_list_from_tree (com_decl, 2);
18404 else if (DECL_EXTERNAL (decl))
18405 add_AT_flag (com_die, DW_AT_declaration, 1);
18406 if (want_pubnames ())
18407 add_pubname_string (cnam, com_die); /* ??? needed? */
18408 com_die->decl_id = DECL_UID (com_decl);
18409 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18410 *slot = (void *) com_die;
18412 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18414 add_AT_location_description (com_die, DW_AT_location, loc);
18415 loc = loc_list_from_tree (com_decl, 2);
18416 remove_AT (com_die, DW_AT_declaration);
18418 var_die = new_die (DW_TAG_variable, com_die, decl);
18419 add_name_and_src_coords_attributes (var_die, decl);
18420 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18421 TREE_THIS_VOLATILE (decl), context_die);
18422 add_AT_flag (var_die, DW_AT_external, 1);
18423 if (loc)
18425 if (off)
18427 /* Optimize the common case. */
18428 if (single_element_loc_list_p (loc)
18429 && loc->expr->dw_loc_opc == DW_OP_addr
18430 && loc->expr->dw_loc_next == NULL
18431 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18433 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18434 loc->expr->dw_loc_oprnd1.v.val_addr
18435 = plus_constant (GET_MODE (x), x, off);
18437 else
18438 loc_list_plus_const (loc, off);
18440 add_AT_location_description (var_die, DW_AT_location, loc);
18442 else if (DECL_EXTERNAL (decl))
18443 add_AT_flag (var_die, DW_AT_declaration, 1);
18444 equate_decl_number_to_die (decl, var_die);
18445 return;
18448 /* If the compiler emitted a definition for the DECL declaration
18449 and if we already emitted a DIE for it, don't emit a second
18450 DIE for it again. Allow re-declarations of DECLs that are
18451 inside functions, though. */
18452 if (old_die && declaration && !local_scope_p (context_die))
18453 return;
18455 /* For static data members, the declaration in the class is supposed
18456 to have DW_TAG_member tag; the specification should still be
18457 DW_TAG_variable referencing the DW_TAG_member DIE. */
18458 if (declaration && class_scope_p (context_die))
18459 var_die = new_die (DW_TAG_member, context_die, decl);
18460 else
18461 var_die = new_die (DW_TAG_variable, context_die, decl);
18463 origin_die = NULL;
18464 if (origin != NULL)
18465 origin_die = add_abstract_origin_attribute (var_die, origin);
18467 /* Loop unrolling can create multiple blocks that refer to the same
18468 static variable, so we must test for the DW_AT_declaration flag.
18470 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18471 copy decls and set the DECL_ABSTRACT flag on them instead of
18472 sharing them.
18474 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18476 ??? The declare_in_namespace support causes us to get two DIEs for one
18477 variable, both of which are declarations. We want to avoid considering
18478 one to be a specification, so we must test that this DIE is not a
18479 declaration. */
18480 else if (old_die && TREE_STATIC (decl) && ! declaration
18481 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18483 /* This is a definition of a C++ class level static. */
18484 add_AT_specification (var_die, old_die);
18485 specialization_p = true;
18486 if (DECL_NAME (decl))
18488 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18489 struct dwarf_file_data * file_index = lookup_filename (s.file);
18491 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18492 add_AT_file (var_die, DW_AT_decl_file, file_index);
18494 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18495 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18497 if (old_die->die_tag == DW_TAG_member)
18498 add_linkage_name (var_die, decl);
18501 else
18502 add_name_and_src_coords_attributes (var_die, decl);
18504 if ((origin == NULL && !specialization_p)
18505 || (origin != NULL
18506 && !DECL_ABSTRACT (decl_or_origin)
18507 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18508 decl_function_context
18509 (decl_or_origin))))
18511 tree type = TREE_TYPE (decl_or_origin);
18513 if (decl_by_reference_p (decl_or_origin))
18514 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18515 else
18516 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18517 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18520 if (origin == NULL && !specialization_p)
18522 if (TREE_PUBLIC (decl))
18523 add_AT_flag (var_die, DW_AT_external, 1);
18525 if (DECL_ARTIFICIAL (decl))
18526 add_AT_flag (var_die, DW_AT_artificial, 1);
18528 add_accessibility_attribute (var_die, decl);
18531 if (declaration)
18532 add_AT_flag (var_die, DW_AT_declaration, 1);
18534 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18535 equate_decl_number_to_die (decl, var_die);
18537 if (! declaration
18538 && (! DECL_ABSTRACT (decl_or_origin)
18539 /* Local static vars are shared between all clones/inlines,
18540 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18541 already set. */
18542 || (TREE_CODE (decl_or_origin) == VAR_DECL
18543 && TREE_STATIC (decl_or_origin)
18544 && DECL_RTL_SET_P (decl_or_origin)))
18545 /* When abstract origin already has DW_AT_location attribute, no need
18546 to add it again. */
18547 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18549 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18550 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18551 defer_location (decl_or_origin, var_die);
18552 else
18553 add_location_or_const_value_attribute (var_die, decl_or_origin,
18554 decl == NULL, DW_AT_location);
18555 add_pubname (decl_or_origin, var_die);
18557 else
18558 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18561 /* Generate a DIE to represent a named constant. */
18563 static void
18564 gen_const_die (tree decl, dw_die_ref context_die)
18566 dw_die_ref const_die;
18567 tree type = TREE_TYPE (decl);
18569 const_die = new_die (DW_TAG_constant, context_die, decl);
18570 add_name_and_src_coords_attributes (const_die, decl);
18571 add_type_attribute (const_die, type, 1, 0, context_die);
18572 if (TREE_PUBLIC (decl))
18573 add_AT_flag (const_die, DW_AT_external, 1);
18574 if (DECL_ARTIFICIAL (decl))
18575 add_AT_flag (const_die, DW_AT_artificial, 1);
18576 tree_add_const_value_attribute_for_decl (const_die, decl);
18579 /* Generate a DIE to represent a label identifier. */
18581 static void
18582 gen_label_die (tree decl, dw_die_ref context_die)
18584 tree origin = decl_ultimate_origin (decl);
18585 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18586 rtx insn;
18587 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18589 if (origin != NULL)
18590 add_abstract_origin_attribute (lbl_die, origin);
18591 else
18592 add_name_and_src_coords_attributes (lbl_die, decl);
18594 if (DECL_ABSTRACT (decl))
18595 equate_decl_number_to_die (decl, lbl_die);
18596 else
18598 insn = DECL_RTL_IF_SET (decl);
18600 /* Deleted labels are programmer specified labels which have been
18601 eliminated because of various optimizations. We still emit them
18602 here so that it is possible to put breakpoints on them. */
18603 if (insn
18604 && (LABEL_P (insn)
18605 || ((NOTE_P (insn)
18606 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18608 /* When optimization is enabled (via -O) some parts of the compiler
18609 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18610 represent source-level labels which were explicitly declared by
18611 the user. This really shouldn't be happening though, so catch
18612 it if it ever does happen. */
18613 gcc_assert (!INSN_DELETED_P (insn));
18615 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18616 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18618 else if (insn
18619 && NOTE_P (insn)
18620 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18621 && CODE_LABEL_NUMBER (insn) != -1)
18623 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18624 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18629 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18630 attributes to the DIE for a block STMT, to describe where the inlined
18631 function was called from. This is similar to add_src_coords_attributes. */
18633 static inline void
18634 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18636 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18638 if (dwarf_version >= 3 || !dwarf_strict)
18640 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18641 add_AT_unsigned (die, DW_AT_call_line, s.line);
18646 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18647 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18649 static inline void
18650 add_high_low_attributes (tree stmt, dw_die_ref die)
18652 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18654 if (BLOCK_FRAGMENT_CHAIN (stmt)
18655 && (dwarf_version >= 3 || !dwarf_strict))
18657 tree chain, superblock = NULL_TREE;
18658 dw_die_ref pdie;
18659 dw_attr_ref attr = NULL;
18661 if (inlined_function_outer_scope_p (stmt))
18663 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18664 BLOCK_NUMBER (stmt));
18665 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18668 /* Optimize duplicate .debug_ranges lists or even tails of
18669 lists. If this BLOCK has same ranges as its supercontext,
18670 lookup DW_AT_ranges attribute in the supercontext (and
18671 recursively so), verify that the ranges_table contains the
18672 right values and use it instead of adding a new .debug_range. */
18673 for (chain = stmt, pdie = die;
18674 BLOCK_SAME_RANGE (chain);
18675 chain = BLOCK_SUPERCONTEXT (chain))
18677 dw_attr_ref new_attr;
18679 pdie = pdie->die_parent;
18680 if (pdie == NULL)
18681 break;
18682 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18683 break;
18684 new_attr = get_AT (pdie, DW_AT_ranges);
18685 if (new_attr == NULL
18686 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18687 break;
18688 attr = new_attr;
18689 superblock = BLOCK_SUPERCONTEXT (chain);
18691 if (attr != NULL
18692 && (ranges_table[attr->dw_attr_val.v.val_offset
18693 / 2 / DWARF2_ADDR_SIZE].num
18694 == BLOCK_NUMBER (superblock))
18695 && BLOCK_FRAGMENT_CHAIN (superblock))
18697 unsigned long off = attr->dw_attr_val.v.val_offset
18698 / 2 / DWARF2_ADDR_SIZE;
18699 unsigned long supercnt = 0, thiscnt = 0;
18700 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18701 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18703 ++supercnt;
18704 gcc_checking_assert (ranges_table[off + supercnt].num
18705 == BLOCK_NUMBER (chain));
18707 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18708 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18709 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18710 ++thiscnt;
18711 gcc_assert (supercnt >= thiscnt);
18712 add_AT_range_list (die, DW_AT_ranges,
18713 ((off + supercnt - thiscnt)
18714 * 2 * DWARF2_ADDR_SIZE),
18715 false);
18716 return;
18719 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18721 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18724 add_ranges (chain);
18725 chain = BLOCK_FRAGMENT_CHAIN (chain);
18727 while (chain);
18728 add_ranges (NULL);
18730 else
18732 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18733 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18734 BLOCK_NUMBER (stmt));
18735 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18736 BLOCK_NUMBER (stmt));
18737 add_AT_low_high_pc (die, label, label_high, false);
18741 /* Generate a DIE for a lexical block. */
18743 static void
18744 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18746 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18748 if (call_arg_locations)
18750 if (block_map.length () <= BLOCK_NUMBER (stmt))
18751 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18752 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18755 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18756 add_high_low_attributes (stmt, stmt_die);
18758 decls_for_scope (stmt, stmt_die, depth);
18761 /* Generate a DIE for an inlined subprogram. */
18763 static void
18764 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18766 tree decl;
18768 /* The instance of function that is effectively being inlined shall not
18769 be abstract. */
18770 gcc_assert (! BLOCK_ABSTRACT (stmt));
18772 decl = block_ultimate_origin (stmt);
18774 /* Emit info for the abstract instance first, if we haven't yet. We
18775 must emit this even if the block is abstract, otherwise when we
18776 emit the block below (or elsewhere), we may end up trying to emit
18777 a die whose origin die hasn't been emitted, and crashing. */
18778 dwarf2out_abstract_function (decl);
18780 if (! BLOCK_ABSTRACT (stmt))
18782 dw_die_ref subr_die
18783 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18785 if (call_arg_locations)
18787 if (block_map.length () <= BLOCK_NUMBER (stmt))
18788 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18789 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18791 add_abstract_origin_attribute (subr_die, decl);
18792 if (TREE_ASM_WRITTEN (stmt))
18793 add_high_low_attributes (stmt, subr_die);
18794 add_call_src_coords_attributes (stmt, subr_die);
18796 decls_for_scope (stmt, subr_die, depth);
18797 current_function_has_inlines = 1;
18801 /* Generate a DIE for a field in a record, or structure. */
18803 static void
18804 gen_field_die (tree decl, dw_die_ref context_die)
18806 dw_die_ref decl_die;
18808 if (TREE_TYPE (decl) == error_mark_node)
18809 return;
18811 decl_die = new_die (DW_TAG_member, context_die, decl);
18812 add_name_and_src_coords_attributes (decl_die, decl);
18813 add_type_attribute (decl_die, member_declared_type (decl),
18814 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18815 context_die);
18817 if (DECL_BIT_FIELD_TYPE (decl))
18819 add_byte_size_attribute (decl_die, decl);
18820 add_bit_size_attribute (decl_die, decl);
18821 add_bit_offset_attribute (decl_die, decl);
18824 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18825 add_data_member_location_attribute (decl_die, decl);
18827 if (DECL_ARTIFICIAL (decl))
18828 add_AT_flag (decl_die, DW_AT_artificial, 1);
18830 add_accessibility_attribute (decl_die, decl);
18832 /* Equate decl number to die, so that we can look up this decl later on. */
18833 equate_decl_number_to_die (decl, decl_die);
18836 #if 0
18837 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18838 Use modified_type_die instead.
18839 We keep this code here just in case these types of DIEs may be needed to
18840 represent certain things in other languages (e.g. Pascal) someday. */
18842 static void
18843 gen_pointer_type_die (tree type, dw_die_ref context_die)
18845 dw_die_ref ptr_die
18846 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18848 equate_type_number_to_die (type, ptr_die);
18849 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18850 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18853 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18854 Use modified_type_die instead.
18855 We keep this code here just in case these types of DIEs may be needed to
18856 represent certain things in other languages (e.g. Pascal) someday. */
18858 static void
18859 gen_reference_type_die (tree type, dw_die_ref context_die)
18861 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18863 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18864 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18865 else
18866 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18868 equate_type_number_to_die (type, ref_die);
18869 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18870 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18872 #endif
18874 /* Generate a DIE for a pointer to a member type. */
18876 static void
18877 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18879 dw_die_ref ptr_die
18880 = new_die (DW_TAG_ptr_to_member_type,
18881 scope_die_for (type, context_die), type);
18883 equate_type_number_to_die (type, ptr_die);
18884 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18885 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18886 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18889 typedef const char *dchar_p; /* For DEF_VEC_P. */
18891 static char *producer_string;
18893 /* Return a heap allocated producer string including command line options
18894 if -grecord-gcc-switches. */
18896 static char *
18897 gen_producer_string (void)
18899 size_t j;
18900 vec<dchar_p> switches = vNULL;
18901 const char *language_string = lang_hooks.name;
18902 char *producer, *tail;
18903 const char *p;
18904 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18905 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18907 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18908 switch (save_decoded_options[j].opt_index)
18910 case OPT_o:
18911 case OPT_d:
18912 case OPT_dumpbase:
18913 case OPT_dumpdir:
18914 case OPT_auxbase:
18915 case OPT_auxbase_strip:
18916 case OPT_quiet:
18917 case OPT_version:
18918 case OPT_v:
18919 case OPT_w:
18920 case OPT_L:
18921 case OPT_D:
18922 case OPT_I:
18923 case OPT_U:
18924 case OPT_SPECIAL_unknown:
18925 case OPT_SPECIAL_ignore:
18926 case OPT_SPECIAL_program_name:
18927 case OPT_SPECIAL_input_file:
18928 case OPT_grecord_gcc_switches:
18929 case OPT_gno_record_gcc_switches:
18930 case OPT__output_pch_:
18931 case OPT_fdiagnostics_show_location_:
18932 case OPT_fdiagnostics_show_option:
18933 case OPT_fdiagnostics_show_caret:
18934 case OPT_fdiagnostics_color_:
18935 case OPT_fverbose_asm:
18936 case OPT____:
18937 case OPT__sysroot_:
18938 case OPT_nostdinc:
18939 case OPT_nostdinc__:
18940 /* Ignore these. */
18941 continue;
18942 default:
18943 if (cl_options[save_decoded_options[j].opt_index].flags
18944 & CL_NO_DWARF_RECORD)
18945 continue;
18946 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18947 == '-');
18948 switch (save_decoded_options[j].canonical_option[0][1])
18950 case 'M':
18951 case 'i':
18952 case 'W':
18953 continue;
18954 case 'f':
18955 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18956 "dump", 4) == 0)
18957 continue;
18958 break;
18959 default:
18960 break;
18962 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18963 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18964 break;
18967 producer = XNEWVEC (char, plen + 1 + len + 1);
18968 tail = producer;
18969 sprintf (tail, "%s %s", language_string, version_string);
18970 tail += plen;
18972 FOR_EACH_VEC_ELT (switches, j, p)
18974 len = strlen (p);
18975 *tail = ' ';
18976 memcpy (tail + 1, p, len);
18977 tail += len + 1;
18980 *tail = '\0';
18981 switches.release ();
18982 return producer;
18985 /* Generate the DIE for the compilation unit. */
18987 static dw_die_ref
18988 gen_compile_unit_die (const char *filename)
18990 dw_die_ref die;
18991 const char *language_string = lang_hooks.name;
18992 int language;
18994 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18996 if (filename)
18998 add_name_attribute (die, filename);
18999 /* Don't add cwd for <built-in>. */
19000 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19001 add_comp_dir_attribute (die);
19004 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19006 /* If our producer is LTO try to figure out a common language to use
19007 from the global list of translation units. */
19008 if (strcmp (language_string, "GNU GIMPLE") == 0)
19010 unsigned i;
19011 tree t;
19012 const char *common_lang = NULL;
19014 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19016 if (!TRANSLATION_UNIT_LANGUAGE (t))
19017 continue;
19018 if (!common_lang)
19019 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19020 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19022 else if (strncmp (common_lang, "GNU C", 5) == 0
19023 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19024 /* Mixing C and C++ is ok, use C++ in that case. */
19025 common_lang = "GNU C++";
19026 else
19028 /* Fall back to C. */
19029 common_lang = NULL;
19030 break;
19034 if (common_lang)
19035 language_string = common_lang;
19038 language = DW_LANG_C89;
19039 if (strcmp (language_string, "GNU C++") == 0)
19040 language = DW_LANG_C_plus_plus;
19041 else if (strcmp (language_string, "GNU F77") == 0)
19042 language = DW_LANG_Fortran77;
19043 else if (strcmp (language_string, "GNU Pascal") == 0)
19044 language = DW_LANG_Pascal83;
19045 else if (dwarf_version >= 3 || !dwarf_strict)
19047 if (strcmp (language_string, "GNU Ada") == 0)
19048 language = DW_LANG_Ada95;
19049 else if (strcmp (language_string, "GNU Fortran") == 0)
19050 language = DW_LANG_Fortran95;
19051 else if (strcmp (language_string, "GNU Java") == 0)
19052 language = DW_LANG_Java;
19053 else if (strcmp (language_string, "GNU Objective-C") == 0)
19054 language = DW_LANG_ObjC;
19055 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19056 language = DW_LANG_ObjC_plus_plus;
19057 else if (dwarf_version >= 5 || !dwarf_strict)
19059 if (strcmp (language_string, "GNU Go") == 0)
19060 language = DW_LANG_Go;
19063 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19064 else if (strcmp (language_string, "GNU Fortran") == 0)
19065 language = DW_LANG_Fortran90;
19067 add_AT_unsigned (die, DW_AT_language, language);
19069 switch (language)
19071 case DW_LANG_Fortran77:
19072 case DW_LANG_Fortran90:
19073 case DW_LANG_Fortran95:
19074 /* Fortran has case insensitive identifiers and the front-end
19075 lowercases everything. */
19076 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19077 break;
19078 default:
19079 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19080 break;
19082 return die;
19085 /* Generate the DIE for a base class. */
19087 static void
19088 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19090 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19092 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19093 add_data_member_location_attribute (die, binfo);
19095 if (BINFO_VIRTUAL_P (binfo))
19096 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19098 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19099 children, otherwise the default is DW_ACCESS_public. In DWARF2
19100 the default has always been DW_ACCESS_private. */
19101 if (access == access_public_node)
19103 if (dwarf_version == 2
19104 || context_die->die_tag == DW_TAG_class_type)
19105 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19107 else if (access == access_protected_node)
19108 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19109 else if (dwarf_version > 2
19110 && context_die->die_tag != DW_TAG_class_type)
19111 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19114 /* Generate a DIE for a class member. */
19116 static void
19117 gen_member_die (tree type, dw_die_ref context_die)
19119 tree member;
19120 tree binfo = TYPE_BINFO (type);
19121 dw_die_ref child;
19123 /* If this is not an incomplete type, output descriptions of each of its
19124 members. Note that as we output the DIEs necessary to represent the
19125 members of this record or union type, we will also be trying to output
19126 DIEs to represent the *types* of those members. However the `type'
19127 function (above) will specifically avoid generating type DIEs for member
19128 types *within* the list of member DIEs for this (containing) type except
19129 for those types (of members) which are explicitly marked as also being
19130 members of this (containing) type themselves. The g++ front- end can
19131 force any given type to be treated as a member of some other (containing)
19132 type by setting the TYPE_CONTEXT of the given (member) type to point to
19133 the TREE node representing the appropriate (containing) type. */
19135 /* First output info about the base classes. */
19136 if (binfo)
19138 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19139 int i;
19140 tree base;
19142 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19143 gen_inheritance_die (base,
19144 (accesses ? (*accesses)[i] : access_public_node),
19145 context_die);
19148 /* Now output info about the data members and type members. */
19149 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19151 /* If we thought we were generating minimal debug info for TYPE
19152 and then changed our minds, some of the member declarations
19153 may have already been defined. Don't define them again, but
19154 do put them in the right order. */
19156 child = lookup_decl_die (member);
19157 if (child)
19158 splice_child_die (context_die, child);
19159 else
19160 gen_decl_die (member, NULL, context_die);
19163 /* Now output info about the function members (if any). */
19164 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19166 /* Don't include clones in the member list. */
19167 if (DECL_ABSTRACT_ORIGIN (member))
19168 continue;
19170 child = lookup_decl_die (member);
19171 if (child)
19172 splice_child_die (context_die, child);
19173 else
19174 gen_decl_die (member, NULL, context_die);
19178 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19179 is set, we pretend that the type was never defined, so we only get the
19180 member DIEs needed by later specification DIEs. */
19182 static void
19183 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19184 enum debug_info_usage usage)
19186 dw_die_ref type_die = lookup_type_die (type);
19187 dw_die_ref scope_die = 0;
19188 int nested = 0;
19189 int complete = (TYPE_SIZE (type)
19190 && (! TYPE_STUB_DECL (type)
19191 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19192 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19193 complete = complete && should_emit_struct_debug (type, usage);
19195 if (type_die && ! complete)
19196 return;
19198 if (TYPE_CONTEXT (type) != NULL_TREE
19199 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19200 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19201 nested = 1;
19203 scope_die = scope_die_for (type, context_die);
19205 /* Generate child dies for template paramaters. */
19206 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19207 schedule_generic_params_dies_gen (type);
19209 if (! type_die || (nested && is_cu_die (scope_die)))
19210 /* First occurrence of type or toplevel definition of nested class. */
19212 dw_die_ref old_die = type_die;
19214 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19215 ? record_type_tag (type) : DW_TAG_union_type,
19216 scope_die, type);
19217 equate_type_number_to_die (type, type_die);
19218 if (old_die)
19219 add_AT_specification (type_die, old_die);
19220 else
19221 add_name_attribute (type_die, type_tag (type));
19223 else
19224 remove_AT (type_die, DW_AT_declaration);
19226 /* If this type has been completed, then give it a byte_size attribute and
19227 then give a list of members. */
19228 if (complete && !ns_decl)
19230 /* Prevent infinite recursion in cases where the type of some member of
19231 this type is expressed in terms of this type itself. */
19232 TREE_ASM_WRITTEN (type) = 1;
19233 add_byte_size_attribute (type_die, type);
19234 if (TYPE_STUB_DECL (type) != NULL_TREE)
19236 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19237 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19240 /* If the first reference to this type was as the return type of an
19241 inline function, then it may not have a parent. Fix this now. */
19242 if (type_die->die_parent == NULL)
19243 add_child_die (scope_die, type_die);
19245 push_decl_scope (type);
19246 gen_member_die (type, type_die);
19247 pop_decl_scope ();
19249 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19250 if (TYPE_ARTIFICIAL (type))
19251 add_AT_flag (type_die, DW_AT_artificial, 1);
19253 /* GNU extension: Record what type our vtable lives in. */
19254 if (TYPE_VFIELD (type))
19256 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19258 gen_type_die (vtype, context_die);
19259 add_AT_die_ref (type_die, DW_AT_containing_type,
19260 lookup_type_die (vtype));
19263 else
19265 add_AT_flag (type_die, DW_AT_declaration, 1);
19267 /* We don't need to do this for function-local types. */
19268 if (TYPE_STUB_DECL (type)
19269 && ! decl_function_context (TYPE_STUB_DECL (type)))
19270 vec_safe_push (incomplete_types, type);
19273 if (get_AT (type_die, DW_AT_name))
19274 add_pubtype (type, type_die);
19277 /* Generate a DIE for a subroutine _type_. */
19279 static void
19280 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19282 tree return_type = TREE_TYPE (type);
19283 dw_die_ref subr_die
19284 = new_die (DW_TAG_subroutine_type,
19285 scope_die_for (type, context_die), type);
19287 equate_type_number_to_die (type, subr_die);
19288 add_prototyped_attribute (subr_die, type);
19289 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19290 gen_formal_types_die (type, subr_die);
19292 if (get_AT (subr_die, DW_AT_name))
19293 add_pubtype (type, subr_die);
19296 /* Generate a DIE for a type definition. */
19298 static void
19299 gen_typedef_die (tree decl, dw_die_ref context_die)
19301 dw_die_ref type_die;
19302 tree origin;
19304 if (TREE_ASM_WRITTEN (decl))
19305 return;
19307 TREE_ASM_WRITTEN (decl) = 1;
19308 type_die = new_die (DW_TAG_typedef, context_die, decl);
19309 origin = decl_ultimate_origin (decl);
19310 if (origin != NULL)
19311 add_abstract_origin_attribute (type_die, origin);
19312 else
19314 tree type;
19316 add_name_and_src_coords_attributes (type_die, decl);
19317 if (DECL_ORIGINAL_TYPE (decl))
19319 type = DECL_ORIGINAL_TYPE (decl);
19321 gcc_assert (type != TREE_TYPE (decl));
19322 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19324 else
19326 type = TREE_TYPE (decl);
19328 if (is_naming_typedef_decl (TYPE_NAME (type)))
19330 /* Here, we are in the case of decl being a typedef naming
19331 an anonymous type, e.g:
19332 typedef struct {...} foo;
19333 In that case TREE_TYPE (decl) is not a typedef variant
19334 type and TYPE_NAME of the anonymous type is set to the
19335 TYPE_DECL of the typedef. This construct is emitted by
19336 the C++ FE.
19338 TYPE is the anonymous struct named by the typedef
19339 DECL. As we need the DW_AT_type attribute of the
19340 DW_TAG_typedef to point to the DIE of TYPE, let's
19341 generate that DIE right away. add_type_attribute
19342 called below will then pick (via lookup_type_die) that
19343 anonymous struct DIE. */
19344 if (!TREE_ASM_WRITTEN (type))
19345 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19347 /* This is a GNU Extension. We are adding a
19348 DW_AT_linkage_name attribute to the DIE of the
19349 anonymous struct TYPE. The value of that attribute
19350 is the name of the typedef decl naming the anonymous
19351 struct. This greatly eases the work of consumers of
19352 this debug info. */
19353 add_linkage_attr (lookup_type_die (type), decl);
19357 add_type_attribute (type_die, type, TREE_READONLY (decl),
19358 TREE_THIS_VOLATILE (decl), context_die);
19360 if (is_naming_typedef_decl (decl))
19361 /* We want that all subsequent calls to lookup_type_die with
19362 TYPE in argument yield the DW_TAG_typedef we have just
19363 created. */
19364 equate_type_number_to_die (type, type_die);
19366 add_accessibility_attribute (type_die, decl);
19369 if (DECL_ABSTRACT (decl))
19370 equate_decl_number_to_die (decl, type_die);
19372 if (get_AT (type_die, DW_AT_name))
19373 add_pubtype (decl, type_die);
19376 /* Generate a DIE for a struct, class, enum or union type. */
19378 static void
19379 gen_tagged_type_die (tree type,
19380 dw_die_ref context_die,
19381 enum debug_info_usage usage)
19383 int need_pop;
19385 if (type == NULL_TREE
19386 || !is_tagged_type (type))
19387 return;
19389 /* If this is a nested type whose containing class hasn't been written
19390 out yet, writing it out will cover this one, too. This does not apply
19391 to instantiations of member class templates; they need to be added to
19392 the containing class as they are generated. FIXME: This hurts the
19393 idea of combining type decls from multiple TUs, since we can't predict
19394 what set of template instantiations we'll get. */
19395 if (TYPE_CONTEXT (type)
19396 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19397 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19399 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19401 if (TREE_ASM_WRITTEN (type))
19402 return;
19404 /* If that failed, attach ourselves to the stub. */
19405 push_decl_scope (TYPE_CONTEXT (type));
19406 context_die = lookup_type_die (TYPE_CONTEXT (type));
19407 need_pop = 1;
19409 else if (TYPE_CONTEXT (type) != NULL_TREE
19410 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19412 /* If this type is local to a function that hasn't been written
19413 out yet, use a NULL context for now; it will be fixed up in
19414 decls_for_scope. */
19415 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19416 /* A declaration DIE doesn't count; nested types need to go in the
19417 specification. */
19418 if (context_die && is_declaration_die (context_die))
19419 context_die = NULL;
19420 need_pop = 0;
19422 else
19424 context_die = declare_in_namespace (type, context_die);
19425 need_pop = 0;
19428 if (TREE_CODE (type) == ENUMERAL_TYPE)
19430 /* This might have been written out by the call to
19431 declare_in_namespace. */
19432 if (!TREE_ASM_WRITTEN (type))
19433 gen_enumeration_type_die (type, context_die);
19435 else
19436 gen_struct_or_union_type_die (type, context_die, usage);
19438 if (need_pop)
19439 pop_decl_scope ();
19441 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19442 it up if it is ever completed. gen_*_type_die will set it for us
19443 when appropriate. */
19446 /* Generate a type description DIE. */
19448 static void
19449 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19450 enum debug_info_usage usage)
19452 struct array_descr_info info;
19454 if (type == NULL_TREE || type == error_mark_node)
19455 return;
19457 if (TYPE_NAME (type) != NULL_TREE
19458 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19459 && is_redundant_typedef (TYPE_NAME (type))
19460 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19461 /* The DECL of this type is a typedef we don't want to emit debug
19462 info for but we want debug info for its underlying typedef.
19463 This can happen for e.g, the injected-class-name of a C++
19464 type. */
19465 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19467 /* If TYPE is a typedef type variant, let's generate debug info
19468 for the parent typedef which TYPE is a type of. */
19469 if (typedef_variant_p (type))
19471 if (TREE_ASM_WRITTEN (type))
19472 return;
19474 /* Prevent broken recursion; we can't hand off to the same type. */
19475 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19477 /* Give typedefs the right scope. */
19478 context_die = scope_die_for (type, context_die);
19480 TREE_ASM_WRITTEN (type) = 1;
19482 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19483 return;
19486 /* If type is an anonymous tagged type named by a typedef, let's
19487 generate debug info for the typedef. */
19488 if (is_naming_typedef_decl (TYPE_NAME (type)))
19490 /* Use the DIE of the containing namespace as the parent DIE of
19491 the type description DIE we want to generate. */
19492 if (DECL_CONTEXT (TYPE_NAME (type))
19493 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19494 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19496 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19497 return;
19500 /* If this is an array type with hidden descriptor, handle it first. */
19501 if (!TREE_ASM_WRITTEN (type)
19502 && lang_hooks.types.get_array_descr_info
19503 && lang_hooks.types.get_array_descr_info (type, &info)
19504 && (dwarf_version >= 3 || !dwarf_strict))
19506 gen_descr_array_type_die (type, &info, context_die);
19507 TREE_ASM_WRITTEN (type) = 1;
19508 return;
19511 /* We are going to output a DIE to represent the unqualified version
19512 of this type (i.e. without any const or volatile qualifiers) so
19513 get the main variant (i.e. the unqualified version) of this type
19514 now. (Vectors are special because the debugging info is in the
19515 cloned type itself). */
19516 if (TREE_CODE (type) != VECTOR_TYPE)
19517 type = type_main_variant (type);
19519 if (TREE_ASM_WRITTEN (type))
19520 return;
19522 switch (TREE_CODE (type))
19524 case ERROR_MARK:
19525 break;
19527 case POINTER_TYPE:
19528 case REFERENCE_TYPE:
19529 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19530 ensures that the gen_type_die recursion will terminate even if the
19531 type is recursive. Recursive types are possible in Ada. */
19532 /* ??? We could perhaps do this for all types before the switch
19533 statement. */
19534 TREE_ASM_WRITTEN (type) = 1;
19536 /* For these types, all that is required is that we output a DIE (or a
19537 set of DIEs) to represent the "basis" type. */
19538 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19539 DINFO_USAGE_IND_USE);
19540 break;
19542 case OFFSET_TYPE:
19543 /* This code is used for C++ pointer-to-data-member types.
19544 Output a description of the relevant class type. */
19545 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19546 DINFO_USAGE_IND_USE);
19548 /* Output a description of the type of the object pointed to. */
19549 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19550 DINFO_USAGE_IND_USE);
19552 /* Now output a DIE to represent this pointer-to-data-member type
19553 itself. */
19554 gen_ptr_to_mbr_type_die (type, context_die);
19555 break;
19557 case FUNCTION_TYPE:
19558 /* Force out return type (in case it wasn't forced out already). */
19559 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19560 DINFO_USAGE_DIR_USE);
19561 gen_subroutine_type_die (type, context_die);
19562 break;
19564 case METHOD_TYPE:
19565 /* Force out return type (in case it wasn't forced out already). */
19566 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19567 DINFO_USAGE_DIR_USE);
19568 gen_subroutine_type_die (type, context_die);
19569 break;
19571 case ARRAY_TYPE:
19572 gen_array_type_die (type, context_die);
19573 break;
19575 case VECTOR_TYPE:
19576 gen_array_type_die (type, context_die);
19577 break;
19579 case ENUMERAL_TYPE:
19580 case RECORD_TYPE:
19581 case UNION_TYPE:
19582 case QUAL_UNION_TYPE:
19583 gen_tagged_type_die (type, context_die, usage);
19584 return;
19586 case VOID_TYPE:
19587 case INTEGER_TYPE:
19588 case REAL_TYPE:
19589 case FIXED_POINT_TYPE:
19590 case COMPLEX_TYPE:
19591 case BOOLEAN_TYPE:
19592 /* No DIEs needed for fundamental types. */
19593 break;
19595 case NULLPTR_TYPE:
19596 case LANG_TYPE:
19597 /* Just use DW_TAG_unspecified_type. */
19599 dw_die_ref type_die = lookup_type_die (type);
19600 if (type_die == NULL)
19602 tree name = TYPE_NAME (type);
19603 if (TREE_CODE (name) == TYPE_DECL)
19604 name = DECL_NAME (name);
19605 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19606 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19607 equate_type_number_to_die (type, type_die);
19610 break;
19612 default:
19613 gcc_unreachable ();
19616 TREE_ASM_WRITTEN (type) = 1;
19619 static void
19620 gen_type_die (tree type, dw_die_ref context_die)
19622 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19625 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19626 things which are local to the given block. */
19628 static void
19629 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19631 int must_output_die = 0;
19632 bool inlined_func;
19634 /* Ignore blocks that are NULL. */
19635 if (stmt == NULL_TREE)
19636 return;
19638 inlined_func = inlined_function_outer_scope_p (stmt);
19640 /* If the block is one fragment of a non-contiguous block, do not
19641 process the variables, since they will have been done by the
19642 origin block. Do process subblocks. */
19643 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19645 tree sub;
19647 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19648 gen_block_die (sub, context_die, depth + 1);
19650 return;
19653 /* Determine if we need to output any Dwarf DIEs at all to represent this
19654 block. */
19655 if (inlined_func)
19656 /* The outer scopes for inlinings *must* always be represented. We
19657 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19658 must_output_die = 1;
19659 else
19661 /* Determine if this block directly contains any "significant"
19662 local declarations which we will need to output DIEs for. */
19663 if (debug_info_level > DINFO_LEVEL_TERSE)
19664 /* We are not in terse mode so *any* local declaration counts
19665 as being a "significant" one. */
19666 must_output_die = ((BLOCK_VARS (stmt) != NULL
19667 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19668 && (TREE_USED (stmt)
19669 || TREE_ASM_WRITTEN (stmt)
19670 || BLOCK_ABSTRACT (stmt)));
19671 else if ((TREE_USED (stmt)
19672 || TREE_ASM_WRITTEN (stmt)
19673 || BLOCK_ABSTRACT (stmt))
19674 && !dwarf2out_ignore_block (stmt))
19675 must_output_die = 1;
19678 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19679 DIE for any block which contains no significant local declarations at
19680 all. Rather, in such cases we just call `decls_for_scope' so that any
19681 needed Dwarf info for any sub-blocks will get properly generated. Note
19682 that in terse mode, our definition of what constitutes a "significant"
19683 local declaration gets restricted to include only inlined function
19684 instances and local (nested) function definitions. */
19685 if (must_output_die)
19687 if (inlined_func)
19689 /* If STMT block is abstract, that means we have been called
19690 indirectly from dwarf2out_abstract_function.
19691 That function rightfully marks the descendent blocks (of
19692 the abstract function it is dealing with) as being abstract,
19693 precisely to prevent us from emitting any
19694 DW_TAG_inlined_subroutine DIE as a descendent
19695 of an abstract function instance. So in that case, we should
19696 not call gen_inlined_subroutine_die.
19698 Later though, when cgraph asks dwarf2out to emit info
19699 for the concrete instance of the function decl into which
19700 the concrete instance of STMT got inlined, the later will lead
19701 to the generation of a DW_TAG_inlined_subroutine DIE. */
19702 if (! BLOCK_ABSTRACT (stmt))
19703 gen_inlined_subroutine_die (stmt, context_die, depth);
19705 else
19706 gen_lexical_block_die (stmt, context_die, depth);
19708 else
19709 decls_for_scope (stmt, context_die, depth);
19712 /* Process variable DECL (or variable with origin ORIGIN) within
19713 block STMT and add it to CONTEXT_DIE. */
19714 static void
19715 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19717 dw_die_ref die;
19718 tree decl_or_origin = decl ? decl : origin;
19720 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19721 die = lookup_decl_die (decl_or_origin);
19722 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19723 && TYPE_DECL_IS_STUB (decl_or_origin))
19724 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19725 else
19726 die = NULL;
19728 if (die != NULL && die->die_parent == NULL)
19729 add_child_die (context_die, die);
19730 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19731 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19732 stmt, context_die);
19733 else
19734 gen_decl_die (decl, origin, context_die);
19737 /* Generate all of the decls declared within a given scope and (recursively)
19738 all of its sub-blocks. */
19740 static void
19741 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19743 tree decl;
19744 unsigned int i;
19745 tree subblocks;
19747 /* Ignore NULL blocks. */
19748 if (stmt == NULL_TREE)
19749 return;
19751 /* Output the DIEs to represent all of the data objects and typedefs
19752 declared directly within this block but not within any nested
19753 sub-blocks. Also, nested function and tag DIEs have been
19754 generated with a parent of NULL; fix that up now. */
19755 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19756 process_scope_var (stmt, decl, NULL_TREE, context_die);
19757 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19758 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19759 context_die);
19761 /* If we're at -g1, we're not interested in subblocks. */
19762 if (debug_info_level <= DINFO_LEVEL_TERSE)
19763 return;
19765 /* Output the DIEs to represent all sub-blocks (and the items declared
19766 therein) of this block. */
19767 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19768 subblocks != NULL;
19769 subblocks = BLOCK_CHAIN (subblocks))
19770 gen_block_die (subblocks, context_die, depth + 1);
19773 /* Is this a typedef we can avoid emitting? */
19775 static inline int
19776 is_redundant_typedef (const_tree decl)
19778 if (TYPE_DECL_IS_STUB (decl))
19779 return 1;
19781 if (DECL_ARTIFICIAL (decl)
19782 && DECL_CONTEXT (decl)
19783 && is_tagged_type (DECL_CONTEXT (decl))
19784 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19785 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19786 /* Also ignore the artificial member typedef for the class name. */
19787 return 1;
19789 return 0;
19792 /* Return TRUE if TYPE is a typedef that names a type for linkage
19793 purposes. This kind of typedefs is produced by the C++ FE for
19794 constructs like:
19796 typedef struct {...} foo;
19798 In that case, there is no typedef variant type produced for foo.
19799 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19800 struct type. */
19802 static bool
19803 is_naming_typedef_decl (const_tree decl)
19805 if (decl == NULL_TREE
19806 || TREE_CODE (decl) != TYPE_DECL
19807 || !is_tagged_type (TREE_TYPE (decl))
19808 || DECL_IS_BUILTIN (decl)
19809 || is_redundant_typedef (decl)
19810 /* It looks like Ada produces TYPE_DECLs that are very similar
19811 to C++ naming typedefs but that have different
19812 semantics. Let's be specific to c++ for now. */
19813 || !is_cxx ())
19814 return FALSE;
19816 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19817 && TYPE_NAME (TREE_TYPE (decl)) == decl
19818 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19819 != TYPE_NAME (TREE_TYPE (decl))));
19822 /* Returns the DIE for a context. */
19824 static inline dw_die_ref
19825 get_context_die (tree context)
19827 if (context)
19829 /* Find die that represents this context. */
19830 if (TYPE_P (context))
19832 context = TYPE_MAIN_VARIANT (context);
19833 return strip_naming_typedef (context, force_type_die (context));
19835 else
19836 return force_decl_die (context);
19838 return comp_unit_die ();
19841 /* Returns the DIE for decl. A DIE will always be returned. */
19843 static dw_die_ref
19844 force_decl_die (tree decl)
19846 dw_die_ref decl_die;
19847 unsigned saved_external_flag;
19848 tree save_fn = NULL_TREE;
19849 decl_die = lookup_decl_die (decl);
19850 if (!decl_die)
19852 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19854 decl_die = lookup_decl_die (decl);
19855 if (decl_die)
19856 return decl_die;
19858 switch (TREE_CODE (decl))
19860 case FUNCTION_DECL:
19861 /* Clear current_function_decl, so that gen_subprogram_die thinks
19862 that this is a declaration. At this point, we just want to force
19863 declaration die. */
19864 save_fn = current_function_decl;
19865 current_function_decl = NULL_TREE;
19866 gen_subprogram_die (decl, context_die);
19867 current_function_decl = save_fn;
19868 break;
19870 case VAR_DECL:
19871 /* Set external flag to force declaration die. Restore it after
19872 gen_decl_die() call. */
19873 saved_external_flag = DECL_EXTERNAL (decl);
19874 DECL_EXTERNAL (decl) = 1;
19875 gen_decl_die (decl, NULL, context_die);
19876 DECL_EXTERNAL (decl) = saved_external_flag;
19877 break;
19879 case NAMESPACE_DECL:
19880 if (dwarf_version >= 3 || !dwarf_strict)
19881 dwarf2out_decl (decl);
19882 else
19883 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19884 decl_die = comp_unit_die ();
19885 break;
19887 case TRANSLATION_UNIT_DECL:
19888 decl_die = comp_unit_die ();
19889 break;
19891 default:
19892 gcc_unreachable ();
19895 /* We should be able to find the DIE now. */
19896 if (!decl_die)
19897 decl_die = lookup_decl_die (decl);
19898 gcc_assert (decl_die);
19901 return decl_die;
19904 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19905 always returned. */
19907 static dw_die_ref
19908 force_type_die (tree type)
19910 dw_die_ref type_die;
19912 type_die = lookup_type_die (type);
19913 if (!type_die)
19915 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19917 type_die = modified_type_die (type, TYPE_READONLY (type),
19918 TYPE_VOLATILE (type), context_die);
19919 gcc_assert (type_die);
19921 return type_die;
19924 /* Force out any required namespaces to be able to output DECL,
19925 and return the new context_die for it, if it's changed. */
19927 static dw_die_ref
19928 setup_namespace_context (tree thing, dw_die_ref context_die)
19930 tree context = (DECL_P (thing)
19931 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19932 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19933 /* Force out the namespace. */
19934 context_die = force_decl_die (context);
19936 return context_die;
19939 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19940 type) within its namespace, if appropriate.
19942 For compatibility with older debuggers, namespace DIEs only contain
19943 declarations; all definitions are emitted at CU scope. */
19945 static dw_die_ref
19946 declare_in_namespace (tree thing, dw_die_ref context_die)
19948 dw_die_ref ns_context;
19950 if (debug_info_level <= DINFO_LEVEL_TERSE)
19951 return context_die;
19953 /* If this decl is from an inlined function, then don't try to emit it in its
19954 namespace, as we will get confused. It would have already been emitted
19955 when the abstract instance of the inline function was emitted anyways. */
19956 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19957 return context_die;
19959 ns_context = setup_namespace_context (thing, context_die);
19961 if (ns_context != context_die)
19963 if (is_fortran ())
19964 return ns_context;
19965 if (DECL_P (thing))
19966 gen_decl_die (thing, NULL, ns_context);
19967 else
19968 gen_type_die (thing, ns_context);
19970 return context_die;
19973 /* Generate a DIE for a namespace or namespace alias. */
19975 static void
19976 gen_namespace_die (tree decl, dw_die_ref context_die)
19978 dw_die_ref namespace_die;
19980 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19981 they are an alias of. */
19982 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19984 /* Output a real namespace or module. */
19985 context_die = setup_namespace_context (decl, comp_unit_die ());
19986 namespace_die = new_die (is_fortran ()
19987 ? DW_TAG_module : DW_TAG_namespace,
19988 context_die, decl);
19989 /* For Fortran modules defined in different CU don't add src coords. */
19990 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19992 const char *name = dwarf2_name (decl, 0);
19993 if (name)
19994 add_name_attribute (namespace_die, name);
19996 else
19997 add_name_and_src_coords_attributes (namespace_die, decl);
19998 if (DECL_EXTERNAL (decl))
19999 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20000 equate_decl_number_to_die (decl, namespace_die);
20002 else
20004 /* Output a namespace alias. */
20006 /* Force out the namespace we are an alias of, if necessary. */
20007 dw_die_ref origin_die
20008 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20010 if (DECL_FILE_SCOPE_P (decl)
20011 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20012 context_die = setup_namespace_context (decl, comp_unit_die ());
20013 /* Now create the namespace alias DIE. */
20014 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20015 add_name_and_src_coords_attributes (namespace_die, decl);
20016 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20017 equate_decl_number_to_die (decl, namespace_die);
20019 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20020 if (want_pubnames ())
20021 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20024 /* Generate Dwarf debug information for a decl described by DECL.
20025 The return value is currently only meaningful for PARM_DECLs,
20026 for all other decls it returns NULL. */
20028 static dw_die_ref
20029 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20031 tree decl_or_origin = decl ? decl : origin;
20032 tree class_origin = NULL, ultimate_origin;
20034 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20035 return NULL;
20037 switch (TREE_CODE (decl_or_origin))
20039 case ERROR_MARK:
20040 break;
20042 case CONST_DECL:
20043 if (!is_fortran () && !is_ada ())
20045 /* The individual enumerators of an enum type get output when we output
20046 the Dwarf representation of the relevant enum type itself. */
20047 break;
20050 /* Emit its type. */
20051 gen_type_die (TREE_TYPE (decl), context_die);
20053 /* And its containing namespace. */
20054 context_die = declare_in_namespace (decl, context_die);
20056 gen_const_die (decl, context_die);
20057 break;
20059 case FUNCTION_DECL:
20060 /* Don't output any DIEs to represent mere function declarations,
20061 unless they are class members or explicit block externs. */
20062 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20063 && DECL_FILE_SCOPE_P (decl_or_origin)
20064 && (current_function_decl == NULL_TREE
20065 || DECL_ARTIFICIAL (decl_or_origin)))
20066 break;
20068 #if 0
20069 /* FIXME */
20070 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20071 on local redeclarations of global functions. That seems broken. */
20072 if (current_function_decl != decl)
20073 /* This is only a declaration. */;
20074 #endif
20076 /* If we're emitting a clone, emit info for the abstract instance. */
20077 if (origin || DECL_ORIGIN (decl) != decl)
20078 dwarf2out_abstract_function (origin
20079 ? DECL_ORIGIN (origin)
20080 : DECL_ABSTRACT_ORIGIN (decl));
20082 /* If we're emitting an out-of-line copy of an inline function,
20083 emit info for the abstract instance and set up to refer to it. */
20084 else if (cgraph_function_possibly_inlined_p (decl)
20085 && ! DECL_ABSTRACT (decl)
20086 && ! class_or_namespace_scope_p (context_die)
20087 /* dwarf2out_abstract_function won't emit a die if this is just
20088 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20089 that case, because that works only if we have a die. */
20090 && DECL_INITIAL (decl) != NULL_TREE)
20092 dwarf2out_abstract_function (decl);
20093 set_decl_origin_self (decl);
20096 /* Otherwise we're emitting the primary DIE for this decl. */
20097 else if (debug_info_level > DINFO_LEVEL_TERSE)
20099 /* Before we describe the FUNCTION_DECL itself, make sure that we
20100 have its containing type. */
20101 if (!origin)
20102 origin = decl_class_context (decl);
20103 if (origin != NULL_TREE)
20104 gen_type_die (origin, context_die);
20106 /* And its return type. */
20107 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20109 /* And its virtual context. */
20110 if (DECL_VINDEX (decl) != NULL_TREE)
20111 gen_type_die (DECL_CONTEXT (decl), context_die);
20113 /* Make sure we have a member DIE for decl. */
20114 if (origin != NULL_TREE)
20115 gen_type_die_for_member (origin, decl, context_die);
20117 /* And its containing namespace. */
20118 context_die = declare_in_namespace (decl, context_die);
20121 /* Now output a DIE to represent the function itself. */
20122 if (decl)
20123 gen_subprogram_die (decl, context_die);
20124 break;
20126 case TYPE_DECL:
20127 /* If we are in terse mode, don't generate any DIEs to represent any
20128 actual typedefs. */
20129 if (debug_info_level <= DINFO_LEVEL_TERSE)
20130 break;
20132 /* In the special case of a TYPE_DECL node representing the declaration
20133 of some type tag, if the given TYPE_DECL is marked as having been
20134 instantiated from some other (original) TYPE_DECL node (e.g. one which
20135 was generated within the original definition of an inline function) we
20136 used to generate a special (abbreviated) DW_TAG_structure_type,
20137 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20138 should be actually referencing those DIEs, as variable DIEs with that
20139 type would be emitted already in the abstract origin, so it was always
20140 removed during unused type prunning. Don't add anything in this
20141 case. */
20142 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20143 break;
20145 if (is_redundant_typedef (decl))
20146 gen_type_die (TREE_TYPE (decl), context_die);
20147 else
20148 /* Output a DIE to represent the typedef itself. */
20149 gen_typedef_die (decl, context_die);
20150 break;
20152 case LABEL_DECL:
20153 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20154 gen_label_die (decl, context_die);
20155 break;
20157 case VAR_DECL:
20158 case RESULT_DECL:
20159 /* If we are in terse mode, don't generate any DIEs to represent any
20160 variable declarations or definitions. */
20161 if (debug_info_level <= DINFO_LEVEL_TERSE)
20162 break;
20164 /* Output any DIEs that are needed to specify the type of this data
20165 object. */
20166 if (decl_by_reference_p (decl_or_origin))
20167 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20168 else
20169 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20171 /* And its containing type. */
20172 class_origin = decl_class_context (decl_or_origin);
20173 if (class_origin != NULL_TREE)
20174 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20176 /* And its containing namespace. */
20177 context_die = declare_in_namespace (decl_or_origin, context_die);
20179 /* Now output the DIE to represent the data object itself. This gets
20180 complicated because of the possibility that the VAR_DECL really
20181 represents an inlined instance of a formal parameter for an inline
20182 function. */
20183 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20184 if (ultimate_origin != NULL_TREE
20185 && TREE_CODE (ultimate_origin) == PARM_DECL)
20186 gen_formal_parameter_die (decl, origin,
20187 true /* Emit name attribute. */,
20188 context_die);
20189 else
20190 gen_variable_die (decl, origin, context_die);
20191 break;
20193 case FIELD_DECL:
20194 /* Ignore the nameless fields that are used to skip bits but handle C++
20195 anonymous unions and structs. */
20196 if (DECL_NAME (decl) != NULL_TREE
20197 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20198 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20200 gen_type_die (member_declared_type (decl), context_die);
20201 gen_field_die (decl, context_die);
20203 break;
20205 case PARM_DECL:
20206 if (DECL_BY_REFERENCE (decl_or_origin))
20207 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20208 else
20209 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20210 return gen_formal_parameter_die (decl, origin,
20211 true /* Emit name attribute. */,
20212 context_die);
20214 case NAMESPACE_DECL:
20215 case IMPORTED_DECL:
20216 if (dwarf_version >= 3 || !dwarf_strict)
20217 gen_namespace_die (decl, context_die);
20218 break;
20220 default:
20221 /* Probably some frontend-internal decl. Assume we don't care. */
20222 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20223 break;
20226 return NULL;
20229 /* Output debug information for global decl DECL. Called from toplev.c after
20230 compilation proper has finished. */
20232 static void
20233 dwarf2out_global_decl (tree decl)
20235 /* Output DWARF2 information for file-scope tentative data object
20236 declarations, file-scope (extern) function declarations (which
20237 had no corresponding body) and file-scope tagged type declarations
20238 and definitions which have not yet been forced out. */
20239 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20240 dwarf2out_decl (decl);
20243 /* Output debug information for type decl DECL. Called from toplev.c
20244 and from language front ends (to record built-in types). */
20245 static void
20246 dwarf2out_type_decl (tree decl, int local)
20248 if (!local)
20249 dwarf2out_decl (decl);
20252 /* Output debug information for imported module or decl DECL.
20253 NAME is non-NULL name in the lexical block if the decl has been renamed.
20254 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20255 that DECL belongs to.
20256 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20257 static void
20258 dwarf2out_imported_module_or_decl_1 (tree decl,
20259 tree name,
20260 tree lexical_block,
20261 dw_die_ref lexical_block_die)
20263 expanded_location xloc;
20264 dw_die_ref imported_die = NULL;
20265 dw_die_ref at_import_die;
20267 if (TREE_CODE (decl) == IMPORTED_DECL)
20269 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20270 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20271 gcc_assert (decl);
20273 else
20274 xloc = expand_location (input_location);
20276 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20278 at_import_die = force_type_die (TREE_TYPE (decl));
20279 /* For namespace N { typedef void T; } using N::T; base_type_die
20280 returns NULL, but DW_TAG_imported_declaration requires
20281 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20282 if (!at_import_die)
20284 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20285 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20286 at_import_die = lookup_type_die (TREE_TYPE (decl));
20287 gcc_assert (at_import_die);
20290 else
20292 at_import_die = lookup_decl_die (decl);
20293 if (!at_import_die)
20295 /* If we're trying to avoid duplicate debug info, we may not have
20296 emitted the member decl for this field. Emit it now. */
20297 if (TREE_CODE (decl) == FIELD_DECL)
20299 tree type = DECL_CONTEXT (decl);
20301 if (TYPE_CONTEXT (type)
20302 && TYPE_P (TYPE_CONTEXT (type))
20303 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20304 DINFO_USAGE_DIR_USE))
20305 return;
20306 gen_type_die_for_member (type, decl,
20307 get_context_die (TYPE_CONTEXT (type)));
20309 at_import_die = force_decl_die (decl);
20313 if (TREE_CODE (decl) == NAMESPACE_DECL)
20315 if (dwarf_version >= 3 || !dwarf_strict)
20316 imported_die = new_die (DW_TAG_imported_module,
20317 lexical_block_die,
20318 lexical_block);
20319 else
20320 return;
20322 else
20323 imported_die = new_die (DW_TAG_imported_declaration,
20324 lexical_block_die,
20325 lexical_block);
20327 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20328 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20329 if (name)
20330 add_AT_string (imported_die, DW_AT_name,
20331 IDENTIFIER_POINTER (name));
20332 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20335 /* Output debug information for imported module or decl DECL.
20336 NAME is non-NULL name in context if the decl has been renamed.
20337 CHILD is true if decl is one of the renamed decls as part of
20338 importing whole module. */
20340 static void
20341 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20342 bool child)
20344 /* dw_die_ref at_import_die; */
20345 dw_die_ref scope_die;
20347 if (debug_info_level <= DINFO_LEVEL_TERSE)
20348 return;
20350 gcc_assert (decl);
20352 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20353 We need decl DIE for reference and scope die. First, get DIE for the decl
20354 itself. */
20356 /* Get the scope die for decl context. Use comp_unit_die for global module
20357 or decl. If die is not found for non globals, force new die. */
20358 if (context
20359 && TYPE_P (context)
20360 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20361 return;
20363 if (!(dwarf_version >= 3 || !dwarf_strict))
20364 return;
20366 scope_die = get_context_die (context);
20368 if (child)
20370 gcc_assert (scope_die->die_child);
20371 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20372 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20373 scope_die = scope_die->die_child;
20376 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20377 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20381 /* Write the debugging output for DECL. */
20383 void
20384 dwarf2out_decl (tree decl)
20386 dw_die_ref context_die = comp_unit_die ();
20388 switch (TREE_CODE (decl))
20390 case ERROR_MARK:
20391 return;
20393 case FUNCTION_DECL:
20394 /* What we would really like to do here is to filter out all mere
20395 file-scope declarations of file-scope functions which are never
20396 referenced later within this translation unit (and keep all of ones
20397 that *are* referenced later on) but we aren't clairvoyant, so we have
20398 no idea which functions will be referenced in the future (i.e. later
20399 on within the current translation unit). So here we just ignore all
20400 file-scope function declarations which are not also definitions. If
20401 and when the debugger needs to know something about these functions,
20402 it will have to hunt around and find the DWARF information associated
20403 with the definition of the function.
20405 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20406 nodes represent definitions and which ones represent mere
20407 declarations. We have to check DECL_INITIAL instead. That's because
20408 the C front-end supports some weird semantics for "extern inline"
20409 function definitions. These can get inlined within the current
20410 translation unit (and thus, we need to generate Dwarf info for their
20411 abstract instances so that the Dwarf info for the concrete inlined
20412 instances can have something to refer to) but the compiler never
20413 generates any out-of-lines instances of such things (despite the fact
20414 that they *are* definitions).
20416 The important point is that the C front-end marks these "extern
20417 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20418 them anyway. Note that the C++ front-end also plays some similar games
20419 for inline function definitions appearing within include files which
20420 also contain `#pragma interface' pragmas.
20422 If we are called from dwarf2out_abstract_function output a DIE
20423 anyway. We can end up here this way with early inlining and LTO
20424 where the inlined function is output in a different LTRANS unit
20425 or not at all. */
20426 if (DECL_INITIAL (decl) == NULL_TREE
20427 && ! DECL_ABSTRACT (decl))
20428 return;
20430 /* If we're a nested function, initially use a parent of NULL; if we're
20431 a plain function, this will be fixed up in decls_for_scope. If
20432 we're a method, it will be ignored, since we already have a DIE. */
20433 if (decl_function_context (decl)
20434 /* But if we're in terse mode, we don't care about scope. */
20435 && debug_info_level > DINFO_LEVEL_TERSE)
20436 context_die = NULL;
20437 break;
20439 case VAR_DECL:
20440 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20441 declaration and if the declaration was never even referenced from
20442 within this entire compilation unit. We suppress these DIEs in
20443 order to save space in the .debug section (by eliminating entries
20444 which are probably useless). Note that we must not suppress
20445 block-local extern declarations (whether used or not) because that
20446 would screw-up the debugger's name lookup mechanism and cause it to
20447 miss things which really ought to be in scope at a given point. */
20448 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20449 return;
20451 /* For local statics lookup proper context die. */
20452 if (TREE_STATIC (decl)
20453 && DECL_CONTEXT (decl)
20454 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20455 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20457 /* If we are in terse mode, don't generate any DIEs to represent any
20458 variable declarations or definitions. */
20459 if (debug_info_level <= DINFO_LEVEL_TERSE)
20460 return;
20461 break;
20463 case CONST_DECL:
20464 if (debug_info_level <= DINFO_LEVEL_TERSE)
20465 return;
20466 if (!is_fortran () && !is_ada ())
20467 return;
20468 if (TREE_STATIC (decl) && decl_function_context (decl))
20469 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20470 break;
20472 case NAMESPACE_DECL:
20473 case IMPORTED_DECL:
20474 if (debug_info_level <= DINFO_LEVEL_TERSE)
20475 return;
20476 if (lookup_decl_die (decl) != NULL)
20477 return;
20478 break;
20480 case TYPE_DECL:
20481 /* Don't emit stubs for types unless they are needed by other DIEs. */
20482 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20483 return;
20485 /* Don't bother trying to generate any DIEs to represent any of the
20486 normal built-in types for the language we are compiling. */
20487 if (DECL_IS_BUILTIN (decl))
20488 return;
20490 /* If we are in terse mode, don't generate any DIEs for types. */
20491 if (debug_info_level <= DINFO_LEVEL_TERSE)
20492 return;
20494 /* If we're a function-scope tag, initially use a parent of NULL;
20495 this will be fixed up in decls_for_scope. */
20496 if (decl_function_context (decl))
20497 context_die = NULL;
20499 break;
20501 default:
20502 return;
20505 gen_decl_die (decl, NULL, context_die);
20508 /* Write the debugging output for DECL. */
20510 static void
20511 dwarf2out_function_decl (tree decl)
20513 dwarf2out_decl (decl);
20514 call_arg_locations = NULL;
20515 call_arg_loc_last = NULL;
20516 call_site_count = -1;
20517 tail_call_site_count = -1;
20518 block_map.release ();
20519 htab_empty (decl_loc_table);
20520 htab_empty (cached_dw_loc_list_table);
20523 /* Output a marker (i.e. a label) for the beginning of the generated code for
20524 a lexical block. */
20526 static void
20527 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20528 unsigned int blocknum)
20530 switch_to_section (current_function_section ());
20531 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20534 /* Output a marker (i.e. a label) for the end of the generated code for a
20535 lexical block. */
20537 static void
20538 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20540 switch_to_section (current_function_section ());
20541 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20544 /* Returns nonzero if it is appropriate not to emit any debugging
20545 information for BLOCK, because it doesn't contain any instructions.
20547 Don't allow this for blocks with nested functions or local classes
20548 as we would end up with orphans, and in the presence of scheduling
20549 we may end up calling them anyway. */
20551 static bool
20552 dwarf2out_ignore_block (const_tree block)
20554 tree decl;
20555 unsigned int i;
20557 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20558 if (TREE_CODE (decl) == FUNCTION_DECL
20559 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20560 return 0;
20561 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20563 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20564 if (TREE_CODE (decl) == FUNCTION_DECL
20565 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20566 return 0;
20569 return 1;
20572 /* Hash table routines for file_hash. */
20574 static int
20575 file_table_eq (const void *p1_p, const void *p2_p)
20577 const struct dwarf_file_data *const p1 =
20578 (const struct dwarf_file_data *) p1_p;
20579 const char *const p2 = (const char *) p2_p;
20580 return filename_cmp (p1->filename, p2) == 0;
20583 static hashval_t
20584 file_table_hash (const void *p_p)
20586 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20587 return htab_hash_string (p->filename);
20590 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20591 dwarf2out.c) and return its "index". The index of each (known) filename is
20592 just a unique number which is associated with only that one filename. We
20593 need such numbers for the sake of generating labels (in the .debug_sfnames
20594 section) and references to those files numbers (in the .debug_srcinfo
20595 and.debug_macinfo sections). If the filename given as an argument is not
20596 found in our current list, add it to the list and assign it the next
20597 available unique index number. In order to speed up searches, we remember
20598 the index of the filename was looked up last. This handles the majority of
20599 all searches. */
20601 static struct dwarf_file_data *
20602 lookup_filename (const char *file_name)
20604 void ** slot;
20605 struct dwarf_file_data * created;
20607 /* Check to see if the file name that was searched on the previous
20608 call matches this file name. If so, return the index. */
20609 if (file_table_last_lookup
20610 && (file_name == file_table_last_lookup->filename
20611 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20612 return file_table_last_lookup;
20614 /* Didn't match the previous lookup, search the table. */
20615 slot = htab_find_slot_with_hash (file_table, file_name,
20616 htab_hash_string (file_name), INSERT);
20617 if (*slot)
20618 return (struct dwarf_file_data *) *slot;
20620 created = ggc_alloc_dwarf_file_data ();
20621 created->filename = file_name;
20622 created->emitted_number = 0;
20623 *slot = created;
20624 return created;
20627 /* If the assembler will construct the file table, then translate the compiler
20628 internal file table number into the assembler file table number, and emit
20629 a .file directive if we haven't already emitted one yet. The file table
20630 numbers are different because we prune debug info for unused variables and
20631 types, which may include filenames. */
20633 static int
20634 maybe_emit_file (struct dwarf_file_data * fd)
20636 if (! fd->emitted_number)
20638 if (last_emitted_file)
20639 fd->emitted_number = last_emitted_file->emitted_number + 1;
20640 else
20641 fd->emitted_number = 1;
20642 last_emitted_file = fd;
20644 if (DWARF2_ASM_LINE_DEBUG_INFO)
20646 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20647 output_quoted_string (asm_out_file,
20648 remap_debug_filename (fd->filename));
20649 fputc ('\n', asm_out_file);
20653 return fd->emitted_number;
20656 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20657 That generation should happen after function debug info has been
20658 generated. The value of the attribute is the constant value of ARG. */
20660 static void
20661 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20663 die_arg_entry entry;
20665 if (!die || !arg)
20666 return;
20668 if (!tmpl_value_parm_die_table)
20669 vec_alloc (tmpl_value_parm_die_table, 32);
20671 entry.die = die;
20672 entry.arg = arg;
20673 vec_safe_push (tmpl_value_parm_die_table, entry);
20676 /* Return TRUE if T is an instance of generic type, FALSE
20677 otherwise. */
20679 static bool
20680 generic_type_p (tree t)
20682 if (t == NULL_TREE || !TYPE_P (t))
20683 return false;
20684 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20687 /* Schedule the generation of the generic parameter dies for the
20688 instance of generic type T. The proper generation itself is later
20689 done by gen_scheduled_generic_parms_dies. */
20691 static void
20692 schedule_generic_params_dies_gen (tree t)
20694 if (!generic_type_p (t))
20695 return;
20697 if (!generic_type_instances)
20698 vec_alloc (generic_type_instances, 256);
20700 vec_safe_push (generic_type_instances, t);
20703 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20704 by append_entry_to_tmpl_value_parm_die_table. This function must
20705 be called after function DIEs have been generated. */
20707 static void
20708 gen_remaining_tmpl_value_param_die_attribute (void)
20710 if (tmpl_value_parm_die_table)
20712 unsigned i;
20713 die_arg_entry *e;
20715 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20716 tree_add_const_value_attribute (e->die, e->arg);
20720 /* Generate generic parameters DIEs for instances of generic types
20721 that have been previously scheduled by
20722 schedule_generic_params_dies_gen. This function must be called
20723 after all the types of the CU have been laid out. */
20725 static void
20726 gen_scheduled_generic_parms_dies (void)
20728 unsigned i;
20729 tree t;
20731 if (!generic_type_instances)
20732 return;
20734 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20735 if (COMPLETE_TYPE_P (t))
20736 gen_generic_params_dies (t);
20740 /* Replace DW_AT_name for the decl with name. */
20742 static void
20743 dwarf2out_set_name (tree decl, tree name)
20745 dw_die_ref die;
20746 dw_attr_ref attr;
20747 const char *dname;
20749 die = TYPE_SYMTAB_DIE (decl);
20750 if (!die)
20751 return;
20753 dname = dwarf2_name (name, 0);
20754 if (!dname)
20755 return;
20757 attr = get_AT (die, DW_AT_name);
20758 if (attr)
20760 struct indirect_string_node *node;
20762 node = find_AT_string (dname);
20763 /* replace the string. */
20764 attr->dw_attr_val.v.val_str = node;
20767 else
20768 add_name_attribute (die, dname);
20771 /* True if before or during processing of the first function being emitted. */
20772 static bool in_first_function_p = true;
20773 /* True if loc_note during dwarf2out_var_location call might still be
20774 before first real instruction at address equal to .Ltext0. */
20775 static bool maybe_at_text_label_p = true;
20776 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20777 static unsigned int first_loclabel_num_not_at_text_label;
20779 /* Called by the final INSN scan whenever we see a var location. We
20780 use it to drop labels in the right places, and throw the location in
20781 our lookup table. */
20783 static void
20784 dwarf2out_var_location (rtx loc_note)
20786 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20787 struct var_loc_node *newloc;
20788 rtx next_real, next_note;
20789 static const char *last_label;
20790 static const char *last_postcall_label;
20791 static bool last_in_cold_section_p;
20792 static rtx expected_next_loc_note;
20793 tree decl;
20794 bool var_loc_p;
20796 if (!NOTE_P (loc_note))
20798 if (CALL_P (loc_note))
20800 call_site_count++;
20801 if (SIBLING_CALL_P (loc_note))
20802 tail_call_site_count++;
20804 return;
20807 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20808 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20809 return;
20811 /* Optimize processing a large consecutive sequence of location
20812 notes so we don't spend too much time in next_real_insn. If the
20813 next insn is another location note, remember the next_real_insn
20814 calculation for next time. */
20815 next_real = cached_next_real_insn;
20816 if (next_real)
20818 if (expected_next_loc_note != loc_note)
20819 next_real = NULL_RTX;
20822 next_note = NEXT_INSN (loc_note);
20823 if (! next_note
20824 || INSN_DELETED_P (next_note)
20825 || ! NOTE_P (next_note)
20826 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20827 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20828 next_note = NULL_RTX;
20830 if (! next_real)
20831 next_real = next_real_insn (loc_note);
20833 if (next_note)
20835 expected_next_loc_note = next_note;
20836 cached_next_real_insn = next_real;
20838 else
20839 cached_next_real_insn = NULL_RTX;
20841 /* If there are no instructions which would be affected by this note,
20842 don't do anything. */
20843 if (var_loc_p
20844 && next_real == NULL_RTX
20845 && !NOTE_DURING_CALL_P (loc_note))
20846 return;
20848 if (next_real == NULL_RTX)
20849 next_real = get_last_insn ();
20851 /* If there were any real insns between note we processed last time
20852 and this note (or if it is the first note), clear
20853 last_{,postcall_}label so that they are not reused this time. */
20854 if (last_var_location_insn == NULL_RTX
20855 || last_var_location_insn != next_real
20856 || last_in_cold_section_p != in_cold_section_p)
20858 last_label = NULL;
20859 last_postcall_label = NULL;
20862 if (var_loc_p)
20864 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20865 newloc = add_var_loc_to_decl (decl, loc_note,
20866 NOTE_DURING_CALL_P (loc_note)
20867 ? last_postcall_label : last_label);
20868 if (newloc == NULL)
20869 return;
20871 else
20873 decl = NULL_TREE;
20874 newloc = NULL;
20877 /* If there were no real insns between note we processed last time
20878 and this note, use the label we emitted last time. Otherwise
20879 create a new label and emit it. */
20880 if (last_label == NULL)
20882 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20883 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20884 loclabel_num++;
20885 last_label = ggc_strdup (loclabel);
20886 /* See if loclabel might be equal to .Ltext0. If yes,
20887 bump first_loclabel_num_not_at_text_label. */
20888 if (!have_multiple_function_sections
20889 && in_first_function_p
20890 && maybe_at_text_label_p)
20892 static rtx last_start;
20893 rtx insn;
20894 for (insn = loc_note; insn; insn = previous_insn (insn))
20895 if (insn == last_start)
20896 break;
20897 else if (!NONDEBUG_INSN_P (insn))
20898 continue;
20899 else
20901 rtx body = PATTERN (insn);
20902 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20903 continue;
20904 /* Inline asm could occupy zero bytes. */
20905 else if (GET_CODE (body) == ASM_INPUT
20906 || asm_noperands (body) >= 0)
20907 continue;
20908 #ifdef HAVE_attr_length
20909 else if (get_attr_min_length (insn) == 0)
20910 continue;
20911 #endif
20912 else
20914 /* Assume insn has non-zero length. */
20915 maybe_at_text_label_p = false;
20916 break;
20919 if (maybe_at_text_label_p)
20921 last_start = loc_note;
20922 first_loclabel_num_not_at_text_label = loclabel_num;
20927 if (!var_loc_p)
20929 struct call_arg_loc_node *ca_loc
20930 = ggc_alloc_cleared_call_arg_loc_node ();
20931 rtx prev = prev_real_insn (loc_note), x;
20932 ca_loc->call_arg_loc_note = loc_note;
20933 ca_loc->next = NULL;
20934 ca_loc->label = last_label;
20935 gcc_assert (prev
20936 && (CALL_P (prev)
20937 || (NONJUMP_INSN_P (prev)
20938 && GET_CODE (PATTERN (prev)) == SEQUENCE
20939 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20940 if (!CALL_P (prev))
20941 prev = XVECEXP (PATTERN (prev), 0, 0);
20942 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20943 x = get_call_rtx_from (PATTERN (prev));
20944 if (x)
20946 x = XEXP (XEXP (x, 0), 0);
20947 if (GET_CODE (x) == SYMBOL_REF
20948 && SYMBOL_REF_DECL (x)
20949 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20950 ca_loc->symbol_ref = x;
20952 ca_loc->block = insn_scope (prev);
20953 if (call_arg_locations)
20954 call_arg_loc_last->next = ca_loc;
20955 else
20956 call_arg_locations = ca_loc;
20957 call_arg_loc_last = ca_loc;
20959 else if (!NOTE_DURING_CALL_P (loc_note))
20960 newloc->label = last_label;
20961 else
20963 if (!last_postcall_label)
20965 sprintf (loclabel, "%s-1", last_label);
20966 last_postcall_label = ggc_strdup (loclabel);
20968 newloc->label = last_postcall_label;
20971 last_var_location_insn = next_real;
20972 last_in_cold_section_p = in_cold_section_p;
20975 /* Note in one location list that text section has changed. */
20977 static int
20978 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20980 var_loc_list *list = (var_loc_list *) *slot;
20981 if (list->first)
20982 list->last_before_switch
20983 = list->last->next ? list->last->next : list->last;
20984 return 1;
20987 /* Note in all location lists that text section has changed. */
20989 static void
20990 var_location_switch_text_section (void)
20992 if (decl_loc_table == NULL)
20993 return;
20995 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20998 /* Create a new line number table. */
21000 static dw_line_info_table *
21001 new_line_info_table (void)
21003 dw_line_info_table *table;
21005 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21006 table->file_num = 1;
21007 table->line_num = 1;
21008 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21010 return table;
21013 /* Lookup the "current" table into which we emit line info, so
21014 that we don't have to do it for every source line. */
21016 static void
21017 set_cur_line_info_table (section *sec)
21019 dw_line_info_table *table;
21021 if (sec == text_section)
21022 table = text_section_line_info;
21023 else if (sec == cold_text_section)
21025 table = cold_text_section_line_info;
21026 if (!table)
21028 cold_text_section_line_info = table = new_line_info_table ();
21029 table->end_label = cold_end_label;
21032 else
21034 const char *end_label;
21036 if (flag_reorder_blocks_and_partition)
21038 if (in_cold_section_p)
21039 end_label = crtl->subsections.cold_section_end_label;
21040 else
21041 end_label = crtl->subsections.hot_section_end_label;
21043 else
21045 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21046 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21047 current_function_funcdef_no);
21048 end_label = ggc_strdup (label);
21051 table = new_line_info_table ();
21052 table->end_label = end_label;
21054 vec_safe_push (separate_line_info, table);
21057 if (DWARF2_ASM_LINE_DEBUG_INFO)
21058 table->is_stmt = (cur_line_info_table
21059 ? cur_line_info_table->is_stmt
21060 : DWARF_LINE_DEFAULT_IS_STMT_START);
21061 cur_line_info_table = table;
21065 /* We need to reset the locations at the beginning of each
21066 function. We can't do this in the end_function hook, because the
21067 declarations that use the locations won't have been output when
21068 that hook is called. Also compute have_multiple_function_sections here. */
21070 static void
21071 dwarf2out_begin_function (tree fun)
21073 section *sec = function_section (fun);
21075 if (sec != text_section)
21076 have_multiple_function_sections = true;
21078 if (flag_reorder_blocks_and_partition && !cold_text_section)
21080 gcc_assert (current_function_decl == fun);
21081 cold_text_section = unlikely_text_section ();
21082 switch_to_section (cold_text_section);
21083 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21084 switch_to_section (sec);
21087 dwarf2out_note_section_used ();
21088 call_site_count = 0;
21089 tail_call_site_count = 0;
21091 set_cur_line_info_table (sec);
21094 /* Helper function of dwarf2out_end_function, called only after emitting
21095 the very first function into assembly. Check if some .debug_loc range
21096 might end with a .LVL* label that could be equal to .Ltext0.
21097 In that case we must force using absolute addresses in .debug_loc ranges,
21098 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21099 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21100 list terminator.
21101 Set have_multiple_function_sections to true in that case and
21102 terminate htab traversal. */
21104 static int
21105 find_empty_loc_ranges_at_text_label (void **slot, void *)
21107 var_loc_list *entry;
21108 struct var_loc_node *node;
21110 entry = (var_loc_list *) *slot;
21111 node = entry->first;
21112 if (node && node->next && node->next->label)
21114 unsigned int i;
21115 const char *label = node->next->label;
21116 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21118 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21120 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21121 if (strcmp (label, loclabel) == 0)
21123 have_multiple_function_sections = true;
21124 return 0;
21128 return 1;
21131 /* Hook called after emitting a function into assembly.
21132 This does something only for the very first function emitted. */
21134 static void
21135 dwarf2out_end_function (unsigned int)
21137 if (in_first_function_p
21138 && !have_multiple_function_sections
21139 && first_loclabel_num_not_at_text_label
21140 && decl_loc_table)
21141 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21142 NULL);
21143 in_first_function_p = false;
21144 maybe_at_text_label_p = false;
21147 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21149 static void
21150 push_dw_line_info_entry (dw_line_info_table *table,
21151 enum dw_line_info_opcode opcode, unsigned int val)
21153 dw_line_info_entry e;
21154 e.opcode = opcode;
21155 e.val = val;
21156 vec_safe_push (table->entries, e);
21159 /* Output a label to mark the beginning of a source code line entry
21160 and record information relating to this source line, in
21161 'line_info_table' for later output of the .debug_line section. */
21162 /* ??? The discriminator parameter ought to be unsigned. */
21164 static void
21165 dwarf2out_source_line (unsigned int line, const char *filename,
21166 int discriminator, bool is_stmt)
21168 unsigned int file_num;
21169 dw_line_info_table *table;
21171 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21172 return;
21174 /* The discriminator column was added in dwarf4. Simplify the below
21175 by simply removing it if we're not supposed to output it. */
21176 if (dwarf_version < 4 && dwarf_strict)
21177 discriminator = 0;
21179 table = cur_line_info_table;
21180 file_num = maybe_emit_file (lookup_filename (filename));
21182 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21183 the debugger has used the second (possibly duplicate) line number
21184 at the beginning of the function to mark the end of the prologue.
21185 We could eliminate any other duplicates within the function. For
21186 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21187 that second line number entry. */
21188 /* Recall that this end-of-prologue indication is *not* the same thing
21189 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21190 to which the hook corresponds, follows the last insn that was
21191 emitted by gen_prologue. What we need is to precede the first insn
21192 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21193 insn that corresponds to something the user wrote. These may be
21194 very different locations once scheduling is enabled. */
21196 if (0 && file_num == table->file_num
21197 && line == table->line_num
21198 && discriminator == table->discrim_num
21199 && is_stmt == table->is_stmt)
21200 return;
21202 switch_to_section (current_function_section ());
21204 /* If requested, emit something human-readable. */
21205 if (flag_debug_asm)
21206 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21208 if (DWARF2_ASM_LINE_DEBUG_INFO)
21210 /* Emit the .loc directive understood by GNU as. */
21211 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21212 file_num, line, is_stmt, discriminator */
21213 fputs ("\t.loc ", asm_out_file);
21214 fprint_ul (asm_out_file, file_num);
21215 putc (' ', asm_out_file);
21216 fprint_ul (asm_out_file, line);
21217 putc (' ', asm_out_file);
21218 putc ('0', asm_out_file);
21220 if (is_stmt != table->is_stmt)
21222 fputs (" is_stmt ", asm_out_file);
21223 putc (is_stmt ? '1' : '0', asm_out_file);
21225 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21227 gcc_assert (discriminator > 0);
21228 fputs (" discriminator ", asm_out_file);
21229 fprint_ul (asm_out_file, (unsigned long) discriminator);
21231 putc ('\n', asm_out_file);
21233 else
21235 unsigned int label_num = ++line_info_label_num;
21237 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21239 push_dw_line_info_entry (table, LI_set_address, label_num);
21240 if (file_num != table->file_num)
21241 push_dw_line_info_entry (table, LI_set_file, file_num);
21242 if (discriminator != table->discrim_num)
21243 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21244 if (is_stmt != table->is_stmt)
21245 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21246 push_dw_line_info_entry (table, LI_set_line, line);
21249 table->file_num = file_num;
21250 table->line_num = line;
21251 table->discrim_num = discriminator;
21252 table->is_stmt = is_stmt;
21253 table->in_use = true;
21256 /* Record the beginning of a new source file. */
21258 static void
21259 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21261 if (flag_eliminate_dwarf2_dups)
21263 /* Record the beginning of the file for break_out_includes. */
21264 dw_die_ref bincl_die;
21266 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21267 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21270 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21272 macinfo_entry e;
21273 e.code = DW_MACINFO_start_file;
21274 e.lineno = lineno;
21275 e.info = ggc_strdup (filename);
21276 vec_safe_push (macinfo_table, e);
21280 /* Record the end of a source file. */
21282 static void
21283 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21285 if (flag_eliminate_dwarf2_dups)
21286 /* Record the end of the file for break_out_includes. */
21287 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21289 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21291 macinfo_entry e;
21292 e.code = DW_MACINFO_end_file;
21293 e.lineno = lineno;
21294 e.info = NULL;
21295 vec_safe_push (macinfo_table, e);
21299 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21300 the tail part of the directive line, i.e. the part which is past the
21301 initial whitespace, #, whitespace, directive-name, whitespace part. */
21303 static void
21304 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21305 const char *buffer ATTRIBUTE_UNUSED)
21307 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21309 macinfo_entry e;
21310 /* Insert a dummy first entry to be able to optimize the whole
21311 predefined macro block using DW_MACRO_GNU_transparent_include. */
21312 if (macinfo_table->is_empty () && lineno <= 1)
21314 e.code = 0;
21315 e.lineno = 0;
21316 e.info = NULL;
21317 vec_safe_push (macinfo_table, e);
21319 e.code = DW_MACINFO_define;
21320 e.lineno = lineno;
21321 e.info = ggc_strdup (buffer);
21322 vec_safe_push (macinfo_table, e);
21326 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21327 the tail part of the directive line, i.e. the part which is past the
21328 initial whitespace, #, whitespace, directive-name, whitespace part. */
21330 static void
21331 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21332 const char *buffer ATTRIBUTE_UNUSED)
21334 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21336 macinfo_entry e;
21337 /* Insert a dummy first entry to be able to optimize the whole
21338 predefined macro block using DW_MACRO_GNU_transparent_include. */
21339 if (macinfo_table->is_empty () && lineno <= 1)
21341 e.code = 0;
21342 e.lineno = 0;
21343 e.info = NULL;
21344 vec_safe_push (macinfo_table, e);
21346 e.code = DW_MACINFO_undef;
21347 e.lineno = lineno;
21348 e.info = ggc_strdup (buffer);
21349 vec_safe_push (macinfo_table, e);
21353 /* Helpers to manipulate hash table of CUs. */
21355 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21357 typedef macinfo_entry value_type;
21358 typedef macinfo_entry compare_type;
21359 static inline hashval_t hash (const value_type *);
21360 static inline bool equal (const value_type *, const compare_type *);
21363 inline hashval_t
21364 macinfo_entry_hasher::hash (const value_type *entry)
21366 return htab_hash_string (entry->info);
21369 inline bool
21370 macinfo_entry_hasher::equal (const value_type *entry1,
21371 const compare_type *entry2)
21373 return !strcmp (entry1->info, entry2->info);
21376 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21378 /* Output a single .debug_macinfo entry. */
21380 static void
21381 output_macinfo_op (macinfo_entry *ref)
21383 int file_num;
21384 size_t len;
21385 struct indirect_string_node *node;
21386 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21387 struct dwarf_file_data *fd;
21389 switch (ref->code)
21391 case DW_MACINFO_start_file:
21392 fd = lookup_filename (ref->info);
21393 file_num = maybe_emit_file (fd);
21394 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21395 dw2_asm_output_data_uleb128 (ref->lineno,
21396 "Included from line number %lu",
21397 (unsigned long) ref->lineno);
21398 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21399 break;
21400 case DW_MACINFO_end_file:
21401 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21402 break;
21403 case DW_MACINFO_define:
21404 case DW_MACINFO_undef:
21405 len = strlen (ref->info) + 1;
21406 if (!dwarf_strict
21407 && len > DWARF_OFFSET_SIZE
21408 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21409 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21411 ref->code = ref->code == DW_MACINFO_define
21412 ? DW_MACRO_GNU_define_indirect
21413 : DW_MACRO_GNU_undef_indirect;
21414 output_macinfo_op (ref);
21415 return;
21417 dw2_asm_output_data (1, ref->code,
21418 ref->code == DW_MACINFO_define
21419 ? "Define macro" : "Undefine macro");
21420 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21421 (unsigned long) ref->lineno);
21422 dw2_asm_output_nstring (ref->info, -1, "The macro");
21423 break;
21424 case DW_MACRO_GNU_define_indirect:
21425 case DW_MACRO_GNU_undef_indirect:
21426 node = find_AT_string (ref->info);
21427 gcc_assert (node
21428 && ((node->form == DW_FORM_strp)
21429 || (node->form == DW_FORM_GNU_str_index)));
21430 dw2_asm_output_data (1, ref->code,
21431 ref->code == DW_MACRO_GNU_define_indirect
21432 ? "Define macro indirect"
21433 : "Undefine macro indirect");
21434 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21435 (unsigned long) ref->lineno);
21436 if (node->form == DW_FORM_strp)
21437 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21438 debug_str_section, "The macro: \"%s\"",
21439 ref->info);
21440 else
21441 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21442 ref->info);
21443 break;
21444 case DW_MACRO_GNU_transparent_include:
21445 dw2_asm_output_data (1, ref->code, "Transparent include");
21446 ASM_GENERATE_INTERNAL_LABEL (label,
21447 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21448 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21449 break;
21450 default:
21451 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21452 ASM_COMMENT_START, (unsigned long) ref->code);
21453 break;
21457 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21458 other compilation unit .debug_macinfo sections. IDX is the first
21459 index of a define/undef, return the number of ops that should be
21460 emitted in a comdat .debug_macinfo section and emit
21461 a DW_MACRO_GNU_transparent_include entry referencing it.
21462 If the define/undef entry should be emitted normally, return 0. */
21464 static unsigned
21465 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21466 macinfo_hash_type *macinfo_htab)
21468 macinfo_entry *first, *second, *cur, *inc;
21469 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21470 unsigned char checksum[16];
21471 struct md5_ctx ctx;
21472 char *grp_name, *tail;
21473 const char *base;
21474 unsigned int i, count, encoded_filename_len, linebuf_len;
21475 macinfo_entry **slot;
21477 first = &(*macinfo_table)[idx];
21478 second = &(*macinfo_table)[idx + 1];
21480 /* Optimize only if there are at least two consecutive define/undef ops,
21481 and either all of them are before first DW_MACINFO_start_file
21482 with lineno {0,1} (i.e. predefined macro block), or all of them are
21483 in some included header file. */
21484 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21485 return 0;
21486 if (vec_safe_is_empty (files))
21488 if (first->lineno > 1 || second->lineno > 1)
21489 return 0;
21491 else if (first->lineno == 0)
21492 return 0;
21494 /* Find the last define/undef entry that can be grouped together
21495 with first and at the same time compute md5 checksum of their
21496 codes, linenumbers and strings. */
21497 md5_init_ctx (&ctx);
21498 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21499 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21500 break;
21501 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21502 break;
21503 else
21505 unsigned char code = cur->code;
21506 md5_process_bytes (&code, 1, &ctx);
21507 checksum_uleb128 (cur->lineno, &ctx);
21508 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21510 md5_finish_ctx (&ctx, checksum);
21511 count = i - idx;
21513 /* From the containing include filename (if any) pick up just
21514 usable characters from its basename. */
21515 if (vec_safe_is_empty (files))
21516 base = "";
21517 else
21518 base = lbasename (files->last ().info);
21519 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21520 if (ISIDNUM (base[i]) || base[i] == '.')
21521 encoded_filename_len++;
21522 /* Count . at the end. */
21523 if (encoded_filename_len)
21524 encoded_filename_len++;
21526 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21527 linebuf_len = strlen (linebuf);
21529 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21530 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21531 + 16 * 2 + 1);
21532 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21533 tail = grp_name + 4;
21534 if (encoded_filename_len)
21536 for (i = 0; base[i]; i++)
21537 if (ISIDNUM (base[i]) || base[i] == '.')
21538 *tail++ = base[i];
21539 *tail++ = '.';
21541 memcpy (tail, linebuf, linebuf_len);
21542 tail += linebuf_len;
21543 *tail++ = '.';
21544 for (i = 0; i < 16; i++)
21545 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21547 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21548 in the empty vector entry before the first define/undef. */
21549 inc = &(*macinfo_table)[idx - 1];
21550 inc->code = DW_MACRO_GNU_transparent_include;
21551 inc->lineno = 0;
21552 inc->info = ggc_strdup (grp_name);
21553 if (!macinfo_htab->is_created ())
21554 macinfo_htab->create (10);
21555 /* Avoid emitting duplicates. */
21556 slot = macinfo_htab->find_slot (inc, INSERT);
21557 if (*slot != NULL)
21559 inc->code = 0;
21560 inc->info = NULL;
21561 /* If such an entry has been used before, just emit
21562 a DW_MACRO_GNU_transparent_include op. */
21563 inc = *slot;
21564 output_macinfo_op (inc);
21565 /* And clear all macinfo_entry in the range to avoid emitting them
21566 in the second pass. */
21567 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21569 cur->code = 0;
21570 cur->info = NULL;
21573 else
21575 *slot = inc;
21576 inc->lineno = macinfo_htab->elements ();
21577 output_macinfo_op (inc);
21579 return count;
21582 /* Save any strings needed by the macinfo table in the debug str
21583 table. All strings must be collected into the table by the time
21584 index_string is called. */
21586 static void
21587 save_macinfo_strings (void)
21589 unsigned len;
21590 unsigned i;
21591 macinfo_entry *ref;
21593 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21595 switch (ref->code)
21597 /* Match the logic in output_macinfo_op to decide on
21598 indirect strings. */
21599 case DW_MACINFO_define:
21600 case DW_MACINFO_undef:
21601 len = strlen (ref->info) + 1;
21602 if (!dwarf_strict
21603 && len > DWARF_OFFSET_SIZE
21604 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21605 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21606 set_indirect_string (find_AT_string (ref->info));
21607 break;
21608 case DW_MACRO_GNU_define_indirect:
21609 case DW_MACRO_GNU_undef_indirect:
21610 set_indirect_string (find_AT_string (ref->info));
21611 break;
21612 default:
21613 break;
21618 /* Output macinfo section(s). */
21620 static void
21621 output_macinfo (void)
21623 unsigned i;
21624 unsigned long length = vec_safe_length (macinfo_table);
21625 macinfo_entry *ref;
21626 vec<macinfo_entry, va_gc> *files = NULL;
21627 macinfo_hash_type macinfo_htab;
21629 if (! length)
21630 return;
21632 /* output_macinfo* uses these interchangeably. */
21633 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21634 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21635 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21636 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21638 /* For .debug_macro emit the section header. */
21639 if (!dwarf_strict)
21641 dw2_asm_output_data (2, 4, "DWARF macro version number");
21642 if (DWARF_OFFSET_SIZE == 8)
21643 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21644 else
21645 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21646 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21647 (!dwarf_split_debug_info ? debug_line_section_label
21648 : debug_skeleton_line_section_label),
21649 debug_line_section, NULL);
21652 /* In the first loop, it emits the primary .debug_macinfo section
21653 and after each emitted op the macinfo_entry is cleared.
21654 If a longer range of define/undef ops can be optimized using
21655 DW_MACRO_GNU_transparent_include, the
21656 DW_MACRO_GNU_transparent_include op is emitted and kept in
21657 the vector before the first define/undef in the range and the
21658 whole range of define/undef ops is not emitted and kept. */
21659 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21661 switch (ref->code)
21663 case DW_MACINFO_start_file:
21664 vec_safe_push (files, *ref);
21665 break;
21666 case DW_MACINFO_end_file:
21667 if (!vec_safe_is_empty (files))
21668 files->pop ();
21669 break;
21670 case DW_MACINFO_define:
21671 case DW_MACINFO_undef:
21672 if (!dwarf_strict
21673 && HAVE_COMDAT_GROUP
21674 && vec_safe_length (files) != 1
21675 && i > 0
21676 && i + 1 < length
21677 && (*macinfo_table)[i - 1].code == 0)
21679 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21680 if (count)
21682 i += count - 1;
21683 continue;
21686 break;
21687 case 0:
21688 /* A dummy entry may be inserted at the beginning to be able
21689 to optimize the whole block of predefined macros. */
21690 if (i == 0)
21691 continue;
21692 default:
21693 break;
21695 output_macinfo_op (ref);
21696 ref->info = NULL;
21697 ref->code = 0;
21700 if (!macinfo_htab.is_created ())
21701 return;
21703 macinfo_htab.dispose ();
21705 /* If any DW_MACRO_GNU_transparent_include were used, on those
21706 DW_MACRO_GNU_transparent_include entries terminate the
21707 current chain and switch to a new comdat .debug_macinfo
21708 section and emit the define/undef entries within it. */
21709 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21710 switch (ref->code)
21712 case 0:
21713 continue;
21714 case DW_MACRO_GNU_transparent_include:
21716 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21717 tree comdat_key = get_identifier (ref->info);
21718 /* Terminate the previous .debug_macinfo section. */
21719 dw2_asm_output_data (1, 0, "End compilation unit");
21720 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21721 SECTION_DEBUG
21722 | SECTION_LINKONCE,
21723 comdat_key);
21724 ASM_GENERATE_INTERNAL_LABEL (label,
21725 DEBUG_MACRO_SECTION_LABEL,
21726 ref->lineno);
21727 ASM_OUTPUT_LABEL (asm_out_file, label);
21728 ref->code = 0;
21729 ref->info = NULL;
21730 dw2_asm_output_data (2, 4, "DWARF macro version number");
21731 if (DWARF_OFFSET_SIZE == 8)
21732 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21733 else
21734 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21736 break;
21737 case DW_MACINFO_define:
21738 case DW_MACINFO_undef:
21739 output_macinfo_op (ref);
21740 ref->code = 0;
21741 ref->info = NULL;
21742 break;
21743 default:
21744 gcc_unreachable ();
21748 /* Set up for Dwarf output at the start of compilation. */
21750 static void
21751 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21753 /* Allocate the file_table. */
21754 file_table = htab_create_ggc (50, file_table_hash,
21755 file_table_eq, NULL);
21757 /* Allocate the decl_die_table. */
21758 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21759 decl_die_table_eq, NULL);
21761 /* Allocate the decl_loc_table. */
21762 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21763 decl_loc_table_eq, NULL);
21765 /* Allocate the cached_dw_loc_list_table. */
21766 cached_dw_loc_list_table
21767 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21768 cached_dw_loc_list_table_eq, NULL);
21770 /* Allocate the initial hunk of the decl_scope_table. */
21771 vec_alloc (decl_scope_table, 256);
21773 /* Allocate the initial hunk of the abbrev_die_table. */
21774 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21775 (ABBREV_DIE_TABLE_INCREMENT);
21776 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21777 /* Zero-th entry is allocated, but unused. */
21778 abbrev_die_table_in_use = 1;
21780 /* Allocate the pubtypes and pubnames vectors. */
21781 vec_alloc (pubname_table, 32);
21782 vec_alloc (pubtype_table, 32);
21784 vec_alloc (incomplete_types, 64);
21786 vec_alloc (used_rtx_array, 32);
21788 if (!dwarf_split_debug_info)
21790 debug_info_section = get_section (DEBUG_INFO_SECTION,
21791 SECTION_DEBUG, NULL);
21792 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21793 SECTION_DEBUG, NULL);
21794 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21795 SECTION_DEBUG, NULL);
21797 else
21799 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21800 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21801 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21802 SECTION_DEBUG | SECTION_EXCLUDE,
21803 NULL);
21804 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21805 SECTION_DEBUG, NULL);
21806 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21807 SECTION_DEBUG, NULL);
21808 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21809 SECTION_DEBUG, NULL);
21810 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21811 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21813 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21814 the main .o, but the skeleton_line goes into the split off dwo. */
21815 debug_skeleton_line_section
21816 = get_section (DEBUG_DWO_LINE_SECTION,
21817 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21818 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21819 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21820 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21821 SECTION_DEBUG | SECTION_EXCLUDE,
21822 NULL);
21823 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21824 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21825 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21826 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21827 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
21828 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
21830 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21831 SECTION_DEBUG, NULL);
21832 debug_macinfo_section = get_section (dwarf_strict
21833 ? DEBUG_MACINFO_SECTION
21834 : DEBUG_MACRO_SECTION,
21835 DEBUG_MACRO_SECTION_FLAGS, NULL);
21836 debug_line_section = get_section (DEBUG_LINE_SECTION,
21837 SECTION_DEBUG, NULL);
21838 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21839 SECTION_DEBUG, NULL);
21840 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21841 SECTION_DEBUG, NULL);
21842 debug_str_section = get_section (DEBUG_STR_SECTION,
21843 DEBUG_STR_SECTION_FLAGS, NULL);
21844 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21845 SECTION_DEBUG, NULL);
21846 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21847 SECTION_DEBUG, NULL);
21849 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21850 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21851 DEBUG_ABBREV_SECTION_LABEL, 0);
21852 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21853 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21854 COLD_TEXT_SECTION_LABEL, 0);
21855 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21857 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21858 DEBUG_INFO_SECTION_LABEL, 0);
21859 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21860 DEBUG_LINE_SECTION_LABEL, 0);
21861 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21862 DEBUG_RANGES_SECTION_LABEL, 0);
21863 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21864 DEBUG_ADDR_SECTION_LABEL, 0);
21865 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21866 dwarf_strict
21867 ? DEBUG_MACINFO_SECTION_LABEL
21868 : DEBUG_MACRO_SECTION_LABEL, 0);
21869 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21871 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21872 vec_alloc (macinfo_table, 64);
21874 switch_to_section (text_section);
21875 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21877 /* Make sure the line number table for .text always exists. */
21878 text_section_line_info = new_line_info_table ();
21879 text_section_line_info->end_label = text_end_label;
21882 /* Called before compile () starts outputtting functions, variables
21883 and toplevel asms into assembly. */
21885 static void
21886 dwarf2out_assembly_start (void)
21888 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21889 && dwarf2out_do_cfi_asm ()
21890 && (!(flag_unwind_tables || flag_exceptions)
21891 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21892 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21895 /* A helper function for dwarf2out_finish called through
21896 htab_traverse. Assign a string its index. All strings must be
21897 collected into the table by the time index_string is called,
21898 because the indexing code relies on htab_traverse to traverse nodes
21899 in the same order for each run. */
21901 static int
21902 index_string (void **h, void *v)
21904 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21905 unsigned int *index = (unsigned int *) v;
21907 find_string_form (node);
21908 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21910 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21911 node->index = *index;
21912 *index += 1;
21914 return 1;
21917 /* A helper function for output_indirect_strings called through
21918 htab_traverse. Output the offset to a string and update the
21919 current offset. */
21921 static int
21922 output_index_string_offset (void **h, void *v)
21924 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21925 unsigned int *offset = (unsigned int *) v;
21927 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21929 /* Assert that this node has been assigned an index. */
21930 gcc_assert (node->index != NO_INDEX_ASSIGNED
21931 && node->index != NOT_INDEXED);
21932 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21933 "indexed string 0x%x: %s", node->index, node->str);
21934 *offset += strlen (node->str) + 1;
21936 return 1;
21939 /* A helper function for dwarf2out_finish called through
21940 htab_traverse. Output the indexed string. */
21942 static int
21943 output_index_string (void **h, void *v)
21945 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21946 unsigned int *cur_idx = (unsigned int *) v;
21948 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21950 /* Assert that the strings are output in the same order as their
21951 indexes were assigned. */
21952 gcc_assert (*cur_idx == node->index);
21953 assemble_string (node->str, strlen (node->str) + 1);
21954 *cur_idx += 1;
21956 return 1;
21959 /* A helper function for dwarf2out_finish called through
21960 htab_traverse. Emit one queued .debug_str string. */
21962 static int
21963 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21965 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21967 node->form = find_string_form (node);
21968 if (node->form == DW_FORM_strp && node->refcount > 0)
21970 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21971 assemble_string (node->str, strlen (node->str) + 1);
21974 return 1;
21977 /* Output the indexed string table. */
21979 static void
21980 output_indirect_strings (void)
21982 switch_to_section (debug_str_section);
21983 if (!dwarf_split_debug_info)
21984 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21985 else
21987 unsigned int offset = 0;
21988 unsigned int cur_idx = 0;
21990 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
21992 switch_to_section (debug_str_offsets_section);
21993 htab_traverse_noresize (debug_str_hash,
21994 output_index_string_offset,
21995 &offset);
21996 switch_to_section (debug_str_dwo_section);
21997 htab_traverse_noresize (debug_str_hash,
21998 output_index_string,
21999 &cur_idx);
22003 /* Callback for htab_traverse to assign an index to an entry in the
22004 table, and to write that entry to the .debug_addr section. */
22006 static int
22007 output_addr_table_entry (void **slot, void *data)
22009 addr_table_entry *entry = (addr_table_entry *) *slot;
22010 unsigned int *cur_index = (unsigned int *)data;
22012 if (entry->refcount == 0)
22014 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22015 || entry->index == NOT_INDEXED);
22016 return 1;
22019 gcc_assert (entry->index == *cur_index);
22020 (*cur_index)++;
22022 switch (entry->kind)
22024 case ate_kind_rtx:
22025 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22026 "0x%x", entry->index);
22027 break;
22028 case ate_kind_rtx_dtprel:
22029 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22030 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22031 DWARF2_ADDR_SIZE,
22032 entry->addr.rtl);
22033 fputc ('\n', asm_out_file);
22034 break;
22035 case ate_kind_label:
22036 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22037 "0x%x", entry->index);
22038 break;
22039 default:
22040 gcc_unreachable ();
22042 return 1;
22045 /* Produce the .debug_addr section. */
22047 static void
22048 output_addr_table (void)
22050 unsigned int index = 0;
22051 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22052 return;
22054 switch_to_section (debug_addr_section);
22055 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22058 #if ENABLE_ASSERT_CHECKING
22059 /* Verify that all marks are clear. */
22061 static void
22062 verify_marks_clear (dw_die_ref die)
22064 dw_die_ref c;
22066 gcc_assert (! die->die_mark);
22067 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22069 #endif /* ENABLE_ASSERT_CHECKING */
22071 /* Clear the marks for a die and its children.
22072 Be cool if the mark isn't set. */
22074 static void
22075 prune_unmark_dies (dw_die_ref die)
22077 dw_die_ref c;
22079 if (die->die_mark)
22080 die->die_mark = 0;
22081 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22084 /* Given DIE that we're marking as used, find any other dies
22085 it references as attributes and mark them as used. */
22087 static void
22088 prune_unused_types_walk_attribs (dw_die_ref die)
22090 dw_attr_ref a;
22091 unsigned ix;
22093 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22095 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22097 /* A reference to another DIE.
22098 Make sure that it will get emitted.
22099 If it was broken out into a comdat group, don't follow it. */
22100 if (! AT_ref (a)->comdat_type_p
22101 || a->dw_attr == DW_AT_specification)
22102 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22104 /* Set the string's refcount to 0 so that prune_unused_types_mark
22105 accounts properly for it. */
22106 if (AT_class (a) == dw_val_class_str)
22107 a->dw_attr_val.v.val_str->refcount = 0;
22111 /* Mark the generic parameters and arguments children DIEs of DIE. */
22113 static void
22114 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22116 dw_die_ref c;
22118 if (die == NULL || die->die_child == NULL)
22119 return;
22120 c = die->die_child;
22123 switch (c->die_tag)
22125 case DW_TAG_template_type_param:
22126 case DW_TAG_template_value_param:
22127 case DW_TAG_GNU_template_template_param:
22128 case DW_TAG_GNU_template_parameter_pack:
22129 prune_unused_types_mark (c, 1);
22130 break;
22131 default:
22132 break;
22134 c = c->die_sib;
22135 } while (c && c != die->die_child);
22138 /* Mark DIE as being used. If DOKIDS is true, then walk down
22139 to DIE's children. */
22141 static void
22142 prune_unused_types_mark (dw_die_ref die, int dokids)
22144 dw_die_ref c;
22146 if (die->die_mark == 0)
22148 /* We haven't done this node yet. Mark it as used. */
22149 die->die_mark = 1;
22150 /* If this is the DIE of a generic type instantiation,
22151 mark the children DIEs that describe its generic parms and
22152 args. */
22153 prune_unused_types_mark_generic_parms_dies (die);
22155 /* We also have to mark its parents as used.
22156 (But we don't want to mark our parent's kids due to this,
22157 unless it is a class.) */
22158 if (die->die_parent)
22159 prune_unused_types_mark (die->die_parent,
22160 class_scope_p (die->die_parent));
22162 /* Mark any referenced nodes. */
22163 prune_unused_types_walk_attribs (die);
22165 /* If this node is a specification,
22166 also mark the definition, if it exists. */
22167 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22168 prune_unused_types_mark (die->die_definition, 1);
22171 if (dokids && die->die_mark != 2)
22173 /* We need to walk the children, but haven't done so yet.
22174 Remember that we've walked the kids. */
22175 die->die_mark = 2;
22177 /* If this is an array type, we need to make sure our
22178 kids get marked, even if they're types. If we're
22179 breaking out types into comdat sections, do this
22180 for all type definitions. */
22181 if (die->die_tag == DW_TAG_array_type
22182 || (use_debug_types
22183 && is_type_die (die) && ! is_declaration_die (die)))
22184 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22185 else
22186 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22190 /* For local classes, look if any static member functions were emitted
22191 and if so, mark them. */
22193 static void
22194 prune_unused_types_walk_local_classes (dw_die_ref die)
22196 dw_die_ref c;
22198 if (die->die_mark == 2)
22199 return;
22201 switch (die->die_tag)
22203 case DW_TAG_structure_type:
22204 case DW_TAG_union_type:
22205 case DW_TAG_class_type:
22206 break;
22208 case DW_TAG_subprogram:
22209 if (!get_AT_flag (die, DW_AT_declaration)
22210 || die->die_definition != NULL)
22211 prune_unused_types_mark (die, 1);
22212 return;
22214 default:
22215 return;
22218 /* Mark children. */
22219 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22222 /* Walk the tree DIE and mark types that we actually use. */
22224 static void
22225 prune_unused_types_walk (dw_die_ref die)
22227 dw_die_ref c;
22229 /* Don't do anything if this node is already marked and
22230 children have been marked as well. */
22231 if (die->die_mark == 2)
22232 return;
22234 switch (die->die_tag)
22236 case DW_TAG_structure_type:
22237 case DW_TAG_union_type:
22238 case DW_TAG_class_type:
22239 if (die->die_perennial_p)
22240 break;
22242 for (c = die->die_parent; c; c = c->die_parent)
22243 if (c->die_tag == DW_TAG_subprogram)
22244 break;
22246 /* Finding used static member functions inside of classes
22247 is needed just for local classes, because for other classes
22248 static member function DIEs with DW_AT_specification
22249 are emitted outside of the DW_TAG_*_type. If we ever change
22250 it, we'd need to call this even for non-local classes. */
22251 if (c)
22252 prune_unused_types_walk_local_classes (die);
22254 /* It's a type node --- don't mark it. */
22255 return;
22257 case DW_TAG_const_type:
22258 case DW_TAG_packed_type:
22259 case DW_TAG_pointer_type:
22260 case DW_TAG_reference_type:
22261 case DW_TAG_rvalue_reference_type:
22262 case DW_TAG_volatile_type:
22263 case DW_TAG_typedef:
22264 case DW_TAG_array_type:
22265 case DW_TAG_interface_type:
22266 case DW_TAG_friend:
22267 case DW_TAG_variant_part:
22268 case DW_TAG_enumeration_type:
22269 case DW_TAG_subroutine_type:
22270 case DW_TAG_string_type:
22271 case DW_TAG_set_type:
22272 case DW_TAG_subrange_type:
22273 case DW_TAG_ptr_to_member_type:
22274 case DW_TAG_file_type:
22275 if (die->die_perennial_p)
22276 break;
22278 /* It's a type node --- don't mark it. */
22279 return;
22281 default:
22282 /* Mark everything else. */
22283 break;
22286 if (die->die_mark == 0)
22288 die->die_mark = 1;
22290 /* Now, mark any dies referenced from here. */
22291 prune_unused_types_walk_attribs (die);
22294 die->die_mark = 2;
22296 /* Mark children. */
22297 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22300 /* Increment the string counts on strings referred to from DIE's
22301 attributes. */
22303 static void
22304 prune_unused_types_update_strings (dw_die_ref die)
22306 dw_attr_ref a;
22307 unsigned ix;
22309 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22310 if (AT_class (a) == dw_val_class_str)
22312 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22313 s->refcount++;
22314 /* Avoid unnecessarily putting strings that are used less than
22315 twice in the hash table. */
22316 if (s->refcount
22317 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22319 void ** slot;
22320 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22321 htab_hash_string (s->str),
22322 INSERT);
22323 gcc_assert (*slot == NULL);
22324 *slot = s;
22329 /* Remove from the tree DIE any dies that aren't marked. */
22331 static void
22332 prune_unused_types_prune (dw_die_ref die)
22334 dw_die_ref c;
22336 gcc_assert (die->die_mark);
22337 prune_unused_types_update_strings (die);
22339 if (! die->die_child)
22340 return;
22342 c = die->die_child;
22343 do {
22344 dw_die_ref prev = c;
22345 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22346 if (c == die->die_child)
22348 /* No marked children between 'prev' and the end of the list. */
22349 if (prev == c)
22350 /* No marked children at all. */
22351 die->die_child = NULL;
22352 else
22354 prev->die_sib = c->die_sib;
22355 die->die_child = prev;
22357 return;
22360 if (c != prev->die_sib)
22361 prev->die_sib = c;
22362 prune_unused_types_prune (c);
22363 } while (c != die->die_child);
22366 /* Remove dies representing declarations that we never use. */
22368 static void
22369 prune_unused_types (void)
22371 unsigned int i;
22372 limbo_die_node *node;
22373 comdat_type_node *ctnode;
22374 pubname_ref pub;
22375 dw_die_ref base_type;
22377 #if ENABLE_ASSERT_CHECKING
22378 /* All the marks should already be clear. */
22379 verify_marks_clear (comp_unit_die ());
22380 for (node = limbo_die_list; node; node = node->next)
22381 verify_marks_clear (node->die);
22382 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22383 verify_marks_clear (ctnode->root_die);
22384 #endif /* ENABLE_ASSERT_CHECKING */
22386 /* Mark types that are used in global variables. */
22387 premark_types_used_by_global_vars ();
22389 /* Set the mark on nodes that are actually used. */
22390 prune_unused_types_walk (comp_unit_die ());
22391 for (node = limbo_die_list; node; node = node->next)
22392 prune_unused_types_walk (node->die);
22393 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22395 prune_unused_types_walk (ctnode->root_die);
22396 prune_unused_types_mark (ctnode->type_die, 1);
22399 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22400 are unusual in that they are pubnames that are the children of pubtypes.
22401 They should only be marked via their parent DW_TAG_enumeration_type die,
22402 not as roots in themselves. */
22403 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22404 if (pub->die->die_tag != DW_TAG_enumerator)
22405 prune_unused_types_mark (pub->die, 1);
22406 for (i = 0; base_types.iterate (i, &base_type); i++)
22407 prune_unused_types_mark (base_type, 1);
22409 if (debug_str_hash)
22410 htab_empty (debug_str_hash);
22411 if (skeleton_debug_str_hash)
22412 htab_empty (skeleton_debug_str_hash);
22413 prune_unused_types_prune (comp_unit_die ());
22414 for (node = limbo_die_list; node; node = node->next)
22415 prune_unused_types_prune (node->die);
22416 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22417 prune_unused_types_prune (ctnode->root_die);
22419 /* Leave the marks clear. */
22420 prune_unmark_dies (comp_unit_die ());
22421 for (node = limbo_die_list; node; node = node->next)
22422 prune_unmark_dies (node->die);
22423 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22424 prune_unmark_dies (ctnode->root_die);
22427 /* Set the parameter to true if there are any relative pathnames in
22428 the file table. */
22429 static int
22430 file_table_relative_p (void ** slot, void *param)
22432 bool *p = (bool *) param;
22433 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22434 if (!IS_ABSOLUTE_PATH (d->filename))
22436 *p = true;
22437 return 0;
22439 return 1;
22442 /* Helpers to manipulate hash table of comdat type units. */
22444 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22446 typedef comdat_type_node value_type;
22447 typedef comdat_type_node compare_type;
22448 static inline hashval_t hash (const value_type *);
22449 static inline bool equal (const value_type *, const compare_type *);
22452 inline hashval_t
22453 comdat_type_hasher::hash (const value_type *type_node)
22455 hashval_t h;
22456 memcpy (&h, type_node->signature, sizeof (h));
22457 return h;
22460 inline bool
22461 comdat_type_hasher::equal (const value_type *type_node_1,
22462 const compare_type *type_node_2)
22464 return (! memcmp (type_node_1->signature, type_node_2->signature,
22465 DWARF_TYPE_SIGNATURE_SIZE));
22468 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22469 to the location it would have been added, should we know its
22470 DECL_ASSEMBLER_NAME when we added other attributes. This will
22471 probably improve compactness of debug info, removing equivalent
22472 abbrevs, and hide any differences caused by deferring the
22473 computation of the assembler name, triggered by e.g. PCH. */
22475 static inline void
22476 move_linkage_attr (dw_die_ref die)
22478 unsigned ix = vec_safe_length (die->die_attr);
22479 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22481 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22482 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22484 while (--ix > 0)
22486 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22488 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22489 break;
22492 if (ix != vec_safe_length (die->die_attr) - 1)
22494 die->die_attr->pop ();
22495 die->die_attr->quick_insert (ix, linkage);
22499 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22500 referenced from typed stack ops and count how often they are used. */
22502 static void
22503 mark_base_types (dw_loc_descr_ref loc)
22505 dw_die_ref base_type = NULL;
22507 for (; loc; loc = loc->dw_loc_next)
22509 switch (loc->dw_loc_opc)
22511 case DW_OP_GNU_regval_type:
22512 case DW_OP_GNU_deref_type:
22513 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22514 break;
22515 case DW_OP_GNU_convert:
22516 case DW_OP_GNU_reinterpret:
22517 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22518 continue;
22519 /* FALLTHRU */
22520 case DW_OP_GNU_const_type:
22521 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22522 break;
22523 case DW_OP_GNU_entry_value:
22524 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22525 continue;
22526 default:
22527 continue;
22529 gcc_assert (base_type->die_parent == comp_unit_die ());
22530 if (base_type->die_mark)
22531 base_type->die_mark++;
22532 else
22534 base_types.safe_push (base_type);
22535 base_type->die_mark = 1;
22540 /* Comparison function for sorting marked base types. */
22542 static int
22543 base_type_cmp (const void *x, const void *y)
22545 dw_die_ref dx = *(const dw_die_ref *) x;
22546 dw_die_ref dy = *(const dw_die_ref *) y;
22547 unsigned int byte_size1, byte_size2;
22548 unsigned int encoding1, encoding2;
22549 if (dx->die_mark > dy->die_mark)
22550 return -1;
22551 if (dx->die_mark < dy->die_mark)
22552 return 1;
22553 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22554 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22555 if (byte_size1 < byte_size2)
22556 return 1;
22557 if (byte_size1 > byte_size2)
22558 return -1;
22559 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22560 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22561 if (encoding1 < encoding2)
22562 return 1;
22563 if (encoding1 > encoding2)
22564 return -1;
22565 return 0;
22568 /* Move base types marked by mark_base_types as early as possible
22569 in the CU, sorted by decreasing usage count both to make the
22570 uleb128 references as small as possible and to make sure they
22571 will have die_offset already computed by calc_die_sizes when
22572 sizes of typed stack loc ops is computed. */
22574 static void
22575 move_marked_base_types (void)
22577 unsigned int i;
22578 dw_die_ref base_type, die, c;
22580 if (base_types.is_empty ())
22581 return;
22583 /* Sort by decreasing usage count, they will be added again in that
22584 order later on. */
22585 base_types.qsort (base_type_cmp);
22586 die = comp_unit_die ();
22587 c = die->die_child;
22590 dw_die_ref prev = c;
22591 c = c->die_sib;
22592 while (c->die_mark)
22594 remove_child_with_prev (c, prev);
22595 /* As base types got marked, there must be at least
22596 one node other than DW_TAG_base_type. */
22597 gcc_assert (c != c->die_sib);
22598 c = c->die_sib;
22601 while (c != die->die_child);
22602 gcc_assert (die->die_child);
22603 c = die->die_child;
22604 for (i = 0; base_types.iterate (i, &base_type); i++)
22606 base_type->die_mark = 0;
22607 base_type->die_sib = c->die_sib;
22608 c->die_sib = base_type;
22609 c = base_type;
22613 /* Helper function for resolve_addr, attempt to resolve
22614 one CONST_STRING, return non-zero if not successful. Similarly verify that
22615 SYMBOL_REFs refer to variables emitted in the current CU. */
22617 static int
22618 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22620 rtx rtl = *addr;
22622 if (GET_CODE (rtl) == CONST_STRING)
22624 size_t len = strlen (XSTR (rtl, 0)) + 1;
22625 tree t = build_string (len, XSTR (rtl, 0));
22626 tree tlen = size_int (len - 1);
22627 TREE_TYPE (t)
22628 = build_array_type (char_type_node, build_index_type (tlen));
22629 rtl = lookup_constant_def (t);
22630 if (!rtl || !MEM_P (rtl))
22631 return 1;
22632 rtl = XEXP (rtl, 0);
22633 if (GET_CODE (rtl) == SYMBOL_REF
22634 && SYMBOL_REF_DECL (rtl)
22635 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22636 return 1;
22637 vec_safe_push (used_rtx_array, rtl);
22638 *addr = rtl;
22639 return 0;
22642 if (GET_CODE (rtl) == SYMBOL_REF
22643 && SYMBOL_REF_DECL (rtl))
22645 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22647 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22648 return 1;
22650 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22651 return 1;
22654 if (GET_CODE (rtl) == CONST
22655 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22656 return 1;
22658 return 0;
22661 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22662 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22663 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22665 static rtx
22666 string_cst_pool_decl (tree t)
22668 rtx rtl = output_constant_def (t, 1);
22669 unsigned char *array;
22670 dw_loc_descr_ref l;
22671 tree decl;
22672 size_t len;
22673 dw_die_ref ref;
22675 if (!rtl || !MEM_P (rtl))
22676 return NULL_RTX;
22677 rtl = XEXP (rtl, 0);
22678 if (GET_CODE (rtl) != SYMBOL_REF
22679 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22680 return NULL_RTX;
22682 decl = SYMBOL_REF_DECL (rtl);
22683 if (!lookup_decl_die (decl))
22685 len = TREE_STRING_LENGTH (t);
22686 vec_safe_push (used_rtx_array, rtl);
22687 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22688 array = (unsigned char *) ggc_alloc_atomic (len);
22689 memcpy (array, TREE_STRING_POINTER (t), len);
22690 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22691 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22692 l->dw_loc_oprnd2.v.val_vec.length = len;
22693 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22694 l->dw_loc_oprnd2.v.val_vec.array = array;
22695 add_AT_loc (ref, DW_AT_location, l);
22696 equate_decl_number_to_die (decl, ref);
22698 return rtl;
22701 /* Helper function of resolve_addr_in_expr. LOC is
22702 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22703 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22704 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22705 with DW_OP_GNU_implicit_pointer if possible
22706 and return true, if unsuccesful, return false. */
22708 static bool
22709 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22711 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22712 HOST_WIDE_INT offset = 0;
22713 dw_die_ref ref = NULL;
22714 tree decl;
22716 if (GET_CODE (rtl) == CONST
22717 && GET_CODE (XEXP (rtl, 0)) == PLUS
22718 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22720 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22721 rtl = XEXP (XEXP (rtl, 0), 0);
22723 if (GET_CODE (rtl) == CONST_STRING)
22725 size_t len = strlen (XSTR (rtl, 0)) + 1;
22726 tree t = build_string (len, XSTR (rtl, 0));
22727 tree tlen = size_int (len - 1);
22729 TREE_TYPE (t)
22730 = build_array_type (char_type_node, build_index_type (tlen));
22731 rtl = string_cst_pool_decl (t);
22732 if (!rtl)
22733 return false;
22735 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22737 decl = SYMBOL_REF_DECL (rtl);
22738 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22740 ref = lookup_decl_die (decl);
22741 if (ref && (get_AT (ref, DW_AT_location)
22742 || get_AT (ref, DW_AT_const_value)))
22744 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22745 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22746 loc->dw_loc_oprnd1.val_entry = NULL;
22747 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22748 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22749 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22750 loc->dw_loc_oprnd2.v.val_int = offset;
22751 return true;
22755 return false;
22758 /* Helper function for resolve_addr, handle one location
22759 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22760 the location list couldn't be resolved. */
22762 static bool
22763 resolve_addr_in_expr (dw_loc_descr_ref loc)
22765 dw_loc_descr_ref keep = NULL;
22766 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22767 switch (loc->dw_loc_opc)
22769 case DW_OP_addr:
22770 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22772 if ((prev == NULL
22773 || prev->dw_loc_opc == DW_OP_piece
22774 || prev->dw_loc_opc == DW_OP_bit_piece)
22775 && loc->dw_loc_next
22776 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22777 && !dwarf_strict
22778 && optimize_one_addr_into_implicit_ptr (loc))
22779 break;
22780 return false;
22782 break;
22783 case DW_OP_GNU_addr_index:
22784 case DW_OP_GNU_const_index:
22785 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22786 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22787 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22788 NULL))
22789 return false;
22790 break;
22791 case DW_OP_const4u:
22792 case DW_OP_const8u:
22793 if (loc->dtprel
22794 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22795 return false;
22796 break;
22797 case DW_OP_plus_uconst:
22798 if (size_of_loc_descr (loc)
22799 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22801 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22803 dw_loc_descr_ref repl
22804 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22805 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22806 add_loc_descr (&repl, loc->dw_loc_next);
22807 *loc = *repl;
22809 break;
22810 case DW_OP_implicit_value:
22811 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22812 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22813 return false;
22814 break;
22815 case DW_OP_GNU_implicit_pointer:
22816 case DW_OP_GNU_parameter_ref:
22817 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22819 dw_die_ref ref
22820 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22821 if (ref == NULL)
22822 return false;
22823 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22824 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22825 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22827 break;
22828 case DW_OP_GNU_const_type:
22829 case DW_OP_GNU_regval_type:
22830 case DW_OP_GNU_deref_type:
22831 case DW_OP_GNU_convert:
22832 case DW_OP_GNU_reinterpret:
22833 while (loc->dw_loc_next
22834 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22836 dw_die_ref base1, base2;
22837 unsigned enc1, enc2, size1, size2;
22838 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22839 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22840 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22841 else if (loc->dw_loc_oprnd1.val_class
22842 == dw_val_class_unsigned_const)
22843 break;
22844 else
22845 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22846 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22847 == dw_val_class_unsigned_const)
22848 break;
22849 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22850 gcc_assert (base1->die_tag == DW_TAG_base_type
22851 && base2->die_tag == DW_TAG_base_type);
22852 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22853 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22854 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22855 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22856 if (size1 == size2
22857 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22858 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22859 && loc != keep)
22860 || enc1 == enc2))
22862 /* Optimize away next DW_OP_GNU_convert after
22863 adjusting LOC's base type die reference. */
22864 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22865 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22866 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22867 else
22868 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22869 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22870 continue;
22872 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22873 point typed stack entry. */
22874 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22875 keep = loc->dw_loc_next;
22876 break;
22878 break;
22879 default:
22880 break;
22882 return true;
22885 /* Helper function of resolve_addr. DIE had DW_AT_location of
22886 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
22887 and DW_OP_addr couldn't be resolved. resolve_addr has already
22888 removed the DW_AT_location attribute. This function attempts to
22889 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
22890 to it or DW_AT_const_value attribute, if possible. */
22892 static void
22893 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
22895 if (TREE_CODE (decl) != VAR_DECL
22896 || lookup_decl_die (decl) != die
22897 || DECL_EXTERNAL (decl)
22898 || !TREE_STATIC (decl)
22899 || DECL_INITIAL (decl) == NULL_TREE
22900 || DECL_P (DECL_INITIAL (decl))
22901 || get_AT (die, DW_AT_const_value))
22902 return;
22904 tree init = DECL_INITIAL (decl);
22905 HOST_WIDE_INT offset = 0;
22906 /* For variables that have been optimized away and thus
22907 don't have a memory location, see if we can emit
22908 DW_AT_const_value instead. */
22909 if (tree_add_const_value_attribute (die, init))
22910 return;
22911 if (dwarf_strict)
22912 return;
22913 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
22914 and ADDR_EXPR refers to a decl that has DW_AT_location or
22915 DW_AT_const_value (but isn't addressable, otherwise
22916 resolving the original DW_OP_addr wouldn't fail), see if
22917 we can add DW_OP_GNU_implicit_pointer. */
22918 STRIP_NOPS (init);
22919 if (TREE_CODE (init) == POINTER_PLUS_EXPR
22920 && host_integerp (TREE_OPERAND (init, 1), 0))
22922 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
22923 init = TREE_OPERAND (init, 0);
22924 STRIP_NOPS (init);
22926 if (TREE_CODE (init) != ADDR_EXPR)
22927 return;
22928 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
22929 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
22930 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
22931 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
22932 && TREE_OPERAND (init, 0) != decl))
22934 dw_die_ref ref;
22935 dw_loc_descr_ref l;
22937 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
22939 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
22940 if (!rtl)
22941 return;
22942 decl = SYMBOL_REF_DECL (rtl);
22944 else
22945 decl = TREE_OPERAND (init, 0);
22946 ref = lookup_decl_die (decl);
22947 if (ref == NULL
22948 || (!get_AT (ref, DW_AT_location)
22949 && !get_AT (ref, DW_AT_const_value)))
22950 return;
22951 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
22952 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22953 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
22954 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
22955 add_AT_loc (die, DW_AT_location, l);
22959 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22960 an address in .rodata section if the string literal is emitted there,
22961 or remove the containing location list or replace DW_AT_const_value
22962 with DW_AT_location and empty location expression, if it isn't found
22963 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22964 to something that has been emitted in the current CU. */
22966 static void
22967 resolve_addr (dw_die_ref die)
22969 dw_die_ref c;
22970 dw_attr_ref a;
22971 dw_loc_list_ref *curr, *start, loc;
22972 unsigned ix;
22974 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22975 switch (AT_class (a))
22977 case dw_val_class_loc_list:
22978 start = curr = AT_loc_list_ptr (a);
22979 loc = *curr;
22980 gcc_assert (loc);
22981 /* The same list can be referenced more than once. See if we have
22982 already recorded the result from a previous pass. */
22983 if (loc->replaced)
22984 *curr = loc->dw_loc_next;
22985 else if (!loc->resolved_addr)
22987 /* As things stand, we do not expect or allow one die to
22988 reference a suffix of another die's location list chain.
22989 References must be identical or completely separate.
22990 There is therefore no need to cache the result of this
22991 pass on any list other than the first; doing so
22992 would lead to unnecessary writes. */
22993 while (*curr)
22995 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22996 if (!resolve_addr_in_expr ((*curr)->expr))
22998 dw_loc_list_ref next = (*curr)->dw_loc_next;
22999 dw_loc_descr_ref l = (*curr)->expr;
23001 if (next && (*curr)->ll_symbol)
23003 gcc_assert (!next->ll_symbol);
23004 next->ll_symbol = (*curr)->ll_symbol;
23006 if (dwarf_split_debug_info)
23007 remove_loc_list_addr_table_entries (l);
23008 *curr = next;
23010 else
23012 mark_base_types ((*curr)->expr);
23013 curr = &(*curr)->dw_loc_next;
23016 if (loc == *start)
23017 loc->resolved_addr = 1;
23018 else
23020 loc->replaced = 1;
23021 loc->dw_loc_next = *start;
23024 if (!*start)
23026 remove_AT (die, a->dw_attr);
23027 ix--;
23029 break;
23030 case dw_val_class_loc:
23032 dw_loc_descr_ref l = AT_loc (a);
23033 /* For -gdwarf-2 don't attempt to optimize
23034 DW_AT_data_member_location containing
23035 DW_OP_plus_uconst - older consumers might
23036 rely on it being that op instead of a more complex,
23037 but shorter, location description. */
23038 if ((dwarf_version > 2
23039 || a->dw_attr != DW_AT_data_member_location
23040 || l == NULL
23041 || l->dw_loc_opc != DW_OP_plus_uconst
23042 || l->dw_loc_next != NULL)
23043 && !resolve_addr_in_expr (l))
23045 if (dwarf_split_debug_info)
23046 remove_loc_list_addr_table_entries (l);
23047 if (l != NULL
23048 && l->dw_loc_next == NULL
23049 && l->dw_loc_opc == DW_OP_addr
23050 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23051 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23052 && a->dw_attr == DW_AT_location)
23054 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23055 remove_AT (die, a->dw_attr);
23056 ix--;
23057 optimize_location_into_implicit_ptr (die, decl);
23058 break;
23060 remove_AT (die, a->dw_attr);
23061 ix--;
23063 else
23064 mark_base_types (l);
23066 break;
23067 case dw_val_class_addr:
23068 if (a->dw_attr == DW_AT_const_value
23069 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23071 if (AT_index (a) != NOT_INDEXED)
23072 remove_addr_table_entry (a->dw_attr_val.val_entry);
23073 remove_AT (die, a->dw_attr);
23074 ix--;
23076 if (die->die_tag == DW_TAG_GNU_call_site
23077 && a->dw_attr == DW_AT_abstract_origin)
23079 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23080 dw_die_ref tdie = lookup_decl_die (tdecl);
23081 if (tdie == NULL
23082 && DECL_EXTERNAL (tdecl)
23083 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23085 force_decl_die (tdecl);
23086 tdie = lookup_decl_die (tdecl);
23088 if (tdie)
23090 a->dw_attr_val.val_class = dw_val_class_die_ref;
23091 a->dw_attr_val.v.val_die_ref.die = tdie;
23092 a->dw_attr_val.v.val_die_ref.external = 0;
23094 else
23096 if (AT_index (a) != NOT_INDEXED)
23097 remove_addr_table_entry (a->dw_attr_val.val_entry);
23098 remove_AT (die, a->dw_attr);
23099 ix--;
23102 break;
23103 default:
23104 break;
23107 FOR_EACH_CHILD (die, c, resolve_addr (c));
23110 /* Helper routines for optimize_location_lists.
23111 This pass tries to share identical local lists in .debug_loc
23112 section. */
23114 /* Iteratively hash operands of LOC opcode. */
23116 static hashval_t
23117 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23119 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23120 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23122 switch (loc->dw_loc_opc)
23124 case DW_OP_const4u:
23125 case DW_OP_const8u:
23126 if (loc->dtprel)
23127 goto hash_addr;
23128 /* FALLTHRU */
23129 case DW_OP_const1u:
23130 case DW_OP_const1s:
23131 case DW_OP_const2u:
23132 case DW_OP_const2s:
23133 case DW_OP_const4s:
23134 case DW_OP_const8s:
23135 case DW_OP_constu:
23136 case DW_OP_consts:
23137 case DW_OP_pick:
23138 case DW_OP_plus_uconst:
23139 case DW_OP_breg0:
23140 case DW_OP_breg1:
23141 case DW_OP_breg2:
23142 case DW_OP_breg3:
23143 case DW_OP_breg4:
23144 case DW_OP_breg5:
23145 case DW_OP_breg6:
23146 case DW_OP_breg7:
23147 case DW_OP_breg8:
23148 case DW_OP_breg9:
23149 case DW_OP_breg10:
23150 case DW_OP_breg11:
23151 case DW_OP_breg12:
23152 case DW_OP_breg13:
23153 case DW_OP_breg14:
23154 case DW_OP_breg15:
23155 case DW_OP_breg16:
23156 case DW_OP_breg17:
23157 case DW_OP_breg18:
23158 case DW_OP_breg19:
23159 case DW_OP_breg20:
23160 case DW_OP_breg21:
23161 case DW_OP_breg22:
23162 case DW_OP_breg23:
23163 case DW_OP_breg24:
23164 case DW_OP_breg25:
23165 case DW_OP_breg26:
23166 case DW_OP_breg27:
23167 case DW_OP_breg28:
23168 case DW_OP_breg29:
23169 case DW_OP_breg30:
23170 case DW_OP_breg31:
23171 case DW_OP_regx:
23172 case DW_OP_fbreg:
23173 case DW_OP_piece:
23174 case DW_OP_deref_size:
23175 case DW_OP_xderef_size:
23176 hash = iterative_hash_object (val1->v.val_int, hash);
23177 break;
23178 case DW_OP_skip:
23179 case DW_OP_bra:
23181 int offset;
23183 gcc_assert (val1->val_class == dw_val_class_loc);
23184 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23185 hash = iterative_hash_object (offset, hash);
23187 break;
23188 case DW_OP_implicit_value:
23189 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23190 switch (val2->val_class)
23192 case dw_val_class_const:
23193 hash = iterative_hash_object (val2->v.val_int, hash);
23194 break;
23195 case dw_val_class_vec:
23197 unsigned int elt_size = val2->v.val_vec.elt_size;
23198 unsigned int len = val2->v.val_vec.length;
23200 hash = iterative_hash_object (elt_size, hash);
23201 hash = iterative_hash_object (len, hash);
23202 hash = iterative_hash (val2->v.val_vec.array,
23203 len * elt_size, hash);
23205 break;
23206 case dw_val_class_const_double:
23207 hash = iterative_hash_object (val2->v.val_double.low, hash);
23208 hash = iterative_hash_object (val2->v.val_double.high, hash);
23209 break;
23210 case dw_val_class_addr:
23211 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23212 break;
23213 default:
23214 gcc_unreachable ();
23216 break;
23217 case DW_OP_bregx:
23218 case DW_OP_bit_piece:
23219 hash = iterative_hash_object (val1->v.val_int, hash);
23220 hash = iterative_hash_object (val2->v.val_int, hash);
23221 break;
23222 case DW_OP_addr:
23223 hash_addr:
23224 if (loc->dtprel)
23226 unsigned char dtprel = 0xd1;
23227 hash = iterative_hash_object (dtprel, hash);
23229 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23230 break;
23231 case DW_OP_GNU_addr_index:
23232 case DW_OP_GNU_const_index:
23234 if (loc->dtprel)
23236 unsigned char dtprel = 0xd1;
23237 hash = iterative_hash_object (dtprel, hash);
23239 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23241 break;
23242 case DW_OP_GNU_implicit_pointer:
23243 hash = iterative_hash_object (val2->v.val_int, hash);
23244 break;
23245 case DW_OP_GNU_entry_value:
23246 hash = hash_loc_operands (val1->v.val_loc, hash);
23247 break;
23248 case DW_OP_GNU_regval_type:
23249 case DW_OP_GNU_deref_type:
23251 unsigned int byte_size
23252 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23253 unsigned int encoding
23254 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23255 hash = iterative_hash_object (val1->v.val_int, hash);
23256 hash = iterative_hash_object (byte_size, hash);
23257 hash = iterative_hash_object (encoding, hash);
23259 break;
23260 case DW_OP_GNU_convert:
23261 case DW_OP_GNU_reinterpret:
23262 if (val1->val_class == dw_val_class_unsigned_const)
23264 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23265 break;
23267 /* FALLTHRU */
23268 case DW_OP_GNU_const_type:
23270 unsigned int byte_size
23271 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23272 unsigned int encoding
23273 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23274 hash = iterative_hash_object (byte_size, hash);
23275 hash = iterative_hash_object (encoding, hash);
23276 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23277 break;
23278 hash = iterative_hash_object (val2->val_class, hash);
23279 switch (val2->val_class)
23281 case dw_val_class_const:
23282 hash = iterative_hash_object (val2->v.val_int, hash);
23283 break;
23284 case dw_val_class_vec:
23286 unsigned int elt_size = val2->v.val_vec.elt_size;
23287 unsigned int len = val2->v.val_vec.length;
23289 hash = iterative_hash_object (elt_size, hash);
23290 hash = iterative_hash_object (len, hash);
23291 hash = iterative_hash (val2->v.val_vec.array,
23292 len * elt_size, hash);
23294 break;
23295 case dw_val_class_const_double:
23296 hash = iterative_hash_object (val2->v.val_double.low, hash);
23297 hash = iterative_hash_object (val2->v.val_double.high, hash);
23298 break;
23299 default:
23300 gcc_unreachable ();
23303 break;
23305 default:
23306 /* Other codes have no operands. */
23307 break;
23309 return hash;
23312 /* Iteratively hash the whole DWARF location expression LOC. */
23314 static inline hashval_t
23315 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23317 dw_loc_descr_ref l;
23318 bool sizes_computed = false;
23319 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23320 size_of_locs (loc);
23322 for (l = loc; l != NULL; l = l->dw_loc_next)
23324 enum dwarf_location_atom opc = l->dw_loc_opc;
23325 hash = iterative_hash_object (opc, hash);
23326 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23328 size_of_locs (loc);
23329 sizes_computed = true;
23331 hash = hash_loc_operands (l, hash);
23333 return hash;
23336 /* Compute hash of the whole location list LIST_HEAD. */
23338 static inline void
23339 hash_loc_list (dw_loc_list_ref list_head)
23341 dw_loc_list_ref curr = list_head;
23342 hashval_t hash = 0;
23344 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23346 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23347 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23348 if (curr->section)
23349 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23350 hash);
23351 hash = hash_locs (curr->expr, hash);
23353 list_head->hash = hash;
23356 /* Return true if X and Y opcodes have the same operands. */
23358 static inline bool
23359 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23361 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23362 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23363 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23364 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23366 switch (x->dw_loc_opc)
23368 case DW_OP_const4u:
23369 case DW_OP_const8u:
23370 if (x->dtprel)
23371 goto hash_addr;
23372 /* FALLTHRU */
23373 case DW_OP_const1u:
23374 case DW_OP_const1s:
23375 case DW_OP_const2u:
23376 case DW_OP_const2s:
23377 case DW_OP_const4s:
23378 case DW_OP_const8s:
23379 case DW_OP_constu:
23380 case DW_OP_consts:
23381 case DW_OP_pick:
23382 case DW_OP_plus_uconst:
23383 case DW_OP_breg0:
23384 case DW_OP_breg1:
23385 case DW_OP_breg2:
23386 case DW_OP_breg3:
23387 case DW_OP_breg4:
23388 case DW_OP_breg5:
23389 case DW_OP_breg6:
23390 case DW_OP_breg7:
23391 case DW_OP_breg8:
23392 case DW_OP_breg9:
23393 case DW_OP_breg10:
23394 case DW_OP_breg11:
23395 case DW_OP_breg12:
23396 case DW_OP_breg13:
23397 case DW_OP_breg14:
23398 case DW_OP_breg15:
23399 case DW_OP_breg16:
23400 case DW_OP_breg17:
23401 case DW_OP_breg18:
23402 case DW_OP_breg19:
23403 case DW_OP_breg20:
23404 case DW_OP_breg21:
23405 case DW_OP_breg22:
23406 case DW_OP_breg23:
23407 case DW_OP_breg24:
23408 case DW_OP_breg25:
23409 case DW_OP_breg26:
23410 case DW_OP_breg27:
23411 case DW_OP_breg28:
23412 case DW_OP_breg29:
23413 case DW_OP_breg30:
23414 case DW_OP_breg31:
23415 case DW_OP_regx:
23416 case DW_OP_fbreg:
23417 case DW_OP_piece:
23418 case DW_OP_deref_size:
23419 case DW_OP_xderef_size:
23420 return valx1->v.val_int == valy1->v.val_int;
23421 case DW_OP_skip:
23422 case DW_OP_bra:
23423 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23424 can cause irrelevant differences in dw_loc_addr. */
23425 gcc_assert (valx1->val_class == dw_val_class_loc
23426 && valy1->val_class == dw_val_class_loc
23427 && (dwarf_split_debug_info
23428 || x->dw_loc_addr == y->dw_loc_addr));
23429 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23430 case DW_OP_implicit_value:
23431 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23432 || valx2->val_class != valy2->val_class)
23433 return false;
23434 switch (valx2->val_class)
23436 case dw_val_class_const:
23437 return valx2->v.val_int == valy2->v.val_int;
23438 case dw_val_class_vec:
23439 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23440 && valx2->v.val_vec.length == valy2->v.val_vec.length
23441 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23442 valx2->v.val_vec.elt_size
23443 * valx2->v.val_vec.length) == 0;
23444 case dw_val_class_const_double:
23445 return valx2->v.val_double.low == valy2->v.val_double.low
23446 && valx2->v.val_double.high == valy2->v.val_double.high;
23447 case dw_val_class_addr:
23448 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23449 default:
23450 gcc_unreachable ();
23452 case DW_OP_bregx:
23453 case DW_OP_bit_piece:
23454 return valx1->v.val_int == valy1->v.val_int
23455 && valx2->v.val_int == valy2->v.val_int;
23456 case DW_OP_addr:
23457 hash_addr:
23458 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23459 case DW_OP_GNU_addr_index:
23460 case DW_OP_GNU_const_index:
23462 rtx ax1 = valx1->val_entry->addr.rtl;
23463 rtx ay1 = valy1->val_entry->addr.rtl;
23464 return rtx_equal_p (ax1, ay1);
23466 case DW_OP_GNU_implicit_pointer:
23467 return valx1->val_class == dw_val_class_die_ref
23468 && valx1->val_class == valy1->val_class
23469 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23470 && valx2->v.val_int == valy2->v.val_int;
23471 case DW_OP_GNU_entry_value:
23472 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23473 case DW_OP_GNU_const_type:
23474 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23475 || valx2->val_class != valy2->val_class)
23476 return false;
23477 switch (valx2->val_class)
23479 case dw_val_class_const:
23480 return valx2->v.val_int == valy2->v.val_int;
23481 case dw_val_class_vec:
23482 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23483 && valx2->v.val_vec.length == valy2->v.val_vec.length
23484 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23485 valx2->v.val_vec.elt_size
23486 * valx2->v.val_vec.length) == 0;
23487 case dw_val_class_const_double:
23488 return valx2->v.val_double.low == valy2->v.val_double.low
23489 && valx2->v.val_double.high == valy2->v.val_double.high;
23490 default:
23491 gcc_unreachable ();
23493 case DW_OP_GNU_regval_type:
23494 case DW_OP_GNU_deref_type:
23495 return valx1->v.val_int == valy1->v.val_int
23496 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23497 case DW_OP_GNU_convert:
23498 case DW_OP_GNU_reinterpret:
23499 if (valx1->val_class != valy1->val_class)
23500 return false;
23501 if (valx1->val_class == dw_val_class_unsigned_const)
23502 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23503 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23504 case DW_OP_GNU_parameter_ref:
23505 return valx1->val_class == dw_val_class_die_ref
23506 && valx1->val_class == valy1->val_class
23507 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23508 default:
23509 /* Other codes have no operands. */
23510 return true;
23514 /* Return true if DWARF location expressions X and Y are the same. */
23516 static inline bool
23517 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23519 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23520 if (x->dw_loc_opc != y->dw_loc_opc
23521 || x->dtprel != y->dtprel
23522 || !compare_loc_operands (x, y))
23523 break;
23524 return x == NULL && y == NULL;
23527 /* Hashtable helpers. */
23529 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23531 typedef dw_loc_list_struct value_type;
23532 typedef dw_loc_list_struct compare_type;
23533 static inline hashval_t hash (const value_type *);
23534 static inline bool equal (const value_type *, const compare_type *);
23537 /* Return precomputed hash of location list X. */
23539 inline hashval_t
23540 loc_list_hasher::hash (const value_type *x)
23542 return x->hash;
23545 /* Return true if location lists A and B are the same. */
23547 inline bool
23548 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23550 if (a == b)
23551 return 1;
23552 if (a->hash != b->hash)
23553 return 0;
23554 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23555 if (strcmp (a->begin, b->begin) != 0
23556 || strcmp (a->end, b->end) != 0
23557 || (a->section == NULL) != (b->section == NULL)
23558 || (a->section && strcmp (a->section, b->section) != 0)
23559 || !compare_locs (a->expr, b->expr))
23560 break;
23561 return a == NULL && b == NULL;
23564 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23567 /* Recursively optimize location lists referenced from DIE
23568 children and share them whenever possible. */
23570 static void
23571 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23573 dw_die_ref c;
23574 dw_attr_ref a;
23575 unsigned ix;
23576 dw_loc_list_struct **slot;
23578 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23579 if (AT_class (a) == dw_val_class_loc_list)
23581 dw_loc_list_ref list = AT_loc_list (a);
23582 /* TODO: perform some optimizations here, before hashing
23583 it and storing into the hash table. */
23584 hash_loc_list (list);
23585 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23586 if (*slot == NULL)
23587 *slot = list;
23588 else
23589 a->dw_attr_val.v.val_loc_list = *slot;
23592 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23596 /* Recursively assign each location list a unique index into the debug_addr
23597 section. */
23599 static void
23600 index_location_lists (dw_die_ref die)
23602 dw_die_ref c;
23603 dw_attr_ref a;
23604 unsigned ix;
23606 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23607 if (AT_class (a) == dw_val_class_loc_list)
23609 dw_loc_list_ref list = AT_loc_list (a);
23610 dw_loc_list_ref curr;
23611 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23613 /* Don't index an entry that has already been indexed
23614 or won't be output. */
23615 if (curr->begin_entry != NULL
23616 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23617 continue;
23619 curr->begin_entry
23620 = add_addr_table_entry (xstrdup (curr->begin),
23621 ate_kind_label);
23625 FOR_EACH_CHILD (die, c, index_location_lists (c));
23628 /* Optimize location lists referenced from DIE
23629 children and share them whenever possible. */
23631 static void
23632 optimize_location_lists (dw_die_ref die)
23634 loc_list_hash_type htab;
23635 htab.create (500);
23636 optimize_location_lists_1 (die, htab);
23637 htab.dispose ();
23640 /* Output stuff that dwarf requires at the end of every file,
23641 and generate the DWARF-2 debugging info. */
23643 static void
23644 dwarf2out_finish (const char *filename)
23646 limbo_die_node *node, *next_node;
23647 comdat_type_node *ctnode;
23648 hash_table <comdat_type_hasher> comdat_type_table;
23649 unsigned int i;
23650 dw_die_ref main_comp_unit_die;
23652 /* PCH might result in DW_AT_producer string being restored from the
23653 header compilation, so always fill it with empty string initially
23654 and overwrite only here. */
23655 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23656 producer_string = gen_producer_string ();
23657 producer->dw_attr_val.v.val_str->refcount--;
23658 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23660 gen_scheduled_generic_parms_dies ();
23661 gen_remaining_tmpl_value_param_die_attribute ();
23663 /* Add the name for the main input file now. We delayed this from
23664 dwarf2out_init to avoid complications with PCH. */
23665 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23666 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23667 add_comp_dir_attribute (comp_unit_die ());
23668 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23670 bool p = false;
23671 htab_traverse (file_table, file_table_relative_p, &p);
23672 if (p)
23673 add_comp_dir_attribute (comp_unit_die ());
23676 if (deferred_locations_list)
23677 for (i = 0; i < deferred_locations_list->length (); i++)
23679 add_location_or_const_value_attribute (
23680 (*deferred_locations_list)[i].die,
23681 (*deferred_locations_list)[i].variable,
23682 false,
23683 DW_AT_location);
23686 /* Traverse the limbo die list, and add parent/child links. The only
23687 dies without parents that should be here are concrete instances of
23688 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23689 For concrete instances, we can get the parent die from the abstract
23690 instance. */
23691 for (node = limbo_die_list; node; node = next_node)
23693 dw_die_ref die = node->die;
23694 next_node = node->next;
23696 if (die->die_parent == NULL)
23698 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23700 if (origin && origin->die_parent)
23701 add_child_die (origin->die_parent, die);
23702 else if (is_cu_die (die))
23704 else if (seen_error ())
23705 /* It's OK to be confused by errors in the input. */
23706 add_child_die (comp_unit_die (), die);
23707 else
23709 /* In certain situations, the lexical block containing a
23710 nested function can be optimized away, which results
23711 in the nested function die being orphaned. Likewise
23712 with the return type of that nested function. Force
23713 this to be a child of the containing function.
23715 It may happen that even the containing function got fully
23716 inlined and optimized out. In that case we are lost and
23717 assign the empty child. This should not be big issue as
23718 the function is likely unreachable too. */
23719 gcc_assert (node->created_for);
23721 if (DECL_P (node->created_for))
23722 origin = get_context_die (DECL_CONTEXT (node->created_for));
23723 else if (TYPE_P (node->created_for))
23724 origin = scope_die_for (node->created_for, comp_unit_die ());
23725 else
23726 origin = comp_unit_die ();
23728 add_child_die (origin, die);
23733 limbo_die_list = NULL;
23735 #if ENABLE_ASSERT_CHECKING
23737 dw_die_ref die = comp_unit_die (), c;
23738 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23740 #endif
23741 resolve_addr (comp_unit_die ());
23742 move_marked_base_types ();
23744 for (node = deferred_asm_name; node; node = node->next)
23746 tree decl = node->created_for;
23747 /* When generating LTO bytecode we can not generate new assembler
23748 names at this point and all important decls got theirs via
23749 free-lang-data. */
23750 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23751 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23753 add_linkage_attr (node->die, decl);
23754 move_linkage_attr (node->die);
23758 deferred_asm_name = NULL;
23760 /* Walk through the list of incomplete types again, trying once more to
23761 emit full debugging info for them. */
23762 retry_incomplete_types ();
23764 if (flag_eliminate_unused_debug_types)
23765 prune_unused_types ();
23767 /* Generate separate COMDAT sections for type DIEs. */
23768 if (use_debug_types)
23770 break_out_comdat_types (comp_unit_die ());
23772 /* Each new type_unit DIE was added to the limbo die list when created.
23773 Since these have all been added to comdat_type_list, clear the
23774 limbo die list. */
23775 limbo_die_list = NULL;
23777 /* For each new comdat type unit, copy declarations for incomplete
23778 types to make the new unit self-contained (i.e., no direct
23779 references to the main compile unit). */
23780 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23781 copy_decls_for_unworthy_types (ctnode->root_die);
23782 copy_decls_for_unworthy_types (comp_unit_die ());
23784 /* In the process of copying declarations from one unit to another,
23785 we may have left some declarations behind that are no longer
23786 referenced. Prune them. */
23787 prune_unused_types ();
23790 /* Generate separate CUs for each of the include files we've seen.
23791 They will go into limbo_die_list. */
23792 if (flag_eliminate_dwarf2_dups)
23793 break_out_includes (comp_unit_die ());
23795 /* Traverse the DIE's and add add sibling attributes to those DIE's
23796 that have children. */
23797 add_sibling_attributes (comp_unit_die ());
23798 for (node = limbo_die_list; node; node = node->next)
23799 add_sibling_attributes (node->die);
23800 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23801 add_sibling_attributes (ctnode->root_die);
23803 /* When splitting DWARF info, we put some attributes in the
23804 skeleton compile_unit DIE that remains in the .o, while
23805 most attributes go in the DWO compile_unit_die. */
23806 if (dwarf_split_debug_info)
23807 main_comp_unit_die = gen_compile_unit_die (NULL);
23808 else
23809 main_comp_unit_die = comp_unit_die ();
23811 /* Output a terminator label for the .text section. */
23812 switch_to_section (text_section);
23813 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23814 if (cold_text_section)
23816 switch_to_section (cold_text_section);
23817 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23820 /* We can only use the low/high_pc attributes if all of the code was
23821 in .text. */
23822 if (!have_multiple_function_sections
23823 || (dwarf_version < 3 && dwarf_strict))
23825 /* Don't add if the CU has no associated code. */
23826 if (text_section_used)
23827 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23828 text_end_label, true);
23830 else
23832 unsigned fde_idx;
23833 dw_fde_ref fde;
23834 bool range_list_added = false;
23836 if (text_section_used)
23837 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23838 text_end_label, &range_list_added, true);
23839 if (cold_text_section_used)
23840 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23841 cold_end_label, &range_list_added, true);
23843 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23845 if (DECL_IGNORED_P (fde->decl))
23846 continue;
23847 if (!fde->in_std_section)
23848 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23849 fde->dw_fde_end, &range_list_added,
23850 true);
23851 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23852 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23853 fde->dw_fde_second_end, &range_list_added,
23854 true);
23857 if (range_list_added)
23859 /* We need to give .debug_loc and .debug_ranges an appropriate
23860 "base address". Use zero so that these addresses become
23861 absolute. Historically, we've emitted the unexpected
23862 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23863 Emit both to give time for other tools to adapt. */
23864 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23865 if (! dwarf_strict && dwarf_version < 4)
23866 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23868 add_ranges (NULL);
23872 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23873 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23874 debug_line_section_label);
23876 if (have_macinfo)
23877 add_AT_macptr (comp_unit_die (),
23878 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23879 macinfo_section_label);
23881 if (dwarf_split_debug_info && addr_index_table != NULL)
23883 /* optimize_location_lists calculates the size of the lists,
23884 so index them first, and assign indices to the entries.
23885 Although optimize_location_lists will remove entries from
23886 the table, it only does so for duplicates, and therefore
23887 only reduces ref_counts to 1. */
23888 unsigned int index = 0;
23889 index_location_lists (comp_unit_die ());
23890 htab_traverse_noresize (addr_index_table,
23891 index_addr_table_entry, &index);
23893 if (have_location_lists)
23894 optimize_location_lists (comp_unit_die ());
23896 save_macinfo_strings ();
23898 if (dwarf_split_debug_info)
23900 unsigned int index = 0;
23902 /* Add attributes common to skeleton compile_units and
23903 type_units. Because these attributes include strings, it
23904 must be done before freezing the string table. Top-level
23905 skeleton die attrs are added when the skeleton type unit is
23906 created, so ensure it is created by this point. */
23907 add_top_level_skeleton_die_attrs (main_comp_unit_die);
23908 (void) get_skeleton_type_unit ();
23909 htab_traverse_noresize (debug_str_hash, index_string, &index);
23912 /* Output all of the compilation units. We put the main one last so that
23913 the offsets are available to output_pubnames. */
23914 for (node = limbo_die_list; node; node = node->next)
23915 output_comp_unit (node->die, 0);
23917 comdat_type_table.create (100);
23918 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23920 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
23922 /* Don't output duplicate types. */
23923 if (*slot != HTAB_EMPTY_ENTRY)
23924 continue;
23926 /* Add a pointer to the line table for the main compilation unit
23927 so that the debugger can make sense of DW_AT_decl_file
23928 attributes. */
23929 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23930 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23931 (!dwarf_split_debug_info
23932 ? debug_line_section_label
23933 : debug_skeleton_line_section_label));
23935 output_comdat_type_unit (ctnode);
23936 *slot = ctnode;
23938 comdat_type_table.dispose ();
23940 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23941 both the main_cu and all skeleton TUs. Making this call unconditional
23942 would end up either adding a second copy of the AT_pubnames attribute, or
23943 requiring a special case in add_top_level_skeleton_die_attrs. */
23944 if (!dwarf_split_debug_info)
23945 add_AT_pubnames (comp_unit_die ());
23947 if (dwarf_split_debug_info)
23949 int mark;
23950 unsigned char checksum[16];
23951 struct md5_ctx ctx;
23953 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23954 md5_init_ctx (&ctx);
23955 mark = 0;
23956 die_checksum (comp_unit_die (), &ctx, &mark);
23957 unmark_all_dies (comp_unit_die ());
23958 md5_finish_ctx (&ctx, checksum);
23960 /* Use the first 8 bytes of the checksum as the dwo_id,
23961 and add it to both comp-unit DIEs. */
23962 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23963 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23965 /* Add the base offset of the ranges table to the skeleton
23966 comp-unit DIE. */
23967 if (ranges_table_in_use)
23968 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23969 ranges_section_label);
23971 switch_to_section (debug_addr_section);
23972 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23973 output_addr_table ();
23976 /* Output the main compilation unit if non-empty or if .debug_macinfo
23977 or .debug_macro will be emitted. */
23978 output_comp_unit (comp_unit_die (), have_macinfo);
23980 if (dwarf_split_debug_info && info_section_emitted)
23981 output_skeleton_debug_sections (main_comp_unit_die);
23983 /* Output the abbreviation table. */
23984 if (abbrev_die_table_in_use != 1)
23986 switch_to_section (debug_abbrev_section);
23987 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23988 output_abbrev_section ();
23991 /* Output location list section if necessary. */
23992 if (have_location_lists)
23994 /* Output the location lists info. */
23995 switch_to_section (debug_loc_section);
23996 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23997 output_location_lists (comp_unit_die ());
24000 /* Output public names and types tables if necessary. */
24001 output_pubnames (pubname_table);
24002 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
24003 It shouldn't hurt to emit it always, since pure DWARF2 consumers
24004 simply won't look for the section. */
24005 output_pubnames (pubtype_table);
24007 /* Output the address range information if a CU (.debug_info section)
24008 was emitted. We output an empty table even if we had no functions
24009 to put in it. This because the consumer has no way to tell the
24010 difference between an empty table that we omitted and failure to
24011 generate a table that would have contained data. */
24012 if (info_section_emitted)
24014 unsigned long aranges_length = size_of_aranges ();
24016 switch_to_section (debug_aranges_section);
24017 output_aranges (aranges_length);
24020 /* Output ranges section if necessary. */
24021 if (ranges_table_in_use)
24023 switch_to_section (debug_ranges_section);
24024 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24025 output_ranges ();
24028 /* Have to end the macro section. */
24029 if (have_macinfo)
24031 switch_to_section (debug_macinfo_section);
24032 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24033 output_macinfo ();
24034 dw2_asm_output_data (1, 0, "End compilation unit");
24037 /* Output the source line correspondence table. We must do this
24038 even if there is no line information. Otherwise, on an empty
24039 translation unit, we will generate a present, but empty,
24040 .debug_info section. IRIX 6.5 `nm' will then complain when
24041 examining the file. This is done late so that any filenames
24042 used by the debug_info section are marked as 'used'. */
24043 switch_to_section (debug_line_section);
24044 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24045 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24046 output_line_info (false);
24048 if (dwarf_split_debug_info && info_section_emitted)
24050 switch_to_section (debug_skeleton_line_section);
24051 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24052 output_line_info (true);
24055 /* If we emitted any indirect strings, output the string table too. */
24056 if (debug_str_hash || skeleton_debug_str_hash)
24057 output_indirect_strings ();
24060 #include "gt-dwarf2out.h"