Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / dwarf2out.c
blobde999d0adb0338d58c7a7fa7da80fac0315a7061
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "l-ipo.h"
99 #include "tree-dfa.h"
100 #include "gdb/gdb-index.h"
102 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
103 static rtx last_var_location_insn;
104 static rtx cached_next_real_insn;
106 #ifdef VMS_DEBUGGING_INFO
107 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
109 /* Define this macro to be a nonzero value if the directory specifications
110 which are output in the debug info should end with a separator. */
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
112 /* Define this macro to evaluate to a nonzero value if GCC should refrain
113 from generating indirect strings in DWARF2 debug information, for instance
114 if your target is stuck with an old version of GDB that is unable to
115 process them properly or uses VMS Debug. */
116 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
117 #else
118 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
119 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
120 #endif
122 /* ??? Poison these here until it can be done generically. They've been
123 totally replaced in this file; make sure it stays that way. */
124 #undef DWARF2_UNWIND_INFO
125 #undef DWARF2_FRAME_INFO
126 #if (GCC_VERSION >= 3000)
127 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
128 #endif
130 /* The size of the target's pointer type. */
131 #ifndef PTR_SIZE
132 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
133 #endif
135 /* Array of RTXes referenced by the debugging information, which therefore
136 must be kept around forever. */
137 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
139 /* A pointer to the base of a list of incomplete types which might be
140 completed at some later time. incomplete_types_list needs to be a
141 vec<tree, va_gc> *because we want to tell the garbage collector about
142 it. */
143 static GTY(()) vec<tree, va_gc> *incomplete_types;
145 /* A pointer to the base of a table of references to declaration
146 scopes. This table is a display which tracks the nesting
147 of declaration scopes at the current scope and containing
148 scopes. This table is used to find the proper place to
149 define type declaration DIE's. */
150 static GTY(()) vec<tree, va_gc> *decl_scope_table;
152 /* Pointers to various DWARF2 sections. */
153 static GTY(()) section *debug_info_section;
154 static GTY(()) section *debug_skeleton_info_section;
155 static GTY(()) section *debug_abbrev_section;
156 static GTY(()) section *debug_skeleton_abbrev_section;
157 static GTY(()) section *debug_aranges_section;
158 static GTY(()) section *debug_addr_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_skeleton_line_section;
162 static GTY(()) section *debug_loc_section;
163 static GTY(()) section *debug_pubnames_section;
164 static GTY(()) section *debug_pubtypes_section;
165 static GTY(()) section *debug_str_section;
166 static GTY(()) section *debug_str_dwo_section;
167 static GTY(()) section *debug_str_offsets_section;
168 static GTY(()) section *debug_ranges_section;
169 static GTY(()) section *debug_frame_section;
171 /* Maximum size (in bytes) of an artificially generated label. */
172 #define MAX_ARTIFICIAL_LABEL_BYTES 30
174 /* According to the (draft) DWARF 3 specification, the initial length
175 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
176 bytes are 0xffffffff, followed by the length stored in the next 8
177 bytes.
179 However, the SGI/MIPS ABI uses an initial length which is equal to
180 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
182 #ifndef DWARF_INITIAL_LENGTH_SIZE
183 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
184 #endif
186 /* Round SIZE up to the nearest BOUNDARY. */
187 #define DWARF_ROUND(SIZE,BOUNDARY) \
188 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
190 /* CIE identifier. */
191 #if HOST_BITS_PER_WIDE_INT >= 64
192 #define DWARF_CIE_ID \
193 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
194 #else
195 #define DWARF_CIE_ID DW_CIE_ID
196 #endif
199 /* A vector for a table that contains frame description
200 information for each routine. */
201 #define NOT_INDEXED (-1U)
202 #define NO_INDEX_ASSIGNED (-2U)
204 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
206 struct GTY(()) indirect_string_node {
207 const char *str;
208 unsigned int refcount;
209 enum dwarf_form form;
210 char *label;
211 unsigned int index;
214 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
216 /* With split_debug_info, both the comp_dir and dwo_name go in the
217 main object file, rather than the dwo, similar to the force_direct
218 parameter elsewhere but with additional complications:
220 1) The string is needed in both the main object file and the dwo.
221 That is, the comp_dir and dwo_name will appear in both places.
223 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
224 DW_FORM_GNU_str_index.
226 3) GCC chooses the form to use late, depending on the size and
227 reference count.
229 Rather than forcing the all debug string handling functions and
230 callers to deal with these complications, simply use a separate,
231 special-cased string table for any attribute that should go in the
232 main object file. This limits the complexity to just the places
233 that need it. */
235 static GTY ((param_is (struct indirect_string_node)))
236 htab_t skeleton_debug_str_hash;
238 static GTY(()) int dw2_string_counter;
240 /* True if the compilation unit places functions in more than one section. */
241 static GTY(()) bool have_multiple_function_sections = false;
243 /* Whether the default text and cold text sections have been used at all. */
245 static GTY(()) bool text_section_used = false;
246 static GTY(()) bool cold_text_section_used = false;
248 /* The default cold text section. */
249 static GTY(()) section *cold_text_section;
251 /* The DIE for C++1y 'auto' in a function return type. */
252 static GTY(()) dw_die_ref auto_die;
254 /* The DIE for C++1y 'decltype(auto)' in a function return type. */
255 static GTY(()) dw_die_ref decltype_auto_die;
257 /* Forward declarations for functions defined in this file. */
259 static char *stripattributes (const char *);
260 static void output_call_frame_info (int);
261 static void dwarf2out_note_section_used (void);
263 /* Personality decl of current unit. Used only when assembler does not support
264 personality CFI. */
265 static GTY(()) rtx current_unit_personality;
267 /* Data and reference forms for relocatable data. */
268 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
269 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
271 #ifndef DEBUG_FRAME_SECTION
272 #define DEBUG_FRAME_SECTION ".debug_frame"
273 #endif
275 #ifndef FUNC_BEGIN_LABEL
276 #define FUNC_BEGIN_LABEL "LFB"
277 #endif
279 #ifndef FUNC_END_LABEL
280 #define FUNC_END_LABEL "LFE"
281 #endif
283 #ifndef PROLOGUE_END_LABEL
284 #define PROLOGUE_END_LABEL "LPE"
285 #endif
287 #ifndef EPILOGUE_BEGIN_LABEL
288 #define EPILOGUE_BEGIN_LABEL "LEB"
289 #endif
291 #ifndef FRAME_BEGIN_LABEL
292 #define FRAME_BEGIN_LABEL "Lframe"
293 #endif
294 #define CIE_AFTER_SIZE_LABEL "LSCIE"
295 #define CIE_END_LABEL "LECIE"
296 #define FDE_LABEL "LSFDE"
297 #define FDE_AFTER_SIZE_LABEL "LASFDE"
298 #define FDE_END_LABEL "LEFDE"
299 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
300 #define LINE_NUMBER_END_LABEL "LELT"
301 #define LN_PROLOG_AS_LABEL "LASLTP"
302 #define LN_PROLOG_END_LABEL "LELTP"
303 #define DIE_LABEL_PREFIX "DW"
305 /* Match the base name of a file to the base name of a compilation unit. */
307 static int
308 matches_main_base (const char *path)
310 /* Cache the last query. */
311 static const char *last_path = NULL;
312 static int last_match = 0;
313 if (path != last_path)
315 const char *base;
316 int length = base_of_path (path, &base);
317 last_path = path;
318 last_match = (length == main_input_baselength
319 && memcmp (base, main_input_basename, length) == 0);
321 return last_match;
324 #ifdef DEBUG_DEBUG_STRUCT
326 static int
327 dump_struct_debug (tree type, enum debug_info_usage usage,
328 enum debug_struct_file criterion, int generic,
329 int matches, int result)
331 /* Find the type name. */
332 tree type_decl = TYPE_STUB_DECL (type);
333 tree t = type_decl;
334 const char *name = 0;
335 if (TREE_CODE (t) == TYPE_DECL)
336 t = DECL_NAME (t);
337 if (t)
338 name = IDENTIFIER_POINTER (t);
340 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
341 criterion,
342 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
343 matches ? "bas" : "hdr",
344 generic ? "gen" : "ord",
345 usage == DINFO_USAGE_DFN ? ";" :
346 usage == DINFO_USAGE_DIR_USE ? "." : "*",
347 result,
348 (void*) type_decl, name);
349 return result;
351 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
352 dump_struct_debug (type, usage, criterion, generic, matches, result)
354 #else
356 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
357 (result)
359 #endif
361 static bool
362 should_emit_struct_debug (tree type, enum debug_info_usage usage)
364 enum debug_struct_file criterion;
365 tree type_decl;
366 bool generic = lang_hooks.types.generic_p (type);
368 if (generic)
369 criterion = debug_struct_generic[usage];
370 else
371 criterion = debug_struct_ordinary[usage];
373 if (criterion == DINFO_STRUCT_FILE_NONE)
374 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
375 if (criterion == DINFO_STRUCT_FILE_ANY)
376 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
378 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
380 if (type_decl != NULL)
382 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
383 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
385 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
386 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
389 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
392 /* Return a pointer to a copy of the section string name S with all
393 attributes stripped off, and an asterisk prepended (for assemble_name). */
395 static inline char *
396 stripattributes (const char *s)
398 char *stripped = XNEWVEC (char, strlen (s) + 2);
399 char *p = stripped;
401 *p++ = '*';
403 while (*s && *s != ',')
404 *p++ = *s++;
406 *p = '\0';
407 return stripped;
410 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
411 switch to the data section instead, and write out a synthetic start label
412 for collect2 the first time around. */
414 static void
415 switch_to_eh_frame_section (bool back)
417 tree label;
419 #ifdef EH_FRAME_SECTION_NAME
420 if (eh_frame_section == 0)
422 int flags;
424 if (EH_TABLES_CAN_BE_READ_ONLY)
426 int fde_encoding;
427 int per_encoding;
428 int lsda_encoding;
430 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
431 /*global=*/0);
432 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
433 /*global=*/1);
434 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
435 /*global=*/0);
436 flags = ((! flag_pic
437 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
438 && (fde_encoding & 0x70) != DW_EH_PE_aligned
439 && (per_encoding & 0x70) != DW_EH_PE_absptr
440 && (per_encoding & 0x70) != DW_EH_PE_aligned
441 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
442 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
443 ? 0 : SECTION_WRITE);
445 else
446 flags = SECTION_WRITE;
447 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
449 #endif /* EH_FRAME_SECTION_NAME */
451 if (eh_frame_section)
452 switch_to_section (eh_frame_section);
453 else
455 /* We have no special eh_frame section. Put the information in
456 the data section and emit special labels to guide collect2. */
457 switch_to_section (data_section);
459 if (!back)
461 label = get_file_function_name ("F");
462 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
463 targetm.asm_out.globalize_label (asm_out_file,
464 IDENTIFIER_POINTER (label));
465 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
470 /* Switch [BACK] to the eh or debug frame table section, depending on
471 FOR_EH. */
473 static void
474 switch_to_frame_table_section (int for_eh, bool back)
476 if (for_eh)
477 switch_to_eh_frame_section (back);
478 else
480 if (!debug_frame_section)
481 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
482 SECTION_DEBUG, NULL);
483 switch_to_section (debug_frame_section);
487 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
489 enum dw_cfi_oprnd_type
490 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
492 switch (cfi)
494 case DW_CFA_nop:
495 case DW_CFA_GNU_window_save:
496 case DW_CFA_remember_state:
497 case DW_CFA_restore_state:
498 return dw_cfi_oprnd_unused;
500 case DW_CFA_set_loc:
501 case DW_CFA_advance_loc1:
502 case DW_CFA_advance_loc2:
503 case DW_CFA_advance_loc4:
504 case DW_CFA_MIPS_advance_loc8:
505 return dw_cfi_oprnd_addr;
507 case DW_CFA_offset:
508 case DW_CFA_offset_extended:
509 case DW_CFA_def_cfa:
510 case DW_CFA_offset_extended_sf:
511 case DW_CFA_def_cfa_sf:
512 case DW_CFA_restore:
513 case DW_CFA_restore_extended:
514 case DW_CFA_undefined:
515 case DW_CFA_same_value:
516 case DW_CFA_def_cfa_register:
517 case DW_CFA_register:
518 case DW_CFA_expression:
519 return dw_cfi_oprnd_reg_num;
521 case DW_CFA_def_cfa_offset:
522 case DW_CFA_GNU_args_size:
523 case DW_CFA_def_cfa_offset_sf:
524 return dw_cfi_oprnd_offset;
526 case DW_CFA_def_cfa_expression:
527 return dw_cfi_oprnd_loc;
529 default:
530 gcc_unreachable ();
534 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
536 enum dw_cfi_oprnd_type
537 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
539 switch (cfi)
541 case DW_CFA_def_cfa:
542 case DW_CFA_def_cfa_sf:
543 case DW_CFA_offset:
544 case DW_CFA_offset_extended_sf:
545 case DW_CFA_offset_extended:
546 return dw_cfi_oprnd_offset;
548 case DW_CFA_register:
549 return dw_cfi_oprnd_reg_num;
551 case DW_CFA_expression:
552 return dw_cfi_oprnd_loc;
554 default:
555 return dw_cfi_oprnd_unused;
559 /* Output one FDE. */
561 static void
562 output_fde (dw_fde_ref fde, bool for_eh, bool second,
563 char *section_start_label, int fde_encoding, char *augmentation,
564 bool any_lsda_needed, int lsda_encoding)
566 const char *begin, *end;
567 static unsigned int j;
568 char l1[20], l2[20];
570 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
571 /* empty */ 0);
572 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
573 for_eh + j);
574 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
575 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
576 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
577 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
578 " indicating 64-bit DWARF extension");
579 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
580 "FDE Length");
581 ASM_OUTPUT_LABEL (asm_out_file, l1);
583 if (for_eh)
584 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
585 else
586 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
587 debug_frame_section, "FDE CIE offset");
589 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
590 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
592 if (for_eh)
594 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
595 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
596 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
597 "FDE initial location");
598 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
599 end, begin, "FDE address range");
601 else
603 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
604 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
607 if (augmentation[0])
609 if (any_lsda_needed)
611 int size = size_of_encoded_value (lsda_encoding);
613 if (lsda_encoding == DW_EH_PE_aligned)
615 int offset = ( 4 /* Length */
616 + 4 /* CIE offset */
617 + 2 * size_of_encoded_value (fde_encoding)
618 + 1 /* Augmentation size */ );
619 int pad = -offset & (PTR_SIZE - 1);
621 size += pad;
622 gcc_assert (size_of_uleb128 (size) == 1);
625 dw2_asm_output_data_uleb128 (size, "Augmentation size");
627 if (fde->uses_eh_lsda)
629 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
630 fde->funcdef_number);
631 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
632 gen_rtx_SYMBOL_REF (Pmode, l1),
633 false,
634 "Language Specific Data Area");
636 else
638 if (lsda_encoding == DW_EH_PE_aligned)
639 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
640 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
641 "Language Specific Data Area (none)");
644 else
645 dw2_asm_output_data_uleb128 (0, "Augmentation size");
648 /* Loop through the Call Frame Instructions associated with this FDE. */
649 fde->dw_fde_current_label = begin;
651 size_t from, until, i;
653 from = 0;
654 until = vec_safe_length (fde->dw_fde_cfi);
656 if (fde->dw_fde_second_begin == NULL)
658 else if (!second)
659 until = fde->dw_fde_switch_cfi_index;
660 else
661 from = fde->dw_fde_switch_cfi_index;
663 for (i = from; i < until; i++)
664 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
667 /* If we are to emit a ref/link from function bodies to their frame tables,
668 do it now. This is typically performed to make sure that tables
669 associated with functions are dragged with them and not discarded in
670 garbage collecting links. We need to do this on a per function basis to
671 cope with -ffunction-sections. */
673 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
674 /* Switch to the function section, emit the ref to the tables, and
675 switch *back* into the table section. */
676 switch_to_section (function_section (fde->decl));
677 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
678 switch_to_frame_table_section (for_eh, true);
679 #endif
681 /* Pad the FDE out to an address sized boundary. */
682 ASM_OUTPUT_ALIGN (asm_out_file,
683 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
684 ASM_OUTPUT_LABEL (asm_out_file, l2);
686 j += 2;
689 /* Return true if frame description entry FDE is needed for EH. */
691 static bool
692 fde_needed_for_eh_p (dw_fde_ref fde)
694 if (flag_asynchronous_unwind_tables)
695 return true;
697 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
698 return true;
700 if (fde->uses_eh_lsda)
701 return true;
703 /* If exceptions are enabled, we have collected nothrow info. */
704 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
705 return false;
707 return true;
710 /* Output the call frame information used to record information
711 that relates to calculating the frame pointer, and records the
712 location of saved registers. */
714 static void
715 output_call_frame_info (int for_eh)
717 unsigned int i;
718 dw_fde_ref fde;
719 dw_cfi_ref cfi;
720 char l1[20], l2[20], section_start_label[20];
721 bool any_lsda_needed = false;
722 char augmentation[6];
723 int augmentation_size;
724 int fde_encoding = DW_EH_PE_absptr;
725 int per_encoding = DW_EH_PE_absptr;
726 int lsda_encoding = DW_EH_PE_absptr;
727 int return_reg;
728 rtx personality = NULL;
729 int dw_cie_version;
731 /* Don't emit a CIE if there won't be any FDEs. */
732 if (!fde_vec)
733 return;
735 /* Nothing to do if the assembler's doing it all. */
736 if (dwarf2out_do_cfi_asm ())
737 return;
739 /* If we don't have any functions we'll want to unwind out of, don't emit
740 any EH unwind information. If we make FDEs linkonce, we may have to
741 emit an empty label for an FDE that wouldn't otherwise be emitted. We
742 want to avoid having an FDE kept around when the function it refers to
743 is discarded. Example where this matters: a primary function template
744 in C++ requires EH information, an explicit specialization doesn't. */
745 if (for_eh)
747 bool any_eh_needed = false;
749 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
751 if (fde->uses_eh_lsda)
752 any_eh_needed = any_lsda_needed = true;
753 else if (fde_needed_for_eh_p (fde))
754 any_eh_needed = true;
755 else if (TARGET_USES_WEAK_UNWIND_INFO)
756 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
759 if (!any_eh_needed)
760 return;
763 /* We're going to be generating comments, so turn on app. */
764 if (flag_debug_asm)
765 app_enable ();
767 /* Switch to the proper frame section, first time. */
768 switch_to_frame_table_section (for_eh, false);
770 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
771 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
773 /* Output the CIE. */
774 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
775 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
776 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
777 dw2_asm_output_data (4, 0xffffffff,
778 "Initial length escape value indicating 64-bit DWARF extension");
779 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
780 "Length of Common Information Entry");
781 ASM_OUTPUT_LABEL (asm_out_file, l1);
783 /* Now that the CIE pointer is PC-relative for EH,
784 use 0 to identify the CIE. */
785 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
786 (for_eh ? 0 : DWARF_CIE_ID),
787 "CIE Identifier Tag");
789 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
790 use CIE version 1, unless that would produce incorrect results
791 due to overflowing the return register column. */
792 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
793 dw_cie_version = 1;
794 if (return_reg >= 256 || dwarf_version > 2)
795 dw_cie_version = 3;
796 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
798 augmentation[0] = 0;
799 augmentation_size = 0;
801 personality = current_unit_personality;
802 if (for_eh)
804 char *p;
806 /* Augmentation:
807 z Indicates that a uleb128 is present to size the
808 augmentation section.
809 L Indicates the encoding (and thus presence) of
810 an LSDA pointer in the FDE augmentation.
811 R Indicates a non-default pointer encoding for
812 FDE code pointers.
813 P Indicates the presence of an encoding + language
814 personality routine in the CIE augmentation. */
816 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
817 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
818 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
820 p = augmentation + 1;
821 if (personality)
823 *p++ = 'P';
824 augmentation_size += 1 + size_of_encoded_value (per_encoding);
825 assemble_external_libcall (personality);
827 if (any_lsda_needed)
829 *p++ = 'L';
830 augmentation_size += 1;
832 if (fde_encoding != DW_EH_PE_absptr)
834 *p++ = 'R';
835 augmentation_size += 1;
837 if (p > augmentation + 1)
839 augmentation[0] = 'z';
840 *p = '\0';
843 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
844 if (personality && per_encoding == DW_EH_PE_aligned)
846 int offset = ( 4 /* Length */
847 + 4 /* CIE Id */
848 + 1 /* CIE version */
849 + strlen (augmentation) + 1 /* Augmentation */
850 + size_of_uleb128 (1) /* Code alignment */
851 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
852 + 1 /* RA column */
853 + 1 /* Augmentation size */
854 + 1 /* Personality encoding */ );
855 int pad = -offset & (PTR_SIZE - 1);
857 augmentation_size += pad;
859 /* Augmentations should be small, so there's scarce need to
860 iterate for a solution. Die if we exceed one uleb128 byte. */
861 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
865 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
866 if (dw_cie_version >= 4)
868 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
869 dw2_asm_output_data (1, 0, "CIE Segment Size");
871 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
872 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
873 "CIE Data Alignment Factor");
875 if (dw_cie_version == 1)
876 dw2_asm_output_data (1, return_reg, "CIE RA Column");
877 else
878 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
880 if (augmentation[0])
882 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
883 if (personality)
885 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
886 eh_data_format_name (per_encoding));
887 dw2_asm_output_encoded_addr_rtx (per_encoding,
888 personality,
889 true, NULL);
892 if (any_lsda_needed)
893 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
894 eh_data_format_name (lsda_encoding));
896 if (fde_encoding != DW_EH_PE_absptr)
897 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
898 eh_data_format_name (fde_encoding));
901 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
902 output_cfi (cfi, NULL, for_eh);
904 /* Pad the CIE out to an address sized boundary. */
905 ASM_OUTPUT_ALIGN (asm_out_file,
906 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
907 ASM_OUTPUT_LABEL (asm_out_file, l2);
909 /* Loop through all of the FDE's. */
910 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
912 unsigned int k;
914 /* Don't emit EH unwind info for leaf functions that don't need it. */
915 if (for_eh && !fde_needed_for_eh_p (fde))
916 continue;
918 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
919 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
920 augmentation, any_lsda_needed, lsda_encoding);
923 if (for_eh && targetm.terminate_dw2_eh_frame_info)
924 dw2_asm_output_data (4, 0, "End of Table");
926 /* Turn off app to make assembly quicker. */
927 if (flag_debug_asm)
928 app_disable ();
931 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
933 static void
934 dwarf2out_do_cfi_startproc (bool second)
936 int enc;
937 rtx ref;
938 rtx personality = get_personality_function (current_function_decl);
940 fprintf (asm_out_file, "\t.cfi_startproc\n");
942 if (personality)
944 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
945 ref = personality;
947 /* ??? The GAS support isn't entirely consistent. We have to
948 handle indirect support ourselves, but PC-relative is done
949 in the assembler. Further, the assembler can't handle any
950 of the weirder relocation types. */
951 if (enc & DW_EH_PE_indirect)
952 ref = dw2_force_const_mem (ref, true);
954 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
955 output_addr_const (asm_out_file, ref);
956 fputc ('\n', asm_out_file);
959 if (crtl->uses_eh_lsda)
961 char lab[20];
963 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
964 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
965 FUNC_LABEL_ID (cfun));
966 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
967 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
969 if (enc & DW_EH_PE_indirect)
970 ref = dw2_force_const_mem (ref, true);
972 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
973 output_addr_const (asm_out_file, ref);
974 fputc ('\n', asm_out_file);
978 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
979 this allocation may be done before pass_final. */
981 dw_fde_ref
982 dwarf2out_alloc_current_fde (void)
984 dw_fde_ref fde;
986 fde = ggc_alloc_cleared_dw_fde_node ();
987 fde->decl = current_function_decl;
988 fde->funcdef_number = FUNC_LABEL_ID (cfun);
989 fde->fde_index = vec_safe_length (fde_vec);
990 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
991 fde->uses_eh_lsda = crtl->uses_eh_lsda;
992 fde->nothrow = crtl->nothrow;
993 fde->drap_reg = INVALID_REGNUM;
994 fde->vdrap_reg = INVALID_REGNUM;
996 /* Record the FDE associated with this function. */
997 cfun->fde = fde;
998 vec_safe_push (fde_vec, fde);
1000 return fde;
1003 /* Output a marker (i.e. a label) for the beginning of a function, before
1004 the prologue. */
1006 void
1007 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1008 const char *file ATTRIBUTE_UNUSED)
1010 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1011 char * dup_label;
1012 dw_fde_ref fde;
1013 section *fnsec;
1014 bool do_frame;
1016 current_function_func_begin_label = NULL;
1018 do_frame = dwarf2out_do_frame ();
1020 /* ??? current_function_func_begin_label is also used by except.c for
1021 call-site information. We must emit this label if it might be used. */
1022 if (!do_frame
1023 && (!flag_exceptions
1024 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1025 return;
1027 fnsec = function_section (current_function_decl);
1028 switch_to_section (fnsec);
1029 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1030 FUNC_LABEL_ID (cfun));
1031 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1032 FUNC_LABEL_ID (cfun));
1033 dup_label = xstrdup (label);
1034 current_function_func_begin_label = dup_label;
1036 /* We can elide the fde allocation if we're not emitting debug info. */
1037 if (!do_frame)
1038 return;
1040 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1041 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1042 would include pass_dwarf2_frame. If we've not created the FDE yet,
1043 do so now. */
1044 fde = cfun->fde;
1045 if (fde == NULL)
1046 fde = dwarf2out_alloc_current_fde ();
1048 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1049 fde->dw_fde_begin = dup_label;
1050 fde->dw_fde_current_label = dup_label;
1051 fde->in_std_section = (fnsec == text_section
1052 || (cold_text_section && fnsec == cold_text_section));
1054 /* We only want to output line number information for the genuine dwarf2
1055 prologue case, not the eh frame case. */
1056 #ifdef DWARF2_DEBUGGING_INFO
1057 if (file)
1058 dwarf2out_source_line (line, file, 0, true);
1059 #endif
1061 if (dwarf2out_do_cfi_asm ())
1062 dwarf2out_do_cfi_startproc (false);
1063 else
1065 rtx personality = get_personality_function (current_function_decl);
1066 if (!current_unit_personality)
1067 current_unit_personality = personality;
1069 /* We cannot keep a current personality per function as without CFI
1070 asm, at the point where we emit the CFI data, there is no current
1071 function anymore. */
1072 if (personality && current_unit_personality != personality)
1073 sorry ("multiple EH personalities are supported only with assemblers "
1074 "supporting .cfi_personality directive");
1078 /* Output a marker (i.e. a label) for the end of the generated code
1079 for a function prologue. This gets called *after* the prologue code has
1080 been generated. */
1082 void
1083 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1084 const char *file ATTRIBUTE_UNUSED)
1086 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1088 /* Output a label to mark the endpoint of the code generated for this
1089 function. */
1090 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1091 FUNC_LABEL_ID (cfun));
1092 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1093 FUNC_LABEL_ID (cfun));
1094 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1097 /* Output a marker (i.e. a label) for the beginning of the generated code
1098 for a function epilogue. This gets called *before* the prologue code has
1099 been generated. */
1101 void
1102 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1103 const char *file ATTRIBUTE_UNUSED)
1105 dw_fde_ref fde = cfun->fde;
1106 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1108 if (fde->dw_fde_vms_begin_epilogue)
1109 return;
1111 /* Output a label to mark the endpoint of the code generated for this
1112 function. */
1113 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1114 FUNC_LABEL_ID (cfun));
1115 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1116 FUNC_LABEL_ID (cfun));
1117 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1120 /* Output a marker (i.e. a label) for the absolute end of the generated code
1121 for a function definition. This gets called *after* the epilogue code has
1122 been generated. */
1124 void
1125 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1126 const char *file ATTRIBUTE_UNUSED)
1128 dw_fde_ref fde;
1129 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1131 last_var_location_insn = NULL_RTX;
1132 cached_next_real_insn = NULL_RTX;
1134 if (dwarf2out_do_cfi_asm ())
1135 fprintf (asm_out_file, "\t.cfi_endproc\n");
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1140 FUNC_LABEL_ID (cfun));
1141 ASM_OUTPUT_LABEL (asm_out_file, label);
1142 fde = cfun->fde;
1143 gcc_assert (fde != NULL);
1144 if (fde->dw_fde_second_begin == NULL)
1145 fde->dw_fde_end = xstrdup (label);
1148 void
1149 dwarf2out_frame_finish (void)
1151 /* Output call frame information. */
1152 if (targetm.debug_unwind_info () == UI_DWARF2)
1153 output_call_frame_info (0);
1155 /* Output another copy for the unwinder. */
1156 if ((flag_unwind_tables || flag_exceptions)
1157 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1158 output_call_frame_info (1);
1161 /* Note that the current function section is being used for code. */
1163 static void
1164 dwarf2out_note_section_used (void)
1166 section *sec = current_function_section ();
1167 if (sec == text_section)
1168 text_section_used = true;
1169 else if (sec == cold_text_section)
1170 cold_text_section_used = true;
1173 static void var_location_switch_text_section (void);
1174 static void set_cur_line_info_table (section *);
1176 void
1177 dwarf2out_switch_text_section (void)
1179 section *sect;
1180 dw_fde_ref fde = cfun->fde;
1182 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1184 if (!in_cold_section_p)
1186 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1187 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1188 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1190 else
1192 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1193 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1194 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1196 have_multiple_function_sections = true;
1198 /* There is no need to mark used sections when not debugging. */
1199 if (cold_text_section != NULL)
1200 dwarf2out_note_section_used ();
1202 if (dwarf2out_do_cfi_asm ())
1203 fprintf (asm_out_file, "\t.cfi_endproc\n");
1205 /* Now do the real section switch. */
1206 sect = current_function_section ();
1207 switch_to_section (sect);
1209 fde->second_in_std_section
1210 = (sect == text_section
1211 || (cold_text_section && sect == cold_text_section));
1213 if (dwarf2out_do_cfi_asm ())
1214 dwarf2out_do_cfi_startproc (true);
1216 var_location_switch_text_section ();
1218 if (cold_text_section != NULL)
1219 set_cur_line_info_table (sect);
1222 /* And now, the subset of the debugging information support code necessary
1223 for emitting location expressions. */
1225 /* Data about a single source file. */
1226 struct GTY(()) dwarf_file_data {
1227 const char * filename;
1228 int emitted_number;
1231 typedef struct GTY(()) deferred_locations_struct
1233 tree variable;
1234 dw_die_ref die;
1235 } deferred_locations;
1238 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1241 /* Describe an entry into the .debug_addr section. */
1243 enum ate_kind {
1244 ate_kind_rtx,
1245 ate_kind_rtx_dtprel,
1246 ate_kind_label
1249 typedef struct GTY(()) addr_table_entry_struct {
1250 enum ate_kind kind;
1251 unsigned int refcount;
1252 unsigned int index;
1253 union addr_table_entry_struct_union
1255 rtx GTY ((tag ("0"))) rtl;
1256 char * GTY ((tag ("1"))) label;
1258 GTY ((desc ("%1.kind"))) addr;
1260 addr_table_entry;
1262 /* Location lists are ranges + location descriptions for that range,
1263 so you can track variables that are in different places over
1264 their entire life. */
1265 typedef struct GTY(()) dw_loc_list_struct {
1266 dw_loc_list_ref dw_loc_next;
1267 const char *begin; /* Label and addr_entry for start of range */
1268 addr_table_entry *begin_entry;
1269 const char *end; /* Label for end of range */
1270 char *ll_symbol; /* Label for beginning of location list.
1271 Only on head of list */
1272 const char *section; /* Section this loclist is relative to */
1273 dw_loc_descr_ref expr;
1274 hashval_t hash;
1275 /* True if all addresses in this and subsequent lists are known to be
1276 resolved. */
1277 bool resolved_addr;
1278 /* True if this list has been replaced by dw_loc_next. */
1279 bool replaced;
1280 bool emitted;
1281 /* True if the range should be emitted even if begin and end
1282 are the same. */
1283 bool force;
1284 } dw_loc_list_node;
1286 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1288 /* Convert a DWARF stack opcode into its string name. */
1290 static const char *
1291 dwarf_stack_op_name (unsigned int op)
1293 const char *name = get_DW_OP_name (op);
1295 if (name != NULL)
1296 return name;
1298 return "OP_<unknown>";
1301 /* Return a pointer to a newly allocated location description. Location
1302 descriptions are simple expression terms that can be strung
1303 together to form more complicated location (address) descriptions. */
1305 static inline dw_loc_descr_ref
1306 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1307 unsigned HOST_WIDE_INT oprnd2)
1309 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1311 descr->dw_loc_opc = op;
1312 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1313 descr->dw_loc_oprnd1.val_entry = NULL;
1314 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1315 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1316 descr->dw_loc_oprnd2.val_entry = NULL;
1317 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1319 return descr;
1322 /* Return a pointer to a newly allocated location description for
1323 REG and OFFSET. */
1325 static inline dw_loc_descr_ref
1326 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1328 if (reg <= 31)
1329 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1330 offset, 0);
1331 else
1332 return new_loc_descr (DW_OP_bregx, reg, offset);
1335 /* Add a location description term to a location description expression. */
1337 static inline void
1338 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1340 dw_loc_descr_ref *d;
1342 /* Find the end of the chain. */
1343 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1346 *d = descr;
1349 /* Compare two location operands for exact equality. */
1351 static bool
1352 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1354 if (a->val_class != b->val_class)
1355 return false;
1356 switch (a->val_class)
1358 case dw_val_class_none:
1359 return true;
1360 case dw_val_class_addr:
1361 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1363 case dw_val_class_offset:
1364 case dw_val_class_unsigned_const:
1365 case dw_val_class_const:
1366 case dw_val_class_range_list:
1367 case dw_val_class_lineptr:
1368 case dw_val_class_macptr:
1369 /* These are all HOST_WIDE_INT, signed or unsigned. */
1370 return a->v.val_unsigned == b->v.val_unsigned;
1372 case dw_val_class_loc:
1373 return a->v.val_loc == b->v.val_loc;
1374 case dw_val_class_loc_list:
1375 return a->v.val_loc_list == b->v.val_loc_list;
1376 case dw_val_class_die_ref:
1377 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1378 case dw_val_class_fde_ref:
1379 return a->v.val_fde_index == b->v.val_fde_index;
1380 case dw_val_class_lbl_id:
1381 case dw_val_class_high_pc:
1382 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1383 case dw_val_class_str:
1384 return a->v.val_str == b->v.val_str;
1385 case dw_val_class_flag:
1386 return a->v.val_flag == b->v.val_flag;
1387 case dw_val_class_file:
1388 return a->v.val_file == b->v.val_file;
1389 case dw_val_class_decl_ref:
1390 return a->v.val_decl_ref == b->v.val_decl_ref;
1392 case dw_val_class_const_double:
1393 return (a->v.val_double.high == b->v.val_double.high
1394 && a->v.val_double.low == b->v.val_double.low);
1396 case dw_val_class_vec:
1398 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1399 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1401 return (a_len == b_len
1402 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1405 case dw_val_class_data8:
1406 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1408 case dw_val_class_vms_delta:
1409 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1410 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1412 gcc_unreachable ();
1415 /* Compare two location atoms for exact equality. */
1417 static bool
1418 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1420 if (a->dw_loc_opc != b->dw_loc_opc)
1421 return false;
1423 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1424 address size, but since we always allocate cleared storage it
1425 should be zero for other types of locations. */
1426 if (a->dtprel != b->dtprel)
1427 return false;
1429 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1430 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1433 /* Compare two complete location expressions for exact equality. */
1435 bool
1436 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1438 while (1)
1440 if (a == b)
1441 return true;
1442 if (a == NULL || b == NULL)
1443 return false;
1444 if (!loc_descr_equal_p_1 (a, b))
1445 return false;
1447 a = a->dw_loc_next;
1448 b = b->dw_loc_next;
1453 /* Add a constant OFFSET to a location expression. */
1455 static void
1456 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1458 dw_loc_descr_ref loc;
1459 HOST_WIDE_INT *p;
1461 gcc_assert (*list_head != NULL);
1463 if (!offset)
1464 return;
1466 /* Find the end of the chain. */
1467 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1470 p = NULL;
1471 if (loc->dw_loc_opc == DW_OP_fbreg
1472 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1473 p = &loc->dw_loc_oprnd1.v.val_int;
1474 else if (loc->dw_loc_opc == DW_OP_bregx)
1475 p = &loc->dw_loc_oprnd2.v.val_int;
1477 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1478 offset. Don't optimize if an signed integer overflow would happen. */
1479 if (p != NULL
1480 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1481 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1482 *p += offset;
1484 else if (offset > 0)
1485 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1487 else
1489 loc->dw_loc_next = int_loc_descriptor (-offset);
1490 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1494 /* Add a constant OFFSET to a location list. */
1496 static void
1497 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1499 dw_loc_list_ref d;
1500 for (d = list_head; d != NULL; d = d->dw_loc_next)
1501 loc_descr_plus_const (&d->expr, offset);
1504 #define DWARF_REF_SIZE \
1505 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1507 static unsigned long int get_base_type_offset (dw_die_ref);
1509 /* Return the size of a location descriptor. */
1511 static unsigned long
1512 size_of_loc_descr (dw_loc_descr_ref loc)
1514 unsigned long size = 1;
1516 switch (loc->dw_loc_opc)
1518 case DW_OP_addr:
1519 size += DWARF2_ADDR_SIZE;
1520 break;
1521 case DW_OP_GNU_addr_index:
1522 case DW_OP_GNU_const_index:
1523 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1524 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1525 break;
1526 case DW_OP_const1u:
1527 case DW_OP_const1s:
1528 size += 1;
1529 break;
1530 case DW_OP_const2u:
1531 case DW_OP_const2s:
1532 size += 2;
1533 break;
1534 case DW_OP_const4u:
1535 case DW_OP_const4s:
1536 size += 4;
1537 break;
1538 case DW_OP_const8u:
1539 case DW_OP_const8s:
1540 size += 8;
1541 break;
1542 case DW_OP_constu:
1543 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1544 break;
1545 case DW_OP_consts:
1546 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1547 break;
1548 case DW_OP_pick:
1549 size += 1;
1550 break;
1551 case DW_OP_plus_uconst:
1552 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1553 break;
1554 case DW_OP_skip:
1555 case DW_OP_bra:
1556 size += 2;
1557 break;
1558 case DW_OP_breg0:
1559 case DW_OP_breg1:
1560 case DW_OP_breg2:
1561 case DW_OP_breg3:
1562 case DW_OP_breg4:
1563 case DW_OP_breg5:
1564 case DW_OP_breg6:
1565 case DW_OP_breg7:
1566 case DW_OP_breg8:
1567 case DW_OP_breg9:
1568 case DW_OP_breg10:
1569 case DW_OP_breg11:
1570 case DW_OP_breg12:
1571 case DW_OP_breg13:
1572 case DW_OP_breg14:
1573 case DW_OP_breg15:
1574 case DW_OP_breg16:
1575 case DW_OP_breg17:
1576 case DW_OP_breg18:
1577 case DW_OP_breg19:
1578 case DW_OP_breg20:
1579 case DW_OP_breg21:
1580 case DW_OP_breg22:
1581 case DW_OP_breg23:
1582 case DW_OP_breg24:
1583 case DW_OP_breg25:
1584 case DW_OP_breg26:
1585 case DW_OP_breg27:
1586 case DW_OP_breg28:
1587 case DW_OP_breg29:
1588 case DW_OP_breg30:
1589 case DW_OP_breg31:
1590 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1591 break;
1592 case DW_OP_regx:
1593 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1594 break;
1595 case DW_OP_fbreg:
1596 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1597 break;
1598 case DW_OP_bregx:
1599 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1600 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1601 break;
1602 case DW_OP_piece:
1603 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1604 break;
1605 case DW_OP_bit_piece:
1606 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1607 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1608 break;
1609 case DW_OP_deref_size:
1610 case DW_OP_xderef_size:
1611 size += 1;
1612 break;
1613 case DW_OP_call2:
1614 size += 2;
1615 break;
1616 case DW_OP_call4:
1617 size += 4;
1618 break;
1619 case DW_OP_call_ref:
1620 size += DWARF_REF_SIZE;
1621 break;
1622 case DW_OP_implicit_value:
1623 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1624 + loc->dw_loc_oprnd1.v.val_unsigned;
1625 break;
1626 case DW_OP_GNU_implicit_pointer:
1627 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1628 break;
1629 case DW_OP_GNU_entry_value:
1631 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1632 size += size_of_uleb128 (op_size) + op_size;
1633 break;
1635 case DW_OP_GNU_const_type:
1637 unsigned long o
1638 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1639 size += size_of_uleb128 (o) + 1;
1640 switch (loc->dw_loc_oprnd2.val_class)
1642 case dw_val_class_vec:
1643 size += loc->dw_loc_oprnd2.v.val_vec.length
1644 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1645 break;
1646 case dw_val_class_const:
1647 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1648 break;
1649 case dw_val_class_const_double:
1650 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1651 break;
1652 default:
1653 gcc_unreachable ();
1655 break;
1657 case DW_OP_GNU_regval_type:
1659 unsigned long o
1660 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1661 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1662 + size_of_uleb128 (o);
1664 break;
1665 case DW_OP_GNU_deref_type:
1667 unsigned long o
1668 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1669 size += 1 + size_of_uleb128 (o);
1671 break;
1672 case DW_OP_GNU_convert:
1673 case DW_OP_GNU_reinterpret:
1674 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1675 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1676 else
1678 unsigned long o
1679 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1680 size += size_of_uleb128 (o);
1682 break;
1683 case DW_OP_GNU_parameter_ref:
1684 size += 4;
1685 break;
1686 default:
1687 break;
1690 return size;
1693 /* Return the size of a series of location descriptors. */
1695 unsigned long
1696 size_of_locs (dw_loc_descr_ref loc)
1698 dw_loc_descr_ref l;
1699 unsigned long size;
1701 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1702 field, to avoid writing to a PCH file. */
1703 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1705 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1706 break;
1707 size += size_of_loc_descr (l);
1709 if (! l)
1710 return size;
1712 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1714 l->dw_loc_addr = size;
1715 size += size_of_loc_descr (l);
1718 return size;
1721 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1722 static void get_ref_die_offset_label (char *, dw_die_ref);
1723 static unsigned long int get_ref_die_offset (dw_die_ref);
1725 /* Output location description stack opcode's operands (if any).
1726 The for_eh_or_skip parameter controls whether register numbers are
1727 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1728 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1729 info). This should be suppressed for the cases that have not been converted
1730 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1732 static void
1733 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1735 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1736 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1738 switch (loc->dw_loc_opc)
1740 #ifdef DWARF2_DEBUGGING_INFO
1741 case DW_OP_const2u:
1742 case DW_OP_const2s:
1743 dw2_asm_output_data (2, val1->v.val_int, NULL);
1744 break;
1745 case DW_OP_const4u:
1746 if (loc->dtprel)
1748 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1749 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1750 val1->v.val_addr);
1751 fputc ('\n', asm_out_file);
1752 break;
1754 /* FALLTHRU */
1755 case DW_OP_const4s:
1756 dw2_asm_output_data (4, val1->v.val_int, NULL);
1757 break;
1758 case DW_OP_const8u:
1759 if (loc->dtprel)
1761 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1762 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1763 val1->v.val_addr);
1764 fputc ('\n', asm_out_file);
1765 break;
1767 /* FALLTHRU */
1768 case DW_OP_const8s:
1769 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1770 dw2_asm_output_data (8, val1->v.val_int, NULL);
1771 break;
1772 case DW_OP_skip:
1773 case DW_OP_bra:
1775 int offset;
1777 gcc_assert (val1->val_class == dw_val_class_loc);
1778 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1780 dw2_asm_output_data (2, offset, NULL);
1782 break;
1783 case DW_OP_implicit_value:
1784 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1785 switch (val2->val_class)
1787 case dw_val_class_const:
1788 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1789 break;
1790 case dw_val_class_vec:
1792 unsigned int elt_size = val2->v.val_vec.elt_size;
1793 unsigned int len = val2->v.val_vec.length;
1794 unsigned int i;
1795 unsigned char *p;
1797 if (elt_size > sizeof (HOST_WIDE_INT))
1799 elt_size /= 2;
1800 len *= 2;
1802 for (i = 0, p = val2->v.val_vec.array;
1803 i < len;
1804 i++, p += elt_size)
1805 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1806 "fp or vector constant word %u", i);
1808 break;
1809 case dw_val_class_const_double:
1811 unsigned HOST_WIDE_INT first, second;
1813 if (WORDS_BIG_ENDIAN)
1815 first = val2->v.val_double.high;
1816 second = val2->v.val_double.low;
1818 else
1820 first = val2->v.val_double.low;
1821 second = val2->v.val_double.high;
1823 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1824 first, NULL);
1825 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1826 second, NULL);
1828 break;
1829 case dw_val_class_addr:
1830 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1831 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1832 break;
1833 default:
1834 gcc_unreachable ();
1836 break;
1837 #else
1838 case DW_OP_const2u:
1839 case DW_OP_const2s:
1840 case DW_OP_const4u:
1841 case DW_OP_const4s:
1842 case DW_OP_const8u:
1843 case DW_OP_const8s:
1844 case DW_OP_skip:
1845 case DW_OP_bra:
1846 case DW_OP_implicit_value:
1847 /* We currently don't make any attempt to make sure these are
1848 aligned properly like we do for the main unwind info, so
1849 don't support emitting things larger than a byte if we're
1850 only doing unwinding. */
1851 gcc_unreachable ();
1852 #endif
1853 case DW_OP_const1u:
1854 case DW_OP_const1s:
1855 dw2_asm_output_data (1, val1->v.val_int, NULL);
1856 break;
1857 case DW_OP_constu:
1858 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1859 break;
1860 case DW_OP_consts:
1861 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1862 break;
1863 case DW_OP_pick:
1864 dw2_asm_output_data (1, val1->v.val_int, NULL);
1865 break;
1866 case DW_OP_plus_uconst:
1867 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1868 break;
1869 case DW_OP_breg0:
1870 case DW_OP_breg1:
1871 case DW_OP_breg2:
1872 case DW_OP_breg3:
1873 case DW_OP_breg4:
1874 case DW_OP_breg5:
1875 case DW_OP_breg6:
1876 case DW_OP_breg7:
1877 case DW_OP_breg8:
1878 case DW_OP_breg9:
1879 case DW_OP_breg10:
1880 case DW_OP_breg11:
1881 case DW_OP_breg12:
1882 case DW_OP_breg13:
1883 case DW_OP_breg14:
1884 case DW_OP_breg15:
1885 case DW_OP_breg16:
1886 case DW_OP_breg17:
1887 case DW_OP_breg18:
1888 case DW_OP_breg19:
1889 case DW_OP_breg20:
1890 case DW_OP_breg21:
1891 case DW_OP_breg22:
1892 case DW_OP_breg23:
1893 case DW_OP_breg24:
1894 case DW_OP_breg25:
1895 case DW_OP_breg26:
1896 case DW_OP_breg27:
1897 case DW_OP_breg28:
1898 case DW_OP_breg29:
1899 case DW_OP_breg30:
1900 case DW_OP_breg31:
1901 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1902 break;
1903 case DW_OP_regx:
1905 unsigned r = val1->v.val_unsigned;
1906 if (for_eh_or_skip >= 0)
1907 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1908 gcc_assert (size_of_uleb128 (r)
1909 == size_of_uleb128 (val1->v.val_unsigned));
1910 dw2_asm_output_data_uleb128 (r, NULL);
1912 break;
1913 case DW_OP_fbreg:
1914 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1915 break;
1916 case DW_OP_bregx:
1918 unsigned r = val1->v.val_unsigned;
1919 if (for_eh_or_skip >= 0)
1920 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1921 gcc_assert (size_of_uleb128 (r)
1922 == size_of_uleb128 (val1->v.val_unsigned));
1923 dw2_asm_output_data_uleb128 (r, NULL);
1924 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1926 break;
1927 case DW_OP_piece:
1928 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1929 break;
1930 case DW_OP_bit_piece:
1931 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1932 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1933 break;
1934 case DW_OP_deref_size:
1935 case DW_OP_xderef_size:
1936 dw2_asm_output_data (1, val1->v.val_int, NULL);
1937 break;
1939 case DW_OP_addr:
1940 if (loc->dtprel)
1942 if (targetm.asm_out.output_dwarf_dtprel)
1944 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1945 DWARF2_ADDR_SIZE,
1946 val1->v.val_addr);
1947 fputc ('\n', asm_out_file);
1949 else
1950 gcc_unreachable ();
1952 else
1954 #ifdef DWARF2_DEBUGGING_INFO
1955 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1956 #else
1957 gcc_unreachable ();
1958 #endif
1960 break;
1962 case DW_OP_GNU_addr_index:
1963 case DW_OP_GNU_const_index:
1964 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1965 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1966 "(index into .debug_addr)");
1967 break;
1969 case DW_OP_GNU_implicit_pointer:
1971 char label[MAX_ARTIFICIAL_LABEL_BYTES
1972 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1973 gcc_assert (val1->val_class == dw_val_class_die_ref);
1974 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1975 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1976 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1978 break;
1980 case DW_OP_GNU_entry_value:
1981 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1982 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1983 break;
1985 case DW_OP_GNU_const_type:
1987 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1988 gcc_assert (o);
1989 dw2_asm_output_data_uleb128 (o, NULL);
1990 switch (val2->val_class)
1992 case dw_val_class_const:
1993 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1994 dw2_asm_output_data (1, l, NULL);
1995 dw2_asm_output_data (l, val2->v.val_int, NULL);
1996 break;
1997 case dw_val_class_vec:
1999 unsigned int elt_size = val2->v.val_vec.elt_size;
2000 unsigned int len = val2->v.val_vec.length;
2001 unsigned int i;
2002 unsigned char *p;
2004 l = len * elt_size;
2005 dw2_asm_output_data (1, l, NULL);
2006 if (elt_size > sizeof (HOST_WIDE_INT))
2008 elt_size /= 2;
2009 len *= 2;
2011 for (i = 0, p = val2->v.val_vec.array;
2012 i < len;
2013 i++, p += elt_size)
2014 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2015 "fp or vector constant word %u", i);
2017 break;
2018 case dw_val_class_const_double:
2020 unsigned HOST_WIDE_INT first, second;
2021 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2023 dw2_asm_output_data (1, 2 * l, NULL);
2024 if (WORDS_BIG_ENDIAN)
2026 first = val2->v.val_double.high;
2027 second = val2->v.val_double.low;
2029 else
2031 first = val2->v.val_double.low;
2032 second = val2->v.val_double.high;
2034 dw2_asm_output_data (l, first, NULL);
2035 dw2_asm_output_data (l, second, NULL);
2037 break;
2038 default:
2039 gcc_unreachable ();
2042 break;
2043 case DW_OP_GNU_regval_type:
2045 unsigned r = val1->v.val_unsigned;
2046 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2047 gcc_assert (o);
2048 if (for_eh_or_skip >= 0)
2050 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2051 gcc_assert (size_of_uleb128 (r)
2052 == size_of_uleb128 (val1->v.val_unsigned));
2054 dw2_asm_output_data_uleb128 (r, NULL);
2055 dw2_asm_output_data_uleb128 (o, NULL);
2057 break;
2058 case DW_OP_GNU_deref_type:
2060 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2061 gcc_assert (o);
2062 dw2_asm_output_data (1, val1->v.val_int, NULL);
2063 dw2_asm_output_data_uleb128 (o, NULL);
2065 break;
2066 case DW_OP_GNU_convert:
2067 case DW_OP_GNU_reinterpret:
2068 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2069 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2070 else
2072 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2073 gcc_assert (o);
2074 dw2_asm_output_data_uleb128 (o, NULL);
2076 break;
2078 case DW_OP_GNU_parameter_ref:
2080 unsigned long o;
2081 gcc_assert (val1->val_class == dw_val_class_die_ref);
2082 o = get_ref_die_offset (val1->v.val_die_ref.die);
2083 dw2_asm_output_data (4, o, NULL);
2085 break;
2087 default:
2088 /* Other codes have no operands. */
2089 break;
2093 /* Output a sequence of location operations.
2094 The for_eh_or_skip parameter controls whether register numbers are
2095 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2096 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2097 info). This should be suppressed for the cases that have not been converted
2098 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2100 void
2101 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2103 for (; loc != NULL; loc = loc->dw_loc_next)
2105 enum dwarf_location_atom opc = loc->dw_loc_opc;
2106 /* Output the opcode. */
2107 if (for_eh_or_skip >= 0
2108 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2110 unsigned r = (opc - DW_OP_breg0);
2111 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2112 gcc_assert (r <= 31);
2113 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2115 else if (for_eh_or_skip >= 0
2116 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2118 unsigned r = (opc - DW_OP_reg0);
2119 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2120 gcc_assert (r <= 31);
2121 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2124 dw2_asm_output_data (1, opc,
2125 "%s", dwarf_stack_op_name (opc));
2127 /* Output the operand(s) (if any). */
2128 output_loc_operands (loc, for_eh_or_skip);
2132 /* Output location description stack opcode's operands (if any).
2133 The output is single bytes on a line, suitable for .cfi_escape. */
2135 static void
2136 output_loc_operands_raw (dw_loc_descr_ref loc)
2138 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2139 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2141 switch (loc->dw_loc_opc)
2143 case DW_OP_addr:
2144 case DW_OP_GNU_addr_index:
2145 case DW_OP_GNU_const_index:
2146 case DW_OP_implicit_value:
2147 /* We cannot output addresses in .cfi_escape, only bytes. */
2148 gcc_unreachable ();
2150 case DW_OP_const1u:
2151 case DW_OP_const1s:
2152 case DW_OP_pick:
2153 case DW_OP_deref_size:
2154 case DW_OP_xderef_size:
2155 fputc (',', asm_out_file);
2156 dw2_asm_output_data_raw (1, val1->v.val_int);
2157 break;
2159 case DW_OP_const2u:
2160 case DW_OP_const2s:
2161 fputc (',', asm_out_file);
2162 dw2_asm_output_data_raw (2, val1->v.val_int);
2163 break;
2165 case DW_OP_const4u:
2166 case DW_OP_const4s:
2167 fputc (',', asm_out_file);
2168 dw2_asm_output_data_raw (4, val1->v.val_int);
2169 break;
2171 case DW_OP_const8u:
2172 case DW_OP_const8s:
2173 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2174 fputc (',', asm_out_file);
2175 dw2_asm_output_data_raw (8, val1->v.val_int);
2176 break;
2178 case DW_OP_skip:
2179 case DW_OP_bra:
2181 int offset;
2183 gcc_assert (val1->val_class == dw_val_class_loc);
2184 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2186 fputc (',', asm_out_file);
2187 dw2_asm_output_data_raw (2, offset);
2189 break;
2191 case DW_OP_regx:
2193 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2194 gcc_assert (size_of_uleb128 (r)
2195 == size_of_uleb128 (val1->v.val_unsigned));
2196 fputc (',', asm_out_file);
2197 dw2_asm_output_data_uleb128_raw (r);
2199 break;
2201 case DW_OP_constu:
2202 case DW_OP_plus_uconst:
2203 case DW_OP_piece:
2204 fputc (',', asm_out_file);
2205 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2206 break;
2208 case DW_OP_bit_piece:
2209 fputc (',', asm_out_file);
2210 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2211 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2212 break;
2214 case DW_OP_consts:
2215 case DW_OP_breg0:
2216 case DW_OP_breg1:
2217 case DW_OP_breg2:
2218 case DW_OP_breg3:
2219 case DW_OP_breg4:
2220 case DW_OP_breg5:
2221 case DW_OP_breg6:
2222 case DW_OP_breg7:
2223 case DW_OP_breg8:
2224 case DW_OP_breg9:
2225 case DW_OP_breg10:
2226 case DW_OP_breg11:
2227 case DW_OP_breg12:
2228 case DW_OP_breg13:
2229 case DW_OP_breg14:
2230 case DW_OP_breg15:
2231 case DW_OP_breg16:
2232 case DW_OP_breg17:
2233 case DW_OP_breg18:
2234 case DW_OP_breg19:
2235 case DW_OP_breg20:
2236 case DW_OP_breg21:
2237 case DW_OP_breg22:
2238 case DW_OP_breg23:
2239 case DW_OP_breg24:
2240 case DW_OP_breg25:
2241 case DW_OP_breg26:
2242 case DW_OP_breg27:
2243 case DW_OP_breg28:
2244 case DW_OP_breg29:
2245 case DW_OP_breg30:
2246 case DW_OP_breg31:
2247 case DW_OP_fbreg:
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2250 break;
2252 case DW_OP_bregx:
2254 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2255 gcc_assert (size_of_uleb128 (r)
2256 == size_of_uleb128 (val1->v.val_unsigned));
2257 fputc (',', asm_out_file);
2258 dw2_asm_output_data_uleb128_raw (r);
2259 fputc (',', asm_out_file);
2260 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2262 break;
2264 case DW_OP_GNU_implicit_pointer:
2265 case DW_OP_GNU_entry_value:
2266 case DW_OP_GNU_const_type:
2267 case DW_OP_GNU_regval_type:
2268 case DW_OP_GNU_deref_type:
2269 case DW_OP_GNU_convert:
2270 case DW_OP_GNU_reinterpret:
2271 case DW_OP_GNU_parameter_ref:
2272 gcc_unreachable ();
2273 break;
2275 default:
2276 /* Other codes have no operands. */
2277 break;
2281 void
2282 output_loc_sequence_raw (dw_loc_descr_ref loc)
2284 while (1)
2286 enum dwarf_location_atom opc = loc->dw_loc_opc;
2287 /* Output the opcode. */
2288 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2290 unsigned r = (opc - DW_OP_breg0);
2291 r = DWARF2_FRAME_REG_OUT (r, 1);
2292 gcc_assert (r <= 31);
2293 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2295 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2297 unsigned r = (opc - DW_OP_reg0);
2298 r = DWARF2_FRAME_REG_OUT (r, 1);
2299 gcc_assert (r <= 31);
2300 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2302 /* Output the opcode. */
2303 fprintf (asm_out_file, "%#x", opc);
2304 output_loc_operands_raw (loc);
2306 if (!loc->dw_loc_next)
2307 break;
2308 loc = loc->dw_loc_next;
2310 fputc (',', asm_out_file);
2314 /* This function builds a dwarf location descriptor sequence from a
2315 dw_cfa_location, adding the given OFFSET to the result of the
2316 expression. */
2318 struct dw_loc_descr_node *
2319 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2321 struct dw_loc_descr_node *head, *tmp;
2323 offset += cfa->offset;
2325 if (cfa->indirect)
2327 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2328 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2329 head->dw_loc_oprnd1.val_entry = NULL;
2330 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2331 add_loc_descr (&head, tmp);
2332 if (offset != 0)
2334 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2335 add_loc_descr (&head, tmp);
2338 else
2339 head = new_reg_loc_descr (cfa->reg, offset);
2341 return head;
2344 /* This function builds a dwarf location descriptor sequence for
2345 the address at OFFSET from the CFA when stack is aligned to
2346 ALIGNMENT byte. */
2348 struct dw_loc_descr_node *
2349 build_cfa_aligned_loc (dw_cfa_location *cfa,
2350 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2352 struct dw_loc_descr_node *head;
2353 unsigned int dwarf_fp
2354 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2356 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2357 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2359 head = new_reg_loc_descr (dwarf_fp, 0);
2360 add_loc_descr (&head, int_loc_descriptor (alignment));
2361 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2362 loc_descr_plus_const (&head, offset);
2364 else
2365 head = new_reg_loc_descr (dwarf_fp, offset);
2366 return head;
2369 /* And now, the support for symbolic debugging information. */
2371 /* .debug_str support. */
2372 static int output_indirect_string (void **, void *);
2374 static void dwarf2out_init (const char *);
2375 static void dwarf2out_finish (const char *);
2376 static void dwarf2out_assembly_start (void);
2377 static void dwarf2out_define (unsigned int, const char *);
2378 static void dwarf2out_undef (unsigned int, const char *);
2379 static void dwarf2out_start_source_file (unsigned, const char *);
2380 static void dwarf2out_end_source_file (unsigned);
2381 static void dwarf2out_function_decl (tree);
2382 static void dwarf2out_begin_block (unsigned, unsigned);
2383 static void dwarf2out_end_block (unsigned, unsigned);
2384 static bool dwarf2out_ignore_block (const_tree);
2385 static void dwarf2out_global_decl (tree);
2386 static void dwarf2out_type_decl (tree, int);
2387 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2388 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2389 dw_die_ref);
2390 static void dwarf2out_abstract_function (tree);
2391 static void dwarf2out_var_location (rtx);
2392 static void dwarf2out_begin_function (tree);
2393 static void dwarf2out_end_function (unsigned int);
2394 static void dwarf2out_set_name (tree, tree);
2396 /* The debug hooks structure. */
2398 const struct gcc_debug_hooks dwarf2_debug_hooks =
2400 dwarf2out_init,
2401 dwarf2out_finish,
2402 dwarf2out_assembly_start,
2403 dwarf2out_define,
2404 dwarf2out_undef,
2405 dwarf2out_start_source_file,
2406 dwarf2out_end_source_file,
2407 dwarf2out_begin_block,
2408 dwarf2out_end_block,
2409 dwarf2out_ignore_block,
2410 dwarf2out_source_line,
2411 dwarf2out_begin_prologue,
2412 #if VMS_DEBUGGING_INFO
2413 dwarf2out_vms_end_prologue,
2414 dwarf2out_vms_begin_epilogue,
2415 #else
2416 debug_nothing_int_charstar,
2417 debug_nothing_int_charstar,
2418 #endif
2419 dwarf2out_end_epilogue,
2420 dwarf2out_begin_function,
2421 dwarf2out_end_function, /* end_function */
2422 dwarf2out_function_decl, /* function_decl */
2423 dwarf2out_global_decl,
2424 dwarf2out_type_decl, /* type_decl */
2425 dwarf2out_imported_module_or_decl,
2426 debug_nothing_tree, /* deferred_inline_function */
2427 /* The DWARF 2 backend tries to reduce debugging bloat by not
2428 emitting the abstract description of inline functions until
2429 something tries to reference them. */
2430 dwarf2out_abstract_function, /* outlining_inline_function */
2431 debug_nothing_rtx, /* label */
2432 debug_nothing_int, /* handle_pch */
2433 dwarf2out_var_location,
2434 dwarf2out_switch_text_section,
2435 dwarf2out_set_name,
2436 1, /* start_end_main_source_file */
2437 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2440 /* NOTE: In the comments in this file, many references are made to
2441 "Debugging Information Entries". This term is abbreviated as `DIE'
2442 throughout the remainder of this file. */
2444 /* An internal representation of the DWARF output is built, and then
2445 walked to generate the DWARF debugging info. The walk of the internal
2446 representation is done after the entire program has been compiled.
2447 The types below are used to describe the internal representation. */
2449 /* Whether to put type DIEs into their own section .debug_types instead
2450 of making them part of the .debug_info section. Only supported for
2451 Dwarf V4 or higher and the user didn't disable them through
2452 -fno-debug-types-section. It is more efficient to put them in a
2453 separate comdat sections since the linker will then be able to
2454 remove duplicates. But not all tools support .debug_types sections
2455 yet. */
2457 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2459 /* Various DIE's use offsets relative to the beginning of the
2460 .debug_info section to refer to each other. */
2462 typedef long int dw_offset;
2464 /* Define typedefs here to avoid circular dependencies. */
2466 typedef struct dw_attr_struct *dw_attr_ref;
2467 typedef struct dw_line_info_struct *dw_line_info_ref;
2468 typedef struct pubname_struct *pubname_ref;
2469 typedef struct dw_ranges_struct *dw_ranges_ref;
2470 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2471 typedef struct comdat_type_struct *comdat_type_node_ref;
2473 /* The entries in the line_info table more-or-less mirror the opcodes
2474 that are used in the real dwarf line table. Arrays of these entries
2475 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2476 supported. */
2478 enum dw_line_info_opcode {
2479 /* Emit DW_LNE_set_address; the operand is the label index. */
2480 LI_set_address,
2482 /* Emit a row to the matrix with the given line. This may be done
2483 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2484 special opcodes. */
2485 LI_set_line,
2487 /* Emit a DW_LNS_set_file. */
2488 LI_set_file,
2490 /* Emit a DW_LNS_set_column. */
2491 LI_set_column,
2493 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2494 LI_negate_stmt,
2496 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2497 LI_set_prologue_end,
2498 LI_set_epilogue_begin,
2500 /* Emit a DW_LNE_set_discriminator. */
2501 LI_set_discriminator
2504 typedef struct GTY(()) dw_line_info_struct {
2505 enum dw_line_info_opcode opcode;
2506 unsigned int val;
2507 } dw_line_info_entry;
2510 typedef struct GTY(()) dw_line_info_table_struct {
2511 /* The label that marks the end of this section. */
2512 const char *end_label;
2514 /* The values for the last row of the matrix, as collected in the table.
2515 These are used to minimize the changes to the next row. */
2516 unsigned int file_num;
2517 unsigned int line_num;
2518 unsigned int column_num;
2519 int discrim_num;
2520 bool is_stmt;
2521 bool in_use;
2523 vec<dw_line_info_entry, va_gc> *entries;
2524 } dw_line_info_table;
2526 typedef dw_line_info_table *dw_line_info_table_p;
2529 /* Each DIE attribute has a field specifying the attribute kind,
2530 a link to the next attribute in the chain, and an attribute value.
2531 Attributes are typically linked below the DIE they modify. */
2533 typedef struct GTY(()) dw_attr_struct {
2534 enum dwarf_attribute dw_attr;
2535 dw_val_node dw_attr_val;
2537 dw_attr_node;
2540 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2541 The children of each node form a circular list linked by
2542 die_sib. die_child points to the node *before* the "first" child node. */
2544 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2545 union die_symbol_or_type_node
2547 const char * GTY ((tag ("0"))) die_symbol;
2548 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2550 GTY ((desc ("%0.comdat_type_p"))) die_id;
2551 vec<dw_attr_node, va_gc> *die_attr;
2552 dw_die_ref die_parent;
2553 dw_die_ref die_child;
2554 dw_die_ref die_sib;
2555 dw_die_ref die_definition; /* ref from a specification to its definition */
2556 dw_offset die_offset;
2557 unsigned long die_abbrev;
2558 int die_mark;
2559 unsigned int decl_id;
2560 enum dwarf_tag die_tag;
2561 /* Die is used and must not be pruned as unused. */
2562 BOOL_BITFIELD die_perennial_p : 1;
2563 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2564 /* Lots of spare bits. */
2566 die_node;
2568 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2569 #define FOR_EACH_CHILD(die, c, expr) do { \
2570 c = die->die_child; \
2571 if (c) do { \
2572 c = c->die_sib; \
2573 expr; \
2574 } while (c != die->die_child); \
2575 } while (0)
2577 /* The pubname structure */
2579 typedef struct GTY(()) pubname_struct {
2580 dw_die_ref die;
2581 const char *name;
2583 pubname_entry;
2586 struct GTY(()) dw_ranges_struct {
2587 /* If this is positive, it's a block number, otherwise it's a
2588 bitwise-negated index into dw_ranges_by_label. */
2589 int num;
2592 /* A structure to hold a macinfo entry. */
2594 typedef struct GTY(()) macinfo_struct {
2595 unsigned char code;
2596 unsigned HOST_WIDE_INT lineno;
2597 const char *info;
2599 macinfo_entry;
2602 struct GTY(()) dw_ranges_by_label_struct {
2603 const char *begin;
2604 const char *end;
2607 /* The comdat type node structure. */
2608 typedef struct GTY(()) comdat_type_struct
2610 dw_die_ref root_die;
2611 dw_die_ref type_die;
2612 dw_die_ref skeleton_die;
2613 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2614 struct comdat_type_struct *next;
2616 comdat_type_node;
2618 /* The limbo die list structure. */
2619 typedef struct GTY(()) limbo_die_struct {
2620 dw_die_ref die;
2621 tree created_for;
2622 struct limbo_die_struct *next;
2624 limbo_die_node;
2626 typedef struct skeleton_chain_struct
2628 dw_die_ref old_die;
2629 dw_die_ref new_die;
2630 struct skeleton_chain_struct *parent;
2632 skeleton_chain_node;
2634 /* Define a macro which returns nonzero for a TYPE_DECL which was
2635 implicitly generated for a type.
2637 Note that, unlike the C front-end (which generates a NULL named
2638 TYPE_DECL node for each complete tagged type, each array type,
2639 and each function type node created) the C++ front-end generates
2640 a _named_ TYPE_DECL node for each tagged type node created.
2641 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2642 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2643 front-end, but for each type, tagged or not. */
2645 #define TYPE_DECL_IS_STUB(decl) \
2646 (DECL_NAME (decl) == NULL_TREE \
2647 || (DECL_ARTIFICIAL (decl) \
2648 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2649 /* This is necessary for stub decls that \
2650 appear in nested inline functions. */ \
2651 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2652 && (decl_ultimate_origin (decl) \
2653 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2655 /* Information concerning the compilation unit's programming
2656 language, and compiler version. */
2658 /* Fixed size portion of the DWARF compilation unit header. */
2659 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2660 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2662 /* Fixed size portion of the DWARF comdat type unit header. */
2663 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2664 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2665 + DWARF_OFFSET_SIZE)
2667 /* Fixed size portion of public names info. */
2668 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2670 /* Fixed size portion of the address range info. */
2671 #define DWARF_ARANGES_HEADER_SIZE \
2672 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2673 DWARF2_ADDR_SIZE * 2) \
2674 - DWARF_INITIAL_LENGTH_SIZE)
2676 /* Size of padding portion in the address range info. It must be
2677 aligned to twice the pointer size. */
2678 #define DWARF_ARANGES_PAD_SIZE \
2679 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2680 DWARF2_ADDR_SIZE * 2) \
2681 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2683 /* Use assembler line directives if available. */
2684 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2685 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2686 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2687 #else
2688 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2689 #endif
2690 #endif
2692 /* Minimum line offset in a special line info. opcode.
2693 This value was chosen to give a reasonable range of values. */
2694 #define DWARF_LINE_BASE -10
2696 /* First special line opcode - leave room for the standard opcodes. */
2697 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2699 /* Range of line offsets in a special line info. opcode. */
2700 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2702 /* Flag that indicates the initial value of the is_stmt_start flag.
2703 In the present implementation, we do not mark any lines as
2704 the beginning of a source statement, because that information
2705 is not made available by the GCC front-end. */
2706 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2708 /* Maximum number of operations per instruction bundle. */
2709 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2710 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2711 #endif
2713 /* This location is used by calc_die_sizes() to keep track
2714 the offset of each DIE within the .debug_info section. */
2715 static unsigned long next_die_offset;
2717 /* Record the root of the DIE's built for the current compilation unit. */
2718 static GTY(()) dw_die_ref single_comp_unit_die;
2720 /* A list of type DIEs that have been separated into comdat sections. */
2721 static GTY(()) comdat_type_node *comdat_type_list;
2723 /* A list of DIEs with a NULL parent waiting to be relocated. */
2724 static GTY(()) limbo_die_node *limbo_die_list;
2726 /* A list of DIEs for which we may have to generate
2727 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2728 static GTY(()) limbo_die_node *deferred_asm_name;
2730 /* Filenames referenced by this compilation unit. */
2731 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2733 /* A hash table of references to DIE's that describe declarations.
2734 The key is a DECL_UID() which is a unique number identifying each decl. */
2735 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2737 /* A hash table of references to DIE's that describe COMMON blocks.
2738 The key is DECL_UID() ^ die_parent. */
2739 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2741 typedef struct GTY(()) die_arg_entry_struct {
2742 dw_die_ref die;
2743 tree arg;
2744 } die_arg_entry;
2747 /* Node of the variable location list. */
2748 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2749 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2750 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2751 in mode of the EXPR_LIST node and first EXPR_LIST operand
2752 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2753 location or NULL for padding. For larger bitsizes,
2754 mode is 0 and first operand is a CONCAT with bitsize
2755 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2756 NULL as second operand. */
2757 rtx GTY (()) loc;
2758 const char * GTY (()) label;
2759 struct var_loc_node * GTY (()) next;
2762 /* Variable location list. */
2763 struct GTY (()) var_loc_list_def {
2764 struct var_loc_node * GTY (()) first;
2766 /* Pointer to the last but one or last element of the
2767 chained list. If the list is empty, both first and
2768 last are NULL, if the list contains just one node
2769 or the last node certainly is not redundant, it points
2770 to the last node, otherwise points to the last but one.
2771 Do not mark it for GC because it is marked through the chain. */
2772 struct var_loc_node * GTY ((skip ("%h"))) last;
2774 /* Pointer to the last element before section switch,
2775 if NULL, either sections weren't switched or first
2776 is after section switch. */
2777 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2779 /* DECL_UID of the variable decl. */
2780 unsigned int decl_id;
2782 typedef struct var_loc_list_def var_loc_list;
2784 /* Call argument location list. */
2785 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2786 rtx GTY (()) call_arg_loc_note;
2787 const char * GTY (()) label;
2788 tree GTY (()) block;
2789 bool tail_call_p;
2790 rtx GTY (()) symbol_ref;
2791 struct call_arg_loc_node * GTY (()) next;
2795 /* Table of decl location linked lists. */
2796 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2798 /* Head and tail of call_arg_loc chain. */
2799 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2800 static struct call_arg_loc_node *call_arg_loc_last;
2802 /* Number of call sites in the current function. */
2803 static int call_site_count = -1;
2804 /* Number of tail call sites in the current function. */
2805 static int tail_call_site_count = -1;
2807 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2808 DIEs. */
2809 static vec<dw_die_ref> block_map;
2811 /* A cached location list. */
2812 struct GTY (()) cached_dw_loc_list_def {
2813 /* The DECL_UID of the decl that this entry describes. */
2814 unsigned int decl_id;
2816 /* The cached location list. */
2817 dw_loc_list_ref loc_list;
2819 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2821 /* Table of cached location lists. */
2822 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2824 /* A pointer to the base of a list of references to DIE's that
2825 are uniquely identified by their tag, presence/absence of
2826 children DIE's, and list of attribute/value pairs. */
2827 static GTY((length ("abbrev_die_table_allocated")))
2828 dw_die_ref *abbrev_die_table;
2830 /* Number of elements currently allocated for abbrev_die_table. */
2831 static GTY(()) unsigned abbrev_die_table_allocated;
2833 /* Number of elements in type_die_table currently in use. */
2834 static GTY(()) unsigned abbrev_die_table_in_use;
2836 /* Size (in elements) of increments by which we may expand the
2837 abbrev_die_table. */
2838 #define ABBREV_DIE_TABLE_INCREMENT 256
2840 /* A global counter for generating labels for line number data. */
2841 static unsigned int line_info_label_num;
2843 /* The current table to which we should emit line number information
2844 for the current function. This will be set up at the beginning of
2845 assembly for the function. */
2846 static dw_line_info_table *cur_line_info_table;
2848 /* The two default tables of line number info. */
2849 static GTY(()) dw_line_info_table *text_section_line_info;
2850 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2852 /* The set of all non-default tables of line number info. */
2853 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2855 /* A flag to tell pubnames/types export if there is an info section to
2856 refer to. */
2857 static bool info_section_emitted;
2859 /* A pointer to the base of a table that contains a list of publicly
2860 accessible names. */
2861 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2863 /* A pointer to the base of a table that contains a list of publicly
2864 accessible types. */
2865 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2867 /* A pointer to the base of a table that contains a list of macro
2868 defines/undefines (and file start/end markers). */
2869 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2871 /* True if .debug_macinfo or .debug_macros section is going to be
2872 emitted. */
2873 #define have_macinfo \
2874 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2875 && !macinfo_table->is_empty ())
2877 /* Array of dies for which we should generate .debug_ranges info. */
2878 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2880 /* Number of elements currently allocated for ranges_table. */
2881 static GTY(()) unsigned ranges_table_allocated;
2883 /* Number of elements in ranges_table currently in use. */
2884 static GTY(()) unsigned ranges_table_in_use;
2886 /* Array of pairs of labels referenced in ranges_table. */
2887 static GTY ((length ("ranges_by_label_allocated")))
2888 dw_ranges_by_label_ref ranges_by_label;
2890 /* Number of elements currently allocated for ranges_by_label. */
2891 static GTY(()) unsigned ranges_by_label_allocated;
2893 /* Number of elements in ranges_by_label currently in use. */
2894 static GTY(()) unsigned ranges_by_label_in_use;
2896 /* Size (in elements) of increments by which we may expand the
2897 ranges_table. */
2898 #define RANGES_TABLE_INCREMENT 64
2900 /* Whether we have location lists that need outputting */
2901 static GTY(()) bool have_location_lists;
2903 /* Unique label counter. */
2904 static GTY(()) unsigned int loclabel_num;
2906 /* Unique label counter for point-of-call tables. */
2907 static GTY(()) unsigned int poc_label_num;
2909 /* Record whether the function being analyzed contains inlined functions. */
2910 static int current_function_has_inlines;
2912 /* The last file entry emitted by maybe_emit_file(). */
2913 static GTY(()) struct dwarf_file_data * last_emitted_file;
2915 /* Number of internal labels generated by gen_internal_sym(). */
2916 static GTY(()) int label_num;
2918 /* Cached result of previous call to lookup_filename. */
2919 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2921 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2923 /* Instances of generic types for which we need to generate debug
2924 info that describe their generic parameters and arguments. That
2925 generation needs to happen once all types are properly laid out so
2926 we do it at the end of compilation. */
2927 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2929 /* Offset from the "steady-state frame pointer" to the frame base,
2930 within the current function. */
2931 static HOST_WIDE_INT frame_pointer_fb_offset;
2932 static bool frame_pointer_fb_offset_valid;
2934 static vec<dw_die_ref> base_types;
2936 /* Forward declarations for functions defined in this file. */
2938 static int is_pseudo_reg (const_rtx);
2939 static tree type_main_variant (tree);
2940 static int is_tagged_type (const_tree);
2941 static const char *dwarf_tag_name (unsigned);
2942 static const char *dwarf_attr_name (unsigned);
2943 static const char *dwarf_form_name (unsigned);
2944 static tree decl_ultimate_origin (const_tree);
2945 static tree decl_class_context (tree);
2946 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2947 static inline enum dw_val_class AT_class (dw_attr_ref);
2948 static inline unsigned int AT_index (dw_attr_ref);
2949 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2950 static inline unsigned AT_flag (dw_attr_ref);
2951 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2952 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2953 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2954 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2955 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2956 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2957 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2958 unsigned int, unsigned char *);
2959 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2960 static hashval_t debug_str_do_hash (const void *);
2961 static int debug_str_eq (const void *, const void *);
2962 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2963 static inline const char *AT_string (dw_attr_ref);
2964 static enum dwarf_form AT_string_form (dw_attr_ref);
2965 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2966 static void add_AT_specification (dw_die_ref, dw_die_ref);
2967 static inline dw_die_ref AT_ref (dw_attr_ref);
2968 static inline int AT_ref_external (dw_attr_ref);
2969 static inline void set_AT_ref_external (dw_attr_ref, int);
2970 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2971 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2972 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2973 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2974 dw_loc_list_ref);
2975 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2976 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2977 static void remove_addr_table_entry (addr_table_entry *);
2978 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2979 static inline rtx AT_addr (dw_attr_ref);
2980 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2981 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2982 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2983 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2984 unsigned HOST_WIDE_INT);
2985 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2986 unsigned long, bool);
2987 static inline const char *AT_lbl (dw_attr_ref);
2988 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2989 static const char *get_AT_low_pc (dw_die_ref);
2990 static const char *get_AT_hi_pc (dw_die_ref);
2991 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2992 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2993 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2994 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2995 static bool is_cxx (void);
2996 static bool is_fortran (void);
2997 static bool is_ada (void);
2998 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2999 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3000 static void add_child_die (dw_die_ref, dw_die_ref);
3001 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3002 static dw_die_ref lookup_type_die (tree);
3003 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3004 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3005 static void equate_type_number_to_die (tree, dw_die_ref);
3006 static hashval_t decl_die_table_hash (const void *);
3007 static int decl_die_table_eq (const void *, const void *);
3008 static dw_die_ref lookup_decl_die (tree);
3009 static hashval_t common_block_die_table_hash (const void *);
3010 static int common_block_die_table_eq (const void *, const void *);
3011 static hashval_t decl_loc_table_hash (const void *);
3012 static int decl_loc_table_eq (const void *, const void *);
3013 static var_loc_list *lookup_decl_loc (const_tree);
3014 static void equate_decl_number_to_die (tree, dw_die_ref);
3015 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3016 static void print_spaces (FILE *);
3017 static void print_die (dw_die_ref, FILE *);
3018 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3019 static dw_die_ref pop_compile_unit (dw_die_ref);
3020 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3021 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3022 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3023 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3024 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3025 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3026 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3027 struct md5_ctx *, int *);
3028 struct checksum_attributes;
3029 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3030 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3031 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3032 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3033 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3034 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3035 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3036 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3037 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3038 static void compute_section_prefix (dw_die_ref);
3039 static int is_type_die (dw_die_ref);
3040 static int is_comdat_die (dw_die_ref);
3041 static int is_symbol_die (dw_die_ref);
3042 static inline bool is_template_instantiation (dw_die_ref);
3043 static void assign_symbol_names (dw_die_ref);
3044 static void break_out_includes (dw_die_ref);
3045 static int is_declaration_die (dw_die_ref);
3046 static int should_move_die_to_comdat (dw_die_ref);
3047 static dw_die_ref clone_as_declaration (dw_die_ref);
3048 static dw_die_ref clone_die (dw_die_ref);
3049 static dw_die_ref clone_tree (dw_die_ref);
3050 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3051 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3052 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3053 static dw_die_ref generate_skeleton (dw_die_ref);
3054 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3055 dw_die_ref,
3056 dw_die_ref);
3057 static void break_out_comdat_types (dw_die_ref);
3058 static void copy_decls_for_unworthy_types (dw_die_ref);
3060 static void add_sibling_attributes (dw_die_ref);
3061 static void output_location_lists (dw_die_ref);
3062 static int constant_size (unsigned HOST_WIDE_INT);
3063 static unsigned long size_of_die (dw_die_ref);
3064 static void calc_die_sizes (dw_die_ref);
3065 static void calc_base_type_die_sizes (void);
3066 static void mark_dies (dw_die_ref);
3067 static void unmark_dies (dw_die_ref);
3068 static void unmark_all_dies (dw_die_ref);
3069 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3070 static unsigned long size_of_aranges (void);
3071 static enum dwarf_form value_format (dw_attr_ref);
3072 static void output_value_format (dw_attr_ref);
3073 static void output_abbrev_section (void);
3074 static void output_die_abbrevs (unsigned long, dw_die_ref);
3075 static void output_die_symbol (dw_die_ref);
3076 static void output_die (dw_die_ref);
3077 static void output_compilation_unit_header (void);
3078 static void output_comp_unit (dw_die_ref, int);
3079 static void output_comdat_type_unit (comdat_type_node *);
3080 static const char *dwarf2_name (tree, int);
3081 static void add_pubname (tree, dw_die_ref);
3082 static void add_enumerator_pubname (const char *, dw_die_ref);
3083 static void add_pubname_string (const char *, dw_die_ref);
3084 static void add_pubtype (tree, dw_die_ref);
3085 static void output_pubnames (vec<pubname_entry, va_gc> *);
3086 static void output_aranges (unsigned long);
3087 static unsigned int add_ranges_num (int);
3088 static unsigned int add_ranges (const_tree);
3089 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3090 bool *, bool);
3091 static void output_ranges (void);
3092 static dw_line_info_table *new_line_info_table (void);
3093 static void output_line_info (bool);
3094 static void output_file_names (void);
3095 static dw_die_ref base_type_die (tree);
3096 static int is_base_type (tree);
3097 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3098 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3099 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3100 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3101 static int type_is_enum (const_tree);
3102 static unsigned int dbx_reg_number (const_rtx);
3103 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3104 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3105 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3106 enum var_init_status);
3107 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3108 enum var_init_status);
3109 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3110 enum var_init_status);
3111 static int is_based_loc (const_rtx);
3112 static int resolve_one_addr (rtx *, void *);
3113 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3114 enum var_init_status);
3115 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3116 enum var_init_status);
3117 static dw_loc_list_ref loc_list_from_tree (tree, int);
3118 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3119 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3120 static tree field_type (const_tree);
3121 static unsigned int simple_type_align_in_bits (const_tree);
3122 static unsigned int simple_decl_align_in_bits (const_tree);
3123 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3124 static HOST_WIDE_INT field_byte_offset (const_tree);
3125 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3126 dw_loc_list_ref);
3127 static void add_data_member_location_attribute (dw_die_ref, tree);
3128 static bool add_const_value_attribute (dw_die_ref, rtx);
3129 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3130 static void insert_double (double_int, unsigned char *);
3131 static void insert_float (const_rtx, unsigned char *);
3132 static rtx rtl_for_decl_location (tree);
3133 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3134 enum dwarf_attribute);
3135 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3136 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3137 static void add_name_attribute (dw_die_ref, const char *);
3138 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3139 static void add_comp_dir_attribute (dw_die_ref);
3140 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3141 static void add_subscript_info (dw_die_ref, tree, bool);
3142 static void add_byte_size_attribute (dw_die_ref, tree);
3143 static void add_bit_offset_attribute (dw_die_ref, tree);
3144 static void add_bit_size_attribute (dw_die_ref, tree);
3145 static void add_prototyped_attribute (dw_die_ref, tree);
3146 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3147 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3148 static void add_src_coords_attributes (dw_die_ref, tree);
3149 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3150 static void push_decl_scope (tree);
3151 static void pop_decl_scope (void);
3152 static dw_die_ref scope_die_for (tree, dw_die_ref);
3153 static inline int local_scope_p (dw_die_ref);
3154 static inline int class_scope_p (dw_die_ref);
3155 static inline int class_or_namespace_scope_p (dw_die_ref);
3156 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3157 static void add_calling_convention_attribute (dw_die_ref, tree);
3158 static const char *type_tag (const_tree);
3159 static tree member_declared_type (const_tree);
3160 #if 0
3161 static const char *decl_start_label (tree);
3162 #endif
3163 static void gen_array_type_die (tree, dw_die_ref);
3164 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3165 #if 0
3166 static void gen_entry_point_die (tree, dw_die_ref);
3167 #endif
3168 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3169 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3170 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3171 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3172 static void gen_formal_types_die (tree, dw_die_ref);
3173 static void gen_subprogram_die (tree, dw_die_ref);
3174 static void gen_variable_die (tree, tree, dw_die_ref);
3175 static void gen_const_die (tree, dw_die_ref);
3176 static void gen_label_die (tree, dw_die_ref);
3177 static void gen_lexical_block_die (tree, dw_die_ref, int);
3178 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3179 static void gen_field_die (tree, dw_die_ref);
3180 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3181 static dw_die_ref gen_compile_unit_die (const char *);
3182 static void gen_inheritance_die (tree, tree, dw_die_ref);
3183 static void gen_member_die (tree, dw_die_ref);
3184 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3185 enum debug_info_usage);
3186 static void gen_subroutine_type_die (tree, dw_die_ref);
3187 static void gen_typedef_die (tree, dw_die_ref);
3188 static void gen_type_die (tree, dw_die_ref);
3189 static void gen_block_die (tree, dw_die_ref, int);
3190 static void decls_for_scope (tree, dw_die_ref, int);
3191 static inline int is_redundant_typedef (const_tree);
3192 static bool is_naming_typedef_decl (const_tree);
3193 static inline dw_die_ref get_context_die (tree);
3194 static void gen_namespace_die (tree, dw_die_ref);
3195 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3196 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3197 static dw_die_ref force_decl_die (tree);
3198 static dw_die_ref force_type_die (tree);
3199 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3200 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3201 static struct dwarf_file_data * lookup_filename (const char *);
3202 static void retry_incomplete_types (void);
3203 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3204 static void gen_generic_params_dies (tree);
3205 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3206 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3207 static void splice_child_die (dw_die_ref, dw_die_ref);
3208 static int file_info_cmp (const void *, const void *);
3209 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3210 const char *, const char *);
3211 static void output_loc_list (dw_loc_list_ref);
3212 static char *gen_internal_sym (const char *);
3213 static bool want_pubnames (void);
3215 static void prune_unmark_dies (dw_die_ref);
3216 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3217 static void prune_unused_types_mark (dw_die_ref, int);
3218 static void prune_unused_types_walk (dw_die_ref);
3219 static void prune_unused_types_walk_attribs (dw_die_ref);
3220 static void prune_unused_types_prune (dw_die_ref);
3221 static void prune_unused_types (void);
3222 static int maybe_emit_file (struct dwarf_file_data *fd);
3223 static inline const char *AT_vms_delta1 (dw_attr_ref);
3224 static inline const char *AT_vms_delta2 (dw_attr_ref);
3225 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3226 const char *, const char *);
3227 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3228 static void gen_remaining_tmpl_value_param_die_attribute (void);
3229 static bool generic_type_p (tree);
3230 static void schedule_generic_params_dies_gen (tree t);
3231 static void gen_scheduled_generic_parms_dies (void);
3233 static const char *comp_dir_string (void);
3235 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3237 /* enum for tracking thread-local variables whose address is really an offset
3238 relative to the TLS pointer, which will need link-time relocation, but will
3239 not need relocation by the DWARF consumer. */
3241 enum dtprel_bool
3243 dtprel_false = 0,
3244 dtprel_true = 1
3247 /* Return the operator to use for an address of a variable. For dtprel_true, we
3248 use DW_OP_const*. For regular variables, which need both link-time
3249 relocation and consumer-level relocation (e.g., to account for shared objects
3250 loaded at a random address), we use DW_OP_addr*. */
3252 static inline enum dwarf_location_atom
3253 dw_addr_op (enum dtprel_bool dtprel)
3255 if (dtprel == dtprel_true)
3256 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3257 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3258 else
3259 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3262 /* Return a pointer to a newly allocated address location description. If
3263 dwarf_split_debug_info is true, then record the address with the appropriate
3264 relocation. */
3265 static inline dw_loc_descr_ref
3266 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3268 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3270 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3271 ref->dw_loc_oprnd1.v.val_addr = addr;
3272 ref->dtprel = dtprel;
3273 if (dwarf_split_debug_info)
3274 ref->dw_loc_oprnd1.val_entry
3275 = add_addr_table_entry (addr,
3276 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3277 else
3278 ref->dw_loc_oprnd1.val_entry = NULL;
3280 return ref;
3283 /* Section names used to hold DWARF debugging information. */
3285 #ifndef DEBUG_INFO_SECTION
3286 #define DEBUG_INFO_SECTION ".debug_info"
3287 #endif
3288 #ifndef DEBUG_DWO_INFO_SECTION
3289 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3290 #endif
3291 #ifndef DEBUG_ABBREV_SECTION
3292 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3293 #endif
3294 #ifndef DEBUG_DWO_ABBREV_SECTION
3295 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3296 #endif
3297 #ifndef DEBUG_ARANGES_SECTION
3298 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3299 #endif
3300 #ifndef DEBUG_ADDR_SECTION
3301 #define DEBUG_ADDR_SECTION ".debug_addr"
3302 #endif
3303 #ifndef DEBUG_NORM_MACINFO_SECTION
3304 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3305 #endif
3306 #ifndef DEBUG_DWO_MACINFO_SECTION
3307 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3308 #endif
3309 #ifndef DEBUG_MACINFO_SECTION
3310 #define DEBUG_MACINFO_SECTION \
3311 (!dwarf_split_debug_info \
3312 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3313 #endif
3314 #ifndef DEBUG_NORM_MACRO_SECTION
3315 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3316 #endif
3317 #ifndef DEBUG_DWO_MACRO_SECTION
3318 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3319 #endif
3320 #ifndef DEBUG_MACRO_SECTION
3321 #define DEBUG_MACRO_SECTION \
3322 (!dwarf_split_debug_info \
3323 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3324 #endif
3325 #ifndef DEBUG_LINE_SECTION
3326 #define DEBUG_LINE_SECTION ".debug_line"
3327 #endif
3328 #ifndef DEBUG_DWO_LINE_SECTION
3329 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3330 #endif
3331 #ifndef DEBUG_LOC_SECTION
3332 #define DEBUG_LOC_SECTION ".debug_loc"
3333 #endif
3334 #ifndef DEBUG_DWO_LOC_SECTION
3335 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3336 #endif
3337 #ifndef DEBUG_PUBNAMES_SECTION
3338 #define DEBUG_PUBNAMES_SECTION \
3339 ((debug_generate_pub_sections == 2) \
3340 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3341 #endif
3342 #ifndef DEBUG_PUBTYPES_SECTION
3343 #define DEBUG_PUBTYPES_SECTION \
3344 ((debug_generate_pub_sections == 2) \
3345 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3346 #endif
3347 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3348 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3349 #ifndef DEBUG_STR_OFFSETS_SECTION
3350 #define DEBUG_STR_OFFSETS_SECTION \
3351 (!dwarf_split_debug_info \
3352 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3353 #endif
3354 #ifndef DEBUG_STR_DWO_SECTION
3355 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3356 #endif
3357 #ifndef DEBUG_STR_SECTION
3358 #define DEBUG_STR_SECTION ".debug_str"
3359 #endif
3360 #ifndef DEBUG_RANGES_SECTION
3361 #define DEBUG_RANGES_SECTION ".debug_ranges"
3362 #endif
3364 /* Standard ELF section names for compiled code and data. */
3365 #ifndef TEXT_SECTION_NAME
3366 #define TEXT_SECTION_NAME ".text"
3367 #endif
3369 /* Section flags for .debug_macinfo/.debug_macro section. */
3370 #define DEBUG_MACRO_SECTION_FLAGS \
3371 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3373 /* Section flags for .debug_str section. */
3374 #define DEBUG_STR_SECTION_FLAGS \
3375 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3376 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3377 : SECTION_DEBUG)
3379 /* Section flags for .debug_str.dwo section. */
3380 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3382 /* Labels we insert at beginning sections we can reference instead of
3383 the section names themselves. */
3385 #ifndef TEXT_SECTION_LABEL
3386 #define TEXT_SECTION_LABEL "Ltext"
3387 #endif
3388 #ifndef COLD_TEXT_SECTION_LABEL
3389 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3390 #endif
3391 #ifndef DEBUG_LINE_SECTION_LABEL
3392 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3393 #endif
3394 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3395 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3396 #endif
3397 #ifndef DEBUG_INFO_SECTION_LABEL
3398 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3399 #endif
3400 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3401 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3402 #endif
3403 #ifndef DEBUG_ABBREV_SECTION_LABEL
3404 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3405 #endif
3406 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3407 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3408 #endif
3409 #ifndef DEBUG_ADDR_SECTION_LABEL
3410 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3411 #endif
3412 #ifndef DEBUG_LOC_SECTION_LABEL
3413 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3414 #endif
3415 #ifndef DEBUG_RANGES_SECTION_LABEL
3416 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3417 #endif
3418 #ifndef DEBUG_MACINFO_SECTION_LABEL
3419 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3420 #endif
3421 #ifndef DEBUG_MACRO_SECTION_LABEL
3422 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3423 #endif
3424 #define SKELETON_COMP_DIE_ABBREV 1
3425 #define SKELETON_TYPE_DIE_ABBREV 2
3427 /* Definitions of defaults for formats and names of various special
3428 (artificial) labels which may be generated within this file (when the -g
3429 options is used and DWARF2_DEBUGGING_INFO is in effect.
3430 If necessary, these may be overridden from within the tm.h file, but
3431 typically, overriding these defaults is unnecessary. */
3433 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3440 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3441 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3442 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3443 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3444 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3445 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3446 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3448 #ifndef TEXT_END_LABEL
3449 #define TEXT_END_LABEL "Letext"
3450 #endif
3451 #ifndef COLD_END_LABEL
3452 #define COLD_END_LABEL "Letext_cold"
3453 #endif
3454 #ifndef BLOCK_BEGIN_LABEL
3455 #define BLOCK_BEGIN_LABEL "LBB"
3456 #endif
3457 #ifndef BLOCK_END_LABEL
3458 #define BLOCK_END_LABEL "LBE"
3459 #endif
3460 #ifndef LINE_CODE_LABEL
3461 #define LINE_CODE_LABEL "LM"
3462 #endif
3465 /* Return the root of the DIE's built for the current compilation unit. */
3466 static dw_die_ref
3467 comp_unit_die (void)
3469 if (!single_comp_unit_die)
3470 single_comp_unit_die = gen_compile_unit_die (NULL);
3471 return single_comp_unit_die;
3474 /* We allow a language front-end to designate a function that is to be
3475 called to "demangle" any name before it is put into a DIE. */
3477 static const char *(*demangle_name_func) (const char *);
3479 void
3480 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3482 demangle_name_func = func;
3485 /* Test if rtl node points to a pseudo register. */
3487 static inline int
3488 is_pseudo_reg (const_rtx rtl)
3490 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3491 || (GET_CODE (rtl) == SUBREG
3492 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3495 /* Return a reference to a type, with its const and volatile qualifiers
3496 removed. */
3498 static inline tree
3499 type_main_variant (tree type)
3501 type = TYPE_MAIN_VARIANT (type);
3503 /* ??? There really should be only one main variant among any group of
3504 variants of a given type (and all of the MAIN_VARIANT values for all
3505 members of the group should point to that one type) but sometimes the C
3506 front-end messes this up for array types, so we work around that bug
3507 here. */
3508 if (TREE_CODE (type) == ARRAY_TYPE)
3509 while (type != TYPE_MAIN_VARIANT (type))
3510 type = TYPE_MAIN_VARIANT (type);
3512 return type;
3515 /* Return nonzero if the given type node represents a tagged type. */
3517 static inline int
3518 is_tagged_type (const_tree type)
3520 enum tree_code code = TREE_CODE (type);
3522 return (code == RECORD_TYPE || code == UNION_TYPE
3523 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3526 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3528 static void
3529 get_ref_die_offset_label (char *label, dw_die_ref ref)
3531 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3534 /* Return die_offset of a DIE reference to a base type. */
3536 static unsigned long int
3537 get_base_type_offset (dw_die_ref ref)
3539 if (ref->die_offset)
3540 return ref->die_offset;
3541 if (comp_unit_die ()->die_abbrev)
3543 calc_base_type_die_sizes ();
3544 gcc_assert (ref->die_offset);
3546 return ref->die_offset;
3549 /* Return die_offset of a DIE reference other than base type. */
3551 static unsigned long int
3552 get_ref_die_offset (dw_die_ref ref)
3554 gcc_assert (ref->die_offset);
3555 return ref->die_offset;
3558 /* Convert a DIE tag into its string name. */
3560 static const char *
3561 dwarf_tag_name (unsigned int tag)
3563 const char *name = get_DW_TAG_name (tag);
3565 if (name != NULL)
3566 return name;
3568 return "DW_TAG_<unknown>";
3571 /* Convert a DWARF attribute code into its string name. */
3573 static const char *
3574 dwarf_attr_name (unsigned int attr)
3576 const char *name;
3578 switch (attr)
3580 #if VMS_DEBUGGING_INFO
3581 case DW_AT_HP_prologue:
3582 return "DW_AT_HP_prologue";
3583 #else
3584 case DW_AT_MIPS_loop_unroll_factor:
3585 return "DW_AT_MIPS_loop_unroll_factor";
3586 #endif
3588 #if VMS_DEBUGGING_INFO
3589 case DW_AT_HP_epilogue:
3590 return "DW_AT_HP_epilogue";
3591 #else
3592 case DW_AT_MIPS_stride:
3593 return "DW_AT_MIPS_stride";
3594 #endif
3597 name = get_DW_AT_name (attr);
3599 if (name != NULL)
3600 return name;
3602 return "DW_AT_<unknown>";
3605 /* Convert a DWARF value form code into its string name. */
3607 static const char *
3608 dwarf_form_name (unsigned int form)
3610 const char *name = get_DW_FORM_name (form);
3612 if (name != NULL)
3613 return name;
3615 return "DW_FORM_<unknown>";
3618 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3619 instance of an inlined instance of a decl which is local to an inline
3620 function, so we have to trace all of the way back through the origin chain
3621 to find out what sort of node actually served as the original seed for the
3622 given block. */
3624 static tree
3625 decl_ultimate_origin (const_tree decl)
3627 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3628 return NULL_TREE;
3630 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3631 nodes in the function to point to themselves; ignore that if
3632 we're trying to output the abstract instance of this function. */
3633 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3634 return NULL_TREE;
3636 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3637 most distant ancestor, this should never happen. */
3638 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3640 return DECL_ABSTRACT_ORIGIN (decl);
3643 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3644 of a virtual function may refer to a base class, so we check the 'this'
3645 parameter. */
3647 static tree
3648 decl_class_context (tree decl)
3650 tree context = NULL_TREE;
3652 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3653 context = DECL_CONTEXT (decl);
3654 else
3655 context = TYPE_MAIN_VARIANT
3656 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3658 if (context && !TYPE_P (context))
3659 context = NULL_TREE;
3661 return context;
3664 /* Add an attribute/value pair to a DIE. */
3666 static inline void
3667 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3669 /* Maybe this should be an assert? */
3670 if (die == NULL)
3671 return;
3673 vec_safe_reserve (die->die_attr, 1);
3674 vec_safe_push (die->die_attr, *attr);
3677 static inline enum dw_val_class
3678 AT_class (dw_attr_ref a)
3680 return a->dw_attr_val.val_class;
3683 /* Return the index for any attribute that will be referenced with a
3684 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3685 are stored in dw_attr_val.v.val_str for reference counting
3686 pruning. */
3688 static inline unsigned int
3689 AT_index (dw_attr_ref a)
3691 if (AT_class (a) == dw_val_class_str)
3692 return a->dw_attr_val.v.val_str->index;
3693 else if (a->dw_attr_val.val_entry != NULL)
3694 return a->dw_attr_val.val_entry->index;
3695 return NOT_INDEXED;
3698 /* Add a flag value attribute to a DIE. */
3700 static inline void
3701 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3703 dw_attr_node attr;
3705 attr.dw_attr = attr_kind;
3706 attr.dw_attr_val.val_class = dw_val_class_flag;
3707 attr.dw_attr_val.val_entry = NULL;
3708 attr.dw_attr_val.v.val_flag = flag;
3709 add_dwarf_attr (die, &attr);
3712 static inline unsigned
3713 AT_flag (dw_attr_ref a)
3715 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3716 return a->dw_attr_val.v.val_flag;
3719 /* Add a signed integer attribute value to a DIE. */
3721 static inline void
3722 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3724 dw_attr_node attr;
3726 attr.dw_attr = attr_kind;
3727 attr.dw_attr_val.val_class = dw_val_class_const;
3728 attr.dw_attr_val.val_entry = NULL;
3729 attr.dw_attr_val.v.val_int = int_val;
3730 add_dwarf_attr (die, &attr);
3733 static inline HOST_WIDE_INT
3734 AT_int (dw_attr_ref a)
3736 gcc_assert (a && AT_class (a) == dw_val_class_const);
3737 return a->dw_attr_val.v.val_int;
3740 /* Add an unsigned integer attribute value to a DIE. */
3742 static inline void
3743 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3744 unsigned HOST_WIDE_INT unsigned_val)
3746 dw_attr_node attr;
3748 attr.dw_attr = attr_kind;
3749 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3750 attr.dw_attr_val.val_entry = NULL;
3751 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3752 add_dwarf_attr (die, &attr);
3755 static inline unsigned HOST_WIDE_INT
3756 AT_unsigned (dw_attr_ref a)
3758 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3759 return a->dw_attr_val.v.val_unsigned;
3762 /* Add an unsigned double integer attribute value to a DIE. */
3764 static inline void
3765 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3766 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3768 dw_attr_node attr;
3770 attr.dw_attr = attr_kind;
3771 attr.dw_attr_val.val_class = dw_val_class_const_double;
3772 attr.dw_attr_val.val_entry = NULL;
3773 attr.dw_attr_val.v.val_double.high = high;
3774 attr.dw_attr_val.v.val_double.low = low;
3775 add_dwarf_attr (die, &attr);
3778 /* Add a floating point attribute value to a DIE and return it. */
3780 static inline void
3781 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3782 unsigned int length, unsigned int elt_size, unsigned char *array)
3784 dw_attr_node attr;
3786 attr.dw_attr = attr_kind;
3787 attr.dw_attr_val.val_class = dw_val_class_vec;
3788 attr.dw_attr_val.val_entry = NULL;
3789 attr.dw_attr_val.v.val_vec.length = length;
3790 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3791 attr.dw_attr_val.v.val_vec.array = array;
3792 add_dwarf_attr (die, &attr);
3795 /* Add an 8-byte data attribute value to a DIE. */
3797 static inline void
3798 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3799 unsigned char data8[8])
3801 dw_attr_node attr;
3803 attr.dw_attr = attr_kind;
3804 attr.dw_attr_val.val_class = dw_val_class_data8;
3805 attr.dw_attr_val.val_entry = NULL;
3806 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3807 add_dwarf_attr (die, &attr);
3810 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3811 dwarf_split_debug_info, address attributes in dies destined for the
3812 final executable have force_direct set to avoid using indexed
3813 references. */
3815 static inline void
3816 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3817 bool force_direct)
3819 dw_attr_node attr;
3820 char * lbl_id;
3822 lbl_id = xstrdup (lbl_low);
3823 attr.dw_attr = DW_AT_low_pc;
3824 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3825 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3826 if (dwarf_split_debug_info && !force_direct)
3827 attr.dw_attr_val.val_entry
3828 = add_addr_table_entry (lbl_id, ate_kind_label);
3829 else
3830 attr.dw_attr_val.val_entry = NULL;
3831 add_dwarf_attr (die, &attr);
3833 attr.dw_attr = DW_AT_high_pc;
3834 if (dwarf_version < 4)
3835 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3836 else
3837 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3838 lbl_id = xstrdup (lbl_high);
3839 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3840 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3841 && dwarf_split_debug_info && !force_direct)
3842 attr.dw_attr_val.val_entry
3843 = add_addr_table_entry (lbl_id, ate_kind_label);
3844 else
3845 attr.dw_attr_val.val_entry = NULL;
3846 add_dwarf_attr (die, &attr);
3849 /* Hash and equality functions for debug_str_hash. */
3851 static hashval_t
3852 debug_str_do_hash (const void *x)
3854 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3857 static int
3858 debug_str_eq (const void *x1, const void *x2)
3860 return strcmp ((((const struct indirect_string_node *)x1)->str),
3861 (const char *)x2) == 0;
3864 /* Add STR to the given string hash table. */
3866 static struct indirect_string_node *
3867 find_AT_string_in_table (const char *str, htab_t table)
3869 struct indirect_string_node *node;
3870 void **slot;
3872 slot = htab_find_slot_with_hash (table, str,
3873 htab_hash_string (str), INSERT);
3874 if (*slot == NULL)
3876 node = ggc_alloc_cleared_indirect_string_node ();
3877 node->str = ggc_strdup (str);
3878 *slot = node;
3880 else
3881 node = (struct indirect_string_node *) *slot;
3883 node->refcount++;
3884 return node;
3887 /* Add STR to the indirect string hash table. */
3889 static struct indirect_string_node *
3890 find_AT_string (const char *str)
3892 if (! debug_str_hash)
3893 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3894 debug_str_eq, NULL);
3896 return find_AT_string_in_table (str, debug_str_hash);
3899 /* Add a string attribute value to a DIE. */
3901 static inline void
3902 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3904 dw_attr_node attr;
3905 struct indirect_string_node *node;
3907 node = find_AT_string (str);
3909 attr.dw_attr = attr_kind;
3910 attr.dw_attr_val.val_class = dw_val_class_str;
3911 attr.dw_attr_val.val_entry = NULL;
3912 attr.dw_attr_val.v.val_str = node;
3913 add_dwarf_attr (die, &attr);
3916 static inline const char *
3917 AT_string (dw_attr_ref a)
3919 gcc_assert (a && AT_class (a) == dw_val_class_str);
3920 return a->dw_attr_val.v.val_str->str;
3923 /* Call this function directly to bypass AT_string_form's logic to put
3924 the string inline in the die. */
3926 static void
3927 set_indirect_string (struct indirect_string_node *node)
3929 char label[32];
3930 /* Already indirect is a no op. */
3931 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3933 gcc_assert (node->label);
3934 return;
3936 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3937 ++dw2_string_counter;
3938 node->label = xstrdup (label);
3940 if (!dwarf_split_debug_info)
3942 node->form = DW_FORM_strp;
3943 node->index = NOT_INDEXED;
3945 else
3947 node->form = DW_FORM_GNU_str_index;
3948 node->index = NO_INDEX_ASSIGNED;
3952 /* Find out whether a string should be output inline in DIE
3953 or out-of-line in .debug_str section. */
3955 static enum dwarf_form
3956 find_string_form (struct indirect_string_node *node)
3958 unsigned int len;
3960 if (node->form)
3961 return node->form;
3963 len = strlen (node->str) + 1;
3965 /* If the string is shorter or equal to the size of the reference, it is
3966 always better to put it inline. */
3967 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3968 return node->form = DW_FORM_string;
3970 /* If we cannot expect the linker to merge strings in .debug_str
3971 section, only put it into .debug_str if it is worth even in this
3972 single module. */
3973 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3974 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3975 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3976 return node->form = DW_FORM_string;
3978 set_indirect_string (node);
3980 return node->form;
3983 /* Find out whether the string referenced from the attribute should be
3984 output inline in DIE or out-of-line in .debug_str section. */
3986 static enum dwarf_form
3987 AT_string_form (dw_attr_ref a)
3989 gcc_assert (a && AT_class (a) == dw_val_class_str);
3990 return find_string_form (a->dw_attr_val.v.val_str);
3993 /* Add a DIE reference attribute value to a DIE. */
3995 static inline void
3996 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3998 dw_attr_node attr;
4000 #ifdef ENABLE_CHECKING
4001 gcc_assert (targ_die != NULL);
4002 #else
4003 /* With LTO we can end up trying to reference something we didn't create
4004 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4005 if (targ_die == NULL)
4006 return;
4007 #endif
4009 attr.dw_attr = attr_kind;
4010 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4011 attr.dw_attr_val.val_entry = NULL;
4012 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4013 attr.dw_attr_val.v.val_die_ref.external = 0;
4014 add_dwarf_attr (die, &attr);
4017 /* Change DIE reference REF to point to NEW_DIE instead. */
4019 static inline void
4020 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4022 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4023 ref->dw_attr_val.v.val_die_ref.die = new_die;
4024 ref->dw_attr_val.v.val_die_ref.external = 0;
4027 /* Add an AT_specification attribute to a DIE, and also make the back
4028 pointer from the specification to the definition. */
4030 static inline void
4031 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4033 add_AT_die_ref (die, DW_AT_specification, targ_die);
4034 gcc_assert (!targ_die->die_definition);
4035 targ_die->die_definition = die;
4038 static inline dw_die_ref
4039 AT_ref (dw_attr_ref a)
4041 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4042 return a->dw_attr_val.v.val_die_ref.die;
4045 static inline int
4046 AT_ref_external (dw_attr_ref a)
4048 if (a && AT_class (a) == dw_val_class_die_ref)
4049 return a->dw_attr_val.v.val_die_ref.external;
4051 return 0;
4054 static inline void
4055 set_AT_ref_external (dw_attr_ref a, int i)
4057 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4058 a->dw_attr_val.v.val_die_ref.external = i;
4061 /* Add an FDE reference attribute value to a DIE. */
4063 static inline void
4064 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4066 dw_attr_node attr;
4068 attr.dw_attr = attr_kind;
4069 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4070 attr.dw_attr_val.val_entry = NULL;
4071 attr.dw_attr_val.v.val_fde_index = targ_fde;
4072 add_dwarf_attr (die, &attr);
4075 /* Add a location description attribute value to a DIE. */
4077 static inline void
4078 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4080 dw_attr_node attr;
4082 attr.dw_attr = attr_kind;
4083 attr.dw_attr_val.val_class = dw_val_class_loc;
4084 attr.dw_attr_val.val_entry = NULL;
4085 attr.dw_attr_val.v.val_loc = loc;
4086 add_dwarf_attr (die, &attr);
4089 static inline dw_loc_descr_ref
4090 AT_loc (dw_attr_ref a)
4092 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4093 return a->dw_attr_val.v.val_loc;
4096 static inline void
4097 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4099 dw_attr_node attr;
4101 attr.dw_attr = attr_kind;
4102 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4103 attr.dw_attr_val.val_entry = NULL;
4104 attr.dw_attr_val.v.val_loc_list = loc_list;
4105 add_dwarf_attr (die, &attr);
4106 have_location_lists = true;
4109 static inline dw_loc_list_ref
4110 AT_loc_list (dw_attr_ref a)
4112 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4113 return a->dw_attr_val.v.val_loc_list;
4116 static inline dw_loc_list_ref *
4117 AT_loc_list_ptr (dw_attr_ref a)
4119 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4120 return &a->dw_attr_val.v.val_loc_list;
4123 /* Table of entries into the .debug_addr section. */
4125 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4127 /* Hash an address_table_entry. */
4129 static hashval_t
4130 addr_table_entry_do_hash (const void *x)
4132 const addr_table_entry *a = (const addr_table_entry *) x;
4133 switch (a->kind)
4135 case ate_kind_rtx:
4136 return iterative_hash_rtx (a->addr.rtl, 0);
4137 case ate_kind_rtx_dtprel:
4138 return iterative_hash_rtx (a->addr.rtl, 1);
4139 case ate_kind_label:
4140 return htab_hash_string (a->addr.label);
4141 default:
4142 gcc_unreachable ();
4146 /* Determine equality for two address_table_entries. */
4148 static int
4149 addr_table_entry_eq (const void *x1, const void *x2)
4151 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4152 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4154 if (a1->kind != a2->kind)
4155 return 0;
4156 switch (a1->kind)
4158 case ate_kind_rtx:
4159 case ate_kind_rtx_dtprel:
4160 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4161 case ate_kind_label:
4162 return strcmp (a1->addr.label, a2->addr.label) == 0;
4163 default:
4164 gcc_unreachable ();
4168 /* Initialize an addr_table_entry. */
4170 void
4171 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4173 e->kind = kind;
4174 switch (kind)
4176 case ate_kind_rtx:
4177 case ate_kind_rtx_dtprel:
4178 e->addr.rtl = (rtx) addr;
4179 break;
4180 case ate_kind_label:
4181 e->addr.label = (char *) addr;
4182 break;
4184 e->refcount = 0;
4185 e->index = NO_INDEX_ASSIGNED;
4188 /* Add attr to the address table entry to the table. Defer setting an
4189 index until output time. */
4191 static addr_table_entry *
4192 add_addr_table_entry (void *addr, enum ate_kind kind)
4194 addr_table_entry *node;
4195 addr_table_entry finder;
4196 void **slot;
4198 gcc_assert (dwarf_split_debug_info);
4199 if (! addr_index_table)
4200 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4201 addr_table_entry_eq, NULL);
4202 init_addr_table_entry (&finder, kind, addr);
4203 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4205 if (*slot == HTAB_EMPTY_ENTRY)
4207 node = ggc_alloc_cleared_addr_table_entry ();
4208 init_addr_table_entry (node, kind, addr);
4209 *slot = node;
4211 else
4212 node = (addr_table_entry *) *slot;
4214 node->refcount++;
4215 return node;
4218 /* Remove an entry from the addr table by decrementing its refcount.
4219 Strictly, decrementing the refcount would be enough, but the
4220 assertion that the entry is actually in the table has found
4221 bugs. */
4223 static void
4224 remove_addr_table_entry (addr_table_entry *entry)
4226 addr_table_entry *node;
4228 gcc_assert (dwarf_split_debug_info && addr_index_table);
4229 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4230 /* After an index is assigned, the table is frozen. */
4231 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4232 node->refcount--;
4235 /* Given a location list, remove all addresses it refers to from the
4236 address_table. */
4238 static void
4239 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4241 for (; descr; descr = descr->dw_loc_next)
4242 if (descr->dw_loc_oprnd1.val_entry != NULL)
4244 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4245 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4249 /* A helper function for dwarf2out_finish called through
4250 htab_traverse. Assign an addr_table_entry its index. All entries
4251 must be collected into the table when this function is called,
4252 because the indexing code relies on htab_traverse to traverse nodes
4253 in the same order for each run. */
4255 static int
4256 index_addr_table_entry (void **h, void *v)
4258 addr_table_entry *node = (addr_table_entry *) *h;
4259 unsigned int *index = (unsigned int *) v;
4261 /* Don't index unreferenced nodes. */
4262 if (node->refcount == 0)
4263 return 1;
4265 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4266 node->index = *index;
4267 *index += 1;
4269 return 1;
4272 /* Add an address constant attribute value to a DIE. When using
4273 dwarf_split_debug_info, address attributes in dies destined for the
4274 final executable should be direct references--setting the parameter
4275 force_direct ensures this behavior. */
4277 static inline void
4278 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4279 bool force_direct)
4281 dw_attr_node attr;
4283 attr.dw_attr = attr_kind;
4284 attr.dw_attr_val.val_class = dw_val_class_addr;
4285 attr.dw_attr_val.v.val_addr = addr;
4286 if (dwarf_split_debug_info && !force_direct)
4287 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4288 else
4289 attr.dw_attr_val.val_entry = NULL;
4290 add_dwarf_attr (die, &attr);
4293 /* Get the RTX from to an address DIE attribute. */
4295 static inline rtx
4296 AT_addr (dw_attr_ref a)
4298 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4299 return a->dw_attr_val.v.val_addr;
4302 /* Add a file attribute value to a DIE. */
4304 static inline void
4305 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4306 struct dwarf_file_data *fd)
4308 dw_attr_node attr;
4310 attr.dw_attr = attr_kind;
4311 attr.dw_attr_val.val_class = dw_val_class_file;
4312 attr.dw_attr_val.val_entry = NULL;
4313 attr.dw_attr_val.v.val_file = fd;
4314 add_dwarf_attr (die, &attr);
4317 /* Get the dwarf_file_data from a file DIE attribute. */
4319 static inline struct dwarf_file_data *
4320 AT_file (dw_attr_ref a)
4322 gcc_assert (a && AT_class (a) == dw_val_class_file);
4323 return a->dw_attr_val.v.val_file;
4326 /* Add a vms delta attribute value to a DIE. */
4328 static inline void
4329 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4330 const char *lbl1, const char *lbl2)
4332 dw_attr_node attr;
4334 attr.dw_attr = attr_kind;
4335 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4336 attr.dw_attr_val.val_entry = NULL;
4337 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4338 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4339 add_dwarf_attr (die, &attr);
4342 /* Add a label identifier attribute value to a DIE. */
4344 static inline void
4345 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4346 const char *lbl_id)
4348 dw_attr_node attr;
4350 attr.dw_attr = attr_kind;
4351 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4352 attr.dw_attr_val.val_entry = NULL;
4353 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4354 if (dwarf_split_debug_info)
4355 attr.dw_attr_val.val_entry
4356 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4357 ate_kind_label);
4358 add_dwarf_attr (die, &attr);
4361 /* Add a section offset attribute value to a DIE, an offset into the
4362 debug_line section. */
4364 static inline void
4365 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4366 const char *label)
4368 dw_attr_node attr;
4370 attr.dw_attr = attr_kind;
4371 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4372 attr.dw_attr_val.val_entry = NULL;
4373 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4374 add_dwarf_attr (die, &attr);
4377 /* Add a section offset attribute value to a DIE, an offset into the
4378 debug_macinfo section. */
4380 static inline void
4381 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4382 const char *label)
4384 dw_attr_node attr;
4386 attr.dw_attr = attr_kind;
4387 attr.dw_attr_val.val_class = dw_val_class_macptr;
4388 attr.dw_attr_val.val_entry = NULL;
4389 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4390 add_dwarf_attr (die, &attr);
4393 /* Add an offset attribute value to a DIE. */
4395 static inline void
4396 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4397 unsigned HOST_WIDE_INT offset)
4399 dw_attr_node attr;
4401 attr.dw_attr = attr_kind;
4402 attr.dw_attr_val.val_class = dw_val_class_offset;
4403 attr.dw_attr_val.val_entry = NULL;
4404 attr.dw_attr_val.v.val_offset = offset;
4405 add_dwarf_attr (die, &attr);
4408 /* Add a range_list attribute value to a DIE. When using
4409 dwarf_split_debug_info, address attributes in dies destined for the
4410 final executable should be direct references--setting the parameter
4411 force_direct ensures this behavior. */
4413 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4414 #define RELOCATED_OFFSET (NULL)
4416 static void
4417 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4418 long unsigned int offset, bool force_direct)
4420 dw_attr_node attr;
4422 attr.dw_attr = attr_kind;
4423 attr.dw_attr_val.val_class = dw_val_class_range_list;
4424 /* For the range_list attribute, use val_entry to store whether the
4425 offset should follow split-debug-info or normal semantics. This
4426 value is read in output_range_list_offset. */
4427 if (dwarf_split_debug_info && !force_direct)
4428 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4429 else
4430 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4431 attr.dw_attr_val.v.val_offset = offset;
4432 add_dwarf_attr (die, &attr);
4435 /* Return the start label of a delta attribute. */
4437 static inline const char *
4438 AT_vms_delta1 (dw_attr_ref a)
4440 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4441 return a->dw_attr_val.v.val_vms_delta.lbl1;
4444 /* Return the end label of a delta attribute. */
4446 static inline const char *
4447 AT_vms_delta2 (dw_attr_ref a)
4449 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4450 return a->dw_attr_val.v.val_vms_delta.lbl2;
4453 static inline const char *
4454 AT_lbl (dw_attr_ref a)
4456 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4457 || AT_class (a) == dw_val_class_lineptr
4458 || AT_class (a) == dw_val_class_macptr
4459 || AT_class (a) == dw_val_class_high_pc));
4460 return a->dw_attr_val.v.val_lbl_id;
4463 /* Get the attribute of type attr_kind. */
4465 static dw_attr_ref
4466 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4468 dw_attr_ref a;
4469 unsigned ix;
4470 dw_die_ref spec = NULL;
4472 if (! die)
4473 return NULL;
4475 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4476 if (a->dw_attr == attr_kind)
4477 return a;
4478 else if (a->dw_attr == DW_AT_specification
4479 || a->dw_attr == DW_AT_abstract_origin)
4480 spec = AT_ref (a);
4482 if (spec)
4483 return get_AT (spec, attr_kind);
4485 return NULL;
4488 /* Returns the parent of the declaration of DIE. */
4490 static dw_die_ref
4491 get_die_parent (dw_die_ref die)
4493 dw_die_ref t;
4495 if (!die)
4496 return NULL;
4498 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4499 || (t = get_AT_ref (die, DW_AT_specification)))
4500 die = t;
4502 return die->die_parent;
4505 /* Return the "low pc" attribute value, typically associated with a subprogram
4506 DIE. Return null if the "low pc" attribute is either not present, or if it
4507 cannot be represented as an assembler label identifier. */
4509 static inline const char *
4510 get_AT_low_pc (dw_die_ref die)
4512 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4514 return a ? AT_lbl (a) : NULL;
4517 /* Return the "high pc" attribute value, typically associated with a subprogram
4518 DIE. Return null if the "high pc" attribute is either not present, or if it
4519 cannot be represented as an assembler label identifier. */
4521 static inline const char *
4522 get_AT_hi_pc (dw_die_ref die)
4524 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4526 return a ? AT_lbl (a) : NULL;
4529 /* Return the value of the string attribute designated by ATTR_KIND, or
4530 NULL if it is not present. */
4532 static inline const char *
4533 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4535 dw_attr_ref a = get_AT (die, attr_kind);
4537 return a ? AT_string (a) : NULL;
4540 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4541 if it is not present. */
4543 static inline int
4544 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4546 dw_attr_ref a = get_AT (die, attr_kind);
4548 return a ? AT_flag (a) : 0;
4551 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4552 if it is not present. */
4554 static inline unsigned
4555 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4557 dw_attr_ref a = get_AT (die, attr_kind);
4559 return a ? AT_unsigned (a) : 0;
4562 static inline dw_die_ref
4563 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4565 dw_attr_ref a = get_AT (die, attr_kind);
4567 return a ? AT_ref (a) : NULL;
4570 static inline struct dwarf_file_data *
4571 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4573 dw_attr_ref a = get_AT (die, attr_kind);
4575 return a ? AT_file (a) : NULL;
4578 /* Return TRUE if the language is C++. */
4580 static inline bool
4581 is_cxx (void)
4583 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4585 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4588 /* Return TRUE if the language is Java. */
4590 static inline bool
4591 is_java (void)
4593 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4595 return lang == DW_LANG_Java;
4598 /* Return TRUE if the language is Fortran. */
4600 static inline bool
4601 is_fortran (void)
4603 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4605 return (lang == DW_LANG_Fortran77
4606 || lang == DW_LANG_Fortran90
4607 || lang == DW_LANG_Fortran95);
4610 /* Return TRUE if the language is Ada. */
4612 static inline bool
4613 is_ada (void)
4615 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4617 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4620 /* Remove the specified attribute if present. */
4622 static void
4623 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4625 dw_attr_ref a;
4626 unsigned ix;
4628 if (! die)
4629 return;
4631 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4632 if (a->dw_attr == attr_kind)
4634 if (AT_class (a) == dw_val_class_str)
4635 if (a->dw_attr_val.v.val_str->refcount)
4636 a->dw_attr_val.v.val_str->refcount--;
4638 /* vec::ordered_remove should help reduce the number of abbrevs
4639 that are needed. */
4640 die->die_attr->ordered_remove (ix);
4641 return;
4645 /* Remove CHILD from its parent. PREV must have the property that
4646 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4648 static void
4649 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4651 gcc_assert (child->die_parent == prev->die_parent);
4652 gcc_assert (prev->die_sib == child);
4653 if (prev == child)
4655 gcc_assert (child->die_parent->die_child == child);
4656 prev = NULL;
4658 else
4659 prev->die_sib = child->die_sib;
4660 if (child->die_parent->die_child == child)
4661 child->die_parent->die_child = prev;
4664 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4665 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4667 static void
4668 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4670 dw_die_ref parent = old_child->die_parent;
4672 gcc_assert (parent == prev->die_parent);
4673 gcc_assert (prev->die_sib == old_child);
4675 new_child->die_parent = parent;
4676 if (prev == old_child)
4678 gcc_assert (parent->die_child == old_child);
4679 new_child->die_sib = new_child;
4681 else
4683 prev->die_sib = new_child;
4684 new_child->die_sib = old_child->die_sib;
4686 if (old_child->die_parent->die_child == old_child)
4687 old_child->die_parent->die_child = new_child;
4690 /* Move all children from OLD_PARENT to NEW_PARENT. */
4692 static void
4693 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4695 dw_die_ref c;
4696 new_parent->die_child = old_parent->die_child;
4697 old_parent->die_child = NULL;
4698 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4701 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4702 matches TAG. */
4704 static void
4705 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4707 dw_die_ref c;
4709 c = die->die_child;
4710 if (c) do {
4711 dw_die_ref prev = c;
4712 c = c->die_sib;
4713 while (c->die_tag == tag)
4715 remove_child_with_prev (c, prev);
4716 /* Might have removed every child. */
4717 if (c == c->die_sib)
4718 return;
4719 c = c->die_sib;
4721 } while (c != die->die_child);
4724 /* Add a CHILD_DIE as the last child of DIE. */
4726 static void
4727 add_child_die (dw_die_ref die, dw_die_ref child_die)
4729 /* FIXME this should probably be an assert. */
4730 if (! die || ! child_die)
4731 return;
4732 gcc_assert (die != child_die);
4734 child_die->die_parent = die;
4735 if (die->die_child)
4737 child_die->die_sib = die->die_child->die_sib;
4738 die->die_child->die_sib = child_die;
4740 else
4741 child_die->die_sib = child_die;
4742 die->die_child = child_die;
4745 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4746 is the specification, to the end of PARENT's list of children.
4747 This is done by removing and re-adding it. */
4749 static void
4750 splice_child_die (dw_die_ref parent, dw_die_ref child)
4752 dw_die_ref p;
4754 /* We want the declaration DIE from inside the class, not the
4755 specification DIE at toplevel. */
4756 if (child->die_parent != parent)
4758 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4760 if (tmp)
4761 child = tmp;
4764 gcc_assert (child->die_parent == parent
4765 || (child->die_parent
4766 == get_AT_ref (parent, DW_AT_specification)));
4768 for (p = child->die_parent->die_child; ; p = p->die_sib)
4769 if (p->die_sib == child)
4771 remove_child_with_prev (child, p);
4772 break;
4775 add_child_die (parent, child);
4778 /* Return a pointer to a newly created DIE node. */
4780 static inline dw_die_ref
4781 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4783 dw_die_ref die = ggc_alloc_cleared_die_node ();
4785 die->die_tag = tag_value;
4787 if (parent_die != NULL)
4788 add_child_die (parent_die, die);
4789 else
4791 limbo_die_node *limbo_node;
4793 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4794 limbo_node->die = die;
4795 limbo_node->created_for = t;
4796 limbo_node->next = limbo_die_list;
4797 limbo_die_list = limbo_node;
4800 return die;
4803 /* Return the DIE associated with the given type specifier. */
4805 static inline dw_die_ref
4806 lookup_type_die (tree type)
4808 return TYPE_SYMTAB_DIE (type);
4811 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4812 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4813 anonymous type instead the one of the naming typedef. */
4815 static inline dw_die_ref
4816 strip_naming_typedef (tree type, dw_die_ref type_die)
4818 if (type
4819 && TREE_CODE (type) == RECORD_TYPE
4820 && type_die
4821 && type_die->die_tag == DW_TAG_typedef
4822 && is_naming_typedef_decl (TYPE_NAME (type)))
4823 type_die = get_AT_ref (type_die, DW_AT_type);
4824 return type_die;
4827 /* Like lookup_type_die, but if type is an anonymous type named by a
4828 typedef[1], return the DIE of the anonymous type instead the one of
4829 the naming typedef. This is because in gen_typedef_die, we did
4830 equate the anonymous struct named by the typedef with the DIE of
4831 the naming typedef. So by default, lookup_type_die on an anonymous
4832 struct yields the DIE of the naming typedef.
4834 [1]: Read the comment of is_naming_typedef_decl to learn about what
4835 a naming typedef is. */
4837 static inline dw_die_ref
4838 lookup_type_die_strip_naming_typedef (tree type)
4840 dw_die_ref die = lookup_type_die (type);
4841 return strip_naming_typedef (type, die);
4844 /* Equate a DIE to a given type specifier. */
4846 static inline void
4847 equate_type_number_to_die (tree type, dw_die_ref type_die)
4849 TYPE_SYMTAB_DIE (type) = type_die;
4852 /* Returns a hash value for X (which really is a die_struct). */
4854 static hashval_t
4855 decl_die_table_hash (const void *x)
4857 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4860 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4862 static int
4863 decl_die_table_eq (const void *x, const void *y)
4865 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4868 /* Return the DIE associated with a given declaration. */
4870 static inline dw_die_ref
4871 lookup_decl_die (tree decl)
4873 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4876 /* Returns a hash value for X (which really is a var_loc_list). */
4878 static hashval_t
4879 decl_loc_table_hash (const void *x)
4881 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4884 /* Return nonzero if decl_id of var_loc_list X is the same as
4885 UID of decl *Y. */
4887 static int
4888 decl_loc_table_eq (const void *x, const void *y)
4890 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4893 /* Return the var_loc list associated with a given declaration. */
4895 static inline var_loc_list *
4896 lookup_decl_loc (const_tree decl)
4898 if (!decl_loc_table)
4899 return NULL;
4900 return (var_loc_list *)
4901 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4904 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4906 static hashval_t
4907 cached_dw_loc_list_table_hash (const void *x)
4909 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4912 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4913 UID of decl *Y. */
4915 static int
4916 cached_dw_loc_list_table_eq (const void *x, const void *y)
4918 return (((const cached_dw_loc_list *) x)->decl_id
4919 == DECL_UID ((const_tree) y));
4922 /* Equate a DIE to a particular declaration. */
4924 static void
4925 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4927 unsigned int decl_id = DECL_UID (decl);
4928 void **slot;
4930 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4931 *slot = decl_die;
4932 decl_die->decl_id = decl_id;
4935 /* Return how many bits covers PIECE EXPR_LIST. */
4937 static int
4938 decl_piece_bitsize (rtx piece)
4940 int ret = (int) GET_MODE (piece);
4941 if (ret)
4942 return ret;
4943 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4944 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4945 return INTVAL (XEXP (XEXP (piece, 0), 0));
4948 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4950 static rtx *
4951 decl_piece_varloc_ptr (rtx piece)
4953 if ((int) GET_MODE (piece))
4954 return &XEXP (piece, 0);
4955 else
4956 return &XEXP (XEXP (piece, 0), 1);
4959 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4960 Next is the chain of following piece nodes. */
4962 static rtx
4963 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4965 if (bitsize <= (int) MAX_MACHINE_MODE)
4966 return alloc_EXPR_LIST (bitsize, loc_note, next);
4967 else
4968 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4969 GEN_INT (bitsize),
4970 loc_note), next);
4973 /* Return rtx that should be stored into loc field for
4974 LOC_NOTE and BITPOS/BITSIZE. */
4976 static rtx
4977 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4978 HOST_WIDE_INT bitsize)
4980 if (bitsize != -1)
4982 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4983 if (bitpos != 0)
4984 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4986 return loc_note;
4989 /* This function either modifies location piece list *DEST in
4990 place (if SRC and INNER is NULL), or copies location piece list
4991 *SRC to *DEST while modifying it. Location BITPOS is modified
4992 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4993 not copied and if needed some padding around it is added.
4994 When modifying in place, DEST should point to EXPR_LIST where
4995 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4996 to the start of the whole list and INNER points to the EXPR_LIST
4997 where earlier pieces cover PIECE_BITPOS bits. */
4999 static void
5000 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5001 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5002 HOST_WIDE_INT bitsize, rtx loc_note)
5004 int diff;
5005 bool copy = inner != NULL;
5007 if (copy)
5009 /* First copy all nodes preceding the current bitpos. */
5010 while (src != inner)
5012 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5013 decl_piece_bitsize (*src), NULL_RTX);
5014 dest = &XEXP (*dest, 1);
5015 src = &XEXP (*src, 1);
5018 /* Add padding if needed. */
5019 if (bitpos != piece_bitpos)
5021 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5022 copy ? NULL_RTX : *dest);
5023 dest = &XEXP (*dest, 1);
5025 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5027 gcc_assert (!copy);
5028 /* A piece with correct bitpos and bitsize already exist,
5029 just update the location for it and return. */
5030 *decl_piece_varloc_ptr (*dest) = loc_note;
5031 return;
5033 /* Add the piece that changed. */
5034 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5035 dest = &XEXP (*dest, 1);
5036 /* Skip over pieces that overlap it. */
5037 diff = bitpos - piece_bitpos + bitsize;
5038 if (!copy)
5039 src = dest;
5040 while (diff > 0 && *src)
5042 rtx piece = *src;
5043 diff -= decl_piece_bitsize (piece);
5044 if (copy)
5045 src = &XEXP (piece, 1);
5046 else
5048 *src = XEXP (piece, 1);
5049 free_EXPR_LIST_node (piece);
5052 /* Add padding if needed. */
5053 if (diff < 0 && *src)
5055 if (!copy)
5056 dest = src;
5057 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5058 dest = &XEXP (*dest, 1);
5060 if (!copy)
5061 return;
5062 /* Finally copy all nodes following it. */
5063 while (*src)
5065 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5066 decl_piece_bitsize (*src), NULL_RTX);
5067 dest = &XEXP (*dest, 1);
5068 src = &XEXP (*src, 1);
5072 /* Add a variable location node to the linked list for DECL. */
5074 static struct var_loc_node *
5075 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5077 unsigned int decl_id;
5078 var_loc_list *temp;
5079 void **slot;
5080 struct var_loc_node *loc = NULL;
5081 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5083 if (TREE_CODE (decl) == VAR_DECL
5084 && DECL_HAS_DEBUG_EXPR_P (decl))
5086 tree realdecl = DECL_DEBUG_EXPR (decl);
5087 if (handled_component_p (realdecl)
5088 || (TREE_CODE (realdecl) == MEM_REF
5089 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5091 HOST_WIDE_INT maxsize;
5092 tree innerdecl;
5093 innerdecl
5094 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5095 if (!DECL_P (innerdecl)
5096 || DECL_IGNORED_P (innerdecl)
5097 || TREE_STATIC (innerdecl)
5098 || bitsize <= 0
5099 || bitpos + bitsize > 256
5100 || bitsize != maxsize)
5101 return NULL;
5102 decl = innerdecl;
5106 decl_id = DECL_UID (decl);
5107 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5108 if (*slot == NULL)
5110 temp = ggc_alloc_cleared_var_loc_list ();
5111 temp->decl_id = decl_id;
5112 *slot = temp;
5114 else
5115 temp = (var_loc_list *) *slot;
5117 /* For PARM_DECLs try to keep around the original incoming value,
5118 even if that means we'll emit a zero-range .debug_loc entry. */
5119 if (temp->last
5120 && temp->first == temp->last
5121 && TREE_CODE (decl) == PARM_DECL
5122 && NOTE_P (temp->first->loc)
5123 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5124 && DECL_INCOMING_RTL (decl)
5125 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5126 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5127 == GET_CODE (DECL_INCOMING_RTL (decl))
5128 && prev_real_insn (temp->first->loc) == NULL_RTX
5129 && (bitsize != -1
5130 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5131 NOTE_VAR_LOCATION_LOC (loc_note))
5132 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5133 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5135 loc = ggc_alloc_cleared_var_loc_node ();
5136 temp->first->next = loc;
5137 temp->last = loc;
5138 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5140 else if (temp->last)
5142 struct var_loc_node *last = temp->last, *unused = NULL;
5143 rtx *piece_loc = NULL, last_loc_note;
5144 int piece_bitpos = 0;
5145 if (last->next)
5147 last = last->next;
5148 gcc_assert (last->next == NULL);
5150 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5152 piece_loc = &last->loc;
5155 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5156 if (piece_bitpos + cur_bitsize > bitpos)
5157 break;
5158 piece_bitpos += cur_bitsize;
5159 piece_loc = &XEXP (*piece_loc, 1);
5161 while (*piece_loc);
5163 /* TEMP->LAST here is either pointer to the last but one or
5164 last element in the chained list, LAST is pointer to the
5165 last element. */
5166 if (label && strcmp (last->label, label) == 0)
5168 /* For SRA optimized variables if there weren't any real
5169 insns since last note, just modify the last node. */
5170 if (piece_loc != NULL)
5172 adjust_piece_list (piece_loc, NULL, NULL,
5173 bitpos, piece_bitpos, bitsize, loc_note);
5174 return NULL;
5176 /* If the last note doesn't cover any instructions, remove it. */
5177 if (temp->last != last)
5179 temp->last->next = NULL;
5180 unused = last;
5181 last = temp->last;
5182 gcc_assert (strcmp (last->label, label) != 0);
5184 else
5186 gcc_assert (temp->first == temp->last
5187 || (temp->first->next == temp->last
5188 && TREE_CODE (decl) == PARM_DECL));
5189 memset (temp->last, '\0', sizeof (*temp->last));
5190 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5191 return temp->last;
5194 if (bitsize == -1 && NOTE_P (last->loc))
5195 last_loc_note = last->loc;
5196 else if (piece_loc != NULL
5197 && *piece_loc != NULL_RTX
5198 && piece_bitpos == bitpos
5199 && decl_piece_bitsize (*piece_loc) == bitsize)
5200 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5201 else
5202 last_loc_note = NULL_RTX;
5203 /* If the current location is the same as the end of the list,
5204 and either both or neither of the locations is uninitialized,
5205 we have nothing to do. */
5206 if (last_loc_note == NULL_RTX
5207 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5208 NOTE_VAR_LOCATION_LOC (loc_note)))
5209 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5210 != NOTE_VAR_LOCATION_STATUS (loc_note))
5211 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5212 == VAR_INIT_STATUS_UNINITIALIZED)
5213 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5214 == VAR_INIT_STATUS_UNINITIALIZED))))
5216 /* Add LOC to the end of list and update LAST. If the last
5217 element of the list has been removed above, reuse its
5218 memory for the new node, otherwise allocate a new one. */
5219 if (unused)
5221 loc = unused;
5222 memset (loc, '\0', sizeof (*loc));
5224 else
5225 loc = ggc_alloc_cleared_var_loc_node ();
5226 if (bitsize == -1 || piece_loc == NULL)
5227 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5228 else
5229 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5230 bitpos, piece_bitpos, bitsize, loc_note);
5231 last->next = loc;
5232 /* Ensure TEMP->LAST will point either to the new last but one
5233 element of the chain, or to the last element in it. */
5234 if (last != temp->last)
5235 temp->last = last;
5237 else if (unused)
5238 ggc_free (unused);
5240 else
5242 loc = ggc_alloc_cleared_var_loc_node ();
5243 temp->first = loc;
5244 temp->last = loc;
5245 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5247 return loc;
5250 /* Keep track of the number of spaces used to indent the
5251 output of the debugging routines that print the structure of
5252 the DIE internal representation. */
5253 static int print_indent;
5255 /* Indent the line the number of spaces given by print_indent. */
5257 static inline void
5258 print_spaces (FILE *outfile)
5260 fprintf (outfile, "%*s", print_indent, "");
5263 /* Print a type signature in hex. */
5265 static inline void
5266 print_signature (FILE *outfile, char *sig)
5268 int i;
5270 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5271 fprintf (outfile, "%02x", sig[i] & 0xff);
5274 /* Print the information associated with a given DIE, and its children.
5275 This routine is a debugging aid only. */
5277 static void
5278 print_die (dw_die_ref die, FILE *outfile)
5280 dw_attr_ref a;
5281 dw_die_ref c;
5282 unsigned ix;
5284 print_spaces (outfile);
5285 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5286 die->die_offset, dwarf_tag_name (die->die_tag),
5287 (void*) die);
5288 print_spaces (outfile);
5289 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5290 fprintf (outfile, " offset: %ld", die->die_offset);
5291 fprintf (outfile, " mark: %d\n", die->die_mark);
5293 if (die->comdat_type_p)
5295 print_spaces (outfile);
5296 fprintf (outfile, " signature: ");
5297 print_signature (outfile, die->die_id.die_type_node->signature);
5298 fprintf (outfile, "\n");
5301 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5303 print_spaces (outfile);
5304 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5306 switch (AT_class (a))
5308 case dw_val_class_addr:
5309 fprintf (outfile, "address");
5310 break;
5311 case dw_val_class_offset:
5312 fprintf (outfile, "offset");
5313 break;
5314 case dw_val_class_loc:
5315 fprintf (outfile, "location descriptor");
5316 break;
5317 case dw_val_class_loc_list:
5318 fprintf (outfile, "location list -> label:%s",
5319 AT_loc_list (a)->ll_symbol);
5320 break;
5321 case dw_val_class_range_list:
5322 fprintf (outfile, "range list");
5323 break;
5324 case dw_val_class_const:
5325 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5326 break;
5327 case dw_val_class_unsigned_const:
5328 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5329 break;
5330 case dw_val_class_const_double:
5331 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5332 HOST_WIDE_INT_PRINT_UNSIGNED")",
5333 a->dw_attr_val.v.val_double.high,
5334 a->dw_attr_val.v.val_double.low);
5335 break;
5336 case dw_val_class_vec:
5337 fprintf (outfile, "floating-point or vector constant");
5338 break;
5339 case dw_val_class_flag:
5340 fprintf (outfile, "%u", AT_flag (a));
5341 break;
5342 case dw_val_class_die_ref:
5343 if (AT_ref (a) != NULL)
5345 if (AT_ref (a)->comdat_type_p)
5347 fprintf (outfile, "die -> signature: ");
5348 print_signature (outfile,
5349 AT_ref (a)->die_id.die_type_node->signature);
5351 else if (AT_ref (a)->die_id.die_symbol)
5352 fprintf (outfile, "die -> label: %s",
5353 AT_ref (a)->die_id.die_symbol);
5354 else
5355 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5356 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5358 else
5359 fprintf (outfile, "die -> <null>");
5360 break;
5361 case dw_val_class_vms_delta:
5362 fprintf (outfile, "delta: @slotcount(%s-%s)",
5363 AT_vms_delta2 (a), AT_vms_delta1 (a));
5364 break;
5365 case dw_val_class_lbl_id:
5366 case dw_val_class_lineptr:
5367 case dw_val_class_macptr:
5368 case dw_val_class_high_pc:
5369 fprintf (outfile, "label: %s", AT_lbl (a));
5370 break;
5371 case dw_val_class_str:
5372 if (AT_string (a) != NULL)
5373 fprintf (outfile, "\"%s\"", AT_string (a));
5374 else
5375 fprintf (outfile, "<null>");
5376 break;
5377 case dw_val_class_file:
5378 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5379 AT_file (a)->emitted_number);
5380 break;
5381 case dw_val_class_data8:
5383 int i;
5385 for (i = 0; i < 8; i++)
5386 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5387 break;
5389 default:
5390 break;
5393 fprintf (outfile, "\n");
5396 if (die->die_child != NULL)
5398 print_indent += 4;
5399 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5400 print_indent -= 4;
5402 if (print_indent == 0)
5403 fprintf (outfile, "\n");
5406 /* Print the information collected for a given DIE. */
5408 DEBUG_FUNCTION void
5409 debug_dwarf_die (dw_die_ref die)
5411 print_die (die, stderr);
5414 DEBUG_FUNCTION void
5415 debug (die_struct &ref)
5417 print_die (&ref, stderr);
5420 DEBUG_FUNCTION void
5421 debug (die_struct *ptr)
5423 if (ptr)
5424 debug (*ptr);
5425 else
5426 fprintf (stderr, "<nil>\n");
5430 /* Print all DWARF information collected for the compilation unit.
5431 This routine is a debugging aid only. */
5433 DEBUG_FUNCTION void
5434 debug_dwarf (void)
5436 print_indent = 0;
5437 print_die (comp_unit_die (), stderr);
5440 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5441 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5442 DIE that marks the start of the DIEs for this include file. */
5444 static dw_die_ref
5445 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5447 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5448 dw_die_ref new_unit = gen_compile_unit_die (filename);
5450 new_unit->die_sib = old_unit;
5451 return new_unit;
5454 /* Close an include-file CU and reopen the enclosing one. */
5456 static dw_die_ref
5457 pop_compile_unit (dw_die_ref old_unit)
5459 dw_die_ref new_unit = old_unit->die_sib;
5461 old_unit->die_sib = NULL;
5462 return new_unit;
5465 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5466 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5467 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5469 /* Calculate the checksum of a location expression. */
5471 static inline void
5472 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5474 int tem;
5475 hashval_t hash = 0;
5477 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5478 CHECKSUM (tem);
5479 hash = hash_loc_operands (loc, hash);
5480 CHECKSUM (hash);
5483 /* Calculate the checksum of an attribute. */
5485 static void
5486 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5488 dw_loc_descr_ref loc;
5489 rtx r;
5491 CHECKSUM (at->dw_attr);
5493 /* We don't care that this was compiled with a different compiler
5494 snapshot; if the output is the same, that's what matters. */
5495 if (at->dw_attr == DW_AT_producer)
5496 return;
5498 switch (AT_class (at))
5500 case dw_val_class_const:
5501 CHECKSUM (at->dw_attr_val.v.val_int);
5502 break;
5503 case dw_val_class_unsigned_const:
5504 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5505 break;
5506 case dw_val_class_const_double:
5507 CHECKSUM (at->dw_attr_val.v.val_double);
5508 break;
5509 case dw_val_class_vec:
5510 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5511 (at->dw_attr_val.v.val_vec.length
5512 * at->dw_attr_val.v.val_vec.elt_size));
5513 break;
5514 case dw_val_class_flag:
5515 CHECKSUM (at->dw_attr_val.v.val_flag);
5516 break;
5517 case dw_val_class_str:
5518 CHECKSUM_STRING (AT_string (at));
5519 break;
5521 case dw_val_class_addr:
5522 r = AT_addr (at);
5523 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5524 CHECKSUM_STRING (XSTR (r, 0));
5525 break;
5527 case dw_val_class_offset:
5528 CHECKSUM (at->dw_attr_val.v.val_offset);
5529 break;
5531 case dw_val_class_loc:
5532 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5533 loc_checksum (loc, ctx);
5534 break;
5536 case dw_val_class_die_ref:
5537 die_checksum (AT_ref (at), ctx, mark);
5538 break;
5540 case dw_val_class_fde_ref:
5541 case dw_val_class_vms_delta:
5542 case dw_val_class_lbl_id:
5543 case dw_val_class_lineptr:
5544 case dw_val_class_macptr:
5545 case dw_val_class_high_pc:
5546 break;
5548 case dw_val_class_file:
5549 CHECKSUM_STRING (AT_file (at)->filename);
5550 break;
5552 case dw_val_class_data8:
5553 CHECKSUM (at->dw_attr_val.v.val_data8);
5554 break;
5556 default:
5557 break;
5561 /* Calculate the checksum of a DIE. */
5563 static void
5564 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5566 dw_die_ref c;
5567 dw_attr_ref a;
5568 unsigned ix;
5570 /* To avoid infinite recursion. */
5571 if (die->die_mark)
5573 CHECKSUM (die->die_mark);
5574 return;
5576 die->die_mark = ++(*mark);
5578 CHECKSUM (die->die_tag);
5580 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5581 attr_checksum (a, ctx, mark);
5583 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5586 #undef CHECKSUM
5587 #undef CHECKSUM_BLOCK
5588 #undef CHECKSUM_STRING
5590 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5591 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5592 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5593 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5594 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5595 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5596 #define CHECKSUM_ATTR(FOO) \
5597 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5599 /* Calculate the checksum of a number in signed LEB128 format. */
5601 static void
5602 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5604 unsigned char byte;
5605 bool more;
5607 while (1)
5609 byte = (value & 0x7f);
5610 value >>= 7;
5611 more = !((value == 0 && (byte & 0x40) == 0)
5612 || (value == -1 && (byte & 0x40) != 0));
5613 if (more)
5614 byte |= 0x80;
5615 CHECKSUM (byte);
5616 if (!more)
5617 break;
5621 /* Calculate the checksum of a number in unsigned LEB128 format. */
5623 static void
5624 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5626 while (1)
5628 unsigned char byte = (value & 0x7f);
5629 value >>= 7;
5630 if (value != 0)
5631 /* More bytes to follow. */
5632 byte |= 0x80;
5633 CHECKSUM (byte);
5634 if (value == 0)
5635 break;
5639 /* Checksum the context of the DIE. This adds the names of any
5640 surrounding namespaces or structures to the checksum. */
5642 static void
5643 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5645 const char *name;
5646 dw_die_ref spec;
5647 int tag = die->die_tag;
5649 if (tag != DW_TAG_namespace
5650 && tag != DW_TAG_structure_type
5651 && tag != DW_TAG_class_type)
5652 return;
5654 name = get_AT_string (die, DW_AT_name);
5656 spec = get_AT_ref (die, DW_AT_specification);
5657 if (spec != NULL)
5658 die = spec;
5660 if (die->die_parent != NULL)
5661 checksum_die_context (die->die_parent, ctx);
5663 CHECKSUM_ULEB128 ('C');
5664 CHECKSUM_ULEB128 (tag);
5665 if (name != NULL)
5666 CHECKSUM_STRING (name);
5669 /* Calculate the checksum of a location expression. */
5671 static inline void
5672 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5674 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5675 were emitted as a DW_FORM_sdata instead of a location expression. */
5676 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5678 CHECKSUM_ULEB128 (DW_FORM_sdata);
5679 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5680 return;
5683 /* Otherwise, just checksum the raw location expression. */
5684 while (loc != NULL)
5686 hashval_t hash = 0;
5688 CHECKSUM_ULEB128 (loc->dtprel);
5689 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5690 hash = hash_loc_operands (loc, hash);
5691 CHECKSUM (hash);
5692 loc = loc->dw_loc_next;
5696 /* Calculate the checksum of an attribute. */
5698 static void
5699 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5700 struct md5_ctx *ctx, int *mark)
5702 dw_loc_descr_ref loc;
5703 rtx r;
5705 if (AT_class (at) == dw_val_class_die_ref)
5707 dw_die_ref target_die = AT_ref (at);
5709 /* For pointer and reference types, we checksum only the (qualified)
5710 name of the target type (if there is a name). For friend entries,
5711 we checksum only the (qualified) name of the target type or function.
5712 This allows the checksum to remain the same whether the target type
5713 is complete or not. */
5714 if ((at->dw_attr == DW_AT_type
5715 && (tag == DW_TAG_pointer_type
5716 || tag == DW_TAG_reference_type
5717 || tag == DW_TAG_rvalue_reference_type
5718 || tag == DW_TAG_ptr_to_member_type))
5719 || (at->dw_attr == DW_AT_friend
5720 && tag == DW_TAG_friend))
5722 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5724 if (name_attr != NULL)
5726 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5728 if (decl == NULL)
5729 decl = target_die;
5730 CHECKSUM_ULEB128 ('N');
5731 CHECKSUM_ULEB128 (at->dw_attr);
5732 if (decl->die_parent != NULL)
5733 checksum_die_context (decl->die_parent, ctx);
5734 CHECKSUM_ULEB128 ('E');
5735 CHECKSUM_STRING (AT_string (name_attr));
5736 return;
5740 /* For all other references to another DIE, we check to see if the
5741 target DIE has already been visited. If it has, we emit a
5742 backward reference; if not, we descend recursively. */
5743 if (target_die->die_mark > 0)
5745 CHECKSUM_ULEB128 ('R');
5746 CHECKSUM_ULEB128 (at->dw_attr);
5747 CHECKSUM_ULEB128 (target_die->die_mark);
5749 else
5751 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5753 if (decl == NULL)
5754 decl = target_die;
5755 target_die->die_mark = ++(*mark);
5756 CHECKSUM_ULEB128 ('T');
5757 CHECKSUM_ULEB128 (at->dw_attr);
5758 if (decl->die_parent != NULL)
5759 checksum_die_context (decl->die_parent, ctx);
5760 die_checksum_ordered (target_die, ctx, mark);
5762 return;
5765 CHECKSUM_ULEB128 ('A');
5766 CHECKSUM_ULEB128 (at->dw_attr);
5768 switch (AT_class (at))
5770 case dw_val_class_const:
5771 CHECKSUM_ULEB128 (DW_FORM_sdata);
5772 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5773 break;
5775 case dw_val_class_unsigned_const:
5776 CHECKSUM_ULEB128 (DW_FORM_sdata);
5777 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5778 break;
5780 case dw_val_class_const_double:
5781 CHECKSUM_ULEB128 (DW_FORM_block);
5782 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5783 CHECKSUM (at->dw_attr_val.v.val_double);
5784 break;
5786 case dw_val_class_vec:
5787 CHECKSUM_ULEB128 (DW_FORM_block);
5788 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5789 * at->dw_attr_val.v.val_vec.elt_size);
5790 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5791 (at->dw_attr_val.v.val_vec.length
5792 * at->dw_attr_val.v.val_vec.elt_size));
5793 break;
5795 case dw_val_class_flag:
5796 CHECKSUM_ULEB128 (DW_FORM_flag);
5797 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5798 break;
5800 case dw_val_class_str:
5801 CHECKSUM_ULEB128 (DW_FORM_string);
5802 CHECKSUM_STRING (AT_string (at));
5803 break;
5805 case dw_val_class_addr:
5806 r = AT_addr (at);
5807 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5808 CHECKSUM_ULEB128 (DW_FORM_string);
5809 CHECKSUM_STRING (XSTR (r, 0));
5810 break;
5812 case dw_val_class_offset:
5813 CHECKSUM_ULEB128 (DW_FORM_sdata);
5814 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5815 break;
5817 case dw_val_class_loc:
5818 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5819 loc_checksum_ordered (loc, ctx);
5820 break;
5822 case dw_val_class_fde_ref:
5823 case dw_val_class_lbl_id:
5824 case dw_val_class_lineptr:
5825 case dw_val_class_macptr:
5826 case dw_val_class_high_pc:
5827 break;
5829 case dw_val_class_file:
5830 CHECKSUM_ULEB128 (DW_FORM_string);
5831 CHECKSUM_STRING (AT_file (at)->filename);
5832 break;
5834 case dw_val_class_data8:
5835 CHECKSUM (at->dw_attr_val.v.val_data8);
5836 break;
5838 default:
5839 break;
5843 struct checksum_attributes
5845 dw_attr_ref at_name;
5846 dw_attr_ref at_type;
5847 dw_attr_ref at_friend;
5848 dw_attr_ref at_accessibility;
5849 dw_attr_ref at_address_class;
5850 dw_attr_ref at_allocated;
5851 dw_attr_ref at_artificial;
5852 dw_attr_ref at_associated;
5853 dw_attr_ref at_binary_scale;
5854 dw_attr_ref at_bit_offset;
5855 dw_attr_ref at_bit_size;
5856 dw_attr_ref at_bit_stride;
5857 dw_attr_ref at_byte_size;
5858 dw_attr_ref at_byte_stride;
5859 dw_attr_ref at_const_value;
5860 dw_attr_ref at_containing_type;
5861 dw_attr_ref at_count;
5862 dw_attr_ref at_data_location;
5863 dw_attr_ref at_data_member_location;
5864 dw_attr_ref at_decimal_scale;
5865 dw_attr_ref at_decimal_sign;
5866 dw_attr_ref at_default_value;
5867 dw_attr_ref at_digit_count;
5868 dw_attr_ref at_discr;
5869 dw_attr_ref at_discr_list;
5870 dw_attr_ref at_discr_value;
5871 dw_attr_ref at_encoding;
5872 dw_attr_ref at_endianity;
5873 dw_attr_ref at_explicit;
5874 dw_attr_ref at_is_optional;
5875 dw_attr_ref at_location;
5876 dw_attr_ref at_lower_bound;
5877 dw_attr_ref at_mutable;
5878 dw_attr_ref at_ordering;
5879 dw_attr_ref at_picture_string;
5880 dw_attr_ref at_prototyped;
5881 dw_attr_ref at_small;
5882 dw_attr_ref at_segment;
5883 dw_attr_ref at_string_length;
5884 dw_attr_ref at_threads_scaled;
5885 dw_attr_ref at_upper_bound;
5886 dw_attr_ref at_use_location;
5887 dw_attr_ref at_use_UTF8;
5888 dw_attr_ref at_variable_parameter;
5889 dw_attr_ref at_virtuality;
5890 dw_attr_ref at_visibility;
5891 dw_attr_ref at_vtable_elem_location;
5894 /* Collect the attributes that we will want to use for the checksum. */
5896 static void
5897 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5899 dw_attr_ref a;
5900 unsigned ix;
5902 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5904 switch (a->dw_attr)
5906 case DW_AT_name:
5907 attrs->at_name = a;
5908 break;
5909 case DW_AT_type:
5910 attrs->at_type = a;
5911 break;
5912 case DW_AT_friend:
5913 attrs->at_friend = a;
5914 break;
5915 case DW_AT_accessibility:
5916 attrs->at_accessibility = a;
5917 break;
5918 case DW_AT_address_class:
5919 attrs->at_address_class = a;
5920 break;
5921 case DW_AT_allocated:
5922 attrs->at_allocated = a;
5923 break;
5924 case DW_AT_artificial:
5925 attrs->at_artificial = a;
5926 break;
5927 case DW_AT_associated:
5928 attrs->at_associated = a;
5929 break;
5930 case DW_AT_binary_scale:
5931 attrs->at_binary_scale = a;
5932 break;
5933 case DW_AT_bit_offset:
5934 attrs->at_bit_offset = a;
5935 break;
5936 case DW_AT_bit_size:
5937 attrs->at_bit_size = a;
5938 break;
5939 case DW_AT_bit_stride:
5940 attrs->at_bit_stride = a;
5941 break;
5942 case DW_AT_byte_size:
5943 attrs->at_byte_size = a;
5944 break;
5945 case DW_AT_byte_stride:
5946 attrs->at_byte_stride = a;
5947 break;
5948 case DW_AT_const_value:
5949 attrs->at_const_value = a;
5950 break;
5951 case DW_AT_containing_type:
5952 attrs->at_containing_type = a;
5953 break;
5954 case DW_AT_count:
5955 attrs->at_count = a;
5956 break;
5957 case DW_AT_data_location:
5958 attrs->at_data_location = a;
5959 break;
5960 case DW_AT_data_member_location:
5961 attrs->at_data_member_location = a;
5962 break;
5963 case DW_AT_decimal_scale:
5964 attrs->at_decimal_scale = a;
5965 break;
5966 case DW_AT_decimal_sign:
5967 attrs->at_decimal_sign = a;
5968 break;
5969 case DW_AT_default_value:
5970 attrs->at_default_value = a;
5971 break;
5972 case DW_AT_digit_count:
5973 attrs->at_digit_count = a;
5974 break;
5975 case DW_AT_discr:
5976 attrs->at_discr = a;
5977 break;
5978 case DW_AT_discr_list:
5979 attrs->at_discr_list = a;
5980 break;
5981 case DW_AT_discr_value:
5982 attrs->at_discr_value = a;
5983 break;
5984 case DW_AT_encoding:
5985 attrs->at_encoding = a;
5986 break;
5987 case DW_AT_endianity:
5988 attrs->at_endianity = a;
5989 break;
5990 case DW_AT_explicit:
5991 attrs->at_explicit = a;
5992 break;
5993 case DW_AT_is_optional:
5994 attrs->at_is_optional = a;
5995 break;
5996 case DW_AT_location:
5997 attrs->at_location = a;
5998 break;
5999 case DW_AT_lower_bound:
6000 attrs->at_lower_bound = a;
6001 break;
6002 case DW_AT_mutable:
6003 attrs->at_mutable = a;
6004 break;
6005 case DW_AT_ordering:
6006 attrs->at_ordering = a;
6007 break;
6008 case DW_AT_picture_string:
6009 attrs->at_picture_string = a;
6010 break;
6011 case DW_AT_prototyped:
6012 attrs->at_prototyped = a;
6013 break;
6014 case DW_AT_small:
6015 attrs->at_small = a;
6016 break;
6017 case DW_AT_segment:
6018 attrs->at_segment = a;
6019 break;
6020 case DW_AT_string_length:
6021 attrs->at_string_length = a;
6022 break;
6023 case DW_AT_threads_scaled:
6024 attrs->at_threads_scaled = a;
6025 break;
6026 case DW_AT_upper_bound:
6027 attrs->at_upper_bound = a;
6028 break;
6029 case DW_AT_use_location:
6030 attrs->at_use_location = a;
6031 break;
6032 case DW_AT_use_UTF8:
6033 attrs->at_use_UTF8 = a;
6034 break;
6035 case DW_AT_variable_parameter:
6036 attrs->at_variable_parameter = a;
6037 break;
6038 case DW_AT_virtuality:
6039 attrs->at_virtuality = a;
6040 break;
6041 case DW_AT_visibility:
6042 attrs->at_visibility = a;
6043 break;
6044 case DW_AT_vtable_elem_location:
6045 attrs->at_vtable_elem_location = a;
6046 break;
6047 default:
6048 break;
6053 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6055 static void
6056 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6058 dw_die_ref c;
6059 dw_die_ref decl;
6060 struct checksum_attributes attrs;
6062 CHECKSUM_ULEB128 ('D');
6063 CHECKSUM_ULEB128 (die->die_tag);
6065 memset (&attrs, 0, sizeof (attrs));
6067 decl = get_AT_ref (die, DW_AT_specification);
6068 if (decl != NULL)
6069 collect_checksum_attributes (&attrs, decl);
6070 collect_checksum_attributes (&attrs, die);
6072 CHECKSUM_ATTR (attrs.at_name);
6073 CHECKSUM_ATTR (attrs.at_accessibility);
6074 CHECKSUM_ATTR (attrs.at_address_class);
6075 CHECKSUM_ATTR (attrs.at_allocated);
6076 CHECKSUM_ATTR (attrs.at_artificial);
6077 CHECKSUM_ATTR (attrs.at_associated);
6078 CHECKSUM_ATTR (attrs.at_binary_scale);
6079 CHECKSUM_ATTR (attrs.at_bit_offset);
6080 CHECKSUM_ATTR (attrs.at_bit_size);
6081 CHECKSUM_ATTR (attrs.at_bit_stride);
6082 CHECKSUM_ATTR (attrs.at_byte_size);
6083 CHECKSUM_ATTR (attrs.at_byte_stride);
6084 CHECKSUM_ATTR (attrs.at_const_value);
6085 CHECKSUM_ATTR (attrs.at_containing_type);
6086 CHECKSUM_ATTR (attrs.at_count);
6087 CHECKSUM_ATTR (attrs.at_data_location);
6088 CHECKSUM_ATTR (attrs.at_data_member_location);
6089 CHECKSUM_ATTR (attrs.at_decimal_scale);
6090 CHECKSUM_ATTR (attrs.at_decimal_sign);
6091 CHECKSUM_ATTR (attrs.at_default_value);
6092 CHECKSUM_ATTR (attrs.at_digit_count);
6093 CHECKSUM_ATTR (attrs.at_discr);
6094 CHECKSUM_ATTR (attrs.at_discr_list);
6095 CHECKSUM_ATTR (attrs.at_discr_value);
6096 CHECKSUM_ATTR (attrs.at_encoding);
6097 CHECKSUM_ATTR (attrs.at_endianity);
6098 CHECKSUM_ATTR (attrs.at_explicit);
6099 CHECKSUM_ATTR (attrs.at_is_optional);
6100 CHECKSUM_ATTR (attrs.at_location);
6101 CHECKSUM_ATTR (attrs.at_lower_bound);
6102 CHECKSUM_ATTR (attrs.at_mutable);
6103 CHECKSUM_ATTR (attrs.at_ordering);
6104 CHECKSUM_ATTR (attrs.at_picture_string);
6105 CHECKSUM_ATTR (attrs.at_prototyped);
6106 CHECKSUM_ATTR (attrs.at_small);
6107 CHECKSUM_ATTR (attrs.at_segment);
6108 CHECKSUM_ATTR (attrs.at_string_length);
6109 CHECKSUM_ATTR (attrs.at_threads_scaled);
6110 CHECKSUM_ATTR (attrs.at_upper_bound);
6111 CHECKSUM_ATTR (attrs.at_use_location);
6112 CHECKSUM_ATTR (attrs.at_use_UTF8);
6113 CHECKSUM_ATTR (attrs.at_variable_parameter);
6114 CHECKSUM_ATTR (attrs.at_virtuality);
6115 CHECKSUM_ATTR (attrs.at_visibility);
6116 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6117 CHECKSUM_ATTR (attrs.at_type);
6118 CHECKSUM_ATTR (attrs.at_friend);
6120 /* Checksum the child DIEs. */
6121 c = die->die_child;
6122 if (c) do {
6123 dw_attr_ref name_attr;
6125 c = c->die_sib;
6126 name_attr = get_AT (c, DW_AT_name);
6127 if (is_template_instantiation (c))
6129 /* Ignore instantiations of member type and function templates. */
6131 else if (name_attr != NULL
6132 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6134 /* Use a shallow checksum for named nested types and member
6135 functions. */
6136 CHECKSUM_ULEB128 ('S');
6137 CHECKSUM_ULEB128 (c->die_tag);
6138 CHECKSUM_STRING (AT_string (name_attr));
6140 else
6142 /* Use a deep checksum for other children. */
6143 /* Mark this DIE so it gets processed when unmarking. */
6144 if (c->die_mark == 0)
6145 c->die_mark = -1;
6146 die_checksum_ordered (c, ctx, mark);
6148 } while (c != die->die_child);
6150 CHECKSUM_ULEB128 (0);
6153 /* Add a type name and tag to a hash. */
6154 static void
6155 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6157 CHECKSUM_ULEB128 (tag);
6158 CHECKSUM_STRING (name);
6161 #undef CHECKSUM
6162 #undef CHECKSUM_STRING
6163 #undef CHECKSUM_ATTR
6164 #undef CHECKSUM_LEB128
6165 #undef CHECKSUM_ULEB128
6167 /* Generate the type signature for DIE. This is computed by generating an
6168 MD5 checksum over the DIE's tag, its relevant attributes, and its
6169 children. Attributes that are references to other DIEs are processed
6170 by recursion, using the MARK field to prevent infinite recursion.
6171 If the DIE is nested inside a namespace or another type, we also
6172 need to include that context in the signature. The lower 64 bits
6173 of the resulting MD5 checksum comprise the signature. */
6175 static void
6176 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6178 int mark;
6179 const char *name;
6180 unsigned char checksum[16];
6181 struct md5_ctx ctx;
6182 dw_die_ref decl;
6183 dw_die_ref parent;
6185 name = get_AT_string (die, DW_AT_name);
6186 decl = get_AT_ref (die, DW_AT_specification);
6187 parent = get_die_parent (die);
6189 /* First, compute a signature for just the type name (and its surrounding
6190 context, if any. This is stored in the type unit DIE for link-time
6191 ODR (one-definition rule) checking. */
6193 if (is_cxx () && name != NULL)
6195 md5_init_ctx (&ctx);
6197 /* Checksum the names of surrounding namespaces and structures. */
6198 if (parent != NULL)
6199 checksum_die_context (parent, &ctx);
6201 /* Checksum the current DIE. */
6202 die_odr_checksum (die->die_tag, name, &ctx);
6203 md5_finish_ctx (&ctx, checksum);
6205 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6208 /* Next, compute the complete type signature. */
6210 md5_init_ctx (&ctx);
6211 mark = 1;
6212 die->die_mark = mark;
6214 /* Checksum the names of surrounding namespaces and structures. */
6215 if (parent != NULL)
6216 checksum_die_context (parent, &ctx);
6218 /* Checksum the DIE and its children. */
6219 die_checksum_ordered (die, &ctx, &mark);
6220 unmark_all_dies (die);
6221 md5_finish_ctx (&ctx, checksum);
6223 /* Store the signature in the type node and link the type DIE and the
6224 type node together. */
6225 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6226 DWARF_TYPE_SIGNATURE_SIZE);
6227 die->comdat_type_p = true;
6228 die->die_id.die_type_node = type_node;
6229 type_node->type_die = die;
6231 /* If the DIE is a specification, link its declaration to the type node
6232 as well. */
6233 if (decl != NULL)
6235 decl->comdat_type_p = true;
6236 decl->die_id.die_type_node = type_node;
6240 /* Do the location expressions look same? */
6241 static inline int
6242 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6244 return loc1->dw_loc_opc == loc2->dw_loc_opc
6245 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6246 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6249 /* Do the values look the same? */
6250 static int
6251 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6253 dw_loc_descr_ref loc1, loc2;
6254 rtx r1, r2;
6256 if (v1->val_class != v2->val_class)
6257 return 0;
6259 switch (v1->val_class)
6261 case dw_val_class_const:
6262 return v1->v.val_int == v2->v.val_int;
6263 case dw_val_class_unsigned_const:
6264 return v1->v.val_unsigned == v2->v.val_unsigned;
6265 case dw_val_class_const_double:
6266 return v1->v.val_double.high == v2->v.val_double.high
6267 && v1->v.val_double.low == v2->v.val_double.low;
6268 case dw_val_class_vec:
6269 if (v1->v.val_vec.length != v2->v.val_vec.length
6270 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6271 return 0;
6272 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6273 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6274 return 0;
6275 return 1;
6276 case dw_val_class_flag:
6277 return v1->v.val_flag == v2->v.val_flag;
6278 case dw_val_class_str:
6279 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6281 case dw_val_class_addr:
6282 r1 = v1->v.val_addr;
6283 r2 = v2->v.val_addr;
6284 if (GET_CODE (r1) != GET_CODE (r2))
6285 return 0;
6286 return !rtx_equal_p (r1, r2);
6288 case dw_val_class_offset:
6289 return v1->v.val_offset == v2->v.val_offset;
6291 case dw_val_class_loc:
6292 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6293 loc1 && loc2;
6294 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6295 if (!same_loc_p (loc1, loc2, mark))
6296 return 0;
6297 return !loc1 && !loc2;
6299 case dw_val_class_die_ref:
6300 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6302 case dw_val_class_fde_ref:
6303 case dw_val_class_vms_delta:
6304 case dw_val_class_lbl_id:
6305 case dw_val_class_lineptr:
6306 case dw_val_class_macptr:
6307 case dw_val_class_high_pc:
6308 return 1;
6310 case dw_val_class_file:
6311 return v1->v.val_file == v2->v.val_file;
6313 case dw_val_class_data8:
6314 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6316 default:
6317 return 1;
6321 /* Do the attributes look the same? */
6323 static int
6324 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6326 if (at1->dw_attr != at2->dw_attr)
6327 return 0;
6329 /* We don't care that this was compiled with a different compiler
6330 snapshot; if the output is the same, that's what matters. */
6331 if (at1->dw_attr == DW_AT_producer)
6332 return 1;
6334 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6337 /* Do the dies look the same? */
6339 static int
6340 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6342 dw_die_ref c1, c2;
6343 dw_attr_ref a1;
6344 unsigned ix;
6346 /* To avoid infinite recursion. */
6347 if (die1->die_mark)
6348 return die1->die_mark == die2->die_mark;
6349 die1->die_mark = die2->die_mark = ++(*mark);
6351 if (die1->die_tag != die2->die_tag)
6352 return 0;
6354 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6355 return 0;
6357 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6358 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6359 return 0;
6361 c1 = die1->die_child;
6362 c2 = die2->die_child;
6363 if (! c1)
6365 if (c2)
6366 return 0;
6368 else
6369 for (;;)
6371 if (!same_die_p (c1, c2, mark))
6372 return 0;
6373 c1 = c1->die_sib;
6374 c2 = c2->die_sib;
6375 if (c1 == die1->die_child)
6377 if (c2 == die2->die_child)
6378 break;
6379 else
6380 return 0;
6384 return 1;
6387 /* Do the dies look the same? Wrapper around same_die_p. */
6389 static int
6390 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6392 int mark = 0;
6393 int ret = same_die_p (die1, die2, &mark);
6395 unmark_all_dies (die1);
6396 unmark_all_dies (die2);
6398 return ret;
6401 /* The prefix to attach to symbols on DIEs in the current comdat debug
6402 info section. */
6403 static const char *comdat_symbol_id;
6405 /* The index of the current symbol within the current comdat CU. */
6406 static unsigned int comdat_symbol_number;
6408 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6409 children, and set comdat_symbol_id accordingly. */
6411 static void
6412 compute_section_prefix (dw_die_ref unit_die)
6414 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6415 const char *base = die_name ? lbasename (die_name) : "anonymous";
6416 char *name = XALLOCAVEC (char, strlen (base) + 64);
6417 char *p;
6418 int i, mark;
6419 unsigned char checksum[16];
6420 struct md5_ctx ctx;
6422 /* Compute the checksum of the DIE, then append part of it as hex digits to
6423 the name filename of the unit. */
6425 md5_init_ctx (&ctx);
6426 mark = 0;
6427 die_checksum (unit_die, &ctx, &mark);
6428 unmark_all_dies (unit_die);
6429 md5_finish_ctx (&ctx, checksum);
6431 sprintf (name, "%s.", base);
6432 clean_symbol_name (name);
6434 p = name + strlen (name);
6435 for (i = 0; i < 4; i++)
6437 sprintf (p, "%.2x", checksum[i]);
6438 p += 2;
6441 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6442 comdat_symbol_number = 0;
6445 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6447 static int
6448 is_type_die (dw_die_ref die)
6450 switch (die->die_tag)
6452 case DW_TAG_array_type:
6453 case DW_TAG_class_type:
6454 case DW_TAG_interface_type:
6455 case DW_TAG_enumeration_type:
6456 case DW_TAG_pointer_type:
6457 case DW_TAG_reference_type:
6458 case DW_TAG_rvalue_reference_type:
6459 case DW_TAG_string_type:
6460 case DW_TAG_structure_type:
6461 case DW_TAG_subroutine_type:
6462 case DW_TAG_union_type:
6463 case DW_TAG_ptr_to_member_type:
6464 case DW_TAG_set_type:
6465 case DW_TAG_subrange_type:
6466 case DW_TAG_base_type:
6467 case DW_TAG_const_type:
6468 case DW_TAG_file_type:
6469 case DW_TAG_packed_type:
6470 case DW_TAG_volatile_type:
6471 case DW_TAG_typedef:
6472 return 1;
6473 default:
6474 return 0;
6478 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6479 Basically, we want to choose the bits that are likely to be shared between
6480 compilations (types) and leave out the bits that are specific to individual
6481 compilations (functions). */
6483 static int
6484 is_comdat_die (dw_die_ref c)
6486 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6487 we do for stabs. The advantage is a greater likelihood of sharing between
6488 objects that don't include headers in the same order (and therefore would
6489 put the base types in a different comdat). jason 8/28/00 */
6491 if (c->die_tag == DW_TAG_base_type)
6492 return 0;
6494 if (c->die_tag == DW_TAG_pointer_type
6495 || c->die_tag == DW_TAG_reference_type
6496 || c->die_tag == DW_TAG_rvalue_reference_type
6497 || c->die_tag == DW_TAG_const_type
6498 || c->die_tag == DW_TAG_volatile_type)
6500 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6502 return t ? is_comdat_die (t) : 0;
6505 return is_type_die (c);
6508 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6509 compilation unit. */
6511 static int
6512 is_symbol_die (dw_die_ref c)
6514 return (is_type_die (c)
6515 || is_declaration_die (c)
6516 || c->die_tag == DW_TAG_namespace
6517 || c->die_tag == DW_TAG_module);
6520 /* Returns true iff C is a compile-unit DIE. */
6522 static inline bool
6523 is_cu_die (dw_die_ref c)
6525 return c && c->die_tag == DW_TAG_compile_unit;
6528 /* Returns true iff C is a unit DIE of some sort. */
6530 static inline bool
6531 is_unit_die (dw_die_ref c)
6533 return c && (c->die_tag == DW_TAG_compile_unit
6534 || c->die_tag == DW_TAG_partial_unit
6535 || c->die_tag == DW_TAG_type_unit);
6538 /* Returns true iff C is a namespace DIE. */
6540 static inline bool
6541 is_namespace_die (dw_die_ref c)
6543 return c && c->die_tag == DW_TAG_namespace;
6546 /* Returns true iff C is a class or structure DIE. */
6548 static inline bool
6549 is_class_die (dw_die_ref c)
6551 return c && (c->die_tag == DW_TAG_class_type
6552 || c->die_tag == DW_TAG_structure_type);
6555 /* Return non-zero if this DIE is a template parameter. */
6557 static inline bool
6558 is_template_parameter (dw_die_ref die)
6560 switch (die->die_tag)
6562 case DW_TAG_template_type_param:
6563 case DW_TAG_template_value_param:
6564 case DW_TAG_GNU_template_template_param:
6565 case DW_TAG_GNU_template_parameter_pack:
6566 return true;
6567 default:
6568 return false;
6572 /* Return non-zero if this DIE represents a template instantiation. */
6574 static inline bool
6575 is_template_instantiation (dw_die_ref die)
6577 dw_die_ref c;
6579 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6580 return false;
6581 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6582 return false;
6585 static char *
6586 gen_internal_sym (const char *prefix)
6588 char buf[256];
6590 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6591 return xstrdup (buf);
6594 /* Assign symbols to all worthy DIEs under DIE. */
6596 static void
6597 assign_symbol_names (dw_die_ref die)
6599 dw_die_ref c;
6601 if (is_symbol_die (die) && !die->comdat_type_p)
6603 if (comdat_symbol_id)
6605 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6607 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6608 comdat_symbol_id, comdat_symbol_number++);
6609 die->die_id.die_symbol = xstrdup (p);
6611 else
6612 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6615 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6618 struct cu_hash_table_entry
6620 dw_die_ref cu;
6621 unsigned min_comdat_num, max_comdat_num;
6622 struct cu_hash_table_entry *next;
6625 /* Helpers to manipulate hash table of CUs. */
6627 struct cu_hash_table_entry_hasher
6629 typedef cu_hash_table_entry value_type;
6630 typedef die_struct compare_type;
6631 static inline hashval_t hash (const value_type *);
6632 static inline bool equal (const value_type *, const compare_type *);
6633 static inline void remove (value_type *);
6636 inline hashval_t
6637 cu_hash_table_entry_hasher::hash (const value_type *entry)
6639 return htab_hash_string (entry->cu->die_id.die_symbol);
6642 inline bool
6643 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6644 const compare_type *entry2)
6646 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6649 inline void
6650 cu_hash_table_entry_hasher::remove (value_type *entry)
6652 struct cu_hash_table_entry *next;
6654 while (entry)
6656 next = entry->next;
6657 free (entry);
6658 entry = next;
6662 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6664 /* Check whether we have already seen this CU and set up SYM_NUM
6665 accordingly. */
6666 static int
6667 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6669 struct cu_hash_table_entry dummy;
6670 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6672 dummy.max_comdat_num = 0;
6674 slot = htable.find_slot_with_hash (cu,
6675 htab_hash_string (cu->die_id.die_symbol),
6676 INSERT);
6677 entry = *slot;
6679 for (; entry; last = entry, entry = entry->next)
6681 if (same_die_p_wrap (cu, entry->cu))
6682 break;
6685 if (entry)
6687 *sym_num = entry->min_comdat_num;
6688 return 1;
6691 entry = XCNEW (struct cu_hash_table_entry);
6692 entry->cu = cu;
6693 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6694 entry->next = *slot;
6695 *slot = entry;
6697 return 0;
6700 /* Record SYM_NUM to record of CU in HTABLE. */
6701 static void
6702 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6703 unsigned int sym_num)
6705 struct cu_hash_table_entry **slot, *entry;
6707 slot = htable.find_slot_with_hash (cu,
6708 htab_hash_string (cu->die_id.die_symbol),
6709 NO_INSERT);
6710 entry = *slot;
6712 entry->max_comdat_num = sym_num;
6715 /* Traverse the DIE (which is always comp_unit_die), and set up
6716 additional compilation units for each of the include files we see
6717 bracketed by BINCL/EINCL. */
6719 static void
6720 break_out_includes (dw_die_ref die)
6722 dw_die_ref c;
6723 dw_die_ref unit = NULL;
6724 limbo_die_node *node, **pnode;
6725 cu_hash_type cu_hash_table;
6727 c = die->die_child;
6728 if (c) do {
6729 dw_die_ref prev = c;
6730 c = c->die_sib;
6731 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6732 || (unit && is_comdat_die (c)))
6734 dw_die_ref next = c->die_sib;
6736 /* This DIE is for a secondary CU; remove it from the main one. */
6737 remove_child_with_prev (c, prev);
6739 if (c->die_tag == DW_TAG_GNU_BINCL)
6740 unit = push_new_compile_unit (unit, c);
6741 else if (c->die_tag == DW_TAG_GNU_EINCL)
6742 unit = pop_compile_unit (unit);
6743 else
6744 add_child_die (unit, c);
6745 c = next;
6746 if (c == die->die_child)
6747 break;
6749 } while (c != die->die_child);
6751 #if 0
6752 /* We can only use this in debugging, since the frontend doesn't check
6753 to make sure that we leave every include file we enter. */
6754 gcc_assert (!unit);
6755 #endif
6757 assign_symbol_names (die);
6758 cu_hash_table.create (10);
6759 for (node = limbo_die_list, pnode = &limbo_die_list;
6760 node;
6761 node = node->next)
6763 int is_dupl;
6765 compute_section_prefix (node->die);
6766 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6767 &comdat_symbol_number);
6768 assign_symbol_names (node->die);
6769 if (is_dupl)
6770 *pnode = node->next;
6771 else
6773 pnode = &node->next;
6774 record_comdat_symbol_number (node->die, cu_hash_table,
6775 comdat_symbol_number);
6778 cu_hash_table.dispose ();
6781 /* Return non-zero if this DIE is a declaration. */
6783 static int
6784 is_declaration_die (dw_die_ref die)
6786 dw_attr_ref a;
6787 unsigned ix;
6789 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6790 if (a->dw_attr == DW_AT_declaration)
6791 return 1;
6793 return 0;
6796 /* Return non-zero if this DIE is nested inside a subprogram. */
6798 static int
6799 is_nested_in_subprogram (dw_die_ref die)
6801 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6803 if (decl == NULL)
6804 decl = die;
6805 return local_scope_p (decl);
6808 /* Return non-zero if this DIE contains a defining declaration of a
6809 subprogram. */
6811 static int
6812 contains_subprogram_definition (dw_die_ref die)
6814 dw_die_ref c;
6816 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6817 return 1;
6818 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6819 return 0;
6822 /* Return non-zero if this is a type DIE that should be moved to a
6823 COMDAT .debug_types section. */
6825 static int
6826 should_move_die_to_comdat (dw_die_ref die)
6828 switch (die->die_tag)
6830 case DW_TAG_class_type:
6831 case DW_TAG_structure_type:
6832 case DW_TAG_enumeration_type:
6833 case DW_TAG_union_type:
6834 /* Don't move declarations, inlined instances, or types nested in a
6835 subprogram. */
6836 if (is_declaration_die (die)
6837 || get_AT (die, DW_AT_abstract_origin)
6838 || is_nested_in_subprogram (die))
6839 return 0;
6840 /* A type definition should never contain a subprogram definition. */
6841 gcc_assert (!contains_subprogram_definition (die));
6842 return 1;
6843 case DW_TAG_array_type:
6844 case DW_TAG_interface_type:
6845 case DW_TAG_pointer_type:
6846 case DW_TAG_reference_type:
6847 case DW_TAG_rvalue_reference_type:
6848 case DW_TAG_string_type:
6849 case DW_TAG_subroutine_type:
6850 case DW_TAG_ptr_to_member_type:
6851 case DW_TAG_set_type:
6852 case DW_TAG_subrange_type:
6853 case DW_TAG_base_type:
6854 case DW_TAG_const_type:
6855 case DW_TAG_file_type:
6856 case DW_TAG_packed_type:
6857 case DW_TAG_volatile_type:
6858 case DW_TAG_typedef:
6859 default:
6860 return 0;
6864 /* Make a clone of DIE. */
6866 static dw_die_ref
6867 clone_die (dw_die_ref die)
6869 dw_die_ref clone;
6870 dw_attr_ref a;
6871 unsigned ix;
6873 clone = ggc_alloc_cleared_die_node ();
6874 clone->die_tag = die->die_tag;
6876 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6877 add_dwarf_attr (clone, a);
6879 return clone;
6882 /* Make a clone of the tree rooted at DIE. */
6884 static dw_die_ref
6885 clone_tree (dw_die_ref die)
6887 dw_die_ref c;
6888 dw_die_ref clone = clone_die (die);
6890 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6892 return clone;
6895 /* Make a clone of DIE as a declaration. */
6897 static dw_die_ref
6898 clone_as_declaration (dw_die_ref die)
6900 dw_die_ref clone;
6901 dw_die_ref decl;
6902 dw_attr_ref a;
6903 unsigned ix;
6905 /* If the DIE is already a declaration, just clone it. */
6906 if (is_declaration_die (die))
6907 return clone_die (die);
6909 /* If the DIE is a specification, just clone its declaration DIE. */
6910 decl = get_AT_ref (die, DW_AT_specification);
6911 if (decl != NULL)
6913 clone = clone_die (decl);
6914 if (die->comdat_type_p)
6915 add_AT_die_ref (clone, DW_AT_signature, die);
6916 return clone;
6919 clone = ggc_alloc_cleared_die_node ();
6920 clone->die_tag = die->die_tag;
6922 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6924 /* We don't want to copy over all attributes.
6925 For example we don't want DW_AT_byte_size because otherwise we will no
6926 longer have a declaration and GDB will treat it as a definition. */
6928 switch (a->dw_attr)
6930 case DW_AT_artificial:
6931 case DW_AT_containing_type:
6932 case DW_AT_external:
6933 case DW_AT_name:
6934 case DW_AT_type:
6935 case DW_AT_virtuality:
6936 case DW_AT_linkage_name:
6937 case DW_AT_MIPS_linkage_name:
6938 add_dwarf_attr (clone, a);
6939 break;
6940 case DW_AT_byte_size:
6941 default:
6942 break;
6946 if (die->comdat_type_p)
6947 add_AT_die_ref (clone, DW_AT_signature, die);
6949 add_AT_flag (clone, DW_AT_declaration, 1);
6950 return clone;
6954 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6956 struct decl_table_entry
6958 dw_die_ref orig;
6959 dw_die_ref copy;
6962 /* Helpers to manipulate hash table of copied declarations. */
6964 /* Hashtable helpers. */
6966 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6968 typedef decl_table_entry value_type;
6969 typedef die_struct compare_type;
6970 static inline hashval_t hash (const value_type *);
6971 static inline bool equal (const value_type *, const compare_type *);
6974 inline hashval_t
6975 decl_table_entry_hasher::hash (const value_type *entry)
6977 return htab_hash_pointer (entry->orig);
6980 inline bool
6981 decl_table_entry_hasher::equal (const value_type *entry1,
6982 const compare_type *entry2)
6984 return entry1->orig == entry2;
6987 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6989 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6990 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6991 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6992 to check if the ancestor has already been copied into UNIT. */
6994 static dw_die_ref
6995 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6997 dw_die_ref parent = die->die_parent;
6998 dw_die_ref new_parent = unit;
6999 dw_die_ref copy;
7000 decl_table_entry **slot = NULL;
7001 struct decl_table_entry *entry = NULL;
7003 if (decl_table.is_created ())
7005 /* Check if the entry has already been copied to UNIT. */
7006 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7007 INSERT);
7008 if (*slot != HTAB_EMPTY_ENTRY)
7010 entry = *slot;
7011 return entry->copy;
7014 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7015 entry = XCNEW (struct decl_table_entry);
7016 entry->orig = die;
7017 entry->copy = NULL;
7018 *slot = entry;
7021 if (parent != NULL)
7023 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7024 if (spec != NULL)
7025 parent = spec;
7026 if (!is_unit_die (parent))
7027 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7030 copy = clone_as_declaration (die);
7031 add_child_die (new_parent, copy);
7033 if (decl_table.is_created ())
7035 /* Record the pointer to the copy. */
7036 entry->copy = copy;
7039 return copy;
7041 /* Copy the declaration context to the new type unit DIE. This includes
7042 any surrounding namespace or type declarations. If the DIE has an
7043 AT_specification attribute, it also includes attributes and children
7044 attached to the specification, and returns a pointer to the original
7045 parent of the declaration DIE. Returns NULL otherwise. */
7047 static dw_die_ref
7048 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7050 dw_die_ref decl;
7051 dw_die_ref new_decl;
7052 dw_die_ref orig_parent = NULL;
7054 decl = get_AT_ref (die, DW_AT_specification);
7055 if (decl == NULL)
7056 decl = die;
7057 else
7059 unsigned ix;
7060 dw_die_ref c;
7061 dw_attr_ref a;
7063 /* The original DIE will be changed to a declaration, and must
7064 be moved to be a child of the original declaration DIE. */
7065 orig_parent = decl->die_parent;
7067 /* Copy the type node pointer from the new DIE to the original
7068 declaration DIE so we can forward references later. */
7069 decl->comdat_type_p = true;
7070 decl->die_id.die_type_node = die->die_id.die_type_node;
7072 remove_AT (die, DW_AT_specification);
7074 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7076 if (a->dw_attr != DW_AT_name
7077 && a->dw_attr != DW_AT_declaration
7078 && a->dw_attr != DW_AT_external)
7079 add_dwarf_attr (die, a);
7082 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7085 if (decl->die_parent != NULL
7086 && !is_unit_die (decl->die_parent))
7088 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7089 if (new_decl != NULL)
7091 remove_AT (new_decl, DW_AT_signature);
7092 add_AT_specification (die, new_decl);
7096 return orig_parent;
7099 /* Generate the skeleton ancestor tree for the given NODE, then clone
7100 the DIE and add the clone into the tree. */
7102 static void
7103 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7105 if (node->new_die != NULL)
7106 return;
7108 node->new_die = clone_as_declaration (node->old_die);
7110 if (node->parent != NULL)
7112 generate_skeleton_ancestor_tree (node->parent);
7113 add_child_die (node->parent->new_die, node->new_die);
7117 /* Generate a skeleton tree of DIEs containing any declarations that are
7118 found in the original tree. We traverse the tree looking for declaration
7119 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7121 static void
7122 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7124 skeleton_chain_node node;
7125 dw_die_ref c;
7126 dw_die_ref first;
7127 dw_die_ref prev = NULL;
7128 dw_die_ref next = NULL;
7130 node.parent = parent;
7132 first = c = parent->old_die->die_child;
7133 if (c)
7134 next = c->die_sib;
7135 if (c) do {
7136 if (prev == NULL || prev->die_sib == c)
7137 prev = c;
7138 c = next;
7139 next = (c == first ? NULL : c->die_sib);
7140 node.old_die = c;
7141 node.new_die = NULL;
7142 if (is_declaration_die (c))
7144 if (is_template_instantiation (c))
7146 /* Instantiated templates do not need to be cloned into the
7147 type unit. Just move the DIE and its children back to
7148 the skeleton tree (in the main CU). */
7149 remove_child_with_prev (c, prev);
7150 add_child_die (parent->new_die, c);
7151 c = prev;
7153 else
7155 /* Clone the existing DIE, move the original to the skeleton
7156 tree (which is in the main CU), and put the clone, with
7157 all the original's children, where the original came from
7158 (which is about to be moved to the type unit). */
7159 dw_die_ref clone = clone_die (c);
7160 move_all_children (c, clone);
7162 replace_child (c, clone, prev);
7163 generate_skeleton_ancestor_tree (parent);
7164 add_child_die (parent->new_die, c);
7165 node.new_die = c;
7166 c = clone;
7169 generate_skeleton_bottom_up (&node);
7170 } while (next != NULL);
7173 /* Wrapper function for generate_skeleton_bottom_up. */
7175 static dw_die_ref
7176 generate_skeleton (dw_die_ref die)
7178 skeleton_chain_node node;
7180 node.old_die = die;
7181 node.new_die = NULL;
7182 node.parent = NULL;
7184 /* If this type definition is nested inside another type,
7185 and is not an instantiation of a template, always leave
7186 at least a declaration in its place. */
7187 if (die->die_parent != NULL
7188 && is_type_die (die->die_parent)
7189 && !is_template_instantiation (die))
7190 node.new_die = clone_as_declaration (die);
7192 generate_skeleton_bottom_up (&node);
7193 return node.new_die;
7196 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7197 declaration. The original DIE is moved to a new compile unit so that
7198 existing references to it follow it to the new location. If any of the
7199 original DIE's descendants is a declaration, we need to replace the
7200 original DIE with a skeleton tree and move the declarations back into the
7201 skeleton tree. */
7203 static dw_die_ref
7204 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7205 dw_die_ref prev)
7207 dw_die_ref skeleton, orig_parent;
7209 /* Copy the declaration context to the type unit DIE. If the returned
7210 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7211 that DIE. */
7212 orig_parent = copy_declaration_context (unit, child);
7214 skeleton = generate_skeleton (child);
7215 if (skeleton == NULL)
7216 remove_child_with_prev (child, prev);
7217 else
7219 skeleton->comdat_type_p = true;
7220 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7222 /* If the original DIE was a specification, we need to put
7223 the skeleton under the parent DIE of the declaration.
7224 This leaves the original declaration in the tree, but
7225 it will be pruned later since there are no longer any
7226 references to it. */
7227 if (orig_parent != NULL)
7229 remove_child_with_prev (child, prev);
7230 add_child_die (orig_parent, skeleton);
7232 else
7233 replace_child (child, skeleton, prev);
7236 return skeleton;
7239 /* Traverse the DIE and set up additional .debug_types sections for each
7240 type worthy of being placed in a COMDAT section. */
7242 static void
7243 break_out_comdat_types (dw_die_ref die)
7245 dw_die_ref c;
7246 dw_die_ref first;
7247 dw_die_ref prev = NULL;
7248 dw_die_ref next = NULL;
7249 dw_die_ref unit = NULL;
7251 first = c = die->die_child;
7252 if (c)
7253 next = c->die_sib;
7254 if (c) do {
7255 if (prev == NULL || prev->die_sib == c)
7256 prev = c;
7257 c = next;
7258 next = (c == first ? NULL : c->die_sib);
7259 if (should_move_die_to_comdat (c))
7261 dw_die_ref replacement;
7262 comdat_type_node_ref type_node;
7264 /* Break out nested types into their own type units. */
7265 break_out_comdat_types (c);
7267 /* Create a new type unit DIE as the root for the new tree, and
7268 add it to the list of comdat types. */
7269 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7270 add_AT_unsigned (unit, DW_AT_language,
7271 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7272 type_node = ggc_alloc_cleared_comdat_type_node ();
7273 type_node->root_die = unit;
7274 type_node->next = comdat_type_list;
7275 comdat_type_list = type_node;
7277 /* Generate the type signature. */
7278 generate_type_signature (c, type_node);
7280 /* Copy the declaration context, attributes, and children of the
7281 declaration into the new type unit DIE, then remove this DIE
7282 from the main CU (or replace it with a skeleton if necessary). */
7283 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7284 type_node->skeleton_die = replacement;
7286 /* Add the DIE to the new compunit. */
7287 add_child_die (unit, c);
7289 if (replacement != NULL)
7290 c = replacement;
7292 else if (c->die_tag == DW_TAG_namespace
7293 || c->die_tag == DW_TAG_class_type
7294 || c->die_tag == DW_TAG_structure_type
7295 || c->die_tag == DW_TAG_union_type)
7297 /* Look for nested types that can be broken out. */
7298 break_out_comdat_types (c);
7300 } while (next != NULL);
7303 /* Like clone_tree, but additionally enter all the children into
7304 the hash table decl_table. */
7306 static dw_die_ref
7307 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7309 dw_die_ref c;
7310 dw_die_ref clone = clone_die (die);
7311 struct decl_table_entry *entry;
7312 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7313 htab_hash_pointer (die), INSERT);
7314 /* Assert that DIE isn't in the hash table yet. If it would be there
7315 before, the ancestors would be necessarily there as well, therefore
7316 clone_tree_hash wouldn't be called. */
7317 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7318 entry = XCNEW (struct decl_table_entry);
7319 entry->orig = die;
7320 entry->copy = clone;
7321 *slot = entry;
7323 FOR_EACH_CHILD (die, c,
7324 add_child_die (clone, clone_tree_hash (c, decl_table)));
7326 return clone;
7329 /* Walk the DIE and its children, looking for references to incomplete
7330 or trivial types that are unmarked (i.e., that are not in the current
7331 type_unit). */
7333 static void
7334 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7336 dw_die_ref c;
7337 dw_attr_ref a;
7338 unsigned ix;
7340 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7342 if (AT_class (a) == dw_val_class_die_ref)
7344 dw_die_ref targ = AT_ref (a);
7345 decl_table_entry **slot;
7346 struct decl_table_entry *entry;
7348 if (targ->die_mark != 0 || targ->comdat_type_p)
7349 continue;
7351 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7352 INSERT);
7354 if (*slot != HTAB_EMPTY_ENTRY)
7356 /* TARG has already been copied, so we just need to
7357 modify the reference to point to the copy. */
7358 entry = *slot;
7359 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7361 else
7363 dw_die_ref parent = unit;
7364 dw_die_ref copy = clone_die (targ);
7366 /* Record in DECL_TABLE that TARG has been copied.
7367 Need to do this now, before the recursive call,
7368 because DECL_TABLE may be expanded and SLOT
7369 would no longer be a valid pointer. */
7370 entry = XCNEW (struct decl_table_entry);
7371 entry->orig = targ;
7372 entry->copy = copy;
7373 *slot = entry;
7375 FOR_EACH_CHILD (targ, c,
7376 add_child_die (copy,
7377 clone_tree_hash (c, decl_table)));
7379 /* Make sure the cloned tree is marked as part of the
7380 type unit. */
7381 mark_dies (copy);
7383 /* If TARG has surrounding context, copy its ancestor tree
7384 into the new type unit. */
7385 if (targ->die_parent != NULL
7386 && !is_unit_die (targ->die_parent))
7387 parent = copy_ancestor_tree (unit, targ->die_parent,
7388 decl_table);
7390 add_child_die (parent, copy);
7391 a->dw_attr_val.v.val_die_ref.die = copy;
7393 /* Make sure the newly-copied DIE is walked. If it was
7394 installed in a previously-added context, it won't
7395 get visited otherwise. */
7396 if (parent != unit)
7398 /* Find the highest point of the newly-added tree,
7399 mark each node along the way, and walk from there. */
7400 parent->die_mark = 1;
7401 while (parent->die_parent
7402 && parent->die_parent->die_mark == 0)
7404 parent = parent->die_parent;
7405 parent->die_mark = 1;
7407 copy_decls_walk (unit, parent, decl_table);
7413 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7416 /* Copy declarations for "unworthy" types into the new comdat section.
7417 Incomplete types, modified types, and certain other types aren't broken
7418 out into comdat sections of their own, so they don't have a signature,
7419 and we need to copy the declaration into the same section so that we
7420 don't have an external reference. */
7422 static void
7423 copy_decls_for_unworthy_types (dw_die_ref unit)
7425 decl_hash_type decl_table;
7427 mark_dies (unit);
7428 decl_table.create (10);
7429 copy_decls_walk (unit, unit, decl_table);
7430 decl_table.dispose ();
7431 unmark_dies (unit);
7434 /* Traverse the DIE and add a sibling attribute if it may have the
7435 effect of speeding up access to siblings. To save some space,
7436 avoid generating sibling attributes for DIE's without children. */
7438 static void
7439 add_sibling_attributes (dw_die_ref die)
7441 dw_die_ref c;
7443 if (! die->die_child)
7444 return;
7446 if (die->die_parent && die != die->die_parent->die_child)
7447 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7449 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7452 /* Output all location lists for the DIE and its children. */
7454 static void
7455 output_location_lists (dw_die_ref die)
7457 dw_die_ref c;
7458 dw_attr_ref a;
7459 unsigned ix;
7461 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7462 if (AT_class (a) == dw_val_class_loc_list)
7463 output_loc_list (AT_loc_list (a));
7465 FOR_EACH_CHILD (die, c, output_location_lists (c));
7468 /* We want to limit the number of external references, because they are
7469 larger than local references: a relocation takes multiple words, and
7470 even a sig8 reference is always eight bytes, whereas a local reference
7471 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7472 So if we encounter multiple external references to the same type DIE, we
7473 make a local typedef stub for it and redirect all references there.
7475 This is the element of the hash table for keeping track of these
7476 references. */
7478 struct external_ref
7480 dw_die_ref type;
7481 dw_die_ref stub;
7482 unsigned n_refs;
7485 /* Hashtable helpers. */
7487 struct external_ref_hasher : typed_free_remove <external_ref>
7489 typedef external_ref value_type;
7490 typedef external_ref compare_type;
7491 static inline hashval_t hash (const value_type *);
7492 static inline bool equal (const value_type *, const compare_type *);
7495 inline hashval_t
7496 external_ref_hasher::hash (const value_type *r)
7498 dw_die_ref die = r->type;
7499 hashval_t h = 0;
7501 /* We can't use the address of the DIE for hashing, because
7502 that will make the order of the stub DIEs non-deterministic. */
7503 if (! die->comdat_type_p)
7504 /* We have a symbol; use it to compute a hash. */
7505 h = htab_hash_string (die->die_id.die_symbol);
7506 else
7508 /* We have a type signature; use a subset of the bits as the hash.
7509 The 8-byte signature is at least as large as hashval_t. */
7510 comdat_type_node_ref type_node = die->die_id.die_type_node;
7511 memcpy (&h, type_node->signature, sizeof (h));
7513 return h;
7516 inline bool
7517 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7519 return r1->type == r2->type;
7522 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7524 /* Return a pointer to the external_ref for references to DIE. */
7526 static struct external_ref *
7527 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7529 struct external_ref ref, *ref_p;
7530 external_ref **slot;
7532 ref.type = die;
7533 slot = map.find_slot (&ref, INSERT);
7534 if (*slot != HTAB_EMPTY_ENTRY)
7535 return *slot;
7537 ref_p = XCNEW (struct external_ref);
7538 ref_p->type = die;
7539 *slot = ref_p;
7540 return ref_p;
7543 /* Subroutine of optimize_external_refs, below.
7545 If we see a type skeleton, record it as our stub. If we see external
7546 references, remember how many we've seen. */
7548 static void
7549 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7551 dw_die_ref c;
7552 dw_attr_ref a;
7553 unsigned ix;
7554 struct external_ref *ref_p;
7556 if (is_type_die (die)
7557 && (c = get_AT_ref (die, DW_AT_signature)))
7559 /* This is a local skeleton; use it for local references. */
7560 ref_p = lookup_external_ref (map, c);
7561 ref_p->stub = die;
7564 /* Scan the DIE references, and remember any that refer to DIEs from
7565 other CUs (i.e. those which are not marked). */
7566 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7567 if (AT_class (a) == dw_val_class_die_ref
7568 && (c = AT_ref (a))->die_mark == 0
7569 && is_type_die (c))
7571 ref_p = lookup_external_ref (map, c);
7572 ref_p->n_refs++;
7575 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7578 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7579 points to an external_ref, DATA is the CU we're processing. If we don't
7580 already have a local stub, and we have multiple refs, build a stub. */
7583 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7585 struct external_ref *ref_p = *slot;
7587 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7589 /* We have multiple references to this type, so build a small stub.
7590 Both of these forms are a bit dodgy from the perspective of the
7591 DWARF standard, since technically they should have names. */
7592 dw_die_ref cu = data;
7593 dw_die_ref type = ref_p->type;
7594 dw_die_ref stub = NULL;
7596 if (type->comdat_type_p)
7598 /* If we refer to this type via sig8, use AT_signature. */
7599 stub = new_die (type->die_tag, cu, NULL_TREE);
7600 add_AT_die_ref (stub, DW_AT_signature, type);
7602 else
7604 /* Otherwise, use a typedef with no name. */
7605 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7606 add_AT_die_ref (stub, DW_AT_type, type);
7609 stub->die_mark++;
7610 ref_p->stub = stub;
7612 return 1;
7615 /* DIE is a unit; look through all the DIE references to see if there are
7616 any external references to types, and if so, create local stubs for
7617 them which will be applied in build_abbrev_table. This is useful because
7618 references to local DIEs are smaller. */
7620 static external_ref_hash_type
7621 optimize_external_refs (dw_die_ref die)
7623 external_ref_hash_type map;
7624 map.create (10);
7625 optimize_external_refs_1 (die, map);
7626 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7627 return map;
7630 /* The format of each DIE (and its attribute value pairs) is encoded in an
7631 abbreviation table. This routine builds the abbreviation table and assigns
7632 a unique abbreviation id for each abbreviation entry. The children of each
7633 die are visited recursively. */
7635 static void
7636 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7638 unsigned long abbrev_id;
7639 unsigned int n_alloc;
7640 dw_die_ref c;
7641 dw_attr_ref a;
7642 unsigned ix;
7644 /* Scan the DIE references, and replace any that refer to
7645 DIEs from other CUs (i.e. those which are not marked) with
7646 the local stubs we built in optimize_external_refs. */
7647 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7648 if (AT_class (a) == dw_val_class_die_ref
7649 && (c = AT_ref (a))->die_mark == 0)
7651 struct external_ref *ref_p;
7652 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7654 ref_p = lookup_external_ref (extern_map, c);
7655 if (ref_p->stub && ref_p->stub != die)
7656 change_AT_die_ref (a, ref_p->stub);
7657 else
7658 /* We aren't changing this reference, so mark it external. */
7659 set_AT_ref_external (a, 1);
7662 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7664 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7665 dw_attr_ref die_a, abbrev_a;
7666 unsigned ix;
7667 bool ok = true;
7669 if (abbrev->die_tag != die->die_tag)
7670 continue;
7671 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7672 continue;
7674 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7675 continue;
7677 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7679 abbrev_a = &(*abbrev->die_attr)[ix];
7680 if ((abbrev_a->dw_attr != die_a->dw_attr)
7681 || (value_format (abbrev_a) != value_format (die_a)))
7683 ok = false;
7684 break;
7687 if (ok)
7688 break;
7691 if (abbrev_id >= abbrev_die_table_in_use)
7693 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7695 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7696 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7697 n_alloc);
7699 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7700 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7701 abbrev_die_table_allocated = n_alloc;
7704 ++abbrev_die_table_in_use;
7705 abbrev_die_table[abbrev_id] = die;
7708 die->die_abbrev = abbrev_id;
7709 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7712 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7714 static int
7715 constant_size (unsigned HOST_WIDE_INT value)
7717 int log;
7719 if (value == 0)
7720 log = 0;
7721 else
7722 log = floor_log2 (value);
7724 log = log / 8;
7725 log = 1 << (floor_log2 (log) + 1);
7727 return log;
7730 /* Return the size of a DIE as it is represented in the
7731 .debug_info section. */
7733 static unsigned long
7734 size_of_die (dw_die_ref die)
7736 unsigned long size = 0;
7737 dw_attr_ref a;
7738 unsigned ix;
7739 enum dwarf_form form;
7741 size += size_of_uleb128 (die->die_abbrev);
7742 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7744 switch (AT_class (a))
7746 case dw_val_class_addr:
7747 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7749 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7750 size += size_of_uleb128 (AT_index (a));
7752 else
7753 size += DWARF2_ADDR_SIZE;
7754 break;
7755 case dw_val_class_offset:
7756 size += DWARF_OFFSET_SIZE;
7757 break;
7758 case dw_val_class_loc:
7760 unsigned long lsize = size_of_locs (AT_loc (a));
7762 /* Block length. */
7763 if (dwarf_version >= 4)
7764 size += size_of_uleb128 (lsize);
7765 else
7766 size += constant_size (lsize);
7767 size += lsize;
7769 break;
7770 case dw_val_class_loc_list:
7771 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7773 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7774 size += size_of_uleb128 (AT_index (a));
7776 else
7777 size += DWARF_OFFSET_SIZE;
7778 break;
7779 case dw_val_class_range_list:
7780 size += DWARF_OFFSET_SIZE;
7781 break;
7782 case dw_val_class_const:
7783 size += size_of_sleb128 (AT_int (a));
7784 break;
7785 case dw_val_class_unsigned_const:
7787 int csize = constant_size (AT_unsigned (a));
7788 if (dwarf_version == 3
7789 && a->dw_attr == DW_AT_data_member_location
7790 && csize >= 4)
7791 size += size_of_uleb128 (AT_unsigned (a));
7792 else
7793 size += csize;
7795 break;
7796 case dw_val_class_const_double:
7797 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7798 if (HOST_BITS_PER_WIDE_INT >= 64)
7799 size++; /* block */
7800 break;
7801 case dw_val_class_vec:
7802 size += constant_size (a->dw_attr_val.v.val_vec.length
7803 * a->dw_attr_val.v.val_vec.elt_size)
7804 + a->dw_attr_val.v.val_vec.length
7805 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7806 break;
7807 case dw_val_class_flag:
7808 if (dwarf_version >= 4)
7809 /* Currently all add_AT_flag calls pass in 1 as last argument,
7810 so DW_FORM_flag_present can be used. If that ever changes,
7811 we'll need to use DW_FORM_flag and have some optimization
7812 in build_abbrev_table that will change those to
7813 DW_FORM_flag_present if it is set to 1 in all DIEs using
7814 the same abbrev entry. */
7815 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7816 else
7817 size += 1;
7818 break;
7819 case dw_val_class_die_ref:
7820 if (AT_ref_external (a))
7822 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7823 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7824 is sized by target address length, whereas in DWARF3
7825 it's always sized as an offset. */
7826 if (use_debug_types)
7827 size += DWARF_TYPE_SIGNATURE_SIZE;
7828 else if (dwarf_version == 2)
7829 size += DWARF2_ADDR_SIZE;
7830 else
7831 size += DWARF_OFFSET_SIZE;
7833 else
7834 size += DWARF_OFFSET_SIZE;
7835 break;
7836 case dw_val_class_fde_ref:
7837 size += DWARF_OFFSET_SIZE;
7838 break;
7839 case dw_val_class_lbl_id:
7840 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7842 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7843 size += size_of_uleb128 (AT_index (a));
7845 else
7846 size += DWARF2_ADDR_SIZE;
7847 break;
7848 case dw_val_class_lineptr:
7849 case dw_val_class_macptr:
7850 size += DWARF_OFFSET_SIZE;
7851 break;
7852 case dw_val_class_str:
7853 form = AT_string_form (a);
7854 if (form == DW_FORM_strp)
7855 size += DWARF_OFFSET_SIZE;
7856 else if (form == DW_FORM_GNU_str_index)
7857 size += size_of_uleb128 (AT_index (a));
7858 else
7859 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7860 break;
7861 case dw_val_class_file:
7862 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7863 break;
7864 case dw_val_class_data8:
7865 size += 8;
7866 break;
7867 case dw_val_class_vms_delta:
7868 size += DWARF_OFFSET_SIZE;
7869 break;
7870 case dw_val_class_high_pc:
7871 size += DWARF2_ADDR_SIZE;
7872 break;
7873 default:
7874 gcc_unreachable ();
7878 return size;
7881 /* Size the debugging information associated with a given DIE. Visits the
7882 DIE's children recursively. Updates the global variable next_die_offset, on
7883 each time through. Uses the current value of next_die_offset to update the
7884 die_offset field in each DIE. */
7886 static void
7887 calc_die_sizes (dw_die_ref die)
7889 dw_die_ref c;
7891 gcc_assert (die->die_offset == 0
7892 || (unsigned long int) die->die_offset == next_die_offset);
7893 die->die_offset = next_die_offset;
7894 next_die_offset += size_of_die (die);
7896 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7898 if (die->die_child != NULL)
7899 /* Count the null byte used to terminate sibling lists. */
7900 next_die_offset += 1;
7903 /* Size just the base type children at the start of the CU.
7904 This is needed because build_abbrev needs to size locs
7905 and sizing of type based stack ops needs to know die_offset
7906 values for the base types. */
7908 static void
7909 calc_base_type_die_sizes (void)
7911 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7912 unsigned int i;
7913 dw_die_ref base_type;
7914 #if ENABLE_ASSERT_CHECKING
7915 dw_die_ref prev = comp_unit_die ()->die_child;
7916 #endif
7918 die_offset += size_of_die (comp_unit_die ());
7919 for (i = 0; base_types.iterate (i, &base_type); i++)
7921 #if ENABLE_ASSERT_CHECKING
7922 gcc_assert (base_type->die_offset == 0
7923 && prev->die_sib == base_type
7924 && base_type->die_child == NULL
7925 && base_type->die_abbrev);
7926 prev = base_type;
7927 #endif
7928 base_type->die_offset = die_offset;
7929 die_offset += size_of_die (base_type);
7933 /* Set the marks for a die and its children. We do this so
7934 that we know whether or not a reference needs to use FORM_ref_addr; only
7935 DIEs in the same CU will be marked. We used to clear out the offset
7936 and use that as the flag, but ran into ordering problems. */
7938 static void
7939 mark_dies (dw_die_ref die)
7941 dw_die_ref c;
7943 gcc_assert (!die->die_mark);
7945 die->die_mark = 1;
7946 FOR_EACH_CHILD (die, c, mark_dies (c));
7949 /* Clear the marks for a die and its children. */
7951 static void
7952 unmark_dies (dw_die_ref die)
7954 dw_die_ref c;
7956 if (! use_debug_types)
7957 gcc_assert (die->die_mark);
7959 die->die_mark = 0;
7960 FOR_EACH_CHILD (die, c, unmark_dies (c));
7963 /* Clear the marks for a die, its children and referred dies. */
7965 static void
7966 unmark_all_dies (dw_die_ref die)
7968 dw_die_ref c;
7969 dw_attr_ref a;
7970 unsigned ix;
7972 if (!die->die_mark)
7973 return;
7974 die->die_mark = 0;
7976 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7978 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7979 if (AT_class (a) == dw_val_class_die_ref)
7980 unmark_all_dies (AT_ref (a));
7983 /* Calculate if the entry should appear in the final output file. It may be
7984 from a pruned a type. */
7986 static bool
7987 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7989 /* By limiting gnu pubnames to definitions only, gold can generate a
7990 gdb index without entries for declarations, which don't include
7991 enough information to be useful. */
7992 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7993 return false;
7995 if (table == pubname_table)
7997 /* Enumerator names are part of the pubname table, but the
7998 parent DW_TAG_enumeration_type die may have been pruned.
7999 Don't output them if that is the case. */
8000 if (p->die->die_tag == DW_TAG_enumerator &&
8001 (p->die->die_parent == NULL
8002 || !p->die->die_parent->die_perennial_p))
8003 return false;
8005 /* Everything else in the pubname table is included. */
8006 return true;
8009 /* The pubtypes table shouldn't include types that have been
8010 pruned. */
8011 return (p->die->die_offset != 0
8012 || !flag_eliminate_unused_debug_types);
8015 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8016 generated for the compilation unit. */
8018 static unsigned long
8019 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8021 unsigned long size;
8022 unsigned i;
8023 pubname_ref p;
8024 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8026 size = DWARF_PUBNAMES_HEADER_SIZE;
8027 FOR_EACH_VEC_ELT (*names, i, p)
8028 if (include_pubname_in_output (names, p))
8029 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8031 size += DWARF_OFFSET_SIZE;
8032 return size;
8035 /* Return the size of the information in the .debug_aranges section. */
8037 static unsigned long
8038 size_of_aranges (void)
8040 unsigned long size;
8042 size = DWARF_ARANGES_HEADER_SIZE;
8044 /* Count the address/length pair for this compilation unit. */
8045 if (text_section_used)
8046 size += 2 * DWARF2_ADDR_SIZE;
8047 if (cold_text_section_used)
8048 size += 2 * DWARF2_ADDR_SIZE;
8049 if (have_multiple_function_sections)
8051 unsigned fde_idx;
8052 dw_fde_ref fde;
8054 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8056 if (DECL_IGNORED_P (fde->decl))
8057 continue;
8058 if (!fde->in_std_section)
8059 size += 2 * DWARF2_ADDR_SIZE;
8060 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8061 size += 2 * DWARF2_ADDR_SIZE;
8065 /* Count the two zero words used to terminated the address range table. */
8066 size += 2 * DWARF2_ADDR_SIZE;
8067 return size;
8070 /* Select the encoding of an attribute value. */
8072 static enum dwarf_form
8073 value_format (dw_attr_ref a)
8075 switch (AT_class (a))
8077 case dw_val_class_addr:
8078 /* Only very few attributes allow DW_FORM_addr. */
8079 switch (a->dw_attr)
8081 case DW_AT_low_pc:
8082 case DW_AT_high_pc:
8083 case DW_AT_entry_pc:
8084 case DW_AT_trampoline:
8085 return (AT_index (a) == NOT_INDEXED
8086 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8087 default:
8088 break;
8090 switch (DWARF2_ADDR_SIZE)
8092 case 1:
8093 return DW_FORM_data1;
8094 case 2:
8095 return DW_FORM_data2;
8096 case 4:
8097 return DW_FORM_data4;
8098 case 8:
8099 return DW_FORM_data8;
8100 default:
8101 gcc_unreachable ();
8103 case dw_val_class_range_list:
8104 case dw_val_class_loc_list:
8105 if (dwarf_version >= 4)
8106 return DW_FORM_sec_offset;
8107 /* FALLTHRU */
8108 case dw_val_class_vms_delta:
8109 case dw_val_class_offset:
8110 switch (DWARF_OFFSET_SIZE)
8112 case 4:
8113 return DW_FORM_data4;
8114 case 8:
8115 return DW_FORM_data8;
8116 default:
8117 gcc_unreachable ();
8119 case dw_val_class_loc:
8120 if (dwarf_version >= 4)
8121 return DW_FORM_exprloc;
8122 switch (constant_size (size_of_locs (AT_loc (a))))
8124 case 1:
8125 return DW_FORM_block1;
8126 case 2:
8127 return DW_FORM_block2;
8128 case 4:
8129 return DW_FORM_block4;
8130 default:
8131 gcc_unreachable ();
8133 case dw_val_class_const:
8134 return DW_FORM_sdata;
8135 case dw_val_class_unsigned_const:
8136 switch (constant_size (AT_unsigned (a)))
8138 case 1:
8139 return DW_FORM_data1;
8140 case 2:
8141 return DW_FORM_data2;
8142 case 4:
8143 /* In DWARF3 DW_AT_data_member_location with
8144 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8145 constant, so we need to use DW_FORM_udata if we need
8146 a large constant. */
8147 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8148 return DW_FORM_udata;
8149 return DW_FORM_data4;
8150 case 8:
8151 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8152 return DW_FORM_udata;
8153 return DW_FORM_data8;
8154 default:
8155 gcc_unreachable ();
8157 case dw_val_class_const_double:
8158 switch (HOST_BITS_PER_WIDE_INT)
8160 case 8:
8161 return DW_FORM_data2;
8162 case 16:
8163 return DW_FORM_data4;
8164 case 32:
8165 return DW_FORM_data8;
8166 case 64:
8167 default:
8168 return DW_FORM_block1;
8170 case dw_val_class_vec:
8171 switch (constant_size (a->dw_attr_val.v.val_vec.length
8172 * a->dw_attr_val.v.val_vec.elt_size))
8174 case 1:
8175 return DW_FORM_block1;
8176 case 2:
8177 return DW_FORM_block2;
8178 case 4:
8179 return DW_FORM_block4;
8180 default:
8181 gcc_unreachable ();
8183 case dw_val_class_flag:
8184 if (dwarf_version >= 4)
8186 /* Currently all add_AT_flag calls pass in 1 as last argument,
8187 so DW_FORM_flag_present can be used. If that ever changes,
8188 we'll need to use DW_FORM_flag and have some optimization
8189 in build_abbrev_table that will change those to
8190 DW_FORM_flag_present if it is set to 1 in all DIEs using
8191 the same abbrev entry. */
8192 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8193 return DW_FORM_flag_present;
8195 return DW_FORM_flag;
8196 case dw_val_class_die_ref:
8197 if (AT_ref_external (a))
8198 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8199 else
8200 return DW_FORM_ref;
8201 case dw_val_class_fde_ref:
8202 return DW_FORM_data;
8203 case dw_val_class_lbl_id:
8204 return (AT_index (a) == NOT_INDEXED
8205 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8206 case dw_val_class_lineptr:
8207 case dw_val_class_macptr:
8208 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8209 case dw_val_class_str:
8210 return AT_string_form (a);
8211 case dw_val_class_file:
8212 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8214 case 1:
8215 return DW_FORM_data1;
8216 case 2:
8217 return DW_FORM_data2;
8218 case 4:
8219 return DW_FORM_data4;
8220 default:
8221 gcc_unreachable ();
8224 case dw_val_class_data8:
8225 return DW_FORM_data8;
8227 case dw_val_class_high_pc:
8228 switch (DWARF2_ADDR_SIZE)
8230 case 1:
8231 return DW_FORM_data1;
8232 case 2:
8233 return DW_FORM_data2;
8234 case 4:
8235 return DW_FORM_data4;
8236 case 8:
8237 return DW_FORM_data8;
8238 default:
8239 gcc_unreachable ();
8242 default:
8243 gcc_unreachable ();
8247 /* Output the encoding of an attribute value. */
8249 static void
8250 output_value_format (dw_attr_ref a)
8252 enum dwarf_form form = value_format (a);
8254 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8257 /* Given a die and id, produce the appropriate abbreviations. */
8259 static void
8260 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8262 unsigned ix;
8263 dw_attr_ref a_attr;
8265 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8266 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8267 dwarf_tag_name (abbrev->die_tag));
8269 if (abbrev->die_child != NULL)
8270 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8271 else
8272 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8274 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8276 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8277 dwarf_attr_name (a_attr->dw_attr));
8278 output_value_format (a_attr);
8281 dw2_asm_output_data (1, 0, NULL);
8282 dw2_asm_output_data (1, 0, NULL);
8286 /* Output the .debug_abbrev section which defines the DIE abbreviation
8287 table. */
8289 static void
8290 output_abbrev_section (void)
8292 unsigned long abbrev_id;
8294 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8295 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8297 /* Terminate the table. */
8298 dw2_asm_output_data (1, 0, NULL);
8301 /* Output a symbol we can use to refer to this DIE from another CU. */
8303 static inline void
8304 output_die_symbol (dw_die_ref die)
8306 const char *sym = die->die_id.die_symbol;
8308 gcc_assert (!die->comdat_type_p);
8310 if (sym == 0)
8311 return;
8313 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8314 /* We make these global, not weak; if the target doesn't support
8315 .linkonce, it doesn't support combining the sections, so debugging
8316 will break. */
8317 targetm.asm_out.globalize_label (asm_out_file, sym);
8319 ASM_OUTPUT_LABEL (asm_out_file, sym);
8322 /* Return a new location list, given the begin and end range, and the
8323 expression. */
8325 static inline dw_loc_list_ref
8326 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8327 const char *section)
8329 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8331 retlist->begin = begin;
8332 retlist->begin_entry = NULL;
8333 retlist->end = end;
8334 retlist->expr = expr;
8335 retlist->section = section;
8337 return retlist;
8340 /* Generate a new internal symbol for this location list node, if it
8341 hasn't got one yet. */
8343 static inline void
8344 gen_llsym (dw_loc_list_ref list)
8346 gcc_assert (!list->ll_symbol);
8347 list->ll_symbol = gen_internal_sym ("LLST");
8350 /* Output the location list given to us. */
8352 static void
8353 output_loc_list (dw_loc_list_ref list_head)
8355 dw_loc_list_ref curr = list_head;
8357 if (list_head->emitted)
8358 return;
8359 list_head->emitted = true;
8361 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8363 /* Walk the location list, and output each range + expression. */
8364 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8366 unsigned long size;
8367 /* Don't output an entry that starts and ends at the same address. */
8368 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8369 continue;
8370 size = size_of_locs (curr->expr);
8371 /* If the expression is too large, drop it on the floor. We could
8372 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8373 in the expression, but >= 64KB expressions for a single value
8374 in a single range are unlikely very useful. */
8375 if (size > 0xffff)
8376 continue;
8377 if (dwarf_split_debug_info)
8379 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8380 "Location list start/length entry (%s)",
8381 list_head->ll_symbol);
8382 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8383 "Location list range start index (%s)",
8384 curr->begin);
8385 /* The length field is 4 bytes. If we ever need to support
8386 an 8-byte length, we can add a new DW_LLE code or fall back
8387 to DW_LLE_GNU_start_end_entry. */
8388 dw2_asm_output_delta (4, curr->end, curr->begin,
8389 "Location list range length (%s)",
8390 list_head->ll_symbol);
8392 else if (!have_multiple_function_sections)
8394 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8395 "Location list begin address (%s)",
8396 list_head->ll_symbol);
8397 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8398 "Location list end address (%s)",
8399 list_head->ll_symbol);
8401 else
8403 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8404 "Location list begin address (%s)",
8405 list_head->ll_symbol);
8406 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8407 "Location list end address (%s)",
8408 list_head->ll_symbol);
8411 /* Output the block length for this list of location operations. */
8412 gcc_assert (size <= 0xffff);
8413 dw2_asm_output_data (2, size, "%s", "Location expression size");
8415 output_loc_sequence (curr->expr, -1);
8418 if (dwarf_split_debug_info)
8419 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8420 "Location list terminator (%s)",
8421 list_head->ll_symbol);
8422 else
8424 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8425 "Location list terminator begin (%s)",
8426 list_head->ll_symbol);
8427 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8428 "Location list terminator end (%s)",
8429 list_head->ll_symbol);
8433 /* Output a range_list offset into the debug_range section. Emit a
8434 relocated reference if val_entry is NULL, otherwise, emit an
8435 indirect reference. */
8437 static void
8438 output_range_list_offset (dw_attr_ref a)
8440 const char *name = dwarf_attr_name (a->dw_attr);
8442 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8444 char *p = strchr (ranges_section_label, '\0');
8445 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8446 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8447 debug_ranges_section, "%s", name);
8448 *p = '\0';
8450 else
8451 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8452 "%s (offset from %s)", name, ranges_section_label);
8455 /* Output the offset into the debug_loc section. */
8457 static void
8458 output_loc_list_offset (dw_attr_ref a)
8460 char *sym = AT_loc_list (a)->ll_symbol;
8462 gcc_assert (sym);
8463 if (dwarf_split_debug_info)
8464 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8465 "%s", dwarf_attr_name (a->dw_attr));
8466 else
8467 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8468 "%s", dwarf_attr_name (a->dw_attr));
8471 /* Output an attribute's index or value appropriately. */
8473 static void
8474 output_attr_index_or_value (dw_attr_ref a)
8476 const char *name = dwarf_attr_name (a->dw_attr);
8478 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8480 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8481 return;
8483 switch (AT_class (a))
8485 case dw_val_class_addr:
8486 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8487 break;
8488 case dw_val_class_high_pc:
8489 case dw_val_class_lbl_id:
8490 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8491 break;
8492 case dw_val_class_loc_list:
8493 output_loc_list_offset (a);
8494 break;
8495 default:
8496 gcc_unreachable ();
8500 /* Output a type signature. */
8502 static inline void
8503 output_signature (const char *sig, const char *name)
8505 int i;
8507 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8508 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8511 /* Output the DIE and its attributes. Called recursively to generate
8512 the definitions of each child DIE. */
8514 static void
8515 output_die (dw_die_ref die)
8517 dw_attr_ref a;
8518 dw_die_ref c;
8519 unsigned long size;
8520 unsigned ix;
8522 /* If someone in another CU might refer to us, set up a symbol for
8523 them to point to. */
8524 if (! die->comdat_type_p && die->die_id.die_symbol)
8525 output_die_symbol (die);
8527 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8528 (unsigned long)die->die_offset,
8529 dwarf_tag_name (die->die_tag));
8531 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8533 const char *name = dwarf_attr_name (a->dw_attr);
8535 switch (AT_class (a))
8537 case dw_val_class_addr:
8538 output_attr_index_or_value (a);
8539 break;
8541 case dw_val_class_offset:
8542 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8543 "%s", name);
8544 break;
8546 case dw_val_class_range_list:
8547 output_range_list_offset (a);
8548 break;
8550 case dw_val_class_loc:
8551 size = size_of_locs (AT_loc (a));
8553 /* Output the block length for this list of location operations. */
8554 if (dwarf_version >= 4)
8555 dw2_asm_output_data_uleb128 (size, "%s", name);
8556 else
8557 dw2_asm_output_data (constant_size (size), size, "%s", name);
8559 output_loc_sequence (AT_loc (a), -1);
8560 break;
8562 case dw_val_class_const:
8563 /* ??? It would be slightly more efficient to use a scheme like is
8564 used for unsigned constants below, but gdb 4.x does not sign
8565 extend. Gdb 5.x does sign extend. */
8566 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8567 break;
8569 case dw_val_class_unsigned_const:
8571 int csize = constant_size (AT_unsigned (a));
8572 if (dwarf_version == 3
8573 && a->dw_attr == DW_AT_data_member_location
8574 && csize >= 4)
8575 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8576 else
8577 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8579 break;
8581 case dw_val_class_const_double:
8583 unsigned HOST_WIDE_INT first, second;
8585 if (HOST_BITS_PER_WIDE_INT >= 64)
8586 dw2_asm_output_data (1,
8587 HOST_BITS_PER_DOUBLE_INT
8588 / HOST_BITS_PER_CHAR,
8589 NULL);
8591 if (WORDS_BIG_ENDIAN)
8593 first = a->dw_attr_val.v.val_double.high;
8594 second = a->dw_attr_val.v.val_double.low;
8596 else
8598 first = a->dw_attr_val.v.val_double.low;
8599 second = a->dw_attr_val.v.val_double.high;
8602 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8603 first, "%s", name);
8604 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8605 second, NULL);
8607 break;
8609 case dw_val_class_vec:
8611 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8612 unsigned int len = a->dw_attr_val.v.val_vec.length;
8613 unsigned int i;
8614 unsigned char *p;
8616 dw2_asm_output_data (constant_size (len * elt_size),
8617 len * elt_size, "%s", name);
8618 if (elt_size > sizeof (HOST_WIDE_INT))
8620 elt_size /= 2;
8621 len *= 2;
8623 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8624 i < len;
8625 i++, p += elt_size)
8626 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8627 "fp or vector constant word %u", i);
8628 break;
8631 case dw_val_class_flag:
8632 if (dwarf_version >= 4)
8634 /* Currently all add_AT_flag calls pass in 1 as last argument,
8635 so DW_FORM_flag_present can be used. If that ever changes,
8636 we'll need to use DW_FORM_flag and have some optimization
8637 in build_abbrev_table that will change those to
8638 DW_FORM_flag_present if it is set to 1 in all DIEs using
8639 the same abbrev entry. */
8640 gcc_assert (AT_flag (a) == 1);
8641 if (flag_debug_asm)
8642 fprintf (asm_out_file, "\t\t\t%s %s\n",
8643 ASM_COMMENT_START, name);
8644 break;
8646 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8647 break;
8649 case dw_val_class_loc_list:
8650 output_attr_index_or_value (a);
8651 break;
8653 case dw_val_class_die_ref:
8654 if (AT_ref_external (a))
8656 if (AT_ref (a)->comdat_type_p)
8658 comdat_type_node_ref type_node =
8659 AT_ref (a)->die_id.die_type_node;
8661 gcc_assert (type_node);
8662 output_signature (type_node->signature, name);
8664 else
8666 const char *sym = AT_ref (a)->die_id.die_symbol;
8667 int size;
8669 gcc_assert (sym);
8670 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8671 length, whereas in DWARF3 it's always sized as an
8672 offset. */
8673 if (dwarf_version == 2)
8674 size = DWARF2_ADDR_SIZE;
8675 else
8676 size = DWARF_OFFSET_SIZE;
8677 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8678 name);
8681 else
8683 gcc_assert (AT_ref (a)->die_offset);
8684 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8685 "%s", name);
8687 break;
8689 case dw_val_class_fde_ref:
8691 char l1[20];
8693 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8694 a->dw_attr_val.v.val_fde_index * 2);
8695 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8696 "%s", name);
8698 break;
8700 case dw_val_class_vms_delta:
8701 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8702 AT_vms_delta2 (a), AT_vms_delta1 (a),
8703 "%s", name);
8704 break;
8706 case dw_val_class_lbl_id:
8707 output_attr_index_or_value (a);
8708 break;
8710 case dw_val_class_lineptr:
8711 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8712 debug_line_section, "%s", name);
8713 break;
8715 case dw_val_class_macptr:
8716 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8717 debug_macinfo_section, "%s", name);
8718 break;
8720 case dw_val_class_str:
8721 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8722 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8723 a->dw_attr_val.v.val_str->label,
8724 debug_str_section,
8725 "%s: \"%s\"", name, AT_string (a));
8726 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8727 dw2_asm_output_data_uleb128 (AT_index (a),
8728 "%s: \"%s\"", name, AT_string (a));
8729 else
8730 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8731 break;
8733 case dw_val_class_file:
8735 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8737 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8738 a->dw_attr_val.v.val_file->filename);
8739 break;
8742 case dw_val_class_data8:
8744 int i;
8746 for (i = 0; i < 8; i++)
8747 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8748 i == 0 ? "%s" : NULL, name);
8749 break;
8752 case dw_val_class_high_pc:
8753 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8754 get_AT_low_pc (die), "DW_AT_high_pc");
8755 break;
8757 default:
8758 gcc_unreachable ();
8762 FOR_EACH_CHILD (die, c, output_die (c));
8764 /* Add null byte to terminate sibling list. */
8765 if (die->die_child != NULL)
8766 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8767 (unsigned long) die->die_offset);
8770 /* Output the compilation unit that appears at the beginning of the
8771 .debug_info section, and precedes the DIE descriptions. */
8773 static void
8774 output_compilation_unit_header (void)
8776 int ver = dwarf_version;
8778 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8779 dw2_asm_output_data (4, 0xffffffff,
8780 "Initial length escape value indicating 64-bit DWARF extension");
8781 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8782 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8783 "Length of Compilation Unit Info");
8784 dw2_asm_output_data (2, ver, "DWARF version number");
8785 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8786 debug_abbrev_section,
8787 "Offset Into Abbrev. Section");
8788 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8791 /* Output the compilation unit DIE and its children. */
8793 static void
8794 output_comp_unit (dw_die_ref die, int output_if_empty)
8796 const char *secname, *oldsym;
8797 char *tmp;
8798 external_ref_hash_type extern_map;
8800 /* Unless we are outputting main CU, we may throw away empty ones. */
8801 if (!output_if_empty && die->die_child == NULL)
8802 return;
8804 /* Even if there are no children of this DIE, we must output the information
8805 about the compilation unit. Otherwise, on an empty translation unit, we
8806 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8807 will then complain when examining the file. First mark all the DIEs in
8808 this CU so we know which get local refs. */
8809 mark_dies (die);
8811 extern_map = optimize_external_refs (die);
8813 build_abbrev_table (die, extern_map);
8815 extern_map.dispose ();
8817 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8818 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8819 calc_die_sizes (die);
8821 oldsym = die->die_id.die_symbol;
8822 if (oldsym)
8824 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8826 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8827 secname = tmp;
8828 die->die_id.die_symbol = NULL;
8829 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8831 else
8833 switch_to_section (debug_info_section);
8834 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8835 info_section_emitted = true;
8838 /* Output debugging information. */
8839 output_compilation_unit_header ();
8840 output_die (die);
8842 /* Leave the marks on the main CU, so we can check them in
8843 output_pubnames. */
8844 if (oldsym)
8846 unmark_dies (die);
8847 die->die_id.die_symbol = oldsym;
8851 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8852 and .debug_pubtypes. This is configured per-target, but can be
8853 overridden by the -gpubnames or -gno-pubnames options. */
8855 static inline bool
8856 want_pubnames (void)
8858 if (debug_info_level <= DINFO_LEVEL_TERSE)
8859 return false;
8860 if (debug_generate_pub_sections != -1)
8861 return debug_generate_pub_sections;
8862 return targetm.want_debug_pub_sections;
8865 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8867 static void
8868 add_AT_pubnames (dw_die_ref die)
8870 if (want_pubnames ())
8871 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8874 /* Add a string attribute value to a skeleton DIE. */
8876 static inline void
8877 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8878 const char *str)
8880 dw_attr_node attr;
8881 struct indirect_string_node *node;
8883 if (! skeleton_debug_str_hash)
8884 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8885 debug_str_eq, NULL);
8887 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8888 find_string_form (node);
8889 if (node->form == DW_FORM_GNU_str_index)
8890 node->form = DW_FORM_strp;
8892 attr.dw_attr = attr_kind;
8893 attr.dw_attr_val.val_class = dw_val_class_str;
8894 attr.dw_attr_val.val_entry = NULL;
8895 attr.dw_attr_val.v.val_str = node;
8896 add_dwarf_attr (die, &attr);
8899 /* Helper function to generate top-level dies for skeleton debug_info and
8900 debug_types. */
8902 static void
8903 add_top_level_skeleton_die_attrs (dw_die_ref die)
8905 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8906 const char *comp_dir = comp_dir_string ();
8908 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8909 if (comp_dir != NULL)
8910 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8911 add_AT_pubnames (die);
8912 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8915 /* Return the single type-unit die for skeleton type units. */
8917 static dw_die_ref
8918 get_skeleton_type_unit (void)
8920 /* For dwarf_split_debug_sections with use_type info, all type units in the
8921 skeleton sections have identical dies (but different headers). This
8922 single die will be output many times. */
8924 static dw_die_ref skeleton_type_unit = NULL;
8926 if (skeleton_type_unit == NULL)
8928 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8929 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8930 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8932 return skeleton_type_unit;
8935 /* Output skeleton debug sections that point to the dwo file. */
8937 static void
8938 output_skeleton_debug_sections (dw_die_ref comp_unit)
8940 /* These attributes will be found in the full debug_info section. */
8941 remove_AT (comp_unit, DW_AT_producer);
8942 remove_AT (comp_unit, DW_AT_language);
8944 switch_to_section (debug_skeleton_info_section);
8945 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8947 /* Produce the skeleton compilation-unit header. This one differs enough from
8948 a normal CU header that it's better not to call output_compilation_unit
8949 header. */
8950 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8951 dw2_asm_output_data (4, 0xffffffff,
8952 "Initial length escape value indicating 64-bit DWARF extension");
8954 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8955 DWARF_COMPILE_UNIT_HEADER_SIZE
8956 - DWARF_INITIAL_LENGTH_SIZE
8957 + size_of_die (comp_unit),
8958 "Length of Compilation Unit Info");
8959 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8960 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8961 debug_abbrev_section,
8962 "Offset Into Abbrev. Section");
8963 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8965 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8966 output_die (comp_unit);
8968 /* Build the skeleton debug_abbrev section. */
8969 switch_to_section (debug_skeleton_abbrev_section);
8970 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8972 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8973 if (use_debug_types)
8974 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8976 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8979 /* Output a comdat type unit DIE and its children. */
8981 static void
8982 output_comdat_type_unit (comdat_type_node *node)
8984 const char *secname;
8985 char *tmp;
8986 int i;
8987 #if defined (OBJECT_FORMAT_ELF)
8988 tree comdat_key;
8989 #endif
8990 external_ref_hash_type extern_map;
8992 /* First mark all the DIEs in this CU so we know which get local refs. */
8993 mark_dies (node->root_die);
8995 extern_map = optimize_external_refs (node->root_die);
8997 build_abbrev_table (node->root_die, extern_map);
8999 extern_map.dispose ();
9001 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9002 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9003 calc_die_sizes (node->root_die);
9005 #if defined (OBJECT_FORMAT_ELF)
9006 if (!dwarf_split_debug_info)
9007 secname = ".debug_types";
9008 else
9009 secname = ".debug_types.dwo";
9011 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9012 sprintf (tmp, "wt.");
9013 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9014 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9015 comdat_key = get_identifier (tmp);
9016 targetm.asm_out.named_section (secname,
9017 SECTION_DEBUG | SECTION_LINKONCE,
9018 comdat_key);
9019 #else
9020 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9021 sprintf (tmp, ".gnu.linkonce.wt.");
9022 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9023 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9024 secname = tmp;
9025 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9026 #endif
9028 /* Output debugging information. */
9029 output_compilation_unit_header ();
9030 output_signature (node->signature, "Type Signature");
9031 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9032 "Offset to Type DIE");
9033 output_die (node->root_die);
9035 unmark_dies (node->root_die);
9037 #if defined (OBJECT_FORMAT_ELF)
9038 if (dwarf_split_debug_info)
9040 /* Produce the skeleton type-unit header. */
9041 const char *secname = ".debug_types";
9043 targetm.asm_out.named_section (secname,
9044 SECTION_DEBUG | SECTION_LINKONCE,
9045 comdat_key);
9046 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9047 dw2_asm_output_data (4, 0xffffffff,
9048 "Initial length escape value indicating 64-bit DWARF extension");
9050 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9051 DWARF_COMPILE_UNIT_HEADER_SIZE
9052 - DWARF_INITIAL_LENGTH_SIZE
9053 + size_of_die (get_skeleton_type_unit ())
9054 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9055 "Length of Type Unit Info");
9056 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9057 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9058 debug_skeleton_abbrev_section_label,
9059 debug_abbrev_section,
9060 "Offset Into Abbrev. Section");
9061 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9062 output_signature (node->signature, "Type Signature");
9063 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9065 output_die (get_skeleton_type_unit ());
9067 #endif
9070 /* Return the DWARF2/3 pubname associated with a decl. */
9072 static const char *
9073 dwarf2_name (tree decl, int scope)
9075 if (DECL_NAMELESS (decl))
9076 return NULL;
9077 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9080 /* Add a new entry to .debug_pubnames if appropriate. */
9082 static void
9083 add_pubname_string (const char *str, dw_die_ref die)
9085 pubname_entry e;
9087 e.die = die;
9088 e.name = xstrdup (str);
9089 vec_safe_push (pubname_table, e);
9092 static void
9093 add_pubname (tree decl, dw_die_ref die)
9095 if (!want_pubnames ())
9096 return;
9098 /* Don't add items to the table when we expect that the consumer will have
9099 just read the enclosing die. For example, if the consumer is looking at a
9100 class_member, it will either be inside the class already, or will have just
9101 looked up the class to find the member. Either way, searching the class is
9102 faster than searching the index. */
9103 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9104 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9106 const char *name = dwarf2_name (decl, 1);
9108 if (name)
9109 add_pubname_string (name, die);
9113 /* Add an enumerator to the pubnames section. */
9115 static void
9116 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9118 pubname_entry e;
9120 gcc_assert (scope_name);
9121 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9122 e.die = die;
9123 vec_safe_push (pubname_table, e);
9126 /* Add a new entry to .debug_pubtypes if appropriate. */
9128 static void
9129 add_pubtype (tree decl, dw_die_ref die)
9131 pubname_entry e;
9133 if (!want_pubnames ())
9134 return;
9136 if ((TREE_PUBLIC (decl)
9137 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9138 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9140 tree scope = NULL;
9141 const char *scope_name = "";
9142 const char *sep = is_cxx () ? "::" : ".";
9143 const char *name;
9145 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9146 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9148 scope_name = lang_hooks.dwarf_name (scope, 1);
9149 if (scope_name != NULL && scope_name[0] != '\0')
9150 scope_name = concat (scope_name, sep, NULL);
9151 else
9152 scope_name = "";
9155 if (TYPE_P (decl))
9156 name = type_tag (decl);
9157 else
9158 name = lang_hooks.dwarf_name (decl, 1);
9160 /* If we don't have a name for the type, there's no point in adding
9161 it to the table. */
9162 if (name != NULL && name[0] != '\0')
9164 e.die = die;
9165 e.name = concat (scope_name, name, NULL);
9166 vec_safe_push (pubtype_table, e);
9169 /* Although it might be more consistent to add the pubinfo for the
9170 enumerators as their dies are created, they should only be added if the
9171 enum type meets the criteria above. So rather than re-check the parent
9172 enum type whenever an enumerator die is created, just output them all
9173 here. This isn't protected by the name conditional because anonymous
9174 enums don't have names. */
9175 if (die->die_tag == DW_TAG_enumeration_type)
9177 dw_die_ref c;
9179 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9184 /* Output a single entry in the pubnames table. */
9186 static void
9187 output_pubname (dw_offset die_offset, pubname_entry *entry)
9189 dw_die_ref die = entry->die;
9190 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9192 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9194 if (debug_generate_pub_sections == 2)
9196 /* This logic follows gdb's method for determining the value of the flag
9197 byte. */
9198 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9199 switch (die->die_tag)
9201 case DW_TAG_typedef:
9202 case DW_TAG_base_type:
9203 case DW_TAG_subrange_type:
9204 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9205 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9206 break;
9207 case DW_TAG_enumerator:
9208 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9209 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9210 if (!is_cxx () && !is_java ())
9211 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9212 break;
9213 case DW_TAG_subprogram:
9214 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9215 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9216 if (!is_ada ())
9217 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9218 break;
9219 case DW_TAG_constant:
9220 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9221 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9222 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9223 break;
9224 case DW_TAG_variable:
9225 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9226 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9227 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9228 break;
9229 case DW_TAG_namespace:
9230 case DW_TAG_imported_declaration:
9231 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9232 break;
9233 case DW_TAG_class_type:
9234 case DW_TAG_interface_type:
9235 case DW_TAG_structure_type:
9236 case DW_TAG_union_type:
9237 case DW_TAG_enumeration_type:
9238 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9239 if (!is_cxx () && !is_java ())
9240 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9241 break;
9242 default:
9243 /* An unusual tag. Leave the flag-byte empty. */
9244 break;
9246 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9247 "GDB-index flags");
9250 dw2_asm_output_nstring (entry->name, -1, "external name");
9254 /* Output the public names table used to speed up access to externally
9255 visible names; or the public types table used to find type definitions. */
9257 static void
9258 output_pubnames (vec<pubname_entry, va_gc> *names)
9260 unsigned i;
9261 unsigned long pubnames_length = size_of_pubnames (names);
9262 pubname_ref pub;
9264 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9265 dw2_asm_output_data (4, 0xffffffff,
9266 "Initial length escape value indicating 64-bit DWARF extension");
9267 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9269 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9270 dw2_asm_output_data (2, 2, "DWARF Version");
9272 if (dwarf_split_debug_info)
9273 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9274 debug_skeleton_info_section,
9275 "Offset of Compilation Unit Info");
9276 else
9277 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9278 debug_info_section,
9279 "Offset of Compilation Unit Info");
9280 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9281 "Compilation Unit Length");
9283 FOR_EACH_VEC_ELT (*names, i, pub)
9285 if (include_pubname_in_output (names, pub))
9287 dw_offset die_offset = pub->die->die_offset;
9289 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9290 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9291 gcc_assert (pub->die->die_mark);
9293 /* If we're putting types in their own .debug_types sections,
9294 the .debug_pubtypes table will still point to the compile
9295 unit (not the type unit), so we want to use the offset of
9296 the skeleton DIE (if there is one). */
9297 if (pub->die->comdat_type_p && names == pubtype_table)
9299 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9301 if (type_node != NULL)
9302 die_offset = (type_node->skeleton_die != NULL
9303 ? type_node->skeleton_die->die_offset
9304 : comp_unit_die ()->die_offset);
9307 output_pubname (die_offset, pub);
9311 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9314 /* Output public names and types tables if necessary. */
9316 static void
9317 output_pubtables (void)
9319 if (!want_pubnames () || !info_section_emitted)
9320 return;
9322 switch_to_section (debug_pubnames_section);
9323 output_pubnames (pubname_table);
9324 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9325 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9326 simply won't look for the section. */
9327 switch_to_section (debug_pubtypes_section);
9328 output_pubnames (pubtype_table);
9332 /* Output the information that goes into the .debug_aranges table.
9333 Namely, define the beginning and ending address range of the
9334 text section generated for this compilation unit. */
9336 static void
9337 output_aranges (unsigned long aranges_length)
9339 unsigned i;
9341 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9342 dw2_asm_output_data (4, 0xffffffff,
9343 "Initial length escape value indicating 64-bit DWARF extension");
9344 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9345 "Length of Address Ranges Info");
9346 /* Version number for aranges is still 2, even in DWARF3. */
9347 dw2_asm_output_data (2, 2, "DWARF Version");
9348 if (dwarf_split_debug_info)
9349 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9350 debug_skeleton_info_section,
9351 "Offset of Compilation Unit Info");
9352 else
9353 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9354 debug_info_section,
9355 "Offset of Compilation Unit Info");
9356 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9357 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9359 /* We need to align to twice the pointer size here. */
9360 if (DWARF_ARANGES_PAD_SIZE)
9362 /* Pad using a 2 byte words so that padding is correct for any
9363 pointer size. */
9364 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9365 2 * DWARF2_ADDR_SIZE);
9366 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9367 dw2_asm_output_data (2, 0, NULL);
9370 /* It is necessary not to output these entries if the sections were
9371 not used; if the sections were not used, the length will be 0 and
9372 the address may end up as 0 if the section is discarded by ld
9373 --gc-sections, leaving an invalid (0, 0) entry that can be
9374 confused with the terminator. */
9375 if (text_section_used)
9377 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9378 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9379 text_section_label, "Length");
9381 if (cold_text_section_used)
9383 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9384 "Address");
9385 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9386 cold_text_section_label, "Length");
9389 if (have_multiple_function_sections)
9391 unsigned fde_idx;
9392 dw_fde_ref fde;
9394 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9396 if (DECL_IGNORED_P (fde->decl))
9397 continue;
9398 if (!fde->in_std_section)
9400 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9401 "Address");
9402 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9403 fde->dw_fde_begin, "Length");
9405 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9407 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9408 "Address");
9409 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9410 fde->dw_fde_second_begin, "Length");
9415 /* Output the terminator words. */
9416 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9417 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9420 /* Add a new entry to .debug_ranges. Return the offset at which it
9421 was placed. */
9423 static unsigned int
9424 add_ranges_num (int num)
9426 unsigned int in_use = ranges_table_in_use;
9428 if (in_use == ranges_table_allocated)
9430 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9431 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9432 ranges_table_allocated);
9433 memset (ranges_table + ranges_table_in_use, 0,
9434 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9437 ranges_table[in_use].num = num;
9438 ranges_table_in_use = in_use + 1;
9440 return in_use * 2 * DWARF2_ADDR_SIZE;
9443 /* Add a new entry to .debug_ranges corresponding to a block, or a
9444 range terminator if BLOCK is NULL. */
9446 static unsigned int
9447 add_ranges (const_tree block)
9449 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9452 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9453 When using dwarf_split_debug_info, address attributes in dies destined
9454 for the final executable should be direct references--setting the
9455 parameter force_direct ensures this behavior. */
9457 static void
9458 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9459 bool *added, bool force_direct)
9461 unsigned int in_use = ranges_by_label_in_use;
9462 unsigned int offset;
9464 if (in_use == ranges_by_label_allocated)
9466 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9467 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9468 ranges_by_label,
9469 ranges_by_label_allocated);
9470 memset (ranges_by_label + ranges_by_label_in_use, 0,
9471 RANGES_TABLE_INCREMENT
9472 * sizeof (struct dw_ranges_by_label_struct));
9475 ranges_by_label[in_use].begin = begin;
9476 ranges_by_label[in_use].end = end;
9477 ranges_by_label_in_use = in_use + 1;
9479 offset = add_ranges_num (-(int)in_use - 1);
9480 if (!*added)
9482 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9483 *added = true;
9487 static void
9488 output_ranges (void)
9490 unsigned i;
9491 static const char *const start_fmt = "Offset %#x";
9492 const char *fmt = start_fmt;
9494 for (i = 0; i < ranges_table_in_use; i++)
9496 int block_num = ranges_table[i].num;
9498 if (block_num > 0)
9500 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9501 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9503 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9504 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9506 /* If all code is in the text section, then the compilation
9507 unit base address defaults to DW_AT_low_pc, which is the
9508 base of the text section. */
9509 if (!have_multiple_function_sections)
9511 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9512 text_section_label,
9513 fmt, i * 2 * DWARF2_ADDR_SIZE);
9514 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9515 text_section_label, NULL);
9518 /* Otherwise, the compilation unit base address is zero,
9519 which allows us to use absolute addresses, and not worry
9520 about whether the target supports cross-section
9521 arithmetic. */
9522 else
9524 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9525 fmt, i * 2 * DWARF2_ADDR_SIZE);
9526 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9529 fmt = NULL;
9532 /* Negative block_num stands for an index into ranges_by_label. */
9533 else if (block_num < 0)
9535 int lab_idx = - block_num - 1;
9537 if (!have_multiple_function_sections)
9539 gcc_unreachable ();
9540 #if 0
9541 /* If we ever use add_ranges_by_labels () for a single
9542 function section, all we have to do is to take out
9543 the #if 0 above. */
9544 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9545 ranges_by_label[lab_idx].begin,
9546 text_section_label,
9547 fmt, i * 2 * DWARF2_ADDR_SIZE);
9548 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9549 ranges_by_label[lab_idx].end,
9550 text_section_label, NULL);
9551 #endif
9553 else
9555 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9556 ranges_by_label[lab_idx].begin,
9557 fmt, i * 2 * DWARF2_ADDR_SIZE);
9558 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9559 ranges_by_label[lab_idx].end,
9560 NULL);
9563 else
9565 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9566 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9567 fmt = start_fmt;
9572 /* Data structure containing information about input files. */
9573 struct file_info
9575 const char *path; /* Complete file name. */
9576 const char *fname; /* File name part. */
9577 int length; /* Length of entire string. */
9578 struct dwarf_file_data * file_idx; /* Index in input file table. */
9579 int dir_idx; /* Index in directory table. */
9582 /* Data structure containing information about directories with source
9583 files. */
9584 struct dir_info
9586 const char *path; /* Path including directory name. */
9587 int length; /* Path length. */
9588 int prefix; /* Index of directory entry which is a prefix. */
9589 int count; /* Number of files in this directory. */
9590 int dir_idx; /* Index of directory used as base. */
9593 /* Callback function for file_info comparison. We sort by looking at
9594 the directories in the path. */
9596 static int
9597 file_info_cmp (const void *p1, const void *p2)
9599 const struct file_info *const s1 = (const struct file_info *) p1;
9600 const struct file_info *const s2 = (const struct file_info *) p2;
9601 const unsigned char *cp1;
9602 const unsigned char *cp2;
9604 /* Take care of file names without directories. We need to make sure that
9605 we return consistent values to qsort since some will get confused if
9606 we return the same value when identical operands are passed in opposite
9607 orders. So if neither has a directory, return 0 and otherwise return
9608 1 or -1 depending on which one has the directory. */
9609 if ((s1->path == s1->fname || s2->path == s2->fname))
9610 return (s2->path == s2->fname) - (s1->path == s1->fname);
9612 cp1 = (const unsigned char *) s1->path;
9613 cp2 = (const unsigned char *) s2->path;
9615 while (1)
9617 ++cp1;
9618 ++cp2;
9619 /* Reached the end of the first path? If so, handle like above. */
9620 if ((cp1 == (const unsigned char *) s1->fname)
9621 || (cp2 == (const unsigned char *) s2->fname))
9622 return ((cp2 == (const unsigned char *) s2->fname)
9623 - (cp1 == (const unsigned char *) s1->fname));
9625 /* Character of current path component the same? */
9626 else if (*cp1 != *cp2)
9627 return *cp1 - *cp2;
9631 struct file_name_acquire_data
9633 struct file_info *files;
9634 int used_files;
9635 int max_files;
9638 /* Traversal function for the hash table. */
9640 static int
9641 file_name_acquire (void ** slot, void *data)
9643 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9644 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9645 struct file_info *fi;
9646 const char *f;
9648 gcc_assert (fnad->max_files >= d->emitted_number);
9650 if (! d->emitted_number)
9651 return 1;
9653 gcc_assert (fnad->max_files != fnad->used_files);
9655 fi = fnad->files + fnad->used_files++;
9657 /* Skip all leading "./". */
9658 f = d->filename;
9659 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9660 f += 2;
9662 /* Create a new array entry. */
9663 fi->path = f;
9664 fi->length = strlen (f);
9665 fi->file_idx = d;
9667 /* Search for the file name part. */
9668 f = strrchr (f, DIR_SEPARATOR);
9669 #if defined (DIR_SEPARATOR_2)
9671 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9673 if (g != NULL)
9675 if (f == NULL || f < g)
9676 f = g;
9679 #endif
9681 fi->fname = f == NULL ? fi->path : f + 1;
9682 return 1;
9685 /* Output the directory table and the file name table. We try to minimize
9686 the total amount of memory needed. A heuristic is used to avoid large
9687 slowdowns with many input files. */
9689 static void
9690 output_file_names (void)
9692 struct file_name_acquire_data fnad;
9693 int numfiles;
9694 struct file_info *files;
9695 struct dir_info *dirs;
9696 int *saved;
9697 int *savehere;
9698 int *backmap;
9699 int ndirs;
9700 int idx_offset;
9701 int i;
9703 if (!last_emitted_file)
9705 dw2_asm_output_data (1, 0, "End directory table");
9706 dw2_asm_output_data (1, 0, "End file name table");
9707 return;
9710 numfiles = last_emitted_file->emitted_number;
9712 /* Allocate the various arrays we need. */
9713 files = XALLOCAVEC (struct file_info, numfiles);
9714 dirs = XALLOCAVEC (struct dir_info, numfiles);
9716 fnad.files = files;
9717 fnad.used_files = 0;
9718 fnad.max_files = numfiles;
9719 htab_traverse (file_table, file_name_acquire, &fnad);
9720 gcc_assert (fnad.used_files == fnad.max_files);
9722 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9724 /* Find all the different directories used. */
9725 dirs[0].path = files[0].path;
9726 dirs[0].length = files[0].fname - files[0].path;
9727 dirs[0].prefix = -1;
9728 dirs[0].count = 1;
9729 dirs[0].dir_idx = 0;
9730 files[0].dir_idx = 0;
9731 ndirs = 1;
9733 for (i = 1; i < numfiles; i++)
9734 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9735 && memcmp (dirs[ndirs - 1].path, files[i].path,
9736 dirs[ndirs - 1].length) == 0)
9738 /* Same directory as last entry. */
9739 files[i].dir_idx = ndirs - 1;
9740 ++dirs[ndirs - 1].count;
9742 else
9744 int j;
9746 /* This is a new directory. */
9747 dirs[ndirs].path = files[i].path;
9748 dirs[ndirs].length = files[i].fname - files[i].path;
9749 dirs[ndirs].count = 1;
9750 dirs[ndirs].dir_idx = ndirs;
9751 files[i].dir_idx = ndirs;
9753 /* Search for a prefix. */
9754 dirs[ndirs].prefix = -1;
9755 for (j = 0; j < ndirs; j++)
9756 if (dirs[j].length < dirs[ndirs].length
9757 && dirs[j].length > 1
9758 && (dirs[ndirs].prefix == -1
9759 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9760 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9761 dirs[ndirs].prefix = j;
9763 ++ndirs;
9766 /* Now to the actual work. We have to find a subset of the directories which
9767 allow expressing the file name using references to the directory table
9768 with the least amount of characters. We do not do an exhaustive search
9769 where we would have to check out every combination of every single
9770 possible prefix. Instead we use a heuristic which provides nearly optimal
9771 results in most cases and never is much off. */
9772 saved = XALLOCAVEC (int, ndirs);
9773 savehere = XALLOCAVEC (int, ndirs);
9775 memset (saved, '\0', ndirs * sizeof (saved[0]));
9776 for (i = 0; i < ndirs; i++)
9778 int j;
9779 int total;
9781 /* We can always save some space for the current directory. But this
9782 does not mean it will be enough to justify adding the directory. */
9783 savehere[i] = dirs[i].length;
9784 total = (savehere[i] - saved[i]) * dirs[i].count;
9786 for (j = i + 1; j < ndirs; j++)
9788 savehere[j] = 0;
9789 if (saved[j] < dirs[i].length)
9791 /* Determine whether the dirs[i] path is a prefix of the
9792 dirs[j] path. */
9793 int k;
9795 k = dirs[j].prefix;
9796 while (k != -1 && k != (int) i)
9797 k = dirs[k].prefix;
9799 if (k == (int) i)
9801 /* Yes it is. We can possibly save some memory by
9802 writing the filenames in dirs[j] relative to
9803 dirs[i]. */
9804 savehere[j] = dirs[i].length;
9805 total += (savehere[j] - saved[j]) * dirs[j].count;
9810 /* Check whether we can save enough to justify adding the dirs[i]
9811 directory. */
9812 if (total > dirs[i].length + 1)
9814 /* It's worthwhile adding. */
9815 for (j = i; j < ndirs; j++)
9816 if (savehere[j] > 0)
9818 /* Remember how much we saved for this directory so far. */
9819 saved[j] = savehere[j];
9821 /* Remember the prefix directory. */
9822 dirs[j].dir_idx = i;
9827 /* Emit the directory name table. */
9828 idx_offset = dirs[0].length > 0 ? 1 : 0;
9829 for (i = 1 - idx_offset; i < ndirs; i++)
9830 dw2_asm_output_nstring (dirs[i].path,
9831 dirs[i].length
9832 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9833 "Directory Entry: %#x", i + idx_offset);
9835 dw2_asm_output_data (1, 0, "End directory table");
9837 /* We have to emit them in the order of emitted_number since that's
9838 used in the debug info generation. To do this efficiently we
9839 generate a back-mapping of the indices first. */
9840 backmap = XALLOCAVEC (int, numfiles);
9841 for (i = 0; i < numfiles; i++)
9842 backmap[files[i].file_idx->emitted_number - 1] = i;
9844 /* Now write all the file names. */
9845 for (i = 0; i < numfiles; i++)
9847 int file_idx = backmap[i];
9848 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9850 #ifdef VMS_DEBUGGING_INFO
9851 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9853 /* Setting these fields can lead to debugger miscomparisons,
9854 but VMS Debug requires them to be set correctly. */
9856 int ver;
9857 long long cdt;
9858 long siz;
9859 int maxfilelen = strlen (files[file_idx].path)
9860 + dirs[dir_idx].length
9861 + MAX_VMS_VERSION_LEN + 1;
9862 char *filebuf = XALLOCAVEC (char, maxfilelen);
9864 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9865 snprintf (filebuf, maxfilelen, "%s;%d",
9866 files[file_idx].path + dirs[dir_idx].length, ver);
9868 dw2_asm_output_nstring
9869 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9871 /* Include directory index. */
9872 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9874 /* Modification time. */
9875 dw2_asm_output_data_uleb128
9876 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9877 ? cdt : 0,
9878 NULL);
9880 /* File length in bytes. */
9881 dw2_asm_output_data_uleb128
9882 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9883 ? siz : 0,
9884 NULL);
9885 #else
9886 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9887 "File Entry: %#x", (unsigned) i + 1);
9889 /* Include directory index. */
9890 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9892 /* Modification time. */
9893 dw2_asm_output_data_uleb128 (0, NULL);
9895 /* File length in bytes. */
9896 dw2_asm_output_data_uleb128 (0, NULL);
9897 #endif /* VMS_DEBUGGING_INFO */
9900 dw2_asm_output_data (1, 0, "End file name table");
9904 /* Output one line number table into the .debug_line section. */
9906 static void
9907 output_one_line_info_table (dw_line_info_table *table)
9909 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9910 unsigned int current_line = 1;
9911 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9912 dw_line_info_entry *ent;
9913 size_t i;
9915 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9917 switch (ent->opcode)
9919 case LI_set_address:
9920 /* ??? Unfortunately, we have little choice here currently, and
9921 must always use the most general form. GCC does not know the
9922 address delta itself, so we can't use DW_LNS_advance_pc. Many
9923 ports do have length attributes which will give an upper bound
9924 on the address range. We could perhaps use length attributes
9925 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9926 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9928 /* This can handle any delta. This takes
9929 4+DWARF2_ADDR_SIZE bytes. */
9930 dw2_asm_output_data (1, 0, "set address %s", line_label);
9931 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9932 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9933 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9934 break;
9936 case LI_set_line:
9937 if (ent->val == current_line)
9939 /* We still need to start a new row, so output a copy insn. */
9940 dw2_asm_output_data (1, DW_LNS_copy,
9941 "copy line %u", current_line);
9943 else
9945 int line_offset = ent->val - current_line;
9946 int line_delta = line_offset - DWARF_LINE_BASE;
9948 current_line = ent->val;
9949 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9951 /* This can handle deltas from -10 to 234, using the current
9952 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9953 This takes 1 byte. */
9954 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9955 "line %u", current_line);
9957 else
9959 /* This can handle any delta. This takes at least 4 bytes,
9960 depending on the value being encoded. */
9961 dw2_asm_output_data (1, DW_LNS_advance_line,
9962 "advance to line %u", current_line);
9963 dw2_asm_output_data_sleb128 (line_offset, NULL);
9964 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9967 break;
9969 case LI_set_file:
9970 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9971 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9972 break;
9974 case LI_set_column:
9975 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9976 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9977 break;
9979 case LI_negate_stmt:
9980 current_is_stmt = !current_is_stmt;
9981 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9982 "is_stmt %d", current_is_stmt);
9983 break;
9985 case LI_set_prologue_end:
9986 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9987 "set prologue end");
9988 break;
9990 case LI_set_epilogue_begin:
9991 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9992 "set epilogue begin");
9993 break;
9995 case LI_set_discriminator:
9996 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9997 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9998 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9999 dw2_asm_output_data_uleb128 (ent->val, NULL);
10000 break;
10004 /* Emit debug info for the address of the end of the table. */
10005 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10006 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10007 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10008 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10010 dw2_asm_output_data (1, 0, "end sequence");
10011 dw2_asm_output_data_uleb128 (1, NULL);
10012 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10015 /* Output the source line number correspondence information. This
10016 information goes into the .debug_line section. */
10018 static void
10019 output_line_info (bool prologue_only)
10021 char l1[20], l2[20], p1[20], p2[20];
10022 int ver = dwarf_version;
10023 bool saw_one = false;
10024 int opc;
10026 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10027 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10028 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10029 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10031 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10032 dw2_asm_output_data (4, 0xffffffff,
10033 "Initial length escape value indicating 64-bit DWARF extension");
10034 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10035 "Length of Source Line Info");
10036 ASM_OUTPUT_LABEL (asm_out_file, l1);
10038 dw2_asm_output_data (2, ver, "DWARF Version");
10039 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10040 ASM_OUTPUT_LABEL (asm_out_file, p1);
10042 /* Define the architecture-dependent minimum instruction length (in bytes).
10043 In this implementation of DWARF, this field is used for information
10044 purposes only. Since GCC generates assembly language, we have no
10045 a priori knowledge of how many instruction bytes are generated for each
10046 source line, and therefore can use only the DW_LNE_set_address and
10047 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10048 this as '1', which is "correct enough" for all architectures,
10049 and don't let the target override. */
10050 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10052 if (ver >= 4)
10053 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10054 "Maximum Operations Per Instruction");
10055 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10056 "Default is_stmt_start flag");
10057 dw2_asm_output_data (1, DWARF_LINE_BASE,
10058 "Line Base Value (Special Opcodes)");
10059 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10060 "Line Range Value (Special Opcodes)");
10061 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10062 "Special Opcode Base");
10064 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10066 int n_op_args;
10067 switch (opc)
10069 case DW_LNS_advance_pc:
10070 case DW_LNS_advance_line:
10071 case DW_LNS_set_file:
10072 case DW_LNS_set_column:
10073 case DW_LNS_fixed_advance_pc:
10074 case DW_LNS_set_isa:
10075 n_op_args = 1;
10076 break;
10077 default:
10078 n_op_args = 0;
10079 break;
10082 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10083 opc, n_op_args);
10086 /* Write out the information about the files we use. */
10087 output_file_names ();
10088 ASM_OUTPUT_LABEL (asm_out_file, p2);
10089 if (prologue_only)
10091 /* Output the marker for the end of the line number info. */
10092 ASM_OUTPUT_LABEL (asm_out_file, l2);
10093 return;
10096 if (separate_line_info)
10098 dw_line_info_table *table;
10099 size_t i;
10101 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10102 if (table->in_use)
10104 output_one_line_info_table (table);
10105 saw_one = true;
10108 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10110 output_one_line_info_table (cold_text_section_line_info);
10111 saw_one = true;
10114 /* ??? Some Darwin linkers crash on a .debug_line section with no
10115 sequences. Further, merely a DW_LNE_end_sequence entry is not
10116 sufficient -- the address column must also be initialized.
10117 Make sure to output at least one set_address/end_sequence pair,
10118 choosing .text since that section is always present. */
10119 if (text_section_line_info->in_use || !saw_one)
10120 output_one_line_info_table (text_section_line_info);
10122 /* Output the marker for the end of the line number info. */
10123 ASM_OUTPUT_LABEL (asm_out_file, l2);
10126 /* Given a pointer to a tree node for some base type, return a pointer to
10127 a DIE that describes the given type.
10129 This routine must only be called for GCC type nodes that correspond to
10130 Dwarf base (fundamental) types. */
10132 static dw_die_ref
10133 base_type_die (tree type)
10135 dw_die_ref base_type_result;
10136 enum dwarf_type encoding;
10138 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10139 return 0;
10141 /* If this is a subtype that should not be emitted as a subrange type,
10142 use the base type. See subrange_type_for_debug_p. */
10143 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10144 type = TREE_TYPE (type);
10146 switch (TREE_CODE (type))
10148 case INTEGER_TYPE:
10149 if ((dwarf_version >= 4 || !dwarf_strict)
10150 && TYPE_NAME (type)
10151 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10152 && DECL_IS_BUILTIN (TYPE_NAME (type))
10153 && DECL_NAME (TYPE_NAME (type)))
10155 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10156 if (strcmp (name, "char16_t") == 0
10157 || strcmp (name, "char32_t") == 0)
10159 encoding = DW_ATE_UTF;
10160 break;
10163 if (TYPE_STRING_FLAG (type))
10165 if (TYPE_UNSIGNED (type))
10166 encoding = DW_ATE_unsigned_char;
10167 else
10168 encoding = DW_ATE_signed_char;
10170 else if (TYPE_UNSIGNED (type))
10171 encoding = DW_ATE_unsigned;
10172 else
10173 encoding = DW_ATE_signed;
10174 break;
10176 case REAL_TYPE:
10177 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10179 if (dwarf_version >= 3 || !dwarf_strict)
10180 encoding = DW_ATE_decimal_float;
10181 else
10182 encoding = DW_ATE_lo_user;
10184 else
10185 encoding = DW_ATE_float;
10186 break;
10188 case FIXED_POINT_TYPE:
10189 if (!(dwarf_version >= 3 || !dwarf_strict))
10190 encoding = DW_ATE_lo_user;
10191 else if (TYPE_UNSIGNED (type))
10192 encoding = DW_ATE_unsigned_fixed;
10193 else
10194 encoding = DW_ATE_signed_fixed;
10195 break;
10197 /* Dwarf2 doesn't know anything about complex ints, so use
10198 a user defined type for it. */
10199 case COMPLEX_TYPE:
10200 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10201 encoding = DW_ATE_complex_float;
10202 else
10203 encoding = DW_ATE_lo_user;
10204 break;
10206 case BOOLEAN_TYPE:
10207 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10208 encoding = DW_ATE_boolean;
10209 break;
10211 default:
10212 /* No other TREE_CODEs are Dwarf fundamental types. */
10213 gcc_unreachable ();
10216 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10218 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10219 int_size_in_bytes (type));
10220 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10221 add_pubtype (type, base_type_result);
10223 return base_type_result;
10226 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10227 named 'auto' in its type: return true for it, false otherwise. */
10229 static inline bool
10230 is_cxx_auto (tree type)
10232 if (is_cxx ())
10234 tree name = TYPE_NAME (type);
10235 if (TREE_CODE (name) == TYPE_DECL)
10236 name = DECL_NAME (name);
10237 if (name == get_identifier ("auto")
10238 || name == get_identifier ("decltype(auto)"))
10239 return true;
10241 return false;
10244 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10245 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10247 static inline int
10248 is_base_type (tree type)
10250 switch (TREE_CODE (type))
10252 case ERROR_MARK:
10253 case VOID_TYPE:
10254 case INTEGER_TYPE:
10255 case REAL_TYPE:
10256 case FIXED_POINT_TYPE:
10257 case COMPLEX_TYPE:
10258 case BOOLEAN_TYPE:
10259 return 1;
10261 case ARRAY_TYPE:
10262 case RECORD_TYPE:
10263 case UNION_TYPE:
10264 case QUAL_UNION_TYPE:
10265 case ENUMERAL_TYPE:
10266 case FUNCTION_TYPE:
10267 case METHOD_TYPE:
10268 case POINTER_TYPE:
10269 case REFERENCE_TYPE:
10270 case NULLPTR_TYPE:
10271 case OFFSET_TYPE:
10272 case LANG_TYPE:
10273 case VECTOR_TYPE:
10274 return 0;
10276 default:
10277 if (is_cxx_auto (type))
10278 return 0;
10279 gcc_unreachable ();
10282 return 0;
10285 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10286 node, return the size in bits for the type if it is a constant, or else
10287 return the alignment for the type if the type's size is not constant, or
10288 else return BITS_PER_WORD if the type actually turns out to be an
10289 ERROR_MARK node. */
10291 static inline unsigned HOST_WIDE_INT
10292 simple_type_size_in_bits (const_tree type)
10294 if (TREE_CODE (type) == ERROR_MARK)
10295 return BITS_PER_WORD;
10296 else if (TYPE_SIZE (type) == NULL_TREE)
10297 return 0;
10298 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10299 return tree_to_uhwi (TYPE_SIZE (type));
10300 else
10301 return TYPE_ALIGN (type);
10304 /* Similarly, but return a double_int instead of UHWI. */
10306 static inline double_int
10307 double_int_type_size_in_bits (const_tree type)
10309 if (TREE_CODE (type) == ERROR_MARK)
10310 return double_int::from_uhwi (BITS_PER_WORD);
10311 else if (TYPE_SIZE (type) == NULL_TREE)
10312 return double_int_zero;
10313 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10314 return tree_to_double_int (TYPE_SIZE (type));
10315 else
10316 return double_int::from_uhwi (TYPE_ALIGN (type));
10319 /* Given a pointer to a tree node for a subrange type, return a pointer
10320 to a DIE that describes the given type. */
10322 static dw_die_ref
10323 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10325 dw_die_ref subrange_die;
10326 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10328 if (context_die == NULL)
10329 context_die = comp_unit_die ();
10331 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10333 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10335 /* The size of the subrange type and its base type do not match,
10336 so we need to generate a size attribute for the subrange type. */
10337 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10340 if (low)
10341 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10342 if (high)
10343 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10345 return subrange_die;
10348 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10349 entry that chains various modifiers in front of the given type. */
10351 static dw_die_ref
10352 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10353 dw_die_ref context_die)
10355 enum tree_code code = TREE_CODE (type);
10356 dw_die_ref mod_type_die;
10357 dw_die_ref sub_die = NULL;
10358 tree item_type = NULL;
10359 tree qualified_type;
10360 tree name, low, high;
10361 dw_die_ref mod_scope;
10363 if (code == ERROR_MARK)
10364 return NULL;
10366 /* See if we already have the appropriately qualified variant of
10367 this type. */
10368 qualified_type
10369 = get_qualified_type (type,
10370 ((is_const_type ? TYPE_QUAL_CONST : 0)
10371 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10373 if (qualified_type == sizetype
10374 && TYPE_NAME (qualified_type)
10375 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10377 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10379 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10380 && TYPE_PRECISION (t)
10381 == TYPE_PRECISION (qualified_type)
10382 && TYPE_UNSIGNED (t)
10383 == TYPE_UNSIGNED (qualified_type));
10384 qualified_type = t;
10387 /* If we do, then we can just use its DIE, if it exists. */
10388 if (qualified_type)
10390 mod_type_die = lookup_type_die (qualified_type);
10391 if (mod_type_die)
10392 return mod_type_die;
10395 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10397 /* Handle C typedef types. */
10398 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10399 && !DECL_ARTIFICIAL (name))
10401 tree dtype = TREE_TYPE (name);
10403 if (qualified_type == dtype)
10405 /* For a named type, use the typedef. */
10406 gen_type_die (qualified_type, context_die);
10407 return lookup_type_die (qualified_type);
10409 else if (is_const_type < TYPE_READONLY (dtype)
10410 || is_volatile_type < TYPE_VOLATILE (dtype)
10411 || (is_const_type <= TYPE_READONLY (dtype)
10412 && is_volatile_type <= TYPE_VOLATILE (dtype)
10413 && DECL_ORIGINAL_TYPE (name) != type))
10414 /* cv-unqualified version of named type. Just use the unnamed
10415 type to which it refers. */
10416 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10417 is_const_type, is_volatile_type,
10418 context_die);
10419 /* Else cv-qualified version of named type; fall through. */
10422 mod_scope = scope_die_for (type, context_die);
10424 if (is_const_type
10425 /* If both is_const_type and is_volatile_type, prefer the path
10426 which leads to a qualified type. */
10427 && (!is_volatile_type
10428 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10429 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10431 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10432 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10434 else if (is_volatile_type)
10436 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10437 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10439 else if (code == POINTER_TYPE)
10441 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10442 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10443 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10444 item_type = TREE_TYPE (type);
10445 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10446 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10447 TYPE_ADDR_SPACE (item_type));
10449 else if (code == REFERENCE_TYPE)
10451 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10452 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10453 type);
10454 else
10455 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10456 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10457 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10458 item_type = TREE_TYPE (type);
10459 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10460 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10461 TYPE_ADDR_SPACE (item_type));
10463 else if (code == INTEGER_TYPE
10464 && TREE_TYPE (type) != NULL_TREE
10465 && subrange_type_for_debug_p (type, &low, &high))
10467 mod_type_die = subrange_type_die (type, low, high, context_die);
10468 item_type = TREE_TYPE (type);
10470 else if (is_base_type (type))
10471 mod_type_die = base_type_die (type);
10472 else
10474 gen_type_die (type, context_die);
10476 /* We have to get the type_main_variant here (and pass that to the
10477 `lookup_type_die' routine) because the ..._TYPE node we have
10478 might simply be a *copy* of some original type node (where the
10479 copy was created to help us keep track of typedef names) and
10480 that copy might have a different TYPE_UID from the original
10481 ..._TYPE node. */
10482 if (TREE_CODE (type) != VECTOR_TYPE)
10483 return lookup_type_die (type_main_variant (type));
10484 else
10485 /* Vectors have the debugging information in the type,
10486 not the main variant. */
10487 return lookup_type_die (type);
10490 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10491 don't output a DW_TAG_typedef, since there isn't one in the
10492 user's program; just attach a DW_AT_name to the type.
10493 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10494 if the base type already has the same name. */
10495 if (name
10496 && ((TREE_CODE (name) != TYPE_DECL
10497 && (qualified_type == TYPE_MAIN_VARIANT (type)
10498 || (!is_const_type && !is_volatile_type)))
10499 || (TREE_CODE (name) == TYPE_DECL
10500 && TREE_TYPE (name) == qualified_type
10501 && DECL_NAME (name))))
10503 if (TREE_CODE (name) == TYPE_DECL)
10504 /* Could just call add_name_and_src_coords_attributes here,
10505 but since this is a builtin type it doesn't have any
10506 useful source coordinates anyway. */
10507 name = DECL_NAME (name);
10508 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10510 /* This probably indicates a bug. */
10511 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10513 name = TYPE_NAME (type);
10514 if (name
10515 && TREE_CODE (name) == TYPE_DECL)
10516 name = DECL_NAME (name);
10517 add_name_attribute (mod_type_die,
10518 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10521 if (qualified_type)
10522 equate_type_number_to_die (qualified_type, mod_type_die);
10524 if (item_type)
10525 /* We must do this after the equate_type_number_to_die call, in case
10526 this is a recursive type. This ensures that the modified_type_die
10527 recursion will terminate even if the type is recursive. Recursive
10528 types are possible in Ada. */
10529 sub_die = modified_type_die (item_type,
10530 TYPE_READONLY (item_type),
10531 TYPE_VOLATILE (item_type),
10532 context_die);
10534 if (sub_die != NULL)
10535 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10537 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10538 if (TYPE_ARTIFICIAL (type))
10539 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10541 return mod_type_die;
10544 /* Generate DIEs for the generic parameters of T.
10545 T must be either a generic type or a generic function.
10546 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10548 static void
10549 gen_generic_params_dies (tree t)
10551 tree parms, args;
10552 int parms_num, i;
10553 dw_die_ref die = NULL;
10554 int non_default;
10556 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10557 return;
10559 if (TYPE_P (t))
10560 die = lookup_type_die (t);
10561 else if (DECL_P (t))
10562 die = lookup_decl_die (t);
10564 gcc_assert (die);
10566 parms = lang_hooks.get_innermost_generic_parms (t);
10567 if (!parms)
10568 /* T has no generic parameter. It means T is neither a generic type
10569 or function. End of story. */
10570 return;
10572 parms_num = TREE_VEC_LENGTH (parms);
10573 args = lang_hooks.get_innermost_generic_args (t);
10574 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10575 non_default = int_cst_value (TREE_CHAIN (args));
10576 else
10577 non_default = TREE_VEC_LENGTH (args);
10578 for (i = 0; i < parms_num; i++)
10580 tree parm, arg, arg_pack_elems;
10581 dw_die_ref parm_die;
10583 parm = TREE_VEC_ELT (parms, i);
10584 arg = TREE_VEC_ELT (args, i);
10585 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10586 gcc_assert (parm && TREE_VALUE (parm) && arg);
10588 if (parm && TREE_VALUE (parm) && arg)
10590 /* If PARM represents a template parameter pack,
10591 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10592 by DW_TAG_template_*_parameter DIEs for the argument
10593 pack elements of ARG. Note that ARG would then be
10594 an argument pack. */
10595 if (arg_pack_elems)
10596 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10597 arg_pack_elems,
10598 die);
10599 else
10600 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10601 true /* emit name */, die);
10602 if (i >= non_default)
10603 add_AT_flag (parm_die, DW_AT_default_value, 1);
10608 /* Create and return a DIE for PARM which should be
10609 the representation of a generic type parameter.
10610 For instance, in the C++ front end, PARM would be a template parameter.
10611 ARG is the argument to PARM.
10612 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10613 name of the PARM.
10614 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10615 as a child node. */
10617 static dw_die_ref
10618 generic_parameter_die (tree parm, tree arg,
10619 bool emit_name_p,
10620 dw_die_ref parent_die)
10622 dw_die_ref tmpl_die = NULL;
10623 const char *name = NULL;
10625 if (!parm || !DECL_NAME (parm) || !arg)
10626 return NULL;
10628 /* We support non-type generic parameters and arguments,
10629 type generic parameters and arguments, as well as
10630 generic generic parameters (a.k.a. template template parameters in C++)
10631 and arguments. */
10632 if (TREE_CODE (parm) == PARM_DECL)
10633 /* PARM is a nontype generic parameter */
10634 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10635 else if (TREE_CODE (parm) == TYPE_DECL)
10636 /* PARM is a type generic parameter. */
10637 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10638 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10639 /* PARM is a generic generic parameter.
10640 Its DIE is a GNU extension. It shall have a
10641 DW_AT_name attribute to represent the name of the template template
10642 parameter, and a DW_AT_GNU_template_name attribute to represent the
10643 name of the template template argument. */
10644 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10645 parent_die, parm);
10646 else
10647 gcc_unreachable ();
10649 if (tmpl_die)
10651 tree tmpl_type;
10653 /* If PARM is a generic parameter pack, it means we are
10654 emitting debug info for a template argument pack element.
10655 In other terms, ARG is a template argument pack element.
10656 In that case, we don't emit any DW_AT_name attribute for
10657 the die. */
10658 if (emit_name_p)
10660 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10661 gcc_assert (name);
10662 add_AT_string (tmpl_die, DW_AT_name, name);
10665 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10667 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10668 TMPL_DIE should have a child DW_AT_type attribute that is set
10669 to the type of the argument to PARM, which is ARG.
10670 If PARM is a type generic parameter, TMPL_DIE should have a
10671 child DW_AT_type that is set to ARG. */
10672 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10673 add_type_attribute (tmpl_die, tmpl_type, 0,
10674 TREE_THIS_VOLATILE (tmpl_type),
10675 parent_die);
10677 else
10679 /* So TMPL_DIE is a DIE representing a
10680 a generic generic template parameter, a.k.a template template
10681 parameter in C++ and arg is a template. */
10683 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10684 to the name of the argument. */
10685 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10686 if (name)
10687 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10690 if (TREE_CODE (parm) == PARM_DECL)
10691 /* So PARM is a non-type generic parameter.
10692 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10693 attribute of TMPL_DIE which value represents the value
10694 of ARG.
10695 We must be careful here:
10696 The value of ARG might reference some function decls.
10697 We might currently be emitting debug info for a generic
10698 type and types are emitted before function decls, we don't
10699 know if the function decls referenced by ARG will actually be
10700 emitted after cgraph computations.
10701 So must defer the generation of the DW_AT_const_value to
10702 after cgraph is ready. */
10703 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10706 return tmpl_die;
10709 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10710 PARM_PACK must be a template parameter pack. The returned DIE
10711 will be child DIE of PARENT_DIE. */
10713 static dw_die_ref
10714 template_parameter_pack_die (tree parm_pack,
10715 tree parm_pack_args,
10716 dw_die_ref parent_die)
10718 dw_die_ref die;
10719 int j;
10721 gcc_assert (parent_die && parm_pack);
10723 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10724 add_name_and_src_coords_attributes (die, parm_pack);
10725 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10726 generic_parameter_die (parm_pack,
10727 TREE_VEC_ELT (parm_pack_args, j),
10728 false /* Don't emit DW_AT_name */,
10729 die);
10730 return die;
10733 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10734 an enumerated type. */
10736 static inline int
10737 type_is_enum (const_tree type)
10739 return TREE_CODE (type) == ENUMERAL_TYPE;
10742 /* Return the DBX register number described by a given RTL node. */
10744 static unsigned int
10745 dbx_reg_number (const_rtx rtl)
10747 unsigned regno = REGNO (rtl);
10749 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10751 #ifdef LEAF_REG_REMAP
10752 if (crtl->uses_only_leaf_regs)
10754 int leaf_reg = LEAF_REG_REMAP (regno);
10755 if (leaf_reg != -1)
10756 regno = (unsigned) leaf_reg;
10758 #endif
10760 regno = DBX_REGISTER_NUMBER (regno);
10761 gcc_assert (regno != INVALID_REGNUM);
10762 return regno;
10765 /* Optionally add a DW_OP_piece term to a location description expression.
10766 DW_OP_piece is only added if the location description expression already
10767 doesn't end with DW_OP_piece. */
10769 static void
10770 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10772 dw_loc_descr_ref loc;
10774 if (*list_head != NULL)
10776 /* Find the end of the chain. */
10777 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10780 if (loc->dw_loc_opc != DW_OP_piece)
10781 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10785 /* Return a location descriptor that designates a machine register or
10786 zero if there is none. */
10788 static dw_loc_descr_ref
10789 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10791 rtx regs;
10793 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10794 return 0;
10796 /* We only use "frame base" when we're sure we're talking about the
10797 post-prologue local stack frame. We do this by *not* running
10798 register elimination until this point, and recognizing the special
10799 argument pointer and soft frame pointer rtx's.
10800 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10801 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10802 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10804 dw_loc_descr_ref result = NULL;
10806 if (dwarf_version >= 4 || !dwarf_strict)
10808 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10809 initialized);
10810 if (result)
10811 add_loc_descr (&result,
10812 new_loc_descr (DW_OP_stack_value, 0, 0));
10814 return result;
10817 regs = targetm.dwarf_register_span (rtl);
10819 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10820 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10821 else
10823 unsigned int dbx_regnum = dbx_reg_number (rtl);
10824 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10825 return 0;
10826 return one_reg_loc_descriptor (dbx_regnum, initialized);
10830 /* Return a location descriptor that designates a machine register for
10831 a given hard register number. */
10833 static dw_loc_descr_ref
10834 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10836 dw_loc_descr_ref reg_loc_descr;
10838 if (regno <= 31)
10839 reg_loc_descr
10840 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10841 else
10842 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10844 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10845 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10847 return reg_loc_descr;
10850 /* Given an RTL of a register, return a location descriptor that
10851 designates a value that spans more than one register. */
10853 static dw_loc_descr_ref
10854 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10855 enum var_init_status initialized)
10857 int size, i;
10858 dw_loc_descr_ref loc_result = NULL;
10860 /* Simple, contiguous registers. */
10861 if (regs == NULL_RTX)
10863 unsigned reg = REGNO (rtl);
10864 int nregs;
10866 #ifdef LEAF_REG_REMAP
10867 if (crtl->uses_only_leaf_regs)
10869 int leaf_reg = LEAF_REG_REMAP (reg);
10870 if (leaf_reg != -1)
10871 reg = (unsigned) leaf_reg;
10873 #endif
10875 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10876 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10878 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10880 loc_result = NULL;
10881 while (nregs--)
10883 dw_loc_descr_ref t;
10885 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10886 VAR_INIT_STATUS_INITIALIZED);
10887 add_loc_descr (&loc_result, t);
10888 add_loc_descr_op_piece (&loc_result, size);
10889 ++reg;
10891 return loc_result;
10894 /* Now onto stupid register sets in non contiguous locations. */
10896 gcc_assert (GET_CODE (regs) == PARALLEL);
10898 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10899 loc_result = NULL;
10901 for (i = 0; i < XVECLEN (regs, 0); ++i)
10903 dw_loc_descr_ref t;
10905 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10906 VAR_INIT_STATUS_INITIALIZED);
10907 add_loc_descr (&loc_result, t);
10908 add_loc_descr_op_piece (&loc_result, size);
10911 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10912 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10913 return loc_result;
10916 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10918 /* Return a location descriptor that designates a constant i,
10919 as a compound operation from constant (i >> shift), constant shift
10920 and DW_OP_shl. */
10922 static dw_loc_descr_ref
10923 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10925 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10926 add_loc_descr (&ret, int_loc_descriptor (shift));
10927 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10928 return ret;
10931 /* Return a location descriptor that designates a constant. */
10933 static dw_loc_descr_ref
10934 int_loc_descriptor (HOST_WIDE_INT i)
10936 enum dwarf_location_atom op;
10938 /* Pick the smallest representation of a constant, rather than just
10939 defaulting to the LEB encoding. */
10940 if (i >= 0)
10942 int clz = clz_hwi (i);
10943 int ctz = ctz_hwi (i);
10944 if (i <= 31)
10945 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10946 else if (i <= 0xff)
10947 op = DW_OP_const1u;
10948 else if (i <= 0xffff)
10949 op = DW_OP_const2u;
10950 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10951 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10952 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10953 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10954 while DW_OP_const4u is 5 bytes. */
10955 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10956 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10957 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10958 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10959 while DW_OP_const4u is 5 bytes. */
10960 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10961 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10962 op = DW_OP_const4u;
10963 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10964 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10965 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10966 while DW_OP_constu of constant >= 0x100000000 takes at least
10967 6 bytes. */
10968 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10969 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10970 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10971 >= HOST_BITS_PER_WIDE_INT)
10972 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10973 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10974 while DW_OP_constu takes in this case at least 6 bytes. */
10975 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10976 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10977 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10978 && size_of_uleb128 (i) > 6)
10979 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10980 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10981 else
10982 op = DW_OP_constu;
10984 else
10986 if (i >= -0x80)
10987 op = DW_OP_const1s;
10988 else if (i >= -0x8000)
10989 op = DW_OP_const2s;
10990 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10992 if (size_of_int_loc_descriptor (i) < 5)
10994 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10995 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10996 return ret;
10998 op = DW_OP_const4s;
11000 else
11002 if (size_of_int_loc_descriptor (i)
11003 < (unsigned long) 1 + size_of_sleb128 (i))
11005 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11006 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11007 return ret;
11009 op = DW_OP_consts;
11013 return new_loc_descr (op, i, 0);
11016 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11017 without actually allocating it. */
11019 static unsigned long
11020 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11022 return size_of_int_loc_descriptor (i >> shift)
11023 + size_of_int_loc_descriptor (shift)
11024 + 1;
11027 /* Return size_of_locs (int_loc_descriptor (i)) without
11028 actually allocating it. */
11030 static unsigned long
11031 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11033 unsigned long s;
11035 if (i >= 0)
11037 int clz, ctz;
11038 if (i <= 31)
11039 return 1;
11040 else if (i <= 0xff)
11041 return 2;
11042 else if (i <= 0xffff)
11043 return 3;
11044 clz = clz_hwi (i);
11045 ctz = ctz_hwi (i);
11046 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11047 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11048 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11049 - clz - 5);
11050 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11051 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11052 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11053 - clz - 8);
11054 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11055 return 5;
11056 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11057 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11058 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11059 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11060 - clz - 8);
11061 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11062 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11063 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11064 - clz - 16);
11065 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11066 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11067 && s > 6)
11068 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11069 - clz - 32);
11070 else
11071 return 1 + s;
11073 else
11075 if (i >= -0x80)
11076 return 2;
11077 else if (i >= -0x8000)
11078 return 3;
11079 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11081 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11083 s = size_of_int_loc_descriptor (-i) + 1;
11084 if (s < 5)
11085 return s;
11087 return 5;
11089 else
11091 unsigned long r = 1 + size_of_sleb128 (i);
11092 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11094 s = size_of_int_loc_descriptor (-i) + 1;
11095 if (s < r)
11096 return s;
11098 return r;
11103 /* Return loc description representing "address" of integer value.
11104 This can appear only as toplevel expression. */
11106 static dw_loc_descr_ref
11107 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11109 int litsize;
11110 dw_loc_descr_ref loc_result = NULL;
11112 if (!(dwarf_version >= 4 || !dwarf_strict))
11113 return NULL;
11115 litsize = size_of_int_loc_descriptor (i);
11116 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11117 is more compact. For DW_OP_stack_value we need:
11118 litsize + 1 (DW_OP_stack_value)
11119 and for DW_OP_implicit_value:
11120 1 (DW_OP_implicit_value) + 1 (length) + size. */
11121 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11123 loc_result = int_loc_descriptor (i);
11124 add_loc_descr (&loc_result,
11125 new_loc_descr (DW_OP_stack_value, 0, 0));
11126 return loc_result;
11129 loc_result = new_loc_descr (DW_OP_implicit_value,
11130 size, 0);
11131 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11132 loc_result->dw_loc_oprnd2.v.val_int = i;
11133 return loc_result;
11136 /* Return a location descriptor that designates a base+offset location. */
11138 static dw_loc_descr_ref
11139 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11140 enum var_init_status initialized)
11142 unsigned int regno;
11143 dw_loc_descr_ref result;
11144 dw_fde_ref fde = cfun->fde;
11146 /* We only use "frame base" when we're sure we're talking about the
11147 post-prologue local stack frame. We do this by *not* running
11148 register elimination until this point, and recognizing the special
11149 argument pointer and soft frame pointer rtx's. */
11150 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11152 rtx elim = (ira_use_lra_p
11153 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11154 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11156 if (elim != reg)
11158 if (GET_CODE (elim) == PLUS)
11160 offset += INTVAL (XEXP (elim, 1));
11161 elim = XEXP (elim, 0);
11163 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11164 && (elim == hard_frame_pointer_rtx
11165 || elim == stack_pointer_rtx))
11166 || elim == (frame_pointer_needed
11167 ? hard_frame_pointer_rtx
11168 : stack_pointer_rtx));
11170 /* If drap register is used to align stack, use frame
11171 pointer + offset to access stack variables. If stack
11172 is aligned without drap, use stack pointer + offset to
11173 access stack variables. */
11174 if (crtl->stack_realign_tried
11175 && reg == frame_pointer_rtx)
11177 int base_reg
11178 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11179 ? HARD_FRAME_POINTER_REGNUM
11180 : REGNO (elim));
11181 return new_reg_loc_descr (base_reg, offset);
11184 gcc_assert (frame_pointer_fb_offset_valid);
11185 offset += frame_pointer_fb_offset;
11186 return new_loc_descr (DW_OP_fbreg, offset, 0);
11190 regno = REGNO (reg);
11191 #ifdef LEAF_REG_REMAP
11192 if (crtl->uses_only_leaf_regs)
11194 int leaf_reg = LEAF_REG_REMAP (regno);
11195 if (leaf_reg != -1)
11196 regno = (unsigned) leaf_reg;
11198 #endif
11199 regno = DWARF_FRAME_REGNUM (regno);
11201 if (!optimize && fde
11202 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11204 /* Use cfa+offset to represent the location of arguments passed
11205 on the stack when drap is used to align stack.
11206 Only do this when not optimizing, for optimized code var-tracking
11207 is supposed to track where the arguments live and the register
11208 used as vdrap or drap in some spot might be used for something
11209 else in other part of the routine. */
11210 return new_loc_descr (DW_OP_fbreg, offset, 0);
11213 if (regno <= 31)
11214 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11215 offset, 0);
11216 else
11217 result = new_loc_descr (DW_OP_bregx, regno, offset);
11219 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11220 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11222 return result;
11225 /* Return true if this RTL expression describes a base+offset calculation. */
11227 static inline int
11228 is_based_loc (const_rtx rtl)
11230 return (GET_CODE (rtl) == PLUS
11231 && ((REG_P (XEXP (rtl, 0))
11232 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11233 && CONST_INT_P (XEXP (rtl, 1)))));
11236 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11237 failed. */
11239 static dw_loc_descr_ref
11240 tls_mem_loc_descriptor (rtx mem)
11242 tree base;
11243 dw_loc_descr_ref loc_result;
11245 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11246 return NULL;
11248 base = get_base_address (MEM_EXPR (mem));
11249 if (base == NULL
11250 || TREE_CODE (base) != VAR_DECL
11251 || !DECL_THREAD_LOCAL_P (base))
11252 return NULL;
11254 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11255 if (loc_result == NULL)
11256 return NULL;
11258 if (MEM_OFFSET (mem))
11259 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11261 return loc_result;
11264 /* Output debug info about reason why we failed to expand expression as dwarf
11265 expression. */
11267 static void
11268 expansion_failed (tree expr, rtx rtl, char const *reason)
11270 if (dump_file && (dump_flags & TDF_DETAILS))
11272 fprintf (dump_file, "Failed to expand as dwarf: ");
11273 if (expr)
11274 print_generic_expr (dump_file, expr, dump_flags);
11275 if (rtl)
11277 fprintf (dump_file, "\n");
11278 print_rtl (dump_file, rtl);
11280 fprintf (dump_file, "\nReason: %s\n", reason);
11284 /* Helper function for const_ok_for_output, called either directly
11285 or via for_each_rtx. */
11287 static int
11288 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11290 rtx rtl = *rtlp;
11292 if (GET_CODE (rtl) == UNSPEC)
11294 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11295 we can't express it in the debug info. */
11296 #ifdef ENABLE_CHECKING
11297 /* Don't complain about TLS UNSPECs, those are just too hard to
11298 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11299 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11300 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11301 if (XVECLEN (rtl, 0) == 0
11302 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11303 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11304 inform (current_function_decl
11305 ? DECL_SOURCE_LOCATION (current_function_decl)
11306 : UNKNOWN_LOCATION,
11307 #if NUM_UNSPEC_VALUES > 0
11308 "non-delegitimized UNSPEC %s (%d) found in variable location",
11309 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11310 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11311 XINT (rtl, 1));
11312 #else
11313 "non-delegitimized UNSPEC %d found in variable location",
11314 XINT (rtl, 1));
11315 #endif
11316 #endif
11317 expansion_failed (NULL_TREE, rtl,
11318 "UNSPEC hasn't been delegitimized.\n");
11319 return 1;
11322 if (targetm.const_not_ok_for_debug_p (rtl))
11324 expansion_failed (NULL_TREE, rtl,
11325 "Expression rejected for debug by the backend.\n");
11326 return 1;
11329 if (GET_CODE (rtl) != SYMBOL_REF)
11330 return 0;
11332 if (CONSTANT_POOL_ADDRESS_P (rtl))
11334 bool marked;
11335 get_pool_constant_mark (rtl, &marked);
11336 /* If all references to this pool constant were optimized away,
11337 it was not output and thus we can't represent it. */
11338 if (!marked)
11340 expansion_failed (NULL_TREE, rtl,
11341 "Constant was removed from constant pool.\n");
11342 return 1;
11346 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11347 return 1;
11349 /* Avoid references to external symbols in debug info, on several targets
11350 the linker might even refuse to link when linking a shared library,
11351 and in many other cases the relocations for .debug_info/.debug_loc are
11352 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11353 to be defined within the same shared library or executable are fine. */
11354 if (SYMBOL_REF_EXTERNAL_P (rtl))
11356 tree decl = SYMBOL_REF_DECL (rtl);
11358 if (decl == NULL || !targetm.binds_local_p (decl))
11360 expansion_failed (NULL_TREE, rtl,
11361 "Symbol not defined in current TU.\n");
11362 return 1;
11366 return 0;
11369 /* Return true if constant RTL can be emitted in DW_OP_addr or
11370 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11371 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11373 static bool
11374 const_ok_for_output (rtx rtl)
11376 if (GET_CODE (rtl) == SYMBOL_REF)
11377 return const_ok_for_output_1 (&rtl, NULL) == 0;
11379 if (GET_CODE (rtl) == CONST)
11380 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11382 return true;
11385 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11386 if possible, NULL otherwise. */
11388 static dw_die_ref
11389 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11391 dw_die_ref type_die;
11392 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11394 if (type == NULL)
11395 return NULL;
11396 switch (TREE_CODE (type))
11398 case INTEGER_TYPE:
11399 case REAL_TYPE:
11400 break;
11401 default:
11402 return NULL;
11404 type_die = lookup_type_die (type);
11405 if (!type_die)
11406 type_die = modified_type_die (type, false, false, comp_unit_die ());
11407 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11408 return NULL;
11409 return type_die;
11412 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11413 type matching MODE, or, if MODE is narrower than or as wide as
11414 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11415 possible. */
11417 static dw_loc_descr_ref
11418 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11420 enum machine_mode outer_mode = mode;
11421 dw_die_ref type_die;
11422 dw_loc_descr_ref cvt;
11424 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11426 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11427 return op;
11429 type_die = base_type_for_mode (outer_mode, 1);
11430 if (type_die == NULL)
11431 return NULL;
11432 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11433 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11434 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11435 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11436 add_loc_descr (&op, cvt);
11437 return op;
11440 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11442 static dw_loc_descr_ref
11443 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11444 dw_loc_descr_ref op1)
11446 dw_loc_descr_ref ret = op0;
11447 add_loc_descr (&ret, op1);
11448 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11449 if (STORE_FLAG_VALUE != 1)
11451 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11452 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11454 return ret;
11457 /* Return location descriptor for signed comparison OP RTL. */
11459 static dw_loc_descr_ref
11460 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11461 enum machine_mode mem_mode)
11463 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11464 dw_loc_descr_ref op0, op1;
11465 int shift;
11467 if (op_mode == VOIDmode)
11468 op_mode = GET_MODE (XEXP (rtl, 1));
11469 if (op_mode == VOIDmode)
11470 return NULL;
11472 if (dwarf_strict
11473 && (GET_MODE_CLASS (op_mode) != MODE_INT
11474 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11475 return NULL;
11477 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11478 VAR_INIT_STATUS_INITIALIZED);
11479 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11480 VAR_INIT_STATUS_INITIALIZED);
11482 if (op0 == NULL || op1 == NULL)
11483 return NULL;
11485 if (GET_MODE_CLASS (op_mode) != MODE_INT
11486 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11487 return compare_loc_descriptor (op, op0, op1);
11489 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11491 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11492 dw_loc_descr_ref cvt;
11494 if (type_die == NULL)
11495 return NULL;
11496 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11497 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11498 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11499 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11500 add_loc_descr (&op0, cvt);
11501 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11502 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11503 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11504 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11505 add_loc_descr (&op1, cvt);
11506 return compare_loc_descriptor (op, op0, op1);
11509 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11510 /* For eq/ne, if the operands are known to be zero-extended,
11511 there is no need to do the fancy shifting up. */
11512 if (op == DW_OP_eq || op == DW_OP_ne)
11514 dw_loc_descr_ref last0, last1;
11515 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11517 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11519 /* deref_size zero extends, and for constants we can check
11520 whether they are zero extended or not. */
11521 if (((last0->dw_loc_opc == DW_OP_deref_size
11522 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11523 || (CONST_INT_P (XEXP (rtl, 0))
11524 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11525 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11526 && ((last1->dw_loc_opc == DW_OP_deref_size
11527 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11528 || (CONST_INT_P (XEXP (rtl, 1))
11529 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11530 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11531 return compare_loc_descriptor (op, op0, op1);
11533 /* EQ/NE comparison against constant in narrower type than
11534 DWARF2_ADDR_SIZE can be performed either as
11535 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11536 DW_OP_{eq,ne}
11538 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11539 DW_OP_{eq,ne}. Pick whatever is shorter. */
11540 if (CONST_INT_P (XEXP (rtl, 1))
11541 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11542 && (size_of_int_loc_descriptor (shift) + 1
11543 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11544 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11545 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11546 & GET_MODE_MASK (op_mode))))
11548 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11549 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11550 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11551 & GET_MODE_MASK (op_mode));
11552 return compare_loc_descriptor (op, op0, op1);
11555 add_loc_descr (&op0, int_loc_descriptor (shift));
11556 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11557 if (CONST_INT_P (XEXP (rtl, 1)))
11558 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11559 else
11561 add_loc_descr (&op1, int_loc_descriptor (shift));
11562 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11564 return compare_loc_descriptor (op, op0, op1);
11567 /* Return location descriptor for unsigned comparison OP RTL. */
11569 static dw_loc_descr_ref
11570 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11571 enum machine_mode mem_mode)
11573 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11574 dw_loc_descr_ref op0, op1;
11576 if (op_mode == VOIDmode)
11577 op_mode = GET_MODE (XEXP (rtl, 1));
11578 if (op_mode == VOIDmode)
11579 return NULL;
11580 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11581 return NULL;
11583 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11584 return NULL;
11586 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11587 VAR_INIT_STATUS_INITIALIZED);
11588 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11589 VAR_INIT_STATUS_INITIALIZED);
11591 if (op0 == NULL || op1 == NULL)
11592 return NULL;
11594 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11596 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11597 dw_loc_descr_ref last0, last1;
11598 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11600 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11602 if (CONST_INT_P (XEXP (rtl, 0)))
11603 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11604 /* deref_size zero extends, so no need to mask it again. */
11605 else if (last0->dw_loc_opc != DW_OP_deref_size
11606 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11608 add_loc_descr (&op0, int_loc_descriptor (mask));
11609 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11611 if (CONST_INT_P (XEXP (rtl, 1)))
11612 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11613 /* deref_size zero extends, so no need to mask it again. */
11614 else if (last1->dw_loc_opc != DW_OP_deref_size
11615 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11617 add_loc_descr (&op1, int_loc_descriptor (mask));
11618 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11621 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11623 HOST_WIDE_INT bias = 1;
11624 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11625 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11626 if (CONST_INT_P (XEXP (rtl, 1)))
11627 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11628 + INTVAL (XEXP (rtl, 1)));
11629 else
11630 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11631 bias, 0));
11633 return compare_loc_descriptor (op, op0, op1);
11636 /* Return location descriptor for {U,S}{MIN,MAX}. */
11638 static dw_loc_descr_ref
11639 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11640 enum machine_mode mem_mode)
11642 enum dwarf_location_atom op;
11643 dw_loc_descr_ref op0, op1, ret;
11644 dw_loc_descr_ref bra_node, drop_node;
11646 if (dwarf_strict
11647 && (GET_MODE_CLASS (mode) != MODE_INT
11648 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11649 return NULL;
11651 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11652 VAR_INIT_STATUS_INITIALIZED);
11653 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11654 VAR_INIT_STATUS_INITIALIZED);
11656 if (op0 == NULL || op1 == NULL)
11657 return NULL;
11659 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11660 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11661 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11662 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11664 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11666 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11667 add_loc_descr (&op0, int_loc_descriptor (mask));
11668 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11669 add_loc_descr (&op1, int_loc_descriptor (mask));
11670 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11672 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11674 HOST_WIDE_INT bias = 1;
11675 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11676 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11677 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11680 else if (GET_MODE_CLASS (mode) == MODE_INT
11681 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11683 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11684 add_loc_descr (&op0, int_loc_descriptor (shift));
11685 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11686 add_loc_descr (&op1, int_loc_descriptor (shift));
11687 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11689 else if (GET_MODE_CLASS (mode) == MODE_INT
11690 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11692 dw_die_ref type_die = base_type_for_mode (mode, 0);
11693 dw_loc_descr_ref cvt;
11694 if (type_die == NULL)
11695 return NULL;
11696 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11697 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11698 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11699 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11700 add_loc_descr (&op0, cvt);
11701 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11702 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11703 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11704 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11705 add_loc_descr (&op1, cvt);
11708 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11709 op = DW_OP_lt;
11710 else
11711 op = DW_OP_gt;
11712 ret = op0;
11713 add_loc_descr (&ret, op1);
11714 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11715 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11716 add_loc_descr (&ret, bra_node);
11717 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11718 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11719 add_loc_descr (&ret, drop_node);
11720 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11721 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11722 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11723 && GET_MODE_CLASS (mode) == MODE_INT
11724 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11725 ret = convert_descriptor_to_mode (mode, ret);
11726 return ret;
11729 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11730 but after converting arguments to type_die, afterwards
11731 convert back to unsigned. */
11733 static dw_loc_descr_ref
11734 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11735 enum machine_mode mode, enum machine_mode mem_mode)
11737 dw_loc_descr_ref cvt, op0, op1;
11739 if (type_die == NULL)
11740 return NULL;
11741 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11742 VAR_INIT_STATUS_INITIALIZED);
11743 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11744 VAR_INIT_STATUS_INITIALIZED);
11745 if (op0 == NULL || op1 == NULL)
11746 return NULL;
11747 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11748 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11749 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11750 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11751 add_loc_descr (&op0, cvt);
11752 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11753 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11754 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11755 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11756 add_loc_descr (&op1, cvt);
11757 add_loc_descr (&op0, op1);
11758 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11759 return convert_descriptor_to_mode (mode, op0);
11762 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11763 const0 is DW_OP_lit0 or corresponding typed constant,
11764 const1 is DW_OP_lit1 or corresponding typed constant
11765 and constMSB is constant with just the MSB bit set
11766 for the mode):
11767 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11768 L1: const0 DW_OP_swap
11769 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11770 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11771 L3: DW_OP_drop
11772 L4: DW_OP_nop
11774 CTZ is similar:
11775 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11776 L1: const0 DW_OP_swap
11777 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11778 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11779 L3: DW_OP_drop
11780 L4: DW_OP_nop
11782 FFS is similar:
11783 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11784 L1: const1 DW_OP_swap
11785 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11786 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11787 L3: DW_OP_drop
11788 L4: DW_OP_nop */
11790 static dw_loc_descr_ref
11791 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11792 enum machine_mode mem_mode)
11794 dw_loc_descr_ref op0, ret, tmp;
11795 HOST_WIDE_INT valv;
11796 dw_loc_descr_ref l1jump, l1label;
11797 dw_loc_descr_ref l2jump, l2label;
11798 dw_loc_descr_ref l3jump, l3label;
11799 dw_loc_descr_ref l4jump, l4label;
11800 rtx msb;
11802 if (GET_MODE_CLASS (mode) != MODE_INT
11803 || GET_MODE (XEXP (rtl, 0)) != mode
11804 || (GET_CODE (rtl) == CLZ
11805 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11806 return NULL;
11808 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11809 VAR_INIT_STATUS_INITIALIZED);
11810 if (op0 == NULL)
11811 return NULL;
11812 ret = op0;
11813 if (GET_CODE (rtl) == CLZ)
11815 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11816 valv = GET_MODE_BITSIZE (mode);
11818 else if (GET_CODE (rtl) == FFS)
11819 valv = 0;
11820 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11821 valv = GET_MODE_BITSIZE (mode);
11822 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11823 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11824 add_loc_descr (&ret, l1jump);
11825 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11826 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11827 VAR_INIT_STATUS_INITIALIZED);
11828 if (tmp == NULL)
11829 return NULL;
11830 add_loc_descr (&ret, tmp);
11831 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11832 add_loc_descr (&ret, l4jump);
11833 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11834 ? const1_rtx : const0_rtx,
11835 mode, mem_mode,
11836 VAR_INIT_STATUS_INITIALIZED);
11837 if (l1label == NULL)
11838 return NULL;
11839 add_loc_descr (&ret, l1label);
11840 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11841 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11842 add_loc_descr (&ret, l2label);
11843 if (GET_CODE (rtl) != CLZ)
11844 msb = const1_rtx;
11845 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11846 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11847 << (GET_MODE_BITSIZE (mode) - 1));
11848 else
11849 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11850 << (GET_MODE_BITSIZE (mode)
11851 - HOST_BITS_PER_WIDE_INT - 1), mode);
11852 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11853 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11854 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11855 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11856 else
11857 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11858 VAR_INIT_STATUS_INITIALIZED);
11859 if (tmp == NULL)
11860 return NULL;
11861 add_loc_descr (&ret, tmp);
11862 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11863 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11864 add_loc_descr (&ret, l3jump);
11865 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11866 VAR_INIT_STATUS_INITIALIZED);
11867 if (tmp == NULL)
11868 return NULL;
11869 add_loc_descr (&ret, tmp);
11870 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11871 ? DW_OP_shl : DW_OP_shr, 0, 0));
11872 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11873 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11874 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11875 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11876 add_loc_descr (&ret, l2jump);
11877 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11878 add_loc_descr (&ret, l3label);
11879 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11880 add_loc_descr (&ret, l4label);
11881 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11882 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11883 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11884 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11885 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11886 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11887 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11888 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11889 return ret;
11892 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11893 const1 is DW_OP_lit1 or corresponding typed constant):
11894 const0 DW_OP_swap
11895 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11896 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11897 L2: DW_OP_drop
11899 PARITY is similar:
11900 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11901 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11902 L2: DW_OP_drop */
11904 static dw_loc_descr_ref
11905 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11906 enum machine_mode mem_mode)
11908 dw_loc_descr_ref op0, ret, tmp;
11909 dw_loc_descr_ref l1jump, l1label;
11910 dw_loc_descr_ref l2jump, l2label;
11912 if (GET_MODE_CLASS (mode) != MODE_INT
11913 || GET_MODE (XEXP (rtl, 0)) != mode)
11914 return NULL;
11916 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11917 VAR_INIT_STATUS_INITIALIZED);
11918 if (op0 == NULL)
11919 return NULL;
11920 ret = op0;
11921 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11922 VAR_INIT_STATUS_INITIALIZED);
11923 if (tmp == NULL)
11924 return NULL;
11925 add_loc_descr (&ret, tmp);
11926 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11927 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11928 add_loc_descr (&ret, l1label);
11929 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11930 add_loc_descr (&ret, l2jump);
11931 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11932 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11933 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11934 VAR_INIT_STATUS_INITIALIZED);
11935 if (tmp == NULL)
11936 return NULL;
11937 add_loc_descr (&ret, tmp);
11938 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11939 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11940 ? DW_OP_plus : DW_OP_xor, 0, 0));
11941 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11942 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11943 VAR_INIT_STATUS_INITIALIZED);
11944 add_loc_descr (&ret, tmp);
11945 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11946 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11947 add_loc_descr (&ret, l1jump);
11948 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11949 add_loc_descr (&ret, l2label);
11950 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11951 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11952 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11953 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11954 return ret;
11957 /* BSWAP (constS is initial shift count, either 56 or 24):
11958 constS const0
11959 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11960 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11961 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11962 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11963 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11965 static dw_loc_descr_ref
11966 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11967 enum machine_mode mem_mode)
11969 dw_loc_descr_ref op0, ret, tmp;
11970 dw_loc_descr_ref l1jump, l1label;
11971 dw_loc_descr_ref l2jump, l2label;
11973 if (GET_MODE_CLASS (mode) != MODE_INT
11974 || BITS_PER_UNIT != 8
11975 || (GET_MODE_BITSIZE (mode) != 32
11976 && GET_MODE_BITSIZE (mode) != 64))
11977 return NULL;
11979 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11980 VAR_INIT_STATUS_INITIALIZED);
11981 if (op0 == NULL)
11982 return NULL;
11984 ret = op0;
11985 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11986 mode, mem_mode,
11987 VAR_INIT_STATUS_INITIALIZED);
11988 if (tmp == NULL)
11989 return NULL;
11990 add_loc_descr (&ret, tmp);
11991 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11992 VAR_INIT_STATUS_INITIALIZED);
11993 if (tmp == NULL)
11994 return NULL;
11995 add_loc_descr (&ret, tmp);
11996 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11997 add_loc_descr (&ret, l1label);
11998 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11999 mode, mem_mode,
12000 VAR_INIT_STATUS_INITIALIZED);
12001 add_loc_descr (&ret, tmp);
12002 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12003 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12004 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12005 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12006 VAR_INIT_STATUS_INITIALIZED);
12007 if (tmp == NULL)
12008 return NULL;
12009 add_loc_descr (&ret, tmp);
12010 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12011 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12012 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12013 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12014 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12015 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12016 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12017 VAR_INIT_STATUS_INITIALIZED);
12018 add_loc_descr (&ret, tmp);
12019 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12020 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12021 add_loc_descr (&ret, l2jump);
12022 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12023 VAR_INIT_STATUS_INITIALIZED);
12024 add_loc_descr (&ret, tmp);
12025 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12026 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12027 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12028 add_loc_descr (&ret, l1jump);
12029 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12030 add_loc_descr (&ret, l2label);
12031 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12032 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12033 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12034 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12035 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12036 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12037 return ret;
12040 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12041 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12042 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12043 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12045 ROTATERT is similar:
12046 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12047 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12048 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12050 static dw_loc_descr_ref
12051 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12052 enum machine_mode mem_mode)
12054 rtx rtlop1 = XEXP (rtl, 1);
12055 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12056 int i;
12058 if (GET_MODE_CLASS (mode) != MODE_INT)
12059 return NULL;
12061 if (GET_MODE (rtlop1) != VOIDmode
12062 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12063 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12064 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12065 VAR_INIT_STATUS_INITIALIZED);
12066 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12067 VAR_INIT_STATUS_INITIALIZED);
12068 if (op0 == NULL || op1 == NULL)
12069 return NULL;
12070 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12071 for (i = 0; i < 2; i++)
12073 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12074 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12075 mode, mem_mode,
12076 VAR_INIT_STATUS_INITIALIZED);
12077 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12078 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12079 ? DW_OP_const4u
12080 : HOST_BITS_PER_WIDE_INT == 64
12081 ? DW_OP_const8u : DW_OP_constu,
12082 GET_MODE_MASK (mode), 0);
12083 else
12084 mask[i] = NULL;
12085 if (mask[i] == NULL)
12086 return NULL;
12087 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12089 ret = op0;
12090 add_loc_descr (&ret, op1);
12091 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12092 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12093 if (GET_CODE (rtl) == ROTATERT)
12095 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12096 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12097 GET_MODE_BITSIZE (mode), 0));
12099 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12100 if (mask[0] != NULL)
12101 add_loc_descr (&ret, mask[0]);
12102 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12103 if (mask[1] != NULL)
12105 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12106 add_loc_descr (&ret, mask[1]);
12107 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12109 if (GET_CODE (rtl) == ROTATE)
12111 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12112 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12113 GET_MODE_BITSIZE (mode), 0));
12115 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12116 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12117 return ret;
12120 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12121 for DEBUG_PARAMETER_REF RTL. */
12123 static dw_loc_descr_ref
12124 parameter_ref_descriptor (rtx rtl)
12126 dw_loc_descr_ref ret;
12127 dw_die_ref ref;
12129 if (dwarf_strict)
12130 return NULL;
12131 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12132 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12133 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12134 if (ref)
12136 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12137 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12138 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12140 else
12142 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12143 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12145 return ret;
12148 /* The following routine converts the RTL for a variable or parameter
12149 (resident in memory) into an equivalent Dwarf representation of a
12150 mechanism for getting the address of that same variable onto the top of a
12151 hypothetical "address evaluation" stack.
12153 When creating memory location descriptors, we are effectively transforming
12154 the RTL for a memory-resident object into its Dwarf postfix expression
12155 equivalent. This routine recursively descends an RTL tree, turning
12156 it into Dwarf postfix code as it goes.
12158 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12160 MEM_MODE is the mode of the memory reference, needed to handle some
12161 autoincrement addressing modes.
12163 Return 0 if we can't represent the location. */
12165 dw_loc_descr_ref
12166 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12167 enum machine_mode mem_mode,
12168 enum var_init_status initialized)
12170 dw_loc_descr_ref mem_loc_result = NULL;
12171 enum dwarf_location_atom op;
12172 dw_loc_descr_ref op0, op1;
12173 rtx inner = NULL_RTX;
12175 if (mode == VOIDmode)
12176 mode = GET_MODE (rtl);
12178 /* Note that for a dynamically sized array, the location we will generate a
12179 description of here will be the lowest numbered location which is
12180 actually within the array. That's *not* necessarily the same as the
12181 zeroth element of the array. */
12183 rtl = targetm.delegitimize_address (rtl);
12185 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12186 return NULL;
12188 switch (GET_CODE (rtl))
12190 case POST_INC:
12191 case POST_DEC:
12192 case POST_MODIFY:
12193 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12195 case SUBREG:
12196 /* The case of a subreg may arise when we have a local (register)
12197 variable or a formal (register) parameter which doesn't quite fill
12198 up an entire register. For now, just assume that it is
12199 legitimate to make the Dwarf info refer to the whole register which
12200 contains the given subreg. */
12201 if (!subreg_lowpart_p (rtl))
12202 break;
12203 inner = SUBREG_REG (rtl);
12204 case TRUNCATE:
12205 if (inner == NULL_RTX)
12206 inner = XEXP (rtl, 0);
12207 if (GET_MODE_CLASS (mode) == MODE_INT
12208 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12209 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12210 #ifdef POINTERS_EXTEND_UNSIGNED
12211 || (mode == Pmode && mem_mode != VOIDmode)
12212 #endif
12214 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12216 mem_loc_result = mem_loc_descriptor (inner,
12217 GET_MODE (inner),
12218 mem_mode, initialized);
12219 break;
12221 if (dwarf_strict)
12222 break;
12223 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12224 break;
12225 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12226 && (GET_MODE_CLASS (mode) != MODE_INT
12227 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12228 break;
12229 else
12231 dw_die_ref type_die;
12232 dw_loc_descr_ref cvt;
12234 mem_loc_result = mem_loc_descriptor (inner,
12235 GET_MODE (inner),
12236 mem_mode, initialized);
12237 if (mem_loc_result == NULL)
12238 break;
12239 type_die = base_type_for_mode (mode,
12240 GET_MODE_CLASS (mode) == MODE_INT);
12241 if (type_die == NULL)
12243 mem_loc_result = NULL;
12244 break;
12246 if (GET_MODE_SIZE (mode)
12247 != GET_MODE_SIZE (GET_MODE (inner)))
12248 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12249 else
12250 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12251 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12252 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12253 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12254 add_loc_descr (&mem_loc_result, cvt);
12256 break;
12258 case REG:
12259 if (GET_MODE_CLASS (mode) != MODE_INT
12260 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12261 && rtl != arg_pointer_rtx
12262 && rtl != frame_pointer_rtx
12263 #ifdef POINTERS_EXTEND_UNSIGNED
12264 && (mode != Pmode || mem_mode == VOIDmode)
12265 #endif
12268 dw_die_ref type_die;
12269 unsigned int dbx_regnum;
12271 if (dwarf_strict)
12272 break;
12273 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12274 break;
12275 type_die = base_type_for_mode (mode,
12276 GET_MODE_CLASS (mode) == MODE_INT);
12277 if (type_die == NULL)
12278 break;
12280 dbx_regnum = dbx_reg_number (rtl);
12281 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12282 break;
12283 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12284 dbx_regnum, 0);
12285 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12286 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12287 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12288 break;
12290 /* Whenever a register number forms a part of the description of the
12291 method for calculating the (dynamic) address of a memory resident
12292 object, DWARF rules require the register number be referred to as
12293 a "base register". This distinction is not based in any way upon
12294 what category of register the hardware believes the given register
12295 belongs to. This is strictly DWARF terminology we're dealing with
12296 here. Note that in cases where the location of a memory-resident
12297 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12298 OP_CONST (0)) the actual DWARF location descriptor that we generate
12299 may just be OP_BASEREG (basereg). This may look deceptively like
12300 the object in question was allocated to a register (rather than in
12301 memory) so DWARF consumers need to be aware of the subtle
12302 distinction between OP_REG and OP_BASEREG. */
12303 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12304 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12305 else if (stack_realign_drap
12306 && crtl->drap_reg
12307 && crtl->args.internal_arg_pointer == rtl
12308 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12310 /* If RTL is internal_arg_pointer, which has been optimized
12311 out, use DRAP instead. */
12312 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12313 VAR_INIT_STATUS_INITIALIZED);
12315 break;
12317 case SIGN_EXTEND:
12318 case ZERO_EXTEND:
12319 if (GET_MODE_CLASS (mode) != MODE_INT)
12320 break;
12321 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12322 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12323 if (op0 == 0)
12324 break;
12325 else if (GET_CODE (rtl) == ZERO_EXTEND
12326 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12327 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12328 < HOST_BITS_PER_WIDE_INT
12329 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12330 to expand zero extend as two shifts instead of
12331 masking. */
12332 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12334 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12335 mem_loc_result = op0;
12336 add_loc_descr (&mem_loc_result,
12337 int_loc_descriptor (GET_MODE_MASK (imode)));
12338 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12340 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12342 int shift = DWARF2_ADDR_SIZE
12343 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12344 shift *= BITS_PER_UNIT;
12345 if (GET_CODE (rtl) == SIGN_EXTEND)
12346 op = DW_OP_shra;
12347 else
12348 op = DW_OP_shr;
12349 mem_loc_result = op0;
12350 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12351 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12352 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12353 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12355 else if (!dwarf_strict)
12357 dw_die_ref type_die1, type_die2;
12358 dw_loc_descr_ref cvt;
12360 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12361 GET_CODE (rtl) == ZERO_EXTEND);
12362 if (type_die1 == NULL)
12363 break;
12364 type_die2 = base_type_for_mode (mode, 1);
12365 if (type_die2 == NULL)
12366 break;
12367 mem_loc_result = op0;
12368 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12369 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12370 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12371 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12372 add_loc_descr (&mem_loc_result, cvt);
12373 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12374 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12375 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12376 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12377 add_loc_descr (&mem_loc_result, cvt);
12379 break;
12381 case MEM:
12383 rtx new_rtl = avoid_constant_pool_reference (rtl);
12384 if (new_rtl != rtl)
12386 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12387 initialized);
12388 if (mem_loc_result != NULL)
12389 return mem_loc_result;
12392 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12393 get_address_mode (rtl), mode,
12394 VAR_INIT_STATUS_INITIALIZED);
12395 if (mem_loc_result == NULL)
12396 mem_loc_result = tls_mem_loc_descriptor (rtl);
12397 if (mem_loc_result != NULL)
12399 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12400 || GET_MODE_CLASS (mode) != MODE_INT)
12402 dw_die_ref type_die;
12403 dw_loc_descr_ref deref;
12405 if (dwarf_strict)
12406 return NULL;
12407 type_die
12408 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12409 if (type_die == NULL)
12410 return NULL;
12411 deref = new_loc_descr (DW_OP_GNU_deref_type,
12412 GET_MODE_SIZE (mode), 0);
12413 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12414 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12415 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12416 add_loc_descr (&mem_loc_result, deref);
12418 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12419 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12420 else
12421 add_loc_descr (&mem_loc_result,
12422 new_loc_descr (DW_OP_deref_size,
12423 GET_MODE_SIZE (mode), 0));
12425 break;
12427 case LO_SUM:
12428 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12430 case LABEL_REF:
12431 /* Some ports can transform a symbol ref into a label ref, because
12432 the symbol ref is too far away and has to be dumped into a constant
12433 pool. */
12434 case CONST:
12435 case SYMBOL_REF:
12436 if (GET_MODE_CLASS (mode) != MODE_INT
12437 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12438 #ifdef POINTERS_EXTEND_UNSIGNED
12439 && (mode != Pmode || mem_mode == VOIDmode)
12440 #endif
12442 break;
12443 if (GET_CODE (rtl) == SYMBOL_REF
12444 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12446 dw_loc_descr_ref temp;
12448 /* If this is not defined, we have no way to emit the data. */
12449 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12450 break;
12452 temp = new_addr_loc_descr (rtl, dtprel_true);
12454 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12455 add_loc_descr (&mem_loc_result, temp);
12457 break;
12460 if (!const_ok_for_output (rtl))
12461 break;
12463 symref:
12464 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12465 vec_safe_push (used_rtx_array, rtl);
12466 break;
12468 case CONCAT:
12469 case CONCATN:
12470 case VAR_LOCATION:
12471 case DEBUG_IMPLICIT_PTR:
12472 expansion_failed (NULL_TREE, rtl,
12473 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12474 return 0;
12476 case ENTRY_VALUE:
12477 if (dwarf_strict)
12478 return NULL;
12479 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12481 if (GET_MODE_CLASS (mode) != MODE_INT
12482 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12483 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12484 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12485 else
12487 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12488 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12489 return NULL;
12490 op0 = one_reg_loc_descriptor (dbx_regnum,
12491 VAR_INIT_STATUS_INITIALIZED);
12494 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12495 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12497 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12498 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12499 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12500 return NULL;
12502 else
12503 gcc_unreachable ();
12504 if (op0 == NULL)
12505 return NULL;
12506 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12507 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12508 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12509 break;
12511 case DEBUG_PARAMETER_REF:
12512 mem_loc_result = parameter_ref_descriptor (rtl);
12513 break;
12515 case PRE_MODIFY:
12516 /* Extract the PLUS expression nested inside and fall into
12517 PLUS code below. */
12518 rtl = XEXP (rtl, 1);
12519 goto plus;
12521 case PRE_INC:
12522 case PRE_DEC:
12523 /* Turn these into a PLUS expression and fall into the PLUS code
12524 below. */
12525 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12526 gen_int_mode (GET_CODE (rtl) == PRE_INC
12527 ? GET_MODE_UNIT_SIZE (mem_mode)
12528 : -GET_MODE_UNIT_SIZE (mem_mode),
12529 mode));
12531 /* ... fall through ... */
12533 case PLUS:
12534 plus:
12535 if (is_based_loc (rtl)
12536 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12537 || XEXP (rtl, 0) == arg_pointer_rtx
12538 || XEXP (rtl, 0) == frame_pointer_rtx)
12539 && GET_MODE_CLASS (mode) == MODE_INT)
12540 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12541 INTVAL (XEXP (rtl, 1)),
12542 VAR_INIT_STATUS_INITIALIZED);
12543 else
12545 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12546 VAR_INIT_STATUS_INITIALIZED);
12547 if (mem_loc_result == 0)
12548 break;
12550 if (CONST_INT_P (XEXP (rtl, 1))
12551 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12552 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12553 else
12555 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12556 VAR_INIT_STATUS_INITIALIZED);
12557 if (op1 == 0)
12558 break;
12559 add_loc_descr (&mem_loc_result, op1);
12560 add_loc_descr (&mem_loc_result,
12561 new_loc_descr (DW_OP_plus, 0, 0));
12564 break;
12566 /* If a pseudo-reg is optimized away, it is possible for it to
12567 be replaced with a MEM containing a multiply or shift. */
12568 case MINUS:
12569 op = DW_OP_minus;
12570 goto do_binop;
12572 case MULT:
12573 op = DW_OP_mul;
12574 goto do_binop;
12576 case DIV:
12577 if (!dwarf_strict
12578 && GET_MODE_CLASS (mode) == MODE_INT
12579 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12581 mem_loc_result = typed_binop (DW_OP_div, rtl,
12582 base_type_for_mode (mode, 0),
12583 mode, mem_mode);
12584 break;
12586 op = DW_OP_div;
12587 goto do_binop;
12589 case UMOD:
12590 op = DW_OP_mod;
12591 goto do_binop;
12593 case ASHIFT:
12594 op = DW_OP_shl;
12595 goto do_shift;
12597 case ASHIFTRT:
12598 op = DW_OP_shra;
12599 goto do_shift;
12601 case LSHIFTRT:
12602 op = DW_OP_shr;
12603 goto do_shift;
12605 do_shift:
12606 if (GET_MODE_CLASS (mode) != MODE_INT)
12607 break;
12608 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12609 VAR_INIT_STATUS_INITIALIZED);
12611 rtx rtlop1 = XEXP (rtl, 1);
12612 if (GET_MODE (rtlop1) != VOIDmode
12613 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12614 < GET_MODE_BITSIZE (mode))
12615 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12616 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12617 VAR_INIT_STATUS_INITIALIZED);
12620 if (op0 == 0 || op1 == 0)
12621 break;
12623 mem_loc_result = op0;
12624 add_loc_descr (&mem_loc_result, op1);
12625 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12626 break;
12628 case AND:
12629 op = DW_OP_and;
12630 goto do_binop;
12632 case IOR:
12633 op = DW_OP_or;
12634 goto do_binop;
12636 case XOR:
12637 op = DW_OP_xor;
12638 goto do_binop;
12640 do_binop:
12641 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12642 VAR_INIT_STATUS_INITIALIZED);
12643 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12644 VAR_INIT_STATUS_INITIALIZED);
12646 if (op0 == 0 || op1 == 0)
12647 break;
12649 mem_loc_result = op0;
12650 add_loc_descr (&mem_loc_result, op1);
12651 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12652 break;
12654 case MOD:
12655 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12657 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12658 base_type_for_mode (mode, 0),
12659 mode, mem_mode);
12660 break;
12663 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12664 VAR_INIT_STATUS_INITIALIZED);
12665 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12666 VAR_INIT_STATUS_INITIALIZED);
12668 if (op0 == 0 || op1 == 0)
12669 break;
12671 mem_loc_result = op0;
12672 add_loc_descr (&mem_loc_result, op1);
12673 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12674 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12675 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12676 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12677 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12678 break;
12680 case UDIV:
12681 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12683 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12685 op = DW_OP_div;
12686 goto do_binop;
12688 mem_loc_result = typed_binop (DW_OP_div, rtl,
12689 base_type_for_mode (mode, 1),
12690 mode, mem_mode);
12692 break;
12694 case NOT:
12695 op = DW_OP_not;
12696 goto do_unop;
12698 case ABS:
12699 op = DW_OP_abs;
12700 goto do_unop;
12702 case NEG:
12703 op = DW_OP_neg;
12704 goto do_unop;
12706 do_unop:
12707 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12708 VAR_INIT_STATUS_INITIALIZED);
12710 if (op0 == 0)
12711 break;
12713 mem_loc_result = op0;
12714 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12715 break;
12717 case CONST_INT:
12718 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12719 #ifdef POINTERS_EXTEND_UNSIGNED
12720 || (mode == Pmode
12721 && mem_mode != VOIDmode
12722 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12723 #endif
12726 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12727 break;
12729 if (!dwarf_strict
12730 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12731 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12733 dw_die_ref type_die = base_type_for_mode (mode, 1);
12734 enum machine_mode amode;
12735 if (type_die == NULL)
12736 return NULL;
12737 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12738 MODE_INT, 0);
12739 if (INTVAL (rtl) >= 0
12740 && amode != BLKmode
12741 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12742 /* const DW_OP_GNU_convert <XXX> vs.
12743 DW_OP_GNU_const_type <XXX, 1, const>. */
12744 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12745 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12747 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12748 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12749 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12750 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12751 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12752 add_loc_descr (&mem_loc_result, op0);
12753 return mem_loc_result;
12755 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12756 INTVAL (rtl));
12757 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12758 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12759 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12760 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12761 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12762 else
12764 mem_loc_result->dw_loc_oprnd2.val_class
12765 = dw_val_class_const_double;
12766 mem_loc_result->dw_loc_oprnd2.v.val_double
12767 = double_int::from_shwi (INTVAL (rtl));
12770 break;
12772 case CONST_DOUBLE:
12773 if (!dwarf_strict)
12775 dw_die_ref type_die;
12777 /* Note that a CONST_DOUBLE rtx could represent either an integer
12778 or a floating-point constant. A CONST_DOUBLE is used whenever
12779 the constant requires more than one word in order to be
12780 adequately represented. We output CONST_DOUBLEs as blocks. */
12781 if (mode == VOIDmode
12782 || (GET_MODE (rtl) == VOIDmode
12783 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12784 break;
12785 type_die = base_type_for_mode (mode,
12786 GET_MODE_CLASS (mode) == MODE_INT);
12787 if (type_die == NULL)
12788 return NULL;
12789 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12790 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12791 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12792 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12793 if (SCALAR_FLOAT_MODE_P (mode))
12795 unsigned int length = GET_MODE_SIZE (mode);
12796 unsigned char *array
12797 = (unsigned char*) ggc_alloc_atomic (length);
12799 insert_float (rtl, array);
12800 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12801 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12802 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12803 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12805 else
12807 mem_loc_result->dw_loc_oprnd2.val_class
12808 = dw_val_class_const_double;
12809 mem_loc_result->dw_loc_oprnd2.v.val_double
12810 = rtx_to_double_int (rtl);
12813 break;
12815 case EQ:
12816 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12817 break;
12819 case GE:
12820 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12821 break;
12823 case GT:
12824 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12825 break;
12827 case LE:
12828 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12829 break;
12831 case LT:
12832 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12833 break;
12835 case NE:
12836 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12837 break;
12839 case GEU:
12840 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12841 break;
12843 case GTU:
12844 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12845 break;
12847 case LEU:
12848 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12849 break;
12851 case LTU:
12852 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12853 break;
12855 case UMIN:
12856 case UMAX:
12857 if (GET_MODE_CLASS (mode) != MODE_INT)
12858 break;
12859 /* FALLTHRU */
12860 case SMIN:
12861 case SMAX:
12862 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12863 break;
12865 case ZERO_EXTRACT:
12866 case SIGN_EXTRACT:
12867 if (CONST_INT_P (XEXP (rtl, 1))
12868 && CONST_INT_P (XEXP (rtl, 2))
12869 && ((unsigned) INTVAL (XEXP (rtl, 1))
12870 + (unsigned) INTVAL (XEXP (rtl, 2))
12871 <= GET_MODE_BITSIZE (mode))
12872 && GET_MODE_CLASS (mode) == MODE_INT
12873 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12874 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12876 int shift, size;
12877 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12878 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12879 if (op0 == 0)
12880 break;
12881 if (GET_CODE (rtl) == SIGN_EXTRACT)
12882 op = DW_OP_shra;
12883 else
12884 op = DW_OP_shr;
12885 mem_loc_result = op0;
12886 size = INTVAL (XEXP (rtl, 1));
12887 shift = INTVAL (XEXP (rtl, 2));
12888 if (BITS_BIG_ENDIAN)
12889 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12890 - shift - size;
12891 if (shift + size != (int) DWARF2_ADDR_SIZE)
12893 add_loc_descr (&mem_loc_result,
12894 int_loc_descriptor (DWARF2_ADDR_SIZE
12895 - shift - size));
12896 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12898 if (size != (int) DWARF2_ADDR_SIZE)
12900 add_loc_descr (&mem_loc_result,
12901 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12902 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12905 break;
12907 case IF_THEN_ELSE:
12909 dw_loc_descr_ref op2, bra_node, drop_node;
12910 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12911 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12912 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12913 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12914 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12915 VAR_INIT_STATUS_INITIALIZED);
12916 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12917 VAR_INIT_STATUS_INITIALIZED);
12918 if (op0 == NULL || op1 == NULL || op2 == NULL)
12919 break;
12921 mem_loc_result = op1;
12922 add_loc_descr (&mem_loc_result, op2);
12923 add_loc_descr (&mem_loc_result, op0);
12924 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12925 add_loc_descr (&mem_loc_result, bra_node);
12926 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12927 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12928 add_loc_descr (&mem_loc_result, drop_node);
12929 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12930 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12932 break;
12934 case FLOAT_EXTEND:
12935 case FLOAT_TRUNCATE:
12936 case FLOAT:
12937 case UNSIGNED_FLOAT:
12938 case FIX:
12939 case UNSIGNED_FIX:
12940 if (!dwarf_strict)
12942 dw_die_ref type_die;
12943 dw_loc_descr_ref cvt;
12945 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12946 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12947 if (op0 == NULL)
12948 break;
12949 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12950 && (GET_CODE (rtl) == FLOAT
12951 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12952 <= DWARF2_ADDR_SIZE))
12954 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12955 GET_CODE (rtl) == UNSIGNED_FLOAT);
12956 if (type_die == NULL)
12957 break;
12958 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12959 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12960 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12961 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12962 add_loc_descr (&op0, cvt);
12964 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12965 if (type_die == NULL)
12966 break;
12967 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12968 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12969 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12970 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12971 add_loc_descr (&op0, cvt);
12972 if (GET_MODE_CLASS (mode) == MODE_INT
12973 && (GET_CODE (rtl) == FIX
12974 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12976 op0 = convert_descriptor_to_mode (mode, op0);
12977 if (op0 == NULL)
12978 break;
12980 mem_loc_result = op0;
12982 break;
12984 case CLZ:
12985 case CTZ:
12986 case FFS:
12987 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12988 break;
12990 case POPCOUNT:
12991 case PARITY:
12992 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12993 break;
12995 case BSWAP:
12996 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12997 break;
12999 case ROTATE:
13000 case ROTATERT:
13001 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13002 break;
13004 case COMPARE:
13005 /* In theory, we could implement the above. */
13006 /* DWARF cannot represent the unsigned compare operations
13007 natively. */
13008 case SS_MULT:
13009 case US_MULT:
13010 case SS_DIV:
13011 case US_DIV:
13012 case SS_PLUS:
13013 case US_PLUS:
13014 case SS_MINUS:
13015 case US_MINUS:
13016 case SS_NEG:
13017 case US_NEG:
13018 case SS_ABS:
13019 case SS_ASHIFT:
13020 case US_ASHIFT:
13021 case SS_TRUNCATE:
13022 case US_TRUNCATE:
13023 case UNORDERED:
13024 case ORDERED:
13025 case UNEQ:
13026 case UNGE:
13027 case UNGT:
13028 case UNLE:
13029 case UNLT:
13030 case LTGT:
13031 case FRACT_CONVERT:
13032 case UNSIGNED_FRACT_CONVERT:
13033 case SAT_FRACT:
13034 case UNSIGNED_SAT_FRACT:
13035 case SQRT:
13036 case ASM_OPERANDS:
13037 case VEC_MERGE:
13038 case VEC_SELECT:
13039 case VEC_CONCAT:
13040 case VEC_DUPLICATE:
13041 case UNSPEC:
13042 case HIGH:
13043 case FMA:
13044 case STRICT_LOW_PART:
13045 case CONST_VECTOR:
13046 case CONST_FIXED:
13047 case CLRSB:
13048 case CLOBBER:
13049 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13050 can't express it in the debug info. This can happen e.g. with some
13051 TLS UNSPECs. */
13052 break;
13054 case CONST_STRING:
13055 resolve_one_addr (&rtl, NULL);
13056 goto symref;
13058 default:
13059 #ifdef ENABLE_CHECKING
13060 print_rtl (stderr, rtl);
13061 gcc_unreachable ();
13062 #else
13063 break;
13064 #endif
13067 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13068 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13070 return mem_loc_result;
13073 /* Return a descriptor that describes the concatenation of two locations.
13074 This is typically a complex variable. */
13076 static dw_loc_descr_ref
13077 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13079 dw_loc_descr_ref cc_loc_result = NULL;
13080 dw_loc_descr_ref x0_ref
13081 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13082 dw_loc_descr_ref x1_ref
13083 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13085 if (x0_ref == 0 || x1_ref == 0)
13086 return 0;
13088 cc_loc_result = x0_ref;
13089 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13091 add_loc_descr (&cc_loc_result, x1_ref);
13092 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13094 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13095 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13097 return cc_loc_result;
13100 /* Return a descriptor that describes the concatenation of N
13101 locations. */
13103 static dw_loc_descr_ref
13104 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13106 unsigned int i;
13107 dw_loc_descr_ref cc_loc_result = NULL;
13108 unsigned int n = XVECLEN (concatn, 0);
13110 for (i = 0; i < n; ++i)
13112 dw_loc_descr_ref ref;
13113 rtx x = XVECEXP (concatn, 0, i);
13115 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13116 if (ref == NULL)
13117 return NULL;
13119 add_loc_descr (&cc_loc_result, ref);
13120 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13123 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13124 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13126 return cc_loc_result;
13129 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13130 for DEBUG_IMPLICIT_PTR RTL. */
13132 static dw_loc_descr_ref
13133 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13135 dw_loc_descr_ref ret;
13136 dw_die_ref ref;
13138 if (dwarf_strict)
13139 return NULL;
13140 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13141 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13142 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13143 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13144 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13145 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13146 if (ref)
13148 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13149 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13150 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13152 else
13154 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13155 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13157 return ret;
13160 /* Output a proper Dwarf location descriptor for a variable or parameter
13161 which is either allocated in a register or in a memory location. For a
13162 register, we just generate an OP_REG and the register number. For a
13163 memory location we provide a Dwarf postfix expression describing how to
13164 generate the (dynamic) address of the object onto the address stack.
13166 MODE is mode of the decl if this loc_descriptor is going to be used in
13167 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13168 allowed, VOIDmode otherwise.
13170 If we don't know how to describe it, return 0. */
13172 static dw_loc_descr_ref
13173 loc_descriptor (rtx rtl, enum machine_mode mode,
13174 enum var_init_status initialized)
13176 dw_loc_descr_ref loc_result = NULL;
13178 switch (GET_CODE (rtl))
13180 case SUBREG:
13181 /* The case of a subreg may arise when we have a local (register)
13182 variable or a formal (register) parameter which doesn't quite fill
13183 up an entire register. For now, just assume that it is
13184 legitimate to make the Dwarf info refer to the whole register which
13185 contains the given subreg. */
13186 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13187 loc_result = loc_descriptor (SUBREG_REG (rtl),
13188 GET_MODE (SUBREG_REG (rtl)), initialized);
13189 else
13190 goto do_default;
13191 break;
13193 case REG:
13194 loc_result = reg_loc_descriptor (rtl, initialized);
13195 break;
13197 case MEM:
13198 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13199 GET_MODE (rtl), initialized);
13200 if (loc_result == NULL)
13201 loc_result = tls_mem_loc_descriptor (rtl);
13202 if (loc_result == NULL)
13204 rtx new_rtl = avoid_constant_pool_reference (rtl);
13205 if (new_rtl != rtl)
13206 loc_result = loc_descriptor (new_rtl, mode, initialized);
13208 break;
13210 case CONCAT:
13211 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13212 initialized);
13213 break;
13215 case CONCATN:
13216 loc_result = concatn_loc_descriptor (rtl, initialized);
13217 break;
13219 case VAR_LOCATION:
13220 /* Single part. */
13221 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13223 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13224 if (GET_CODE (loc) == EXPR_LIST)
13225 loc = XEXP (loc, 0);
13226 loc_result = loc_descriptor (loc, mode, initialized);
13227 break;
13230 rtl = XEXP (rtl, 1);
13231 /* FALLTHRU */
13233 case PARALLEL:
13235 rtvec par_elems = XVEC (rtl, 0);
13236 int num_elem = GET_NUM_ELEM (par_elems);
13237 enum machine_mode mode;
13238 int i;
13240 /* Create the first one, so we have something to add to. */
13241 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13242 VOIDmode, initialized);
13243 if (loc_result == NULL)
13244 return NULL;
13245 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13246 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13247 for (i = 1; i < num_elem; i++)
13249 dw_loc_descr_ref temp;
13251 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13252 VOIDmode, initialized);
13253 if (temp == NULL)
13254 return NULL;
13255 add_loc_descr (&loc_result, temp);
13256 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13257 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13260 break;
13262 case CONST_INT:
13263 if (mode != VOIDmode && mode != BLKmode)
13264 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13265 INTVAL (rtl));
13266 break;
13268 case CONST_DOUBLE:
13269 if (mode == VOIDmode)
13270 mode = GET_MODE (rtl);
13272 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13274 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13276 /* Note that a CONST_DOUBLE rtx could represent either an integer
13277 or a floating-point constant. A CONST_DOUBLE is used whenever
13278 the constant requires more than one word in order to be
13279 adequately represented. We output CONST_DOUBLEs as blocks. */
13280 loc_result = new_loc_descr (DW_OP_implicit_value,
13281 GET_MODE_SIZE (mode), 0);
13282 if (SCALAR_FLOAT_MODE_P (mode))
13284 unsigned int length = GET_MODE_SIZE (mode);
13285 unsigned char *array
13286 = (unsigned char*) ggc_alloc_atomic (length);
13288 insert_float (rtl, array);
13289 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13290 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13291 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13292 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13294 else
13296 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13297 loc_result->dw_loc_oprnd2.v.val_double
13298 = rtx_to_double_int (rtl);
13301 break;
13303 case CONST_VECTOR:
13304 if (mode == VOIDmode)
13305 mode = GET_MODE (rtl);
13307 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13309 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13310 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13311 unsigned char *array = (unsigned char *)
13312 ggc_alloc_atomic (length * elt_size);
13313 unsigned int i;
13314 unsigned char *p;
13316 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13317 switch (GET_MODE_CLASS (mode))
13319 case MODE_VECTOR_INT:
13320 for (i = 0, p = array; i < length; i++, p += elt_size)
13322 rtx elt = CONST_VECTOR_ELT (rtl, i);
13323 double_int val = rtx_to_double_int (elt);
13325 if (elt_size <= sizeof (HOST_WIDE_INT))
13326 insert_int (val.to_shwi (), elt_size, p);
13327 else
13329 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13330 insert_double (val, p);
13333 break;
13335 case MODE_VECTOR_FLOAT:
13336 for (i = 0, p = array; i < length; i++, p += elt_size)
13338 rtx elt = CONST_VECTOR_ELT (rtl, i);
13339 insert_float (elt, p);
13341 break;
13343 default:
13344 gcc_unreachable ();
13347 loc_result = new_loc_descr (DW_OP_implicit_value,
13348 length * elt_size, 0);
13349 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13350 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13351 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13352 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13354 break;
13356 case CONST:
13357 if (mode == VOIDmode
13358 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13359 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13360 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13362 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13363 break;
13365 /* FALLTHROUGH */
13366 case SYMBOL_REF:
13367 if (!const_ok_for_output (rtl))
13368 break;
13369 case LABEL_REF:
13370 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13371 && (dwarf_version >= 4 || !dwarf_strict))
13373 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13374 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13375 vec_safe_push (used_rtx_array, rtl);
13377 break;
13379 case DEBUG_IMPLICIT_PTR:
13380 loc_result = implicit_ptr_descriptor (rtl, 0);
13381 break;
13383 case PLUS:
13384 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13385 && CONST_INT_P (XEXP (rtl, 1)))
13387 loc_result
13388 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13389 break;
13391 /* FALLTHRU */
13392 do_default:
13393 default:
13394 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13395 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13396 && dwarf_version >= 4)
13397 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13399 /* Value expression. */
13400 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13401 if (loc_result)
13402 add_loc_descr (&loc_result,
13403 new_loc_descr (DW_OP_stack_value, 0, 0));
13405 break;
13408 return loc_result;
13411 /* We need to figure out what section we should use as the base for the
13412 address ranges where a given location is valid.
13413 1. If this particular DECL has a section associated with it, use that.
13414 2. If this function has a section associated with it, use that.
13415 3. Otherwise, use the text section.
13416 XXX: If you split a variable across multiple sections, we won't notice. */
13418 static const char *
13419 secname_for_decl (const_tree decl)
13421 const char *secname;
13423 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13425 tree sectree = DECL_SECTION_NAME (decl);
13426 secname = TREE_STRING_POINTER (sectree);
13428 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13430 tree sectree = DECL_SECTION_NAME (current_function_decl);
13431 secname = TREE_STRING_POINTER (sectree);
13433 else if (cfun && in_cold_section_p)
13434 secname = crtl->subsections.cold_section_label;
13435 else
13436 secname = text_section_label;
13438 return secname;
13441 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13443 static bool
13444 decl_by_reference_p (tree decl)
13446 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13447 || TREE_CODE (decl) == VAR_DECL)
13448 && DECL_BY_REFERENCE (decl));
13451 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13452 for VARLOC. */
13454 static dw_loc_descr_ref
13455 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13456 enum var_init_status initialized)
13458 int have_address = 0;
13459 dw_loc_descr_ref descr;
13460 enum machine_mode mode;
13462 if (want_address != 2)
13464 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13465 /* Single part. */
13466 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13468 varloc = PAT_VAR_LOCATION_LOC (varloc);
13469 if (GET_CODE (varloc) == EXPR_LIST)
13470 varloc = XEXP (varloc, 0);
13471 mode = GET_MODE (varloc);
13472 if (MEM_P (varloc))
13474 rtx addr = XEXP (varloc, 0);
13475 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13476 mode, initialized);
13477 if (descr)
13478 have_address = 1;
13479 else
13481 rtx x = avoid_constant_pool_reference (varloc);
13482 if (x != varloc)
13483 descr = mem_loc_descriptor (x, mode, VOIDmode,
13484 initialized);
13487 else
13488 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13490 else
13491 return 0;
13493 else
13495 if (GET_CODE (varloc) == VAR_LOCATION)
13496 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13497 else
13498 mode = DECL_MODE (loc);
13499 descr = loc_descriptor (varloc, mode, initialized);
13500 have_address = 1;
13503 if (!descr)
13504 return 0;
13506 if (want_address == 2 && !have_address
13507 && (dwarf_version >= 4 || !dwarf_strict))
13509 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13511 expansion_failed (loc, NULL_RTX,
13512 "DWARF address size mismatch");
13513 return 0;
13515 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13516 have_address = 1;
13518 /* Show if we can't fill the request for an address. */
13519 if (want_address && !have_address)
13521 expansion_failed (loc, NULL_RTX,
13522 "Want address and only have value");
13523 return 0;
13526 /* If we've got an address and don't want one, dereference. */
13527 if (!want_address && have_address)
13529 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13530 enum dwarf_location_atom op;
13532 if (size > DWARF2_ADDR_SIZE || size == -1)
13534 expansion_failed (loc, NULL_RTX,
13535 "DWARF address size mismatch");
13536 return 0;
13538 else if (size == DWARF2_ADDR_SIZE)
13539 op = DW_OP_deref;
13540 else
13541 op = DW_OP_deref_size;
13543 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13546 return descr;
13549 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13550 if it is not possible. */
13552 static dw_loc_descr_ref
13553 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13555 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13556 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13557 else if (dwarf_version >= 3 || !dwarf_strict)
13558 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13559 else
13560 return NULL;
13563 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13564 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13566 static dw_loc_descr_ref
13567 dw_sra_loc_expr (tree decl, rtx loc)
13569 rtx p;
13570 unsigned int padsize = 0;
13571 dw_loc_descr_ref descr, *descr_tail;
13572 unsigned HOST_WIDE_INT decl_size;
13573 rtx varloc;
13574 enum var_init_status initialized;
13576 if (DECL_SIZE (decl) == NULL
13577 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13578 return NULL;
13580 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13581 descr = NULL;
13582 descr_tail = &descr;
13584 for (p = loc; p; p = XEXP (p, 1))
13586 unsigned int bitsize = decl_piece_bitsize (p);
13587 rtx loc_note = *decl_piece_varloc_ptr (p);
13588 dw_loc_descr_ref cur_descr;
13589 dw_loc_descr_ref *tail, last = NULL;
13590 unsigned int opsize = 0;
13592 if (loc_note == NULL_RTX
13593 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13595 padsize += bitsize;
13596 continue;
13598 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13599 varloc = NOTE_VAR_LOCATION (loc_note);
13600 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13601 if (cur_descr == NULL)
13603 padsize += bitsize;
13604 continue;
13607 /* Check that cur_descr either doesn't use
13608 DW_OP_*piece operations, or their sum is equal
13609 to bitsize. Otherwise we can't embed it. */
13610 for (tail = &cur_descr; *tail != NULL;
13611 tail = &(*tail)->dw_loc_next)
13612 if ((*tail)->dw_loc_opc == DW_OP_piece)
13614 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13615 * BITS_PER_UNIT;
13616 last = *tail;
13618 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13620 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13621 last = *tail;
13624 if (last != NULL && opsize != bitsize)
13626 padsize += bitsize;
13627 /* Discard the current piece of the descriptor and release any
13628 addr_table entries it uses. */
13629 remove_loc_list_addr_table_entries (cur_descr);
13630 continue;
13633 /* If there is a hole, add DW_OP_*piece after empty DWARF
13634 expression, which means that those bits are optimized out. */
13635 if (padsize)
13637 if (padsize > decl_size)
13639 remove_loc_list_addr_table_entries (cur_descr);
13640 goto discard_descr;
13642 decl_size -= padsize;
13643 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13644 if (*descr_tail == NULL)
13646 remove_loc_list_addr_table_entries (cur_descr);
13647 goto discard_descr;
13649 descr_tail = &(*descr_tail)->dw_loc_next;
13650 padsize = 0;
13652 *descr_tail = cur_descr;
13653 descr_tail = tail;
13654 if (bitsize > decl_size)
13655 goto discard_descr;
13656 decl_size -= bitsize;
13657 if (last == NULL)
13659 HOST_WIDE_INT offset = 0;
13660 if (GET_CODE (varloc) == VAR_LOCATION
13661 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13663 varloc = PAT_VAR_LOCATION_LOC (varloc);
13664 if (GET_CODE (varloc) == EXPR_LIST)
13665 varloc = XEXP (varloc, 0);
13669 if (GET_CODE (varloc) == CONST
13670 || GET_CODE (varloc) == SIGN_EXTEND
13671 || GET_CODE (varloc) == ZERO_EXTEND)
13672 varloc = XEXP (varloc, 0);
13673 else if (GET_CODE (varloc) == SUBREG)
13674 varloc = SUBREG_REG (varloc);
13675 else
13676 break;
13678 while (1);
13679 /* DW_OP_bit_size offset should be zero for register
13680 or implicit location descriptions and empty location
13681 descriptions, but for memory addresses needs big endian
13682 adjustment. */
13683 if (MEM_P (varloc))
13685 unsigned HOST_WIDE_INT memsize
13686 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13687 if (memsize != bitsize)
13689 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13690 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13691 goto discard_descr;
13692 if (memsize < bitsize)
13693 goto discard_descr;
13694 if (BITS_BIG_ENDIAN)
13695 offset = memsize - bitsize;
13699 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13700 if (*descr_tail == NULL)
13701 goto discard_descr;
13702 descr_tail = &(*descr_tail)->dw_loc_next;
13706 /* If there were any non-empty expressions, add padding till the end of
13707 the decl. */
13708 if (descr != NULL && decl_size != 0)
13710 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13711 if (*descr_tail == NULL)
13712 goto discard_descr;
13714 return descr;
13716 discard_descr:
13717 /* Discard the descriptor and release any addr_table entries it uses. */
13718 remove_loc_list_addr_table_entries (descr);
13719 return NULL;
13722 /* Return the dwarf representation of the location list LOC_LIST of
13723 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13724 function. */
13726 static dw_loc_list_ref
13727 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13729 const char *endname, *secname;
13730 rtx varloc;
13731 enum var_init_status initialized;
13732 struct var_loc_node *node;
13733 dw_loc_descr_ref descr;
13734 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13735 dw_loc_list_ref list = NULL;
13736 dw_loc_list_ref *listp = &list;
13738 /* Now that we know what section we are using for a base,
13739 actually construct the list of locations.
13740 The first location information is what is passed to the
13741 function that creates the location list, and the remaining
13742 locations just get added on to that list.
13743 Note that we only know the start address for a location
13744 (IE location changes), so to build the range, we use
13745 the range [current location start, next location start].
13746 This means we have to special case the last node, and generate
13747 a range of [last location start, end of function label]. */
13749 secname = secname_for_decl (decl);
13751 for (node = loc_list->first; node; node = node->next)
13752 if (GET_CODE (node->loc) == EXPR_LIST
13753 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13755 if (GET_CODE (node->loc) == EXPR_LIST)
13757 /* This requires DW_OP_{,bit_}piece, which is not usable
13758 inside DWARF expressions. */
13759 if (want_address != 2)
13760 continue;
13761 descr = dw_sra_loc_expr (decl, node->loc);
13762 if (descr == NULL)
13763 continue;
13765 else
13767 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13768 varloc = NOTE_VAR_LOCATION (node->loc);
13769 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13771 if (descr)
13773 bool range_across_switch = false;
13774 /* If section switch happens in between node->label
13775 and node->next->label (or end of function) and
13776 we can't emit it as a single entry list,
13777 emit two ranges, first one ending at the end
13778 of first partition and second one starting at the
13779 beginning of second partition. */
13780 if (node == loc_list->last_before_switch
13781 && (node != loc_list->first || loc_list->first->next)
13782 && current_function_decl)
13784 endname = cfun->fde->dw_fde_end;
13785 range_across_switch = true;
13787 /* The variable has a location between NODE->LABEL and
13788 NODE->NEXT->LABEL. */
13789 else if (node->next)
13790 endname = node->next->label;
13791 /* If the variable has a location at the last label
13792 it keeps its location until the end of function. */
13793 else if (!current_function_decl)
13794 endname = text_end_label;
13795 else
13797 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13798 FUNC_LABEL_ID (cfun));
13799 endname = ggc_strdup (label_id);
13802 *listp = new_loc_list (descr, node->label, endname, secname);
13803 if (TREE_CODE (decl) == PARM_DECL
13804 && node == loc_list->first
13805 && NOTE_P (node->loc)
13806 && strcmp (node->label, endname) == 0)
13807 (*listp)->force = true;
13808 listp = &(*listp)->dw_loc_next;
13810 if (range_across_switch)
13812 if (GET_CODE (node->loc) == EXPR_LIST)
13813 descr = dw_sra_loc_expr (decl, node->loc);
13814 else
13816 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13817 varloc = NOTE_VAR_LOCATION (node->loc);
13818 descr = dw_loc_list_1 (decl, varloc, want_address,
13819 initialized);
13821 gcc_assert (descr);
13822 /* The variable has a location between NODE->LABEL and
13823 NODE->NEXT->LABEL. */
13824 if (node->next)
13825 endname = node->next->label;
13826 else
13827 endname = cfun->fde->dw_fde_second_end;
13828 *listp = new_loc_list (descr,
13829 cfun->fde->dw_fde_second_begin,
13830 endname, secname);
13831 listp = &(*listp)->dw_loc_next;
13836 /* Try to avoid the overhead of a location list emitting a location
13837 expression instead, but only if we didn't have more than one
13838 location entry in the first place. If some entries were not
13839 representable, we don't want to pretend a single entry that was
13840 applies to the entire scope in which the variable is
13841 available. */
13842 if (list && loc_list->first->next)
13843 gen_llsym (list);
13845 return list;
13848 /* Return if the loc_list has only single element and thus can be represented
13849 as location description. */
13851 static bool
13852 single_element_loc_list_p (dw_loc_list_ref list)
13854 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13855 return !list->ll_symbol;
13858 /* To each location in list LIST add loc descr REF. */
13860 static void
13861 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13863 dw_loc_descr_ref copy;
13864 add_loc_descr (&list->expr, ref);
13865 list = list->dw_loc_next;
13866 while (list)
13868 copy = ggc_alloc_dw_loc_descr_node ();
13869 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13870 add_loc_descr (&list->expr, copy);
13871 while (copy->dw_loc_next)
13873 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13874 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13875 copy->dw_loc_next = new_copy;
13876 copy = new_copy;
13878 list = list->dw_loc_next;
13882 /* Given two lists RET and LIST
13883 produce location list that is result of adding expression in LIST
13884 to expression in RET on each position in program.
13885 Might be destructive on both RET and LIST.
13887 TODO: We handle only simple cases of RET or LIST having at most one
13888 element. General case would inolve sorting the lists in program order
13889 and merging them that will need some additional work.
13890 Adding that will improve quality of debug info especially for SRA-ed
13891 structures. */
13893 static void
13894 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13896 if (!list)
13897 return;
13898 if (!*ret)
13900 *ret = list;
13901 return;
13903 if (!list->dw_loc_next)
13905 add_loc_descr_to_each (*ret, list->expr);
13906 return;
13908 if (!(*ret)->dw_loc_next)
13910 add_loc_descr_to_each (list, (*ret)->expr);
13911 *ret = list;
13912 return;
13914 expansion_failed (NULL_TREE, NULL_RTX,
13915 "Don't know how to merge two non-trivial"
13916 " location lists.\n");
13917 *ret = NULL;
13918 return;
13921 /* LOC is constant expression. Try a luck, look it up in constant
13922 pool and return its loc_descr of its address. */
13924 static dw_loc_descr_ref
13925 cst_pool_loc_descr (tree loc)
13927 /* Get an RTL for this, if something has been emitted. */
13928 rtx rtl = lookup_constant_def (loc);
13930 if (!rtl || !MEM_P (rtl))
13932 gcc_assert (!rtl);
13933 return 0;
13935 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13937 /* TODO: We might get more coverage if we was actually delaying expansion
13938 of all expressions till end of compilation when constant pools are fully
13939 populated. */
13940 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13942 expansion_failed (loc, NULL_RTX,
13943 "CST value in contant pool but not marked.");
13944 return 0;
13946 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13947 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13950 /* Return dw_loc_list representing address of addr_expr LOC
13951 by looking for inner INDIRECT_REF expression and turning
13952 it into simple arithmetics. */
13954 static dw_loc_list_ref
13955 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13957 tree obj, offset;
13958 HOST_WIDE_INT bitsize, bitpos, bytepos;
13959 enum machine_mode mode;
13960 int unsignedp, volatilep = 0;
13961 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13963 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13964 &bitsize, &bitpos, &offset, &mode,
13965 &unsignedp, &volatilep, false);
13966 STRIP_NOPS (obj);
13967 if (bitpos % BITS_PER_UNIT)
13969 expansion_failed (loc, NULL_RTX, "bitfield access");
13970 return 0;
13972 if (!INDIRECT_REF_P (obj))
13974 expansion_failed (obj,
13975 NULL_RTX, "no indirect ref in inner refrence");
13976 return 0;
13978 if (!offset && !bitpos)
13979 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13980 else if (toplev
13981 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13982 && (dwarf_version >= 4 || !dwarf_strict))
13984 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13985 if (!list_ret)
13986 return 0;
13987 if (offset)
13989 /* Variable offset. */
13990 list_ret1 = loc_list_from_tree (offset, 0);
13991 if (list_ret1 == 0)
13992 return 0;
13993 add_loc_list (&list_ret, list_ret1);
13994 if (!list_ret)
13995 return 0;
13996 add_loc_descr_to_each (list_ret,
13997 new_loc_descr (DW_OP_plus, 0, 0));
13999 bytepos = bitpos / BITS_PER_UNIT;
14000 if (bytepos > 0)
14001 add_loc_descr_to_each (list_ret,
14002 new_loc_descr (DW_OP_plus_uconst,
14003 bytepos, 0));
14004 else if (bytepos < 0)
14005 loc_list_plus_const (list_ret, bytepos);
14006 add_loc_descr_to_each (list_ret,
14007 new_loc_descr (DW_OP_stack_value, 0, 0));
14009 return list_ret;
14013 /* Generate Dwarf location list representing LOC.
14014 If WANT_ADDRESS is false, expression computing LOC will be computed
14015 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14016 if WANT_ADDRESS is 2, expression computing address useable in location
14017 will be returned (i.e. DW_OP_reg can be used
14018 to refer to register values). */
14020 static dw_loc_list_ref
14021 loc_list_from_tree (tree loc, int want_address)
14023 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14024 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14025 int have_address = 0;
14026 enum dwarf_location_atom op;
14028 /* ??? Most of the time we do not take proper care for sign/zero
14029 extending the values properly. Hopefully this won't be a real
14030 problem... */
14032 switch (TREE_CODE (loc))
14034 case ERROR_MARK:
14035 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14036 return 0;
14038 case PLACEHOLDER_EXPR:
14039 /* This case involves extracting fields from an object to determine the
14040 position of other fields. We don't try to encode this here. The
14041 only user of this is Ada, which encodes the needed information using
14042 the names of types. */
14043 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14044 return 0;
14046 case CALL_EXPR:
14047 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14048 /* There are no opcodes for these operations. */
14049 return 0;
14051 case PREINCREMENT_EXPR:
14052 case PREDECREMENT_EXPR:
14053 case POSTINCREMENT_EXPR:
14054 case POSTDECREMENT_EXPR:
14055 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14056 /* There are no opcodes for these operations. */
14057 return 0;
14059 case ADDR_EXPR:
14060 /* If we already want an address, see if there is INDIRECT_REF inside
14061 e.g. for &this->field. */
14062 if (want_address)
14064 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14065 (loc, want_address == 2);
14066 if (list_ret)
14067 have_address = 1;
14068 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14069 && (ret = cst_pool_loc_descr (loc)))
14070 have_address = 1;
14072 /* Otherwise, process the argument and look for the address. */
14073 if (!list_ret && !ret)
14074 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14075 else
14077 if (want_address)
14078 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14079 return NULL;
14081 break;
14083 case VAR_DECL:
14084 if (DECL_THREAD_LOCAL_P (loc))
14086 rtx rtl;
14087 enum dwarf_location_atom tls_op;
14088 enum dtprel_bool dtprel = dtprel_false;
14090 if (targetm.have_tls)
14092 /* If this is not defined, we have no way to emit the
14093 data. */
14094 if (!targetm.asm_out.output_dwarf_dtprel)
14095 return 0;
14097 /* The way DW_OP_GNU_push_tls_address is specified, we
14098 can only look up addresses of objects in the current
14099 module. We used DW_OP_addr as first op, but that's
14100 wrong, because DW_OP_addr is relocated by the debug
14101 info consumer, while DW_OP_GNU_push_tls_address
14102 operand shouldn't be. */
14103 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14104 return 0;
14105 dtprel = dtprel_true;
14106 tls_op = DW_OP_GNU_push_tls_address;
14108 else
14110 if (!targetm.emutls.debug_form_tls_address
14111 || !(dwarf_version >= 3 || !dwarf_strict))
14112 return 0;
14113 /* We stuffed the control variable into the DECL_VALUE_EXPR
14114 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14115 no longer appear in gimple code. We used the control
14116 variable in specific so that we could pick it up here. */
14117 loc = DECL_VALUE_EXPR (loc);
14118 tls_op = DW_OP_form_tls_address;
14121 rtl = rtl_for_decl_location (loc);
14122 if (rtl == NULL_RTX)
14123 return 0;
14125 if (!MEM_P (rtl))
14126 return 0;
14127 rtl = XEXP (rtl, 0);
14128 if (! CONSTANT_P (rtl))
14129 return 0;
14131 ret = new_addr_loc_descr (rtl, dtprel);
14132 ret1 = new_loc_descr (tls_op, 0, 0);
14133 add_loc_descr (&ret, ret1);
14135 have_address = 1;
14136 break;
14138 /* FALLTHRU */
14140 case PARM_DECL:
14141 case RESULT_DECL:
14142 if (DECL_HAS_VALUE_EXPR_P (loc))
14143 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14144 want_address);
14145 /* FALLTHRU */
14147 case FUNCTION_DECL:
14149 rtx rtl;
14150 var_loc_list *loc_list = lookup_decl_loc (loc);
14152 if (loc_list && loc_list->first)
14154 list_ret = dw_loc_list (loc_list, loc, want_address);
14155 have_address = want_address != 0;
14156 break;
14158 rtl = rtl_for_decl_location (loc);
14159 if (rtl == NULL_RTX)
14161 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14162 return 0;
14164 else if (CONST_INT_P (rtl))
14166 HOST_WIDE_INT val = INTVAL (rtl);
14167 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14168 val &= GET_MODE_MASK (DECL_MODE (loc));
14169 ret = int_loc_descriptor (val);
14171 else if (GET_CODE (rtl) == CONST_STRING)
14173 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14174 return 0;
14176 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14177 ret = new_addr_loc_descr (rtl, dtprel_false);
14178 else
14180 enum machine_mode mode, mem_mode;
14182 /* Certain constructs can only be represented at top-level. */
14183 if (want_address == 2)
14185 ret = loc_descriptor (rtl, VOIDmode,
14186 VAR_INIT_STATUS_INITIALIZED);
14187 have_address = 1;
14189 else
14191 mode = GET_MODE (rtl);
14192 mem_mode = VOIDmode;
14193 if (MEM_P (rtl))
14195 mem_mode = mode;
14196 mode = get_address_mode (rtl);
14197 rtl = XEXP (rtl, 0);
14198 have_address = 1;
14200 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14201 VAR_INIT_STATUS_INITIALIZED);
14203 if (!ret)
14204 expansion_failed (loc, rtl,
14205 "failed to produce loc descriptor for rtl");
14208 break;
14210 case MEM_REF:
14211 /* ??? FIXME. */
14212 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14213 return 0;
14214 /* Fallthru. */
14215 case INDIRECT_REF:
14216 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14217 have_address = 1;
14218 break;
14220 case COMPOUND_EXPR:
14221 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14223 CASE_CONVERT:
14224 case VIEW_CONVERT_EXPR:
14225 case SAVE_EXPR:
14226 case MODIFY_EXPR:
14227 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14229 case COMPONENT_REF:
14230 case BIT_FIELD_REF:
14231 case ARRAY_REF:
14232 case ARRAY_RANGE_REF:
14233 case REALPART_EXPR:
14234 case IMAGPART_EXPR:
14236 tree obj, offset;
14237 HOST_WIDE_INT bitsize, bitpos, bytepos;
14238 enum machine_mode mode;
14239 int unsignedp, volatilep = 0;
14241 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14242 &unsignedp, &volatilep, false);
14244 gcc_assert (obj != loc);
14246 list_ret = loc_list_from_tree (obj,
14247 want_address == 2
14248 && !bitpos && !offset ? 2 : 1);
14249 /* TODO: We can extract value of the small expression via shifting even
14250 for nonzero bitpos. */
14251 if (list_ret == 0)
14252 return 0;
14253 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14255 expansion_failed (loc, NULL_RTX,
14256 "bitfield access");
14257 return 0;
14260 if (offset != NULL_TREE)
14262 /* Variable offset. */
14263 list_ret1 = loc_list_from_tree (offset, 0);
14264 if (list_ret1 == 0)
14265 return 0;
14266 add_loc_list (&list_ret, list_ret1);
14267 if (!list_ret)
14268 return 0;
14269 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14272 bytepos = bitpos / BITS_PER_UNIT;
14273 if (bytepos > 0)
14274 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14275 else if (bytepos < 0)
14276 loc_list_plus_const (list_ret, bytepos);
14278 have_address = 1;
14279 break;
14282 case INTEGER_CST:
14283 if ((want_address || !tree_fits_shwi_p (loc))
14284 && (ret = cst_pool_loc_descr (loc)))
14285 have_address = 1;
14286 else if (want_address == 2
14287 && tree_fits_shwi_p (loc)
14288 && (ret = address_of_int_loc_descriptor
14289 (int_size_in_bytes (TREE_TYPE (loc)),
14290 tree_to_shwi (loc))))
14291 have_address = 1;
14292 else if (tree_fits_shwi_p (loc))
14293 ret = int_loc_descriptor (tree_to_shwi (loc));
14294 else
14296 expansion_failed (loc, NULL_RTX,
14297 "Integer operand is not host integer");
14298 return 0;
14300 break;
14302 case CONSTRUCTOR:
14303 case REAL_CST:
14304 case STRING_CST:
14305 case COMPLEX_CST:
14306 if ((ret = cst_pool_loc_descr (loc)))
14307 have_address = 1;
14308 else
14309 /* We can construct small constants here using int_loc_descriptor. */
14310 expansion_failed (loc, NULL_RTX,
14311 "constructor or constant not in constant pool");
14312 break;
14314 case TRUTH_AND_EXPR:
14315 case TRUTH_ANDIF_EXPR:
14316 case BIT_AND_EXPR:
14317 op = DW_OP_and;
14318 goto do_binop;
14320 case TRUTH_XOR_EXPR:
14321 case BIT_XOR_EXPR:
14322 op = DW_OP_xor;
14323 goto do_binop;
14325 case TRUTH_OR_EXPR:
14326 case TRUTH_ORIF_EXPR:
14327 case BIT_IOR_EXPR:
14328 op = DW_OP_or;
14329 goto do_binop;
14331 case FLOOR_DIV_EXPR:
14332 case CEIL_DIV_EXPR:
14333 case ROUND_DIV_EXPR:
14334 case TRUNC_DIV_EXPR:
14335 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14336 return 0;
14337 op = DW_OP_div;
14338 goto do_binop;
14340 case MINUS_EXPR:
14341 op = DW_OP_minus;
14342 goto do_binop;
14344 case FLOOR_MOD_EXPR:
14345 case CEIL_MOD_EXPR:
14346 case ROUND_MOD_EXPR:
14347 case TRUNC_MOD_EXPR:
14348 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14350 op = DW_OP_mod;
14351 goto do_binop;
14353 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14354 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14355 if (list_ret == 0 || list_ret1 == 0)
14356 return 0;
14358 add_loc_list (&list_ret, list_ret1);
14359 if (list_ret == 0)
14360 return 0;
14361 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14362 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14363 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14364 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14365 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14366 break;
14368 case MULT_EXPR:
14369 op = DW_OP_mul;
14370 goto do_binop;
14372 case LSHIFT_EXPR:
14373 op = DW_OP_shl;
14374 goto do_binop;
14376 case RSHIFT_EXPR:
14377 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14378 goto do_binop;
14380 case POINTER_PLUS_EXPR:
14381 case PLUS_EXPR:
14382 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14384 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14385 if (list_ret == 0)
14386 return 0;
14388 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14389 break;
14392 op = DW_OP_plus;
14393 goto do_binop;
14395 case LE_EXPR:
14396 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14397 return 0;
14399 op = DW_OP_le;
14400 goto do_binop;
14402 case GE_EXPR:
14403 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14404 return 0;
14406 op = DW_OP_ge;
14407 goto do_binop;
14409 case LT_EXPR:
14410 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14411 return 0;
14413 op = DW_OP_lt;
14414 goto do_binop;
14416 case GT_EXPR:
14417 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14418 return 0;
14420 op = DW_OP_gt;
14421 goto do_binop;
14423 case EQ_EXPR:
14424 op = DW_OP_eq;
14425 goto do_binop;
14427 case NE_EXPR:
14428 op = DW_OP_ne;
14429 goto do_binop;
14431 do_binop:
14432 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14433 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14434 if (list_ret == 0 || list_ret1 == 0)
14435 return 0;
14437 add_loc_list (&list_ret, list_ret1);
14438 if (list_ret == 0)
14439 return 0;
14440 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14441 break;
14443 case TRUTH_NOT_EXPR:
14444 case BIT_NOT_EXPR:
14445 op = DW_OP_not;
14446 goto do_unop;
14448 case ABS_EXPR:
14449 op = DW_OP_abs;
14450 goto do_unop;
14452 case NEGATE_EXPR:
14453 op = DW_OP_neg;
14454 goto do_unop;
14456 do_unop:
14457 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14458 if (list_ret == 0)
14459 return 0;
14461 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14462 break;
14464 case MIN_EXPR:
14465 case MAX_EXPR:
14467 const enum tree_code code =
14468 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14470 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14471 build2 (code, integer_type_node,
14472 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14473 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14476 /* ... fall through ... */
14478 case COND_EXPR:
14480 dw_loc_descr_ref lhs
14481 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14482 dw_loc_list_ref rhs
14483 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14484 dw_loc_descr_ref bra_node, jump_node, tmp;
14486 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14487 if (list_ret == 0 || lhs == 0 || rhs == 0)
14488 return 0;
14490 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14491 add_loc_descr_to_each (list_ret, bra_node);
14493 add_loc_list (&list_ret, rhs);
14494 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14495 add_loc_descr_to_each (list_ret, jump_node);
14497 add_loc_descr_to_each (list_ret, lhs);
14498 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14499 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14501 /* ??? Need a node to point the skip at. Use a nop. */
14502 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14503 add_loc_descr_to_each (list_ret, tmp);
14504 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14505 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14507 break;
14509 case FIX_TRUNC_EXPR:
14510 return 0;
14512 default:
14513 /* Leave front-end specific codes as simply unknown. This comes
14514 up, for instance, with the C STMT_EXPR. */
14515 if ((unsigned int) TREE_CODE (loc)
14516 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14518 expansion_failed (loc, NULL_RTX,
14519 "language specific tree node");
14520 return 0;
14523 #ifdef ENABLE_CHECKING
14524 /* Otherwise this is a generic code; we should just lists all of
14525 these explicitly. We forgot one. */
14526 gcc_unreachable ();
14527 #else
14528 /* In a release build, we want to degrade gracefully: better to
14529 generate incomplete debugging information than to crash. */
14530 return NULL;
14531 #endif
14534 if (!ret && !list_ret)
14535 return 0;
14537 if (want_address == 2 && !have_address
14538 && (dwarf_version >= 4 || !dwarf_strict))
14540 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14542 expansion_failed (loc, NULL_RTX,
14543 "DWARF address size mismatch");
14544 return 0;
14546 if (ret)
14547 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14548 else
14549 add_loc_descr_to_each (list_ret,
14550 new_loc_descr (DW_OP_stack_value, 0, 0));
14551 have_address = 1;
14553 /* Show if we can't fill the request for an address. */
14554 if (want_address && !have_address)
14556 expansion_failed (loc, NULL_RTX,
14557 "Want address and only have value");
14558 return 0;
14561 gcc_assert (!ret || !list_ret);
14563 /* If we've got an address and don't want one, dereference. */
14564 if (!want_address && have_address)
14566 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14568 if (size > DWARF2_ADDR_SIZE || size == -1)
14570 expansion_failed (loc, NULL_RTX,
14571 "DWARF address size mismatch");
14572 return 0;
14574 else if (size == DWARF2_ADDR_SIZE)
14575 op = DW_OP_deref;
14576 else
14577 op = DW_OP_deref_size;
14579 if (ret)
14580 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14581 else
14582 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14584 if (ret)
14585 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14587 return list_ret;
14590 /* Same as above but return only single location expression. */
14591 static dw_loc_descr_ref
14592 loc_descriptor_from_tree (tree loc, int want_address)
14594 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14595 if (!ret)
14596 return NULL;
14597 if (ret->dw_loc_next)
14599 expansion_failed (loc, NULL_RTX,
14600 "Location list where only loc descriptor needed");
14601 return NULL;
14603 return ret->expr;
14606 /* Given a value, round it up to the lowest multiple of `boundary'
14607 which is not less than the value itself. */
14609 static inline HOST_WIDE_INT
14610 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14612 return (((value + boundary - 1) / boundary) * boundary);
14615 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14616 pointer to the declared type for the relevant field variable, or return
14617 `integer_type_node' if the given node turns out to be an
14618 ERROR_MARK node. */
14620 static inline tree
14621 field_type (const_tree decl)
14623 tree type;
14625 if (TREE_CODE (decl) == ERROR_MARK)
14626 return integer_type_node;
14628 type = DECL_BIT_FIELD_TYPE (decl);
14629 if (type == NULL_TREE)
14630 type = TREE_TYPE (decl);
14632 return type;
14635 /* Given a pointer to a tree node, return the alignment in bits for
14636 it, or else return BITS_PER_WORD if the node actually turns out to
14637 be an ERROR_MARK node. */
14639 static inline unsigned
14640 simple_type_align_in_bits (const_tree type)
14642 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14645 static inline unsigned
14646 simple_decl_align_in_bits (const_tree decl)
14648 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14651 /* Return the result of rounding T up to ALIGN. */
14653 static inline double_int
14654 round_up_to_align (double_int t, unsigned int align)
14656 double_int alignd = double_int::from_uhwi (align);
14657 t += alignd;
14658 t += double_int_minus_one;
14659 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14660 t *= alignd;
14661 return t;
14664 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14665 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14666 or return 0 if we are unable to determine what that offset is, either
14667 because the argument turns out to be a pointer to an ERROR_MARK node, or
14668 because the offset is actually variable. (We can't handle the latter case
14669 just yet). */
14671 static HOST_WIDE_INT
14672 field_byte_offset (const_tree decl)
14674 double_int object_offset_in_bits;
14675 double_int object_offset_in_bytes;
14676 double_int bitpos_int;
14678 if (TREE_CODE (decl) == ERROR_MARK)
14679 return 0;
14681 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14683 /* We cannot yet cope with fields whose positions are variable, so
14684 for now, when we see such things, we simply return 0. Someday, we may
14685 be able to handle such cases, but it will be damn difficult. */
14686 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14687 return 0;
14689 bitpos_int = tree_to_double_int (bit_position (decl));
14691 #ifdef PCC_BITFIELD_TYPE_MATTERS
14692 if (PCC_BITFIELD_TYPE_MATTERS)
14694 tree type;
14695 tree field_size_tree;
14696 double_int deepest_bitpos;
14697 double_int field_size_in_bits;
14698 unsigned int type_align_in_bits;
14699 unsigned int decl_align_in_bits;
14700 double_int type_size_in_bits;
14702 type = field_type (decl);
14703 type_size_in_bits = double_int_type_size_in_bits (type);
14704 type_align_in_bits = simple_type_align_in_bits (type);
14706 field_size_tree = DECL_SIZE (decl);
14708 /* The size could be unspecified if there was an error, or for
14709 a flexible array member. */
14710 if (!field_size_tree)
14711 field_size_tree = bitsize_zero_node;
14713 /* If the size of the field is not constant, use the type size. */
14714 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14715 field_size_in_bits = tree_to_double_int (field_size_tree);
14716 else
14717 field_size_in_bits = type_size_in_bits;
14719 decl_align_in_bits = simple_decl_align_in_bits (decl);
14721 /* The GCC front-end doesn't make any attempt to keep track of the
14722 starting bit offset (relative to the start of the containing
14723 structure type) of the hypothetical "containing object" for a
14724 bit-field. Thus, when computing the byte offset value for the
14725 start of the "containing object" of a bit-field, we must deduce
14726 this information on our own. This can be rather tricky to do in
14727 some cases. For example, handling the following structure type
14728 definition when compiling for an i386/i486 target (which only
14729 aligns long long's to 32-bit boundaries) can be very tricky:
14731 struct S { int field1; long long field2:31; };
14733 Fortunately, there is a simple rule-of-thumb which can be used
14734 in such cases. When compiling for an i386/i486, GCC will
14735 allocate 8 bytes for the structure shown above. It decides to
14736 do this based upon one simple rule for bit-field allocation.
14737 GCC allocates each "containing object" for each bit-field at
14738 the first (i.e. lowest addressed) legitimate alignment boundary
14739 (based upon the required minimum alignment for the declared
14740 type of the field) which it can possibly use, subject to the
14741 condition that there is still enough available space remaining
14742 in the containing object (when allocated at the selected point)
14743 to fully accommodate all of the bits of the bit-field itself.
14745 This simple rule makes it obvious why GCC allocates 8 bytes for
14746 each object of the structure type shown above. When looking
14747 for a place to allocate the "containing object" for `field2',
14748 the compiler simply tries to allocate a 64-bit "containing
14749 object" at each successive 32-bit boundary (starting at zero)
14750 until it finds a place to allocate that 64- bit field such that
14751 at least 31 contiguous (and previously unallocated) bits remain
14752 within that selected 64 bit field. (As it turns out, for the
14753 example above, the compiler finds it is OK to allocate the
14754 "containing object" 64-bit field at bit-offset zero within the
14755 structure type.)
14757 Here we attempt to work backwards from the limited set of facts
14758 we're given, and we try to deduce from those facts, where GCC
14759 must have believed that the containing object started (within
14760 the structure type). The value we deduce is then used (by the
14761 callers of this routine) to generate DW_AT_location and
14762 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14763 the case of DW_AT_location, regular fields as well). */
14765 /* Figure out the bit-distance from the start of the structure to
14766 the "deepest" bit of the bit-field. */
14767 deepest_bitpos = bitpos_int + field_size_in_bits;
14769 /* This is the tricky part. Use some fancy footwork to deduce
14770 where the lowest addressed bit of the containing object must
14771 be. */
14772 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14774 /* Round up to type_align by default. This works best for
14775 bitfields. */
14776 object_offset_in_bits
14777 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14779 if (object_offset_in_bits.ugt (bitpos_int))
14781 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14783 /* Round up to decl_align instead. */
14784 object_offset_in_bits
14785 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14788 else
14789 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14790 object_offset_in_bits = bitpos_int;
14792 object_offset_in_bytes
14793 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14794 true, TRUNC_DIV_EXPR);
14795 return object_offset_in_bytes.to_shwi ();
14798 /* The following routines define various Dwarf attributes and any data
14799 associated with them. */
14801 /* Add a location description attribute value to a DIE.
14803 This emits location attributes suitable for whole variables and
14804 whole parameters. Note that the location attributes for struct fields are
14805 generated by the routine `data_member_location_attribute' below. */
14807 static inline void
14808 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14809 dw_loc_list_ref descr)
14811 if (descr == 0)
14812 return;
14813 if (single_element_loc_list_p (descr))
14814 add_AT_loc (die, attr_kind, descr->expr);
14815 else
14816 add_AT_loc_list (die, attr_kind, descr);
14819 /* Add DW_AT_accessibility attribute to DIE if needed. */
14821 static void
14822 add_accessibility_attribute (dw_die_ref die, tree decl)
14824 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14825 children, otherwise the default is DW_ACCESS_public. In DWARF2
14826 the default has always been DW_ACCESS_public. */
14827 if (TREE_PROTECTED (decl))
14828 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14829 else if (TREE_PRIVATE (decl))
14831 if (dwarf_version == 2
14832 || die->die_parent == NULL
14833 || die->die_parent->die_tag != DW_TAG_class_type)
14834 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14836 else if (dwarf_version > 2
14837 && die->die_parent
14838 && die->die_parent->die_tag == DW_TAG_class_type)
14839 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14842 /* Attach the specialized form of location attribute used for data members of
14843 struct and union types. In the special case of a FIELD_DECL node which
14844 represents a bit-field, the "offset" part of this special location
14845 descriptor must indicate the distance in bytes from the lowest-addressed
14846 byte of the containing struct or union type to the lowest-addressed byte of
14847 the "containing object" for the bit-field. (See the `field_byte_offset'
14848 function above).
14850 For any given bit-field, the "containing object" is a hypothetical object
14851 (of some integral or enum type) within which the given bit-field lives. The
14852 type of this hypothetical "containing object" is always the same as the
14853 declared type of the individual bit-field itself (for GCC anyway... the
14854 DWARF spec doesn't actually mandate this). Note that it is the size (in
14855 bytes) of the hypothetical "containing object" which will be given in the
14856 DW_AT_byte_size attribute for this bit-field. (See the
14857 `byte_size_attribute' function below.) It is also used when calculating the
14858 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14859 function below.) */
14861 static void
14862 add_data_member_location_attribute (dw_die_ref die, tree decl)
14864 HOST_WIDE_INT offset;
14865 dw_loc_descr_ref loc_descr = 0;
14867 if (TREE_CODE (decl) == TREE_BINFO)
14869 /* We're working on the TAG_inheritance for a base class. */
14870 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14872 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14873 aren't at a fixed offset from all (sub)objects of the same
14874 type. We need to extract the appropriate offset from our
14875 vtable. The following dwarf expression means
14877 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14879 This is specific to the V3 ABI, of course. */
14881 dw_loc_descr_ref tmp;
14883 /* Make a copy of the object address. */
14884 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14885 add_loc_descr (&loc_descr, tmp);
14887 /* Extract the vtable address. */
14888 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14889 add_loc_descr (&loc_descr, tmp);
14891 /* Calculate the address of the offset. */
14892 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14893 gcc_assert (offset < 0);
14895 tmp = int_loc_descriptor (-offset);
14896 add_loc_descr (&loc_descr, tmp);
14897 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14898 add_loc_descr (&loc_descr, tmp);
14900 /* Extract the offset. */
14901 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14902 add_loc_descr (&loc_descr, tmp);
14904 /* Add it to the object address. */
14905 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14906 add_loc_descr (&loc_descr, tmp);
14908 else
14909 offset = tree_to_shwi (BINFO_OFFSET (decl));
14911 else
14912 offset = field_byte_offset (decl);
14914 if (! loc_descr)
14916 if (dwarf_version > 2)
14918 /* Don't need to output a location expression, just the constant. */
14919 if (offset < 0)
14920 add_AT_int (die, DW_AT_data_member_location, offset);
14921 else
14922 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14923 return;
14925 else
14927 enum dwarf_location_atom op;
14929 /* The DWARF2 standard says that we should assume that the structure
14930 address is already on the stack, so we can specify a structure
14931 field address by using DW_OP_plus_uconst. */
14932 op = DW_OP_plus_uconst;
14933 loc_descr = new_loc_descr (op, offset, 0);
14937 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14940 /* Writes integer values to dw_vec_const array. */
14942 static void
14943 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14945 while (size != 0)
14947 *dest++ = val & 0xff;
14948 val >>= 8;
14949 --size;
14953 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14955 static HOST_WIDE_INT
14956 extract_int (const unsigned char *src, unsigned int size)
14958 HOST_WIDE_INT val = 0;
14960 src += size;
14961 while (size != 0)
14963 val <<= 8;
14964 val |= *--src & 0xff;
14965 --size;
14967 return val;
14970 /* Writes double_int values to dw_vec_const array. */
14972 static void
14973 insert_double (double_int val, unsigned char *dest)
14975 unsigned char *p0 = dest;
14976 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14978 if (WORDS_BIG_ENDIAN)
14980 p0 = p1;
14981 p1 = dest;
14984 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14985 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14988 /* Writes floating point values to dw_vec_const array. */
14990 static void
14991 insert_float (const_rtx rtl, unsigned char *array)
14993 REAL_VALUE_TYPE rv;
14994 long val[4];
14995 int i;
14997 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14998 real_to_target (val, &rv, GET_MODE (rtl));
15000 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15001 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15003 insert_int (val[i], 4, array);
15004 array += 4;
15008 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15009 does not have a "location" either in memory or in a register. These
15010 things can arise in GNU C when a constant is passed as an actual parameter
15011 to an inlined function. They can also arise in C++ where declared
15012 constants do not necessarily get memory "homes". */
15014 static bool
15015 add_const_value_attribute (dw_die_ref die, rtx rtl)
15017 switch (GET_CODE (rtl))
15019 case CONST_INT:
15021 HOST_WIDE_INT val = INTVAL (rtl);
15023 if (val < 0)
15024 add_AT_int (die, DW_AT_const_value, val);
15025 else
15026 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15028 return true;
15030 case CONST_DOUBLE:
15031 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15032 floating-point constant. A CONST_DOUBLE is used whenever the
15033 constant requires more than one word in order to be adequately
15034 represented. */
15036 enum machine_mode mode = GET_MODE (rtl);
15038 if (SCALAR_FLOAT_MODE_P (mode))
15040 unsigned int length = GET_MODE_SIZE (mode);
15041 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15043 insert_float (rtl, array);
15044 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15046 else
15047 add_AT_double (die, DW_AT_const_value,
15048 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15050 return true;
15052 case CONST_VECTOR:
15054 enum machine_mode mode = GET_MODE (rtl);
15055 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15056 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15057 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15058 (length * elt_size);
15059 unsigned int i;
15060 unsigned char *p;
15062 switch (GET_MODE_CLASS (mode))
15064 case MODE_VECTOR_INT:
15065 for (i = 0, p = array; i < length; i++, p += elt_size)
15067 rtx elt = CONST_VECTOR_ELT (rtl, i);
15068 double_int val = rtx_to_double_int (elt);
15070 if (elt_size <= sizeof (HOST_WIDE_INT))
15071 insert_int (val.to_shwi (), elt_size, p);
15072 else
15074 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15075 insert_double (val, p);
15078 break;
15080 case MODE_VECTOR_FLOAT:
15081 for (i = 0, p = array; i < length; i++, p += elt_size)
15083 rtx elt = CONST_VECTOR_ELT (rtl, i);
15084 insert_float (elt, p);
15086 break;
15088 default:
15089 gcc_unreachable ();
15092 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15094 return true;
15096 case CONST_STRING:
15097 if (dwarf_version >= 4 || !dwarf_strict)
15099 dw_loc_descr_ref loc_result;
15100 resolve_one_addr (&rtl, NULL);
15101 rtl_addr:
15102 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15103 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15104 add_AT_loc (die, DW_AT_location, loc_result);
15105 vec_safe_push (used_rtx_array, rtl);
15106 return true;
15108 return false;
15110 case CONST:
15111 if (CONSTANT_P (XEXP (rtl, 0)))
15112 return add_const_value_attribute (die, XEXP (rtl, 0));
15113 /* FALLTHROUGH */
15114 case SYMBOL_REF:
15115 if (!const_ok_for_output (rtl))
15116 return false;
15117 case LABEL_REF:
15118 if (dwarf_version >= 4 || !dwarf_strict)
15119 goto rtl_addr;
15120 return false;
15122 case PLUS:
15123 /* In cases where an inlined instance of an inline function is passed
15124 the address of an `auto' variable (which is local to the caller) we
15125 can get a situation where the DECL_RTL of the artificial local
15126 variable (for the inlining) which acts as a stand-in for the
15127 corresponding formal parameter (of the inline function) will look
15128 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15129 exactly a compile-time constant expression, but it isn't the address
15130 of the (artificial) local variable either. Rather, it represents the
15131 *value* which the artificial local variable always has during its
15132 lifetime. We currently have no way to represent such quasi-constant
15133 values in Dwarf, so for now we just punt and generate nothing. */
15134 return false;
15136 case HIGH:
15137 case CONST_FIXED:
15138 return false;
15140 case MEM:
15141 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15142 && MEM_READONLY_P (rtl)
15143 && GET_MODE (rtl) == BLKmode)
15145 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15146 return true;
15148 return false;
15150 default:
15151 /* No other kinds of rtx should be possible here. */
15152 gcc_unreachable ();
15154 return false;
15157 /* Determine whether the evaluation of EXPR references any variables
15158 or functions which aren't otherwise used (and therefore may not be
15159 output). */
15160 static tree
15161 reference_to_unused (tree * tp, int * walk_subtrees,
15162 void * data ATTRIBUTE_UNUSED)
15164 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15165 *walk_subtrees = 0;
15167 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15168 && ! TREE_ASM_WRITTEN (*tp))
15169 return *tp;
15170 /* ??? The C++ FE emits debug information for using decls, so
15171 putting gcc_unreachable here falls over. See PR31899. For now
15172 be conservative. */
15173 else if (!cgraph_global_info_ready
15174 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15175 return *tp;
15176 else if (TREE_CODE (*tp) == VAR_DECL)
15178 varpool_node *node = varpool_get_node (*tp);
15179 if (!node || !node->definition)
15180 return *tp;
15182 else if (TREE_CODE (*tp) == FUNCTION_DECL
15183 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15185 /* The call graph machinery must have finished analyzing,
15186 optimizing and gimplifying the CU by now.
15187 So if *TP has no call graph node associated
15188 to it, it means *TP will not be emitted. */
15189 if (!cgraph_get_node (*tp))
15190 return *tp;
15192 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15193 return *tp;
15195 return NULL_TREE;
15198 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15199 for use in a later add_const_value_attribute call. */
15201 static rtx
15202 rtl_for_decl_init (tree init, tree type)
15204 rtx rtl = NULL_RTX;
15206 STRIP_NOPS (init);
15208 /* If a variable is initialized with a string constant without embedded
15209 zeros, build CONST_STRING. */
15210 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15212 tree enttype = TREE_TYPE (type);
15213 tree domain = TYPE_DOMAIN (type);
15214 enum machine_mode mode = TYPE_MODE (enttype);
15216 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15217 && domain
15218 && integer_zerop (TYPE_MIN_VALUE (domain))
15219 && compare_tree_int (TYPE_MAX_VALUE (domain),
15220 TREE_STRING_LENGTH (init) - 1) == 0
15221 && ((size_t) TREE_STRING_LENGTH (init)
15222 == strlen (TREE_STRING_POINTER (init)) + 1))
15224 rtl = gen_rtx_CONST_STRING (VOIDmode,
15225 ggc_strdup (TREE_STRING_POINTER (init)));
15226 rtl = gen_rtx_MEM (BLKmode, rtl);
15227 MEM_READONLY_P (rtl) = 1;
15230 /* Other aggregates, and complex values, could be represented using
15231 CONCAT: FIXME! */
15232 else if (AGGREGATE_TYPE_P (type)
15233 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15234 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15235 || TREE_CODE (type) == COMPLEX_TYPE)
15237 /* Vectors only work if their mode is supported by the target.
15238 FIXME: generic vectors ought to work too. */
15239 else if (TREE_CODE (type) == VECTOR_TYPE
15240 && !VECTOR_MODE_P (TYPE_MODE (type)))
15242 /* If the initializer is something that we know will expand into an
15243 immediate RTL constant, expand it now. We must be careful not to
15244 reference variables which won't be output. */
15245 else if (initializer_constant_valid_p (init, type)
15246 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15248 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15249 possible. */
15250 if (TREE_CODE (type) == VECTOR_TYPE)
15251 switch (TREE_CODE (init))
15253 case VECTOR_CST:
15254 break;
15255 case CONSTRUCTOR:
15256 if (TREE_CONSTANT (init))
15258 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15259 bool constant_p = true;
15260 tree value;
15261 unsigned HOST_WIDE_INT ix;
15263 /* Even when ctor is constant, it might contain non-*_CST
15264 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15265 belong into VECTOR_CST nodes. */
15266 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15267 if (!CONSTANT_CLASS_P (value))
15269 constant_p = false;
15270 break;
15273 if (constant_p)
15275 init = build_vector_from_ctor (type, elts);
15276 break;
15279 /* FALLTHRU */
15281 default:
15282 return NULL;
15285 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15287 /* If expand_expr returns a MEM, it wasn't immediate. */
15288 gcc_assert (!rtl || !MEM_P (rtl));
15291 return rtl;
15294 /* Generate RTL for the variable DECL to represent its location. */
15296 static rtx
15297 rtl_for_decl_location (tree decl)
15299 rtx rtl;
15301 /* Here we have to decide where we are going to say the parameter "lives"
15302 (as far as the debugger is concerned). We only have a couple of
15303 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15305 DECL_RTL normally indicates where the parameter lives during most of the
15306 activation of the function. If optimization is enabled however, this
15307 could be either NULL or else a pseudo-reg. Both of those cases indicate
15308 that the parameter doesn't really live anywhere (as far as the code
15309 generation parts of GCC are concerned) during most of the function's
15310 activation. That will happen (for example) if the parameter is never
15311 referenced within the function.
15313 We could just generate a location descriptor here for all non-NULL
15314 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15315 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15316 where DECL_RTL is NULL or is a pseudo-reg.
15318 Note however that we can only get away with using DECL_INCOMING_RTL as
15319 a backup substitute for DECL_RTL in certain limited cases. In cases
15320 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15321 we can be sure that the parameter was passed using the same type as it is
15322 declared to have within the function, and that its DECL_INCOMING_RTL
15323 points us to a place where a value of that type is passed.
15325 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15326 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15327 because in these cases DECL_INCOMING_RTL points us to a value of some
15328 type which is *different* from the type of the parameter itself. Thus,
15329 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15330 such cases, the debugger would end up (for example) trying to fetch a
15331 `float' from a place which actually contains the first part of a
15332 `double'. That would lead to really incorrect and confusing
15333 output at debug-time.
15335 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15336 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15337 are a couple of exceptions however. On little-endian machines we can
15338 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15339 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15340 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15341 when (on a little-endian machine) a non-prototyped function has a
15342 parameter declared to be of type `short' or `char'. In such cases,
15343 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15344 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15345 passed `int' value. If the debugger then uses that address to fetch
15346 a `short' or a `char' (on a little-endian machine) the result will be
15347 the correct data, so we allow for such exceptional cases below.
15349 Note that our goal here is to describe the place where the given formal
15350 parameter lives during most of the function's activation (i.e. between the
15351 end of the prologue and the start of the epilogue). We'll do that as best
15352 as we can. Note however that if the given formal parameter is modified
15353 sometime during the execution of the function, then a stack backtrace (at
15354 debug-time) will show the function as having been called with the *new*
15355 value rather than the value which was originally passed in. This happens
15356 rarely enough that it is not a major problem, but it *is* a problem, and
15357 I'd like to fix it.
15359 A future version of dwarf2out.c may generate two additional attributes for
15360 any given DW_TAG_formal_parameter DIE which will describe the "passed
15361 type" and the "passed location" for the given formal parameter in addition
15362 to the attributes we now generate to indicate the "declared type" and the
15363 "active location" for each parameter. This additional set of attributes
15364 could be used by debuggers for stack backtraces. Separately, note that
15365 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15366 This happens (for example) for inlined-instances of inline function formal
15367 parameters which are never referenced. This really shouldn't be
15368 happening. All PARM_DECL nodes should get valid non-NULL
15369 DECL_INCOMING_RTL values. FIXME. */
15371 /* Use DECL_RTL as the "location" unless we find something better. */
15372 rtl = DECL_RTL_IF_SET (decl);
15374 /* When generating abstract instances, ignore everything except
15375 constants, symbols living in memory, and symbols living in
15376 fixed registers. */
15377 if (! reload_completed)
15379 if (rtl
15380 && (CONSTANT_P (rtl)
15381 || (MEM_P (rtl)
15382 && CONSTANT_P (XEXP (rtl, 0)))
15383 || (REG_P (rtl)
15384 && TREE_CODE (decl) == VAR_DECL
15385 && TREE_STATIC (decl))))
15387 rtl = targetm.delegitimize_address (rtl);
15388 return rtl;
15390 rtl = NULL_RTX;
15392 else if (TREE_CODE (decl) == PARM_DECL)
15394 if (rtl == NULL_RTX
15395 || is_pseudo_reg (rtl)
15396 || (MEM_P (rtl)
15397 && is_pseudo_reg (XEXP (rtl, 0))
15398 && DECL_INCOMING_RTL (decl)
15399 && MEM_P (DECL_INCOMING_RTL (decl))
15400 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15402 tree declared_type = TREE_TYPE (decl);
15403 tree passed_type = DECL_ARG_TYPE (decl);
15404 enum machine_mode dmode = TYPE_MODE (declared_type);
15405 enum machine_mode pmode = TYPE_MODE (passed_type);
15407 /* This decl represents a formal parameter which was optimized out.
15408 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15409 all cases where (rtl == NULL_RTX) just below. */
15410 if (dmode == pmode)
15411 rtl = DECL_INCOMING_RTL (decl);
15412 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15413 && SCALAR_INT_MODE_P (dmode)
15414 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15415 && DECL_INCOMING_RTL (decl))
15417 rtx inc = DECL_INCOMING_RTL (decl);
15418 if (REG_P (inc))
15419 rtl = inc;
15420 else if (MEM_P (inc))
15422 if (BYTES_BIG_ENDIAN)
15423 rtl = adjust_address_nv (inc, dmode,
15424 GET_MODE_SIZE (pmode)
15425 - GET_MODE_SIZE (dmode));
15426 else
15427 rtl = inc;
15432 /* If the parm was passed in registers, but lives on the stack, then
15433 make a big endian correction if the mode of the type of the
15434 parameter is not the same as the mode of the rtl. */
15435 /* ??? This is the same series of checks that are made in dbxout.c before
15436 we reach the big endian correction code there. It isn't clear if all
15437 of these checks are necessary here, but keeping them all is the safe
15438 thing to do. */
15439 else if (MEM_P (rtl)
15440 && XEXP (rtl, 0) != const0_rtx
15441 && ! CONSTANT_P (XEXP (rtl, 0))
15442 /* Not passed in memory. */
15443 && !MEM_P (DECL_INCOMING_RTL (decl))
15444 /* Not passed by invisible reference. */
15445 && (!REG_P (XEXP (rtl, 0))
15446 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15447 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15448 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15449 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15450 #endif
15452 /* Big endian correction check. */
15453 && BYTES_BIG_ENDIAN
15454 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15455 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15456 < UNITS_PER_WORD))
15458 enum machine_mode addr_mode = get_address_mode (rtl);
15459 int offset = (UNITS_PER_WORD
15460 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15462 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15463 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15466 else if (TREE_CODE (decl) == VAR_DECL
15467 && rtl
15468 && MEM_P (rtl)
15469 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15470 && BYTES_BIG_ENDIAN)
15472 enum machine_mode addr_mode = get_address_mode (rtl);
15473 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15474 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15476 /* If a variable is declared "register" yet is smaller than
15477 a register, then if we store the variable to memory, it
15478 looks like we're storing a register-sized value, when in
15479 fact we are not. We need to adjust the offset of the
15480 storage location to reflect the actual value's bytes,
15481 else gdb will not be able to display it. */
15482 if (rsize > dsize)
15483 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15484 plus_constant (addr_mode, XEXP (rtl, 0),
15485 rsize - dsize));
15488 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15489 and will have been substituted directly into all expressions that use it.
15490 C does not have such a concept, but C++ and other languages do. */
15491 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15492 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15494 if (rtl)
15495 rtl = targetm.delegitimize_address (rtl);
15497 /* If we don't look past the constant pool, we risk emitting a
15498 reference to a constant pool entry that isn't referenced from
15499 code, and thus is not emitted. */
15500 if (rtl)
15501 rtl = avoid_constant_pool_reference (rtl);
15503 /* Try harder to get a rtl. If this symbol ends up not being emitted
15504 in the current CU, resolve_addr will remove the expression referencing
15505 it. */
15506 if (rtl == NULL_RTX
15507 && TREE_CODE (decl) == VAR_DECL
15508 && !DECL_EXTERNAL (decl)
15509 && TREE_STATIC (decl)
15510 && DECL_NAME (decl)
15511 && !DECL_HARD_REGISTER (decl)
15512 && DECL_MODE (decl) != VOIDmode)
15514 rtl = make_decl_rtl_for_debug (decl);
15515 if (!MEM_P (rtl)
15516 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15517 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15518 rtl = NULL_RTX;
15521 return rtl;
15524 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15525 returned. If so, the decl for the COMMON block is returned, and the
15526 value is the offset into the common block for the symbol. */
15528 static tree
15529 fortran_common (tree decl, HOST_WIDE_INT *value)
15531 tree val_expr, cvar;
15532 enum machine_mode mode;
15533 HOST_WIDE_INT bitsize, bitpos;
15534 tree offset;
15535 int unsignedp, volatilep = 0;
15537 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15538 it does not have a value (the offset into the common area), or if it
15539 is thread local (as opposed to global) then it isn't common, and shouldn't
15540 be handled as such. */
15541 if (TREE_CODE (decl) != VAR_DECL
15542 || !TREE_STATIC (decl)
15543 || !DECL_HAS_VALUE_EXPR_P (decl)
15544 || !is_fortran ())
15545 return NULL_TREE;
15547 val_expr = DECL_VALUE_EXPR (decl);
15548 if (TREE_CODE (val_expr) != COMPONENT_REF)
15549 return NULL_TREE;
15551 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15552 &mode, &unsignedp, &volatilep, true);
15554 if (cvar == NULL_TREE
15555 || TREE_CODE (cvar) != VAR_DECL
15556 || DECL_ARTIFICIAL (cvar)
15557 || !TREE_PUBLIC (cvar))
15558 return NULL_TREE;
15560 *value = 0;
15561 if (offset != NULL)
15563 if (!tree_fits_shwi_p (offset))
15564 return NULL_TREE;
15565 *value = tree_to_shwi (offset);
15567 if (bitpos != 0)
15568 *value += bitpos / BITS_PER_UNIT;
15570 return cvar;
15573 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15574 data attribute for a variable or a parameter. We generate the
15575 DW_AT_const_value attribute only in those cases where the given variable
15576 or parameter does not have a true "location" either in memory or in a
15577 register. This can happen (for example) when a constant is passed as an
15578 actual argument in a call to an inline function. (It's possible that
15579 these things can crop up in other ways also.) Note that one type of
15580 constant value which can be passed into an inlined function is a constant
15581 pointer. This can happen for example if an actual argument in an inlined
15582 function call evaluates to a compile-time constant address.
15584 CACHE_P is true if it is worth caching the location list for DECL,
15585 so that future calls can reuse it rather than regenerate it from scratch.
15586 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15587 since we will need to refer to them each time the function is inlined. */
15589 static bool
15590 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15591 enum dwarf_attribute attr)
15593 rtx rtl;
15594 dw_loc_list_ref list;
15595 var_loc_list *loc_list;
15596 cached_dw_loc_list *cache;
15597 void **slot;
15599 if (TREE_CODE (decl) == ERROR_MARK)
15600 return false;
15602 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15603 || TREE_CODE (decl) == RESULT_DECL);
15605 /* Try to get some constant RTL for this decl, and use that as the value of
15606 the location. */
15608 rtl = rtl_for_decl_location (decl);
15609 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15610 && add_const_value_attribute (die, rtl))
15611 return true;
15613 /* See if we have single element location list that is equivalent to
15614 a constant value. That way we are better to use add_const_value_attribute
15615 rather than expanding constant value equivalent. */
15616 loc_list = lookup_decl_loc (decl);
15617 if (loc_list
15618 && loc_list->first
15619 && loc_list->first->next == NULL
15620 && NOTE_P (loc_list->first->loc)
15621 && NOTE_VAR_LOCATION (loc_list->first->loc)
15622 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15624 struct var_loc_node *node;
15626 node = loc_list->first;
15627 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15628 if (GET_CODE (rtl) == EXPR_LIST)
15629 rtl = XEXP (rtl, 0);
15630 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15631 && add_const_value_attribute (die, rtl))
15632 return true;
15634 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15635 list several times. See if we've already cached the contents. */
15636 list = NULL;
15637 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15638 cache_p = false;
15639 if (cache_p)
15641 cache = (cached_dw_loc_list *)
15642 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15643 if (cache)
15644 list = cache->loc_list;
15646 if (list == NULL)
15648 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15649 /* It is usually worth caching this result if the decl is from
15650 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15651 if (cache_p && list && list->dw_loc_next)
15653 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15654 DECL_UID (decl), INSERT);
15655 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15656 cache->decl_id = DECL_UID (decl);
15657 cache->loc_list = list;
15658 *slot = cache;
15661 if (list)
15663 add_AT_location_description (die, attr, list);
15664 return true;
15666 /* None of that worked, so it must not really have a location;
15667 try adding a constant value attribute from the DECL_INITIAL. */
15668 return tree_add_const_value_attribute_for_decl (die, decl);
15671 /* Add VARIABLE and DIE into deferred locations list. */
15673 static void
15674 defer_location (tree variable, dw_die_ref die)
15676 deferred_locations entry;
15677 entry.variable = variable;
15678 entry.die = die;
15679 vec_safe_push (deferred_locations_list, entry);
15682 /* Helper function for tree_add_const_value_attribute. Natively encode
15683 initializer INIT into an array. Return true if successful. */
15685 static bool
15686 native_encode_initializer (tree init, unsigned char *array, int size)
15688 tree type;
15690 if (init == NULL_TREE)
15691 return false;
15693 STRIP_NOPS (init);
15694 switch (TREE_CODE (init))
15696 case STRING_CST:
15697 type = TREE_TYPE (init);
15698 if (TREE_CODE (type) == ARRAY_TYPE)
15700 tree enttype = TREE_TYPE (type);
15701 enum machine_mode mode = TYPE_MODE (enttype);
15703 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15704 return false;
15705 if (int_size_in_bytes (type) != size)
15706 return false;
15707 if (size > TREE_STRING_LENGTH (init))
15709 memcpy (array, TREE_STRING_POINTER (init),
15710 TREE_STRING_LENGTH (init));
15711 memset (array + TREE_STRING_LENGTH (init),
15712 '\0', size - TREE_STRING_LENGTH (init));
15714 else
15715 memcpy (array, TREE_STRING_POINTER (init), size);
15716 return true;
15718 return false;
15719 case CONSTRUCTOR:
15720 type = TREE_TYPE (init);
15721 if (int_size_in_bytes (type) != size)
15722 return false;
15723 if (TREE_CODE (type) == ARRAY_TYPE)
15725 HOST_WIDE_INT min_index;
15726 unsigned HOST_WIDE_INT cnt;
15727 int curpos = 0, fieldsize;
15728 constructor_elt *ce;
15730 if (TYPE_DOMAIN (type) == NULL_TREE
15731 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15732 return false;
15734 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15735 if (fieldsize <= 0)
15736 return false;
15738 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15739 memset (array, '\0', size);
15740 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15742 tree val = ce->value;
15743 tree index = ce->index;
15744 int pos = curpos;
15745 if (index && TREE_CODE (index) == RANGE_EXPR)
15746 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15747 * fieldsize;
15748 else if (index)
15749 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15751 if (val)
15753 STRIP_NOPS (val);
15754 if (!native_encode_initializer (val, array + pos, fieldsize))
15755 return false;
15757 curpos = pos + fieldsize;
15758 if (index && TREE_CODE (index) == RANGE_EXPR)
15760 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15761 - tree_to_shwi (TREE_OPERAND (index, 0));
15762 while (count-- > 0)
15764 if (val)
15765 memcpy (array + curpos, array + pos, fieldsize);
15766 curpos += fieldsize;
15769 gcc_assert (curpos <= size);
15771 return true;
15773 else if (TREE_CODE (type) == RECORD_TYPE
15774 || TREE_CODE (type) == UNION_TYPE)
15776 tree field = NULL_TREE;
15777 unsigned HOST_WIDE_INT cnt;
15778 constructor_elt *ce;
15780 if (int_size_in_bytes (type) != size)
15781 return false;
15783 if (TREE_CODE (type) == RECORD_TYPE)
15784 field = TYPE_FIELDS (type);
15786 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15788 tree val = ce->value;
15789 int pos, fieldsize;
15791 if (ce->index != 0)
15792 field = ce->index;
15794 if (val)
15795 STRIP_NOPS (val);
15797 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15798 return false;
15800 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15801 && TYPE_DOMAIN (TREE_TYPE (field))
15802 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15803 return false;
15804 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15805 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15806 return false;
15807 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15808 pos = int_byte_position (field);
15809 gcc_assert (pos + fieldsize <= size);
15810 if (val
15811 && !native_encode_initializer (val, array + pos, fieldsize))
15812 return false;
15814 return true;
15816 return false;
15817 case VIEW_CONVERT_EXPR:
15818 case NON_LVALUE_EXPR:
15819 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15820 default:
15821 return native_encode_expr (init, array, size) == size;
15825 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15826 attribute is the const value T. */
15828 static bool
15829 tree_add_const_value_attribute (dw_die_ref die, tree t)
15831 tree init;
15832 tree type = TREE_TYPE (t);
15833 rtx rtl;
15835 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15836 return false;
15838 init = t;
15839 gcc_assert (!DECL_P (init));
15841 rtl = rtl_for_decl_init (init, type);
15842 if (rtl)
15843 return add_const_value_attribute (die, rtl);
15844 /* If the host and target are sane, try harder. */
15845 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15846 && initializer_constant_valid_p (init, type))
15848 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15849 if (size > 0 && (int) size == size)
15851 unsigned char *array = (unsigned char *)
15852 ggc_alloc_cleared_atomic (size);
15854 if (native_encode_initializer (init, array, size))
15856 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15857 return true;
15859 ggc_free (array);
15862 return false;
15865 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15866 attribute is the const value of T, where T is an integral constant
15867 variable with static storage duration
15868 (so it can't be a PARM_DECL or a RESULT_DECL). */
15870 static bool
15871 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15874 if (!decl
15875 || (TREE_CODE (decl) != VAR_DECL
15876 && TREE_CODE (decl) != CONST_DECL)
15877 || (TREE_CODE (decl) == VAR_DECL
15878 && !TREE_STATIC (decl)))
15879 return false;
15881 if (TREE_READONLY (decl)
15882 && ! TREE_THIS_VOLATILE (decl)
15883 && DECL_INITIAL (decl))
15884 /* OK */;
15885 else
15886 return false;
15888 /* Don't add DW_AT_const_value if abstract origin already has one. */
15889 if (get_AT (var_die, DW_AT_const_value))
15890 return false;
15892 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15895 /* Convert the CFI instructions for the current function into a
15896 location list. This is used for DW_AT_frame_base when we targeting
15897 a dwarf2 consumer that does not support the dwarf3
15898 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15899 expressions. */
15901 static dw_loc_list_ref
15902 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15904 int ix;
15905 dw_fde_ref fde;
15906 dw_loc_list_ref list, *list_tail;
15907 dw_cfi_ref cfi;
15908 dw_cfa_location last_cfa, next_cfa;
15909 const char *start_label, *last_label, *section;
15910 dw_cfa_location remember;
15912 fde = cfun->fde;
15913 gcc_assert (fde != NULL);
15915 section = secname_for_decl (current_function_decl);
15916 list_tail = &list;
15917 list = NULL;
15919 memset (&next_cfa, 0, sizeof (next_cfa));
15920 next_cfa.reg = INVALID_REGNUM;
15921 remember = next_cfa;
15923 start_label = fde->dw_fde_begin;
15925 /* ??? Bald assumption that the CIE opcode list does not contain
15926 advance opcodes. */
15927 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15928 lookup_cfa_1 (cfi, &next_cfa, &remember);
15930 last_cfa = next_cfa;
15931 last_label = start_label;
15933 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15935 /* If the first partition contained no CFI adjustments, the
15936 CIE opcodes apply to the whole first partition. */
15937 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15938 fde->dw_fde_begin, fde->dw_fde_end, section);
15939 list_tail =&(*list_tail)->dw_loc_next;
15940 start_label = last_label = fde->dw_fde_second_begin;
15943 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15945 switch (cfi->dw_cfi_opc)
15947 case DW_CFA_set_loc:
15948 case DW_CFA_advance_loc1:
15949 case DW_CFA_advance_loc2:
15950 case DW_CFA_advance_loc4:
15951 if (!cfa_equal_p (&last_cfa, &next_cfa))
15953 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15954 start_label, last_label, section);
15956 list_tail = &(*list_tail)->dw_loc_next;
15957 last_cfa = next_cfa;
15958 start_label = last_label;
15960 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15961 break;
15963 case DW_CFA_advance_loc:
15964 /* The encoding is complex enough that we should never emit this. */
15965 gcc_unreachable ();
15967 default:
15968 lookup_cfa_1 (cfi, &next_cfa, &remember);
15969 break;
15971 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15973 if (!cfa_equal_p (&last_cfa, &next_cfa))
15975 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15976 start_label, last_label, section);
15978 list_tail = &(*list_tail)->dw_loc_next;
15979 last_cfa = next_cfa;
15980 start_label = last_label;
15982 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15983 start_label, fde->dw_fde_end, section);
15984 list_tail = &(*list_tail)->dw_loc_next;
15985 start_label = last_label = fde->dw_fde_second_begin;
15989 if (!cfa_equal_p (&last_cfa, &next_cfa))
15991 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15992 start_label, last_label, section);
15993 list_tail = &(*list_tail)->dw_loc_next;
15994 start_label = last_label;
15997 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15998 start_label,
15999 fde->dw_fde_second_begin
16000 ? fde->dw_fde_second_end : fde->dw_fde_end,
16001 section);
16003 if (list && list->dw_loc_next)
16004 gen_llsym (list);
16006 return list;
16009 /* Compute a displacement from the "steady-state frame pointer" to the
16010 frame base (often the same as the CFA), and store it in
16011 frame_pointer_fb_offset. OFFSET is added to the displacement
16012 before the latter is negated. */
16014 static void
16015 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16017 rtx reg, elim;
16019 #ifdef FRAME_POINTER_CFA_OFFSET
16020 reg = frame_pointer_rtx;
16021 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16022 #else
16023 reg = arg_pointer_rtx;
16024 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16025 #endif
16027 elim = (ira_use_lra_p
16028 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16029 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16030 if (GET_CODE (elim) == PLUS)
16032 offset += INTVAL (XEXP (elim, 1));
16033 elim = XEXP (elim, 0);
16036 frame_pointer_fb_offset = -offset;
16038 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16039 in which to eliminate. This is because it's stack pointer isn't
16040 directly accessible as a register within the ISA. To work around
16041 this, assume that while we cannot provide a proper value for
16042 frame_pointer_fb_offset, we won't need one either. */
16043 frame_pointer_fb_offset_valid
16044 = ((SUPPORTS_STACK_ALIGNMENT
16045 && (elim == hard_frame_pointer_rtx
16046 || elim == stack_pointer_rtx))
16047 || elim == (frame_pointer_needed
16048 ? hard_frame_pointer_rtx
16049 : stack_pointer_rtx));
16052 /* Generate a DW_AT_name attribute given some string value to be included as
16053 the value of the attribute. */
16055 static void
16056 add_name_attribute (dw_die_ref die, const char *name_string)
16058 if (name_string != NULL && *name_string != 0)
16060 if (demangle_name_func)
16061 name_string = (*demangle_name_func) (name_string);
16063 add_AT_string (die, DW_AT_name, name_string);
16067 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16068 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16069 of TYPE accordingly.
16071 ??? This is a temporary measure until after we're able to generate
16072 regular DWARF for the complex Ada type system. */
16074 static void
16075 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16076 dw_die_ref context_die)
16078 tree dtype;
16079 dw_die_ref dtype_die;
16081 if (!lang_hooks.types.descriptive_type)
16082 return;
16084 dtype = lang_hooks.types.descriptive_type (type);
16085 if (!dtype)
16086 return;
16088 dtype_die = lookup_type_die (dtype);
16089 if (!dtype_die)
16091 gen_type_die (dtype, context_die);
16092 dtype_die = lookup_type_die (dtype);
16093 gcc_assert (dtype_die);
16096 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16099 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16101 static const char *
16102 comp_dir_string (void)
16104 const char *wd;
16105 char *wd1;
16106 static const char *cached_wd = NULL;
16108 if (cached_wd != NULL)
16109 return cached_wd;
16111 wd = get_src_pwd ();
16112 if (wd == NULL)
16113 return NULL;
16115 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16117 int wdlen;
16119 wdlen = strlen (wd);
16120 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16121 strcpy (wd1, wd);
16122 wd1 [wdlen] = DIR_SEPARATOR;
16123 wd1 [wdlen + 1] = 0;
16124 wd = wd1;
16127 cached_wd = remap_debug_filename (wd);
16128 return cached_wd;
16131 /* Generate a DW_AT_comp_dir attribute for DIE. */
16133 static void
16134 add_comp_dir_attribute (dw_die_ref die)
16136 const char * wd = comp_dir_string ();
16137 if (wd != NULL)
16138 add_AT_string (die, DW_AT_comp_dir, wd);
16141 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16142 default. */
16144 static int
16145 lower_bound_default (void)
16147 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16149 case DW_LANG_C:
16150 case DW_LANG_C89:
16151 case DW_LANG_C99:
16152 case DW_LANG_C_plus_plus:
16153 case DW_LANG_ObjC:
16154 case DW_LANG_ObjC_plus_plus:
16155 case DW_LANG_Java:
16156 return 0;
16157 case DW_LANG_Fortran77:
16158 case DW_LANG_Fortran90:
16159 case DW_LANG_Fortran95:
16160 return 1;
16161 case DW_LANG_UPC:
16162 case DW_LANG_D:
16163 case DW_LANG_Python:
16164 return dwarf_version >= 4 ? 0 : -1;
16165 case DW_LANG_Ada95:
16166 case DW_LANG_Ada83:
16167 case DW_LANG_Cobol74:
16168 case DW_LANG_Cobol85:
16169 case DW_LANG_Pascal83:
16170 case DW_LANG_Modula2:
16171 case DW_LANG_PLI:
16172 return dwarf_version >= 4 ? 1 : -1;
16173 default:
16174 return -1;
16178 /* Given a tree node describing an array bound (either lower or upper) output
16179 a representation for that bound. */
16181 static void
16182 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16184 switch (TREE_CODE (bound))
16186 case ERROR_MARK:
16187 return;
16189 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16190 case INTEGER_CST:
16192 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16193 int dflt;
16195 /* Use the default if possible. */
16196 if (bound_attr == DW_AT_lower_bound
16197 && tree_fits_shwi_p (bound)
16198 && (dflt = lower_bound_default ()) != -1
16199 && tree_to_shwi (bound) == dflt)
16202 /* Otherwise represent the bound as an unsigned value with the
16203 precision of its type. The precision and signedness of the
16204 type will be necessary to re-interpret it unambiguously. */
16205 else if (prec < HOST_BITS_PER_WIDE_INT)
16207 unsigned HOST_WIDE_INT mask
16208 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16209 add_AT_unsigned (subrange_die, bound_attr,
16210 TREE_INT_CST_LOW (bound) & mask);
16212 else if (prec == HOST_BITS_PER_WIDE_INT
16213 || TREE_INT_CST_HIGH (bound) == 0)
16214 add_AT_unsigned (subrange_die, bound_attr,
16215 TREE_INT_CST_LOW (bound));
16216 else
16217 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16218 TREE_INT_CST_LOW (bound));
16220 break;
16222 CASE_CONVERT:
16223 case VIEW_CONVERT_EXPR:
16224 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16225 break;
16227 case SAVE_EXPR:
16228 break;
16230 case VAR_DECL:
16231 case PARM_DECL:
16232 case RESULT_DECL:
16234 dw_die_ref decl_die = lookup_decl_die (bound);
16236 /* ??? Can this happen, or should the variable have been bound
16237 first? Probably it can, since I imagine that we try to create
16238 the types of parameters in the order in which they exist in
16239 the list, and won't have created a forward reference to a
16240 later parameter. */
16241 if (decl_die != NULL)
16243 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16244 break;
16247 /* FALLTHRU */
16249 default:
16251 /* Otherwise try to create a stack operation procedure to
16252 evaluate the value of the array bound. */
16254 dw_die_ref ctx, decl_die;
16255 dw_loc_list_ref list;
16257 list = loc_list_from_tree (bound, 2);
16258 if (list == NULL || single_element_loc_list_p (list))
16260 /* If DW_AT_*bound is not a reference nor constant, it is
16261 a DWARF expression rather than location description.
16262 For that loc_list_from_tree (bound, 0) is needed.
16263 If that fails to give a single element list,
16264 fall back to outputting this as a reference anyway. */
16265 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16266 if (list2 && single_element_loc_list_p (list2))
16268 add_AT_loc (subrange_die, bound_attr, list2->expr);
16269 break;
16272 if (list == NULL)
16273 break;
16275 if (current_function_decl == 0)
16276 ctx = comp_unit_die ();
16277 else
16278 ctx = lookup_decl_die (current_function_decl);
16280 decl_die = new_die (DW_TAG_variable, ctx, bound);
16281 add_AT_flag (decl_die, DW_AT_artificial, 1);
16282 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16283 add_AT_location_description (decl_die, DW_AT_location, list);
16284 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16285 break;
16290 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16291 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16292 Note that the block of subscript information for an array type also
16293 includes information about the element type of the given array type. */
16295 static void
16296 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16298 unsigned dimension_number;
16299 tree lower, upper;
16300 dw_die_ref subrange_die;
16302 for (dimension_number = 0;
16303 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16304 type = TREE_TYPE (type), dimension_number++)
16306 tree domain = TYPE_DOMAIN (type);
16308 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16309 break;
16311 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16312 and (in GNU C only) variable bounds. Handle all three forms
16313 here. */
16314 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16315 if (domain)
16317 /* We have an array type with specified bounds. */
16318 lower = TYPE_MIN_VALUE (domain);
16319 upper = TYPE_MAX_VALUE (domain);
16321 /* Define the index type. */
16322 if (TREE_TYPE (domain))
16324 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16325 TREE_TYPE field. We can't emit debug info for this
16326 because it is an unnamed integral type. */
16327 if (TREE_CODE (domain) == INTEGER_TYPE
16328 && TYPE_NAME (domain) == NULL_TREE
16329 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16330 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16332 else
16333 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16334 type_die);
16337 /* ??? If upper is NULL, the array has unspecified length,
16338 but it does have a lower bound. This happens with Fortran
16339 dimension arr(N:*)
16340 Since the debugger is definitely going to need to know N
16341 to produce useful results, go ahead and output the lower
16342 bound solo, and hope the debugger can cope. */
16344 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16345 if (upper)
16346 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16349 /* Otherwise we have an array type with an unspecified length. The
16350 DWARF-2 spec does not say how to handle this; let's just leave out the
16351 bounds. */
16355 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16357 static void
16358 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16360 dw_die_ref decl_die;
16361 HOST_WIDE_INT size;
16363 switch (TREE_CODE (tree_node))
16365 case ERROR_MARK:
16366 size = 0;
16367 break;
16368 case ENUMERAL_TYPE:
16369 case RECORD_TYPE:
16370 case UNION_TYPE:
16371 case QUAL_UNION_TYPE:
16372 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16373 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16375 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16376 return;
16378 size = int_size_in_bytes (tree_node);
16379 break;
16380 case FIELD_DECL:
16381 /* For a data member of a struct or union, the DW_AT_byte_size is
16382 generally given as the number of bytes normally allocated for an
16383 object of the *declared* type of the member itself. This is true
16384 even for bit-fields. */
16385 size = int_size_in_bytes (field_type (tree_node));
16386 break;
16387 default:
16388 gcc_unreachable ();
16391 /* Note that `size' might be -1 when we get to this point. If it is, that
16392 indicates that the byte size of the entity in question is variable. We
16393 have no good way of expressing this fact in Dwarf at the present time,
16394 when location description was not used by the caller code instead. */
16395 if (size >= 0)
16396 add_AT_unsigned (die, DW_AT_byte_size, size);
16399 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16400 which specifies the distance in bits from the highest order bit of the
16401 "containing object" for the bit-field to the highest order bit of the
16402 bit-field itself.
16404 For any given bit-field, the "containing object" is a hypothetical object
16405 (of some integral or enum type) within which the given bit-field lives. The
16406 type of this hypothetical "containing object" is always the same as the
16407 declared type of the individual bit-field itself. The determination of the
16408 exact location of the "containing object" for a bit-field is rather
16409 complicated. It's handled by the `field_byte_offset' function (above).
16411 Note that it is the size (in bytes) of the hypothetical "containing object"
16412 which will be given in the DW_AT_byte_size attribute for this bit-field.
16413 (See `byte_size_attribute' above). */
16415 static inline void
16416 add_bit_offset_attribute (dw_die_ref die, tree decl)
16418 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16419 tree type = DECL_BIT_FIELD_TYPE (decl);
16420 HOST_WIDE_INT bitpos_int;
16421 HOST_WIDE_INT highest_order_object_bit_offset;
16422 HOST_WIDE_INT highest_order_field_bit_offset;
16423 HOST_WIDE_INT bit_offset;
16425 /* Must be a field and a bit field. */
16426 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16428 /* We can't yet handle bit-fields whose offsets are variable, so if we
16429 encounter such things, just return without generating any attribute
16430 whatsoever. Likewise for variable or too large size. */
16431 if (! tree_fits_shwi_p (bit_position (decl))
16432 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16433 return;
16435 bitpos_int = int_bit_position (decl);
16437 /* Note that the bit offset is always the distance (in bits) from the
16438 highest-order bit of the "containing object" to the highest-order bit of
16439 the bit-field itself. Since the "high-order end" of any object or field
16440 is different on big-endian and little-endian machines, the computation
16441 below must take account of these differences. */
16442 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16443 highest_order_field_bit_offset = bitpos_int;
16445 if (! BYTES_BIG_ENDIAN)
16447 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16448 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16451 bit_offset
16452 = (! BYTES_BIG_ENDIAN
16453 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16454 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16456 if (bit_offset < 0)
16457 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16458 else
16459 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16462 /* For a FIELD_DECL node which represents a bit field, output an attribute
16463 which specifies the length in bits of the given field. */
16465 static inline void
16466 add_bit_size_attribute (dw_die_ref die, tree decl)
16468 /* Must be a field and a bit field. */
16469 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16470 && DECL_BIT_FIELD_TYPE (decl));
16472 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16473 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16476 /* If the compiled language is ANSI C, then add a 'prototyped'
16477 attribute, if arg types are given for the parameters of a function. */
16479 static inline void
16480 add_prototyped_attribute (dw_die_ref die, tree func_type)
16482 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16483 && prototype_p (func_type))
16484 add_AT_flag (die, DW_AT_prototyped, 1);
16487 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16488 by looking in either the type declaration or object declaration
16489 equate table. */
16491 static inline dw_die_ref
16492 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16494 dw_die_ref origin_die = NULL;
16496 if (TREE_CODE (origin) != FUNCTION_DECL)
16498 /* We may have gotten separated from the block for the inlined
16499 function, if we're in an exception handler or some such; make
16500 sure that the abstract function has been written out.
16502 Doing this for nested functions is wrong, however; functions are
16503 distinct units, and our context might not even be inline. */
16504 tree fn = origin;
16506 if (TYPE_P (fn))
16507 fn = TYPE_STUB_DECL (fn);
16509 fn = decl_function_context (fn);
16510 if (fn)
16511 dwarf2out_abstract_function (fn);
16514 if (DECL_P (origin))
16515 origin_die = lookup_decl_die (origin);
16516 else if (TYPE_P (origin))
16517 origin_die = lookup_type_die (origin);
16519 /* XXX: Functions that are never lowered don't always have correct block
16520 trees (in the case of java, they simply have no block tree, in some other
16521 languages). For these functions, there is nothing we can really do to
16522 output correct debug info for inlined functions in all cases. Rather
16523 than die, we'll just produce deficient debug info now, in that we will
16524 have variables without a proper abstract origin. In the future, when all
16525 functions are lowered, we should re-add a gcc_assert (origin_die)
16526 here. */
16528 if (origin_die)
16529 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16530 return origin_die;
16533 /* We do not currently support the pure_virtual attribute. */
16535 static inline void
16536 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16538 if (DECL_VINDEX (func_decl))
16540 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16542 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16543 add_AT_loc (die, DW_AT_vtable_elem_location,
16544 new_loc_descr (DW_OP_constu,
16545 tree_to_shwi (DECL_VINDEX (func_decl)),
16546 0));
16548 /* GNU extension: Record what type this method came from originally. */
16549 if (debug_info_level > DINFO_LEVEL_TERSE
16550 && DECL_CONTEXT (func_decl))
16551 add_AT_die_ref (die, DW_AT_containing_type,
16552 lookup_type_die (DECL_CONTEXT (func_decl)));
16556 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16557 given decl. This used to be a vendor extension until after DWARF 4
16558 standardized it. */
16560 static void
16561 add_linkage_attr (dw_die_ref die, tree decl)
16563 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16565 /* Mimic what assemble_name_raw does with a leading '*'. */
16566 if (name[0] == '*')
16567 name = &name[1];
16569 if (dwarf_version >= 4)
16570 add_AT_string (die, DW_AT_linkage_name, name);
16571 else
16572 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16575 /* Add source coordinate attributes for the given decl. */
16577 static void
16578 add_src_coords_attributes (dw_die_ref die, tree decl)
16580 expanded_location s;
16582 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16583 return;
16584 s = expand_location (DECL_SOURCE_LOCATION (decl));
16585 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16586 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16589 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16591 static void
16592 add_linkage_name (dw_die_ref die, tree decl)
16594 if (debug_info_level > DINFO_LEVEL_TERSE
16595 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16596 && TREE_PUBLIC (decl)
16597 && !DECL_ABSTRACT (decl)
16598 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16599 && die->die_tag != DW_TAG_member)
16601 /* Defer until we have an assembler name set. */
16602 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16604 limbo_die_node *asm_name;
16606 asm_name = ggc_alloc_cleared_limbo_die_node ();
16607 asm_name->die = die;
16608 asm_name->created_for = decl;
16609 asm_name->next = deferred_asm_name;
16610 deferred_asm_name = asm_name;
16612 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16613 add_linkage_attr (die, decl);
16617 /* Add a DW_AT_name attribute and source coordinate attribute for the
16618 given decl, but only if it actually has a name. */
16620 static void
16621 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16623 tree decl_name;
16625 decl_name = DECL_NAME (decl);
16626 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16628 const char *name = dwarf2_name (decl, 0);
16629 if (name)
16630 add_name_attribute (die, name);
16631 if (! DECL_ARTIFICIAL (decl))
16632 add_src_coords_attributes (die, decl);
16634 add_linkage_name (die, decl);
16637 #ifdef VMS_DEBUGGING_INFO
16638 /* Get the function's name, as described by its RTL. This may be different
16639 from the DECL_NAME name used in the source file. */
16640 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16642 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16643 XEXP (DECL_RTL (decl), 0), false);
16644 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16646 #endif /* VMS_DEBUGGING_INFO */
16649 #ifdef VMS_DEBUGGING_INFO
16650 /* Output the debug main pointer die for VMS */
16652 void
16653 dwarf2out_vms_debug_main_pointer (void)
16655 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16656 dw_die_ref die;
16658 /* Allocate the VMS debug main subprogram die. */
16659 die = ggc_alloc_cleared_die_node ();
16660 die->die_tag = DW_TAG_subprogram;
16661 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16662 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16663 FUNC_LABEL_ID (cfun));
16664 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16666 /* Make it the first child of comp_unit_die (). */
16667 die->die_parent = comp_unit_die ();
16668 if (comp_unit_die ()->die_child)
16670 die->die_sib = comp_unit_die ()->die_child->die_sib;
16671 comp_unit_die ()->die_child->die_sib = die;
16673 else
16675 die->die_sib = die;
16676 comp_unit_die ()->die_child = die;
16679 #endif /* VMS_DEBUGGING_INFO */
16681 /* Push a new declaration scope. */
16683 static void
16684 push_decl_scope (tree scope)
16686 vec_safe_push (decl_scope_table, scope);
16689 /* Pop a declaration scope. */
16691 static inline void
16692 pop_decl_scope (void)
16694 decl_scope_table->pop ();
16697 /* walk_tree helper function for uses_local_type, below. */
16699 static tree
16700 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16702 if (!TYPE_P (*tp))
16703 *walk_subtrees = 0;
16704 else
16706 tree name = TYPE_NAME (*tp);
16707 if (name && DECL_P (name) && decl_function_context (name))
16708 return *tp;
16710 return NULL_TREE;
16713 /* If TYPE involves a function-local type (including a local typedef to a
16714 non-local type), returns that type; otherwise returns NULL_TREE. */
16716 static tree
16717 uses_local_type (tree type)
16719 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16720 return used;
16723 /* Return the DIE for the scope that immediately contains this type.
16724 Non-named types that do not involve a function-local type get global
16725 scope. Named types nested in namespaces or other types get their
16726 containing scope. All other types (i.e. function-local named types) get
16727 the current active scope. */
16729 static dw_die_ref
16730 scope_die_for (tree t, dw_die_ref context_die)
16732 dw_die_ref scope_die = NULL;
16733 tree containing_scope;
16735 /* Non-types always go in the current scope. */
16736 gcc_assert (TYPE_P (t));
16738 /* Use the scope of the typedef, rather than the scope of the type
16739 it refers to. */
16740 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16741 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16742 else
16743 containing_scope = TYPE_CONTEXT (t);
16745 /* Use the containing namespace if there is one. */
16746 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16748 if (context_die == lookup_decl_die (containing_scope))
16749 /* OK */;
16750 else if (debug_info_level > DINFO_LEVEL_TERSE)
16751 context_die = get_context_die (containing_scope);
16752 else
16753 containing_scope = NULL_TREE;
16756 /* Ignore function type "scopes" from the C frontend. They mean that
16757 a tagged type is local to a parmlist of a function declarator, but
16758 that isn't useful to DWARF. */
16759 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16760 containing_scope = NULL_TREE;
16762 if (SCOPE_FILE_SCOPE_P (containing_scope))
16764 /* If T uses a local type keep it local as well, to avoid references
16765 to function-local DIEs from outside the function. */
16766 if (current_function_decl && uses_local_type (t))
16767 scope_die = context_die;
16768 else
16769 scope_die = comp_unit_die ();
16771 else if (TYPE_P (containing_scope))
16773 /* For types, we can just look up the appropriate DIE. */
16774 if (debug_info_level > DINFO_LEVEL_TERSE)
16775 scope_die = get_context_die (containing_scope);
16776 else
16778 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16779 if (scope_die == NULL)
16780 scope_die = comp_unit_die ();
16783 else
16784 scope_die = context_die;
16786 return scope_die;
16789 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16791 static inline int
16792 local_scope_p (dw_die_ref context_die)
16794 for (; context_die; context_die = context_die->die_parent)
16795 if (context_die->die_tag == DW_TAG_inlined_subroutine
16796 || context_die->die_tag == DW_TAG_subprogram)
16797 return 1;
16799 return 0;
16802 /* Returns nonzero if CONTEXT_DIE is a class. */
16804 static inline int
16805 class_scope_p (dw_die_ref context_die)
16807 return (context_die
16808 && (context_die->die_tag == DW_TAG_structure_type
16809 || context_die->die_tag == DW_TAG_class_type
16810 || context_die->die_tag == DW_TAG_interface_type
16811 || context_die->die_tag == DW_TAG_union_type));
16814 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16815 whether or not to treat a DIE in this context as a declaration. */
16817 static inline int
16818 class_or_namespace_scope_p (dw_die_ref context_die)
16820 return (class_scope_p (context_die)
16821 || (context_die && context_die->die_tag == DW_TAG_namespace));
16824 /* Many forms of DIEs require a "type description" attribute. This
16825 routine locates the proper "type descriptor" die for the type given
16826 by 'type', and adds a DW_AT_type attribute below the given die. */
16828 static void
16829 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16830 int decl_volatile, dw_die_ref context_die)
16832 enum tree_code code = TREE_CODE (type);
16833 dw_die_ref type_die = NULL;
16835 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16836 or fixed-point type, use the inner type. This is because we have no
16837 support for unnamed types in base_type_die. This can happen if this is
16838 an Ada subrange type. Correct solution is emit a subrange type die. */
16839 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16840 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16841 type = TREE_TYPE (type), code = TREE_CODE (type);
16843 if (code == ERROR_MARK
16844 /* Handle a special case. For functions whose return type is void, we
16845 generate *no* type attribute. (Note that no object may have type
16846 `void', so this only applies to function return types). */
16847 || code == VOID_TYPE)
16848 return;
16850 type_die = modified_type_die (type,
16851 decl_const || TYPE_READONLY (type),
16852 decl_volatile || TYPE_VOLATILE (type),
16853 context_die);
16855 if (type_die != NULL)
16856 add_AT_die_ref (object_die, DW_AT_type, type_die);
16859 /* Given an object die, add the calling convention attribute for the
16860 function call type. */
16861 static void
16862 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16864 enum dwarf_calling_convention value = DW_CC_normal;
16866 value = ((enum dwarf_calling_convention)
16867 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16869 if (is_fortran ()
16870 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16872 /* DWARF 2 doesn't provide a way to identify a program's source-level
16873 entry point. DW_AT_calling_convention attributes are only meant
16874 to describe functions' calling conventions. However, lacking a
16875 better way to signal the Fortran main program, we used this for
16876 a long time, following existing custom. Now, DWARF 4 has
16877 DW_AT_main_subprogram, which we add below, but some tools still
16878 rely on the old way, which we thus keep. */
16879 value = DW_CC_program;
16881 if (dwarf_version >= 4 || !dwarf_strict)
16882 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16885 /* Only add the attribute if the backend requests it, and
16886 is not DW_CC_normal. */
16887 if (value && (value != DW_CC_normal))
16888 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16891 /* Given a tree pointer to a struct, class, union, or enum type node, return
16892 a pointer to the (string) tag name for the given type, or zero if the type
16893 was declared without a tag. */
16895 static const char *
16896 type_tag (const_tree type)
16898 const char *name = 0;
16900 if (TYPE_NAME (type) != 0)
16902 tree t = 0;
16904 /* Find the IDENTIFIER_NODE for the type name. */
16905 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16906 && !TYPE_NAMELESS (type))
16907 t = TYPE_NAME (type);
16909 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16910 a TYPE_DECL node, regardless of whether or not a `typedef' was
16911 involved. */
16912 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16913 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16915 /* We want to be extra verbose. Don't call dwarf_name if
16916 DECL_NAME isn't set. The default hook for decl_printable_name
16917 doesn't like that, and in this context it's correct to return
16918 0, instead of "<anonymous>" or the like. */
16919 if (DECL_NAME (TYPE_NAME (type))
16920 && !DECL_NAMELESS (TYPE_NAME (type)))
16921 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16924 /* Now get the name as a string, or invent one. */
16925 if (!name && t != 0)
16926 name = IDENTIFIER_POINTER (t);
16929 return (name == 0 || *name == '\0') ? 0 : name;
16932 /* Return the type associated with a data member, make a special check
16933 for bit field types. */
16935 static inline tree
16936 member_declared_type (const_tree member)
16938 return (DECL_BIT_FIELD_TYPE (member)
16939 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16942 /* Get the decl's label, as described by its RTL. This may be different
16943 from the DECL_NAME name used in the source file. */
16945 #if 0
16946 static const char *
16947 decl_start_label (tree decl)
16949 rtx x;
16950 const char *fnname;
16952 x = DECL_RTL (decl);
16953 gcc_assert (MEM_P (x));
16955 x = XEXP (x, 0);
16956 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16958 fnname = XSTR (x, 0);
16959 return fnname;
16961 #endif
16963 /* These routines generate the internal representation of the DIE's for
16964 the compilation unit. Debugging information is collected by walking
16965 the declaration trees passed in from dwarf2out_decl(). */
16967 static void
16968 gen_array_type_die (tree type, dw_die_ref context_die)
16970 dw_die_ref scope_die = scope_die_for (type, context_die);
16971 dw_die_ref array_die;
16973 /* GNU compilers represent multidimensional array types as sequences of one
16974 dimensional array types whose element types are themselves array types.
16975 We sometimes squish that down to a single array_type DIE with multiple
16976 subscripts in the Dwarf debugging info. The draft Dwarf specification
16977 say that we are allowed to do this kind of compression in C, because
16978 there is no difference between an array of arrays and a multidimensional
16979 array. We don't do this for Ada to remain as close as possible to the
16980 actual representation, which is especially important against the language
16981 flexibilty wrt arrays of variable size. */
16983 bool collapse_nested_arrays = !is_ada ();
16984 tree element_type;
16986 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16987 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16988 if (TYPE_STRING_FLAG (type)
16989 && TREE_CODE (type) == ARRAY_TYPE
16990 && is_fortran ()
16991 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16993 HOST_WIDE_INT size;
16995 array_die = new_die (DW_TAG_string_type, scope_die, type);
16996 add_name_attribute (array_die, type_tag (type));
16997 equate_type_number_to_die (type, array_die);
16998 size = int_size_in_bytes (type);
16999 if (size >= 0)
17000 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17001 else if (TYPE_DOMAIN (type) != NULL_TREE
17002 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17003 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17005 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17006 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17008 size = int_size_in_bytes (TREE_TYPE (szdecl));
17009 if (loc && size > 0)
17011 add_AT_location_description (array_die, DW_AT_string_length, loc);
17012 if (size != DWARF2_ADDR_SIZE)
17013 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17016 return;
17019 array_die = new_die (DW_TAG_array_type, scope_die, type);
17020 add_name_attribute (array_die, type_tag (type));
17021 equate_type_number_to_die (type, array_die);
17023 if (TREE_CODE (type) == VECTOR_TYPE)
17024 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17026 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17027 if (is_fortran ()
17028 && TREE_CODE (type) == ARRAY_TYPE
17029 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17030 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17031 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17033 #if 0
17034 /* We default the array ordering. SDB will probably do
17035 the right things even if DW_AT_ordering is not present. It's not even
17036 an issue until we start to get into multidimensional arrays anyway. If
17037 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17038 then we'll have to put the DW_AT_ordering attribute back in. (But if
17039 and when we find out that we need to put these in, we will only do so
17040 for multidimensional arrays. */
17041 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17042 #endif
17044 if (TREE_CODE (type) == VECTOR_TYPE)
17046 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17047 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17048 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17049 add_bound_info (subrange_die, DW_AT_upper_bound,
17050 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17052 else
17053 add_subscript_info (array_die, type, collapse_nested_arrays);
17055 /* Add representation of the type of the elements of this array type and
17056 emit the corresponding DIE if we haven't done it already. */
17057 element_type = TREE_TYPE (type);
17058 if (collapse_nested_arrays)
17059 while (TREE_CODE (element_type) == ARRAY_TYPE)
17061 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17062 break;
17063 element_type = TREE_TYPE (element_type);
17066 add_type_attribute (array_die, element_type, 0, 0, context_die);
17068 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17069 if (TYPE_ARTIFICIAL (type))
17070 add_AT_flag (array_die, DW_AT_artificial, 1);
17072 if (get_AT (array_die, DW_AT_name))
17073 add_pubtype (type, array_die);
17076 static dw_loc_descr_ref
17077 descr_info_loc (tree val, tree base_decl)
17079 HOST_WIDE_INT size;
17080 dw_loc_descr_ref loc, loc2;
17081 enum dwarf_location_atom op;
17083 if (val == base_decl)
17084 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17086 switch (TREE_CODE (val))
17088 CASE_CONVERT:
17089 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17090 case VAR_DECL:
17091 return loc_descriptor_from_tree (val, 0);
17092 case INTEGER_CST:
17093 if (tree_fits_shwi_p (val))
17094 return int_loc_descriptor (tree_to_shwi (val));
17095 break;
17096 case INDIRECT_REF:
17097 size = int_size_in_bytes (TREE_TYPE (val));
17098 if (size < 0)
17099 break;
17100 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17101 if (!loc)
17102 break;
17103 if (size == DWARF2_ADDR_SIZE)
17104 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17105 else
17106 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17107 return loc;
17108 case POINTER_PLUS_EXPR:
17109 case PLUS_EXPR:
17110 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17111 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17113 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17114 if (!loc)
17115 break;
17116 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17118 else
17120 op = DW_OP_plus;
17121 do_binop:
17122 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17123 if (!loc)
17124 break;
17125 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17126 if (!loc2)
17127 break;
17128 add_loc_descr (&loc, loc2);
17129 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17131 return loc;
17132 case MINUS_EXPR:
17133 op = DW_OP_minus;
17134 goto do_binop;
17135 case MULT_EXPR:
17136 op = DW_OP_mul;
17137 goto do_binop;
17138 case EQ_EXPR:
17139 op = DW_OP_eq;
17140 goto do_binop;
17141 case NE_EXPR:
17142 op = DW_OP_ne;
17143 goto do_binop;
17144 default:
17145 break;
17147 return NULL;
17150 static void
17151 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17152 tree val, tree base_decl)
17154 dw_loc_descr_ref loc;
17156 if (tree_fits_shwi_p (val))
17158 add_AT_unsigned (die, attr, tree_to_shwi (val));
17159 return;
17162 loc = descr_info_loc (val, base_decl);
17163 if (!loc)
17164 return;
17166 add_AT_loc (die, attr, loc);
17169 /* This routine generates DIE for array with hidden descriptor, details
17170 are filled into *info by a langhook. */
17172 static void
17173 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17174 dw_die_ref context_die)
17176 dw_die_ref scope_die = scope_die_for (type, context_die);
17177 dw_die_ref array_die;
17178 int dim;
17180 array_die = new_die (DW_TAG_array_type, scope_die, type);
17181 add_name_attribute (array_die, type_tag (type));
17182 equate_type_number_to_die (type, array_die);
17184 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17185 if (is_fortran ()
17186 && info->ndimensions >= 2)
17187 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17189 if (info->data_location)
17190 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17191 info->base_decl);
17192 if (info->associated)
17193 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17194 info->base_decl);
17195 if (info->allocated)
17196 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17197 info->base_decl);
17199 for (dim = 0; dim < info->ndimensions; dim++)
17201 dw_die_ref subrange_die
17202 = new_die (DW_TAG_subrange_type, array_die, NULL);
17204 if (info->dimen[dim].lower_bound)
17206 /* If it is the default value, omit it. */
17207 int dflt;
17209 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17210 && (dflt = lower_bound_default ()) != -1
17211 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17213 else
17214 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17215 info->dimen[dim].lower_bound,
17216 info->base_decl);
17218 if (info->dimen[dim].upper_bound)
17219 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17220 info->dimen[dim].upper_bound,
17221 info->base_decl);
17222 if (info->dimen[dim].stride)
17223 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17224 info->dimen[dim].stride,
17225 info->base_decl);
17228 gen_type_die (info->element_type, context_die);
17229 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17231 if (get_AT (array_die, DW_AT_name))
17232 add_pubtype (type, array_die);
17235 #if 0
17236 static void
17237 gen_entry_point_die (tree decl, dw_die_ref context_die)
17239 tree origin = decl_ultimate_origin (decl);
17240 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17242 if (origin != NULL)
17243 add_abstract_origin_attribute (decl_die, origin);
17244 else
17246 add_name_and_src_coords_attributes (decl_die, decl);
17247 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17248 0, 0, context_die);
17251 if (DECL_ABSTRACT (decl))
17252 equate_decl_number_to_die (decl, decl_die);
17253 else
17254 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17256 #endif
17258 /* Walk through the list of incomplete types again, trying once more to
17259 emit full debugging info for them. */
17261 static void
17262 retry_incomplete_types (void)
17264 int i;
17266 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17267 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17268 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17271 /* Determine what tag to use for a record type. */
17273 static enum dwarf_tag
17274 record_type_tag (tree type)
17276 if (! lang_hooks.types.classify_record)
17277 return DW_TAG_structure_type;
17279 switch (lang_hooks.types.classify_record (type))
17281 case RECORD_IS_STRUCT:
17282 return DW_TAG_structure_type;
17284 case RECORD_IS_CLASS:
17285 return DW_TAG_class_type;
17287 case RECORD_IS_INTERFACE:
17288 if (dwarf_version >= 3 || !dwarf_strict)
17289 return DW_TAG_interface_type;
17290 return DW_TAG_structure_type;
17292 default:
17293 gcc_unreachable ();
17297 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17298 include all of the information about the enumeration values also. Each
17299 enumerated type name/value is listed as a child of the enumerated type
17300 DIE. */
17302 static dw_die_ref
17303 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17305 dw_die_ref type_die = lookup_type_die (type);
17307 if (type_die == NULL)
17309 type_die = new_die (DW_TAG_enumeration_type,
17310 scope_die_for (type, context_die), type);
17311 equate_type_number_to_die (type, type_die);
17312 add_name_attribute (type_die, type_tag (type));
17313 if (dwarf_version >= 4 || !dwarf_strict)
17315 if (ENUM_IS_SCOPED (type))
17316 add_AT_flag (type_die, DW_AT_enum_class, 1);
17317 if (ENUM_IS_OPAQUE (type))
17318 add_AT_flag (type_die, DW_AT_declaration, 1);
17321 else if (! TYPE_SIZE (type))
17322 return type_die;
17323 else
17324 remove_AT (type_die, DW_AT_declaration);
17326 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17327 given enum type is incomplete, do not generate the DW_AT_byte_size
17328 attribute or the DW_AT_element_list attribute. */
17329 if (TYPE_SIZE (type))
17331 tree link;
17333 TREE_ASM_WRITTEN (type) = 1;
17334 add_byte_size_attribute (type_die, type);
17335 if (TYPE_STUB_DECL (type) != NULL_TREE)
17337 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17338 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17341 /* If the first reference to this type was as the return type of an
17342 inline function, then it may not have a parent. Fix this now. */
17343 if (type_die->die_parent == NULL)
17344 add_child_die (scope_die_for (type, context_die), type_die);
17346 for (link = TYPE_VALUES (type);
17347 link != NULL; link = TREE_CHAIN (link))
17349 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17350 tree value = TREE_VALUE (link);
17352 add_name_attribute (enum_die,
17353 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17355 if (TREE_CODE (value) == CONST_DECL)
17356 value = DECL_INITIAL (value);
17358 if (simple_type_size_in_bits (TREE_TYPE (value))
17359 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17360 /* DWARF2 does not provide a way of indicating whether or
17361 not enumeration constants are signed or unsigned. GDB
17362 always assumes the values are signed, so we output all
17363 values as if they were signed. That means that
17364 enumeration constants with very large unsigned values
17365 will appear to have negative values in the debugger.
17367 TODO: the above comment is wrong, DWARF2 does provide
17368 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17369 This should be re-worked to use correct signed/unsigned
17370 int/double tags for all cases, instead of always treating as
17371 signed. */
17372 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17373 else
17374 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17375 that here. */
17376 add_AT_double (enum_die, DW_AT_const_value,
17377 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17380 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17381 if (TYPE_ARTIFICIAL (type))
17382 add_AT_flag (type_die, DW_AT_artificial, 1);
17384 else
17385 add_AT_flag (type_die, DW_AT_declaration, 1);
17387 add_pubtype (type, type_die);
17389 return type_die;
17392 /* Generate a DIE to represent either a real live formal parameter decl or to
17393 represent just the type of some formal parameter position in some function
17394 type.
17396 Note that this routine is a bit unusual because its argument may be a
17397 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17398 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17399 node. If it's the former then this function is being called to output a
17400 DIE to represent a formal parameter object (or some inlining thereof). If
17401 it's the latter, then this function is only being called to output a
17402 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17403 argument type of some subprogram type.
17404 If EMIT_NAME_P is true, name and source coordinate attributes
17405 are emitted. */
17407 static dw_die_ref
17408 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17409 dw_die_ref context_die)
17411 tree node_or_origin = node ? node : origin;
17412 tree ultimate_origin;
17413 dw_die_ref parm_die
17414 = new_die (DW_TAG_formal_parameter, context_die, node);
17416 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17418 case tcc_declaration:
17419 ultimate_origin = decl_ultimate_origin (node_or_origin);
17420 if (node || ultimate_origin)
17421 origin = ultimate_origin;
17422 if (origin != NULL)
17423 add_abstract_origin_attribute (parm_die, origin);
17424 else if (emit_name_p)
17425 add_name_and_src_coords_attributes (parm_die, node);
17426 if (origin == NULL
17427 || (! DECL_ABSTRACT (node_or_origin)
17428 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17429 decl_function_context
17430 (node_or_origin))))
17432 tree type = TREE_TYPE (node_or_origin);
17433 if (decl_by_reference_p (node_or_origin))
17434 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17435 context_die);
17436 else
17437 add_type_attribute (parm_die, type,
17438 TREE_READONLY (node_or_origin),
17439 TREE_THIS_VOLATILE (node_or_origin),
17440 context_die);
17442 if (origin == NULL && DECL_ARTIFICIAL (node))
17443 add_AT_flag (parm_die, DW_AT_artificial, 1);
17445 if (node && node != origin)
17446 equate_decl_number_to_die (node, parm_die);
17447 if (! DECL_ABSTRACT (node_or_origin))
17448 add_location_or_const_value_attribute (parm_die, node_or_origin,
17449 node == NULL, DW_AT_location);
17451 break;
17453 case tcc_type:
17454 /* We were called with some kind of a ..._TYPE node. */
17455 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17456 break;
17458 default:
17459 gcc_unreachable ();
17462 return parm_die;
17465 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17466 children DW_TAG_formal_parameter DIEs representing the arguments of the
17467 parameter pack.
17469 PARM_PACK must be a function parameter pack.
17470 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17471 must point to the subsequent arguments of the function PACK_ARG belongs to.
17472 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17473 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17474 following the last one for which a DIE was generated. */
17476 static dw_die_ref
17477 gen_formal_parameter_pack_die (tree parm_pack,
17478 tree pack_arg,
17479 dw_die_ref subr_die,
17480 tree *next_arg)
17482 tree arg;
17483 dw_die_ref parm_pack_die;
17485 gcc_assert (parm_pack
17486 && lang_hooks.function_parameter_pack_p (parm_pack)
17487 && subr_die);
17489 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17490 add_src_coords_attributes (parm_pack_die, parm_pack);
17492 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17494 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17495 parm_pack))
17496 break;
17497 gen_formal_parameter_die (arg, NULL,
17498 false /* Don't emit name attribute. */,
17499 parm_pack_die);
17501 if (next_arg)
17502 *next_arg = arg;
17503 return parm_pack_die;
17506 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17507 at the end of an (ANSI prototyped) formal parameters list. */
17509 static void
17510 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17512 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17515 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17516 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17517 parameters as specified in some function type specification (except for
17518 those which appear as part of a function *definition*). */
17520 static void
17521 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17523 tree link;
17524 tree formal_type = NULL;
17525 tree first_parm_type;
17526 tree arg;
17528 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17530 arg = DECL_ARGUMENTS (function_or_method_type);
17531 function_or_method_type = TREE_TYPE (function_or_method_type);
17533 else
17534 arg = NULL_TREE;
17536 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17538 /* Make our first pass over the list of formal parameter types and output a
17539 DW_TAG_formal_parameter DIE for each one. */
17540 for (link = first_parm_type; link; )
17542 dw_die_ref parm_die;
17544 formal_type = TREE_VALUE (link);
17545 if (formal_type == void_type_node)
17546 break;
17548 /* Output a (nameless) DIE to represent the formal parameter itself. */
17549 parm_die = gen_formal_parameter_die (formal_type, NULL,
17550 true /* Emit name attribute. */,
17551 context_die);
17552 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17553 && link == first_parm_type)
17555 add_AT_flag (parm_die, DW_AT_artificial, 1);
17556 if (dwarf_version >= 3 || !dwarf_strict)
17557 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17559 else if (arg && DECL_ARTIFICIAL (arg))
17560 add_AT_flag (parm_die, DW_AT_artificial, 1);
17562 link = TREE_CHAIN (link);
17563 if (arg)
17564 arg = DECL_CHAIN (arg);
17567 /* If this function type has an ellipsis, add a
17568 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17569 if (formal_type != void_type_node)
17570 gen_unspecified_parameters_die (function_or_method_type, context_die);
17572 /* Make our second (and final) pass over the list of formal parameter types
17573 and output DIEs to represent those types (as necessary). */
17574 for (link = TYPE_ARG_TYPES (function_or_method_type);
17575 link && TREE_VALUE (link);
17576 link = TREE_CHAIN (link))
17577 gen_type_die (TREE_VALUE (link), context_die);
17580 /* We want to generate the DIE for TYPE so that we can generate the
17581 die for MEMBER, which has been defined; we will need to refer back
17582 to the member declaration nested within TYPE. If we're trying to
17583 generate minimal debug info for TYPE, processing TYPE won't do the
17584 trick; we need to attach the member declaration by hand. */
17586 static void
17587 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17589 gen_type_die (type, context_die);
17591 /* If we're trying to avoid duplicate debug info, we may not have
17592 emitted the member decl for this function. Emit it now. */
17593 if (TYPE_STUB_DECL (type)
17594 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17595 && ! lookup_decl_die (member))
17597 dw_die_ref type_die;
17598 gcc_assert (!decl_ultimate_origin (member));
17600 push_decl_scope (type);
17601 type_die = lookup_type_die_strip_naming_typedef (type);
17602 if (TREE_CODE (member) == FUNCTION_DECL)
17603 gen_subprogram_die (member, type_die);
17604 else if (TREE_CODE (member) == FIELD_DECL)
17606 /* Ignore the nameless fields that are used to skip bits but handle
17607 C++ anonymous unions and structs. */
17608 if (DECL_NAME (member) != NULL_TREE
17609 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17610 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17612 gen_type_die (member_declared_type (member), type_die);
17613 gen_field_die (member, type_die);
17616 else
17617 gen_variable_die (member, NULL_TREE, type_die);
17619 pop_decl_scope ();
17623 /* Forward declare these functions, because they are mutually recursive
17624 with their set_block_* pairing functions. */
17625 static void set_decl_origin_self (tree);
17626 static void set_decl_abstract_flags (tree, int);
17628 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17629 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17630 that it points to the node itself, thus indicating that the node is its
17631 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17632 the given node is NULL, recursively descend the decl/block tree which
17633 it is the root of, and for each other ..._DECL or BLOCK node contained
17634 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17635 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17636 values to point to themselves. */
17638 static void
17639 set_block_origin_self (tree stmt)
17641 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17643 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17646 tree local_decl;
17648 for (local_decl = BLOCK_VARS (stmt);
17649 local_decl != NULL_TREE;
17650 local_decl = DECL_CHAIN (local_decl))
17651 if (! DECL_EXTERNAL (local_decl))
17652 set_decl_origin_self (local_decl); /* Potential recursion. */
17656 tree subblock;
17658 for (subblock = BLOCK_SUBBLOCKS (stmt);
17659 subblock != NULL_TREE;
17660 subblock = BLOCK_CHAIN (subblock))
17661 set_block_origin_self (subblock); /* Recurse. */
17666 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17667 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17668 node to so that it points to the node itself, thus indicating that the
17669 node represents its own (abstract) origin. Additionally, if the
17670 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17671 the decl/block tree of which the given node is the root of, and for
17672 each other ..._DECL or BLOCK node contained therein whose
17673 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17674 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17675 point to themselves. */
17677 static void
17678 set_decl_origin_self (tree decl)
17680 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17682 DECL_ABSTRACT_ORIGIN (decl) = decl;
17683 if (TREE_CODE (decl) == FUNCTION_DECL)
17685 tree arg;
17687 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17688 DECL_ABSTRACT_ORIGIN (arg) = arg;
17689 if (DECL_INITIAL (decl) != NULL_TREE
17690 && DECL_INITIAL (decl) != error_mark_node)
17691 set_block_origin_self (DECL_INITIAL (decl));
17696 /* Given a pointer to some BLOCK node, and a boolean value to set the
17697 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17698 the given block, and for all local decls and all local sub-blocks
17699 (recursively) which are contained therein. */
17701 static void
17702 set_block_abstract_flags (tree stmt, int setting)
17704 tree local_decl;
17705 tree subblock;
17706 unsigned int i;
17708 BLOCK_ABSTRACT (stmt) = setting;
17710 for (local_decl = BLOCK_VARS (stmt);
17711 local_decl != NULL_TREE;
17712 local_decl = DECL_CHAIN (local_decl))
17713 if (! DECL_EXTERNAL (local_decl))
17714 set_decl_abstract_flags (local_decl, setting);
17716 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17718 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17719 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17720 || TREE_CODE (local_decl) == PARM_DECL)
17721 set_decl_abstract_flags (local_decl, setting);
17724 for (subblock = BLOCK_SUBBLOCKS (stmt);
17725 subblock != NULL_TREE;
17726 subblock = BLOCK_CHAIN (subblock))
17727 set_block_abstract_flags (subblock, setting);
17730 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17731 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17732 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17733 set the abstract flags for all of the parameters, local vars, local
17734 blocks and sub-blocks (recursively) to the same setting. */
17736 static void
17737 set_decl_abstract_flags (tree decl, int setting)
17739 DECL_ABSTRACT (decl) = setting;
17740 if (TREE_CODE (decl) == FUNCTION_DECL)
17742 tree arg;
17744 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17745 DECL_ABSTRACT (arg) = setting;
17746 if (DECL_INITIAL (decl) != NULL_TREE
17747 && DECL_INITIAL (decl) != error_mark_node)
17748 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17752 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17753 may later generate inlined and/or out-of-line instances of. */
17755 static void
17756 dwarf2out_abstract_function (tree decl)
17758 dw_die_ref old_die;
17759 tree save_fn;
17760 tree context;
17761 int was_abstract;
17762 htab_t old_decl_loc_table;
17763 htab_t old_cached_dw_loc_list_table;
17764 int old_call_site_count, old_tail_call_site_count;
17765 struct call_arg_loc_node *old_call_arg_locations;
17767 /* Make sure we have the actual abstract inline, not a clone. */
17768 decl = DECL_ORIGIN (decl);
17770 old_die = lookup_decl_die (decl);
17771 if (old_die && get_AT (old_die, DW_AT_inline))
17772 /* We've already generated the abstract instance. */
17773 return;
17775 /* We can be called while recursively when seeing block defining inlined subroutine
17776 DIE. Be sure to not clobber the outer location table nor use it or we would
17777 get locations in abstract instantces. */
17778 old_decl_loc_table = decl_loc_table;
17779 decl_loc_table = NULL;
17780 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17781 cached_dw_loc_list_table = NULL;
17782 old_call_arg_locations = call_arg_locations;
17783 call_arg_locations = NULL;
17784 old_call_site_count = call_site_count;
17785 call_site_count = -1;
17786 old_tail_call_site_count = tail_call_site_count;
17787 tail_call_site_count = -1;
17789 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17790 we don't get confused by DECL_ABSTRACT. */
17791 if (debug_info_level > DINFO_LEVEL_TERSE)
17793 context = decl_class_context (decl);
17794 if (context)
17795 gen_type_die_for_member
17796 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17799 /* Pretend we've just finished compiling this function. */
17800 save_fn = current_function_decl;
17801 current_function_decl = decl;
17803 was_abstract = DECL_ABSTRACT (decl);
17804 set_decl_abstract_flags (decl, 1);
17805 dwarf2out_decl (decl);
17806 if (! was_abstract)
17807 set_decl_abstract_flags (decl, 0);
17809 current_function_decl = save_fn;
17810 decl_loc_table = old_decl_loc_table;
17811 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17812 call_arg_locations = old_call_arg_locations;
17813 call_site_count = old_call_site_count;
17814 tail_call_site_count = old_tail_call_site_count;
17817 /* Helper function of premark_used_types() which gets called through
17818 htab_traverse.
17820 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17821 marked as unused by prune_unused_types. */
17823 static int
17824 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17826 tree type;
17827 dw_die_ref die;
17829 type = (tree) *slot;
17830 die = lookup_type_die (type);
17831 if (die != NULL)
17832 die->die_perennial_p = 1;
17833 return 1;
17836 /* Helper function of premark_types_used_by_global_vars which gets called
17837 through htab_traverse.
17839 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17840 marked as unused by prune_unused_types. The DIE of the type is marked
17841 only if the global variable using the type will actually be emitted. */
17843 static int
17844 premark_types_used_by_global_vars_helper (void **slot,
17845 void *data ATTRIBUTE_UNUSED)
17847 struct types_used_by_vars_entry *entry;
17848 dw_die_ref die;
17850 entry = (struct types_used_by_vars_entry *) *slot;
17851 gcc_assert (entry->type != NULL
17852 && entry->var_decl != NULL);
17853 die = lookup_type_die (entry->type);
17854 if (die)
17856 /* Ask cgraph if the global variable really is to be emitted.
17857 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17858 varpool_node *node = varpool_get_node (entry->var_decl);
17859 if (node && node->definition)
17861 die->die_perennial_p = 1;
17862 /* Keep the parent DIEs as well. */
17863 while ((die = die->die_parent) && die->die_perennial_p == 0)
17864 die->die_perennial_p = 1;
17867 return 1;
17870 /* Mark all members of used_types_hash as perennial. */
17872 static void
17873 premark_used_types (struct function *fun)
17875 if (fun && fun->used_types_hash)
17876 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17879 /* Mark all members of types_used_by_vars_entry as perennial. */
17881 static void
17882 premark_types_used_by_global_vars (void)
17884 if (types_used_by_vars_hash)
17885 htab_traverse (types_used_by_vars_hash,
17886 premark_types_used_by_global_vars_helper, NULL);
17889 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17890 for CA_LOC call arg loc node. */
17892 static dw_die_ref
17893 gen_call_site_die (tree decl, dw_die_ref subr_die,
17894 struct call_arg_loc_node *ca_loc)
17896 dw_die_ref stmt_die = NULL, die;
17897 tree block = ca_loc->block;
17899 while (block
17900 && block != DECL_INITIAL (decl)
17901 && TREE_CODE (block) == BLOCK)
17903 if (block_map.length () > BLOCK_NUMBER (block))
17904 stmt_die = block_map[BLOCK_NUMBER (block)];
17905 if (stmt_die)
17906 break;
17907 block = BLOCK_SUPERCONTEXT (block);
17909 if (stmt_die == NULL)
17910 stmt_die = subr_die;
17911 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17912 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17913 if (ca_loc->tail_call_p)
17914 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17915 if (ca_loc->symbol_ref)
17917 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17918 if (tdie)
17919 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17920 else
17921 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17923 return die;
17926 /* Generate a DIE to represent a declared function (either file-scope or
17927 block-local). */
17929 static void
17930 gen_subprogram_die (tree decl, dw_die_ref context_die)
17932 tree origin = decl_ultimate_origin (decl);
17933 dw_die_ref subr_die;
17934 tree outer_scope;
17935 dw_die_ref old_die = lookup_decl_die (decl);
17936 int declaration = (current_function_decl != decl
17937 || class_or_namespace_scope_p (context_die));
17939 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17941 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17942 started to generate the abstract instance of an inline, decided to output
17943 its containing class, and proceeded to emit the declaration of the inline
17944 from the member list for the class. If so, DECLARATION takes priority;
17945 we'll get back to the abstract instance when done with the class. */
17947 /* The class-scope declaration DIE must be the primary DIE. */
17948 if (origin && declaration && class_or_namespace_scope_p (context_die))
17950 origin = NULL;
17951 gcc_assert (!old_die);
17954 /* Now that the C++ front end lazily declares artificial member fns, we
17955 might need to retrofit the declaration into its class. */
17956 if (!declaration && !origin && !old_die
17957 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17958 && !class_or_namespace_scope_p (context_die)
17959 && debug_info_level > DINFO_LEVEL_TERSE)
17960 old_die = force_decl_die (decl);
17962 if (origin != NULL)
17964 gcc_assert (!declaration || local_scope_p (context_die));
17966 /* Fixup die_parent for the abstract instance of a nested
17967 inline function. */
17968 if (old_die && old_die->die_parent == NULL)
17969 add_child_die (context_die, old_die);
17971 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17972 add_abstract_origin_attribute (subr_die, origin);
17973 /* This is where the actual code for a cloned function is.
17974 Let's emit linkage name attribute for it. This helps
17975 debuggers to e.g, set breakpoints into
17976 constructors/destructors when the user asks "break
17977 K::K". */
17978 add_linkage_name (subr_die, decl);
17980 else if (old_die)
17982 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17983 struct dwarf_file_data * file_index = lookup_filename (s.file);
17985 if (!get_AT_flag (old_die, DW_AT_declaration)
17986 /* We can have a normal definition following an inline one in the
17987 case of redefinition of GNU C extern inlines.
17988 It seems reasonable to use AT_specification in this case. */
17989 && !get_AT (old_die, DW_AT_inline))
17991 /* Detect and ignore this case, where we are trying to output
17992 something we have already output. */
17993 return;
17996 /* If the definition comes from the same place as the declaration,
17997 maybe use the old DIE. We always want the DIE for this function
17998 that has the *_pc attributes to be under comp_unit_die so the
17999 debugger can find it. We also need to do this for abstract
18000 instances of inlines, since the spec requires the out-of-line copy
18001 to have the same parent. For local class methods, this doesn't
18002 apply; we just use the old DIE. */
18003 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18004 && (DECL_ARTIFICIAL (decl)
18005 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18006 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18007 == (unsigned) s.line))))
18009 subr_die = old_die;
18011 /* Clear out the declaration attribute and the formal parameters.
18012 Do not remove all children, because it is possible that this
18013 declaration die was forced using force_decl_die(). In such
18014 cases die that forced declaration die (e.g. TAG_imported_module)
18015 is one of the children that we do not want to remove. */
18016 remove_AT (subr_die, DW_AT_declaration);
18017 remove_AT (subr_die, DW_AT_object_pointer);
18018 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18020 else
18022 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18023 add_AT_specification (subr_die, old_die);
18024 add_pubname (decl, subr_die);
18025 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18026 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18027 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18028 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18030 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18031 emit the real type on the definition die. */
18032 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18034 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18035 if (die == auto_die || die == decltype_auto_die)
18036 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18037 0, 0, context_die);
18041 else
18043 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18045 if (TREE_PUBLIC (decl))
18046 add_AT_flag (subr_die, DW_AT_external, 1);
18048 add_name_and_src_coords_attributes (subr_die, decl);
18049 add_pubname (decl, subr_die);
18050 if (debug_info_level > DINFO_LEVEL_TERSE)
18052 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18053 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18054 0, 0, context_die);
18057 add_pure_or_virtual_attribute (subr_die, decl);
18058 if (DECL_ARTIFICIAL (decl))
18059 add_AT_flag (subr_die, DW_AT_artificial, 1);
18061 add_accessibility_attribute (subr_die, decl);
18064 if (declaration)
18066 if (!old_die || !get_AT (old_die, DW_AT_inline))
18068 add_AT_flag (subr_die, DW_AT_declaration, 1);
18070 /* If this is an explicit function declaration then generate
18071 a DW_AT_explicit attribute. */
18072 if (lang_hooks.decls.function_decl_explicit_p (decl)
18073 && (dwarf_version >= 3 || !dwarf_strict))
18074 add_AT_flag (subr_die, DW_AT_explicit, 1);
18076 /* The first time we see a member function, it is in the context of
18077 the class to which it belongs. We make sure of this by emitting
18078 the class first. The next time is the definition, which is
18079 handled above. The two may come from the same source text.
18081 Note that force_decl_die() forces function declaration die. It is
18082 later reused to represent definition. */
18083 equate_decl_number_to_die (decl, subr_die);
18086 else if (DECL_ABSTRACT (decl))
18088 if (DECL_DECLARED_INLINE_P (decl))
18090 if (cgraph_function_possibly_inlined_p (decl))
18091 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18092 else
18093 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18095 else
18097 if (cgraph_function_possibly_inlined_p (decl))
18098 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18099 else
18100 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18103 if (DECL_DECLARED_INLINE_P (decl)
18104 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18105 add_AT_flag (subr_die, DW_AT_artificial, 1);
18107 equate_decl_number_to_die (decl, subr_die);
18109 else if (!DECL_EXTERNAL (decl))
18111 HOST_WIDE_INT cfa_fb_offset;
18112 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18114 if (!old_die || !get_AT (old_die, DW_AT_inline))
18115 equate_decl_number_to_die (decl, subr_die);
18117 gcc_checking_assert (fun);
18118 if (!flag_reorder_blocks_and_partition)
18120 dw_fde_ref fde = fun->fde;
18121 if (fde->dw_fde_begin)
18123 /* We have already generated the labels. */
18124 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18125 fde->dw_fde_end, false);
18127 else
18129 /* Create start/end labels and add the range. */
18130 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18131 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18132 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18133 FUNC_LABEL_ID (cfun));
18134 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18135 FUNC_LABEL_ID (cfun));
18136 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18137 false);
18140 #if VMS_DEBUGGING_INFO
18141 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18142 Section 2.3 Prologue and Epilogue Attributes:
18143 When a breakpoint is set on entry to a function, it is generally
18144 desirable for execution to be suspended, not on the very first
18145 instruction of the function, but rather at a point after the
18146 function's frame has been set up, after any language defined local
18147 declaration processing has been completed, and before execution of
18148 the first statement of the function begins. Debuggers generally
18149 cannot properly determine where this point is. Similarly for a
18150 breakpoint set on exit from a function. The prologue and epilogue
18151 attributes allow a compiler to communicate the location(s) to use. */
18154 if (fde->dw_fde_vms_end_prologue)
18155 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18156 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18158 if (fde->dw_fde_vms_begin_epilogue)
18159 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18160 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18162 #endif
18165 else
18167 /* Generate pubnames entries for the split function code ranges. */
18168 dw_fde_ref fde = fun->fde;
18170 if (fde->dw_fde_second_begin)
18172 if (dwarf_version >= 3 || !dwarf_strict)
18174 /* We should use ranges for non-contiguous code section
18175 addresses. Use the actual code range for the initial
18176 section, since the HOT/COLD labels might precede an
18177 alignment offset. */
18178 bool range_list_added = false;
18179 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18180 fde->dw_fde_end, &range_list_added,
18181 false);
18182 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18183 fde->dw_fde_second_end,
18184 &range_list_added, false);
18185 if (range_list_added)
18186 add_ranges (NULL);
18188 else
18190 /* There is no real support in DW2 for this .. so we make
18191 a work-around. First, emit the pub name for the segment
18192 containing the function label. Then make and emit a
18193 simplified subprogram DIE for the second segment with the
18194 name pre-fixed by __hot/cold_sect_of_. We use the same
18195 linkage name for the second die so that gdb will find both
18196 sections when given "b foo". */
18197 const char *name = NULL;
18198 tree decl_name = DECL_NAME (decl);
18199 dw_die_ref seg_die;
18201 /* Do the 'primary' section. */
18202 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18203 fde->dw_fde_end, false);
18205 /* Build a minimal DIE for the secondary section. */
18206 seg_die = new_die (DW_TAG_subprogram,
18207 subr_die->die_parent, decl);
18209 if (TREE_PUBLIC (decl))
18210 add_AT_flag (seg_die, DW_AT_external, 1);
18212 if (decl_name != NULL
18213 && IDENTIFIER_POINTER (decl_name) != NULL)
18215 name = dwarf2_name (decl, 1);
18216 if (! DECL_ARTIFICIAL (decl))
18217 add_src_coords_attributes (seg_die, decl);
18219 add_linkage_name (seg_die, decl);
18221 gcc_assert (name != NULL);
18222 add_pure_or_virtual_attribute (seg_die, decl);
18223 if (DECL_ARTIFICIAL (decl))
18224 add_AT_flag (seg_die, DW_AT_artificial, 1);
18226 name = concat ("__second_sect_of_", name, NULL);
18227 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18228 fde->dw_fde_second_end, false);
18229 add_name_attribute (seg_die, name);
18230 if (want_pubnames ())
18231 add_pubname_string (name, seg_die);
18234 else
18235 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18236 false);
18239 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18241 /* We define the "frame base" as the function's CFA. This is more
18242 convenient for several reasons: (1) It's stable across the prologue
18243 and epilogue, which makes it better than just a frame pointer,
18244 (2) With dwarf3, there exists a one-byte encoding that allows us
18245 to reference the .debug_frame data by proxy, but failing that,
18246 (3) We can at least reuse the code inspection and interpretation
18247 code that determines the CFA position at various points in the
18248 function. */
18249 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18251 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18252 add_AT_loc (subr_die, DW_AT_frame_base, op);
18254 else
18256 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18257 if (list->dw_loc_next)
18258 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18259 else
18260 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18263 /* Compute a displacement from the "steady-state frame pointer" to
18264 the CFA. The former is what all stack slots and argument slots
18265 will reference in the rtl; the latter is what we've told the
18266 debugger about. We'll need to adjust all frame_base references
18267 by this displacement. */
18268 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18270 if (fun->static_chain_decl)
18271 add_AT_location_description (subr_die, DW_AT_static_link,
18272 loc_list_from_tree (fun->static_chain_decl, 2));
18275 /* Generate child dies for template paramaters. */
18276 if (debug_info_level > DINFO_LEVEL_TERSE)
18277 gen_generic_params_dies (decl);
18279 /* Now output descriptions of the arguments for this function. This gets
18280 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18281 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18282 `...' at the end of the formal parameter list. In order to find out if
18283 there was a trailing ellipsis or not, we must instead look at the type
18284 associated with the FUNCTION_DECL. This will be a node of type
18285 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18286 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18287 an ellipsis at the end. */
18289 /* In the case where we are describing a mere function declaration, all we
18290 need to do here (and all we *can* do here) is to describe the *types* of
18291 its formal parameters. */
18292 if (debug_info_level <= DINFO_LEVEL_TERSE)
18294 else if (declaration)
18295 gen_formal_types_die (decl, subr_die);
18296 else
18298 /* Generate DIEs to represent all known formal parameters. */
18299 tree parm = DECL_ARGUMENTS (decl);
18300 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18301 tree generic_decl_parm = generic_decl
18302 ? DECL_ARGUMENTS (generic_decl)
18303 : NULL;
18305 /* Now we want to walk the list of parameters of the function and
18306 emit their relevant DIEs.
18308 We consider the case of DECL being an instance of a generic function
18309 as well as it being a normal function.
18311 If DECL is an instance of a generic function we walk the
18312 parameters of the generic function declaration _and_ the parameters of
18313 DECL itself. This is useful because we want to emit specific DIEs for
18314 function parameter packs and those are declared as part of the
18315 generic function declaration. In that particular case,
18316 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18317 That DIE has children DIEs representing the set of arguments
18318 of the pack. Note that the set of pack arguments can be empty.
18319 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18320 children DIE.
18322 Otherwise, we just consider the parameters of DECL. */
18323 while (generic_decl_parm || parm)
18325 if (generic_decl_parm
18326 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18327 gen_formal_parameter_pack_die (generic_decl_parm,
18328 parm, subr_die,
18329 &parm);
18330 else if (parm)
18332 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18334 if (parm == DECL_ARGUMENTS (decl)
18335 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18336 && parm_die
18337 && (dwarf_version >= 3 || !dwarf_strict))
18338 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18340 parm = DECL_CHAIN (parm);
18343 if (generic_decl_parm)
18344 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18347 /* Decide whether we need an unspecified_parameters DIE at the end.
18348 There are 2 more cases to do this for: 1) the ansi ... declaration -
18349 this is detectable when the end of the arg list is not a
18350 void_type_node 2) an unprototyped function declaration (not a
18351 definition). This just means that we have no info about the
18352 parameters at all. */
18353 if (prototype_p (TREE_TYPE (decl)))
18355 /* This is the prototyped case, check for.... */
18356 if (stdarg_p (TREE_TYPE (decl)))
18357 gen_unspecified_parameters_die (decl, subr_die);
18359 else if (DECL_INITIAL (decl) == NULL_TREE)
18360 gen_unspecified_parameters_die (decl, subr_die);
18363 /* Output Dwarf info for all of the stuff within the body of the function
18364 (if it has one - it may be just a declaration). */
18365 outer_scope = DECL_INITIAL (decl);
18367 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18368 a function. This BLOCK actually represents the outermost binding contour
18369 for the function, i.e. the contour in which the function's formal
18370 parameters and labels get declared. Curiously, it appears that the front
18371 end doesn't actually put the PARM_DECL nodes for the current function onto
18372 the BLOCK_VARS list for this outer scope, but are strung off of the
18373 DECL_ARGUMENTS list for the function instead.
18375 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18376 the LABEL_DECL nodes for the function however, and we output DWARF info
18377 for those in decls_for_scope. Just within the `outer_scope' there will be
18378 a BLOCK node representing the function's outermost pair of curly braces,
18379 and any blocks used for the base and member initializers of a C++
18380 constructor function. */
18381 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18383 int call_site_note_count = 0;
18384 int tail_call_site_note_count = 0;
18386 /* Emit a DW_TAG_variable DIE for a named return value. */
18387 if (DECL_NAME (DECL_RESULT (decl)))
18388 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18390 current_function_has_inlines = 0;
18391 decls_for_scope (outer_scope, subr_die, 0);
18393 if (call_arg_locations && !dwarf_strict)
18395 struct call_arg_loc_node *ca_loc;
18396 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18398 dw_die_ref die = NULL;
18399 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18400 rtx arg, next_arg;
18402 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18403 arg; arg = next_arg)
18405 dw_loc_descr_ref reg, val;
18406 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18407 dw_die_ref cdie, tdie = NULL;
18409 next_arg = XEXP (arg, 1);
18410 if (REG_P (XEXP (XEXP (arg, 0), 0))
18411 && next_arg
18412 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18413 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18414 && REGNO (XEXP (XEXP (arg, 0), 0))
18415 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18416 next_arg = XEXP (next_arg, 1);
18417 if (mode == VOIDmode)
18419 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18420 if (mode == VOIDmode)
18421 mode = GET_MODE (XEXP (arg, 0));
18423 if (mode == VOIDmode || mode == BLKmode)
18424 continue;
18425 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18427 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18428 tloc = XEXP (XEXP (arg, 0), 1);
18429 continue;
18431 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18432 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18434 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18435 tlocc = XEXP (XEXP (arg, 0), 1);
18436 continue;
18438 reg = NULL;
18439 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18440 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18441 VAR_INIT_STATUS_INITIALIZED);
18442 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18444 rtx mem = XEXP (XEXP (arg, 0), 0);
18445 reg = mem_loc_descriptor (XEXP (mem, 0),
18446 get_address_mode (mem),
18447 GET_MODE (mem),
18448 VAR_INIT_STATUS_INITIALIZED);
18450 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18451 == DEBUG_PARAMETER_REF)
18453 tree tdecl
18454 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18455 tdie = lookup_decl_die (tdecl);
18456 if (tdie == NULL)
18457 continue;
18459 else
18460 continue;
18461 if (reg == NULL
18462 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18463 != DEBUG_PARAMETER_REF)
18464 continue;
18465 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18466 VOIDmode,
18467 VAR_INIT_STATUS_INITIALIZED);
18468 if (val == NULL)
18469 continue;
18470 if (die == NULL)
18471 die = gen_call_site_die (decl, subr_die, ca_loc);
18472 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18473 NULL_TREE);
18474 if (reg != NULL)
18475 add_AT_loc (cdie, DW_AT_location, reg);
18476 else if (tdie != NULL)
18477 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18478 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18479 if (next_arg != XEXP (arg, 1))
18481 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18482 if (mode == VOIDmode)
18483 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18484 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18485 0), 1),
18486 mode, VOIDmode,
18487 VAR_INIT_STATUS_INITIALIZED);
18488 if (val != NULL)
18489 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18492 if (die == NULL
18493 && (ca_loc->symbol_ref || tloc))
18494 die = gen_call_site_die (decl, subr_die, ca_loc);
18495 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18497 dw_loc_descr_ref tval = NULL;
18499 if (tloc != NULL_RTX)
18500 tval = mem_loc_descriptor (tloc,
18501 GET_MODE (tloc) == VOIDmode
18502 ? Pmode : GET_MODE (tloc),
18503 VOIDmode,
18504 VAR_INIT_STATUS_INITIALIZED);
18505 if (tval)
18506 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18507 else if (tlocc != NULL_RTX)
18509 tval = mem_loc_descriptor (tlocc,
18510 GET_MODE (tlocc) == VOIDmode
18511 ? Pmode : GET_MODE (tlocc),
18512 VOIDmode,
18513 VAR_INIT_STATUS_INITIALIZED);
18514 if (tval)
18515 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18516 tval);
18519 if (die != NULL)
18521 call_site_note_count++;
18522 if (ca_loc->tail_call_p)
18523 tail_call_site_note_count++;
18527 call_arg_locations = NULL;
18528 call_arg_loc_last = NULL;
18529 if (tail_call_site_count >= 0
18530 && tail_call_site_count == tail_call_site_note_count
18531 && !dwarf_strict)
18533 if (call_site_count >= 0
18534 && call_site_count == call_site_note_count)
18535 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18536 else
18537 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18539 call_site_count = -1;
18540 tail_call_site_count = -1;
18543 if (subr_die != old_die)
18544 /* Add the calling convention attribute if requested. */
18545 add_calling_convention_attribute (subr_die, decl);
18548 /* Returns a hash value for X (which really is a die_struct). */
18550 static hashval_t
18551 common_block_die_table_hash (const void *x)
18553 const_dw_die_ref d = (const_dw_die_ref) x;
18554 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18557 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18558 as decl_id and die_parent of die_struct Y. */
18560 static int
18561 common_block_die_table_eq (const void *x, const void *y)
18563 const_dw_die_ref d = (const_dw_die_ref) x;
18564 const_dw_die_ref e = (const_dw_die_ref) y;
18565 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18568 /* Generate a DIE to represent a declared data object.
18569 Either DECL or ORIGIN must be non-null. */
18571 static void
18572 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18574 HOST_WIDE_INT off = 0;
18575 tree com_decl;
18576 tree decl_or_origin = decl ? decl : origin;
18577 tree ultimate_origin;
18578 dw_die_ref var_die;
18579 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18580 dw_die_ref origin_die;
18581 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18582 || class_or_namespace_scope_p (context_die));
18583 bool specialization_p = false;
18585 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18586 if (decl || ultimate_origin)
18587 origin = ultimate_origin;
18588 com_decl = fortran_common (decl_or_origin, &off);
18590 /* Symbol in common gets emitted as a child of the common block, in the form
18591 of a data member. */
18592 if (com_decl)
18594 dw_die_ref com_die;
18595 dw_loc_list_ref loc;
18596 die_node com_die_arg;
18598 var_die = lookup_decl_die (decl_or_origin);
18599 if (var_die)
18601 if (get_AT (var_die, DW_AT_location) == NULL)
18603 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18604 if (loc)
18606 if (off)
18608 /* Optimize the common case. */
18609 if (single_element_loc_list_p (loc)
18610 && loc->expr->dw_loc_opc == DW_OP_addr
18611 && loc->expr->dw_loc_next == NULL
18612 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18613 == SYMBOL_REF)
18615 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18616 loc->expr->dw_loc_oprnd1.v.val_addr
18617 = plus_constant (GET_MODE (x), x , off);
18619 else
18620 loc_list_plus_const (loc, off);
18622 add_AT_location_description (var_die, DW_AT_location, loc);
18623 remove_AT (var_die, DW_AT_declaration);
18626 return;
18629 if (common_block_die_table == NULL)
18630 common_block_die_table
18631 = htab_create_ggc (10, common_block_die_table_hash,
18632 common_block_die_table_eq, NULL);
18634 com_die_arg.decl_id = DECL_UID (com_decl);
18635 com_die_arg.die_parent = context_die;
18636 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18637 loc = loc_list_from_tree (com_decl, 2);
18638 if (com_die == NULL)
18640 const char *cnam
18641 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18642 void **slot;
18644 com_die = new_die (DW_TAG_common_block, context_die, decl);
18645 add_name_and_src_coords_attributes (com_die, com_decl);
18646 if (loc)
18648 add_AT_location_description (com_die, DW_AT_location, loc);
18649 /* Avoid sharing the same loc descriptor between
18650 DW_TAG_common_block and DW_TAG_variable. */
18651 loc = loc_list_from_tree (com_decl, 2);
18653 else if (DECL_EXTERNAL (decl))
18654 add_AT_flag (com_die, DW_AT_declaration, 1);
18655 if (want_pubnames ())
18656 add_pubname_string (cnam, com_die); /* ??? needed? */
18657 com_die->decl_id = DECL_UID (com_decl);
18658 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18659 *slot = (void *) com_die;
18661 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18663 add_AT_location_description (com_die, DW_AT_location, loc);
18664 loc = loc_list_from_tree (com_decl, 2);
18665 remove_AT (com_die, DW_AT_declaration);
18667 var_die = new_die (DW_TAG_variable, com_die, decl);
18668 add_name_and_src_coords_attributes (var_die, decl);
18669 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18670 TREE_THIS_VOLATILE (decl), context_die);
18671 add_AT_flag (var_die, DW_AT_external, 1);
18672 if (loc)
18674 if (off)
18676 /* Optimize the common case. */
18677 if (single_element_loc_list_p (loc)
18678 && loc->expr->dw_loc_opc == DW_OP_addr
18679 && loc->expr->dw_loc_next == NULL
18680 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18682 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18683 loc->expr->dw_loc_oprnd1.v.val_addr
18684 = plus_constant (GET_MODE (x), x, off);
18686 else
18687 loc_list_plus_const (loc, off);
18689 add_AT_location_description (var_die, DW_AT_location, loc);
18691 else if (DECL_EXTERNAL (decl))
18692 add_AT_flag (var_die, DW_AT_declaration, 1);
18693 equate_decl_number_to_die (decl, var_die);
18694 return;
18697 /* If the compiler emitted a definition for the DECL declaration
18698 and if we already emitted a DIE for it, don't emit a second
18699 DIE for it again. Allow re-declarations of DECLs that are
18700 inside functions, though. */
18701 if (old_die && declaration && !local_scope_p (context_die))
18702 return;
18704 /* For static data members, the declaration in the class is supposed
18705 to have DW_TAG_member tag; the specification should still be
18706 DW_TAG_variable referencing the DW_TAG_member DIE. */
18707 if (declaration && class_scope_p (context_die))
18708 var_die = new_die (DW_TAG_member, context_die, decl);
18709 else
18710 var_die = new_die (DW_TAG_variable, context_die, decl);
18712 origin_die = NULL;
18713 if (origin != NULL)
18714 origin_die = add_abstract_origin_attribute (var_die, origin);
18716 /* Loop unrolling can create multiple blocks that refer to the same
18717 static variable, so we must test for the DW_AT_declaration flag.
18719 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18720 copy decls and set the DECL_ABSTRACT flag on them instead of
18721 sharing them.
18723 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18725 ??? The declare_in_namespace support causes us to get two DIEs for one
18726 variable, both of which are declarations. We want to avoid considering
18727 one to be a specification, so we must test that this DIE is not a
18728 declaration. */
18729 else if (old_die && TREE_STATIC (decl) && ! declaration
18730 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18732 /* This is a definition of a C++ class level static. */
18733 add_AT_specification (var_die, old_die);
18734 specialization_p = true;
18735 if (DECL_NAME (decl))
18737 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18738 struct dwarf_file_data * file_index = lookup_filename (s.file);
18740 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18741 add_AT_file (var_die, DW_AT_decl_file, file_index);
18743 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18744 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18746 if (old_die->die_tag == DW_TAG_member)
18747 add_linkage_name (var_die, decl);
18750 else
18751 add_name_and_src_coords_attributes (var_die, decl);
18753 if ((origin == NULL && !specialization_p)
18754 || (origin != NULL
18755 && !DECL_ABSTRACT (decl_or_origin)
18756 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18757 decl_function_context
18758 (decl_or_origin))))
18760 tree type = TREE_TYPE (decl_or_origin);
18762 if (decl_by_reference_p (decl_or_origin))
18763 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18764 else
18765 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18766 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18769 if (origin == NULL && !specialization_p)
18771 if (TREE_PUBLIC (decl))
18772 add_AT_flag (var_die, DW_AT_external, 1);
18774 if (DECL_ARTIFICIAL (decl))
18775 add_AT_flag (var_die, DW_AT_artificial, 1);
18777 add_accessibility_attribute (var_die, decl);
18780 if (declaration)
18781 add_AT_flag (var_die, DW_AT_declaration, 1);
18783 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18784 equate_decl_number_to_die (decl, var_die);
18786 if (! declaration
18787 && (! DECL_ABSTRACT (decl_or_origin)
18788 /* Local static vars are shared between all clones/inlines,
18789 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18790 already set. */
18791 || (TREE_CODE (decl_or_origin) == VAR_DECL
18792 && TREE_STATIC (decl_or_origin)
18793 && DECL_RTL_SET_P (decl_or_origin)))
18794 /* When abstract origin already has DW_AT_location attribute, no need
18795 to add it again. */
18796 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18798 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18799 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18800 defer_location (decl_or_origin, var_die);
18801 else
18802 add_location_or_const_value_attribute (var_die, decl_or_origin,
18803 decl == NULL, DW_AT_location);
18804 add_pubname (decl_or_origin, var_die);
18806 else
18807 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18810 /* Generate a DIE to represent a named constant. */
18812 static void
18813 gen_const_die (tree decl, dw_die_ref context_die)
18815 dw_die_ref const_die;
18816 tree type = TREE_TYPE (decl);
18818 const_die = new_die (DW_TAG_constant, context_die, decl);
18819 add_name_and_src_coords_attributes (const_die, decl);
18820 add_type_attribute (const_die, type, 1, 0, context_die);
18821 if (TREE_PUBLIC (decl))
18822 add_AT_flag (const_die, DW_AT_external, 1);
18823 if (DECL_ARTIFICIAL (decl))
18824 add_AT_flag (const_die, DW_AT_artificial, 1);
18825 tree_add_const_value_attribute_for_decl (const_die, decl);
18828 /* Generate a DIE to represent a label identifier. */
18830 static void
18831 gen_label_die (tree decl, dw_die_ref context_die)
18833 tree origin = decl_ultimate_origin (decl);
18834 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18835 rtx insn;
18836 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18838 if (origin != NULL)
18839 add_abstract_origin_attribute (lbl_die, origin);
18840 else
18841 add_name_and_src_coords_attributes (lbl_die, decl);
18843 if (DECL_ABSTRACT (decl))
18844 equate_decl_number_to_die (decl, lbl_die);
18845 else
18847 insn = DECL_RTL_IF_SET (decl);
18849 /* Deleted labels are programmer specified labels which have been
18850 eliminated because of various optimizations. We still emit them
18851 here so that it is possible to put breakpoints on them. */
18852 if (insn
18853 && (LABEL_P (insn)
18854 || ((NOTE_P (insn)
18855 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18857 /* When optimization is enabled (via -O) some parts of the compiler
18858 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18859 represent source-level labels which were explicitly declared by
18860 the user. This really shouldn't be happening though, so catch
18861 it if it ever does happen. */
18862 gcc_assert (!INSN_DELETED_P (insn));
18864 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18865 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18867 else if (insn
18868 && NOTE_P (insn)
18869 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18870 && CODE_LABEL_NUMBER (insn) != -1)
18872 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18873 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18878 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18879 attributes to the DIE for a block STMT, to describe where the inlined
18880 function was called from. This is similar to add_src_coords_attributes. */
18882 static inline void
18883 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18885 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18887 if (dwarf_version >= 3 || !dwarf_strict)
18889 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18890 add_AT_unsigned (die, DW_AT_call_line, s.line);
18895 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18896 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18898 static inline void
18899 add_high_low_attributes (tree stmt, dw_die_ref die)
18901 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18903 if (BLOCK_FRAGMENT_CHAIN (stmt)
18904 && (dwarf_version >= 3 || !dwarf_strict))
18906 tree chain, superblock = NULL_TREE;
18907 dw_die_ref pdie;
18908 dw_attr_ref attr = NULL;
18910 if (inlined_function_outer_scope_p (stmt))
18912 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18913 BLOCK_NUMBER (stmt));
18914 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18917 /* Optimize duplicate .debug_ranges lists or even tails of
18918 lists. If this BLOCK has same ranges as its supercontext,
18919 lookup DW_AT_ranges attribute in the supercontext (and
18920 recursively so), verify that the ranges_table contains the
18921 right values and use it instead of adding a new .debug_range. */
18922 for (chain = stmt, pdie = die;
18923 BLOCK_SAME_RANGE (chain);
18924 chain = BLOCK_SUPERCONTEXT (chain))
18926 dw_attr_ref new_attr;
18928 pdie = pdie->die_parent;
18929 if (pdie == NULL)
18930 break;
18931 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18932 break;
18933 new_attr = get_AT (pdie, DW_AT_ranges);
18934 if (new_attr == NULL
18935 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18936 break;
18937 attr = new_attr;
18938 superblock = BLOCK_SUPERCONTEXT (chain);
18940 if (attr != NULL
18941 && (ranges_table[attr->dw_attr_val.v.val_offset
18942 / 2 / DWARF2_ADDR_SIZE].num
18943 == BLOCK_NUMBER (superblock))
18944 && BLOCK_FRAGMENT_CHAIN (superblock))
18946 unsigned long off = attr->dw_attr_val.v.val_offset
18947 / 2 / DWARF2_ADDR_SIZE;
18948 unsigned long supercnt = 0, thiscnt = 0;
18949 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18950 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18952 ++supercnt;
18953 gcc_checking_assert (ranges_table[off + supercnt].num
18954 == BLOCK_NUMBER (chain));
18956 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18957 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18958 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18959 ++thiscnt;
18960 gcc_assert (supercnt >= thiscnt);
18961 add_AT_range_list (die, DW_AT_ranges,
18962 ((off + supercnt - thiscnt)
18963 * 2 * DWARF2_ADDR_SIZE),
18964 false);
18965 return;
18968 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18970 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18973 add_ranges (chain);
18974 chain = BLOCK_FRAGMENT_CHAIN (chain);
18976 while (chain);
18977 add_ranges (NULL);
18979 else
18981 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18982 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18983 BLOCK_NUMBER (stmt));
18984 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18985 BLOCK_NUMBER (stmt));
18986 add_AT_low_high_pc (die, label, label_high, false);
18990 /* Generate a DIE for a lexical block. */
18992 static void
18993 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18995 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18997 if (call_arg_locations)
18999 if (block_map.length () <= BLOCK_NUMBER (stmt))
19000 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19001 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19004 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19005 add_high_low_attributes (stmt, stmt_die);
19007 decls_for_scope (stmt, stmt_die, depth);
19010 /* Generate a DIE for an inlined subprogram. */
19012 static void
19013 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19015 tree decl;
19017 /* The instance of function that is effectively being inlined shall not
19018 be abstract. */
19019 gcc_assert (! BLOCK_ABSTRACT (stmt));
19021 decl = block_ultimate_origin (stmt);
19023 /* Emit info for the abstract instance first, if we haven't yet. We
19024 must emit this even if the block is abstract, otherwise when we
19025 emit the block below (or elsewhere), we may end up trying to emit
19026 a die whose origin die hasn't been emitted, and crashing. */
19027 dwarf2out_abstract_function (decl);
19029 if (! BLOCK_ABSTRACT (stmt))
19031 dw_die_ref subr_die
19032 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19034 if (call_arg_locations)
19036 if (block_map.length () <= BLOCK_NUMBER (stmt))
19037 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19038 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19040 add_abstract_origin_attribute (subr_die, decl);
19041 if (TREE_ASM_WRITTEN (stmt))
19042 add_high_low_attributes (stmt, subr_die);
19043 add_call_src_coords_attributes (stmt, subr_die);
19045 decls_for_scope (stmt, subr_die, depth);
19046 current_function_has_inlines = 1;
19050 /* Generate a DIE for a field in a record, or structure. */
19052 static void
19053 gen_field_die (tree decl, dw_die_ref context_die)
19055 dw_die_ref decl_die;
19057 if (TREE_TYPE (decl) == error_mark_node)
19058 return;
19060 decl_die = new_die (DW_TAG_member, context_die, decl);
19061 add_name_and_src_coords_attributes (decl_die, decl);
19062 add_type_attribute (decl_die, member_declared_type (decl),
19063 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19064 context_die);
19066 if (DECL_BIT_FIELD_TYPE (decl))
19068 add_byte_size_attribute (decl_die, decl);
19069 add_bit_size_attribute (decl_die, decl);
19070 add_bit_offset_attribute (decl_die, decl);
19073 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19074 add_data_member_location_attribute (decl_die, decl);
19076 if (DECL_ARTIFICIAL (decl))
19077 add_AT_flag (decl_die, DW_AT_artificial, 1);
19079 add_accessibility_attribute (decl_die, decl);
19081 /* Equate decl number to die, so that we can look up this decl later on. */
19082 equate_decl_number_to_die (decl, decl_die);
19085 #if 0
19086 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19087 Use modified_type_die instead.
19088 We keep this code here just in case these types of DIEs may be needed to
19089 represent certain things in other languages (e.g. Pascal) someday. */
19091 static void
19092 gen_pointer_type_die (tree type, dw_die_ref context_die)
19094 dw_die_ref ptr_die
19095 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19097 equate_type_number_to_die (type, ptr_die);
19098 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19099 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19102 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19103 Use modified_type_die instead.
19104 We keep this code here just in case these types of DIEs may be needed to
19105 represent certain things in other languages (e.g. Pascal) someday. */
19107 static void
19108 gen_reference_type_die (tree type, dw_die_ref context_die)
19110 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19112 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19113 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19114 else
19115 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19117 equate_type_number_to_die (type, ref_die);
19118 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19119 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19121 #endif
19123 /* Generate a DIE for a pointer to a member type. */
19125 static void
19126 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19128 dw_die_ref ptr_die
19129 = new_die (DW_TAG_ptr_to_member_type,
19130 scope_die_for (type, context_die), type);
19132 equate_type_number_to_die (type, ptr_die);
19133 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19134 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19135 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19138 typedef const char *dchar_p; /* For DEF_VEC_P. */
19140 static char *producer_string;
19142 /* Return a heap allocated producer string including command line options
19143 if -grecord-gcc-switches. */
19145 static char *
19146 gen_producer_string (void)
19148 size_t j;
19149 auto_vec<dchar_p> switches;
19150 const char *language_string = lang_hooks.name;
19151 char *producer, *tail;
19152 const char *p;
19153 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19154 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19156 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19157 switch (save_decoded_options[j].opt_index)
19159 case OPT_o:
19160 case OPT_d:
19161 case OPT_dumpbase:
19162 case OPT_dumpdir:
19163 case OPT_auxbase:
19164 case OPT_auxbase_strip:
19165 case OPT_quiet:
19166 case OPT_version:
19167 case OPT_v:
19168 case OPT_w:
19169 case OPT_L:
19170 case OPT_D:
19171 case OPT_I:
19172 case OPT_U:
19173 case OPT_SPECIAL_unknown:
19174 case OPT_SPECIAL_ignore:
19175 case OPT_SPECIAL_program_name:
19176 case OPT_SPECIAL_input_file:
19177 case OPT_grecord_gcc_switches:
19178 case OPT_gno_record_gcc_switches:
19179 case OPT__output_pch_:
19180 case OPT_fdiagnostics_show_location_:
19181 case OPT_fdiagnostics_show_option:
19182 case OPT_fdiagnostics_show_caret:
19183 case OPT_fdiagnostics_color_:
19184 case OPT_fverbose_asm:
19185 case OPT____:
19186 case OPT__sysroot_:
19187 case OPT_nostdinc:
19188 case OPT_nostdinc__:
19189 /* Ignore these. */
19190 continue;
19191 default:
19192 if (cl_options[save_decoded_options[j].opt_index].flags
19193 & CL_NO_DWARF_RECORD)
19194 continue;
19195 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19196 == '-');
19197 switch (save_decoded_options[j].canonical_option[0][1])
19199 case 'M':
19200 case 'i':
19201 case 'W':
19202 continue;
19203 case 'f':
19204 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19205 "dump", 4) == 0)
19206 continue;
19207 break;
19208 default:
19209 break;
19211 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19212 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19213 break;
19216 producer = XNEWVEC (char, plen + 1 + len + 1);
19217 tail = producer;
19218 sprintf (tail, "%s %s", language_string, version_string);
19219 tail += plen;
19221 FOR_EACH_VEC_ELT (switches, j, p)
19223 len = strlen (p);
19224 *tail = ' ';
19225 memcpy (tail + 1, p, len);
19226 tail += len + 1;
19229 *tail = '\0';
19230 return producer;
19233 /* Generate the DIE for the compilation unit. */
19235 static dw_die_ref
19236 gen_compile_unit_die (const char *filename)
19238 dw_die_ref die;
19239 const char *language_string = lang_hooks.name;
19240 int language;
19242 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19244 if (filename)
19246 add_name_attribute (die, filename);
19247 /* Don't add cwd for <built-in>. */
19248 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19249 add_comp_dir_attribute (die);
19252 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19254 /* If our producer is LTO try to figure out a common language to use
19255 from the global list of translation units. */
19256 if (strcmp (language_string, "GNU GIMPLE") == 0)
19258 unsigned i;
19259 tree t;
19260 const char *common_lang = NULL;
19262 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19264 if (!TRANSLATION_UNIT_LANGUAGE (t))
19265 continue;
19266 if (!common_lang)
19267 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19268 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19270 else if (strncmp (common_lang, "GNU C", 5) == 0
19271 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19272 /* Mixing C and C++ is ok, use C++ in that case. */
19273 common_lang = "GNU C++";
19274 else
19276 /* Fall back to C. */
19277 common_lang = NULL;
19278 break;
19282 if (common_lang)
19283 language_string = common_lang;
19286 language = DW_LANG_C89;
19287 if (strcmp (language_string, "GNU C++") == 0)
19288 language = DW_LANG_C_plus_plus;
19289 else if (strcmp (language_string, "GNU F77") == 0)
19290 language = DW_LANG_Fortran77;
19291 else if (strcmp (language_string, "GNU Pascal") == 0)
19292 language = DW_LANG_Pascal83;
19293 else if (dwarf_version >= 3 || !dwarf_strict)
19295 if (strcmp (language_string, "GNU Ada") == 0)
19296 language = DW_LANG_Ada95;
19297 else if (strcmp (language_string, "GNU Fortran") == 0)
19298 language = DW_LANG_Fortran95;
19299 else if (strcmp (language_string, "GNU Java") == 0)
19300 language = DW_LANG_Java;
19301 else if (strcmp (language_string, "GNU Objective-C") == 0)
19302 language = DW_LANG_ObjC;
19303 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19304 language = DW_LANG_ObjC_plus_plus;
19305 else if (dwarf_version >= 5 || !dwarf_strict)
19307 if (strcmp (language_string, "GNU Go") == 0)
19308 language = DW_LANG_Go;
19311 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19312 else if (strcmp (language_string, "GNU Fortran") == 0)
19313 language = DW_LANG_Fortran90;
19315 add_AT_unsigned (die, DW_AT_language, language);
19317 switch (language)
19319 case DW_LANG_Fortran77:
19320 case DW_LANG_Fortran90:
19321 case DW_LANG_Fortran95:
19322 /* Fortran has case insensitive identifiers and the front-end
19323 lowercases everything. */
19324 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19325 break;
19326 default:
19327 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19328 break;
19330 return die;
19333 /* Generate the DIE for a base class. */
19335 static void
19336 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19338 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19340 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19341 add_data_member_location_attribute (die, binfo);
19343 if (BINFO_VIRTUAL_P (binfo))
19344 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19346 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19347 children, otherwise the default is DW_ACCESS_public. In DWARF2
19348 the default has always been DW_ACCESS_private. */
19349 if (access == access_public_node)
19351 if (dwarf_version == 2
19352 || context_die->die_tag == DW_TAG_class_type)
19353 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19355 else if (access == access_protected_node)
19356 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19357 else if (dwarf_version > 2
19358 && context_die->die_tag != DW_TAG_class_type)
19359 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19362 /* Generate a DIE for a class member. */
19364 static void
19365 gen_member_die (tree type, dw_die_ref context_die)
19367 tree member;
19368 tree binfo = TYPE_BINFO (type);
19369 dw_die_ref child;
19371 /* If this is not an incomplete type, output descriptions of each of its
19372 members. Note that as we output the DIEs necessary to represent the
19373 members of this record or union type, we will also be trying to output
19374 DIEs to represent the *types* of those members. However the `type'
19375 function (above) will specifically avoid generating type DIEs for member
19376 types *within* the list of member DIEs for this (containing) type except
19377 for those types (of members) which are explicitly marked as also being
19378 members of this (containing) type themselves. The g++ front- end can
19379 force any given type to be treated as a member of some other (containing)
19380 type by setting the TYPE_CONTEXT of the given (member) type to point to
19381 the TREE node representing the appropriate (containing) type. */
19383 /* First output info about the base classes. */
19384 if (binfo)
19386 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19387 int i;
19388 tree base;
19390 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19391 gen_inheritance_die (base,
19392 (accesses ? (*accesses)[i] : access_public_node),
19393 context_die);
19396 /* Now output info about the data members and type members. */
19397 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19399 /* If we thought we were generating minimal debug info for TYPE
19400 and then changed our minds, some of the member declarations
19401 may have already been defined. Don't define them again, but
19402 do put them in the right order. */
19404 child = lookup_decl_die (member);
19405 if (child)
19406 splice_child_die (context_die, child);
19407 else
19408 gen_decl_die (member, NULL, context_die);
19411 /* Now output info about the function members (if any). */
19412 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19414 /* Don't include clones in the member list. */
19415 if (DECL_ABSTRACT_ORIGIN (member))
19416 continue;
19418 child = lookup_decl_die (member);
19419 if (child)
19420 splice_child_die (context_die, child);
19421 else
19422 gen_decl_die (member, NULL, context_die);
19426 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19427 is set, we pretend that the type was never defined, so we only get the
19428 member DIEs needed by later specification DIEs. */
19430 static void
19431 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19432 enum debug_info_usage usage)
19434 dw_die_ref type_die = lookup_type_die (type);
19435 dw_die_ref scope_die = 0;
19436 int nested = 0;
19437 int complete = (TYPE_SIZE (type)
19438 && (! TYPE_STUB_DECL (type)
19439 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19440 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19441 complete = complete && should_emit_struct_debug (type, usage);
19443 if (type_die && ! complete)
19444 return;
19446 if (TYPE_CONTEXT (type) != NULL_TREE
19447 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19448 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19449 nested = 1;
19451 scope_die = scope_die_for (type, context_die);
19453 /* Generate child dies for template paramaters. */
19454 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19455 schedule_generic_params_dies_gen (type);
19457 if (! type_die || (nested && is_cu_die (scope_die)))
19458 /* First occurrence of type or toplevel definition of nested class. */
19460 dw_die_ref old_die = type_die;
19462 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19463 ? record_type_tag (type) : DW_TAG_union_type,
19464 scope_die, type);
19465 equate_type_number_to_die (type, type_die);
19466 if (old_die)
19467 add_AT_specification (type_die, old_die);
19468 else
19469 add_name_attribute (type_die, type_tag (type));
19471 else
19472 remove_AT (type_die, DW_AT_declaration);
19474 /* If this type has been completed, then give it a byte_size attribute and
19475 then give a list of members. */
19476 if (complete && !ns_decl)
19478 /* Prevent infinite recursion in cases where the type of some member of
19479 this type is expressed in terms of this type itself. */
19480 TREE_ASM_WRITTEN (type) = 1;
19481 add_byte_size_attribute (type_die, type);
19482 if (TYPE_STUB_DECL (type) != NULL_TREE)
19484 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19485 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19488 /* If the first reference to this type was as the return type of an
19489 inline function, then it may not have a parent. Fix this now. */
19490 if (type_die->die_parent == NULL)
19491 add_child_die (scope_die, type_die);
19493 push_decl_scope (type);
19494 gen_member_die (type, type_die);
19495 pop_decl_scope ();
19497 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19498 if (TYPE_ARTIFICIAL (type))
19499 add_AT_flag (type_die, DW_AT_artificial, 1);
19501 /* GNU extension: Record what type our vtable lives in. */
19502 if (TYPE_VFIELD (type))
19504 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19506 gen_type_die (vtype, context_die);
19507 add_AT_die_ref (type_die, DW_AT_containing_type,
19508 lookup_type_die (vtype));
19511 else
19513 add_AT_flag (type_die, DW_AT_declaration, 1);
19515 /* We don't need to do this for function-local types. */
19516 if (TYPE_STUB_DECL (type)
19517 && ! decl_function_context (TYPE_STUB_DECL (type)))
19518 vec_safe_push (incomplete_types, type);
19521 if (get_AT (type_die, DW_AT_name))
19522 add_pubtype (type, type_die);
19525 /* Generate a DIE for a subroutine _type_. */
19527 static void
19528 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19530 tree return_type = TREE_TYPE (type);
19531 dw_die_ref subr_die
19532 = new_die (DW_TAG_subroutine_type,
19533 scope_die_for (type, context_die), type);
19535 equate_type_number_to_die (type, subr_die);
19536 add_prototyped_attribute (subr_die, type);
19537 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19538 gen_formal_types_die (type, subr_die);
19540 if (get_AT (subr_die, DW_AT_name))
19541 add_pubtype (type, subr_die);
19544 /* Generate a DIE for a type definition. */
19546 static void
19547 gen_typedef_die (tree decl, dw_die_ref context_die)
19549 dw_die_ref type_die;
19550 tree origin;
19552 if (TREE_ASM_WRITTEN (decl))
19553 return;
19555 TREE_ASM_WRITTEN (decl) = 1;
19556 type_die = new_die (DW_TAG_typedef, context_die, decl);
19557 origin = decl_ultimate_origin (decl);
19558 if (origin != NULL)
19559 add_abstract_origin_attribute (type_die, origin);
19560 else
19562 tree type;
19564 add_name_and_src_coords_attributes (type_die, decl);
19565 if (DECL_ORIGINAL_TYPE (decl))
19567 type = DECL_ORIGINAL_TYPE (decl);
19569 gcc_assert (type != TREE_TYPE (decl));
19570 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19572 else
19574 type = TREE_TYPE (decl);
19576 if (is_naming_typedef_decl (TYPE_NAME (type)))
19578 /* Here, we are in the case of decl being a typedef naming
19579 an anonymous type, e.g:
19580 typedef struct {...} foo;
19581 In that case TREE_TYPE (decl) is not a typedef variant
19582 type and TYPE_NAME of the anonymous type is set to the
19583 TYPE_DECL of the typedef. This construct is emitted by
19584 the C++ FE.
19586 TYPE is the anonymous struct named by the typedef
19587 DECL. As we need the DW_AT_type attribute of the
19588 DW_TAG_typedef to point to the DIE of TYPE, let's
19589 generate that DIE right away. add_type_attribute
19590 called below will then pick (via lookup_type_die) that
19591 anonymous struct DIE. */
19592 if (!TREE_ASM_WRITTEN (type))
19593 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19595 /* This is a GNU Extension. We are adding a
19596 DW_AT_linkage_name attribute to the DIE of the
19597 anonymous struct TYPE. The value of that attribute
19598 is the name of the typedef decl naming the anonymous
19599 struct. This greatly eases the work of consumers of
19600 this debug info. */
19601 add_linkage_attr (lookup_type_die (type), decl);
19605 add_type_attribute (type_die, type, TREE_READONLY (decl),
19606 TREE_THIS_VOLATILE (decl), context_die);
19608 if (is_naming_typedef_decl (decl))
19609 /* We want that all subsequent calls to lookup_type_die with
19610 TYPE in argument yield the DW_TAG_typedef we have just
19611 created. */
19612 equate_type_number_to_die (type, type_die);
19614 add_accessibility_attribute (type_die, decl);
19617 if (DECL_ABSTRACT (decl))
19618 equate_decl_number_to_die (decl, type_die);
19620 if (get_AT (type_die, DW_AT_name))
19621 add_pubtype (decl, type_die);
19624 /* Generate a DIE for a struct, class, enum or union type. */
19626 static void
19627 gen_tagged_type_die (tree type,
19628 dw_die_ref context_die,
19629 enum debug_info_usage usage)
19631 int need_pop;
19633 if (type == NULL_TREE
19634 || !is_tagged_type (type))
19635 return;
19637 /* If this is a nested type whose containing class hasn't been written
19638 out yet, writing it out will cover this one, too. This does not apply
19639 to instantiations of member class templates; they need to be added to
19640 the containing class as they are generated. FIXME: This hurts the
19641 idea of combining type decls from multiple TUs, since we can't predict
19642 what set of template instantiations we'll get. */
19643 if (TYPE_CONTEXT (type)
19644 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19645 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19647 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19649 if (TREE_ASM_WRITTEN (type))
19650 return;
19652 /* If that failed, attach ourselves to the stub. */
19653 push_decl_scope (TYPE_CONTEXT (type));
19654 context_die = lookup_type_die (TYPE_CONTEXT (type));
19655 need_pop = 1;
19657 else if (TYPE_CONTEXT (type) != NULL_TREE
19658 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19660 /* If this type is local to a function that hasn't been written
19661 out yet, use a NULL context for now; it will be fixed up in
19662 decls_for_scope. */
19663 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19664 /* A declaration DIE doesn't count; nested types need to go in the
19665 specification. */
19666 if (context_die && is_declaration_die (context_die))
19667 context_die = NULL;
19668 need_pop = 0;
19670 else
19672 context_die = declare_in_namespace (type, context_die);
19673 need_pop = 0;
19676 if (TREE_CODE (type) == ENUMERAL_TYPE)
19678 /* This might have been written out by the call to
19679 declare_in_namespace. */
19680 if (!TREE_ASM_WRITTEN (type))
19681 gen_enumeration_type_die (type, context_die);
19683 else
19684 gen_struct_or_union_type_die (type, context_die, usage);
19686 if (need_pop)
19687 pop_decl_scope ();
19689 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19690 it up if it is ever completed. gen_*_type_die will set it for us
19691 when appropriate. */
19694 /* Generate a type description DIE. */
19696 static void
19697 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19698 enum debug_info_usage usage)
19700 struct array_descr_info info;
19702 if (type == NULL_TREE || type == error_mark_node)
19703 return;
19705 if (TYPE_NAME (type) != NULL_TREE
19706 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19707 && is_redundant_typedef (TYPE_NAME (type))
19708 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19709 /* The DECL of this type is a typedef we don't want to emit debug
19710 info for but we want debug info for its underlying typedef.
19711 This can happen for e.g, the injected-class-name of a C++
19712 type. */
19713 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19715 /* If TYPE is a typedef type variant, let's generate debug info
19716 for the parent typedef which TYPE is a type of. */
19717 if (typedef_variant_p (type))
19719 if (TREE_ASM_WRITTEN (type))
19720 return;
19722 /* Prevent broken recursion; we can't hand off to the same type. */
19723 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19725 /* Give typedefs the right scope. */
19726 context_die = scope_die_for (type, context_die);
19728 TREE_ASM_WRITTEN (type) = 1;
19730 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19731 return;
19734 /* If type is an anonymous tagged type named by a typedef, let's
19735 generate debug info for the typedef. */
19736 if (is_naming_typedef_decl (TYPE_NAME (type)))
19738 /* Use the DIE of the containing namespace as the parent DIE of
19739 the type description DIE we want to generate. */
19740 if (DECL_CONTEXT (TYPE_NAME (type))
19741 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19742 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19744 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19745 return;
19748 /* If this is an array type with hidden descriptor, handle it first. */
19749 if (!TREE_ASM_WRITTEN (type)
19750 && lang_hooks.types.get_array_descr_info
19751 && lang_hooks.types.get_array_descr_info (type, &info)
19752 && (dwarf_version >= 3 || !dwarf_strict))
19754 gen_descr_array_type_die (type, &info, context_die);
19755 TREE_ASM_WRITTEN (type) = 1;
19756 return;
19759 /* We are going to output a DIE to represent the unqualified version
19760 of this type (i.e. without any const or volatile qualifiers) so
19761 get the main variant (i.e. the unqualified version) of this type
19762 now. (Vectors are special because the debugging info is in the
19763 cloned type itself). */
19764 if (TREE_CODE (type) != VECTOR_TYPE)
19765 type = type_main_variant (type);
19767 if (TREE_ASM_WRITTEN (type))
19768 return;
19770 switch (TREE_CODE (type))
19772 case ERROR_MARK:
19773 break;
19775 case POINTER_TYPE:
19776 case REFERENCE_TYPE:
19777 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19778 ensures that the gen_type_die recursion will terminate even if the
19779 type is recursive. Recursive types are possible in Ada. */
19780 /* ??? We could perhaps do this for all types before the switch
19781 statement. */
19782 TREE_ASM_WRITTEN (type) = 1;
19784 /* For these types, all that is required is that we output a DIE (or a
19785 set of DIEs) to represent the "basis" type. */
19786 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19787 DINFO_USAGE_IND_USE);
19788 break;
19790 case OFFSET_TYPE:
19791 /* This code is used for C++ pointer-to-data-member types.
19792 Output a description of the relevant class type. */
19793 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19794 DINFO_USAGE_IND_USE);
19796 /* Output a description of the type of the object pointed to. */
19797 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19798 DINFO_USAGE_IND_USE);
19800 /* Now output a DIE to represent this pointer-to-data-member type
19801 itself. */
19802 gen_ptr_to_mbr_type_die (type, context_die);
19803 break;
19805 case FUNCTION_TYPE:
19806 /* Force out return type (in case it wasn't forced out already). */
19807 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19808 DINFO_USAGE_DIR_USE);
19809 gen_subroutine_type_die (type, context_die);
19810 break;
19812 case METHOD_TYPE:
19813 /* Force out return type (in case it wasn't forced out already). */
19814 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19815 DINFO_USAGE_DIR_USE);
19816 gen_subroutine_type_die (type, context_die);
19817 break;
19819 case ARRAY_TYPE:
19820 gen_array_type_die (type, context_die);
19821 break;
19823 case VECTOR_TYPE:
19824 gen_array_type_die (type, context_die);
19825 break;
19827 case ENUMERAL_TYPE:
19828 case RECORD_TYPE:
19829 case UNION_TYPE:
19830 case QUAL_UNION_TYPE:
19831 gen_tagged_type_die (type, context_die, usage);
19832 return;
19834 case VOID_TYPE:
19835 case INTEGER_TYPE:
19836 case REAL_TYPE:
19837 case FIXED_POINT_TYPE:
19838 case COMPLEX_TYPE:
19839 case BOOLEAN_TYPE:
19840 /* No DIEs needed for fundamental types. */
19841 break;
19843 case NULLPTR_TYPE:
19844 case LANG_TYPE:
19845 /* Just use DW_TAG_unspecified_type. */
19847 dw_die_ref type_die = lookup_type_die (type);
19848 if (type_die == NULL)
19850 tree name = TYPE_NAME (type);
19851 if (TREE_CODE (name) == TYPE_DECL)
19852 name = DECL_NAME (name);
19853 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19854 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19855 equate_type_number_to_die (type, type_die);
19858 break;
19860 default:
19861 if (is_cxx_auto (type))
19863 tree name = TYPE_NAME (type);
19864 if (TREE_CODE (name) == TYPE_DECL)
19865 name = DECL_NAME (name);
19866 dw_die_ref *die = (name == get_identifier ("auto")
19867 ? &auto_die : &decltype_auto_die);
19868 if (!*die)
19870 *die = new_die (DW_TAG_unspecified_type,
19871 comp_unit_die (), NULL_TREE);
19872 add_name_attribute (*die, IDENTIFIER_POINTER (name));
19874 equate_type_number_to_die (type, *die);
19875 break;
19877 gcc_unreachable ();
19880 TREE_ASM_WRITTEN (type) = 1;
19883 static void
19884 gen_type_die (tree type, dw_die_ref context_die)
19886 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19889 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19890 things which are local to the given block. */
19892 static void
19893 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19895 int must_output_die = 0;
19896 bool inlined_func;
19898 /* Ignore blocks that are NULL. */
19899 if (stmt == NULL_TREE)
19900 return;
19902 inlined_func = inlined_function_outer_scope_p (stmt);
19904 /* If the block is one fragment of a non-contiguous block, do not
19905 process the variables, since they will have been done by the
19906 origin block. Do process subblocks. */
19907 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19909 tree sub;
19911 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19912 gen_block_die (sub, context_die, depth + 1);
19914 return;
19917 /* Determine if we need to output any Dwarf DIEs at all to represent this
19918 block. */
19919 if (inlined_func)
19920 /* The outer scopes for inlinings *must* always be represented. We
19921 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19922 must_output_die = 1;
19923 else
19925 /* Determine if this block directly contains any "significant"
19926 local declarations which we will need to output DIEs for. */
19927 if (debug_info_level > DINFO_LEVEL_TERSE)
19928 /* We are not in terse mode so *any* local declaration counts
19929 as being a "significant" one. */
19930 must_output_die = ((BLOCK_VARS (stmt) != NULL
19931 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19932 && (TREE_USED (stmt)
19933 || TREE_ASM_WRITTEN (stmt)
19934 || BLOCK_ABSTRACT (stmt)));
19935 else if ((TREE_USED (stmt)
19936 || TREE_ASM_WRITTEN (stmt)
19937 || BLOCK_ABSTRACT (stmt))
19938 && !dwarf2out_ignore_block (stmt))
19939 must_output_die = 1;
19942 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19943 DIE for any block which contains no significant local declarations at
19944 all. Rather, in such cases we just call `decls_for_scope' so that any
19945 needed Dwarf info for any sub-blocks will get properly generated. Note
19946 that in terse mode, our definition of what constitutes a "significant"
19947 local declaration gets restricted to include only inlined function
19948 instances and local (nested) function definitions. */
19949 if (must_output_die)
19951 if (inlined_func)
19953 /* If STMT block is abstract, that means we have been called
19954 indirectly from dwarf2out_abstract_function.
19955 That function rightfully marks the descendent blocks (of
19956 the abstract function it is dealing with) as being abstract,
19957 precisely to prevent us from emitting any
19958 DW_TAG_inlined_subroutine DIE as a descendent
19959 of an abstract function instance. So in that case, we should
19960 not call gen_inlined_subroutine_die.
19962 Later though, when cgraph asks dwarf2out to emit info
19963 for the concrete instance of the function decl into which
19964 the concrete instance of STMT got inlined, the later will lead
19965 to the generation of a DW_TAG_inlined_subroutine DIE. */
19966 if (! BLOCK_ABSTRACT (stmt))
19967 gen_inlined_subroutine_die (stmt, context_die, depth);
19969 else
19970 gen_lexical_block_die (stmt, context_die, depth);
19972 else
19973 decls_for_scope (stmt, context_die, depth);
19976 /* Process variable DECL (or variable with origin ORIGIN) within
19977 block STMT and add it to CONTEXT_DIE. */
19978 static void
19979 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19981 dw_die_ref die;
19982 tree decl_or_origin = decl ? decl : origin;
19984 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19985 die = lookup_decl_die (decl_or_origin);
19986 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19987 && TYPE_DECL_IS_STUB (decl_or_origin))
19988 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19989 else
19990 die = NULL;
19992 if (die != NULL && die->die_parent == NULL)
19993 add_child_die (context_die, die);
19994 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19995 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19996 stmt, context_die);
19997 else
19998 gen_decl_die (decl, origin, context_die);
20001 /* Generate all of the decls declared within a given scope and (recursively)
20002 all of its sub-blocks. */
20004 static void
20005 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20007 tree decl;
20008 unsigned int i;
20009 tree subblocks;
20011 /* Ignore NULL blocks. */
20012 if (stmt == NULL_TREE)
20013 return;
20015 /* Output the DIEs to represent all of the data objects and typedefs
20016 declared directly within this block but not within any nested
20017 sub-blocks. Also, nested function and tag DIEs have been
20018 generated with a parent of NULL; fix that up now. We don't
20019 have to do this if we're at -g1. */
20020 if (debug_info_level > DINFO_LEVEL_TERSE)
20022 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20023 process_scope_var (stmt, decl, NULL_TREE, context_die);
20024 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20025 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20026 context_die);
20029 /* Even if we're at -g1, we need to process the subblocks in order to get
20030 inlined call information. */
20032 /* Output the DIEs to represent all sub-blocks (and the items declared
20033 therein) of this block. */
20034 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20035 subblocks != NULL;
20036 subblocks = BLOCK_CHAIN (subblocks))
20037 gen_block_die (subblocks, context_die, depth + 1);
20040 /* Is this a typedef we can avoid emitting? */
20042 static inline int
20043 is_redundant_typedef (const_tree decl)
20045 if (TYPE_DECL_IS_STUB (decl))
20046 return 1;
20048 if (DECL_ARTIFICIAL (decl)
20049 && DECL_CONTEXT (decl)
20050 && is_tagged_type (DECL_CONTEXT (decl))
20051 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20052 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20053 /* Also ignore the artificial member typedef for the class name. */
20054 return 1;
20056 return 0;
20059 /* Return TRUE if TYPE is a typedef that names a type for linkage
20060 purposes. This kind of typedefs is produced by the C++ FE for
20061 constructs like:
20063 typedef struct {...} foo;
20065 In that case, there is no typedef variant type produced for foo.
20066 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20067 struct type. */
20069 static bool
20070 is_naming_typedef_decl (const_tree decl)
20072 if (decl == NULL_TREE
20073 || TREE_CODE (decl) != TYPE_DECL
20074 || !is_tagged_type (TREE_TYPE (decl))
20075 || DECL_IS_BUILTIN (decl)
20076 || is_redundant_typedef (decl)
20077 /* It looks like Ada produces TYPE_DECLs that are very similar
20078 to C++ naming typedefs but that have different
20079 semantics. Let's be specific to c++ for now. */
20080 || !is_cxx ())
20081 return FALSE;
20083 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20084 && TYPE_NAME (TREE_TYPE (decl)) == decl
20085 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20086 != TYPE_NAME (TREE_TYPE (decl))));
20089 /* Returns the DIE for a context. */
20091 static inline dw_die_ref
20092 get_context_die (tree context)
20094 if (context)
20096 /* Find die that represents this context. */
20097 if (TYPE_P (context))
20099 context = TYPE_MAIN_VARIANT (context);
20100 return strip_naming_typedef (context, force_type_die (context));
20102 else
20103 return force_decl_die (context);
20105 return comp_unit_die ();
20108 /* Returns the DIE for decl. A DIE will always be returned. */
20110 static dw_die_ref
20111 force_decl_die (tree decl)
20113 dw_die_ref decl_die;
20114 unsigned saved_external_flag;
20115 tree save_fn = NULL_TREE;
20116 decl_die = lookup_decl_die (decl);
20117 if (!decl_die)
20119 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20121 decl_die = lookup_decl_die (decl);
20122 if (decl_die)
20123 return decl_die;
20125 switch (TREE_CODE (decl))
20127 case FUNCTION_DECL:
20128 /* Clear current_function_decl, so that gen_subprogram_die thinks
20129 that this is a declaration. At this point, we just want to force
20130 declaration die. */
20131 save_fn = current_function_decl;
20132 current_function_decl = NULL_TREE;
20133 gen_subprogram_die (decl, context_die);
20134 current_function_decl = save_fn;
20135 break;
20137 case VAR_DECL:
20138 /* Set external flag to force declaration die. Restore it after
20139 gen_decl_die() call. */
20140 saved_external_flag = DECL_EXTERNAL (decl);
20141 DECL_EXTERNAL (decl) = 1;
20142 gen_decl_die (decl, NULL, context_die);
20143 DECL_EXTERNAL (decl) = saved_external_flag;
20144 break;
20146 case NAMESPACE_DECL:
20147 if (dwarf_version >= 3 || !dwarf_strict)
20148 dwarf2out_decl (decl);
20149 else
20150 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20151 decl_die = comp_unit_die ();
20152 break;
20154 case TRANSLATION_UNIT_DECL:
20155 decl_die = comp_unit_die ();
20156 break;
20158 default:
20159 gcc_unreachable ();
20162 /* We should be able to find the DIE now. */
20163 if (!decl_die)
20164 decl_die = lookup_decl_die (decl);
20165 gcc_assert (decl_die);
20168 return decl_die;
20171 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20172 always returned. */
20174 static dw_die_ref
20175 force_type_die (tree type)
20177 dw_die_ref type_die;
20179 type_die = lookup_type_die (type);
20180 if (!type_die)
20182 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20184 type_die = modified_type_die (type, TYPE_READONLY (type),
20185 TYPE_VOLATILE (type), context_die);
20186 gcc_assert (type_die);
20188 return type_die;
20191 /* Force out any required namespaces to be able to output DECL,
20192 and return the new context_die for it, if it's changed. */
20194 static dw_die_ref
20195 setup_namespace_context (tree thing, dw_die_ref context_die)
20197 tree context = (DECL_P (thing)
20198 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20199 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20200 /* Force out the namespace. */
20201 context_die = force_decl_die (context);
20203 return context_die;
20206 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20207 type) within its namespace, if appropriate.
20209 For compatibility with older debuggers, namespace DIEs only contain
20210 declarations; all definitions are emitted at CU scope. */
20212 static dw_die_ref
20213 declare_in_namespace (tree thing, dw_die_ref context_die)
20215 dw_die_ref ns_context;
20217 if (debug_info_level <= DINFO_LEVEL_TERSE)
20218 return context_die;
20220 /* If this decl is from an inlined function, then don't try to emit it in its
20221 namespace, as we will get confused. It would have already been emitted
20222 when the abstract instance of the inline function was emitted anyways. */
20223 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20224 return context_die;
20226 ns_context = setup_namespace_context (thing, context_die);
20228 if (ns_context != context_die)
20230 if (is_fortran ())
20231 return ns_context;
20232 if (DECL_P (thing))
20233 gen_decl_die (thing, NULL, ns_context);
20234 else
20235 gen_type_die (thing, ns_context);
20237 return context_die;
20240 /* Generate a DIE for a namespace or namespace alias. */
20242 static void
20243 gen_namespace_die (tree decl, dw_die_ref context_die)
20245 dw_die_ref namespace_die;
20247 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20248 they are an alias of. */
20249 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20251 /* Output a real namespace or module. */
20252 context_die = setup_namespace_context (decl, comp_unit_die ());
20253 namespace_die = new_die (is_fortran ()
20254 ? DW_TAG_module : DW_TAG_namespace,
20255 context_die, decl);
20256 /* For Fortran modules defined in different CU don't add src coords. */
20257 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20259 const char *name = dwarf2_name (decl, 0);
20260 if (name)
20261 add_name_attribute (namespace_die, name);
20263 else
20264 add_name_and_src_coords_attributes (namespace_die, decl);
20265 if (DECL_EXTERNAL (decl))
20266 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20267 equate_decl_number_to_die (decl, namespace_die);
20269 else
20271 /* Output a namespace alias. */
20273 /* Force out the namespace we are an alias of, if necessary. */
20274 dw_die_ref origin_die
20275 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20277 if (DECL_FILE_SCOPE_P (decl)
20278 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20279 context_die = setup_namespace_context (decl, comp_unit_die ());
20280 /* Now create the namespace alias DIE. */
20281 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20282 add_name_and_src_coords_attributes (namespace_die, decl);
20283 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20284 equate_decl_number_to_die (decl, namespace_die);
20286 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20287 if (want_pubnames ())
20288 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20291 /* Generate Dwarf debug information for a decl described by DECL.
20292 The return value is currently only meaningful for PARM_DECLs,
20293 for all other decls it returns NULL. */
20295 static dw_die_ref
20296 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20298 tree decl_or_origin = decl ? decl : origin;
20299 tree class_origin = NULL, ultimate_origin;
20301 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20302 return NULL;
20304 switch (TREE_CODE (decl_or_origin))
20306 case ERROR_MARK:
20307 break;
20309 case CONST_DECL:
20310 if (!is_fortran () && !is_ada ())
20312 /* The individual enumerators of an enum type get output when we output
20313 the Dwarf representation of the relevant enum type itself. */
20314 break;
20317 /* Emit its type. */
20318 gen_type_die (TREE_TYPE (decl), context_die);
20320 /* And its containing namespace. */
20321 context_die = declare_in_namespace (decl, context_die);
20323 gen_const_die (decl, context_die);
20324 break;
20326 case FUNCTION_DECL:
20327 /* Don't output any DIEs to represent mere function declarations,
20328 unless they are class members or explicit block externs. */
20329 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20330 && DECL_FILE_SCOPE_P (decl_or_origin)
20331 && (current_function_decl == NULL_TREE
20332 || DECL_ARTIFICIAL (decl_or_origin)))
20333 break;
20335 #if 0
20336 /* FIXME */
20337 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20338 on local redeclarations of global functions. That seems broken. */
20339 if (current_function_decl != decl)
20340 /* This is only a declaration. */;
20341 #endif
20343 /* If we're emitting a clone, emit info for the abstract instance. */
20344 if (origin || DECL_ORIGIN (decl) != decl)
20345 dwarf2out_abstract_function (origin
20346 ? DECL_ORIGIN (origin)
20347 : DECL_ABSTRACT_ORIGIN (decl));
20349 /* If we're emitting an out-of-line copy of an inline function,
20350 emit info for the abstract instance and set up to refer to it. */
20351 else if (cgraph_function_possibly_inlined_p (decl)
20352 && ! DECL_ABSTRACT (decl)
20353 && ! class_or_namespace_scope_p (context_die)
20354 /* dwarf2out_abstract_function won't emit a die if this is just
20355 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20356 that case, because that works only if we have a die. */
20357 && DECL_INITIAL (decl) != NULL_TREE)
20359 dwarf2out_abstract_function (decl);
20360 set_decl_origin_self (decl);
20363 /* Otherwise we're emitting the primary DIE for this decl. */
20364 else if (debug_info_level > DINFO_LEVEL_TERSE)
20366 /* Before we describe the FUNCTION_DECL itself, make sure that we
20367 have its containing type. */
20368 if (!origin)
20369 origin = decl_class_context (decl);
20370 if (origin != NULL_TREE)
20371 gen_type_die (origin, context_die);
20373 /* And its return type. */
20374 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20376 /* And its virtual context. */
20377 if (DECL_VINDEX (decl) != NULL_TREE)
20378 gen_type_die (DECL_CONTEXT (decl), context_die);
20380 /* Make sure we have a member DIE for decl. */
20381 if (origin != NULL_TREE)
20382 gen_type_die_for_member (origin, decl, context_die);
20384 /* And its containing namespace. */
20385 context_die = declare_in_namespace (decl, context_die);
20388 /* Now output a DIE to represent the function itself. */
20389 if (decl)
20390 gen_subprogram_die (decl, context_die);
20391 break;
20393 case TYPE_DECL:
20394 /* If we are in terse mode, don't generate any DIEs to represent any
20395 actual typedefs. */
20396 if (debug_info_level <= DINFO_LEVEL_TERSE)
20397 break;
20399 /* In the special case of a TYPE_DECL node representing the declaration
20400 of some type tag, if the given TYPE_DECL is marked as having been
20401 instantiated from some other (original) TYPE_DECL node (e.g. one which
20402 was generated within the original definition of an inline function) we
20403 used to generate a special (abbreviated) DW_TAG_structure_type,
20404 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20405 should be actually referencing those DIEs, as variable DIEs with that
20406 type would be emitted already in the abstract origin, so it was always
20407 removed during unused type prunning. Don't add anything in this
20408 case. */
20409 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20410 break;
20412 if (is_redundant_typedef (decl))
20413 gen_type_die (TREE_TYPE (decl), context_die);
20414 else
20415 /* Output a DIE to represent the typedef itself. */
20416 gen_typedef_die (decl, context_die);
20417 break;
20419 case LABEL_DECL:
20420 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20421 gen_label_die (decl, context_die);
20422 break;
20424 case VAR_DECL:
20425 case RESULT_DECL:
20426 /* If we are in terse mode, don't generate any DIEs to represent any
20427 variable declarations or definitions. */
20428 if (debug_info_level <= DINFO_LEVEL_TERSE)
20429 break;
20431 /* Output any DIEs that are needed to specify the type of this data
20432 object. */
20433 if (decl_by_reference_p (decl_or_origin))
20434 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20435 else
20436 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20438 /* And its containing type. */
20439 class_origin = decl_class_context (decl_or_origin);
20440 if (class_origin != NULL_TREE)
20441 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20443 /* And its containing namespace. */
20444 context_die = declare_in_namespace (decl_or_origin, context_die);
20446 /* Now output the DIE to represent the data object itself. This gets
20447 complicated because of the possibility that the VAR_DECL really
20448 represents an inlined instance of a formal parameter for an inline
20449 function. */
20450 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20451 if (ultimate_origin != NULL_TREE
20452 && TREE_CODE (ultimate_origin) == PARM_DECL)
20453 gen_formal_parameter_die (decl, origin,
20454 true /* Emit name attribute. */,
20455 context_die);
20456 else
20457 gen_variable_die (decl, origin, context_die);
20458 break;
20460 case FIELD_DECL:
20461 /* Ignore the nameless fields that are used to skip bits but handle C++
20462 anonymous unions and structs. */
20463 if (DECL_NAME (decl) != NULL_TREE
20464 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20465 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20467 gen_type_die (member_declared_type (decl), context_die);
20468 gen_field_die (decl, context_die);
20470 break;
20472 case PARM_DECL:
20473 if (DECL_BY_REFERENCE (decl_or_origin))
20474 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20475 else
20476 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20477 return gen_formal_parameter_die (decl, origin,
20478 true /* Emit name attribute. */,
20479 context_die);
20481 case NAMESPACE_DECL:
20482 case IMPORTED_DECL:
20483 if (dwarf_version >= 3 || !dwarf_strict)
20484 gen_namespace_die (decl, context_die);
20485 break;
20487 case NAMELIST_DECL:
20488 gen_namelist_decl (DECL_NAME (decl), context_die,
20489 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20490 break;
20492 default:
20493 /* Probably some frontend-internal decl. Assume we don't care. */
20494 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20495 break;
20498 return NULL;
20501 /* Output debug information for global decl DECL. Called from toplev.c after
20502 compilation proper has finished. */
20504 static void
20505 dwarf2out_global_decl (tree decl)
20507 /* Output DWARF2 information for file-scope tentative data object
20508 declarations, file-scope (extern) function declarations (which
20509 had no corresponding body) and file-scope tagged type declarations
20510 and definitions which have not yet been forced out. */
20511 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20512 dwarf2out_decl (decl);
20515 /* Output debug information for type decl DECL. Called from toplev.c
20516 and from language front ends (to record built-in types). */
20517 static void
20518 dwarf2out_type_decl (tree decl, int local)
20520 if (!local)
20521 dwarf2out_decl (decl);
20524 /* Output debug information for imported module or decl DECL.
20525 NAME is non-NULL name in the lexical block if the decl has been renamed.
20526 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20527 that DECL belongs to.
20528 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20529 static void
20530 dwarf2out_imported_module_or_decl_1 (tree decl,
20531 tree name,
20532 tree lexical_block,
20533 dw_die_ref lexical_block_die)
20535 expanded_location xloc;
20536 dw_die_ref imported_die = NULL;
20537 dw_die_ref at_import_die;
20539 if (TREE_CODE (decl) == IMPORTED_DECL)
20541 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20542 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20543 gcc_assert (decl);
20545 else
20546 xloc = expand_location (input_location);
20548 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20550 at_import_die = force_type_die (TREE_TYPE (decl));
20551 /* For namespace N { typedef void T; } using N::T; base_type_die
20552 returns NULL, but DW_TAG_imported_declaration requires
20553 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20554 if (!at_import_die)
20556 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20557 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20558 at_import_die = lookup_type_die (TREE_TYPE (decl));
20559 gcc_assert (at_import_die);
20562 else
20564 at_import_die = lookup_decl_die (decl);
20565 if (!at_import_die)
20567 /* If we're trying to avoid duplicate debug info, we may not have
20568 emitted the member decl for this field. Emit it now. */
20569 if (TREE_CODE (decl) == FIELD_DECL)
20571 tree type = DECL_CONTEXT (decl);
20573 if (TYPE_CONTEXT (type)
20574 && TYPE_P (TYPE_CONTEXT (type))
20575 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20576 DINFO_USAGE_DIR_USE))
20577 return;
20578 gen_type_die_for_member (type, decl,
20579 get_context_die (TYPE_CONTEXT (type)));
20581 if (TREE_CODE (decl) == NAMELIST_DECL)
20582 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20583 get_context_die (DECL_CONTEXT (decl)),
20584 NULL_TREE);
20585 else
20586 at_import_die = force_decl_die (decl);
20590 if (TREE_CODE (decl) == NAMESPACE_DECL)
20592 if (dwarf_version >= 3 || !dwarf_strict)
20593 imported_die = new_die (DW_TAG_imported_module,
20594 lexical_block_die,
20595 lexical_block);
20596 else
20597 return;
20599 else
20600 imported_die = new_die (DW_TAG_imported_declaration,
20601 lexical_block_die,
20602 lexical_block);
20604 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20605 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20606 if (name)
20607 add_AT_string (imported_die, DW_AT_name,
20608 IDENTIFIER_POINTER (name));
20609 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20612 /* Output debug information for imported module or decl DECL.
20613 NAME is non-NULL name in context if the decl has been renamed.
20614 CHILD is true if decl is one of the renamed decls as part of
20615 importing whole module. */
20617 static void
20618 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20619 bool child)
20621 /* dw_die_ref at_import_die; */
20622 dw_die_ref scope_die;
20624 if (debug_info_level <= DINFO_LEVEL_TERSE)
20625 return;
20627 gcc_assert (decl);
20629 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20630 We need decl DIE for reference and scope die. First, get DIE for the decl
20631 itself. */
20633 /* Get the scope die for decl context. Use comp_unit_die for global module
20634 or decl. If die is not found for non globals, force new die. */
20635 if (context
20636 && TYPE_P (context)
20637 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20638 return;
20640 if (!(dwarf_version >= 3 || !dwarf_strict))
20641 return;
20643 scope_die = get_context_die (context);
20645 if (child)
20647 gcc_assert (scope_die->die_child);
20648 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20649 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20650 scope_die = scope_die->die_child;
20653 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20654 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20658 /* Output debug information for namelists. */
20660 static dw_die_ref
20661 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20663 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20664 tree value;
20665 unsigned i;
20667 if (debug_info_level <= DINFO_LEVEL_TERSE)
20668 return NULL;
20670 gcc_assert (scope_die != NULL);
20671 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20672 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20674 /* If there are no item_decls, we have a nondefining namelist, e.g.
20675 with USE association; hence, set DW_AT_declaration. */
20676 if (item_decls == NULL_TREE)
20678 add_AT_flag (nml_die, DW_AT_declaration, 1);
20679 return nml_die;
20682 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20684 nml_item_ref_die = lookup_decl_die (value);
20685 if (!nml_item_ref_die)
20686 nml_item_ref_die = force_decl_die (value);
20688 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20689 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20691 return nml_die;
20695 /* Write the debugging output for DECL. */
20697 void
20698 dwarf2out_decl (tree decl)
20700 dw_die_ref context_die;
20702 /* In LIPO mode, we may output some functions whose type is defined
20703 in another function that will not be output. This can result in
20704 undefined location list symbols in the debug type info.
20705 Here we disable the output of the type info for this case.
20706 It is safe since this function and its debug info should never
20707 be referenced. */
20708 if (L_IPO_COMP_MODE)
20710 tree decl_context, orig_decl;
20712 decl_context = DECL_CONTEXT (decl);
20713 while (decl_context &&
20714 TREE_CODE (decl_context) != TRANSLATION_UNIT_DECL)
20716 struct cgraph_node *node;
20718 /* Refer to cgraph_mark_functions_to_output() in cgraphunit.c,
20719 if cgraph_is_aux_decl_external() is true,
20720 this function will not be output in LIPO mode. */
20721 if (TREE_CODE (decl_context) == FUNCTION_DECL &&
20722 TREE_PUBLIC (decl_context) &&
20723 (node = cgraph_get_node (decl_context)) &&
20724 cgraph_is_aux_decl_external (node))
20725 return;
20727 if (TYPE_P (decl_context))
20729 decl_context = TYPE_CONTEXT (decl_context);
20730 continue;
20733 orig_decl = DECL_ORIGIN (decl_context);
20734 while (orig_decl != DECL_ORIGIN (orig_decl))
20735 orig_decl = DECL_ORIGIN (orig_decl);
20737 decl_context = DECL_CONTEXT (orig_decl);
20741 context_die = comp_unit_die ();
20743 switch (TREE_CODE (decl))
20745 case ERROR_MARK:
20746 return;
20748 case FUNCTION_DECL:
20749 /* What we would really like to do here is to filter out all mere
20750 file-scope declarations of file-scope functions which are never
20751 referenced later within this translation unit (and keep all of ones
20752 that *are* referenced later on) but we aren't clairvoyant, so we have
20753 no idea which functions will be referenced in the future (i.e. later
20754 on within the current translation unit). So here we just ignore all
20755 file-scope function declarations which are not also definitions. If
20756 and when the debugger needs to know something about these functions,
20757 it will have to hunt around and find the DWARF information associated
20758 with the definition of the function.
20760 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20761 nodes represent definitions and which ones represent mere
20762 declarations. We have to check DECL_INITIAL instead. That's because
20763 the C front-end supports some weird semantics for "extern inline"
20764 function definitions. These can get inlined within the current
20765 translation unit (and thus, we need to generate Dwarf info for their
20766 abstract instances so that the Dwarf info for the concrete inlined
20767 instances can have something to refer to) but the compiler never
20768 generates any out-of-lines instances of such things (despite the fact
20769 that they *are* definitions).
20771 The important point is that the C front-end marks these "extern
20772 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20773 them anyway. Note that the C++ front-end also plays some similar games
20774 for inline function definitions appearing within include files which
20775 also contain `#pragma interface' pragmas.
20777 If we are called from dwarf2out_abstract_function output a DIE
20778 anyway. We can end up here this way with early inlining and LTO
20779 where the inlined function is output in a different LTRANS unit
20780 or not at all. */
20781 if (DECL_INITIAL (decl) == NULL_TREE
20782 && ! DECL_ABSTRACT (decl))
20783 return;
20785 /* If we're a nested function, initially use a parent of NULL; if we're
20786 a plain function, this will be fixed up in decls_for_scope. If
20787 we're a method, it will be ignored, since we already have a DIE. */
20788 if (decl_function_context (decl)
20789 /* But if we're in terse mode, we don't care about scope. */
20790 && debug_info_level > DINFO_LEVEL_TERSE)
20791 context_die = NULL;
20792 break;
20794 case VAR_DECL:
20795 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20796 declaration and if the declaration was never even referenced from
20797 within this entire compilation unit. We suppress these DIEs in
20798 order to save space in the .debug section (by eliminating entries
20799 which are probably useless). Note that we must not suppress
20800 block-local extern declarations (whether used or not) because that
20801 would screw-up the debugger's name lookup mechanism and cause it to
20802 miss things which really ought to be in scope at a given point. */
20803 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20804 return;
20806 /* For local statics lookup proper context die. */
20807 if (TREE_STATIC (decl)
20808 && DECL_CONTEXT (decl)
20809 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20810 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20812 /* If we are in terse mode, don't generate any DIEs to represent any
20813 variable declarations or definitions. */
20814 if (debug_info_level <= DINFO_LEVEL_TERSE)
20815 return;
20816 break;
20818 case CONST_DECL:
20819 if (debug_info_level <= DINFO_LEVEL_TERSE)
20820 return;
20821 if (!is_fortran () && !is_ada ())
20822 return;
20823 if (TREE_STATIC (decl) && decl_function_context (decl))
20824 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20825 break;
20827 case NAMESPACE_DECL:
20828 case IMPORTED_DECL:
20829 if (debug_info_level <= DINFO_LEVEL_TERSE)
20830 return;
20831 if (lookup_decl_die (decl) != NULL)
20832 return;
20833 break;
20835 case TYPE_DECL:
20836 /* Don't emit stubs for types unless they are needed by other DIEs. */
20837 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20838 return;
20840 /* Don't bother trying to generate any DIEs to represent any of the
20841 normal built-in types for the language we are compiling. */
20842 if (DECL_IS_BUILTIN (decl))
20843 return;
20845 /* If we are in terse mode, don't generate any DIEs for types. */
20846 if (debug_info_level <= DINFO_LEVEL_TERSE)
20847 return;
20849 /* If we're a function-scope tag, initially use a parent of NULL;
20850 this will be fixed up in decls_for_scope. */
20851 if (decl_function_context (decl))
20852 context_die = NULL;
20854 break;
20856 case NAMELIST_DECL:
20857 break;
20859 default:
20860 return;
20863 gen_decl_die (decl, NULL, context_die);
20866 /* Write the debugging output for DECL. */
20868 static void
20869 dwarf2out_function_decl (tree decl)
20871 dwarf2out_decl (decl);
20872 call_arg_locations = NULL;
20873 call_arg_loc_last = NULL;
20874 call_site_count = -1;
20875 tail_call_site_count = -1;
20876 block_map.release ();
20877 htab_empty (decl_loc_table);
20878 htab_empty (cached_dw_loc_list_table);
20881 /* Output a marker (i.e. a label) for the beginning of the generated code for
20882 a lexical block. */
20884 static void
20885 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20886 unsigned int blocknum)
20888 switch_to_section (current_function_section ());
20889 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20892 /* Output a marker (i.e. a label) for the end of the generated code for a
20893 lexical block. */
20895 static void
20896 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20898 switch_to_section (current_function_section ());
20899 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20902 /* Returns nonzero if it is appropriate not to emit any debugging
20903 information for BLOCK, because it doesn't contain any instructions.
20905 Don't allow this for blocks with nested functions or local classes
20906 as we would end up with orphans, and in the presence of scheduling
20907 we may end up calling them anyway. */
20909 static bool
20910 dwarf2out_ignore_block (const_tree block)
20912 tree decl;
20913 unsigned int i;
20915 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20916 if (TREE_CODE (decl) == FUNCTION_DECL
20917 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20918 return 0;
20919 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20921 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20922 if (TREE_CODE (decl) == FUNCTION_DECL
20923 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20924 return 0;
20927 return 1;
20930 /* Hash table routines for file_hash. */
20932 static int
20933 file_table_eq (const void *p1_p, const void *p2_p)
20935 const struct dwarf_file_data *const p1 =
20936 (const struct dwarf_file_data *) p1_p;
20937 const char *const p2 = (const char *) p2_p;
20938 return filename_cmp (p1->filename, p2) == 0;
20941 static hashval_t
20942 file_table_hash (const void *p_p)
20944 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20945 return htab_hash_string (p->filename);
20948 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20949 dwarf2out.c) and return its "index". The index of each (known) filename is
20950 just a unique number which is associated with only that one filename. We
20951 need such numbers for the sake of generating labels (in the .debug_sfnames
20952 section) and references to those files numbers (in the .debug_srcinfo
20953 and.debug_macinfo sections). If the filename given as an argument is not
20954 found in our current list, add it to the list and assign it the next
20955 available unique index number. In order to speed up searches, we remember
20956 the index of the filename was looked up last. This handles the majority of
20957 all searches. */
20959 static struct dwarf_file_data *
20960 lookup_filename (const char *file_name)
20962 void ** slot;
20963 struct dwarf_file_data * created;
20965 /* Check to see if the file name that was searched on the previous
20966 call matches this file name. If so, return the index. */
20967 if (file_table_last_lookup
20968 && (file_name == file_table_last_lookup->filename
20969 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20970 return file_table_last_lookup;
20972 /* Didn't match the previous lookup, search the table. */
20973 slot = htab_find_slot_with_hash (file_table, file_name,
20974 htab_hash_string (file_name), INSERT);
20975 if (*slot)
20976 return (struct dwarf_file_data *) *slot;
20978 created = ggc_alloc_dwarf_file_data ();
20979 created->filename = file_name;
20980 created->emitted_number = 0;
20981 *slot = created;
20982 return created;
20985 /* If the assembler will construct the file table, then translate the compiler
20986 internal file table number into the assembler file table number, and emit
20987 a .file directive if we haven't already emitted one yet. The file table
20988 numbers are different because we prune debug info for unused variables and
20989 types, which may include filenames. */
20991 static int
20992 maybe_emit_file (struct dwarf_file_data * fd)
20994 if (! fd->emitted_number)
20996 if (last_emitted_file)
20997 fd->emitted_number = last_emitted_file->emitted_number + 1;
20998 else
20999 fd->emitted_number = 1;
21000 last_emitted_file = fd;
21002 if (DWARF2_ASM_LINE_DEBUG_INFO)
21004 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21005 output_quoted_string (asm_out_file,
21006 remap_debug_filename (fd->filename));
21007 fputc ('\n', asm_out_file);
21011 return fd->emitted_number;
21014 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21015 That generation should happen after function debug info has been
21016 generated. The value of the attribute is the constant value of ARG. */
21018 static void
21019 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21021 die_arg_entry entry;
21023 if (!die || !arg)
21024 return;
21026 if (!tmpl_value_parm_die_table)
21027 vec_alloc (tmpl_value_parm_die_table, 32);
21029 entry.die = die;
21030 entry.arg = arg;
21031 vec_safe_push (tmpl_value_parm_die_table, entry);
21034 /* Return TRUE if T is an instance of generic type, FALSE
21035 otherwise. */
21037 static bool
21038 generic_type_p (tree t)
21040 if (t == NULL_TREE || !TYPE_P (t))
21041 return false;
21042 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21045 /* Schedule the generation of the generic parameter dies for the
21046 instance of generic type T. The proper generation itself is later
21047 done by gen_scheduled_generic_parms_dies. */
21049 static void
21050 schedule_generic_params_dies_gen (tree t)
21052 if (!generic_type_p (t))
21053 return;
21055 if (!generic_type_instances)
21056 vec_alloc (generic_type_instances, 256);
21058 vec_safe_push (generic_type_instances, t);
21061 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21062 by append_entry_to_tmpl_value_parm_die_table. This function must
21063 be called after function DIEs have been generated. */
21065 static void
21066 gen_remaining_tmpl_value_param_die_attribute (void)
21068 if (tmpl_value_parm_die_table)
21070 unsigned i;
21071 die_arg_entry *e;
21073 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21074 tree_add_const_value_attribute (e->die, e->arg);
21078 /* Generate generic parameters DIEs for instances of generic types
21079 that have been previously scheduled by
21080 schedule_generic_params_dies_gen. This function must be called
21081 after all the types of the CU have been laid out. */
21083 static void
21084 gen_scheduled_generic_parms_dies (void)
21086 unsigned i;
21087 tree t;
21089 if (!generic_type_instances)
21090 return;
21092 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21093 if (COMPLETE_TYPE_P (t))
21094 gen_generic_params_dies (t);
21098 /* Replace DW_AT_name for the decl with name. */
21100 static void
21101 dwarf2out_set_name (tree decl, tree name)
21103 dw_die_ref die;
21104 dw_attr_ref attr;
21105 const char *dname;
21107 die = TYPE_SYMTAB_DIE (decl);
21108 if (!die)
21109 return;
21111 dname = dwarf2_name (name, 0);
21112 if (!dname)
21113 return;
21115 attr = get_AT (die, DW_AT_name);
21116 if (attr)
21118 struct indirect_string_node *node;
21120 node = find_AT_string (dname);
21121 /* replace the string. */
21122 attr->dw_attr_val.v.val_str = node;
21125 else
21126 add_name_attribute (die, dname);
21129 /* True if before or during processing of the first function being emitted. */
21130 static bool in_first_function_p = true;
21131 /* True if loc_note during dwarf2out_var_location call might still be
21132 before first real instruction at address equal to .Ltext0. */
21133 static bool maybe_at_text_label_p = true;
21134 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21135 static unsigned int first_loclabel_num_not_at_text_label;
21137 /* Called by the final INSN scan whenever we see a var location. We
21138 use it to drop labels in the right places, and throw the location in
21139 our lookup table. */
21141 static void
21142 dwarf2out_var_location (rtx loc_note)
21144 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21145 struct var_loc_node *newloc;
21146 rtx next_real, next_note;
21147 static const char *last_label;
21148 static const char *last_postcall_label;
21149 static bool last_in_cold_section_p;
21150 static rtx expected_next_loc_note;
21151 tree decl;
21152 bool var_loc_p;
21154 if (!NOTE_P (loc_note))
21156 if (CALL_P (loc_note))
21158 call_site_count++;
21159 if (SIBLING_CALL_P (loc_note))
21160 tail_call_site_count++;
21162 return;
21165 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21166 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21167 return;
21169 /* Optimize processing a large consecutive sequence of location
21170 notes so we don't spend too much time in next_real_insn. If the
21171 next insn is another location note, remember the next_real_insn
21172 calculation for next time. */
21173 next_real = cached_next_real_insn;
21174 if (next_real)
21176 if (expected_next_loc_note != loc_note)
21177 next_real = NULL_RTX;
21180 next_note = NEXT_INSN (loc_note);
21181 if (! next_note
21182 || INSN_DELETED_P (next_note)
21183 || ! NOTE_P (next_note)
21184 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21185 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21186 next_note = NULL_RTX;
21188 if (! next_real)
21189 next_real = next_real_insn (loc_note);
21191 if (next_note)
21193 expected_next_loc_note = next_note;
21194 cached_next_real_insn = next_real;
21196 else
21197 cached_next_real_insn = NULL_RTX;
21199 /* If there are no instructions which would be affected by this note,
21200 don't do anything. */
21201 if (var_loc_p
21202 && next_real == NULL_RTX
21203 && !NOTE_DURING_CALL_P (loc_note))
21204 return;
21206 if (next_real == NULL_RTX)
21207 next_real = get_last_insn ();
21209 /* If there were any real insns between note we processed last time
21210 and this note (or if it is the first note), clear
21211 last_{,postcall_}label so that they are not reused this time. */
21212 if (last_var_location_insn == NULL_RTX
21213 || last_var_location_insn != next_real
21214 || last_in_cold_section_p != in_cold_section_p)
21216 last_label = NULL;
21217 last_postcall_label = NULL;
21220 if (var_loc_p)
21222 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21223 newloc = add_var_loc_to_decl (decl, loc_note,
21224 NOTE_DURING_CALL_P (loc_note)
21225 ? last_postcall_label : last_label);
21226 if (newloc == NULL)
21227 return;
21229 else
21231 decl = NULL_TREE;
21232 newloc = NULL;
21235 /* If there were no real insns between note we processed last time
21236 and this note, use the label we emitted last time. Otherwise
21237 create a new label and emit it. */
21238 if (last_label == NULL)
21240 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21241 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21242 loclabel_num++;
21243 last_label = ggc_strdup (loclabel);
21244 /* See if loclabel might be equal to .Ltext0. If yes,
21245 bump first_loclabel_num_not_at_text_label. */
21246 if (!have_multiple_function_sections
21247 && in_first_function_p
21248 && maybe_at_text_label_p)
21250 static rtx last_start;
21251 rtx insn;
21252 for (insn = loc_note; insn; insn = previous_insn (insn))
21253 if (insn == last_start)
21254 break;
21255 else if (!NONDEBUG_INSN_P (insn))
21256 continue;
21257 else
21259 rtx body = PATTERN (insn);
21260 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21261 continue;
21262 /* Inline asm could occupy zero bytes. */
21263 else if (GET_CODE (body) == ASM_INPUT
21264 || asm_noperands (body) >= 0)
21265 continue;
21266 #ifdef HAVE_attr_length
21267 else if (get_attr_min_length (insn) == 0)
21268 continue;
21269 #endif
21270 else
21272 /* Assume insn has non-zero length. */
21273 maybe_at_text_label_p = false;
21274 break;
21277 if (maybe_at_text_label_p)
21279 last_start = loc_note;
21280 first_loclabel_num_not_at_text_label = loclabel_num;
21285 if (!var_loc_p)
21287 struct call_arg_loc_node *ca_loc
21288 = ggc_alloc_cleared_call_arg_loc_node ();
21289 rtx prev = prev_real_insn (loc_note), x;
21290 ca_loc->call_arg_loc_note = loc_note;
21291 ca_loc->next = NULL;
21292 ca_loc->label = last_label;
21293 gcc_assert (prev
21294 && (CALL_P (prev)
21295 || (NONJUMP_INSN_P (prev)
21296 && GET_CODE (PATTERN (prev)) == SEQUENCE
21297 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21298 if (!CALL_P (prev))
21299 prev = XVECEXP (PATTERN (prev), 0, 0);
21300 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21301 x = get_call_rtx_from (PATTERN (prev));
21302 if (x)
21304 x = XEXP (XEXP (x, 0), 0);
21305 if (GET_CODE (x) == SYMBOL_REF
21306 && SYMBOL_REF_DECL (x)
21307 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21308 ca_loc->symbol_ref = x;
21310 ca_loc->block = insn_scope (prev);
21311 if (call_arg_locations)
21312 call_arg_loc_last->next = ca_loc;
21313 else
21314 call_arg_locations = ca_loc;
21315 call_arg_loc_last = ca_loc;
21317 else if (!NOTE_DURING_CALL_P (loc_note))
21318 newloc->label = last_label;
21319 else
21321 if (!last_postcall_label)
21323 sprintf (loclabel, "%s-1", last_label);
21324 last_postcall_label = ggc_strdup (loclabel);
21326 newloc->label = last_postcall_label;
21329 last_var_location_insn = next_real;
21330 last_in_cold_section_p = in_cold_section_p;
21333 /* Note in one location list that text section has changed. */
21335 static int
21336 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21338 var_loc_list *list = (var_loc_list *) *slot;
21339 if (list->first)
21340 list->last_before_switch
21341 = list->last->next ? list->last->next : list->last;
21342 return 1;
21345 /* Note in all location lists that text section has changed. */
21347 static void
21348 var_location_switch_text_section (void)
21350 if (decl_loc_table == NULL)
21351 return;
21353 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21356 /* Create a new line number table. */
21358 static dw_line_info_table *
21359 new_line_info_table (void)
21361 dw_line_info_table *table;
21363 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21364 table->file_num = 1;
21365 table->line_num = 1;
21366 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21368 return table;
21371 /* Lookup the "current" table into which we emit line info, so
21372 that we don't have to do it for every source line. */
21374 static void
21375 set_cur_line_info_table (section *sec)
21377 dw_line_info_table *table;
21379 if (sec == text_section)
21380 table = text_section_line_info;
21381 else if (sec == cold_text_section)
21383 table = cold_text_section_line_info;
21384 if (!table)
21386 cold_text_section_line_info = table = new_line_info_table ();
21387 table->end_label = cold_end_label;
21390 else
21392 const char *end_label;
21394 if (flag_reorder_blocks_and_partition)
21396 if (in_cold_section_p)
21397 end_label = crtl->subsections.cold_section_end_label;
21398 else
21399 end_label = crtl->subsections.hot_section_end_label;
21401 else
21403 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21404 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21405 FUNC_LABEL_ID (cfun));
21406 end_label = ggc_strdup (label);
21409 table = new_line_info_table ();
21410 table->end_label = end_label;
21412 vec_safe_push (separate_line_info, table);
21415 if (DWARF2_ASM_LINE_DEBUG_INFO)
21416 table->is_stmt = (cur_line_info_table
21417 ? cur_line_info_table->is_stmt
21418 : DWARF_LINE_DEFAULT_IS_STMT_START);
21419 cur_line_info_table = table;
21423 /* We need to reset the locations at the beginning of each
21424 function. We can't do this in the end_function hook, because the
21425 declarations that use the locations won't have been output when
21426 that hook is called. Also compute have_multiple_function_sections here. */
21428 static void
21429 dwarf2out_begin_function (tree fun)
21431 section *sec = function_section (fun);
21433 if (sec != text_section)
21434 have_multiple_function_sections = true;
21436 if (flag_reorder_blocks_and_partition && !cold_text_section)
21438 gcc_assert (current_function_decl == fun);
21439 cold_text_section = unlikely_text_section ();
21440 switch_to_section (cold_text_section);
21441 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21442 switch_to_section (sec);
21445 dwarf2out_note_section_used ();
21446 call_site_count = 0;
21447 tail_call_site_count = 0;
21449 set_cur_line_info_table (sec);
21452 /* Helper function of dwarf2out_end_function, called only after emitting
21453 the very first function into assembly. Check if some .debug_loc range
21454 might end with a .LVL* label that could be equal to .Ltext0.
21455 In that case we must force using absolute addresses in .debug_loc ranges,
21456 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21457 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21458 list terminator.
21459 Set have_multiple_function_sections to true in that case and
21460 terminate htab traversal. */
21462 static int
21463 find_empty_loc_ranges_at_text_label (void **slot, void *)
21465 var_loc_list *entry;
21466 struct var_loc_node *node;
21468 entry = (var_loc_list *) *slot;
21469 node = entry->first;
21470 if (node && node->next && node->next->label)
21472 unsigned int i;
21473 const char *label = node->next->label;
21474 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21476 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21478 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21479 if (strcmp (label, loclabel) == 0)
21481 have_multiple_function_sections = true;
21482 return 0;
21486 return 1;
21489 /* Hook called after emitting a function into assembly.
21490 This does something only for the very first function emitted. */
21492 static void
21493 dwarf2out_end_function (unsigned int)
21495 if (in_first_function_p
21496 && !have_multiple_function_sections
21497 && first_loclabel_num_not_at_text_label
21498 && decl_loc_table)
21499 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21500 NULL);
21501 in_first_function_p = false;
21502 maybe_at_text_label_p = false;
21505 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21507 static void
21508 push_dw_line_info_entry (dw_line_info_table *table,
21509 enum dw_line_info_opcode opcode, unsigned int val)
21511 dw_line_info_entry e;
21512 e.opcode = opcode;
21513 e.val = val;
21514 vec_safe_push (table->entries, e);
21517 /* Output a label to mark the beginning of a source code line entry
21518 and record information relating to this source line, in
21519 'line_info_table' for later output of the .debug_line section. */
21520 /* ??? The discriminator parameter ought to be unsigned. */
21522 static void
21523 dwarf2out_source_line (unsigned int line, const char *filename,
21524 int discriminator, bool is_stmt)
21526 unsigned int file_num;
21527 dw_line_info_table *table;
21529 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21530 return;
21532 /* The discriminator column was added in dwarf4. Simplify the below
21533 by simply removing it if we're not supposed to output it. */
21534 if (dwarf_version < 4 && dwarf_strict)
21535 discriminator = 0;
21537 table = cur_line_info_table;
21538 file_num = maybe_emit_file (lookup_filename (filename));
21540 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21541 the debugger has used the second (possibly duplicate) line number
21542 at the beginning of the function to mark the end of the prologue.
21543 We could eliminate any other duplicates within the function. For
21544 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21545 that second line number entry. */
21546 /* Recall that this end-of-prologue indication is *not* the same thing
21547 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21548 to which the hook corresponds, follows the last insn that was
21549 emitted by gen_prologue. What we need is to precede the first insn
21550 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21551 insn that corresponds to something the user wrote. These may be
21552 very different locations once scheduling is enabled. */
21554 if (0 && file_num == table->file_num
21555 && line == table->line_num
21556 && discriminator == table->discrim_num
21557 && is_stmt == table->is_stmt)
21558 return;
21560 switch_to_section (current_function_section ());
21562 /* If requested, emit something human-readable. */
21563 if (flag_debug_asm)
21564 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21566 if (DWARF2_ASM_LINE_DEBUG_INFO)
21568 /* Emit the .loc directive understood by GNU as. */
21569 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21570 file_num, line, is_stmt, discriminator */
21571 fputs ("\t.loc ", asm_out_file);
21572 fprint_ul (asm_out_file, file_num);
21573 putc (' ', asm_out_file);
21574 fprint_ul (asm_out_file, line);
21575 putc (' ', asm_out_file);
21576 putc ('0', asm_out_file);
21578 if (is_stmt != table->is_stmt)
21580 fputs (" is_stmt ", asm_out_file);
21581 putc (is_stmt ? '1' : '0', asm_out_file);
21583 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21585 gcc_assert (discriminator > 0);
21586 fputs (" discriminator ", asm_out_file);
21587 fprint_ul (asm_out_file, (unsigned long) discriminator);
21589 putc ('\n', asm_out_file);
21591 else
21593 unsigned int label_num = ++line_info_label_num;
21595 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21597 push_dw_line_info_entry (table, LI_set_address, label_num);
21598 if (file_num != table->file_num)
21599 push_dw_line_info_entry (table, LI_set_file, file_num);
21600 if (discriminator != table->discrim_num)
21601 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21602 if (is_stmt != table->is_stmt)
21603 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21604 push_dw_line_info_entry (table, LI_set_line, line);
21607 table->file_num = file_num;
21608 table->line_num = line;
21609 table->discrim_num = discriminator;
21610 table->is_stmt = is_stmt;
21611 table->in_use = true;
21614 /* Record the beginning of a new source file. */
21616 static void
21617 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21619 if (flag_eliminate_dwarf2_dups)
21621 /* Record the beginning of the file for break_out_includes. */
21622 dw_die_ref bincl_die;
21624 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21625 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21628 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21630 macinfo_entry e;
21631 e.code = DW_MACINFO_start_file;
21632 e.lineno = lineno;
21633 e.info = ggc_strdup (filename);
21634 vec_safe_push (macinfo_table, e);
21638 /* Record the end of a source file. */
21640 static void
21641 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21643 if (flag_eliminate_dwarf2_dups)
21644 /* Record the end of the file for break_out_includes. */
21645 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21647 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21649 macinfo_entry e;
21650 e.code = DW_MACINFO_end_file;
21651 e.lineno = lineno;
21652 e.info = NULL;
21653 vec_safe_push (macinfo_table, e);
21657 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21658 the tail part of the directive line, i.e. the part which is past the
21659 initial whitespace, #, whitespace, directive-name, whitespace part. */
21661 static void
21662 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21663 const char *buffer ATTRIBUTE_UNUSED)
21665 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21667 macinfo_entry e;
21668 /* Insert a dummy first entry to be able to optimize the whole
21669 predefined macro block using DW_MACRO_GNU_transparent_include. */
21670 if (macinfo_table->is_empty () && lineno <= 1)
21672 e.code = 0;
21673 e.lineno = 0;
21674 e.info = NULL;
21675 vec_safe_push (macinfo_table, e);
21677 e.code = DW_MACINFO_define;
21678 e.lineno = lineno;
21679 e.info = ggc_strdup (buffer);
21680 vec_safe_push (macinfo_table, e);
21684 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21685 the tail part of the directive line, i.e. the part which is past the
21686 initial whitespace, #, whitespace, directive-name, whitespace part. */
21688 static void
21689 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21690 const char *buffer ATTRIBUTE_UNUSED)
21692 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21694 macinfo_entry e;
21695 /* Insert a dummy first entry to be able to optimize the whole
21696 predefined macro block using DW_MACRO_GNU_transparent_include. */
21697 if (macinfo_table->is_empty () && lineno <= 1)
21699 e.code = 0;
21700 e.lineno = 0;
21701 e.info = NULL;
21702 vec_safe_push (macinfo_table, e);
21704 e.code = DW_MACINFO_undef;
21705 e.lineno = lineno;
21706 e.info = ggc_strdup (buffer);
21707 vec_safe_push (macinfo_table, e);
21711 /* Helpers to manipulate hash table of CUs. */
21713 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21715 typedef macinfo_entry value_type;
21716 typedef macinfo_entry compare_type;
21717 static inline hashval_t hash (const value_type *);
21718 static inline bool equal (const value_type *, const compare_type *);
21721 inline hashval_t
21722 macinfo_entry_hasher::hash (const value_type *entry)
21724 return htab_hash_string (entry->info);
21727 inline bool
21728 macinfo_entry_hasher::equal (const value_type *entry1,
21729 const compare_type *entry2)
21731 return !strcmp (entry1->info, entry2->info);
21734 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21736 /* Output a single .debug_macinfo entry. */
21738 static void
21739 output_macinfo_op (macinfo_entry *ref)
21741 int file_num;
21742 size_t len;
21743 struct indirect_string_node *node;
21744 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21745 struct dwarf_file_data *fd;
21747 switch (ref->code)
21749 case DW_MACINFO_start_file:
21750 fd = lookup_filename (ref->info);
21751 file_num = maybe_emit_file (fd);
21752 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21753 dw2_asm_output_data_uleb128 (ref->lineno,
21754 "Included from line number %lu",
21755 (unsigned long) ref->lineno);
21756 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21757 break;
21758 case DW_MACINFO_end_file:
21759 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21760 break;
21761 case DW_MACINFO_define:
21762 case DW_MACINFO_undef:
21763 len = strlen (ref->info) + 1;
21764 if (!dwarf_strict
21765 && len > DWARF_OFFSET_SIZE
21766 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21767 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21769 ref->code = ref->code == DW_MACINFO_define
21770 ? DW_MACRO_GNU_define_indirect
21771 : DW_MACRO_GNU_undef_indirect;
21772 output_macinfo_op (ref);
21773 return;
21775 dw2_asm_output_data (1, ref->code,
21776 ref->code == DW_MACINFO_define
21777 ? "Define macro" : "Undefine macro");
21778 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21779 (unsigned long) ref->lineno);
21780 dw2_asm_output_nstring (ref->info, -1, "The macro");
21781 break;
21782 case DW_MACRO_GNU_define_indirect:
21783 case DW_MACRO_GNU_undef_indirect:
21784 node = find_AT_string (ref->info);
21785 gcc_assert (node
21786 && ((node->form == DW_FORM_strp)
21787 || (node->form == DW_FORM_GNU_str_index)));
21788 dw2_asm_output_data (1, ref->code,
21789 ref->code == DW_MACRO_GNU_define_indirect
21790 ? "Define macro indirect"
21791 : "Undefine macro indirect");
21792 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21793 (unsigned long) ref->lineno);
21794 if (node->form == DW_FORM_strp)
21795 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21796 debug_str_section, "The macro: \"%s\"",
21797 ref->info);
21798 else
21799 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21800 ref->info);
21801 break;
21802 case DW_MACRO_GNU_transparent_include:
21803 dw2_asm_output_data (1, ref->code, "Transparent include");
21804 ASM_GENERATE_INTERNAL_LABEL (label,
21805 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21806 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21807 break;
21808 default:
21809 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21810 ASM_COMMENT_START, (unsigned long) ref->code);
21811 break;
21815 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21816 other compilation unit .debug_macinfo sections. IDX is the first
21817 index of a define/undef, return the number of ops that should be
21818 emitted in a comdat .debug_macinfo section and emit
21819 a DW_MACRO_GNU_transparent_include entry referencing it.
21820 If the define/undef entry should be emitted normally, return 0. */
21822 static unsigned
21823 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21824 macinfo_hash_type *macinfo_htab)
21826 macinfo_entry *first, *second, *cur, *inc;
21827 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21828 unsigned char checksum[16];
21829 struct md5_ctx ctx;
21830 char *grp_name, *tail;
21831 const char *base;
21832 unsigned int i, count, encoded_filename_len, linebuf_len;
21833 macinfo_entry **slot;
21835 first = &(*macinfo_table)[idx];
21836 second = &(*macinfo_table)[idx + 1];
21838 /* Optimize only if there are at least two consecutive define/undef ops,
21839 and either all of them are before first DW_MACINFO_start_file
21840 with lineno {0,1} (i.e. predefined macro block), or all of them are
21841 in some included header file. */
21842 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21843 return 0;
21844 if (vec_safe_is_empty (files))
21846 if (first->lineno > 1 || second->lineno > 1)
21847 return 0;
21849 else if (first->lineno == 0)
21850 return 0;
21852 /* Find the last define/undef entry that can be grouped together
21853 with first and at the same time compute md5 checksum of their
21854 codes, linenumbers and strings. */
21855 md5_init_ctx (&ctx);
21856 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21857 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21858 break;
21859 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21860 break;
21861 else
21863 unsigned char code = cur->code;
21864 md5_process_bytes (&code, 1, &ctx);
21865 checksum_uleb128 (cur->lineno, &ctx);
21866 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21868 md5_finish_ctx (&ctx, checksum);
21869 count = i - idx;
21871 /* From the containing include filename (if any) pick up just
21872 usable characters from its basename. */
21873 if (vec_safe_is_empty (files))
21874 base = "";
21875 else
21876 base = lbasename (files->last ().info);
21877 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21878 if (ISIDNUM (base[i]) || base[i] == '.')
21879 encoded_filename_len++;
21880 /* Count . at the end. */
21881 if (encoded_filename_len)
21882 encoded_filename_len++;
21884 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21885 linebuf_len = strlen (linebuf);
21887 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21888 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21889 + 16 * 2 + 1);
21890 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21891 tail = grp_name + 4;
21892 if (encoded_filename_len)
21894 for (i = 0; base[i]; i++)
21895 if (ISIDNUM (base[i]) || base[i] == '.')
21896 *tail++ = base[i];
21897 *tail++ = '.';
21899 memcpy (tail, linebuf, linebuf_len);
21900 tail += linebuf_len;
21901 *tail++ = '.';
21902 for (i = 0; i < 16; i++)
21903 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21905 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21906 in the empty vector entry before the first define/undef. */
21907 inc = &(*macinfo_table)[idx - 1];
21908 inc->code = DW_MACRO_GNU_transparent_include;
21909 inc->lineno = 0;
21910 inc->info = ggc_strdup (grp_name);
21911 if (!macinfo_htab->is_created ())
21912 macinfo_htab->create (10);
21913 /* Avoid emitting duplicates. */
21914 slot = macinfo_htab->find_slot (inc, INSERT);
21915 if (*slot != NULL)
21917 inc->code = 0;
21918 inc->info = NULL;
21919 /* If such an entry has been used before, just emit
21920 a DW_MACRO_GNU_transparent_include op. */
21921 inc = *slot;
21922 output_macinfo_op (inc);
21923 /* And clear all macinfo_entry in the range to avoid emitting them
21924 in the second pass. */
21925 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21927 cur->code = 0;
21928 cur->info = NULL;
21931 else
21933 *slot = inc;
21934 inc->lineno = macinfo_htab->elements ();
21935 output_macinfo_op (inc);
21937 return count;
21940 /* Save any strings needed by the macinfo table in the debug str
21941 table. All strings must be collected into the table by the time
21942 index_string is called. */
21944 static void
21945 save_macinfo_strings (void)
21947 unsigned len;
21948 unsigned i;
21949 macinfo_entry *ref;
21951 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21953 switch (ref->code)
21955 /* Match the logic in output_macinfo_op to decide on
21956 indirect strings. */
21957 case DW_MACINFO_define:
21958 case DW_MACINFO_undef:
21959 len = strlen (ref->info) + 1;
21960 if (!dwarf_strict
21961 && len > DWARF_OFFSET_SIZE
21962 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21963 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21964 set_indirect_string (find_AT_string (ref->info));
21965 break;
21966 case DW_MACRO_GNU_define_indirect:
21967 case DW_MACRO_GNU_undef_indirect:
21968 set_indirect_string (find_AT_string (ref->info));
21969 break;
21970 default:
21971 break;
21976 /* Output macinfo section(s). */
21978 static void
21979 output_macinfo (void)
21981 unsigned i;
21982 unsigned long length = vec_safe_length (macinfo_table);
21983 macinfo_entry *ref;
21984 vec<macinfo_entry, va_gc> *files = NULL;
21985 macinfo_hash_type macinfo_htab;
21987 if (! length)
21988 return;
21990 /* output_macinfo* uses these interchangeably. */
21991 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21992 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21993 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21994 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21996 /* For .debug_macro emit the section header. */
21997 if (!dwarf_strict)
21999 dw2_asm_output_data (2, 4, "DWARF macro version number");
22000 if (DWARF_OFFSET_SIZE == 8)
22001 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22002 else
22003 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22004 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22005 (!dwarf_split_debug_info ? debug_line_section_label
22006 : debug_skeleton_line_section_label),
22007 debug_line_section, NULL);
22010 /* In the first loop, it emits the primary .debug_macinfo section
22011 and after each emitted op the macinfo_entry is cleared.
22012 If a longer range of define/undef ops can be optimized using
22013 DW_MACRO_GNU_transparent_include, the
22014 DW_MACRO_GNU_transparent_include op is emitted and kept in
22015 the vector before the first define/undef in the range and the
22016 whole range of define/undef ops is not emitted and kept. */
22017 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22019 switch (ref->code)
22021 case DW_MACINFO_start_file:
22022 vec_safe_push (files, *ref);
22023 break;
22024 case DW_MACINFO_end_file:
22025 if (!vec_safe_is_empty (files))
22026 files->pop ();
22027 break;
22028 case DW_MACINFO_define:
22029 case DW_MACINFO_undef:
22030 if (!dwarf_strict
22031 && HAVE_COMDAT_GROUP
22032 && vec_safe_length (files) != 1
22033 && i > 0
22034 && i + 1 < length
22035 && (*macinfo_table)[i - 1].code == 0)
22037 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22038 if (count)
22040 i += count - 1;
22041 continue;
22044 break;
22045 case 0:
22046 /* A dummy entry may be inserted at the beginning to be able
22047 to optimize the whole block of predefined macros. */
22048 if (i == 0)
22049 continue;
22050 default:
22051 break;
22053 output_macinfo_op (ref);
22054 ref->info = NULL;
22055 ref->code = 0;
22058 if (!macinfo_htab.is_created ())
22059 return;
22061 macinfo_htab.dispose ();
22063 /* If any DW_MACRO_GNU_transparent_include were used, on those
22064 DW_MACRO_GNU_transparent_include entries terminate the
22065 current chain and switch to a new comdat .debug_macinfo
22066 section and emit the define/undef entries within it. */
22067 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22068 switch (ref->code)
22070 case 0:
22071 continue;
22072 case DW_MACRO_GNU_transparent_include:
22074 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22075 tree comdat_key = get_identifier (ref->info);
22076 /* Terminate the previous .debug_macinfo section. */
22077 dw2_asm_output_data (1, 0, "End compilation unit");
22078 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22079 SECTION_DEBUG
22080 | SECTION_LINKONCE,
22081 comdat_key);
22082 ASM_GENERATE_INTERNAL_LABEL (label,
22083 DEBUG_MACRO_SECTION_LABEL,
22084 ref->lineno);
22085 ASM_OUTPUT_LABEL (asm_out_file, label);
22086 ref->code = 0;
22087 ref->info = NULL;
22088 dw2_asm_output_data (2, 4, "DWARF macro version number");
22089 if (DWARF_OFFSET_SIZE == 8)
22090 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22091 else
22092 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22094 break;
22095 case DW_MACINFO_define:
22096 case DW_MACINFO_undef:
22097 output_macinfo_op (ref);
22098 ref->code = 0;
22099 ref->info = NULL;
22100 break;
22101 default:
22102 gcc_unreachable ();
22106 /* Set up for Dwarf output at the start of compilation. */
22108 static void
22109 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22111 /* Allocate the file_table. */
22112 file_table = htab_create_ggc (50, file_table_hash,
22113 file_table_eq, NULL);
22115 /* Allocate the decl_die_table. */
22116 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22117 decl_die_table_eq, NULL);
22119 /* Allocate the decl_loc_table. */
22120 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22121 decl_loc_table_eq, NULL);
22123 /* Allocate the cached_dw_loc_list_table. */
22124 cached_dw_loc_list_table
22125 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22126 cached_dw_loc_list_table_eq, NULL);
22128 /* Allocate the initial hunk of the decl_scope_table. */
22129 vec_alloc (decl_scope_table, 256);
22131 /* Allocate the initial hunk of the abbrev_die_table. */
22132 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22133 (ABBREV_DIE_TABLE_INCREMENT);
22134 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22135 /* Zero-th entry is allocated, but unused. */
22136 abbrev_die_table_in_use = 1;
22138 /* Allocate the pubtypes and pubnames vectors. */
22139 vec_alloc (pubname_table, 32);
22140 vec_alloc (pubtype_table, 32);
22142 vec_alloc (incomplete_types, 64);
22144 vec_alloc (used_rtx_array, 32);
22146 if (!dwarf_split_debug_info)
22148 debug_info_section = get_section (DEBUG_INFO_SECTION,
22149 SECTION_DEBUG, NULL);
22150 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22151 SECTION_DEBUG, NULL);
22152 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22153 SECTION_DEBUG, NULL);
22155 else
22157 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22158 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22159 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22160 SECTION_DEBUG | SECTION_EXCLUDE,
22161 NULL);
22162 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22163 SECTION_DEBUG, NULL);
22164 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22165 SECTION_DEBUG, NULL);
22166 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22167 SECTION_DEBUG, NULL);
22168 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22169 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22171 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22172 the main .o, but the skeleton_line goes into the split off dwo. */
22173 debug_skeleton_line_section
22174 = get_section (DEBUG_DWO_LINE_SECTION,
22175 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22176 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22177 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22178 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22179 SECTION_DEBUG | SECTION_EXCLUDE,
22180 NULL);
22181 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22182 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22183 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22184 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22185 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22186 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22188 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22189 SECTION_DEBUG, NULL);
22190 debug_macinfo_section = get_section (dwarf_strict
22191 ? DEBUG_MACINFO_SECTION
22192 : DEBUG_MACRO_SECTION,
22193 DEBUG_MACRO_SECTION_FLAGS, NULL);
22194 debug_line_section = get_section (DEBUG_LINE_SECTION,
22195 SECTION_DEBUG, NULL);
22196 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22197 SECTION_DEBUG, NULL);
22198 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22199 SECTION_DEBUG, NULL);
22200 debug_str_section = get_section (DEBUG_STR_SECTION,
22201 DEBUG_STR_SECTION_FLAGS, NULL);
22202 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22203 SECTION_DEBUG, NULL);
22204 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22205 SECTION_DEBUG, NULL);
22207 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22208 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22209 DEBUG_ABBREV_SECTION_LABEL, 0);
22210 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22211 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22212 COLD_TEXT_SECTION_LABEL, 0);
22213 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22215 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22216 DEBUG_INFO_SECTION_LABEL, 0);
22217 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22218 DEBUG_LINE_SECTION_LABEL, 0);
22219 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22220 DEBUG_RANGES_SECTION_LABEL, 0);
22221 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22222 DEBUG_ADDR_SECTION_LABEL, 0);
22223 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22224 dwarf_strict
22225 ? DEBUG_MACINFO_SECTION_LABEL
22226 : DEBUG_MACRO_SECTION_LABEL, 0);
22227 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22229 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22230 vec_alloc (macinfo_table, 64);
22232 switch_to_section (text_section);
22233 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22235 /* Make sure the line number table for .text always exists. */
22236 text_section_line_info = new_line_info_table ();
22237 text_section_line_info->end_label = text_end_label;
22240 /* Called before compile () starts outputtting functions, variables
22241 and toplevel asms into assembly. */
22243 static void
22244 dwarf2out_assembly_start (void)
22246 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22247 && dwarf2out_do_cfi_asm ()
22248 && (!(flag_unwind_tables || flag_exceptions)
22249 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22250 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22253 /* A helper function for dwarf2out_finish called through
22254 htab_traverse. Assign a string its index. All strings must be
22255 collected into the table by the time index_string is called,
22256 because the indexing code relies on htab_traverse to traverse nodes
22257 in the same order for each run. */
22259 static int
22260 index_string (void **h, void *v)
22262 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22263 unsigned int *index = (unsigned int *) v;
22265 find_string_form (node);
22266 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22268 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22269 node->index = *index;
22270 *index += 1;
22272 return 1;
22275 /* A helper function for output_indirect_strings called through
22276 htab_traverse. Output the offset to a string and update the
22277 current offset. */
22279 static int
22280 output_index_string_offset (void **h, void *v)
22282 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22283 unsigned int *offset = (unsigned int *) v;
22285 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22287 /* Assert that this node has been assigned an index. */
22288 gcc_assert (node->index != NO_INDEX_ASSIGNED
22289 && node->index != NOT_INDEXED);
22290 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22291 "indexed string 0x%x: %s", node->index, node->str);
22292 *offset += strlen (node->str) + 1;
22294 return 1;
22297 /* A helper function for dwarf2out_finish called through
22298 htab_traverse. Output the indexed string. */
22300 static int
22301 output_index_string (void **h, void *v)
22303 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22304 unsigned int *cur_idx = (unsigned int *) v;
22306 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22308 /* Assert that the strings are output in the same order as their
22309 indexes were assigned. */
22310 gcc_assert (*cur_idx == node->index);
22311 assemble_string (node->str, strlen (node->str) + 1);
22312 *cur_idx += 1;
22314 return 1;
22317 /* A helper function for dwarf2out_finish called through
22318 htab_traverse. Emit one queued .debug_str string. */
22320 static int
22321 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22323 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22325 node->form = find_string_form (node);
22326 if (node->form == DW_FORM_strp && node->refcount > 0)
22328 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22329 assemble_string (node->str, strlen (node->str) + 1);
22332 return 1;
22335 /* Output the indexed string table. */
22337 static void
22338 output_indirect_strings (void)
22340 switch_to_section (debug_str_section);
22341 if (!dwarf_split_debug_info)
22342 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22343 else
22345 unsigned int offset = 0;
22346 unsigned int cur_idx = 0;
22348 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22350 switch_to_section (debug_str_offsets_section);
22351 htab_traverse_noresize (debug_str_hash,
22352 output_index_string_offset,
22353 &offset);
22354 switch_to_section (debug_str_dwo_section);
22355 htab_traverse_noresize (debug_str_hash,
22356 output_index_string,
22357 &cur_idx);
22361 /* Callback for htab_traverse to assign an index to an entry in the
22362 table, and to write that entry to the .debug_addr section. */
22364 static int
22365 output_addr_table_entry (void **slot, void *data)
22367 addr_table_entry *entry = (addr_table_entry *) *slot;
22368 unsigned int *cur_index = (unsigned int *)data;
22370 if (entry->refcount == 0)
22372 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22373 || entry->index == NOT_INDEXED);
22374 return 1;
22377 gcc_assert (entry->index == *cur_index);
22378 (*cur_index)++;
22380 switch (entry->kind)
22382 case ate_kind_rtx:
22383 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22384 "0x%x", entry->index);
22385 break;
22386 case ate_kind_rtx_dtprel:
22387 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22388 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22389 DWARF2_ADDR_SIZE,
22390 entry->addr.rtl);
22391 fputc ('\n', asm_out_file);
22392 break;
22393 case ate_kind_label:
22394 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22395 "0x%x", entry->index);
22396 break;
22397 default:
22398 gcc_unreachable ();
22400 return 1;
22403 /* Produce the .debug_addr section. */
22405 static void
22406 output_addr_table (void)
22408 unsigned int index = 0;
22409 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22410 return;
22412 switch_to_section (debug_addr_section);
22413 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22416 #if ENABLE_ASSERT_CHECKING
22417 /* Verify that all marks are clear. */
22419 static void
22420 verify_marks_clear (dw_die_ref die)
22422 dw_die_ref c;
22424 gcc_assert (! die->die_mark);
22425 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22427 #endif /* ENABLE_ASSERT_CHECKING */
22429 /* Clear the marks for a die and its children.
22430 Be cool if the mark isn't set. */
22432 static void
22433 prune_unmark_dies (dw_die_ref die)
22435 dw_die_ref c;
22437 if (die->die_mark)
22438 die->die_mark = 0;
22439 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22442 /* Given DIE that we're marking as used, find any other dies
22443 it references as attributes and mark them as used. */
22445 static void
22446 prune_unused_types_walk_attribs (dw_die_ref die)
22448 dw_attr_ref a;
22449 unsigned ix;
22451 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22453 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22455 /* A reference to another DIE.
22456 Make sure that it will get emitted.
22457 If it was broken out into a comdat group, don't follow it. */
22458 if (! AT_ref (a)->comdat_type_p
22459 || a->dw_attr == DW_AT_specification)
22460 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22462 /* Set the string's refcount to 0 so that prune_unused_types_mark
22463 accounts properly for it. */
22464 if (AT_class (a) == dw_val_class_str)
22465 a->dw_attr_val.v.val_str->refcount = 0;
22469 /* Mark the generic parameters and arguments children DIEs of DIE. */
22471 static void
22472 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22474 dw_die_ref c;
22476 if (die == NULL || die->die_child == NULL)
22477 return;
22478 c = die->die_child;
22481 if (is_template_parameter (c))
22482 prune_unused_types_mark (c, 1);
22483 c = c->die_sib;
22484 } while (c && c != die->die_child);
22487 /* Mark DIE as being used. If DOKIDS is true, then walk down
22488 to DIE's children. */
22490 static void
22491 prune_unused_types_mark (dw_die_ref die, int dokids)
22493 dw_die_ref c;
22495 if (die->die_mark == 0)
22497 /* We haven't done this node yet. Mark it as used. */
22498 die->die_mark = 1;
22499 /* If this is the DIE of a generic type instantiation,
22500 mark the children DIEs that describe its generic parms and
22501 args. */
22502 prune_unused_types_mark_generic_parms_dies (die);
22504 /* We also have to mark its parents as used.
22505 (But we don't want to mark our parent's kids due to this,
22506 unless it is a class.) */
22507 if (die->die_parent)
22508 prune_unused_types_mark (die->die_parent,
22509 class_scope_p (die->die_parent));
22511 /* Mark any referenced nodes. */
22512 prune_unused_types_walk_attribs (die);
22514 /* If this node is a specification,
22515 also mark the definition, if it exists. */
22516 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22517 prune_unused_types_mark (die->die_definition, 1);
22520 if (dokids && die->die_mark != 2)
22522 /* We need to walk the children, but haven't done so yet.
22523 Remember that we've walked the kids. */
22524 die->die_mark = 2;
22526 /* If this is an array type, we need to make sure our
22527 kids get marked, even if they're types. If we're
22528 breaking out types into comdat sections, do this
22529 for all type definitions. */
22530 if (die->die_tag == DW_TAG_array_type
22531 || (use_debug_types
22532 && is_type_die (die) && ! is_declaration_die (die)))
22533 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22534 else
22535 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22539 /* For local classes, look if any static member functions were emitted
22540 and if so, mark them. */
22542 static void
22543 prune_unused_types_walk_local_classes (dw_die_ref die)
22545 dw_die_ref c;
22547 if (die->die_mark == 2)
22548 return;
22550 switch (die->die_tag)
22552 case DW_TAG_structure_type:
22553 case DW_TAG_union_type:
22554 case DW_TAG_class_type:
22555 break;
22557 case DW_TAG_subprogram:
22558 if (!get_AT_flag (die, DW_AT_declaration)
22559 || die->die_definition != NULL)
22560 prune_unused_types_mark (die, 1);
22561 return;
22563 default:
22564 return;
22567 /* Mark children. */
22568 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22571 /* Walk the tree DIE and mark types that we actually use. */
22573 static void
22574 prune_unused_types_walk (dw_die_ref die)
22576 dw_die_ref c;
22578 /* Don't do anything if this node is already marked and
22579 children have been marked as well. */
22580 if (die->die_mark == 2)
22581 return;
22583 switch (die->die_tag)
22585 case DW_TAG_structure_type:
22586 case DW_TAG_union_type:
22587 case DW_TAG_class_type:
22588 if (die->die_perennial_p)
22589 break;
22591 for (c = die->die_parent; c; c = c->die_parent)
22592 if (c->die_tag == DW_TAG_subprogram)
22593 break;
22595 /* Finding used static member functions inside of classes
22596 is needed just for local classes, because for other classes
22597 static member function DIEs with DW_AT_specification
22598 are emitted outside of the DW_TAG_*_type. If we ever change
22599 it, we'd need to call this even for non-local classes. */
22600 if (c)
22601 prune_unused_types_walk_local_classes (die);
22603 /* It's a type node --- don't mark it. */
22604 return;
22606 case DW_TAG_const_type:
22607 case DW_TAG_packed_type:
22608 case DW_TAG_pointer_type:
22609 case DW_TAG_reference_type:
22610 case DW_TAG_rvalue_reference_type:
22611 case DW_TAG_volatile_type:
22612 case DW_TAG_typedef:
22613 case DW_TAG_array_type:
22614 case DW_TAG_interface_type:
22615 case DW_TAG_friend:
22616 case DW_TAG_variant_part:
22617 case DW_TAG_enumeration_type:
22618 case DW_TAG_subroutine_type:
22619 case DW_TAG_string_type:
22620 case DW_TAG_set_type:
22621 case DW_TAG_subrange_type:
22622 case DW_TAG_ptr_to_member_type:
22623 case DW_TAG_file_type:
22624 if (die->die_perennial_p)
22625 break;
22627 /* It's a type node --- don't mark it. */
22628 return;
22630 default:
22631 /* Mark everything else. */
22632 break;
22635 if (die->die_mark == 0)
22637 die->die_mark = 1;
22639 /* Now, mark any dies referenced from here. */
22640 prune_unused_types_walk_attribs (die);
22643 die->die_mark = 2;
22645 /* Mark children. */
22646 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22649 /* Increment the string counts on strings referred to from DIE's
22650 attributes. */
22652 static void
22653 prune_unused_types_update_strings (dw_die_ref die)
22655 dw_attr_ref a;
22656 unsigned ix;
22658 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22659 if (AT_class (a) == dw_val_class_str)
22661 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22662 s->refcount++;
22663 /* Avoid unnecessarily putting strings that are used less than
22664 twice in the hash table. */
22665 if (s->refcount
22666 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22668 void ** slot;
22669 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22670 htab_hash_string (s->str),
22671 INSERT);
22672 gcc_assert (*slot == NULL);
22673 *slot = s;
22678 /* Remove from the tree DIE any dies that aren't marked. */
22680 static void
22681 prune_unused_types_prune (dw_die_ref die)
22683 dw_die_ref c;
22685 gcc_assert (die->die_mark);
22686 prune_unused_types_update_strings (die);
22688 if (! die->die_child)
22689 return;
22691 c = die->die_child;
22692 do {
22693 dw_die_ref prev = c;
22694 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22695 if (c == die->die_child)
22697 /* No marked children between 'prev' and the end of the list. */
22698 if (prev == c)
22699 /* No marked children at all. */
22700 die->die_child = NULL;
22701 else
22703 prev->die_sib = c->die_sib;
22704 die->die_child = prev;
22706 return;
22709 if (c != prev->die_sib)
22710 prev->die_sib = c;
22711 prune_unused_types_prune (c);
22712 } while (c != die->die_child);
22715 /* Remove dies representing declarations that we never use. */
22717 static void
22718 prune_unused_types (void)
22720 unsigned int i;
22721 limbo_die_node *node;
22722 comdat_type_node *ctnode;
22723 pubname_ref pub;
22724 dw_die_ref base_type;
22726 #if ENABLE_ASSERT_CHECKING
22727 /* All the marks should already be clear. */
22728 verify_marks_clear (comp_unit_die ());
22729 for (node = limbo_die_list; node; node = node->next)
22730 verify_marks_clear (node->die);
22731 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22732 verify_marks_clear (ctnode->root_die);
22733 #endif /* ENABLE_ASSERT_CHECKING */
22735 /* Mark types that are used in global variables. */
22736 premark_types_used_by_global_vars ();
22738 /* Set the mark on nodes that are actually used. */
22739 prune_unused_types_walk (comp_unit_die ());
22740 for (node = limbo_die_list; node; node = node->next)
22741 prune_unused_types_walk (node->die);
22742 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22744 prune_unused_types_walk (ctnode->root_die);
22745 prune_unused_types_mark (ctnode->type_die, 1);
22748 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22749 are unusual in that they are pubnames that are the children of pubtypes.
22750 They should only be marked via their parent DW_TAG_enumeration_type die,
22751 not as roots in themselves. */
22752 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22753 if (pub->die->die_tag != DW_TAG_enumerator)
22754 prune_unused_types_mark (pub->die, 1);
22755 for (i = 0; base_types.iterate (i, &base_type); i++)
22756 prune_unused_types_mark (base_type, 1);
22758 if (debug_str_hash)
22759 htab_empty (debug_str_hash);
22760 if (skeleton_debug_str_hash)
22761 htab_empty (skeleton_debug_str_hash);
22762 prune_unused_types_prune (comp_unit_die ());
22763 for (node = limbo_die_list; node; node = node->next)
22764 prune_unused_types_prune (node->die);
22765 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22766 prune_unused_types_prune (ctnode->root_die);
22768 /* Leave the marks clear. */
22769 prune_unmark_dies (comp_unit_die ());
22770 for (node = limbo_die_list; node; node = node->next)
22771 prune_unmark_dies (node->die);
22772 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22773 prune_unmark_dies (ctnode->root_die);
22776 /* Set the parameter to true if there are any relative pathnames in
22777 the file table. */
22778 static int
22779 file_table_relative_p (void ** slot, void *param)
22781 bool *p = (bool *) param;
22782 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22783 if (!IS_ABSOLUTE_PATH (d->filename))
22785 *p = true;
22786 return 0;
22788 return 1;
22791 /* Helpers to manipulate hash table of comdat type units. */
22793 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22795 typedef comdat_type_node value_type;
22796 typedef comdat_type_node compare_type;
22797 static inline hashval_t hash (const value_type *);
22798 static inline bool equal (const value_type *, const compare_type *);
22801 inline hashval_t
22802 comdat_type_hasher::hash (const value_type *type_node)
22804 hashval_t h;
22805 memcpy (&h, type_node->signature, sizeof (h));
22806 return h;
22809 inline bool
22810 comdat_type_hasher::equal (const value_type *type_node_1,
22811 const compare_type *type_node_2)
22813 return (! memcmp (type_node_1->signature, type_node_2->signature,
22814 DWARF_TYPE_SIGNATURE_SIZE));
22817 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22818 to the location it would have been added, should we know its
22819 DECL_ASSEMBLER_NAME when we added other attributes. This will
22820 probably improve compactness of debug info, removing equivalent
22821 abbrevs, and hide any differences caused by deferring the
22822 computation of the assembler name, triggered by e.g. PCH. */
22824 static inline void
22825 move_linkage_attr (dw_die_ref die)
22827 unsigned ix = vec_safe_length (die->die_attr);
22828 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22830 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22831 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22833 while (--ix > 0)
22835 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22837 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22838 break;
22841 if (ix != vec_safe_length (die->die_attr) - 1)
22843 die->die_attr->pop ();
22844 die->die_attr->quick_insert (ix, linkage);
22848 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22849 referenced from typed stack ops and count how often they are used. */
22851 static void
22852 mark_base_types (dw_loc_descr_ref loc)
22854 dw_die_ref base_type = NULL;
22856 for (; loc; loc = loc->dw_loc_next)
22858 switch (loc->dw_loc_opc)
22860 case DW_OP_GNU_regval_type:
22861 case DW_OP_GNU_deref_type:
22862 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22863 break;
22864 case DW_OP_GNU_convert:
22865 case DW_OP_GNU_reinterpret:
22866 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22867 continue;
22868 /* FALLTHRU */
22869 case DW_OP_GNU_const_type:
22870 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22871 break;
22872 case DW_OP_GNU_entry_value:
22873 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22874 continue;
22875 default:
22876 continue;
22878 gcc_assert (base_type->die_parent == comp_unit_die ());
22879 if (base_type->die_mark)
22880 base_type->die_mark++;
22881 else
22883 base_types.safe_push (base_type);
22884 base_type->die_mark = 1;
22889 /* Comparison function for sorting marked base types. */
22891 static int
22892 base_type_cmp (const void *x, const void *y)
22894 dw_die_ref dx = *(const dw_die_ref *) x;
22895 dw_die_ref dy = *(const dw_die_ref *) y;
22896 unsigned int byte_size1, byte_size2;
22897 unsigned int encoding1, encoding2;
22898 if (dx->die_mark > dy->die_mark)
22899 return -1;
22900 if (dx->die_mark < dy->die_mark)
22901 return 1;
22902 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22903 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22904 if (byte_size1 < byte_size2)
22905 return 1;
22906 if (byte_size1 > byte_size2)
22907 return -1;
22908 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22909 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22910 if (encoding1 < encoding2)
22911 return 1;
22912 if (encoding1 > encoding2)
22913 return -1;
22914 return 0;
22917 /* Move base types marked by mark_base_types as early as possible
22918 in the CU, sorted by decreasing usage count both to make the
22919 uleb128 references as small as possible and to make sure they
22920 will have die_offset already computed by calc_die_sizes when
22921 sizes of typed stack loc ops is computed. */
22923 static void
22924 move_marked_base_types (void)
22926 unsigned int i;
22927 dw_die_ref base_type, die, c;
22929 if (base_types.is_empty ())
22930 return;
22932 /* Sort by decreasing usage count, they will be added again in that
22933 order later on. */
22934 base_types.qsort (base_type_cmp);
22935 die = comp_unit_die ();
22936 c = die->die_child;
22939 dw_die_ref prev = c;
22940 c = c->die_sib;
22941 while (c->die_mark)
22943 remove_child_with_prev (c, prev);
22944 /* As base types got marked, there must be at least
22945 one node other than DW_TAG_base_type. */
22946 gcc_assert (c != c->die_sib);
22947 c = c->die_sib;
22950 while (c != die->die_child);
22951 gcc_assert (die->die_child);
22952 c = die->die_child;
22953 for (i = 0; base_types.iterate (i, &base_type); i++)
22955 base_type->die_mark = 0;
22956 base_type->die_sib = c->die_sib;
22957 c->die_sib = base_type;
22958 c = base_type;
22962 /* Helper function for resolve_addr, attempt to resolve
22963 one CONST_STRING, return non-zero if not successful. Similarly verify that
22964 SYMBOL_REFs refer to variables emitted in the current CU. */
22966 static int
22967 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22969 rtx rtl = *addr;
22971 if (GET_CODE (rtl) == CONST_STRING)
22973 size_t len = strlen (XSTR (rtl, 0)) + 1;
22974 tree t = build_string (len, XSTR (rtl, 0));
22975 tree tlen = size_int (len - 1);
22976 TREE_TYPE (t)
22977 = build_array_type (char_type_node, build_index_type (tlen));
22978 rtl = lookup_constant_def (t);
22979 if (!rtl || !MEM_P (rtl))
22980 return 1;
22981 rtl = XEXP (rtl, 0);
22982 if (GET_CODE (rtl) == SYMBOL_REF
22983 && SYMBOL_REF_DECL (rtl)
22984 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22985 return 1;
22986 vec_safe_push (used_rtx_array, rtl);
22987 *addr = rtl;
22988 return 0;
22991 if (GET_CODE (rtl) == SYMBOL_REF
22992 && SYMBOL_REF_DECL (rtl))
22994 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22996 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22997 return 1;
22999 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23000 return 1;
23003 if (GET_CODE (rtl) == CONST
23004 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23005 return 1;
23007 return 0;
23010 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23011 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23012 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23014 static rtx
23015 string_cst_pool_decl (tree t)
23017 rtx rtl = output_constant_def (t, 1);
23018 unsigned char *array;
23019 dw_loc_descr_ref l;
23020 tree decl;
23021 size_t len;
23022 dw_die_ref ref;
23024 if (!rtl || !MEM_P (rtl))
23025 return NULL_RTX;
23026 rtl = XEXP (rtl, 0);
23027 if (GET_CODE (rtl) != SYMBOL_REF
23028 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23029 return NULL_RTX;
23031 decl = SYMBOL_REF_DECL (rtl);
23032 if (!lookup_decl_die (decl))
23034 len = TREE_STRING_LENGTH (t);
23035 vec_safe_push (used_rtx_array, rtl);
23036 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23037 array = (unsigned char *) ggc_alloc_atomic (len);
23038 memcpy (array, TREE_STRING_POINTER (t), len);
23039 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23040 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23041 l->dw_loc_oprnd2.v.val_vec.length = len;
23042 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23043 l->dw_loc_oprnd2.v.val_vec.array = array;
23044 add_AT_loc (ref, DW_AT_location, l);
23045 equate_decl_number_to_die (decl, ref);
23047 return rtl;
23050 /* Helper function of resolve_addr_in_expr. LOC is
23051 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23052 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23053 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23054 with DW_OP_GNU_implicit_pointer if possible
23055 and return true, if unsuccessful, return false. */
23057 static bool
23058 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23060 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23061 HOST_WIDE_INT offset = 0;
23062 dw_die_ref ref = NULL;
23063 tree decl;
23065 if (GET_CODE (rtl) == CONST
23066 && GET_CODE (XEXP (rtl, 0)) == PLUS
23067 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23069 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23070 rtl = XEXP (XEXP (rtl, 0), 0);
23072 if (GET_CODE (rtl) == CONST_STRING)
23074 size_t len = strlen (XSTR (rtl, 0)) + 1;
23075 tree t = build_string (len, XSTR (rtl, 0));
23076 tree tlen = size_int (len - 1);
23078 TREE_TYPE (t)
23079 = build_array_type (char_type_node, build_index_type (tlen));
23080 rtl = string_cst_pool_decl (t);
23081 if (!rtl)
23082 return false;
23084 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23086 decl = SYMBOL_REF_DECL (rtl);
23087 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23089 ref = lookup_decl_die (decl);
23090 if (ref && (get_AT (ref, DW_AT_location)
23091 || get_AT (ref, DW_AT_const_value)))
23093 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23094 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23095 loc->dw_loc_oprnd1.val_entry = NULL;
23096 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23097 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23098 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23099 loc->dw_loc_oprnd2.v.val_int = offset;
23100 return true;
23104 return false;
23107 /* Helper function for resolve_addr, handle one location
23108 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23109 the location list couldn't be resolved. */
23111 static bool
23112 resolve_addr_in_expr (dw_loc_descr_ref loc)
23114 dw_loc_descr_ref keep = NULL;
23115 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23116 switch (loc->dw_loc_opc)
23118 case DW_OP_addr:
23119 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23121 if ((prev == NULL
23122 || prev->dw_loc_opc == DW_OP_piece
23123 || prev->dw_loc_opc == DW_OP_bit_piece)
23124 && loc->dw_loc_next
23125 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23126 && !dwarf_strict
23127 && optimize_one_addr_into_implicit_ptr (loc))
23128 break;
23129 return false;
23131 break;
23132 case DW_OP_GNU_addr_index:
23133 case DW_OP_GNU_const_index:
23134 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23135 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23136 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23137 NULL))
23138 return false;
23139 break;
23140 case DW_OP_const4u:
23141 case DW_OP_const8u:
23142 if (loc->dtprel
23143 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23144 return false;
23145 break;
23146 case DW_OP_plus_uconst:
23147 if (size_of_loc_descr (loc)
23148 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23150 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23152 dw_loc_descr_ref repl
23153 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23154 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23155 add_loc_descr (&repl, loc->dw_loc_next);
23156 *loc = *repl;
23158 break;
23159 case DW_OP_implicit_value:
23160 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23161 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23162 return false;
23163 break;
23164 case DW_OP_GNU_implicit_pointer:
23165 case DW_OP_GNU_parameter_ref:
23166 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23168 dw_die_ref ref
23169 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23170 if (ref == NULL)
23171 return false;
23172 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23173 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23174 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23176 break;
23177 case DW_OP_GNU_const_type:
23178 case DW_OP_GNU_regval_type:
23179 case DW_OP_GNU_deref_type:
23180 case DW_OP_GNU_convert:
23181 case DW_OP_GNU_reinterpret:
23182 while (loc->dw_loc_next
23183 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23185 dw_die_ref base1, base2;
23186 unsigned enc1, enc2, size1, size2;
23187 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23188 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23189 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23190 else if (loc->dw_loc_oprnd1.val_class
23191 == dw_val_class_unsigned_const)
23192 break;
23193 else
23194 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23195 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23196 == dw_val_class_unsigned_const)
23197 break;
23198 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23199 gcc_assert (base1->die_tag == DW_TAG_base_type
23200 && base2->die_tag == DW_TAG_base_type);
23201 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23202 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23203 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23204 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23205 if (size1 == size2
23206 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23207 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23208 && loc != keep)
23209 || enc1 == enc2))
23211 /* Optimize away next DW_OP_GNU_convert after
23212 adjusting LOC's base type die reference. */
23213 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23214 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23215 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23216 else
23217 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23218 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23219 continue;
23221 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23222 point typed stack entry. */
23223 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23224 keep = loc->dw_loc_next;
23225 break;
23227 break;
23228 default:
23229 break;
23231 return true;
23234 /* Helper function of resolve_addr. DIE had DW_AT_location of
23235 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23236 and DW_OP_addr couldn't be resolved. resolve_addr has already
23237 removed the DW_AT_location attribute. This function attempts to
23238 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23239 to it or DW_AT_const_value attribute, if possible. */
23241 static void
23242 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23244 if (TREE_CODE (decl) != VAR_DECL
23245 || lookup_decl_die (decl) != die
23246 || DECL_EXTERNAL (decl)
23247 || !TREE_STATIC (decl)
23248 || DECL_INITIAL (decl) == NULL_TREE
23249 || DECL_P (DECL_INITIAL (decl))
23250 || get_AT (die, DW_AT_const_value))
23251 return;
23253 tree init = DECL_INITIAL (decl);
23254 HOST_WIDE_INT offset = 0;
23255 /* For variables that have been optimized away and thus
23256 don't have a memory location, see if we can emit
23257 DW_AT_const_value instead. */
23258 if (tree_add_const_value_attribute (die, init))
23259 return;
23260 if (dwarf_strict)
23261 return;
23262 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23263 and ADDR_EXPR refers to a decl that has DW_AT_location or
23264 DW_AT_const_value (but isn't addressable, otherwise
23265 resolving the original DW_OP_addr wouldn't fail), see if
23266 we can add DW_OP_GNU_implicit_pointer. */
23267 STRIP_NOPS (init);
23268 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23269 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23271 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23272 init = TREE_OPERAND (init, 0);
23273 STRIP_NOPS (init);
23275 if (TREE_CODE (init) != ADDR_EXPR)
23276 return;
23277 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23278 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23279 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23280 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23281 && TREE_OPERAND (init, 0) != decl))
23283 dw_die_ref ref;
23284 dw_loc_descr_ref l;
23286 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23288 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23289 if (!rtl)
23290 return;
23291 decl = SYMBOL_REF_DECL (rtl);
23293 else
23294 decl = TREE_OPERAND (init, 0);
23295 ref = lookup_decl_die (decl);
23296 if (ref == NULL
23297 || (!get_AT (ref, DW_AT_location)
23298 && !get_AT (ref, DW_AT_const_value)))
23299 return;
23300 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23301 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23302 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23303 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23304 add_AT_loc (die, DW_AT_location, l);
23308 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23309 an address in .rodata section if the string literal is emitted there,
23310 or remove the containing location list or replace DW_AT_const_value
23311 with DW_AT_location and empty location expression, if it isn't found
23312 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23313 to something that has been emitted in the current CU. */
23315 static void
23316 resolve_addr (dw_die_ref die)
23318 dw_die_ref c;
23319 dw_attr_ref a;
23320 dw_loc_list_ref *curr, *start, loc;
23321 unsigned ix;
23323 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23324 switch (AT_class (a))
23326 case dw_val_class_loc_list:
23327 start = curr = AT_loc_list_ptr (a);
23328 loc = *curr;
23329 gcc_assert (loc);
23330 /* The same list can be referenced more than once. See if we have
23331 already recorded the result from a previous pass. */
23332 if (loc->replaced)
23333 *curr = loc->dw_loc_next;
23334 else if (!loc->resolved_addr)
23336 /* As things stand, we do not expect or allow one die to
23337 reference a suffix of another die's location list chain.
23338 References must be identical or completely separate.
23339 There is therefore no need to cache the result of this
23340 pass on any list other than the first; doing so
23341 would lead to unnecessary writes. */
23342 while (*curr)
23344 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23345 if (!resolve_addr_in_expr ((*curr)->expr))
23347 dw_loc_list_ref next = (*curr)->dw_loc_next;
23348 dw_loc_descr_ref l = (*curr)->expr;
23350 if (next && (*curr)->ll_symbol)
23352 gcc_assert (!next->ll_symbol);
23353 next->ll_symbol = (*curr)->ll_symbol;
23355 if (dwarf_split_debug_info)
23356 remove_loc_list_addr_table_entries (l);
23357 *curr = next;
23359 else
23361 mark_base_types ((*curr)->expr);
23362 curr = &(*curr)->dw_loc_next;
23365 if (loc == *start)
23366 loc->resolved_addr = 1;
23367 else
23369 loc->replaced = 1;
23370 loc->dw_loc_next = *start;
23373 if (!*start)
23375 remove_AT (die, a->dw_attr);
23376 ix--;
23378 break;
23379 case dw_val_class_loc:
23381 dw_loc_descr_ref l = AT_loc (a);
23382 /* For -gdwarf-2 don't attempt to optimize
23383 DW_AT_data_member_location containing
23384 DW_OP_plus_uconst - older consumers might
23385 rely on it being that op instead of a more complex,
23386 but shorter, location description. */
23387 if ((dwarf_version > 2
23388 || a->dw_attr != DW_AT_data_member_location
23389 || l == NULL
23390 || l->dw_loc_opc != DW_OP_plus_uconst
23391 || l->dw_loc_next != NULL)
23392 && !resolve_addr_in_expr (l))
23394 if (dwarf_split_debug_info)
23395 remove_loc_list_addr_table_entries (l);
23396 if (l != NULL
23397 && l->dw_loc_next == NULL
23398 && l->dw_loc_opc == DW_OP_addr
23399 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23400 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23401 && a->dw_attr == DW_AT_location)
23403 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23404 remove_AT (die, a->dw_attr);
23405 ix--;
23406 optimize_location_into_implicit_ptr (die, decl);
23407 break;
23409 remove_AT (die, a->dw_attr);
23410 ix--;
23412 else
23413 mark_base_types (l);
23415 break;
23416 case dw_val_class_addr:
23417 if (a->dw_attr == DW_AT_const_value
23418 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23420 if (AT_index (a) != NOT_INDEXED)
23421 remove_addr_table_entry (a->dw_attr_val.val_entry);
23422 remove_AT (die, a->dw_attr);
23423 ix--;
23425 if (die->die_tag == DW_TAG_GNU_call_site
23426 && a->dw_attr == DW_AT_abstract_origin)
23428 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23429 dw_die_ref tdie = lookup_decl_die (tdecl);
23430 if (tdie == NULL
23431 && DECL_EXTERNAL (tdecl)
23432 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23434 force_decl_die (tdecl);
23435 tdie = lookup_decl_die (tdecl);
23437 if (tdie)
23439 a->dw_attr_val.val_class = dw_val_class_die_ref;
23440 a->dw_attr_val.v.val_die_ref.die = tdie;
23441 a->dw_attr_val.v.val_die_ref.external = 0;
23443 else
23445 if (AT_index (a) != NOT_INDEXED)
23446 remove_addr_table_entry (a->dw_attr_val.val_entry);
23447 remove_AT (die, a->dw_attr);
23448 ix--;
23451 break;
23452 default:
23453 break;
23456 FOR_EACH_CHILD (die, c, resolve_addr (c));
23459 /* Helper routines for optimize_location_lists.
23460 This pass tries to share identical local lists in .debug_loc
23461 section. */
23463 /* Iteratively hash operands of LOC opcode. */
23465 static hashval_t
23466 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23468 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23469 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23471 switch (loc->dw_loc_opc)
23473 case DW_OP_const4u:
23474 case DW_OP_const8u:
23475 if (loc->dtprel)
23476 goto hash_addr;
23477 /* FALLTHRU */
23478 case DW_OP_const1u:
23479 case DW_OP_const1s:
23480 case DW_OP_const2u:
23481 case DW_OP_const2s:
23482 case DW_OP_const4s:
23483 case DW_OP_const8s:
23484 case DW_OP_constu:
23485 case DW_OP_consts:
23486 case DW_OP_pick:
23487 case DW_OP_plus_uconst:
23488 case DW_OP_breg0:
23489 case DW_OP_breg1:
23490 case DW_OP_breg2:
23491 case DW_OP_breg3:
23492 case DW_OP_breg4:
23493 case DW_OP_breg5:
23494 case DW_OP_breg6:
23495 case DW_OP_breg7:
23496 case DW_OP_breg8:
23497 case DW_OP_breg9:
23498 case DW_OP_breg10:
23499 case DW_OP_breg11:
23500 case DW_OP_breg12:
23501 case DW_OP_breg13:
23502 case DW_OP_breg14:
23503 case DW_OP_breg15:
23504 case DW_OP_breg16:
23505 case DW_OP_breg17:
23506 case DW_OP_breg18:
23507 case DW_OP_breg19:
23508 case DW_OP_breg20:
23509 case DW_OP_breg21:
23510 case DW_OP_breg22:
23511 case DW_OP_breg23:
23512 case DW_OP_breg24:
23513 case DW_OP_breg25:
23514 case DW_OP_breg26:
23515 case DW_OP_breg27:
23516 case DW_OP_breg28:
23517 case DW_OP_breg29:
23518 case DW_OP_breg30:
23519 case DW_OP_breg31:
23520 case DW_OP_regx:
23521 case DW_OP_fbreg:
23522 case DW_OP_piece:
23523 case DW_OP_deref_size:
23524 case DW_OP_xderef_size:
23525 hash = iterative_hash_object (val1->v.val_int, hash);
23526 break;
23527 case DW_OP_skip:
23528 case DW_OP_bra:
23530 int offset;
23532 gcc_assert (val1->val_class == dw_val_class_loc);
23533 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23534 hash = iterative_hash_object (offset, hash);
23536 break;
23537 case DW_OP_implicit_value:
23538 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23539 switch (val2->val_class)
23541 case dw_val_class_const:
23542 hash = iterative_hash_object (val2->v.val_int, hash);
23543 break;
23544 case dw_val_class_vec:
23546 unsigned int elt_size = val2->v.val_vec.elt_size;
23547 unsigned int len = val2->v.val_vec.length;
23549 hash = iterative_hash_object (elt_size, hash);
23550 hash = iterative_hash_object (len, hash);
23551 hash = iterative_hash (val2->v.val_vec.array,
23552 len * elt_size, hash);
23554 break;
23555 case dw_val_class_const_double:
23556 hash = iterative_hash_object (val2->v.val_double.low, hash);
23557 hash = iterative_hash_object (val2->v.val_double.high, hash);
23558 break;
23559 case dw_val_class_addr:
23560 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23561 break;
23562 default:
23563 gcc_unreachable ();
23565 break;
23566 case DW_OP_bregx:
23567 case DW_OP_bit_piece:
23568 hash = iterative_hash_object (val1->v.val_int, hash);
23569 hash = iterative_hash_object (val2->v.val_int, hash);
23570 break;
23571 case DW_OP_addr:
23572 hash_addr:
23573 if (loc->dtprel)
23575 unsigned char dtprel = 0xd1;
23576 hash = iterative_hash_object (dtprel, hash);
23578 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23579 break;
23580 case DW_OP_GNU_addr_index:
23581 case DW_OP_GNU_const_index:
23583 if (loc->dtprel)
23585 unsigned char dtprel = 0xd1;
23586 hash = iterative_hash_object (dtprel, hash);
23588 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23590 break;
23591 case DW_OP_GNU_implicit_pointer:
23592 hash = iterative_hash_object (val2->v.val_int, hash);
23593 break;
23594 case DW_OP_GNU_entry_value:
23595 hash = hash_loc_operands (val1->v.val_loc, hash);
23596 break;
23597 case DW_OP_GNU_regval_type:
23598 case DW_OP_GNU_deref_type:
23600 unsigned int byte_size
23601 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23602 unsigned int encoding
23603 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23604 hash = iterative_hash_object (val1->v.val_int, hash);
23605 hash = iterative_hash_object (byte_size, hash);
23606 hash = iterative_hash_object (encoding, hash);
23608 break;
23609 case DW_OP_GNU_convert:
23610 case DW_OP_GNU_reinterpret:
23611 if (val1->val_class == dw_val_class_unsigned_const)
23613 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23614 break;
23616 /* FALLTHRU */
23617 case DW_OP_GNU_const_type:
23619 unsigned int byte_size
23620 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23621 unsigned int encoding
23622 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23623 hash = iterative_hash_object (byte_size, hash);
23624 hash = iterative_hash_object (encoding, hash);
23625 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23626 break;
23627 hash = iterative_hash_object (val2->val_class, hash);
23628 switch (val2->val_class)
23630 case dw_val_class_const:
23631 hash = iterative_hash_object (val2->v.val_int, hash);
23632 break;
23633 case dw_val_class_vec:
23635 unsigned int elt_size = val2->v.val_vec.elt_size;
23636 unsigned int len = val2->v.val_vec.length;
23638 hash = iterative_hash_object (elt_size, hash);
23639 hash = iterative_hash_object (len, hash);
23640 hash = iterative_hash (val2->v.val_vec.array,
23641 len * elt_size, hash);
23643 break;
23644 case dw_val_class_const_double:
23645 hash = iterative_hash_object (val2->v.val_double.low, hash);
23646 hash = iterative_hash_object (val2->v.val_double.high, hash);
23647 break;
23648 default:
23649 gcc_unreachable ();
23652 break;
23654 default:
23655 /* Other codes have no operands. */
23656 break;
23658 return hash;
23661 /* Iteratively hash the whole DWARF location expression LOC. */
23663 static inline hashval_t
23664 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23666 dw_loc_descr_ref l;
23667 bool sizes_computed = false;
23668 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23669 size_of_locs (loc);
23671 for (l = loc; l != NULL; l = l->dw_loc_next)
23673 enum dwarf_location_atom opc = l->dw_loc_opc;
23674 hash = iterative_hash_object (opc, hash);
23675 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23677 size_of_locs (loc);
23678 sizes_computed = true;
23680 hash = hash_loc_operands (l, hash);
23682 return hash;
23685 /* Compute hash of the whole location list LIST_HEAD. */
23687 static inline void
23688 hash_loc_list (dw_loc_list_ref list_head)
23690 dw_loc_list_ref curr = list_head;
23691 hashval_t hash = 0;
23693 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23695 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23696 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23697 if (curr->section)
23698 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23699 hash);
23700 hash = hash_locs (curr->expr, hash);
23702 list_head->hash = hash;
23705 /* Return true if X and Y opcodes have the same operands. */
23707 static inline bool
23708 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23710 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23711 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23712 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23713 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23715 switch (x->dw_loc_opc)
23717 case DW_OP_const4u:
23718 case DW_OP_const8u:
23719 if (x->dtprel)
23720 goto hash_addr;
23721 /* FALLTHRU */
23722 case DW_OP_const1u:
23723 case DW_OP_const1s:
23724 case DW_OP_const2u:
23725 case DW_OP_const2s:
23726 case DW_OP_const4s:
23727 case DW_OP_const8s:
23728 case DW_OP_constu:
23729 case DW_OP_consts:
23730 case DW_OP_pick:
23731 case DW_OP_plus_uconst:
23732 case DW_OP_breg0:
23733 case DW_OP_breg1:
23734 case DW_OP_breg2:
23735 case DW_OP_breg3:
23736 case DW_OP_breg4:
23737 case DW_OP_breg5:
23738 case DW_OP_breg6:
23739 case DW_OP_breg7:
23740 case DW_OP_breg8:
23741 case DW_OP_breg9:
23742 case DW_OP_breg10:
23743 case DW_OP_breg11:
23744 case DW_OP_breg12:
23745 case DW_OP_breg13:
23746 case DW_OP_breg14:
23747 case DW_OP_breg15:
23748 case DW_OP_breg16:
23749 case DW_OP_breg17:
23750 case DW_OP_breg18:
23751 case DW_OP_breg19:
23752 case DW_OP_breg20:
23753 case DW_OP_breg21:
23754 case DW_OP_breg22:
23755 case DW_OP_breg23:
23756 case DW_OP_breg24:
23757 case DW_OP_breg25:
23758 case DW_OP_breg26:
23759 case DW_OP_breg27:
23760 case DW_OP_breg28:
23761 case DW_OP_breg29:
23762 case DW_OP_breg30:
23763 case DW_OP_breg31:
23764 case DW_OP_regx:
23765 case DW_OP_fbreg:
23766 case DW_OP_piece:
23767 case DW_OP_deref_size:
23768 case DW_OP_xderef_size:
23769 return valx1->v.val_int == valy1->v.val_int;
23770 case DW_OP_skip:
23771 case DW_OP_bra:
23772 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23773 can cause irrelevant differences in dw_loc_addr. */
23774 gcc_assert (valx1->val_class == dw_val_class_loc
23775 && valy1->val_class == dw_val_class_loc
23776 && (dwarf_split_debug_info
23777 || x->dw_loc_addr == y->dw_loc_addr));
23778 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23779 case DW_OP_implicit_value:
23780 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23781 || valx2->val_class != valy2->val_class)
23782 return false;
23783 switch (valx2->val_class)
23785 case dw_val_class_const:
23786 return valx2->v.val_int == valy2->v.val_int;
23787 case dw_val_class_vec:
23788 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23789 && valx2->v.val_vec.length == valy2->v.val_vec.length
23790 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23791 valx2->v.val_vec.elt_size
23792 * valx2->v.val_vec.length) == 0;
23793 case dw_val_class_const_double:
23794 return valx2->v.val_double.low == valy2->v.val_double.low
23795 && valx2->v.val_double.high == valy2->v.val_double.high;
23796 case dw_val_class_addr:
23797 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23798 default:
23799 gcc_unreachable ();
23801 case DW_OP_bregx:
23802 case DW_OP_bit_piece:
23803 return valx1->v.val_int == valy1->v.val_int
23804 && valx2->v.val_int == valy2->v.val_int;
23805 case DW_OP_addr:
23806 hash_addr:
23807 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23808 case DW_OP_GNU_addr_index:
23809 case DW_OP_GNU_const_index:
23811 rtx ax1 = valx1->val_entry->addr.rtl;
23812 rtx ay1 = valy1->val_entry->addr.rtl;
23813 return rtx_equal_p (ax1, ay1);
23815 case DW_OP_GNU_implicit_pointer:
23816 return valx1->val_class == dw_val_class_die_ref
23817 && valx1->val_class == valy1->val_class
23818 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23819 && valx2->v.val_int == valy2->v.val_int;
23820 case DW_OP_GNU_entry_value:
23821 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23822 case DW_OP_GNU_const_type:
23823 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23824 || valx2->val_class != valy2->val_class)
23825 return false;
23826 switch (valx2->val_class)
23828 case dw_val_class_const:
23829 return valx2->v.val_int == valy2->v.val_int;
23830 case dw_val_class_vec:
23831 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23832 && valx2->v.val_vec.length == valy2->v.val_vec.length
23833 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23834 valx2->v.val_vec.elt_size
23835 * valx2->v.val_vec.length) == 0;
23836 case dw_val_class_const_double:
23837 return valx2->v.val_double.low == valy2->v.val_double.low
23838 && valx2->v.val_double.high == valy2->v.val_double.high;
23839 default:
23840 gcc_unreachable ();
23842 case DW_OP_GNU_regval_type:
23843 case DW_OP_GNU_deref_type:
23844 return valx1->v.val_int == valy1->v.val_int
23845 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23846 case DW_OP_GNU_convert:
23847 case DW_OP_GNU_reinterpret:
23848 if (valx1->val_class != valy1->val_class)
23849 return false;
23850 if (valx1->val_class == dw_val_class_unsigned_const)
23851 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23852 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23853 case DW_OP_GNU_parameter_ref:
23854 return valx1->val_class == dw_val_class_die_ref
23855 && valx1->val_class == valy1->val_class
23856 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23857 default:
23858 /* Other codes have no operands. */
23859 return true;
23863 /* Return true if DWARF location expressions X and Y are the same. */
23865 static inline bool
23866 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23868 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23869 if (x->dw_loc_opc != y->dw_loc_opc
23870 || x->dtprel != y->dtprel
23871 || !compare_loc_operands (x, y))
23872 break;
23873 return x == NULL && y == NULL;
23876 /* Hashtable helpers. */
23878 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23880 typedef dw_loc_list_struct value_type;
23881 typedef dw_loc_list_struct compare_type;
23882 static inline hashval_t hash (const value_type *);
23883 static inline bool equal (const value_type *, const compare_type *);
23886 /* Return precomputed hash of location list X. */
23888 inline hashval_t
23889 loc_list_hasher::hash (const value_type *x)
23891 return x->hash;
23894 /* Return true if location lists A and B are the same. */
23896 inline bool
23897 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23899 if (a == b)
23900 return 1;
23901 if (a->hash != b->hash)
23902 return 0;
23903 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23904 if (strcmp (a->begin, b->begin) != 0
23905 || strcmp (a->end, b->end) != 0
23906 || (a->section == NULL) != (b->section == NULL)
23907 || (a->section && strcmp (a->section, b->section) != 0)
23908 || !compare_locs (a->expr, b->expr))
23909 break;
23910 return a == NULL && b == NULL;
23913 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23916 /* Recursively optimize location lists referenced from DIE
23917 children and share them whenever possible. */
23919 static void
23920 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23922 dw_die_ref c;
23923 dw_attr_ref a;
23924 unsigned ix;
23925 dw_loc_list_struct **slot;
23927 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23928 if (AT_class (a) == dw_val_class_loc_list)
23930 dw_loc_list_ref list = AT_loc_list (a);
23931 /* TODO: perform some optimizations here, before hashing
23932 it and storing into the hash table. */
23933 hash_loc_list (list);
23934 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23935 if (*slot == NULL)
23936 *slot = list;
23937 else
23938 a->dw_attr_val.v.val_loc_list = *slot;
23941 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23945 /* Recursively assign each location list a unique index into the debug_addr
23946 section. */
23948 static void
23949 index_location_lists (dw_die_ref die)
23951 dw_die_ref c;
23952 dw_attr_ref a;
23953 unsigned ix;
23955 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23956 if (AT_class (a) == dw_val_class_loc_list)
23958 dw_loc_list_ref list = AT_loc_list (a);
23959 dw_loc_list_ref curr;
23960 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23962 /* Don't index an entry that has already been indexed
23963 or won't be output. */
23964 if (curr->begin_entry != NULL
23965 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23966 continue;
23968 curr->begin_entry
23969 = add_addr_table_entry (xstrdup (curr->begin),
23970 ate_kind_label);
23974 FOR_EACH_CHILD (die, c, index_location_lists (c));
23977 /* Optimize location lists referenced from DIE
23978 children and share them whenever possible. */
23980 static void
23981 optimize_location_lists (dw_die_ref die)
23983 loc_list_hash_type htab;
23984 htab.create (500);
23985 optimize_location_lists_1 (die, htab);
23986 htab.dispose ();
23989 /* Output stuff that dwarf requires at the end of every file,
23990 and generate the DWARF-2 debugging info. */
23992 static void
23993 dwarf2out_finish (const char *filename)
23995 limbo_die_node *node, *next_node;
23996 comdat_type_node *ctnode;
23997 hash_table <comdat_type_hasher> comdat_type_table;
23998 unsigned int i;
23999 dw_die_ref main_comp_unit_die;
24001 /* PCH might result in DW_AT_producer string being restored from the
24002 header compilation, so always fill it with empty string initially
24003 and overwrite only here. */
24004 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24005 producer_string = gen_producer_string ();
24006 producer->dw_attr_val.v.val_str->refcount--;
24007 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24009 gen_scheduled_generic_parms_dies ();
24010 gen_remaining_tmpl_value_param_die_attribute ();
24012 /* Add the name for the main input file now. We delayed this from
24013 dwarf2out_init to avoid complications with PCH. */
24014 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24015 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24016 add_comp_dir_attribute (comp_unit_die ());
24017 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24019 bool p = false;
24020 htab_traverse (file_table, file_table_relative_p, &p);
24021 if (p)
24022 add_comp_dir_attribute (comp_unit_die ());
24025 if (deferred_locations_list)
24026 for (i = 0; i < deferred_locations_list->length (); i++)
24028 add_location_or_const_value_attribute (
24029 (*deferred_locations_list)[i].die,
24030 (*deferred_locations_list)[i].variable,
24031 false,
24032 DW_AT_location);
24035 /* Traverse the limbo die list, and add parent/child links. The only
24036 dies without parents that should be here are concrete instances of
24037 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24038 For concrete instances, we can get the parent die from the abstract
24039 instance. */
24040 for (node = limbo_die_list; node; node = next_node)
24042 dw_die_ref die = node->die;
24043 next_node = node->next;
24045 if (die->die_parent == NULL)
24047 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24049 if (origin && origin->die_parent)
24050 add_child_die (origin->die_parent, die);
24051 else if (is_cu_die (die))
24053 else if (seen_error ())
24054 /* It's OK to be confused by errors in the input. */
24055 add_child_die (comp_unit_die (), die);
24056 else
24058 /* In certain situations, the lexical block containing a
24059 nested function can be optimized away, which results
24060 in the nested function die being orphaned. Likewise
24061 with the return type of that nested function. Force
24062 this to be a child of the containing function.
24064 It may happen that even the containing function got fully
24065 inlined and optimized out. In that case we are lost and
24066 assign the empty child. This should not be big issue as
24067 the function is likely unreachable too. */
24068 gcc_assert (node->created_for);
24070 if (DECL_P (node->created_for))
24071 origin = get_context_die (DECL_CONTEXT (node->created_for));
24072 else if (TYPE_P (node->created_for))
24073 origin = scope_die_for (node->created_for, comp_unit_die ());
24074 else
24075 origin = comp_unit_die ();
24077 add_child_die (origin, die);
24082 limbo_die_list = NULL;
24084 #if ENABLE_ASSERT_CHECKING
24086 dw_die_ref die = comp_unit_die (), c;
24087 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24089 #endif
24090 resolve_addr (comp_unit_die ());
24091 move_marked_base_types ();
24093 for (node = deferred_asm_name; node; node = node->next)
24095 tree decl = node->created_for;
24096 /* When generating LTO bytecode we can not generate new assembler
24097 names at this point and all important decls got theirs via
24098 free-lang-data. */
24099 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24100 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24102 add_linkage_attr (node->die, decl);
24103 move_linkage_attr (node->die);
24107 deferred_asm_name = NULL;
24109 /* Walk through the list of incomplete types again, trying once more to
24110 emit full debugging info for them. */
24111 retry_incomplete_types ();
24113 if (flag_eliminate_unused_debug_types)
24114 prune_unused_types ();
24116 /* Generate separate COMDAT sections for type DIEs. */
24117 if (use_debug_types)
24119 break_out_comdat_types (comp_unit_die ());
24121 /* Each new type_unit DIE was added to the limbo die list when created.
24122 Since these have all been added to comdat_type_list, clear the
24123 limbo die list. */
24124 limbo_die_list = NULL;
24126 /* For each new comdat type unit, copy declarations for incomplete
24127 types to make the new unit self-contained (i.e., no direct
24128 references to the main compile unit). */
24129 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24130 copy_decls_for_unworthy_types (ctnode->root_die);
24131 copy_decls_for_unworthy_types (comp_unit_die ());
24133 /* In the process of copying declarations from one unit to another,
24134 we may have left some declarations behind that are no longer
24135 referenced. Prune them. */
24136 prune_unused_types ();
24139 /* Generate separate CUs for each of the include files we've seen.
24140 They will go into limbo_die_list. */
24141 if (flag_eliminate_dwarf2_dups)
24142 break_out_includes (comp_unit_die ());
24144 /* Traverse the DIE's and add add sibling attributes to those DIE's
24145 that have children. */
24146 add_sibling_attributes (comp_unit_die ());
24147 for (node = limbo_die_list; node; node = node->next)
24148 add_sibling_attributes (node->die);
24149 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24150 add_sibling_attributes (ctnode->root_die);
24152 /* When splitting DWARF info, we put some attributes in the
24153 skeleton compile_unit DIE that remains in the .o, while
24154 most attributes go in the DWO compile_unit_die. */
24155 if (dwarf_split_debug_info)
24156 main_comp_unit_die = gen_compile_unit_die (NULL);
24157 else
24158 main_comp_unit_die = comp_unit_die ();
24160 /* Output a terminator label for the .text section. */
24161 switch_to_section (text_section);
24162 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24163 if (cold_text_section)
24165 switch_to_section (cold_text_section);
24166 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24169 /* We can only use the low/high_pc attributes if all of the code was
24170 in .text. */
24171 if (!have_multiple_function_sections
24172 || (dwarf_version < 3 && dwarf_strict))
24174 /* Don't add if the CU has no associated code. */
24175 if (text_section_used)
24176 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24177 text_end_label, true);
24179 else
24181 unsigned fde_idx;
24182 dw_fde_ref fde;
24183 bool range_list_added = false;
24185 if (text_section_used)
24186 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24187 text_end_label, &range_list_added, true);
24188 if (cold_text_section_used)
24189 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24190 cold_end_label, &range_list_added, true);
24192 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24194 if (DECL_IGNORED_P (fde->decl))
24195 continue;
24196 if (!fde->in_std_section)
24197 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24198 fde->dw_fde_end, &range_list_added,
24199 true);
24200 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24201 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24202 fde->dw_fde_second_end, &range_list_added,
24203 true);
24206 if (range_list_added)
24208 /* We need to give .debug_loc and .debug_ranges an appropriate
24209 "base address". Use zero so that these addresses become
24210 absolute. Historically, we've emitted the unexpected
24211 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24212 Emit both to give time for other tools to adapt. */
24213 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24214 if (! dwarf_strict && dwarf_version < 4)
24215 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24217 add_ranges (NULL);
24221 if (debug_info_level >= DINFO_LEVEL_TERSE)
24222 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24223 debug_line_section_label);
24225 if (have_macinfo)
24226 add_AT_macptr (comp_unit_die (),
24227 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24228 macinfo_section_label);
24230 if (dwarf_split_debug_info && addr_index_table != NULL)
24232 /* optimize_location_lists calculates the size of the lists,
24233 so index them first, and assign indices to the entries.
24234 Although optimize_location_lists will remove entries from
24235 the table, it only does so for duplicates, and therefore
24236 only reduces ref_counts to 1. */
24237 unsigned int index = 0;
24238 index_location_lists (comp_unit_die ());
24239 htab_traverse_noresize (addr_index_table,
24240 index_addr_table_entry, &index);
24242 if (have_location_lists)
24243 optimize_location_lists (comp_unit_die ());
24245 save_macinfo_strings ();
24247 if (dwarf_split_debug_info)
24249 unsigned int index = 0;
24251 /* Add attributes common to skeleton compile_units and
24252 type_units. Because these attributes include strings, it
24253 must be done before freezing the string table. Top-level
24254 skeleton die attrs are added when the skeleton type unit is
24255 created, so ensure it is created by this point. */
24256 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24257 (void) get_skeleton_type_unit ();
24258 htab_traverse_noresize (debug_str_hash, index_string, &index);
24261 /* Output all of the compilation units. We put the main one last so that
24262 the offsets are available to output_pubnames. */
24263 for (node = limbo_die_list; node; node = node->next)
24264 output_comp_unit (node->die, 0);
24266 comdat_type_table.create (100);
24267 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24269 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24271 /* Don't output duplicate types. */
24272 if (*slot != HTAB_EMPTY_ENTRY)
24273 continue;
24275 /* Add a pointer to the line table for the main compilation unit
24276 so that the debugger can make sense of DW_AT_decl_file
24277 attributes. */
24278 if (debug_info_level >= DINFO_LEVEL_TERSE)
24279 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24280 (!dwarf_split_debug_info
24281 ? debug_line_section_label
24282 : debug_skeleton_line_section_label));
24284 output_comdat_type_unit (ctnode);
24285 *slot = ctnode;
24287 comdat_type_table.dispose ();
24289 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24290 both the main_cu and all skeleton TUs. Making this call unconditional
24291 would end up either adding a second copy of the AT_pubnames attribute, or
24292 requiring a special case in add_top_level_skeleton_die_attrs. */
24293 if (!dwarf_split_debug_info)
24294 add_AT_pubnames (comp_unit_die ());
24296 if (dwarf_split_debug_info)
24298 int mark;
24299 unsigned char checksum[16];
24300 struct md5_ctx ctx;
24302 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24303 md5_init_ctx (&ctx);
24304 mark = 0;
24305 die_checksum (comp_unit_die (), &ctx, &mark);
24306 unmark_all_dies (comp_unit_die ());
24307 md5_finish_ctx (&ctx, checksum);
24309 /* Use the first 8 bytes of the checksum as the dwo_id,
24310 and add it to both comp-unit DIEs. */
24311 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24312 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24314 /* Add the base offset of the ranges table to the skeleton
24315 comp-unit DIE. */
24316 if (ranges_table_in_use)
24317 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24318 ranges_section_label);
24320 switch_to_section (debug_addr_section);
24321 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24322 output_addr_table ();
24325 /* Output the main compilation unit if non-empty or if .debug_macinfo
24326 or .debug_macro will be emitted. */
24327 output_comp_unit (comp_unit_die (), have_macinfo);
24329 if (dwarf_split_debug_info && info_section_emitted)
24330 output_skeleton_debug_sections (main_comp_unit_die);
24332 /* Output the abbreviation table. */
24333 if (abbrev_die_table_in_use != 1)
24335 switch_to_section (debug_abbrev_section);
24336 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24337 output_abbrev_section ();
24340 /* Output location list section if necessary. */
24341 if (have_location_lists)
24343 /* Output the location lists info. */
24344 switch_to_section (debug_loc_section);
24345 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24346 output_location_lists (comp_unit_die ());
24349 output_pubtables ();
24351 /* Output the address range information if a CU (.debug_info section)
24352 was emitted. We output an empty table even if we had no functions
24353 to put in it. This because the consumer has no way to tell the
24354 difference between an empty table that we omitted and failure to
24355 generate a table that would have contained data. */
24356 if (info_section_emitted)
24358 unsigned long aranges_length = size_of_aranges ();
24360 switch_to_section (debug_aranges_section);
24361 output_aranges (aranges_length);
24364 /* Output ranges section if necessary. */
24365 if (ranges_table_in_use)
24367 switch_to_section (debug_ranges_section);
24368 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24369 output_ranges ();
24372 /* Have to end the macro section. */
24373 if (have_macinfo)
24375 switch_to_section (debug_macinfo_section);
24376 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24377 output_macinfo ();
24378 dw2_asm_output_data (1, 0, "End compilation unit");
24381 /* Output the source line correspondence table. We must do this
24382 even if there is no line information. Otherwise, on an empty
24383 translation unit, we will generate a present, but empty,
24384 .debug_info section. IRIX 6.5 `nm' will then complain when
24385 examining the file. This is done late so that any filenames
24386 used by the debug_info section are marked as 'used'. */
24387 switch_to_section (debug_line_section);
24388 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24389 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24390 output_line_info (false);
24392 if (dwarf_split_debug_info && info_section_emitted)
24394 switch_to_section (debug_skeleton_line_section);
24395 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24396 output_line_info (true);
24399 /* If we emitted any indirect strings, output the string table too. */
24400 if (debug_str_hash || skeleton_debug_str_hash)
24401 output_indirect_strings ();
24404 #include "gt-dwarf2out.h"