2013-11-29 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / dwarf2out.c
blob14d1bf77f227e8d5f82509b02ff4a8c57c632180
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "tree-dfa.h"
99 #include "gdb/gdb-index.h"
101 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
102 static rtx last_var_location_insn;
103 static rtx cached_next_real_insn;
105 #ifdef VMS_DEBUGGING_INFO
106 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
108 /* Define this macro to be a nonzero value if the directory specifications
109 which are output in the debug info should end with a separator. */
110 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
111 /* Define this macro to evaluate to a nonzero value if GCC should refrain
112 from generating indirect strings in DWARF2 debug information, for instance
113 if your target is stuck with an old version of GDB that is unable to
114 process them properly or uses VMS Debug. */
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
116 #else
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
119 #endif
121 /* ??? Poison these here until it can be done generically. They've been
122 totally replaced in this file; make sure it stays that way. */
123 #undef DWARF2_UNWIND_INFO
124 #undef DWARF2_FRAME_INFO
125 #if (GCC_VERSION >= 3000)
126 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
127 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 vec<tree, va_gc> *because we want to tell the garbage collector about
141 it. */
142 static GTY(()) vec<tree, va_gc> *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) vec<tree, va_gc> *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_skeleton_info_section;
154 static GTY(()) section *debug_abbrev_section;
155 static GTY(()) section *debug_skeleton_abbrev_section;
156 static GTY(()) section *debug_aranges_section;
157 static GTY(()) section *debug_addr_section;
158 static GTY(()) section *debug_macinfo_section;
159 static GTY(()) section *debug_line_section;
160 static GTY(()) section *debug_skeleton_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_str_dwo_section;
166 static GTY(()) section *debug_str_offsets_section;
167 static GTY(()) section *debug_ranges_section;
168 static GTY(()) section *debug_frame_section;
170 /* Maximum size (in bytes) of an artificially generated label. */
171 #define MAX_ARTIFICIAL_LABEL_BYTES 30
173 /* According to the (draft) DWARF 3 specification, the initial length
174 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
175 bytes are 0xffffffff, followed by the length stored in the next 8
176 bytes.
178 However, the SGI/MIPS ABI uses an initial length which is equal to
179 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
181 #ifndef DWARF_INITIAL_LENGTH_SIZE
182 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
183 #endif
185 /* Round SIZE up to the nearest BOUNDARY. */
186 #define DWARF_ROUND(SIZE,BOUNDARY) \
187 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
189 /* CIE identifier. */
190 #if HOST_BITS_PER_WIDE_INT >= 64
191 #define DWARF_CIE_ID \
192 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
193 #else
194 #define DWARF_CIE_ID DW_CIE_ID
195 #endif
198 /* A vector for a table that contains frame description
199 information for each routine. */
200 #define NOT_INDEXED (-1U)
201 #define NO_INDEX_ASSIGNED (-2U)
203 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
205 struct GTY(()) indirect_string_node {
206 const char *str;
207 unsigned int refcount;
208 enum dwarf_form form;
209 char *label;
210 unsigned int index;
213 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
215 /* With split_debug_info, both the comp_dir and dwo_name go in the
216 main object file, rather than the dwo, similar to the force_direct
217 parameter elsewhere but with additional complications:
219 1) The string is needed in both the main object file and the dwo.
220 That is, the comp_dir and dwo_name will appear in both places.
222 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
223 DW_FORM_GNU_str_index.
225 3) GCC chooses the form to use late, depending on the size and
226 reference count.
228 Rather than forcing the all debug string handling functions and
229 callers to deal with these complications, simply use a separate,
230 special-cased string table for any attribute that should go in the
231 main object file. This limits the complexity to just the places
232 that need it. */
234 static GTY ((param_is (struct indirect_string_node)))
235 htab_t skeleton_debug_str_hash;
237 static GTY(()) int dw2_string_counter;
239 /* True if the compilation unit places functions in more than one section. */
240 static GTY(()) bool have_multiple_function_sections = false;
242 /* Whether the default text and cold text sections have been used at all. */
244 static GTY(()) bool text_section_used = false;
245 static GTY(()) bool cold_text_section_used = false;
247 /* The default cold text section. */
248 static GTY(()) section *cold_text_section;
250 /* Forward declarations for functions defined in this file. */
252 static char *stripattributes (const char *);
253 static void output_call_frame_info (int);
254 static void dwarf2out_note_section_used (void);
256 /* Personality decl of current unit. Used only when assembler does not support
257 personality CFI. */
258 static GTY(()) rtx current_unit_personality;
260 /* Data and reference forms for relocatable data. */
261 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
262 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
264 #ifndef DEBUG_FRAME_SECTION
265 #define DEBUG_FRAME_SECTION ".debug_frame"
266 #endif
268 #ifndef FUNC_BEGIN_LABEL
269 #define FUNC_BEGIN_LABEL "LFB"
270 #endif
272 #ifndef FUNC_END_LABEL
273 #define FUNC_END_LABEL "LFE"
274 #endif
276 #ifndef PROLOGUE_END_LABEL
277 #define PROLOGUE_END_LABEL "LPE"
278 #endif
280 #ifndef EPILOGUE_BEGIN_LABEL
281 #define EPILOGUE_BEGIN_LABEL "LEB"
282 #endif
284 #ifndef FRAME_BEGIN_LABEL
285 #define FRAME_BEGIN_LABEL "Lframe"
286 #endif
287 #define CIE_AFTER_SIZE_LABEL "LSCIE"
288 #define CIE_END_LABEL "LECIE"
289 #define FDE_LABEL "LSFDE"
290 #define FDE_AFTER_SIZE_LABEL "LASFDE"
291 #define FDE_END_LABEL "LEFDE"
292 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
293 #define LINE_NUMBER_END_LABEL "LELT"
294 #define LN_PROLOG_AS_LABEL "LASLTP"
295 #define LN_PROLOG_END_LABEL "LELTP"
296 #define DIE_LABEL_PREFIX "DW"
298 /* Match the base name of a file to the base name of a compilation unit. */
300 static int
301 matches_main_base (const char *path)
303 /* Cache the last query. */
304 static const char *last_path = NULL;
305 static int last_match = 0;
306 if (path != last_path)
308 const char *base;
309 int length = base_of_path (path, &base);
310 last_path = path;
311 last_match = (length == main_input_baselength
312 && memcmp (base, main_input_basename, length) == 0);
314 return last_match;
317 #ifdef DEBUG_DEBUG_STRUCT
319 static int
320 dump_struct_debug (tree type, enum debug_info_usage usage,
321 enum debug_struct_file criterion, int generic,
322 int matches, int result)
324 /* Find the type name. */
325 tree type_decl = TYPE_STUB_DECL (type);
326 tree t = type_decl;
327 const char *name = 0;
328 if (TREE_CODE (t) == TYPE_DECL)
329 t = DECL_NAME (t);
330 if (t)
331 name = IDENTIFIER_POINTER (t);
333 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
334 criterion,
335 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
336 matches ? "bas" : "hdr",
337 generic ? "gen" : "ord",
338 usage == DINFO_USAGE_DFN ? ";" :
339 usage == DINFO_USAGE_DIR_USE ? "." : "*",
340 result,
341 (void*) type_decl, name);
342 return result;
344 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
345 dump_struct_debug (type, usage, criterion, generic, matches, result)
347 #else
349 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
350 (result)
352 #endif
354 static bool
355 should_emit_struct_debug (tree type, enum debug_info_usage usage)
357 enum debug_struct_file criterion;
358 tree type_decl;
359 bool generic = lang_hooks.types.generic_p (type);
361 if (generic)
362 criterion = debug_struct_generic[usage];
363 else
364 criterion = debug_struct_ordinary[usage];
366 if (criterion == DINFO_STRUCT_FILE_NONE)
367 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
368 if (criterion == DINFO_STRUCT_FILE_ANY)
369 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
371 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
373 if (type_decl != NULL)
375 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
376 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
378 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
379 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
382 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
385 /* Return a pointer to a copy of the section string name S with all
386 attributes stripped off, and an asterisk prepended (for assemble_name). */
388 static inline char *
389 stripattributes (const char *s)
391 char *stripped = XNEWVEC (char, strlen (s) + 2);
392 char *p = stripped;
394 *p++ = '*';
396 while (*s && *s != ',')
397 *p++ = *s++;
399 *p = '\0';
400 return stripped;
403 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
404 switch to the data section instead, and write out a synthetic start label
405 for collect2 the first time around. */
407 static void
408 switch_to_eh_frame_section (bool back)
410 tree label;
412 #ifdef EH_FRAME_SECTION_NAME
413 if (eh_frame_section == 0)
415 int flags;
417 if (EH_TABLES_CAN_BE_READ_ONLY)
419 int fde_encoding;
420 int per_encoding;
421 int lsda_encoding;
423 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
424 /*global=*/0);
425 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
426 /*global=*/1);
427 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
428 /*global=*/0);
429 flags = ((! flag_pic
430 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
431 && (fde_encoding & 0x70) != DW_EH_PE_aligned
432 && (per_encoding & 0x70) != DW_EH_PE_absptr
433 && (per_encoding & 0x70) != DW_EH_PE_aligned
434 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
435 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
436 ? 0 : SECTION_WRITE);
438 else
439 flags = SECTION_WRITE;
440 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
442 #endif /* EH_FRAME_SECTION_NAME */
444 if (eh_frame_section)
445 switch_to_section (eh_frame_section);
446 else
448 /* We have no special eh_frame section. Put the information in
449 the data section and emit special labels to guide collect2. */
450 switch_to_section (data_section);
452 if (!back)
454 label = get_file_function_name ("F");
455 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
456 targetm.asm_out.globalize_label (asm_out_file,
457 IDENTIFIER_POINTER (label));
458 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
463 /* Switch [BACK] to the eh or debug frame table section, depending on
464 FOR_EH. */
466 static void
467 switch_to_frame_table_section (int for_eh, bool back)
469 if (for_eh)
470 switch_to_eh_frame_section (back);
471 else
473 if (!debug_frame_section)
474 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
475 SECTION_DEBUG, NULL);
476 switch_to_section (debug_frame_section);
480 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
482 enum dw_cfi_oprnd_type
483 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
485 switch (cfi)
487 case DW_CFA_nop:
488 case DW_CFA_GNU_window_save:
489 case DW_CFA_remember_state:
490 case DW_CFA_restore_state:
491 return dw_cfi_oprnd_unused;
493 case DW_CFA_set_loc:
494 case DW_CFA_advance_loc1:
495 case DW_CFA_advance_loc2:
496 case DW_CFA_advance_loc4:
497 case DW_CFA_MIPS_advance_loc8:
498 return dw_cfi_oprnd_addr;
500 case DW_CFA_offset:
501 case DW_CFA_offset_extended:
502 case DW_CFA_def_cfa:
503 case DW_CFA_offset_extended_sf:
504 case DW_CFA_def_cfa_sf:
505 case DW_CFA_restore:
506 case DW_CFA_restore_extended:
507 case DW_CFA_undefined:
508 case DW_CFA_same_value:
509 case DW_CFA_def_cfa_register:
510 case DW_CFA_register:
511 case DW_CFA_expression:
512 return dw_cfi_oprnd_reg_num;
514 case DW_CFA_def_cfa_offset:
515 case DW_CFA_GNU_args_size:
516 case DW_CFA_def_cfa_offset_sf:
517 return dw_cfi_oprnd_offset;
519 case DW_CFA_def_cfa_expression:
520 return dw_cfi_oprnd_loc;
522 default:
523 gcc_unreachable ();
527 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
529 enum dw_cfi_oprnd_type
530 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
532 switch (cfi)
534 case DW_CFA_def_cfa:
535 case DW_CFA_def_cfa_sf:
536 case DW_CFA_offset:
537 case DW_CFA_offset_extended_sf:
538 case DW_CFA_offset_extended:
539 return dw_cfi_oprnd_offset;
541 case DW_CFA_register:
542 return dw_cfi_oprnd_reg_num;
544 case DW_CFA_expression:
545 return dw_cfi_oprnd_loc;
547 default:
548 return dw_cfi_oprnd_unused;
552 /* Output one FDE. */
554 static void
555 output_fde (dw_fde_ref fde, bool for_eh, bool second,
556 char *section_start_label, int fde_encoding, char *augmentation,
557 bool any_lsda_needed, int lsda_encoding)
559 const char *begin, *end;
560 static unsigned int j;
561 char l1[20], l2[20];
563 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
564 /* empty */ 0);
565 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
566 for_eh + j);
567 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
568 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
569 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
570 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
571 " indicating 64-bit DWARF extension");
572 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
573 "FDE Length");
574 ASM_OUTPUT_LABEL (asm_out_file, l1);
576 if (for_eh)
577 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
578 else
579 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
580 debug_frame_section, "FDE CIE offset");
582 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
583 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
585 if (for_eh)
587 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
588 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
589 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
590 "FDE initial location");
591 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
592 end, begin, "FDE address range");
594 else
596 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
597 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
600 if (augmentation[0])
602 if (any_lsda_needed)
604 int size = size_of_encoded_value (lsda_encoding);
606 if (lsda_encoding == DW_EH_PE_aligned)
608 int offset = ( 4 /* Length */
609 + 4 /* CIE offset */
610 + 2 * size_of_encoded_value (fde_encoding)
611 + 1 /* Augmentation size */ );
612 int pad = -offset & (PTR_SIZE - 1);
614 size += pad;
615 gcc_assert (size_of_uleb128 (size) == 1);
618 dw2_asm_output_data_uleb128 (size, "Augmentation size");
620 if (fde->uses_eh_lsda)
622 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
623 fde->funcdef_number);
624 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
625 gen_rtx_SYMBOL_REF (Pmode, l1),
626 false,
627 "Language Specific Data Area");
629 else
631 if (lsda_encoding == DW_EH_PE_aligned)
632 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
633 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
634 "Language Specific Data Area (none)");
637 else
638 dw2_asm_output_data_uleb128 (0, "Augmentation size");
641 /* Loop through the Call Frame Instructions associated with this FDE. */
642 fde->dw_fde_current_label = begin;
644 size_t from, until, i;
646 from = 0;
647 until = vec_safe_length (fde->dw_fde_cfi);
649 if (fde->dw_fde_second_begin == NULL)
651 else if (!second)
652 until = fde->dw_fde_switch_cfi_index;
653 else
654 from = fde->dw_fde_switch_cfi_index;
656 for (i = from; i < until; i++)
657 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
660 /* If we are to emit a ref/link from function bodies to their frame tables,
661 do it now. This is typically performed to make sure that tables
662 associated with functions are dragged with them and not discarded in
663 garbage collecting links. We need to do this on a per function basis to
664 cope with -ffunction-sections. */
666 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
667 /* Switch to the function section, emit the ref to the tables, and
668 switch *back* into the table section. */
669 switch_to_section (function_section (fde->decl));
670 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
671 switch_to_frame_table_section (for_eh, true);
672 #endif
674 /* Pad the FDE out to an address sized boundary. */
675 ASM_OUTPUT_ALIGN (asm_out_file,
676 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
677 ASM_OUTPUT_LABEL (asm_out_file, l2);
679 j += 2;
682 /* Return true if frame description entry FDE is needed for EH. */
684 static bool
685 fde_needed_for_eh_p (dw_fde_ref fde)
687 if (flag_asynchronous_unwind_tables)
688 return true;
690 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
691 return true;
693 if (fde->uses_eh_lsda)
694 return true;
696 /* If exceptions are enabled, we have collected nothrow info. */
697 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
698 return false;
700 return true;
703 /* Output the call frame information used to record information
704 that relates to calculating the frame pointer, and records the
705 location of saved registers. */
707 static void
708 output_call_frame_info (int for_eh)
710 unsigned int i;
711 dw_fde_ref fde;
712 dw_cfi_ref cfi;
713 char l1[20], l2[20], section_start_label[20];
714 bool any_lsda_needed = false;
715 char augmentation[6];
716 int augmentation_size;
717 int fde_encoding = DW_EH_PE_absptr;
718 int per_encoding = DW_EH_PE_absptr;
719 int lsda_encoding = DW_EH_PE_absptr;
720 int return_reg;
721 rtx personality = NULL;
722 int dw_cie_version;
724 /* Don't emit a CIE if there won't be any FDEs. */
725 if (!fde_vec)
726 return;
728 /* Nothing to do if the assembler's doing it all. */
729 if (dwarf2out_do_cfi_asm ())
730 return;
732 /* If we don't have any functions we'll want to unwind out of, don't emit
733 any EH unwind information. If we make FDEs linkonce, we may have to
734 emit an empty label for an FDE that wouldn't otherwise be emitted. We
735 want to avoid having an FDE kept around when the function it refers to
736 is discarded. Example where this matters: a primary function template
737 in C++ requires EH information, an explicit specialization doesn't. */
738 if (for_eh)
740 bool any_eh_needed = false;
742 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
744 if (fde->uses_eh_lsda)
745 any_eh_needed = any_lsda_needed = true;
746 else if (fde_needed_for_eh_p (fde))
747 any_eh_needed = true;
748 else if (TARGET_USES_WEAK_UNWIND_INFO)
749 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
752 if (!any_eh_needed)
753 return;
756 /* We're going to be generating comments, so turn on app. */
757 if (flag_debug_asm)
758 app_enable ();
760 /* Switch to the proper frame section, first time. */
761 switch_to_frame_table_section (for_eh, false);
763 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
764 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
766 /* Output the CIE. */
767 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
768 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
769 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
770 dw2_asm_output_data (4, 0xffffffff,
771 "Initial length escape value indicating 64-bit DWARF extension");
772 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
773 "Length of Common Information Entry");
774 ASM_OUTPUT_LABEL (asm_out_file, l1);
776 /* Now that the CIE pointer is PC-relative for EH,
777 use 0 to identify the CIE. */
778 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
779 (for_eh ? 0 : DWARF_CIE_ID),
780 "CIE Identifier Tag");
782 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
783 use CIE version 1, unless that would produce incorrect results
784 due to overflowing the return register column. */
785 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
786 dw_cie_version = 1;
787 if (return_reg >= 256 || dwarf_version > 2)
788 dw_cie_version = 3;
789 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
791 augmentation[0] = 0;
792 augmentation_size = 0;
794 personality = current_unit_personality;
795 if (for_eh)
797 char *p;
799 /* Augmentation:
800 z Indicates that a uleb128 is present to size the
801 augmentation section.
802 L Indicates the encoding (and thus presence) of
803 an LSDA pointer in the FDE augmentation.
804 R Indicates a non-default pointer encoding for
805 FDE code pointers.
806 P Indicates the presence of an encoding + language
807 personality routine in the CIE augmentation. */
809 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
810 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
811 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
813 p = augmentation + 1;
814 if (personality)
816 *p++ = 'P';
817 augmentation_size += 1 + size_of_encoded_value (per_encoding);
818 assemble_external_libcall (personality);
820 if (any_lsda_needed)
822 *p++ = 'L';
823 augmentation_size += 1;
825 if (fde_encoding != DW_EH_PE_absptr)
827 *p++ = 'R';
828 augmentation_size += 1;
830 if (p > augmentation + 1)
832 augmentation[0] = 'z';
833 *p = '\0';
836 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
837 if (personality && per_encoding == DW_EH_PE_aligned)
839 int offset = ( 4 /* Length */
840 + 4 /* CIE Id */
841 + 1 /* CIE version */
842 + strlen (augmentation) + 1 /* Augmentation */
843 + size_of_uleb128 (1) /* Code alignment */
844 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
845 + 1 /* RA column */
846 + 1 /* Augmentation size */
847 + 1 /* Personality encoding */ );
848 int pad = -offset & (PTR_SIZE - 1);
850 augmentation_size += pad;
852 /* Augmentations should be small, so there's scarce need to
853 iterate for a solution. Die if we exceed one uleb128 byte. */
854 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
858 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
859 if (dw_cie_version >= 4)
861 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
862 dw2_asm_output_data (1, 0, "CIE Segment Size");
864 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
865 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
866 "CIE Data Alignment Factor");
868 if (dw_cie_version == 1)
869 dw2_asm_output_data (1, return_reg, "CIE RA Column");
870 else
871 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
873 if (augmentation[0])
875 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
876 if (personality)
878 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
879 eh_data_format_name (per_encoding));
880 dw2_asm_output_encoded_addr_rtx (per_encoding,
881 personality,
882 true, NULL);
885 if (any_lsda_needed)
886 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
887 eh_data_format_name (lsda_encoding));
889 if (fde_encoding != DW_EH_PE_absptr)
890 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
891 eh_data_format_name (fde_encoding));
894 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
895 output_cfi (cfi, NULL, for_eh);
897 /* Pad the CIE out to an address sized boundary. */
898 ASM_OUTPUT_ALIGN (asm_out_file,
899 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
900 ASM_OUTPUT_LABEL (asm_out_file, l2);
902 /* Loop through all of the FDE's. */
903 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
905 unsigned int k;
907 /* Don't emit EH unwind info for leaf functions that don't need it. */
908 if (for_eh && !fde_needed_for_eh_p (fde))
909 continue;
911 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
912 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
913 augmentation, any_lsda_needed, lsda_encoding);
916 if (for_eh && targetm.terminate_dw2_eh_frame_info)
917 dw2_asm_output_data (4, 0, "End of Table");
919 /* Turn off app to make assembly quicker. */
920 if (flag_debug_asm)
921 app_disable ();
924 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
926 static void
927 dwarf2out_do_cfi_startproc (bool second)
929 int enc;
930 rtx ref;
931 rtx personality = get_personality_function (current_function_decl);
933 fprintf (asm_out_file, "\t.cfi_startproc\n");
935 if (personality)
937 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
938 ref = personality;
940 /* ??? The GAS support isn't entirely consistent. We have to
941 handle indirect support ourselves, but PC-relative is done
942 in the assembler. Further, the assembler can't handle any
943 of the weirder relocation types. */
944 if (enc & DW_EH_PE_indirect)
945 ref = dw2_force_const_mem (ref, true);
947 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
948 output_addr_const (asm_out_file, ref);
949 fputc ('\n', asm_out_file);
952 if (crtl->uses_eh_lsda)
954 char lab[20];
956 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
957 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
958 current_function_funcdef_no);
959 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
960 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
962 if (enc & DW_EH_PE_indirect)
963 ref = dw2_force_const_mem (ref, true);
965 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
966 output_addr_const (asm_out_file, ref);
967 fputc ('\n', asm_out_file);
971 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
972 this allocation may be done before pass_final. */
974 dw_fde_ref
975 dwarf2out_alloc_current_fde (void)
977 dw_fde_ref fde;
979 fde = ggc_alloc_cleared_dw_fde_node ();
980 fde->decl = current_function_decl;
981 fde->funcdef_number = current_function_funcdef_no;
982 fde->fde_index = vec_safe_length (fde_vec);
983 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
984 fde->uses_eh_lsda = crtl->uses_eh_lsda;
985 fde->nothrow = crtl->nothrow;
986 fde->drap_reg = INVALID_REGNUM;
987 fde->vdrap_reg = INVALID_REGNUM;
989 /* Record the FDE associated with this function. */
990 cfun->fde = fde;
991 vec_safe_push (fde_vec, fde);
993 return fde;
996 /* Output a marker (i.e. a label) for the beginning of a function, before
997 the prologue. */
999 void
1000 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1001 const char *file ATTRIBUTE_UNUSED)
1003 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1004 char * dup_label;
1005 dw_fde_ref fde;
1006 section *fnsec;
1007 bool do_frame;
1009 current_function_func_begin_label = NULL;
1011 do_frame = dwarf2out_do_frame ();
1013 /* ??? current_function_func_begin_label is also used by except.c for
1014 call-site information. We must emit this label if it might be used. */
1015 if (!do_frame
1016 && (!flag_exceptions
1017 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1018 return;
1020 fnsec = function_section (current_function_decl);
1021 switch_to_section (fnsec);
1022 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1023 current_function_funcdef_no);
1024 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1025 current_function_funcdef_no);
1026 dup_label = xstrdup (label);
1027 current_function_func_begin_label = dup_label;
1029 /* We can elide the fde allocation if we're not emitting debug info. */
1030 if (!do_frame)
1031 return;
1033 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1034 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1035 would include pass_dwarf2_frame. If we've not created the FDE yet,
1036 do so now. */
1037 fde = cfun->fde;
1038 if (fde == NULL)
1039 fde = dwarf2out_alloc_current_fde ();
1041 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1042 fde->dw_fde_begin = dup_label;
1043 fde->dw_fde_current_label = dup_label;
1044 fde->in_std_section = (fnsec == text_section
1045 || (cold_text_section && fnsec == cold_text_section));
1047 /* We only want to output line number information for the genuine dwarf2
1048 prologue case, not the eh frame case. */
1049 #ifdef DWARF2_DEBUGGING_INFO
1050 if (file)
1051 dwarf2out_source_line (line, file, 0, true);
1052 #endif
1054 if (dwarf2out_do_cfi_asm ())
1055 dwarf2out_do_cfi_startproc (false);
1056 else
1058 rtx personality = get_personality_function (current_function_decl);
1059 if (!current_unit_personality)
1060 current_unit_personality = personality;
1062 /* We cannot keep a current personality per function as without CFI
1063 asm, at the point where we emit the CFI data, there is no current
1064 function anymore. */
1065 if (personality && current_unit_personality != personality)
1066 sorry ("multiple EH personalities are supported only with assemblers "
1067 "supporting .cfi_personality directive");
1071 /* Output a marker (i.e. a label) for the end of the generated code
1072 for a function prologue. This gets called *after* the prologue code has
1073 been generated. */
1075 void
1076 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1077 const char *file ATTRIBUTE_UNUSED)
1079 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1081 /* Output a label to mark the endpoint of the code generated for this
1082 function. */
1083 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1084 current_function_funcdef_no);
1085 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1086 current_function_funcdef_no);
1087 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1090 /* Output a marker (i.e. a label) for the beginning of the generated code
1091 for a function epilogue. This gets called *before* the prologue code has
1092 been generated. */
1094 void
1095 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1096 const char *file ATTRIBUTE_UNUSED)
1098 dw_fde_ref fde = cfun->fde;
1099 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1101 if (fde->dw_fde_vms_begin_epilogue)
1102 return;
1104 /* Output a label to mark the endpoint of the code generated for this
1105 function. */
1106 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1107 current_function_funcdef_no);
1108 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1109 current_function_funcdef_no);
1110 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1113 /* Output a marker (i.e. a label) for the absolute end of the generated code
1114 for a function definition. This gets called *after* the epilogue code has
1115 been generated. */
1117 void
1118 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1119 const char *file ATTRIBUTE_UNUSED)
1121 dw_fde_ref fde;
1122 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1124 last_var_location_insn = NULL_RTX;
1125 cached_next_real_insn = NULL_RTX;
1127 if (dwarf2out_do_cfi_asm ())
1128 fprintf (asm_out_file, "\t.cfi_endproc\n");
1130 /* Output a label to mark the endpoint of the code generated for this
1131 function. */
1132 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1133 current_function_funcdef_no);
1134 ASM_OUTPUT_LABEL (asm_out_file, label);
1135 fde = cfun->fde;
1136 gcc_assert (fde != NULL);
1137 if (fde->dw_fde_second_begin == NULL)
1138 fde->dw_fde_end = xstrdup (label);
1141 void
1142 dwarf2out_frame_finish (void)
1144 /* Output call frame information. */
1145 if (targetm.debug_unwind_info () == UI_DWARF2)
1146 output_call_frame_info (0);
1148 /* Output another copy for the unwinder. */
1149 if ((flag_unwind_tables || flag_exceptions)
1150 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1151 output_call_frame_info (1);
1154 /* Note that the current function section is being used for code. */
1156 static void
1157 dwarf2out_note_section_used (void)
1159 section *sec = current_function_section ();
1160 if (sec == text_section)
1161 text_section_used = true;
1162 else if (sec == cold_text_section)
1163 cold_text_section_used = true;
1166 static void var_location_switch_text_section (void);
1167 static void set_cur_line_info_table (section *);
1169 void
1170 dwarf2out_switch_text_section (void)
1172 section *sect;
1173 dw_fde_ref fde = cfun->fde;
1175 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1177 if (!in_cold_section_p)
1179 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1180 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1181 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1183 else
1185 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1186 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1187 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1189 have_multiple_function_sections = true;
1191 /* There is no need to mark used sections when not debugging. */
1192 if (cold_text_section != NULL)
1193 dwarf2out_note_section_used ();
1195 if (dwarf2out_do_cfi_asm ())
1196 fprintf (asm_out_file, "\t.cfi_endproc\n");
1198 /* Now do the real section switch. */
1199 sect = current_function_section ();
1200 switch_to_section (sect);
1202 fde->second_in_std_section
1203 = (sect == text_section
1204 || (cold_text_section && sect == cold_text_section));
1206 if (dwarf2out_do_cfi_asm ())
1207 dwarf2out_do_cfi_startproc (true);
1209 var_location_switch_text_section ();
1211 if (cold_text_section != NULL)
1212 set_cur_line_info_table (sect);
1215 /* And now, the subset of the debugging information support code necessary
1216 for emitting location expressions. */
1218 /* Data about a single source file. */
1219 struct GTY(()) dwarf_file_data {
1220 const char * filename;
1221 int emitted_number;
1224 typedef struct GTY(()) deferred_locations_struct
1226 tree variable;
1227 dw_die_ref die;
1228 } deferred_locations;
1231 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1234 /* Describe an entry into the .debug_addr section. */
1236 enum ate_kind {
1237 ate_kind_rtx,
1238 ate_kind_rtx_dtprel,
1239 ate_kind_label
1242 typedef struct GTY(()) addr_table_entry_struct {
1243 enum ate_kind kind;
1244 unsigned int refcount;
1245 unsigned int index;
1246 union addr_table_entry_struct_union
1248 rtx GTY ((tag ("0"))) rtl;
1249 char * GTY ((tag ("1"))) label;
1251 GTY ((desc ("%1.kind"))) addr;
1253 addr_table_entry;
1255 /* Location lists are ranges + location descriptions for that range,
1256 so you can track variables that are in different places over
1257 their entire life. */
1258 typedef struct GTY(()) dw_loc_list_struct {
1259 dw_loc_list_ref dw_loc_next;
1260 const char *begin; /* Label and addr_entry for start of range */
1261 addr_table_entry *begin_entry;
1262 const char *end; /* Label for end of range */
1263 char *ll_symbol; /* Label for beginning of location list.
1264 Only on head of list */
1265 const char *section; /* Section this loclist is relative to */
1266 dw_loc_descr_ref expr;
1267 hashval_t hash;
1268 /* True if all addresses in this and subsequent lists are known to be
1269 resolved. */
1270 bool resolved_addr;
1271 /* True if this list has been replaced by dw_loc_next. */
1272 bool replaced;
1273 bool emitted;
1274 /* True if the range should be emitted even if begin and end
1275 are the same. */
1276 bool force;
1277 } dw_loc_list_node;
1279 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1281 /* Convert a DWARF stack opcode into its string name. */
1283 static const char *
1284 dwarf_stack_op_name (unsigned int op)
1286 const char *name = get_DW_OP_name (op);
1288 if (name != NULL)
1289 return name;
1291 return "OP_<unknown>";
1294 /* Return a pointer to a newly allocated location description. Location
1295 descriptions are simple expression terms that can be strung
1296 together to form more complicated location (address) descriptions. */
1298 static inline dw_loc_descr_ref
1299 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1300 unsigned HOST_WIDE_INT oprnd2)
1302 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1304 descr->dw_loc_opc = op;
1305 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1306 descr->dw_loc_oprnd1.val_entry = NULL;
1307 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1308 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1309 descr->dw_loc_oprnd2.val_entry = NULL;
1310 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1312 return descr;
1315 /* Return a pointer to a newly allocated location description for
1316 REG and OFFSET. */
1318 static inline dw_loc_descr_ref
1319 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1321 if (reg <= 31)
1322 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1323 offset, 0);
1324 else
1325 return new_loc_descr (DW_OP_bregx, reg, offset);
1328 /* Add a location description term to a location description expression. */
1330 static inline void
1331 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1333 dw_loc_descr_ref *d;
1335 /* Find the end of the chain. */
1336 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1339 *d = descr;
1342 /* Compare two location operands for exact equality. */
1344 static bool
1345 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1347 if (a->val_class != b->val_class)
1348 return false;
1349 switch (a->val_class)
1351 case dw_val_class_none:
1352 return true;
1353 case dw_val_class_addr:
1354 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1356 case dw_val_class_offset:
1357 case dw_val_class_unsigned_const:
1358 case dw_val_class_const:
1359 case dw_val_class_range_list:
1360 case dw_val_class_lineptr:
1361 case dw_val_class_macptr:
1362 /* These are all HOST_WIDE_INT, signed or unsigned. */
1363 return a->v.val_unsigned == b->v.val_unsigned;
1365 case dw_val_class_loc:
1366 return a->v.val_loc == b->v.val_loc;
1367 case dw_val_class_loc_list:
1368 return a->v.val_loc_list == b->v.val_loc_list;
1369 case dw_val_class_die_ref:
1370 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1371 case dw_val_class_fde_ref:
1372 return a->v.val_fde_index == b->v.val_fde_index;
1373 case dw_val_class_lbl_id:
1374 case dw_val_class_high_pc:
1375 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1376 case dw_val_class_str:
1377 return a->v.val_str == b->v.val_str;
1378 case dw_val_class_flag:
1379 return a->v.val_flag == b->v.val_flag;
1380 case dw_val_class_file:
1381 return a->v.val_file == b->v.val_file;
1382 case dw_val_class_decl_ref:
1383 return a->v.val_decl_ref == b->v.val_decl_ref;
1385 case dw_val_class_const_double:
1386 return (a->v.val_double.high == b->v.val_double.high
1387 && a->v.val_double.low == b->v.val_double.low);
1389 case dw_val_class_vec:
1391 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1392 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1394 return (a_len == b_len
1395 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1398 case dw_val_class_data8:
1399 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1401 case dw_val_class_vms_delta:
1402 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1403 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1405 gcc_unreachable ();
1408 /* Compare two location atoms for exact equality. */
1410 static bool
1411 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1413 if (a->dw_loc_opc != b->dw_loc_opc)
1414 return false;
1416 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1417 address size, but since we always allocate cleared storage it
1418 should be zero for other types of locations. */
1419 if (a->dtprel != b->dtprel)
1420 return false;
1422 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1423 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1426 /* Compare two complete location expressions for exact equality. */
1428 bool
1429 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1431 while (1)
1433 if (a == b)
1434 return true;
1435 if (a == NULL || b == NULL)
1436 return false;
1437 if (!loc_descr_equal_p_1 (a, b))
1438 return false;
1440 a = a->dw_loc_next;
1441 b = b->dw_loc_next;
1446 /* Add a constant OFFSET to a location expression. */
1448 static void
1449 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1451 dw_loc_descr_ref loc;
1452 HOST_WIDE_INT *p;
1454 gcc_assert (*list_head != NULL);
1456 if (!offset)
1457 return;
1459 /* Find the end of the chain. */
1460 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1463 p = NULL;
1464 if (loc->dw_loc_opc == DW_OP_fbreg
1465 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1466 p = &loc->dw_loc_oprnd1.v.val_int;
1467 else if (loc->dw_loc_opc == DW_OP_bregx)
1468 p = &loc->dw_loc_oprnd2.v.val_int;
1470 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1471 offset. Don't optimize if an signed integer overflow would happen. */
1472 if (p != NULL
1473 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1474 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1475 *p += offset;
1477 else if (offset > 0)
1478 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1480 else
1482 loc->dw_loc_next = int_loc_descriptor (-offset);
1483 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1487 /* Add a constant OFFSET to a location list. */
1489 static void
1490 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1492 dw_loc_list_ref d;
1493 for (d = list_head; d != NULL; d = d->dw_loc_next)
1494 loc_descr_plus_const (&d->expr, offset);
1497 #define DWARF_REF_SIZE \
1498 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1500 static unsigned long int get_base_type_offset (dw_die_ref);
1502 /* Return the size of a location descriptor. */
1504 static unsigned long
1505 size_of_loc_descr (dw_loc_descr_ref loc)
1507 unsigned long size = 1;
1509 switch (loc->dw_loc_opc)
1511 case DW_OP_addr:
1512 size += DWARF2_ADDR_SIZE;
1513 break;
1514 case DW_OP_GNU_addr_index:
1515 case DW_OP_GNU_const_index:
1516 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1517 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1518 break;
1519 case DW_OP_const1u:
1520 case DW_OP_const1s:
1521 size += 1;
1522 break;
1523 case DW_OP_const2u:
1524 case DW_OP_const2s:
1525 size += 2;
1526 break;
1527 case DW_OP_const4u:
1528 case DW_OP_const4s:
1529 size += 4;
1530 break;
1531 case DW_OP_const8u:
1532 case DW_OP_const8s:
1533 size += 8;
1534 break;
1535 case DW_OP_constu:
1536 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1537 break;
1538 case DW_OP_consts:
1539 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1540 break;
1541 case DW_OP_pick:
1542 size += 1;
1543 break;
1544 case DW_OP_plus_uconst:
1545 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1546 break;
1547 case DW_OP_skip:
1548 case DW_OP_bra:
1549 size += 2;
1550 break;
1551 case DW_OP_breg0:
1552 case DW_OP_breg1:
1553 case DW_OP_breg2:
1554 case DW_OP_breg3:
1555 case DW_OP_breg4:
1556 case DW_OP_breg5:
1557 case DW_OP_breg6:
1558 case DW_OP_breg7:
1559 case DW_OP_breg8:
1560 case DW_OP_breg9:
1561 case DW_OP_breg10:
1562 case DW_OP_breg11:
1563 case DW_OP_breg12:
1564 case DW_OP_breg13:
1565 case DW_OP_breg14:
1566 case DW_OP_breg15:
1567 case DW_OP_breg16:
1568 case DW_OP_breg17:
1569 case DW_OP_breg18:
1570 case DW_OP_breg19:
1571 case DW_OP_breg20:
1572 case DW_OP_breg21:
1573 case DW_OP_breg22:
1574 case DW_OP_breg23:
1575 case DW_OP_breg24:
1576 case DW_OP_breg25:
1577 case DW_OP_breg26:
1578 case DW_OP_breg27:
1579 case DW_OP_breg28:
1580 case DW_OP_breg29:
1581 case DW_OP_breg30:
1582 case DW_OP_breg31:
1583 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1584 break;
1585 case DW_OP_regx:
1586 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1587 break;
1588 case DW_OP_fbreg:
1589 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590 break;
1591 case DW_OP_bregx:
1592 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1593 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1594 break;
1595 case DW_OP_piece:
1596 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1597 break;
1598 case DW_OP_bit_piece:
1599 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1600 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1601 break;
1602 case DW_OP_deref_size:
1603 case DW_OP_xderef_size:
1604 size += 1;
1605 break;
1606 case DW_OP_call2:
1607 size += 2;
1608 break;
1609 case DW_OP_call4:
1610 size += 4;
1611 break;
1612 case DW_OP_call_ref:
1613 size += DWARF_REF_SIZE;
1614 break;
1615 case DW_OP_implicit_value:
1616 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1617 + loc->dw_loc_oprnd1.v.val_unsigned;
1618 break;
1619 case DW_OP_GNU_implicit_pointer:
1620 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1621 break;
1622 case DW_OP_GNU_entry_value:
1624 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1625 size += size_of_uleb128 (op_size) + op_size;
1626 break;
1628 case DW_OP_GNU_const_type:
1630 unsigned long o
1631 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1632 size += size_of_uleb128 (o) + 1;
1633 switch (loc->dw_loc_oprnd2.val_class)
1635 case dw_val_class_vec:
1636 size += loc->dw_loc_oprnd2.v.val_vec.length
1637 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1638 break;
1639 case dw_val_class_const:
1640 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1641 break;
1642 case dw_val_class_const_double:
1643 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1644 break;
1645 default:
1646 gcc_unreachable ();
1648 break;
1650 case DW_OP_GNU_regval_type:
1652 unsigned long o
1653 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1654 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1655 + size_of_uleb128 (o);
1657 break;
1658 case DW_OP_GNU_deref_type:
1660 unsigned long o
1661 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1662 size += 1 + size_of_uleb128 (o);
1664 break;
1665 case DW_OP_GNU_convert:
1666 case DW_OP_GNU_reinterpret:
1667 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1668 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1669 else
1671 unsigned long o
1672 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1673 size += size_of_uleb128 (o);
1675 break;
1676 case DW_OP_GNU_parameter_ref:
1677 size += 4;
1678 break;
1679 default:
1680 break;
1683 return size;
1686 /* Return the size of a series of location descriptors. */
1688 unsigned long
1689 size_of_locs (dw_loc_descr_ref loc)
1691 dw_loc_descr_ref l;
1692 unsigned long size;
1694 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1695 field, to avoid writing to a PCH file. */
1696 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1698 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1699 break;
1700 size += size_of_loc_descr (l);
1702 if (! l)
1703 return size;
1705 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1707 l->dw_loc_addr = size;
1708 size += size_of_loc_descr (l);
1711 return size;
1714 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1715 static void get_ref_die_offset_label (char *, dw_die_ref);
1716 static unsigned long int get_ref_die_offset (dw_die_ref);
1718 /* Output location description stack opcode's operands (if any).
1719 The for_eh_or_skip parameter controls whether register numbers are
1720 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1721 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1722 info). This should be suppressed for the cases that have not been converted
1723 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1725 static void
1726 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1728 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1729 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1731 switch (loc->dw_loc_opc)
1733 #ifdef DWARF2_DEBUGGING_INFO
1734 case DW_OP_const2u:
1735 case DW_OP_const2s:
1736 dw2_asm_output_data (2, val1->v.val_int, NULL);
1737 break;
1738 case DW_OP_const4u:
1739 if (loc->dtprel)
1741 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1742 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1743 val1->v.val_addr);
1744 fputc ('\n', asm_out_file);
1745 break;
1747 /* FALLTHRU */
1748 case DW_OP_const4s:
1749 dw2_asm_output_data (4, val1->v.val_int, NULL);
1750 break;
1751 case DW_OP_const8u:
1752 if (loc->dtprel)
1754 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1755 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1756 val1->v.val_addr);
1757 fputc ('\n', asm_out_file);
1758 break;
1760 /* FALLTHRU */
1761 case DW_OP_const8s:
1762 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1763 dw2_asm_output_data (8, val1->v.val_int, NULL);
1764 break;
1765 case DW_OP_skip:
1766 case DW_OP_bra:
1768 int offset;
1770 gcc_assert (val1->val_class == dw_val_class_loc);
1771 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1773 dw2_asm_output_data (2, offset, NULL);
1775 break;
1776 case DW_OP_implicit_value:
1777 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1778 switch (val2->val_class)
1780 case dw_val_class_const:
1781 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1782 break;
1783 case dw_val_class_vec:
1785 unsigned int elt_size = val2->v.val_vec.elt_size;
1786 unsigned int len = val2->v.val_vec.length;
1787 unsigned int i;
1788 unsigned char *p;
1790 if (elt_size > sizeof (HOST_WIDE_INT))
1792 elt_size /= 2;
1793 len *= 2;
1795 for (i = 0, p = val2->v.val_vec.array;
1796 i < len;
1797 i++, p += elt_size)
1798 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1799 "fp or vector constant word %u", i);
1801 break;
1802 case dw_val_class_const_double:
1804 unsigned HOST_WIDE_INT first, second;
1806 if (WORDS_BIG_ENDIAN)
1808 first = val2->v.val_double.high;
1809 second = val2->v.val_double.low;
1811 else
1813 first = val2->v.val_double.low;
1814 second = val2->v.val_double.high;
1816 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1817 first, NULL);
1818 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1819 second, NULL);
1821 break;
1822 case dw_val_class_addr:
1823 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1824 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1825 break;
1826 default:
1827 gcc_unreachable ();
1829 break;
1830 #else
1831 case DW_OP_const2u:
1832 case DW_OP_const2s:
1833 case DW_OP_const4u:
1834 case DW_OP_const4s:
1835 case DW_OP_const8u:
1836 case DW_OP_const8s:
1837 case DW_OP_skip:
1838 case DW_OP_bra:
1839 case DW_OP_implicit_value:
1840 /* We currently don't make any attempt to make sure these are
1841 aligned properly like we do for the main unwind info, so
1842 don't support emitting things larger than a byte if we're
1843 only doing unwinding. */
1844 gcc_unreachable ();
1845 #endif
1846 case DW_OP_const1u:
1847 case DW_OP_const1s:
1848 dw2_asm_output_data (1, val1->v.val_int, NULL);
1849 break;
1850 case DW_OP_constu:
1851 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1852 break;
1853 case DW_OP_consts:
1854 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1855 break;
1856 case DW_OP_pick:
1857 dw2_asm_output_data (1, val1->v.val_int, NULL);
1858 break;
1859 case DW_OP_plus_uconst:
1860 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1861 break;
1862 case DW_OP_breg0:
1863 case DW_OP_breg1:
1864 case DW_OP_breg2:
1865 case DW_OP_breg3:
1866 case DW_OP_breg4:
1867 case DW_OP_breg5:
1868 case DW_OP_breg6:
1869 case DW_OP_breg7:
1870 case DW_OP_breg8:
1871 case DW_OP_breg9:
1872 case DW_OP_breg10:
1873 case DW_OP_breg11:
1874 case DW_OP_breg12:
1875 case DW_OP_breg13:
1876 case DW_OP_breg14:
1877 case DW_OP_breg15:
1878 case DW_OP_breg16:
1879 case DW_OP_breg17:
1880 case DW_OP_breg18:
1881 case DW_OP_breg19:
1882 case DW_OP_breg20:
1883 case DW_OP_breg21:
1884 case DW_OP_breg22:
1885 case DW_OP_breg23:
1886 case DW_OP_breg24:
1887 case DW_OP_breg25:
1888 case DW_OP_breg26:
1889 case DW_OP_breg27:
1890 case DW_OP_breg28:
1891 case DW_OP_breg29:
1892 case DW_OP_breg30:
1893 case DW_OP_breg31:
1894 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1895 break;
1896 case DW_OP_regx:
1898 unsigned r = val1->v.val_unsigned;
1899 if (for_eh_or_skip >= 0)
1900 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1901 gcc_assert (size_of_uleb128 (r)
1902 == size_of_uleb128 (val1->v.val_unsigned));
1903 dw2_asm_output_data_uleb128 (r, NULL);
1905 break;
1906 case DW_OP_fbreg:
1907 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1908 break;
1909 case DW_OP_bregx:
1911 unsigned r = val1->v.val_unsigned;
1912 if (for_eh_or_skip >= 0)
1913 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1914 gcc_assert (size_of_uleb128 (r)
1915 == size_of_uleb128 (val1->v.val_unsigned));
1916 dw2_asm_output_data_uleb128 (r, NULL);
1917 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1919 break;
1920 case DW_OP_piece:
1921 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1922 break;
1923 case DW_OP_bit_piece:
1924 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1925 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1926 break;
1927 case DW_OP_deref_size:
1928 case DW_OP_xderef_size:
1929 dw2_asm_output_data (1, val1->v.val_int, NULL);
1930 break;
1932 case DW_OP_addr:
1933 if (loc->dtprel)
1935 if (targetm.asm_out.output_dwarf_dtprel)
1937 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1938 DWARF2_ADDR_SIZE,
1939 val1->v.val_addr);
1940 fputc ('\n', asm_out_file);
1942 else
1943 gcc_unreachable ();
1945 else
1947 #ifdef DWARF2_DEBUGGING_INFO
1948 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1949 #else
1950 gcc_unreachable ();
1951 #endif
1953 break;
1955 case DW_OP_GNU_addr_index:
1956 case DW_OP_GNU_const_index:
1957 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1958 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1959 "(index into .debug_addr)");
1960 break;
1962 case DW_OP_GNU_implicit_pointer:
1964 char label[MAX_ARTIFICIAL_LABEL_BYTES
1965 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1966 gcc_assert (val1->val_class == dw_val_class_die_ref);
1967 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1968 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1969 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1971 break;
1973 case DW_OP_GNU_entry_value:
1974 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1975 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1976 break;
1978 case DW_OP_GNU_const_type:
1980 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1981 gcc_assert (o);
1982 dw2_asm_output_data_uleb128 (o, NULL);
1983 switch (val2->val_class)
1985 case dw_val_class_const:
1986 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1987 dw2_asm_output_data (1, l, NULL);
1988 dw2_asm_output_data (l, val2->v.val_int, NULL);
1989 break;
1990 case dw_val_class_vec:
1992 unsigned int elt_size = val2->v.val_vec.elt_size;
1993 unsigned int len = val2->v.val_vec.length;
1994 unsigned int i;
1995 unsigned char *p;
1997 l = len * elt_size;
1998 dw2_asm_output_data (1, l, NULL);
1999 if (elt_size > sizeof (HOST_WIDE_INT))
2001 elt_size /= 2;
2002 len *= 2;
2004 for (i = 0, p = val2->v.val_vec.array;
2005 i < len;
2006 i++, p += elt_size)
2007 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2008 "fp or vector constant word %u", i);
2010 break;
2011 case dw_val_class_const_double:
2013 unsigned HOST_WIDE_INT first, second;
2014 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2016 dw2_asm_output_data (1, 2 * l, NULL);
2017 if (WORDS_BIG_ENDIAN)
2019 first = val2->v.val_double.high;
2020 second = val2->v.val_double.low;
2022 else
2024 first = val2->v.val_double.low;
2025 second = val2->v.val_double.high;
2027 dw2_asm_output_data (l, first, NULL);
2028 dw2_asm_output_data (l, second, NULL);
2030 break;
2031 default:
2032 gcc_unreachable ();
2035 break;
2036 case DW_OP_GNU_regval_type:
2038 unsigned r = val1->v.val_unsigned;
2039 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2040 gcc_assert (o);
2041 if (for_eh_or_skip >= 0)
2043 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2044 gcc_assert (size_of_uleb128 (r)
2045 == size_of_uleb128 (val1->v.val_unsigned));
2047 dw2_asm_output_data_uleb128 (r, NULL);
2048 dw2_asm_output_data_uleb128 (o, NULL);
2050 break;
2051 case DW_OP_GNU_deref_type:
2053 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2054 gcc_assert (o);
2055 dw2_asm_output_data (1, val1->v.val_int, NULL);
2056 dw2_asm_output_data_uleb128 (o, NULL);
2058 break;
2059 case DW_OP_GNU_convert:
2060 case DW_OP_GNU_reinterpret:
2061 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2062 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2063 else
2065 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2066 gcc_assert (o);
2067 dw2_asm_output_data_uleb128 (o, NULL);
2069 break;
2071 case DW_OP_GNU_parameter_ref:
2073 unsigned long o;
2074 gcc_assert (val1->val_class == dw_val_class_die_ref);
2075 o = get_ref_die_offset (val1->v.val_die_ref.die);
2076 dw2_asm_output_data (4, o, NULL);
2078 break;
2080 default:
2081 /* Other codes have no operands. */
2082 break;
2086 /* Output a sequence of location operations.
2087 The for_eh_or_skip parameter controls whether register numbers are
2088 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2089 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2090 info). This should be suppressed for the cases that have not been converted
2091 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2093 void
2094 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2096 for (; loc != NULL; loc = loc->dw_loc_next)
2098 enum dwarf_location_atom opc = loc->dw_loc_opc;
2099 /* Output the opcode. */
2100 if (for_eh_or_skip >= 0
2101 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2103 unsigned r = (opc - DW_OP_breg0);
2104 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2105 gcc_assert (r <= 31);
2106 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2108 else if (for_eh_or_skip >= 0
2109 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2111 unsigned r = (opc - DW_OP_reg0);
2112 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2113 gcc_assert (r <= 31);
2114 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2117 dw2_asm_output_data (1, opc,
2118 "%s", dwarf_stack_op_name (opc));
2120 /* Output the operand(s) (if any). */
2121 output_loc_operands (loc, for_eh_or_skip);
2125 /* Output location description stack opcode's operands (if any).
2126 The output is single bytes on a line, suitable for .cfi_escape. */
2128 static void
2129 output_loc_operands_raw (dw_loc_descr_ref loc)
2131 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2132 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2134 switch (loc->dw_loc_opc)
2136 case DW_OP_addr:
2137 case DW_OP_GNU_addr_index:
2138 case DW_OP_GNU_const_index:
2139 case DW_OP_implicit_value:
2140 /* We cannot output addresses in .cfi_escape, only bytes. */
2141 gcc_unreachable ();
2143 case DW_OP_const1u:
2144 case DW_OP_const1s:
2145 case DW_OP_pick:
2146 case DW_OP_deref_size:
2147 case DW_OP_xderef_size:
2148 fputc (',', asm_out_file);
2149 dw2_asm_output_data_raw (1, val1->v.val_int);
2150 break;
2152 case DW_OP_const2u:
2153 case DW_OP_const2s:
2154 fputc (',', asm_out_file);
2155 dw2_asm_output_data_raw (2, val1->v.val_int);
2156 break;
2158 case DW_OP_const4u:
2159 case DW_OP_const4s:
2160 fputc (',', asm_out_file);
2161 dw2_asm_output_data_raw (4, val1->v.val_int);
2162 break;
2164 case DW_OP_const8u:
2165 case DW_OP_const8s:
2166 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2167 fputc (',', asm_out_file);
2168 dw2_asm_output_data_raw (8, val1->v.val_int);
2169 break;
2171 case DW_OP_skip:
2172 case DW_OP_bra:
2174 int offset;
2176 gcc_assert (val1->val_class == dw_val_class_loc);
2177 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2179 fputc (',', asm_out_file);
2180 dw2_asm_output_data_raw (2, offset);
2182 break;
2184 case DW_OP_regx:
2186 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2187 gcc_assert (size_of_uleb128 (r)
2188 == size_of_uleb128 (val1->v.val_unsigned));
2189 fputc (',', asm_out_file);
2190 dw2_asm_output_data_uleb128_raw (r);
2192 break;
2194 case DW_OP_constu:
2195 case DW_OP_plus_uconst:
2196 case DW_OP_piece:
2197 fputc (',', asm_out_file);
2198 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2199 break;
2201 case DW_OP_bit_piece:
2202 fputc (',', asm_out_file);
2203 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2204 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2205 break;
2207 case DW_OP_consts:
2208 case DW_OP_breg0:
2209 case DW_OP_breg1:
2210 case DW_OP_breg2:
2211 case DW_OP_breg3:
2212 case DW_OP_breg4:
2213 case DW_OP_breg5:
2214 case DW_OP_breg6:
2215 case DW_OP_breg7:
2216 case DW_OP_breg8:
2217 case DW_OP_breg9:
2218 case DW_OP_breg10:
2219 case DW_OP_breg11:
2220 case DW_OP_breg12:
2221 case DW_OP_breg13:
2222 case DW_OP_breg14:
2223 case DW_OP_breg15:
2224 case DW_OP_breg16:
2225 case DW_OP_breg17:
2226 case DW_OP_breg18:
2227 case DW_OP_breg19:
2228 case DW_OP_breg20:
2229 case DW_OP_breg21:
2230 case DW_OP_breg22:
2231 case DW_OP_breg23:
2232 case DW_OP_breg24:
2233 case DW_OP_breg25:
2234 case DW_OP_breg26:
2235 case DW_OP_breg27:
2236 case DW_OP_breg28:
2237 case DW_OP_breg29:
2238 case DW_OP_breg30:
2239 case DW_OP_breg31:
2240 case DW_OP_fbreg:
2241 fputc (',', asm_out_file);
2242 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2243 break;
2245 case DW_OP_bregx:
2247 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2248 gcc_assert (size_of_uleb128 (r)
2249 == size_of_uleb128 (val1->v.val_unsigned));
2250 fputc (',', asm_out_file);
2251 dw2_asm_output_data_uleb128_raw (r);
2252 fputc (',', asm_out_file);
2253 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2255 break;
2257 case DW_OP_GNU_implicit_pointer:
2258 case DW_OP_GNU_entry_value:
2259 case DW_OP_GNU_const_type:
2260 case DW_OP_GNU_regval_type:
2261 case DW_OP_GNU_deref_type:
2262 case DW_OP_GNU_convert:
2263 case DW_OP_GNU_reinterpret:
2264 case DW_OP_GNU_parameter_ref:
2265 gcc_unreachable ();
2266 break;
2268 default:
2269 /* Other codes have no operands. */
2270 break;
2274 void
2275 output_loc_sequence_raw (dw_loc_descr_ref loc)
2277 while (1)
2279 enum dwarf_location_atom opc = loc->dw_loc_opc;
2280 /* Output the opcode. */
2281 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2283 unsigned r = (opc - DW_OP_breg0);
2284 r = DWARF2_FRAME_REG_OUT (r, 1);
2285 gcc_assert (r <= 31);
2286 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2288 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2290 unsigned r = (opc - DW_OP_reg0);
2291 r = DWARF2_FRAME_REG_OUT (r, 1);
2292 gcc_assert (r <= 31);
2293 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2295 /* Output the opcode. */
2296 fprintf (asm_out_file, "%#x", opc);
2297 output_loc_operands_raw (loc);
2299 if (!loc->dw_loc_next)
2300 break;
2301 loc = loc->dw_loc_next;
2303 fputc (',', asm_out_file);
2307 /* This function builds a dwarf location descriptor sequence from a
2308 dw_cfa_location, adding the given OFFSET to the result of the
2309 expression. */
2311 struct dw_loc_descr_struct *
2312 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2314 struct dw_loc_descr_struct *head, *tmp;
2316 offset += cfa->offset;
2318 if (cfa->indirect)
2320 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2321 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2322 head->dw_loc_oprnd1.val_entry = NULL;
2323 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2324 add_loc_descr (&head, tmp);
2325 if (offset != 0)
2327 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2328 add_loc_descr (&head, tmp);
2331 else
2332 head = new_reg_loc_descr (cfa->reg, offset);
2334 return head;
2337 /* This function builds a dwarf location descriptor sequence for
2338 the address at OFFSET from the CFA when stack is aligned to
2339 ALIGNMENT byte. */
2341 struct dw_loc_descr_struct *
2342 build_cfa_aligned_loc (dw_cfa_location *cfa,
2343 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2345 struct dw_loc_descr_struct *head;
2346 unsigned int dwarf_fp
2347 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2349 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2350 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2352 head = new_reg_loc_descr (dwarf_fp, 0);
2353 add_loc_descr (&head, int_loc_descriptor (alignment));
2354 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2355 loc_descr_plus_const (&head, offset);
2357 else
2358 head = new_reg_loc_descr (dwarf_fp, offset);
2359 return head;
2362 /* And now, the support for symbolic debugging information. */
2364 /* .debug_str support. */
2365 static int output_indirect_string (void **, void *);
2367 static void dwarf2out_init (const char *);
2368 static void dwarf2out_finish (const char *);
2369 static void dwarf2out_assembly_start (void);
2370 static void dwarf2out_define (unsigned int, const char *);
2371 static void dwarf2out_undef (unsigned int, const char *);
2372 static void dwarf2out_start_source_file (unsigned, const char *);
2373 static void dwarf2out_end_source_file (unsigned);
2374 static void dwarf2out_function_decl (tree);
2375 static void dwarf2out_begin_block (unsigned, unsigned);
2376 static void dwarf2out_end_block (unsigned, unsigned);
2377 static bool dwarf2out_ignore_block (const_tree);
2378 static void dwarf2out_global_decl (tree);
2379 static void dwarf2out_type_decl (tree, int);
2380 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2381 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2382 dw_die_ref);
2383 static void dwarf2out_abstract_function (tree);
2384 static void dwarf2out_var_location (rtx);
2385 static void dwarf2out_begin_function (tree);
2386 static void dwarf2out_end_function (unsigned int);
2387 static void dwarf2out_set_name (tree, tree);
2389 /* The debug hooks structure. */
2391 const struct gcc_debug_hooks dwarf2_debug_hooks =
2393 dwarf2out_init,
2394 dwarf2out_finish,
2395 dwarf2out_assembly_start,
2396 dwarf2out_define,
2397 dwarf2out_undef,
2398 dwarf2out_start_source_file,
2399 dwarf2out_end_source_file,
2400 dwarf2out_begin_block,
2401 dwarf2out_end_block,
2402 dwarf2out_ignore_block,
2403 dwarf2out_source_line,
2404 dwarf2out_begin_prologue,
2405 #if VMS_DEBUGGING_INFO
2406 dwarf2out_vms_end_prologue,
2407 dwarf2out_vms_begin_epilogue,
2408 #else
2409 debug_nothing_int_charstar,
2410 debug_nothing_int_charstar,
2411 #endif
2412 dwarf2out_end_epilogue,
2413 dwarf2out_begin_function,
2414 dwarf2out_end_function, /* end_function */
2415 dwarf2out_function_decl, /* function_decl */
2416 dwarf2out_global_decl,
2417 dwarf2out_type_decl, /* type_decl */
2418 dwarf2out_imported_module_or_decl,
2419 debug_nothing_tree, /* deferred_inline_function */
2420 /* The DWARF 2 backend tries to reduce debugging bloat by not
2421 emitting the abstract description of inline functions until
2422 something tries to reference them. */
2423 dwarf2out_abstract_function, /* outlining_inline_function */
2424 debug_nothing_rtx, /* label */
2425 debug_nothing_int, /* handle_pch */
2426 dwarf2out_var_location,
2427 dwarf2out_switch_text_section,
2428 dwarf2out_set_name,
2429 1, /* start_end_main_source_file */
2430 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2433 /* NOTE: In the comments in this file, many references are made to
2434 "Debugging Information Entries". This term is abbreviated as `DIE'
2435 throughout the remainder of this file. */
2437 /* An internal representation of the DWARF output is built, and then
2438 walked to generate the DWARF debugging info. The walk of the internal
2439 representation is done after the entire program has been compiled.
2440 The types below are used to describe the internal representation. */
2442 /* Whether to put type DIEs into their own section .debug_types instead
2443 of making them part of the .debug_info section. Only supported for
2444 Dwarf V4 or higher and the user didn't disable them through
2445 -fno-debug-types-section. It is more efficient to put them in a
2446 separate comdat sections since the linker will then be able to
2447 remove duplicates. But not all tools support .debug_types sections
2448 yet. */
2450 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2452 /* Various DIE's use offsets relative to the beginning of the
2453 .debug_info section to refer to each other. */
2455 typedef long int dw_offset;
2457 /* Define typedefs here to avoid circular dependencies. */
2459 typedef struct dw_attr_struct *dw_attr_ref;
2460 typedef struct dw_line_info_struct *dw_line_info_ref;
2461 typedef struct pubname_struct *pubname_ref;
2462 typedef struct dw_ranges_struct *dw_ranges_ref;
2463 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2464 typedef struct comdat_type_struct *comdat_type_node_ref;
2466 /* The entries in the line_info table more-or-less mirror the opcodes
2467 that are used in the real dwarf line table. Arrays of these entries
2468 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2469 supported. */
2471 enum dw_line_info_opcode {
2472 /* Emit DW_LNE_set_address; the operand is the label index. */
2473 LI_set_address,
2475 /* Emit a row to the matrix with the given line. This may be done
2476 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2477 special opcodes. */
2478 LI_set_line,
2480 /* Emit a DW_LNS_set_file. */
2481 LI_set_file,
2483 /* Emit a DW_LNS_set_column. */
2484 LI_set_column,
2486 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2487 LI_negate_stmt,
2489 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2490 LI_set_prologue_end,
2491 LI_set_epilogue_begin,
2493 /* Emit a DW_LNE_set_discriminator. */
2494 LI_set_discriminator
2497 typedef struct GTY(()) dw_line_info_struct {
2498 enum dw_line_info_opcode opcode;
2499 unsigned int val;
2500 } dw_line_info_entry;
2503 typedef struct GTY(()) dw_line_info_table_struct {
2504 /* The label that marks the end of this section. */
2505 const char *end_label;
2507 /* The values for the last row of the matrix, as collected in the table.
2508 These are used to minimize the changes to the next row. */
2509 unsigned int file_num;
2510 unsigned int line_num;
2511 unsigned int column_num;
2512 int discrim_num;
2513 bool is_stmt;
2514 bool in_use;
2516 vec<dw_line_info_entry, va_gc> *entries;
2517 } dw_line_info_table;
2519 typedef dw_line_info_table *dw_line_info_table_p;
2522 /* Each DIE attribute has a field specifying the attribute kind,
2523 a link to the next attribute in the chain, and an attribute value.
2524 Attributes are typically linked below the DIE they modify. */
2526 typedef struct GTY(()) dw_attr_struct {
2527 enum dwarf_attribute dw_attr;
2528 dw_val_node dw_attr_val;
2530 dw_attr_node;
2533 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2534 The children of each node form a circular list linked by
2535 die_sib. die_child points to the node *before* the "first" child node. */
2537 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2538 union die_symbol_or_type_node
2540 const char * GTY ((tag ("0"))) die_symbol;
2541 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2543 GTY ((desc ("%0.comdat_type_p"))) die_id;
2544 vec<dw_attr_node, va_gc> *die_attr;
2545 dw_die_ref die_parent;
2546 dw_die_ref die_child;
2547 dw_die_ref die_sib;
2548 dw_die_ref die_definition; /* ref from a specification to its definition */
2549 dw_offset die_offset;
2550 unsigned long die_abbrev;
2551 int die_mark;
2552 unsigned int decl_id;
2553 enum dwarf_tag die_tag;
2554 /* Die is used and must not be pruned as unused. */
2555 BOOL_BITFIELD die_perennial_p : 1;
2556 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2557 /* Lots of spare bits. */
2559 die_node;
2561 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2562 #define FOR_EACH_CHILD(die, c, expr) do { \
2563 c = die->die_child; \
2564 if (c) do { \
2565 c = c->die_sib; \
2566 expr; \
2567 } while (c != die->die_child); \
2568 } while (0)
2570 /* The pubname structure */
2572 typedef struct GTY(()) pubname_struct {
2573 dw_die_ref die;
2574 const char *name;
2576 pubname_entry;
2579 struct GTY(()) dw_ranges_struct {
2580 /* If this is positive, it's a block number, otherwise it's a
2581 bitwise-negated index into dw_ranges_by_label. */
2582 int num;
2585 /* A structure to hold a macinfo entry. */
2587 typedef struct GTY(()) macinfo_struct {
2588 unsigned char code;
2589 unsigned HOST_WIDE_INT lineno;
2590 const char *info;
2592 macinfo_entry;
2595 struct GTY(()) dw_ranges_by_label_struct {
2596 const char *begin;
2597 const char *end;
2600 /* The comdat type node structure. */
2601 typedef struct GTY(()) comdat_type_struct
2603 dw_die_ref root_die;
2604 dw_die_ref type_die;
2605 dw_die_ref skeleton_die;
2606 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2607 struct comdat_type_struct *next;
2609 comdat_type_node;
2611 /* The limbo die list structure. */
2612 typedef struct GTY(()) limbo_die_struct {
2613 dw_die_ref die;
2614 tree created_for;
2615 struct limbo_die_struct *next;
2617 limbo_die_node;
2619 typedef struct skeleton_chain_struct
2621 dw_die_ref old_die;
2622 dw_die_ref new_die;
2623 struct skeleton_chain_struct *parent;
2625 skeleton_chain_node;
2627 /* Define a macro which returns nonzero for a TYPE_DECL which was
2628 implicitly generated for a type.
2630 Note that, unlike the C front-end (which generates a NULL named
2631 TYPE_DECL node for each complete tagged type, each array type,
2632 and each function type node created) the C++ front-end generates
2633 a _named_ TYPE_DECL node for each tagged type node created.
2634 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2635 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2636 front-end, but for each type, tagged or not. */
2638 #define TYPE_DECL_IS_STUB(decl) \
2639 (DECL_NAME (decl) == NULL_TREE \
2640 || (DECL_ARTIFICIAL (decl) \
2641 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2642 /* This is necessary for stub decls that \
2643 appear in nested inline functions. */ \
2644 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2645 && (decl_ultimate_origin (decl) \
2646 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2648 /* Information concerning the compilation unit's programming
2649 language, and compiler version. */
2651 /* Fixed size portion of the DWARF compilation unit header. */
2652 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2653 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2655 /* Fixed size portion of the DWARF comdat type unit header. */
2656 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2657 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2658 + DWARF_OFFSET_SIZE)
2660 /* Fixed size portion of public names info. */
2661 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2663 /* Fixed size portion of the address range info. */
2664 #define DWARF_ARANGES_HEADER_SIZE \
2665 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2666 DWARF2_ADDR_SIZE * 2) \
2667 - DWARF_INITIAL_LENGTH_SIZE)
2669 /* Size of padding portion in the address range info. It must be
2670 aligned to twice the pointer size. */
2671 #define DWARF_ARANGES_PAD_SIZE \
2672 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2673 DWARF2_ADDR_SIZE * 2) \
2674 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2676 /* Use assembler line directives if available. */
2677 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2678 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2679 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2680 #else
2681 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2682 #endif
2683 #endif
2685 /* Minimum line offset in a special line info. opcode.
2686 This value was chosen to give a reasonable range of values. */
2687 #define DWARF_LINE_BASE -10
2689 /* First special line opcode - leave room for the standard opcodes. */
2690 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2692 /* Range of line offsets in a special line info. opcode. */
2693 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2695 /* Flag that indicates the initial value of the is_stmt_start flag.
2696 In the present implementation, we do not mark any lines as
2697 the beginning of a source statement, because that information
2698 is not made available by the GCC front-end. */
2699 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2701 /* Maximum number of operations per instruction bundle. */
2702 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2703 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2704 #endif
2706 /* This location is used by calc_die_sizes() to keep track
2707 the offset of each DIE within the .debug_info section. */
2708 static unsigned long next_die_offset;
2710 /* Record the root of the DIE's built for the current compilation unit. */
2711 static GTY(()) dw_die_ref single_comp_unit_die;
2713 /* A list of type DIEs that have been separated into comdat sections. */
2714 static GTY(()) comdat_type_node *comdat_type_list;
2716 /* A list of DIEs with a NULL parent waiting to be relocated. */
2717 static GTY(()) limbo_die_node *limbo_die_list;
2719 /* A list of DIEs for which we may have to generate
2720 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2721 static GTY(()) limbo_die_node *deferred_asm_name;
2723 /* Filenames referenced by this compilation unit. */
2724 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2726 /* A hash table of references to DIE's that describe declarations.
2727 The key is a DECL_UID() which is a unique number identifying each decl. */
2728 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2730 /* A hash table of references to DIE's that describe COMMON blocks.
2731 The key is DECL_UID() ^ die_parent. */
2732 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2734 typedef struct GTY(()) die_arg_entry_struct {
2735 dw_die_ref die;
2736 tree arg;
2737 } die_arg_entry;
2740 /* Node of the variable location list. */
2741 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2742 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2743 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2744 in mode of the EXPR_LIST node and first EXPR_LIST operand
2745 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2746 location or NULL for padding. For larger bitsizes,
2747 mode is 0 and first operand is a CONCAT with bitsize
2748 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2749 NULL as second operand. */
2750 rtx GTY (()) loc;
2751 const char * GTY (()) label;
2752 struct var_loc_node * GTY (()) next;
2755 /* Variable location list. */
2756 struct GTY (()) var_loc_list_def {
2757 struct var_loc_node * GTY (()) first;
2759 /* Pointer to the last but one or last element of the
2760 chained list. If the list is empty, both first and
2761 last are NULL, if the list contains just one node
2762 or the last node certainly is not redundant, it points
2763 to the last node, otherwise points to the last but one.
2764 Do not mark it for GC because it is marked through the chain. */
2765 struct var_loc_node * GTY ((skip ("%h"))) last;
2767 /* Pointer to the last element before section switch,
2768 if NULL, either sections weren't switched or first
2769 is after section switch. */
2770 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2772 /* DECL_UID of the variable decl. */
2773 unsigned int decl_id;
2775 typedef struct var_loc_list_def var_loc_list;
2777 /* Call argument location list. */
2778 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2779 rtx GTY (()) call_arg_loc_note;
2780 const char * GTY (()) label;
2781 tree GTY (()) block;
2782 bool tail_call_p;
2783 rtx GTY (()) symbol_ref;
2784 struct call_arg_loc_node * GTY (()) next;
2788 /* Table of decl location linked lists. */
2789 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2791 /* Head and tail of call_arg_loc chain. */
2792 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2793 static struct call_arg_loc_node *call_arg_loc_last;
2795 /* Number of call sites in the current function. */
2796 static int call_site_count = -1;
2797 /* Number of tail call sites in the current function. */
2798 static int tail_call_site_count = -1;
2800 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2801 DIEs. */
2802 static vec<dw_die_ref> block_map;
2804 /* A cached location list. */
2805 struct GTY (()) cached_dw_loc_list_def {
2806 /* The DECL_UID of the decl that this entry describes. */
2807 unsigned int decl_id;
2809 /* The cached location list. */
2810 dw_loc_list_ref loc_list;
2812 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2814 /* Table of cached location lists. */
2815 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2817 /* A pointer to the base of a list of references to DIE's that
2818 are uniquely identified by their tag, presence/absence of
2819 children DIE's, and list of attribute/value pairs. */
2820 static GTY((length ("abbrev_die_table_allocated")))
2821 dw_die_ref *abbrev_die_table;
2823 /* Number of elements currently allocated for abbrev_die_table. */
2824 static GTY(()) unsigned abbrev_die_table_allocated;
2826 /* Number of elements in type_die_table currently in use. */
2827 static GTY(()) unsigned abbrev_die_table_in_use;
2829 /* Size (in elements) of increments by which we may expand the
2830 abbrev_die_table. */
2831 #define ABBREV_DIE_TABLE_INCREMENT 256
2833 /* A global counter for generating labels for line number data. */
2834 static unsigned int line_info_label_num;
2836 /* The current table to which we should emit line number information
2837 for the current function. This will be set up at the beginning of
2838 assembly for the function. */
2839 static dw_line_info_table *cur_line_info_table;
2841 /* The two default tables of line number info. */
2842 static GTY(()) dw_line_info_table *text_section_line_info;
2843 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2845 /* The set of all non-default tables of line number info. */
2846 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2848 /* A flag to tell pubnames/types export if there is an info section to
2849 refer to. */
2850 static bool info_section_emitted;
2852 /* A pointer to the base of a table that contains a list of publicly
2853 accessible names. */
2854 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2856 /* A pointer to the base of a table that contains a list of publicly
2857 accessible types. */
2858 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2860 /* A pointer to the base of a table that contains a list of macro
2861 defines/undefines (and file start/end markers). */
2862 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2864 /* True if .debug_macinfo or .debug_macros section is going to be
2865 emitted. */
2866 #define have_macinfo \
2867 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2868 && !macinfo_table->is_empty ())
2870 /* Array of dies for which we should generate .debug_ranges info. */
2871 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2873 /* Number of elements currently allocated for ranges_table. */
2874 static GTY(()) unsigned ranges_table_allocated;
2876 /* Number of elements in ranges_table currently in use. */
2877 static GTY(()) unsigned ranges_table_in_use;
2879 /* Array of pairs of labels referenced in ranges_table. */
2880 static GTY ((length ("ranges_by_label_allocated")))
2881 dw_ranges_by_label_ref ranges_by_label;
2883 /* Number of elements currently allocated for ranges_by_label. */
2884 static GTY(()) unsigned ranges_by_label_allocated;
2886 /* Number of elements in ranges_by_label currently in use. */
2887 static GTY(()) unsigned ranges_by_label_in_use;
2889 /* Size (in elements) of increments by which we may expand the
2890 ranges_table. */
2891 #define RANGES_TABLE_INCREMENT 64
2893 /* Whether we have location lists that need outputting */
2894 static GTY(()) bool have_location_lists;
2896 /* Unique label counter. */
2897 static GTY(()) unsigned int loclabel_num;
2899 /* Unique label counter for point-of-call tables. */
2900 static GTY(()) unsigned int poc_label_num;
2902 /* Record whether the function being analyzed contains inlined functions. */
2903 static int current_function_has_inlines;
2905 /* The last file entry emitted by maybe_emit_file(). */
2906 static GTY(()) struct dwarf_file_data * last_emitted_file;
2908 /* Number of internal labels generated by gen_internal_sym(). */
2909 static GTY(()) int label_num;
2911 /* Cached result of previous call to lookup_filename. */
2912 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2914 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2916 /* Instances of generic types for which we need to generate debug
2917 info that describe their generic parameters and arguments. That
2918 generation needs to happen once all types are properly laid out so
2919 we do it at the end of compilation. */
2920 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2922 /* Offset from the "steady-state frame pointer" to the frame base,
2923 within the current function. */
2924 static HOST_WIDE_INT frame_pointer_fb_offset;
2925 static bool frame_pointer_fb_offset_valid;
2927 static vec<dw_die_ref> base_types;
2929 /* Forward declarations for functions defined in this file. */
2931 static int is_pseudo_reg (const_rtx);
2932 static tree type_main_variant (tree);
2933 static int is_tagged_type (const_tree);
2934 static const char *dwarf_tag_name (unsigned);
2935 static const char *dwarf_attr_name (unsigned);
2936 static const char *dwarf_form_name (unsigned);
2937 static tree decl_ultimate_origin (const_tree);
2938 static tree decl_class_context (tree);
2939 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2940 static inline enum dw_val_class AT_class (dw_attr_ref);
2941 static inline unsigned int AT_index (dw_attr_ref);
2942 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2943 static inline unsigned AT_flag (dw_attr_ref);
2944 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2945 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2946 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2947 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2948 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2949 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2950 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2951 unsigned int, unsigned char *);
2952 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2953 static hashval_t debug_str_do_hash (const void *);
2954 static int debug_str_eq (const void *, const void *);
2955 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2956 static inline const char *AT_string (dw_attr_ref);
2957 static enum dwarf_form AT_string_form (dw_attr_ref);
2958 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2959 static void add_AT_specification (dw_die_ref, dw_die_ref);
2960 static inline dw_die_ref AT_ref (dw_attr_ref);
2961 static inline int AT_ref_external (dw_attr_ref);
2962 static inline void set_AT_ref_external (dw_attr_ref, int);
2963 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2964 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2965 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2966 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2967 dw_loc_list_ref);
2968 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2969 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2970 static void remove_addr_table_entry (addr_table_entry *);
2971 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2972 static inline rtx AT_addr (dw_attr_ref);
2973 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2974 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2975 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2976 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2977 unsigned HOST_WIDE_INT);
2978 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2979 unsigned long, bool);
2980 static inline const char *AT_lbl (dw_attr_ref);
2981 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2982 static const char *get_AT_low_pc (dw_die_ref);
2983 static const char *get_AT_hi_pc (dw_die_ref);
2984 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2985 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2986 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2987 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2988 static bool is_cxx (void);
2989 static bool is_fortran (void);
2990 static bool is_ada (void);
2991 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2992 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2993 static void add_child_die (dw_die_ref, dw_die_ref);
2994 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2995 static dw_die_ref lookup_type_die (tree);
2996 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2997 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2998 static void equate_type_number_to_die (tree, dw_die_ref);
2999 static hashval_t decl_die_table_hash (const void *);
3000 static int decl_die_table_eq (const void *, const void *);
3001 static dw_die_ref lookup_decl_die (tree);
3002 static hashval_t common_block_die_table_hash (const void *);
3003 static int common_block_die_table_eq (const void *, const void *);
3004 static hashval_t decl_loc_table_hash (const void *);
3005 static int decl_loc_table_eq (const void *, const void *);
3006 static var_loc_list *lookup_decl_loc (const_tree);
3007 static void equate_decl_number_to_die (tree, dw_die_ref);
3008 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3009 static void print_spaces (FILE *);
3010 static void print_die (dw_die_ref, FILE *);
3011 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3012 static dw_die_ref pop_compile_unit (dw_die_ref);
3013 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3014 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3015 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3016 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3017 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3018 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3019 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3020 struct md5_ctx *, int *);
3021 struct checksum_attributes;
3022 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3023 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3024 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3025 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3026 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3027 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3028 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3029 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3030 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3031 static void compute_section_prefix (dw_die_ref);
3032 static int is_type_die (dw_die_ref);
3033 static int is_comdat_die (dw_die_ref);
3034 static int is_symbol_die (dw_die_ref);
3035 static inline bool is_template_instantiation (dw_die_ref);
3036 static void assign_symbol_names (dw_die_ref);
3037 static void break_out_includes (dw_die_ref);
3038 static int is_declaration_die (dw_die_ref);
3039 static int should_move_die_to_comdat (dw_die_ref);
3040 static dw_die_ref clone_as_declaration (dw_die_ref);
3041 static dw_die_ref clone_die (dw_die_ref);
3042 static dw_die_ref clone_tree (dw_die_ref);
3043 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3044 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3045 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3046 static dw_die_ref generate_skeleton (dw_die_ref);
3047 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3048 dw_die_ref,
3049 dw_die_ref);
3050 static void break_out_comdat_types (dw_die_ref);
3051 static void copy_decls_for_unworthy_types (dw_die_ref);
3053 static void add_sibling_attributes (dw_die_ref);
3054 static void output_location_lists (dw_die_ref);
3055 static int constant_size (unsigned HOST_WIDE_INT);
3056 static unsigned long size_of_die (dw_die_ref);
3057 static void calc_die_sizes (dw_die_ref);
3058 static void calc_base_type_die_sizes (void);
3059 static void mark_dies (dw_die_ref);
3060 static void unmark_dies (dw_die_ref);
3061 static void unmark_all_dies (dw_die_ref);
3062 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3063 static unsigned long size_of_aranges (void);
3064 static enum dwarf_form value_format (dw_attr_ref);
3065 static void output_value_format (dw_attr_ref);
3066 static void output_abbrev_section (void);
3067 static void output_die_abbrevs (unsigned long, dw_die_ref);
3068 static void output_die_symbol (dw_die_ref);
3069 static void output_die (dw_die_ref);
3070 static void output_compilation_unit_header (void);
3071 static void output_comp_unit (dw_die_ref, int);
3072 static void output_comdat_type_unit (comdat_type_node *);
3073 static const char *dwarf2_name (tree, int);
3074 static void add_pubname (tree, dw_die_ref);
3075 static void add_enumerator_pubname (const char *, dw_die_ref);
3076 static void add_pubname_string (const char *, dw_die_ref);
3077 static void add_pubtype (tree, dw_die_ref);
3078 static void output_pubnames (vec<pubname_entry, va_gc> *);
3079 static void output_aranges (unsigned long);
3080 static unsigned int add_ranges_num (int);
3081 static unsigned int add_ranges (const_tree);
3082 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3083 bool *, bool);
3084 static void output_ranges (void);
3085 static dw_line_info_table *new_line_info_table (void);
3086 static void output_line_info (bool);
3087 static void output_file_names (void);
3088 static dw_die_ref base_type_die (tree);
3089 static int is_base_type (tree);
3090 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3091 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3092 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3093 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3094 static int type_is_enum (const_tree);
3095 static unsigned int dbx_reg_number (const_rtx);
3096 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3097 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3098 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3099 enum var_init_status);
3100 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3101 enum var_init_status);
3102 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3103 enum var_init_status);
3104 static int is_based_loc (const_rtx);
3105 static int resolve_one_addr (rtx *, void *);
3106 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3107 enum var_init_status);
3108 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3109 enum var_init_status);
3110 static dw_loc_list_ref loc_list_from_tree (tree, int);
3111 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3112 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3113 static tree field_type (const_tree);
3114 static unsigned int simple_type_align_in_bits (const_tree);
3115 static unsigned int simple_decl_align_in_bits (const_tree);
3116 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3117 static HOST_WIDE_INT field_byte_offset (const_tree);
3118 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3119 dw_loc_list_ref);
3120 static void add_data_member_location_attribute (dw_die_ref, tree);
3121 static bool add_const_value_attribute (dw_die_ref, rtx);
3122 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3123 static void insert_double (double_int, unsigned char *);
3124 static void insert_float (const_rtx, unsigned char *);
3125 static rtx rtl_for_decl_location (tree);
3126 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3127 enum dwarf_attribute);
3128 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3129 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3130 static void add_name_attribute (dw_die_ref, const char *);
3131 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3132 static void add_comp_dir_attribute (dw_die_ref);
3133 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3134 static void add_subscript_info (dw_die_ref, tree, bool);
3135 static void add_byte_size_attribute (dw_die_ref, tree);
3136 static void add_bit_offset_attribute (dw_die_ref, tree);
3137 static void add_bit_size_attribute (dw_die_ref, tree);
3138 static void add_prototyped_attribute (dw_die_ref, tree);
3139 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3140 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3141 static void add_src_coords_attributes (dw_die_ref, tree);
3142 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3143 static void push_decl_scope (tree);
3144 static void pop_decl_scope (void);
3145 static dw_die_ref scope_die_for (tree, dw_die_ref);
3146 static inline int local_scope_p (dw_die_ref);
3147 static inline int class_scope_p (dw_die_ref);
3148 static inline int class_or_namespace_scope_p (dw_die_ref);
3149 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3150 static void add_calling_convention_attribute (dw_die_ref, tree);
3151 static const char *type_tag (const_tree);
3152 static tree member_declared_type (const_tree);
3153 #if 0
3154 static const char *decl_start_label (tree);
3155 #endif
3156 static void gen_array_type_die (tree, dw_die_ref);
3157 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3158 #if 0
3159 static void gen_entry_point_die (tree, dw_die_ref);
3160 #endif
3161 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3162 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3163 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3164 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3165 static void gen_formal_types_die (tree, dw_die_ref);
3166 static void gen_subprogram_die (tree, dw_die_ref);
3167 static void gen_variable_die (tree, tree, dw_die_ref);
3168 static void gen_const_die (tree, dw_die_ref);
3169 static void gen_label_die (tree, dw_die_ref);
3170 static void gen_lexical_block_die (tree, dw_die_ref, int);
3171 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3172 static void gen_field_die (tree, dw_die_ref);
3173 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3174 static dw_die_ref gen_compile_unit_die (const char *);
3175 static void gen_inheritance_die (tree, tree, dw_die_ref);
3176 static void gen_member_die (tree, dw_die_ref);
3177 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3178 enum debug_info_usage);
3179 static void gen_subroutine_type_die (tree, dw_die_ref);
3180 static void gen_typedef_die (tree, dw_die_ref);
3181 static void gen_type_die (tree, dw_die_ref);
3182 static void gen_block_die (tree, dw_die_ref, int);
3183 static void decls_for_scope (tree, dw_die_ref, int);
3184 static inline int is_redundant_typedef (const_tree);
3185 static bool is_naming_typedef_decl (const_tree);
3186 static inline dw_die_ref get_context_die (tree);
3187 static void gen_namespace_die (tree, dw_die_ref);
3188 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3189 static dw_die_ref force_decl_die (tree);
3190 static dw_die_ref force_type_die (tree);
3191 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3192 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3193 static struct dwarf_file_data * lookup_filename (const char *);
3194 static void retry_incomplete_types (void);
3195 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3196 static void gen_generic_params_dies (tree);
3197 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3198 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3199 static void splice_child_die (dw_die_ref, dw_die_ref);
3200 static int file_info_cmp (const void *, const void *);
3201 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3202 const char *, const char *);
3203 static void output_loc_list (dw_loc_list_ref);
3204 static char *gen_internal_sym (const char *);
3205 static bool want_pubnames (void);
3207 static void prune_unmark_dies (dw_die_ref);
3208 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3209 static void prune_unused_types_mark (dw_die_ref, int);
3210 static void prune_unused_types_walk (dw_die_ref);
3211 static void prune_unused_types_walk_attribs (dw_die_ref);
3212 static void prune_unused_types_prune (dw_die_ref);
3213 static void prune_unused_types (void);
3214 static int maybe_emit_file (struct dwarf_file_data *fd);
3215 static inline const char *AT_vms_delta1 (dw_attr_ref);
3216 static inline const char *AT_vms_delta2 (dw_attr_ref);
3217 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3218 const char *, const char *);
3219 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3220 static void gen_remaining_tmpl_value_param_die_attribute (void);
3221 static bool generic_type_p (tree);
3222 static void schedule_generic_params_dies_gen (tree t);
3223 static void gen_scheduled_generic_parms_dies (void);
3225 static const char *comp_dir_string (void);
3227 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3229 /* enum for tracking thread-local variables whose address is really an offset
3230 relative to the TLS pointer, which will need link-time relocation, but will
3231 not need relocation by the DWARF consumer. */
3233 enum dtprel_bool
3235 dtprel_false = 0,
3236 dtprel_true = 1
3239 /* Return the operator to use for an address of a variable. For dtprel_true, we
3240 use DW_OP_const*. For regular variables, which need both link-time
3241 relocation and consumer-level relocation (e.g., to account for shared objects
3242 loaded at a random address), we use DW_OP_addr*. */
3244 static inline enum dwarf_location_atom
3245 dw_addr_op (enum dtprel_bool dtprel)
3247 if (dtprel == dtprel_true)
3248 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3249 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3250 else
3251 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3254 /* Return a pointer to a newly allocated address location description. If
3255 dwarf_split_debug_info is true, then record the address with the appropriate
3256 relocation. */
3257 static inline dw_loc_descr_ref
3258 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3260 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3262 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3263 ref->dw_loc_oprnd1.v.val_addr = addr;
3264 ref->dtprel = dtprel;
3265 if (dwarf_split_debug_info)
3266 ref->dw_loc_oprnd1.val_entry
3267 = add_addr_table_entry (addr,
3268 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3269 else
3270 ref->dw_loc_oprnd1.val_entry = NULL;
3272 return ref;
3275 /* Section names used to hold DWARF debugging information. */
3277 #ifndef DEBUG_INFO_SECTION
3278 #define DEBUG_INFO_SECTION ".debug_info"
3279 #endif
3280 #ifndef DEBUG_DWO_INFO_SECTION
3281 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3282 #endif
3283 #ifndef DEBUG_ABBREV_SECTION
3284 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3285 #endif
3286 #ifndef DEBUG_DWO_ABBREV_SECTION
3287 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3288 #endif
3289 #ifndef DEBUG_ARANGES_SECTION
3290 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3291 #endif
3292 #ifndef DEBUG_ADDR_SECTION
3293 #define DEBUG_ADDR_SECTION ".debug_addr"
3294 #endif
3295 #ifndef DEBUG_NORM_MACINFO_SECTION
3296 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3297 #endif
3298 #ifndef DEBUG_DWO_MACINFO_SECTION
3299 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3300 #endif
3301 #ifndef DEBUG_MACINFO_SECTION
3302 #define DEBUG_MACINFO_SECTION \
3303 (!dwarf_split_debug_info \
3304 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3305 #endif
3306 #ifndef DEBUG_NORM_MACRO_SECTION
3307 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3308 #endif
3309 #ifndef DEBUG_DWO_MACRO_SECTION
3310 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3311 #endif
3312 #ifndef DEBUG_MACRO_SECTION
3313 #define DEBUG_MACRO_SECTION \
3314 (!dwarf_split_debug_info \
3315 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3316 #endif
3317 #ifndef DEBUG_LINE_SECTION
3318 #define DEBUG_LINE_SECTION ".debug_line"
3319 #endif
3320 #ifndef DEBUG_DWO_LINE_SECTION
3321 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3322 #endif
3323 #ifndef DEBUG_LOC_SECTION
3324 #define DEBUG_LOC_SECTION ".debug_loc"
3325 #endif
3326 #ifndef DEBUG_DWO_LOC_SECTION
3327 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3328 #endif
3329 #ifndef DEBUG_PUBNAMES_SECTION
3330 #define DEBUG_PUBNAMES_SECTION \
3331 ((debug_generate_pub_sections == 2) \
3332 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3333 #endif
3334 #ifndef DEBUG_PUBTYPES_SECTION
3335 #define DEBUG_PUBTYPES_SECTION \
3336 ((debug_generate_pub_sections == 2) \
3337 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3338 #endif
3339 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3340 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3341 #ifndef DEBUG_STR_OFFSETS_SECTION
3342 #define DEBUG_STR_OFFSETS_SECTION \
3343 (!dwarf_split_debug_info \
3344 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3345 #endif
3346 #ifndef DEBUG_STR_DWO_SECTION
3347 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3348 #endif
3349 #ifndef DEBUG_STR_SECTION
3350 #define DEBUG_STR_SECTION ".debug_str"
3351 #endif
3352 #ifndef DEBUG_RANGES_SECTION
3353 #define DEBUG_RANGES_SECTION ".debug_ranges"
3354 #endif
3356 /* Standard ELF section names for compiled code and data. */
3357 #ifndef TEXT_SECTION_NAME
3358 #define TEXT_SECTION_NAME ".text"
3359 #endif
3361 /* Section flags for .debug_macinfo/.debug_macro section. */
3362 #define DEBUG_MACRO_SECTION_FLAGS \
3363 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3365 /* Section flags for .debug_str section. */
3366 #define DEBUG_STR_SECTION_FLAGS \
3367 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3368 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3369 : SECTION_DEBUG)
3371 /* Section flags for .debug_str.dwo section. */
3372 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3374 /* Labels we insert at beginning sections we can reference instead of
3375 the section names themselves. */
3377 #ifndef TEXT_SECTION_LABEL
3378 #define TEXT_SECTION_LABEL "Ltext"
3379 #endif
3380 #ifndef COLD_TEXT_SECTION_LABEL
3381 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3382 #endif
3383 #ifndef DEBUG_LINE_SECTION_LABEL
3384 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3385 #endif
3386 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3387 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3388 #endif
3389 #ifndef DEBUG_INFO_SECTION_LABEL
3390 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3391 #endif
3392 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3393 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3394 #endif
3395 #ifndef DEBUG_ABBREV_SECTION_LABEL
3396 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3397 #endif
3398 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3399 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3400 #endif
3401 #ifndef DEBUG_ADDR_SECTION_LABEL
3402 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3403 #endif
3404 #ifndef DEBUG_LOC_SECTION_LABEL
3405 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3406 #endif
3407 #ifndef DEBUG_RANGES_SECTION_LABEL
3408 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3409 #endif
3410 #ifndef DEBUG_MACINFO_SECTION_LABEL
3411 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3412 #endif
3413 #ifndef DEBUG_MACRO_SECTION_LABEL
3414 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3415 #endif
3416 #define SKELETON_COMP_DIE_ABBREV 1
3417 #define SKELETON_TYPE_DIE_ABBREV 2
3419 /* Definitions of defaults for formats and names of various special
3420 (artificial) labels which may be generated within this file (when the -g
3421 options is used and DWARF2_DEBUGGING_INFO is in effect.
3422 If necessary, these may be overridden from within the tm.h file, but
3423 typically, overriding these defaults is unnecessary. */
3425 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3426 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3427 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3428 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3429 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3430 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3431 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3432 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3440 #ifndef TEXT_END_LABEL
3441 #define TEXT_END_LABEL "Letext"
3442 #endif
3443 #ifndef COLD_END_LABEL
3444 #define COLD_END_LABEL "Letext_cold"
3445 #endif
3446 #ifndef BLOCK_BEGIN_LABEL
3447 #define BLOCK_BEGIN_LABEL "LBB"
3448 #endif
3449 #ifndef BLOCK_END_LABEL
3450 #define BLOCK_END_LABEL "LBE"
3451 #endif
3452 #ifndef LINE_CODE_LABEL
3453 #define LINE_CODE_LABEL "LM"
3454 #endif
3457 /* Return the root of the DIE's built for the current compilation unit. */
3458 static dw_die_ref
3459 comp_unit_die (void)
3461 if (!single_comp_unit_die)
3462 single_comp_unit_die = gen_compile_unit_die (NULL);
3463 return single_comp_unit_die;
3466 /* We allow a language front-end to designate a function that is to be
3467 called to "demangle" any name before it is put into a DIE. */
3469 static const char *(*demangle_name_func) (const char *);
3471 void
3472 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3474 demangle_name_func = func;
3477 /* Test if rtl node points to a pseudo register. */
3479 static inline int
3480 is_pseudo_reg (const_rtx rtl)
3482 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3483 || (GET_CODE (rtl) == SUBREG
3484 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3487 /* Return a reference to a type, with its const and volatile qualifiers
3488 removed. */
3490 static inline tree
3491 type_main_variant (tree type)
3493 type = TYPE_MAIN_VARIANT (type);
3495 /* ??? There really should be only one main variant among any group of
3496 variants of a given type (and all of the MAIN_VARIANT values for all
3497 members of the group should point to that one type) but sometimes the C
3498 front-end messes this up for array types, so we work around that bug
3499 here. */
3500 if (TREE_CODE (type) == ARRAY_TYPE)
3501 while (type != TYPE_MAIN_VARIANT (type))
3502 type = TYPE_MAIN_VARIANT (type);
3504 return type;
3507 /* Return nonzero if the given type node represents a tagged type. */
3509 static inline int
3510 is_tagged_type (const_tree type)
3512 enum tree_code code = TREE_CODE (type);
3514 return (code == RECORD_TYPE || code == UNION_TYPE
3515 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3518 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3520 static void
3521 get_ref_die_offset_label (char *label, dw_die_ref ref)
3523 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3526 /* Return die_offset of a DIE reference to a base type. */
3528 static unsigned long int
3529 get_base_type_offset (dw_die_ref ref)
3531 if (ref->die_offset)
3532 return ref->die_offset;
3533 if (comp_unit_die ()->die_abbrev)
3535 calc_base_type_die_sizes ();
3536 gcc_assert (ref->die_offset);
3538 return ref->die_offset;
3541 /* Return die_offset of a DIE reference other than base type. */
3543 static unsigned long int
3544 get_ref_die_offset (dw_die_ref ref)
3546 gcc_assert (ref->die_offset);
3547 return ref->die_offset;
3550 /* Convert a DIE tag into its string name. */
3552 static const char *
3553 dwarf_tag_name (unsigned int tag)
3555 const char *name = get_DW_TAG_name (tag);
3557 if (name != NULL)
3558 return name;
3560 return "DW_TAG_<unknown>";
3563 /* Convert a DWARF attribute code into its string name. */
3565 static const char *
3566 dwarf_attr_name (unsigned int attr)
3568 const char *name;
3570 switch (attr)
3572 #if VMS_DEBUGGING_INFO
3573 case DW_AT_HP_prologue:
3574 return "DW_AT_HP_prologue";
3575 #else
3576 case DW_AT_MIPS_loop_unroll_factor:
3577 return "DW_AT_MIPS_loop_unroll_factor";
3578 #endif
3580 #if VMS_DEBUGGING_INFO
3581 case DW_AT_HP_epilogue:
3582 return "DW_AT_HP_epilogue";
3583 #else
3584 case DW_AT_MIPS_stride:
3585 return "DW_AT_MIPS_stride";
3586 #endif
3589 name = get_DW_AT_name (attr);
3591 if (name != NULL)
3592 return name;
3594 return "DW_AT_<unknown>";
3597 /* Convert a DWARF value form code into its string name. */
3599 static const char *
3600 dwarf_form_name (unsigned int form)
3602 const char *name = get_DW_FORM_name (form);
3604 if (name != NULL)
3605 return name;
3607 return "DW_FORM_<unknown>";
3610 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3611 instance of an inlined instance of a decl which is local to an inline
3612 function, so we have to trace all of the way back through the origin chain
3613 to find out what sort of node actually served as the original seed for the
3614 given block. */
3616 static tree
3617 decl_ultimate_origin (const_tree decl)
3619 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3620 return NULL_TREE;
3622 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3623 nodes in the function to point to themselves; ignore that if
3624 we're trying to output the abstract instance of this function. */
3625 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3626 return NULL_TREE;
3628 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3629 most distant ancestor, this should never happen. */
3630 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3632 return DECL_ABSTRACT_ORIGIN (decl);
3635 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3636 of a virtual function may refer to a base class, so we check the 'this'
3637 parameter. */
3639 static tree
3640 decl_class_context (tree decl)
3642 tree context = NULL_TREE;
3644 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3645 context = DECL_CONTEXT (decl);
3646 else
3647 context = TYPE_MAIN_VARIANT
3648 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3650 if (context && !TYPE_P (context))
3651 context = NULL_TREE;
3653 return context;
3656 /* Add an attribute/value pair to a DIE. */
3658 static inline void
3659 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3661 /* Maybe this should be an assert? */
3662 if (die == NULL)
3663 return;
3665 vec_safe_reserve (die->die_attr, 1);
3666 vec_safe_push (die->die_attr, *attr);
3669 static inline enum dw_val_class
3670 AT_class (dw_attr_ref a)
3672 return a->dw_attr_val.val_class;
3675 /* Return the index for any attribute that will be referenced with a
3676 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3677 are stored in dw_attr_val.v.val_str for reference counting
3678 pruning. */
3680 static inline unsigned int
3681 AT_index (dw_attr_ref a)
3683 if (AT_class (a) == dw_val_class_str)
3684 return a->dw_attr_val.v.val_str->index;
3685 else if (a->dw_attr_val.val_entry != NULL)
3686 return a->dw_attr_val.val_entry->index;
3687 return NOT_INDEXED;
3690 /* Add a flag value attribute to a DIE. */
3692 static inline void
3693 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3695 dw_attr_node attr;
3697 attr.dw_attr = attr_kind;
3698 attr.dw_attr_val.val_class = dw_val_class_flag;
3699 attr.dw_attr_val.val_entry = NULL;
3700 attr.dw_attr_val.v.val_flag = flag;
3701 add_dwarf_attr (die, &attr);
3704 static inline unsigned
3705 AT_flag (dw_attr_ref a)
3707 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3708 return a->dw_attr_val.v.val_flag;
3711 /* Add a signed integer attribute value to a DIE. */
3713 static inline void
3714 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3716 dw_attr_node attr;
3718 attr.dw_attr = attr_kind;
3719 attr.dw_attr_val.val_class = dw_val_class_const;
3720 attr.dw_attr_val.val_entry = NULL;
3721 attr.dw_attr_val.v.val_int = int_val;
3722 add_dwarf_attr (die, &attr);
3725 static inline HOST_WIDE_INT
3726 AT_int (dw_attr_ref a)
3728 gcc_assert (a && AT_class (a) == dw_val_class_const);
3729 return a->dw_attr_val.v.val_int;
3732 /* Add an unsigned integer attribute value to a DIE. */
3734 static inline void
3735 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3736 unsigned HOST_WIDE_INT unsigned_val)
3738 dw_attr_node attr;
3740 attr.dw_attr = attr_kind;
3741 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3742 attr.dw_attr_val.val_entry = NULL;
3743 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3744 add_dwarf_attr (die, &attr);
3747 static inline unsigned HOST_WIDE_INT
3748 AT_unsigned (dw_attr_ref a)
3750 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3751 return a->dw_attr_val.v.val_unsigned;
3754 /* Add an unsigned double integer attribute value to a DIE. */
3756 static inline void
3757 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3758 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3760 dw_attr_node attr;
3762 attr.dw_attr = attr_kind;
3763 attr.dw_attr_val.val_class = dw_val_class_const_double;
3764 attr.dw_attr_val.val_entry = NULL;
3765 attr.dw_attr_val.v.val_double.high = high;
3766 attr.dw_attr_val.v.val_double.low = low;
3767 add_dwarf_attr (die, &attr);
3770 /* Add a floating point attribute value to a DIE and return it. */
3772 static inline void
3773 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3774 unsigned int length, unsigned int elt_size, unsigned char *array)
3776 dw_attr_node attr;
3778 attr.dw_attr = attr_kind;
3779 attr.dw_attr_val.val_class = dw_val_class_vec;
3780 attr.dw_attr_val.val_entry = NULL;
3781 attr.dw_attr_val.v.val_vec.length = length;
3782 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3783 attr.dw_attr_val.v.val_vec.array = array;
3784 add_dwarf_attr (die, &attr);
3787 /* Add an 8-byte data attribute value to a DIE. */
3789 static inline void
3790 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3791 unsigned char data8[8])
3793 dw_attr_node attr;
3795 attr.dw_attr = attr_kind;
3796 attr.dw_attr_val.val_class = dw_val_class_data8;
3797 attr.dw_attr_val.val_entry = NULL;
3798 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3799 add_dwarf_attr (die, &attr);
3802 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3803 dwarf_split_debug_info, address attributes in dies destined for the
3804 final executable have force_direct set to avoid using indexed
3805 references. */
3807 static inline void
3808 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3809 bool force_direct)
3811 dw_attr_node attr;
3812 char * lbl_id;
3814 lbl_id = xstrdup (lbl_low);
3815 attr.dw_attr = DW_AT_low_pc;
3816 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3817 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3818 if (dwarf_split_debug_info && !force_direct)
3819 attr.dw_attr_val.val_entry
3820 = add_addr_table_entry (lbl_id, ate_kind_label);
3821 else
3822 attr.dw_attr_val.val_entry = NULL;
3823 add_dwarf_attr (die, &attr);
3825 attr.dw_attr = DW_AT_high_pc;
3826 if (dwarf_version < 4)
3827 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3828 else
3829 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3830 lbl_id = xstrdup (lbl_high);
3831 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3832 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3833 && dwarf_split_debug_info && !force_direct)
3834 attr.dw_attr_val.val_entry
3835 = add_addr_table_entry (lbl_id, ate_kind_label);
3836 else
3837 attr.dw_attr_val.val_entry = NULL;
3838 add_dwarf_attr (die, &attr);
3841 /* Hash and equality functions for debug_str_hash. */
3843 static hashval_t
3844 debug_str_do_hash (const void *x)
3846 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3849 static int
3850 debug_str_eq (const void *x1, const void *x2)
3852 return strcmp ((((const struct indirect_string_node *)x1)->str),
3853 (const char *)x2) == 0;
3856 /* Add STR to the given string hash table. */
3858 static struct indirect_string_node *
3859 find_AT_string_in_table (const char *str, htab_t table)
3861 struct indirect_string_node *node;
3862 void **slot;
3864 slot = htab_find_slot_with_hash (table, str,
3865 htab_hash_string (str), INSERT);
3866 if (*slot == NULL)
3868 node = ggc_alloc_cleared_indirect_string_node ();
3869 node->str = ggc_strdup (str);
3870 *slot = node;
3872 else
3873 node = (struct indirect_string_node *) *slot;
3875 node->refcount++;
3876 return node;
3879 /* Add STR to the indirect string hash table. */
3881 static struct indirect_string_node *
3882 find_AT_string (const char *str)
3884 if (! debug_str_hash)
3885 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3886 debug_str_eq, NULL);
3888 return find_AT_string_in_table (str, debug_str_hash);
3891 /* Add a string attribute value to a DIE. */
3893 static inline void
3894 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3896 dw_attr_node attr;
3897 struct indirect_string_node *node;
3899 node = find_AT_string (str);
3901 attr.dw_attr = attr_kind;
3902 attr.dw_attr_val.val_class = dw_val_class_str;
3903 attr.dw_attr_val.val_entry = NULL;
3904 attr.dw_attr_val.v.val_str = node;
3905 add_dwarf_attr (die, &attr);
3908 static inline const char *
3909 AT_string (dw_attr_ref a)
3911 gcc_assert (a && AT_class (a) == dw_val_class_str);
3912 return a->dw_attr_val.v.val_str->str;
3915 /* Call this function directly to bypass AT_string_form's logic to put
3916 the string inline in the die. */
3918 static void
3919 set_indirect_string (struct indirect_string_node *node)
3921 char label[32];
3922 /* Already indirect is a no op. */
3923 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3925 gcc_assert (node->label);
3926 return;
3928 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3929 ++dw2_string_counter;
3930 node->label = xstrdup (label);
3932 if (!dwarf_split_debug_info)
3934 node->form = DW_FORM_strp;
3935 node->index = NOT_INDEXED;
3937 else
3939 node->form = DW_FORM_GNU_str_index;
3940 node->index = NO_INDEX_ASSIGNED;
3944 /* Find out whether a string should be output inline in DIE
3945 or out-of-line in .debug_str section. */
3947 static enum dwarf_form
3948 find_string_form (struct indirect_string_node *node)
3950 unsigned int len;
3952 if (node->form)
3953 return node->form;
3955 len = strlen (node->str) + 1;
3957 /* If the string is shorter or equal to the size of the reference, it is
3958 always better to put it inline. */
3959 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3960 return node->form = DW_FORM_string;
3962 /* If we cannot expect the linker to merge strings in .debug_str
3963 section, only put it into .debug_str if it is worth even in this
3964 single module. */
3965 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3966 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3967 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3968 return node->form = DW_FORM_string;
3970 set_indirect_string (node);
3972 return node->form;
3975 /* Find out whether the string referenced from the attribute should be
3976 output inline in DIE or out-of-line in .debug_str section. */
3978 static enum dwarf_form
3979 AT_string_form (dw_attr_ref a)
3981 gcc_assert (a && AT_class (a) == dw_val_class_str);
3982 return find_string_form (a->dw_attr_val.v.val_str);
3985 /* Add a DIE reference attribute value to a DIE. */
3987 static inline void
3988 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3990 dw_attr_node attr;
3992 #ifdef ENABLE_CHECKING
3993 gcc_assert (targ_die != NULL);
3994 #else
3995 /* With LTO we can end up trying to reference something we didn't create
3996 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3997 if (targ_die == NULL)
3998 return;
3999 #endif
4001 attr.dw_attr = attr_kind;
4002 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4003 attr.dw_attr_val.val_entry = NULL;
4004 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4005 attr.dw_attr_val.v.val_die_ref.external = 0;
4006 add_dwarf_attr (die, &attr);
4009 /* Change DIE reference REF to point to NEW_DIE instead. */
4011 static inline void
4012 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4014 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4015 ref->dw_attr_val.v.val_die_ref.die = new_die;
4016 ref->dw_attr_val.v.val_die_ref.external = 0;
4019 /* Add an AT_specification attribute to a DIE, and also make the back
4020 pointer from the specification to the definition. */
4022 static inline void
4023 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4025 add_AT_die_ref (die, DW_AT_specification, targ_die);
4026 gcc_assert (!targ_die->die_definition);
4027 targ_die->die_definition = die;
4030 static inline dw_die_ref
4031 AT_ref (dw_attr_ref a)
4033 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4034 return a->dw_attr_val.v.val_die_ref.die;
4037 static inline int
4038 AT_ref_external (dw_attr_ref a)
4040 if (a && AT_class (a) == dw_val_class_die_ref)
4041 return a->dw_attr_val.v.val_die_ref.external;
4043 return 0;
4046 static inline void
4047 set_AT_ref_external (dw_attr_ref a, int i)
4049 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4050 a->dw_attr_val.v.val_die_ref.external = i;
4053 /* Add an FDE reference attribute value to a DIE. */
4055 static inline void
4056 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4058 dw_attr_node attr;
4060 attr.dw_attr = attr_kind;
4061 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4062 attr.dw_attr_val.val_entry = NULL;
4063 attr.dw_attr_val.v.val_fde_index = targ_fde;
4064 add_dwarf_attr (die, &attr);
4067 /* Add a location description attribute value to a DIE. */
4069 static inline void
4070 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4072 dw_attr_node attr;
4074 attr.dw_attr = attr_kind;
4075 attr.dw_attr_val.val_class = dw_val_class_loc;
4076 attr.dw_attr_val.val_entry = NULL;
4077 attr.dw_attr_val.v.val_loc = loc;
4078 add_dwarf_attr (die, &attr);
4081 static inline dw_loc_descr_ref
4082 AT_loc (dw_attr_ref a)
4084 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4085 return a->dw_attr_val.v.val_loc;
4088 static inline void
4089 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4091 dw_attr_node attr;
4093 attr.dw_attr = attr_kind;
4094 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4095 attr.dw_attr_val.val_entry = NULL;
4096 attr.dw_attr_val.v.val_loc_list = loc_list;
4097 add_dwarf_attr (die, &attr);
4098 have_location_lists = true;
4101 static inline dw_loc_list_ref
4102 AT_loc_list (dw_attr_ref a)
4104 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4105 return a->dw_attr_val.v.val_loc_list;
4108 static inline dw_loc_list_ref *
4109 AT_loc_list_ptr (dw_attr_ref a)
4111 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4112 return &a->dw_attr_val.v.val_loc_list;
4115 /* Table of entries into the .debug_addr section. */
4117 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4119 /* Hash an address_table_entry. */
4121 static hashval_t
4122 addr_table_entry_do_hash (const void *x)
4124 const addr_table_entry *a = (const addr_table_entry *) x;
4125 switch (a->kind)
4127 case ate_kind_rtx:
4128 return iterative_hash_rtx (a->addr.rtl, 0);
4129 case ate_kind_rtx_dtprel:
4130 return iterative_hash_rtx (a->addr.rtl, 1);
4131 case ate_kind_label:
4132 return htab_hash_string (a->addr.label);
4133 default:
4134 gcc_unreachable ();
4138 /* Determine equality for two address_table_entries. */
4140 static int
4141 addr_table_entry_eq (const void *x1, const void *x2)
4143 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4144 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4146 if (a1->kind != a2->kind)
4147 return 0;
4148 switch (a1->kind)
4150 case ate_kind_rtx:
4151 case ate_kind_rtx_dtprel:
4152 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4153 case ate_kind_label:
4154 return strcmp (a1->addr.label, a2->addr.label) == 0;
4155 default:
4156 gcc_unreachable ();
4160 /* Initialize an addr_table_entry. */
4162 void
4163 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4165 e->kind = kind;
4166 switch (kind)
4168 case ate_kind_rtx:
4169 case ate_kind_rtx_dtprel:
4170 e->addr.rtl = (rtx) addr;
4171 break;
4172 case ate_kind_label:
4173 e->addr.label = (char *) addr;
4174 break;
4176 e->refcount = 0;
4177 e->index = NO_INDEX_ASSIGNED;
4180 /* Add attr to the address table entry to the table. Defer setting an
4181 index until output time. */
4183 static addr_table_entry *
4184 add_addr_table_entry (void *addr, enum ate_kind kind)
4186 addr_table_entry *node;
4187 addr_table_entry finder;
4188 void **slot;
4190 gcc_assert (dwarf_split_debug_info);
4191 if (! addr_index_table)
4192 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4193 addr_table_entry_eq, NULL);
4194 init_addr_table_entry (&finder, kind, addr);
4195 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4197 if (*slot == HTAB_EMPTY_ENTRY)
4199 node = ggc_alloc_cleared_addr_table_entry ();
4200 init_addr_table_entry (node, kind, addr);
4201 *slot = node;
4203 else
4204 node = (addr_table_entry *) *slot;
4206 node->refcount++;
4207 return node;
4210 /* Remove an entry from the addr table by decrementing its refcount.
4211 Strictly, decrementing the refcount would be enough, but the
4212 assertion that the entry is actually in the table has found
4213 bugs. */
4215 static void
4216 remove_addr_table_entry (addr_table_entry *entry)
4218 addr_table_entry *node;
4220 gcc_assert (dwarf_split_debug_info && addr_index_table);
4221 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4222 /* After an index is assigned, the table is frozen. */
4223 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4224 node->refcount--;
4227 /* Given a location list, remove all addresses it refers to from the
4228 address_table. */
4230 static void
4231 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4233 for (; descr; descr = descr->dw_loc_next)
4234 if (descr->dw_loc_oprnd1.val_entry != NULL)
4236 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4237 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4241 /* A helper function for dwarf2out_finish called through
4242 htab_traverse. Assign an addr_table_entry its index. All entries
4243 must be collected into the table when this function is called,
4244 because the indexing code relies on htab_traverse to traverse nodes
4245 in the same order for each run. */
4247 static int
4248 index_addr_table_entry (void **h, void *v)
4250 addr_table_entry *node = (addr_table_entry *) *h;
4251 unsigned int *index = (unsigned int *) v;
4253 /* Don't index unreferenced nodes. */
4254 if (node->refcount == 0)
4255 return 1;
4257 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4258 node->index = *index;
4259 *index += 1;
4261 return 1;
4264 /* Add an address constant attribute value to a DIE. When using
4265 dwarf_split_debug_info, address attributes in dies destined for the
4266 final executable should be direct references--setting the parameter
4267 force_direct ensures this behavior. */
4269 static inline void
4270 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4271 bool force_direct)
4273 dw_attr_node attr;
4275 attr.dw_attr = attr_kind;
4276 attr.dw_attr_val.val_class = dw_val_class_addr;
4277 attr.dw_attr_val.v.val_addr = addr;
4278 if (dwarf_split_debug_info && !force_direct)
4279 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4280 else
4281 attr.dw_attr_val.val_entry = NULL;
4282 add_dwarf_attr (die, &attr);
4285 /* Get the RTX from to an address DIE attribute. */
4287 static inline rtx
4288 AT_addr (dw_attr_ref a)
4290 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4291 return a->dw_attr_val.v.val_addr;
4294 /* Add a file attribute value to a DIE. */
4296 static inline void
4297 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4298 struct dwarf_file_data *fd)
4300 dw_attr_node attr;
4302 attr.dw_attr = attr_kind;
4303 attr.dw_attr_val.val_class = dw_val_class_file;
4304 attr.dw_attr_val.val_entry = NULL;
4305 attr.dw_attr_val.v.val_file = fd;
4306 add_dwarf_attr (die, &attr);
4309 /* Get the dwarf_file_data from a file DIE attribute. */
4311 static inline struct dwarf_file_data *
4312 AT_file (dw_attr_ref a)
4314 gcc_assert (a && AT_class (a) == dw_val_class_file);
4315 return a->dw_attr_val.v.val_file;
4318 /* Add a vms delta attribute value to a DIE. */
4320 static inline void
4321 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4322 const char *lbl1, const char *lbl2)
4324 dw_attr_node attr;
4326 attr.dw_attr = attr_kind;
4327 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4328 attr.dw_attr_val.val_entry = NULL;
4329 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4330 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4331 add_dwarf_attr (die, &attr);
4334 /* Add a label identifier attribute value to a DIE. */
4336 static inline void
4337 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4338 const char *lbl_id)
4340 dw_attr_node attr;
4342 attr.dw_attr = attr_kind;
4343 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4344 attr.dw_attr_val.val_entry = NULL;
4345 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4346 if (dwarf_split_debug_info)
4347 attr.dw_attr_val.val_entry
4348 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4349 ate_kind_label);
4350 add_dwarf_attr (die, &attr);
4353 /* Add a section offset attribute value to a DIE, an offset into the
4354 debug_line section. */
4356 static inline void
4357 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4358 const char *label)
4360 dw_attr_node attr;
4362 attr.dw_attr = attr_kind;
4363 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4364 attr.dw_attr_val.val_entry = NULL;
4365 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4366 add_dwarf_attr (die, &attr);
4369 /* Add a section offset attribute value to a DIE, an offset into the
4370 debug_macinfo section. */
4372 static inline void
4373 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4374 const char *label)
4376 dw_attr_node attr;
4378 attr.dw_attr = attr_kind;
4379 attr.dw_attr_val.val_class = dw_val_class_macptr;
4380 attr.dw_attr_val.val_entry = NULL;
4381 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4382 add_dwarf_attr (die, &attr);
4385 /* Add an offset attribute value to a DIE. */
4387 static inline void
4388 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4389 unsigned HOST_WIDE_INT offset)
4391 dw_attr_node attr;
4393 attr.dw_attr = attr_kind;
4394 attr.dw_attr_val.val_class = dw_val_class_offset;
4395 attr.dw_attr_val.val_entry = NULL;
4396 attr.dw_attr_val.v.val_offset = offset;
4397 add_dwarf_attr (die, &attr);
4400 /* Add a range_list attribute value to a DIE. When using
4401 dwarf_split_debug_info, address attributes in dies destined for the
4402 final executable should be direct references--setting the parameter
4403 force_direct ensures this behavior. */
4405 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4406 #define RELOCATED_OFFSET (NULL)
4408 static void
4409 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4410 long unsigned int offset, bool force_direct)
4412 dw_attr_node attr;
4414 attr.dw_attr = attr_kind;
4415 attr.dw_attr_val.val_class = dw_val_class_range_list;
4416 /* For the range_list attribute, use val_entry to store whether the
4417 offset should follow split-debug-info or normal semantics. This
4418 value is read in output_range_list_offset. */
4419 if (dwarf_split_debug_info && !force_direct)
4420 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4421 else
4422 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4423 attr.dw_attr_val.v.val_offset = offset;
4424 add_dwarf_attr (die, &attr);
4427 /* Return the start label of a delta attribute. */
4429 static inline const char *
4430 AT_vms_delta1 (dw_attr_ref a)
4432 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4433 return a->dw_attr_val.v.val_vms_delta.lbl1;
4436 /* Return the end label of a delta attribute. */
4438 static inline const char *
4439 AT_vms_delta2 (dw_attr_ref a)
4441 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4442 return a->dw_attr_val.v.val_vms_delta.lbl2;
4445 static inline const char *
4446 AT_lbl (dw_attr_ref a)
4448 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4449 || AT_class (a) == dw_val_class_lineptr
4450 || AT_class (a) == dw_val_class_macptr
4451 || AT_class (a) == dw_val_class_high_pc));
4452 return a->dw_attr_val.v.val_lbl_id;
4455 /* Get the attribute of type attr_kind. */
4457 static dw_attr_ref
4458 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4460 dw_attr_ref a;
4461 unsigned ix;
4462 dw_die_ref spec = NULL;
4464 if (! die)
4465 return NULL;
4467 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4468 if (a->dw_attr == attr_kind)
4469 return a;
4470 else if (a->dw_attr == DW_AT_specification
4471 || a->dw_attr == DW_AT_abstract_origin)
4472 spec = AT_ref (a);
4474 if (spec)
4475 return get_AT (spec, attr_kind);
4477 return NULL;
4480 /* Returns the parent of the declaration of DIE. */
4482 static dw_die_ref
4483 get_die_parent (dw_die_ref die)
4485 dw_die_ref t;
4487 if (!die)
4488 return NULL;
4490 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4491 || (t = get_AT_ref (die, DW_AT_specification)))
4492 die = t;
4494 return die->die_parent;
4497 /* Return the "low pc" attribute value, typically associated with a subprogram
4498 DIE. Return null if the "low pc" attribute is either not present, or if it
4499 cannot be represented as an assembler label identifier. */
4501 static inline const char *
4502 get_AT_low_pc (dw_die_ref die)
4504 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4506 return a ? AT_lbl (a) : NULL;
4509 /* Return the "high pc" attribute value, typically associated with a subprogram
4510 DIE. Return null if the "high pc" attribute is either not present, or if it
4511 cannot be represented as an assembler label identifier. */
4513 static inline const char *
4514 get_AT_hi_pc (dw_die_ref die)
4516 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4518 return a ? AT_lbl (a) : NULL;
4521 /* Return the value of the string attribute designated by ATTR_KIND, or
4522 NULL if it is not present. */
4524 static inline const char *
4525 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4527 dw_attr_ref a = get_AT (die, attr_kind);
4529 return a ? AT_string (a) : NULL;
4532 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4533 if it is not present. */
4535 static inline int
4536 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4538 dw_attr_ref a = get_AT (die, attr_kind);
4540 return a ? AT_flag (a) : 0;
4543 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4544 if it is not present. */
4546 static inline unsigned
4547 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4549 dw_attr_ref a = get_AT (die, attr_kind);
4551 return a ? AT_unsigned (a) : 0;
4554 static inline dw_die_ref
4555 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4557 dw_attr_ref a = get_AT (die, attr_kind);
4559 return a ? AT_ref (a) : NULL;
4562 static inline struct dwarf_file_data *
4563 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4565 dw_attr_ref a = get_AT (die, attr_kind);
4567 return a ? AT_file (a) : NULL;
4570 /* Return TRUE if the language is C++. */
4572 static inline bool
4573 is_cxx (void)
4575 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4577 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4580 /* Return TRUE if the language is Java. */
4582 static inline bool
4583 is_java (void)
4585 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4587 return lang == DW_LANG_Java;
4590 /* Return TRUE if the language is Fortran. */
4592 static inline bool
4593 is_fortran (void)
4595 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4597 return (lang == DW_LANG_Fortran77
4598 || lang == DW_LANG_Fortran90
4599 || lang == DW_LANG_Fortran95);
4602 /* Return TRUE if the language is Ada. */
4604 static inline bool
4605 is_ada (void)
4607 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4609 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4612 /* Remove the specified attribute if present. */
4614 static void
4615 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4617 dw_attr_ref a;
4618 unsigned ix;
4620 if (! die)
4621 return;
4623 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4624 if (a->dw_attr == attr_kind)
4626 if (AT_class (a) == dw_val_class_str)
4627 if (a->dw_attr_val.v.val_str->refcount)
4628 a->dw_attr_val.v.val_str->refcount--;
4630 /* vec::ordered_remove should help reduce the number of abbrevs
4631 that are needed. */
4632 die->die_attr->ordered_remove (ix);
4633 return;
4637 /* Remove CHILD from its parent. PREV must have the property that
4638 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4640 static void
4641 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4643 gcc_assert (child->die_parent == prev->die_parent);
4644 gcc_assert (prev->die_sib == child);
4645 if (prev == child)
4647 gcc_assert (child->die_parent->die_child == child);
4648 prev = NULL;
4650 else
4651 prev->die_sib = child->die_sib;
4652 if (child->die_parent->die_child == child)
4653 child->die_parent->die_child = prev;
4656 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4657 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4659 static void
4660 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4662 dw_die_ref parent = old_child->die_parent;
4664 gcc_assert (parent == prev->die_parent);
4665 gcc_assert (prev->die_sib == old_child);
4667 new_child->die_parent = parent;
4668 if (prev == old_child)
4670 gcc_assert (parent->die_child == old_child);
4671 new_child->die_sib = new_child;
4673 else
4675 prev->die_sib = new_child;
4676 new_child->die_sib = old_child->die_sib;
4678 if (old_child->die_parent->die_child == old_child)
4679 old_child->die_parent->die_child = new_child;
4682 /* Move all children from OLD_PARENT to NEW_PARENT. */
4684 static void
4685 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4687 dw_die_ref c;
4688 new_parent->die_child = old_parent->die_child;
4689 old_parent->die_child = NULL;
4690 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4693 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4694 matches TAG. */
4696 static void
4697 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4699 dw_die_ref c;
4701 c = die->die_child;
4702 if (c) do {
4703 dw_die_ref prev = c;
4704 c = c->die_sib;
4705 while (c->die_tag == tag)
4707 remove_child_with_prev (c, prev);
4708 /* Might have removed every child. */
4709 if (c == c->die_sib)
4710 return;
4711 c = c->die_sib;
4713 } while (c != die->die_child);
4716 /* Add a CHILD_DIE as the last child of DIE. */
4718 static void
4719 add_child_die (dw_die_ref die, dw_die_ref child_die)
4721 /* FIXME this should probably be an assert. */
4722 if (! die || ! child_die)
4723 return;
4724 gcc_assert (die != child_die);
4726 child_die->die_parent = die;
4727 if (die->die_child)
4729 child_die->die_sib = die->die_child->die_sib;
4730 die->die_child->die_sib = child_die;
4732 else
4733 child_die->die_sib = child_die;
4734 die->die_child = child_die;
4737 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4738 is the specification, to the end of PARENT's list of children.
4739 This is done by removing and re-adding it. */
4741 static void
4742 splice_child_die (dw_die_ref parent, dw_die_ref child)
4744 dw_die_ref p;
4746 /* We want the declaration DIE from inside the class, not the
4747 specification DIE at toplevel. */
4748 if (child->die_parent != parent)
4750 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4752 if (tmp)
4753 child = tmp;
4756 gcc_assert (child->die_parent == parent
4757 || (child->die_parent
4758 == get_AT_ref (parent, DW_AT_specification)));
4760 for (p = child->die_parent->die_child; ; p = p->die_sib)
4761 if (p->die_sib == child)
4763 remove_child_with_prev (child, p);
4764 break;
4767 add_child_die (parent, child);
4770 /* Return a pointer to a newly created DIE node. */
4772 static inline dw_die_ref
4773 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4775 dw_die_ref die = ggc_alloc_cleared_die_node ();
4777 die->die_tag = tag_value;
4779 if (parent_die != NULL)
4780 add_child_die (parent_die, die);
4781 else
4783 limbo_die_node *limbo_node;
4785 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4786 limbo_node->die = die;
4787 limbo_node->created_for = t;
4788 limbo_node->next = limbo_die_list;
4789 limbo_die_list = limbo_node;
4792 return die;
4795 /* Return the DIE associated with the given type specifier. */
4797 static inline dw_die_ref
4798 lookup_type_die (tree type)
4800 return TYPE_SYMTAB_DIE (type);
4803 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4804 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4805 anonymous type instead the one of the naming typedef. */
4807 static inline dw_die_ref
4808 strip_naming_typedef (tree type, dw_die_ref type_die)
4810 if (type
4811 && TREE_CODE (type) == RECORD_TYPE
4812 && type_die
4813 && type_die->die_tag == DW_TAG_typedef
4814 && is_naming_typedef_decl (TYPE_NAME (type)))
4815 type_die = get_AT_ref (type_die, DW_AT_type);
4816 return type_die;
4819 /* Like lookup_type_die, but if type is an anonymous type named by a
4820 typedef[1], return the DIE of the anonymous type instead the one of
4821 the naming typedef. This is because in gen_typedef_die, we did
4822 equate the anonymous struct named by the typedef with the DIE of
4823 the naming typedef. So by default, lookup_type_die on an anonymous
4824 struct yields the DIE of the naming typedef.
4826 [1]: Read the comment of is_naming_typedef_decl to learn about what
4827 a naming typedef is. */
4829 static inline dw_die_ref
4830 lookup_type_die_strip_naming_typedef (tree type)
4832 dw_die_ref die = lookup_type_die (type);
4833 return strip_naming_typedef (type, die);
4836 /* Equate a DIE to a given type specifier. */
4838 static inline void
4839 equate_type_number_to_die (tree type, dw_die_ref type_die)
4841 TYPE_SYMTAB_DIE (type) = type_die;
4844 /* Returns a hash value for X (which really is a die_struct). */
4846 static hashval_t
4847 decl_die_table_hash (const void *x)
4849 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4852 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4854 static int
4855 decl_die_table_eq (const void *x, const void *y)
4857 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4860 /* Return the DIE associated with a given declaration. */
4862 static inline dw_die_ref
4863 lookup_decl_die (tree decl)
4865 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4868 /* Returns a hash value for X (which really is a var_loc_list). */
4870 static hashval_t
4871 decl_loc_table_hash (const void *x)
4873 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4876 /* Return nonzero if decl_id of var_loc_list X is the same as
4877 UID of decl *Y. */
4879 static int
4880 decl_loc_table_eq (const void *x, const void *y)
4882 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4885 /* Return the var_loc list associated with a given declaration. */
4887 static inline var_loc_list *
4888 lookup_decl_loc (const_tree decl)
4890 if (!decl_loc_table)
4891 return NULL;
4892 return (var_loc_list *)
4893 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4896 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4898 static hashval_t
4899 cached_dw_loc_list_table_hash (const void *x)
4901 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4904 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4905 UID of decl *Y. */
4907 static int
4908 cached_dw_loc_list_table_eq (const void *x, const void *y)
4910 return (((const cached_dw_loc_list *) x)->decl_id
4911 == DECL_UID ((const_tree) y));
4914 /* Equate a DIE to a particular declaration. */
4916 static void
4917 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4919 unsigned int decl_id = DECL_UID (decl);
4920 void **slot;
4922 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4923 *slot = decl_die;
4924 decl_die->decl_id = decl_id;
4927 /* Return how many bits covers PIECE EXPR_LIST. */
4929 static int
4930 decl_piece_bitsize (rtx piece)
4932 int ret = (int) GET_MODE (piece);
4933 if (ret)
4934 return ret;
4935 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4936 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4937 return INTVAL (XEXP (XEXP (piece, 0), 0));
4940 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4942 static rtx *
4943 decl_piece_varloc_ptr (rtx piece)
4945 if ((int) GET_MODE (piece))
4946 return &XEXP (piece, 0);
4947 else
4948 return &XEXP (XEXP (piece, 0), 1);
4951 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4952 Next is the chain of following piece nodes. */
4954 static rtx
4955 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4957 if (bitsize <= (int) MAX_MACHINE_MODE)
4958 return alloc_EXPR_LIST (bitsize, loc_note, next);
4959 else
4960 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4961 GEN_INT (bitsize),
4962 loc_note), next);
4965 /* Return rtx that should be stored into loc field for
4966 LOC_NOTE and BITPOS/BITSIZE. */
4968 static rtx
4969 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4970 HOST_WIDE_INT bitsize)
4972 if (bitsize != -1)
4974 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4975 if (bitpos != 0)
4976 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4978 return loc_note;
4981 /* This function either modifies location piece list *DEST in
4982 place (if SRC and INNER is NULL), or copies location piece list
4983 *SRC to *DEST while modifying it. Location BITPOS is modified
4984 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4985 not copied and if needed some padding around it is added.
4986 When modifying in place, DEST should point to EXPR_LIST where
4987 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4988 to the start of the whole list and INNER points to the EXPR_LIST
4989 where earlier pieces cover PIECE_BITPOS bits. */
4991 static void
4992 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4993 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4994 HOST_WIDE_INT bitsize, rtx loc_note)
4996 int diff;
4997 bool copy = inner != NULL;
4999 if (copy)
5001 /* First copy all nodes preceding the current bitpos. */
5002 while (src != inner)
5004 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5005 decl_piece_bitsize (*src), NULL_RTX);
5006 dest = &XEXP (*dest, 1);
5007 src = &XEXP (*src, 1);
5010 /* Add padding if needed. */
5011 if (bitpos != piece_bitpos)
5013 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5014 copy ? NULL_RTX : *dest);
5015 dest = &XEXP (*dest, 1);
5017 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5019 gcc_assert (!copy);
5020 /* A piece with correct bitpos and bitsize already exist,
5021 just update the location for it and return. */
5022 *decl_piece_varloc_ptr (*dest) = loc_note;
5023 return;
5025 /* Add the piece that changed. */
5026 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5027 dest = &XEXP (*dest, 1);
5028 /* Skip over pieces that overlap it. */
5029 diff = bitpos - piece_bitpos + bitsize;
5030 if (!copy)
5031 src = dest;
5032 while (diff > 0 && *src)
5034 rtx piece = *src;
5035 diff -= decl_piece_bitsize (piece);
5036 if (copy)
5037 src = &XEXP (piece, 1);
5038 else
5040 *src = XEXP (piece, 1);
5041 free_EXPR_LIST_node (piece);
5044 /* Add padding if needed. */
5045 if (diff < 0 && *src)
5047 if (!copy)
5048 dest = src;
5049 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5050 dest = &XEXP (*dest, 1);
5052 if (!copy)
5053 return;
5054 /* Finally copy all nodes following it. */
5055 while (*src)
5057 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5058 decl_piece_bitsize (*src), NULL_RTX);
5059 dest = &XEXP (*dest, 1);
5060 src = &XEXP (*src, 1);
5064 /* Add a variable location node to the linked list for DECL. */
5066 static struct var_loc_node *
5067 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5069 unsigned int decl_id;
5070 var_loc_list *temp;
5071 void **slot;
5072 struct var_loc_node *loc = NULL;
5073 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5075 if (TREE_CODE (decl) == VAR_DECL
5076 && DECL_HAS_DEBUG_EXPR_P (decl))
5078 tree realdecl = DECL_DEBUG_EXPR (decl);
5079 if (handled_component_p (realdecl)
5080 || (TREE_CODE (realdecl) == MEM_REF
5081 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5083 HOST_WIDE_INT maxsize;
5084 tree innerdecl;
5085 innerdecl
5086 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5087 if (!DECL_P (innerdecl)
5088 || DECL_IGNORED_P (innerdecl)
5089 || TREE_STATIC (innerdecl)
5090 || bitsize <= 0
5091 || bitpos + bitsize > 256
5092 || bitsize != maxsize)
5093 return NULL;
5094 decl = innerdecl;
5098 decl_id = DECL_UID (decl);
5099 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5100 if (*slot == NULL)
5102 temp = ggc_alloc_cleared_var_loc_list ();
5103 temp->decl_id = decl_id;
5104 *slot = temp;
5106 else
5107 temp = (var_loc_list *) *slot;
5109 /* For PARM_DECLs try to keep around the original incoming value,
5110 even if that means we'll emit a zero-range .debug_loc entry. */
5111 if (temp->last
5112 && temp->first == temp->last
5113 && TREE_CODE (decl) == PARM_DECL
5114 && NOTE_P (temp->first->loc)
5115 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5116 && DECL_INCOMING_RTL (decl)
5117 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5118 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5119 == GET_CODE (DECL_INCOMING_RTL (decl))
5120 && prev_real_insn (temp->first->loc) == NULL_RTX
5121 && (bitsize != -1
5122 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5123 NOTE_VAR_LOCATION_LOC (loc_note))
5124 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5125 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5127 loc = ggc_alloc_cleared_var_loc_node ();
5128 temp->first->next = loc;
5129 temp->last = loc;
5130 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5132 else if (temp->last)
5134 struct var_loc_node *last = temp->last, *unused = NULL;
5135 rtx *piece_loc = NULL, last_loc_note;
5136 int piece_bitpos = 0;
5137 if (last->next)
5139 last = last->next;
5140 gcc_assert (last->next == NULL);
5142 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5144 piece_loc = &last->loc;
5147 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5148 if (piece_bitpos + cur_bitsize > bitpos)
5149 break;
5150 piece_bitpos += cur_bitsize;
5151 piece_loc = &XEXP (*piece_loc, 1);
5153 while (*piece_loc);
5155 /* TEMP->LAST here is either pointer to the last but one or
5156 last element in the chained list, LAST is pointer to the
5157 last element. */
5158 if (label && strcmp (last->label, label) == 0)
5160 /* For SRA optimized variables if there weren't any real
5161 insns since last note, just modify the last node. */
5162 if (piece_loc != NULL)
5164 adjust_piece_list (piece_loc, NULL, NULL,
5165 bitpos, piece_bitpos, bitsize, loc_note);
5166 return NULL;
5168 /* If the last note doesn't cover any instructions, remove it. */
5169 if (temp->last != last)
5171 temp->last->next = NULL;
5172 unused = last;
5173 last = temp->last;
5174 gcc_assert (strcmp (last->label, label) != 0);
5176 else
5178 gcc_assert (temp->first == temp->last
5179 || (temp->first->next == temp->last
5180 && TREE_CODE (decl) == PARM_DECL));
5181 memset (temp->last, '\0', sizeof (*temp->last));
5182 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5183 return temp->last;
5186 if (bitsize == -1 && NOTE_P (last->loc))
5187 last_loc_note = last->loc;
5188 else if (piece_loc != NULL
5189 && *piece_loc != NULL_RTX
5190 && piece_bitpos == bitpos
5191 && decl_piece_bitsize (*piece_loc) == bitsize)
5192 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5193 else
5194 last_loc_note = NULL_RTX;
5195 /* If the current location is the same as the end of the list,
5196 and either both or neither of the locations is uninitialized,
5197 we have nothing to do. */
5198 if (last_loc_note == NULL_RTX
5199 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5200 NOTE_VAR_LOCATION_LOC (loc_note)))
5201 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5202 != NOTE_VAR_LOCATION_STATUS (loc_note))
5203 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5204 == VAR_INIT_STATUS_UNINITIALIZED)
5205 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5206 == VAR_INIT_STATUS_UNINITIALIZED))))
5208 /* Add LOC to the end of list and update LAST. If the last
5209 element of the list has been removed above, reuse its
5210 memory for the new node, otherwise allocate a new one. */
5211 if (unused)
5213 loc = unused;
5214 memset (loc, '\0', sizeof (*loc));
5216 else
5217 loc = ggc_alloc_cleared_var_loc_node ();
5218 if (bitsize == -1 || piece_loc == NULL)
5219 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5220 else
5221 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5222 bitpos, piece_bitpos, bitsize, loc_note);
5223 last->next = loc;
5224 /* Ensure TEMP->LAST will point either to the new last but one
5225 element of the chain, or to the last element in it. */
5226 if (last != temp->last)
5227 temp->last = last;
5229 else if (unused)
5230 ggc_free (unused);
5232 else
5234 loc = ggc_alloc_cleared_var_loc_node ();
5235 temp->first = loc;
5236 temp->last = loc;
5237 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5239 return loc;
5242 /* Keep track of the number of spaces used to indent the
5243 output of the debugging routines that print the structure of
5244 the DIE internal representation. */
5245 static int print_indent;
5247 /* Indent the line the number of spaces given by print_indent. */
5249 static inline void
5250 print_spaces (FILE *outfile)
5252 fprintf (outfile, "%*s", print_indent, "");
5255 /* Print a type signature in hex. */
5257 static inline void
5258 print_signature (FILE *outfile, char *sig)
5260 int i;
5262 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5263 fprintf (outfile, "%02x", sig[i] & 0xff);
5266 /* Print the information associated with a given DIE, and its children.
5267 This routine is a debugging aid only. */
5269 static void
5270 print_die (dw_die_ref die, FILE *outfile)
5272 dw_attr_ref a;
5273 dw_die_ref c;
5274 unsigned ix;
5276 print_spaces (outfile);
5277 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5278 die->die_offset, dwarf_tag_name (die->die_tag),
5279 (void*) die);
5280 print_spaces (outfile);
5281 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5282 fprintf (outfile, " offset: %ld", die->die_offset);
5283 fprintf (outfile, " mark: %d\n", die->die_mark);
5285 if (die->comdat_type_p)
5287 print_spaces (outfile);
5288 fprintf (outfile, " signature: ");
5289 print_signature (outfile, die->die_id.die_type_node->signature);
5290 fprintf (outfile, "\n");
5293 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5295 print_spaces (outfile);
5296 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5298 switch (AT_class (a))
5300 case dw_val_class_addr:
5301 fprintf (outfile, "address");
5302 break;
5303 case dw_val_class_offset:
5304 fprintf (outfile, "offset");
5305 break;
5306 case dw_val_class_loc:
5307 fprintf (outfile, "location descriptor");
5308 break;
5309 case dw_val_class_loc_list:
5310 fprintf (outfile, "location list -> label:%s",
5311 AT_loc_list (a)->ll_symbol);
5312 break;
5313 case dw_val_class_range_list:
5314 fprintf (outfile, "range list");
5315 break;
5316 case dw_val_class_const:
5317 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5318 break;
5319 case dw_val_class_unsigned_const:
5320 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5321 break;
5322 case dw_val_class_const_double:
5323 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5324 HOST_WIDE_INT_PRINT_UNSIGNED")",
5325 a->dw_attr_val.v.val_double.high,
5326 a->dw_attr_val.v.val_double.low);
5327 break;
5328 case dw_val_class_vec:
5329 fprintf (outfile, "floating-point or vector constant");
5330 break;
5331 case dw_val_class_flag:
5332 fprintf (outfile, "%u", AT_flag (a));
5333 break;
5334 case dw_val_class_die_ref:
5335 if (AT_ref (a) != NULL)
5337 if (AT_ref (a)->comdat_type_p)
5339 fprintf (outfile, "die -> signature: ");
5340 print_signature (outfile,
5341 AT_ref (a)->die_id.die_type_node->signature);
5343 else if (AT_ref (a)->die_id.die_symbol)
5344 fprintf (outfile, "die -> label: %s",
5345 AT_ref (a)->die_id.die_symbol);
5346 else
5347 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5348 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5350 else
5351 fprintf (outfile, "die -> <null>");
5352 break;
5353 case dw_val_class_vms_delta:
5354 fprintf (outfile, "delta: @slotcount(%s-%s)",
5355 AT_vms_delta2 (a), AT_vms_delta1 (a));
5356 break;
5357 case dw_val_class_lbl_id:
5358 case dw_val_class_lineptr:
5359 case dw_val_class_macptr:
5360 case dw_val_class_high_pc:
5361 fprintf (outfile, "label: %s", AT_lbl (a));
5362 break;
5363 case dw_val_class_str:
5364 if (AT_string (a) != NULL)
5365 fprintf (outfile, "\"%s\"", AT_string (a));
5366 else
5367 fprintf (outfile, "<null>");
5368 break;
5369 case dw_val_class_file:
5370 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5371 AT_file (a)->emitted_number);
5372 break;
5373 case dw_val_class_data8:
5375 int i;
5377 for (i = 0; i < 8; i++)
5378 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5379 break;
5381 default:
5382 break;
5385 fprintf (outfile, "\n");
5388 if (die->die_child != NULL)
5390 print_indent += 4;
5391 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5392 print_indent -= 4;
5394 if (print_indent == 0)
5395 fprintf (outfile, "\n");
5398 /* Print the information collected for a given DIE. */
5400 DEBUG_FUNCTION void
5401 debug_dwarf_die (dw_die_ref die)
5403 print_die (die, stderr);
5406 DEBUG_FUNCTION void
5407 debug (die_struct &ref)
5409 print_die (&ref, stderr);
5412 DEBUG_FUNCTION void
5413 debug (die_struct *ptr)
5415 if (ptr)
5416 debug (*ptr);
5417 else
5418 fprintf (stderr, "<nil>\n");
5422 /* Print all DWARF information collected for the compilation unit.
5423 This routine is a debugging aid only. */
5425 DEBUG_FUNCTION void
5426 debug_dwarf (void)
5428 print_indent = 0;
5429 print_die (comp_unit_die (), stderr);
5432 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5433 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5434 DIE that marks the start of the DIEs for this include file. */
5436 static dw_die_ref
5437 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5439 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5440 dw_die_ref new_unit = gen_compile_unit_die (filename);
5442 new_unit->die_sib = old_unit;
5443 return new_unit;
5446 /* Close an include-file CU and reopen the enclosing one. */
5448 static dw_die_ref
5449 pop_compile_unit (dw_die_ref old_unit)
5451 dw_die_ref new_unit = old_unit->die_sib;
5453 old_unit->die_sib = NULL;
5454 return new_unit;
5457 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5458 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5459 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5461 /* Calculate the checksum of a location expression. */
5463 static inline void
5464 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5466 int tem;
5467 hashval_t hash = 0;
5469 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5470 CHECKSUM (tem);
5471 hash = hash_loc_operands (loc, hash);
5472 CHECKSUM (hash);
5475 /* Calculate the checksum of an attribute. */
5477 static void
5478 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5480 dw_loc_descr_ref loc;
5481 rtx r;
5483 CHECKSUM (at->dw_attr);
5485 /* We don't care that this was compiled with a different compiler
5486 snapshot; if the output is the same, that's what matters. */
5487 if (at->dw_attr == DW_AT_producer)
5488 return;
5490 switch (AT_class (at))
5492 case dw_val_class_const:
5493 CHECKSUM (at->dw_attr_val.v.val_int);
5494 break;
5495 case dw_val_class_unsigned_const:
5496 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5497 break;
5498 case dw_val_class_const_double:
5499 CHECKSUM (at->dw_attr_val.v.val_double);
5500 break;
5501 case dw_val_class_vec:
5502 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5503 (at->dw_attr_val.v.val_vec.length
5504 * at->dw_attr_val.v.val_vec.elt_size));
5505 break;
5506 case dw_val_class_flag:
5507 CHECKSUM (at->dw_attr_val.v.val_flag);
5508 break;
5509 case dw_val_class_str:
5510 CHECKSUM_STRING (AT_string (at));
5511 break;
5513 case dw_val_class_addr:
5514 r = AT_addr (at);
5515 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5516 CHECKSUM_STRING (XSTR (r, 0));
5517 break;
5519 case dw_val_class_offset:
5520 CHECKSUM (at->dw_attr_val.v.val_offset);
5521 break;
5523 case dw_val_class_loc:
5524 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5525 loc_checksum (loc, ctx);
5526 break;
5528 case dw_val_class_die_ref:
5529 die_checksum (AT_ref (at), ctx, mark);
5530 break;
5532 case dw_val_class_fde_ref:
5533 case dw_val_class_vms_delta:
5534 case dw_val_class_lbl_id:
5535 case dw_val_class_lineptr:
5536 case dw_val_class_macptr:
5537 case dw_val_class_high_pc:
5538 break;
5540 case dw_val_class_file:
5541 CHECKSUM_STRING (AT_file (at)->filename);
5542 break;
5544 case dw_val_class_data8:
5545 CHECKSUM (at->dw_attr_val.v.val_data8);
5546 break;
5548 default:
5549 break;
5553 /* Calculate the checksum of a DIE. */
5555 static void
5556 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5558 dw_die_ref c;
5559 dw_attr_ref a;
5560 unsigned ix;
5562 /* To avoid infinite recursion. */
5563 if (die->die_mark)
5565 CHECKSUM (die->die_mark);
5566 return;
5568 die->die_mark = ++(*mark);
5570 CHECKSUM (die->die_tag);
5572 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5573 attr_checksum (a, ctx, mark);
5575 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5578 #undef CHECKSUM
5579 #undef CHECKSUM_BLOCK
5580 #undef CHECKSUM_STRING
5582 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5583 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5584 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5585 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5586 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5587 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5588 #define CHECKSUM_ATTR(FOO) \
5589 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5591 /* Calculate the checksum of a number in signed LEB128 format. */
5593 static void
5594 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5596 unsigned char byte;
5597 bool more;
5599 while (1)
5601 byte = (value & 0x7f);
5602 value >>= 7;
5603 more = !((value == 0 && (byte & 0x40) == 0)
5604 || (value == -1 && (byte & 0x40) != 0));
5605 if (more)
5606 byte |= 0x80;
5607 CHECKSUM (byte);
5608 if (!more)
5609 break;
5613 /* Calculate the checksum of a number in unsigned LEB128 format. */
5615 static void
5616 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5618 while (1)
5620 unsigned char byte = (value & 0x7f);
5621 value >>= 7;
5622 if (value != 0)
5623 /* More bytes to follow. */
5624 byte |= 0x80;
5625 CHECKSUM (byte);
5626 if (value == 0)
5627 break;
5631 /* Checksum the context of the DIE. This adds the names of any
5632 surrounding namespaces or structures to the checksum. */
5634 static void
5635 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5637 const char *name;
5638 dw_die_ref spec;
5639 int tag = die->die_tag;
5641 if (tag != DW_TAG_namespace
5642 && tag != DW_TAG_structure_type
5643 && tag != DW_TAG_class_type)
5644 return;
5646 name = get_AT_string (die, DW_AT_name);
5648 spec = get_AT_ref (die, DW_AT_specification);
5649 if (spec != NULL)
5650 die = spec;
5652 if (die->die_parent != NULL)
5653 checksum_die_context (die->die_parent, ctx);
5655 CHECKSUM_ULEB128 ('C');
5656 CHECKSUM_ULEB128 (tag);
5657 if (name != NULL)
5658 CHECKSUM_STRING (name);
5661 /* Calculate the checksum of a location expression. */
5663 static inline void
5664 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5666 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5667 were emitted as a DW_FORM_sdata instead of a location expression. */
5668 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5670 CHECKSUM_ULEB128 (DW_FORM_sdata);
5671 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5672 return;
5675 /* Otherwise, just checksum the raw location expression. */
5676 while (loc != NULL)
5678 hashval_t hash = 0;
5680 CHECKSUM_ULEB128 (loc->dtprel);
5681 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5682 hash = hash_loc_operands (loc, hash);
5683 CHECKSUM (hash);
5684 loc = loc->dw_loc_next;
5688 /* Calculate the checksum of an attribute. */
5690 static void
5691 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5692 struct md5_ctx *ctx, int *mark)
5694 dw_loc_descr_ref loc;
5695 rtx r;
5697 if (AT_class (at) == dw_val_class_die_ref)
5699 dw_die_ref target_die = AT_ref (at);
5701 /* For pointer and reference types, we checksum only the (qualified)
5702 name of the target type (if there is a name). For friend entries,
5703 we checksum only the (qualified) name of the target type or function.
5704 This allows the checksum to remain the same whether the target type
5705 is complete or not. */
5706 if ((at->dw_attr == DW_AT_type
5707 && (tag == DW_TAG_pointer_type
5708 || tag == DW_TAG_reference_type
5709 || tag == DW_TAG_rvalue_reference_type
5710 || tag == DW_TAG_ptr_to_member_type))
5711 || (at->dw_attr == DW_AT_friend
5712 && tag == DW_TAG_friend))
5714 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5716 if (name_attr != NULL)
5718 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5720 if (decl == NULL)
5721 decl = target_die;
5722 CHECKSUM_ULEB128 ('N');
5723 CHECKSUM_ULEB128 (at->dw_attr);
5724 if (decl->die_parent != NULL)
5725 checksum_die_context (decl->die_parent, ctx);
5726 CHECKSUM_ULEB128 ('E');
5727 CHECKSUM_STRING (AT_string (name_attr));
5728 return;
5732 /* For all other references to another DIE, we check to see if the
5733 target DIE has already been visited. If it has, we emit a
5734 backward reference; if not, we descend recursively. */
5735 if (target_die->die_mark > 0)
5737 CHECKSUM_ULEB128 ('R');
5738 CHECKSUM_ULEB128 (at->dw_attr);
5739 CHECKSUM_ULEB128 (target_die->die_mark);
5741 else
5743 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5745 if (decl == NULL)
5746 decl = target_die;
5747 target_die->die_mark = ++(*mark);
5748 CHECKSUM_ULEB128 ('T');
5749 CHECKSUM_ULEB128 (at->dw_attr);
5750 if (decl->die_parent != NULL)
5751 checksum_die_context (decl->die_parent, ctx);
5752 die_checksum_ordered (target_die, ctx, mark);
5754 return;
5757 CHECKSUM_ULEB128 ('A');
5758 CHECKSUM_ULEB128 (at->dw_attr);
5760 switch (AT_class (at))
5762 case dw_val_class_const:
5763 CHECKSUM_ULEB128 (DW_FORM_sdata);
5764 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5765 break;
5767 case dw_val_class_unsigned_const:
5768 CHECKSUM_ULEB128 (DW_FORM_sdata);
5769 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5770 break;
5772 case dw_val_class_const_double:
5773 CHECKSUM_ULEB128 (DW_FORM_block);
5774 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5775 CHECKSUM (at->dw_attr_val.v.val_double);
5776 break;
5778 case dw_val_class_vec:
5779 CHECKSUM_ULEB128 (DW_FORM_block);
5780 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5781 * at->dw_attr_val.v.val_vec.elt_size);
5782 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5783 (at->dw_attr_val.v.val_vec.length
5784 * at->dw_attr_val.v.val_vec.elt_size));
5785 break;
5787 case dw_val_class_flag:
5788 CHECKSUM_ULEB128 (DW_FORM_flag);
5789 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5790 break;
5792 case dw_val_class_str:
5793 CHECKSUM_ULEB128 (DW_FORM_string);
5794 CHECKSUM_STRING (AT_string (at));
5795 break;
5797 case dw_val_class_addr:
5798 r = AT_addr (at);
5799 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5800 CHECKSUM_ULEB128 (DW_FORM_string);
5801 CHECKSUM_STRING (XSTR (r, 0));
5802 break;
5804 case dw_val_class_offset:
5805 CHECKSUM_ULEB128 (DW_FORM_sdata);
5806 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5807 break;
5809 case dw_val_class_loc:
5810 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5811 loc_checksum_ordered (loc, ctx);
5812 break;
5814 case dw_val_class_fde_ref:
5815 case dw_val_class_lbl_id:
5816 case dw_val_class_lineptr:
5817 case dw_val_class_macptr:
5818 case dw_val_class_high_pc:
5819 break;
5821 case dw_val_class_file:
5822 CHECKSUM_ULEB128 (DW_FORM_string);
5823 CHECKSUM_STRING (AT_file (at)->filename);
5824 break;
5826 case dw_val_class_data8:
5827 CHECKSUM (at->dw_attr_val.v.val_data8);
5828 break;
5830 default:
5831 break;
5835 struct checksum_attributes
5837 dw_attr_ref at_name;
5838 dw_attr_ref at_type;
5839 dw_attr_ref at_friend;
5840 dw_attr_ref at_accessibility;
5841 dw_attr_ref at_address_class;
5842 dw_attr_ref at_allocated;
5843 dw_attr_ref at_artificial;
5844 dw_attr_ref at_associated;
5845 dw_attr_ref at_binary_scale;
5846 dw_attr_ref at_bit_offset;
5847 dw_attr_ref at_bit_size;
5848 dw_attr_ref at_bit_stride;
5849 dw_attr_ref at_byte_size;
5850 dw_attr_ref at_byte_stride;
5851 dw_attr_ref at_const_value;
5852 dw_attr_ref at_containing_type;
5853 dw_attr_ref at_count;
5854 dw_attr_ref at_data_location;
5855 dw_attr_ref at_data_member_location;
5856 dw_attr_ref at_decimal_scale;
5857 dw_attr_ref at_decimal_sign;
5858 dw_attr_ref at_default_value;
5859 dw_attr_ref at_digit_count;
5860 dw_attr_ref at_discr;
5861 dw_attr_ref at_discr_list;
5862 dw_attr_ref at_discr_value;
5863 dw_attr_ref at_encoding;
5864 dw_attr_ref at_endianity;
5865 dw_attr_ref at_explicit;
5866 dw_attr_ref at_is_optional;
5867 dw_attr_ref at_location;
5868 dw_attr_ref at_lower_bound;
5869 dw_attr_ref at_mutable;
5870 dw_attr_ref at_ordering;
5871 dw_attr_ref at_picture_string;
5872 dw_attr_ref at_prototyped;
5873 dw_attr_ref at_small;
5874 dw_attr_ref at_segment;
5875 dw_attr_ref at_string_length;
5876 dw_attr_ref at_threads_scaled;
5877 dw_attr_ref at_upper_bound;
5878 dw_attr_ref at_use_location;
5879 dw_attr_ref at_use_UTF8;
5880 dw_attr_ref at_variable_parameter;
5881 dw_attr_ref at_virtuality;
5882 dw_attr_ref at_visibility;
5883 dw_attr_ref at_vtable_elem_location;
5886 /* Collect the attributes that we will want to use for the checksum. */
5888 static void
5889 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5891 dw_attr_ref a;
5892 unsigned ix;
5894 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5896 switch (a->dw_attr)
5898 case DW_AT_name:
5899 attrs->at_name = a;
5900 break;
5901 case DW_AT_type:
5902 attrs->at_type = a;
5903 break;
5904 case DW_AT_friend:
5905 attrs->at_friend = a;
5906 break;
5907 case DW_AT_accessibility:
5908 attrs->at_accessibility = a;
5909 break;
5910 case DW_AT_address_class:
5911 attrs->at_address_class = a;
5912 break;
5913 case DW_AT_allocated:
5914 attrs->at_allocated = a;
5915 break;
5916 case DW_AT_artificial:
5917 attrs->at_artificial = a;
5918 break;
5919 case DW_AT_associated:
5920 attrs->at_associated = a;
5921 break;
5922 case DW_AT_binary_scale:
5923 attrs->at_binary_scale = a;
5924 break;
5925 case DW_AT_bit_offset:
5926 attrs->at_bit_offset = a;
5927 break;
5928 case DW_AT_bit_size:
5929 attrs->at_bit_size = a;
5930 break;
5931 case DW_AT_bit_stride:
5932 attrs->at_bit_stride = a;
5933 break;
5934 case DW_AT_byte_size:
5935 attrs->at_byte_size = a;
5936 break;
5937 case DW_AT_byte_stride:
5938 attrs->at_byte_stride = a;
5939 break;
5940 case DW_AT_const_value:
5941 attrs->at_const_value = a;
5942 break;
5943 case DW_AT_containing_type:
5944 attrs->at_containing_type = a;
5945 break;
5946 case DW_AT_count:
5947 attrs->at_count = a;
5948 break;
5949 case DW_AT_data_location:
5950 attrs->at_data_location = a;
5951 break;
5952 case DW_AT_data_member_location:
5953 attrs->at_data_member_location = a;
5954 break;
5955 case DW_AT_decimal_scale:
5956 attrs->at_decimal_scale = a;
5957 break;
5958 case DW_AT_decimal_sign:
5959 attrs->at_decimal_sign = a;
5960 break;
5961 case DW_AT_default_value:
5962 attrs->at_default_value = a;
5963 break;
5964 case DW_AT_digit_count:
5965 attrs->at_digit_count = a;
5966 break;
5967 case DW_AT_discr:
5968 attrs->at_discr = a;
5969 break;
5970 case DW_AT_discr_list:
5971 attrs->at_discr_list = a;
5972 break;
5973 case DW_AT_discr_value:
5974 attrs->at_discr_value = a;
5975 break;
5976 case DW_AT_encoding:
5977 attrs->at_encoding = a;
5978 break;
5979 case DW_AT_endianity:
5980 attrs->at_endianity = a;
5981 break;
5982 case DW_AT_explicit:
5983 attrs->at_explicit = a;
5984 break;
5985 case DW_AT_is_optional:
5986 attrs->at_is_optional = a;
5987 break;
5988 case DW_AT_location:
5989 attrs->at_location = a;
5990 break;
5991 case DW_AT_lower_bound:
5992 attrs->at_lower_bound = a;
5993 break;
5994 case DW_AT_mutable:
5995 attrs->at_mutable = a;
5996 break;
5997 case DW_AT_ordering:
5998 attrs->at_ordering = a;
5999 break;
6000 case DW_AT_picture_string:
6001 attrs->at_picture_string = a;
6002 break;
6003 case DW_AT_prototyped:
6004 attrs->at_prototyped = a;
6005 break;
6006 case DW_AT_small:
6007 attrs->at_small = a;
6008 break;
6009 case DW_AT_segment:
6010 attrs->at_segment = a;
6011 break;
6012 case DW_AT_string_length:
6013 attrs->at_string_length = a;
6014 break;
6015 case DW_AT_threads_scaled:
6016 attrs->at_threads_scaled = a;
6017 break;
6018 case DW_AT_upper_bound:
6019 attrs->at_upper_bound = a;
6020 break;
6021 case DW_AT_use_location:
6022 attrs->at_use_location = a;
6023 break;
6024 case DW_AT_use_UTF8:
6025 attrs->at_use_UTF8 = a;
6026 break;
6027 case DW_AT_variable_parameter:
6028 attrs->at_variable_parameter = a;
6029 break;
6030 case DW_AT_virtuality:
6031 attrs->at_virtuality = a;
6032 break;
6033 case DW_AT_visibility:
6034 attrs->at_visibility = a;
6035 break;
6036 case DW_AT_vtable_elem_location:
6037 attrs->at_vtable_elem_location = a;
6038 break;
6039 default:
6040 break;
6045 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6047 static void
6048 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6050 dw_die_ref c;
6051 dw_die_ref decl;
6052 struct checksum_attributes attrs;
6054 CHECKSUM_ULEB128 ('D');
6055 CHECKSUM_ULEB128 (die->die_tag);
6057 memset (&attrs, 0, sizeof (attrs));
6059 decl = get_AT_ref (die, DW_AT_specification);
6060 if (decl != NULL)
6061 collect_checksum_attributes (&attrs, decl);
6062 collect_checksum_attributes (&attrs, die);
6064 CHECKSUM_ATTR (attrs.at_name);
6065 CHECKSUM_ATTR (attrs.at_accessibility);
6066 CHECKSUM_ATTR (attrs.at_address_class);
6067 CHECKSUM_ATTR (attrs.at_allocated);
6068 CHECKSUM_ATTR (attrs.at_artificial);
6069 CHECKSUM_ATTR (attrs.at_associated);
6070 CHECKSUM_ATTR (attrs.at_binary_scale);
6071 CHECKSUM_ATTR (attrs.at_bit_offset);
6072 CHECKSUM_ATTR (attrs.at_bit_size);
6073 CHECKSUM_ATTR (attrs.at_bit_stride);
6074 CHECKSUM_ATTR (attrs.at_byte_size);
6075 CHECKSUM_ATTR (attrs.at_byte_stride);
6076 CHECKSUM_ATTR (attrs.at_const_value);
6077 CHECKSUM_ATTR (attrs.at_containing_type);
6078 CHECKSUM_ATTR (attrs.at_count);
6079 CHECKSUM_ATTR (attrs.at_data_location);
6080 CHECKSUM_ATTR (attrs.at_data_member_location);
6081 CHECKSUM_ATTR (attrs.at_decimal_scale);
6082 CHECKSUM_ATTR (attrs.at_decimal_sign);
6083 CHECKSUM_ATTR (attrs.at_default_value);
6084 CHECKSUM_ATTR (attrs.at_digit_count);
6085 CHECKSUM_ATTR (attrs.at_discr);
6086 CHECKSUM_ATTR (attrs.at_discr_list);
6087 CHECKSUM_ATTR (attrs.at_discr_value);
6088 CHECKSUM_ATTR (attrs.at_encoding);
6089 CHECKSUM_ATTR (attrs.at_endianity);
6090 CHECKSUM_ATTR (attrs.at_explicit);
6091 CHECKSUM_ATTR (attrs.at_is_optional);
6092 CHECKSUM_ATTR (attrs.at_location);
6093 CHECKSUM_ATTR (attrs.at_lower_bound);
6094 CHECKSUM_ATTR (attrs.at_mutable);
6095 CHECKSUM_ATTR (attrs.at_ordering);
6096 CHECKSUM_ATTR (attrs.at_picture_string);
6097 CHECKSUM_ATTR (attrs.at_prototyped);
6098 CHECKSUM_ATTR (attrs.at_small);
6099 CHECKSUM_ATTR (attrs.at_segment);
6100 CHECKSUM_ATTR (attrs.at_string_length);
6101 CHECKSUM_ATTR (attrs.at_threads_scaled);
6102 CHECKSUM_ATTR (attrs.at_upper_bound);
6103 CHECKSUM_ATTR (attrs.at_use_location);
6104 CHECKSUM_ATTR (attrs.at_use_UTF8);
6105 CHECKSUM_ATTR (attrs.at_variable_parameter);
6106 CHECKSUM_ATTR (attrs.at_virtuality);
6107 CHECKSUM_ATTR (attrs.at_visibility);
6108 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6109 CHECKSUM_ATTR (attrs.at_type);
6110 CHECKSUM_ATTR (attrs.at_friend);
6112 /* Checksum the child DIEs. */
6113 c = die->die_child;
6114 if (c) do {
6115 dw_attr_ref name_attr;
6117 c = c->die_sib;
6118 name_attr = get_AT (c, DW_AT_name);
6119 if (is_template_instantiation (c))
6121 /* Ignore instantiations of member type and function templates. */
6123 else if (name_attr != NULL
6124 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6126 /* Use a shallow checksum for named nested types and member
6127 functions. */
6128 CHECKSUM_ULEB128 ('S');
6129 CHECKSUM_ULEB128 (c->die_tag);
6130 CHECKSUM_STRING (AT_string (name_attr));
6132 else
6134 /* Use a deep checksum for other children. */
6135 /* Mark this DIE so it gets processed when unmarking. */
6136 if (c->die_mark == 0)
6137 c->die_mark = -1;
6138 die_checksum_ordered (c, ctx, mark);
6140 } while (c != die->die_child);
6142 CHECKSUM_ULEB128 (0);
6145 /* Add a type name and tag to a hash. */
6146 static void
6147 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6149 CHECKSUM_ULEB128 (tag);
6150 CHECKSUM_STRING (name);
6153 #undef CHECKSUM
6154 #undef CHECKSUM_STRING
6155 #undef CHECKSUM_ATTR
6156 #undef CHECKSUM_LEB128
6157 #undef CHECKSUM_ULEB128
6159 /* Generate the type signature for DIE. This is computed by generating an
6160 MD5 checksum over the DIE's tag, its relevant attributes, and its
6161 children. Attributes that are references to other DIEs are processed
6162 by recursion, using the MARK field to prevent infinite recursion.
6163 If the DIE is nested inside a namespace or another type, we also
6164 need to include that context in the signature. The lower 64 bits
6165 of the resulting MD5 checksum comprise the signature. */
6167 static void
6168 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6170 int mark;
6171 const char *name;
6172 unsigned char checksum[16];
6173 struct md5_ctx ctx;
6174 dw_die_ref decl;
6175 dw_die_ref parent;
6177 name = get_AT_string (die, DW_AT_name);
6178 decl = get_AT_ref (die, DW_AT_specification);
6179 parent = get_die_parent (die);
6181 /* First, compute a signature for just the type name (and its surrounding
6182 context, if any. This is stored in the type unit DIE for link-time
6183 ODR (one-definition rule) checking. */
6185 if (is_cxx () && name != NULL)
6187 md5_init_ctx (&ctx);
6189 /* Checksum the names of surrounding namespaces and structures. */
6190 if (parent != NULL)
6191 checksum_die_context (parent, &ctx);
6193 /* Checksum the current DIE. */
6194 die_odr_checksum (die->die_tag, name, &ctx);
6195 md5_finish_ctx (&ctx, checksum);
6197 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6200 /* Next, compute the complete type signature. */
6202 md5_init_ctx (&ctx);
6203 mark = 1;
6204 die->die_mark = mark;
6206 /* Checksum the names of surrounding namespaces and structures. */
6207 if (parent != NULL)
6208 checksum_die_context (parent, &ctx);
6210 /* Checksum the DIE and its children. */
6211 die_checksum_ordered (die, &ctx, &mark);
6212 unmark_all_dies (die);
6213 md5_finish_ctx (&ctx, checksum);
6215 /* Store the signature in the type node and link the type DIE and the
6216 type node together. */
6217 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6218 DWARF_TYPE_SIGNATURE_SIZE);
6219 die->comdat_type_p = true;
6220 die->die_id.die_type_node = type_node;
6221 type_node->type_die = die;
6223 /* If the DIE is a specification, link its declaration to the type node
6224 as well. */
6225 if (decl != NULL)
6227 decl->comdat_type_p = true;
6228 decl->die_id.die_type_node = type_node;
6232 /* Do the location expressions look same? */
6233 static inline int
6234 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6236 return loc1->dw_loc_opc == loc2->dw_loc_opc
6237 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6238 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6241 /* Do the values look the same? */
6242 static int
6243 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6245 dw_loc_descr_ref loc1, loc2;
6246 rtx r1, r2;
6248 if (v1->val_class != v2->val_class)
6249 return 0;
6251 switch (v1->val_class)
6253 case dw_val_class_const:
6254 return v1->v.val_int == v2->v.val_int;
6255 case dw_val_class_unsigned_const:
6256 return v1->v.val_unsigned == v2->v.val_unsigned;
6257 case dw_val_class_const_double:
6258 return v1->v.val_double.high == v2->v.val_double.high
6259 && v1->v.val_double.low == v2->v.val_double.low;
6260 case dw_val_class_vec:
6261 if (v1->v.val_vec.length != v2->v.val_vec.length
6262 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6263 return 0;
6264 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6265 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6266 return 0;
6267 return 1;
6268 case dw_val_class_flag:
6269 return v1->v.val_flag == v2->v.val_flag;
6270 case dw_val_class_str:
6271 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6273 case dw_val_class_addr:
6274 r1 = v1->v.val_addr;
6275 r2 = v2->v.val_addr;
6276 if (GET_CODE (r1) != GET_CODE (r2))
6277 return 0;
6278 return !rtx_equal_p (r1, r2);
6280 case dw_val_class_offset:
6281 return v1->v.val_offset == v2->v.val_offset;
6283 case dw_val_class_loc:
6284 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6285 loc1 && loc2;
6286 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6287 if (!same_loc_p (loc1, loc2, mark))
6288 return 0;
6289 return !loc1 && !loc2;
6291 case dw_val_class_die_ref:
6292 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6294 case dw_val_class_fde_ref:
6295 case dw_val_class_vms_delta:
6296 case dw_val_class_lbl_id:
6297 case dw_val_class_lineptr:
6298 case dw_val_class_macptr:
6299 case dw_val_class_high_pc:
6300 return 1;
6302 case dw_val_class_file:
6303 return v1->v.val_file == v2->v.val_file;
6305 case dw_val_class_data8:
6306 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6308 default:
6309 return 1;
6313 /* Do the attributes look the same? */
6315 static int
6316 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6318 if (at1->dw_attr != at2->dw_attr)
6319 return 0;
6321 /* We don't care that this was compiled with a different compiler
6322 snapshot; if the output is the same, that's what matters. */
6323 if (at1->dw_attr == DW_AT_producer)
6324 return 1;
6326 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6329 /* Do the dies look the same? */
6331 static int
6332 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6334 dw_die_ref c1, c2;
6335 dw_attr_ref a1;
6336 unsigned ix;
6338 /* To avoid infinite recursion. */
6339 if (die1->die_mark)
6340 return die1->die_mark == die2->die_mark;
6341 die1->die_mark = die2->die_mark = ++(*mark);
6343 if (die1->die_tag != die2->die_tag)
6344 return 0;
6346 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6347 return 0;
6349 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6350 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6351 return 0;
6353 c1 = die1->die_child;
6354 c2 = die2->die_child;
6355 if (! c1)
6357 if (c2)
6358 return 0;
6360 else
6361 for (;;)
6363 if (!same_die_p (c1, c2, mark))
6364 return 0;
6365 c1 = c1->die_sib;
6366 c2 = c2->die_sib;
6367 if (c1 == die1->die_child)
6369 if (c2 == die2->die_child)
6370 break;
6371 else
6372 return 0;
6376 return 1;
6379 /* Do the dies look the same? Wrapper around same_die_p. */
6381 static int
6382 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6384 int mark = 0;
6385 int ret = same_die_p (die1, die2, &mark);
6387 unmark_all_dies (die1);
6388 unmark_all_dies (die2);
6390 return ret;
6393 /* The prefix to attach to symbols on DIEs in the current comdat debug
6394 info section. */
6395 static const char *comdat_symbol_id;
6397 /* The index of the current symbol within the current comdat CU. */
6398 static unsigned int comdat_symbol_number;
6400 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6401 children, and set comdat_symbol_id accordingly. */
6403 static void
6404 compute_section_prefix (dw_die_ref unit_die)
6406 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6407 const char *base = die_name ? lbasename (die_name) : "anonymous";
6408 char *name = XALLOCAVEC (char, strlen (base) + 64);
6409 char *p;
6410 int i, mark;
6411 unsigned char checksum[16];
6412 struct md5_ctx ctx;
6414 /* Compute the checksum of the DIE, then append part of it as hex digits to
6415 the name filename of the unit. */
6417 md5_init_ctx (&ctx);
6418 mark = 0;
6419 die_checksum (unit_die, &ctx, &mark);
6420 unmark_all_dies (unit_die);
6421 md5_finish_ctx (&ctx, checksum);
6423 sprintf (name, "%s.", base);
6424 clean_symbol_name (name);
6426 p = name + strlen (name);
6427 for (i = 0; i < 4; i++)
6429 sprintf (p, "%.2x", checksum[i]);
6430 p += 2;
6433 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6434 comdat_symbol_number = 0;
6437 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6439 static int
6440 is_type_die (dw_die_ref die)
6442 switch (die->die_tag)
6444 case DW_TAG_array_type:
6445 case DW_TAG_class_type:
6446 case DW_TAG_interface_type:
6447 case DW_TAG_enumeration_type:
6448 case DW_TAG_pointer_type:
6449 case DW_TAG_reference_type:
6450 case DW_TAG_rvalue_reference_type:
6451 case DW_TAG_string_type:
6452 case DW_TAG_structure_type:
6453 case DW_TAG_subroutine_type:
6454 case DW_TAG_union_type:
6455 case DW_TAG_ptr_to_member_type:
6456 case DW_TAG_set_type:
6457 case DW_TAG_subrange_type:
6458 case DW_TAG_base_type:
6459 case DW_TAG_const_type:
6460 case DW_TAG_file_type:
6461 case DW_TAG_packed_type:
6462 case DW_TAG_volatile_type:
6463 case DW_TAG_typedef:
6464 return 1;
6465 default:
6466 return 0;
6470 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6471 Basically, we want to choose the bits that are likely to be shared between
6472 compilations (types) and leave out the bits that are specific to individual
6473 compilations (functions). */
6475 static int
6476 is_comdat_die (dw_die_ref c)
6478 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6479 we do for stabs. The advantage is a greater likelihood of sharing between
6480 objects that don't include headers in the same order (and therefore would
6481 put the base types in a different comdat). jason 8/28/00 */
6483 if (c->die_tag == DW_TAG_base_type)
6484 return 0;
6486 if (c->die_tag == DW_TAG_pointer_type
6487 || c->die_tag == DW_TAG_reference_type
6488 || c->die_tag == DW_TAG_rvalue_reference_type
6489 || c->die_tag == DW_TAG_const_type
6490 || c->die_tag == DW_TAG_volatile_type)
6492 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6494 return t ? is_comdat_die (t) : 0;
6497 return is_type_die (c);
6500 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6501 compilation unit. */
6503 static int
6504 is_symbol_die (dw_die_ref c)
6506 return (is_type_die (c)
6507 || is_declaration_die (c)
6508 || c->die_tag == DW_TAG_namespace
6509 || c->die_tag == DW_TAG_module);
6512 /* Returns true iff C is a compile-unit DIE. */
6514 static inline bool
6515 is_cu_die (dw_die_ref c)
6517 return c && c->die_tag == DW_TAG_compile_unit;
6520 /* Returns true iff C is a unit DIE of some sort. */
6522 static inline bool
6523 is_unit_die (dw_die_ref c)
6525 return c && (c->die_tag == DW_TAG_compile_unit
6526 || c->die_tag == DW_TAG_partial_unit
6527 || c->die_tag == DW_TAG_type_unit);
6530 /* Returns true iff C is a namespace DIE. */
6532 static inline bool
6533 is_namespace_die (dw_die_ref c)
6535 return c && c->die_tag == DW_TAG_namespace;
6538 /* Returns true iff C is a class or structure DIE. */
6540 static inline bool
6541 is_class_die (dw_die_ref c)
6543 return c && (c->die_tag == DW_TAG_class_type
6544 || c->die_tag == DW_TAG_structure_type);
6547 /* Return non-zero if this DIE is a template parameter. */
6549 static inline bool
6550 is_template_parameter (dw_die_ref die)
6552 switch (die->die_tag)
6554 case DW_TAG_template_type_param:
6555 case DW_TAG_template_value_param:
6556 case DW_TAG_GNU_template_template_param:
6557 case DW_TAG_GNU_template_parameter_pack:
6558 return true;
6559 default:
6560 return false;
6564 /* Return non-zero if this DIE represents a template instantiation. */
6566 static inline bool
6567 is_template_instantiation (dw_die_ref die)
6569 dw_die_ref c;
6571 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6572 return false;
6573 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6574 return false;
6577 static char *
6578 gen_internal_sym (const char *prefix)
6580 char buf[256];
6582 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6583 return xstrdup (buf);
6586 /* Assign symbols to all worthy DIEs under DIE. */
6588 static void
6589 assign_symbol_names (dw_die_ref die)
6591 dw_die_ref c;
6593 if (is_symbol_die (die) && !die->comdat_type_p)
6595 if (comdat_symbol_id)
6597 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6599 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6600 comdat_symbol_id, comdat_symbol_number++);
6601 die->die_id.die_symbol = xstrdup (p);
6603 else
6604 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6607 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6610 struct cu_hash_table_entry
6612 dw_die_ref cu;
6613 unsigned min_comdat_num, max_comdat_num;
6614 struct cu_hash_table_entry *next;
6617 /* Helpers to manipulate hash table of CUs. */
6619 struct cu_hash_table_entry_hasher
6621 typedef cu_hash_table_entry value_type;
6622 typedef die_struct compare_type;
6623 static inline hashval_t hash (const value_type *);
6624 static inline bool equal (const value_type *, const compare_type *);
6625 static inline void remove (value_type *);
6628 inline hashval_t
6629 cu_hash_table_entry_hasher::hash (const value_type *entry)
6631 return htab_hash_string (entry->cu->die_id.die_symbol);
6634 inline bool
6635 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6636 const compare_type *entry2)
6638 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6641 inline void
6642 cu_hash_table_entry_hasher::remove (value_type *entry)
6644 struct cu_hash_table_entry *next;
6646 while (entry)
6648 next = entry->next;
6649 free (entry);
6650 entry = next;
6654 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6656 /* Check whether we have already seen this CU and set up SYM_NUM
6657 accordingly. */
6658 static int
6659 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6661 struct cu_hash_table_entry dummy;
6662 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6664 dummy.max_comdat_num = 0;
6666 slot = htable.find_slot_with_hash (cu,
6667 htab_hash_string (cu->die_id.die_symbol),
6668 INSERT);
6669 entry = *slot;
6671 for (; entry; last = entry, entry = entry->next)
6673 if (same_die_p_wrap (cu, entry->cu))
6674 break;
6677 if (entry)
6679 *sym_num = entry->min_comdat_num;
6680 return 1;
6683 entry = XCNEW (struct cu_hash_table_entry);
6684 entry->cu = cu;
6685 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6686 entry->next = *slot;
6687 *slot = entry;
6689 return 0;
6692 /* Record SYM_NUM to record of CU in HTABLE. */
6693 static void
6694 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6695 unsigned int sym_num)
6697 struct cu_hash_table_entry **slot, *entry;
6699 slot = htable.find_slot_with_hash (cu,
6700 htab_hash_string (cu->die_id.die_symbol),
6701 NO_INSERT);
6702 entry = *slot;
6704 entry->max_comdat_num = sym_num;
6707 /* Traverse the DIE (which is always comp_unit_die), and set up
6708 additional compilation units for each of the include files we see
6709 bracketed by BINCL/EINCL. */
6711 static void
6712 break_out_includes (dw_die_ref die)
6714 dw_die_ref c;
6715 dw_die_ref unit = NULL;
6716 limbo_die_node *node, **pnode;
6717 cu_hash_type cu_hash_table;
6719 c = die->die_child;
6720 if (c) do {
6721 dw_die_ref prev = c;
6722 c = c->die_sib;
6723 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6724 || (unit && is_comdat_die (c)))
6726 dw_die_ref next = c->die_sib;
6728 /* This DIE is for a secondary CU; remove it from the main one. */
6729 remove_child_with_prev (c, prev);
6731 if (c->die_tag == DW_TAG_GNU_BINCL)
6732 unit = push_new_compile_unit (unit, c);
6733 else if (c->die_tag == DW_TAG_GNU_EINCL)
6734 unit = pop_compile_unit (unit);
6735 else
6736 add_child_die (unit, c);
6737 c = next;
6738 if (c == die->die_child)
6739 break;
6741 } while (c != die->die_child);
6743 #if 0
6744 /* We can only use this in debugging, since the frontend doesn't check
6745 to make sure that we leave every include file we enter. */
6746 gcc_assert (!unit);
6747 #endif
6749 assign_symbol_names (die);
6750 cu_hash_table.create (10);
6751 for (node = limbo_die_list, pnode = &limbo_die_list;
6752 node;
6753 node = node->next)
6755 int is_dupl;
6757 compute_section_prefix (node->die);
6758 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6759 &comdat_symbol_number);
6760 assign_symbol_names (node->die);
6761 if (is_dupl)
6762 *pnode = node->next;
6763 else
6765 pnode = &node->next;
6766 record_comdat_symbol_number (node->die, cu_hash_table,
6767 comdat_symbol_number);
6770 cu_hash_table.dispose ();
6773 /* Return non-zero if this DIE is a declaration. */
6775 static int
6776 is_declaration_die (dw_die_ref die)
6778 dw_attr_ref a;
6779 unsigned ix;
6781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6782 if (a->dw_attr == DW_AT_declaration)
6783 return 1;
6785 return 0;
6788 /* Return non-zero if this DIE is nested inside a subprogram. */
6790 static int
6791 is_nested_in_subprogram (dw_die_ref die)
6793 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6795 if (decl == NULL)
6796 decl = die;
6797 return local_scope_p (decl);
6800 /* Return non-zero if this DIE contains a defining declaration of a
6801 subprogram. */
6803 static int
6804 contains_subprogram_definition (dw_die_ref die)
6806 dw_die_ref c;
6808 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6809 return 1;
6810 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6811 return 0;
6814 /* Return non-zero if this is a type DIE that should be moved to a
6815 COMDAT .debug_types section. */
6817 static int
6818 should_move_die_to_comdat (dw_die_ref die)
6820 switch (die->die_tag)
6822 case DW_TAG_class_type:
6823 case DW_TAG_structure_type:
6824 case DW_TAG_enumeration_type:
6825 case DW_TAG_union_type:
6826 /* Don't move declarations, inlined instances, or types nested in a
6827 subprogram. */
6828 if (is_declaration_die (die)
6829 || get_AT (die, DW_AT_abstract_origin)
6830 || is_nested_in_subprogram (die))
6831 return 0;
6832 /* A type definition should never contain a subprogram definition. */
6833 gcc_assert (!contains_subprogram_definition (die));
6834 return 1;
6835 case DW_TAG_array_type:
6836 case DW_TAG_interface_type:
6837 case DW_TAG_pointer_type:
6838 case DW_TAG_reference_type:
6839 case DW_TAG_rvalue_reference_type:
6840 case DW_TAG_string_type:
6841 case DW_TAG_subroutine_type:
6842 case DW_TAG_ptr_to_member_type:
6843 case DW_TAG_set_type:
6844 case DW_TAG_subrange_type:
6845 case DW_TAG_base_type:
6846 case DW_TAG_const_type:
6847 case DW_TAG_file_type:
6848 case DW_TAG_packed_type:
6849 case DW_TAG_volatile_type:
6850 case DW_TAG_typedef:
6851 default:
6852 return 0;
6856 /* Make a clone of DIE. */
6858 static dw_die_ref
6859 clone_die (dw_die_ref die)
6861 dw_die_ref clone;
6862 dw_attr_ref a;
6863 unsigned ix;
6865 clone = ggc_alloc_cleared_die_node ();
6866 clone->die_tag = die->die_tag;
6868 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6869 add_dwarf_attr (clone, a);
6871 return clone;
6874 /* Make a clone of the tree rooted at DIE. */
6876 static dw_die_ref
6877 clone_tree (dw_die_ref die)
6879 dw_die_ref c;
6880 dw_die_ref clone = clone_die (die);
6882 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6884 return clone;
6887 /* Make a clone of DIE as a declaration. */
6889 static dw_die_ref
6890 clone_as_declaration (dw_die_ref die)
6892 dw_die_ref clone;
6893 dw_die_ref decl;
6894 dw_attr_ref a;
6895 unsigned ix;
6897 /* If the DIE is already a declaration, just clone it. */
6898 if (is_declaration_die (die))
6899 return clone_die (die);
6901 /* If the DIE is a specification, just clone its declaration DIE. */
6902 decl = get_AT_ref (die, DW_AT_specification);
6903 if (decl != NULL)
6905 clone = clone_die (decl);
6906 if (die->comdat_type_p)
6907 add_AT_die_ref (clone, DW_AT_signature, die);
6908 return clone;
6911 clone = ggc_alloc_cleared_die_node ();
6912 clone->die_tag = die->die_tag;
6914 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6916 /* We don't want to copy over all attributes.
6917 For example we don't want DW_AT_byte_size because otherwise we will no
6918 longer have a declaration and GDB will treat it as a definition. */
6920 switch (a->dw_attr)
6922 case DW_AT_artificial:
6923 case DW_AT_containing_type:
6924 case DW_AT_external:
6925 case DW_AT_name:
6926 case DW_AT_type:
6927 case DW_AT_virtuality:
6928 case DW_AT_linkage_name:
6929 case DW_AT_MIPS_linkage_name:
6930 add_dwarf_attr (clone, a);
6931 break;
6932 case DW_AT_byte_size:
6933 default:
6934 break;
6938 if (die->comdat_type_p)
6939 add_AT_die_ref (clone, DW_AT_signature, die);
6941 add_AT_flag (clone, DW_AT_declaration, 1);
6942 return clone;
6946 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6948 struct decl_table_entry
6950 dw_die_ref orig;
6951 dw_die_ref copy;
6954 /* Helpers to manipulate hash table of copied declarations. */
6956 /* Hashtable helpers. */
6958 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6960 typedef decl_table_entry value_type;
6961 typedef die_struct compare_type;
6962 static inline hashval_t hash (const value_type *);
6963 static inline bool equal (const value_type *, const compare_type *);
6966 inline hashval_t
6967 decl_table_entry_hasher::hash (const value_type *entry)
6969 return htab_hash_pointer (entry->orig);
6972 inline bool
6973 decl_table_entry_hasher::equal (const value_type *entry1,
6974 const compare_type *entry2)
6976 return entry1->orig == entry2;
6979 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6981 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6982 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6983 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6984 to check if the ancestor has already been copied into UNIT. */
6986 static dw_die_ref
6987 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6989 dw_die_ref parent = die->die_parent;
6990 dw_die_ref new_parent = unit;
6991 dw_die_ref copy;
6992 decl_table_entry **slot = NULL;
6993 struct decl_table_entry *entry = NULL;
6995 if (decl_table.is_created ())
6997 /* Check if the entry has already been copied to UNIT. */
6998 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
6999 INSERT);
7000 if (*slot != HTAB_EMPTY_ENTRY)
7002 entry = *slot;
7003 return entry->copy;
7006 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7007 entry = XCNEW (struct decl_table_entry);
7008 entry->orig = die;
7009 entry->copy = NULL;
7010 *slot = entry;
7013 if (parent != NULL)
7015 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7016 if (spec != NULL)
7017 parent = spec;
7018 if (!is_unit_die (parent))
7019 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7022 copy = clone_as_declaration (die);
7023 add_child_die (new_parent, copy);
7025 if (decl_table.is_created ())
7027 /* Record the pointer to the copy. */
7028 entry->copy = copy;
7031 return copy;
7033 /* Copy the declaration context to the new type unit DIE. This includes
7034 any surrounding namespace or type declarations. If the DIE has an
7035 AT_specification attribute, it also includes attributes and children
7036 attached to the specification, and returns a pointer to the original
7037 parent of the declaration DIE. Returns NULL otherwise. */
7039 static dw_die_ref
7040 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7042 dw_die_ref decl;
7043 dw_die_ref new_decl;
7044 dw_die_ref orig_parent = NULL;
7046 decl = get_AT_ref (die, DW_AT_specification);
7047 if (decl == NULL)
7048 decl = die;
7049 else
7051 unsigned ix;
7052 dw_die_ref c;
7053 dw_attr_ref a;
7055 /* The original DIE will be changed to a declaration, and must
7056 be moved to be a child of the original declaration DIE. */
7057 orig_parent = decl->die_parent;
7059 /* Copy the type node pointer from the new DIE to the original
7060 declaration DIE so we can forward references later. */
7061 decl->comdat_type_p = true;
7062 decl->die_id.die_type_node = die->die_id.die_type_node;
7064 remove_AT (die, DW_AT_specification);
7066 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7068 if (a->dw_attr != DW_AT_name
7069 && a->dw_attr != DW_AT_declaration
7070 && a->dw_attr != DW_AT_external)
7071 add_dwarf_attr (die, a);
7074 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7077 if (decl->die_parent != NULL
7078 && !is_unit_die (decl->die_parent))
7080 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7081 if (new_decl != NULL)
7083 remove_AT (new_decl, DW_AT_signature);
7084 add_AT_specification (die, new_decl);
7088 return orig_parent;
7091 /* Generate the skeleton ancestor tree for the given NODE, then clone
7092 the DIE and add the clone into the tree. */
7094 static void
7095 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7097 if (node->new_die != NULL)
7098 return;
7100 node->new_die = clone_as_declaration (node->old_die);
7102 if (node->parent != NULL)
7104 generate_skeleton_ancestor_tree (node->parent);
7105 add_child_die (node->parent->new_die, node->new_die);
7109 /* Generate a skeleton tree of DIEs containing any declarations that are
7110 found in the original tree. We traverse the tree looking for declaration
7111 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7113 static void
7114 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7116 skeleton_chain_node node;
7117 dw_die_ref c;
7118 dw_die_ref first;
7119 dw_die_ref prev = NULL;
7120 dw_die_ref next = NULL;
7122 node.parent = parent;
7124 first = c = parent->old_die->die_child;
7125 if (c)
7126 next = c->die_sib;
7127 if (c) do {
7128 if (prev == NULL || prev->die_sib == c)
7129 prev = c;
7130 c = next;
7131 next = (c == first ? NULL : c->die_sib);
7132 node.old_die = c;
7133 node.new_die = NULL;
7134 if (is_declaration_die (c))
7136 if (is_template_instantiation (c))
7138 /* Instantiated templates do not need to be cloned into the
7139 type unit. Just move the DIE and its children back to
7140 the skeleton tree (in the main CU). */
7141 remove_child_with_prev (c, prev);
7142 add_child_die (parent->new_die, c);
7143 c = prev;
7145 else
7147 /* Clone the existing DIE, move the original to the skeleton
7148 tree (which is in the main CU), and put the clone, with
7149 all the original's children, where the original came from
7150 (which is about to be moved to the type unit). */
7151 dw_die_ref clone = clone_die (c);
7152 move_all_children (c, clone);
7154 replace_child (c, clone, prev);
7155 generate_skeleton_ancestor_tree (parent);
7156 add_child_die (parent->new_die, c);
7157 node.new_die = c;
7158 c = clone;
7161 generate_skeleton_bottom_up (&node);
7162 } while (next != NULL);
7165 /* Wrapper function for generate_skeleton_bottom_up. */
7167 static dw_die_ref
7168 generate_skeleton (dw_die_ref die)
7170 skeleton_chain_node node;
7172 node.old_die = die;
7173 node.new_die = NULL;
7174 node.parent = NULL;
7176 /* If this type definition is nested inside another type,
7177 and is not an instantiation of a template, always leave
7178 at least a declaration in its place. */
7179 if (die->die_parent != NULL
7180 && is_type_die (die->die_parent)
7181 && !is_template_instantiation (die))
7182 node.new_die = clone_as_declaration (die);
7184 generate_skeleton_bottom_up (&node);
7185 return node.new_die;
7188 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7189 declaration. The original DIE is moved to a new compile unit so that
7190 existing references to it follow it to the new location. If any of the
7191 original DIE's descendants is a declaration, we need to replace the
7192 original DIE with a skeleton tree and move the declarations back into the
7193 skeleton tree. */
7195 static dw_die_ref
7196 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7197 dw_die_ref prev)
7199 dw_die_ref skeleton, orig_parent;
7201 /* Copy the declaration context to the type unit DIE. If the returned
7202 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7203 that DIE. */
7204 orig_parent = copy_declaration_context (unit, child);
7206 skeleton = generate_skeleton (child);
7207 if (skeleton == NULL)
7208 remove_child_with_prev (child, prev);
7209 else
7211 skeleton->comdat_type_p = true;
7212 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7214 /* If the original DIE was a specification, we need to put
7215 the skeleton under the parent DIE of the declaration.
7216 This leaves the original declaration in the tree, but
7217 it will be pruned later since there are no longer any
7218 references to it. */
7219 if (orig_parent != NULL)
7221 remove_child_with_prev (child, prev);
7222 add_child_die (orig_parent, skeleton);
7224 else
7225 replace_child (child, skeleton, prev);
7228 return skeleton;
7231 /* Traverse the DIE and set up additional .debug_types sections for each
7232 type worthy of being placed in a COMDAT section. */
7234 static void
7235 break_out_comdat_types (dw_die_ref die)
7237 dw_die_ref c;
7238 dw_die_ref first;
7239 dw_die_ref prev = NULL;
7240 dw_die_ref next = NULL;
7241 dw_die_ref unit = NULL;
7243 first = c = die->die_child;
7244 if (c)
7245 next = c->die_sib;
7246 if (c) do {
7247 if (prev == NULL || prev->die_sib == c)
7248 prev = c;
7249 c = next;
7250 next = (c == first ? NULL : c->die_sib);
7251 if (should_move_die_to_comdat (c))
7253 dw_die_ref replacement;
7254 comdat_type_node_ref type_node;
7256 /* Break out nested types into their own type units. */
7257 break_out_comdat_types (c);
7259 /* Create a new type unit DIE as the root for the new tree, and
7260 add it to the list of comdat types. */
7261 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7262 add_AT_unsigned (unit, DW_AT_language,
7263 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7264 type_node = ggc_alloc_cleared_comdat_type_node ();
7265 type_node->root_die = unit;
7266 type_node->next = comdat_type_list;
7267 comdat_type_list = type_node;
7269 /* Generate the type signature. */
7270 generate_type_signature (c, type_node);
7272 /* Copy the declaration context, attributes, and children of the
7273 declaration into the new type unit DIE, then remove this DIE
7274 from the main CU (or replace it with a skeleton if necessary). */
7275 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7276 type_node->skeleton_die = replacement;
7278 /* Add the DIE to the new compunit. */
7279 add_child_die (unit, c);
7281 if (replacement != NULL)
7282 c = replacement;
7284 else if (c->die_tag == DW_TAG_namespace
7285 || c->die_tag == DW_TAG_class_type
7286 || c->die_tag == DW_TAG_structure_type
7287 || c->die_tag == DW_TAG_union_type)
7289 /* Look for nested types that can be broken out. */
7290 break_out_comdat_types (c);
7292 } while (next != NULL);
7295 /* Like clone_tree, but additionally enter all the children into
7296 the hash table decl_table. */
7298 static dw_die_ref
7299 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7301 dw_die_ref c;
7302 dw_die_ref clone = clone_die (die);
7303 struct decl_table_entry *entry;
7304 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7305 htab_hash_pointer (die), INSERT);
7306 /* Assert that DIE isn't in the hash table yet. If it would be there
7307 before, the ancestors would be necessarily there as well, therefore
7308 clone_tree_hash wouldn't be called. */
7309 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7310 entry = XCNEW (struct decl_table_entry);
7311 entry->orig = die;
7312 entry->copy = clone;
7313 *slot = entry;
7315 FOR_EACH_CHILD (die, c,
7316 add_child_die (clone, clone_tree_hash (c, decl_table)));
7318 return clone;
7321 /* Walk the DIE and its children, looking for references to incomplete
7322 or trivial types that are unmarked (i.e., that are not in the current
7323 type_unit). */
7325 static void
7326 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7328 dw_die_ref c;
7329 dw_attr_ref a;
7330 unsigned ix;
7332 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7334 if (AT_class (a) == dw_val_class_die_ref)
7336 dw_die_ref targ = AT_ref (a);
7337 decl_table_entry **slot;
7338 struct decl_table_entry *entry;
7340 if (targ->die_mark != 0 || targ->comdat_type_p)
7341 continue;
7343 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7344 INSERT);
7346 if (*slot != HTAB_EMPTY_ENTRY)
7348 /* TARG has already been copied, so we just need to
7349 modify the reference to point to the copy. */
7350 entry = *slot;
7351 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7353 else
7355 dw_die_ref parent = unit;
7356 dw_die_ref copy = clone_die (targ);
7358 /* Record in DECL_TABLE that TARG has been copied.
7359 Need to do this now, before the recursive call,
7360 because DECL_TABLE may be expanded and SLOT
7361 would no longer be a valid pointer. */
7362 entry = XCNEW (struct decl_table_entry);
7363 entry->orig = targ;
7364 entry->copy = copy;
7365 *slot = entry;
7367 FOR_EACH_CHILD (targ, c,
7368 add_child_die (copy,
7369 clone_tree_hash (c, decl_table)));
7371 /* Make sure the cloned tree is marked as part of the
7372 type unit. */
7373 mark_dies (copy);
7375 /* If TARG has surrounding context, copy its ancestor tree
7376 into the new type unit. */
7377 if (targ->die_parent != NULL
7378 && !is_unit_die (targ->die_parent))
7379 parent = copy_ancestor_tree (unit, targ->die_parent,
7380 decl_table);
7382 add_child_die (parent, copy);
7383 a->dw_attr_val.v.val_die_ref.die = copy;
7385 /* Make sure the newly-copied DIE is walked. If it was
7386 installed in a previously-added context, it won't
7387 get visited otherwise. */
7388 if (parent != unit)
7390 /* Find the highest point of the newly-added tree,
7391 mark each node along the way, and walk from there. */
7392 parent->die_mark = 1;
7393 while (parent->die_parent
7394 && parent->die_parent->die_mark == 0)
7396 parent = parent->die_parent;
7397 parent->die_mark = 1;
7399 copy_decls_walk (unit, parent, decl_table);
7405 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7408 /* Copy declarations for "unworthy" types into the new comdat section.
7409 Incomplete types, modified types, and certain other types aren't broken
7410 out into comdat sections of their own, so they don't have a signature,
7411 and we need to copy the declaration into the same section so that we
7412 don't have an external reference. */
7414 static void
7415 copy_decls_for_unworthy_types (dw_die_ref unit)
7417 decl_hash_type decl_table;
7419 mark_dies (unit);
7420 decl_table.create (10);
7421 copy_decls_walk (unit, unit, decl_table);
7422 decl_table.dispose ();
7423 unmark_dies (unit);
7426 /* Traverse the DIE and add a sibling attribute if it may have the
7427 effect of speeding up access to siblings. To save some space,
7428 avoid generating sibling attributes for DIE's without children. */
7430 static void
7431 add_sibling_attributes (dw_die_ref die)
7433 dw_die_ref c;
7435 if (! die->die_child)
7436 return;
7438 if (die->die_parent && die != die->die_parent->die_child)
7439 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7441 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7444 /* Output all location lists for the DIE and its children. */
7446 static void
7447 output_location_lists (dw_die_ref die)
7449 dw_die_ref c;
7450 dw_attr_ref a;
7451 unsigned ix;
7453 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7454 if (AT_class (a) == dw_val_class_loc_list)
7455 output_loc_list (AT_loc_list (a));
7457 FOR_EACH_CHILD (die, c, output_location_lists (c));
7460 /* We want to limit the number of external references, because they are
7461 larger than local references: a relocation takes multiple words, and
7462 even a sig8 reference is always eight bytes, whereas a local reference
7463 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7464 So if we encounter multiple external references to the same type DIE, we
7465 make a local typedef stub for it and redirect all references there.
7467 This is the element of the hash table for keeping track of these
7468 references. */
7470 struct external_ref
7472 dw_die_ref type;
7473 dw_die_ref stub;
7474 unsigned n_refs;
7477 /* Hashtable helpers. */
7479 struct external_ref_hasher : typed_free_remove <external_ref>
7481 typedef external_ref value_type;
7482 typedef external_ref compare_type;
7483 static inline hashval_t hash (const value_type *);
7484 static inline bool equal (const value_type *, const compare_type *);
7487 inline hashval_t
7488 external_ref_hasher::hash (const value_type *r)
7490 dw_die_ref die = r->type;
7491 hashval_t h = 0;
7493 /* We can't use the address of the DIE for hashing, because
7494 that will make the order of the stub DIEs non-deterministic. */
7495 if (! die->comdat_type_p)
7496 /* We have a symbol; use it to compute a hash. */
7497 h = htab_hash_string (die->die_id.die_symbol);
7498 else
7500 /* We have a type signature; use a subset of the bits as the hash.
7501 The 8-byte signature is at least as large as hashval_t. */
7502 comdat_type_node_ref type_node = die->die_id.die_type_node;
7503 memcpy (&h, type_node->signature, sizeof (h));
7505 return h;
7508 inline bool
7509 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7511 return r1->type == r2->type;
7514 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7516 /* Return a pointer to the external_ref for references to DIE. */
7518 static struct external_ref *
7519 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7521 struct external_ref ref, *ref_p;
7522 external_ref **slot;
7524 ref.type = die;
7525 slot = map.find_slot (&ref, INSERT);
7526 if (*slot != HTAB_EMPTY_ENTRY)
7527 return *slot;
7529 ref_p = XCNEW (struct external_ref);
7530 ref_p->type = die;
7531 *slot = ref_p;
7532 return ref_p;
7535 /* Subroutine of optimize_external_refs, below.
7537 If we see a type skeleton, record it as our stub. If we see external
7538 references, remember how many we've seen. */
7540 static void
7541 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7543 dw_die_ref c;
7544 dw_attr_ref a;
7545 unsigned ix;
7546 struct external_ref *ref_p;
7548 if (is_type_die (die)
7549 && (c = get_AT_ref (die, DW_AT_signature)))
7551 /* This is a local skeleton; use it for local references. */
7552 ref_p = lookup_external_ref (map, c);
7553 ref_p->stub = die;
7556 /* Scan the DIE references, and remember any that refer to DIEs from
7557 other CUs (i.e. those which are not marked). */
7558 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7559 if (AT_class (a) == dw_val_class_die_ref
7560 && (c = AT_ref (a))->die_mark == 0
7561 && is_type_die (c))
7563 ref_p = lookup_external_ref (map, c);
7564 ref_p->n_refs++;
7567 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7570 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7571 points to an external_ref, DATA is the CU we're processing. If we don't
7572 already have a local stub, and we have multiple refs, build a stub. */
7575 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7577 struct external_ref *ref_p = *slot;
7579 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7581 /* We have multiple references to this type, so build a small stub.
7582 Both of these forms are a bit dodgy from the perspective of the
7583 DWARF standard, since technically they should have names. */
7584 dw_die_ref cu = data;
7585 dw_die_ref type = ref_p->type;
7586 dw_die_ref stub = NULL;
7588 if (type->comdat_type_p)
7590 /* If we refer to this type via sig8, use AT_signature. */
7591 stub = new_die (type->die_tag, cu, NULL_TREE);
7592 add_AT_die_ref (stub, DW_AT_signature, type);
7594 else
7596 /* Otherwise, use a typedef with no name. */
7597 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7598 add_AT_die_ref (stub, DW_AT_type, type);
7601 stub->die_mark++;
7602 ref_p->stub = stub;
7604 return 1;
7607 /* DIE is a unit; look through all the DIE references to see if there are
7608 any external references to types, and if so, create local stubs for
7609 them which will be applied in build_abbrev_table. This is useful because
7610 references to local DIEs are smaller. */
7612 static external_ref_hash_type
7613 optimize_external_refs (dw_die_ref die)
7615 external_ref_hash_type map;
7616 map.create (10);
7617 optimize_external_refs_1 (die, map);
7618 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7619 return map;
7622 /* The format of each DIE (and its attribute value pairs) is encoded in an
7623 abbreviation table. This routine builds the abbreviation table and assigns
7624 a unique abbreviation id for each abbreviation entry. The children of each
7625 die are visited recursively. */
7627 static void
7628 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7630 unsigned long abbrev_id;
7631 unsigned int n_alloc;
7632 dw_die_ref c;
7633 dw_attr_ref a;
7634 unsigned ix;
7636 /* Scan the DIE references, and replace any that refer to
7637 DIEs from other CUs (i.e. those which are not marked) with
7638 the local stubs we built in optimize_external_refs. */
7639 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7640 if (AT_class (a) == dw_val_class_die_ref
7641 && (c = AT_ref (a))->die_mark == 0)
7643 struct external_ref *ref_p;
7644 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7646 ref_p = lookup_external_ref (extern_map, c);
7647 if (ref_p->stub && ref_p->stub != die)
7648 change_AT_die_ref (a, ref_p->stub);
7649 else
7650 /* We aren't changing this reference, so mark it external. */
7651 set_AT_ref_external (a, 1);
7654 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7656 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7657 dw_attr_ref die_a, abbrev_a;
7658 unsigned ix;
7659 bool ok = true;
7661 if (abbrev->die_tag != die->die_tag)
7662 continue;
7663 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7664 continue;
7666 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7667 continue;
7669 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7671 abbrev_a = &(*abbrev->die_attr)[ix];
7672 if ((abbrev_a->dw_attr != die_a->dw_attr)
7673 || (value_format (abbrev_a) != value_format (die_a)))
7675 ok = false;
7676 break;
7679 if (ok)
7680 break;
7683 if (abbrev_id >= abbrev_die_table_in_use)
7685 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7687 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7688 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7689 n_alloc);
7691 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7692 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7693 abbrev_die_table_allocated = n_alloc;
7696 ++abbrev_die_table_in_use;
7697 abbrev_die_table[abbrev_id] = die;
7700 die->die_abbrev = abbrev_id;
7701 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7704 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7706 static int
7707 constant_size (unsigned HOST_WIDE_INT value)
7709 int log;
7711 if (value == 0)
7712 log = 0;
7713 else
7714 log = floor_log2 (value);
7716 log = log / 8;
7717 log = 1 << (floor_log2 (log) + 1);
7719 return log;
7722 /* Return the size of a DIE as it is represented in the
7723 .debug_info section. */
7725 static unsigned long
7726 size_of_die (dw_die_ref die)
7728 unsigned long size = 0;
7729 dw_attr_ref a;
7730 unsigned ix;
7731 enum dwarf_form form;
7733 size += size_of_uleb128 (die->die_abbrev);
7734 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7736 switch (AT_class (a))
7738 case dw_val_class_addr:
7739 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7741 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7742 size += size_of_uleb128 (AT_index (a));
7744 else
7745 size += DWARF2_ADDR_SIZE;
7746 break;
7747 case dw_val_class_offset:
7748 size += DWARF_OFFSET_SIZE;
7749 break;
7750 case dw_val_class_loc:
7752 unsigned long lsize = size_of_locs (AT_loc (a));
7754 /* Block length. */
7755 if (dwarf_version >= 4)
7756 size += size_of_uleb128 (lsize);
7757 else
7758 size += constant_size (lsize);
7759 size += lsize;
7761 break;
7762 case dw_val_class_loc_list:
7763 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7765 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7766 size += size_of_uleb128 (AT_index (a));
7768 else
7769 size += DWARF_OFFSET_SIZE;
7770 break;
7771 case dw_val_class_range_list:
7772 size += DWARF_OFFSET_SIZE;
7773 break;
7774 case dw_val_class_const:
7775 size += size_of_sleb128 (AT_int (a));
7776 break;
7777 case dw_val_class_unsigned_const:
7779 int csize = constant_size (AT_unsigned (a));
7780 if (dwarf_version == 3
7781 && a->dw_attr == DW_AT_data_member_location
7782 && csize >= 4)
7783 size += size_of_uleb128 (AT_unsigned (a));
7784 else
7785 size += csize;
7787 break;
7788 case dw_val_class_const_double:
7789 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7790 if (HOST_BITS_PER_WIDE_INT >= 64)
7791 size++; /* block */
7792 break;
7793 case dw_val_class_vec:
7794 size += constant_size (a->dw_attr_val.v.val_vec.length
7795 * a->dw_attr_val.v.val_vec.elt_size)
7796 + a->dw_attr_val.v.val_vec.length
7797 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7798 break;
7799 case dw_val_class_flag:
7800 if (dwarf_version >= 4)
7801 /* Currently all add_AT_flag calls pass in 1 as last argument,
7802 so DW_FORM_flag_present can be used. If that ever changes,
7803 we'll need to use DW_FORM_flag and have some optimization
7804 in build_abbrev_table that will change those to
7805 DW_FORM_flag_present if it is set to 1 in all DIEs using
7806 the same abbrev entry. */
7807 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7808 else
7809 size += 1;
7810 break;
7811 case dw_val_class_die_ref:
7812 if (AT_ref_external (a))
7814 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7815 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7816 is sized by target address length, whereas in DWARF3
7817 it's always sized as an offset. */
7818 if (use_debug_types)
7819 size += DWARF_TYPE_SIGNATURE_SIZE;
7820 else if (dwarf_version == 2)
7821 size += DWARF2_ADDR_SIZE;
7822 else
7823 size += DWARF_OFFSET_SIZE;
7825 else
7826 size += DWARF_OFFSET_SIZE;
7827 break;
7828 case dw_val_class_fde_ref:
7829 size += DWARF_OFFSET_SIZE;
7830 break;
7831 case dw_val_class_lbl_id:
7832 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7834 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7835 size += size_of_uleb128 (AT_index (a));
7837 else
7838 size += DWARF2_ADDR_SIZE;
7839 break;
7840 case dw_val_class_lineptr:
7841 case dw_val_class_macptr:
7842 size += DWARF_OFFSET_SIZE;
7843 break;
7844 case dw_val_class_str:
7845 form = AT_string_form (a);
7846 if (form == DW_FORM_strp)
7847 size += DWARF_OFFSET_SIZE;
7848 else if (form == DW_FORM_GNU_str_index)
7849 size += size_of_uleb128 (AT_index (a));
7850 else
7851 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7852 break;
7853 case dw_val_class_file:
7854 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7855 break;
7856 case dw_val_class_data8:
7857 size += 8;
7858 break;
7859 case dw_val_class_vms_delta:
7860 size += DWARF_OFFSET_SIZE;
7861 break;
7862 case dw_val_class_high_pc:
7863 size += DWARF2_ADDR_SIZE;
7864 break;
7865 default:
7866 gcc_unreachable ();
7870 return size;
7873 /* Size the debugging information associated with a given DIE. Visits the
7874 DIE's children recursively. Updates the global variable next_die_offset, on
7875 each time through. Uses the current value of next_die_offset to update the
7876 die_offset field in each DIE. */
7878 static void
7879 calc_die_sizes (dw_die_ref die)
7881 dw_die_ref c;
7883 gcc_assert (die->die_offset == 0
7884 || (unsigned long int) die->die_offset == next_die_offset);
7885 die->die_offset = next_die_offset;
7886 next_die_offset += size_of_die (die);
7888 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7890 if (die->die_child != NULL)
7891 /* Count the null byte used to terminate sibling lists. */
7892 next_die_offset += 1;
7895 /* Size just the base type children at the start of the CU.
7896 This is needed because build_abbrev needs to size locs
7897 and sizing of type based stack ops needs to know die_offset
7898 values for the base types. */
7900 static void
7901 calc_base_type_die_sizes (void)
7903 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7904 unsigned int i;
7905 dw_die_ref base_type;
7906 #if ENABLE_ASSERT_CHECKING
7907 dw_die_ref prev = comp_unit_die ()->die_child;
7908 #endif
7910 die_offset += size_of_die (comp_unit_die ());
7911 for (i = 0; base_types.iterate (i, &base_type); i++)
7913 #if ENABLE_ASSERT_CHECKING
7914 gcc_assert (base_type->die_offset == 0
7915 && prev->die_sib == base_type
7916 && base_type->die_child == NULL
7917 && base_type->die_abbrev);
7918 prev = base_type;
7919 #endif
7920 base_type->die_offset = die_offset;
7921 die_offset += size_of_die (base_type);
7925 /* Set the marks for a die and its children. We do this so
7926 that we know whether or not a reference needs to use FORM_ref_addr; only
7927 DIEs in the same CU will be marked. We used to clear out the offset
7928 and use that as the flag, but ran into ordering problems. */
7930 static void
7931 mark_dies (dw_die_ref die)
7933 dw_die_ref c;
7935 gcc_assert (!die->die_mark);
7937 die->die_mark = 1;
7938 FOR_EACH_CHILD (die, c, mark_dies (c));
7941 /* Clear the marks for a die and its children. */
7943 static void
7944 unmark_dies (dw_die_ref die)
7946 dw_die_ref c;
7948 if (! use_debug_types)
7949 gcc_assert (die->die_mark);
7951 die->die_mark = 0;
7952 FOR_EACH_CHILD (die, c, unmark_dies (c));
7955 /* Clear the marks for a die, its children and referred dies. */
7957 static void
7958 unmark_all_dies (dw_die_ref die)
7960 dw_die_ref c;
7961 dw_attr_ref a;
7962 unsigned ix;
7964 if (!die->die_mark)
7965 return;
7966 die->die_mark = 0;
7968 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7970 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7971 if (AT_class (a) == dw_val_class_die_ref)
7972 unmark_all_dies (AT_ref (a));
7975 /* Calculate if the entry should appear in the final output file. It may be
7976 from a pruned a type. */
7978 static bool
7979 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7981 /* By limiting gnu pubnames to definitions only, gold can generate a
7982 gdb index without entries for declarations, which don't include
7983 enough information to be useful. */
7984 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7985 return false;
7987 if (table == pubname_table)
7989 /* Enumerator names are part of the pubname table, but the
7990 parent DW_TAG_enumeration_type die may have been pruned.
7991 Don't output them if that is the case. */
7992 if (p->die->die_tag == DW_TAG_enumerator &&
7993 (p->die->die_parent == NULL
7994 || !p->die->die_parent->die_perennial_p))
7995 return false;
7997 /* Everything else in the pubname table is included. */
7998 return true;
8001 /* The pubtypes table shouldn't include types that have been
8002 pruned. */
8003 return (p->die->die_offset != 0
8004 || !flag_eliminate_unused_debug_types);
8007 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8008 generated for the compilation unit. */
8010 static unsigned long
8011 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8013 unsigned long size;
8014 unsigned i;
8015 pubname_ref p;
8016 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8018 size = DWARF_PUBNAMES_HEADER_SIZE;
8019 FOR_EACH_VEC_ELT (*names, i, p)
8020 if (include_pubname_in_output (names, p))
8021 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8023 size += DWARF_OFFSET_SIZE;
8024 return size;
8027 /* Return the size of the information in the .debug_aranges section. */
8029 static unsigned long
8030 size_of_aranges (void)
8032 unsigned long size;
8034 size = DWARF_ARANGES_HEADER_SIZE;
8036 /* Count the address/length pair for this compilation unit. */
8037 if (text_section_used)
8038 size += 2 * DWARF2_ADDR_SIZE;
8039 if (cold_text_section_used)
8040 size += 2 * DWARF2_ADDR_SIZE;
8041 if (have_multiple_function_sections)
8043 unsigned fde_idx;
8044 dw_fde_ref fde;
8046 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8048 if (DECL_IGNORED_P (fde->decl))
8049 continue;
8050 if (!fde->in_std_section)
8051 size += 2 * DWARF2_ADDR_SIZE;
8052 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8053 size += 2 * DWARF2_ADDR_SIZE;
8057 /* Count the two zero words used to terminated the address range table. */
8058 size += 2 * DWARF2_ADDR_SIZE;
8059 return size;
8062 /* Select the encoding of an attribute value. */
8064 static enum dwarf_form
8065 value_format (dw_attr_ref a)
8067 switch (AT_class (a))
8069 case dw_val_class_addr:
8070 /* Only very few attributes allow DW_FORM_addr. */
8071 switch (a->dw_attr)
8073 case DW_AT_low_pc:
8074 case DW_AT_high_pc:
8075 case DW_AT_entry_pc:
8076 case DW_AT_trampoline:
8077 return (AT_index (a) == NOT_INDEXED
8078 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8079 default:
8080 break;
8082 switch (DWARF2_ADDR_SIZE)
8084 case 1:
8085 return DW_FORM_data1;
8086 case 2:
8087 return DW_FORM_data2;
8088 case 4:
8089 return DW_FORM_data4;
8090 case 8:
8091 return DW_FORM_data8;
8092 default:
8093 gcc_unreachable ();
8095 case dw_val_class_range_list:
8096 case dw_val_class_loc_list:
8097 if (dwarf_version >= 4)
8098 return DW_FORM_sec_offset;
8099 /* FALLTHRU */
8100 case dw_val_class_vms_delta:
8101 case dw_val_class_offset:
8102 switch (DWARF_OFFSET_SIZE)
8104 case 4:
8105 return DW_FORM_data4;
8106 case 8:
8107 return DW_FORM_data8;
8108 default:
8109 gcc_unreachable ();
8111 case dw_val_class_loc:
8112 if (dwarf_version >= 4)
8113 return DW_FORM_exprloc;
8114 switch (constant_size (size_of_locs (AT_loc (a))))
8116 case 1:
8117 return DW_FORM_block1;
8118 case 2:
8119 return DW_FORM_block2;
8120 case 4:
8121 return DW_FORM_block4;
8122 default:
8123 gcc_unreachable ();
8125 case dw_val_class_const:
8126 return DW_FORM_sdata;
8127 case dw_val_class_unsigned_const:
8128 switch (constant_size (AT_unsigned (a)))
8130 case 1:
8131 return DW_FORM_data1;
8132 case 2:
8133 return DW_FORM_data2;
8134 case 4:
8135 /* In DWARF3 DW_AT_data_member_location with
8136 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8137 constant, so we need to use DW_FORM_udata if we need
8138 a large constant. */
8139 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8140 return DW_FORM_udata;
8141 return DW_FORM_data4;
8142 case 8:
8143 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8144 return DW_FORM_udata;
8145 return DW_FORM_data8;
8146 default:
8147 gcc_unreachable ();
8149 case dw_val_class_const_double:
8150 switch (HOST_BITS_PER_WIDE_INT)
8152 case 8:
8153 return DW_FORM_data2;
8154 case 16:
8155 return DW_FORM_data4;
8156 case 32:
8157 return DW_FORM_data8;
8158 case 64:
8159 default:
8160 return DW_FORM_block1;
8162 case dw_val_class_vec:
8163 switch (constant_size (a->dw_attr_val.v.val_vec.length
8164 * a->dw_attr_val.v.val_vec.elt_size))
8166 case 1:
8167 return DW_FORM_block1;
8168 case 2:
8169 return DW_FORM_block2;
8170 case 4:
8171 return DW_FORM_block4;
8172 default:
8173 gcc_unreachable ();
8175 case dw_val_class_flag:
8176 if (dwarf_version >= 4)
8178 /* Currently all add_AT_flag calls pass in 1 as last argument,
8179 so DW_FORM_flag_present can be used. If that ever changes,
8180 we'll need to use DW_FORM_flag and have some optimization
8181 in build_abbrev_table that will change those to
8182 DW_FORM_flag_present if it is set to 1 in all DIEs using
8183 the same abbrev entry. */
8184 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8185 return DW_FORM_flag_present;
8187 return DW_FORM_flag;
8188 case dw_val_class_die_ref:
8189 if (AT_ref_external (a))
8190 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8191 else
8192 return DW_FORM_ref;
8193 case dw_val_class_fde_ref:
8194 return DW_FORM_data;
8195 case dw_val_class_lbl_id:
8196 return (AT_index (a) == NOT_INDEXED
8197 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8198 case dw_val_class_lineptr:
8199 case dw_val_class_macptr:
8200 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8201 case dw_val_class_str:
8202 return AT_string_form (a);
8203 case dw_val_class_file:
8204 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8206 case 1:
8207 return DW_FORM_data1;
8208 case 2:
8209 return DW_FORM_data2;
8210 case 4:
8211 return DW_FORM_data4;
8212 default:
8213 gcc_unreachable ();
8216 case dw_val_class_data8:
8217 return DW_FORM_data8;
8219 case dw_val_class_high_pc:
8220 switch (DWARF2_ADDR_SIZE)
8222 case 1:
8223 return DW_FORM_data1;
8224 case 2:
8225 return DW_FORM_data2;
8226 case 4:
8227 return DW_FORM_data4;
8228 case 8:
8229 return DW_FORM_data8;
8230 default:
8231 gcc_unreachable ();
8234 default:
8235 gcc_unreachable ();
8239 /* Output the encoding of an attribute value. */
8241 static void
8242 output_value_format (dw_attr_ref a)
8244 enum dwarf_form form = value_format (a);
8246 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8249 /* Given a die and id, produce the appropriate abbreviations. */
8251 static void
8252 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8254 unsigned ix;
8255 dw_attr_ref a_attr;
8257 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8258 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8259 dwarf_tag_name (abbrev->die_tag));
8261 if (abbrev->die_child != NULL)
8262 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8263 else
8264 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8266 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8268 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8269 dwarf_attr_name (a_attr->dw_attr));
8270 output_value_format (a_attr);
8273 dw2_asm_output_data (1, 0, NULL);
8274 dw2_asm_output_data (1, 0, NULL);
8278 /* Output the .debug_abbrev section which defines the DIE abbreviation
8279 table. */
8281 static void
8282 output_abbrev_section (void)
8284 unsigned long abbrev_id;
8286 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8287 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8289 /* Terminate the table. */
8290 dw2_asm_output_data (1, 0, NULL);
8293 /* Output a symbol we can use to refer to this DIE from another CU. */
8295 static inline void
8296 output_die_symbol (dw_die_ref die)
8298 const char *sym = die->die_id.die_symbol;
8300 gcc_assert (!die->comdat_type_p);
8302 if (sym == 0)
8303 return;
8305 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8306 /* We make these global, not weak; if the target doesn't support
8307 .linkonce, it doesn't support combining the sections, so debugging
8308 will break. */
8309 targetm.asm_out.globalize_label (asm_out_file, sym);
8311 ASM_OUTPUT_LABEL (asm_out_file, sym);
8314 /* Return a new location list, given the begin and end range, and the
8315 expression. */
8317 static inline dw_loc_list_ref
8318 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8319 const char *section)
8321 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8323 retlist->begin = begin;
8324 retlist->begin_entry = NULL;
8325 retlist->end = end;
8326 retlist->expr = expr;
8327 retlist->section = section;
8329 return retlist;
8332 /* Generate a new internal symbol for this location list node, if it
8333 hasn't got one yet. */
8335 static inline void
8336 gen_llsym (dw_loc_list_ref list)
8338 gcc_assert (!list->ll_symbol);
8339 list->ll_symbol = gen_internal_sym ("LLST");
8342 /* Output the location list given to us. */
8344 static void
8345 output_loc_list (dw_loc_list_ref list_head)
8347 dw_loc_list_ref curr = list_head;
8349 if (list_head->emitted)
8350 return;
8351 list_head->emitted = true;
8353 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8355 /* Walk the location list, and output each range + expression. */
8356 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8358 unsigned long size;
8359 /* Don't output an entry that starts and ends at the same address. */
8360 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8361 continue;
8362 size = size_of_locs (curr->expr);
8363 /* If the expression is too large, drop it on the floor. We could
8364 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8365 in the expression, but >= 64KB expressions for a single value
8366 in a single range are unlikely very useful. */
8367 if (size > 0xffff)
8368 continue;
8369 if (dwarf_split_debug_info)
8371 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8372 "Location list start/length entry (%s)",
8373 list_head->ll_symbol);
8374 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8375 "Location list range start index (%s)",
8376 curr->begin);
8377 /* The length field is 4 bytes. If we ever need to support
8378 an 8-byte length, we can add a new DW_LLE code or fall back
8379 to DW_LLE_GNU_start_end_entry. */
8380 dw2_asm_output_delta (4, curr->end, curr->begin,
8381 "Location list range length (%s)",
8382 list_head->ll_symbol);
8384 else if (!have_multiple_function_sections)
8386 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8387 "Location list begin address (%s)",
8388 list_head->ll_symbol);
8389 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8390 "Location list end address (%s)",
8391 list_head->ll_symbol);
8393 else
8395 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8396 "Location list begin address (%s)",
8397 list_head->ll_symbol);
8398 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8399 "Location list end address (%s)",
8400 list_head->ll_symbol);
8403 /* Output the block length for this list of location operations. */
8404 gcc_assert (size <= 0xffff);
8405 dw2_asm_output_data (2, size, "%s", "Location expression size");
8407 output_loc_sequence (curr->expr, -1);
8410 if (dwarf_split_debug_info)
8411 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8412 "Location list terminator (%s)",
8413 list_head->ll_symbol);
8414 else
8416 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8417 "Location list terminator begin (%s)",
8418 list_head->ll_symbol);
8419 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8420 "Location list terminator end (%s)",
8421 list_head->ll_symbol);
8425 /* Output a range_list offset into the debug_range section. Emit a
8426 relocated reference if val_entry is NULL, otherwise, emit an
8427 indirect reference. */
8429 static void
8430 output_range_list_offset (dw_attr_ref a)
8432 const char *name = dwarf_attr_name (a->dw_attr);
8434 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8436 char *p = strchr (ranges_section_label, '\0');
8437 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8438 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8439 debug_ranges_section, "%s", name);
8440 *p = '\0';
8442 else
8443 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8444 "%s (offset from %s)", name, ranges_section_label);
8447 /* Output the offset into the debug_loc section. */
8449 static void
8450 output_loc_list_offset (dw_attr_ref a)
8452 char *sym = AT_loc_list (a)->ll_symbol;
8454 gcc_assert (sym);
8455 if (dwarf_split_debug_info)
8456 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8457 "%s", dwarf_attr_name (a->dw_attr));
8458 else
8459 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8460 "%s", dwarf_attr_name (a->dw_attr));
8463 /* Output an attribute's index or value appropriately. */
8465 static void
8466 output_attr_index_or_value (dw_attr_ref a)
8468 const char *name = dwarf_attr_name (a->dw_attr);
8470 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8472 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8473 return;
8475 switch (AT_class (a))
8477 case dw_val_class_addr:
8478 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8479 break;
8480 case dw_val_class_high_pc:
8481 case dw_val_class_lbl_id:
8482 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8483 break;
8484 case dw_val_class_loc_list:
8485 output_loc_list_offset (a);
8486 break;
8487 default:
8488 gcc_unreachable ();
8492 /* Output a type signature. */
8494 static inline void
8495 output_signature (const char *sig, const char *name)
8497 int i;
8499 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8500 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8503 /* Output the DIE and its attributes. Called recursively to generate
8504 the definitions of each child DIE. */
8506 static void
8507 output_die (dw_die_ref die)
8509 dw_attr_ref a;
8510 dw_die_ref c;
8511 unsigned long size;
8512 unsigned ix;
8514 /* If someone in another CU might refer to us, set up a symbol for
8515 them to point to. */
8516 if (! die->comdat_type_p && die->die_id.die_symbol)
8517 output_die_symbol (die);
8519 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8520 (unsigned long)die->die_offset,
8521 dwarf_tag_name (die->die_tag));
8523 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8525 const char *name = dwarf_attr_name (a->dw_attr);
8527 switch (AT_class (a))
8529 case dw_val_class_addr:
8530 output_attr_index_or_value (a);
8531 break;
8533 case dw_val_class_offset:
8534 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8535 "%s", name);
8536 break;
8538 case dw_val_class_range_list:
8539 output_range_list_offset (a);
8540 break;
8542 case dw_val_class_loc:
8543 size = size_of_locs (AT_loc (a));
8545 /* Output the block length for this list of location operations. */
8546 if (dwarf_version >= 4)
8547 dw2_asm_output_data_uleb128 (size, "%s", name);
8548 else
8549 dw2_asm_output_data (constant_size (size), size, "%s", name);
8551 output_loc_sequence (AT_loc (a), -1);
8552 break;
8554 case dw_val_class_const:
8555 /* ??? It would be slightly more efficient to use a scheme like is
8556 used for unsigned constants below, but gdb 4.x does not sign
8557 extend. Gdb 5.x does sign extend. */
8558 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8559 break;
8561 case dw_val_class_unsigned_const:
8563 int csize = constant_size (AT_unsigned (a));
8564 if (dwarf_version == 3
8565 && a->dw_attr == DW_AT_data_member_location
8566 && csize >= 4)
8567 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8568 else
8569 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8571 break;
8573 case dw_val_class_const_double:
8575 unsigned HOST_WIDE_INT first, second;
8577 if (HOST_BITS_PER_WIDE_INT >= 64)
8578 dw2_asm_output_data (1,
8579 HOST_BITS_PER_DOUBLE_INT
8580 / HOST_BITS_PER_CHAR,
8581 NULL);
8583 if (WORDS_BIG_ENDIAN)
8585 first = a->dw_attr_val.v.val_double.high;
8586 second = a->dw_attr_val.v.val_double.low;
8588 else
8590 first = a->dw_attr_val.v.val_double.low;
8591 second = a->dw_attr_val.v.val_double.high;
8594 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8595 first, "%s", name);
8596 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8597 second, NULL);
8599 break;
8601 case dw_val_class_vec:
8603 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8604 unsigned int len = a->dw_attr_val.v.val_vec.length;
8605 unsigned int i;
8606 unsigned char *p;
8608 dw2_asm_output_data (constant_size (len * elt_size),
8609 len * elt_size, "%s", name);
8610 if (elt_size > sizeof (HOST_WIDE_INT))
8612 elt_size /= 2;
8613 len *= 2;
8615 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8616 i < len;
8617 i++, p += elt_size)
8618 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8619 "fp or vector constant word %u", i);
8620 break;
8623 case dw_val_class_flag:
8624 if (dwarf_version >= 4)
8626 /* Currently all add_AT_flag calls pass in 1 as last argument,
8627 so DW_FORM_flag_present can be used. If that ever changes,
8628 we'll need to use DW_FORM_flag and have some optimization
8629 in build_abbrev_table that will change those to
8630 DW_FORM_flag_present if it is set to 1 in all DIEs using
8631 the same abbrev entry. */
8632 gcc_assert (AT_flag (a) == 1);
8633 if (flag_debug_asm)
8634 fprintf (asm_out_file, "\t\t\t%s %s\n",
8635 ASM_COMMENT_START, name);
8636 break;
8638 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8639 break;
8641 case dw_val_class_loc_list:
8642 output_attr_index_or_value (a);
8643 break;
8645 case dw_val_class_die_ref:
8646 if (AT_ref_external (a))
8648 if (AT_ref (a)->comdat_type_p)
8650 comdat_type_node_ref type_node =
8651 AT_ref (a)->die_id.die_type_node;
8653 gcc_assert (type_node);
8654 output_signature (type_node->signature, name);
8656 else
8658 const char *sym = AT_ref (a)->die_id.die_symbol;
8659 int size;
8661 gcc_assert (sym);
8662 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8663 length, whereas in DWARF3 it's always sized as an
8664 offset. */
8665 if (dwarf_version == 2)
8666 size = DWARF2_ADDR_SIZE;
8667 else
8668 size = DWARF_OFFSET_SIZE;
8669 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8670 name);
8673 else
8675 gcc_assert (AT_ref (a)->die_offset);
8676 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8677 "%s", name);
8679 break;
8681 case dw_val_class_fde_ref:
8683 char l1[20];
8685 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8686 a->dw_attr_val.v.val_fde_index * 2);
8687 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8688 "%s", name);
8690 break;
8692 case dw_val_class_vms_delta:
8693 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8694 AT_vms_delta2 (a), AT_vms_delta1 (a),
8695 "%s", name);
8696 break;
8698 case dw_val_class_lbl_id:
8699 output_attr_index_or_value (a);
8700 break;
8702 case dw_val_class_lineptr:
8703 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8704 debug_line_section, "%s", name);
8705 break;
8707 case dw_val_class_macptr:
8708 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8709 debug_macinfo_section, "%s", name);
8710 break;
8712 case dw_val_class_str:
8713 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8714 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8715 a->dw_attr_val.v.val_str->label,
8716 debug_str_section,
8717 "%s: \"%s\"", name, AT_string (a));
8718 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8719 dw2_asm_output_data_uleb128 (AT_index (a),
8720 "%s: \"%s\"", name, AT_string (a));
8721 else
8722 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8723 break;
8725 case dw_val_class_file:
8727 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8729 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8730 a->dw_attr_val.v.val_file->filename);
8731 break;
8734 case dw_val_class_data8:
8736 int i;
8738 for (i = 0; i < 8; i++)
8739 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8740 i == 0 ? "%s" : NULL, name);
8741 break;
8744 case dw_val_class_high_pc:
8745 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8746 get_AT_low_pc (die), "DW_AT_high_pc");
8747 break;
8749 default:
8750 gcc_unreachable ();
8754 FOR_EACH_CHILD (die, c, output_die (c));
8756 /* Add null byte to terminate sibling list. */
8757 if (die->die_child != NULL)
8758 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8759 (unsigned long) die->die_offset);
8762 /* Output the compilation unit that appears at the beginning of the
8763 .debug_info section, and precedes the DIE descriptions. */
8765 static void
8766 output_compilation_unit_header (void)
8768 int ver = dwarf_version;
8770 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8771 dw2_asm_output_data (4, 0xffffffff,
8772 "Initial length escape value indicating 64-bit DWARF extension");
8773 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8774 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8775 "Length of Compilation Unit Info");
8776 dw2_asm_output_data (2, ver, "DWARF version number");
8777 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8778 debug_abbrev_section,
8779 "Offset Into Abbrev. Section");
8780 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8783 /* Output the compilation unit DIE and its children. */
8785 static void
8786 output_comp_unit (dw_die_ref die, int output_if_empty)
8788 const char *secname, *oldsym;
8789 char *tmp;
8790 external_ref_hash_type extern_map;
8792 /* Unless we are outputting main CU, we may throw away empty ones. */
8793 if (!output_if_empty && die->die_child == NULL)
8794 return;
8796 /* Even if there are no children of this DIE, we must output the information
8797 about the compilation unit. Otherwise, on an empty translation unit, we
8798 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8799 will then complain when examining the file. First mark all the DIEs in
8800 this CU so we know which get local refs. */
8801 mark_dies (die);
8803 extern_map = optimize_external_refs (die);
8805 build_abbrev_table (die, extern_map);
8807 extern_map.dispose ();
8809 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8810 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8811 calc_die_sizes (die);
8813 oldsym = die->die_id.die_symbol;
8814 if (oldsym)
8816 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8818 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8819 secname = tmp;
8820 die->die_id.die_symbol = NULL;
8821 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8823 else
8825 switch_to_section (debug_info_section);
8826 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8827 info_section_emitted = true;
8830 /* Output debugging information. */
8831 output_compilation_unit_header ();
8832 output_die (die);
8834 /* Leave the marks on the main CU, so we can check them in
8835 output_pubnames. */
8836 if (oldsym)
8838 unmark_dies (die);
8839 die->die_id.die_symbol = oldsym;
8843 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8844 and .debug_pubtypes. This is configured per-target, but can be
8845 overridden by the -gpubnames or -gno-pubnames options. */
8847 static inline bool
8848 want_pubnames (void)
8850 if (debug_info_level <= DINFO_LEVEL_TERSE)
8851 return false;
8852 if (debug_generate_pub_sections != -1)
8853 return debug_generate_pub_sections;
8854 return targetm.want_debug_pub_sections;
8857 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8859 static void
8860 add_AT_pubnames (dw_die_ref die)
8862 if (want_pubnames ())
8863 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8866 /* Add a string attribute value to a skeleton DIE. */
8868 static inline void
8869 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8870 const char *str)
8872 dw_attr_node attr;
8873 struct indirect_string_node *node;
8875 if (! skeleton_debug_str_hash)
8876 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8877 debug_str_eq, NULL);
8879 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8880 find_string_form (node);
8881 if (node->form == DW_FORM_GNU_str_index)
8882 node->form = DW_FORM_strp;
8884 attr.dw_attr = attr_kind;
8885 attr.dw_attr_val.val_class = dw_val_class_str;
8886 attr.dw_attr_val.val_entry = NULL;
8887 attr.dw_attr_val.v.val_str = node;
8888 add_dwarf_attr (die, &attr);
8891 /* Helper function to generate top-level dies for skeleton debug_info and
8892 debug_types. */
8894 static void
8895 add_top_level_skeleton_die_attrs (dw_die_ref die)
8897 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8898 const char *comp_dir = comp_dir_string ();
8900 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8901 if (comp_dir != NULL)
8902 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8903 add_AT_pubnames (die);
8904 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8907 /* Return the single type-unit die for skeleton type units. */
8909 static dw_die_ref
8910 get_skeleton_type_unit (void)
8912 /* For dwarf_split_debug_sections with use_type info, all type units in the
8913 skeleton sections have identical dies (but different headers). This
8914 single die will be output many times. */
8916 static dw_die_ref skeleton_type_unit = NULL;
8918 if (skeleton_type_unit == NULL)
8920 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8921 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8922 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8924 return skeleton_type_unit;
8927 /* Output skeleton debug sections that point to the dwo file. */
8929 static void
8930 output_skeleton_debug_sections (dw_die_ref comp_unit)
8932 /* These attributes will be found in the full debug_info section. */
8933 remove_AT (comp_unit, DW_AT_producer);
8934 remove_AT (comp_unit, DW_AT_language);
8936 switch_to_section (debug_skeleton_info_section);
8937 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8939 /* Produce the skeleton compilation-unit header. This one differs enough from
8940 a normal CU header that it's better not to call output_compilation_unit
8941 header. */
8942 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8943 dw2_asm_output_data (4, 0xffffffff,
8944 "Initial length escape value indicating 64-bit DWARF extension");
8946 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8947 DWARF_COMPILE_UNIT_HEADER_SIZE
8948 - DWARF_INITIAL_LENGTH_SIZE
8949 + size_of_die (comp_unit),
8950 "Length of Compilation Unit Info");
8951 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8952 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8953 debug_abbrev_section,
8954 "Offset Into Abbrev. Section");
8955 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8957 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8958 output_die (comp_unit);
8960 /* Build the skeleton debug_abbrev section. */
8961 switch_to_section (debug_skeleton_abbrev_section);
8962 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8964 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8965 if (use_debug_types)
8966 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8968 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8971 /* Output a comdat type unit DIE and its children. */
8973 static void
8974 output_comdat_type_unit (comdat_type_node *node)
8976 const char *secname;
8977 char *tmp;
8978 int i;
8979 #if defined (OBJECT_FORMAT_ELF)
8980 tree comdat_key;
8981 #endif
8982 external_ref_hash_type extern_map;
8984 /* First mark all the DIEs in this CU so we know which get local refs. */
8985 mark_dies (node->root_die);
8987 extern_map = optimize_external_refs (node->root_die);
8989 build_abbrev_table (node->root_die, extern_map);
8991 extern_map.dispose ();
8993 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8994 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8995 calc_die_sizes (node->root_die);
8997 #if defined (OBJECT_FORMAT_ELF)
8998 if (!dwarf_split_debug_info)
8999 secname = ".debug_types";
9000 else
9001 secname = ".debug_types.dwo";
9003 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9004 sprintf (tmp, "wt.");
9005 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9006 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9007 comdat_key = get_identifier (tmp);
9008 targetm.asm_out.named_section (secname,
9009 SECTION_DEBUG | SECTION_LINKONCE,
9010 comdat_key);
9011 #else
9012 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9013 sprintf (tmp, ".gnu.linkonce.wt.");
9014 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9015 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9016 secname = tmp;
9017 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9018 #endif
9020 /* Output debugging information. */
9021 output_compilation_unit_header ();
9022 output_signature (node->signature, "Type Signature");
9023 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9024 "Offset to Type DIE");
9025 output_die (node->root_die);
9027 unmark_dies (node->root_die);
9029 #if defined (OBJECT_FORMAT_ELF)
9030 if (dwarf_split_debug_info)
9032 /* Produce the skeleton type-unit header. */
9033 const char *secname = ".debug_types";
9035 targetm.asm_out.named_section (secname,
9036 SECTION_DEBUG | SECTION_LINKONCE,
9037 comdat_key);
9038 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9039 dw2_asm_output_data (4, 0xffffffff,
9040 "Initial length escape value indicating 64-bit DWARF extension");
9042 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9043 DWARF_COMPILE_UNIT_HEADER_SIZE
9044 - DWARF_INITIAL_LENGTH_SIZE
9045 + size_of_die (get_skeleton_type_unit ())
9046 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9047 "Length of Type Unit Info");
9048 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9049 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9050 debug_skeleton_abbrev_section_label,
9051 debug_abbrev_section,
9052 "Offset Into Abbrev. Section");
9053 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9054 output_signature (node->signature, "Type Signature");
9055 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9057 output_die (get_skeleton_type_unit ());
9059 #endif
9062 /* Return the DWARF2/3 pubname associated with a decl. */
9064 static const char *
9065 dwarf2_name (tree decl, int scope)
9067 if (DECL_NAMELESS (decl))
9068 return NULL;
9069 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9072 /* Add a new entry to .debug_pubnames if appropriate. */
9074 static void
9075 add_pubname_string (const char *str, dw_die_ref die)
9077 pubname_entry e;
9079 e.die = die;
9080 e.name = xstrdup (str);
9081 vec_safe_push (pubname_table, e);
9084 static void
9085 add_pubname (tree decl, dw_die_ref die)
9087 if (!want_pubnames ())
9088 return;
9090 /* Don't add items to the table when we expect that the consumer will have
9091 just read the enclosing die. For example, if the consumer is looking at a
9092 class_member, it will either be inside the class already, or will have just
9093 looked up the class to find the member. Either way, searching the class is
9094 faster than searching the index. */
9095 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9096 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9098 const char *name = dwarf2_name (decl, 1);
9100 if (name)
9101 add_pubname_string (name, die);
9105 /* Add an enumerator to the pubnames section. */
9107 static void
9108 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9110 pubname_entry e;
9112 gcc_assert (scope_name);
9113 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9114 e.die = die;
9115 vec_safe_push (pubname_table, e);
9118 /* Add a new entry to .debug_pubtypes if appropriate. */
9120 static void
9121 add_pubtype (tree decl, dw_die_ref die)
9123 pubname_entry e;
9125 if (!want_pubnames ())
9126 return;
9128 if ((TREE_PUBLIC (decl)
9129 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9130 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9132 tree scope = NULL;
9133 const char *scope_name = "";
9134 const char *sep = is_cxx () ? "::" : ".";
9135 const char *name;
9137 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9138 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9140 scope_name = lang_hooks.dwarf_name (scope, 1);
9141 if (scope_name != NULL && scope_name[0] != '\0')
9142 scope_name = concat (scope_name, sep, NULL);
9143 else
9144 scope_name = "";
9147 if (TYPE_P (decl))
9148 name = type_tag (decl);
9149 else
9150 name = lang_hooks.dwarf_name (decl, 1);
9152 /* If we don't have a name for the type, there's no point in adding
9153 it to the table. */
9154 if (name != NULL && name[0] != '\0')
9156 e.die = die;
9157 e.name = concat (scope_name, name, NULL);
9158 vec_safe_push (pubtype_table, e);
9161 /* Although it might be more consistent to add the pubinfo for the
9162 enumerators as their dies are created, they should only be added if the
9163 enum type meets the criteria above. So rather than re-check the parent
9164 enum type whenever an enumerator die is created, just output them all
9165 here. This isn't protected by the name conditional because anonymous
9166 enums don't have names. */
9167 if (die->die_tag == DW_TAG_enumeration_type)
9169 dw_die_ref c;
9171 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9176 /* Output a single entry in the pubnames table. */
9178 static void
9179 output_pubname (dw_offset die_offset, pubname_entry *entry)
9181 dw_die_ref die = entry->die;
9182 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9184 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9186 if (debug_generate_pub_sections == 2)
9188 /* This logic follows gdb's method for determining the value of the flag
9189 byte. */
9190 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9191 switch (die->die_tag)
9193 case DW_TAG_typedef:
9194 case DW_TAG_base_type:
9195 case DW_TAG_subrange_type:
9196 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9197 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9198 break;
9199 case DW_TAG_enumerator:
9200 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9201 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9202 if (!is_cxx () && !is_java ())
9203 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9204 break;
9205 case DW_TAG_subprogram:
9206 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9207 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9208 if (!is_ada ())
9209 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9210 break;
9211 case DW_TAG_constant:
9212 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9213 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9214 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9215 break;
9216 case DW_TAG_variable:
9217 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9218 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9219 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9220 break;
9221 case DW_TAG_namespace:
9222 case DW_TAG_imported_declaration:
9223 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9224 break;
9225 case DW_TAG_class_type:
9226 case DW_TAG_interface_type:
9227 case DW_TAG_structure_type:
9228 case DW_TAG_union_type:
9229 case DW_TAG_enumeration_type:
9230 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9231 if (!is_cxx () && !is_java ())
9232 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9233 break;
9234 default:
9235 /* An unusual tag. Leave the flag-byte empty. */
9236 break;
9238 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9239 "GDB-index flags");
9242 dw2_asm_output_nstring (entry->name, -1, "external name");
9246 /* Output the public names table used to speed up access to externally
9247 visible names; or the public types table used to find type definitions. */
9249 static void
9250 output_pubnames (vec<pubname_entry, va_gc> *names)
9252 unsigned i;
9253 unsigned long pubnames_length = size_of_pubnames (names);
9254 pubname_ref pub;
9256 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9257 dw2_asm_output_data (4, 0xffffffff,
9258 "Initial length escape value indicating 64-bit DWARF extension");
9259 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9261 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9262 dw2_asm_output_data (2, 2, "DWARF Version");
9264 if (dwarf_split_debug_info)
9265 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9266 debug_skeleton_info_section,
9267 "Offset of Compilation Unit Info");
9268 else
9269 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9270 debug_info_section,
9271 "Offset of Compilation Unit Info");
9272 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9273 "Compilation Unit Length");
9275 FOR_EACH_VEC_ELT (*names, i, pub)
9277 if (include_pubname_in_output (names, pub))
9279 dw_offset die_offset = pub->die->die_offset;
9281 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9282 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9283 gcc_assert (pub->die->die_mark);
9285 /* If we're putting types in their own .debug_types sections,
9286 the .debug_pubtypes table will still point to the compile
9287 unit (not the type unit), so we want to use the offset of
9288 the skeleton DIE (if there is one). */
9289 if (pub->die->comdat_type_p && names == pubtype_table)
9291 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9293 if (type_node != NULL)
9294 die_offset = (type_node->skeleton_die != NULL
9295 ? type_node->skeleton_die->die_offset
9296 : 0);
9299 output_pubname (die_offset, pub);
9303 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9306 /* Output public names and types tables if necessary. */
9308 static void
9309 output_pubtables (void)
9311 if (!want_pubnames () || !info_section_emitted)
9312 return;
9314 switch_to_section (debug_pubnames_section);
9315 output_pubnames (pubname_table);
9316 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9317 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9318 simply won't look for the section. */
9319 switch_to_section (debug_pubtypes_section);
9320 output_pubnames (pubtype_table);
9324 /* Output the information that goes into the .debug_aranges table.
9325 Namely, define the beginning and ending address range of the
9326 text section generated for this compilation unit. */
9328 static void
9329 output_aranges (unsigned long aranges_length)
9331 unsigned i;
9333 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9334 dw2_asm_output_data (4, 0xffffffff,
9335 "Initial length escape value indicating 64-bit DWARF extension");
9336 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9337 "Length of Address Ranges Info");
9338 /* Version number for aranges is still 2, even in DWARF3. */
9339 dw2_asm_output_data (2, 2, "DWARF Version");
9340 if (dwarf_split_debug_info)
9341 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9342 debug_skeleton_info_section,
9343 "Offset of Compilation Unit Info");
9344 else
9345 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9346 debug_info_section,
9347 "Offset of Compilation Unit Info");
9348 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9349 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9351 /* We need to align to twice the pointer size here. */
9352 if (DWARF_ARANGES_PAD_SIZE)
9354 /* Pad using a 2 byte words so that padding is correct for any
9355 pointer size. */
9356 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9357 2 * DWARF2_ADDR_SIZE);
9358 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9359 dw2_asm_output_data (2, 0, NULL);
9362 /* It is necessary not to output these entries if the sections were
9363 not used; if the sections were not used, the length will be 0 and
9364 the address may end up as 0 if the section is discarded by ld
9365 --gc-sections, leaving an invalid (0, 0) entry that can be
9366 confused with the terminator. */
9367 if (text_section_used)
9369 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9370 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9371 text_section_label, "Length");
9373 if (cold_text_section_used)
9375 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9376 "Address");
9377 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9378 cold_text_section_label, "Length");
9381 if (have_multiple_function_sections)
9383 unsigned fde_idx;
9384 dw_fde_ref fde;
9386 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9388 if (DECL_IGNORED_P (fde->decl))
9389 continue;
9390 if (!fde->in_std_section)
9392 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9393 "Address");
9394 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9395 fde->dw_fde_begin, "Length");
9397 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9400 "Address");
9401 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9402 fde->dw_fde_second_begin, "Length");
9407 /* Output the terminator words. */
9408 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9409 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9412 /* Add a new entry to .debug_ranges. Return the offset at which it
9413 was placed. */
9415 static unsigned int
9416 add_ranges_num (int num)
9418 unsigned int in_use = ranges_table_in_use;
9420 if (in_use == ranges_table_allocated)
9422 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9423 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9424 ranges_table_allocated);
9425 memset (ranges_table + ranges_table_in_use, 0,
9426 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9429 ranges_table[in_use].num = num;
9430 ranges_table_in_use = in_use + 1;
9432 return in_use * 2 * DWARF2_ADDR_SIZE;
9435 /* Add a new entry to .debug_ranges corresponding to a block, or a
9436 range terminator if BLOCK is NULL. */
9438 static unsigned int
9439 add_ranges (const_tree block)
9441 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9444 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9445 When using dwarf_split_debug_info, address attributes in dies destined
9446 for the final executable should be direct references--setting the
9447 parameter force_direct ensures this behavior. */
9449 static void
9450 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9451 bool *added, bool force_direct)
9453 unsigned int in_use = ranges_by_label_in_use;
9454 unsigned int offset;
9456 if (in_use == ranges_by_label_allocated)
9458 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9459 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9460 ranges_by_label,
9461 ranges_by_label_allocated);
9462 memset (ranges_by_label + ranges_by_label_in_use, 0,
9463 RANGES_TABLE_INCREMENT
9464 * sizeof (struct dw_ranges_by_label_struct));
9467 ranges_by_label[in_use].begin = begin;
9468 ranges_by_label[in_use].end = end;
9469 ranges_by_label_in_use = in_use + 1;
9471 offset = add_ranges_num (-(int)in_use - 1);
9472 if (!*added)
9474 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9475 *added = true;
9479 static void
9480 output_ranges (void)
9482 unsigned i;
9483 static const char *const start_fmt = "Offset %#x";
9484 const char *fmt = start_fmt;
9486 for (i = 0; i < ranges_table_in_use; i++)
9488 int block_num = ranges_table[i].num;
9490 if (block_num > 0)
9492 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9493 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9495 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9496 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9498 /* If all code is in the text section, then the compilation
9499 unit base address defaults to DW_AT_low_pc, which is the
9500 base of the text section. */
9501 if (!have_multiple_function_sections)
9503 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9504 text_section_label,
9505 fmt, i * 2 * DWARF2_ADDR_SIZE);
9506 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9507 text_section_label, NULL);
9510 /* Otherwise, the compilation unit base address is zero,
9511 which allows us to use absolute addresses, and not worry
9512 about whether the target supports cross-section
9513 arithmetic. */
9514 else
9516 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9517 fmt, i * 2 * DWARF2_ADDR_SIZE);
9518 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9521 fmt = NULL;
9524 /* Negative block_num stands for an index into ranges_by_label. */
9525 else if (block_num < 0)
9527 int lab_idx = - block_num - 1;
9529 if (!have_multiple_function_sections)
9531 gcc_unreachable ();
9532 #if 0
9533 /* If we ever use add_ranges_by_labels () for a single
9534 function section, all we have to do is to take out
9535 the #if 0 above. */
9536 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9537 ranges_by_label[lab_idx].begin,
9538 text_section_label,
9539 fmt, i * 2 * DWARF2_ADDR_SIZE);
9540 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9541 ranges_by_label[lab_idx].end,
9542 text_section_label, NULL);
9543 #endif
9545 else
9547 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9548 ranges_by_label[lab_idx].begin,
9549 fmt, i * 2 * DWARF2_ADDR_SIZE);
9550 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9551 ranges_by_label[lab_idx].end,
9552 NULL);
9555 else
9557 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9558 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9559 fmt = start_fmt;
9564 /* Data structure containing information about input files. */
9565 struct file_info
9567 const char *path; /* Complete file name. */
9568 const char *fname; /* File name part. */
9569 int length; /* Length of entire string. */
9570 struct dwarf_file_data * file_idx; /* Index in input file table. */
9571 int dir_idx; /* Index in directory table. */
9574 /* Data structure containing information about directories with source
9575 files. */
9576 struct dir_info
9578 const char *path; /* Path including directory name. */
9579 int length; /* Path length. */
9580 int prefix; /* Index of directory entry which is a prefix. */
9581 int count; /* Number of files in this directory. */
9582 int dir_idx; /* Index of directory used as base. */
9585 /* Callback function for file_info comparison. We sort by looking at
9586 the directories in the path. */
9588 static int
9589 file_info_cmp (const void *p1, const void *p2)
9591 const struct file_info *const s1 = (const struct file_info *) p1;
9592 const struct file_info *const s2 = (const struct file_info *) p2;
9593 const unsigned char *cp1;
9594 const unsigned char *cp2;
9596 /* Take care of file names without directories. We need to make sure that
9597 we return consistent values to qsort since some will get confused if
9598 we return the same value when identical operands are passed in opposite
9599 orders. So if neither has a directory, return 0 and otherwise return
9600 1 or -1 depending on which one has the directory. */
9601 if ((s1->path == s1->fname || s2->path == s2->fname))
9602 return (s2->path == s2->fname) - (s1->path == s1->fname);
9604 cp1 = (const unsigned char *) s1->path;
9605 cp2 = (const unsigned char *) s2->path;
9607 while (1)
9609 ++cp1;
9610 ++cp2;
9611 /* Reached the end of the first path? If so, handle like above. */
9612 if ((cp1 == (const unsigned char *) s1->fname)
9613 || (cp2 == (const unsigned char *) s2->fname))
9614 return ((cp2 == (const unsigned char *) s2->fname)
9615 - (cp1 == (const unsigned char *) s1->fname));
9617 /* Character of current path component the same? */
9618 else if (*cp1 != *cp2)
9619 return *cp1 - *cp2;
9623 struct file_name_acquire_data
9625 struct file_info *files;
9626 int used_files;
9627 int max_files;
9630 /* Traversal function for the hash table. */
9632 static int
9633 file_name_acquire (void ** slot, void *data)
9635 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9636 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9637 struct file_info *fi;
9638 const char *f;
9640 gcc_assert (fnad->max_files >= d->emitted_number);
9642 if (! d->emitted_number)
9643 return 1;
9645 gcc_assert (fnad->max_files != fnad->used_files);
9647 fi = fnad->files + fnad->used_files++;
9649 /* Skip all leading "./". */
9650 f = d->filename;
9651 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9652 f += 2;
9654 /* Create a new array entry. */
9655 fi->path = f;
9656 fi->length = strlen (f);
9657 fi->file_idx = d;
9659 /* Search for the file name part. */
9660 f = strrchr (f, DIR_SEPARATOR);
9661 #if defined (DIR_SEPARATOR_2)
9663 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9665 if (g != NULL)
9667 if (f == NULL || f < g)
9668 f = g;
9671 #endif
9673 fi->fname = f == NULL ? fi->path : f + 1;
9674 return 1;
9677 /* Output the directory table and the file name table. We try to minimize
9678 the total amount of memory needed. A heuristic is used to avoid large
9679 slowdowns with many input files. */
9681 static void
9682 output_file_names (void)
9684 struct file_name_acquire_data fnad;
9685 int numfiles;
9686 struct file_info *files;
9687 struct dir_info *dirs;
9688 int *saved;
9689 int *savehere;
9690 int *backmap;
9691 int ndirs;
9692 int idx_offset;
9693 int i;
9695 if (!last_emitted_file)
9697 dw2_asm_output_data (1, 0, "End directory table");
9698 dw2_asm_output_data (1, 0, "End file name table");
9699 return;
9702 numfiles = last_emitted_file->emitted_number;
9704 /* Allocate the various arrays we need. */
9705 files = XALLOCAVEC (struct file_info, numfiles);
9706 dirs = XALLOCAVEC (struct dir_info, numfiles);
9708 fnad.files = files;
9709 fnad.used_files = 0;
9710 fnad.max_files = numfiles;
9711 htab_traverse (file_table, file_name_acquire, &fnad);
9712 gcc_assert (fnad.used_files == fnad.max_files);
9714 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9716 /* Find all the different directories used. */
9717 dirs[0].path = files[0].path;
9718 dirs[0].length = files[0].fname - files[0].path;
9719 dirs[0].prefix = -1;
9720 dirs[0].count = 1;
9721 dirs[0].dir_idx = 0;
9722 files[0].dir_idx = 0;
9723 ndirs = 1;
9725 for (i = 1; i < numfiles; i++)
9726 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9727 && memcmp (dirs[ndirs - 1].path, files[i].path,
9728 dirs[ndirs - 1].length) == 0)
9730 /* Same directory as last entry. */
9731 files[i].dir_idx = ndirs - 1;
9732 ++dirs[ndirs - 1].count;
9734 else
9736 int j;
9738 /* This is a new directory. */
9739 dirs[ndirs].path = files[i].path;
9740 dirs[ndirs].length = files[i].fname - files[i].path;
9741 dirs[ndirs].count = 1;
9742 dirs[ndirs].dir_idx = ndirs;
9743 files[i].dir_idx = ndirs;
9745 /* Search for a prefix. */
9746 dirs[ndirs].prefix = -1;
9747 for (j = 0; j < ndirs; j++)
9748 if (dirs[j].length < dirs[ndirs].length
9749 && dirs[j].length > 1
9750 && (dirs[ndirs].prefix == -1
9751 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9752 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9753 dirs[ndirs].prefix = j;
9755 ++ndirs;
9758 /* Now to the actual work. We have to find a subset of the directories which
9759 allow expressing the file name using references to the directory table
9760 with the least amount of characters. We do not do an exhaustive search
9761 where we would have to check out every combination of every single
9762 possible prefix. Instead we use a heuristic which provides nearly optimal
9763 results in most cases and never is much off. */
9764 saved = XALLOCAVEC (int, ndirs);
9765 savehere = XALLOCAVEC (int, ndirs);
9767 memset (saved, '\0', ndirs * sizeof (saved[0]));
9768 for (i = 0; i < ndirs; i++)
9770 int j;
9771 int total;
9773 /* We can always save some space for the current directory. But this
9774 does not mean it will be enough to justify adding the directory. */
9775 savehere[i] = dirs[i].length;
9776 total = (savehere[i] - saved[i]) * dirs[i].count;
9778 for (j = i + 1; j < ndirs; j++)
9780 savehere[j] = 0;
9781 if (saved[j] < dirs[i].length)
9783 /* Determine whether the dirs[i] path is a prefix of the
9784 dirs[j] path. */
9785 int k;
9787 k = dirs[j].prefix;
9788 while (k != -1 && k != (int) i)
9789 k = dirs[k].prefix;
9791 if (k == (int) i)
9793 /* Yes it is. We can possibly save some memory by
9794 writing the filenames in dirs[j] relative to
9795 dirs[i]. */
9796 savehere[j] = dirs[i].length;
9797 total += (savehere[j] - saved[j]) * dirs[j].count;
9802 /* Check whether we can save enough to justify adding the dirs[i]
9803 directory. */
9804 if (total > dirs[i].length + 1)
9806 /* It's worthwhile adding. */
9807 for (j = i; j < ndirs; j++)
9808 if (savehere[j] > 0)
9810 /* Remember how much we saved for this directory so far. */
9811 saved[j] = savehere[j];
9813 /* Remember the prefix directory. */
9814 dirs[j].dir_idx = i;
9819 /* Emit the directory name table. */
9820 idx_offset = dirs[0].length > 0 ? 1 : 0;
9821 for (i = 1 - idx_offset; i < ndirs; i++)
9822 dw2_asm_output_nstring (dirs[i].path,
9823 dirs[i].length
9824 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9825 "Directory Entry: %#x", i + idx_offset);
9827 dw2_asm_output_data (1, 0, "End directory table");
9829 /* We have to emit them in the order of emitted_number since that's
9830 used in the debug info generation. To do this efficiently we
9831 generate a back-mapping of the indices first. */
9832 backmap = XALLOCAVEC (int, numfiles);
9833 for (i = 0; i < numfiles; i++)
9834 backmap[files[i].file_idx->emitted_number - 1] = i;
9836 /* Now write all the file names. */
9837 for (i = 0; i < numfiles; i++)
9839 int file_idx = backmap[i];
9840 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9842 #ifdef VMS_DEBUGGING_INFO
9843 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9845 /* Setting these fields can lead to debugger miscomparisons,
9846 but VMS Debug requires them to be set correctly. */
9848 int ver;
9849 long long cdt;
9850 long siz;
9851 int maxfilelen = strlen (files[file_idx].path)
9852 + dirs[dir_idx].length
9853 + MAX_VMS_VERSION_LEN + 1;
9854 char *filebuf = XALLOCAVEC (char, maxfilelen);
9856 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9857 snprintf (filebuf, maxfilelen, "%s;%d",
9858 files[file_idx].path + dirs[dir_idx].length, ver);
9860 dw2_asm_output_nstring
9861 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9863 /* Include directory index. */
9864 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9866 /* Modification time. */
9867 dw2_asm_output_data_uleb128
9868 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9869 ? cdt : 0,
9870 NULL);
9872 /* File length in bytes. */
9873 dw2_asm_output_data_uleb128
9874 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9875 ? siz : 0,
9876 NULL);
9877 #else
9878 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9879 "File Entry: %#x", (unsigned) i + 1);
9881 /* Include directory index. */
9882 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9884 /* Modification time. */
9885 dw2_asm_output_data_uleb128 (0, NULL);
9887 /* File length in bytes. */
9888 dw2_asm_output_data_uleb128 (0, NULL);
9889 #endif /* VMS_DEBUGGING_INFO */
9892 dw2_asm_output_data (1, 0, "End file name table");
9896 /* Output one line number table into the .debug_line section. */
9898 static void
9899 output_one_line_info_table (dw_line_info_table *table)
9901 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9902 unsigned int current_line = 1;
9903 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9904 dw_line_info_entry *ent;
9905 size_t i;
9907 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9909 switch (ent->opcode)
9911 case LI_set_address:
9912 /* ??? Unfortunately, we have little choice here currently, and
9913 must always use the most general form. GCC does not know the
9914 address delta itself, so we can't use DW_LNS_advance_pc. Many
9915 ports do have length attributes which will give an upper bound
9916 on the address range. We could perhaps use length attributes
9917 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9918 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9920 /* This can handle any delta. This takes
9921 4+DWARF2_ADDR_SIZE bytes. */
9922 dw2_asm_output_data (1, 0, "set address %s", line_label);
9923 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9924 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9925 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9926 break;
9928 case LI_set_line:
9929 if (ent->val == current_line)
9931 /* We still need to start a new row, so output a copy insn. */
9932 dw2_asm_output_data (1, DW_LNS_copy,
9933 "copy line %u", current_line);
9935 else
9937 int line_offset = ent->val - current_line;
9938 int line_delta = line_offset - DWARF_LINE_BASE;
9940 current_line = ent->val;
9941 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9943 /* This can handle deltas from -10 to 234, using the current
9944 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9945 This takes 1 byte. */
9946 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9947 "line %u", current_line);
9949 else
9951 /* This can handle any delta. This takes at least 4 bytes,
9952 depending on the value being encoded. */
9953 dw2_asm_output_data (1, DW_LNS_advance_line,
9954 "advance to line %u", current_line);
9955 dw2_asm_output_data_sleb128 (line_offset, NULL);
9956 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9959 break;
9961 case LI_set_file:
9962 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9963 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9964 break;
9966 case LI_set_column:
9967 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9968 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9969 break;
9971 case LI_negate_stmt:
9972 current_is_stmt = !current_is_stmt;
9973 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9974 "is_stmt %d", current_is_stmt);
9975 break;
9977 case LI_set_prologue_end:
9978 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9979 "set prologue end");
9980 break;
9982 case LI_set_epilogue_begin:
9983 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9984 "set epilogue begin");
9985 break;
9987 case LI_set_discriminator:
9988 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9989 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9990 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9991 dw2_asm_output_data_uleb128 (ent->val, NULL);
9992 break;
9996 /* Emit debug info for the address of the end of the table. */
9997 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9998 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9999 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10000 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10002 dw2_asm_output_data (1, 0, "end sequence");
10003 dw2_asm_output_data_uleb128 (1, NULL);
10004 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10007 /* Output the source line number correspondence information. This
10008 information goes into the .debug_line section. */
10010 static void
10011 output_line_info (bool prologue_only)
10013 char l1[20], l2[20], p1[20], p2[20];
10014 int ver = dwarf_version;
10015 bool saw_one = false;
10016 int opc;
10018 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10019 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10020 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10021 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10023 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10024 dw2_asm_output_data (4, 0xffffffff,
10025 "Initial length escape value indicating 64-bit DWARF extension");
10026 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10027 "Length of Source Line Info");
10028 ASM_OUTPUT_LABEL (asm_out_file, l1);
10030 dw2_asm_output_data (2, ver, "DWARF Version");
10031 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10032 ASM_OUTPUT_LABEL (asm_out_file, p1);
10034 /* Define the architecture-dependent minimum instruction length (in bytes).
10035 In this implementation of DWARF, this field is used for information
10036 purposes only. Since GCC generates assembly language, we have no
10037 a priori knowledge of how many instruction bytes are generated for each
10038 source line, and therefore can use only the DW_LNE_set_address and
10039 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10040 this as '1', which is "correct enough" for all architectures,
10041 and don't let the target override. */
10042 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10044 if (ver >= 4)
10045 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10046 "Maximum Operations Per Instruction");
10047 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10048 "Default is_stmt_start flag");
10049 dw2_asm_output_data (1, DWARF_LINE_BASE,
10050 "Line Base Value (Special Opcodes)");
10051 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10052 "Line Range Value (Special Opcodes)");
10053 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10054 "Special Opcode Base");
10056 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10058 int n_op_args;
10059 switch (opc)
10061 case DW_LNS_advance_pc:
10062 case DW_LNS_advance_line:
10063 case DW_LNS_set_file:
10064 case DW_LNS_set_column:
10065 case DW_LNS_fixed_advance_pc:
10066 case DW_LNS_set_isa:
10067 n_op_args = 1;
10068 break;
10069 default:
10070 n_op_args = 0;
10071 break;
10074 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10075 opc, n_op_args);
10078 /* Write out the information about the files we use. */
10079 output_file_names ();
10080 ASM_OUTPUT_LABEL (asm_out_file, p2);
10081 if (prologue_only)
10083 /* Output the marker for the end of the line number info. */
10084 ASM_OUTPUT_LABEL (asm_out_file, l2);
10085 return;
10088 if (separate_line_info)
10090 dw_line_info_table *table;
10091 size_t i;
10093 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10094 if (table->in_use)
10096 output_one_line_info_table (table);
10097 saw_one = true;
10100 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10102 output_one_line_info_table (cold_text_section_line_info);
10103 saw_one = true;
10106 /* ??? Some Darwin linkers crash on a .debug_line section with no
10107 sequences. Further, merely a DW_LNE_end_sequence entry is not
10108 sufficient -- the address column must also be initialized.
10109 Make sure to output at least one set_address/end_sequence pair,
10110 choosing .text since that section is always present. */
10111 if (text_section_line_info->in_use || !saw_one)
10112 output_one_line_info_table (text_section_line_info);
10114 /* Output the marker for the end of the line number info. */
10115 ASM_OUTPUT_LABEL (asm_out_file, l2);
10118 /* Given a pointer to a tree node for some base type, return a pointer to
10119 a DIE that describes the given type.
10121 This routine must only be called for GCC type nodes that correspond to
10122 Dwarf base (fundamental) types. */
10124 static dw_die_ref
10125 base_type_die (tree type)
10127 dw_die_ref base_type_result;
10128 enum dwarf_type encoding;
10130 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10131 return 0;
10133 /* If this is a subtype that should not be emitted as a subrange type,
10134 use the base type. See subrange_type_for_debug_p. */
10135 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10136 type = TREE_TYPE (type);
10138 switch (TREE_CODE (type))
10140 case INTEGER_TYPE:
10141 if ((dwarf_version >= 4 || !dwarf_strict)
10142 && TYPE_NAME (type)
10143 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10144 && DECL_IS_BUILTIN (TYPE_NAME (type))
10145 && DECL_NAME (TYPE_NAME (type)))
10147 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10148 if (strcmp (name, "char16_t") == 0
10149 || strcmp (name, "char32_t") == 0)
10151 encoding = DW_ATE_UTF;
10152 break;
10155 if (TYPE_STRING_FLAG (type))
10157 if (TYPE_UNSIGNED (type))
10158 encoding = DW_ATE_unsigned_char;
10159 else
10160 encoding = DW_ATE_signed_char;
10162 else if (TYPE_UNSIGNED (type))
10163 encoding = DW_ATE_unsigned;
10164 else
10165 encoding = DW_ATE_signed;
10166 break;
10168 case REAL_TYPE:
10169 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10171 if (dwarf_version >= 3 || !dwarf_strict)
10172 encoding = DW_ATE_decimal_float;
10173 else
10174 encoding = DW_ATE_lo_user;
10176 else
10177 encoding = DW_ATE_float;
10178 break;
10180 case FIXED_POINT_TYPE:
10181 if (!(dwarf_version >= 3 || !dwarf_strict))
10182 encoding = DW_ATE_lo_user;
10183 else if (TYPE_UNSIGNED (type))
10184 encoding = DW_ATE_unsigned_fixed;
10185 else
10186 encoding = DW_ATE_signed_fixed;
10187 break;
10189 /* Dwarf2 doesn't know anything about complex ints, so use
10190 a user defined type for it. */
10191 case COMPLEX_TYPE:
10192 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10193 encoding = DW_ATE_complex_float;
10194 else
10195 encoding = DW_ATE_lo_user;
10196 break;
10198 case BOOLEAN_TYPE:
10199 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10200 encoding = DW_ATE_boolean;
10201 break;
10203 default:
10204 /* No other TREE_CODEs are Dwarf fundamental types. */
10205 gcc_unreachable ();
10208 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10210 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10211 int_size_in_bytes (type));
10212 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10213 add_pubtype (type, base_type_result);
10215 return base_type_result;
10218 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10219 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10221 static inline int
10222 is_base_type (tree type)
10224 switch (TREE_CODE (type))
10226 case ERROR_MARK:
10227 case VOID_TYPE:
10228 case INTEGER_TYPE:
10229 case REAL_TYPE:
10230 case FIXED_POINT_TYPE:
10231 case COMPLEX_TYPE:
10232 case BOOLEAN_TYPE:
10233 return 1;
10235 case ARRAY_TYPE:
10236 case RECORD_TYPE:
10237 case UNION_TYPE:
10238 case QUAL_UNION_TYPE:
10239 case ENUMERAL_TYPE:
10240 case FUNCTION_TYPE:
10241 case METHOD_TYPE:
10242 case POINTER_TYPE:
10243 case REFERENCE_TYPE:
10244 case NULLPTR_TYPE:
10245 case OFFSET_TYPE:
10246 case LANG_TYPE:
10247 case VECTOR_TYPE:
10248 return 0;
10250 default:
10251 gcc_unreachable ();
10254 return 0;
10257 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10258 node, return the size in bits for the type if it is a constant, or else
10259 return the alignment for the type if the type's size is not constant, or
10260 else return BITS_PER_WORD if the type actually turns out to be an
10261 ERROR_MARK node. */
10263 static inline unsigned HOST_WIDE_INT
10264 simple_type_size_in_bits (const_tree type)
10266 if (TREE_CODE (type) == ERROR_MARK)
10267 return BITS_PER_WORD;
10268 else if (TYPE_SIZE (type) == NULL_TREE)
10269 return 0;
10270 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10271 return tree_to_uhwi (TYPE_SIZE (type));
10272 else
10273 return TYPE_ALIGN (type);
10276 /* Similarly, but return a double_int instead of UHWI. */
10278 static inline double_int
10279 double_int_type_size_in_bits (const_tree type)
10281 if (TREE_CODE (type) == ERROR_MARK)
10282 return double_int::from_uhwi (BITS_PER_WORD);
10283 else if (TYPE_SIZE (type) == NULL_TREE)
10284 return double_int_zero;
10285 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10286 return tree_to_double_int (TYPE_SIZE (type));
10287 else
10288 return double_int::from_uhwi (TYPE_ALIGN (type));
10291 /* Given a pointer to a tree node for a subrange type, return a pointer
10292 to a DIE that describes the given type. */
10294 static dw_die_ref
10295 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10297 dw_die_ref subrange_die;
10298 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10300 if (context_die == NULL)
10301 context_die = comp_unit_die ();
10303 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10305 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10307 /* The size of the subrange type and its base type do not match,
10308 so we need to generate a size attribute for the subrange type. */
10309 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10312 if (low)
10313 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10314 if (high)
10315 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10317 return subrange_die;
10320 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10321 entry that chains various modifiers in front of the given type. */
10323 static dw_die_ref
10324 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10325 dw_die_ref context_die)
10327 enum tree_code code = TREE_CODE (type);
10328 dw_die_ref mod_type_die;
10329 dw_die_ref sub_die = NULL;
10330 tree item_type = NULL;
10331 tree qualified_type;
10332 tree name, low, high;
10333 dw_die_ref mod_scope;
10335 if (code == ERROR_MARK)
10336 return NULL;
10338 /* See if we already have the appropriately qualified variant of
10339 this type. */
10340 qualified_type
10341 = get_qualified_type (type,
10342 ((is_const_type ? TYPE_QUAL_CONST : 0)
10343 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10345 if (qualified_type == sizetype
10346 && TYPE_NAME (qualified_type)
10347 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10349 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10351 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10352 && TYPE_PRECISION (t)
10353 == TYPE_PRECISION (qualified_type)
10354 && TYPE_UNSIGNED (t)
10355 == TYPE_UNSIGNED (qualified_type));
10356 qualified_type = t;
10359 /* If we do, then we can just use its DIE, if it exists. */
10360 if (qualified_type)
10362 mod_type_die = lookup_type_die (qualified_type);
10363 if (mod_type_die)
10364 return mod_type_die;
10367 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10369 /* Handle C typedef types. */
10370 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10371 && !DECL_ARTIFICIAL (name))
10373 tree dtype = TREE_TYPE (name);
10375 if (qualified_type == dtype)
10377 /* For a named type, use the typedef. */
10378 gen_type_die (qualified_type, context_die);
10379 return lookup_type_die (qualified_type);
10381 else if (is_const_type < TYPE_READONLY (dtype)
10382 || is_volatile_type < TYPE_VOLATILE (dtype)
10383 || (is_const_type <= TYPE_READONLY (dtype)
10384 && is_volatile_type <= TYPE_VOLATILE (dtype)
10385 && DECL_ORIGINAL_TYPE (name) != type))
10386 /* cv-unqualified version of named type. Just use the unnamed
10387 type to which it refers. */
10388 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10389 is_const_type, is_volatile_type,
10390 context_die);
10391 /* Else cv-qualified version of named type; fall through. */
10394 mod_scope = scope_die_for (type, context_die);
10396 if (is_const_type
10397 /* If both is_const_type and is_volatile_type, prefer the path
10398 which leads to a qualified type. */
10399 && (!is_volatile_type
10400 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10401 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10403 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10404 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10406 else if (is_volatile_type)
10408 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10409 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10411 else if (code == POINTER_TYPE)
10413 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10414 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10415 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10416 item_type = TREE_TYPE (type);
10417 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10418 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10419 TYPE_ADDR_SPACE (item_type));
10421 else if (code == REFERENCE_TYPE)
10423 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10424 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10425 type);
10426 else
10427 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10428 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10429 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10430 item_type = TREE_TYPE (type);
10431 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10432 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10433 TYPE_ADDR_SPACE (item_type));
10435 else if (code == INTEGER_TYPE
10436 && TREE_TYPE (type) != NULL_TREE
10437 && subrange_type_for_debug_p (type, &low, &high))
10439 mod_type_die = subrange_type_die (type, low, high, context_die);
10440 item_type = TREE_TYPE (type);
10442 else if (is_base_type (type))
10443 mod_type_die = base_type_die (type);
10444 else
10446 gen_type_die (type, context_die);
10448 /* We have to get the type_main_variant here (and pass that to the
10449 `lookup_type_die' routine) because the ..._TYPE node we have
10450 might simply be a *copy* of some original type node (where the
10451 copy was created to help us keep track of typedef names) and
10452 that copy might have a different TYPE_UID from the original
10453 ..._TYPE node. */
10454 if (TREE_CODE (type) != VECTOR_TYPE)
10455 return lookup_type_die (type_main_variant (type));
10456 else
10457 /* Vectors have the debugging information in the type,
10458 not the main variant. */
10459 return lookup_type_die (type);
10462 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10463 don't output a DW_TAG_typedef, since there isn't one in the
10464 user's program; just attach a DW_AT_name to the type.
10465 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10466 if the base type already has the same name. */
10467 if (name
10468 && ((TREE_CODE (name) != TYPE_DECL
10469 && (qualified_type == TYPE_MAIN_VARIANT (type)
10470 || (!is_const_type && !is_volatile_type)))
10471 || (TREE_CODE (name) == TYPE_DECL
10472 && TREE_TYPE (name) == qualified_type
10473 && DECL_NAME (name))))
10475 if (TREE_CODE (name) == TYPE_DECL)
10476 /* Could just call add_name_and_src_coords_attributes here,
10477 but since this is a builtin type it doesn't have any
10478 useful source coordinates anyway. */
10479 name = DECL_NAME (name);
10480 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10482 /* This probably indicates a bug. */
10483 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10485 name = TYPE_NAME (type);
10486 if (name
10487 && TREE_CODE (name) == TYPE_DECL)
10488 name = DECL_NAME (name);
10489 add_name_attribute (mod_type_die,
10490 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10493 if (qualified_type)
10494 equate_type_number_to_die (qualified_type, mod_type_die);
10496 if (item_type)
10497 /* We must do this after the equate_type_number_to_die call, in case
10498 this is a recursive type. This ensures that the modified_type_die
10499 recursion will terminate even if the type is recursive. Recursive
10500 types are possible in Ada. */
10501 sub_die = modified_type_die (item_type,
10502 TYPE_READONLY (item_type),
10503 TYPE_VOLATILE (item_type),
10504 context_die);
10506 if (sub_die != NULL)
10507 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10509 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10510 if (TYPE_ARTIFICIAL (type))
10511 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10513 return mod_type_die;
10516 /* Generate DIEs for the generic parameters of T.
10517 T must be either a generic type or a generic function.
10518 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10520 static void
10521 gen_generic_params_dies (tree t)
10523 tree parms, args;
10524 int parms_num, i;
10525 dw_die_ref die = NULL;
10526 int non_default;
10528 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10529 return;
10531 if (TYPE_P (t))
10532 die = lookup_type_die (t);
10533 else if (DECL_P (t))
10534 die = lookup_decl_die (t);
10536 gcc_assert (die);
10538 parms = lang_hooks.get_innermost_generic_parms (t);
10539 if (!parms)
10540 /* T has no generic parameter. It means T is neither a generic type
10541 or function. End of story. */
10542 return;
10544 parms_num = TREE_VEC_LENGTH (parms);
10545 args = lang_hooks.get_innermost_generic_args (t);
10546 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10547 non_default = int_cst_value (TREE_CHAIN (args));
10548 else
10549 non_default = TREE_VEC_LENGTH (args);
10550 for (i = 0; i < parms_num; i++)
10552 tree parm, arg, arg_pack_elems;
10553 dw_die_ref parm_die;
10555 parm = TREE_VEC_ELT (parms, i);
10556 arg = TREE_VEC_ELT (args, i);
10557 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10558 gcc_assert (parm && TREE_VALUE (parm) && arg);
10560 if (parm && TREE_VALUE (parm) && arg)
10562 /* If PARM represents a template parameter pack,
10563 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10564 by DW_TAG_template_*_parameter DIEs for the argument
10565 pack elements of ARG. Note that ARG would then be
10566 an argument pack. */
10567 if (arg_pack_elems)
10568 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10569 arg_pack_elems,
10570 die);
10571 else
10572 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10573 true /* emit name */, die);
10574 if (i >= non_default)
10575 add_AT_flag (parm_die, DW_AT_default_value, 1);
10580 /* Create and return a DIE for PARM which should be
10581 the representation of a generic type parameter.
10582 For instance, in the C++ front end, PARM would be a template parameter.
10583 ARG is the argument to PARM.
10584 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10585 name of the PARM.
10586 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10587 as a child node. */
10589 static dw_die_ref
10590 generic_parameter_die (tree parm, tree arg,
10591 bool emit_name_p,
10592 dw_die_ref parent_die)
10594 dw_die_ref tmpl_die = NULL;
10595 const char *name = NULL;
10597 if (!parm || !DECL_NAME (parm) || !arg)
10598 return NULL;
10600 /* We support non-type generic parameters and arguments,
10601 type generic parameters and arguments, as well as
10602 generic generic parameters (a.k.a. template template parameters in C++)
10603 and arguments. */
10604 if (TREE_CODE (parm) == PARM_DECL)
10605 /* PARM is a nontype generic parameter */
10606 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10607 else if (TREE_CODE (parm) == TYPE_DECL)
10608 /* PARM is a type generic parameter. */
10609 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10610 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10611 /* PARM is a generic generic parameter.
10612 Its DIE is a GNU extension. It shall have a
10613 DW_AT_name attribute to represent the name of the template template
10614 parameter, and a DW_AT_GNU_template_name attribute to represent the
10615 name of the template template argument. */
10616 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10617 parent_die, parm);
10618 else
10619 gcc_unreachable ();
10621 if (tmpl_die)
10623 tree tmpl_type;
10625 /* If PARM is a generic parameter pack, it means we are
10626 emitting debug info for a template argument pack element.
10627 In other terms, ARG is a template argument pack element.
10628 In that case, we don't emit any DW_AT_name attribute for
10629 the die. */
10630 if (emit_name_p)
10632 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10633 gcc_assert (name);
10634 add_AT_string (tmpl_die, DW_AT_name, name);
10637 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10639 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10640 TMPL_DIE should have a child DW_AT_type attribute that is set
10641 to the type of the argument to PARM, which is ARG.
10642 If PARM is a type generic parameter, TMPL_DIE should have a
10643 child DW_AT_type that is set to ARG. */
10644 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10645 add_type_attribute (tmpl_die, tmpl_type, 0,
10646 TREE_THIS_VOLATILE (tmpl_type),
10647 parent_die);
10649 else
10651 /* So TMPL_DIE is a DIE representing a
10652 a generic generic template parameter, a.k.a template template
10653 parameter in C++ and arg is a template. */
10655 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10656 to the name of the argument. */
10657 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10658 if (name)
10659 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10662 if (TREE_CODE (parm) == PARM_DECL)
10663 /* So PARM is a non-type generic parameter.
10664 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10665 attribute of TMPL_DIE which value represents the value
10666 of ARG.
10667 We must be careful here:
10668 The value of ARG might reference some function decls.
10669 We might currently be emitting debug info for a generic
10670 type and types are emitted before function decls, we don't
10671 know if the function decls referenced by ARG will actually be
10672 emitted after cgraph computations.
10673 So must defer the generation of the DW_AT_const_value to
10674 after cgraph is ready. */
10675 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10678 return tmpl_die;
10681 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10682 PARM_PACK must be a template parameter pack. The returned DIE
10683 will be child DIE of PARENT_DIE. */
10685 static dw_die_ref
10686 template_parameter_pack_die (tree parm_pack,
10687 tree parm_pack_args,
10688 dw_die_ref parent_die)
10690 dw_die_ref die;
10691 int j;
10693 gcc_assert (parent_die && parm_pack);
10695 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10696 add_name_and_src_coords_attributes (die, parm_pack);
10697 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10698 generic_parameter_die (parm_pack,
10699 TREE_VEC_ELT (parm_pack_args, j),
10700 false /* Don't emit DW_AT_name */,
10701 die);
10702 return die;
10705 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10706 an enumerated type. */
10708 static inline int
10709 type_is_enum (const_tree type)
10711 return TREE_CODE (type) == ENUMERAL_TYPE;
10714 /* Return the DBX register number described by a given RTL node. */
10716 static unsigned int
10717 dbx_reg_number (const_rtx rtl)
10719 unsigned regno = REGNO (rtl);
10721 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10723 #ifdef LEAF_REG_REMAP
10724 if (crtl->uses_only_leaf_regs)
10726 int leaf_reg = LEAF_REG_REMAP (regno);
10727 if (leaf_reg != -1)
10728 regno = (unsigned) leaf_reg;
10730 #endif
10732 regno = DBX_REGISTER_NUMBER (regno);
10733 gcc_assert (regno != INVALID_REGNUM);
10734 return regno;
10737 /* Optionally add a DW_OP_piece term to a location description expression.
10738 DW_OP_piece is only added if the location description expression already
10739 doesn't end with DW_OP_piece. */
10741 static void
10742 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10744 dw_loc_descr_ref loc;
10746 if (*list_head != NULL)
10748 /* Find the end of the chain. */
10749 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10752 if (loc->dw_loc_opc != DW_OP_piece)
10753 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10757 /* Return a location descriptor that designates a machine register or
10758 zero if there is none. */
10760 static dw_loc_descr_ref
10761 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10763 rtx regs;
10765 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10766 return 0;
10768 /* We only use "frame base" when we're sure we're talking about the
10769 post-prologue local stack frame. We do this by *not* running
10770 register elimination until this point, and recognizing the special
10771 argument pointer and soft frame pointer rtx's.
10772 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10773 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10774 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10776 dw_loc_descr_ref result = NULL;
10778 if (dwarf_version >= 4 || !dwarf_strict)
10780 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10781 initialized);
10782 if (result)
10783 add_loc_descr (&result,
10784 new_loc_descr (DW_OP_stack_value, 0, 0));
10786 return result;
10789 regs = targetm.dwarf_register_span (rtl);
10791 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10792 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10793 else
10795 unsigned int dbx_regnum = dbx_reg_number (rtl);
10796 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10797 return 0;
10798 return one_reg_loc_descriptor (dbx_regnum, initialized);
10802 /* Return a location descriptor that designates a machine register for
10803 a given hard register number. */
10805 static dw_loc_descr_ref
10806 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10808 dw_loc_descr_ref reg_loc_descr;
10810 if (regno <= 31)
10811 reg_loc_descr
10812 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10813 else
10814 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10816 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10817 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10819 return reg_loc_descr;
10822 /* Given an RTL of a register, return a location descriptor that
10823 designates a value that spans more than one register. */
10825 static dw_loc_descr_ref
10826 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10827 enum var_init_status initialized)
10829 int size, i;
10830 dw_loc_descr_ref loc_result = NULL;
10832 /* Simple, contiguous registers. */
10833 if (regs == NULL_RTX)
10835 unsigned reg = REGNO (rtl);
10836 int nregs;
10838 #ifdef LEAF_REG_REMAP
10839 if (crtl->uses_only_leaf_regs)
10841 int leaf_reg = LEAF_REG_REMAP (reg);
10842 if (leaf_reg != -1)
10843 reg = (unsigned) leaf_reg;
10845 #endif
10847 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10848 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10850 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10852 loc_result = NULL;
10853 while (nregs--)
10855 dw_loc_descr_ref t;
10857 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10858 VAR_INIT_STATUS_INITIALIZED);
10859 add_loc_descr (&loc_result, t);
10860 add_loc_descr_op_piece (&loc_result, size);
10861 ++reg;
10863 return loc_result;
10866 /* Now onto stupid register sets in non contiguous locations. */
10868 gcc_assert (GET_CODE (regs) == PARALLEL);
10870 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10871 loc_result = NULL;
10873 for (i = 0; i < XVECLEN (regs, 0); ++i)
10875 dw_loc_descr_ref t;
10877 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10878 VAR_INIT_STATUS_INITIALIZED);
10879 add_loc_descr (&loc_result, t);
10880 add_loc_descr_op_piece (&loc_result, size);
10883 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10884 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10885 return loc_result;
10888 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10890 /* Return a location descriptor that designates a constant i,
10891 as a compound operation from constant (i >> shift), constant shift
10892 and DW_OP_shl. */
10894 static dw_loc_descr_ref
10895 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10897 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10898 add_loc_descr (&ret, int_loc_descriptor (shift));
10899 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10900 return ret;
10903 /* Return a location descriptor that designates a constant. */
10905 static dw_loc_descr_ref
10906 int_loc_descriptor (HOST_WIDE_INT i)
10908 enum dwarf_location_atom op;
10910 /* Pick the smallest representation of a constant, rather than just
10911 defaulting to the LEB encoding. */
10912 if (i >= 0)
10914 int clz = clz_hwi (i);
10915 int ctz = ctz_hwi (i);
10916 if (i <= 31)
10917 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10918 else if (i <= 0xff)
10919 op = DW_OP_const1u;
10920 else if (i <= 0xffff)
10921 op = DW_OP_const2u;
10922 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10923 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10924 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10925 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10926 while DW_OP_const4u is 5 bytes. */
10927 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10928 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10929 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10930 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10931 while DW_OP_const4u is 5 bytes. */
10932 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10933 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10934 op = DW_OP_const4u;
10935 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10936 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10937 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10938 while DW_OP_constu of constant >= 0x100000000 takes at least
10939 6 bytes. */
10940 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10941 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10942 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10943 >= HOST_BITS_PER_WIDE_INT)
10944 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10945 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10946 while DW_OP_constu takes in this case at least 6 bytes. */
10947 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10948 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10949 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10950 && size_of_uleb128 (i) > 6)
10951 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10952 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10953 else
10954 op = DW_OP_constu;
10956 else
10958 if (i >= -0x80)
10959 op = DW_OP_const1s;
10960 else if (i >= -0x8000)
10961 op = DW_OP_const2s;
10962 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10964 if (size_of_int_loc_descriptor (i) < 5)
10966 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10967 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10968 return ret;
10970 op = DW_OP_const4s;
10972 else
10974 if (size_of_int_loc_descriptor (i)
10975 < (unsigned long) 1 + size_of_sleb128 (i))
10977 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10978 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10979 return ret;
10981 op = DW_OP_consts;
10985 return new_loc_descr (op, i, 0);
10988 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10989 without actually allocating it. */
10991 static unsigned long
10992 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10994 return size_of_int_loc_descriptor (i >> shift)
10995 + size_of_int_loc_descriptor (shift)
10996 + 1;
10999 /* Return size_of_locs (int_loc_descriptor (i)) without
11000 actually allocating it. */
11002 static unsigned long
11003 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11005 unsigned long s;
11007 if (i >= 0)
11009 int clz, ctz;
11010 if (i <= 31)
11011 return 1;
11012 else if (i <= 0xff)
11013 return 2;
11014 else if (i <= 0xffff)
11015 return 3;
11016 clz = clz_hwi (i);
11017 ctz = ctz_hwi (i);
11018 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11019 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11020 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11021 - clz - 5);
11022 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11023 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11024 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11025 - clz - 8);
11026 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11027 return 5;
11028 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11029 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11030 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11031 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11032 - clz - 8);
11033 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11034 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11035 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11036 - clz - 16);
11037 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11038 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11039 && s > 6)
11040 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11041 - clz - 32);
11042 else
11043 return 1 + s;
11045 else
11047 if (i >= -0x80)
11048 return 2;
11049 else if (i >= -0x8000)
11050 return 3;
11051 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11053 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11055 s = size_of_int_loc_descriptor (-i) + 1;
11056 if (s < 5)
11057 return s;
11059 return 5;
11061 else
11063 unsigned long r = 1 + size_of_sleb128 (i);
11064 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11066 s = size_of_int_loc_descriptor (-i) + 1;
11067 if (s < r)
11068 return s;
11070 return r;
11075 /* Return loc description representing "address" of integer value.
11076 This can appear only as toplevel expression. */
11078 static dw_loc_descr_ref
11079 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11081 int litsize;
11082 dw_loc_descr_ref loc_result = NULL;
11084 if (!(dwarf_version >= 4 || !dwarf_strict))
11085 return NULL;
11087 litsize = size_of_int_loc_descriptor (i);
11088 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11089 is more compact. For DW_OP_stack_value we need:
11090 litsize + 1 (DW_OP_stack_value)
11091 and for DW_OP_implicit_value:
11092 1 (DW_OP_implicit_value) + 1 (length) + size. */
11093 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11095 loc_result = int_loc_descriptor (i);
11096 add_loc_descr (&loc_result,
11097 new_loc_descr (DW_OP_stack_value, 0, 0));
11098 return loc_result;
11101 loc_result = new_loc_descr (DW_OP_implicit_value,
11102 size, 0);
11103 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11104 loc_result->dw_loc_oprnd2.v.val_int = i;
11105 return loc_result;
11108 /* Return a location descriptor that designates a base+offset location. */
11110 static dw_loc_descr_ref
11111 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11112 enum var_init_status initialized)
11114 unsigned int regno;
11115 dw_loc_descr_ref result;
11116 dw_fde_ref fde = cfun->fde;
11118 /* We only use "frame base" when we're sure we're talking about the
11119 post-prologue local stack frame. We do this by *not* running
11120 register elimination until this point, and recognizing the special
11121 argument pointer and soft frame pointer rtx's. */
11122 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11124 rtx elim = (ira_use_lra_p
11125 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11126 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11128 if (elim != reg)
11130 if (GET_CODE (elim) == PLUS)
11132 offset += INTVAL (XEXP (elim, 1));
11133 elim = XEXP (elim, 0);
11135 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11136 && (elim == hard_frame_pointer_rtx
11137 || elim == stack_pointer_rtx))
11138 || elim == (frame_pointer_needed
11139 ? hard_frame_pointer_rtx
11140 : stack_pointer_rtx));
11142 /* If drap register is used to align stack, use frame
11143 pointer + offset to access stack variables. If stack
11144 is aligned without drap, use stack pointer + offset to
11145 access stack variables. */
11146 if (crtl->stack_realign_tried
11147 && reg == frame_pointer_rtx)
11149 int base_reg
11150 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11151 ? HARD_FRAME_POINTER_REGNUM
11152 : REGNO (elim));
11153 return new_reg_loc_descr (base_reg, offset);
11156 gcc_assert (frame_pointer_fb_offset_valid);
11157 offset += frame_pointer_fb_offset;
11158 return new_loc_descr (DW_OP_fbreg, offset, 0);
11162 regno = REGNO (reg);
11163 #ifdef LEAF_REG_REMAP
11164 if (crtl->uses_only_leaf_regs)
11166 int leaf_reg = LEAF_REG_REMAP (regno);
11167 if (leaf_reg != -1)
11168 regno = (unsigned) leaf_reg;
11170 #endif
11171 regno = DWARF_FRAME_REGNUM (regno);
11173 if (!optimize && fde
11174 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11176 /* Use cfa+offset to represent the location of arguments passed
11177 on the stack when drap is used to align stack.
11178 Only do this when not optimizing, for optimized code var-tracking
11179 is supposed to track where the arguments live and the register
11180 used as vdrap or drap in some spot might be used for something
11181 else in other part of the routine. */
11182 return new_loc_descr (DW_OP_fbreg, offset, 0);
11185 if (regno <= 31)
11186 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11187 offset, 0);
11188 else
11189 result = new_loc_descr (DW_OP_bregx, regno, offset);
11191 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11192 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11194 return result;
11197 /* Return true if this RTL expression describes a base+offset calculation. */
11199 static inline int
11200 is_based_loc (const_rtx rtl)
11202 return (GET_CODE (rtl) == PLUS
11203 && ((REG_P (XEXP (rtl, 0))
11204 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11205 && CONST_INT_P (XEXP (rtl, 1)))));
11208 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11209 failed. */
11211 static dw_loc_descr_ref
11212 tls_mem_loc_descriptor (rtx mem)
11214 tree base;
11215 dw_loc_descr_ref loc_result;
11217 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11218 return NULL;
11220 base = get_base_address (MEM_EXPR (mem));
11221 if (base == NULL
11222 || TREE_CODE (base) != VAR_DECL
11223 || !DECL_THREAD_LOCAL_P (base))
11224 return NULL;
11226 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11227 if (loc_result == NULL)
11228 return NULL;
11230 if (MEM_OFFSET (mem))
11231 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11233 return loc_result;
11236 /* Output debug info about reason why we failed to expand expression as dwarf
11237 expression. */
11239 static void
11240 expansion_failed (tree expr, rtx rtl, char const *reason)
11242 if (dump_file && (dump_flags & TDF_DETAILS))
11244 fprintf (dump_file, "Failed to expand as dwarf: ");
11245 if (expr)
11246 print_generic_expr (dump_file, expr, dump_flags);
11247 if (rtl)
11249 fprintf (dump_file, "\n");
11250 print_rtl (dump_file, rtl);
11252 fprintf (dump_file, "\nReason: %s\n", reason);
11256 /* Helper function for const_ok_for_output, called either directly
11257 or via for_each_rtx. */
11259 static int
11260 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11262 rtx rtl = *rtlp;
11264 if (GET_CODE (rtl) == UNSPEC)
11266 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11267 we can't express it in the debug info. */
11268 #ifdef ENABLE_CHECKING
11269 /* Don't complain about TLS UNSPECs, those are just too hard to
11270 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11271 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11272 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11273 if (XVECLEN (rtl, 0) == 0
11274 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11275 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11276 inform (current_function_decl
11277 ? DECL_SOURCE_LOCATION (current_function_decl)
11278 : UNKNOWN_LOCATION,
11279 #if NUM_UNSPEC_VALUES > 0
11280 "non-delegitimized UNSPEC %s (%d) found in variable location",
11281 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11282 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11283 XINT (rtl, 1));
11284 #else
11285 "non-delegitimized UNSPEC %d found in variable location",
11286 XINT (rtl, 1));
11287 #endif
11288 #endif
11289 expansion_failed (NULL_TREE, rtl,
11290 "UNSPEC hasn't been delegitimized.\n");
11291 return 1;
11294 if (targetm.const_not_ok_for_debug_p (rtl))
11296 expansion_failed (NULL_TREE, rtl,
11297 "Expression rejected for debug by the backend.\n");
11298 return 1;
11301 if (GET_CODE (rtl) != SYMBOL_REF)
11302 return 0;
11304 if (CONSTANT_POOL_ADDRESS_P (rtl))
11306 bool marked;
11307 get_pool_constant_mark (rtl, &marked);
11308 /* If all references to this pool constant were optimized away,
11309 it was not output and thus we can't represent it. */
11310 if (!marked)
11312 expansion_failed (NULL_TREE, rtl,
11313 "Constant was removed from constant pool.\n");
11314 return 1;
11318 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11319 return 1;
11321 /* Avoid references to external symbols in debug info, on several targets
11322 the linker might even refuse to link when linking a shared library,
11323 and in many other cases the relocations for .debug_info/.debug_loc are
11324 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11325 to be defined within the same shared library or executable are fine. */
11326 if (SYMBOL_REF_EXTERNAL_P (rtl))
11328 tree decl = SYMBOL_REF_DECL (rtl);
11330 if (decl == NULL || !targetm.binds_local_p (decl))
11332 expansion_failed (NULL_TREE, rtl,
11333 "Symbol not defined in current TU.\n");
11334 return 1;
11338 return 0;
11341 /* Return true if constant RTL can be emitted in DW_OP_addr or
11342 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11343 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11345 static bool
11346 const_ok_for_output (rtx rtl)
11348 if (GET_CODE (rtl) == SYMBOL_REF)
11349 return const_ok_for_output_1 (&rtl, NULL) == 0;
11351 if (GET_CODE (rtl) == CONST)
11352 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11354 return true;
11357 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11358 if possible, NULL otherwise. */
11360 static dw_die_ref
11361 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11363 dw_die_ref type_die;
11364 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11366 if (type == NULL)
11367 return NULL;
11368 switch (TREE_CODE (type))
11370 case INTEGER_TYPE:
11371 case REAL_TYPE:
11372 break;
11373 default:
11374 return NULL;
11376 type_die = lookup_type_die (type);
11377 if (!type_die)
11378 type_die = modified_type_die (type, false, false, comp_unit_die ());
11379 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11380 return NULL;
11381 return type_die;
11384 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11385 type matching MODE, or, if MODE is narrower than or as wide as
11386 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11387 possible. */
11389 static dw_loc_descr_ref
11390 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11392 enum machine_mode outer_mode = mode;
11393 dw_die_ref type_die;
11394 dw_loc_descr_ref cvt;
11396 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11398 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11399 return op;
11401 type_die = base_type_for_mode (outer_mode, 1);
11402 if (type_die == NULL)
11403 return NULL;
11404 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11405 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11406 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11407 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11408 add_loc_descr (&op, cvt);
11409 return op;
11412 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11414 static dw_loc_descr_ref
11415 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11416 dw_loc_descr_ref op1)
11418 dw_loc_descr_ref ret = op0;
11419 add_loc_descr (&ret, op1);
11420 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11421 if (STORE_FLAG_VALUE != 1)
11423 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11424 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11426 return ret;
11429 /* Return location descriptor for signed comparison OP RTL. */
11431 static dw_loc_descr_ref
11432 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11433 enum machine_mode mem_mode)
11435 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11436 dw_loc_descr_ref op0, op1;
11437 int shift;
11439 if (op_mode == VOIDmode)
11440 op_mode = GET_MODE (XEXP (rtl, 1));
11441 if (op_mode == VOIDmode)
11442 return NULL;
11444 if (dwarf_strict
11445 && (GET_MODE_CLASS (op_mode) != MODE_INT
11446 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11447 return NULL;
11449 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11450 VAR_INIT_STATUS_INITIALIZED);
11451 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11452 VAR_INIT_STATUS_INITIALIZED);
11454 if (op0 == NULL || op1 == NULL)
11455 return NULL;
11457 if (GET_MODE_CLASS (op_mode) != MODE_INT
11458 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11459 return compare_loc_descriptor (op, op0, op1);
11461 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11463 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11464 dw_loc_descr_ref cvt;
11466 if (type_die == NULL)
11467 return NULL;
11468 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11469 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11470 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11471 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11472 add_loc_descr (&op0, cvt);
11473 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11474 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11475 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11476 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11477 add_loc_descr (&op1, cvt);
11478 return compare_loc_descriptor (op, op0, op1);
11481 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11482 /* For eq/ne, if the operands are known to be zero-extended,
11483 there is no need to do the fancy shifting up. */
11484 if (op == DW_OP_eq || op == DW_OP_ne)
11486 dw_loc_descr_ref last0, last1;
11487 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11489 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11491 /* deref_size zero extends, and for constants we can check
11492 whether they are zero extended or not. */
11493 if (((last0->dw_loc_opc == DW_OP_deref_size
11494 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11495 || (CONST_INT_P (XEXP (rtl, 0))
11496 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11497 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11498 && ((last1->dw_loc_opc == DW_OP_deref_size
11499 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11500 || (CONST_INT_P (XEXP (rtl, 1))
11501 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11502 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11503 return compare_loc_descriptor (op, op0, op1);
11505 /* EQ/NE comparison against constant in narrower type than
11506 DWARF2_ADDR_SIZE can be performed either as
11507 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11508 DW_OP_{eq,ne}
11510 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11511 DW_OP_{eq,ne}. Pick whatever is shorter. */
11512 if (CONST_INT_P (XEXP (rtl, 1))
11513 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11514 && (size_of_int_loc_descriptor (shift) + 1
11515 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11516 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11517 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11518 & GET_MODE_MASK (op_mode))))
11520 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11521 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11522 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11523 & GET_MODE_MASK (op_mode));
11524 return compare_loc_descriptor (op, op0, op1);
11527 add_loc_descr (&op0, int_loc_descriptor (shift));
11528 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11529 if (CONST_INT_P (XEXP (rtl, 1)))
11530 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11531 else
11533 add_loc_descr (&op1, int_loc_descriptor (shift));
11534 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11536 return compare_loc_descriptor (op, op0, op1);
11539 /* Return location descriptor for unsigned comparison OP RTL. */
11541 static dw_loc_descr_ref
11542 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11543 enum machine_mode mem_mode)
11545 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11546 dw_loc_descr_ref op0, op1;
11548 if (op_mode == VOIDmode)
11549 op_mode = GET_MODE (XEXP (rtl, 1));
11550 if (op_mode == VOIDmode)
11551 return NULL;
11552 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11553 return NULL;
11555 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11556 return NULL;
11558 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11559 VAR_INIT_STATUS_INITIALIZED);
11560 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11561 VAR_INIT_STATUS_INITIALIZED);
11563 if (op0 == NULL || op1 == NULL)
11564 return NULL;
11566 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11568 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11569 dw_loc_descr_ref last0, last1;
11570 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11572 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11574 if (CONST_INT_P (XEXP (rtl, 0)))
11575 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11576 /* deref_size zero extends, so no need to mask it again. */
11577 else if (last0->dw_loc_opc != DW_OP_deref_size
11578 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11580 add_loc_descr (&op0, int_loc_descriptor (mask));
11581 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11583 if (CONST_INT_P (XEXP (rtl, 1)))
11584 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11585 /* deref_size zero extends, so no need to mask it again. */
11586 else if (last1->dw_loc_opc != DW_OP_deref_size
11587 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11589 add_loc_descr (&op1, int_loc_descriptor (mask));
11590 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11593 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11595 HOST_WIDE_INT bias = 1;
11596 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11597 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11598 if (CONST_INT_P (XEXP (rtl, 1)))
11599 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11600 + INTVAL (XEXP (rtl, 1)));
11601 else
11602 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11603 bias, 0));
11605 return compare_loc_descriptor (op, op0, op1);
11608 /* Return location descriptor for {U,S}{MIN,MAX}. */
11610 static dw_loc_descr_ref
11611 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11612 enum machine_mode mem_mode)
11614 enum dwarf_location_atom op;
11615 dw_loc_descr_ref op0, op1, ret;
11616 dw_loc_descr_ref bra_node, drop_node;
11618 if (dwarf_strict
11619 && (GET_MODE_CLASS (mode) != MODE_INT
11620 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11621 return NULL;
11623 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11624 VAR_INIT_STATUS_INITIALIZED);
11625 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11626 VAR_INIT_STATUS_INITIALIZED);
11628 if (op0 == NULL || op1 == NULL)
11629 return NULL;
11631 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11632 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11633 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11634 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11636 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11638 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11639 add_loc_descr (&op0, int_loc_descriptor (mask));
11640 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11641 add_loc_descr (&op1, int_loc_descriptor (mask));
11642 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11644 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11646 HOST_WIDE_INT bias = 1;
11647 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11648 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11649 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11652 else if (GET_MODE_CLASS (mode) == MODE_INT
11653 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11655 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11656 add_loc_descr (&op0, int_loc_descriptor (shift));
11657 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11658 add_loc_descr (&op1, int_loc_descriptor (shift));
11659 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11661 else if (GET_MODE_CLASS (mode) == MODE_INT
11662 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11664 dw_die_ref type_die = base_type_for_mode (mode, 0);
11665 dw_loc_descr_ref cvt;
11666 if (type_die == NULL)
11667 return NULL;
11668 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11669 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11670 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11671 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11672 add_loc_descr (&op0, cvt);
11673 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11674 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11675 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11676 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11677 add_loc_descr (&op1, cvt);
11680 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11681 op = DW_OP_lt;
11682 else
11683 op = DW_OP_gt;
11684 ret = op0;
11685 add_loc_descr (&ret, op1);
11686 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11687 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11688 add_loc_descr (&ret, bra_node);
11689 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11690 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11691 add_loc_descr (&ret, drop_node);
11692 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11693 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11694 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11695 && GET_MODE_CLASS (mode) == MODE_INT
11696 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11697 ret = convert_descriptor_to_mode (mode, ret);
11698 return ret;
11701 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11702 but after converting arguments to type_die, afterwards
11703 convert back to unsigned. */
11705 static dw_loc_descr_ref
11706 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11707 enum machine_mode mode, enum machine_mode mem_mode)
11709 dw_loc_descr_ref cvt, op0, op1;
11711 if (type_die == NULL)
11712 return NULL;
11713 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11714 VAR_INIT_STATUS_INITIALIZED);
11715 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11716 VAR_INIT_STATUS_INITIALIZED);
11717 if (op0 == NULL || op1 == NULL)
11718 return NULL;
11719 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11720 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11721 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11722 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11723 add_loc_descr (&op0, cvt);
11724 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11725 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11726 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11727 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11728 add_loc_descr (&op1, cvt);
11729 add_loc_descr (&op0, op1);
11730 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11731 return convert_descriptor_to_mode (mode, op0);
11734 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11735 const0 is DW_OP_lit0 or corresponding typed constant,
11736 const1 is DW_OP_lit1 or corresponding typed constant
11737 and constMSB is constant with just the MSB bit set
11738 for the mode):
11739 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11740 L1: const0 DW_OP_swap
11741 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11742 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11743 L3: DW_OP_drop
11744 L4: DW_OP_nop
11746 CTZ is similar:
11747 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11748 L1: const0 DW_OP_swap
11749 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11750 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11751 L3: DW_OP_drop
11752 L4: DW_OP_nop
11754 FFS is similar:
11755 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11756 L1: const1 DW_OP_swap
11757 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11758 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11759 L3: DW_OP_drop
11760 L4: DW_OP_nop */
11762 static dw_loc_descr_ref
11763 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11764 enum machine_mode mem_mode)
11766 dw_loc_descr_ref op0, ret, tmp;
11767 HOST_WIDE_INT valv;
11768 dw_loc_descr_ref l1jump, l1label;
11769 dw_loc_descr_ref l2jump, l2label;
11770 dw_loc_descr_ref l3jump, l3label;
11771 dw_loc_descr_ref l4jump, l4label;
11772 rtx msb;
11774 if (GET_MODE_CLASS (mode) != MODE_INT
11775 || GET_MODE (XEXP (rtl, 0)) != mode
11776 || (GET_CODE (rtl) == CLZ
11777 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11778 return NULL;
11780 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11781 VAR_INIT_STATUS_INITIALIZED);
11782 if (op0 == NULL)
11783 return NULL;
11784 ret = op0;
11785 if (GET_CODE (rtl) == CLZ)
11787 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11788 valv = GET_MODE_BITSIZE (mode);
11790 else if (GET_CODE (rtl) == FFS)
11791 valv = 0;
11792 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11793 valv = GET_MODE_BITSIZE (mode);
11794 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11795 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11796 add_loc_descr (&ret, l1jump);
11797 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11798 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11799 VAR_INIT_STATUS_INITIALIZED);
11800 if (tmp == NULL)
11801 return NULL;
11802 add_loc_descr (&ret, tmp);
11803 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11804 add_loc_descr (&ret, l4jump);
11805 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11806 ? const1_rtx : const0_rtx,
11807 mode, mem_mode,
11808 VAR_INIT_STATUS_INITIALIZED);
11809 if (l1label == NULL)
11810 return NULL;
11811 add_loc_descr (&ret, l1label);
11812 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11813 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11814 add_loc_descr (&ret, l2label);
11815 if (GET_CODE (rtl) != CLZ)
11816 msb = const1_rtx;
11817 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11818 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11819 << (GET_MODE_BITSIZE (mode) - 1));
11820 else
11821 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11822 << (GET_MODE_BITSIZE (mode)
11823 - HOST_BITS_PER_WIDE_INT - 1), mode);
11824 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11825 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11826 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11827 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11828 else
11829 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11830 VAR_INIT_STATUS_INITIALIZED);
11831 if (tmp == NULL)
11832 return NULL;
11833 add_loc_descr (&ret, tmp);
11834 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11835 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11836 add_loc_descr (&ret, l3jump);
11837 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11838 VAR_INIT_STATUS_INITIALIZED);
11839 if (tmp == NULL)
11840 return NULL;
11841 add_loc_descr (&ret, tmp);
11842 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11843 ? DW_OP_shl : DW_OP_shr, 0, 0));
11844 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11845 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11846 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11847 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11848 add_loc_descr (&ret, l2jump);
11849 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11850 add_loc_descr (&ret, l3label);
11851 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11852 add_loc_descr (&ret, l4label);
11853 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11854 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11855 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11856 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11857 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11858 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11859 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11860 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11861 return ret;
11864 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11865 const1 is DW_OP_lit1 or corresponding typed constant):
11866 const0 DW_OP_swap
11867 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11868 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11869 L2: DW_OP_drop
11871 PARITY is similar:
11872 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11873 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11874 L2: DW_OP_drop */
11876 static dw_loc_descr_ref
11877 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11878 enum machine_mode mem_mode)
11880 dw_loc_descr_ref op0, ret, tmp;
11881 dw_loc_descr_ref l1jump, l1label;
11882 dw_loc_descr_ref l2jump, l2label;
11884 if (GET_MODE_CLASS (mode) != MODE_INT
11885 || GET_MODE (XEXP (rtl, 0)) != mode)
11886 return NULL;
11888 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11889 VAR_INIT_STATUS_INITIALIZED);
11890 if (op0 == NULL)
11891 return NULL;
11892 ret = op0;
11893 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11894 VAR_INIT_STATUS_INITIALIZED);
11895 if (tmp == NULL)
11896 return NULL;
11897 add_loc_descr (&ret, tmp);
11898 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11899 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11900 add_loc_descr (&ret, l1label);
11901 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11902 add_loc_descr (&ret, l2jump);
11903 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11904 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11905 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11906 VAR_INIT_STATUS_INITIALIZED);
11907 if (tmp == NULL)
11908 return NULL;
11909 add_loc_descr (&ret, tmp);
11910 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11911 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11912 ? DW_OP_plus : DW_OP_xor, 0, 0));
11913 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11914 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11915 VAR_INIT_STATUS_INITIALIZED);
11916 add_loc_descr (&ret, tmp);
11917 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11918 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11919 add_loc_descr (&ret, l1jump);
11920 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11921 add_loc_descr (&ret, l2label);
11922 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11923 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11924 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11925 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11926 return ret;
11929 /* BSWAP (constS is initial shift count, either 56 or 24):
11930 constS const0
11931 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11932 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11933 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11934 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11935 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11937 static dw_loc_descr_ref
11938 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11939 enum machine_mode mem_mode)
11941 dw_loc_descr_ref op0, ret, tmp;
11942 dw_loc_descr_ref l1jump, l1label;
11943 dw_loc_descr_ref l2jump, l2label;
11945 if (GET_MODE_CLASS (mode) != MODE_INT
11946 || BITS_PER_UNIT != 8
11947 || (GET_MODE_BITSIZE (mode) != 32
11948 && GET_MODE_BITSIZE (mode) != 64))
11949 return NULL;
11951 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11952 VAR_INIT_STATUS_INITIALIZED);
11953 if (op0 == NULL)
11954 return NULL;
11956 ret = op0;
11957 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11958 mode, mem_mode,
11959 VAR_INIT_STATUS_INITIALIZED);
11960 if (tmp == NULL)
11961 return NULL;
11962 add_loc_descr (&ret, tmp);
11963 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11964 VAR_INIT_STATUS_INITIALIZED);
11965 if (tmp == NULL)
11966 return NULL;
11967 add_loc_descr (&ret, tmp);
11968 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11969 add_loc_descr (&ret, l1label);
11970 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11971 mode, mem_mode,
11972 VAR_INIT_STATUS_INITIALIZED);
11973 add_loc_descr (&ret, tmp);
11974 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11975 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11976 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11977 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11978 VAR_INIT_STATUS_INITIALIZED);
11979 if (tmp == NULL)
11980 return NULL;
11981 add_loc_descr (&ret, tmp);
11982 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11983 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11984 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11985 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11986 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11987 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11988 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11989 VAR_INIT_STATUS_INITIALIZED);
11990 add_loc_descr (&ret, tmp);
11991 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11992 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11993 add_loc_descr (&ret, l2jump);
11994 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11995 VAR_INIT_STATUS_INITIALIZED);
11996 add_loc_descr (&ret, tmp);
11997 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11998 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11999 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12000 add_loc_descr (&ret, l1jump);
12001 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12002 add_loc_descr (&ret, l2label);
12003 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12004 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12005 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12006 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12007 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12008 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12009 return ret;
12012 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12013 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12014 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12015 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12017 ROTATERT is similar:
12018 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12019 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12020 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12022 static dw_loc_descr_ref
12023 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12024 enum machine_mode mem_mode)
12026 rtx rtlop1 = XEXP (rtl, 1);
12027 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12028 int i;
12030 if (GET_MODE_CLASS (mode) != MODE_INT)
12031 return NULL;
12033 if (GET_MODE (rtlop1) != VOIDmode
12034 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12035 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12036 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12037 VAR_INIT_STATUS_INITIALIZED);
12038 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12039 VAR_INIT_STATUS_INITIALIZED);
12040 if (op0 == NULL || op1 == NULL)
12041 return NULL;
12042 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12043 for (i = 0; i < 2; i++)
12045 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12046 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12047 mode, mem_mode,
12048 VAR_INIT_STATUS_INITIALIZED);
12049 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12050 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12051 ? DW_OP_const4u
12052 : HOST_BITS_PER_WIDE_INT == 64
12053 ? DW_OP_const8u : DW_OP_constu,
12054 GET_MODE_MASK (mode), 0);
12055 else
12056 mask[i] = NULL;
12057 if (mask[i] == NULL)
12058 return NULL;
12059 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12061 ret = op0;
12062 add_loc_descr (&ret, op1);
12063 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12064 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12065 if (GET_CODE (rtl) == ROTATERT)
12067 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12068 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12069 GET_MODE_BITSIZE (mode), 0));
12071 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12072 if (mask[0] != NULL)
12073 add_loc_descr (&ret, mask[0]);
12074 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12075 if (mask[1] != NULL)
12077 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12078 add_loc_descr (&ret, mask[1]);
12079 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12081 if (GET_CODE (rtl) == ROTATE)
12083 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12084 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12085 GET_MODE_BITSIZE (mode), 0));
12087 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12088 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12089 return ret;
12092 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12093 for DEBUG_PARAMETER_REF RTL. */
12095 static dw_loc_descr_ref
12096 parameter_ref_descriptor (rtx rtl)
12098 dw_loc_descr_ref ret;
12099 dw_die_ref ref;
12101 if (dwarf_strict)
12102 return NULL;
12103 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12104 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12105 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12106 if (ref)
12108 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12109 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12110 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12112 else
12114 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12115 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12117 return ret;
12120 /* The following routine converts the RTL for a variable or parameter
12121 (resident in memory) into an equivalent Dwarf representation of a
12122 mechanism for getting the address of that same variable onto the top of a
12123 hypothetical "address evaluation" stack.
12125 When creating memory location descriptors, we are effectively transforming
12126 the RTL for a memory-resident object into its Dwarf postfix expression
12127 equivalent. This routine recursively descends an RTL tree, turning
12128 it into Dwarf postfix code as it goes.
12130 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12132 MEM_MODE is the mode of the memory reference, needed to handle some
12133 autoincrement addressing modes.
12135 Return 0 if we can't represent the location. */
12137 dw_loc_descr_ref
12138 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12139 enum machine_mode mem_mode,
12140 enum var_init_status initialized)
12142 dw_loc_descr_ref mem_loc_result = NULL;
12143 enum dwarf_location_atom op;
12144 dw_loc_descr_ref op0, op1;
12145 rtx inner = NULL_RTX;
12147 if (mode == VOIDmode)
12148 mode = GET_MODE (rtl);
12150 /* Note that for a dynamically sized array, the location we will generate a
12151 description of here will be the lowest numbered location which is
12152 actually within the array. That's *not* necessarily the same as the
12153 zeroth element of the array. */
12155 rtl = targetm.delegitimize_address (rtl);
12157 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12158 return NULL;
12160 switch (GET_CODE (rtl))
12162 case POST_INC:
12163 case POST_DEC:
12164 case POST_MODIFY:
12165 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12167 case SUBREG:
12168 /* The case of a subreg may arise when we have a local (register)
12169 variable or a formal (register) parameter which doesn't quite fill
12170 up an entire register. For now, just assume that it is
12171 legitimate to make the Dwarf info refer to the whole register which
12172 contains the given subreg. */
12173 if (!subreg_lowpart_p (rtl))
12174 break;
12175 inner = SUBREG_REG (rtl);
12176 case TRUNCATE:
12177 if (inner == NULL_RTX)
12178 inner = XEXP (rtl, 0);
12179 if (GET_MODE_CLASS (mode) == MODE_INT
12180 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12181 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12182 #ifdef POINTERS_EXTEND_UNSIGNED
12183 || (mode == Pmode && mem_mode != VOIDmode)
12184 #endif
12186 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12188 mem_loc_result = mem_loc_descriptor (inner,
12189 GET_MODE (inner),
12190 mem_mode, initialized);
12191 break;
12193 if (dwarf_strict)
12194 break;
12195 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12196 break;
12197 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12198 && (GET_MODE_CLASS (mode) != MODE_INT
12199 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12200 break;
12201 else
12203 dw_die_ref type_die;
12204 dw_loc_descr_ref cvt;
12206 mem_loc_result = mem_loc_descriptor (inner,
12207 GET_MODE (inner),
12208 mem_mode, initialized);
12209 if (mem_loc_result == NULL)
12210 break;
12211 type_die = base_type_for_mode (mode,
12212 GET_MODE_CLASS (mode) == MODE_INT);
12213 if (type_die == NULL)
12215 mem_loc_result = NULL;
12216 break;
12218 if (GET_MODE_SIZE (mode)
12219 != GET_MODE_SIZE (GET_MODE (inner)))
12220 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12221 else
12222 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12223 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12224 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12225 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12226 add_loc_descr (&mem_loc_result, cvt);
12228 break;
12230 case REG:
12231 if (GET_MODE_CLASS (mode) != MODE_INT
12232 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12233 && rtl != arg_pointer_rtx
12234 && rtl != frame_pointer_rtx
12235 #ifdef POINTERS_EXTEND_UNSIGNED
12236 && (mode != Pmode || mem_mode == VOIDmode)
12237 #endif
12240 dw_die_ref type_die;
12241 unsigned int dbx_regnum;
12243 if (dwarf_strict)
12244 break;
12245 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12246 break;
12247 type_die = base_type_for_mode (mode,
12248 GET_MODE_CLASS (mode) == MODE_INT);
12249 if (type_die == NULL)
12250 break;
12252 dbx_regnum = dbx_reg_number (rtl);
12253 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12254 break;
12255 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12256 dbx_regnum, 0);
12257 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12258 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12259 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12260 break;
12262 /* Whenever a register number forms a part of the description of the
12263 method for calculating the (dynamic) address of a memory resident
12264 object, DWARF rules require the register number be referred to as
12265 a "base register". This distinction is not based in any way upon
12266 what category of register the hardware believes the given register
12267 belongs to. This is strictly DWARF terminology we're dealing with
12268 here. Note that in cases where the location of a memory-resident
12269 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12270 OP_CONST (0)) the actual DWARF location descriptor that we generate
12271 may just be OP_BASEREG (basereg). This may look deceptively like
12272 the object in question was allocated to a register (rather than in
12273 memory) so DWARF consumers need to be aware of the subtle
12274 distinction between OP_REG and OP_BASEREG. */
12275 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12276 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12277 else if (stack_realign_drap
12278 && crtl->drap_reg
12279 && crtl->args.internal_arg_pointer == rtl
12280 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12282 /* If RTL is internal_arg_pointer, which has been optimized
12283 out, use DRAP instead. */
12284 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12285 VAR_INIT_STATUS_INITIALIZED);
12287 break;
12289 case SIGN_EXTEND:
12290 case ZERO_EXTEND:
12291 if (GET_MODE_CLASS (mode) != MODE_INT)
12292 break;
12293 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12294 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12295 if (op0 == 0)
12296 break;
12297 else if (GET_CODE (rtl) == ZERO_EXTEND
12298 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12299 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12300 < HOST_BITS_PER_WIDE_INT
12301 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12302 to expand zero extend as two shifts instead of
12303 masking. */
12304 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12306 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12307 mem_loc_result = op0;
12308 add_loc_descr (&mem_loc_result,
12309 int_loc_descriptor (GET_MODE_MASK (imode)));
12310 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12312 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12314 int shift = DWARF2_ADDR_SIZE
12315 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12316 shift *= BITS_PER_UNIT;
12317 if (GET_CODE (rtl) == SIGN_EXTEND)
12318 op = DW_OP_shra;
12319 else
12320 op = DW_OP_shr;
12321 mem_loc_result = op0;
12322 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12323 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12324 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12325 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12327 else if (!dwarf_strict)
12329 dw_die_ref type_die1, type_die2;
12330 dw_loc_descr_ref cvt;
12332 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12333 GET_CODE (rtl) == ZERO_EXTEND);
12334 if (type_die1 == NULL)
12335 break;
12336 type_die2 = base_type_for_mode (mode, 1);
12337 if (type_die2 == NULL)
12338 break;
12339 mem_loc_result = op0;
12340 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12341 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12342 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12343 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12344 add_loc_descr (&mem_loc_result, cvt);
12345 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12346 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12347 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12348 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12349 add_loc_descr (&mem_loc_result, cvt);
12351 break;
12353 case MEM:
12355 rtx new_rtl = avoid_constant_pool_reference (rtl);
12356 if (new_rtl != rtl)
12358 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12359 initialized);
12360 if (mem_loc_result != NULL)
12361 return mem_loc_result;
12364 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12365 get_address_mode (rtl), mode,
12366 VAR_INIT_STATUS_INITIALIZED);
12367 if (mem_loc_result == NULL)
12368 mem_loc_result = tls_mem_loc_descriptor (rtl);
12369 if (mem_loc_result != NULL)
12371 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12372 || GET_MODE_CLASS (mode) != MODE_INT)
12374 dw_die_ref type_die;
12375 dw_loc_descr_ref deref;
12377 if (dwarf_strict)
12378 return NULL;
12379 type_die
12380 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12381 if (type_die == NULL)
12382 return NULL;
12383 deref = new_loc_descr (DW_OP_GNU_deref_type,
12384 GET_MODE_SIZE (mode), 0);
12385 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12386 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12387 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12388 add_loc_descr (&mem_loc_result, deref);
12390 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12391 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12392 else
12393 add_loc_descr (&mem_loc_result,
12394 new_loc_descr (DW_OP_deref_size,
12395 GET_MODE_SIZE (mode), 0));
12397 break;
12399 case LO_SUM:
12400 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12402 case LABEL_REF:
12403 /* Some ports can transform a symbol ref into a label ref, because
12404 the symbol ref is too far away and has to be dumped into a constant
12405 pool. */
12406 case CONST:
12407 case SYMBOL_REF:
12408 if (GET_MODE_CLASS (mode) != MODE_INT
12409 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12410 #ifdef POINTERS_EXTEND_UNSIGNED
12411 && (mode != Pmode || mem_mode == VOIDmode)
12412 #endif
12414 break;
12415 if (GET_CODE (rtl) == SYMBOL_REF
12416 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12418 dw_loc_descr_ref temp;
12420 /* If this is not defined, we have no way to emit the data. */
12421 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12422 break;
12424 temp = new_addr_loc_descr (rtl, dtprel_true);
12426 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12427 add_loc_descr (&mem_loc_result, temp);
12429 break;
12432 if (!const_ok_for_output (rtl))
12433 break;
12435 symref:
12436 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12437 vec_safe_push (used_rtx_array, rtl);
12438 break;
12440 case CONCAT:
12441 case CONCATN:
12442 case VAR_LOCATION:
12443 case DEBUG_IMPLICIT_PTR:
12444 expansion_failed (NULL_TREE, rtl,
12445 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12446 return 0;
12448 case ENTRY_VALUE:
12449 if (dwarf_strict)
12450 return NULL;
12451 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12453 if (GET_MODE_CLASS (mode) != MODE_INT
12454 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12455 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12456 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12457 else
12459 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12460 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12461 return NULL;
12462 op0 = one_reg_loc_descriptor (dbx_regnum,
12463 VAR_INIT_STATUS_INITIALIZED);
12466 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12467 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12469 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12470 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12471 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12472 return NULL;
12474 else
12475 gcc_unreachable ();
12476 if (op0 == NULL)
12477 return NULL;
12478 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12479 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12480 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12481 break;
12483 case DEBUG_PARAMETER_REF:
12484 mem_loc_result = parameter_ref_descriptor (rtl);
12485 break;
12487 case PRE_MODIFY:
12488 /* Extract the PLUS expression nested inside and fall into
12489 PLUS code below. */
12490 rtl = XEXP (rtl, 1);
12491 goto plus;
12493 case PRE_INC:
12494 case PRE_DEC:
12495 /* Turn these into a PLUS expression and fall into the PLUS code
12496 below. */
12497 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12498 gen_int_mode (GET_CODE (rtl) == PRE_INC
12499 ? GET_MODE_UNIT_SIZE (mem_mode)
12500 : -GET_MODE_UNIT_SIZE (mem_mode),
12501 mode));
12503 /* ... fall through ... */
12505 case PLUS:
12506 plus:
12507 if (is_based_loc (rtl)
12508 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12509 || XEXP (rtl, 0) == arg_pointer_rtx
12510 || XEXP (rtl, 0) == frame_pointer_rtx)
12511 && GET_MODE_CLASS (mode) == MODE_INT)
12512 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12513 INTVAL (XEXP (rtl, 1)),
12514 VAR_INIT_STATUS_INITIALIZED);
12515 else
12517 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12518 VAR_INIT_STATUS_INITIALIZED);
12519 if (mem_loc_result == 0)
12520 break;
12522 if (CONST_INT_P (XEXP (rtl, 1))
12523 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12524 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12525 else
12527 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12528 VAR_INIT_STATUS_INITIALIZED);
12529 if (op1 == 0)
12530 break;
12531 add_loc_descr (&mem_loc_result, op1);
12532 add_loc_descr (&mem_loc_result,
12533 new_loc_descr (DW_OP_plus, 0, 0));
12536 break;
12538 /* If a pseudo-reg is optimized away, it is possible for it to
12539 be replaced with a MEM containing a multiply or shift. */
12540 case MINUS:
12541 op = DW_OP_minus;
12542 goto do_binop;
12544 case MULT:
12545 op = DW_OP_mul;
12546 goto do_binop;
12548 case DIV:
12549 if (!dwarf_strict
12550 && GET_MODE_CLASS (mode) == MODE_INT
12551 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12553 mem_loc_result = typed_binop (DW_OP_div, rtl,
12554 base_type_for_mode (mode, 0),
12555 mode, mem_mode);
12556 break;
12558 op = DW_OP_div;
12559 goto do_binop;
12561 case UMOD:
12562 op = DW_OP_mod;
12563 goto do_binop;
12565 case ASHIFT:
12566 op = DW_OP_shl;
12567 goto do_shift;
12569 case ASHIFTRT:
12570 op = DW_OP_shra;
12571 goto do_shift;
12573 case LSHIFTRT:
12574 op = DW_OP_shr;
12575 goto do_shift;
12577 do_shift:
12578 if (GET_MODE_CLASS (mode) != MODE_INT)
12579 break;
12580 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12581 VAR_INIT_STATUS_INITIALIZED);
12583 rtx rtlop1 = XEXP (rtl, 1);
12584 if (GET_MODE (rtlop1) != VOIDmode
12585 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12586 < GET_MODE_BITSIZE (mode))
12587 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12588 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12589 VAR_INIT_STATUS_INITIALIZED);
12592 if (op0 == 0 || op1 == 0)
12593 break;
12595 mem_loc_result = op0;
12596 add_loc_descr (&mem_loc_result, op1);
12597 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12598 break;
12600 case AND:
12601 op = DW_OP_and;
12602 goto do_binop;
12604 case IOR:
12605 op = DW_OP_or;
12606 goto do_binop;
12608 case XOR:
12609 op = DW_OP_xor;
12610 goto do_binop;
12612 do_binop:
12613 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12614 VAR_INIT_STATUS_INITIALIZED);
12615 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12616 VAR_INIT_STATUS_INITIALIZED);
12618 if (op0 == 0 || op1 == 0)
12619 break;
12621 mem_loc_result = op0;
12622 add_loc_descr (&mem_loc_result, op1);
12623 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12624 break;
12626 case MOD:
12627 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12629 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12630 base_type_for_mode (mode, 0),
12631 mode, mem_mode);
12632 break;
12635 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12636 VAR_INIT_STATUS_INITIALIZED);
12637 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12638 VAR_INIT_STATUS_INITIALIZED);
12640 if (op0 == 0 || op1 == 0)
12641 break;
12643 mem_loc_result = op0;
12644 add_loc_descr (&mem_loc_result, op1);
12645 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12646 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12647 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12648 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12649 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12650 break;
12652 case UDIV:
12653 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12655 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12657 op = DW_OP_div;
12658 goto do_binop;
12660 mem_loc_result = typed_binop (DW_OP_div, rtl,
12661 base_type_for_mode (mode, 1),
12662 mode, mem_mode);
12664 break;
12666 case NOT:
12667 op = DW_OP_not;
12668 goto do_unop;
12670 case ABS:
12671 op = DW_OP_abs;
12672 goto do_unop;
12674 case NEG:
12675 op = DW_OP_neg;
12676 goto do_unop;
12678 do_unop:
12679 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12680 VAR_INIT_STATUS_INITIALIZED);
12682 if (op0 == 0)
12683 break;
12685 mem_loc_result = op0;
12686 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12687 break;
12689 case CONST_INT:
12690 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12691 #ifdef POINTERS_EXTEND_UNSIGNED
12692 || (mode == Pmode
12693 && mem_mode != VOIDmode
12694 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12695 #endif
12698 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12699 break;
12701 if (!dwarf_strict
12702 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12703 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12705 dw_die_ref type_die = base_type_for_mode (mode, 1);
12706 enum machine_mode amode;
12707 if (type_die == NULL)
12708 return NULL;
12709 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12710 MODE_INT, 0);
12711 if (INTVAL (rtl) >= 0
12712 && amode != BLKmode
12713 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12714 /* const DW_OP_GNU_convert <XXX> vs.
12715 DW_OP_GNU_const_type <XXX, 1, const>. */
12716 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12717 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12719 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12720 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12721 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12722 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12723 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12724 add_loc_descr (&mem_loc_result, op0);
12725 return mem_loc_result;
12727 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12728 INTVAL (rtl));
12729 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12730 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12731 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12732 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12733 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12734 else
12736 mem_loc_result->dw_loc_oprnd2.val_class
12737 = dw_val_class_const_double;
12738 mem_loc_result->dw_loc_oprnd2.v.val_double
12739 = double_int::from_shwi (INTVAL (rtl));
12742 break;
12744 case CONST_DOUBLE:
12745 if (!dwarf_strict)
12747 dw_die_ref type_die;
12749 /* Note that a CONST_DOUBLE rtx could represent either an integer
12750 or a floating-point constant. A CONST_DOUBLE is used whenever
12751 the constant requires more than one word in order to be
12752 adequately represented. We output CONST_DOUBLEs as blocks. */
12753 if (mode == VOIDmode
12754 || (GET_MODE (rtl) == VOIDmode
12755 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12756 break;
12757 type_die = base_type_for_mode (mode,
12758 GET_MODE_CLASS (mode) == MODE_INT);
12759 if (type_die == NULL)
12760 return NULL;
12761 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12762 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12763 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12764 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12765 if (SCALAR_FLOAT_MODE_P (mode))
12767 unsigned int length = GET_MODE_SIZE (mode);
12768 unsigned char *array
12769 = (unsigned char*) ggc_alloc_atomic (length);
12771 insert_float (rtl, array);
12772 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12773 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12774 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12775 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12777 else
12779 mem_loc_result->dw_loc_oprnd2.val_class
12780 = dw_val_class_const_double;
12781 mem_loc_result->dw_loc_oprnd2.v.val_double
12782 = rtx_to_double_int (rtl);
12785 break;
12787 case EQ:
12788 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12789 break;
12791 case GE:
12792 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12793 break;
12795 case GT:
12796 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12797 break;
12799 case LE:
12800 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12801 break;
12803 case LT:
12804 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12805 break;
12807 case NE:
12808 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12809 break;
12811 case GEU:
12812 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12813 break;
12815 case GTU:
12816 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12817 break;
12819 case LEU:
12820 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12821 break;
12823 case LTU:
12824 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12825 break;
12827 case UMIN:
12828 case UMAX:
12829 if (GET_MODE_CLASS (mode) != MODE_INT)
12830 break;
12831 /* FALLTHRU */
12832 case SMIN:
12833 case SMAX:
12834 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12835 break;
12837 case ZERO_EXTRACT:
12838 case SIGN_EXTRACT:
12839 if (CONST_INT_P (XEXP (rtl, 1))
12840 && CONST_INT_P (XEXP (rtl, 2))
12841 && ((unsigned) INTVAL (XEXP (rtl, 1))
12842 + (unsigned) INTVAL (XEXP (rtl, 2))
12843 <= GET_MODE_BITSIZE (mode))
12844 && GET_MODE_CLASS (mode) == MODE_INT
12845 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12846 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12848 int shift, size;
12849 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12850 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12851 if (op0 == 0)
12852 break;
12853 if (GET_CODE (rtl) == SIGN_EXTRACT)
12854 op = DW_OP_shra;
12855 else
12856 op = DW_OP_shr;
12857 mem_loc_result = op0;
12858 size = INTVAL (XEXP (rtl, 1));
12859 shift = INTVAL (XEXP (rtl, 2));
12860 if (BITS_BIG_ENDIAN)
12861 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12862 - shift - size;
12863 if (shift + size != (int) DWARF2_ADDR_SIZE)
12865 add_loc_descr (&mem_loc_result,
12866 int_loc_descriptor (DWARF2_ADDR_SIZE
12867 - shift - size));
12868 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12870 if (size != (int) DWARF2_ADDR_SIZE)
12872 add_loc_descr (&mem_loc_result,
12873 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12874 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12877 break;
12879 case IF_THEN_ELSE:
12881 dw_loc_descr_ref op2, bra_node, drop_node;
12882 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12883 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12884 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12885 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12886 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12887 VAR_INIT_STATUS_INITIALIZED);
12888 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12889 VAR_INIT_STATUS_INITIALIZED);
12890 if (op0 == NULL || op1 == NULL || op2 == NULL)
12891 break;
12893 mem_loc_result = op1;
12894 add_loc_descr (&mem_loc_result, op2);
12895 add_loc_descr (&mem_loc_result, op0);
12896 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12897 add_loc_descr (&mem_loc_result, bra_node);
12898 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12899 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12900 add_loc_descr (&mem_loc_result, drop_node);
12901 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12902 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12904 break;
12906 case FLOAT_EXTEND:
12907 case FLOAT_TRUNCATE:
12908 case FLOAT:
12909 case UNSIGNED_FLOAT:
12910 case FIX:
12911 case UNSIGNED_FIX:
12912 if (!dwarf_strict)
12914 dw_die_ref type_die;
12915 dw_loc_descr_ref cvt;
12917 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12918 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12919 if (op0 == NULL)
12920 break;
12921 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12922 && (GET_CODE (rtl) == FLOAT
12923 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12924 <= DWARF2_ADDR_SIZE))
12926 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12927 GET_CODE (rtl) == UNSIGNED_FLOAT);
12928 if (type_die == NULL)
12929 break;
12930 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12931 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12932 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12933 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12934 add_loc_descr (&op0, cvt);
12936 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12937 if (type_die == NULL)
12938 break;
12939 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12940 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12941 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12942 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12943 add_loc_descr (&op0, cvt);
12944 if (GET_MODE_CLASS (mode) == MODE_INT
12945 && (GET_CODE (rtl) == FIX
12946 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12948 op0 = convert_descriptor_to_mode (mode, op0);
12949 if (op0 == NULL)
12950 break;
12952 mem_loc_result = op0;
12954 break;
12956 case CLZ:
12957 case CTZ:
12958 case FFS:
12959 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12960 break;
12962 case POPCOUNT:
12963 case PARITY:
12964 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12965 break;
12967 case BSWAP:
12968 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12969 break;
12971 case ROTATE:
12972 case ROTATERT:
12973 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12974 break;
12976 case COMPARE:
12977 /* In theory, we could implement the above. */
12978 /* DWARF cannot represent the unsigned compare operations
12979 natively. */
12980 case SS_MULT:
12981 case US_MULT:
12982 case SS_DIV:
12983 case US_DIV:
12984 case SS_PLUS:
12985 case US_PLUS:
12986 case SS_MINUS:
12987 case US_MINUS:
12988 case SS_NEG:
12989 case US_NEG:
12990 case SS_ABS:
12991 case SS_ASHIFT:
12992 case US_ASHIFT:
12993 case SS_TRUNCATE:
12994 case US_TRUNCATE:
12995 case UNORDERED:
12996 case ORDERED:
12997 case UNEQ:
12998 case UNGE:
12999 case UNGT:
13000 case UNLE:
13001 case UNLT:
13002 case LTGT:
13003 case FRACT_CONVERT:
13004 case UNSIGNED_FRACT_CONVERT:
13005 case SAT_FRACT:
13006 case UNSIGNED_SAT_FRACT:
13007 case SQRT:
13008 case ASM_OPERANDS:
13009 case VEC_MERGE:
13010 case VEC_SELECT:
13011 case VEC_CONCAT:
13012 case VEC_DUPLICATE:
13013 case UNSPEC:
13014 case HIGH:
13015 case FMA:
13016 case STRICT_LOW_PART:
13017 case CONST_VECTOR:
13018 case CONST_FIXED:
13019 case CLRSB:
13020 case CLOBBER:
13021 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13022 can't express it in the debug info. This can happen e.g. with some
13023 TLS UNSPECs. */
13024 break;
13026 case CONST_STRING:
13027 resolve_one_addr (&rtl, NULL);
13028 goto symref;
13030 default:
13031 #ifdef ENABLE_CHECKING
13032 print_rtl (stderr, rtl);
13033 gcc_unreachable ();
13034 #else
13035 break;
13036 #endif
13039 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13040 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13042 return mem_loc_result;
13045 /* Return a descriptor that describes the concatenation of two locations.
13046 This is typically a complex variable. */
13048 static dw_loc_descr_ref
13049 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13051 dw_loc_descr_ref cc_loc_result = NULL;
13052 dw_loc_descr_ref x0_ref
13053 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13054 dw_loc_descr_ref x1_ref
13055 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13057 if (x0_ref == 0 || x1_ref == 0)
13058 return 0;
13060 cc_loc_result = x0_ref;
13061 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13063 add_loc_descr (&cc_loc_result, x1_ref);
13064 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13066 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13067 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13069 return cc_loc_result;
13072 /* Return a descriptor that describes the concatenation of N
13073 locations. */
13075 static dw_loc_descr_ref
13076 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13078 unsigned int i;
13079 dw_loc_descr_ref cc_loc_result = NULL;
13080 unsigned int n = XVECLEN (concatn, 0);
13082 for (i = 0; i < n; ++i)
13084 dw_loc_descr_ref ref;
13085 rtx x = XVECEXP (concatn, 0, i);
13087 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13088 if (ref == NULL)
13089 return NULL;
13091 add_loc_descr (&cc_loc_result, ref);
13092 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13095 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13096 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13098 return cc_loc_result;
13101 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13102 for DEBUG_IMPLICIT_PTR RTL. */
13104 static dw_loc_descr_ref
13105 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13107 dw_loc_descr_ref ret;
13108 dw_die_ref ref;
13110 if (dwarf_strict)
13111 return NULL;
13112 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13113 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13114 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13115 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13116 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13117 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13118 if (ref)
13120 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13121 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13122 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13124 else
13126 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13127 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13129 return ret;
13132 /* Output a proper Dwarf location descriptor for a variable or parameter
13133 which is either allocated in a register or in a memory location. For a
13134 register, we just generate an OP_REG and the register number. For a
13135 memory location we provide a Dwarf postfix expression describing how to
13136 generate the (dynamic) address of the object onto the address stack.
13138 MODE is mode of the decl if this loc_descriptor is going to be used in
13139 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13140 allowed, VOIDmode otherwise.
13142 If we don't know how to describe it, return 0. */
13144 static dw_loc_descr_ref
13145 loc_descriptor (rtx rtl, enum machine_mode mode,
13146 enum var_init_status initialized)
13148 dw_loc_descr_ref loc_result = NULL;
13150 switch (GET_CODE (rtl))
13152 case SUBREG:
13153 /* The case of a subreg may arise when we have a local (register)
13154 variable or a formal (register) parameter which doesn't quite fill
13155 up an entire register. For now, just assume that it is
13156 legitimate to make the Dwarf info refer to the whole register which
13157 contains the given subreg. */
13158 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13159 loc_result = loc_descriptor (SUBREG_REG (rtl),
13160 GET_MODE (SUBREG_REG (rtl)), initialized);
13161 else
13162 goto do_default;
13163 break;
13165 case REG:
13166 loc_result = reg_loc_descriptor (rtl, initialized);
13167 break;
13169 case MEM:
13170 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13171 GET_MODE (rtl), initialized);
13172 if (loc_result == NULL)
13173 loc_result = tls_mem_loc_descriptor (rtl);
13174 if (loc_result == NULL)
13176 rtx new_rtl = avoid_constant_pool_reference (rtl);
13177 if (new_rtl != rtl)
13178 loc_result = loc_descriptor (new_rtl, mode, initialized);
13180 break;
13182 case CONCAT:
13183 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13184 initialized);
13185 break;
13187 case CONCATN:
13188 loc_result = concatn_loc_descriptor (rtl, initialized);
13189 break;
13191 case VAR_LOCATION:
13192 /* Single part. */
13193 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13195 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13196 if (GET_CODE (loc) == EXPR_LIST)
13197 loc = XEXP (loc, 0);
13198 loc_result = loc_descriptor (loc, mode, initialized);
13199 break;
13202 rtl = XEXP (rtl, 1);
13203 /* FALLTHRU */
13205 case PARALLEL:
13207 rtvec par_elems = XVEC (rtl, 0);
13208 int num_elem = GET_NUM_ELEM (par_elems);
13209 enum machine_mode mode;
13210 int i;
13212 /* Create the first one, so we have something to add to. */
13213 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13214 VOIDmode, initialized);
13215 if (loc_result == NULL)
13216 return NULL;
13217 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13218 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13219 for (i = 1; i < num_elem; i++)
13221 dw_loc_descr_ref temp;
13223 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13224 VOIDmode, initialized);
13225 if (temp == NULL)
13226 return NULL;
13227 add_loc_descr (&loc_result, temp);
13228 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13229 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13232 break;
13234 case CONST_INT:
13235 if (mode != VOIDmode && mode != BLKmode)
13236 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13237 INTVAL (rtl));
13238 break;
13240 case CONST_DOUBLE:
13241 if (mode == VOIDmode)
13242 mode = GET_MODE (rtl);
13244 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13246 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13248 /* Note that a CONST_DOUBLE rtx could represent either an integer
13249 or a floating-point constant. A CONST_DOUBLE is used whenever
13250 the constant requires more than one word in order to be
13251 adequately represented. We output CONST_DOUBLEs as blocks. */
13252 loc_result = new_loc_descr (DW_OP_implicit_value,
13253 GET_MODE_SIZE (mode), 0);
13254 if (SCALAR_FLOAT_MODE_P (mode))
13256 unsigned int length = GET_MODE_SIZE (mode);
13257 unsigned char *array
13258 = (unsigned char*) ggc_alloc_atomic (length);
13260 insert_float (rtl, array);
13261 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13262 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13263 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13264 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13266 else
13268 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13269 loc_result->dw_loc_oprnd2.v.val_double
13270 = rtx_to_double_int (rtl);
13273 break;
13275 case CONST_VECTOR:
13276 if (mode == VOIDmode)
13277 mode = GET_MODE (rtl);
13279 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13281 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13282 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13283 unsigned char *array = (unsigned char *)
13284 ggc_alloc_atomic (length * elt_size);
13285 unsigned int i;
13286 unsigned char *p;
13288 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13289 switch (GET_MODE_CLASS (mode))
13291 case MODE_VECTOR_INT:
13292 for (i = 0, p = array; i < length; i++, p += elt_size)
13294 rtx elt = CONST_VECTOR_ELT (rtl, i);
13295 double_int val = rtx_to_double_int (elt);
13297 if (elt_size <= sizeof (HOST_WIDE_INT))
13298 insert_int (val.to_shwi (), elt_size, p);
13299 else
13301 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13302 insert_double (val, p);
13305 break;
13307 case MODE_VECTOR_FLOAT:
13308 for (i = 0, p = array; i < length; i++, p += elt_size)
13310 rtx elt = CONST_VECTOR_ELT (rtl, i);
13311 insert_float (elt, p);
13313 break;
13315 default:
13316 gcc_unreachable ();
13319 loc_result = new_loc_descr (DW_OP_implicit_value,
13320 length * elt_size, 0);
13321 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13322 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13323 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13324 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13326 break;
13328 case CONST:
13329 if (mode == VOIDmode
13330 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13331 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13332 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13334 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13335 break;
13337 /* FALLTHROUGH */
13338 case SYMBOL_REF:
13339 if (!const_ok_for_output (rtl))
13340 break;
13341 case LABEL_REF:
13342 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13343 && (dwarf_version >= 4 || !dwarf_strict))
13345 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13346 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13347 vec_safe_push (used_rtx_array, rtl);
13349 break;
13351 case DEBUG_IMPLICIT_PTR:
13352 loc_result = implicit_ptr_descriptor (rtl, 0);
13353 break;
13355 case PLUS:
13356 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13357 && CONST_INT_P (XEXP (rtl, 1)))
13359 loc_result
13360 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13361 break;
13363 /* FALLTHRU */
13364 do_default:
13365 default:
13366 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13367 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13368 && dwarf_version >= 4)
13369 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13371 /* Value expression. */
13372 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13373 if (loc_result)
13374 add_loc_descr (&loc_result,
13375 new_loc_descr (DW_OP_stack_value, 0, 0));
13377 break;
13380 return loc_result;
13383 /* We need to figure out what section we should use as the base for the
13384 address ranges where a given location is valid.
13385 1. If this particular DECL has a section associated with it, use that.
13386 2. If this function has a section associated with it, use that.
13387 3. Otherwise, use the text section.
13388 XXX: If you split a variable across multiple sections, we won't notice. */
13390 static const char *
13391 secname_for_decl (const_tree decl)
13393 const char *secname;
13395 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13397 tree sectree = DECL_SECTION_NAME (decl);
13398 secname = TREE_STRING_POINTER (sectree);
13400 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13402 tree sectree = DECL_SECTION_NAME (current_function_decl);
13403 secname = TREE_STRING_POINTER (sectree);
13405 else if (cfun && in_cold_section_p)
13406 secname = crtl->subsections.cold_section_label;
13407 else
13408 secname = text_section_label;
13410 return secname;
13413 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13415 static bool
13416 decl_by_reference_p (tree decl)
13418 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13419 || TREE_CODE (decl) == VAR_DECL)
13420 && DECL_BY_REFERENCE (decl));
13423 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13424 for VARLOC. */
13426 static dw_loc_descr_ref
13427 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13428 enum var_init_status initialized)
13430 int have_address = 0;
13431 dw_loc_descr_ref descr;
13432 enum machine_mode mode;
13434 if (want_address != 2)
13436 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13437 /* Single part. */
13438 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13440 varloc = PAT_VAR_LOCATION_LOC (varloc);
13441 if (GET_CODE (varloc) == EXPR_LIST)
13442 varloc = XEXP (varloc, 0);
13443 mode = GET_MODE (varloc);
13444 if (MEM_P (varloc))
13446 rtx addr = XEXP (varloc, 0);
13447 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13448 mode, initialized);
13449 if (descr)
13450 have_address = 1;
13451 else
13453 rtx x = avoid_constant_pool_reference (varloc);
13454 if (x != varloc)
13455 descr = mem_loc_descriptor (x, mode, VOIDmode,
13456 initialized);
13459 else
13460 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13462 else
13463 return 0;
13465 else
13467 if (GET_CODE (varloc) == VAR_LOCATION)
13468 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13469 else
13470 mode = DECL_MODE (loc);
13471 descr = loc_descriptor (varloc, mode, initialized);
13472 have_address = 1;
13475 if (!descr)
13476 return 0;
13478 if (want_address == 2 && !have_address
13479 && (dwarf_version >= 4 || !dwarf_strict))
13481 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13483 expansion_failed (loc, NULL_RTX,
13484 "DWARF address size mismatch");
13485 return 0;
13487 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13488 have_address = 1;
13490 /* Show if we can't fill the request for an address. */
13491 if (want_address && !have_address)
13493 expansion_failed (loc, NULL_RTX,
13494 "Want address and only have value");
13495 return 0;
13498 /* If we've got an address and don't want one, dereference. */
13499 if (!want_address && have_address)
13501 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13502 enum dwarf_location_atom op;
13504 if (size > DWARF2_ADDR_SIZE || size == -1)
13506 expansion_failed (loc, NULL_RTX,
13507 "DWARF address size mismatch");
13508 return 0;
13510 else if (size == DWARF2_ADDR_SIZE)
13511 op = DW_OP_deref;
13512 else
13513 op = DW_OP_deref_size;
13515 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13518 return descr;
13521 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13522 if it is not possible. */
13524 static dw_loc_descr_ref
13525 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13527 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13528 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13529 else if (dwarf_version >= 3 || !dwarf_strict)
13530 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13531 else
13532 return NULL;
13535 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13536 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13538 static dw_loc_descr_ref
13539 dw_sra_loc_expr (tree decl, rtx loc)
13541 rtx p;
13542 unsigned int padsize = 0;
13543 dw_loc_descr_ref descr, *descr_tail;
13544 unsigned HOST_WIDE_INT decl_size;
13545 rtx varloc;
13546 enum var_init_status initialized;
13548 if (DECL_SIZE (decl) == NULL
13549 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13550 return NULL;
13552 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13553 descr = NULL;
13554 descr_tail = &descr;
13556 for (p = loc; p; p = XEXP (p, 1))
13558 unsigned int bitsize = decl_piece_bitsize (p);
13559 rtx loc_note = *decl_piece_varloc_ptr (p);
13560 dw_loc_descr_ref cur_descr;
13561 dw_loc_descr_ref *tail, last = NULL;
13562 unsigned int opsize = 0;
13564 if (loc_note == NULL_RTX
13565 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13567 padsize += bitsize;
13568 continue;
13570 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13571 varloc = NOTE_VAR_LOCATION (loc_note);
13572 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13573 if (cur_descr == NULL)
13575 padsize += bitsize;
13576 continue;
13579 /* Check that cur_descr either doesn't use
13580 DW_OP_*piece operations, or their sum is equal
13581 to bitsize. Otherwise we can't embed it. */
13582 for (tail = &cur_descr; *tail != NULL;
13583 tail = &(*tail)->dw_loc_next)
13584 if ((*tail)->dw_loc_opc == DW_OP_piece)
13586 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13587 * BITS_PER_UNIT;
13588 last = *tail;
13590 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13592 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13593 last = *tail;
13596 if (last != NULL && opsize != bitsize)
13598 padsize += bitsize;
13599 /* Discard the current piece of the descriptor and release any
13600 addr_table entries it uses. */
13601 remove_loc_list_addr_table_entries (cur_descr);
13602 continue;
13605 /* If there is a hole, add DW_OP_*piece after empty DWARF
13606 expression, which means that those bits are optimized out. */
13607 if (padsize)
13609 if (padsize > decl_size)
13611 remove_loc_list_addr_table_entries (cur_descr);
13612 goto discard_descr;
13614 decl_size -= padsize;
13615 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13616 if (*descr_tail == NULL)
13618 remove_loc_list_addr_table_entries (cur_descr);
13619 goto discard_descr;
13621 descr_tail = &(*descr_tail)->dw_loc_next;
13622 padsize = 0;
13624 *descr_tail = cur_descr;
13625 descr_tail = tail;
13626 if (bitsize > decl_size)
13627 goto discard_descr;
13628 decl_size -= bitsize;
13629 if (last == NULL)
13631 HOST_WIDE_INT offset = 0;
13632 if (GET_CODE (varloc) == VAR_LOCATION
13633 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13635 varloc = PAT_VAR_LOCATION_LOC (varloc);
13636 if (GET_CODE (varloc) == EXPR_LIST)
13637 varloc = XEXP (varloc, 0);
13641 if (GET_CODE (varloc) == CONST
13642 || GET_CODE (varloc) == SIGN_EXTEND
13643 || GET_CODE (varloc) == ZERO_EXTEND)
13644 varloc = XEXP (varloc, 0);
13645 else if (GET_CODE (varloc) == SUBREG)
13646 varloc = SUBREG_REG (varloc);
13647 else
13648 break;
13650 while (1);
13651 /* DW_OP_bit_size offset should be zero for register
13652 or implicit location descriptions and empty location
13653 descriptions, but for memory addresses needs big endian
13654 adjustment. */
13655 if (MEM_P (varloc))
13657 unsigned HOST_WIDE_INT memsize
13658 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13659 if (memsize != bitsize)
13661 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13662 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13663 goto discard_descr;
13664 if (memsize < bitsize)
13665 goto discard_descr;
13666 if (BITS_BIG_ENDIAN)
13667 offset = memsize - bitsize;
13671 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13672 if (*descr_tail == NULL)
13673 goto discard_descr;
13674 descr_tail = &(*descr_tail)->dw_loc_next;
13678 /* If there were any non-empty expressions, add padding till the end of
13679 the decl. */
13680 if (descr != NULL && decl_size != 0)
13682 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13683 if (*descr_tail == NULL)
13684 goto discard_descr;
13686 return descr;
13688 discard_descr:
13689 /* Discard the descriptor and release any addr_table entries it uses. */
13690 remove_loc_list_addr_table_entries (descr);
13691 return NULL;
13694 /* Return the dwarf representation of the location list LOC_LIST of
13695 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13696 function. */
13698 static dw_loc_list_ref
13699 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13701 const char *endname, *secname;
13702 rtx varloc;
13703 enum var_init_status initialized;
13704 struct var_loc_node *node;
13705 dw_loc_descr_ref descr;
13706 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13707 dw_loc_list_ref list = NULL;
13708 dw_loc_list_ref *listp = &list;
13710 /* Now that we know what section we are using for a base,
13711 actually construct the list of locations.
13712 The first location information is what is passed to the
13713 function that creates the location list, and the remaining
13714 locations just get added on to that list.
13715 Note that we only know the start address for a location
13716 (IE location changes), so to build the range, we use
13717 the range [current location start, next location start].
13718 This means we have to special case the last node, and generate
13719 a range of [last location start, end of function label]. */
13721 secname = secname_for_decl (decl);
13723 for (node = loc_list->first; node; node = node->next)
13724 if (GET_CODE (node->loc) == EXPR_LIST
13725 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13727 if (GET_CODE (node->loc) == EXPR_LIST)
13729 /* This requires DW_OP_{,bit_}piece, which is not usable
13730 inside DWARF expressions. */
13731 if (want_address != 2)
13732 continue;
13733 descr = dw_sra_loc_expr (decl, node->loc);
13734 if (descr == NULL)
13735 continue;
13737 else
13739 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13740 varloc = NOTE_VAR_LOCATION (node->loc);
13741 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13743 if (descr)
13745 bool range_across_switch = false;
13746 /* If section switch happens in between node->label
13747 and node->next->label (or end of function) and
13748 we can't emit it as a single entry list,
13749 emit two ranges, first one ending at the end
13750 of first partition and second one starting at the
13751 beginning of second partition. */
13752 if (node == loc_list->last_before_switch
13753 && (node != loc_list->first || loc_list->first->next)
13754 && current_function_decl)
13756 endname = cfun->fde->dw_fde_end;
13757 range_across_switch = true;
13759 /* The variable has a location between NODE->LABEL and
13760 NODE->NEXT->LABEL. */
13761 else if (node->next)
13762 endname = node->next->label;
13763 /* If the variable has a location at the last label
13764 it keeps its location until the end of function. */
13765 else if (!current_function_decl)
13766 endname = text_end_label;
13767 else
13769 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13770 current_function_funcdef_no);
13771 endname = ggc_strdup (label_id);
13774 *listp = new_loc_list (descr, node->label, endname, secname);
13775 if (TREE_CODE (decl) == PARM_DECL
13776 && node == loc_list->first
13777 && NOTE_P (node->loc)
13778 && strcmp (node->label, endname) == 0)
13779 (*listp)->force = true;
13780 listp = &(*listp)->dw_loc_next;
13782 if (range_across_switch)
13784 if (GET_CODE (node->loc) == EXPR_LIST)
13785 descr = dw_sra_loc_expr (decl, node->loc);
13786 else
13788 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13789 varloc = NOTE_VAR_LOCATION (node->loc);
13790 descr = dw_loc_list_1 (decl, varloc, want_address,
13791 initialized);
13793 gcc_assert (descr);
13794 /* The variable has a location between NODE->LABEL and
13795 NODE->NEXT->LABEL. */
13796 if (node->next)
13797 endname = node->next->label;
13798 else
13799 endname = cfun->fde->dw_fde_second_end;
13800 *listp = new_loc_list (descr,
13801 cfun->fde->dw_fde_second_begin,
13802 endname, secname);
13803 listp = &(*listp)->dw_loc_next;
13808 /* Try to avoid the overhead of a location list emitting a location
13809 expression instead, but only if we didn't have more than one
13810 location entry in the first place. If some entries were not
13811 representable, we don't want to pretend a single entry that was
13812 applies to the entire scope in which the variable is
13813 available. */
13814 if (list && loc_list->first->next)
13815 gen_llsym (list);
13817 return list;
13820 /* Return if the loc_list has only single element and thus can be represented
13821 as location description. */
13823 static bool
13824 single_element_loc_list_p (dw_loc_list_ref list)
13826 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13827 return !list->ll_symbol;
13830 /* To each location in list LIST add loc descr REF. */
13832 static void
13833 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13835 dw_loc_descr_ref copy;
13836 add_loc_descr (&list->expr, ref);
13837 list = list->dw_loc_next;
13838 while (list)
13840 copy = ggc_alloc_dw_loc_descr_node ();
13841 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13842 add_loc_descr (&list->expr, copy);
13843 while (copy->dw_loc_next)
13845 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13846 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13847 copy->dw_loc_next = new_copy;
13848 copy = new_copy;
13850 list = list->dw_loc_next;
13854 /* Given two lists RET and LIST
13855 produce location list that is result of adding expression in LIST
13856 to expression in RET on each position in program.
13857 Might be destructive on both RET and LIST.
13859 TODO: We handle only simple cases of RET or LIST having at most one
13860 element. General case would inolve sorting the lists in program order
13861 and merging them that will need some additional work.
13862 Adding that will improve quality of debug info especially for SRA-ed
13863 structures. */
13865 static void
13866 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13868 if (!list)
13869 return;
13870 if (!*ret)
13872 *ret = list;
13873 return;
13875 if (!list->dw_loc_next)
13877 add_loc_descr_to_each (*ret, list->expr);
13878 return;
13880 if (!(*ret)->dw_loc_next)
13882 add_loc_descr_to_each (list, (*ret)->expr);
13883 *ret = list;
13884 return;
13886 expansion_failed (NULL_TREE, NULL_RTX,
13887 "Don't know how to merge two non-trivial"
13888 " location lists.\n");
13889 *ret = NULL;
13890 return;
13893 /* LOC is constant expression. Try a luck, look it up in constant
13894 pool and return its loc_descr of its address. */
13896 static dw_loc_descr_ref
13897 cst_pool_loc_descr (tree loc)
13899 /* Get an RTL for this, if something has been emitted. */
13900 rtx rtl = lookup_constant_def (loc);
13902 if (!rtl || !MEM_P (rtl))
13904 gcc_assert (!rtl);
13905 return 0;
13907 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13909 /* TODO: We might get more coverage if we was actually delaying expansion
13910 of all expressions till end of compilation when constant pools are fully
13911 populated. */
13912 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13914 expansion_failed (loc, NULL_RTX,
13915 "CST value in contant pool but not marked.");
13916 return 0;
13918 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13919 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13922 /* Return dw_loc_list representing address of addr_expr LOC
13923 by looking for inner INDIRECT_REF expression and turning
13924 it into simple arithmetics. */
13926 static dw_loc_list_ref
13927 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13929 tree obj, offset;
13930 HOST_WIDE_INT bitsize, bitpos, bytepos;
13931 enum machine_mode mode;
13932 int unsignedp, volatilep = 0;
13933 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13935 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13936 &bitsize, &bitpos, &offset, &mode,
13937 &unsignedp, &volatilep, false);
13938 STRIP_NOPS (obj);
13939 if (bitpos % BITS_PER_UNIT)
13941 expansion_failed (loc, NULL_RTX, "bitfield access");
13942 return 0;
13944 if (!INDIRECT_REF_P (obj))
13946 expansion_failed (obj,
13947 NULL_RTX, "no indirect ref in inner refrence");
13948 return 0;
13950 if (!offset && !bitpos)
13951 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13952 else if (toplev
13953 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13954 && (dwarf_version >= 4 || !dwarf_strict))
13956 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13957 if (!list_ret)
13958 return 0;
13959 if (offset)
13961 /* Variable offset. */
13962 list_ret1 = loc_list_from_tree (offset, 0);
13963 if (list_ret1 == 0)
13964 return 0;
13965 add_loc_list (&list_ret, list_ret1);
13966 if (!list_ret)
13967 return 0;
13968 add_loc_descr_to_each (list_ret,
13969 new_loc_descr (DW_OP_plus, 0, 0));
13971 bytepos = bitpos / BITS_PER_UNIT;
13972 if (bytepos > 0)
13973 add_loc_descr_to_each (list_ret,
13974 new_loc_descr (DW_OP_plus_uconst,
13975 bytepos, 0));
13976 else if (bytepos < 0)
13977 loc_list_plus_const (list_ret, bytepos);
13978 add_loc_descr_to_each (list_ret,
13979 new_loc_descr (DW_OP_stack_value, 0, 0));
13981 return list_ret;
13985 /* Generate Dwarf location list representing LOC.
13986 If WANT_ADDRESS is false, expression computing LOC will be computed
13987 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13988 if WANT_ADDRESS is 2, expression computing address useable in location
13989 will be returned (i.e. DW_OP_reg can be used
13990 to refer to register values). */
13992 static dw_loc_list_ref
13993 loc_list_from_tree (tree loc, int want_address)
13995 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13996 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13997 int have_address = 0;
13998 enum dwarf_location_atom op;
14000 /* ??? Most of the time we do not take proper care for sign/zero
14001 extending the values properly. Hopefully this won't be a real
14002 problem... */
14004 switch (TREE_CODE (loc))
14006 case ERROR_MARK:
14007 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14008 return 0;
14010 case PLACEHOLDER_EXPR:
14011 /* This case involves extracting fields from an object to determine the
14012 position of other fields. We don't try to encode this here. The
14013 only user of this is Ada, which encodes the needed information using
14014 the names of types. */
14015 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14016 return 0;
14018 case CALL_EXPR:
14019 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14020 /* There are no opcodes for these operations. */
14021 return 0;
14023 case PREINCREMENT_EXPR:
14024 case PREDECREMENT_EXPR:
14025 case POSTINCREMENT_EXPR:
14026 case POSTDECREMENT_EXPR:
14027 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14028 /* There are no opcodes for these operations. */
14029 return 0;
14031 case ADDR_EXPR:
14032 /* If we already want an address, see if there is INDIRECT_REF inside
14033 e.g. for &this->field. */
14034 if (want_address)
14036 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14037 (loc, want_address == 2);
14038 if (list_ret)
14039 have_address = 1;
14040 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14041 && (ret = cst_pool_loc_descr (loc)))
14042 have_address = 1;
14044 /* Otherwise, process the argument and look for the address. */
14045 if (!list_ret && !ret)
14046 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14047 else
14049 if (want_address)
14050 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14051 return NULL;
14053 break;
14055 case VAR_DECL:
14056 if (DECL_THREAD_LOCAL_P (loc))
14058 rtx rtl;
14059 enum dwarf_location_atom tls_op;
14060 enum dtprel_bool dtprel = dtprel_false;
14062 if (targetm.have_tls)
14064 /* If this is not defined, we have no way to emit the
14065 data. */
14066 if (!targetm.asm_out.output_dwarf_dtprel)
14067 return 0;
14069 /* The way DW_OP_GNU_push_tls_address is specified, we
14070 can only look up addresses of objects in the current
14071 module. We used DW_OP_addr as first op, but that's
14072 wrong, because DW_OP_addr is relocated by the debug
14073 info consumer, while DW_OP_GNU_push_tls_address
14074 operand shouldn't be. */
14075 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14076 return 0;
14077 dtprel = dtprel_true;
14078 tls_op = DW_OP_GNU_push_tls_address;
14080 else
14082 if (!targetm.emutls.debug_form_tls_address
14083 || !(dwarf_version >= 3 || !dwarf_strict))
14084 return 0;
14085 /* We stuffed the control variable into the DECL_VALUE_EXPR
14086 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14087 no longer appear in gimple code. We used the control
14088 variable in specific so that we could pick it up here. */
14089 loc = DECL_VALUE_EXPR (loc);
14090 tls_op = DW_OP_form_tls_address;
14093 rtl = rtl_for_decl_location (loc);
14094 if (rtl == NULL_RTX)
14095 return 0;
14097 if (!MEM_P (rtl))
14098 return 0;
14099 rtl = XEXP (rtl, 0);
14100 if (! CONSTANT_P (rtl))
14101 return 0;
14103 ret = new_addr_loc_descr (rtl, dtprel);
14104 ret1 = new_loc_descr (tls_op, 0, 0);
14105 add_loc_descr (&ret, ret1);
14107 have_address = 1;
14108 break;
14110 /* FALLTHRU */
14112 case PARM_DECL:
14113 case RESULT_DECL:
14114 if (DECL_HAS_VALUE_EXPR_P (loc))
14115 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14116 want_address);
14117 /* FALLTHRU */
14119 case FUNCTION_DECL:
14121 rtx rtl;
14122 var_loc_list *loc_list = lookup_decl_loc (loc);
14124 if (loc_list && loc_list->first)
14126 list_ret = dw_loc_list (loc_list, loc, want_address);
14127 have_address = want_address != 0;
14128 break;
14130 rtl = rtl_for_decl_location (loc);
14131 if (rtl == NULL_RTX)
14133 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14134 return 0;
14136 else if (CONST_INT_P (rtl))
14138 HOST_WIDE_INT val = INTVAL (rtl);
14139 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14140 val &= GET_MODE_MASK (DECL_MODE (loc));
14141 ret = int_loc_descriptor (val);
14143 else if (GET_CODE (rtl) == CONST_STRING)
14145 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14146 return 0;
14148 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14149 ret = new_addr_loc_descr (rtl, dtprel_false);
14150 else
14152 enum machine_mode mode, mem_mode;
14154 /* Certain constructs can only be represented at top-level. */
14155 if (want_address == 2)
14157 ret = loc_descriptor (rtl, VOIDmode,
14158 VAR_INIT_STATUS_INITIALIZED);
14159 have_address = 1;
14161 else
14163 mode = GET_MODE (rtl);
14164 mem_mode = VOIDmode;
14165 if (MEM_P (rtl))
14167 mem_mode = mode;
14168 mode = get_address_mode (rtl);
14169 rtl = XEXP (rtl, 0);
14170 have_address = 1;
14172 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14173 VAR_INIT_STATUS_INITIALIZED);
14175 if (!ret)
14176 expansion_failed (loc, rtl,
14177 "failed to produce loc descriptor for rtl");
14180 break;
14182 case MEM_REF:
14183 /* ??? FIXME. */
14184 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14185 return 0;
14186 /* Fallthru. */
14187 case INDIRECT_REF:
14188 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14189 have_address = 1;
14190 break;
14192 case COMPOUND_EXPR:
14193 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14195 CASE_CONVERT:
14196 case VIEW_CONVERT_EXPR:
14197 case SAVE_EXPR:
14198 case MODIFY_EXPR:
14199 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14201 case COMPONENT_REF:
14202 case BIT_FIELD_REF:
14203 case ARRAY_REF:
14204 case ARRAY_RANGE_REF:
14205 case REALPART_EXPR:
14206 case IMAGPART_EXPR:
14208 tree obj, offset;
14209 HOST_WIDE_INT bitsize, bitpos, bytepos;
14210 enum machine_mode mode;
14211 int unsignedp, volatilep = 0;
14213 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14214 &unsignedp, &volatilep, false);
14216 gcc_assert (obj != loc);
14218 list_ret = loc_list_from_tree (obj,
14219 want_address == 2
14220 && !bitpos && !offset ? 2 : 1);
14221 /* TODO: We can extract value of the small expression via shifting even
14222 for nonzero bitpos. */
14223 if (list_ret == 0)
14224 return 0;
14225 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14227 expansion_failed (loc, NULL_RTX,
14228 "bitfield access");
14229 return 0;
14232 if (offset != NULL_TREE)
14234 /* Variable offset. */
14235 list_ret1 = loc_list_from_tree (offset, 0);
14236 if (list_ret1 == 0)
14237 return 0;
14238 add_loc_list (&list_ret, list_ret1);
14239 if (!list_ret)
14240 return 0;
14241 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14244 bytepos = bitpos / BITS_PER_UNIT;
14245 if (bytepos > 0)
14246 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14247 else if (bytepos < 0)
14248 loc_list_plus_const (list_ret, bytepos);
14250 have_address = 1;
14251 break;
14254 case INTEGER_CST:
14255 if ((want_address || !tree_fits_shwi_p (loc))
14256 && (ret = cst_pool_loc_descr (loc)))
14257 have_address = 1;
14258 else if (want_address == 2
14259 && tree_fits_shwi_p (loc)
14260 && (ret = address_of_int_loc_descriptor
14261 (int_size_in_bytes (TREE_TYPE (loc)),
14262 tree_to_shwi (loc))))
14263 have_address = 1;
14264 else if (tree_fits_shwi_p (loc))
14265 ret = int_loc_descriptor (tree_to_shwi (loc));
14266 else
14268 expansion_failed (loc, NULL_RTX,
14269 "Integer operand is not host integer");
14270 return 0;
14272 break;
14274 case CONSTRUCTOR:
14275 case REAL_CST:
14276 case STRING_CST:
14277 case COMPLEX_CST:
14278 if ((ret = cst_pool_loc_descr (loc)))
14279 have_address = 1;
14280 else
14281 /* We can construct small constants here using int_loc_descriptor. */
14282 expansion_failed (loc, NULL_RTX,
14283 "constructor or constant not in constant pool");
14284 break;
14286 case TRUTH_AND_EXPR:
14287 case TRUTH_ANDIF_EXPR:
14288 case BIT_AND_EXPR:
14289 op = DW_OP_and;
14290 goto do_binop;
14292 case TRUTH_XOR_EXPR:
14293 case BIT_XOR_EXPR:
14294 op = DW_OP_xor;
14295 goto do_binop;
14297 case TRUTH_OR_EXPR:
14298 case TRUTH_ORIF_EXPR:
14299 case BIT_IOR_EXPR:
14300 op = DW_OP_or;
14301 goto do_binop;
14303 case FLOOR_DIV_EXPR:
14304 case CEIL_DIV_EXPR:
14305 case ROUND_DIV_EXPR:
14306 case TRUNC_DIV_EXPR:
14307 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14308 return 0;
14309 op = DW_OP_div;
14310 goto do_binop;
14312 case MINUS_EXPR:
14313 op = DW_OP_minus;
14314 goto do_binop;
14316 case FLOOR_MOD_EXPR:
14317 case CEIL_MOD_EXPR:
14318 case ROUND_MOD_EXPR:
14319 case TRUNC_MOD_EXPR:
14320 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14322 op = DW_OP_mod;
14323 goto do_binop;
14325 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14326 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14327 if (list_ret == 0 || list_ret1 == 0)
14328 return 0;
14330 add_loc_list (&list_ret, list_ret1);
14331 if (list_ret == 0)
14332 return 0;
14333 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14334 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14335 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14336 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14337 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14338 break;
14340 case MULT_EXPR:
14341 op = DW_OP_mul;
14342 goto do_binop;
14344 case LSHIFT_EXPR:
14345 op = DW_OP_shl;
14346 goto do_binop;
14348 case RSHIFT_EXPR:
14349 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14350 goto do_binop;
14352 case POINTER_PLUS_EXPR:
14353 case PLUS_EXPR:
14354 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14356 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14357 if (list_ret == 0)
14358 return 0;
14360 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14361 break;
14364 op = DW_OP_plus;
14365 goto do_binop;
14367 case LE_EXPR:
14368 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14369 return 0;
14371 op = DW_OP_le;
14372 goto do_binop;
14374 case GE_EXPR:
14375 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14376 return 0;
14378 op = DW_OP_ge;
14379 goto do_binop;
14381 case LT_EXPR:
14382 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14383 return 0;
14385 op = DW_OP_lt;
14386 goto do_binop;
14388 case GT_EXPR:
14389 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14390 return 0;
14392 op = DW_OP_gt;
14393 goto do_binop;
14395 case EQ_EXPR:
14396 op = DW_OP_eq;
14397 goto do_binop;
14399 case NE_EXPR:
14400 op = DW_OP_ne;
14401 goto do_binop;
14403 do_binop:
14404 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14405 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14406 if (list_ret == 0 || list_ret1 == 0)
14407 return 0;
14409 add_loc_list (&list_ret, list_ret1);
14410 if (list_ret == 0)
14411 return 0;
14412 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14413 break;
14415 case TRUTH_NOT_EXPR:
14416 case BIT_NOT_EXPR:
14417 op = DW_OP_not;
14418 goto do_unop;
14420 case ABS_EXPR:
14421 op = DW_OP_abs;
14422 goto do_unop;
14424 case NEGATE_EXPR:
14425 op = DW_OP_neg;
14426 goto do_unop;
14428 do_unop:
14429 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14430 if (list_ret == 0)
14431 return 0;
14433 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14434 break;
14436 case MIN_EXPR:
14437 case MAX_EXPR:
14439 const enum tree_code code =
14440 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14442 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14443 build2 (code, integer_type_node,
14444 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14445 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14448 /* ... fall through ... */
14450 case COND_EXPR:
14452 dw_loc_descr_ref lhs
14453 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14454 dw_loc_list_ref rhs
14455 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14456 dw_loc_descr_ref bra_node, jump_node, tmp;
14458 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14459 if (list_ret == 0 || lhs == 0 || rhs == 0)
14460 return 0;
14462 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14463 add_loc_descr_to_each (list_ret, bra_node);
14465 add_loc_list (&list_ret, rhs);
14466 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14467 add_loc_descr_to_each (list_ret, jump_node);
14469 add_loc_descr_to_each (list_ret, lhs);
14470 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14471 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14473 /* ??? Need a node to point the skip at. Use a nop. */
14474 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14475 add_loc_descr_to_each (list_ret, tmp);
14476 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14477 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14479 break;
14481 case FIX_TRUNC_EXPR:
14482 return 0;
14484 default:
14485 /* Leave front-end specific codes as simply unknown. This comes
14486 up, for instance, with the C STMT_EXPR. */
14487 if ((unsigned int) TREE_CODE (loc)
14488 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14490 expansion_failed (loc, NULL_RTX,
14491 "language specific tree node");
14492 return 0;
14495 #ifdef ENABLE_CHECKING
14496 /* Otherwise this is a generic code; we should just lists all of
14497 these explicitly. We forgot one. */
14498 gcc_unreachable ();
14499 #else
14500 /* In a release build, we want to degrade gracefully: better to
14501 generate incomplete debugging information than to crash. */
14502 return NULL;
14503 #endif
14506 if (!ret && !list_ret)
14507 return 0;
14509 if (want_address == 2 && !have_address
14510 && (dwarf_version >= 4 || !dwarf_strict))
14512 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14514 expansion_failed (loc, NULL_RTX,
14515 "DWARF address size mismatch");
14516 return 0;
14518 if (ret)
14519 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14520 else
14521 add_loc_descr_to_each (list_ret,
14522 new_loc_descr (DW_OP_stack_value, 0, 0));
14523 have_address = 1;
14525 /* Show if we can't fill the request for an address. */
14526 if (want_address && !have_address)
14528 expansion_failed (loc, NULL_RTX,
14529 "Want address and only have value");
14530 return 0;
14533 gcc_assert (!ret || !list_ret);
14535 /* If we've got an address and don't want one, dereference. */
14536 if (!want_address && have_address)
14538 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14540 if (size > DWARF2_ADDR_SIZE || size == -1)
14542 expansion_failed (loc, NULL_RTX,
14543 "DWARF address size mismatch");
14544 return 0;
14546 else if (size == DWARF2_ADDR_SIZE)
14547 op = DW_OP_deref;
14548 else
14549 op = DW_OP_deref_size;
14551 if (ret)
14552 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14553 else
14554 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14556 if (ret)
14557 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14559 return list_ret;
14562 /* Same as above but return only single location expression. */
14563 static dw_loc_descr_ref
14564 loc_descriptor_from_tree (tree loc, int want_address)
14566 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14567 if (!ret)
14568 return NULL;
14569 if (ret->dw_loc_next)
14571 expansion_failed (loc, NULL_RTX,
14572 "Location list where only loc descriptor needed");
14573 return NULL;
14575 return ret->expr;
14578 /* Given a value, round it up to the lowest multiple of `boundary'
14579 which is not less than the value itself. */
14581 static inline HOST_WIDE_INT
14582 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14584 return (((value + boundary - 1) / boundary) * boundary);
14587 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14588 pointer to the declared type for the relevant field variable, or return
14589 `integer_type_node' if the given node turns out to be an
14590 ERROR_MARK node. */
14592 static inline tree
14593 field_type (const_tree decl)
14595 tree type;
14597 if (TREE_CODE (decl) == ERROR_MARK)
14598 return integer_type_node;
14600 type = DECL_BIT_FIELD_TYPE (decl);
14601 if (type == NULL_TREE)
14602 type = TREE_TYPE (decl);
14604 return type;
14607 /* Given a pointer to a tree node, return the alignment in bits for
14608 it, or else return BITS_PER_WORD if the node actually turns out to
14609 be an ERROR_MARK node. */
14611 static inline unsigned
14612 simple_type_align_in_bits (const_tree type)
14614 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14617 static inline unsigned
14618 simple_decl_align_in_bits (const_tree decl)
14620 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14623 /* Return the result of rounding T up to ALIGN. */
14625 static inline double_int
14626 round_up_to_align (double_int t, unsigned int align)
14628 double_int alignd = double_int::from_uhwi (align);
14629 t += alignd;
14630 t += double_int_minus_one;
14631 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14632 t *= alignd;
14633 return t;
14636 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14637 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14638 or return 0 if we are unable to determine what that offset is, either
14639 because the argument turns out to be a pointer to an ERROR_MARK node, or
14640 because the offset is actually variable. (We can't handle the latter case
14641 just yet). */
14643 static HOST_WIDE_INT
14644 field_byte_offset (const_tree decl)
14646 double_int object_offset_in_bits;
14647 double_int object_offset_in_bytes;
14648 double_int bitpos_int;
14650 if (TREE_CODE (decl) == ERROR_MARK)
14651 return 0;
14653 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14655 /* We cannot yet cope with fields whose positions are variable, so
14656 for now, when we see such things, we simply return 0. Someday, we may
14657 be able to handle such cases, but it will be damn difficult. */
14658 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14659 return 0;
14661 bitpos_int = tree_to_double_int (bit_position (decl));
14663 #ifdef PCC_BITFIELD_TYPE_MATTERS
14664 if (PCC_BITFIELD_TYPE_MATTERS)
14666 tree type;
14667 tree field_size_tree;
14668 double_int deepest_bitpos;
14669 double_int field_size_in_bits;
14670 unsigned int type_align_in_bits;
14671 unsigned int decl_align_in_bits;
14672 double_int type_size_in_bits;
14674 type = field_type (decl);
14675 type_size_in_bits = double_int_type_size_in_bits (type);
14676 type_align_in_bits = simple_type_align_in_bits (type);
14678 field_size_tree = DECL_SIZE (decl);
14680 /* The size could be unspecified if there was an error, or for
14681 a flexible array member. */
14682 if (!field_size_tree)
14683 field_size_tree = bitsize_zero_node;
14685 /* If the size of the field is not constant, use the type size. */
14686 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14687 field_size_in_bits = tree_to_double_int (field_size_tree);
14688 else
14689 field_size_in_bits = type_size_in_bits;
14691 decl_align_in_bits = simple_decl_align_in_bits (decl);
14693 /* The GCC front-end doesn't make any attempt to keep track of the
14694 starting bit offset (relative to the start of the containing
14695 structure type) of the hypothetical "containing object" for a
14696 bit-field. Thus, when computing the byte offset value for the
14697 start of the "containing object" of a bit-field, we must deduce
14698 this information on our own. This can be rather tricky to do in
14699 some cases. For example, handling the following structure type
14700 definition when compiling for an i386/i486 target (which only
14701 aligns long long's to 32-bit boundaries) can be very tricky:
14703 struct S { int field1; long long field2:31; };
14705 Fortunately, there is a simple rule-of-thumb which can be used
14706 in such cases. When compiling for an i386/i486, GCC will
14707 allocate 8 bytes for the structure shown above. It decides to
14708 do this based upon one simple rule for bit-field allocation.
14709 GCC allocates each "containing object" for each bit-field at
14710 the first (i.e. lowest addressed) legitimate alignment boundary
14711 (based upon the required minimum alignment for the declared
14712 type of the field) which it can possibly use, subject to the
14713 condition that there is still enough available space remaining
14714 in the containing object (when allocated at the selected point)
14715 to fully accommodate all of the bits of the bit-field itself.
14717 This simple rule makes it obvious why GCC allocates 8 bytes for
14718 each object of the structure type shown above. When looking
14719 for a place to allocate the "containing object" for `field2',
14720 the compiler simply tries to allocate a 64-bit "containing
14721 object" at each successive 32-bit boundary (starting at zero)
14722 until it finds a place to allocate that 64- bit field such that
14723 at least 31 contiguous (and previously unallocated) bits remain
14724 within that selected 64 bit field. (As it turns out, for the
14725 example above, the compiler finds it is OK to allocate the
14726 "containing object" 64-bit field at bit-offset zero within the
14727 structure type.)
14729 Here we attempt to work backwards from the limited set of facts
14730 we're given, and we try to deduce from those facts, where GCC
14731 must have believed that the containing object started (within
14732 the structure type). The value we deduce is then used (by the
14733 callers of this routine) to generate DW_AT_location and
14734 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14735 the case of DW_AT_location, regular fields as well). */
14737 /* Figure out the bit-distance from the start of the structure to
14738 the "deepest" bit of the bit-field. */
14739 deepest_bitpos = bitpos_int + field_size_in_bits;
14741 /* This is the tricky part. Use some fancy footwork to deduce
14742 where the lowest addressed bit of the containing object must
14743 be. */
14744 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14746 /* Round up to type_align by default. This works best for
14747 bitfields. */
14748 object_offset_in_bits
14749 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14751 if (object_offset_in_bits.ugt (bitpos_int))
14753 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14755 /* Round up to decl_align instead. */
14756 object_offset_in_bits
14757 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14760 else
14761 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14762 object_offset_in_bits = bitpos_int;
14764 object_offset_in_bytes
14765 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14766 true, TRUNC_DIV_EXPR);
14767 return object_offset_in_bytes.to_shwi ();
14770 /* The following routines define various Dwarf attributes and any data
14771 associated with them. */
14773 /* Add a location description attribute value to a DIE.
14775 This emits location attributes suitable for whole variables and
14776 whole parameters. Note that the location attributes for struct fields are
14777 generated by the routine `data_member_location_attribute' below. */
14779 static inline void
14780 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14781 dw_loc_list_ref descr)
14783 if (descr == 0)
14784 return;
14785 if (single_element_loc_list_p (descr))
14786 add_AT_loc (die, attr_kind, descr->expr);
14787 else
14788 add_AT_loc_list (die, attr_kind, descr);
14791 /* Add DW_AT_accessibility attribute to DIE if needed. */
14793 static void
14794 add_accessibility_attribute (dw_die_ref die, tree decl)
14796 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14797 children, otherwise the default is DW_ACCESS_public. In DWARF2
14798 the default has always been DW_ACCESS_public. */
14799 if (TREE_PROTECTED (decl))
14800 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14801 else if (TREE_PRIVATE (decl))
14803 if (dwarf_version == 2
14804 || die->die_parent == NULL
14805 || die->die_parent->die_tag != DW_TAG_class_type)
14806 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14808 else if (dwarf_version > 2
14809 && die->die_parent
14810 && die->die_parent->die_tag == DW_TAG_class_type)
14811 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14814 /* Attach the specialized form of location attribute used for data members of
14815 struct and union types. In the special case of a FIELD_DECL node which
14816 represents a bit-field, the "offset" part of this special location
14817 descriptor must indicate the distance in bytes from the lowest-addressed
14818 byte of the containing struct or union type to the lowest-addressed byte of
14819 the "containing object" for the bit-field. (See the `field_byte_offset'
14820 function above).
14822 For any given bit-field, the "containing object" is a hypothetical object
14823 (of some integral or enum type) within which the given bit-field lives. The
14824 type of this hypothetical "containing object" is always the same as the
14825 declared type of the individual bit-field itself (for GCC anyway... the
14826 DWARF spec doesn't actually mandate this). Note that it is the size (in
14827 bytes) of the hypothetical "containing object" which will be given in the
14828 DW_AT_byte_size attribute for this bit-field. (See the
14829 `byte_size_attribute' function below.) It is also used when calculating the
14830 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14831 function below.) */
14833 static void
14834 add_data_member_location_attribute (dw_die_ref die, tree decl)
14836 HOST_WIDE_INT offset;
14837 dw_loc_descr_ref loc_descr = 0;
14839 if (TREE_CODE (decl) == TREE_BINFO)
14841 /* We're working on the TAG_inheritance for a base class. */
14842 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14844 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14845 aren't at a fixed offset from all (sub)objects of the same
14846 type. We need to extract the appropriate offset from our
14847 vtable. The following dwarf expression means
14849 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14851 This is specific to the V3 ABI, of course. */
14853 dw_loc_descr_ref tmp;
14855 /* Make a copy of the object address. */
14856 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14857 add_loc_descr (&loc_descr, tmp);
14859 /* Extract the vtable address. */
14860 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14861 add_loc_descr (&loc_descr, tmp);
14863 /* Calculate the address of the offset. */
14864 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14865 gcc_assert (offset < 0);
14867 tmp = int_loc_descriptor (-offset);
14868 add_loc_descr (&loc_descr, tmp);
14869 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14870 add_loc_descr (&loc_descr, tmp);
14872 /* Extract the offset. */
14873 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14874 add_loc_descr (&loc_descr, tmp);
14876 /* Add it to the object address. */
14877 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14878 add_loc_descr (&loc_descr, tmp);
14880 else
14881 offset = tree_to_shwi (BINFO_OFFSET (decl));
14883 else
14884 offset = field_byte_offset (decl);
14886 if (! loc_descr)
14888 if (dwarf_version > 2)
14890 /* Don't need to output a location expression, just the constant. */
14891 if (offset < 0)
14892 add_AT_int (die, DW_AT_data_member_location, offset);
14893 else
14894 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14895 return;
14897 else
14899 enum dwarf_location_atom op;
14901 /* The DWARF2 standard says that we should assume that the structure
14902 address is already on the stack, so we can specify a structure
14903 field address by using DW_OP_plus_uconst. */
14904 op = DW_OP_plus_uconst;
14905 loc_descr = new_loc_descr (op, offset, 0);
14909 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14912 /* Writes integer values to dw_vec_const array. */
14914 static void
14915 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14917 while (size != 0)
14919 *dest++ = val & 0xff;
14920 val >>= 8;
14921 --size;
14925 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14927 static HOST_WIDE_INT
14928 extract_int (const unsigned char *src, unsigned int size)
14930 HOST_WIDE_INT val = 0;
14932 src += size;
14933 while (size != 0)
14935 val <<= 8;
14936 val |= *--src & 0xff;
14937 --size;
14939 return val;
14942 /* Writes double_int values to dw_vec_const array. */
14944 static void
14945 insert_double (double_int val, unsigned char *dest)
14947 unsigned char *p0 = dest;
14948 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14950 if (WORDS_BIG_ENDIAN)
14952 p0 = p1;
14953 p1 = dest;
14956 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14957 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14960 /* Writes floating point values to dw_vec_const array. */
14962 static void
14963 insert_float (const_rtx rtl, unsigned char *array)
14965 REAL_VALUE_TYPE rv;
14966 long val[4];
14967 int i;
14969 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14970 real_to_target (val, &rv, GET_MODE (rtl));
14972 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14973 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14975 insert_int (val[i], 4, array);
14976 array += 4;
14980 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14981 does not have a "location" either in memory or in a register. These
14982 things can arise in GNU C when a constant is passed as an actual parameter
14983 to an inlined function. They can also arise in C++ where declared
14984 constants do not necessarily get memory "homes". */
14986 static bool
14987 add_const_value_attribute (dw_die_ref die, rtx rtl)
14989 switch (GET_CODE (rtl))
14991 case CONST_INT:
14993 HOST_WIDE_INT val = INTVAL (rtl);
14995 if (val < 0)
14996 add_AT_int (die, DW_AT_const_value, val);
14997 else
14998 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15000 return true;
15002 case CONST_DOUBLE:
15003 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15004 floating-point constant. A CONST_DOUBLE is used whenever the
15005 constant requires more than one word in order to be adequately
15006 represented. */
15008 enum machine_mode mode = GET_MODE (rtl);
15010 if (SCALAR_FLOAT_MODE_P (mode))
15012 unsigned int length = GET_MODE_SIZE (mode);
15013 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15015 insert_float (rtl, array);
15016 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15018 else
15019 add_AT_double (die, DW_AT_const_value,
15020 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15022 return true;
15024 case CONST_VECTOR:
15026 enum machine_mode mode = GET_MODE (rtl);
15027 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15028 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15029 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15030 (length * elt_size);
15031 unsigned int i;
15032 unsigned char *p;
15034 switch (GET_MODE_CLASS (mode))
15036 case MODE_VECTOR_INT:
15037 for (i = 0, p = array; i < length; i++, p += elt_size)
15039 rtx elt = CONST_VECTOR_ELT (rtl, i);
15040 double_int val = rtx_to_double_int (elt);
15042 if (elt_size <= sizeof (HOST_WIDE_INT))
15043 insert_int (val.to_shwi (), elt_size, p);
15044 else
15046 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15047 insert_double (val, p);
15050 break;
15052 case MODE_VECTOR_FLOAT:
15053 for (i = 0, p = array; i < length; i++, p += elt_size)
15055 rtx elt = CONST_VECTOR_ELT (rtl, i);
15056 insert_float (elt, p);
15058 break;
15060 default:
15061 gcc_unreachable ();
15064 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15066 return true;
15068 case CONST_STRING:
15069 if (dwarf_version >= 4 || !dwarf_strict)
15071 dw_loc_descr_ref loc_result;
15072 resolve_one_addr (&rtl, NULL);
15073 rtl_addr:
15074 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15075 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15076 add_AT_loc (die, DW_AT_location, loc_result);
15077 vec_safe_push (used_rtx_array, rtl);
15078 return true;
15080 return false;
15082 case CONST:
15083 if (CONSTANT_P (XEXP (rtl, 0)))
15084 return add_const_value_attribute (die, XEXP (rtl, 0));
15085 /* FALLTHROUGH */
15086 case SYMBOL_REF:
15087 if (!const_ok_for_output (rtl))
15088 return false;
15089 case LABEL_REF:
15090 if (dwarf_version >= 4 || !dwarf_strict)
15091 goto rtl_addr;
15092 return false;
15094 case PLUS:
15095 /* In cases where an inlined instance of an inline function is passed
15096 the address of an `auto' variable (which is local to the caller) we
15097 can get a situation where the DECL_RTL of the artificial local
15098 variable (for the inlining) which acts as a stand-in for the
15099 corresponding formal parameter (of the inline function) will look
15100 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15101 exactly a compile-time constant expression, but it isn't the address
15102 of the (artificial) local variable either. Rather, it represents the
15103 *value* which the artificial local variable always has during its
15104 lifetime. We currently have no way to represent such quasi-constant
15105 values in Dwarf, so for now we just punt and generate nothing. */
15106 return false;
15108 case HIGH:
15109 case CONST_FIXED:
15110 return false;
15112 case MEM:
15113 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15114 && MEM_READONLY_P (rtl)
15115 && GET_MODE (rtl) == BLKmode)
15117 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15118 return true;
15120 return false;
15122 default:
15123 /* No other kinds of rtx should be possible here. */
15124 gcc_unreachable ();
15126 return false;
15129 /* Determine whether the evaluation of EXPR references any variables
15130 or functions which aren't otherwise used (and therefore may not be
15131 output). */
15132 static tree
15133 reference_to_unused (tree * tp, int * walk_subtrees,
15134 void * data ATTRIBUTE_UNUSED)
15136 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15137 *walk_subtrees = 0;
15139 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15140 && ! TREE_ASM_WRITTEN (*tp))
15141 return *tp;
15142 /* ??? The C++ FE emits debug information for using decls, so
15143 putting gcc_unreachable here falls over. See PR31899. For now
15144 be conservative. */
15145 else if (!cgraph_global_info_ready
15146 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15147 return *tp;
15148 else if (TREE_CODE (*tp) == VAR_DECL)
15150 struct varpool_node *node = varpool_get_node (*tp);
15151 if (!node || !node->definition)
15152 return *tp;
15154 else if (TREE_CODE (*tp) == FUNCTION_DECL
15155 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15157 /* The call graph machinery must have finished analyzing,
15158 optimizing and gimplifying the CU by now.
15159 So if *TP has no call graph node associated
15160 to it, it means *TP will not be emitted. */
15161 if (!cgraph_get_node (*tp))
15162 return *tp;
15164 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15165 return *tp;
15167 return NULL_TREE;
15170 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15171 for use in a later add_const_value_attribute call. */
15173 static rtx
15174 rtl_for_decl_init (tree init, tree type)
15176 rtx rtl = NULL_RTX;
15178 STRIP_NOPS (init);
15180 /* If a variable is initialized with a string constant without embedded
15181 zeros, build CONST_STRING. */
15182 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15184 tree enttype = TREE_TYPE (type);
15185 tree domain = TYPE_DOMAIN (type);
15186 enum machine_mode mode = TYPE_MODE (enttype);
15188 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15189 && domain
15190 && integer_zerop (TYPE_MIN_VALUE (domain))
15191 && compare_tree_int (TYPE_MAX_VALUE (domain),
15192 TREE_STRING_LENGTH (init) - 1) == 0
15193 && ((size_t) TREE_STRING_LENGTH (init)
15194 == strlen (TREE_STRING_POINTER (init)) + 1))
15196 rtl = gen_rtx_CONST_STRING (VOIDmode,
15197 ggc_strdup (TREE_STRING_POINTER (init)));
15198 rtl = gen_rtx_MEM (BLKmode, rtl);
15199 MEM_READONLY_P (rtl) = 1;
15202 /* Other aggregates, and complex values, could be represented using
15203 CONCAT: FIXME! */
15204 else if (AGGREGATE_TYPE_P (type)
15205 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15206 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15207 || TREE_CODE (type) == COMPLEX_TYPE)
15209 /* Vectors only work if their mode is supported by the target.
15210 FIXME: generic vectors ought to work too. */
15211 else if (TREE_CODE (type) == VECTOR_TYPE
15212 && !VECTOR_MODE_P (TYPE_MODE (type)))
15214 /* If the initializer is something that we know will expand into an
15215 immediate RTL constant, expand it now. We must be careful not to
15216 reference variables which won't be output. */
15217 else if (initializer_constant_valid_p (init, type)
15218 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15220 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15221 possible. */
15222 if (TREE_CODE (type) == VECTOR_TYPE)
15223 switch (TREE_CODE (init))
15225 case VECTOR_CST:
15226 break;
15227 case CONSTRUCTOR:
15228 if (TREE_CONSTANT (init))
15230 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15231 bool constant_p = true;
15232 tree value;
15233 unsigned HOST_WIDE_INT ix;
15235 /* Even when ctor is constant, it might contain non-*_CST
15236 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15237 belong into VECTOR_CST nodes. */
15238 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15239 if (!CONSTANT_CLASS_P (value))
15241 constant_p = false;
15242 break;
15245 if (constant_p)
15247 init = build_vector_from_ctor (type, elts);
15248 break;
15251 /* FALLTHRU */
15253 default:
15254 return NULL;
15257 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15259 /* If expand_expr returns a MEM, it wasn't immediate. */
15260 gcc_assert (!rtl || !MEM_P (rtl));
15263 return rtl;
15266 /* Generate RTL for the variable DECL to represent its location. */
15268 static rtx
15269 rtl_for_decl_location (tree decl)
15271 rtx rtl;
15273 /* Here we have to decide where we are going to say the parameter "lives"
15274 (as far as the debugger is concerned). We only have a couple of
15275 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15277 DECL_RTL normally indicates where the parameter lives during most of the
15278 activation of the function. If optimization is enabled however, this
15279 could be either NULL or else a pseudo-reg. Both of those cases indicate
15280 that the parameter doesn't really live anywhere (as far as the code
15281 generation parts of GCC are concerned) during most of the function's
15282 activation. That will happen (for example) if the parameter is never
15283 referenced within the function.
15285 We could just generate a location descriptor here for all non-NULL
15286 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15287 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15288 where DECL_RTL is NULL or is a pseudo-reg.
15290 Note however that we can only get away with using DECL_INCOMING_RTL as
15291 a backup substitute for DECL_RTL in certain limited cases. In cases
15292 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15293 we can be sure that the parameter was passed using the same type as it is
15294 declared to have within the function, and that its DECL_INCOMING_RTL
15295 points us to a place where a value of that type is passed.
15297 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15298 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15299 because in these cases DECL_INCOMING_RTL points us to a value of some
15300 type which is *different* from the type of the parameter itself. Thus,
15301 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15302 such cases, the debugger would end up (for example) trying to fetch a
15303 `float' from a place which actually contains the first part of a
15304 `double'. That would lead to really incorrect and confusing
15305 output at debug-time.
15307 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15308 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15309 are a couple of exceptions however. On little-endian machines we can
15310 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15311 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15312 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15313 when (on a little-endian machine) a non-prototyped function has a
15314 parameter declared to be of type `short' or `char'. In such cases,
15315 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15316 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15317 passed `int' value. If the debugger then uses that address to fetch
15318 a `short' or a `char' (on a little-endian machine) the result will be
15319 the correct data, so we allow for such exceptional cases below.
15321 Note that our goal here is to describe the place where the given formal
15322 parameter lives during most of the function's activation (i.e. between the
15323 end of the prologue and the start of the epilogue). We'll do that as best
15324 as we can. Note however that if the given formal parameter is modified
15325 sometime during the execution of the function, then a stack backtrace (at
15326 debug-time) will show the function as having been called with the *new*
15327 value rather than the value which was originally passed in. This happens
15328 rarely enough that it is not a major problem, but it *is* a problem, and
15329 I'd like to fix it.
15331 A future version of dwarf2out.c may generate two additional attributes for
15332 any given DW_TAG_formal_parameter DIE which will describe the "passed
15333 type" and the "passed location" for the given formal parameter in addition
15334 to the attributes we now generate to indicate the "declared type" and the
15335 "active location" for each parameter. This additional set of attributes
15336 could be used by debuggers for stack backtraces. Separately, note that
15337 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15338 This happens (for example) for inlined-instances of inline function formal
15339 parameters which are never referenced. This really shouldn't be
15340 happening. All PARM_DECL nodes should get valid non-NULL
15341 DECL_INCOMING_RTL values. FIXME. */
15343 /* Use DECL_RTL as the "location" unless we find something better. */
15344 rtl = DECL_RTL_IF_SET (decl);
15346 /* When generating abstract instances, ignore everything except
15347 constants, symbols living in memory, and symbols living in
15348 fixed registers. */
15349 if (! reload_completed)
15351 if (rtl
15352 && (CONSTANT_P (rtl)
15353 || (MEM_P (rtl)
15354 && CONSTANT_P (XEXP (rtl, 0)))
15355 || (REG_P (rtl)
15356 && TREE_CODE (decl) == VAR_DECL
15357 && TREE_STATIC (decl))))
15359 rtl = targetm.delegitimize_address (rtl);
15360 return rtl;
15362 rtl = NULL_RTX;
15364 else if (TREE_CODE (decl) == PARM_DECL)
15366 if (rtl == NULL_RTX
15367 || is_pseudo_reg (rtl)
15368 || (MEM_P (rtl)
15369 && is_pseudo_reg (XEXP (rtl, 0))
15370 && DECL_INCOMING_RTL (decl)
15371 && MEM_P (DECL_INCOMING_RTL (decl))
15372 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15374 tree declared_type = TREE_TYPE (decl);
15375 tree passed_type = DECL_ARG_TYPE (decl);
15376 enum machine_mode dmode = TYPE_MODE (declared_type);
15377 enum machine_mode pmode = TYPE_MODE (passed_type);
15379 /* This decl represents a formal parameter which was optimized out.
15380 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15381 all cases where (rtl == NULL_RTX) just below. */
15382 if (dmode == pmode)
15383 rtl = DECL_INCOMING_RTL (decl);
15384 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15385 && SCALAR_INT_MODE_P (dmode)
15386 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15387 && DECL_INCOMING_RTL (decl))
15389 rtx inc = DECL_INCOMING_RTL (decl);
15390 if (REG_P (inc))
15391 rtl = inc;
15392 else if (MEM_P (inc))
15394 if (BYTES_BIG_ENDIAN)
15395 rtl = adjust_address_nv (inc, dmode,
15396 GET_MODE_SIZE (pmode)
15397 - GET_MODE_SIZE (dmode));
15398 else
15399 rtl = inc;
15404 /* If the parm was passed in registers, but lives on the stack, then
15405 make a big endian correction if the mode of the type of the
15406 parameter is not the same as the mode of the rtl. */
15407 /* ??? This is the same series of checks that are made in dbxout.c before
15408 we reach the big endian correction code there. It isn't clear if all
15409 of these checks are necessary here, but keeping them all is the safe
15410 thing to do. */
15411 else if (MEM_P (rtl)
15412 && XEXP (rtl, 0) != const0_rtx
15413 && ! CONSTANT_P (XEXP (rtl, 0))
15414 /* Not passed in memory. */
15415 && !MEM_P (DECL_INCOMING_RTL (decl))
15416 /* Not passed by invisible reference. */
15417 && (!REG_P (XEXP (rtl, 0))
15418 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15419 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15420 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15421 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15422 #endif
15424 /* Big endian correction check. */
15425 && BYTES_BIG_ENDIAN
15426 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15427 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15428 < UNITS_PER_WORD))
15430 enum machine_mode addr_mode = get_address_mode (rtl);
15431 int offset = (UNITS_PER_WORD
15432 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15434 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15435 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15438 else if (TREE_CODE (decl) == VAR_DECL
15439 && rtl
15440 && MEM_P (rtl)
15441 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15442 && BYTES_BIG_ENDIAN)
15444 enum machine_mode addr_mode = get_address_mode (rtl);
15445 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15446 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15448 /* If a variable is declared "register" yet is smaller than
15449 a register, then if we store the variable to memory, it
15450 looks like we're storing a register-sized value, when in
15451 fact we are not. We need to adjust the offset of the
15452 storage location to reflect the actual value's bytes,
15453 else gdb will not be able to display it. */
15454 if (rsize > dsize)
15455 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15456 plus_constant (addr_mode, XEXP (rtl, 0),
15457 rsize - dsize));
15460 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15461 and will have been substituted directly into all expressions that use it.
15462 C does not have such a concept, but C++ and other languages do. */
15463 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15464 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15466 if (rtl)
15467 rtl = targetm.delegitimize_address (rtl);
15469 /* If we don't look past the constant pool, we risk emitting a
15470 reference to a constant pool entry that isn't referenced from
15471 code, and thus is not emitted. */
15472 if (rtl)
15473 rtl = avoid_constant_pool_reference (rtl);
15475 /* Try harder to get a rtl. If this symbol ends up not being emitted
15476 in the current CU, resolve_addr will remove the expression referencing
15477 it. */
15478 if (rtl == NULL_RTX
15479 && TREE_CODE (decl) == VAR_DECL
15480 && !DECL_EXTERNAL (decl)
15481 && TREE_STATIC (decl)
15482 && DECL_NAME (decl)
15483 && !DECL_HARD_REGISTER (decl)
15484 && DECL_MODE (decl) != VOIDmode)
15486 rtl = make_decl_rtl_for_debug (decl);
15487 if (!MEM_P (rtl)
15488 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15489 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15490 rtl = NULL_RTX;
15493 return rtl;
15496 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15497 returned. If so, the decl for the COMMON block is returned, and the
15498 value is the offset into the common block for the symbol. */
15500 static tree
15501 fortran_common (tree decl, HOST_WIDE_INT *value)
15503 tree val_expr, cvar;
15504 enum machine_mode mode;
15505 HOST_WIDE_INT bitsize, bitpos;
15506 tree offset;
15507 int unsignedp, volatilep = 0;
15509 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15510 it does not have a value (the offset into the common area), or if it
15511 is thread local (as opposed to global) then it isn't common, and shouldn't
15512 be handled as such. */
15513 if (TREE_CODE (decl) != VAR_DECL
15514 || !TREE_STATIC (decl)
15515 || !DECL_HAS_VALUE_EXPR_P (decl)
15516 || !is_fortran ())
15517 return NULL_TREE;
15519 val_expr = DECL_VALUE_EXPR (decl);
15520 if (TREE_CODE (val_expr) != COMPONENT_REF)
15521 return NULL_TREE;
15523 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15524 &mode, &unsignedp, &volatilep, true);
15526 if (cvar == NULL_TREE
15527 || TREE_CODE (cvar) != VAR_DECL
15528 || DECL_ARTIFICIAL (cvar)
15529 || !TREE_PUBLIC (cvar))
15530 return NULL_TREE;
15532 *value = 0;
15533 if (offset != NULL)
15535 if (!tree_fits_shwi_p (offset))
15536 return NULL_TREE;
15537 *value = tree_to_shwi (offset);
15539 if (bitpos != 0)
15540 *value += bitpos / BITS_PER_UNIT;
15542 return cvar;
15545 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15546 data attribute for a variable or a parameter. We generate the
15547 DW_AT_const_value attribute only in those cases where the given variable
15548 or parameter does not have a true "location" either in memory or in a
15549 register. This can happen (for example) when a constant is passed as an
15550 actual argument in a call to an inline function. (It's possible that
15551 these things can crop up in other ways also.) Note that one type of
15552 constant value which can be passed into an inlined function is a constant
15553 pointer. This can happen for example if an actual argument in an inlined
15554 function call evaluates to a compile-time constant address.
15556 CACHE_P is true if it is worth caching the location list for DECL,
15557 so that future calls can reuse it rather than regenerate it from scratch.
15558 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15559 since we will need to refer to them each time the function is inlined. */
15561 static bool
15562 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15563 enum dwarf_attribute attr)
15565 rtx rtl;
15566 dw_loc_list_ref list;
15567 var_loc_list *loc_list;
15568 cached_dw_loc_list *cache;
15569 void **slot;
15571 if (TREE_CODE (decl) == ERROR_MARK)
15572 return false;
15574 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15575 || TREE_CODE (decl) == RESULT_DECL);
15577 /* Try to get some constant RTL for this decl, and use that as the value of
15578 the location. */
15580 rtl = rtl_for_decl_location (decl);
15581 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15582 && add_const_value_attribute (die, rtl))
15583 return true;
15585 /* See if we have single element location list that is equivalent to
15586 a constant value. That way we are better to use add_const_value_attribute
15587 rather than expanding constant value equivalent. */
15588 loc_list = lookup_decl_loc (decl);
15589 if (loc_list
15590 && loc_list->first
15591 && loc_list->first->next == NULL
15592 && NOTE_P (loc_list->first->loc)
15593 && NOTE_VAR_LOCATION (loc_list->first->loc)
15594 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15596 struct var_loc_node *node;
15598 node = loc_list->first;
15599 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15600 if (GET_CODE (rtl) == EXPR_LIST)
15601 rtl = XEXP (rtl, 0);
15602 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15603 && add_const_value_attribute (die, rtl))
15604 return true;
15606 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15607 list several times. See if we've already cached the contents. */
15608 list = NULL;
15609 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15610 cache_p = false;
15611 if (cache_p)
15613 cache = (cached_dw_loc_list *)
15614 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15615 if (cache)
15616 list = cache->loc_list;
15618 if (list == NULL)
15620 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15621 /* It is usually worth caching this result if the decl is from
15622 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15623 if (cache_p && list && list->dw_loc_next)
15625 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15626 DECL_UID (decl), INSERT);
15627 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15628 cache->decl_id = DECL_UID (decl);
15629 cache->loc_list = list;
15630 *slot = cache;
15633 if (list)
15635 add_AT_location_description (die, attr, list);
15636 return true;
15638 /* None of that worked, so it must not really have a location;
15639 try adding a constant value attribute from the DECL_INITIAL. */
15640 return tree_add_const_value_attribute_for_decl (die, decl);
15643 /* Add VARIABLE and DIE into deferred locations list. */
15645 static void
15646 defer_location (tree variable, dw_die_ref die)
15648 deferred_locations entry;
15649 entry.variable = variable;
15650 entry.die = die;
15651 vec_safe_push (deferred_locations_list, entry);
15654 /* Helper function for tree_add_const_value_attribute. Natively encode
15655 initializer INIT into an array. Return true if successful. */
15657 static bool
15658 native_encode_initializer (tree init, unsigned char *array, int size)
15660 tree type;
15662 if (init == NULL_TREE)
15663 return false;
15665 STRIP_NOPS (init);
15666 switch (TREE_CODE (init))
15668 case STRING_CST:
15669 type = TREE_TYPE (init);
15670 if (TREE_CODE (type) == ARRAY_TYPE)
15672 tree enttype = TREE_TYPE (type);
15673 enum machine_mode mode = TYPE_MODE (enttype);
15675 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15676 return false;
15677 if (int_size_in_bytes (type) != size)
15678 return false;
15679 if (size > TREE_STRING_LENGTH (init))
15681 memcpy (array, TREE_STRING_POINTER (init),
15682 TREE_STRING_LENGTH (init));
15683 memset (array + TREE_STRING_LENGTH (init),
15684 '\0', size - TREE_STRING_LENGTH (init));
15686 else
15687 memcpy (array, TREE_STRING_POINTER (init), size);
15688 return true;
15690 return false;
15691 case CONSTRUCTOR:
15692 type = TREE_TYPE (init);
15693 if (int_size_in_bytes (type) != size)
15694 return false;
15695 if (TREE_CODE (type) == ARRAY_TYPE)
15697 HOST_WIDE_INT min_index;
15698 unsigned HOST_WIDE_INT cnt;
15699 int curpos = 0, fieldsize;
15700 constructor_elt *ce;
15702 if (TYPE_DOMAIN (type) == NULL_TREE
15703 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15704 return false;
15706 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15707 if (fieldsize <= 0)
15708 return false;
15710 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15711 memset (array, '\0', size);
15712 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15714 tree val = ce->value;
15715 tree index = ce->index;
15716 int pos = curpos;
15717 if (index && TREE_CODE (index) == RANGE_EXPR)
15718 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15719 * fieldsize;
15720 else if (index)
15721 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15723 if (val)
15725 STRIP_NOPS (val);
15726 if (!native_encode_initializer (val, array + pos, fieldsize))
15727 return false;
15729 curpos = pos + fieldsize;
15730 if (index && TREE_CODE (index) == RANGE_EXPR)
15732 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15733 - tree_to_shwi (TREE_OPERAND (index, 0));
15734 while (count-- > 0)
15736 if (val)
15737 memcpy (array + curpos, array + pos, fieldsize);
15738 curpos += fieldsize;
15741 gcc_assert (curpos <= size);
15743 return true;
15745 else if (TREE_CODE (type) == RECORD_TYPE
15746 || TREE_CODE (type) == UNION_TYPE)
15748 tree field = NULL_TREE;
15749 unsigned HOST_WIDE_INT cnt;
15750 constructor_elt *ce;
15752 if (int_size_in_bytes (type) != size)
15753 return false;
15755 if (TREE_CODE (type) == RECORD_TYPE)
15756 field = TYPE_FIELDS (type);
15758 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15760 tree val = ce->value;
15761 int pos, fieldsize;
15763 if (ce->index != 0)
15764 field = ce->index;
15766 if (val)
15767 STRIP_NOPS (val);
15769 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15770 return false;
15772 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15773 && TYPE_DOMAIN (TREE_TYPE (field))
15774 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15775 return false;
15776 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15777 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15778 return false;
15779 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15780 pos = int_byte_position (field);
15781 gcc_assert (pos + fieldsize <= size);
15782 if (val
15783 && !native_encode_initializer (val, array + pos, fieldsize))
15784 return false;
15786 return true;
15788 return false;
15789 case VIEW_CONVERT_EXPR:
15790 case NON_LVALUE_EXPR:
15791 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15792 default:
15793 return native_encode_expr (init, array, size) == size;
15797 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15798 attribute is the const value T. */
15800 static bool
15801 tree_add_const_value_attribute (dw_die_ref die, tree t)
15803 tree init;
15804 tree type = TREE_TYPE (t);
15805 rtx rtl;
15807 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15808 return false;
15810 init = t;
15811 gcc_assert (!DECL_P (init));
15813 rtl = rtl_for_decl_init (init, type);
15814 if (rtl)
15815 return add_const_value_attribute (die, rtl);
15816 /* If the host and target are sane, try harder. */
15817 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15818 && initializer_constant_valid_p (init, type))
15820 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15821 if (size > 0 && (int) size == size)
15823 unsigned char *array = (unsigned char *)
15824 ggc_alloc_cleared_atomic (size);
15826 if (native_encode_initializer (init, array, size))
15828 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15829 return true;
15831 ggc_free (array);
15834 return false;
15837 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15838 attribute is the const value of T, where T is an integral constant
15839 variable with static storage duration
15840 (so it can't be a PARM_DECL or a RESULT_DECL). */
15842 static bool
15843 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15846 if (!decl
15847 || (TREE_CODE (decl) != VAR_DECL
15848 && TREE_CODE (decl) != CONST_DECL)
15849 || (TREE_CODE (decl) == VAR_DECL
15850 && !TREE_STATIC (decl)))
15851 return false;
15853 if (TREE_READONLY (decl)
15854 && ! TREE_THIS_VOLATILE (decl)
15855 && DECL_INITIAL (decl))
15856 /* OK */;
15857 else
15858 return false;
15860 /* Don't add DW_AT_const_value if abstract origin already has one. */
15861 if (get_AT (var_die, DW_AT_const_value))
15862 return false;
15864 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15867 /* Convert the CFI instructions for the current function into a
15868 location list. This is used for DW_AT_frame_base when we targeting
15869 a dwarf2 consumer that does not support the dwarf3
15870 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15871 expressions. */
15873 static dw_loc_list_ref
15874 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15876 int ix;
15877 dw_fde_ref fde;
15878 dw_loc_list_ref list, *list_tail;
15879 dw_cfi_ref cfi;
15880 dw_cfa_location last_cfa, next_cfa;
15881 const char *start_label, *last_label, *section;
15882 dw_cfa_location remember;
15884 fde = cfun->fde;
15885 gcc_assert (fde != NULL);
15887 section = secname_for_decl (current_function_decl);
15888 list_tail = &list;
15889 list = NULL;
15891 memset (&next_cfa, 0, sizeof (next_cfa));
15892 next_cfa.reg = INVALID_REGNUM;
15893 remember = next_cfa;
15895 start_label = fde->dw_fde_begin;
15897 /* ??? Bald assumption that the CIE opcode list does not contain
15898 advance opcodes. */
15899 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15900 lookup_cfa_1 (cfi, &next_cfa, &remember);
15902 last_cfa = next_cfa;
15903 last_label = start_label;
15905 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15907 /* If the first partition contained no CFI adjustments, the
15908 CIE opcodes apply to the whole first partition. */
15909 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15910 fde->dw_fde_begin, fde->dw_fde_end, section);
15911 list_tail =&(*list_tail)->dw_loc_next;
15912 start_label = last_label = fde->dw_fde_second_begin;
15915 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15917 switch (cfi->dw_cfi_opc)
15919 case DW_CFA_set_loc:
15920 case DW_CFA_advance_loc1:
15921 case DW_CFA_advance_loc2:
15922 case DW_CFA_advance_loc4:
15923 if (!cfa_equal_p (&last_cfa, &next_cfa))
15925 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15926 start_label, last_label, section);
15928 list_tail = &(*list_tail)->dw_loc_next;
15929 last_cfa = next_cfa;
15930 start_label = last_label;
15932 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15933 break;
15935 case DW_CFA_advance_loc:
15936 /* The encoding is complex enough that we should never emit this. */
15937 gcc_unreachable ();
15939 default:
15940 lookup_cfa_1 (cfi, &next_cfa, &remember);
15941 break;
15943 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15945 if (!cfa_equal_p (&last_cfa, &next_cfa))
15947 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15948 start_label, last_label, section);
15950 list_tail = &(*list_tail)->dw_loc_next;
15951 last_cfa = next_cfa;
15952 start_label = last_label;
15954 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15955 start_label, fde->dw_fde_end, section);
15956 list_tail = &(*list_tail)->dw_loc_next;
15957 start_label = last_label = fde->dw_fde_second_begin;
15961 if (!cfa_equal_p (&last_cfa, &next_cfa))
15963 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15964 start_label, last_label, section);
15965 list_tail = &(*list_tail)->dw_loc_next;
15966 start_label = last_label;
15969 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15970 start_label,
15971 fde->dw_fde_second_begin
15972 ? fde->dw_fde_second_end : fde->dw_fde_end,
15973 section);
15975 if (list && list->dw_loc_next)
15976 gen_llsym (list);
15978 return list;
15981 /* Compute a displacement from the "steady-state frame pointer" to the
15982 frame base (often the same as the CFA), and store it in
15983 frame_pointer_fb_offset. OFFSET is added to the displacement
15984 before the latter is negated. */
15986 static void
15987 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15989 rtx reg, elim;
15991 #ifdef FRAME_POINTER_CFA_OFFSET
15992 reg = frame_pointer_rtx;
15993 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15994 #else
15995 reg = arg_pointer_rtx;
15996 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15997 #endif
15999 elim = (ira_use_lra_p
16000 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16001 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16002 if (GET_CODE (elim) == PLUS)
16004 offset += INTVAL (XEXP (elim, 1));
16005 elim = XEXP (elim, 0);
16008 frame_pointer_fb_offset = -offset;
16010 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16011 in which to eliminate. This is because it's stack pointer isn't
16012 directly accessible as a register within the ISA. To work around
16013 this, assume that while we cannot provide a proper value for
16014 frame_pointer_fb_offset, we won't need one either. */
16015 frame_pointer_fb_offset_valid
16016 = ((SUPPORTS_STACK_ALIGNMENT
16017 && (elim == hard_frame_pointer_rtx
16018 || elim == stack_pointer_rtx))
16019 || elim == (frame_pointer_needed
16020 ? hard_frame_pointer_rtx
16021 : stack_pointer_rtx));
16024 /* Generate a DW_AT_name attribute given some string value to be included as
16025 the value of the attribute. */
16027 static void
16028 add_name_attribute (dw_die_ref die, const char *name_string)
16030 if (name_string != NULL && *name_string != 0)
16032 if (demangle_name_func)
16033 name_string = (*demangle_name_func) (name_string);
16035 add_AT_string (die, DW_AT_name, name_string);
16039 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16040 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16041 of TYPE accordingly.
16043 ??? This is a temporary measure until after we're able to generate
16044 regular DWARF for the complex Ada type system. */
16046 static void
16047 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16048 dw_die_ref context_die)
16050 tree dtype;
16051 dw_die_ref dtype_die;
16053 if (!lang_hooks.types.descriptive_type)
16054 return;
16056 dtype = lang_hooks.types.descriptive_type (type);
16057 if (!dtype)
16058 return;
16060 dtype_die = lookup_type_die (dtype);
16061 if (!dtype_die)
16063 gen_type_die (dtype, context_die);
16064 dtype_die = lookup_type_die (dtype);
16065 gcc_assert (dtype_die);
16068 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16071 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16073 static const char *
16074 comp_dir_string (void)
16076 const char *wd;
16077 char *wd1;
16078 static const char *cached_wd = NULL;
16080 if (cached_wd != NULL)
16081 return cached_wd;
16083 wd = get_src_pwd ();
16084 if (wd == NULL)
16085 return NULL;
16087 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16089 int wdlen;
16091 wdlen = strlen (wd);
16092 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16093 strcpy (wd1, wd);
16094 wd1 [wdlen] = DIR_SEPARATOR;
16095 wd1 [wdlen + 1] = 0;
16096 wd = wd1;
16099 cached_wd = remap_debug_filename (wd);
16100 return cached_wd;
16103 /* Generate a DW_AT_comp_dir attribute for DIE. */
16105 static void
16106 add_comp_dir_attribute (dw_die_ref die)
16108 const char * wd = comp_dir_string ();
16109 if (wd != NULL)
16110 add_AT_string (die, DW_AT_comp_dir, wd);
16113 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16114 default. */
16116 static int
16117 lower_bound_default (void)
16119 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16121 case DW_LANG_C:
16122 case DW_LANG_C89:
16123 case DW_LANG_C99:
16124 case DW_LANG_C_plus_plus:
16125 case DW_LANG_ObjC:
16126 case DW_LANG_ObjC_plus_plus:
16127 case DW_LANG_Java:
16128 return 0;
16129 case DW_LANG_Fortran77:
16130 case DW_LANG_Fortran90:
16131 case DW_LANG_Fortran95:
16132 return 1;
16133 case DW_LANG_UPC:
16134 case DW_LANG_D:
16135 case DW_LANG_Python:
16136 return dwarf_version >= 4 ? 0 : -1;
16137 case DW_LANG_Ada95:
16138 case DW_LANG_Ada83:
16139 case DW_LANG_Cobol74:
16140 case DW_LANG_Cobol85:
16141 case DW_LANG_Pascal83:
16142 case DW_LANG_Modula2:
16143 case DW_LANG_PLI:
16144 return dwarf_version >= 4 ? 1 : -1;
16145 default:
16146 return -1;
16150 /* Given a tree node describing an array bound (either lower or upper) output
16151 a representation for that bound. */
16153 static void
16154 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16156 switch (TREE_CODE (bound))
16158 case ERROR_MARK:
16159 return;
16161 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16162 case INTEGER_CST:
16164 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16165 int dflt;
16167 /* Use the default if possible. */
16168 if (bound_attr == DW_AT_lower_bound
16169 && tree_fits_shwi_p (bound)
16170 && (dflt = lower_bound_default ()) != -1
16171 && tree_to_shwi (bound) == dflt)
16174 /* Otherwise represent the bound as an unsigned value with the
16175 precision of its type. The precision and signedness of the
16176 type will be necessary to re-interpret it unambiguously. */
16177 else if (prec < HOST_BITS_PER_WIDE_INT)
16179 unsigned HOST_WIDE_INT mask
16180 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16181 add_AT_unsigned (subrange_die, bound_attr,
16182 TREE_INT_CST_LOW (bound) & mask);
16184 else if (prec == HOST_BITS_PER_WIDE_INT
16185 || TREE_INT_CST_HIGH (bound) == 0)
16186 add_AT_unsigned (subrange_die, bound_attr,
16187 TREE_INT_CST_LOW (bound));
16188 else
16189 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16190 TREE_INT_CST_LOW (bound));
16192 break;
16194 CASE_CONVERT:
16195 case VIEW_CONVERT_EXPR:
16196 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16197 break;
16199 case SAVE_EXPR:
16200 break;
16202 case VAR_DECL:
16203 case PARM_DECL:
16204 case RESULT_DECL:
16206 dw_die_ref decl_die = lookup_decl_die (bound);
16208 /* ??? Can this happen, or should the variable have been bound
16209 first? Probably it can, since I imagine that we try to create
16210 the types of parameters in the order in which they exist in
16211 the list, and won't have created a forward reference to a
16212 later parameter. */
16213 if (decl_die != NULL)
16215 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16216 break;
16219 /* FALLTHRU */
16221 default:
16223 /* Otherwise try to create a stack operation procedure to
16224 evaluate the value of the array bound. */
16226 dw_die_ref ctx, decl_die;
16227 dw_loc_list_ref list;
16229 list = loc_list_from_tree (bound, 2);
16230 if (list == NULL || single_element_loc_list_p (list))
16232 /* If DW_AT_*bound is not a reference nor constant, it is
16233 a DWARF expression rather than location description.
16234 For that loc_list_from_tree (bound, 0) is needed.
16235 If that fails to give a single element list,
16236 fall back to outputting this as a reference anyway. */
16237 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16238 if (list2 && single_element_loc_list_p (list2))
16240 add_AT_loc (subrange_die, bound_attr, list2->expr);
16241 break;
16244 if (list == NULL)
16245 break;
16247 if (current_function_decl == 0)
16248 ctx = comp_unit_die ();
16249 else
16250 ctx = lookup_decl_die (current_function_decl);
16252 decl_die = new_die (DW_TAG_variable, ctx, bound);
16253 add_AT_flag (decl_die, DW_AT_artificial, 1);
16254 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16255 add_AT_location_description (decl_die, DW_AT_location, list);
16256 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16257 break;
16262 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16263 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16264 Note that the block of subscript information for an array type also
16265 includes information about the element type of the given array type. */
16267 static void
16268 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16270 unsigned dimension_number;
16271 tree lower, upper;
16272 dw_die_ref subrange_die;
16274 for (dimension_number = 0;
16275 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16276 type = TREE_TYPE (type), dimension_number++)
16278 tree domain = TYPE_DOMAIN (type);
16280 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16281 break;
16283 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16284 and (in GNU C only) variable bounds. Handle all three forms
16285 here. */
16286 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16287 if (domain)
16289 /* We have an array type with specified bounds. */
16290 lower = TYPE_MIN_VALUE (domain);
16291 upper = TYPE_MAX_VALUE (domain);
16293 /* Define the index type. */
16294 if (TREE_TYPE (domain))
16296 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16297 TREE_TYPE field. We can't emit debug info for this
16298 because it is an unnamed integral type. */
16299 if (TREE_CODE (domain) == INTEGER_TYPE
16300 && TYPE_NAME (domain) == NULL_TREE
16301 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16302 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16304 else
16305 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16306 type_die);
16309 /* ??? If upper is NULL, the array has unspecified length,
16310 but it does have a lower bound. This happens with Fortran
16311 dimension arr(N:*)
16312 Since the debugger is definitely going to need to know N
16313 to produce useful results, go ahead and output the lower
16314 bound solo, and hope the debugger can cope. */
16316 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16317 if (upper)
16318 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16321 /* Otherwise we have an array type with an unspecified length. The
16322 DWARF-2 spec does not say how to handle this; let's just leave out the
16323 bounds. */
16327 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16329 static void
16330 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16332 dw_die_ref decl_die;
16333 HOST_WIDE_INT size;
16335 switch (TREE_CODE (tree_node))
16337 case ERROR_MARK:
16338 size = 0;
16339 break;
16340 case ENUMERAL_TYPE:
16341 case RECORD_TYPE:
16342 case UNION_TYPE:
16343 case QUAL_UNION_TYPE:
16344 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16345 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16347 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16348 return;
16350 size = int_size_in_bytes (tree_node);
16351 break;
16352 case FIELD_DECL:
16353 /* For a data member of a struct or union, the DW_AT_byte_size is
16354 generally given as the number of bytes normally allocated for an
16355 object of the *declared* type of the member itself. This is true
16356 even for bit-fields. */
16357 size = int_size_in_bytes (field_type (tree_node));
16358 break;
16359 default:
16360 gcc_unreachable ();
16363 /* Note that `size' might be -1 when we get to this point. If it is, that
16364 indicates that the byte size of the entity in question is variable. We
16365 have no good way of expressing this fact in Dwarf at the present time,
16366 when location description was not used by the caller code instead. */
16367 if (size >= 0)
16368 add_AT_unsigned (die, DW_AT_byte_size, size);
16371 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16372 which specifies the distance in bits from the highest order bit of the
16373 "containing object" for the bit-field to the highest order bit of the
16374 bit-field itself.
16376 For any given bit-field, the "containing object" is a hypothetical object
16377 (of some integral or enum type) within which the given bit-field lives. The
16378 type of this hypothetical "containing object" is always the same as the
16379 declared type of the individual bit-field itself. The determination of the
16380 exact location of the "containing object" for a bit-field is rather
16381 complicated. It's handled by the `field_byte_offset' function (above).
16383 Note that it is the size (in bytes) of the hypothetical "containing object"
16384 which will be given in the DW_AT_byte_size attribute for this bit-field.
16385 (See `byte_size_attribute' above). */
16387 static inline void
16388 add_bit_offset_attribute (dw_die_ref die, tree decl)
16390 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16391 tree type = DECL_BIT_FIELD_TYPE (decl);
16392 HOST_WIDE_INT bitpos_int;
16393 HOST_WIDE_INT highest_order_object_bit_offset;
16394 HOST_WIDE_INT highest_order_field_bit_offset;
16395 HOST_WIDE_INT bit_offset;
16397 /* Must be a field and a bit field. */
16398 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16400 /* We can't yet handle bit-fields whose offsets are variable, so if we
16401 encounter such things, just return without generating any attribute
16402 whatsoever. Likewise for variable or too large size. */
16403 if (! tree_fits_shwi_p (bit_position (decl))
16404 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16405 return;
16407 bitpos_int = int_bit_position (decl);
16409 /* Note that the bit offset is always the distance (in bits) from the
16410 highest-order bit of the "containing object" to the highest-order bit of
16411 the bit-field itself. Since the "high-order end" of any object or field
16412 is different on big-endian and little-endian machines, the computation
16413 below must take account of these differences. */
16414 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16415 highest_order_field_bit_offset = bitpos_int;
16417 if (! BYTES_BIG_ENDIAN)
16419 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16420 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16423 bit_offset
16424 = (! BYTES_BIG_ENDIAN
16425 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16426 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16428 if (bit_offset < 0)
16429 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16430 else
16431 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16434 /* For a FIELD_DECL node which represents a bit field, output an attribute
16435 which specifies the length in bits of the given field. */
16437 static inline void
16438 add_bit_size_attribute (dw_die_ref die, tree decl)
16440 /* Must be a field and a bit field. */
16441 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16442 && DECL_BIT_FIELD_TYPE (decl));
16444 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16445 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16448 /* If the compiled language is ANSI C, then add a 'prototyped'
16449 attribute, if arg types are given for the parameters of a function. */
16451 static inline void
16452 add_prototyped_attribute (dw_die_ref die, tree func_type)
16454 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16455 && prototype_p (func_type))
16456 add_AT_flag (die, DW_AT_prototyped, 1);
16459 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16460 by looking in either the type declaration or object declaration
16461 equate table. */
16463 static inline dw_die_ref
16464 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16466 dw_die_ref origin_die = NULL;
16468 if (TREE_CODE (origin) != FUNCTION_DECL)
16470 /* We may have gotten separated from the block for the inlined
16471 function, if we're in an exception handler or some such; make
16472 sure that the abstract function has been written out.
16474 Doing this for nested functions is wrong, however; functions are
16475 distinct units, and our context might not even be inline. */
16476 tree fn = origin;
16478 if (TYPE_P (fn))
16479 fn = TYPE_STUB_DECL (fn);
16481 fn = decl_function_context (fn);
16482 if (fn)
16483 dwarf2out_abstract_function (fn);
16486 if (DECL_P (origin))
16487 origin_die = lookup_decl_die (origin);
16488 else if (TYPE_P (origin))
16489 origin_die = lookup_type_die (origin);
16491 /* XXX: Functions that are never lowered don't always have correct block
16492 trees (in the case of java, they simply have no block tree, in some other
16493 languages). For these functions, there is nothing we can really do to
16494 output correct debug info for inlined functions in all cases. Rather
16495 than die, we'll just produce deficient debug info now, in that we will
16496 have variables without a proper abstract origin. In the future, when all
16497 functions are lowered, we should re-add a gcc_assert (origin_die)
16498 here. */
16500 if (origin_die)
16501 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16502 return origin_die;
16505 /* We do not currently support the pure_virtual attribute. */
16507 static inline void
16508 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16510 if (DECL_VINDEX (func_decl))
16512 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16514 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16515 add_AT_loc (die, DW_AT_vtable_elem_location,
16516 new_loc_descr (DW_OP_constu,
16517 tree_to_shwi (DECL_VINDEX (func_decl)),
16518 0));
16520 /* GNU extension: Record what type this method came from originally. */
16521 if (debug_info_level > DINFO_LEVEL_TERSE
16522 && DECL_CONTEXT (func_decl))
16523 add_AT_die_ref (die, DW_AT_containing_type,
16524 lookup_type_die (DECL_CONTEXT (func_decl)));
16528 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16529 given decl. This used to be a vendor extension until after DWARF 4
16530 standardized it. */
16532 static void
16533 add_linkage_attr (dw_die_ref die, tree decl)
16535 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16537 /* Mimic what assemble_name_raw does with a leading '*'. */
16538 if (name[0] == '*')
16539 name = &name[1];
16541 if (dwarf_version >= 4)
16542 add_AT_string (die, DW_AT_linkage_name, name);
16543 else
16544 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16547 /* Add source coordinate attributes for the given decl. */
16549 static void
16550 add_src_coords_attributes (dw_die_ref die, tree decl)
16552 expanded_location s;
16554 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16555 return;
16556 s = expand_location (DECL_SOURCE_LOCATION (decl));
16557 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16558 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16561 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16563 static void
16564 add_linkage_name (dw_die_ref die, tree decl)
16566 if (debug_info_level > DINFO_LEVEL_TERSE
16567 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16568 && TREE_PUBLIC (decl)
16569 && !DECL_ABSTRACT (decl)
16570 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16571 && die->die_tag != DW_TAG_member)
16573 /* Defer until we have an assembler name set. */
16574 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16576 limbo_die_node *asm_name;
16578 asm_name = ggc_alloc_cleared_limbo_die_node ();
16579 asm_name->die = die;
16580 asm_name->created_for = decl;
16581 asm_name->next = deferred_asm_name;
16582 deferred_asm_name = asm_name;
16584 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16585 add_linkage_attr (die, decl);
16589 /* Add a DW_AT_name attribute and source coordinate attribute for the
16590 given decl, but only if it actually has a name. */
16592 static void
16593 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16595 tree decl_name;
16597 decl_name = DECL_NAME (decl);
16598 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16600 const char *name = dwarf2_name (decl, 0);
16601 if (name)
16602 add_name_attribute (die, name);
16603 if (! DECL_ARTIFICIAL (decl))
16604 add_src_coords_attributes (die, decl);
16606 add_linkage_name (die, decl);
16609 #ifdef VMS_DEBUGGING_INFO
16610 /* Get the function's name, as described by its RTL. This may be different
16611 from the DECL_NAME name used in the source file. */
16612 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16614 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16615 XEXP (DECL_RTL (decl), 0), false);
16616 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16618 #endif /* VMS_DEBUGGING_INFO */
16621 #ifdef VMS_DEBUGGING_INFO
16622 /* Output the debug main pointer die for VMS */
16624 void
16625 dwarf2out_vms_debug_main_pointer (void)
16627 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16628 dw_die_ref die;
16630 /* Allocate the VMS debug main subprogram die. */
16631 die = ggc_alloc_cleared_die_node ();
16632 die->die_tag = DW_TAG_subprogram;
16633 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16634 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16635 current_function_funcdef_no);
16636 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16638 /* Make it the first child of comp_unit_die (). */
16639 die->die_parent = comp_unit_die ();
16640 if (comp_unit_die ()->die_child)
16642 die->die_sib = comp_unit_die ()->die_child->die_sib;
16643 comp_unit_die ()->die_child->die_sib = die;
16645 else
16647 die->die_sib = die;
16648 comp_unit_die ()->die_child = die;
16651 #endif /* VMS_DEBUGGING_INFO */
16653 /* Push a new declaration scope. */
16655 static void
16656 push_decl_scope (tree scope)
16658 vec_safe_push (decl_scope_table, scope);
16661 /* Pop a declaration scope. */
16663 static inline void
16664 pop_decl_scope (void)
16666 decl_scope_table->pop ();
16669 /* walk_tree helper function for uses_local_type, below. */
16671 static tree
16672 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16674 if (!TYPE_P (*tp))
16675 *walk_subtrees = 0;
16676 else
16678 tree name = TYPE_NAME (*tp);
16679 if (name && DECL_P (name) && decl_function_context (name))
16680 return *tp;
16682 return NULL_TREE;
16685 /* If TYPE involves a function-local type (including a local typedef to a
16686 non-local type), returns that type; otherwise returns NULL_TREE. */
16688 static tree
16689 uses_local_type (tree type)
16691 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16692 return used;
16695 /* Return the DIE for the scope that immediately contains this type.
16696 Non-named types that do not involve a function-local type get global
16697 scope. Named types nested in namespaces or other types get their
16698 containing scope. All other types (i.e. function-local named types) get
16699 the current active scope. */
16701 static dw_die_ref
16702 scope_die_for (tree t, dw_die_ref context_die)
16704 dw_die_ref scope_die = NULL;
16705 tree containing_scope;
16707 /* Non-types always go in the current scope. */
16708 gcc_assert (TYPE_P (t));
16710 /* Use the scope of the typedef, rather than the scope of the type
16711 it refers to. */
16712 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16713 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16714 else
16715 containing_scope = TYPE_CONTEXT (t);
16717 /* Use the containing namespace if there is one. */
16718 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16720 if (context_die == lookup_decl_die (containing_scope))
16721 /* OK */;
16722 else if (debug_info_level > DINFO_LEVEL_TERSE)
16723 context_die = get_context_die (containing_scope);
16724 else
16725 containing_scope = NULL_TREE;
16728 /* Ignore function type "scopes" from the C frontend. They mean that
16729 a tagged type is local to a parmlist of a function declarator, but
16730 that isn't useful to DWARF. */
16731 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16732 containing_scope = NULL_TREE;
16734 if (SCOPE_FILE_SCOPE_P (containing_scope))
16736 /* If T uses a local type keep it local as well, to avoid references
16737 to function-local DIEs from outside the function. */
16738 if (current_function_decl && uses_local_type (t))
16739 scope_die = context_die;
16740 else
16741 scope_die = comp_unit_die ();
16743 else if (TYPE_P (containing_scope))
16745 /* For types, we can just look up the appropriate DIE. */
16746 if (debug_info_level > DINFO_LEVEL_TERSE)
16747 scope_die = get_context_die (containing_scope);
16748 else
16750 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16751 if (scope_die == NULL)
16752 scope_die = comp_unit_die ();
16755 else
16756 scope_die = context_die;
16758 return scope_die;
16761 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16763 static inline int
16764 local_scope_p (dw_die_ref context_die)
16766 for (; context_die; context_die = context_die->die_parent)
16767 if (context_die->die_tag == DW_TAG_inlined_subroutine
16768 || context_die->die_tag == DW_TAG_subprogram)
16769 return 1;
16771 return 0;
16774 /* Returns nonzero if CONTEXT_DIE is a class. */
16776 static inline int
16777 class_scope_p (dw_die_ref context_die)
16779 return (context_die
16780 && (context_die->die_tag == DW_TAG_structure_type
16781 || context_die->die_tag == DW_TAG_class_type
16782 || context_die->die_tag == DW_TAG_interface_type
16783 || context_die->die_tag == DW_TAG_union_type));
16786 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16787 whether or not to treat a DIE in this context as a declaration. */
16789 static inline int
16790 class_or_namespace_scope_p (dw_die_ref context_die)
16792 return (class_scope_p (context_die)
16793 || (context_die && context_die->die_tag == DW_TAG_namespace));
16796 /* Many forms of DIEs require a "type description" attribute. This
16797 routine locates the proper "type descriptor" die for the type given
16798 by 'type', and adds a DW_AT_type attribute below the given die. */
16800 static void
16801 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16802 int decl_volatile, dw_die_ref context_die)
16804 enum tree_code code = TREE_CODE (type);
16805 dw_die_ref type_die = NULL;
16807 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16808 or fixed-point type, use the inner type. This is because we have no
16809 support for unnamed types in base_type_die. This can happen if this is
16810 an Ada subrange type. Correct solution is emit a subrange type die. */
16811 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16812 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16813 type = TREE_TYPE (type), code = TREE_CODE (type);
16815 if (code == ERROR_MARK
16816 /* Handle a special case. For functions whose return type is void, we
16817 generate *no* type attribute. (Note that no object may have type
16818 `void', so this only applies to function return types). */
16819 || code == VOID_TYPE)
16820 return;
16822 type_die = modified_type_die (type,
16823 decl_const || TYPE_READONLY (type),
16824 decl_volatile || TYPE_VOLATILE (type),
16825 context_die);
16827 if (type_die != NULL)
16828 add_AT_die_ref (object_die, DW_AT_type, type_die);
16831 /* Given an object die, add the calling convention attribute for the
16832 function call type. */
16833 static void
16834 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16836 enum dwarf_calling_convention value = DW_CC_normal;
16838 value = ((enum dwarf_calling_convention)
16839 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16841 if (is_fortran ()
16842 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16844 /* DWARF 2 doesn't provide a way to identify a program's source-level
16845 entry point. DW_AT_calling_convention attributes are only meant
16846 to describe functions' calling conventions. However, lacking a
16847 better way to signal the Fortran main program, we used this for
16848 a long time, following existing custom. Now, DWARF 4 has
16849 DW_AT_main_subprogram, which we add below, but some tools still
16850 rely on the old way, which we thus keep. */
16851 value = DW_CC_program;
16853 if (dwarf_version >= 4 || !dwarf_strict)
16854 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16857 /* Only add the attribute if the backend requests it, and
16858 is not DW_CC_normal. */
16859 if (value && (value != DW_CC_normal))
16860 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16863 /* Given a tree pointer to a struct, class, union, or enum type node, return
16864 a pointer to the (string) tag name for the given type, or zero if the type
16865 was declared without a tag. */
16867 static const char *
16868 type_tag (const_tree type)
16870 const char *name = 0;
16872 if (TYPE_NAME (type) != 0)
16874 tree t = 0;
16876 /* Find the IDENTIFIER_NODE for the type name. */
16877 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16878 && !TYPE_NAMELESS (type))
16879 t = TYPE_NAME (type);
16881 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16882 a TYPE_DECL node, regardless of whether or not a `typedef' was
16883 involved. */
16884 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16885 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16887 /* We want to be extra verbose. Don't call dwarf_name if
16888 DECL_NAME isn't set. The default hook for decl_printable_name
16889 doesn't like that, and in this context it's correct to return
16890 0, instead of "<anonymous>" or the like. */
16891 if (DECL_NAME (TYPE_NAME (type))
16892 && !DECL_NAMELESS (TYPE_NAME (type)))
16893 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16896 /* Now get the name as a string, or invent one. */
16897 if (!name && t != 0)
16898 name = IDENTIFIER_POINTER (t);
16901 return (name == 0 || *name == '\0') ? 0 : name;
16904 /* Return the type associated with a data member, make a special check
16905 for bit field types. */
16907 static inline tree
16908 member_declared_type (const_tree member)
16910 return (DECL_BIT_FIELD_TYPE (member)
16911 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16914 /* Get the decl's label, as described by its RTL. This may be different
16915 from the DECL_NAME name used in the source file. */
16917 #if 0
16918 static const char *
16919 decl_start_label (tree decl)
16921 rtx x;
16922 const char *fnname;
16924 x = DECL_RTL (decl);
16925 gcc_assert (MEM_P (x));
16927 x = XEXP (x, 0);
16928 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16930 fnname = XSTR (x, 0);
16931 return fnname;
16933 #endif
16935 /* These routines generate the internal representation of the DIE's for
16936 the compilation unit. Debugging information is collected by walking
16937 the declaration trees passed in from dwarf2out_decl(). */
16939 static void
16940 gen_array_type_die (tree type, dw_die_ref context_die)
16942 dw_die_ref scope_die = scope_die_for (type, context_die);
16943 dw_die_ref array_die;
16945 /* GNU compilers represent multidimensional array types as sequences of one
16946 dimensional array types whose element types are themselves array types.
16947 We sometimes squish that down to a single array_type DIE with multiple
16948 subscripts in the Dwarf debugging info. The draft Dwarf specification
16949 say that we are allowed to do this kind of compression in C, because
16950 there is no difference between an array of arrays and a multidimensional
16951 array. We don't do this for Ada to remain as close as possible to the
16952 actual representation, which is especially important against the language
16953 flexibilty wrt arrays of variable size. */
16955 bool collapse_nested_arrays = !is_ada ();
16956 tree element_type;
16958 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16959 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16960 if (TYPE_STRING_FLAG (type)
16961 && TREE_CODE (type) == ARRAY_TYPE
16962 && is_fortran ()
16963 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16965 HOST_WIDE_INT size;
16967 array_die = new_die (DW_TAG_string_type, scope_die, type);
16968 add_name_attribute (array_die, type_tag (type));
16969 equate_type_number_to_die (type, array_die);
16970 size = int_size_in_bytes (type);
16971 if (size >= 0)
16972 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16973 else if (TYPE_DOMAIN (type) != NULL_TREE
16974 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16975 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16977 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16978 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16980 size = int_size_in_bytes (TREE_TYPE (szdecl));
16981 if (loc && size > 0)
16983 add_AT_location_description (array_die, DW_AT_string_length, loc);
16984 if (size != DWARF2_ADDR_SIZE)
16985 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16988 return;
16991 array_die = new_die (DW_TAG_array_type, scope_die, type);
16992 add_name_attribute (array_die, type_tag (type));
16993 equate_type_number_to_die (type, array_die);
16995 if (TREE_CODE (type) == VECTOR_TYPE)
16996 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16998 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16999 if (is_fortran ()
17000 && TREE_CODE (type) == ARRAY_TYPE
17001 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17002 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17003 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17005 #if 0
17006 /* We default the array ordering. SDB will probably do
17007 the right things even if DW_AT_ordering is not present. It's not even
17008 an issue until we start to get into multidimensional arrays anyway. If
17009 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17010 then we'll have to put the DW_AT_ordering attribute back in. (But if
17011 and when we find out that we need to put these in, we will only do so
17012 for multidimensional arrays. */
17013 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17014 #endif
17016 if (TREE_CODE (type) == VECTOR_TYPE)
17018 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17019 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17020 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17021 add_bound_info (subrange_die, DW_AT_upper_bound,
17022 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17024 else
17025 add_subscript_info (array_die, type, collapse_nested_arrays);
17027 /* Add representation of the type of the elements of this array type and
17028 emit the corresponding DIE if we haven't done it already. */
17029 element_type = TREE_TYPE (type);
17030 if (collapse_nested_arrays)
17031 while (TREE_CODE (element_type) == ARRAY_TYPE)
17033 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17034 break;
17035 element_type = TREE_TYPE (element_type);
17038 add_type_attribute (array_die, element_type, 0, 0, context_die);
17040 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17041 if (TYPE_ARTIFICIAL (type))
17042 add_AT_flag (array_die, DW_AT_artificial, 1);
17044 if (get_AT (array_die, DW_AT_name))
17045 add_pubtype (type, array_die);
17048 static dw_loc_descr_ref
17049 descr_info_loc (tree val, tree base_decl)
17051 HOST_WIDE_INT size;
17052 dw_loc_descr_ref loc, loc2;
17053 enum dwarf_location_atom op;
17055 if (val == base_decl)
17056 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17058 switch (TREE_CODE (val))
17060 CASE_CONVERT:
17061 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17062 case VAR_DECL:
17063 return loc_descriptor_from_tree (val, 0);
17064 case INTEGER_CST:
17065 if (tree_fits_shwi_p (val))
17066 return int_loc_descriptor (tree_to_shwi (val));
17067 break;
17068 case INDIRECT_REF:
17069 size = int_size_in_bytes (TREE_TYPE (val));
17070 if (size < 0)
17071 break;
17072 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17073 if (!loc)
17074 break;
17075 if (size == DWARF2_ADDR_SIZE)
17076 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17077 else
17078 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17079 return loc;
17080 case POINTER_PLUS_EXPR:
17081 case PLUS_EXPR:
17082 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17083 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17085 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17086 if (!loc)
17087 break;
17088 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17090 else
17092 op = DW_OP_plus;
17093 do_binop:
17094 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17095 if (!loc)
17096 break;
17097 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17098 if (!loc2)
17099 break;
17100 add_loc_descr (&loc, loc2);
17101 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17103 return loc;
17104 case MINUS_EXPR:
17105 op = DW_OP_minus;
17106 goto do_binop;
17107 case MULT_EXPR:
17108 op = DW_OP_mul;
17109 goto do_binop;
17110 case EQ_EXPR:
17111 op = DW_OP_eq;
17112 goto do_binop;
17113 case NE_EXPR:
17114 op = DW_OP_ne;
17115 goto do_binop;
17116 default:
17117 break;
17119 return NULL;
17122 static void
17123 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17124 tree val, tree base_decl)
17126 dw_loc_descr_ref loc;
17128 if (tree_fits_shwi_p (val))
17130 add_AT_unsigned (die, attr, tree_to_shwi (val));
17131 return;
17134 loc = descr_info_loc (val, base_decl);
17135 if (!loc)
17136 return;
17138 add_AT_loc (die, attr, loc);
17141 /* This routine generates DIE for array with hidden descriptor, details
17142 are filled into *info by a langhook. */
17144 static void
17145 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17146 dw_die_ref context_die)
17148 dw_die_ref scope_die = scope_die_for (type, context_die);
17149 dw_die_ref array_die;
17150 int dim;
17152 array_die = new_die (DW_TAG_array_type, scope_die, type);
17153 add_name_attribute (array_die, type_tag (type));
17154 equate_type_number_to_die (type, array_die);
17156 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17157 if (is_fortran ()
17158 && info->ndimensions >= 2)
17159 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17161 if (info->data_location)
17162 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17163 info->base_decl);
17164 if (info->associated)
17165 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17166 info->base_decl);
17167 if (info->allocated)
17168 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17169 info->base_decl);
17171 for (dim = 0; dim < info->ndimensions; dim++)
17173 dw_die_ref subrange_die
17174 = new_die (DW_TAG_subrange_type, array_die, NULL);
17176 if (info->dimen[dim].lower_bound)
17178 /* If it is the default value, omit it. */
17179 int dflt;
17181 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17182 && (dflt = lower_bound_default ()) != -1
17183 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17185 else
17186 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17187 info->dimen[dim].lower_bound,
17188 info->base_decl);
17190 if (info->dimen[dim].upper_bound)
17191 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17192 info->dimen[dim].upper_bound,
17193 info->base_decl);
17194 if (info->dimen[dim].stride)
17195 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17196 info->dimen[dim].stride,
17197 info->base_decl);
17200 gen_type_die (info->element_type, context_die);
17201 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17203 if (get_AT (array_die, DW_AT_name))
17204 add_pubtype (type, array_die);
17207 #if 0
17208 static void
17209 gen_entry_point_die (tree decl, dw_die_ref context_die)
17211 tree origin = decl_ultimate_origin (decl);
17212 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17214 if (origin != NULL)
17215 add_abstract_origin_attribute (decl_die, origin);
17216 else
17218 add_name_and_src_coords_attributes (decl_die, decl);
17219 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17220 0, 0, context_die);
17223 if (DECL_ABSTRACT (decl))
17224 equate_decl_number_to_die (decl, decl_die);
17225 else
17226 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17228 #endif
17230 /* Walk through the list of incomplete types again, trying once more to
17231 emit full debugging info for them. */
17233 static void
17234 retry_incomplete_types (void)
17236 int i;
17238 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17239 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17240 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17243 /* Determine what tag to use for a record type. */
17245 static enum dwarf_tag
17246 record_type_tag (tree type)
17248 if (! lang_hooks.types.classify_record)
17249 return DW_TAG_structure_type;
17251 switch (lang_hooks.types.classify_record (type))
17253 case RECORD_IS_STRUCT:
17254 return DW_TAG_structure_type;
17256 case RECORD_IS_CLASS:
17257 return DW_TAG_class_type;
17259 case RECORD_IS_INTERFACE:
17260 if (dwarf_version >= 3 || !dwarf_strict)
17261 return DW_TAG_interface_type;
17262 return DW_TAG_structure_type;
17264 default:
17265 gcc_unreachable ();
17269 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17270 include all of the information about the enumeration values also. Each
17271 enumerated type name/value is listed as a child of the enumerated type
17272 DIE. */
17274 static dw_die_ref
17275 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17277 dw_die_ref type_die = lookup_type_die (type);
17279 if (type_die == NULL)
17281 type_die = new_die (DW_TAG_enumeration_type,
17282 scope_die_for (type, context_die), type);
17283 equate_type_number_to_die (type, type_die);
17284 add_name_attribute (type_die, type_tag (type));
17285 if (dwarf_version >= 4 || !dwarf_strict)
17287 if (ENUM_IS_SCOPED (type))
17288 add_AT_flag (type_die, DW_AT_enum_class, 1);
17289 if (ENUM_IS_OPAQUE (type))
17290 add_AT_flag (type_die, DW_AT_declaration, 1);
17293 else if (! TYPE_SIZE (type))
17294 return type_die;
17295 else
17296 remove_AT (type_die, DW_AT_declaration);
17298 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17299 given enum type is incomplete, do not generate the DW_AT_byte_size
17300 attribute or the DW_AT_element_list attribute. */
17301 if (TYPE_SIZE (type))
17303 tree link;
17305 TREE_ASM_WRITTEN (type) = 1;
17306 add_byte_size_attribute (type_die, type);
17307 if (TYPE_STUB_DECL (type) != NULL_TREE)
17309 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17310 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17313 /* If the first reference to this type was as the return type of an
17314 inline function, then it may not have a parent. Fix this now. */
17315 if (type_die->die_parent == NULL)
17316 add_child_die (scope_die_for (type, context_die), type_die);
17318 for (link = TYPE_VALUES (type);
17319 link != NULL; link = TREE_CHAIN (link))
17321 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17322 tree value = TREE_VALUE (link);
17324 add_name_attribute (enum_die,
17325 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17327 if (TREE_CODE (value) == CONST_DECL)
17328 value = DECL_INITIAL (value);
17330 if (simple_type_size_in_bits (TREE_TYPE (value))
17331 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17332 /* DWARF2 does not provide a way of indicating whether or
17333 not enumeration constants are signed or unsigned. GDB
17334 always assumes the values are signed, so we output all
17335 values as if they were signed. That means that
17336 enumeration constants with very large unsigned values
17337 will appear to have negative values in the debugger.
17339 TODO: the above comment is wrong, DWARF2 does provide
17340 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17341 This should be re-worked to use correct signed/unsigned
17342 int/double tags for all cases, instead of always treating as
17343 signed. */
17344 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17345 else
17346 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17347 that here. */
17348 add_AT_double (enum_die, DW_AT_const_value,
17349 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17352 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17353 if (TYPE_ARTIFICIAL (type))
17354 add_AT_flag (type_die, DW_AT_artificial, 1);
17356 else
17357 add_AT_flag (type_die, DW_AT_declaration, 1);
17359 add_pubtype (type, type_die);
17361 return type_die;
17364 /* Generate a DIE to represent either a real live formal parameter decl or to
17365 represent just the type of some formal parameter position in some function
17366 type.
17368 Note that this routine is a bit unusual because its argument may be a
17369 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17370 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17371 node. If it's the former then this function is being called to output a
17372 DIE to represent a formal parameter object (or some inlining thereof). If
17373 it's the latter, then this function is only being called to output a
17374 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17375 argument type of some subprogram type.
17376 If EMIT_NAME_P is true, name and source coordinate attributes
17377 are emitted. */
17379 static dw_die_ref
17380 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17381 dw_die_ref context_die)
17383 tree node_or_origin = node ? node : origin;
17384 tree ultimate_origin;
17385 dw_die_ref parm_die
17386 = new_die (DW_TAG_formal_parameter, context_die, node);
17388 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17390 case tcc_declaration:
17391 ultimate_origin = decl_ultimate_origin (node_or_origin);
17392 if (node || ultimate_origin)
17393 origin = ultimate_origin;
17394 if (origin != NULL)
17395 add_abstract_origin_attribute (parm_die, origin);
17396 else if (emit_name_p)
17397 add_name_and_src_coords_attributes (parm_die, node);
17398 if (origin == NULL
17399 || (! DECL_ABSTRACT (node_or_origin)
17400 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17401 decl_function_context
17402 (node_or_origin))))
17404 tree type = TREE_TYPE (node_or_origin);
17405 if (decl_by_reference_p (node_or_origin))
17406 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17407 context_die);
17408 else
17409 add_type_attribute (parm_die, type,
17410 TREE_READONLY (node_or_origin),
17411 TREE_THIS_VOLATILE (node_or_origin),
17412 context_die);
17414 if (origin == NULL && DECL_ARTIFICIAL (node))
17415 add_AT_flag (parm_die, DW_AT_artificial, 1);
17417 if (node && node != origin)
17418 equate_decl_number_to_die (node, parm_die);
17419 if (! DECL_ABSTRACT (node_or_origin))
17420 add_location_or_const_value_attribute (parm_die, node_or_origin,
17421 node == NULL, DW_AT_location);
17423 break;
17425 case tcc_type:
17426 /* We were called with some kind of a ..._TYPE node. */
17427 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17428 break;
17430 default:
17431 gcc_unreachable ();
17434 return parm_die;
17437 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17438 children DW_TAG_formal_parameter DIEs representing the arguments of the
17439 parameter pack.
17441 PARM_PACK must be a function parameter pack.
17442 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17443 must point to the subsequent arguments of the function PACK_ARG belongs to.
17444 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17445 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17446 following the last one for which a DIE was generated. */
17448 static dw_die_ref
17449 gen_formal_parameter_pack_die (tree parm_pack,
17450 tree pack_arg,
17451 dw_die_ref subr_die,
17452 tree *next_arg)
17454 tree arg;
17455 dw_die_ref parm_pack_die;
17457 gcc_assert (parm_pack
17458 && lang_hooks.function_parameter_pack_p (parm_pack)
17459 && subr_die);
17461 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17462 add_src_coords_attributes (parm_pack_die, parm_pack);
17464 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17466 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17467 parm_pack))
17468 break;
17469 gen_formal_parameter_die (arg, NULL,
17470 false /* Don't emit name attribute. */,
17471 parm_pack_die);
17473 if (next_arg)
17474 *next_arg = arg;
17475 return parm_pack_die;
17478 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17479 at the end of an (ANSI prototyped) formal parameters list. */
17481 static void
17482 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17484 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17487 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17488 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17489 parameters as specified in some function type specification (except for
17490 those which appear as part of a function *definition*). */
17492 static void
17493 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17495 tree link;
17496 tree formal_type = NULL;
17497 tree first_parm_type;
17498 tree arg;
17500 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17502 arg = DECL_ARGUMENTS (function_or_method_type);
17503 function_or_method_type = TREE_TYPE (function_or_method_type);
17505 else
17506 arg = NULL_TREE;
17508 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17510 /* Make our first pass over the list of formal parameter types and output a
17511 DW_TAG_formal_parameter DIE for each one. */
17512 for (link = first_parm_type; link; )
17514 dw_die_ref parm_die;
17516 formal_type = TREE_VALUE (link);
17517 if (formal_type == void_type_node)
17518 break;
17520 /* Output a (nameless) DIE to represent the formal parameter itself. */
17521 parm_die = gen_formal_parameter_die (formal_type, NULL,
17522 true /* Emit name attribute. */,
17523 context_die);
17524 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17525 && link == first_parm_type)
17527 add_AT_flag (parm_die, DW_AT_artificial, 1);
17528 if (dwarf_version >= 3 || !dwarf_strict)
17529 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17531 else if (arg && DECL_ARTIFICIAL (arg))
17532 add_AT_flag (parm_die, DW_AT_artificial, 1);
17534 link = TREE_CHAIN (link);
17535 if (arg)
17536 arg = DECL_CHAIN (arg);
17539 /* If this function type has an ellipsis, add a
17540 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17541 if (formal_type != void_type_node)
17542 gen_unspecified_parameters_die (function_or_method_type, context_die);
17544 /* Make our second (and final) pass over the list of formal parameter types
17545 and output DIEs to represent those types (as necessary). */
17546 for (link = TYPE_ARG_TYPES (function_or_method_type);
17547 link && TREE_VALUE (link);
17548 link = TREE_CHAIN (link))
17549 gen_type_die (TREE_VALUE (link), context_die);
17552 /* We want to generate the DIE for TYPE so that we can generate the
17553 die for MEMBER, which has been defined; we will need to refer back
17554 to the member declaration nested within TYPE. If we're trying to
17555 generate minimal debug info for TYPE, processing TYPE won't do the
17556 trick; we need to attach the member declaration by hand. */
17558 static void
17559 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17561 gen_type_die (type, context_die);
17563 /* If we're trying to avoid duplicate debug info, we may not have
17564 emitted the member decl for this function. Emit it now. */
17565 if (TYPE_STUB_DECL (type)
17566 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17567 && ! lookup_decl_die (member))
17569 dw_die_ref type_die;
17570 gcc_assert (!decl_ultimate_origin (member));
17572 push_decl_scope (type);
17573 type_die = lookup_type_die_strip_naming_typedef (type);
17574 if (TREE_CODE (member) == FUNCTION_DECL)
17575 gen_subprogram_die (member, type_die);
17576 else if (TREE_CODE (member) == FIELD_DECL)
17578 /* Ignore the nameless fields that are used to skip bits but handle
17579 C++ anonymous unions and structs. */
17580 if (DECL_NAME (member) != NULL_TREE
17581 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17582 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17584 gen_type_die (member_declared_type (member), type_die);
17585 gen_field_die (member, type_die);
17588 else
17589 gen_variable_die (member, NULL_TREE, type_die);
17591 pop_decl_scope ();
17595 /* Forward declare these functions, because they are mutually recursive
17596 with their set_block_* pairing functions. */
17597 static void set_decl_origin_self (tree);
17598 static void set_decl_abstract_flags (tree, int);
17600 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17601 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17602 that it points to the node itself, thus indicating that the node is its
17603 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17604 the given node is NULL, recursively descend the decl/block tree which
17605 it is the root of, and for each other ..._DECL or BLOCK node contained
17606 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17607 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17608 values to point to themselves. */
17610 static void
17611 set_block_origin_self (tree stmt)
17613 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17615 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17618 tree local_decl;
17620 for (local_decl = BLOCK_VARS (stmt);
17621 local_decl != NULL_TREE;
17622 local_decl = DECL_CHAIN (local_decl))
17623 if (! DECL_EXTERNAL (local_decl))
17624 set_decl_origin_self (local_decl); /* Potential recursion. */
17628 tree subblock;
17630 for (subblock = BLOCK_SUBBLOCKS (stmt);
17631 subblock != NULL_TREE;
17632 subblock = BLOCK_CHAIN (subblock))
17633 set_block_origin_self (subblock); /* Recurse. */
17638 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17639 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17640 node to so that it points to the node itself, thus indicating that the
17641 node represents its own (abstract) origin. Additionally, if the
17642 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17643 the decl/block tree of which the given node is the root of, and for
17644 each other ..._DECL or BLOCK node contained therein whose
17645 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17646 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17647 point to themselves. */
17649 static void
17650 set_decl_origin_self (tree decl)
17652 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17654 DECL_ABSTRACT_ORIGIN (decl) = decl;
17655 if (TREE_CODE (decl) == FUNCTION_DECL)
17657 tree arg;
17659 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17660 DECL_ABSTRACT_ORIGIN (arg) = arg;
17661 if (DECL_INITIAL (decl) != NULL_TREE
17662 && DECL_INITIAL (decl) != error_mark_node)
17663 set_block_origin_self (DECL_INITIAL (decl));
17668 /* Given a pointer to some BLOCK node, and a boolean value to set the
17669 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17670 the given block, and for all local decls and all local sub-blocks
17671 (recursively) which are contained therein. */
17673 static void
17674 set_block_abstract_flags (tree stmt, int setting)
17676 tree local_decl;
17677 tree subblock;
17678 unsigned int i;
17680 BLOCK_ABSTRACT (stmt) = setting;
17682 for (local_decl = BLOCK_VARS (stmt);
17683 local_decl != NULL_TREE;
17684 local_decl = DECL_CHAIN (local_decl))
17685 if (! DECL_EXTERNAL (local_decl))
17686 set_decl_abstract_flags (local_decl, setting);
17688 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17690 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17691 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17692 || TREE_CODE (local_decl) == PARM_DECL)
17693 set_decl_abstract_flags (local_decl, setting);
17696 for (subblock = BLOCK_SUBBLOCKS (stmt);
17697 subblock != NULL_TREE;
17698 subblock = BLOCK_CHAIN (subblock))
17699 set_block_abstract_flags (subblock, setting);
17702 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17703 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17704 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17705 set the abstract flags for all of the parameters, local vars, local
17706 blocks and sub-blocks (recursively) to the same setting. */
17708 static void
17709 set_decl_abstract_flags (tree decl, int setting)
17711 DECL_ABSTRACT (decl) = setting;
17712 if (TREE_CODE (decl) == FUNCTION_DECL)
17714 tree arg;
17716 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17717 DECL_ABSTRACT (arg) = setting;
17718 if (DECL_INITIAL (decl) != NULL_TREE
17719 && DECL_INITIAL (decl) != error_mark_node)
17720 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17724 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17725 may later generate inlined and/or out-of-line instances of. */
17727 static void
17728 dwarf2out_abstract_function (tree decl)
17730 dw_die_ref old_die;
17731 tree save_fn;
17732 tree context;
17733 int was_abstract;
17734 htab_t old_decl_loc_table;
17735 htab_t old_cached_dw_loc_list_table;
17736 int old_call_site_count, old_tail_call_site_count;
17737 struct call_arg_loc_node *old_call_arg_locations;
17739 /* Make sure we have the actual abstract inline, not a clone. */
17740 decl = DECL_ORIGIN (decl);
17742 old_die = lookup_decl_die (decl);
17743 if (old_die && get_AT (old_die, DW_AT_inline))
17744 /* We've already generated the abstract instance. */
17745 return;
17747 /* We can be called while recursively when seeing block defining inlined subroutine
17748 DIE. Be sure to not clobber the outer location table nor use it or we would
17749 get locations in abstract instantces. */
17750 old_decl_loc_table = decl_loc_table;
17751 decl_loc_table = NULL;
17752 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17753 cached_dw_loc_list_table = NULL;
17754 old_call_arg_locations = call_arg_locations;
17755 call_arg_locations = NULL;
17756 old_call_site_count = call_site_count;
17757 call_site_count = -1;
17758 old_tail_call_site_count = tail_call_site_count;
17759 tail_call_site_count = -1;
17761 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17762 we don't get confused by DECL_ABSTRACT. */
17763 if (debug_info_level > DINFO_LEVEL_TERSE)
17765 context = decl_class_context (decl);
17766 if (context)
17767 gen_type_die_for_member
17768 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17771 /* Pretend we've just finished compiling this function. */
17772 save_fn = current_function_decl;
17773 current_function_decl = decl;
17775 was_abstract = DECL_ABSTRACT (decl);
17776 set_decl_abstract_flags (decl, 1);
17777 dwarf2out_decl (decl);
17778 if (! was_abstract)
17779 set_decl_abstract_flags (decl, 0);
17781 current_function_decl = save_fn;
17782 decl_loc_table = old_decl_loc_table;
17783 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17784 call_arg_locations = old_call_arg_locations;
17785 call_site_count = old_call_site_count;
17786 tail_call_site_count = old_tail_call_site_count;
17789 /* Helper function of premark_used_types() which gets called through
17790 htab_traverse.
17792 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17793 marked as unused by prune_unused_types. */
17795 static int
17796 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17798 tree type;
17799 dw_die_ref die;
17801 type = (tree) *slot;
17802 die = lookup_type_die (type);
17803 if (die != NULL)
17804 die->die_perennial_p = 1;
17805 return 1;
17808 /* Helper function of premark_types_used_by_global_vars which gets called
17809 through htab_traverse.
17811 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17812 marked as unused by prune_unused_types. The DIE of the type is marked
17813 only if the global variable using the type will actually be emitted. */
17815 static int
17816 premark_types_used_by_global_vars_helper (void **slot,
17817 void *data ATTRIBUTE_UNUSED)
17819 struct types_used_by_vars_entry *entry;
17820 dw_die_ref die;
17822 entry = (struct types_used_by_vars_entry *) *slot;
17823 gcc_assert (entry->type != NULL
17824 && entry->var_decl != NULL);
17825 die = lookup_type_die (entry->type);
17826 if (die)
17828 /* Ask cgraph if the global variable really is to be emitted.
17829 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17830 struct varpool_node *node = varpool_get_node (entry->var_decl);
17831 if (node && node->definition)
17833 die->die_perennial_p = 1;
17834 /* Keep the parent DIEs as well. */
17835 while ((die = die->die_parent) && die->die_perennial_p == 0)
17836 die->die_perennial_p = 1;
17839 return 1;
17842 /* Mark all members of used_types_hash as perennial. */
17844 static void
17845 premark_used_types (struct function *fun)
17847 if (fun && fun->used_types_hash)
17848 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17851 /* Mark all members of types_used_by_vars_entry as perennial. */
17853 static void
17854 premark_types_used_by_global_vars (void)
17856 if (types_used_by_vars_hash)
17857 htab_traverse (types_used_by_vars_hash,
17858 premark_types_used_by_global_vars_helper, NULL);
17861 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17862 for CA_LOC call arg loc node. */
17864 static dw_die_ref
17865 gen_call_site_die (tree decl, dw_die_ref subr_die,
17866 struct call_arg_loc_node *ca_loc)
17868 dw_die_ref stmt_die = NULL, die;
17869 tree block = ca_loc->block;
17871 while (block
17872 && block != DECL_INITIAL (decl)
17873 && TREE_CODE (block) == BLOCK)
17875 if (block_map.length () > BLOCK_NUMBER (block))
17876 stmt_die = block_map[BLOCK_NUMBER (block)];
17877 if (stmt_die)
17878 break;
17879 block = BLOCK_SUPERCONTEXT (block);
17881 if (stmt_die == NULL)
17882 stmt_die = subr_die;
17883 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17884 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17885 if (ca_loc->tail_call_p)
17886 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17887 if (ca_loc->symbol_ref)
17889 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17890 if (tdie)
17891 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17892 else
17893 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17895 return die;
17898 /* Generate a DIE to represent a declared function (either file-scope or
17899 block-local). */
17901 static void
17902 gen_subprogram_die (tree decl, dw_die_ref context_die)
17904 tree origin = decl_ultimate_origin (decl);
17905 dw_die_ref subr_die;
17906 tree outer_scope;
17907 dw_die_ref old_die = lookup_decl_die (decl);
17908 int declaration = (current_function_decl != decl
17909 || class_or_namespace_scope_p (context_die));
17911 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17913 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17914 started to generate the abstract instance of an inline, decided to output
17915 its containing class, and proceeded to emit the declaration of the inline
17916 from the member list for the class. If so, DECLARATION takes priority;
17917 we'll get back to the abstract instance when done with the class. */
17919 /* The class-scope declaration DIE must be the primary DIE. */
17920 if (origin && declaration && class_or_namespace_scope_p (context_die))
17922 origin = NULL;
17923 gcc_assert (!old_die);
17926 /* Now that the C++ front end lazily declares artificial member fns, we
17927 might need to retrofit the declaration into its class. */
17928 if (!declaration && !origin && !old_die
17929 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17930 && !class_or_namespace_scope_p (context_die)
17931 && debug_info_level > DINFO_LEVEL_TERSE)
17932 old_die = force_decl_die (decl);
17934 if (origin != NULL)
17936 gcc_assert (!declaration || local_scope_p (context_die));
17938 /* Fixup die_parent for the abstract instance of a nested
17939 inline function. */
17940 if (old_die && old_die->die_parent == NULL)
17941 add_child_die (context_die, old_die);
17943 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17944 add_abstract_origin_attribute (subr_die, origin);
17945 /* This is where the actual code for a cloned function is.
17946 Let's emit linkage name attribute for it. This helps
17947 debuggers to e.g, set breakpoints into
17948 constructors/destructors when the user asks "break
17949 K::K". */
17950 add_linkage_name (subr_die, decl);
17952 else if (old_die)
17954 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17955 struct dwarf_file_data * file_index = lookup_filename (s.file);
17957 if (!get_AT_flag (old_die, DW_AT_declaration)
17958 /* We can have a normal definition following an inline one in the
17959 case of redefinition of GNU C extern inlines.
17960 It seems reasonable to use AT_specification in this case. */
17961 && !get_AT (old_die, DW_AT_inline))
17963 /* Detect and ignore this case, where we are trying to output
17964 something we have already output. */
17965 return;
17968 /* If the definition comes from the same place as the declaration,
17969 maybe use the old DIE. We always want the DIE for this function
17970 that has the *_pc attributes to be under comp_unit_die so the
17971 debugger can find it. We also need to do this for abstract
17972 instances of inlines, since the spec requires the out-of-line copy
17973 to have the same parent. For local class methods, this doesn't
17974 apply; we just use the old DIE. */
17975 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17976 && (DECL_ARTIFICIAL (decl)
17977 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17978 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17979 == (unsigned) s.line))))
17981 subr_die = old_die;
17983 /* Clear out the declaration attribute and the formal parameters.
17984 Do not remove all children, because it is possible that this
17985 declaration die was forced using force_decl_die(). In such
17986 cases die that forced declaration die (e.g. TAG_imported_module)
17987 is one of the children that we do not want to remove. */
17988 remove_AT (subr_die, DW_AT_declaration);
17989 remove_AT (subr_die, DW_AT_object_pointer);
17990 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17992 else
17994 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17995 add_AT_specification (subr_die, old_die);
17996 add_pubname (decl, subr_die);
17997 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17998 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17999 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18000 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18003 else
18005 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18007 if (TREE_PUBLIC (decl))
18008 add_AT_flag (subr_die, DW_AT_external, 1);
18010 add_name_and_src_coords_attributes (subr_die, decl);
18011 add_pubname (decl, subr_die);
18012 if (debug_info_level > DINFO_LEVEL_TERSE)
18014 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18015 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18016 0, 0, context_die);
18019 add_pure_or_virtual_attribute (subr_die, decl);
18020 if (DECL_ARTIFICIAL (decl))
18021 add_AT_flag (subr_die, DW_AT_artificial, 1);
18023 add_accessibility_attribute (subr_die, decl);
18026 if (declaration)
18028 if (!old_die || !get_AT (old_die, DW_AT_inline))
18030 add_AT_flag (subr_die, DW_AT_declaration, 1);
18032 /* If this is an explicit function declaration then generate
18033 a DW_AT_explicit attribute. */
18034 if (lang_hooks.decls.function_decl_explicit_p (decl)
18035 && (dwarf_version >= 3 || !dwarf_strict))
18036 add_AT_flag (subr_die, DW_AT_explicit, 1);
18038 /* The first time we see a member function, it is in the context of
18039 the class to which it belongs. We make sure of this by emitting
18040 the class first. The next time is the definition, which is
18041 handled above. The two may come from the same source text.
18043 Note that force_decl_die() forces function declaration die. It is
18044 later reused to represent definition. */
18045 equate_decl_number_to_die (decl, subr_die);
18048 else if (DECL_ABSTRACT (decl))
18050 if (DECL_DECLARED_INLINE_P (decl))
18052 if (cgraph_function_possibly_inlined_p (decl))
18053 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18054 else
18055 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18057 else
18059 if (cgraph_function_possibly_inlined_p (decl))
18060 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18061 else
18062 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18065 if (DECL_DECLARED_INLINE_P (decl)
18066 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18067 add_AT_flag (subr_die, DW_AT_artificial, 1);
18069 equate_decl_number_to_die (decl, subr_die);
18071 else if (!DECL_EXTERNAL (decl))
18073 HOST_WIDE_INT cfa_fb_offset;
18074 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18076 if (!old_die || !get_AT (old_die, DW_AT_inline))
18077 equate_decl_number_to_die (decl, subr_die);
18079 gcc_checking_assert (fun);
18080 if (!flag_reorder_blocks_and_partition)
18082 dw_fde_ref fde = fun->fde;
18083 if (fde->dw_fde_begin)
18085 /* We have already generated the labels. */
18086 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18087 fde->dw_fde_end, false);
18089 else
18091 /* Create start/end labels and add the range. */
18092 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18093 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18094 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18095 current_function_funcdef_no);
18096 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18097 current_function_funcdef_no);
18098 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18099 false);
18102 #if VMS_DEBUGGING_INFO
18103 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18104 Section 2.3 Prologue and Epilogue Attributes:
18105 When a breakpoint is set on entry to a function, it is generally
18106 desirable for execution to be suspended, not on the very first
18107 instruction of the function, but rather at a point after the
18108 function's frame has been set up, after any language defined local
18109 declaration processing has been completed, and before execution of
18110 the first statement of the function begins. Debuggers generally
18111 cannot properly determine where this point is. Similarly for a
18112 breakpoint set on exit from a function. The prologue and epilogue
18113 attributes allow a compiler to communicate the location(s) to use. */
18116 if (fde->dw_fde_vms_end_prologue)
18117 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18118 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18120 if (fde->dw_fde_vms_begin_epilogue)
18121 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18122 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18124 #endif
18127 else
18129 /* Generate pubnames entries for the split function code ranges. */
18130 dw_fde_ref fde = fun->fde;
18132 if (fde->dw_fde_second_begin)
18134 if (dwarf_version >= 3 || !dwarf_strict)
18136 /* We should use ranges for non-contiguous code section
18137 addresses. Use the actual code range for the initial
18138 section, since the HOT/COLD labels might precede an
18139 alignment offset. */
18140 bool range_list_added = false;
18141 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18142 fde->dw_fde_end, &range_list_added,
18143 false);
18144 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18145 fde->dw_fde_second_end,
18146 &range_list_added, false);
18147 if (range_list_added)
18148 add_ranges (NULL);
18150 else
18152 /* There is no real support in DW2 for this .. so we make
18153 a work-around. First, emit the pub name for the segment
18154 containing the function label. Then make and emit a
18155 simplified subprogram DIE for the second segment with the
18156 name pre-fixed by __hot/cold_sect_of_. We use the same
18157 linkage name for the second die so that gdb will find both
18158 sections when given "b foo". */
18159 const char *name = NULL;
18160 tree decl_name = DECL_NAME (decl);
18161 dw_die_ref seg_die;
18163 /* Do the 'primary' section. */
18164 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18165 fde->dw_fde_end, false);
18167 /* Build a minimal DIE for the secondary section. */
18168 seg_die = new_die (DW_TAG_subprogram,
18169 subr_die->die_parent, decl);
18171 if (TREE_PUBLIC (decl))
18172 add_AT_flag (seg_die, DW_AT_external, 1);
18174 if (decl_name != NULL
18175 && IDENTIFIER_POINTER (decl_name) != NULL)
18177 name = dwarf2_name (decl, 1);
18178 if (! DECL_ARTIFICIAL (decl))
18179 add_src_coords_attributes (seg_die, decl);
18181 add_linkage_name (seg_die, decl);
18183 gcc_assert (name != NULL);
18184 add_pure_or_virtual_attribute (seg_die, decl);
18185 if (DECL_ARTIFICIAL (decl))
18186 add_AT_flag (seg_die, DW_AT_artificial, 1);
18188 name = concat ("__second_sect_of_", name, NULL);
18189 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18190 fde->dw_fde_second_end, false);
18191 add_name_attribute (seg_die, name);
18192 if (want_pubnames ())
18193 add_pubname_string (name, seg_die);
18196 else
18197 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18198 false);
18201 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18203 /* We define the "frame base" as the function's CFA. This is more
18204 convenient for several reasons: (1) It's stable across the prologue
18205 and epilogue, which makes it better than just a frame pointer,
18206 (2) With dwarf3, there exists a one-byte encoding that allows us
18207 to reference the .debug_frame data by proxy, but failing that,
18208 (3) We can at least reuse the code inspection and interpretation
18209 code that determines the CFA position at various points in the
18210 function. */
18211 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18213 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18214 add_AT_loc (subr_die, DW_AT_frame_base, op);
18216 else
18218 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18219 if (list->dw_loc_next)
18220 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18221 else
18222 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18225 /* Compute a displacement from the "steady-state frame pointer" to
18226 the CFA. The former is what all stack slots and argument slots
18227 will reference in the rtl; the latter is what we've told the
18228 debugger about. We'll need to adjust all frame_base references
18229 by this displacement. */
18230 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18232 if (fun->static_chain_decl)
18233 add_AT_location_description (subr_die, DW_AT_static_link,
18234 loc_list_from_tree (fun->static_chain_decl, 2));
18237 /* Generate child dies for template paramaters. */
18238 if (debug_info_level > DINFO_LEVEL_TERSE)
18239 gen_generic_params_dies (decl);
18241 /* Now output descriptions of the arguments for this function. This gets
18242 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18243 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18244 `...' at the end of the formal parameter list. In order to find out if
18245 there was a trailing ellipsis or not, we must instead look at the type
18246 associated with the FUNCTION_DECL. This will be a node of type
18247 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18248 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18249 an ellipsis at the end. */
18251 /* In the case where we are describing a mere function declaration, all we
18252 need to do here (and all we *can* do here) is to describe the *types* of
18253 its formal parameters. */
18254 if (debug_info_level <= DINFO_LEVEL_TERSE)
18256 else if (declaration)
18257 gen_formal_types_die (decl, subr_die);
18258 else
18260 /* Generate DIEs to represent all known formal parameters. */
18261 tree parm = DECL_ARGUMENTS (decl);
18262 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18263 tree generic_decl_parm = generic_decl
18264 ? DECL_ARGUMENTS (generic_decl)
18265 : NULL;
18267 /* Now we want to walk the list of parameters of the function and
18268 emit their relevant DIEs.
18270 We consider the case of DECL being an instance of a generic function
18271 as well as it being a normal function.
18273 If DECL is an instance of a generic function we walk the
18274 parameters of the generic function declaration _and_ the parameters of
18275 DECL itself. This is useful because we want to emit specific DIEs for
18276 function parameter packs and those are declared as part of the
18277 generic function declaration. In that particular case,
18278 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18279 That DIE has children DIEs representing the set of arguments
18280 of the pack. Note that the set of pack arguments can be empty.
18281 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18282 children DIE.
18284 Otherwise, we just consider the parameters of DECL. */
18285 while (generic_decl_parm || parm)
18287 if (generic_decl_parm
18288 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18289 gen_formal_parameter_pack_die (generic_decl_parm,
18290 parm, subr_die,
18291 &parm);
18292 else if (parm)
18294 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18296 if (parm == DECL_ARGUMENTS (decl)
18297 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18298 && parm_die
18299 && (dwarf_version >= 3 || !dwarf_strict))
18300 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18302 parm = DECL_CHAIN (parm);
18305 if (generic_decl_parm)
18306 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18309 /* Decide whether we need an unspecified_parameters DIE at the end.
18310 There are 2 more cases to do this for: 1) the ansi ... declaration -
18311 this is detectable when the end of the arg list is not a
18312 void_type_node 2) an unprototyped function declaration (not a
18313 definition). This just means that we have no info about the
18314 parameters at all. */
18315 if (prototype_p (TREE_TYPE (decl)))
18317 /* This is the prototyped case, check for.... */
18318 if (stdarg_p (TREE_TYPE (decl)))
18319 gen_unspecified_parameters_die (decl, subr_die);
18321 else if (DECL_INITIAL (decl) == NULL_TREE)
18322 gen_unspecified_parameters_die (decl, subr_die);
18325 /* Output Dwarf info for all of the stuff within the body of the function
18326 (if it has one - it may be just a declaration). */
18327 outer_scope = DECL_INITIAL (decl);
18329 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18330 a function. This BLOCK actually represents the outermost binding contour
18331 for the function, i.e. the contour in which the function's formal
18332 parameters and labels get declared. Curiously, it appears that the front
18333 end doesn't actually put the PARM_DECL nodes for the current function onto
18334 the BLOCK_VARS list for this outer scope, but are strung off of the
18335 DECL_ARGUMENTS list for the function instead.
18337 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18338 the LABEL_DECL nodes for the function however, and we output DWARF info
18339 for those in decls_for_scope. Just within the `outer_scope' there will be
18340 a BLOCK node representing the function's outermost pair of curly braces,
18341 and any blocks used for the base and member initializers of a C++
18342 constructor function. */
18343 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18345 int call_site_note_count = 0;
18346 int tail_call_site_note_count = 0;
18348 /* Emit a DW_TAG_variable DIE for a named return value. */
18349 if (DECL_NAME (DECL_RESULT (decl)))
18350 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18352 current_function_has_inlines = 0;
18353 decls_for_scope (outer_scope, subr_die, 0);
18355 if (call_arg_locations && !dwarf_strict)
18357 struct call_arg_loc_node *ca_loc;
18358 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18360 dw_die_ref die = NULL;
18361 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18362 rtx arg, next_arg;
18364 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18365 arg; arg = next_arg)
18367 dw_loc_descr_ref reg, val;
18368 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18369 dw_die_ref cdie, tdie = NULL;
18371 next_arg = XEXP (arg, 1);
18372 if (REG_P (XEXP (XEXP (arg, 0), 0))
18373 && next_arg
18374 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18375 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18376 && REGNO (XEXP (XEXP (arg, 0), 0))
18377 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18378 next_arg = XEXP (next_arg, 1);
18379 if (mode == VOIDmode)
18381 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18382 if (mode == VOIDmode)
18383 mode = GET_MODE (XEXP (arg, 0));
18385 if (mode == VOIDmode || mode == BLKmode)
18386 continue;
18387 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18389 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18390 tloc = XEXP (XEXP (arg, 0), 1);
18391 continue;
18393 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18394 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18396 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18397 tlocc = XEXP (XEXP (arg, 0), 1);
18398 continue;
18400 reg = NULL;
18401 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18402 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18403 VAR_INIT_STATUS_INITIALIZED);
18404 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18406 rtx mem = XEXP (XEXP (arg, 0), 0);
18407 reg = mem_loc_descriptor (XEXP (mem, 0),
18408 get_address_mode (mem),
18409 GET_MODE (mem),
18410 VAR_INIT_STATUS_INITIALIZED);
18412 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18413 == DEBUG_PARAMETER_REF)
18415 tree tdecl
18416 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18417 tdie = lookup_decl_die (tdecl);
18418 if (tdie == NULL)
18419 continue;
18421 else
18422 continue;
18423 if (reg == NULL
18424 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18425 != DEBUG_PARAMETER_REF)
18426 continue;
18427 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18428 VOIDmode,
18429 VAR_INIT_STATUS_INITIALIZED);
18430 if (val == NULL)
18431 continue;
18432 if (die == NULL)
18433 die = gen_call_site_die (decl, subr_die, ca_loc);
18434 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18435 NULL_TREE);
18436 if (reg != NULL)
18437 add_AT_loc (cdie, DW_AT_location, reg);
18438 else if (tdie != NULL)
18439 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18440 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18441 if (next_arg != XEXP (arg, 1))
18443 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18444 if (mode == VOIDmode)
18445 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18446 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18447 0), 1),
18448 mode, VOIDmode,
18449 VAR_INIT_STATUS_INITIALIZED);
18450 if (val != NULL)
18451 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18454 if (die == NULL
18455 && (ca_loc->symbol_ref || tloc))
18456 die = gen_call_site_die (decl, subr_die, ca_loc);
18457 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18459 dw_loc_descr_ref tval = NULL;
18461 if (tloc != NULL_RTX)
18462 tval = mem_loc_descriptor (tloc,
18463 GET_MODE (tloc) == VOIDmode
18464 ? Pmode : GET_MODE (tloc),
18465 VOIDmode,
18466 VAR_INIT_STATUS_INITIALIZED);
18467 if (tval)
18468 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18469 else if (tlocc != NULL_RTX)
18471 tval = mem_loc_descriptor (tlocc,
18472 GET_MODE (tlocc) == VOIDmode
18473 ? Pmode : GET_MODE (tlocc),
18474 VOIDmode,
18475 VAR_INIT_STATUS_INITIALIZED);
18476 if (tval)
18477 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18478 tval);
18481 if (die != NULL)
18483 call_site_note_count++;
18484 if (ca_loc->tail_call_p)
18485 tail_call_site_note_count++;
18489 call_arg_locations = NULL;
18490 call_arg_loc_last = NULL;
18491 if (tail_call_site_count >= 0
18492 && tail_call_site_count == tail_call_site_note_count
18493 && !dwarf_strict)
18495 if (call_site_count >= 0
18496 && call_site_count == call_site_note_count)
18497 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18498 else
18499 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18501 call_site_count = -1;
18502 tail_call_site_count = -1;
18504 /* Add the calling convention attribute if requested. */
18505 add_calling_convention_attribute (subr_die, decl);
18509 /* Returns a hash value for X (which really is a die_struct). */
18511 static hashval_t
18512 common_block_die_table_hash (const void *x)
18514 const_dw_die_ref d = (const_dw_die_ref) x;
18515 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18518 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18519 as decl_id and die_parent of die_struct Y. */
18521 static int
18522 common_block_die_table_eq (const void *x, const void *y)
18524 const_dw_die_ref d = (const_dw_die_ref) x;
18525 const_dw_die_ref e = (const_dw_die_ref) y;
18526 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18529 /* Generate a DIE to represent a declared data object.
18530 Either DECL or ORIGIN must be non-null. */
18532 static void
18533 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18535 HOST_WIDE_INT off = 0;
18536 tree com_decl;
18537 tree decl_or_origin = decl ? decl : origin;
18538 tree ultimate_origin;
18539 dw_die_ref var_die;
18540 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18541 dw_die_ref origin_die;
18542 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18543 || class_or_namespace_scope_p (context_die));
18544 bool specialization_p = false;
18546 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18547 if (decl || ultimate_origin)
18548 origin = ultimate_origin;
18549 com_decl = fortran_common (decl_or_origin, &off);
18551 /* Symbol in common gets emitted as a child of the common block, in the form
18552 of a data member. */
18553 if (com_decl)
18555 dw_die_ref com_die;
18556 dw_loc_list_ref loc;
18557 die_node com_die_arg;
18559 var_die = lookup_decl_die (decl_or_origin);
18560 if (var_die)
18562 if (get_AT (var_die, DW_AT_location) == NULL)
18564 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18565 if (loc)
18567 if (off)
18569 /* Optimize the common case. */
18570 if (single_element_loc_list_p (loc)
18571 && loc->expr->dw_loc_opc == DW_OP_addr
18572 && loc->expr->dw_loc_next == NULL
18573 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18574 == SYMBOL_REF)
18576 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18577 loc->expr->dw_loc_oprnd1.v.val_addr
18578 = plus_constant (GET_MODE (x), x , off);
18580 else
18581 loc_list_plus_const (loc, off);
18583 add_AT_location_description (var_die, DW_AT_location, loc);
18584 remove_AT (var_die, DW_AT_declaration);
18587 return;
18590 if (common_block_die_table == NULL)
18591 common_block_die_table
18592 = htab_create_ggc (10, common_block_die_table_hash,
18593 common_block_die_table_eq, NULL);
18595 com_die_arg.decl_id = DECL_UID (com_decl);
18596 com_die_arg.die_parent = context_die;
18597 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18598 loc = loc_list_from_tree (com_decl, 2);
18599 if (com_die == NULL)
18601 const char *cnam
18602 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18603 void **slot;
18605 com_die = new_die (DW_TAG_common_block, context_die, decl);
18606 add_name_and_src_coords_attributes (com_die, com_decl);
18607 if (loc)
18609 add_AT_location_description (com_die, DW_AT_location, loc);
18610 /* Avoid sharing the same loc descriptor between
18611 DW_TAG_common_block and DW_TAG_variable. */
18612 loc = loc_list_from_tree (com_decl, 2);
18614 else if (DECL_EXTERNAL (decl))
18615 add_AT_flag (com_die, DW_AT_declaration, 1);
18616 if (want_pubnames ())
18617 add_pubname_string (cnam, com_die); /* ??? needed? */
18618 com_die->decl_id = DECL_UID (com_decl);
18619 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18620 *slot = (void *) com_die;
18622 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18624 add_AT_location_description (com_die, DW_AT_location, loc);
18625 loc = loc_list_from_tree (com_decl, 2);
18626 remove_AT (com_die, DW_AT_declaration);
18628 var_die = new_die (DW_TAG_variable, com_die, decl);
18629 add_name_and_src_coords_attributes (var_die, decl);
18630 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18631 TREE_THIS_VOLATILE (decl), context_die);
18632 add_AT_flag (var_die, DW_AT_external, 1);
18633 if (loc)
18635 if (off)
18637 /* Optimize the common case. */
18638 if (single_element_loc_list_p (loc)
18639 && loc->expr->dw_loc_opc == DW_OP_addr
18640 && loc->expr->dw_loc_next == NULL
18641 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18643 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18644 loc->expr->dw_loc_oprnd1.v.val_addr
18645 = plus_constant (GET_MODE (x), x, off);
18647 else
18648 loc_list_plus_const (loc, off);
18650 add_AT_location_description (var_die, DW_AT_location, loc);
18652 else if (DECL_EXTERNAL (decl))
18653 add_AT_flag (var_die, DW_AT_declaration, 1);
18654 equate_decl_number_to_die (decl, var_die);
18655 return;
18658 /* If the compiler emitted a definition for the DECL declaration
18659 and if we already emitted a DIE for it, don't emit a second
18660 DIE for it again. Allow re-declarations of DECLs that are
18661 inside functions, though. */
18662 if (old_die && declaration && !local_scope_p (context_die))
18663 return;
18665 /* For static data members, the declaration in the class is supposed
18666 to have DW_TAG_member tag; the specification should still be
18667 DW_TAG_variable referencing the DW_TAG_member DIE. */
18668 if (declaration && class_scope_p (context_die))
18669 var_die = new_die (DW_TAG_member, context_die, decl);
18670 else
18671 var_die = new_die (DW_TAG_variable, context_die, decl);
18673 origin_die = NULL;
18674 if (origin != NULL)
18675 origin_die = add_abstract_origin_attribute (var_die, origin);
18677 /* Loop unrolling can create multiple blocks that refer to the same
18678 static variable, so we must test for the DW_AT_declaration flag.
18680 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18681 copy decls and set the DECL_ABSTRACT flag on them instead of
18682 sharing them.
18684 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18686 ??? The declare_in_namespace support causes us to get two DIEs for one
18687 variable, both of which are declarations. We want to avoid considering
18688 one to be a specification, so we must test that this DIE is not a
18689 declaration. */
18690 else if (old_die && TREE_STATIC (decl) && ! declaration
18691 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18693 /* This is a definition of a C++ class level static. */
18694 add_AT_specification (var_die, old_die);
18695 specialization_p = true;
18696 if (DECL_NAME (decl))
18698 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18699 struct dwarf_file_data * file_index = lookup_filename (s.file);
18701 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18702 add_AT_file (var_die, DW_AT_decl_file, file_index);
18704 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18705 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18707 if (old_die->die_tag == DW_TAG_member)
18708 add_linkage_name (var_die, decl);
18711 else
18712 add_name_and_src_coords_attributes (var_die, decl);
18714 if ((origin == NULL && !specialization_p)
18715 || (origin != NULL
18716 && !DECL_ABSTRACT (decl_or_origin)
18717 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18718 decl_function_context
18719 (decl_or_origin))))
18721 tree type = TREE_TYPE (decl_or_origin);
18723 if (decl_by_reference_p (decl_or_origin))
18724 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18725 else
18726 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18727 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18730 if (origin == NULL && !specialization_p)
18732 if (TREE_PUBLIC (decl))
18733 add_AT_flag (var_die, DW_AT_external, 1);
18735 if (DECL_ARTIFICIAL (decl))
18736 add_AT_flag (var_die, DW_AT_artificial, 1);
18738 add_accessibility_attribute (var_die, decl);
18741 if (declaration)
18742 add_AT_flag (var_die, DW_AT_declaration, 1);
18744 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18745 equate_decl_number_to_die (decl, var_die);
18747 if (! declaration
18748 && (! DECL_ABSTRACT (decl_or_origin)
18749 /* Local static vars are shared between all clones/inlines,
18750 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18751 already set. */
18752 || (TREE_CODE (decl_or_origin) == VAR_DECL
18753 && TREE_STATIC (decl_or_origin)
18754 && DECL_RTL_SET_P (decl_or_origin)))
18755 /* When abstract origin already has DW_AT_location attribute, no need
18756 to add it again. */
18757 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18759 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18760 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18761 defer_location (decl_or_origin, var_die);
18762 else
18763 add_location_or_const_value_attribute (var_die, decl_or_origin,
18764 decl == NULL, DW_AT_location);
18765 add_pubname (decl_or_origin, var_die);
18767 else
18768 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18771 /* Generate a DIE to represent a named constant. */
18773 static void
18774 gen_const_die (tree decl, dw_die_ref context_die)
18776 dw_die_ref const_die;
18777 tree type = TREE_TYPE (decl);
18779 const_die = new_die (DW_TAG_constant, context_die, decl);
18780 add_name_and_src_coords_attributes (const_die, decl);
18781 add_type_attribute (const_die, type, 1, 0, context_die);
18782 if (TREE_PUBLIC (decl))
18783 add_AT_flag (const_die, DW_AT_external, 1);
18784 if (DECL_ARTIFICIAL (decl))
18785 add_AT_flag (const_die, DW_AT_artificial, 1);
18786 tree_add_const_value_attribute_for_decl (const_die, decl);
18789 /* Generate a DIE to represent a label identifier. */
18791 static void
18792 gen_label_die (tree decl, dw_die_ref context_die)
18794 tree origin = decl_ultimate_origin (decl);
18795 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18796 rtx insn;
18797 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18799 if (origin != NULL)
18800 add_abstract_origin_attribute (lbl_die, origin);
18801 else
18802 add_name_and_src_coords_attributes (lbl_die, decl);
18804 if (DECL_ABSTRACT (decl))
18805 equate_decl_number_to_die (decl, lbl_die);
18806 else
18808 insn = DECL_RTL_IF_SET (decl);
18810 /* Deleted labels are programmer specified labels which have been
18811 eliminated because of various optimizations. We still emit them
18812 here so that it is possible to put breakpoints on them. */
18813 if (insn
18814 && (LABEL_P (insn)
18815 || ((NOTE_P (insn)
18816 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18818 /* When optimization is enabled (via -O) some parts of the compiler
18819 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18820 represent source-level labels which were explicitly declared by
18821 the user. This really shouldn't be happening though, so catch
18822 it if it ever does happen. */
18823 gcc_assert (!INSN_DELETED_P (insn));
18825 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18826 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18828 else if (insn
18829 && NOTE_P (insn)
18830 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18831 && CODE_LABEL_NUMBER (insn) != -1)
18833 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18834 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18839 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18840 attributes to the DIE for a block STMT, to describe where the inlined
18841 function was called from. This is similar to add_src_coords_attributes. */
18843 static inline void
18844 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18846 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18848 if (dwarf_version >= 3 || !dwarf_strict)
18850 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18851 add_AT_unsigned (die, DW_AT_call_line, s.line);
18856 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18857 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18859 static inline void
18860 add_high_low_attributes (tree stmt, dw_die_ref die)
18862 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18864 if (BLOCK_FRAGMENT_CHAIN (stmt)
18865 && (dwarf_version >= 3 || !dwarf_strict))
18867 tree chain, superblock = NULL_TREE;
18868 dw_die_ref pdie;
18869 dw_attr_ref attr = NULL;
18871 if (inlined_function_outer_scope_p (stmt))
18873 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18874 BLOCK_NUMBER (stmt));
18875 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18878 /* Optimize duplicate .debug_ranges lists or even tails of
18879 lists. If this BLOCK has same ranges as its supercontext,
18880 lookup DW_AT_ranges attribute in the supercontext (and
18881 recursively so), verify that the ranges_table contains the
18882 right values and use it instead of adding a new .debug_range. */
18883 for (chain = stmt, pdie = die;
18884 BLOCK_SAME_RANGE (chain);
18885 chain = BLOCK_SUPERCONTEXT (chain))
18887 dw_attr_ref new_attr;
18889 pdie = pdie->die_parent;
18890 if (pdie == NULL)
18891 break;
18892 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18893 break;
18894 new_attr = get_AT (pdie, DW_AT_ranges);
18895 if (new_attr == NULL
18896 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18897 break;
18898 attr = new_attr;
18899 superblock = BLOCK_SUPERCONTEXT (chain);
18901 if (attr != NULL
18902 && (ranges_table[attr->dw_attr_val.v.val_offset
18903 / 2 / DWARF2_ADDR_SIZE].num
18904 == BLOCK_NUMBER (superblock))
18905 && BLOCK_FRAGMENT_CHAIN (superblock))
18907 unsigned long off = attr->dw_attr_val.v.val_offset
18908 / 2 / DWARF2_ADDR_SIZE;
18909 unsigned long supercnt = 0, thiscnt = 0;
18910 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18911 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18913 ++supercnt;
18914 gcc_checking_assert (ranges_table[off + supercnt].num
18915 == BLOCK_NUMBER (chain));
18917 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18918 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18919 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18920 ++thiscnt;
18921 gcc_assert (supercnt >= thiscnt);
18922 add_AT_range_list (die, DW_AT_ranges,
18923 ((off + supercnt - thiscnt)
18924 * 2 * DWARF2_ADDR_SIZE),
18925 false);
18926 return;
18929 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18931 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18934 add_ranges (chain);
18935 chain = BLOCK_FRAGMENT_CHAIN (chain);
18937 while (chain);
18938 add_ranges (NULL);
18940 else
18942 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18943 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18944 BLOCK_NUMBER (stmt));
18945 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18946 BLOCK_NUMBER (stmt));
18947 add_AT_low_high_pc (die, label, label_high, false);
18951 /* Generate a DIE for a lexical block. */
18953 static void
18954 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18956 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18958 if (call_arg_locations)
18960 if (block_map.length () <= BLOCK_NUMBER (stmt))
18961 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18962 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18965 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18966 add_high_low_attributes (stmt, stmt_die);
18968 decls_for_scope (stmt, stmt_die, depth);
18971 /* Generate a DIE for an inlined subprogram. */
18973 static void
18974 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18976 tree decl;
18978 /* The instance of function that is effectively being inlined shall not
18979 be abstract. */
18980 gcc_assert (! BLOCK_ABSTRACT (stmt));
18982 decl = block_ultimate_origin (stmt);
18984 /* Emit info for the abstract instance first, if we haven't yet. We
18985 must emit this even if the block is abstract, otherwise when we
18986 emit the block below (or elsewhere), we may end up trying to emit
18987 a die whose origin die hasn't been emitted, and crashing. */
18988 dwarf2out_abstract_function (decl);
18990 if (! BLOCK_ABSTRACT (stmt))
18992 dw_die_ref subr_die
18993 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18995 if (call_arg_locations)
18997 if (block_map.length () <= BLOCK_NUMBER (stmt))
18998 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18999 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19001 add_abstract_origin_attribute (subr_die, decl);
19002 if (TREE_ASM_WRITTEN (stmt))
19003 add_high_low_attributes (stmt, subr_die);
19004 add_call_src_coords_attributes (stmt, subr_die);
19006 decls_for_scope (stmt, subr_die, depth);
19007 current_function_has_inlines = 1;
19011 /* Generate a DIE for a field in a record, or structure. */
19013 static void
19014 gen_field_die (tree decl, dw_die_ref context_die)
19016 dw_die_ref decl_die;
19018 if (TREE_TYPE (decl) == error_mark_node)
19019 return;
19021 decl_die = new_die (DW_TAG_member, context_die, decl);
19022 add_name_and_src_coords_attributes (decl_die, decl);
19023 add_type_attribute (decl_die, member_declared_type (decl),
19024 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19025 context_die);
19027 if (DECL_BIT_FIELD_TYPE (decl))
19029 add_byte_size_attribute (decl_die, decl);
19030 add_bit_size_attribute (decl_die, decl);
19031 add_bit_offset_attribute (decl_die, decl);
19034 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19035 add_data_member_location_attribute (decl_die, decl);
19037 if (DECL_ARTIFICIAL (decl))
19038 add_AT_flag (decl_die, DW_AT_artificial, 1);
19040 add_accessibility_attribute (decl_die, decl);
19042 /* Equate decl number to die, so that we can look up this decl later on. */
19043 equate_decl_number_to_die (decl, decl_die);
19046 #if 0
19047 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19048 Use modified_type_die instead.
19049 We keep this code here just in case these types of DIEs may be needed to
19050 represent certain things in other languages (e.g. Pascal) someday. */
19052 static void
19053 gen_pointer_type_die (tree type, dw_die_ref context_die)
19055 dw_die_ref ptr_die
19056 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19058 equate_type_number_to_die (type, ptr_die);
19059 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19060 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19063 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19064 Use modified_type_die instead.
19065 We keep this code here just in case these types of DIEs may be needed to
19066 represent certain things in other languages (e.g. Pascal) someday. */
19068 static void
19069 gen_reference_type_die (tree type, dw_die_ref context_die)
19071 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19073 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19074 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19075 else
19076 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19078 equate_type_number_to_die (type, ref_die);
19079 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19080 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19082 #endif
19084 /* Generate a DIE for a pointer to a member type. */
19086 static void
19087 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19089 dw_die_ref ptr_die
19090 = new_die (DW_TAG_ptr_to_member_type,
19091 scope_die_for (type, context_die), type);
19093 equate_type_number_to_die (type, ptr_die);
19094 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19095 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19096 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19099 typedef const char *dchar_p; /* For DEF_VEC_P. */
19101 static char *producer_string;
19103 /* Return a heap allocated producer string including command line options
19104 if -grecord-gcc-switches. */
19106 static char *
19107 gen_producer_string (void)
19109 size_t j;
19110 auto_vec<dchar_p> switches;
19111 const char *language_string = lang_hooks.name;
19112 char *producer, *tail;
19113 const char *p;
19114 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19115 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19117 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19118 switch (save_decoded_options[j].opt_index)
19120 case OPT_o:
19121 case OPT_d:
19122 case OPT_dumpbase:
19123 case OPT_dumpdir:
19124 case OPT_auxbase:
19125 case OPT_auxbase_strip:
19126 case OPT_quiet:
19127 case OPT_version:
19128 case OPT_v:
19129 case OPT_w:
19130 case OPT_L:
19131 case OPT_D:
19132 case OPT_I:
19133 case OPT_U:
19134 case OPT_SPECIAL_unknown:
19135 case OPT_SPECIAL_ignore:
19136 case OPT_SPECIAL_program_name:
19137 case OPT_SPECIAL_input_file:
19138 case OPT_grecord_gcc_switches:
19139 case OPT_gno_record_gcc_switches:
19140 case OPT__output_pch_:
19141 case OPT_fdiagnostics_show_location_:
19142 case OPT_fdiagnostics_show_option:
19143 case OPT_fdiagnostics_show_caret:
19144 case OPT_fdiagnostics_color_:
19145 case OPT_fverbose_asm:
19146 case OPT____:
19147 case OPT__sysroot_:
19148 case OPT_nostdinc:
19149 case OPT_nostdinc__:
19150 /* Ignore these. */
19151 continue;
19152 default:
19153 if (cl_options[save_decoded_options[j].opt_index].flags
19154 & CL_NO_DWARF_RECORD)
19155 continue;
19156 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19157 == '-');
19158 switch (save_decoded_options[j].canonical_option[0][1])
19160 case 'M':
19161 case 'i':
19162 case 'W':
19163 continue;
19164 case 'f':
19165 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19166 "dump", 4) == 0)
19167 continue;
19168 break;
19169 default:
19170 break;
19172 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19173 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19174 break;
19177 producer = XNEWVEC (char, plen + 1 + len + 1);
19178 tail = producer;
19179 sprintf (tail, "%s %s", language_string, version_string);
19180 tail += plen;
19182 FOR_EACH_VEC_ELT (switches, j, p)
19184 len = strlen (p);
19185 *tail = ' ';
19186 memcpy (tail + 1, p, len);
19187 tail += len + 1;
19190 *tail = '\0';
19191 return producer;
19194 /* Generate the DIE for the compilation unit. */
19196 static dw_die_ref
19197 gen_compile_unit_die (const char *filename)
19199 dw_die_ref die;
19200 const char *language_string = lang_hooks.name;
19201 int language;
19203 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19205 if (filename)
19207 add_name_attribute (die, filename);
19208 /* Don't add cwd for <built-in>. */
19209 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19210 add_comp_dir_attribute (die);
19213 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19215 /* If our producer is LTO try to figure out a common language to use
19216 from the global list of translation units. */
19217 if (strcmp (language_string, "GNU GIMPLE") == 0)
19219 unsigned i;
19220 tree t;
19221 const char *common_lang = NULL;
19223 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19225 if (!TRANSLATION_UNIT_LANGUAGE (t))
19226 continue;
19227 if (!common_lang)
19228 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19229 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19231 else if (strncmp (common_lang, "GNU C", 5) == 0
19232 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19233 /* Mixing C and C++ is ok, use C++ in that case. */
19234 common_lang = "GNU C++";
19235 else
19237 /* Fall back to C. */
19238 common_lang = NULL;
19239 break;
19243 if (common_lang)
19244 language_string = common_lang;
19247 language = DW_LANG_C89;
19248 if (strcmp (language_string, "GNU C++") == 0)
19249 language = DW_LANG_C_plus_plus;
19250 else if (strcmp (language_string, "GNU F77") == 0)
19251 language = DW_LANG_Fortran77;
19252 else if (strcmp (language_string, "GNU Pascal") == 0)
19253 language = DW_LANG_Pascal83;
19254 else if (dwarf_version >= 3 || !dwarf_strict)
19256 if (strcmp (language_string, "GNU Ada") == 0)
19257 language = DW_LANG_Ada95;
19258 else if (strcmp (language_string, "GNU Fortran") == 0)
19259 language = DW_LANG_Fortran95;
19260 else if (strcmp (language_string, "GNU Java") == 0)
19261 language = DW_LANG_Java;
19262 else if (strcmp (language_string, "GNU Objective-C") == 0)
19263 language = DW_LANG_ObjC;
19264 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19265 language = DW_LANG_ObjC_plus_plus;
19266 else if (dwarf_version >= 5 || !dwarf_strict)
19268 if (strcmp (language_string, "GNU Go") == 0)
19269 language = DW_LANG_Go;
19272 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19273 else if (strcmp (language_string, "GNU Fortran") == 0)
19274 language = DW_LANG_Fortran90;
19276 add_AT_unsigned (die, DW_AT_language, language);
19278 switch (language)
19280 case DW_LANG_Fortran77:
19281 case DW_LANG_Fortran90:
19282 case DW_LANG_Fortran95:
19283 /* Fortran has case insensitive identifiers and the front-end
19284 lowercases everything. */
19285 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19286 break;
19287 default:
19288 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19289 break;
19291 return die;
19294 /* Generate the DIE for a base class. */
19296 static void
19297 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19299 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19301 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19302 add_data_member_location_attribute (die, binfo);
19304 if (BINFO_VIRTUAL_P (binfo))
19305 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19307 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19308 children, otherwise the default is DW_ACCESS_public. In DWARF2
19309 the default has always been DW_ACCESS_private. */
19310 if (access == access_public_node)
19312 if (dwarf_version == 2
19313 || context_die->die_tag == DW_TAG_class_type)
19314 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19316 else if (access == access_protected_node)
19317 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19318 else if (dwarf_version > 2
19319 && context_die->die_tag != DW_TAG_class_type)
19320 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19323 /* Generate a DIE for a class member. */
19325 static void
19326 gen_member_die (tree type, dw_die_ref context_die)
19328 tree member;
19329 tree binfo = TYPE_BINFO (type);
19330 dw_die_ref child;
19332 /* If this is not an incomplete type, output descriptions of each of its
19333 members. Note that as we output the DIEs necessary to represent the
19334 members of this record or union type, we will also be trying to output
19335 DIEs to represent the *types* of those members. However the `type'
19336 function (above) will specifically avoid generating type DIEs for member
19337 types *within* the list of member DIEs for this (containing) type except
19338 for those types (of members) which are explicitly marked as also being
19339 members of this (containing) type themselves. The g++ front- end can
19340 force any given type to be treated as a member of some other (containing)
19341 type by setting the TYPE_CONTEXT of the given (member) type to point to
19342 the TREE node representing the appropriate (containing) type. */
19344 /* First output info about the base classes. */
19345 if (binfo)
19347 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19348 int i;
19349 tree base;
19351 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19352 gen_inheritance_die (base,
19353 (accesses ? (*accesses)[i] : access_public_node),
19354 context_die);
19357 /* Now output info about the data members and type members. */
19358 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19360 /* If we thought we were generating minimal debug info for TYPE
19361 and then changed our minds, some of the member declarations
19362 may have already been defined. Don't define them again, but
19363 do put them in the right order. */
19365 child = lookup_decl_die (member);
19366 if (child)
19367 splice_child_die (context_die, child);
19368 else
19369 gen_decl_die (member, NULL, context_die);
19372 /* Now output info about the function members (if any). */
19373 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19375 /* Don't include clones in the member list. */
19376 if (DECL_ABSTRACT_ORIGIN (member))
19377 continue;
19379 child = lookup_decl_die (member);
19380 if (child)
19381 splice_child_die (context_die, child);
19382 else
19383 gen_decl_die (member, NULL, context_die);
19387 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19388 is set, we pretend that the type was never defined, so we only get the
19389 member DIEs needed by later specification DIEs. */
19391 static void
19392 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19393 enum debug_info_usage usage)
19395 dw_die_ref type_die = lookup_type_die (type);
19396 dw_die_ref scope_die = 0;
19397 int nested = 0;
19398 int complete = (TYPE_SIZE (type)
19399 && (! TYPE_STUB_DECL (type)
19400 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19401 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19402 complete = complete && should_emit_struct_debug (type, usage);
19404 if (type_die && ! complete)
19405 return;
19407 if (TYPE_CONTEXT (type) != NULL_TREE
19408 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19409 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19410 nested = 1;
19412 scope_die = scope_die_for (type, context_die);
19414 /* Generate child dies for template paramaters. */
19415 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19416 schedule_generic_params_dies_gen (type);
19418 if (! type_die || (nested && is_cu_die (scope_die)))
19419 /* First occurrence of type or toplevel definition of nested class. */
19421 dw_die_ref old_die = type_die;
19423 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19424 ? record_type_tag (type) : DW_TAG_union_type,
19425 scope_die, type);
19426 equate_type_number_to_die (type, type_die);
19427 if (old_die)
19428 add_AT_specification (type_die, old_die);
19429 else
19430 add_name_attribute (type_die, type_tag (type));
19432 else
19433 remove_AT (type_die, DW_AT_declaration);
19435 /* If this type has been completed, then give it a byte_size attribute and
19436 then give a list of members. */
19437 if (complete && !ns_decl)
19439 /* Prevent infinite recursion in cases where the type of some member of
19440 this type is expressed in terms of this type itself. */
19441 TREE_ASM_WRITTEN (type) = 1;
19442 add_byte_size_attribute (type_die, type);
19443 if (TYPE_STUB_DECL (type) != NULL_TREE)
19445 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19446 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19449 /* If the first reference to this type was as the return type of an
19450 inline function, then it may not have a parent. Fix this now. */
19451 if (type_die->die_parent == NULL)
19452 add_child_die (scope_die, type_die);
19454 push_decl_scope (type);
19455 gen_member_die (type, type_die);
19456 pop_decl_scope ();
19458 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19459 if (TYPE_ARTIFICIAL (type))
19460 add_AT_flag (type_die, DW_AT_artificial, 1);
19462 /* GNU extension: Record what type our vtable lives in. */
19463 if (TYPE_VFIELD (type))
19465 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19467 gen_type_die (vtype, context_die);
19468 add_AT_die_ref (type_die, DW_AT_containing_type,
19469 lookup_type_die (vtype));
19472 else
19474 add_AT_flag (type_die, DW_AT_declaration, 1);
19476 /* We don't need to do this for function-local types. */
19477 if (TYPE_STUB_DECL (type)
19478 && ! decl_function_context (TYPE_STUB_DECL (type)))
19479 vec_safe_push (incomplete_types, type);
19482 if (get_AT (type_die, DW_AT_name))
19483 add_pubtype (type, type_die);
19486 /* Generate a DIE for a subroutine _type_. */
19488 static void
19489 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19491 tree return_type = TREE_TYPE (type);
19492 dw_die_ref subr_die
19493 = new_die (DW_TAG_subroutine_type,
19494 scope_die_for (type, context_die), type);
19496 equate_type_number_to_die (type, subr_die);
19497 add_prototyped_attribute (subr_die, type);
19498 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19499 gen_formal_types_die (type, subr_die);
19501 if (get_AT (subr_die, DW_AT_name))
19502 add_pubtype (type, subr_die);
19505 /* Generate a DIE for a type definition. */
19507 static void
19508 gen_typedef_die (tree decl, dw_die_ref context_die)
19510 dw_die_ref type_die;
19511 tree origin;
19513 if (TREE_ASM_WRITTEN (decl))
19514 return;
19516 TREE_ASM_WRITTEN (decl) = 1;
19517 type_die = new_die (DW_TAG_typedef, context_die, decl);
19518 origin = decl_ultimate_origin (decl);
19519 if (origin != NULL)
19520 add_abstract_origin_attribute (type_die, origin);
19521 else
19523 tree type;
19525 add_name_and_src_coords_attributes (type_die, decl);
19526 if (DECL_ORIGINAL_TYPE (decl))
19528 type = DECL_ORIGINAL_TYPE (decl);
19530 gcc_assert (type != TREE_TYPE (decl));
19531 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19533 else
19535 type = TREE_TYPE (decl);
19537 if (is_naming_typedef_decl (TYPE_NAME (type)))
19539 /* Here, we are in the case of decl being a typedef naming
19540 an anonymous type, e.g:
19541 typedef struct {...} foo;
19542 In that case TREE_TYPE (decl) is not a typedef variant
19543 type and TYPE_NAME of the anonymous type is set to the
19544 TYPE_DECL of the typedef. This construct is emitted by
19545 the C++ FE.
19547 TYPE is the anonymous struct named by the typedef
19548 DECL. As we need the DW_AT_type attribute of the
19549 DW_TAG_typedef to point to the DIE of TYPE, let's
19550 generate that DIE right away. add_type_attribute
19551 called below will then pick (via lookup_type_die) that
19552 anonymous struct DIE. */
19553 if (!TREE_ASM_WRITTEN (type))
19554 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19556 /* This is a GNU Extension. We are adding a
19557 DW_AT_linkage_name attribute to the DIE of the
19558 anonymous struct TYPE. The value of that attribute
19559 is the name of the typedef decl naming the anonymous
19560 struct. This greatly eases the work of consumers of
19561 this debug info. */
19562 add_linkage_attr (lookup_type_die (type), decl);
19566 add_type_attribute (type_die, type, TREE_READONLY (decl),
19567 TREE_THIS_VOLATILE (decl), context_die);
19569 if (is_naming_typedef_decl (decl))
19570 /* We want that all subsequent calls to lookup_type_die with
19571 TYPE in argument yield the DW_TAG_typedef we have just
19572 created. */
19573 equate_type_number_to_die (type, type_die);
19575 add_accessibility_attribute (type_die, decl);
19578 if (DECL_ABSTRACT (decl))
19579 equate_decl_number_to_die (decl, type_die);
19581 if (get_AT (type_die, DW_AT_name))
19582 add_pubtype (decl, type_die);
19585 /* Generate a DIE for a struct, class, enum or union type. */
19587 static void
19588 gen_tagged_type_die (tree type,
19589 dw_die_ref context_die,
19590 enum debug_info_usage usage)
19592 int need_pop;
19594 if (type == NULL_TREE
19595 || !is_tagged_type (type))
19596 return;
19598 /* If this is a nested type whose containing class hasn't been written
19599 out yet, writing it out will cover this one, too. This does not apply
19600 to instantiations of member class templates; they need to be added to
19601 the containing class as they are generated. FIXME: This hurts the
19602 idea of combining type decls from multiple TUs, since we can't predict
19603 what set of template instantiations we'll get. */
19604 if (TYPE_CONTEXT (type)
19605 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19606 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19608 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19610 if (TREE_ASM_WRITTEN (type))
19611 return;
19613 /* If that failed, attach ourselves to the stub. */
19614 push_decl_scope (TYPE_CONTEXT (type));
19615 context_die = lookup_type_die (TYPE_CONTEXT (type));
19616 need_pop = 1;
19618 else if (TYPE_CONTEXT (type) != NULL_TREE
19619 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19621 /* If this type is local to a function that hasn't been written
19622 out yet, use a NULL context for now; it will be fixed up in
19623 decls_for_scope. */
19624 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19625 /* A declaration DIE doesn't count; nested types need to go in the
19626 specification. */
19627 if (context_die && is_declaration_die (context_die))
19628 context_die = NULL;
19629 need_pop = 0;
19631 else
19633 context_die = declare_in_namespace (type, context_die);
19634 need_pop = 0;
19637 if (TREE_CODE (type) == ENUMERAL_TYPE)
19639 /* This might have been written out by the call to
19640 declare_in_namespace. */
19641 if (!TREE_ASM_WRITTEN (type))
19642 gen_enumeration_type_die (type, context_die);
19644 else
19645 gen_struct_or_union_type_die (type, context_die, usage);
19647 if (need_pop)
19648 pop_decl_scope ();
19650 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19651 it up if it is ever completed. gen_*_type_die will set it for us
19652 when appropriate. */
19655 /* Generate a type description DIE. */
19657 static void
19658 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19659 enum debug_info_usage usage)
19661 struct array_descr_info info;
19663 if (type == NULL_TREE || type == error_mark_node)
19664 return;
19666 if (TYPE_NAME (type) != NULL_TREE
19667 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19668 && is_redundant_typedef (TYPE_NAME (type))
19669 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19670 /* The DECL of this type is a typedef we don't want to emit debug
19671 info for but we want debug info for its underlying typedef.
19672 This can happen for e.g, the injected-class-name of a C++
19673 type. */
19674 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19676 /* If TYPE is a typedef type variant, let's generate debug info
19677 for the parent typedef which TYPE is a type of. */
19678 if (typedef_variant_p (type))
19680 if (TREE_ASM_WRITTEN (type))
19681 return;
19683 /* Prevent broken recursion; we can't hand off to the same type. */
19684 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19686 /* Give typedefs the right scope. */
19687 context_die = scope_die_for (type, context_die);
19689 TREE_ASM_WRITTEN (type) = 1;
19691 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19692 return;
19695 /* If type is an anonymous tagged type named by a typedef, let's
19696 generate debug info for the typedef. */
19697 if (is_naming_typedef_decl (TYPE_NAME (type)))
19699 /* Use the DIE of the containing namespace as the parent DIE of
19700 the type description DIE we want to generate. */
19701 if (DECL_CONTEXT (TYPE_NAME (type))
19702 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19703 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19705 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19706 return;
19709 /* If this is an array type with hidden descriptor, handle it first. */
19710 if (!TREE_ASM_WRITTEN (type)
19711 && lang_hooks.types.get_array_descr_info
19712 && lang_hooks.types.get_array_descr_info (type, &info)
19713 && (dwarf_version >= 3 || !dwarf_strict))
19715 gen_descr_array_type_die (type, &info, context_die);
19716 TREE_ASM_WRITTEN (type) = 1;
19717 return;
19720 /* We are going to output a DIE to represent the unqualified version
19721 of this type (i.e. without any const or volatile qualifiers) so
19722 get the main variant (i.e. the unqualified version) of this type
19723 now. (Vectors are special because the debugging info is in the
19724 cloned type itself). */
19725 if (TREE_CODE (type) != VECTOR_TYPE)
19726 type = type_main_variant (type);
19728 if (TREE_ASM_WRITTEN (type))
19729 return;
19731 switch (TREE_CODE (type))
19733 case ERROR_MARK:
19734 break;
19736 case POINTER_TYPE:
19737 case REFERENCE_TYPE:
19738 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19739 ensures that the gen_type_die recursion will terminate even if the
19740 type is recursive. Recursive types are possible in Ada. */
19741 /* ??? We could perhaps do this for all types before the switch
19742 statement. */
19743 TREE_ASM_WRITTEN (type) = 1;
19745 /* For these types, all that is required is that we output a DIE (or a
19746 set of DIEs) to represent the "basis" type. */
19747 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19748 DINFO_USAGE_IND_USE);
19749 break;
19751 case OFFSET_TYPE:
19752 /* This code is used for C++ pointer-to-data-member types.
19753 Output a description of the relevant class type. */
19754 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19755 DINFO_USAGE_IND_USE);
19757 /* Output a description of the type of the object pointed to. */
19758 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19759 DINFO_USAGE_IND_USE);
19761 /* Now output a DIE to represent this pointer-to-data-member type
19762 itself. */
19763 gen_ptr_to_mbr_type_die (type, context_die);
19764 break;
19766 case FUNCTION_TYPE:
19767 /* Force out return type (in case it wasn't forced out already). */
19768 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19769 DINFO_USAGE_DIR_USE);
19770 gen_subroutine_type_die (type, context_die);
19771 break;
19773 case METHOD_TYPE:
19774 /* Force out return type (in case it wasn't forced out already). */
19775 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19776 DINFO_USAGE_DIR_USE);
19777 gen_subroutine_type_die (type, context_die);
19778 break;
19780 case ARRAY_TYPE:
19781 gen_array_type_die (type, context_die);
19782 break;
19784 case VECTOR_TYPE:
19785 gen_array_type_die (type, context_die);
19786 break;
19788 case ENUMERAL_TYPE:
19789 case RECORD_TYPE:
19790 case UNION_TYPE:
19791 case QUAL_UNION_TYPE:
19792 gen_tagged_type_die (type, context_die, usage);
19793 return;
19795 case VOID_TYPE:
19796 case INTEGER_TYPE:
19797 case REAL_TYPE:
19798 case FIXED_POINT_TYPE:
19799 case COMPLEX_TYPE:
19800 case BOOLEAN_TYPE:
19801 /* No DIEs needed for fundamental types. */
19802 break;
19804 case NULLPTR_TYPE:
19805 case LANG_TYPE:
19806 /* Just use DW_TAG_unspecified_type. */
19808 dw_die_ref type_die = lookup_type_die (type);
19809 if (type_die == NULL)
19811 tree name = TYPE_NAME (type);
19812 if (TREE_CODE (name) == TYPE_DECL)
19813 name = DECL_NAME (name);
19814 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19815 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19816 equate_type_number_to_die (type, type_die);
19819 break;
19821 default:
19822 gcc_unreachable ();
19825 TREE_ASM_WRITTEN (type) = 1;
19828 static void
19829 gen_type_die (tree type, dw_die_ref context_die)
19831 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19834 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19835 things which are local to the given block. */
19837 static void
19838 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19840 int must_output_die = 0;
19841 bool inlined_func;
19843 /* Ignore blocks that are NULL. */
19844 if (stmt == NULL_TREE)
19845 return;
19847 inlined_func = inlined_function_outer_scope_p (stmt);
19849 /* If the block is one fragment of a non-contiguous block, do not
19850 process the variables, since they will have been done by the
19851 origin block. Do process subblocks. */
19852 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19854 tree sub;
19856 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19857 gen_block_die (sub, context_die, depth + 1);
19859 return;
19862 /* Determine if we need to output any Dwarf DIEs at all to represent this
19863 block. */
19864 if (inlined_func)
19865 /* The outer scopes for inlinings *must* always be represented. We
19866 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19867 must_output_die = 1;
19868 else
19870 /* Determine if this block directly contains any "significant"
19871 local declarations which we will need to output DIEs for. */
19872 if (debug_info_level > DINFO_LEVEL_TERSE)
19873 /* We are not in terse mode so *any* local declaration counts
19874 as being a "significant" one. */
19875 must_output_die = ((BLOCK_VARS (stmt) != NULL
19876 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19877 && (TREE_USED (stmt)
19878 || TREE_ASM_WRITTEN (stmt)
19879 || BLOCK_ABSTRACT (stmt)));
19880 else if ((TREE_USED (stmt)
19881 || TREE_ASM_WRITTEN (stmt)
19882 || BLOCK_ABSTRACT (stmt))
19883 && !dwarf2out_ignore_block (stmt))
19884 must_output_die = 1;
19887 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19888 DIE for any block which contains no significant local declarations at
19889 all. Rather, in such cases we just call `decls_for_scope' so that any
19890 needed Dwarf info for any sub-blocks will get properly generated. Note
19891 that in terse mode, our definition of what constitutes a "significant"
19892 local declaration gets restricted to include only inlined function
19893 instances and local (nested) function definitions. */
19894 if (must_output_die)
19896 if (inlined_func)
19898 /* If STMT block is abstract, that means we have been called
19899 indirectly from dwarf2out_abstract_function.
19900 That function rightfully marks the descendent blocks (of
19901 the abstract function it is dealing with) as being abstract,
19902 precisely to prevent us from emitting any
19903 DW_TAG_inlined_subroutine DIE as a descendent
19904 of an abstract function instance. So in that case, we should
19905 not call gen_inlined_subroutine_die.
19907 Later though, when cgraph asks dwarf2out to emit info
19908 for the concrete instance of the function decl into which
19909 the concrete instance of STMT got inlined, the later will lead
19910 to the generation of a DW_TAG_inlined_subroutine DIE. */
19911 if (! BLOCK_ABSTRACT (stmt))
19912 gen_inlined_subroutine_die (stmt, context_die, depth);
19914 else
19915 gen_lexical_block_die (stmt, context_die, depth);
19917 else
19918 decls_for_scope (stmt, context_die, depth);
19921 /* Process variable DECL (or variable with origin ORIGIN) within
19922 block STMT and add it to CONTEXT_DIE. */
19923 static void
19924 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19926 dw_die_ref die;
19927 tree decl_or_origin = decl ? decl : origin;
19929 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19930 die = lookup_decl_die (decl_or_origin);
19931 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19932 && TYPE_DECL_IS_STUB (decl_or_origin))
19933 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19934 else
19935 die = NULL;
19937 if (die != NULL && die->die_parent == NULL)
19938 add_child_die (context_die, die);
19939 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19940 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19941 stmt, context_die);
19942 else
19943 gen_decl_die (decl, origin, context_die);
19946 /* Generate all of the decls declared within a given scope and (recursively)
19947 all of its sub-blocks. */
19949 static void
19950 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19952 tree decl;
19953 unsigned int i;
19954 tree subblocks;
19956 /* Ignore NULL blocks. */
19957 if (stmt == NULL_TREE)
19958 return;
19960 /* Output the DIEs to represent all of the data objects and typedefs
19961 declared directly within this block but not within any nested
19962 sub-blocks. Also, nested function and tag DIEs have been
19963 generated with a parent of NULL; fix that up now. We don't
19964 have to do this if we're at -g1. */
19965 if (debug_info_level > DINFO_LEVEL_TERSE)
19967 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19968 process_scope_var (stmt, decl, NULL_TREE, context_die);
19969 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19970 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19971 context_die);
19974 /* Even if we're at -g1, we need to process the subblocks in order to get
19975 inlined call information. */
19977 /* Output the DIEs to represent all sub-blocks (and the items declared
19978 therein) of this block. */
19979 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19980 subblocks != NULL;
19981 subblocks = BLOCK_CHAIN (subblocks))
19982 gen_block_die (subblocks, context_die, depth + 1);
19985 /* Is this a typedef we can avoid emitting? */
19987 static inline int
19988 is_redundant_typedef (const_tree decl)
19990 if (TYPE_DECL_IS_STUB (decl))
19991 return 1;
19993 if (DECL_ARTIFICIAL (decl)
19994 && DECL_CONTEXT (decl)
19995 && is_tagged_type (DECL_CONTEXT (decl))
19996 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19997 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19998 /* Also ignore the artificial member typedef for the class name. */
19999 return 1;
20001 return 0;
20004 /* Return TRUE if TYPE is a typedef that names a type for linkage
20005 purposes. This kind of typedefs is produced by the C++ FE for
20006 constructs like:
20008 typedef struct {...} foo;
20010 In that case, there is no typedef variant type produced for foo.
20011 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20012 struct type. */
20014 static bool
20015 is_naming_typedef_decl (const_tree decl)
20017 if (decl == NULL_TREE
20018 || TREE_CODE (decl) != TYPE_DECL
20019 || !is_tagged_type (TREE_TYPE (decl))
20020 || DECL_IS_BUILTIN (decl)
20021 || is_redundant_typedef (decl)
20022 /* It looks like Ada produces TYPE_DECLs that are very similar
20023 to C++ naming typedefs but that have different
20024 semantics. Let's be specific to c++ for now. */
20025 || !is_cxx ())
20026 return FALSE;
20028 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20029 && TYPE_NAME (TREE_TYPE (decl)) == decl
20030 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20031 != TYPE_NAME (TREE_TYPE (decl))));
20034 /* Returns the DIE for a context. */
20036 static inline dw_die_ref
20037 get_context_die (tree context)
20039 if (context)
20041 /* Find die that represents this context. */
20042 if (TYPE_P (context))
20044 context = TYPE_MAIN_VARIANT (context);
20045 return strip_naming_typedef (context, force_type_die (context));
20047 else
20048 return force_decl_die (context);
20050 return comp_unit_die ();
20053 /* Returns the DIE for decl. A DIE will always be returned. */
20055 static dw_die_ref
20056 force_decl_die (tree decl)
20058 dw_die_ref decl_die;
20059 unsigned saved_external_flag;
20060 tree save_fn = NULL_TREE;
20061 decl_die = lookup_decl_die (decl);
20062 if (!decl_die)
20064 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20066 decl_die = lookup_decl_die (decl);
20067 if (decl_die)
20068 return decl_die;
20070 switch (TREE_CODE (decl))
20072 case FUNCTION_DECL:
20073 /* Clear current_function_decl, so that gen_subprogram_die thinks
20074 that this is a declaration. At this point, we just want to force
20075 declaration die. */
20076 save_fn = current_function_decl;
20077 current_function_decl = NULL_TREE;
20078 gen_subprogram_die (decl, context_die);
20079 current_function_decl = save_fn;
20080 break;
20082 case VAR_DECL:
20083 /* Set external flag to force declaration die. Restore it after
20084 gen_decl_die() call. */
20085 saved_external_flag = DECL_EXTERNAL (decl);
20086 DECL_EXTERNAL (decl) = 1;
20087 gen_decl_die (decl, NULL, context_die);
20088 DECL_EXTERNAL (decl) = saved_external_flag;
20089 break;
20091 case NAMESPACE_DECL:
20092 if (dwarf_version >= 3 || !dwarf_strict)
20093 dwarf2out_decl (decl);
20094 else
20095 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20096 decl_die = comp_unit_die ();
20097 break;
20099 case TRANSLATION_UNIT_DECL:
20100 decl_die = comp_unit_die ();
20101 break;
20103 default:
20104 gcc_unreachable ();
20107 /* We should be able to find the DIE now. */
20108 if (!decl_die)
20109 decl_die = lookup_decl_die (decl);
20110 gcc_assert (decl_die);
20113 return decl_die;
20116 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20117 always returned. */
20119 static dw_die_ref
20120 force_type_die (tree type)
20122 dw_die_ref type_die;
20124 type_die = lookup_type_die (type);
20125 if (!type_die)
20127 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20129 type_die = modified_type_die (type, TYPE_READONLY (type),
20130 TYPE_VOLATILE (type), context_die);
20131 gcc_assert (type_die);
20133 return type_die;
20136 /* Force out any required namespaces to be able to output DECL,
20137 and return the new context_die for it, if it's changed. */
20139 static dw_die_ref
20140 setup_namespace_context (tree thing, dw_die_ref context_die)
20142 tree context = (DECL_P (thing)
20143 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20144 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20145 /* Force out the namespace. */
20146 context_die = force_decl_die (context);
20148 return context_die;
20151 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20152 type) within its namespace, if appropriate.
20154 For compatibility with older debuggers, namespace DIEs only contain
20155 declarations; all definitions are emitted at CU scope. */
20157 static dw_die_ref
20158 declare_in_namespace (tree thing, dw_die_ref context_die)
20160 dw_die_ref ns_context;
20162 if (debug_info_level <= DINFO_LEVEL_TERSE)
20163 return context_die;
20165 /* If this decl is from an inlined function, then don't try to emit it in its
20166 namespace, as we will get confused. It would have already been emitted
20167 when the abstract instance of the inline function was emitted anyways. */
20168 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20169 return context_die;
20171 ns_context = setup_namespace_context (thing, context_die);
20173 if (ns_context != context_die)
20175 if (is_fortran ())
20176 return ns_context;
20177 if (DECL_P (thing))
20178 gen_decl_die (thing, NULL, ns_context);
20179 else
20180 gen_type_die (thing, ns_context);
20182 return context_die;
20185 /* Generate a DIE for a namespace or namespace alias. */
20187 static void
20188 gen_namespace_die (tree decl, dw_die_ref context_die)
20190 dw_die_ref namespace_die;
20192 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20193 they are an alias of. */
20194 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20196 /* Output a real namespace or module. */
20197 context_die = setup_namespace_context (decl, comp_unit_die ());
20198 namespace_die = new_die (is_fortran ()
20199 ? DW_TAG_module : DW_TAG_namespace,
20200 context_die, decl);
20201 /* For Fortran modules defined in different CU don't add src coords. */
20202 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20204 const char *name = dwarf2_name (decl, 0);
20205 if (name)
20206 add_name_attribute (namespace_die, name);
20208 else
20209 add_name_and_src_coords_attributes (namespace_die, decl);
20210 if (DECL_EXTERNAL (decl))
20211 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20212 equate_decl_number_to_die (decl, namespace_die);
20214 else
20216 /* Output a namespace alias. */
20218 /* Force out the namespace we are an alias of, if necessary. */
20219 dw_die_ref origin_die
20220 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20222 if (DECL_FILE_SCOPE_P (decl)
20223 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20224 context_die = setup_namespace_context (decl, comp_unit_die ());
20225 /* Now create the namespace alias DIE. */
20226 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20227 add_name_and_src_coords_attributes (namespace_die, decl);
20228 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20229 equate_decl_number_to_die (decl, namespace_die);
20231 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20232 if (want_pubnames ())
20233 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20236 /* Generate Dwarf debug information for a decl described by DECL.
20237 The return value is currently only meaningful for PARM_DECLs,
20238 for all other decls it returns NULL. */
20240 static dw_die_ref
20241 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20243 tree decl_or_origin = decl ? decl : origin;
20244 tree class_origin = NULL, ultimate_origin;
20246 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20247 return NULL;
20249 switch (TREE_CODE (decl_or_origin))
20251 case ERROR_MARK:
20252 break;
20254 case CONST_DECL:
20255 if (!is_fortran () && !is_ada ())
20257 /* The individual enumerators of an enum type get output when we output
20258 the Dwarf representation of the relevant enum type itself. */
20259 break;
20262 /* Emit its type. */
20263 gen_type_die (TREE_TYPE (decl), context_die);
20265 /* And its containing namespace. */
20266 context_die = declare_in_namespace (decl, context_die);
20268 gen_const_die (decl, context_die);
20269 break;
20271 case FUNCTION_DECL:
20272 /* Don't output any DIEs to represent mere function declarations,
20273 unless they are class members or explicit block externs. */
20274 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20275 && DECL_FILE_SCOPE_P (decl_or_origin)
20276 && (current_function_decl == NULL_TREE
20277 || DECL_ARTIFICIAL (decl_or_origin)))
20278 break;
20280 #if 0
20281 /* FIXME */
20282 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20283 on local redeclarations of global functions. That seems broken. */
20284 if (current_function_decl != decl)
20285 /* This is only a declaration. */;
20286 #endif
20288 /* If we're emitting a clone, emit info for the abstract instance. */
20289 if (origin || DECL_ORIGIN (decl) != decl)
20290 dwarf2out_abstract_function (origin
20291 ? DECL_ORIGIN (origin)
20292 : DECL_ABSTRACT_ORIGIN (decl));
20294 /* If we're emitting an out-of-line copy of an inline function,
20295 emit info for the abstract instance and set up to refer to it. */
20296 else if (cgraph_function_possibly_inlined_p (decl)
20297 && ! DECL_ABSTRACT (decl)
20298 && ! class_or_namespace_scope_p (context_die)
20299 /* dwarf2out_abstract_function won't emit a die if this is just
20300 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20301 that case, because that works only if we have a die. */
20302 && DECL_INITIAL (decl) != NULL_TREE)
20304 dwarf2out_abstract_function (decl);
20305 set_decl_origin_self (decl);
20308 /* Otherwise we're emitting the primary DIE for this decl. */
20309 else if (debug_info_level > DINFO_LEVEL_TERSE)
20311 /* Before we describe the FUNCTION_DECL itself, make sure that we
20312 have its containing type. */
20313 if (!origin)
20314 origin = decl_class_context (decl);
20315 if (origin != NULL_TREE)
20316 gen_type_die (origin, context_die);
20318 /* And its return type. */
20319 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20321 /* And its virtual context. */
20322 if (DECL_VINDEX (decl) != NULL_TREE)
20323 gen_type_die (DECL_CONTEXT (decl), context_die);
20325 /* Make sure we have a member DIE for decl. */
20326 if (origin != NULL_TREE)
20327 gen_type_die_for_member (origin, decl, context_die);
20329 /* And its containing namespace. */
20330 context_die = declare_in_namespace (decl, context_die);
20333 /* Now output a DIE to represent the function itself. */
20334 if (decl)
20335 gen_subprogram_die (decl, context_die);
20336 break;
20338 case TYPE_DECL:
20339 /* If we are in terse mode, don't generate any DIEs to represent any
20340 actual typedefs. */
20341 if (debug_info_level <= DINFO_LEVEL_TERSE)
20342 break;
20344 /* In the special case of a TYPE_DECL node representing the declaration
20345 of some type tag, if the given TYPE_DECL is marked as having been
20346 instantiated from some other (original) TYPE_DECL node (e.g. one which
20347 was generated within the original definition of an inline function) we
20348 used to generate a special (abbreviated) DW_TAG_structure_type,
20349 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20350 should be actually referencing those DIEs, as variable DIEs with that
20351 type would be emitted already in the abstract origin, so it was always
20352 removed during unused type prunning. Don't add anything in this
20353 case. */
20354 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20355 break;
20357 if (is_redundant_typedef (decl))
20358 gen_type_die (TREE_TYPE (decl), context_die);
20359 else
20360 /* Output a DIE to represent the typedef itself. */
20361 gen_typedef_die (decl, context_die);
20362 break;
20364 case LABEL_DECL:
20365 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20366 gen_label_die (decl, context_die);
20367 break;
20369 case VAR_DECL:
20370 case RESULT_DECL:
20371 /* If we are in terse mode, don't generate any DIEs to represent any
20372 variable declarations or definitions. */
20373 if (debug_info_level <= DINFO_LEVEL_TERSE)
20374 break;
20376 /* Output any DIEs that are needed to specify the type of this data
20377 object. */
20378 if (decl_by_reference_p (decl_or_origin))
20379 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20380 else
20381 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20383 /* And its containing type. */
20384 class_origin = decl_class_context (decl_or_origin);
20385 if (class_origin != NULL_TREE)
20386 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20388 /* And its containing namespace. */
20389 context_die = declare_in_namespace (decl_or_origin, context_die);
20391 /* Now output the DIE to represent the data object itself. This gets
20392 complicated because of the possibility that the VAR_DECL really
20393 represents an inlined instance of a formal parameter for an inline
20394 function. */
20395 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20396 if (ultimate_origin != NULL_TREE
20397 && TREE_CODE (ultimate_origin) == PARM_DECL)
20398 gen_formal_parameter_die (decl, origin,
20399 true /* Emit name attribute. */,
20400 context_die);
20401 else
20402 gen_variable_die (decl, origin, context_die);
20403 break;
20405 case FIELD_DECL:
20406 /* Ignore the nameless fields that are used to skip bits but handle C++
20407 anonymous unions and structs. */
20408 if (DECL_NAME (decl) != NULL_TREE
20409 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20410 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20412 gen_type_die (member_declared_type (decl), context_die);
20413 gen_field_die (decl, context_die);
20415 break;
20417 case PARM_DECL:
20418 if (DECL_BY_REFERENCE (decl_or_origin))
20419 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20420 else
20421 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20422 return gen_formal_parameter_die (decl, origin,
20423 true /* Emit name attribute. */,
20424 context_die);
20426 case NAMESPACE_DECL:
20427 case IMPORTED_DECL:
20428 if (dwarf_version >= 3 || !dwarf_strict)
20429 gen_namespace_die (decl, context_die);
20430 break;
20432 default:
20433 /* Probably some frontend-internal decl. Assume we don't care. */
20434 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20435 break;
20438 return NULL;
20441 /* Output debug information for global decl DECL. Called from toplev.c after
20442 compilation proper has finished. */
20444 static void
20445 dwarf2out_global_decl (tree decl)
20447 /* Output DWARF2 information for file-scope tentative data object
20448 declarations, file-scope (extern) function declarations (which
20449 had no corresponding body) and file-scope tagged type declarations
20450 and definitions which have not yet been forced out. */
20451 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20452 dwarf2out_decl (decl);
20455 /* Output debug information for type decl DECL. Called from toplev.c
20456 and from language front ends (to record built-in types). */
20457 static void
20458 dwarf2out_type_decl (tree decl, int local)
20460 if (!local)
20461 dwarf2out_decl (decl);
20464 /* Output debug information for imported module or decl DECL.
20465 NAME is non-NULL name in the lexical block if the decl has been renamed.
20466 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20467 that DECL belongs to.
20468 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20469 static void
20470 dwarf2out_imported_module_or_decl_1 (tree decl,
20471 tree name,
20472 tree lexical_block,
20473 dw_die_ref lexical_block_die)
20475 expanded_location xloc;
20476 dw_die_ref imported_die = NULL;
20477 dw_die_ref at_import_die;
20479 if (TREE_CODE (decl) == IMPORTED_DECL)
20481 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20482 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20483 gcc_assert (decl);
20485 else
20486 xloc = expand_location (input_location);
20488 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20490 at_import_die = force_type_die (TREE_TYPE (decl));
20491 /* For namespace N { typedef void T; } using N::T; base_type_die
20492 returns NULL, but DW_TAG_imported_declaration requires
20493 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20494 if (!at_import_die)
20496 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20497 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20498 at_import_die = lookup_type_die (TREE_TYPE (decl));
20499 gcc_assert (at_import_die);
20502 else
20504 at_import_die = lookup_decl_die (decl);
20505 if (!at_import_die)
20507 /* If we're trying to avoid duplicate debug info, we may not have
20508 emitted the member decl for this field. Emit it now. */
20509 if (TREE_CODE (decl) == FIELD_DECL)
20511 tree type = DECL_CONTEXT (decl);
20513 if (TYPE_CONTEXT (type)
20514 && TYPE_P (TYPE_CONTEXT (type))
20515 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20516 DINFO_USAGE_DIR_USE))
20517 return;
20518 gen_type_die_for_member (type, decl,
20519 get_context_die (TYPE_CONTEXT (type)));
20521 at_import_die = force_decl_die (decl);
20525 if (TREE_CODE (decl) == NAMESPACE_DECL)
20527 if (dwarf_version >= 3 || !dwarf_strict)
20528 imported_die = new_die (DW_TAG_imported_module,
20529 lexical_block_die,
20530 lexical_block);
20531 else
20532 return;
20534 else
20535 imported_die = new_die (DW_TAG_imported_declaration,
20536 lexical_block_die,
20537 lexical_block);
20539 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20540 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20541 if (name)
20542 add_AT_string (imported_die, DW_AT_name,
20543 IDENTIFIER_POINTER (name));
20544 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20547 /* Output debug information for imported module or decl DECL.
20548 NAME is non-NULL name in context if the decl has been renamed.
20549 CHILD is true if decl is one of the renamed decls as part of
20550 importing whole module. */
20552 static void
20553 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20554 bool child)
20556 /* dw_die_ref at_import_die; */
20557 dw_die_ref scope_die;
20559 if (debug_info_level <= DINFO_LEVEL_TERSE)
20560 return;
20562 gcc_assert (decl);
20564 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20565 We need decl DIE for reference and scope die. First, get DIE for the decl
20566 itself. */
20568 /* Get the scope die for decl context. Use comp_unit_die for global module
20569 or decl. If die is not found for non globals, force new die. */
20570 if (context
20571 && TYPE_P (context)
20572 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20573 return;
20575 if (!(dwarf_version >= 3 || !dwarf_strict))
20576 return;
20578 scope_die = get_context_die (context);
20580 if (child)
20582 gcc_assert (scope_die->die_child);
20583 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20584 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20585 scope_die = scope_die->die_child;
20588 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20589 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20593 /* Write the debugging output for DECL. */
20595 void
20596 dwarf2out_decl (tree decl)
20598 dw_die_ref context_die = comp_unit_die ();
20600 switch (TREE_CODE (decl))
20602 case ERROR_MARK:
20603 return;
20605 case FUNCTION_DECL:
20606 /* What we would really like to do here is to filter out all mere
20607 file-scope declarations of file-scope functions which are never
20608 referenced later within this translation unit (and keep all of ones
20609 that *are* referenced later on) but we aren't clairvoyant, so we have
20610 no idea which functions will be referenced in the future (i.e. later
20611 on within the current translation unit). So here we just ignore all
20612 file-scope function declarations which are not also definitions. If
20613 and when the debugger needs to know something about these functions,
20614 it will have to hunt around and find the DWARF information associated
20615 with the definition of the function.
20617 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20618 nodes represent definitions and which ones represent mere
20619 declarations. We have to check DECL_INITIAL instead. That's because
20620 the C front-end supports some weird semantics for "extern inline"
20621 function definitions. These can get inlined within the current
20622 translation unit (and thus, we need to generate Dwarf info for their
20623 abstract instances so that the Dwarf info for the concrete inlined
20624 instances can have something to refer to) but the compiler never
20625 generates any out-of-lines instances of such things (despite the fact
20626 that they *are* definitions).
20628 The important point is that the C front-end marks these "extern
20629 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20630 them anyway. Note that the C++ front-end also plays some similar games
20631 for inline function definitions appearing within include files which
20632 also contain `#pragma interface' pragmas.
20634 If we are called from dwarf2out_abstract_function output a DIE
20635 anyway. We can end up here this way with early inlining and LTO
20636 where the inlined function is output in a different LTRANS unit
20637 or not at all. */
20638 if (DECL_INITIAL (decl) == NULL_TREE
20639 && ! DECL_ABSTRACT (decl))
20640 return;
20642 /* If we're a nested function, initially use a parent of NULL; if we're
20643 a plain function, this will be fixed up in decls_for_scope. If
20644 we're a method, it will be ignored, since we already have a DIE. */
20645 if (decl_function_context (decl)
20646 /* But if we're in terse mode, we don't care about scope. */
20647 && debug_info_level > DINFO_LEVEL_TERSE)
20648 context_die = NULL;
20649 break;
20651 case VAR_DECL:
20652 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20653 declaration and if the declaration was never even referenced from
20654 within this entire compilation unit. We suppress these DIEs in
20655 order to save space in the .debug section (by eliminating entries
20656 which are probably useless). Note that we must not suppress
20657 block-local extern declarations (whether used or not) because that
20658 would screw-up the debugger's name lookup mechanism and cause it to
20659 miss things which really ought to be in scope at a given point. */
20660 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20661 return;
20663 /* For local statics lookup proper context die. */
20664 if (TREE_STATIC (decl)
20665 && DECL_CONTEXT (decl)
20666 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20667 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20669 /* If we are in terse mode, don't generate any DIEs to represent any
20670 variable declarations or definitions. */
20671 if (debug_info_level <= DINFO_LEVEL_TERSE)
20672 return;
20673 break;
20675 case CONST_DECL:
20676 if (debug_info_level <= DINFO_LEVEL_TERSE)
20677 return;
20678 if (!is_fortran () && !is_ada ())
20679 return;
20680 if (TREE_STATIC (decl) && decl_function_context (decl))
20681 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20682 break;
20684 case NAMESPACE_DECL:
20685 case IMPORTED_DECL:
20686 if (debug_info_level <= DINFO_LEVEL_TERSE)
20687 return;
20688 if (lookup_decl_die (decl) != NULL)
20689 return;
20690 break;
20692 case TYPE_DECL:
20693 /* Don't emit stubs for types unless they are needed by other DIEs. */
20694 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20695 return;
20697 /* Don't bother trying to generate any DIEs to represent any of the
20698 normal built-in types for the language we are compiling. */
20699 if (DECL_IS_BUILTIN (decl))
20700 return;
20702 /* If we are in terse mode, don't generate any DIEs for types. */
20703 if (debug_info_level <= DINFO_LEVEL_TERSE)
20704 return;
20706 /* If we're a function-scope tag, initially use a parent of NULL;
20707 this will be fixed up in decls_for_scope. */
20708 if (decl_function_context (decl))
20709 context_die = NULL;
20711 break;
20713 default:
20714 return;
20717 gen_decl_die (decl, NULL, context_die);
20720 /* Write the debugging output for DECL. */
20722 static void
20723 dwarf2out_function_decl (tree decl)
20725 dwarf2out_decl (decl);
20726 call_arg_locations = NULL;
20727 call_arg_loc_last = NULL;
20728 call_site_count = -1;
20729 tail_call_site_count = -1;
20730 block_map.release ();
20731 htab_empty (decl_loc_table);
20732 htab_empty (cached_dw_loc_list_table);
20735 /* Output a marker (i.e. a label) for the beginning of the generated code for
20736 a lexical block. */
20738 static void
20739 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20740 unsigned int blocknum)
20742 switch_to_section (current_function_section ());
20743 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20746 /* Output a marker (i.e. a label) for the end of the generated code for a
20747 lexical block. */
20749 static void
20750 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20752 switch_to_section (current_function_section ());
20753 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20756 /* Returns nonzero if it is appropriate not to emit any debugging
20757 information for BLOCK, because it doesn't contain any instructions.
20759 Don't allow this for blocks with nested functions or local classes
20760 as we would end up with orphans, and in the presence of scheduling
20761 we may end up calling them anyway. */
20763 static bool
20764 dwarf2out_ignore_block (const_tree block)
20766 tree decl;
20767 unsigned int i;
20769 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20770 if (TREE_CODE (decl) == FUNCTION_DECL
20771 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20772 return 0;
20773 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20775 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20776 if (TREE_CODE (decl) == FUNCTION_DECL
20777 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20778 return 0;
20781 return 1;
20784 /* Hash table routines for file_hash. */
20786 static int
20787 file_table_eq (const void *p1_p, const void *p2_p)
20789 const struct dwarf_file_data *const p1 =
20790 (const struct dwarf_file_data *) p1_p;
20791 const char *const p2 = (const char *) p2_p;
20792 return filename_cmp (p1->filename, p2) == 0;
20795 static hashval_t
20796 file_table_hash (const void *p_p)
20798 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20799 return htab_hash_string (p->filename);
20802 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20803 dwarf2out.c) and return its "index". The index of each (known) filename is
20804 just a unique number which is associated with only that one filename. We
20805 need such numbers for the sake of generating labels (in the .debug_sfnames
20806 section) and references to those files numbers (in the .debug_srcinfo
20807 and.debug_macinfo sections). If the filename given as an argument is not
20808 found in our current list, add it to the list and assign it the next
20809 available unique index number. In order to speed up searches, we remember
20810 the index of the filename was looked up last. This handles the majority of
20811 all searches. */
20813 static struct dwarf_file_data *
20814 lookup_filename (const char *file_name)
20816 void ** slot;
20817 struct dwarf_file_data * created;
20819 /* Check to see if the file name that was searched on the previous
20820 call matches this file name. If so, return the index. */
20821 if (file_table_last_lookup
20822 && (file_name == file_table_last_lookup->filename
20823 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20824 return file_table_last_lookup;
20826 /* Didn't match the previous lookup, search the table. */
20827 slot = htab_find_slot_with_hash (file_table, file_name,
20828 htab_hash_string (file_name), INSERT);
20829 if (*slot)
20830 return (struct dwarf_file_data *) *slot;
20832 created = ggc_alloc_dwarf_file_data ();
20833 created->filename = file_name;
20834 created->emitted_number = 0;
20835 *slot = created;
20836 return created;
20839 /* If the assembler will construct the file table, then translate the compiler
20840 internal file table number into the assembler file table number, and emit
20841 a .file directive if we haven't already emitted one yet. The file table
20842 numbers are different because we prune debug info for unused variables and
20843 types, which may include filenames. */
20845 static int
20846 maybe_emit_file (struct dwarf_file_data * fd)
20848 if (! fd->emitted_number)
20850 if (last_emitted_file)
20851 fd->emitted_number = last_emitted_file->emitted_number + 1;
20852 else
20853 fd->emitted_number = 1;
20854 last_emitted_file = fd;
20856 if (DWARF2_ASM_LINE_DEBUG_INFO)
20858 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20859 output_quoted_string (asm_out_file,
20860 remap_debug_filename (fd->filename));
20861 fputc ('\n', asm_out_file);
20865 return fd->emitted_number;
20868 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20869 That generation should happen after function debug info has been
20870 generated. The value of the attribute is the constant value of ARG. */
20872 static void
20873 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20875 die_arg_entry entry;
20877 if (!die || !arg)
20878 return;
20880 if (!tmpl_value_parm_die_table)
20881 vec_alloc (tmpl_value_parm_die_table, 32);
20883 entry.die = die;
20884 entry.arg = arg;
20885 vec_safe_push (tmpl_value_parm_die_table, entry);
20888 /* Return TRUE if T is an instance of generic type, FALSE
20889 otherwise. */
20891 static bool
20892 generic_type_p (tree t)
20894 if (t == NULL_TREE || !TYPE_P (t))
20895 return false;
20896 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20899 /* Schedule the generation of the generic parameter dies for the
20900 instance of generic type T. The proper generation itself is later
20901 done by gen_scheduled_generic_parms_dies. */
20903 static void
20904 schedule_generic_params_dies_gen (tree t)
20906 if (!generic_type_p (t))
20907 return;
20909 if (!generic_type_instances)
20910 vec_alloc (generic_type_instances, 256);
20912 vec_safe_push (generic_type_instances, t);
20915 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20916 by append_entry_to_tmpl_value_parm_die_table. This function must
20917 be called after function DIEs have been generated. */
20919 static void
20920 gen_remaining_tmpl_value_param_die_attribute (void)
20922 if (tmpl_value_parm_die_table)
20924 unsigned i;
20925 die_arg_entry *e;
20927 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20928 tree_add_const_value_attribute (e->die, e->arg);
20932 /* Generate generic parameters DIEs for instances of generic types
20933 that have been previously scheduled by
20934 schedule_generic_params_dies_gen. This function must be called
20935 after all the types of the CU have been laid out. */
20937 static void
20938 gen_scheduled_generic_parms_dies (void)
20940 unsigned i;
20941 tree t;
20943 if (!generic_type_instances)
20944 return;
20946 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20947 if (COMPLETE_TYPE_P (t))
20948 gen_generic_params_dies (t);
20952 /* Replace DW_AT_name for the decl with name. */
20954 static void
20955 dwarf2out_set_name (tree decl, tree name)
20957 dw_die_ref die;
20958 dw_attr_ref attr;
20959 const char *dname;
20961 die = TYPE_SYMTAB_DIE (decl);
20962 if (!die)
20963 return;
20965 dname = dwarf2_name (name, 0);
20966 if (!dname)
20967 return;
20969 attr = get_AT (die, DW_AT_name);
20970 if (attr)
20972 struct indirect_string_node *node;
20974 node = find_AT_string (dname);
20975 /* replace the string. */
20976 attr->dw_attr_val.v.val_str = node;
20979 else
20980 add_name_attribute (die, dname);
20983 /* True if before or during processing of the first function being emitted. */
20984 static bool in_first_function_p = true;
20985 /* True if loc_note during dwarf2out_var_location call might still be
20986 before first real instruction at address equal to .Ltext0. */
20987 static bool maybe_at_text_label_p = true;
20988 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20989 static unsigned int first_loclabel_num_not_at_text_label;
20991 /* Called by the final INSN scan whenever we see a var location. We
20992 use it to drop labels in the right places, and throw the location in
20993 our lookup table. */
20995 static void
20996 dwarf2out_var_location (rtx loc_note)
20998 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20999 struct var_loc_node *newloc;
21000 rtx next_real, next_note;
21001 static const char *last_label;
21002 static const char *last_postcall_label;
21003 static bool last_in_cold_section_p;
21004 static rtx expected_next_loc_note;
21005 tree decl;
21006 bool var_loc_p;
21008 if (!NOTE_P (loc_note))
21010 if (CALL_P (loc_note))
21012 call_site_count++;
21013 if (SIBLING_CALL_P (loc_note))
21014 tail_call_site_count++;
21016 return;
21019 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21020 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21021 return;
21023 /* Optimize processing a large consecutive sequence of location
21024 notes so we don't spend too much time in next_real_insn. If the
21025 next insn is another location note, remember the next_real_insn
21026 calculation for next time. */
21027 next_real = cached_next_real_insn;
21028 if (next_real)
21030 if (expected_next_loc_note != loc_note)
21031 next_real = NULL_RTX;
21034 next_note = NEXT_INSN (loc_note);
21035 if (! next_note
21036 || INSN_DELETED_P (next_note)
21037 || ! NOTE_P (next_note)
21038 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21039 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21040 next_note = NULL_RTX;
21042 if (! next_real)
21043 next_real = next_real_insn (loc_note);
21045 if (next_note)
21047 expected_next_loc_note = next_note;
21048 cached_next_real_insn = next_real;
21050 else
21051 cached_next_real_insn = NULL_RTX;
21053 /* If there are no instructions which would be affected by this note,
21054 don't do anything. */
21055 if (var_loc_p
21056 && next_real == NULL_RTX
21057 && !NOTE_DURING_CALL_P (loc_note))
21058 return;
21060 if (next_real == NULL_RTX)
21061 next_real = get_last_insn ();
21063 /* If there were any real insns between note we processed last time
21064 and this note (or if it is the first note), clear
21065 last_{,postcall_}label so that they are not reused this time. */
21066 if (last_var_location_insn == NULL_RTX
21067 || last_var_location_insn != next_real
21068 || last_in_cold_section_p != in_cold_section_p)
21070 last_label = NULL;
21071 last_postcall_label = NULL;
21074 if (var_loc_p)
21076 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21077 newloc = add_var_loc_to_decl (decl, loc_note,
21078 NOTE_DURING_CALL_P (loc_note)
21079 ? last_postcall_label : last_label);
21080 if (newloc == NULL)
21081 return;
21083 else
21085 decl = NULL_TREE;
21086 newloc = NULL;
21089 /* If there were no real insns between note we processed last time
21090 and this note, use the label we emitted last time. Otherwise
21091 create a new label and emit it. */
21092 if (last_label == NULL)
21094 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21095 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21096 loclabel_num++;
21097 last_label = ggc_strdup (loclabel);
21098 /* See if loclabel might be equal to .Ltext0. If yes,
21099 bump first_loclabel_num_not_at_text_label. */
21100 if (!have_multiple_function_sections
21101 && in_first_function_p
21102 && maybe_at_text_label_p)
21104 static rtx last_start;
21105 rtx insn;
21106 for (insn = loc_note; insn; insn = previous_insn (insn))
21107 if (insn == last_start)
21108 break;
21109 else if (!NONDEBUG_INSN_P (insn))
21110 continue;
21111 else
21113 rtx body = PATTERN (insn);
21114 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21115 continue;
21116 /* Inline asm could occupy zero bytes. */
21117 else if (GET_CODE (body) == ASM_INPUT
21118 || asm_noperands (body) >= 0)
21119 continue;
21120 #ifdef HAVE_attr_length
21121 else if (get_attr_min_length (insn) == 0)
21122 continue;
21123 #endif
21124 else
21126 /* Assume insn has non-zero length. */
21127 maybe_at_text_label_p = false;
21128 break;
21131 if (maybe_at_text_label_p)
21133 last_start = loc_note;
21134 first_loclabel_num_not_at_text_label = loclabel_num;
21139 if (!var_loc_p)
21141 struct call_arg_loc_node *ca_loc
21142 = ggc_alloc_cleared_call_arg_loc_node ();
21143 rtx prev = prev_real_insn (loc_note), x;
21144 ca_loc->call_arg_loc_note = loc_note;
21145 ca_loc->next = NULL;
21146 ca_loc->label = last_label;
21147 gcc_assert (prev
21148 && (CALL_P (prev)
21149 || (NONJUMP_INSN_P (prev)
21150 && GET_CODE (PATTERN (prev)) == SEQUENCE
21151 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21152 if (!CALL_P (prev))
21153 prev = XVECEXP (PATTERN (prev), 0, 0);
21154 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21155 x = get_call_rtx_from (PATTERN (prev));
21156 if (x)
21158 x = XEXP (XEXP (x, 0), 0);
21159 if (GET_CODE (x) == SYMBOL_REF
21160 && SYMBOL_REF_DECL (x)
21161 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21162 ca_loc->symbol_ref = x;
21164 ca_loc->block = insn_scope (prev);
21165 if (call_arg_locations)
21166 call_arg_loc_last->next = ca_loc;
21167 else
21168 call_arg_locations = ca_loc;
21169 call_arg_loc_last = ca_loc;
21171 else if (!NOTE_DURING_CALL_P (loc_note))
21172 newloc->label = last_label;
21173 else
21175 if (!last_postcall_label)
21177 sprintf (loclabel, "%s-1", last_label);
21178 last_postcall_label = ggc_strdup (loclabel);
21180 newloc->label = last_postcall_label;
21183 last_var_location_insn = next_real;
21184 last_in_cold_section_p = in_cold_section_p;
21187 /* Note in one location list that text section has changed. */
21189 static int
21190 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21192 var_loc_list *list = (var_loc_list *) *slot;
21193 if (list->first)
21194 list->last_before_switch
21195 = list->last->next ? list->last->next : list->last;
21196 return 1;
21199 /* Note in all location lists that text section has changed. */
21201 static void
21202 var_location_switch_text_section (void)
21204 if (decl_loc_table == NULL)
21205 return;
21207 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21210 /* Create a new line number table. */
21212 static dw_line_info_table *
21213 new_line_info_table (void)
21215 dw_line_info_table *table;
21217 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21218 table->file_num = 1;
21219 table->line_num = 1;
21220 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21222 return table;
21225 /* Lookup the "current" table into which we emit line info, so
21226 that we don't have to do it for every source line. */
21228 static void
21229 set_cur_line_info_table (section *sec)
21231 dw_line_info_table *table;
21233 if (sec == text_section)
21234 table = text_section_line_info;
21235 else if (sec == cold_text_section)
21237 table = cold_text_section_line_info;
21238 if (!table)
21240 cold_text_section_line_info = table = new_line_info_table ();
21241 table->end_label = cold_end_label;
21244 else
21246 const char *end_label;
21248 if (flag_reorder_blocks_and_partition)
21250 if (in_cold_section_p)
21251 end_label = crtl->subsections.cold_section_end_label;
21252 else
21253 end_label = crtl->subsections.hot_section_end_label;
21255 else
21257 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21258 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21259 current_function_funcdef_no);
21260 end_label = ggc_strdup (label);
21263 table = new_line_info_table ();
21264 table->end_label = end_label;
21266 vec_safe_push (separate_line_info, table);
21269 if (DWARF2_ASM_LINE_DEBUG_INFO)
21270 table->is_stmt = (cur_line_info_table
21271 ? cur_line_info_table->is_stmt
21272 : DWARF_LINE_DEFAULT_IS_STMT_START);
21273 cur_line_info_table = table;
21277 /* We need to reset the locations at the beginning of each
21278 function. We can't do this in the end_function hook, because the
21279 declarations that use the locations won't have been output when
21280 that hook is called. Also compute have_multiple_function_sections here. */
21282 static void
21283 dwarf2out_begin_function (tree fun)
21285 section *sec = function_section (fun);
21287 if (sec != text_section)
21288 have_multiple_function_sections = true;
21290 if (flag_reorder_blocks_and_partition && !cold_text_section)
21292 gcc_assert (current_function_decl == fun);
21293 cold_text_section = unlikely_text_section ();
21294 switch_to_section (cold_text_section);
21295 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21296 switch_to_section (sec);
21299 dwarf2out_note_section_used ();
21300 call_site_count = 0;
21301 tail_call_site_count = 0;
21303 set_cur_line_info_table (sec);
21306 /* Helper function of dwarf2out_end_function, called only after emitting
21307 the very first function into assembly. Check if some .debug_loc range
21308 might end with a .LVL* label that could be equal to .Ltext0.
21309 In that case we must force using absolute addresses in .debug_loc ranges,
21310 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21311 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21312 list terminator.
21313 Set have_multiple_function_sections to true in that case and
21314 terminate htab traversal. */
21316 static int
21317 find_empty_loc_ranges_at_text_label (void **slot, void *)
21319 var_loc_list *entry;
21320 struct var_loc_node *node;
21322 entry = (var_loc_list *) *slot;
21323 node = entry->first;
21324 if (node && node->next && node->next->label)
21326 unsigned int i;
21327 const char *label = node->next->label;
21328 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21330 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21332 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21333 if (strcmp (label, loclabel) == 0)
21335 have_multiple_function_sections = true;
21336 return 0;
21340 return 1;
21343 /* Hook called after emitting a function into assembly.
21344 This does something only for the very first function emitted. */
21346 static void
21347 dwarf2out_end_function (unsigned int)
21349 if (in_first_function_p
21350 && !have_multiple_function_sections
21351 && first_loclabel_num_not_at_text_label
21352 && decl_loc_table)
21353 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21354 NULL);
21355 in_first_function_p = false;
21356 maybe_at_text_label_p = false;
21359 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21361 static void
21362 push_dw_line_info_entry (dw_line_info_table *table,
21363 enum dw_line_info_opcode opcode, unsigned int val)
21365 dw_line_info_entry e;
21366 e.opcode = opcode;
21367 e.val = val;
21368 vec_safe_push (table->entries, e);
21371 /* Output a label to mark the beginning of a source code line entry
21372 and record information relating to this source line, in
21373 'line_info_table' for later output of the .debug_line section. */
21374 /* ??? The discriminator parameter ought to be unsigned. */
21376 static void
21377 dwarf2out_source_line (unsigned int line, const char *filename,
21378 int discriminator, bool is_stmt)
21380 unsigned int file_num;
21381 dw_line_info_table *table;
21383 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21384 return;
21386 /* The discriminator column was added in dwarf4. Simplify the below
21387 by simply removing it if we're not supposed to output it. */
21388 if (dwarf_version < 4 && dwarf_strict)
21389 discriminator = 0;
21391 table = cur_line_info_table;
21392 file_num = maybe_emit_file (lookup_filename (filename));
21394 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21395 the debugger has used the second (possibly duplicate) line number
21396 at the beginning of the function to mark the end of the prologue.
21397 We could eliminate any other duplicates within the function. For
21398 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21399 that second line number entry. */
21400 /* Recall that this end-of-prologue indication is *not* the same thing
21401 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21402 to which the hook corresponds, follows the last insn that was
21403 emitted by gen_prologue. What we need is to precede the first insn
21404 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21405 insn that corresponds to something the user wrote. These may be
21406 very different locations once scheduling is enabled. */
21408 if (0 && file_num == table->file_num
21409 && line == table->line_num
21410 && discriminator == table->discrim_num
21411 && is_stmt == table->is_stmt)
21412 return;
21414 switch_to_section (current_function_section ());
21416 /* If requested, emit something human-readable. */
21417 if (flag_debug_asm)
21418 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21420 if (DWARF2_ASM_LINE_DEBUG_INFO)
21422 /* Emit the .loc directive understood by GNU as. */
21423 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21424 file_num, line, is_stmt, discriminator */
21425 fputs ("\t.loc ", asm_out_file);
21426 fprint_ul (asm_out_file, file_num);
21427 putc (' ', asm_out_file);
21428 fprint_ul (asm_out_file, line);
21429 putc (' ', asm_out_file);
21430 putc ('0', asm_out_file);
21432 if (is_stmt != table->is_stmt)
21434 fputs (" is_stmt ", asm_out_file);
21435 putc (is_stmt ? '1' : '0', asm_out_file);
21437 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21439 gcc_assert (discriminator > 0);
21440 fputs (" discriminator ", asm_out_file);
21441 fprint_ul (asm_out_file, (unsigned long) discriminator);
21443 putc ('\n', asm_out_file);
21445 else
21447 unsigned int label_num = ++line_info_label_num;
21449 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21451 push_dw_line_info_entry (table, LI_set_address, label_num);
21452 if (file_num != table->file_num)
21453 push_dw_line_info_entry (table, LI_set_file, file_num);
21454 if (discriminator != table->discrim_num)
21455 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21456 if (is_stmt != table->is_stmt)
21457 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21458 push_dw_line_info_entry (table, LI_set_line, line);
21461 table->file_num = file_num;
21462 table->line_num = line;
21463 table->discrim_num = discriminator;
21464 table->is_stmt = is_stmt;
21465 table->in_use = true;
21468 /* Record the beginning of a new source file. */
21470 static void
21471 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21473 if (flag_eliminate_dwarf2_dups)
21475 /* Record the beginning of the file for break_out_includes. */
21476 dw_die_ref bincl_die;
21478 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21479 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21482 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21484 macinfo_entry e;
21485 e.code = DW_MACINFO_start_file;
21486 e.lineno = lineno;
21487 e.info = ggc_strdup (filename);
21488 vec_safe_push (macinfo_table, e);
21492 /* Record the end of a source file. */
21494 static void
21495 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21497 if (flag_eliminate_dwarf2_dups)
21498 /* Record the end of the file for break_out_includes. */
21499 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21501 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21503 macinfo_entry e;
21504 e.code = DW_MACINFO_end_file;
21505 e.lineno = lineno;
21506 e.info = NULL;
21507 vec_safe_push (macinfo_table, e);
21511 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21512 the tail part of the directive line, i.e. the part which is past the
21513 initial whitespace, #, whitespace, directive-name, whitespace part. */
21515 static void
21516 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21517 const char *buffer ATTRIBUTE_UNUSED)
21519 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21521 macinfo_entry e;
21522 /* Insert a dummy first entry to be able to optimize the whole
21523 predefined macro block using DW_MACRO_GNU_transparent_include. */
21524 if (macinfo_table->is_empty () && lineno <= 1)
21526 e.code = 0;
21527 e.lineno = 0;
21528 e.info = NULL;
21529 vec_safe_push (macinfo_table, e);
21531 e.code = DW_MACINFO_define;
21532 e.lineno = lineno;
21533 e.info = ggc_strdup (buffer);
21534 vec_safe_push (macinfo_table, e);
21538 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21539 the tail part of the directive line, i.e. the part which is past the
21540 initial whitespace, #, whitespace, directive-name, whitespace part. */
21542 static void
21543 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21544 const char *buffer ATTRIBUTE_UNUSED)
21546 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21548 macinfo_entry e;
21549 /* Insert a dummy first entry to be able to optimize the whole
21550 predefined macro block using DW_MACRO_GNU_transparent_include. */
21551 if (macinfo_table->is_empty () && lineno <= 1)
21553 e.code = 0;
21554 e.lineno = 0;
21555 e.info = NULL;
21556 vec_safe_push (macinfo_table, e);
21558 e.code = DW_MACINFO_undef;
21559 e.lineno = lineno;
21560 e.info = ggc_strdup (buffer);
21561 vec_safe_push (macinfo_table, e);
21565 /* Helpers to manipulate hash table of CUs. */
21567 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21569 typedef macinfo_entry value_type;
21570 typedef macinfo_entry compare_type;
21571 static inline hashval_t hash (const value_type *);
21572 static inline bool equal (const value_type *, const compare_type *);
21575 inline hashval_t
21576 macinfo_entry_hasher::hash (const value_type *entry)
21578 return htab_hash_string (entry->info);
21581 inline bool
21582 macinfo_entry_hasher::equal (const value_type *entry1,
21583 const compare_type *entry2)
21585 return !strcmp (entry1->info, entry2->info);
21588 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21590 /* Output a single .debug_macinfo entry. */
21592 static void
21593 output_macinfo_op (macinfo_entry *ref)
21595 int file_num;
21596 size_t len;
21597 struct indirect_string_node *node;
21598 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21599 struct dwarf_file_data *fd;
21601 switch (ref->code)
21603 case DW_MACINFO_start_file:
21604 fd = lookup_filename (ref->info);
21605 file_num = maybe_emit_file (fd);
21606 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21607 dw2_asm_output_data_uleb128 (ref->lineno,
21608 "Included from line number %lu",
21609 (unsigned long) ref->lineno);
21610 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21611 break;
21612 case DW_MACINFO_end_file:
21613 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21614 break;
21615 case DW_MACINFO_define:
21616 case DW_MACINFO_undef:
21617 len = strlen (ref->info) + 1;
21618 if (!dwarf_strict
21619 && len > DWARF_OFFSET_SIZE
21620 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21621 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21623 ref->code = ref->code == DW_MACINFO_define
21624 ? DW_MACRO_GNU_define_indirect
21625 : DW_MACRO_GNU_undef_indirect;
21626 output_macinfo_op (ref);
21627 return;
21629 dw2_asm_output_data (1, ref->code,
21630 ref->code == DW_MACINFO_define
21631 ? "Define macro" : "Undefine macro");
21632 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21633 (unsigned long) ref->lineno);
21634 dw2_asm_output_nstring (ref->info, -1, "The macro");
21635 break;
21636 case DW_MACRO_GNU_define_indirect:
21637 case DW_MACRO_GNU_undef_indirect:
21638 node = find_AT_string (ref->info);
21639 gcc_assert (node
21640 && ((node->form == DW_FORM_strp)
21641 || (node->form == DW_FORM_GNU_str_index)));
21642 dw2_asm_output_data (1, ref->code,
21643 ref->code == DW_MACRO_GNU_define_indirect
21644 ? "Define macro indirect"
21645 : "Undefine macro indirect");
21646 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21647 (unsigned long) ref->lineno);
21648 if (node->form == DW_FORM_strp)
21649 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21650 debug_str_section, "The macro: \"%s\"",
21651 ref->info);
21652 else
21653 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21654 ref->info);
21655 break;
21656 case DW_MACRO_GNU_transparent_include:
21657 dw2_asm_output_data (1, ref->code, "Transparent include");
21658 ASM_GENERATE_INTERNAL_LABEL (label,
21659 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21660 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21661 break;
21662 default:
21663 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21664 ASM_COMMENT_START, (unsigned long) ref->code);
21665 break;
21669 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21670 other compilation unit .debug_macinfo sections. IDX is the first
21671 index of a define/undef, return the number of ops that should be
21672 emitted in a comdat .debug_macinfo section and emit
21673 a DW_MACRO_GNU_transparent_include entry referencing it.
21674 If the define/undef entry should be emitted normally, return 0. */
21676 static unsigned
21677 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21678 macinfo_hash_type *macinfo_htab)
21680 macinfo_entry *first, *second, *cur, *inc;
21681 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21682 unsigned char checksum[16];
21683 struct md5_ctx ctx;
21684 char *grp_name, *tail;
21685 const char *base;
21686 unsigned int i, count, encoded_filename_len, linebuf_len;
21687 macinfo_entry **slot;
21689 first = &(*macinfo_table)[idx];
21690 second = &(*macinfo_table)[idx + 1];
21692 /* Optimize only if there are at least two consecutive define/undef ops,
21693 and either all of them are before first DW_MACINFO_start_file
21694 with lineno {0,1} (i.e. predefined macro block), or all of them are
21695 in some included header file. */
21696 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21697 return 0;
21698 if (vec_safe_is_empty (files))
21700 if (first->lineno > 1 || second->lineno > 1)
21701 return 0;
21703 else if (first->lineno == 0)
21704 return 0;
21706 /* Find the last define/undef entry that can be grouped together
21707 with first and at the same time compute md5 checksum of their
21708 codes, linenumbers and strings. */
21709 md5_init_ctx (&ctx);
21710 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21711 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21712 break;
21713 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21714 break;
21715 else
21717 unsigned char code = cur->code;
21718 md5_process_bytes (&code, 1, &ctx);
21719 checksum_uleb128 (cur->lineno, &ctx);
21720 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21722 md5_finish_ctx (&ctx, checksum);
21723 count = i - idx;
21725 /* From the containing include filename (if any) pick up just
21726 usable characters from its basename. */
21727 if (vec_safe_is_empty (files))
21728 base = "";
21729 else
21730 base = lbasename (files->last ().info);
21731 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21732 if (ISIDNUM (base[i]) || base[i] == '.')
21733 encoded_filename_len++;
21734 /* Count . at the end. */
21735 if (encoded_filename_len)
21736 encoded_filename_len++;
21738 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21739 linebuf_len = strlen (linebuf);
21741 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21742 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21743 + 16 * 2 + 1);
21744 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21745 tail = grp_name + 4;
21746 if (encoded_filename_len)
21748 for (i = 0; base[i]; i++)
21749 if (ISIDNUM (base[i]) || base[i] == '.')
21750 *tail++ = base[i];
21751 *tail++ = '.';
21753 memcpy (tail, linebuf, linebuf_len);
21754 tail += linebuf_len;
21755 *tail++ = '.';
21756 for (i = 0; i < 16; i++)
21757 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21759 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21760 in the empty vector entry before the first define/undef. */
21761 inc = &(*macinfo_table)[idx - 1];
21762 inc->code = DW_MACRO_GNU_transparent_include;
21763 inc->lineno = 0;
21764 inc->info = ggc_strdup (grp_name);
21765 if (!macinfo_htab->is_created ())
21766 macinfo_htab->create (10);
21767 /* Avoid emitting duplicates. */
21768 slot = macinfo_htab->find_slot (inc, INSERT);
21769 if (*slot != NULL)
21771 inc->code = 0;
21772 inc->info = NULL;
21773 /* If such an entry has been used before, just emit
21774 a DW_MACRO_GNU_transparent_include op. */
21775 inc = *slot;
21776 output_macinfo_op (inc);
21777 /* And clear all macinfo_entry in the range to avoid emitting them
21778 in the second pass. */
21779 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21781 cur->code = 0;
21782 cur->info = NULL;
21785 else
21787 *slot = inc;
21788 inc->lineno = macinfo_htab->elements ();
21789 output_macinfo_op (inc);
21791 return count;
21794 /* Save any strings needed by the macinfo table in the debug str
21795 table. All strings must be collected into the table by the time
21796 index_string is called. */
21798 static void
21799 save_macinfo_strings (void)
21801 unsigned len;
21802 unsigned i;
21803 macinfo_entry *ref;
21805 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21807 switch (ref->code)
21809 /* Match the logic in output_macinfo_op to decide on
21810 indirect strings. */
21811 case DW_MACINFO_define:
21812 case DW_MACINFO_undef:
21813 len = strlen (ref->info) + 1;
21814 if (!dwarf_strict
21815 && len > DWARF_OFFSET_SIZE
21816 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21817 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21818 set_indirect_string (find_AT_string (ref->info));
21819 break;
21820 case DW_MACRO_GNU_define_indirect:
21821 case DW_MACRO_GNU_undef_indirect:
21822 set_indirect_string (find_AT_string (ref->info));
21823 break;
21824 default:
21825 break;
21830 /* Output macinfo section(s). */
21832 static void
21833 output_macinfo (void)
21835 unsigned i;
21836 unsigned long length = vec_safe_length (macinfo_table);
21837 macinfo_entry *ref;
21838 vec<macinfo_entry, va_gc> *files = NULL;
21839 macinfo_hash_type macinfo_htab;
21841 if (! length)
21842 return;
21844 /* output_macinfo* uses these interchangeably. */
21845 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21846 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21847 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21848 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21850 /* For .debug_macro emit the section header. */
21851 if (!dwarf_strict)
21853 dw2_asm_output_data (2, 4, "DWARF macro version number");
21854 if (DWARF_OFFSET_SIZE == 8)
21855 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21856 else
21857 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21858 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21859 (!dwarf_split_debug_info ? debug_line_section_label
21860 : debug_skeleton_line_section_label),
21861 debug_line_section, NULL);
21864 /* In the first loop, it emits the primary .debug_macinfo section
21865 and after each emitted op the macinfo_entry is cleared.
21866 If a longer range of define/undef ops can be optimized using
21867 DW_MACRO_GNU_transparent_include, the
21868 DW_MACRO_GNU_transparent_include op is emitted and kept in
21869 the vector before the first define/undef in the range and the
21870 whole range of define/undef ops is not emitted and kept. */
21871 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21873 switch (ref->code)
21875 case DW_MACINFO_start_file:
21876 vec_safe_push (files, *ref);
21877 break;
21878 case DW_MACINFO_end_file:
21879 if (!vec_safe_is_empty (files))
21880 files->pop ();
21881 break;
21882 case DW_MACINFO_define:
21883 case DW_MACINFO_undef:
21884 if (!dwarf_strict
21885 && HAVE_COMDAT_GROUP
21886 && vec_safe_length (files) != 1
21887 && i > 0
21888 && i + 1 < length
21889 && (*macinfo_table)[i - 1].code == 0)
21891 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21892 if (count)
21894 i += count - 1;
21895 continue;
21898 break;
21899 case 0:
21900 /* A dummy entry may be inserted at the beginning to be able
21901 to optimize the whole block of predefined macros. */
21902 if (i == 0)
21903 continue;
21904 default:
21905 break;
21907 output_macinfo_op (ref);
21908 ref->info = NULL;
21909 ref->code = 0;
21912 if (!macinfo_htab.is_created ())
21913 return;
21915 macinfo_htab.dispose ();
21917 /* If any DW_MACRO_GNU_transparent_include were used, on those
21918 DW_MACRO_GNU_transparent_include entries terminate the
21919 current chain and switch to a new comdat .debug_macinfo
21920 section and emit the define/undef entries within it. */
21921 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21922 switch (ref->code)
21924 case 0:
21925 continue;
21926 case DW_MACRO_GNU_transparent_include:
21928 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21929 tree comdat_key = get_identifier (ref->info);
21930 /* Terminate the previous .debug_macinfo section. */
21931 dw2_asm_output_data (1, 0, "End compilation unit");
21932 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21933 SECTION_DEBUG
21934 | SECTION_LINKONCE,
21935 comdat_key);
21936 ASM_GENERATE_INTERNAL_LABEL (label,
21937 DEBUG_MACRO_SECTION_LABEL,
21938 ref->lineno);
21939 ASM_OUTPUT_LABEL (asm_out_file, label);
21940 ref->code = 0;
21941 ref->info = NULL;
21942 dw2_asm_output_data (2, 4, "DWARF macro version number");
21943 if (DWARF_OFFSET_SIZE == 8)
21944 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21945 else
21946 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21948 break;
21949 case DW_MACINFO_define:
21950 case DW_MACINFO_undef:
21951 output_macinfo_op (ref);
21952 ref->code = 0;
21953 ref->info = NULL;
21954 break;
21955 default:
21956 gcc_unreachable ();
21960 /* Set up for Dwarf output at the start of compilation. */
21962 static void
21963 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21965 /* Allocate the file_table. */
21966 file_table = htab_create_ggc (50, file_table_hash,
21967 file_table_eq, NULL);
21969 /* Allocate the decl_die_table. */
21970 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21971 decl_die_table_eq, NULL);
21973 /* Allocate the decl_loc_table. */
21974 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21975 decl_loc_table_eq, NULL);
21977 /* Allocate the cached_dw_loc_list_table. */
21978 cached_dw_loc_list_table
21979 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21980 cached_dw_loc_list_table_eq, NULL);
21982 /* Allocate the initial hunk of the decl_scope_table. */
21983 vec_alloc (decl_scope_table, 256);
21985 /* Allocate the initial hunk of the abbrev_die_table. */
21986 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21987 (ABBREV_DIE_TABLE_INCREMENT);
21988 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21989 /* Zero-th entry is allocated, but unused. */
21990 abbrev_die_table_in_use = 1;
21992 /* Allocate the pubtypes and pubnames vectors. */
21993 vec_alloc (pubname_table, 32);
21994 vec_alloc (pubtype_table, 32);
21996 vec_alloc (incomplete_types, 64);
21998 vec_alloc (used_rtx_array, 32);
22000 if (!dwarf_split_debug_info)
22002 debug_info_section = get_section (DEBUG_INFO_SECTION,
22003 SECTION_DEBUG, NULL);
22004 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22005 SECTION_DEBUG, NULL);
22006 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22007 SECTION_DEBUG, NULL);
22009 else
22011 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22012 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22013 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22014 SECTION_DEBUG | SECTION_EXCLUDE,
22015 NULL);
22016 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22017 SECTION_DEBUG, NULL);
22018 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22019 SECTION_DEBUG, NULL);
22020 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22021 SECTION_DEBUG, NULL);
22022 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22023 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22025 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22026 the main .o, but the skeleton_line goes into the split off dwo. */
22027 debug_skeleton_line_section
22028 = get_section (DEBUG_DWO_LINE_SECTION,
22029 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22030 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22031 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22032 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22033 SECTION_DEBUG | SECTION_EXCLUDE,
22034 NULL);
22035 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22036 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22037 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22038 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22039 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22040 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22042 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22043 SECTION_DEBUG, NULL);
22044 debug_macinfo_section = get_section (dwarf_strict
22045 ? DEBUG_MACINFO_SECTION
22046 : DEBUG_MACRO_SECTION,
22047 DEBUG_MACRO_SECTION_FLAGS, NULL);
22048 debug_line_section = get_section (DEBUG_LINE_SECTION,
22049 SECTION_DEBUG, NULL);
22050 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22051 SECTION_DEBUG, NULL);
22052 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22053 SECTION_DEBUG, NULL);
22054 debug_str_section = get_section (DEBUG_STR_SECTION,
22055 DEBUG_STR_SECTION_FLAGS, NULL);
22056 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22057 SECTION_DEBUG, NULL);
22058 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22059 SECTION_DEBUG, NULL);
22061 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22062 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22063 DEBUG_ABBREV_SECTION_LABEL, 0);
22064 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22065 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22066 COLD_TEXT_SECTION_LABEL, 0);
22067 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22069 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22070 DEBUG_INFO_SECTION_LABEL, 0);
22071 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22072 DEBUG_LINE_SECTION_LABEL, 0);
22073 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22074 DEBUG_RANGES_SECTION_LABEL, 0);
22075 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22076 DEBUG_ADDR_SECTION_LABEL, 0);
22077 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22078 dwarf_strict
22079 ? DEBUG_MACINFO_SECTION_LABEL
22080 : DEBUG_MACRO_SECTION_LABEL, 0);
22081 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22083 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22084 vec_alloc (macinfo_table, 64);
22086 switch_to_section (text_section);
22087 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22089 /* Make sure the line number table for .text always exists. */
22090 text_section_line_info = new_line_info_table ();
22091 text_section_line_info->end_label = text_end_label;
22094 /* Called before compile () starts outputtting functions, variables
22095 and toplevel asms into assembly. */
22097 static void
22098 dwarf2out_assembly_start (void)
22100 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22101 && dwarf2out_do_cfi_asm ()
22102 && (!(flag_unwind_tables || flag_exceptions)
22103 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22104 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22107 /* A helper function for dwarf2out_finish called through
22108 htab_traverse. Assign a string its index. All strings must be
22109 collected into the table by the time index_string is called,
22110 because the indexing code relies on htab_traverse to traverse nodes
22111 in the same order for each run. */
22113 static int
22114 index_string (void **h, void *v)
22116 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22117 unsigned int *index = (unsigned int *) v;
22119 find_string_form (node);
22120 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22122 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22123 node->index = *index;
22124 *index += 1;
22126 return 1;
22129 /* A helper function for output_indirect_strings called through
22130 htab_traverse. Output the offset to a string and update the
22131 current offset. */
22133 static int
22134 output_index_string_offset (void **h, void *v)
22136 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22137 unsigned int *offset = (unsigned int *) v;
22139 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22141 /* Assert that this node has been assigned an index. */
22142 gcc_assert (node->index != NO_INDEX_ASSIGNED
22143 && node->index != NOT_INDEXED);
22144 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22145 "indexed string 0x%x: %s", node->index, node->str);
22146 *offset += strlen (node->str) + 1;
22148 return 1;
22151 /* A helper function for dwarf2out_finish called through
22152 htab_traverse. Output the indexed string. */
22154 static int
22155 output_index_string (void **h, void *v)
22157 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22158 unsigned int *cur_idx = (unsigned int *) v;
22160 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22162 /* Assert that the strings are output in the same order as their
22163 indexes were assigned. */
22164 gcc_assert (*cur_idx == node->index);
22165 assemble_string (node->str, strlen (node->str) + 1);
22166 *cur_idx += 1;
22168 return 1;
22171 /* A helper function for dwarf2out_finish called through
22172 htab_traverse. Emit one queued .debug_str string. */
22174 static int
22175 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22177 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22179 node->form = find_string_form (node);
22180 if (node->form == DW_FORM_strp && node->refcount > 0)
22182 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22183 assemble_string (node->str, strlen (node->str) + 1);
22186 return 1;
22189 /* Output the indexed string table. */
22191 static void
22192 output_indirect_strings (void)
22194 switch_to_section (debug_str_section);
22195 if (!dwarf_split_debug_info)
22196 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22197 else
22199 unsigned int offset = 0;
22200 unsigned int cur_idx = 0;
22202 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22204 switch_to_section (debug_str_offsets_section);
22205 htab_traverse_noresize (debug_str_hash,
22206 output_index_string_offset,
22207 &offset);
22208 switch_to_section (debug_str_dwo_section);
22209 htab_traverse_noresize (debug_str_hash,
22210 output_index_string,
22211 &cur_idx);
22215 /* Callback for htab_traverse to assign an index to an entry in the
22216 table, and to write that entry to the .debug_addr section. */
22218 static int
22219 output_addr_table_entry (void **slot, void *data)
22221 addr_table_entry *entry = (addr_table_entry *) *slot;
22222 unsigned int *cur_index = (unsigned int *)data;
22224 if (entry->refcount == 0)
22226 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22227 || entry->index == NOT_INDEXED);
22228 return 1;
22231 gcc_assert (entry->index == *cur_index);
22232 (*cur_index)++;
22234 switch (entry->kind)
22236 case ate_kind_rtx:
22237 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22238 "0x%x", entry->index);
22239 break;
22240 case ate_kind_rtx_dtprel:
22241 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22242 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22243 DWARF2_ADDR_SIZE,
22244 entry->addr.rtl);
22245 fputc ('\n', asm_out_file);
22246 break;
22247 case ate_kind_label:
22248 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22249 "0x%x", entry->index);
22250 break;
22251 default:
22252 gcc_unreachable ();
22254 return 1;
22257 /* Produce the .debug_addr section. */
22259 static void
22260 output_addr_table (void)
22262 unsigned int index = 0;
22263 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22264 return;
22266 switch_to_section (debug_addr_section);
22267 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22270 #if ENABLE_ASSERT_CHECKING
22271 /* Verify that all marks are clear. */
22273 static void
22274 verify_marks_clear (dw_die_ref die)
22276 dw_die_ref c;
22278 gcc_assert (! die->die_mark);
22279 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22281 #endif /* ENABLE_ASSERT_CHECKING */
22283 /* Clear the marks for a die and its children.
22284 Be cool if the mark isn't set. */
22286 static void
22287 prune_unmark_dies (dw_die_ref die)
22289 dw_die_ref c;
22291 if (die->die_mark)
22292 die->die_mark = 0;
22293 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22296 /* Given DIE that we're marking as used, find any other dies
22297 it references as attributes and mark them as used. */
22299 static void
22300 prune_unused_types_walk_attribs (dw_die_ref die)
22302 dw_attr_ref a;
22303 unsigned ix;
22305 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22307 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22309 /* A reference to another DIE.
22310 Make sure that it will get emitted.
22311 If it was broken out into a comdat group, don't follow it. */
22312 if (! AT_ref (a)->comdat_type_p
22313 || a->dw_attr == DW_AT_specification)
22314 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22316 /* Set the string's refcount to 0 so that prune_unused_types_mark
22317 accounts properly for it. */
22318 if (AT_class (a) == dw_val_class_str)
22319 a->dw_attr_val.v.val_str->refcount = 0;
22323 /* Mark the generic parameters and arguments children DIEs of DIE. */
22325 static void
22326 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22328 dw_die_ref c;
22330 if (die == NULL || die->die_child == NULL)
22331 return;
22332 c = die->die_child;
22335 if (is_template_parameter (c))
22336 prune_unused_types_mark (c, 1);
22337 c = c->die_sib;
22338 } while (c && c != die->die_child);
22341 /* Mark DIE as being used. If DOKIDS is true, then walk down
22342 to DIE's children. */
22344 static void
22345 prune_unused_types_mark (dw_die_ref die, int dokids)
22347 dw_die_ref c;
22349 if (die->die_mark == 0)
22351 /* We haven't done this node yet. Mark it as used. */
22352 die->die_mark = 1;
22353 /* If this is the DIE of a generic type instantiation,
22354 mark the children DIEs that describe its generic parms and
22355 args. */
22356 prune_unused_types_mark_generic_parms_dies (die);
22358 /* We also have to mark its parents as used.
22359 (But we don't want to mark our parent's kids due to this,
22360 unless it is a class.) */
22361 if (die->die_parent)
22362 prune_unused_types_mark (die->die_parent,
22363 class_scope_p (die->die_parent));
22365 /* Mark any referenced nodes. */
22366 prune_unused_types_walk_attribs (die);
22368 /* If this node is a specification,
22369 also mark the definition, if it exists. */
22370 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22371 prune_unused_types_mark (die->die_definition, 1);
22374 if (dokids && die->die_mark != 2)
22376 /* We need to walk the children, but haven't done so yet.
22377 Remember that we've walked the kids. */
22378 die->die_mark = 2;
22380 /* If this is an array type, we need to make sure our
22381 kids get marked, even if they're types. If we're
22382 breaking out types into comdat sections, do this
22383 for all type definitions. */
22384 if (die->die_tag == DW_TAG_array_type
22385 || (use_debug_types
22386 && is_type_die (die) && ! is_declaration_die (die)))
22387 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22388 else
22389 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22393 /* For local classes, look if any static member functions were emitted
22394 and if so, mark them. */
22396 static void
22397 prune_unused_types_walk_local_classes (dw_die_ref die)
22399 dw_die_ref c;
22401 if (die->die_mark == 2)
22402 return;
22404 switch (die->die_tag)
22406 case DW_TAG_structure_type:
22407 case DW_TAG_union_type:
22408 case DW_TAG_class_type:
22409 break;
22411 case DW_TAG_subprogram:
22412 if (!get_AT_flag (die, DW_AT_declaration)
22413 || die->die_definition != NULL)
22414 prune_unused_types_mark (die, 1);
22415 return;
22417 default:
22418 return;
22421 /* Mark children. */
22422 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22425 /* Walk the tree DIE and mark types that we actually use. */
22427 static void
22428 prune_unused_types_walk (dw_die_ref die)
22430 dw_die_ref c;
22432 /* Don't do anything if this node is already marked and
22433 children have been marked as well. */
22434 if (die->die_mark == 2)
22435 return;
22437 switch (die->die_tag)
22439 case DW_TAG_structure_type:
22440 case DW_TAG_union_type:
22441 case DW_TAG_class_type:
22442 if (die->die_perennial_p)
22443 break;
22445 for (c = die->die_parent; c; c = c->die_parent)
22446 if (c->die_tag == DW_TAG_subprogram)
22447 break;
22449 /* Finding used static member functions inside of classes
22450 is needed just for local classes, because for other classes
22451 static member function DIEs with DW_AT_specification
22452 are emitted outside of the DW_TAG_*_type. If we ever change
22453 it, we'd need to call this even for non-local classes. */
22454 if (c)
22455 prune_unused_types_walk_local_classes (die);
22457 /* It's a type node --- don't mark it. */
22458 return;
22460 case DW_TAG_const_type:
22461 case DW_TAG_packed_type:
22462 case DW_TAG_pointer_type:
22463 case DW_TAG_reference_type:
22464 case DW_TAG_rvalue_reference_type:
22465 case DW_TAG_volatile_type:
22466 case DW_TAG_typedef:
22467 case DW_TAG_array_type:
22468 case DW_TAG_interface_type:
22469 case DW_TAG_friend:
22470 case DW_TAG_variant_part:
22471 case DW_TAG_enumeration_type:
22472 case DW_TAG_subroutine_type:
22473 case DW_TAG_string_type:
22474 case DW_TAG_set_type:
22475 case DW_TAG_subrange_type:
22476 case DW_TAG_ptr_to_member_type:
22477 case DW_TAG_file_type:
22478 if (die->die_perennial_p)
22479 break;
22481 /* It's a type node --- don't mark it. */
22482 return;
22484 default:
22485 /* Mark everything else. */
22486 break;
22489 if (die->die_mark == 0)
22491 die->die_mark = 1;
22493 /* Now, mark any dies referenced from here. */
22494 prune_unused_types_walk_attribs (die);
22497 die->die_mark = 2;
22499 /* Mark children. */
22500 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22503 /* Increment the string counts on strings referred to from DIE's
22504 attributes. */
22506 static void
22507 prune_unused_types_update_strings (dw_die_ref die)
22509 dw_attr_ref a;
22510 unsigned ix;
22512 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22513 if (AT_class (a) == dw_val_class_str)
22515 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22516 s->refcount++;
22517 /* Avoid unnecessarily putting strings that are used less than
22518 twice in the hash table. */
22519 if (s->refcount
22520 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22522 void ** slot;
22523 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22524 htab_hash_string (s->str),
22525 INSERT);
22526 gcc_assert (*slot == NULL);
22527 *slot = s;
22532 /* Remove from the tree DIE any dies that aren't marked. */
22534 static void
22535 prune_unused_types_prune (dw_die_ref die)
22537 dw_die_ref c;
22539 gcc_assert (die->die_mark);
22540 prune_unused_types_update_strings (die);
22542 if (! die->die_child)
22543 return;
22545 c = die->die_child;
22546 do {
22547 dw_die_ref prev = c;
22548 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22549 if (c == die->die_child)
22551 /* No marked children between 'prev' and the end of the list. */
22552 if (prev == c)
22553 /* No marked children at all. */
22554 die->die_child = NULL;
22555 else
22557 prev->die_sib = c->die_sib;
22558 die->die_child = prev;
22560 return;
22563 if (c != prev->die_sib)
22564 prev->die_sib = c;
22565 prune_unused_types_prune (c);
22566 } while (c != die->die_child);
22569 /* Remove dies representing declarations that we never use. */
22571 static void
22572 prune_unused_types (void)
22574 unsigned int i;
22575 limbo_die_node *node;
22576 comdat_type_node *ctnode;
22577 pubname_ref pub;
22578 dw_die_ref base_type;
22580 #if ENABLE_ASSERT_CHECKING
22581 /* All the marks should already be clear. */
22582 verify_marks_clear (comp_unit_die ());
22583 for (node = limbo_die_list; node; node = node->next)
22584 verify_marks_clear (node->die);
22585 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22586 verify_marks_clear (ctnode->root_die);
22587 #endif /* ENABLE_ASSERT_CHECKING */
22589 /* Mark types that are used in global variables. */
22590 premark_types_used_by_global_vars ();
22592 /* Set the mark on nodes that are actually used. */
22593 prune_unused_types_walk (comp_unit_die ());
22594 for (node = limbo_die_list; node; node = node->next)
22595 prune_unused_types_walk (node->die);
22596 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22598 prune_unused_types_walk (ctnode->root_die);
22599 prune_unused_types_mark (ctnode->type_die, 1);
22602 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22603 are unusual in that they are pubnames that are the children of pubtypes.
22604 They should only be marked via their parent DW_TAG_enumeration_type die,
22605 not as roots in themselves. */
22606 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22607 if (pub->die->die_tag != DW_TAG_enumerator)
22608 prune_unused_types_mark (pub->die, 1);
22609 for (i = 0; base_types.iterate (i, &base_type); i++)
22610 prune_unused_types_mark (base_type, 1);
22612 if (debug_str_hash)
22613 htab_empty (debug_str_hash);
22614 if (skeleton_debug_str_hash)
22615 htab_empty (skeleton_debug_str_hash);
22616 prune_unused_types_prune (comp_unit_die ());
22617 for (node = limbo_die_list; node; node = node->next)
22618 prune_unused_types_prune (node->die);
22619 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22620 prune_unused_types_prune (ctnode->root_die);
22622 /* Leave the marks clear. */
22623 prune_unmark_dies (comp_unit_die ());
22624 for (node = limbo_die_list; node; node = node->next)
22625 prune_unmark_dies (node->die);
22626 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22627 prune_unmark_dies (ctnode->root_die);
22630 /* Set the parameter to true if there are any relative pathnames in
22631 the file table. */
22632 static int
22633 file_table_relative_p (void ** slot, void *param)
22635 bool *p = (bool *) param;
22636 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22637 if (!IS_ABSOLUTE_PATH (d->filename))
22639 *p = true;
22640 return 0;
22642 return 1;
22645 /* Helpers to manipulate hash table of comdat type units. */
22647 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22649 typedef comdat_type_node value_type;
22650 typedef comdat_type_node compare_type;
22651 static inline hashval_t hash (const value_type *);
22652 static inline bool equal (const value_type *, const compare_type *);
22655 inline hashval_t
22656 comdat_type_hasher::hash (const value_type *type_node)
22658 hashval_t h;
22659 memcpy (&h, type_node->signature, sizeof (h));
22660 return h;
22663 inline bool
22664 comdat_type_hasher::equal (const value_type *type_node_1,
22665 const compare_type *type_node_2)
22667 return (! memcmp (type_node_1->signature, type_node_2->signature,
22668 DWARF_TYPE_SIGNATURE_SIZE));
22671 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22672 to the location it would have been added, should we know its
22673 DECL_ASSEMBLER_NAME when we added other attributes. This will
22674 probably improve compactness of debug info, removing equivalent
22675 abbrevs, and hide any differences caused by deferring the
22676 computation of the assembler name, triggered by e.g. PCH. */
22678 static inline void
22679 move_linkage_attr (dw_die_ref die)
22681 unsigned ix = vec_safe_length (die->die_attr);
22682 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22684 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22685 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22687 while (--ix > 0)
22689 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22691 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22692 break;
22695 if (ix != vec_safe_length (die->die_attr) - 1)
22697 die->die_attr->pop ();
22698 die->die_attr->quick_insert (ix, linkage);
22702 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22703 referenced from typed stack ops and count how often they are used. */
22705 static void
22706 mark_base_types (dw_loc_descr_ref loc)
22708 dw_die_ref base_type = NULL;
22710 for (; loc; loc = loc->dw_loc_next)
22712 switch (loc->dw_loc_opc)
22714 case DW_OP_GNU_regval_type:
22715 case DW_OP_GNU_deref_type:
22716 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22717 break;
22718 case DW_OP_GNU_convert:
22719 case DW_OP_GNU_reinterpret:
22720 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22721 continue;
22722 /* FALLTHRU */
22723 case DW_OP_GNU_const_type:
22724 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22725 break;
22726 case DW_OP_GNU_entry_value:
22727 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22728 continue;
22729 default:
22730 continue;
22732 gcc_assert (base_type->die_parent == comp_unit_die ());
22733 if (base_type->die_mark)
22734 base_type->die_mark++;
22735 else
22737 base_types.safe_push (base_type);
22738 base_type->die_mark = 1;
22743 /* Comparison function for sorting marked base types. */
22745 static int
22746 base_type_cmp (const void *x, const void *y)
22748 dw_die_ref dx = *(const dw_die_ref *) x;
22749 dw_die_ref dy = *(const dw_die_ref *) y;
22750 unsigned int byte_size1, byte_size2;
22751 unsigned int encoding1, encoding2;
22752 if (dx->die_mark > dy->die_mark)
22753 return -1;
22754 if (dx->die_mark < dy->die_mark)
22755 return 1;
22756 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22757 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22758 if (byte_size1 < byte_size2)
22759 return 1;
22760 if (byte_size1 > byte_size2)
22761 return -1;
22762 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22763 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22764 if (encoding1 < encoding2)
22765 return 1;
22766 if (encoding1 > encoding2)
22767 return -1;
22768 return 0;
22771 /* Move base types marked by mark_base_types as early as possible
22772 in the CU, sorted by decreasing usage count both to make the
22773 uleb128 references as small as possible and to make sure they
22774 will have die_offset already computed by calc_die_sizes when
22775 sizes of typed stack loc ops is computed. */
22777 static void
22778 move_marked_base_types (void)
22780 unsigned int i;
22781 dw_die_ref base_type, die, c;
22783 if (base_types.is_empty ())
22784 return;
22786 /* Sort by decreasing usage count, they will be added again in that
22787 order later on. */
22788 base_types.qsort (base_type_cmp);
22789 die = comp_unit_die ();
22790 c = die->die_child;
22793 dw_die_ref prev = c;
22794 c = c->die_sib;
22795 while (c->die_mark)
22797 remove_child_with_prev (c, prev);
22798 /* As base types got marked, there must be at least
22799 one node other than DW_TAG_base_type. */
22800 gcc_assert (c != c->die_sib);
22801 c = c->die_sib;
22804 while (c != die->die_child);
22805 gcc_assert (die->die_child);
22806 c = die->die_child;
22807 for (i = 0; base_types.iterate (i, &base_type); i++)
22809 base_type->die_mark = 0;
22810 base_type->die_sib = c->die_sib;
22811 c->die_sib = base_type;
22812 c = base_type;
22816 /* Helper function for resolve_addr, attempt to resolve
22817 one CONST_STRING, return non-zero if not successful. Similarly verify that
22818 SYMBOL_REFs refer to variables emitted in the current CU. */
22820 static int
22821 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22823 rtx rtl = *addr;
22825 if (GET_CODE (rtl) == CONST_STRING)
22827 size_t len = strlen (XSTR (rtl, 0)) + 1;
22828 tree t = build_string (len, XSTR (rtl, 0));
22829 tree tlen = size_int (len - 1);
22830 TREE_TYPE (t)
22831 = build_array_type (char_type_node, build_index_type (tlen));
22832 rtl = lookup_constant_def (t);
22833 if (!rtl || !MEM_P (rtl))
22834 return 1;
22835 rtl = XEXP (rtl, 0);
22836 if (GET_CODE (rtl) == SYMBOL_REF
22837 && SYMBOL_REF_DECL (rtl)
22838 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22839 return 1;
22840 vec_safe_push (used_rtx_array, rtl);
22841 *addr = rtl;
22842 return 0;
22845 if (GET_CODE (rtl) == SYMBOL_REF
22846 && SYMBOL_REF_DECL (rtl))
22848 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22850 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22851 return 1;
22853 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22854 return 1;
22857 if (GET_CODE (rtl) == CONST
22858 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22859 return 1;
22861 return 0;
22864 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22865 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22866 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22868 static rtx
22869 string_cst_pool_decl (tree t)
22871 rtx rtl = output_constant_def (t, 1);
22872 unsigned char *array;
22873 dw_loc_descr_ref l;
22874 tree decl;
22875 size_t len;
22876 dw_die_ref ref;
22878 if (!rtl || !MEM_P (rtl))
22879 return NULL_RTX;
22880 rtl = XEXP (rtl, 0);
22881 if (GET_CODE (rtl) != SYMBOL_REF
22882 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22883 return NULL_RTX;
22885 decl = SYMBOL_REF_DECL (rtl);
22886 if (!lookup_decl_die (decl))
22888 len = TREE_STRING_LENGTH (t);
22889 vec_safe_push (used_rtx_array, rtl);
22890 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22891 array = (unsigned char *) ggc_alloc_atomic (len);
22892 memcpy (array, TREE_STRING_POINTER (t), len);
22893 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22894 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22895 l->dw_loc_oprnd2.v.val_vec.length = len;
22896 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22897 l->dw_loc_oprnd2.v.val_vec.array = array;
22898 add_AT_loc (ref, DW_AT_location, l);
22899 equate_decl_number_to_die (decl, ref);
22901 return rtl;
22904 /* Helper function of resolve_addr_in_expr. LOC is
22905 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22906 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22907 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22908 with DW_OP_GNU_implicit_pointer if possible
22909 and return true, if unsuccessful, return false. */
22911 static bool
22912 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22914 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22915 HOST_WIDE_INT offset = 0;
22916 dw_die_ref ref = NULL;
22917 tree decl;
22919 if (GET_CODE (rtl) == CONST
22920 && GET_CODE (XEXP (rtl, 0)) == PLUS
22921 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22923 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22924 rtl = XEXP (XEXP (rtl, 0), 0);
22926 if (GET_CODE (rtl) == CONST_STRING)
22928 size_t len = strlen (XSTR (rtl, 0)) + 1;
22929 tree t = build_string (len, XSTR (rtl, 0));
22930 tree tlen = size_int (len - 1);
22932 TREE_TYPE (t)
22933 = build_array_type (char_type_node, build_index_type (tlen));
22934 rtl = string_cst_pool_decl (t);
22935 if (!rtl)
22936 return false;
22938 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22940 decl = SYMBOL_REF_DECL (rtl);
22941 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22943 ref = lookup_decl_die (decl);
22944 if (ref && (get_AT (ref, DW_AT_location)
22945 || get_AT (ref, DW_AT_const_value)))
22947 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22948 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22949 loc->dw_loc_oprnd1.val_entry = NULL;
22950 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22951 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22952 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22953 loc->dw_loc_oprnd2.v.val_int = offset;
22954 return true;
22958 return false;
22961 /* Helper function for resolve_addr, handle one location
22962 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22963 the location list couldn't be resolved. */
22965 static bool
22966 resolve_addr_in_expr (dw_loc_descr_ref loc)
22968 dw_loc_descr_ref keep = NULL;
22969 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22970 switch (loc->dw_loc_opc)
22972 case DW_OP_addr:
22973 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22975 if ((prev == NULL
22976 || prev->dw_loc_opc == DW_OP_piece
22977 || prev->dw_loc_opc == DW_OP_bit_piece)
22978 && loc->dw_loc_next
22979 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22980 && !dwarf_strict
22981 && optimize_one_addr_into_implicit_ptr (loc))
22982 break;
22983 return false;
22985 break;
22986 case DW_OP_GNU_addr_index:
22987 case DW_OP_GNU_const_index:
22988 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22989 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22990 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22991 NULL))
22992 return false;
22993 break;
22994 case DW_OP_const4u:
22995 case DW_OP_const8u:
22996 if (loc->dtprel
22997 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22998 return false;
22999 break;
23000 case DW_OP_plus_uconst:
23001 if (size_of_loc_descr (loc)
23002 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23004 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23006 dw_loc_descr_ref repl
23007 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23008 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23009 add_loc_descr (&repl, loc->dw_loc_next);
23010 *loc = *repl;
23012 break;
23013 case DW_OP_implicit_value:
23014 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23015 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23016 return false;
23017 break;
23018 case DW_OP_GNU_implicit_pointer:
23019 case DW_OP_GNU_parameter_ref:
23020 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23022 dw_die_ref ref
23023 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23024 if (ref == NULL)
23025 return false;
23026 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23027 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23028 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23030 break;
23031 case DW_OP_GNU_const_type:
23032 case DW_OP_GNU_regval_type:
23033 case DW_OP_GNU_deref_type:
23034 case DW_OP_GNU_convert:
23035 case DW_OP_GNU_reinterpret:
23036 while (loc->dw_loc_next
23037 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23039 dw_die_ref base1, base2;
23040 unsigned enc1, enc2, size1, size2;
23041 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23042 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23043 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23044 else if (loc->dw_loc_oprnd1.val_class
23045 == dw_val_class_unsigned_const)
23046 break;
23047 else
23048 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23049 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23050 == dw_val_class_unsigned_const)
23051 break;
23052 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23053 gcc_assert (base1->die_tag == DW_TAG_base_type
23054 && base2->die_tag == DW_TAG_base_type);
23055 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23056 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23057 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23058 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23059 if (size1 == size2
23060 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23061 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23062 && loc != keep)
23063 || enc1 == enc2))
23065 /* Optimize away next DW_OP_GNU_convert after
23066 adjusting LOC's base type die reference. */
23067 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23068 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23069 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23070 else
23071 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23072 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23073 continue;
23075 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23076 point typed stack entry. */
23077 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23078 keep = loc->dw_loc_next;
23079 break;
23081 break;
23082 default:
23083 break;
23085 return true;
23088 /* Helper function of resolve_addr. DIE had DW_AT_location of
23089 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23090 and DW_OP_addr couldn't be resolved. resolve_addr has already
23091 removed the DW_AT_location attribute. This function attempts to
23092 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23093 to it or DW_AT_const_value attribute, if possible. */
23095 static void
23096 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23098 if (TREE_CODE (decl) != VAR_DECL
23099 || lookup_decl_die (decl) != die
23100 || DECL_EXTERNAL (decl)
23101 || !TREE_STATIC (decl)
23102 || DECL_INITIAL (decl) == NULL_TREE
23103 || DECL_P (DECL_INITIAL (decl))
23104 || get_AT (die, DW_AT_const_value))
23105 return;
23107 tree init = DECL_INITIAL (decl);
23108 HOST_WIDE_INT offset = 0;
23109 /* For variables that have been optimized away and thus
23110 don't have a memory location, see if we can emit
23111 DW_AT_const_value instead. */
23112 if (tree_add_const_value_attribute (die, init))
23113 return;
23114 if (dwarf_strict)
23115 return;
23116 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23117 and ADDR_EXPR refers to a decl that has DW_AT_location or
23118 DW_AT_const_value (but isn't addressable, otherwise
23119 resolving the original DW_OP_addr wouldn't fail), see if
23120 we can add DW_OP_GNU_implicit_pointer. */
23121 STRIP_NOPS (init);
23122 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23123 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23125 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23126 init = TREE_OPERAND (init, 0);
23127 STRIP_NOPS (init);
23129 if (TREE_CODE (init) != ADDR_EXPR)
23130 return;
23131 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23132 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23133 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23134 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23135 && TREE_OPERAND (init, 0) != decl))
23137 dw_die_ref ref;
23138 dw_loc_descr_ref l;
23140 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23142 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23143 if (!rtl)
23144 return;
23145 decl = SYMBOL_REF_DECL (rtl);
23147 else
23148 decl = TREE_OPERAND (init, 0);
23149 ref = lookup_decl_die (decl);
23150 if (ref == NULL
23151 || (!get_AT (ref, DW_AT_location)
23152 && !get_AT (ref, DW_AT_const_value)))
23153 return;
23154 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23155 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23156 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23157 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23158 add_AT_loc (die, DW_AT_location, l);
23162 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23163 an address in .rodata section if the string literal is emitted there,
23164 or remove the containing location list or replace DW_AT_const_value
23165 with DW_AT_location and empty location expression, if it isn't found
23166 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23167 to something that has been emitted in the current CU. */
23169 static void
23170 resolve_addr (dw_die_ref die)
23172 dw_die_ref c;
23173 dw_attr_ref a;
23174 dw_loc_list_ref *curr, *start, loc;
23175 unsigned ix;
23177 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23178 switch (AT_class (a))
23180 case dw_val_class_loc_list:
23181 start = curr = AT_loc_list_ptr (a);
23182 loc = *curr;
23183 gcc_assert (loc);
23184 /* The same list can be referenced more than once. See if we have
23185 already recorded the result from a previous pass. */
23186 if (loc->replaced)
23187 *curr = loc->dw_loc_next;
23188 else if (!loc->resolved_addr)
23190 /* As things stand, we do not expect or allow one die to
23191 reference a suffix of another die's location list chain.
23192 References must be identical or completely separate.
23193 There is therefore no need to cache the result of this
23194 pass on any list other than the first; doing so
23195 would lead to unnecessary writes. */
23196 while (*curr)
23198 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23199 if (!resolve_addr_in_expr ((*curr)->expr))
23201 dw_loc_list_ref next = (*curr)->dw_loc_next;
23202 dw_loc_descr_ref l = (*curr)->expr;
23204 if (next && (*curr)->ll_symbol)
23206 gcc_assert (!next->ll_symbol);
23207 next->ll_symbol = (*curr)->ll_symbol;
23209 if (dwarf_split_debug_info)
23210 remove_loc_list_addr_table_entries (l);
23211 *curr = next;
23213 else
23215 mark_base_types ((*curr)->expr);
23216 curr = &(*curr)->dw_loc_next;
23219 if (loc == *start)
23220 loc->resolved_addr = 1;
23221 else
23223 loc->replaced = 1;
23224 loc->dw_loc_next = *start;
23227 if (!*start)
23229 remove_AT (die, a->dw_attr);
23230 ix--;
23232 break;
23233 case dw_val_class_loc:
23235 dw_loc_descr_ref l = AT_loc (a);
23236 /* For -gdwarf-2 don't attempt to optimize
23237 DW_AT_data_member_location containing
23238 DW_OP_plus_uconst - older consumers might
23239 rely on it being that op instead of a more complex,
23240 but shorter, location description. */
23241 if ((dwarf_version > 2
23242 || a->dw_attr != DW_AT_data_member_location
23243 || l == NULL
23244 || l->dw_loc_opc != DW_OP_plus_uconst
23245 || l->dw_loc_next != NULL)
23246 && !resolve_addr_in_expr (l))
23248 if (dwarf_split_debug_info)
23249 remove_loc_list_addr_table_entries (l);
23250 if (l != NULL
23251 && l->dw_loc_next == NULL
23252 && l->dw_loc_opc == DW_OP_addr
23253 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23254 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23255 && a->dw_attr == DW_AT_location)
23257 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23258 remove_AT (die, a->dw_attr);
23259 ix--;
23260 optimize_location_into_implicit_ptr (die, decl);
23261 break;
23263 remove_AT (die, a->dw_attr);
23264 ix--;
23266 else
23267 mark_base_types (l);
23269 break;
23270 case dw_val_class_addr:
23271 if (a->dw_attr == DW_AT_const_value
23272 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23274 if (AT_index (a) != NOT_INDEXED)
23275 remove_addr_table_entry (a->dw_attr_val.val_entry);
23276 remove_AT (die, a->dw_attr);
23277 ix--;
23279 if (die->die_tag == DW_TAG_GNU_call_site
23280 && a->dw_attr == DW_AT_abstract_origin)
23282 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23283 dw_die_ref tdie = lookup_decl_die (tdecl);
23284 if (tdie == NULL
23285 && DECL_EXTERNAL (tdecl)
23286 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23288 force_decl_die (tdecl);
23289 tdie = lookup_decl_die (tdecl);
23291 if (tdie)
23293 a->dw_attr_val.val_class = dw_val_class_die_ref;
23294 a->dw_attr_val.v.val_die_ref.die = tdie;
23295 a->dw_attr_val.v.val_die_ref.external = 0;
23297 else
23299 if (AT_index (a) != NOT_INDEXED)
23300 remove_addr_table_entry (a->dw_attr_val.val_entry);
23301 remove_AT (die, a->dw_attr);
23302 ix--;
23305 break;
23306 default:
23307 break;
23310 FOR_EACH_CHILD (die, c, resolve_addr (c));
23313 /* Helper routines for optimize_location_lists.
23314 This pass tries to share identical local lists in .debug_loc
23315 section. */
23317 /* Iteratively hash operands of LOC opcode. */
23319 static hashval_t
23320 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23322 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23323 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23325 switch (loc->dw_loc_opc)
23327 case DW_OP_const4u:
23328 case DW_OP_const8u:
23329 if (loc->dtprel)
23330 goto hash_addr;
23331 /* FALLTHRU */
23332 case DW_OP_const1u:
23333 case DW_OP_const1s:
23334 case DW_OP_const2u:
23335 case DW_OP_const2s:
23336 case DW_OP_const4s:
23337 case DW_OP_const8s:
23338 case DW_OP_constu:
23339 case DW_OP_consts:
23340 case DW_OP_pick:
23341 case DW_OP_plus_uconst:
23342 case DW_OP_breg0:
23343 case DW_OP_breg1:
23344 case DW_OP_breg2:
23345 case DW_OP_breg3:
23346 case DW_OP_breg4:
23347 case DW_OP_breg5:
23348 case DW_OP_breg6:
23349 case DW_OP_breg7:
23350 case DW_OP_breg8:
23351 case DW_OP_breg9:
23352 case DW_OP_breg10:
23353 case DW_OP_breg11:
23354 case DW_OP_breg12:
23355 case DW_OP_breg13:
23356 case DW_OP_breg14:
23357 case DW_OP_breg15:
23358 case DW_OP_breg16:
23359 case DW_OP_breg17:
23360 case DW_OP_breg18:
23361 case DW_OP_breg19:
23362 case DW_OP_breg20:
23363 case DW_OP_breg21:
23364 case DW_OP_breg22:
23365 case DW_OP_breg23:
23366 case DW_OP_breg24:
23367 case DW_OP_breg25:
23368 case DW_OP_breg26:
23369 case DW_OP_breg27:
23370 case DW_OP_breg28:
23371 case DW_OP_breg29:
23372 case DW_OP_breg30:
23373 case DW_OP_breg31:
23374 case DW_OP_regx:
23375 case DW_OP_fbreg:
23376 case DW_OP_piece:
23377 case DW_OP_deref_size:
23378 case DW_OP_xderef_size:
23379 hash = iterative_hash_object (val1->v.val_int, hash);
23380 break;
23381 case DW_OP_skip:
23382 case DW_OP_bra:
23384 int offset;
23386 gcc_assert (val1->val_class == dw_val_class_loc);
23387 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23388 hash = iterative_hash_object (offset, hash);
23390 break;
23391 case DW_OP_implicit_value:
23392 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23393 switch (val2->val_class)
23395 case dw_val_class_const:
23396 hash = iterative_hash_object (val2->v.val_int, hash);
23397 break;
23398 case dw_val_class_vec:
23400 unsigned int elt_size = val2->v.val_vec.elt_size;
23401 unsigned int len = val2->v.val_vec.length;
23403 hash = iterative_hash_object (elt_size, hash);
23404 hash = iterative_hash_object (len, hash);
23405 hash = iterative_hash (val2->v.val_vec.array,
23406 len * elt_size, hash);
23408 break;
23409 case dw_val_class_const_double:
23410 hash = iterative_hash_object (val2->v.val_double.low, hash);
23411 hash = iterative_hash_object (val2->v.val_double.high, hash);
23412 break;
23413 case dw_val_class_addr:
23414 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23415 break;
23416 default:
23417 gcc_unreachable ();
23419 break;
23420 case DW_OP_bregx:
23421 case DW_OP_bit_piece:
23422 hash = iterative_hash_object (val1->v.val_int, hash);
23423 hash = iterative_hash_object (val2->v.val_int, hash);
23424 break;
23425 case DW_OP_addr:
23426 hash_addr:
23427 if (loc->dtprel)
23429 unsigned char dtprel = 0xd1;
23430 hash = iterative_hash_object (dtprel, hash);
23432 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23433 break;
23434 case DW_OP_GNU_addr_index:
23435 case DW_OP_GNU_const_index:
23437 if (loc->dtprel)
23439 unsigned char dtprel = 0xd1;
23440 hash = iterative_hash_object (dtprel, hash);
23442 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23444 break;
23445 case DW_OP_GNU_implicit_pointer:
23446 hash = iterative_hash_object (val2->v.val_int, hash);
23447 break;
23448 case DW_OP_GNU_entry_value:
23449 hash = hash_loc_operands (val1->v.val_loc, hash);
23450 break;
23451 case DW_OP_GNU_regval_type:
23452 case DW_OP_GNU_deref_type:
23454 unsigned int byte_size
23455 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23456 unsigned int encoding
23457 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23458 hash = iterative_hash_object (val1->v.val_int, hash);
23459 hash = iterative_hash_object (byte_size, hash);
23460 hash = iterative_hash_object (encoding, hash);
23462 break;
23463 case DW_OP_GNU_convert:
23464 case DW_OP_GNU_reinterpret:
23465 if (val1->val_class == dw_val_class_unsigned_const)
23467 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23468 break;
23470 /* FALLTHRU */
23471 case DW_OP_GNU_const_type:
23473 unsigned int byte_size
23474 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23475 unsigned int encoding
23476 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23477 hash = iterative_hash_object (byte_size, hash);
23478 hash = iterative_hash_object (encoding, hash);
23479 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23480 break;
23481 hash = iterative_hash_object (val2->val_class, hash);
23482 switch (val2->val_class)
23484 case dw_val_class_const:
23485 hash = iterative_hash_object (val2->v.val_int, hash);
23486 break;
23487 case dw_val_class_vec:
23489 unsigned int elt_size = val2->v.val_vec.elt_size;
23490 unsigned int len = val2->v.val_vec.length;
23492 hash = iterative_hash_object (elt_size, hash);
23493 hash = iterative_hash_object (len, hash);
23494 hash = iterative_hash (val2->v.val_vec.array,
23495 len * elt_size, hash);
23497 break;
23498 case dw_val_class_const_double:
23499 hash = iterative_hash_object (val2->v.val_double.low, hash);
23500 hash = iterative_hash_object (val2->v.val_double.high, hash);
23501 break;
23502 default:
23503 gcc_unreachable ();
23506 break;
23508 default:
23509 /* Other codes have no operands. */
23510 break;
23512 return hash;
23515 /* Iteratively hash the whole DWARF location expression LOC. */
23517 static inline hashval_t
23518 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23520 dw_loc_descr_ref l;
23521 bool sizes_computed = false;
23522 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23523 size_of_locs (loc);
23525 for (l = loc; l != NULL; l = l->dw_loc_next)
23527 enum dwarf_location_atom opc = l->dw_loc_opc;
23528 hash = iterative_hash_object (opc, hash);
23529 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23531 size_of_locs (loc);
23532 sizes_computed = true;
23534 hash = hash_loc_operands (l, hash);
23536 return hash;
23539 /* Compute hash of the whole location list LIST_HEAD. */
23541 static inline void
23542 hash_loc_list (dw_loc_list_ref list_head)
23544 dw_loc_list_ref curr = list_head;
23545 hashval_t hash = 0;
23547 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23549 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23550 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23551 if (curr->section)
23552 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23553 hash);
23554 hash = hash_locs (curr->expr, hash);
23556 list_head->hash = hash;
23559 /* Return true if X and Y opcodes have the same operands. */
23561 static inline bool
23562 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23564 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23565 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23566 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23567 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23569 switch (x->dw_loc_opc)
23571 case DW_OP_const4u:
23572 case DW_OP_const8u:
23573 if (x->dtprel)
23574 goto hash_addr;
23575 /* FALLTHRU */
23576 case DW_OP_const1u:
23577 case DW_OP_const1s:
23578 case DW_OP_const2u:
23579 case DW_OP_const2s:
23580 case DW_OP_const4s:
23581 case DW_OP_const8s:
23582 case DW_OP_constu:
23583 case DW_OP_consts:
23584 case DW_OP_pick:
23585 case DW_OP_plus_uconst:
23586 case DW_OP_breg0:
23587 case DW_OP_breg1:
23588 case DW_OP_breg2:
23589 case DW_OP_breg3:
23590 case DW_OP_breg4:
23591 case DW_OP_breg5:
23592 case DW_OP_breg6:
23593 case DW_OP_breg7:
23594 case DW_OP_breg8:
23595 case DW_OP_breg9:
23596 case DW_OP_breg10:
23597 case DW_OP_breg11:
23598 case DW_OP_breg12:
23599 case DW_OP_breg13:
23600 case DW_OP_breg14:
23601 case DW_OP_breg15:
23602 case DW_OP_breg16:
23603 case DW_OP_breg17:
23604 case DW_OP_breg18:
23605 case DW_OP_breg19:
23606 case DW_OP_breg20:
23607 case DW_OP_breg21:
23608 case DW_OP_breg22:
23609 case DW_OP_breg23:
23610 case DW_OP_breg24:
23611 case DW_OP_breg25:
23612 case DW_OP_breg26:
23613 case DW_OP_breg27:
23614 case DW_OP_breg28:
23615 case DW_OP_breg29:
23616 case DW_OP_breg30:
23617 case DW_OP_breg31:
23618 case DW_OP_regx:
23619 case DW_OP_fbreg:
23620 case DW_OP_piece:
23621 case DW_OP_deref_size:
23622 case DW_OP_xderef_size:
23623 return valx1->v.val_int == valy1->v.val_int;
23624 case DW_OP_skip:
23625 case DW_OP_bra:
23626 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23627 can cause irrelevant differences in dw_loc_addr. */
23628 gcc_assert (valx1->val_class == dw_val_class_loc
23629 && valy1->val_class == dw_val_class_loc
23630 && (dwarf_split_debug_info
23631 || x->dw_loc_addr == y->dw_loc_addr));
23632 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23633 case DW_OP_implicit_value:
23634 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23635 || valx2->val_class != valy2->val_class)
23636 return false;
23637 switch (valx2->val_class)
23639 case dw_val_class_const:
23640 return valx2->v.val_int == valy2->v.val_int;
23641 case dw_val_class_vec:
23642 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23643 && valx2->v.val_vec.length == valy2->v.val_vec.length
23644 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23645 valx2->v.val_vec.elt_size
23646 * valx2->v.val_vec.length) == 0;
23647 case dw_val_class_const_double:
23648 return valx2->v.val_double.low == valy2->v.val_double.low
23649 && valx2->v.val_double.high == valy2->v.val_double.high;
23650 case dw_val_class_addr:
23651 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23652 default:
23653 gcc_unreachable ();
23655 case DW_OP_bregx:
23656 case DW_OP_bit_piece:
23657 return valx1->v.val_int == valy1->v.val_int
23658 && valx2->v.val_int == valy2->v.val_int;
23659 case DW_OP_addr:
23660 hash_addr:
23661 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23662 case DW_OP_GNU_addr_index:
23663 case DW_OP_GNU_const_index:
23665 rtx ax1 = valx1->val_entry->addr.rtl;
23666 rtx ay1 = valy1->val_entry->addr.rtl;
23667 return rtx_equal_p (ax1, ay1);
23669 case DW_OP_GNU_implicit_pointer:
23670 return valx1->val_class == dw_val_class_die_ref
23671 && valx1->val_class == valy1->val_class
23672 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23673 && valx2->v.val_int == valy2->v.val_int;
23674 case DW_OP_GNU_entry_value:
23675 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23676 case DW_OP_GNU_const_type:
23677 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23678 || valx2->val_class != valy2->val_class)
23679 return false;
23680 switch (valx2->val_class)
23682 case dw_val_class_const:
23683 return valx2->v.val_int == valy2->v.val_int;
23684 case dw_val_class_vec:
23685 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23686 && valx2->v.val_vec.length == valy2->v.val_vec.length
23687 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23688 valx2->v.val_vec.elt_size
23689 * valx2->v.val_vec.length) == 0;
23690 case dw_val_class_const_double:
23691 return valx2->v.val_double.low == valy2->v.val_double.low
23692 && valx2->v.val_double.high == valy2->v.val_double.high;
23693 default:
23694 gcc_unreachable ();
23696 case DW_OP_GNU_regval_type:
23697 case DW_OP_GNU_deref_type:
23698 return valx1->v.val_int == valy1->v.val_int
23699 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23700 case DW_OP_GNU_convert:
23701 case DW_OP_GNU_reinterpret:
23702 if (valx1->val_class != valy1->val_class)
23703 return false;
23704 if (valx1->val_class == dw_val_class_unsigned_const)
23705 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23706 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23707 case DW_OP_GNU_parameter_ref:
23708 return valx1->val_class == dw_val_class_die_ref
23709 && valx1->val_class == valy1->val_class
23710 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23711 default:
23712 /* Other codes have no operands. */
23713 return true;
23717 /* Return true if DWARF location expressions X and Y are the same. */
23719 static inline bool
23720 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23722 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23723 if (x->dw_loc_opc != y->dw_loc_opc
23724 || x->dtprel != y->dtprel
23725 || !compare_loc_operands (x, y))
23726 break;
23727 return x == NULL && y == NULL;
23730 /* Hashtable helpers. */
23732 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23734 typedef dw_loc_list_struct value_type;
23735 typedef dw_loc_list_struct compare_type;
23736 static inline hashval_t hash (const value_type *);
23737 static inline bool equal (const value_type *, const compare_type *);
23740 /* Return precomputed hash of location list X. */
23742 inline hashval_t
23743 loc_list_hasher::hash (const value_type *x)
23745 return x->hash;
23748 /* Return true if location lists A and B are the same. */
23750 inline bool
23751 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23753 if (a == b)
23754 return 1;
23755 if (a->hash != b->hash)
23756 return 0;
23757 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23758 if (strcmp (a->begin, b->begin) != 0
23759 || strcmp (a->end, b->end) != 0
23760 || (a->section == NULL) != (b->section == NULL)
23761 || (a->section && strcmp (a->section, b->section) != 0)
23762 || !compare_locs (a->expr, b->expr))
23763 break;
23764 return a == NULL && b == NULL;
23767 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23770 /* Recursively optimize location lists referenced from DIE
23771 children and share them whenever possible. */
23773 static void
23774 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23776 dw_die_ref c;
23777 dw_attr_ref a;
23778 unsigned ix;
23779 dw_loc_list_struct **slot;
23781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23782 if (AT_class (a) == dw_val_class_loc_list)
23784 dw_loc_list_ref list = AT_loc_list (a);
23785 /* TODO: perform some optimizations here, before hashing
23786 it and storing into the hash table. */
23787 hash_loc_list (list);
23788 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23789 if (*slot == NULL)
23790 *slot = list;
23791 else
23792 a->dw_attr_val.v.val_loc_list = *slot;
23795 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23799 /* Recursively assign each location list a unique index into the debug_addr
23800 section. */
23802 static void
23803 index_location_lists (dw_die_ref die)
23805 dw_die_ref c;
23806 dw_attr_ref a;
23807 unsigned ix;
23809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23810 if (AT_class (a) == dw_val_class_loc_list)
23812 dw_loc_list_ref list = AT_loc_list (a);
23813 dw_loc_list_ref curr;
23814 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23816 /* Don't index an entry that has already been indexed
23817 or won't be output. */
23818 if (curr->begin_entry != NULL
23819 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23820 continue;
23822 curr->begin_entry
23823 = add_addr_table_entry (xstrdup (curr->begin),
23824 ate_kind_label);
23828 FOR_EACH_CHILD (die, c, index_location_lists (c));
23831 /* Optimize location lists referenced from DIE
23832 children and share them whenever possible. */
23834 static void
23835 optimize_location_lists (dw_die_ref die)
23837 loc_list_hash_type htab;
23838 htab.create (500);
23839 optimize_location_lists_1 (die, htab);
23840 htab.dispose ();
23843 /* Output stuff that dwarf requires at the end of every file,
23844 and generate the DWARF-2 debugging info. */
23846 static void
23847 dwarf2out_finish (const char *filename)
23849 limbo_die_node *node, *next_node;
23850 comdat_type_node *ctnode;
23851 hash_table <comdat_type_hasher> comdat_type_table;
23852 unsigned int i;
23853 dw_die_ref main_comp_unit_die;
23855 /* PCH might result in DW_AT_producer string being restored from the
23856 header compilation, so always fill it with empty string initially
23857 and overwrite only here. */
23858 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23859 producer_string = gen_producer_string ();
23860 producer->dw_attr_val.v.val_str->refcount--;
23861 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23863 gen_scheduled_generic_parms_dies ();
23864 gen_remaining_tmpl_value_param_die_attribute ();
23866 /* Add the name for the main input file now. We delayed this from
23867 dwarf2out_init to avoid complications with PCH. */
23868 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23869 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23870 add_comp_dir_attribute (comp_unit_die ());
23871 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23873 bool p = false;
23874 htab_traverse (file_table, file_table_relative_p, &p);
23875 if (p)
23876 add_comp_dir_attribute (comp_unit_die ());
23879 if (deferred_locations_list)
23880 for (i = 0; i < deferred_locations_list->length (); i++)
23882 add_location_or_const_value_attribute (
23883 (*deferred_locations_list)[i].die,
23884 (*deferred_locations_list)[i].variable,
23885 false,
23886 DW_AT_location);
23889 /* Traverse the limbo die list, and add parent/child links. The only
23890 dies without parents that should be here are concrete instances of
23891 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23892 For concrete instances, we can get the parent die from the abstract
23893 instance. */
23894 for (node = limbo_die_list; node; node = next_node)
23896 dw_die_ref die = node->die;
23897 next_node = node->next;
23899 if (die->die_parent == NULL)
23901 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23903 if (origin && origin->die_parent)
23904 add_child_die (origin->die_parent, die);
23905 else if (is_cu_die (die))
23907 else if (seen_error ())
23908 /* It's OK to be confused by errors in the input. */
23909 add_child_die (comp_unit_die (), die);
23910 else
23912 /* In certain situations, the lexical block containing a
23913 nested function can be optimized away, which results
23914 in the nested function die being orphaned. Likewise
23915 with the return type of that nested function. Force
23916 this to be a child of the containing function.
23918 It may happen that even the containing function got fully
23919 inlined and optimized out. In that case we are lost and
23920 assign the empty child. This should not be big issue as
23921 the function is likely unreachable too. */
23922 gcc_assert (node->created_for);
23924 if (DECL_P (node->created_for))
23925 origin = get_context_die (DECL_CONTEXT (node->created_for));
23926 else if (TYPE_P (node->created_for))
23927 origin = scope_die_for (node->created_for, comp_unit_die ());
23928 else
23929 origin = comp_unit_die ();
23931 add_child_die (origin, die);
23936 limbo_die_list = NULL;
23938 #if ENABLE_ASSERT_CHECKING
23940 dw_die_ref die = comp_unit_die (), c;
23941 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23943 #endif
23944 resolve_addr (comp_unit_die ());
23945 move_marked_base_types ();
23947 for (node = deferred_asm_name; node; node = node->next)
23949 tree decl = node->created_for;
23950 /* When generating LTO bytecode we can not generate new assembler
23951 names at this point and all important decls got theirs via
23952 free-lang-data. */
23953 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23954 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23956 add_linkage_attr (node->die, decl);
23957 move_linkage_attr (node->die);
23961 deferred_asm_name = NULL;
23963 /* Walk through the list of incomplete types again, trying once more to
23964 emit full debugging info for them. */
23965 retry_incomplete_types ();
23967 if (flag_eliminate_unused_debug_types)
23968 prune_unused_types ();
23970 /* Generate separate COMDAT sections for type DIEs. */
23971 if (use_debug_types)
23973 break_out_comdat_types (comp_unit_die ());
23975 /* Each new type_unit DIE was added to the limbo die list when created.
23976 Since these have all been added to comdat_type_list, clear the
23977 limbo die list. */
23978 limbo_die_list = NULL;
23980 /* For each new comdat type unit, copy declarations for incomplete
23981 types to make the new unit self-contained (i.e., no direct
23982 references to the main compile unit). */
23983 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23984 copy_decls_for_unworthy_types (ctnode->root_die);
23985 copy_decls_for_unworthy_types (comp_unit_die ());
23987 /* In the process of copying declarations from one unit to another,
23988 we may have left some declarations behind that are no longer
23989 referenced. Prune them. */
23990 prune_unused_types ();
23993 /* Generate separate CUs for each of the include files we've seen.
23994 They will go into limbo_die_list. */
23995 if (flag_eliminate_dwarf2_dups)
23996 break_out_includes (comp_unit_die ());
23998 /* Traverse the DIE's and add add sibling attributes to those DIE's
23999 that have children. */
24000 add_sibling_attributes (comp_unit_die ());
24001 for (node = limbo_die_list; node; node = node->next)
24002 add_sibling_attributes (node->die);
24003 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24004 add_sibling_attributes (ctnode->root_die);
24006 /* When splitting DWARF info, we put some attributes in the
24007 skeleton compile_unit DIE that remains in the .o, while
24008 most attributes go in the DWO compile_unit_die. */
24009 if (dwarf_split_debug_info)
24010 main_comp_unit_die = gen_compile_unit_die (NULL);
24011 else
24012 main_comp_unit_die = comp_unit_die ();
24014 /* Output a terminator label for the .text section. */
24015 switch_to_section (text_section);
24016 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24017 if (cold_text_section)
24019 switch_to_section (cold_text_section);
24020 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24023 /* We can only use the low/high_pc attributes if all of the code was
24024 in .text. */
24025 if (!have_multiple_function_sections
24026 || (dwarf_version < 3 && dwarf_strict))
24028 /* Don't add if the CU has no associated code. */
24029 if (text_section_used)
24030 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24031 text_end_label, true);
24033 else
24035 unsigned fde_idx;
24036 dw_fde_ref fde;
24037 bool range_list_added = false;
24039 if (text_section_used)
24040 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24041 text_end_label, &range_list_added, true);
24042 if (cold_text_section_used)
24043 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24044 cold_end_label, &range_list_added, true);
24046 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24048 if (DECL_IGNORED_P (fde->decl))
24049 continue;
24050 if (!fde->in_std_section)
24051 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24052 fde->dw_fde_end, &range_list_added,
24053 true);
24054 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24055 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24056 fde->dw_fde_second_end, &range_list_added,
24057 true);
24060 if (range_list_added)
24062 /* We need to give .debug_loc and .debug_ranges an appropriate
24063 "base address". Use zero so that these addresses become
24064 absolute. Historically, we've emitted the unexpected
24065 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24066 Emit both to give time for other tools to adapt. */
24067 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24068 if (! dwarf_strict && dwarf_version < 4)
24069 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24071 add_ranges (NULL);
24075 if (debug_info_level >= DINFO_LEVEL_TERSE)
24076 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24077 debug_line_section_label);
24079 if (have_macinfo)
24080 add_AT_macptr (comp_unit_die (),
24081 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24082 macinfo_section_label);
24084 if (dwarf_split_debug_info && addr_index_table != NULL)
24086 /* optimize_location_lists calculates the size of the lists,
24087 so index them first, and assign indices to the entries.
24088 Although optimize_location_lists will remove entries from
24089 the table, it only does so for duplicates, and therefore
24090 only reduces ref_counts to 1. */
24091 unsigned int index = 0;
24092 index_location_lists (comp_unit_die ());
24093 htab_traverse_noresize (addr_index_table,
24094 index_addr_table_entry, &index);
24096 if (have_location_lists)
24097 optimize_location_lists (comp_unit_die ());
24099 save_macinfo_strings ();
24101 if (dwarf_split_debug_info)
24103 unsigned int index = 0;
24105 /* Add attributes common to skeleton compile_units and
24106 type_units. Because these attributes include strings, it
24107 must be done before freezing the string table. Top-level
24108 skeleton die attrs are added when the skeleton type unit is
24109 created, so ensure it is created by this point. */
24110 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24111 (void) get_skeleton_type_unit ();
24112 htab_traverse_noresize (debug_str_hash, index_string, &index);
24115 /* Output all of the compilation units. We put the main one last so that
24116 the offsets are available to output_pubnames. */
24117 for (node = limbo_die_list; node; node = node->next)
24118 output_comp_unit (node->die, 0);
24120 comdat_type_table.create (100);
24121 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24123 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24125 /* Don't output duplicate types. */
24126 if (*slot != HTAB_EMPTY_ENTRY)
24127 continue;
24129 /* Add a pointer to the line table for the main compilation unit
24130 so that the debugger can make sense of DW_AT_decl_file
24131 attributes. */
24132 if (debug_info_level >= DINFO_LEVEL_TERSE)
24133 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24134 (!dwarf_split_debug_info
24135 ? debug_line_section_label
24136 : debug_skeleton_line_section_label));
24138 output_comdat_type_unit (ctnode);
24139 *slot = ctnode;
24141 comdat_type_table.dispose ();
24143 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24144 both the main_cu and all skeleton TUs. Making this call unconditional
24145 would end up either adding a second copy of the AT_pubnames attribute, or
24146 requiring a special case in add_top_level_skeleton_die_attrs. */
24147 if (!dwarf_split_debug_info)
24148 add_AT_pubnames (comp_unit_die ());
24150 if (dwarf_split_debug_info)
24152 int mark;
24153 unsigned char checksum[16];
24154 struct md5_ctx ctx;
24156 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24157 md5_init_ctx (&ctx);
24158 mark = 0;
24159 die_checksum (comp_unit_die (), &ctx, &mark);
24160 unmark_all_dies (comp_unit_die ());
24161 md5_finish_ctx (&ctx, checksum);
24163 /* Use the first 8 bytes of the checksum as the dwo_id,
24164 and add it to both comp-unit DIEs. */
24165 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24166 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24168 /* Add the base offset of the ranges table to the skeleton
24169 comp-unit DIE. */
24170 if (ranges_table_in_use)
24171 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24172 ranges_section_label);
24174 switch_to_section (debug_addr_section);
24175 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24176 output_addr_table ();
24179 /* Output the main compilation unit if non-empty or if .debug_macinfo
24180 or .debug_macro will be emitted. */
24181 output_comp_unit (comp_unit_die (), have_macinfo);
24183 if (dwarf_split_debug_info && info_section_emitted)
24184 output_skeleton_debug_sections (main_comp_unit_die);
24186 /* Output the abbreviation table. */
24187 if (abbrev_die_table_in_use != 1)
24189 switch_to_section (debug_abbrev_section);
24190 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24191 output_abbrev_section ();
24194 /* Output location list section if necessary. */
24195 if (have_location_lists)
24197 /* Output the location lists info. */
24198 switch_to_section (debug_loc_section);
24199 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24200 output_location_lists (comp_unit_die ());
24203 output_pubtables ();
24205 /* Output the address range information if a CU (.debug_info section)
24206 was emitted. We output an empty table even if we had no functions
24207 to put in it. This because the consumer has no way to tell the
24208 difference between an empty table that we omitted and failure to
24209 generate a table that would have contained data. */
24210 if (info_section_emitted)
24212 unsigned long aranges_length = size_of_aranges ();
24214 switch_to_section (debug_aranges_section);
24215 output_aranges (aranges_length);
24218 /* Output ranges section if necessary. */
24219 if (ranges_table_in_use)
24221 switch_to_section (debug_ranges_section);
24222 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24223 output_ranges ();
24226 /* Have to end the macro section. */
24227 if (have_macinfo)
24229 switch_to_section (debug_macinfo_section);
24230 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24231 output_macinfo ();
24232 dw2_asm_output_data (1, 0, "End compilation unit");
24235 /* Output the source line correspondence table. We must do this
24236 even if there is no line information. Otherwise, on an empty
24237 translation unit, we will generate a present, but empty,
24238 .debug_info section. IRIX 6.5 `nm' will then complain when
24239 examining the file. This is done late so that any filenames
24240 used by the debug_info section are marked as 'used'. */
24241 switch_to_section (debug_line_section);
24242 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24243 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24244 output_line_info (false);
24246 if (dwarf_split_debug_info && info_section_emitted)
24248 switch_to_section (debug_skeleton_line_section);
24249 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24250 output_line_info (true);
24253 /* If we emitted any indirect strings, output the string table too. */
24254 if (debug_str_hash || skeleton_debug_str_hash)
24255 output_indirect_strings ();
24258 #include "gt-dwarf2out.h"