2014-01-24 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / dwarf2out.c
blob22282d8b1cac1ba795a674ef09783bfbf3233eb9
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "tree-dfa.h"
99 #include "gdb/gdb-index.h"
101 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
102 static rtx last_var_location_insn;
103 static rtx cached_next_real_insn;
105 #ifdef VMS_DEBUGGING_INFO
106 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
108 /* Define this macro to be a nonzero value if the directory specifications
109 which are output in the debug info should end with a separator. */
110 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
111 /* Define this macro to evaluate to a nonzero value if GCC should refrain
112 from generating indirect strings in DWARF2 debug information, for instance
113 if your target is stuck with an old version of GDB that is unable to
114 process them properly or uses VMS Debug. */
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
116 #else
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
119 #endif
121 /* ??? Poison these here until it can be done generically. They've been
122 totally replaced in this file; make sure it stays that way. */
123 #undef DWARF2_UNWIND_INFO
124 #undef DWARF2_FRAME_INFO
125 #if (GCC_VERSION >= 3000)
126 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
127 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 vec<tree, va_gc> *because we want to tell the garbage collector about
141 it. */
142 static GTY(()) vec<tree, va_gc> *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) vec<tree, va_gc> *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_skeleton_info_section;
154 static GTY(()) section *debug_abbrev_section;
155 static GTY(()) section *debug_skeleton_abbrev_section;
156 static GTY(()) section *debug_aranges_section;
157 static GTY(()) section *debug_addr_section;
158 static GTY(()) section *debug_macinfo_section;
159 static GTY(()) section *debug_line_section;
160 static GTY(()) section *debug_skeleton_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_str_dwo_section;
166 static GTY(()) section *debug_str_offsets_section;
167 static GTY(()) section *debug_ranges_section;
168 static GTY(()) section *debug_frame_section;
170 /* Maximum size (in bytes) of an artificially generated label. */
171 #define MAX_ARTIFICIAL_LABEL_BYTES 30
173 /* According to the (draft) DWARF 3 specification, the initial length
174 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
175 bytes are 0xffffffff, followed by the length stored in the next 8
176 bytes.
178 However, the SGI/MIPS ABI uses an initial length which is equal to
179 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
181 #ifndef DWARF_INITIAL_LENGTH_SIZE
182 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
183 #endif
185 /* Round SIZE up to the nearest BOUNDARY. */
186 #define DWARF_ROUND(SIZE,BOUNDARY) \
187 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
189 /* CIE identifier. */
190 #if HOST_BITS_PER_WIDE_INT >= 64
191 #define DWARF_CIE_ID \
192 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
193 #else
194 #define DWARF_CIE_ID DW_CIE_ID
195 #endif
198 /* A vector for a table that contains frame description
199 information for each routine. */
200 #define NOT_INDEXED (-1U)
201 #define NO_INDEX_ASSIGNED (-2U)
203 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
205 struct GTY(()) indirect_string_node {
206 const char *str;
207 unsigned int refcount;
208 enum dwarf_form form;
209 char *label;
210 unsigned int index;
213 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
215 /* With split_debug_info, both the comp_dir and dwo_name go in the
216 main object file, rather than the dwo, similar to the force_direct
217 parameter elsewhere but with additional complications:
219 1) The string is needed in both the main object file and the dwo.
220 That is, the comp_dir and dwo_name will appear in both places.
222 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
223 DW_FORM_GNU_str_index.
225 3) GCC chooses the form to use late, depending on the size and
226 reference count.
228 Rather than forcing the all debug string handling functions and
229 callers to deal with these complications, simply use a separate,
230 special-cased string table for any attribute that should go in the
231 main object file. This limits the complexity to just the places
232 that need it. */
234 static GTY ((param_is (struct indirect_string_node)))
235 htab_t skeleton_debug_str_hash;
237 static GTY(()) int dw2_string_counter;
239 /* True if the compilation unit places functions in more than one section. */
240 static GTY(()) bool have_multiple_function_sections = false;
242 /* Whether the default text and cold text sections have been used at all. */
244 static GTY(()) bool text_section_used = false;
245 static GTY(()) bool cold_text_section_used = false;
247 /* The default cold text section. */
248 static GTY(()) section *cold_text_section;
250 /* 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_node *
2312 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2314 struct dw_loc_descr_node *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_node *
2342 build_cfa_aligned_loc (dw_cfa_location *cfa,
2343 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2345 struct dw_loc_descr_node *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_namelist_decl (tree, dw_die_ref, tree);
3189 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3190 static dw_die_ref force_decl_die (tree);
3191 static dw_die_ref force_type_die (tree);
3192 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3193 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3194 static struct dwarf_file_data * lookup_filename (const char *);
3195 static void retry_incomplete_types (void);
3196 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3197 static void gen_generic_params_dies (tree);
3198 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3199 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3200 static void splice_child_die (dw_die_ref, dw_die_ref);
3201 static int file_info_cmp (const void *, const void *);
3202 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3203 const char *, const char *);
3204 static void output_loc_list (dw_loc_list_ref);
3205 static char *gen_internal_sym (const char *);
3206 static bool want_pubnames (void);
3208 static void prune_unmark_dies (dw_die_ref);
3209 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3210 static void prune_unused_types_mark (dw_die_ref, int);
3211 static void prune_unused_types_walk (dw_die_ref);
3212 static void prune_unused_types_walk_attribs (dw_die_ref);
3213 static void prune_unused_types_prune (dw_die_ref);
3214 static void prune_unused_types (void);
3215 static int maybe_emit_file (struct dwarf_file_data *fd);
3216 static inline const char *AT_vms_delta1 (dw_attr_ref);
3217 static inline const char *AT_vms_delta2 (dw_attr_ref);
3218 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3219 const char *, const char *);
3220 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3221 static void gen_remaining_tmpl_value_param_die_attribute (void);
3222 static bool generic_type_p (tree);
3223 static void schedule_generic_params_dies_gen (tree t);
3224 static void gen_scheduled_generic_parms_dies (void);
3226 static const char *comp_dir_string (void);
3228 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3230 /* enum for tracking thread-local variables whose address is really an offset
3231 relative to the TLS pointer, which will need link-time relocation, but will
3232 not need relocation by the DWARF consumer. */
3234 enum dtprel_bool
3236 dtprel_false = 0,
3237 dtprel_true = 1
3240 /* Return the operator to use for an address of a variable. For dtprel_true, we
3241 use DW_OP_const*. For regular variables, which need both link-time
3242 relocation and consumer-level relocation (e.g., to account for shared objects
3243 loaded at a random address), we use DW_OP_addr*. */
3245 static inline enum dwarf_location_atom
3246 dw_addr_op (enum dtprel_bool dtprel)
3248 if (dtprel == dtprel_true)
3249 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3250 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3251 else
3252 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3255 /* Return a pointer to a newly allocated address location description. If
3256 dwarf_split_debug_info is true, then record the address with the appropriate
3257 relocation. */
3258 static inline dw_loc_descr_ref
3259 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3261 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3263 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3264 ref->dw_loc_oprnd1.v.val_addr = addr;
3265 ref->dtprel = dtprel;
3266 if (dwarf_split_debug_info)
3267 ref->dw_loc_oprnd1.val_entry
3268 = add_addr_table_entry (addr,
3269 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3270 else
3271 ref->dw_loc_oprnd1.val_entry = NULL;
3273 return ref;
3276 /* Section names used to hold DWARF debugging information. */
3278 #ifndef DEBUG_INFO_SECTION
3279 #define DEBUG_INFO_SECTION ".debug_info"
3280 #endif
3281 #ifndef DEBUG_DWO_INFO_SECTION
3282 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3283 #endif
3284 #ifndef DEBUG_ABBREV_SECTION
3285 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3286 #endif
3287 #ifndef DEBUG_DWO_ABBREV_SECTION
3288 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3289 #endif
3290 #ifndef DEBUG_ARANGES_SECTION
3291 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3292 #endif
3293 #ifndef DEBUG_ADDR_SECTION
3294 #define DEBUG_ADDR_SECTION ".debug_addr"
3295 #endif
3296 #ifndef DEBUG_NORM_MACINFO_SECTION
3297 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3298 #endif
3299 #ifndef DEBUG_DWO_MACINFO_SECTION
3300 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3301 #endif
3302 #ifndef DEBUG_MACINFO_SECTION
3303 #define DEBUG_MACINFO_SECTION \
3304 (!dwarf_split_debug_info \
3305 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3306 #endif
3307 #ifndef DEBUG_NORM_MACRO_SECTION
3308 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3309 #endif
3310 #ifndef DEBUG_DWO_MACRO_SECTION
3311 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3312 #endif
3313 #ifndef DEBUG_MACRO_SECTION
3314 #define DEBUG_MACRO_SECTION \
3315 (!dwarf_split_debug_info \
3316 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3317 #endif
3318 #ifndef DEBUG_LINE_SECTION
3319 #define DEBUG_LINE_SECTION ".debug_line"
3320 #endif
3321 #ifndef DEBUG_DWO_LINE_SECTION
3322 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3323 #endif
3324 #ifndef DEBUG_LOC_SECTION
3325 #define DEBUG_LOC_SECTION ".debug_loc"
3326 #endif
3327 #ifndef DEBUG_DWO_LOC_SECTION
3328 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3329 #endif
3330 #ifndef DEBUG_PUBNAMES_SECTION
3331 #define DEBUG_PUBNAMES_SECTION \
3332 ((debug_generate_pub_sections == 2) \
3333 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3334 #endif
3335 #ifndef DEBUG_PUBTYPES_SECTION
3336 #define DEBUG_PUBTYPES_SECTION \
3337 ((debug_generate_pub_sections == 2) \
3338 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3339 #endif
3340 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3341 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3342 #ifndef DEBUG_STR_OFFSETS_SECTION
3343 #define DEBUG_STR_OFFSETS_SECTION \
3344 (!dwarf_split_debug_info \
3345 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3346 #endif
3347 #ifndef DEBUG_STR_DWO_SECTION
3348 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3349 #endif
3350 #ifndef DEBUG_STR_SECTION
3351 #define DEBUG_STR_SECTION ".debug_str"
3352 #endif
3353 #ifndef DEBUG_RANGES_SECTION
3354 #define DEBUG_RANGES_SECTION ".debug_ranges"
3355 #endif
3357 /* Standard ELF section names for compiled code and data. */
3358 #ifndef TEXT_SECTION_NAME
3359 #define TEXT_SECTION_NAME ".text"
3360 #endif
3362 /* Section flags for .debug_macinfo/.debug_macro section. */
3363 #define DEBUG_MACRO_SECTION_FLAGS \
3364 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3366 /* Section flags for .debug_str section. */
3367 #define DEBUG_STR_SECTION_FLAGS \
3368 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3369 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3370 : SECTION_DEBUG)
3372 /* Section flags for .debug_str.dwo section. */
3373 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3375 /* Labels we insert at beginning sections we can reference instead of
3376 the section names themselves. */
3378 #ifndef TEXT_SECTION_LABEL
3379 #define TEXT_SECTION_LABEL "Ltext"
3380 #endif
3381 #ifndef COLD_TEXT_SECTION_LABEL
3382 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3383 #endif
3384 #ifndef DEBUG_LINE_SECTION_LABEL
3385 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3386 #endif
3387 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3388 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3389 #endif
3390 #ifndef DEBUG_INFO_SECTION_LABEL
3391 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3392 #endif
3393 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3394 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3395 #endif
3396 #ifndef DEBUG_ABBREV_SECTION_LABEL
3397 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3398 #endif
3399 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3400 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3401 #endif
3402 #ifndef DEBUG_ADDR_SECTION_LABEL
3403 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3404 #endif
3405 #ifndef DEBUG_LOC_SECTION_LABEL
3406 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3407 #endif
3408 #ifndef DEBUG_RANGES_SECTION_LABEL
3409 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3410 #endif
3411 #ifndef DEBUG_MACINFO_SECTION_LABEL
3412 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3413 #endif
3414 #ifndef DEBUG_MACRO_SECTION_LABEL
3415 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3416 #endif
3417 #define SKELETON_COMP_DIE_ABBREV 1
3418 #define SKELETON_TYPE_DIE_ABBREV 2
3420 /* Definitions of defaults for formats and names of various special
3421 (artificial) labels which may be generated within this file (when the -g
3422 options is used and DWARF2_DEBUGGING_INFO is in effect.
3423 If necessary, these may be overridden from within the tm.h file, but
3424 typically, overriding these defaults is unnecessary. */
3426 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3427 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3428 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3429 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3430 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3431 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3432 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3441 #ifndef TEXT_END_LABEL
3442 #define TEXT_END_LABEL "Letext"
3443 #endif
3444 #ifndef COLD_END_LABEL
3445 #define COLD_END_LABEL "Letext_cold"
3446 #endif
3447 #ifndef BLOCK_BEGIN_LABEL
3448 #define BLOCK_BEGIN_LABEL "LBB"
3449 #endif
3450 #ifndef BLOCK_END_LABEL
3451 #define BLOCK_END_LABEL "LBE"
3452 #endif
3453 #ifndef LINE_CODE_LABEL
3454 #define LINE_CODE_LABEL "LM"
3455 #endif
3458 /* Return the root of the DIE's built for the current compilation unit. */
3459 static dw_die_ref
3460 comp_unit_die (void)
3462 if (!single_comp_unit_die)
3463 single_comp_unit_die = gen_compile_unit_die (NULL);
3464 return single_comp_unit_die;
3467 /* We allow a language front-end to designate a function that is to be
3468 called to "demangle" any name before it is put into a DIE. */
3470 static const char *(*demangle_name_func) (const char *);
3472 void
3473 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3475 demangle_name_func = func;
3478 /* Test if rtl node points to a pseudo register. */
3480 static inline int
3481 is_pseudo_reg (const_rtx rtl)
3483 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3484 || (GET_CODE (rtl) == SUBREG
3485 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3488 /* Return a reference to a type, with its const and volatile qualifiers
3489 removed. */
3491 static inline tree
3492 type_main_variant (tree type)
3494 type = TYPE_MAIN_VARIANT (type);
3496 /* ??? There really should be only one main variant among any group of
3497 variants of a given type (and all of the MAIN_VARIANT values for all
3498 members of the group should point to that one type) but sometimes the C
3499 front-end messes this up for array types, so we work around that bug
3500 here. */
3501 if (TREE_CODE (type) == ARRAY_TYPE)
3502 while (type != TYPE_MAIN_VARIANT (type))
3503 type = TYPE_MAIN_VARIANT (type);
3505 return type;
3508 /* Return nonzero if the given type node represents a tagged type. */
3510 static inline int
3511 is_tagged_type (const_tree type)
3513 enum tree_code code = TREE_CODE (type);
3515 return (code == RECORD_TYPE || code == UNION_TYPE
3516 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3519 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3521 static void
3522 get_ref_die_offset_label (char *label, dw_die_ref ref)
3524 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3527 /* Return die_offset of a DIE reference to a base type. */
3529 static unsigned long int
3530 get_base_type_offset (dw_die_ref ref)
3532 if (ref->die_offset)
3533 return ref->die_offset;
3534 if (comp_unit_die ()->die_abbrev)
3536 calc_base_type_die_sizes ();
3537 gcc_assert (ref->die_offset);
3539 return ref->die_offset;
3542 /* Return die_offset of a DIE reference other than base type. */
3544 static unsigned long int
3545 get_ref_die_offset (dw_die_ref ref)
3547 gcc_assert (ref->die_offset);
3548 return ref->die_offset;
3551 /* Convert a DIE tag into its string name. */
3553 static const char *
3554 dwarf_tag_name (unsigned int tag)
3556 const char *name = get_DW_TAG_name (tag);
3558 if (name != NULL)
3559 return name;
3561 return "DW_TAG_<unknown>";
3564 /* Convert a DWARF attribute code into its string name. */
3566 static const char *
3567 dwarf_attr_name (unsigned int attr)
3569 const char *name;
3571 switch (attr)
3573 #if VMS_DEBUGGING_INFO
3574 case DW_AT_HP_prologue:
3575 return "DW_AT_HP_prologue";
3576 #else
3577 case DW_AT_MIPS_loop_unroll_factor:
3578 return "DW_AT_MIPS_loop_unroll_factor";
3579 #endif
3581 #if VMS_DEBUGGING_INFO
3582 case DW_AT_HP_epilogue:
3583 return "DW_AT_HP_epilogue";
3584 #else
3585 case DW_AT_MIPS_stride:
3586 return "DW_AT_MIPS_stride";
3587 #endif
3590 name = get_DW_AT_name (attr);
3592 if (name != NULL)
3593 return name;
3595 return "DW_AT_<unknown>";
3598 /* Convert a DWARF value form code into its string name. */
3600 static const char *
3601 dwarf_form_name (unsigned int form)
3603 const char *name = get_DW_FORM_name (form);
3605 if (name != NULL)
3606 return name;
3608 return "DW_FORM_<unknown>";
3611 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3612 instance of an inlined instance of a decl which is local to an inline
3613 function, so we have to trace all of the way back through the origin chain
3614 to find out what sort of node actually served as the original seed for the
3615 given block. */
3617 static tree
3618 decl_ultimate_origin (const_tree decl)
3620 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3621 return NULL_TREE;
3623 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3624 nodes in the function to point to themselves; ignore that if
3625 we're trying to output the abstract instance of this function. */
3626 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3627 return NULL_TREE;
3629 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3630 most distant ancestor, this should never happen. */
3631 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3633 return DECL_ABSTRACT_ORIGIN (decl);
3636 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3637 of a virtual function may refer to a base class, so we check the 'this'
3638 parameter. */
3640 static tree
3641 decl_class_context (tree decl)
3643 tree context = NULL_TREE;
3645 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3646 context = DECL_CONTEXT (decl);
3647 else
3648 context = TYPE_MAIN_VARIANT
3649 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3651 if (context && !TYPE_P (context))
3652 context = NULL_TREE;
3654 return context;
3657 /* Add an attribute/value pair to a DIE. */
3659 static inline void
3660 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3662 /* Maybe this should be an assert? */
3663 if (die == NULL)
3664 return;
3666 vec_safe_reserve (die->die_attr, 1);
3667 vec_safe_push (die->die_attr, *attr);
3670 static inline enum dw_val_class
3671 AT_class (dw_attr_ref a)
3673 return a->dw_attr_val.val_class;
3676 /* Return the index for any attribute that will be referenced with a
3677 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3678 are stored in dw_attr_val.v.val_str for reference counting
3679 pruning. */
3681 static inline unsigned int
3682 AT_index (dw_attr_ref a)
3684 if (AT_class (a) == dw_val_class_str)
3685 return a->dw_attr_val.v.val_str->index;
3686 else if (a->dw_attr_val.val_entry != NULL)
3687 return a->dw_attr_val.val_entry->index;
3688 return NOT_INDEXED;
3691 /* Add a flag value attribute to a DIE. */
3693 static inline void
3694 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3696 dw_attr_node attr;
3698 attr.dw_attr = attr_kind;
3699 attr.dw_attr_val.val_class = dw_val_class_flag;
3700 attr.dw_attr_val.val_entry = NULL;
3701 attr.dw_attr_val.v.val_flag = flag;
3702 add_dwarf_attr (die, &attr);
3705 static inline unsigned
3706 AT_flag (dw_attr_ref a)
3708 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3709 return a->dw_attr_val.v.val_flag;
3712 /* Add a signed integer attribute value to a DIE. */
3714 static inline void
3715 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3717 dw_attr_node attr;
3719 attr.dw_attr = attr_kind;
3720 attr.dw_attr_val.val_class = dw_val_class_const;
3721 attr.dw_attr_val.val_entry = NULL;
3722 attr.dw_attr_val.v.val_int = int_val;
3723 add_dwarf_attr (die, &attr);
3726 static inline HOST_WIDE_INT
3727 AT_int (dw_attr_ref a)
3729 gcc_assert (a && AT_class (a) == dw_val_class_const);
3730 return a->dw_attr_val.v.val_int;
3733 /* Add an unsigned integer attribute value to a DIE. */
3735 static inline void
3736 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3737 unsigned HOST_WIDE_INT unsigned_val)
3739 dw_attr_node attr;
3741 attr.dw_attr = attr_kind;
3742 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3743 attr.dw_attr_val.val_entry = NULL;
3744 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3745 add_dwarf_attr (die, &attr);
3748 static inline unsigned HOST_WIDE_INT
3749 AT_unsigned (dw_attr_ref a)
3751 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3752 return a->dw_attr_val.v.val_unsigned;
3755 /* Add an unsigned double integer attribute value to a DIE. */
3757 static inline void
3758 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3759 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3761 dw_attr_node attr;
3763 attr.dw_attr = attr_kind;
3764 attr.dw_attr_val.val_class = dw_val_class_const_double;
3765 attr.dw_attr_val.val_entry = NULL;
3766 attr.dw_attr_val.v.val_double.high = high;
3767 attr.dw_attr_val.v.val_double.low = low;
3768 add_dwarf_attr (die, &attr);
3771 /* Add a floating point attribute value to a DIE and return it. */
3773 static inline void
3774 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3775 unsigned int length, unsigned int elt_size, unsigned char *array)
3777 dw_attr_node attr;
3779 attr.dw_attr = attr_kind;
3780 attr.dw_attr_val.val_class = dw_val_class_vec;
3781 attr.dw_attr_val.val_entry = NULL;
3782 attr.dw_attr_val.v.val_vec.length = length;
3783 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3784 attr.dw_attr_val.v.val_vec.array = array;
3785 add_dwarf_attr (die, &attr);
3788 /* Add an 8-byte data attribute value to a DIE. */
3790 static inline void
3791 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3792 unsigned char data8[8])
3794 dw_attr_node attr;
3796 attr.dw_attr = attr_kind;
3797 attr.dw_attr_val.val_class = dw_val_class_data8;
3798 attr.dw_attr_val.val_entry = NULL;
3799 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3800 add_dwarf_attr (die, &attr);
3803 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3804 dwarf_split_debug_info, address attributes in dies destined for the
3805 final executable have force_direct set to avoid using indexed
3806 references. */
3808 static inline void
3809 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3810 bool force_direct)
3812 dw_attr_node attr;
3813 char * lbl_id;
3815 lbl_id = xstrdup (lbl_low);
3816 attr.dw_attr = DW_AT_low_pc;
3817 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3818 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3819 if (dwarf_split_debug_info && !force_direct)
3820 attr.dw_attr_val.val_entry
3821 = add_addr_table_entry (lbl_id, ate_kind_label);
3822 else
3823 attr.dw_attr_val.val_entry = NULL;
3824 add_dwarf_attr (die, &attr);
3826 attr.dw_attr = DW_AT_high_pc;
3827 if (dwarf_version < 4)
3828 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3829 else
3830 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3831 lbl_id = xstrdup (lbl_high);
3832 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3833 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3834 && dwarf_split_debug_info && !force_direct)
3835 attr.dw_attr_val.val_entry
3836 = add_addr_table_entry (lbl_id, ate_kind_label);
3837 else
3838 attr.dw_attr_val.val_entry = NULL;
3839 add_dwarf_attr (die, &attr);
3842 /* Hash and equality functions for debug_str_hash. */
3844 static hashval_t
3845 debug_str_do_hash (const void *x)
3847 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3850 static int
3851 debug_str_eq (const void *x1, const void *x2)
3853 return strcmp ((((const struct indirect_string_node *)x1)->str),
3854 (const char *)x2) == 0;
3857 /* Add STR to the given string hash table. */
3859 static struct indirect_string_node *
3860 find_AT_string_in_table (const char *str, htab_t table)
3862 struct indirect_string_node *node;
3863 void **slot;
3865 slot = htab_find_slot_with_hash (table, str,
3866 htab_hash_string (str), INSERT);
3867 if (*slot == NULL)
3869 node = ggc_alloc_cleared_indirect_string_node ();
3870 node->str = ggc_strdup (str);
3871 *slot = node;
3873 else
3874 node = (struct indirect_string_node *) *slot;
3876 node->refcount++;
3877 return node;
3880 /* Add STR to the indirect string hash table. */
3882 static struct indirect_string_node *
3883 find_AT_string (const char *str)
3885 if (! debug_str_hash)
3886 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3887 debug_str_eq, NULL);
3889 return find_AT_string_in_table (str, debug_str_hash);
3892 /* Add a string attribute value to a DIE. */
3894 static inline void
3895 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3897 dw_attr_node attr;
3898 struct indirect_string_node *node;
3900 node = find_AT_string (str);
3902 attr.dw_attr = attr_kind;
3903 attr.dw_attr_val.val_class = dw_val_class_str;
3904 attr.dw_attr_val.val_entry = NULL;
3905 attr.dw_attr_val.v.val_str = node;
3906 add_dwarf_attr (die, &attr);
3909 static inline const char *
3910 AT_string (dw_attr_ref a)
3912 gcc_assert (a && AT_class (a) == dw_val_class_str);
3913 return a->dw_attr_val.v.val_str->str;
3916 /* Call this function directly to bypass AT_string_form's logic to put
3917 the string inline in the die. */
3919 static void
3920 set_indirect_string (struct indirect_string_node *node)
3922 char label[32];
3923 /* Already indirect is a no op. */
3924 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3926 gcc_assert (node->label);
3927 return;
3929 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3930 ++dw2_string_counter;
3931 node->label = xstrdup (label);
3933 if (!dwarf_split_debug_info)
3935 node->form = DW_FORM_strp;
3936 node->index = NOT_INDEXED;
3938 else
3940 node->form = DW_FORM_GNU_str_index;
3941 node->index = NO_INDEX_ASSIGNED;
3945 /* Find out whether a string should be output inline in DIE
3946 or out-of-line in .debug_str section. */
3948 static enum dwarf_form
3949 find_string_form (struct indirect_string_node *node)
3951 unsigned int len;
3953 if (node->form)
3954 return node->form;
3956 len = strlen (node->str) + 1;
3958 /* If the string is shorter or equal to the size of the reference, it is
3959 always better to put it inline. */
3960 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3961 return node->form = DW_FORM_string;
3963 /* If we cannot expect the linker to merge strings in .debug_str
3964 section, only put it into .debug_str if it is worth even in this
3965 single module. */
3966 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3967 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3968 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3969 return node->form = DW_FORM_string;
3971 set_indirect_string (node);
3973 return node->form;
3976 /* Find out whether the string referenced from the attribute should be
3977 output inline in DIE or out-of-line in .debug_str section. */
3979 static enum dwarf_form
3980 AT_string_form (dw_attr_ref a)
3982 gcc_assert (a && AT_class (a) == dw_val_class_str);
3983 return find_string_form (a->dw_attr_val.v.val_str);
3986 /* Add a DIE reference attribute value to a DIE. */
3988 static inline void
3989 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3991 dw_attr_node attr;
3993 #ifdef ENABLE_CHECKING
3994 gcc_assert (targ_die != NULL);
3995 #else
3996 /* With LTO we can end up trying to reference something we didn't create
3997 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3998 if (targ_die == NULL)
3999 return;
4000 #endif
4002 attr.dw_attr = attr_kind;
4003 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4004 attr.dw_attr_val.val_entry = NULL;
4005 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4006 attr.dw_attr_val.v.val_die_ref.external = 0;
4007 add_dwarf_attr (die, &attr);
4010 /* Change DIE reference REF to point to NEW_DIE instead. */
4012 static inline void
4013 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4015 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4016 ref->dw_attr_val.v.val_die_ref.die = new_die;
4017 ref->dw_attr_val.v.val_die_ref.external = 0;
4020 /* Add an AT_specification attribute to a DIE, and also make the back
4021 pointer from the specification to the definition. */
4023 static inline void
4024 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4026 add_AT_die_ref (die, DW_AT_specification, targ_die);
4027 gcc_assert (!targ_die->die_definition);
4028 targ_die->die_definition = die;
4031 static inline dw_die_ref
4032 AT_ref (dw_attr_ref a)
4034 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4035 return a->dw_attr_val.v.val_die_ref.die;
4038 static inline int
4039 AT_ref_external (dw_attr_ref a)
4041 if (a && AT_class (a) == dw_val_class_die_ref)
4042 return a->dw_attr_val.v.val_die_ref.external;
4044 return 0;
4047 static inline void
4048 set_AT_ref_external (dw_attr_ref a, int i)
4050 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4051 a->dw_attr_val.v.val_die_ref.external = i;
4054 /* Add an FDE reference attribute value to a DIE. */
4056 static inline void
4057 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4059 dw_attr_node attr;
4061 attr.dw_attr = attr_kind;
4062 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4063 attr.dw_attr_val.val_entry = NULL;
4064 attr.dw_attr_val.v.val_fde_index = targ_fde;
4065 add_dwarf_attr (die, &attr);
4068 /* Add a location description attribute value to a DIE. */
4070 static inline void
4071 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4073 dw_attr_node attr;
4075 attr.dw_attr = attr_kind;
4076 attr.dw_attr_val.val_class = dw_val_class_loc;
4077 attr.dw_attr_val.val_entry = NULL;
4078 attr.dw_attr_val.v.val_loc = loc;
4079 add_dwarf_attr (die, &attr);
4082 static inline dw_loc_descr_ref
4083 AT_loc (dw_attr_ref a)
4085 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4086 return a->dw_attr_val.v.val_loc;
4089 static inline void
4090 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4092 dw_attr_node attr;
4094 attr.dw_attr = attr_kind;
4095 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4096 attr.dw_attr_val.val_entry = NULL;
4097 attr.dw_attr_val.v.val_loc_list = loc_list;
4098 add_dwarf_attr (die, &attr);
4099 have_location_lists = true;
4102 static inline dw_loc_list_ref
4103 AT_loc_list (dw_attr_ref a)
4105 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4106 return a->dw_attr_val.v.val_loc_list;
4109 static inline dw_loc_list_ref *
4110 AT_loc_list_ptr (dw_attr_ref a)
4112 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4113 return &a->dw_attr_val.v.val_loc_list;
4116 /* Table of entries into the .debug_addr section. */
4118 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4120 /* Hash an address_table_entry. */
4122 static hashval_t
4123 addr_table_entry_do_hash (const void *x)
4125 const addr_table_entry *a = (const addr_table_entry *) x;
4126 switch (a->kind)
4128 case ate_kind_rtx:
4129 return iterative_hash_rtx (a->addr.rtl, 0);
4130 case ate_kind_rtx_dtprel:
4131 return iterative_hash_rtx (a->addr.rtl, 1);
4132 case ate_kind_label:
4133 return htab_hash_string (a->addr.label);
4134 default:
4135 gcc_unreachable ();
4139 /* Determine equality for two address_table_entries. */
4141 static int
4142 addr_table_entry_eq (const void *x1, const void *x2)
4144 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4145 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4147 if (a1->kind != a2->kind)
4148 return 0;
4149 switch (a1->kind)
4151 case ate_kind_rtx:
4152 case ate_kind_rtx_dtprel:
4153 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4154 case ate_kind_label:
4155 return strcmp (a1->addr.label, a2->addr.label) == 0;
4156 default:
4157 gcc_unreachable ();
4161 /* Initialize an addr_table_entry. */
4163 void
4164 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4166 e->kind = kind;
4167 switch (kind)
4169 case ate_kind_rtx:
4170 case ate_kind_rtx_dtprel:
4171 e->addr.rtl = (rtx) addr;
4172 break;
4173 case ate_kind_label:
4174 e->addr.label = (char *) addr;
4175 break;
4177 e->refcount = 0;
4178 e->index = NO_INDEX_ASSIGNED;
4181 /* Add attr to the address table entry to the table. Defer setting an
4182 index until output time. */
4184 static addr_table_entry *
4185 add_addr_table_entry (void *addr, enum ate_kind kind)
4187 addr_table_entry *node;
4188 addr_table_entry finder;
4189 void **slot;
4191 gcc_assert (dwarf_split_debug_info);
4192 if (! addr_index_table)
4193 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4194 addr_table_entry_eq, NULL);
4195 init_addr_table_entry (&finder, kind, addr);
4196 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4198 if (*slot == HTAB_EMPTY_ENTRY)
4200 node = ggc_alloc_cleared_addr_table_entry ();
4201 init_addr_table_entry (node, kind, addr);
4202 *slot = node;
4204 else
4205 node = (addr_table_entry *) *slot;
4207 node->refcount++;
4208 return node;
4211 /* Remove an entry from the addr table by decrementing its refcount.
4212 Strictly, decrementing the refcount would be enough, but the
4213 assertion that the entry is actually in the table has found
4214 bugs. */
4216 static void
4217 remove_addr_table_entry (addr_table_entry *entry)
4219 addr_table_entry *node;
4221 gcc_assert (dwarf_split_debug_info && addr_index_table);
4222 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4223 /* After an index is assigned, the table is frozen. */
4224 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4225 node->refcount--;
4228 /* Given a location list, remove all addresses it refers to from the
4229 address_table. */
4231 static void
4232 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4234 for (; descr; descr = descr->dw_loc_next)
4235 if (descr->dw_loc_oprnd1.val_entry != NULL)
4237 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4238 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4242 /* A helper function for dwarf2out_finish called through
4243 htab_traverse. Assign an addr_table_entry its index. All entries
4244 must be collected into the table when this function is called,
4245 because the indexing code relies on htab_traverse to traverse nodes
4246 in the same order for each run. */
4248 static int
4249 index_addr_table_entry (void **h, void *v)
4251 addr_table_entry *node = (addr_table_entry *) *h;
4252 unsigned int *index = (unsigned int *) v;
4254 /* Don't index unreferenced nodes. */
4255 if (node->refcount == 0)
4256 return 1;
4258 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4259 node->index = *index;
4260 *index += 1;
4262 return 1;
4265 /* Add an address constant attribute value to a DIE. When using
4266 dwarf_split_debug_info, address attributes in dies destined for the
4267 final executable should be direct references--setting the parameter
4268 force_direct ensures this behavior. */
4270 static inline void
4271 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4272 bool force_direct)
4274 dw_attr_node attr;
4276 attr.dw_attr = attr_kind;
4277 attr.dw_attr_val.val_class = dw_val_class_addr;
4278 attr.dw_attr_val.v.val_addr = addr;
4279 if (dwarf_split_debug_info && !force_direct)
4280 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4281 else
4282 attr.dw_attr_val.val_entry = NULL;
4283 add_dwarf_attr (die, &attr);
4286 /* Get the RTX from to an address DIE attribute. */
4288 static inline rtx
4289 AT_addr (dw_attr_ref a)
4291 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4292 return a->dw_attr_val.v.val_addr;
4295 /* Add a file attribute value to a DIE. */
4297 static inline void
4298 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4299 struct dwarf_file_data *fd)
4301 dw_attr_node attr;
4303 attr.dw_attr = attr_kind;
4304 attr.dw_attr_val.val_class = dw_val_class_file;
4305 attr.dw_attr_val.val_entry = NULL;
4306 attr.dw_attr_val.v.val_file = fd;
4307 add_dwarf_attr (die, &attr);
4310 /* Get the dwarf_file_data from a file DIE attribute. */
4312 static inline struct dwarf_file_data *
4313 AT_file (dw_attr_ref a)
4315 gcc_assert (a && AT_class (a) == dw_val_class_file);
4316 return a->dw_attr_val.v.val_file;
4319 /* Add a vms delta attribute value to a DIE. */
4321 static inline void
4322 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4323 const char *lbl1, const char *lbl2)
4325 dw_attr_node attr;
4327 attr.dw_attr = attr_kind;
4328 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4329 attr.dw_attr_val.val_entry = NULL;
4330 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4331 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4332 add_dwarf_attr (die, &attr);
4335 /* Add a label identifier attribute value to a DIE. */
4337 static inline void
4338 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4339 const char *lbl_id)
4341 dw_attr_node attr;
4343 attr.dw_attr = attr_kind;
4344 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4345 attr.dw_attr_val.val_entry = NULL;
4346 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4347 if (dwarf_split_debug_info)
4348 attr.dw_attr_val.val_entry
4349 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4350 ate_kind_label);
4351 add_dwarf_attr (die, &attr);
4354 /* Add a section offset attribute value to a DIE, an offset into the
4355 debug_line section. */
4357 static inline void
4358 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4359 const char *label)
4361 dw_attr_node attr;
4363 attr.dw_attr = attr_kind;
4364 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4365 attr.dw_attr_val.val_entry = NULL;
4366 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4367 add_dwarf_attr (die, &attr);
4370 /* Add a section offset attribute value to a DIE, an offset into the
4371 debug_macinfo section. */
4373 static inline void
4374 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4375 const char *label)
4377 dw_attr_node attr;
4379 attr.dw_attr = attr_kind;
4380 attr.dw_attr_val.val_class = dw_val_class_macptr;
4381 attr.dw_attr_val.val_entry = NULL;
4382 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4383 add_dwarf_attr (die, &attr);
4386 /* Add an offset attribute value to a DIE. */
4388 static inline void
4389 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4390 unsigned HOST_WIDE_INT offset)
4392 dw_attr_node attr;
4394 attr.dw_attr = attr_kind;
4395 attr.dw_attr_val.val_class = dw_val_class_offset;
4396 attr.dw_attr_val.val_entry = NULL;
4397 attr.dw_attr_val.v.val_offset = offset;
4398 add_dwarf_attr (die, &attr);
4401 /* Add a range_list attribute value to a DIE. When using
4402 dwarf_split_debug_info, address attributes in dies destined for the
4403 final executable should be direct references--setting the parameter
4404 force_direct ensures this behavior. */
4406 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4407 #define RELOCATED_OFFSET (NULL)
4409 static void
4410 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4411 long unsigned int offset, bool force_direct)
4413 dw_attr_node attr;
4415 attr.dw_attr = attr_kind;
4416 attr.dw_attr_val.val_class = dw_val_class_range_list;
4417 /* For the range_list attribute, use val_entry to store whether the
4418 offset should follow split-debug-info or normal semantics. This
4419 value is read in output_range_list_offset. */
4420 if (dwarf_split_debug_info && !force_direct)
4421 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4422 else
4423 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4424 attr.dw_attr_val.v.val_offset = offset;
4425 add_dwarf_attr (die, &attr);
4428 /* Return the start label of a delta attribute. */
4430 static inline const char *
4431 AT_vms_delta1 (dw_attr_ref a)
4433 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4434 return a->dw_attr_val.v.val_vms_delta.lbl1;
4437 /* Return the end label of a delta attribute. */
4439 static inline const char *
4440 AT_vms_delta2 (dw_attr_ref a)
4442 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4443 return a->dw_attr_val.v.val_vms_delta.lbl2;
4446 static inline const char *
4447 AT_lbl (dw_attr_ref a)
4449 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4450 || AT_class (a) == dw_val_class_lineptr
4451 || AT_class (a) == dw_val_class_macptr
4452 || AT_class (a) == dw_val_class_high_pc));
4453 return a->dw_attr_val.v.val_lbl_id;
4456 /* Get the attribute of type attr_kind. */
4458 static dw_attr_ref
4459 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4461 dw_attr_ref a;
4462 unsigned ix;
4463 dw_die_ref spec = NULL;
4465 if (! die)
4466 return NULL;
4468 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4469 if (a->dw_attr == attr_kind)
4470 return a;
4471 else if (a->dw_attr == DW_AT_specification
4472 || a->dw_attr == DW_AT_abstract_origin)
4473 spec = AT_ref (a);
4475 if (spec)
4476 return get_AT (spec, attr_kind);
4478 return NULL;
4481 /* Returns the parent of the declaration of DIE. */
4483 static dw_die_ref
4484 get_die_parent (dw_die_ref die)
4486 dw_die_ref t;
4488 if (!die)
4489 return NULL;
4491 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4492 || (t = get_AT_ref (die, DW_AT_specification)))
4493 die = t;
4495 return die->die_parent;
4498 /* Return the "low pc" attribute value, typically associated with a subprogram
4499 DIE. Return null if the "low pc" attribute is either not present, or if it
4500 cannot be represented as an assembler label identifier. */
4502 static inline const char *
4503 get_AT_low_pc (dw_die_ref die)
4505 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4507 return a ? AT_lbl (a) : NULL;
4510 /* Return the "high pc" attribute value, typically associated with a subprogram
4511 DIE. Return null if the "high pc" attribute is either not present, or if it
4512 cannot be represented as an assembler label identifier. */
4514 static inline const char *
4515 get_AT_hi_pc (dw_die_ref die)
4517 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4519 return a ? AT_lbl (a) : NULL;
4522 /* Return the value of the string attribute designated by ATTR_KIND, or
4523 NULL if it is not present. */
4525 static inline const char *
4526 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4528 dw_attr_ref a = get_AT (die, attr_kind);
4530 return a ? AT_string (a) : NULL;
4533 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4534 if it is not present. */
4536 static inline int
4537 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4539 dw_attr_ref a = get_AT (die, attr_kind);
4541 return a ? AT_flag (a) : 0;
4544 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4545 if it is not present. */
4547 static inline unsigned
4548 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4550 dw_attr_ref a = get_AT (die, attr_kind);
4552 return a ? AT_unsigned (a) : 0;
4555 static inline dw_die_ref
4556 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4558 dw_attr_ref a = get_AT (die, attr_kind);
4560 return a ? AT_ref (a) : NULL;
4563 static inline struct dwarf_file_data *
4564 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4566 dw_attr_ref a = get_AT (die, attr_kind);
4568 return a ? AT_file (a) : NULL;
4571 /* Return TRUE if the language is C++. */
4573 static inline bool
4574 is_cxx (void)
4576 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4578 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4581 /* Return TRUE if the language is Java. */
4583 static inline bool
4584 is_java (void)
4586 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4588 return lang == DW_LANG_Java;
4591 /* Return TRUE if the language is Fortran. */
4593 static inline bool
4594 is_fortran (void)
4596 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4598 return (lang == DW_LANG_Fortran77
4599 || lang == DW_LANG_Fortran90
4600 || lang == DW_LANG_Fortran95);
4603 /* Return TRUE if the language is Ada. */
4605 static inline bool
4606 is_ada (void)
4608 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4610 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4613 /* Remove the specified attribute if present. */
4615 static void
4616 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4618 dw_attr_ref a;
4619 unsigned ix;
4621 if (! die)
4622 return;
4624 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4625 if (a->dw_attr == attr_kind)
4627 if (AT_class (a) == dw_val_class_str)
4628 if (a->dw_attr_val.v.val_str->refcount)
4629 a->dw_attr_val.v.val_str->refcount--;
4631 /* vec::ordered_remove should help reduce the number of abbrevs
4632 that are needed. */
4633 die->die_attr->ordered_remove (ix);
4634 return;
4638 /* Remove CHILD from its parent. PREV must have the property that
4639 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4641 static void
4642 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4644 gcc_assert (child->die_parent == prev->die_parent);
4645 gcc_assert (prev->die_sib == child);
4646 if (prev == child)
4648 gcc_assert (child->die_parent->die_child == child);
4649 prev = NULL;
4651 else
4652 prev->die_sib = child->die_sib;
4653 if (child->die_parent->die_child == child)
4654 child->die_parent->die_child = prev;
4657 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4658 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4660 static void
4661 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4663 dw_die_ref parent = old_child->die_parent;
4665 gcc_assert (parent == prev->die_parent);
4666 gcc_assert (prev->die_sib == old_child);
4668 new_child->die_parent = parent;
4669 if (prev == old_child)
4671 gcc_assert (parent->die_child == old_child);
4672 new_child->die_sib = new_child;
4674 else
4676 prev->die_sib = new_child;
4677 new_child->die_sib = old_child->die_sib;
4679 if (old_child->die_parent->die_child == old_child)
4680 old_child->die_parent->die_child = new_child;
4683 /* Move all children from OLD_PARENT to NEW_PARENT. */
4685 static void
4686 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4688 dw_die_ref c;
4689 new_parent->die_child = old_parent->die_child;
4690 old_parent->die_child = NULL;
4691 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4694 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4695 matches TAG. */
4697 static void
4698 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4700 dw_die_ref c;
4702 c = die->die_child;
4703 if (c) do {
4704 dw_die_ref prev = c;
4705 c = c->die_sib;
4706 while (c->die_tag == tag)
4708 remove_child_with_prev (c, prev);
4709 /* Might have removed every child. */
4710 if (c == c->die_sib)
4711 return;
4712 c = c->die_sib;
4714 } while (c != die->die_child);
4717 /* Add a CHILD_DIE as the last child of DIE. */
4719 static void
4720 add_child_die (dw_die_ref die, dw_die_ref child_die)
4722 /* FIXME this should probably be an assert. */
4723 if (! die || ! child_die)
4724 return;
4725 gcc_assert (die != child_die);
4727 child_die->die_parent = die;
4728 if (die->die_child)
4730 child_die->die_sib = die->die_child->die_sib;
4731 die->die_child->die_sib = child_die;
4733 else
4734 child_die->die_sib = child_die;
4735 die->die_child = child_die;
4738 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4739 is the specification, to the end of PARENT's list of children.
4740 This is done by removing and re-adding it. */
4742 static void
4743 splice_child_die (dw_die_ref parent, dw_die_ref child)
4745 dw_die_ref p;
4747 /* We want the declaration DIE from inside the class, not the
4748 specification DIE at toplevel. */
4749 if (child->die_parent != parent)
4751 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4753 if (tmp)
4754 child = tmp;
4757 gcc_assert (child->die_parent == parent
4758 || (child->die_parent
4759 == get_AT_ref (parent, DW_AT_specification)));
4761 for (p = child->die_parent->die_child; ; p = p->die_sib)
4762 if (p->die_sib == child)
4764 remove_child_with_prev (child, p);
4765 break;
4768 add_child_die (parent, child);
4771 /* Return a pointer to a newly created DIE node. */
4773 static inline dw_die_ref
4774 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4776 dw_die_ref die = ggc_alloc_cleared_die_node ();
4778 die->die_tag = tag_value;
4780 if (parent_die != NULL)
4781 add_child_die (parent_die, die);
4782 else
4784 limbo_die_node *limbo_node;
4786 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4787 limbo_node->die = die;
4788 limbo_node->created_for = t;
4789 limbo_node->next = limbo_die_list;
4790 limbo_die_list = limbo_node;
4793 return die;
4796 /* Return the DIE associated with the given type specifier. */
4798 static inline dw_die_ref
4799 lookup_type_die (tree type)
4801 return TYPE_SYMTAB_DIE (type);
4804 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4805 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4806 anonymous type instead the one of the naming typedef. */
4808 static inline dw_die_ref
4809 strip_naming_typedef (tree type, dw_die_ref type_die)
4811 if (type
4812 && TREE_CODE (type) == RECORD_TYPE
4813 && type_die
4814 && type_die->die_tag == DW_TAG_typedef
4815 && is_naming_typedef_decl (TYPE_NAME (type)))
4816 type_die = get_AT_ref (type_die, DW_AT_type);
4817 return type_die;
4820 /* Like lookup_type_die, but if type is an anonymous type named by a
4821 typedef[1], return the DIE of the anonymous type instead the one of
4822 the naming typedef. This is because in gen_typedef_die, we did
4823 equate the anonymous struct named by the typedef with the DIE of
4824 the naming typedef. So by default, lookup_type_die on an anonymous
4825 struct yields the DIE of the naming typedef.
4827 [1]: Read the comment of is_naming_typedef_decl to learn about what
4828 a naming typedef is. */
4830 static inline dw_die_ref
4831 lookup_type_die_strip_naming_typedef (tree type)
4833 dw_die_ref die = lookup_type_die (type);
4834 return strip_naming_typedef (type, die);
4837 /* Equate a DIE to a given type specifier. */
4839 static inline void
4840 equate_type_number_to_die (tree type, dw_die_ref type_die)
4842 TYPE_SYMTAB_DIE (type) = type_die;
4845 /* Returns a hash value for X (which really is a die_struct). */
4847 static hashval_t
4848 decl_die_table_hash (const void *x)
4850 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4853 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4855 static int
4856 decl_die_table_eq (const void *x, const void *y)
4858 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4861 /* Return the DIE associated with a given declaration. */
4863 static inline dw_die_ref
4864 lookup_decl_die (tree decl)
4866 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4869 /* Returns a hash value for X (which really is a var_loc_list). */
4871 static hashval_t
4872 decl_loc_table_hash (const void *x)
4874 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4877 /* Return nonzero if decl_id of var_loc_list X is the same as
4878 UID of decl *Y. */
4880 static int
4881 decl_loc_table_eq (const void *x, const void *y)
4883 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4886 /* Return the var_loc list associated with a given declaration. */
4888 static inline var_loc_list *
4889 lookup_decl_loc (const_tree decl)
4891 if (!decl_loc_table)
4892 return NULL;
4893 return (var_loc_list *)
4894 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4897 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4899 static hashval_t
4900 cached_dw_loc_list_table_hash (const void *x)
4902 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4905 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4906 UID of decl *Y. */
4908 static int
4909 cached_dw_loc_list_table_eq (const void *x, const void *y)
4911 return (((const cached_dw_loc_list *) x)->decl_id
4912 == DECL_UID ((const_tree) y));
4915 /* Equate a DIE to a particular declaration. */
4917 static void
4918 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4920 unsigned int decl_id = DECL_UID (decl);
4921 void **slot;
4923 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4924 *slot = decl_die;
4925 decl_die->decl_id = decl_id;
4928 /* Return how many bits covers PIECE EXPR_LIST. */
4930 static int
4931 decl_piece_bitsize (rtx piece)
4933 int ret = (int) GET_MODE (piece);
4934 if (ret)
4935 return ret;
4936 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4937 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4938 return INTVAL (XEXP (XEXP (piece, 0), 0));
4941 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4943 static rtx *
4944 decl_piece_varloc_ptr (rtx piece)
4946 if ((int) GET_MODE (piece))
4947 return &XEXP (piece, 0);
4948 else
4949 return &XEXP (XEXP (piece, 0), 1);
4952 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4953 Next is the chain of following piece nodes. */
4955 static rtx
4956 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4958 if (bitsize <= (int) MAX_MACHINE_MODE)
4959 return alloc_EXPR_LIST (bitsize, loc_note, next);
4960 else
4961 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4962 GEN_INT (bitsize),
4963 loc_note), next);
4966 /* Return rtx that should be stored into loc field for
4967 LOC_NOTE and BITPOS/BITSIZE. */
4969 static rtx
4970 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4971 HOST_WIDE_INT bitsize)
4973 if (bitsize != -1)
4975 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4976 if (bitpos != 0)
4977 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4979 return loc_note;
4982 /* This function either modifies location piece list *DEST in
4983 place (if SRC and INNER is NULL), or copies location piece list
4984 *SRC to *DEST while modifying it. Location BITPOS is modified
4985 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4986 not copied and if needed some padding around it is added.
4987 When modifying in place, DEST should point to EXPR_LIST where
4988 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4989 to the start of the whole list and INNER points to the EXPR_LIST
4990 where earlier pieces cover PIECE_BITPOS bits. */
4992 static void
4993 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4994 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4995 HOST_WIDE_INT bitsize, rtx loc_note)
4997 int diff;
4998 bool copy = inner != NULL;
5000 if (copy)
5002 /* First copy all nodes preceding the current bitpos. */
5003 while (src != inner)
5005 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5006 decl_piece_bitsize (*src), NULL_RTX);
5007 dest = &XEXP (*dest, 1);
5008 src = &XEXP (*src, 1);
5011 /* Add padding if needed. */
5012 if (bitpos != piece_bitpos)
5014 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5015 copy ? NULL_RTX : *dest);
5016 dest = &XEXP (*dest, 1);
5018 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5020 gcc_assert (!copy);
5021 /* A piece with correct bitpos and bitsize already exist,
5022 just update the location for it and return. */
5023 *decl_piece_varloc_ptr (*dest) = loc_note;
5024 return;
5026 /* Add the piece that changed. */
5027 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5028 dest = &XEXP (*dest, 1);
5029 /* Skip over pieces that overlap it. */
5030 diff = bitpos - piece_bitpos + bitsize;
5031 if (!copy)
5032 src = dest;
5033 while (diff > 0 && *src)
5035 rtx piece = *src;
5036 diff -= decl_piece_bitsize (piece);
5037 if (copy)
5038 src = &XEXP (piece, 1);
5039 else
5041 *src = XEXP (piece, 1);
5042 free_EXPR_LIST_node (piece);
5045 /* Add padding if needed. */
5046 if (diff < 0 && *src)
5048 if (!copy)
5049 dest = src;
5050 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5051 dest = &XEXP (*dest, 1);
5053 if (!copy)
5054 return;
5055 /* Finally copy all nodes following it. */
5056 while (*src)
5058 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5059 decl_piece_bitsize (*src), NULL_RTX);
5060 dest = &XEXP (*dest, 1);
5061 src = &XEXP (*src, 1);
5065 /* Add a variable location node to the linked list for DECL. */
5067 static struct var_loc_node *
5068 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5070 unsigned int decl_id;
5071 var_loc_list *temp;
5072 void **slot;
5073 struct var_loc_node *loc = NULL;
5074 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5076 if (TREE_CODE (decl) == VAR_DECL
5077 && DECL_HAS_DEBUG_EXPR_P (decl))
5079 tree realdecl = DECL_DEBUG_EXPR (decl);
5080 if (handled_component_p (realdecl)
5081 || (TREE_CODE (realdecl) == MEM_REF
5082 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5084 HOST_WIDE_INT maxsize;
5085 tree innerdecl;
5086 innerdecl
5087 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5088 if (!DECL_P (innerdecl)
5089 || DECL_IGNORED_P (innerdecl)
5090 || TREE_STATIC (innerdecl)
5091 || bitsize <= 0
5092 || bitpos + bitsize > 256
5093 || bitsize != maxsize)
5094 return NULL;
5095 decl = innerdecl;
5099 decl_id = DECL_UID (decl);
5100 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5101 if (*slot == NULL)
5103 temp = ggc_alloc_cleared_var_loc_list ();
5104 temp->decl_id = decl_id;
5105 *slot = temp;
5107 else
5108 temp = (var_loc_list *) *slot;
5110 /* For PARM_DECLs try to keep around the original incoming value,
5111 even if that means we'll emit a zero-range .debug_loc entry. */
5112 if (temp->last
5113 && temp->first == temp->last
5114 && TREE_CODE (decl) == PARM_DECL
5115 && NOTE_P (temp->first->loc)
5116 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5117 && DECL_INCOMING_RTL (decl)
5118 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5119 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5120 == GET_CODE (DECL_INCOMING_RTL (decl))
5121 && prev_real_insn (temp->first->loc) == NULL_RTX
5122 && (bitsize != -1
5123 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5124 NOTE_VAR_LOCATION_LOC (loc_note))
5125 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5126 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5128 loc = ggc_alloc_cleared_var_loc_node ();
5129 temp->first->next = loc;
5130 temp->last = loc;
5131 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5133 else if (temp->last)
5135 struct var_loc_node *last = temp->last, *unused = NULL;
5136 rtx *piece_loc = NULL, last_loc_note;
5137 int piece_bitpos = 0;
5138 if (last->next)
5140 last = last->next;
5141 gcc_assert (last->next == NULL);
5143 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5145 piece_loc = &last->loc;
5148 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5149 if (piece_bitpos + cur_bitsize > bitpos)
5150 break;
5151 piece_bitpos += cur_bitsize;
5152 piece_loc = &XEXP (*piece_loc, 1);
5154 while (*piece_loc);
5156 /* TEMP->LAST here is either pointer to the last but one or
5157 last element in the chained list, LAST is pointer to the
5158 last element. */
5159 if (label && strcmp (last->label, label) == 0)
5161 /* For SRA optimized variables if there weren't any real
5162 insns since last note, just modify the last node. */
5163 if (piece_loc != NULL)
5165 adjust_piece_list (piece_loc, NULL, NULL,
5166 bitpos, piece_bitpos, bitsize, loc_note);
5167 return NULL;
5169 /* If the last note doesn't cover any instructions, remove it. */
5170 if (temp->last != last)
5172 temp->last->next = NULL;
5173 unused = last;
5174 last = temp->last;
5175 gcc_assert (strcmp (last->label, label) != 0);
5177 else
5179 gcc_assert (temp->first == temp->last
5180 || (temp->first->next == temp->last
5181 && TREE_CODE (decl) == PARM_DECL));
5182 memset (temp->last, '\0', sizeof (*temp->last));
5183 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5184 return temp->last;
5187 if (bitsize == -1 && NOTE_P (last->loc))
5188 last_loc_note = last->loc;
5189 else if (piece_loc != NULL
5190 && *piece_loc != NULL_RTX
5191 && piece_bitpos == bitpos
5192 && decl_piece_bitsize (*piece_loc) == bitsize)
5193 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5194 else
5195 last_loc_note = NULL_RTX;
5196 /* If the current location is the same as the end of the list,
5197 and either both or neither of the locations is uninitialized,
5198 we have nothing to do. */
5199 if (last_loc_note == NULL_RTX
5200 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5201 NOTE_VAR_LOCATION_LOC (loc_note)))
5202 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5203 != NOTE_VAR_LOCATION_STATUS (loc_note))
5204 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5205 == VAR_INIT_STATUS_UNINITIALIZED)
5206 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5207 == VAR_INIT_STATUS_UNINITIALIZED))))
5209 /* Add LOC to the end of list and update LAST. If the last
5210 element of the list has been removed above, reuse its
5211 memory for the new node, otherwise allocate a new one. */
5212 if (unused)
5214 loc = unused;
5215 memset (loc, '\0', sizeof (*loc));
5217 else
5218 loc = ggc_alloc_cleared_var_loc_node ();
5219 if (bitsize == -1 || piece_loc == NULL)
5220 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5221 else
5222 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5223 bitpos, piece_bitpos, bitsize, loc_note);
5224 last->next = loc;
5225 /* Ensure TEMP->LAST will point either to the new last but one
5226 element of the chain, or to the last element in it. */
5227 if (last != temp->last)
5228 temp->last = last;
5230 else if (unused)
5231 ggc_free (unused);
5233 else
5235 loc = ggc_alloc_cleared_var_loc_node ();
5236 temp->first = loc;
5237 temp->last = loc;
5238 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5240 return loc;
5243 /* Keep track of the number of spaces used to indent the
5244 output of the debugging routines that print the structure of
5245 the DIE internal representation. */
5246 static int print_indent;
5248 /* Indent the line the number of spaces given by print_indent. */
5250 static inline void
5251 print_spaces (FILE *outfile)
5253 fprintf (outfile, "%*s", print_indent, "");
5256 /* Print a type signature in hex. */
5258 static inline void
5259 print_signature (FILE *outfile, char *sig)
5261 int i;
5263 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5264 fprintf (outfile, "%02x", sig[i] & 0xff);
5267 /* Print the information associated with a given DIE, and its children.
5268 This routine is a debugging aid only. */
5270 static void
5271 print_die (dw_die_ref die, FILE *outfile)
5273 dw_attr_ref a;
5274 dw_die_ref c;
5275 unsigned ix;
5277 print_spaces (outfile);
5278 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5279 die->die_offset, dwarf_tag_name (die->die_tag),
5280 (void*) die);
5281 print_spaces (outfile);
5282 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5283 fprintf (outfile, " offset: %ld", die->die_offset);
5284 fprintf (outfile, " mark: %d\n", die->die_mark);
5286 if (die->comdat_type_p)
5288 print_spaces (outfile);
5289 fprintf (outfile, " signature: ");
5290 print_signature (outfile, die->die_id.die_type_node->signature);
5291 fprintf (outfile, "\n");
5294 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5296 print_spaces (outfile);
5297 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5299 switch (AT_class (a))
5301 case dw_val_class_addr:
5302 fprintf (outfile, "address");
5303 break;
5304 case dw_val_class_offset:
5305 fprintf (outfile, "offset");
5306 break;
5307 case dw_val_class_loc:
5308 fprintf (outfile, "location descriptor");
5309 break;
5310 case dw_val_class_loc_list:
5311 fprintf (outfile, "location list -> label:%s",
5312 AT_loc_list (a)->ll_symbol);
5313 break;
5314 case dw_val_class_range_list:
5315 fprintf (outfile, "range list");
5316 break;
5317 case dw_val_class_const:
5318 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5319 break;
5320 case dw_val_class_unsigned_const:
5321 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5322 break;
5323 case dw_val_class_const_double:
5324 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5325 HOST_WIDE_INT_PRINT_UNSIGNED")",
5326 a->dw_attr_val.v.val_double.high,
5327 a->dw_attr_val.v.val_double.low);
5328 break;
5329 case dw_val_class_vec:
5330 fprintf (outfile, "floating-point or vector constant");
5331 break;
5332 case dw_val_class_flag:
5333 fprintf (outfile, "%u", AT_flag (a));
5334 break;
5335 case dw_val_class_die_ref:
5336 if (AT_ref (a) != NULL)
5338 if (AT_ref (a)->comdat_type_p)
5340 fprintf (outfile, "die -> signature: ");
5341 print_signature (outfile,
5342 AT_ref (a)->die_id.die_type_node->signature);
5344 else if (AT_ref (a)->die_id.die_symbol)
5345 fprintf (outfile, "die -> label: %s",
5346 AT_ref (a)->die_id.die_symbol);
5347 else
5348 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5349 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5351 else
5352 fprintf (outfile, "die -> <null>");
5353 break;
5354 case dw_val_class_vms_delta:
5355 fprintf (outfile, "delta: @slotcount(%s-%s)",
5356 AT_vms_delta2 (a), AT_vms_delta1 (a));
5357 break;
5358 case dw_val_class_lbl_id:
5359 case dw_val_class_lineptr:
5360 case dw_val_class_macptr:
5361 case dw_val_class_high_pc:
5362 fprintf (outfile, "label: %s", AT_lbl (a));
5363 break;
5364 case dw_val_class_str:
5365 if (AT_string (a) != NULL)
5366 fprintf (outfile, "\"%s\"", AT_string (a));
5367 else
5368 fprintf (outfile, "<null>");
5369 break;
5370 case dw_val_class_file:
5371 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5372 AT_file (a)->emitted_number);
5373 break;
5374 case dw_val_class_data8:
5376 int i;
5378 for (i = 0; i < 8; i++)
5379 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5380 break;
5382 default:
5383 break;
5386 fprintf (outfile, "\n");
5389 if (die->die_child != NULL)
5391 print_indent += 4;
5392 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5393 print_indent -= 4;
5395 if (print_indent == 0)
5396 fprintf (outfile, "\n");
5399 /* Print the information collected for a given DIE. */
5401 DEBUG_FUNCTION void
5402 debug_dwarf_die (dw_die_ref die)
5404 print_die (die, stderr);
5407 DEBUG_FUNCTION void
5408 debug (die_struct &ref)
5410 print_die (&ref, stderr);
5413 DEBUG_FUNCTION void
5414 debug (die_struct *ptr)
5416 if (ptr)
5417 debug (*ptr);
5418 else
5419 fprintf (stderr, "<nil>\n");
5423 /* Print all DWARF information collected for the compilation unit.
5424 This routine is a debugging aid only. */
5426 DEBUG_FUNCTION void
5427 debug_dwarf (void)
5429 print_indent = 0;
5430 print_die (comp_unit_die (), stderr);
5433 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5434 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5435 DIE that marks the start of the DIEs for this include file. */
5437 static dw_die_ref
5438 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5440 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5441 dw_die_ref new_unit = gen_compile_unit_die (filename);
5443 new_unit->die_sib = old_unit;
5444 return new_unit;
5447 /* Close an include-file CU and reopen the enclosing one. */
5449 static dw_die_ref
5450 pop_compile_unit (dw_die_ref old_unit)
5452 dw_die_ref new_unit = old_unit->die_sib;
5454 old_unit->die_sib = NULL;
5455 return new_unit;
5458 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5459 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5460 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5462 /* Calculate the checksum of a location expression. */
5464 static inline void
5465 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5467 int tem;
5468 hashval_t hash = 0;
5470 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5471 CHECKSUM (tem);
5472 hash = hash_loc_operands (loc, hash);
5473 CHECKSUM (hash);
5476 /* Calculate the checksum of an attribute. */
5478 static void
5479 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5481 dw_loc_descr_ref loc;
5482 rtx r;
5484 CHECKSUM (at->dw_attr);
5486 /* We don't care that this was compiled with a different compiler
5487 snapshot; if the output is the same, that's what matters. */
5488 if (at->dw_attr == DW_AT_producer)
5489 return;
5491 switch (AT_class (at))
5493 case dw_val_class_const:
5494 CHECKSUM (at->dw_attr_val.v.val_int);
5495 break;
5496 case dw_val_class_unsigned_const:
5497 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5498 break;
5499 case dw_val_class_const_double:
5500 CHECKSUM (at->dw_attr_val.v.val_double);
5501 break;
5502 case dw_val_class_vec:
5503 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5504 (at->dw_attr_val.v.val_vec.length
5505 * at->dw_attr_val.v.val_vec.elt_size));
5506 break;
5507 case dw_val_class_flag:
5508 CHECKSUM (at->dw_attr_val.v.val_flag);
5509 break;
5510 case dw_val_class_str:
5511 CHECKSUM_STRING (AT_string (at));
5512 break;
5514 case dw_val_class_addr:
5515 r = AT_addr (at);
5516 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5517 CHECKSUM_STRING (XSTR (r, 0));
5518 break;
5520 case dw_val_class_offset:
5521 CHECKSUM (at->dw_attr_val.v.val_offset);
5522 break;
5524 case dw_val_class_loc:
5525 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5526 loc_checksum (loc, ctx);
5527 break;
5529 case dw_val_class_die_ref:
5530 die_checksum (AT_ref (at), ctx, mark);
5531 break;
5533 case dw_val_class_fde_ref:
5534 case dw_val_class_vms_delta:
5535 case dw_val_class_lbl_id:
5536 case dw_val_class_lineptr:
5537 case dw_val_class_macptr:
5538 case dw_val_class_high_pc:
5539 break;
5541 case dw_val_class_file:
5542 CHECKSUM_STRING (AT_file (at)->filename);
5543 break;
5545 case dw_val_class_data8:
5546 CHECKSUM (at->dw_attr_val.v.val_data8);
5547 break;
5549 default:
5550 break;
5554 /* Calculate the checksum of a DIE. */
5556 static void
5557 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5559 dw_die_ref c;
5560 dw_attr_ref a;
5561 unsigned ix;
5563 /* To avoid infinite recursion. */
5564 if (die->die_mark)
5566 CHECKSUM (die->die_mark);
5567 return;
5569 die->die_mark = ++(*mark);
5571 CHECKSUM (die->die_tag);
5573 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5574 attr_checksum (a, ctx, mark);
5576 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5579 #undef CHECKSUM
5580 #undef CHECKSUM_BLOCK
5581 #undef CHECKSUM_STRING
5583 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5584 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5585 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5586 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5587 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5588 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5589 #define CHECKSUM_ATTR(FOO) \
5590 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5592 /* Calculate the checksum of a number in signed LEB128 format. */
5594 static void
5595 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5597 unsigned char byte;
5598 bool more;
5600 while (1)
5602 byte = (value & 0x7f);
5603 value >>= 7;
5604 more = !((value == 0 && (byte & 0x40) == 0)
5605 || (value == -1 && (byte & 0x40) != 0));
5606 if (more)
5607 byte |= 0x80;
5608 CHECKSUM (byte);
5609 if (!more)
5610 break;
5614 /* Calculate the checksum of a number in unsigned LEB128 format. */
5616 static void
5617 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5619 while (1)
5621 unsigned char byte = (value & 0x7f);
5622 value >>= 7;
5623 if (value != 0)
5624 /* More bytes to follow. */
5625 byte |= 0x80;
5626 CHECKSUM (byte);
5627 if (value == 0)
5628 break;
5632 /* Checksum the context of the DIE. This adds the names of any
5633 surrounding namespaces or structures to the checksum. */
5635 static void
5636 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5638 const char *name;
5639 dw_die_ref spec;
5640 int tag = die->die_tag;
5642 if (tag != DW_TAG_namespace
5643 && tag != DW_TAG_structure_type
5644 && tag != DW_TAG_class_type)
5645 return;
5647 name = get_AT_string (die, DW_AT_name);
5649 spec = get_AT_ref (die, DW_AT_specification);
5650 if (spec != NULL)
5651 die = spec;
5653 if (die->die_parent != NULL)
5654 checksum_die_context (die->die_parent, ctx);
5656 CHECKSUM_ULEB128 ('C');
5657 CHECKSUM_ULEB128 (tag);
5658 if (name != NULL)
5659 CHECKSUM_STRING (name);
5662 /* Calculate the checksum of a location expression. */
5664 static inline void
5665 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5667 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5668 were emitted as a DW_FORM_sdata instead of a location expression. */
5669 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5671 CHECKSUM_ULEB128 (DW_FORM_sdata);
5672 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5673 return;
5676 /* Otherwise, just checksum the raw location expression. */
5677 while (loc != NULL)
5679 hashval_t hash = 0;
5681 CHECKSUM_ULEB128 (loc->dtprel);
5682 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5683 hash = hash_loc_operands (loc, hash);
5684 CHECKSUM (hash);
5685 loc = loc->dw_loc_next;
5689 /* Calculate the checksum of an attribute. */
5691 static void
5692 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5693 struct md5_ctx *ctx, int *mark)
5695 dw_loc_descr_ref loc;
5696 rtx r;
5698 if (AT_class (at) == dw_val_class_die_ref)
5700 dw_die_ref target_die = AT_ref (at);
5702 /* For pointer and reference types, we checksum only the (qualified)
5703 name of the target type (if there is a name). For friend entries,
5704 we checksum only the (qualified) name of the target type or function.
5705 This allows the checksum to remain the same whether the target type
5706 is complete or not. */
5707 if ((at->dw_attr == DW_AT_type
5708 && (tag == DW_TAG_pointer_type
5709 || tag == DW_TAG_reference_type
5710 || tag == DW_TAG_rvalue_reference_type
5711 || tag == DW_TAG_ptr_to_member_type))
5712 || (at->dw_attr == DW_AT_friend
5713 && tag == DW_TAG_friend))
5715 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5717 if (name_attr != NULL)
5719 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5721 if (decl == NULL)
5722 decl = target_die;
5723 CHECKSUM_ULEB128 ('N');
5724 CHECKSUM_ULEB128 (at->dw_attr);
5725 if (decl->die_parent != NULL)
5726 checksum_die_context (decl->die_parent, ctx);
5727 CHECKSUM_ULEB128 ('E');
5728 CHECKSUM_STRING (AT_string (name_attr));
5729 return;
5733 /* For all other references to another DIE, we check to see if the
5734 target DIE has already been visited. If it has, we emit a
5735 backward reference; if not, we descend recursively. */
5736 if (target_die->die_mark > 0)
5738 CHECKSUM_ULEB128 ('R');
5739 CHECKSUM_ULEB128 (at->dw_attr);
5740 CHECKSUM_ULEB128 (target_die->die_mark);
5742 else
5744 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5746 if (decl == NULL)
5747 decl = target_die;
5748 target_die->die_mark = ++(*mark);
5749 CHECKSUM_ULEB128 ('T');
5750 CHECKSUM_ULEB128 (at->dw_attr);
5751 if (decl->die_parent != NULL)
5752 checksum_die_context (decl->die_parent, ctx);
5753 die_checksum_ordered (target_die, ctx, mark);
5755 return;
5758 CHECKSUM_ULEB128 ('A');
5759 CHECKSUM_ULEB128 (at->dw_attr);
5761 switch (AT_class (at))
5763 case dw_val_class_const:
5764 CHECKSUM_ULEB128 (DW_FORM_sdata);
5765 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5766 break;
5768 case dw_val_class_unsigned_const:
5769 CHECKSUM_ULEB128 (DW_FORM_sdata);
5770 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5771 break;
5773 case dw_val_class_const_double:
5774 CHECKSUM_ULEB128 (DW_FORM_block);
5775 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5776 CHECKSUM (at->dw_attr_val.v.val_double);
5777 break;
5779 case dw_val_class_vec:
5780 CHECKSUM_ULEB128 (DW_FORM_block);
5781 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5782 * at->dw_attr_val.v.val_vec.elt_size);
5783 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5784 (at->dw_attr_val.v.val_vec.length
5785 * at->dw_attr_val.v.val_vec.elt_size));
5786 break;
5788 case dw_val_class_flag:
5789 CHECKSUM_ULEB128 (DW_FORM_flag);
5790 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5791 break;
5793 case dw_val_class_str:
5794 CHECKSUM_ULEB128 (DW_FORM_string);
5795 CHECKSUM_STRING (AT_string (at));
5796 break;
5798 case dw_val_class_addr:
5799 r = AT_addr (at);
5800 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5801 CHECKSUM_ULEB128 (DW_FORM_string);
5802 CHECKSUM_STRING (XSTR (r, 0));
5803 break;
5805 case dw_val_class_offset:
5806 CHECKSUM_ULEB128 (DW_FORM_sdata);
5807 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5808 break;
5810 case dw_val_class_loc:
5811 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5812 loc_checksum_ordered (loc, ctx);
5813 break;
5815 case dw_val_class_fde_ref:
5816 case dw_val_class_lbl_id:
5817 case dw_val_class_lineptr:
5818 case dw_val_class_macptr:
5819 case dw_val_class_high_pc:
5820 break;
5822 case dw_val_class_file:
5823 CHECKSUM_ULEB128 (DW_FORM_string);
5824 CHECKSUM_STRING (AT_file (at)->filename);
5825 break;
5827 case dw_val_class_data8:
5828 CHECKSUM (at->dw_attr_val.v.val_data8);
5829 break;
5831 default:
5832 break;
5836 struct checksum_attributes
5838 dw_attr_ref at_name;
5839 dw_attr_ref at_type;
5840 dw_attr_ref at_friend;
5841 dw_attr_ref at_accessibility;
5842 dw_attr_ref at_address_class;
5843 dw_attr_ref at_allocated;
5844 dw_attr_ref at_artificial;
5845 dw_attr_ref at_associated;
5846 dw_attr_ref at_binary_scale;
5847 dw_attr_ref at_bit_offset;
5848 dw_attr_ref at_bit_size;
5849 dw_attr_ref at_bit_stride;
5850 dw_attr_ref at_byte_size;
5851 dw_attr_ref at_byte_stride;
5852 dw_attr_ref at_const_value;
5853 dw_attr_ref at_containing_type;
5854 dw_attr_ref at_count;
5855 dw_attr_ref at_data_location;
5856 dw_attr_ref at_data_member_location;
5857 dw_attr_ref at_decimal_scale;
5858 dw_attr_ref at_decimal_sign;
5859 dw_attr_ref at_default_value;
5860 dw_attr_ref at_digit_count;
5861 dw_attr_ref at_discr;
5862 dw_attr_ref at_discr_list;
5863 dw_attr_ref at_discr_value;
5864 dw_attr_ref at_encoding;
5865 dw_attr_ref at_endianity;
5866 dw_attr_ref at_explicit;
5867 dw_attr_ref at_is_optional;
5868 dw_attr_ref at_location;
5869 dw_attr_ref at_lower_bound;
5870 dw_attr_ref at_mutable;
5871 dw_attr_ref at_ordering;
5872 dw_attr_ref at_picture_string;
5873 dw_attr_ref at_prototyped;
5874 dw_attr_ref at_small;
5875 dw_attr_ref at_segment;
5876 dw_attr_ref at_string_length;
5877 dw_attr_ref at_threads_scaled;
5878 dw_attr_ref at_upper_bound;
5879 dw_attr_ref at_use_location;
5880 dw_attr_ref at_use_UTF8;
5881 dw_attr_ref at_variable_parameter;
5882 dw_attr_ref at_virtuality;
5883 dw_attr_ref at_visibility;
5884 dw_attr_ref at_vtable_elem_location;
5887 /* Collect the attributes that we will want to use for the checksum. */
5889 static void
5890 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5892 dw_attr_ref a;
5893 unsigned ix;
5895 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5897 switch (a->dw_attr)
5899 case DW_AT_name:
5900 attrs->at_name = a;
5901 break;
5902 case DW_AT_type:
5903 attrs->at_type = a;
5904 break;
5905 case DW_AT_friend:
5906 attrs->at_friend = a;
5907 break;
5908 case DW_AT_accessibility:
5909 attrs->at_accessibility = a;
5910 break;
5911 case DW_AT_address_class:
5912 attrs->at_address_class = a;
5913 break;
5914 case DW_AT_allocated:
5915 attrs->at_allocated = a;
5916 break;
5917 case DW_AT_artificial:
5918 attrs->at_artificial = a;
5919 break;
5920 case DW_AT_associated:
5921 attrs->at_associated = a;
5922 break;
5923 case DW_AT_binary_scale:
5924 attrs->at_binary_scale = a;
5925 break;
5926 case DW_AT_bit_offset:
5927 attrs->at_bit_offset = a;
5928 break;
5929 case DW_AT_bit_size:
5930 attrs->at_bit_size = a;
5931 break;
5932 case DW_AT_bit_stride:
5933 attrs->at_bit_stride = a;
5934 break;
5935 case DW_AT_byte_size:
5936 attrs->at_byte_size = a;
5937 break;
5938 case DW_AT_byte_stride:
5939 attrs->at_byte_stride = a;
5940 break;
5941 case DW_AT_const_value:
5942 attrs->at_const_value = a;
5943 break;
5944 case DW_AT_containing_type:
5945 attrs->at_containing_type = a;
5946 break;
5947 case DW_AT_count:
5948 attrs->at_count = a;
5949 break;
5950 case DW_AT_data_location:
5951 attrs->at_data_location = a;
5952 break;
5953 case DW_AT_data_member_location:
5954 attrs->at_data_member_location = a;
5955 break;
5956 case DW_AT_decimal_scale:
5957 attrs->at_decimal_scale = a;
5958 break;
5959 case DW_AT_decimal_sign:
5960 attrs->at_decimal_sign = a;
5961 break;
5962 case DW_AT_default_value:
5963 attrs->at_default_value = a;
5964 break;
5965 case DW_AT_digit_count:
5966 attrs->at_digit_count = a;
5967 break;
5968 case DW_AT_discr:
5969 attrs->at_discr = a;
5970 break;
5971 case DW_AT_discr_list:
5972 attrs->at_discr_list = a;
5973 break;
5974 case DW_AT_discr_value:
5975 attrs->at_discr_value = a;
5976 break;
5977 case DW_AT_encoding:
5978 attrs->at_encoding = a;
5979 break;
5980 case DW_AT_endianity:
5981 attrs->at_endianity = a;
5982 break;
5983 case DW_AT_explicit:
5984 attrs->at_explicit = a;
5985 break;
5986 case DW_AT_is_optional:
5987 attrs->at_is_optional = a;
5988 break;
5989 case DW_AT_location:
5990 attrs->at_location = a;
5991 break;
5992 case DW_AT_lower_bound:
5993 attrs->at_lower_bound = a;
5994 break;
5995 case DW_AT_mutable:
5996 attrs->at_mutable = a;
5997 break;
5998 case DW_AT_ordering:
5999 attrs->at_ordering = a;
6000 break;
6001 case DW_AT_picture_string:
6002 attrs->at_picture_string = a;
6003 break;
6004 case DW_AT_prototyped:
6005 attrs->at_prototyped = a;
6006 break;
6007 case DW_AT_small:
6008 attrs->at_small = a;
6009 break;
6010 case DW_AT_segment:
6011 attrs->at_segment = a;
6012 break;
6013 case DW_AT_string_length:
6014 attrs->at_string_length = a;
6015 break;
6016 case DW_AT_threads_scaled:
6017 attrs->at_threads_scaled = a;
6018 break;
6019 case DW_AT_upper_bound:
6020 attrs->at_upper_bound = a;
6021 break;
6022 case DW_AT_use_location:
6023 attrs->at_use_location = a;
6024 break;
6025 case DW_AT_use_UTF8:
6026 attrs->at_use_UTF8 = a;
6027 break;
6028 case DW_AT_variable_parameter:
6029 attrs->at_variable_parameter = a;
6030 break;
6031 case DW_AT_virtuality:
6032 attrs->at_virtuality = a;
6033 break;
6034 case DW_AT_visibility:
6035 attrs->at_visibility = a;
6036 break;
6037 case DW_AT_vtable_elem_location:
6038 attrs->at_vtable_elem_location = a;
6039 break;
6040 default:
6041 break;
6046 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6048 static void
6049 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6051 dw_die_ref c;
6052 dw_die_ref decl;
6053 struct checksum_attributes attrs;
6055 CHECKSUM_ULEB128 ('D');
6056 CHECKSUM_ULEB128 (die->die_tag);
6058 memset (&attrs, 0, sizeof (attrs));
6060 decl = get_AT_ref (die, DW_AT_specification);
6061 if (decl != NULL)
6062 collect_checksum_attributes (&attrs, decl);
6063 collect_checksum_attributes (&attrs, die);
6065 CHECKSUM_ATTR (attrs.at_name);
6066 CHECKSUM_ATTR (attrs.at_accessibility);
6067 CHECKSUM_ATTR (attrs.at_address_class);
6068 CHECKSUM_ATTR (attrs.at_allocated);
6069 CHECKSUM_ATTR (attrs.at_artificial);
6070 CHECKSUM_ATTR (attrs.at_associated);
6071 CHECKSUM_ATTR (attrs.at_binary_scale);
6072 CHECKSUM_ATTR (attrs.at_bit_offset);
6073 CHECKSUM_ATTR (attrs.at_bit_size);
6074 CHECKSUM_ATTR (attrs.at_bit_stride);
6075 CHECKSUM_ATTR (attrs.at_byte_size);
6076 CHECKSUM_ATTR (attrs.at_byte_stride);
6077 CHECKSUM_ATTR (attrs.at_const_value);
6078 CHECKSUM_ATTR (attrs.at_containing_type);
6079 CHECKSUM_ATTR (attrs.at_count);
6080 CHECKSUM_ATTR (attrs.at_data_location);
6081 CHECKSUM_ATTR (attrs.at_data_member_location);
6082 CHECKSUM_ATTR (attrs.at_decimal_scale);
6083 CHECKSUM_ATTR (attrs.at_decimal_sign);
6084 CHECKSUM_ATTR (attrs.at_default_value);
6085 CHECKSUM_ATTR (attrs.at_digit_count);
6086 CHECKSUM_ATTR (attrs.at_discr);
6087 CHECKSUM_ATTR (attrs.at_discr_list);
6088 CHECKSUM_ATTR (attrs.at_discr_value);
6089 CHECKSUM_ATTR (attrs.at_encoding);
6090 CHECKSUM_ATTR (attrs.at_endianity);
6091 CHECKSUM_ATTR (attrs.at_explicit);
6092 CHECKSUM_ATTR (attrs.at_is_optional);
6093 CHECKSUM_ATTR (attrs.at_location);
6094 CHECKSUM_ATTR (attrs.at_lower_bound);
6095 CHECKSUM_ATTR (attrs.at_mutable);
6096 CHECKSUM_ATTR (attrs.at_ordering);
6097 CHECKSUM_ATTR (attrs.at_picture_string);
6098 CHECKSUM_ATTR (attrs.at_prototyped);
6099 CHECKSUM_ATTR (attrs.at_small);
6100 CHECKSUM_ATTR (attrs.at_segment);
6101 CHECKSUM_ATTR (attrs.at_string_length);
6102 CHECKSUM_ATTR (attrs.at_threads_scaled);
6103 CHECKSUM_ATTR (attrs.at_upper_bound);
6104 CHECKSUM_ATTR (attrs.at_use_location);
6105 CHECKSUM_ATTR (attrs.at_use_UTF8);
6106 CHECKSUM_ATTR (attrs.at_variable_parameter);
6107 CHECKSUM_ATTR (attrs.at_virtuality);
6108 CHECKSUM_ATTR (attrs.at_visibility);
6109 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6110 CHECKSUM_ATTR (attrs.at_type);
6111 CHECKSUM_ATTR (attrs.at_friend);
6113 /* Checksum the child DIEs. */
6114 c = die->die_child;
6115 if (c) do {
6116 dw_attr_ref name_attr;
6118 c = c->die_sib;
6119 name_attr = get_AT (c, DW_AT_name);
6120 if (is_template_instantiation (c))
6122 /* Ignore instantiations of member type and function templates. */
6124 else if (name_attr != NULL
6125 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6127 /* Use a shallow checksum for named nested types and member
6128 functions. */
6129 CHECKSUM_ULEB128 ('S');
6130 CHECKSUM_ULEB128 (c->die_tag);
6131 CHECKSUM_STRING (AT_string (name_attr));
6133 else
6135 /* Use a deep checksum for other children. */
6136 /* Mark this DIE so it gets processed when unmarking. */
6137 if (c->die_mark == 0)
6138 c->die_mark = -1;
6139 die_checksum_ordered (c, ctx, mark);
6141 } while (c != die->die_child);
6143 CHECKSUM_ULEB128 (0);
6146 /* Add a type name and tag to a hash. */
6147 static void
6148 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6150 CHECKSUM_ULEB128 (tag);
6151 CHECKSUM_STRING (name);
6154 #undef CHECKSUM
6155 #undef CHECKSUM_STRING
6156 #undef CHECKSUM_ATTR
6157 #undef CHECKSUM_LEB128
6158 #undef CHECKSUM_ULEB128
6160 /* Generate the type signature for DIE. This is computed by generating an
6161 MD5 checksum over the DIE's tag, its relevant attributes, and its
6162 children. Attributes that are references to other DIEs are processed
6163 by recursion, using the MARK field to prevent infinite recursion.
6164 If the DIE is nested inside a namespace or another type, we also
6165 need to include that context in the signature. The lower 64 bits
6166 of the resulting MD5 checksum comprise the signature. */
6168 static void
6169 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6171 int mark;
6172 const char *name;
6173 unsigned char checksum[16];
6174 struct md5_ctx ctx;
6175 dw_die_ref decl;
6176 dw_die_ref parent;
6178 name = get_AT_string (die, DW_AT_name);
6179 decl = get_AT_ref (die, DW_AT_specification);
6180 parent = get_die_parent (die);
6182 /* First, compute a signature for just the type name (and its surrounding
6183 context, if any. This is stored in the type unit DIE for link-time
6184 ODR (one-definition rule) checking. */
6186 if (is_cxx () && name != NULL)
6188 md5_init_ctx (&ctx);
6190 /* Checksum the names of surrounding namespaces and structures. */
6191 if (parent != NULL)
6192 checksum_die_context (parent, &ctx);
6194 /* Checksum the current DIE. */
6195 die_odr_checksum (die->die_tag, name, &ctx);
6196 md5_finish_ctx (&ctx, checksum);
6198 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6201 /* Next, compute the complete type signature. */
6203 md5_init_ctx (&ctx);
6204 mark = 1;
6205 die->die_mark = mark;
6207 /* Checksum the names of surrounding namespaces and structures. */
6208 if (parent != NULL)
6209 checksum_die_context (parent, &ctx);
6211 /* Checksum the DIE and its children. */
6212 die_checksum_ordered (die, &ctx, &mark);
6213 unmark_all_dies (die);
6214 md5_finish_ctx (&ctx, checksum);
6216 /* Store the signature in the type node and link the type DIE and the
6217 type node together. */
6218 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6219 DWARF_TYPE_SIGNATURE_SIZE);
6220 die->comdat_type_p = true;
6221 die->die_id.die_type_node = type_node;
6222 type_node->type_die = die;
6224 /* If the DIE is a specification, link its declaration to the type node
6225 as well. */
6226 if (decl != NULL)
6228 decl->comdat_type_p = true;
6229 decl->die_id.die_type_node = type_node;
6233 /* Do the location expressions look same? */
6234 static inline int
6235 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6237 return loc1->dw_loc_opc == loc2->dw_loc_opc
6238 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6239 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6242 /* Do the values look the same? */
6243 static int
6244 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6246 dw_loc_descr_ref loc1, loc2;
6247 rtx r1, r2;
6249 if (v1->val_class != v2->val_class)
6250 return 0;
6252 switch (v1->val_class)
6254 case dw_val_class_const:
6255 return v1->v.val_int == v2->v.val_int;
6256 case dw_val_class_unsigned_const:
6257 return v1->v.val_unsigned == v2->v.val_unsigned;
6258 case dw_val_class_const_double:
6259 return v1->v.val_double.high == v2->v.val_double.high
6260 && v1->v.val_double.low == v2->v.val_double.low;
6261 case dw_val_class_vec:
6262 if (v1->v.val_vec.length != v2->v.val_vec.length
6263 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6264 return 0;
6265 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6266 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6267 return 0;
6268 return 1;
6269 case dw_val_class_flag:
6270 return v1->v.val_flag == v2->v.val_flag;
6271 case dw_val_class_str:
6272 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6274 case dw_val_class_addr:
6275 r1 = v1->v.val_addr;
6276 r2 = v2->v.val_addr;
6277 if (GET_CODE (r1) != GET_CODE (r2))
6278 return 0;
6279 return !rtx_equal_p (r1, r2);
6281 case dw_val_class_offset:
6282 return v1->v.val_offset == v2->v.val_offset;
6284 case dw_val_class_loc:
6285 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6286 loc1 && loc2;
6287 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6288 if (!same_loc_p (loc1, loc2, mark))
6289 return 0;
6290 return !loc1 && !loc2;
6292 case dw_val_class_die_ref:
6293 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6295 case dw_val_class_fde_ref:
6296 case dw_val_class_vms_delta:
6297 case dw_val_class_lbl_id:
6298 case dw_val_class_lineptr:
6299 case dw_val_class_macptr:
6300 case dw_val_class_high_pc:
6301 return 1;
6303 case dw_val_class_file:
6304 return v1->v.val_file == v2->v.val_file;
6306 case dw_val_class_data8:
6307 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6309 default:
6310 return 1;
6314 /* Do the attributes look the same? */
6316 static int
6317 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6319 if (at1->dw_attr != at2->dw_attr)
6320 return 0;
6322 /* We don't care that this was compiled with a different compiler
6323 snapshot; if the output is the same, that's what matters. */
6324 if (at1->dw_attr == DW_AT_producer)
6325 return 1;
6327 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6330 /* Do the dies look the same? */
6332 static int
6333 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6335 dw_die_ref c1, c2;
6336 dw_attr_ref a1;
6337 unsigned ix;
6339 /* To avoid infinite recursion. */
6340 if (die1->die_mark)
6341 return die1->die_mark == die2->die_mark;
6342 die1->die_mark = die2->die_mark = ++(*mark);
6344 if (die1->die_tag != die2->die_tag)
6345 return 0;
6347 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6348 return 0;
6350 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6351 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6352 return 0;
6354 c1 = die1->die_child;
6355 c2 = die2->die_child;
6356 if (! c1)
6358 if (c2)
6359 return 0;
6361 else
6362 for (;;)
6364 if (!same_die_p (c1, c2, mark))
6365 return 0;
6366 c1 = c1->die_sib;
6367 c2 = c2->die_sib;
6368 if (c1 == die1->die_child)
6370 if (c2 == die2->die_child)
6371 break;
6372 else
6373 return 0;
6377 return 1;
6380 /* Do the dies look the same? Wrapper around same_die_p. */
6382 static int
6383 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6385 int mark = 0;
6386 int ret = same_die_p (die1, die2, &mark);
6388 unmark_all_dies (die1);
6389 unmark_all_dies (die2);
6391 return ret;
6394 /* The prefix to attach to symbols on DIEs in the current comdat debug
6395 info section. */
6396 static const char *comdat_symbol_id;
6398 /* The index of the current symbol within the current comdat CU. */
6399 static unsigned int comdat_symbol_number;
6401 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6402 children, and set comdat_symbol_id accordingly. */
6404 static void
6405 compute_section_prefix (dw_die_ref unit_die)
6407 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6408 const char *base = die_name ? lbasename (die_name) : "anonymous";
6409 char *name = XALLOCAVEC (char, strlen (base) + 64);
6410 char *p;
6411 int i, mark;
6412 unsigned char checksum[16];
6413 struct md5_ctx ctx;
6415 /* Compute the checksum of the DIE, then append part of it as hex digits to
6416 the name filename of the unit. */
6418 md5_init_ctx (&ctx);
6419 mark = 0;
6420 die_checksum (unit_die, &ctx, &mark);
6421 unmark_all_dies (unit_die);
6422 md5_finish_ctx (&ctx, checksum);
6424 sprintf (name, "%s.", base);
6425 clean_symbol_name (name);
6427 p = name + strlen (name);
6428 for (i = 0; i < 4; i++)
6430 sprintf (p, "%.2x", checksum[i]);
6431 p += 2;
6434 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6435 comdat_symbol_number = 0;
6438 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6440 static int
6441 is_type_die (dw_die_ref die)
6443 switch (die->die_tag)
6445 case DW_TAG_array_type:
6446 case DW_TAG_class_type:
6447 case DW_TAG_interface_type:
6448 case DW_TAG_enumeration_type:
6449 case DW_TAG_pointer_type:
6450 case DW_TAG_reference_type:
6451 case DW_TAG_rvalue_reference_type:
6452 case DW_TAG_string_type:
6453 case DW_TAG_structure_type:
6454 case DW_TAG_subroutine_type:
6455 case DW_TAG_union_type:
6456 case DW_TAG_ptr_to_member_type:
6457 case DW_TAG_set_type:
6458 case DW_TAG_subrange_type:
6459 case DW_TAG_base_type:
6460 case DW_TAG_const_type:
6461 case DW_TAG_file_type:
6462 case DW_TAG_packed_type:
6463 case DW_TAG_volatile_type:
6464 case DW_TAG_typedef:
6465 return 1;
6466 default:
6467 return 0;
6471 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6472 Basically, we want to choose the bits that are likely to be shared between
6473 compilations (types) and leave out the bits that are specific to individual
6474 compilations (functions). */
6476 static int
6477 is_comdat_die (dw_die_ref c)
6479 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6480 we do for stabs. The advantage is a greater likelihood of sharing between
6481 objects that don't include headers in the same order (and therefore would
6482 put the base types in a different comdat). jason 8/28/00 */
6484 if (c->die_tag == DW_TAG_base_type)
6485 return 0;
6487 if (c->die_tag == DW_TAG_pointer_type
6488 || c->die_tag == DW_TAG_reference_type
6489 || c->die_tag == DW_TAG_rvalue_reference_type
6490 || c->die_tag == DW_TAG_const_type
6491 || c->die_tag == DW_TAG_volatile_type)
6493 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6495 return t ? is_comdat_die (t) : 0;
6498 return is_type_die (c);
6501 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6502 compilation unit. */
6504 static int
6505 is_symbol_die (dw_die_ref c)
6507 return (is_type_die (c)
6508 || is_declaration_die (c)
6509 || c->die_tag == DW_TAG_namespace
6510 || c->die_tag == DW_TAG_module);
6513 /* Returns true iff C is a compile-unit DIE. */
6515 static inline bool
6516 is_cu_die (dw_die_ref c)
6518 return c && c->die_tag == DW_TAG_compile_unit;
6521 /* Returns true iff C is a unit DIE of some sort. */
6523 static inline bool
6524 is_unit_die (dw_die_ref c)
6526 return c && (c->die_tag == DW_TAG_compile_unit
6527 || c->die_tag == DW_TAG_partial_unit
6528 || c->die_tag == DW_TAG_type_unit);
6531 /* Returns true iff C is a namespace DIE. */
6533 static inline bool
6534 is_namespace_die (dw_die_ref c)
6536 return c && c->die_tag == DW_TAG_namespace;
6539 /* Returns true iff C is a class or structure DIE. */
6541 static inline bool
6542 is_class_die (dw_die_ref c)
6544 return c && (c->die_tag == DW_TAG_class_type
6545 || c->die_tag == DW_TAG_structure_type);
6548 /* Return non-zero if this DIE is a template parameter. */
6550 static inline bool
6551 is_template_parameter (dw_die_ref die)
6553 switch (die->die_tag)
6555 case DW_TAG_template_type_param:
6556 case DW_TAG_template_value_param:
6557 case DW_TAG_GNU_template_template_param:
6558 case DW_TAG_GNU_template_parameter_pack:
6559 return true;
6560 default:
6561 return false;
6565 /* Return non-zero if this DIE represents a template instantiation. */
6567 static inline bool
6568 is_template_instantiation (dw_die_ref die)
6570 dw_die_ref c;
6572 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6573 return false;
6574 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6575 return false;
6578 static char *
6579 gen_internal_sym (const char *prefix)
6581 char buf[256];
6583 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6584 return xstrdup (buf);
6587 /* Assign symbols to all worthy DIEs under DIE. */
6589 static void
6590 assign_symbol_names (dw_die_ref die)
6592 dw_die_ref c;
6594 if (is_symbol_die (die) && !die->comdat_type_p)
6596 if (comdat_symbol_id)
6598 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6600 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6601 comdat_symbol_id, comdat_symbol_number++);
6602 die->die_id.die_symbol = xstrdup (p);
6604 else
6605 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6608 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6611 struct cu_hash_table_entry
6613 dw_die_ref cu;
6614 unsigned min_comdat_num, max_comdat_num;
6615 struct cu_hash_table_entry *next;
6618 /* Helpers to manipulate hash table of CUs. */
6620 struct cu_hash_table_entry_hasher
6622 typedef cu_hash_table_entry value_type;
6623 typedef die_struct compare_type;
6624 static inline hashval_t hash (const value_type *);
6625 static inline bool equal (const value_type *, const compare_type *);
6626 static inline void remove (value_type *);
6629 inline hashval_t
6630 cu_hash_table_entry_hasher::hash (const value_type *entry)
6632 return htab_hash_string (entry->cu->die_id.die_symbol);
6635 inline bool
6636 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6637 const compare_type *entry2)
6639 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6642 inline void
6643 cu_hash_table_entry_hasher::remove (value_type *entry)
6645 struct cu_hash_table_entry *next;
6647 while (entry)
6649 next = entry->next;
6650 free (entry);
6651 entry = next;
6655 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6657 /* Check whether we have already seen this CU and set up SYM_NUM
6658 accordingly. */
6659 static int
6660 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6662 struct cu_hash_table_entry dummy;
6663 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6665 dummy.max_comdat_num = 0;
6667 slot = htable.find_slot_with_hash (cu,
6668 htab_hash_string (cu->die_id.die_symbol),
6669 INSERT);
6670 entry = *slot;
6672 for (; entry; last = entry, entry = entry->next)
6674 if (same_die_p_wrap (cu, entry->cu))
6675 break;
6678 if (entry)
6680 *sym_num = entry->min_comdat_num;
6681 return 1;
6684 entry = XCNEW (struct cu_hash_table_entry);
6685 entry->cu = cu;
6686 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6687 entry->next = *slot;
6688 *slot = entry;
6690 return 0;
6693 /* Record SYM_NUM to record of CU in HTABLE. */
6694 static void
6695 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6696 unsigned int sym_num)
6698 struct cu_hash_table_entry **slot, *entry;
6700 slot = htable.find_slot_with_hash (cu,
6701 htab_hash_string (cu->die_id.die_symbol),
6702 NO_INSERT);
6703 entry = *slot;
6705 entry->max_comdat_num = sym_num;
6708 /* Traverse the DIE (which is always comp_unit_die), and set up
6709 additional compilation units for each of the include files we see
6710 bracketed by BINCL/EINCL. */
6712 static void
6713 break_out_includes (dw_die_ref die)
6715 dw_die_ref c;
6716 dw_die_ref unit = NULL;
6717 limbo_die_node *node, **pnode;
6718 cu_hash_type cu_hash_table;
6720 c = die->die_child;
6721 if (c) do {
6722 dw_die_ref prev = c;
6723 c = c->die_sib;
6724 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6725 || (unit && is_comdat_die (c)))
6727 dw_die_ref next = c->die_sib;
6729 /* This DIE is for a secondary CU; remove it from the main one. */
6730 remove_child_with_prev (c, prev);
6732 if (c->die_tag == DW_TAG_GNU_BINCL)
6733 unit = push_new_compile_unit (unit, c);
6734 else if (c->die_tag == DW_TAG_GNU_EINCL)
6735 unit = pop_compile_unit (unit);
6736 else
6737 add_child_die (unit, c);
6738 c = next;
6739 if (c == die->die_child)
6740 break;
6742 } while (c != die->die_child);
6744 #if 0
6745 /* We can only use this in debugging, since the frontend doesn't check
6746 to make sure that we leave every include file we enter. */
6747 gcc_assert (!unit);
6748 #endif
6750 assign_symbol_names (die);
6751 cu_hash_table.create (10);
6752 for (node = limbo_die_list, pnode = &limbo_die_list;
6753 node;
6754 node = node->next)
6756 int is_dupl;
6758 compute_section_prefix (node->die);
6759 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6760 &comdat_symbol_number);
6761 assign_symbol_names (node->die);
6762 if (is_dupl)
6763 *pnode = node->next;
6764 else
6766 pnode = &node->next;
6767 record_comdat_symbol_number (node->die, cu_hash_table,
6768 comdat_symbol_number);
6771 cu_hash_table.dispose ();
6774 /* Return non-zero if this DIE is a declaration. */
6776 static int
6777 is_declaration_die (dw_die_ref die)
6779 dw_attr_ref a;
6780 unsigned ix;
6782 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6783 if (a->dw_attr == DW_AT_declaration)
6784 return 1;
6786 return 0;
6789 /* Return non-zero if this DIE is nested inside a subprogram. */
6791 static int
6792 is_nested_in_subprogram (dw_die_ref die)
6794 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6796 if (decl == NULL)
6797 decl = die;
6798 return local_scope_p (decl);
6801 /* Return non-zero if this DIE contains a defining declaration of a
6802 subprogram. */
6804 static int
6805 contains_subprogram_definition (dw_die_ref die)
6807 dw_die_ref c;
6809 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6810 return 1;
6811 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6812 return 0;
6815 /* Return non-zero if this is a type DIE that should be moved to a
6816 COMDAT .debug_types section. */
6818 static int
6819 should_move_die_to_comdat (dw_die_ref die)
6821 switch (die->die_tag)
6823 case DW_TAG_class_type:
6824 case DW_TAG_structure_type:
6825 case DW_TAG_enumeration_type:
6826 case DW_TAG_union_type:
6827 /* Don't move declarations, inlined instances, or types nested in a
6828 subprogram. */
6829 if (is_declaration_die (die)
6830 || get_AT (die, DW_AT_abstract_origin)
6831 || is_nested_in_subprogram (die))
6832 return 0;
6833 /* A type definition should never contain a subprogram definition. */
6834 gcc_assert (!contains_subprogram_definition (die));
6835 return 1;
6836 case DW_TAG_array_type:
6837 case DW_TAG_interface_type:
6838 case DW_TAG_pointer_type:
6839 case DW_TAG_reference_type:
6840 case DW_TAG_rvalue_reference_type:
6841 case DW_TAG_string_type:
6842 case DW_TAG_subroutine_type:
6843 case DW_TAG_ptr_to_member_type:
6844 case DW_TAG_set_type:
6845 case DW_TAG_subrange_type:
6846 case DW_TAG_base_type:
6847 case DW_TAG_const_type:
6848 case DW_TAG_file_type:
6849 case DW_TAG_packed_type:
6850 case DW_TAG_volatile_type:
6851 case DW_TAG_typedef:
6852 default:
6853 return 0;
6857 /* Make a clone of DIE. */
6859 static dw_die_ref
6860 clone_die (dw_die_ref die)
6862 dw_die_ref clone;
6863 dw_attr_ref a;
6864 unsigned ix;
6866 clone = ggc_alloc_cleared_die_node ();
6867 clone->die_tag = die->die_tag;
6869 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6870 add_dwarf_attr (clone, a);
6872 return clone;
6875 /* Make a clone of the tree rooted at DIE. */
6877 static dw_die_ref
6878 clone_tree (dw_die_ref die)
6880 dw_die_ref c;
6881 dw_die_ref clone = clone_die (die);
6883 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6885 return clone;
6888 /* Make a clone of DIE as a declaration. */
6890 static dw_die_ref
6891 clone_as_declaration (dw_die_ref die)
6893 dw_die_ref clone;
6894 dw_die_ref decl;
6895 dw_attr_ref a;
6896 unsigned ix;
6898 /* If the DIE is already a declaration, just clone it. */
6899 if (is_declaration_die (die))
6900 return clone_die (die);
6902 /* If the DIE is a specification, just clone its declaration DIE. */
6903 decl = get_AT_ref (die, DW_AT_specification);
6904 if (decl != NULL)
6906 clone = clone_die (decl);
6907 if (die->comdat_type_p)
6908 add_AT_die_ref (clone, DW_AT_signature, die);
6909 return clone;
6912 clone = ggc_alloc_cleared_die_node ();
6913 clone->die_tag = die->die_tag;
6915 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6917 /* We don't want to copy over all attributes.
6918 For example we don't want DW_AT_byte_size because otherwise we will no
6919 longer have a declaration and GDB will treat it as a definition. */
6921 switch (a->dw_attr)
6923 case DW_AT_artificial:
6924 case DW_AT_containing_type:
6925 case DW_AT_external:
6926 case DW_AT_name:
6927 case DW_AT_type:
6928 case DW_AT_virtuality:
6929 case DW_AT_linkage_name:
6930 case DW_AT_MIPS_linkage_name:
6931 add_dwarf_attr (clone, a);
6932 break;
6933 case DW_AT_byte_size:
6934 default:
6935 break;
6939 if (die->comdat_type_p)
6940 add_AT_die_ref (clone, DW_AT_signature, die);
6942 add_AT_flag (clone, DW_AT_declaration, 1);
6943 return clone;
6947 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6949 struct decl_table_entry
6951 dw_die_ref orig;
6952 dw_die_ref copy;
6955 /* Helpers to manipulate hash table of copied declarations. */
6957 /* Hashtable helpers. */
6959 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6961 typedef decl_table_entry value_type;
6962 typedef die_struct compare_type;
6963 static inline hashval_t hash (const value_type *);
6964 static inline bool equal (const value_type *, const compare_type *);
6967 inline hashval_t
6968 decl_table_entry_hasher::hash (const value_type *entry)
6970 return htab_hash_pointer (entry->orig);
6973 inline bool
6974 decl_table_entry_hasher::equal (const value_type *entry1,
6975 const compare_type *entry2)
6977 return entry1->orig == entry2;
6980 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6982 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6983 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6984 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6985 to check if the ancestor has already been copied into UNIT. */
6987 static dw_die_ref
6988 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6990 dw_die_ref parent = die->die_parent;
6991 dw_die_ref new_parent = unit;
6992 dw_die_ref copy;
6993 decl_table_entry **slot = NULL;
6994 struct decl_table_entry *entry = NULL;
6996 if (decl_table.is_created ())
6998 /* Check if the entry has already been copied to UNIT. */
6999 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7000 INSERT);
7001 if (*slot != HTAB_EMPTY_ENTRY)
7003 entry = *slot;
7004 return entry->copy;
7007 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7008 entry = XCNEW (struct decl_table_entry);
7009 entry->orig = die;
7010 entry->copy = NULL;
7011 *slot = entry;
7014 if (parent != NULL)
7016 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7017 if (spec != NULL)
7018 parent = spec;
7019 if (!is_unit_die (parent))
7020 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7023 copy = clone_as_declaration (die);
7024 add_child_die (new_parent, copy);
7026 if (decl_table.is_created ())
7028 /* Record the pointer to the copy. */
7029 entry->copy = copy;
7032 return copy;
7034 /* Copy the declaration context to the new type unit DIE. This includes
7035 any surrounding namespace or type declarations. If the DIE has an
7036 AT_specification attribute, it also includes attributes and children
7037 attached to the specification, and returns a pointer to the original
7038 parent of the declaration DIE. Returns NULL otherwise. */
7040 static dw_die_ref
7041 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7043 dw_die_ref decl;
7044 dw_die_ref new_decl;
7045 dw_die_ref orig_parent = NULL;
7047 decl = get_AT_ref (die, DW_AT_specification);
7048 if (decl == NULL)
7049 decl = die;
7050 else
7052 unsigned ix;
7053 dw_die_ref c;
7054 dw_attr_ref a;
7056 /* The original DIE will be changed to a declaration, and must
7057 be moved to be a child of the original declaration DIE. */
7058 orig_parent = decl->die_parent;
7060 /* Copy the type node pointer from the new DIE to the original
7061 declaration DIE so we can forward references later. */
7062 decl->comdat_type_p = true;
7063 decl->die_id.die_type_node = die->die_id.die_type_node;
7065 remove_AT (die, DW_AT_specification);
7067 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7069 if (a->dw_attr != DW_AT_name
7070 && a->dw_attr != DW_AT_declaration
7071 && a->dw_attr != DW_AT_external)
7072 add_dwarf_attr (die, a);
7075 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7078 if (decl->die_parent != NULL
7079 && !is_unit_die (decl->die_parent))
7081 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7082 if (new_decl != NULL)
7084 remove_AT (new_decl, DW_AT_signature);
7085 add_AT_specification (die, new_decl);
7089 return orig_parent;
7092 /* Generate the skeleton ancestor tree for the given NODE, then clone
7093 the DIE and add the clone into the tree. */
7095 static void
7096 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7098 if (node->new_die != NULL)
7099 return;
7101 node->new_die = clone_as_declaration (node->old_die);
7103 if (node->parent != NULL)
7105 generate_skeleton_ancestor_tree (node->parent);
7106 add_child_die (node->parent->new_die, node->new_die);
7110 /* Generate a skeleton tree of DIEs containing any declarations that are
7111 found in the original tree. We traverse the tree looking for declaration
7112 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7114 static void
7115 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7117 skeleton_chain_node node;
7118 dw_die_ref c;
7119 dw_die_ref first;
7120 dw_die_ref prev = NULL;
7121 dw_die_ref next = NULL;
7123 node.parent = parent;
7125 first = c = parent->old_die->die_child;
7126 if (c)
7127 next = c->die_sib;
7128 if (c) do {
7129 if (prev == NULL || prev->die_sib == c)
7130 prev = c;
7131 c = next;
7132 next = (c == first ? NULL : c->die_sib);
7133 node.old_die = c;
7134 node.new_die = NULL;
7135 if (is_declaration_die (c))
7137 if (is_template_instantiation (c))
7139 /* Instantiated templates do not need to be cloned into the
7140 type unit. Just move the DIE and its children back to
7141 the skeleton tree (in the main CU). */
7142 remove_child_with_prev (c, prev);
7143 add_child_die (parent->new_die, c);
7144 c = prev;
7146 else
7148 /* Clone the existing DIE, move the original to the skeleton
7149 tree (which is in the main CU), and put the clone, with
7150 all the original's children, where the original came from
7151 (which is about to be moved to the type unit). */
7152 dw_die_ref clone = clone_die (c);
7153 move_all_children (c, clone);
7155 replace_child (c, clone, prev);
7156 generate_skeleton_ancestor_tree (parent);
7157 add_child_die (parent->new_die, c);
7158 node.new_die = c;
7159 c = clone;
7162 generate_skeleton_bottom_up (&node);
7163 } while (next != NULL);
7166 /* Wrapper function for generate_skeleton_bottom_up. */
7168 static dw_die_ref
7169 generate_skeleton (dw_die_ref die)
7171 skeleton_chain_node node;
7173 node.old_die = die;
7174 node.new_die = NULL;
7175 node.parent = NULL;
7177 /* If this type definition is nested inside another type,
7178 and is not an instantiation of a template, always leave
7179 at least a declaration in its place. */
7180 if (die->die_parent != NULL
7181 && is_type_die (die->die_parent)
7182 && !is_template_instantiation (die))
7183 node.new_die = clone_as_declaration (die);
7185 generate_skeleton_bottom_up (&node);
7186 return node.new_die;
7189 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7190 declaration. The original DIE is moved to a new compile unit so that
7191 existing references to it follow it to the new location. If any of the
7192 original DIE's descendants is a declaration, we need to replace the
7193 original DIE with a skeleton tree and move the declarations back into the
7194 skeleton tree. */
7196 static dw_die_ref
7197 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7198 dw_die_ref prev)
7200 dw_die_ref skeleton, orig_parent;
7202 /* Copy the declaration context to the type unit DIE. If the returned
7203 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7204 that DIE. */
7205 orig_parent = copy_declaration_context (unit, child);
7207 skeleton = generate_skeleton (child);
7208 if (skeleton == NULL)
7209 remove_child_with_prev (child, prev);
7210 else
7212 skeleton->comdat_type_p = true;
7213 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7215 /* If the original DIE was a specification, we need to put
7216 the skeleton under the parent DIE of the declaration.
7217 This leaves the original declaration in the tree, but
7218 it will be pruned later since there are no longer any
7219 references to it. */
7220 if (orig_parent != NULL)
7222 remove_child_with_prev (child, prev);
7223 add_child_die (orig_parent, skeleton);
7225 else
7226 replace_child (child, skeleton, prev);
7229 return skeleton;
7232 /* Traverse the DIE and set up additional .debug_types sections for each
7233 type worthy of being placed in a COMDAT section. */
7235 static void
7236 break_out_comdat_types (dw_die_ref die)
7238 dw_die_ref c;
7239 dw_die_ref first;
7240 dw_die_ref prev = NULL;
7241 dw_die_ref next = NULL;
7242 dw_die_ref unit = NULL;
7244 first = c = die->die_child;
7245 if (c)
7246 next = c->die_sib;
7247 if (c) do {
7248 if (prev == NULL || prev->die_sib == c)
7249 prev = c;
7250 c = next;
7251 next = (c == first ? NULL : c->die_sib);
7252 if (should_move_die_to_comdat (c))
7254 dw_die_ref replacement;
7255 comdat_type_node_ref type_node;
7257 /* Break out nested types into their own type units. */
7258 break_out_comdat_types (c);
7260 /* Create a new type unit DIE as the root for the new tree, and
7261 add it to the list of comdat types. */
7262 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7263 add_AT_unsigned (unit, DW_AT_language,
7264 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7265 type_node = ggc_alloc_cleared_comdat_type_node ();
7266 type_node->root_die = unit;
7267 type_node->next = comdat_type_list;
7268 comdat_type_list = type_node;
7270 /* Generate the type signature. */
7271 generate_type_signature (c, type_node);
7273 /* Copy the declaration context, attributes, and children of the
7274 declaration into the new type unit DIE, then remove this DIE
7275 from the main CU (or replace it with a skeleton if necessary). */
7276 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7277 type_node->skeleton_die = replacement;
7279 /* Add the DIE to the new compunit. */
7280 add_child_die (unit, c);
7282 if (replacement != NULL)
7283 c = replacement;
7285 else if (c->die_tag == DW_TAG_namespace
7286 || c->die_tag == DW_TAG_class_type
7287 || c->die_tag == DW_TAG_structure_type
7288 || c->die_tag == DW_TAG_union_type)
7290 /* Look for nested types that can be broken out. */
7291 break_out_comdat_types (c);
7293 } while (next != NULL);
7296 /* Like clone_tree, but additionally enter all the children into
7297 the hash table decl_table. */
7299 static dw_die_ref
7300 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7302 dw_die_ref c;
7303 dw_die_ref clone = clone_die (die);
7304 struct decl_table_entry *entry;
7305 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7306 htab_hash_pointer (die), INSERT);
7307 /* Assert that DIE isn't in the hash table yet. If it would be there
7308 before, the ancestors would be necessarily there as well, therefore
7309 clone_tree_hash wouldn't be called. */
7310 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7311 entry = XCNEW (struct decl_table_entry);
7312 entry->orig = die;
7313 entry->copy = clone;
7314 *slot = entry;
7316 FOR_EACH_CHILD (die, c,
7317 add_child_die (clone, clone_tree_hash (c, decl_table)));
7319 return clone;
7322 /* Walk the DIE and its children, looking for references to incomplete
7323 or trivial types that are unmarked (i.e., that are not in the current
7324 type_unit). */
7326 static void
7327 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7329 dw_die_ref c;
7330 dw_attr_ref a;
7331 unsigned ix;
7333 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7335 if (AT_class (a) == dw_val_class_die_ref)
7337 dw_die_ref targ = AT_ref (a);
7338 decl_table_entry **slot;
7339 struct decl_table_entry *entry;
7341 if (targ->die_mark != 0 || targ->comdat_type_p)
7342 continue;
7344 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7345 INSERT);
7347 if (*slot != HTAB_EMPTY_ENTRY)
7349 /* TARG has already been copied, so we just need to
7350 modify the reference to point to the copy. */
7351 entry = *slot;
7352 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7354 else
7356 dw_die_ref parent = unit;
7357 dw_die_ref copy = clone_die (targ);
7359 /* Record in DECL_TABLE that TARG has been copied.
7360 Need to do this now, before the recursive call,
7361 because DECL_TABLE may be expanded and SLOT
7362 would no longer be a valid pointer. */
7363 entry = XCNEW (struct decl_table_entry);
7364 entry->orig = targ;
7365 entry->copy = copy;
7366 *slot = entry;
7368 FOR_EACH_CHILD (targ, c,
7369 add_child_die (copy,
7370 clone_tree_hash (c, decl_table)));
7372 /* Make sure the cloned tree is marked as part of the
7373 type unit. */
7374 mark_dies (copy);
7376 /* If TARG has surrounding context, copy its ancestor tree
7377 into the new type unit. */
7378 if (targ->die_parent != NULL
7379 && !is_unit_die (targ->die_parent))
7380 parent = copy_ancestor_tree (unit, targ->die_parent,
7381 decl_table);
7383 add_child_die (parent, copy);
7384 a->dw_attr_val.v.val_die_ref.die = copy;
7386 /* Make sure the newly-copied DIE is walked. If it was
7387 installed in a previously-added context, it won't
7388 get visited otherwise. */
7389 if (parent != unit)
7391 /* Find the highest point of the newly-added tree,
7392 mark each node along the way, and walk from there. */
7393 parent->die_mark = 1;
7394 while (parent->die_parent
7395 && parent->die_parent->die_mark == 0)
7397 parent = parent->die_parent;
7398 parent->die_mark = 1;
7400 copy_decls_walk (unit, parent, decl_table);
7406 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7409 /* Copy declarations for "unworthy" types into the new comdat section.
7410 Incomplete types, modified types, and certain other types aren't broken
7411 out into comdat sections of their own, so they don't have a signature,
7412 and we need to copy the declaration into the same section so that we
7413 don't have an external reference. */
7415 static void
7416 copy_decls_for_unworthy_types (dw_die_ref unit)
7418 decl_hash_type decl_table;
7420 mark_dies (unit);
7421 decl_table.create (10);
7422 copy_decls_walk (unit, unit, decl_table);
7423 decl_table.dispose ();
7424 unmark_dies (unit);
7427 /* Traverse the DIE and add a sibling attribute if it may have the
7428 effect of speeding up access to siblings. To save some space,
7429 avoid generating sibling attributes for DIE's without children. */
7431 static void
7432 add_sibling_attributes (dw_die_ref die)
7434 dw_die_ref c;
7436 if (! die->die_child)
7437 return;
7439 if (die->die_parent && die != die->die_parent->die_child)
7440 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7442 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7445 /* Output all location lists for the DIE and its children. */
7447 static void
7448 output_location_lists (dw_die_ref die)
7450 dw_die_ref c;
7451 dw_attr_ref a;
7452 unsigned ix;
7454 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7455 if (AT_class (a) == dw_val_class_loc_list)
7456 output_loc_list (AT_loc_list (a));
7458 FOR_EACH_CHILD (die, c, output_location_lists (c));
7461 /* We want to limit the number of external references, because they are
7462 larger than local references: a relocation takes multiple words, and
7463 even a sig8 reference is always eight bytes, whereas a local reference
7464 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7465 So if we encounter multiple external references to the same type DIE, we
7466 make a local typedef stub for it and redirect all references there.
7468 This is the element of the hash table for keeping track of these
7469 references. */
7471 struct external_ref
7473 dw_die_ref type;
7474 dw_die_ref stub;
7475 unsigned n_refs;
7478 /* Hashtable helpers. */
7480 struct external_ref_hasher : typed_free_remove <external_ref>
7482 typedef external_ref value_type;
7483 typedef external_ref compare_type;
7484 static inline hashval_t hash (const value_type *);
7485 static inline bool equal (const value_type *, const compare_type *);
7488 inline hashval_t
7489 external_ref_hasher::hash (const value_type *r)
7491 dw_die_ref die = r->type;
7492 hashval_t h = 0;
7494 /* We can't use the address of the DIE for hashing, because
7495 that will make the order of the stub DIEs non-deterministic. */
7496 if (! die->comdat_type_p)
7497 /* We have a symbol; use it to compute a hash. */
7498 h = htab_hash_string (die->die_id.die_symbol);
7499 else
7501 /* We have a type signature; use a subset of the bits as the hash.
7502 The 8-byte signature is at least as large as hashval_t. */
7503 comdat_type_node_ref type_node = die->die_id.die_type_node;
7504 memcpy (&h, type_node->signature, sizeof (h));
7506 return h;
7509 inline bool
7510 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7512 return r1->type == r2->type;
7515 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7517 /* Return a pointer to the external_ref for references to DIE. */
7519 static struct external_ref *
7520 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7522 struct external_ref ref, *ref_p;
7523 external_ref **slot;
7525 ref.type = die;
7526 slot = map.find_slot (&ref, INSERT);
7527 if (*slot != HTAB_EMPTY_ENTRY)
7528 return *slot;
7530 ref_p = XCNEW (struct external_ref);
7531 ref_p->type = die;
7532 *slot = ref_p;
7533 return ref_p;
7536 /* Subroutine of optimize_external_refs, below.
7538 If we see a type skeleton, record it as our stub. If we see external
7539 references, remember how many we've seen. */
7541 static void
7542 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7544 dw_die_ref c;
7545 dw_attr_ref a;
7546 unsigned ix;
7547 struct external_ref *ref_p;
7549 if (is_type_die (die)
7550 && (c = get_AT_ref (die, DW_AT_signature)))
7552 /* This is a local skeleton; use it for local references. */
7553 ref_p = lookup_external_ref (map, c);
7554 ref_p->stub = die;
7557 /* Scan the DIE references, and remember any that refer to DIEs from
7558 other CUs (i.e. those which are not marked). */
7559 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7560 if (AT_class (a) == dw_val_class_die_ref
7561 && (c = AT_ref (a))->die_mark == 0
7562 && is_type_die (c))
7564 ref_p = lookup_external_ref (map, c);
7565 ref_p->n_refs++;
7568 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7571 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7572 points to an external_ref, DATA is the CU we're processing. If we don't
7573 already have a local stub, and we have multiple refs, build a stub. */
7576 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7578 struct external_ref *ref_p = *slot;
7580 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7582 /* We have multiple references to this type, so build a small stub.
7583 Both of these forms are a bit dodgy from the perspective of the
7584 DWARF standard, since technically they should have names. */
7585 dw_die_ref cu = data;
7586 dw_die_ref type = ref_p->type;
7587 dw_die_ref stub = NULL;
7589 if (type->comdat_type_p)
7591 /* If we refer to this type via sig8, use AT_signature. */
7592 stub = new_die (type->die_tag, cu, NULL_TREE);
7593 add_AT_die_ref (stub, DW_AT_signature, type);
7595 else
7597 /* Otherwise, use a typedef with no name. */
7598 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7599 add_AT_die_ref (stub, DW_AT_type, type);
7602 stub->die_mark++;
7603 ref_p->stub = stub;
7605 return 1;
7608 /* DIE is a unit; look through all the DIE references to see if there are
7609 any external references to types, and if so, create local stubs for
7610 them which will be applied in build_abbrev_table. This is useful because
7611 references to local DIEs are smaller. */
7613 static external_ref_hash_type
7614 optimize_external_refs (dw_die_ref die)
7616 external_ref_hash_type map;
7617 map.create (10);
7618 optimize_external_refs_1 (die, map);
7619 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7620 return map;
7623 /* The format of each DIE (and its attribute value pairs) is encoded in an
7624 abbreviation table. This routine builds the abbreviation table and assigns
7625 a unique abbreviation id for each abbreviation entry. The children of each
7626 die are visited recursively. */
7628 static void
7629 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7631 unsigned long abbrev_id;
7632 unsigned int n_alloc;
7633 dw_die_ref c;
7634 dw_attr_ref a;
7635 unsigned ix;
7637 /* Scan the DIE references, and replace any that refer to
7638 DIEs from other CUs (i.e. those which are not marked) with
7639 the local stubs we built in optimize_external_refs. */
7640 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7641 if (AT_class (a) == dw_val_class_die_ref
7642 && (c = AT_ref (a))->die_mark == 0)
7644 struct external_ref *ref_p;
7645 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7647 ref_p = lookup_external_ref (extern_map, c);
7648 if (ref_p->stub && ref_p->stub != die)
7649 change_AT_die_ref (a, ref_p->stub);
7650 else
7651 /* We aren't changing this reference, so mark it external. */
7652 set_AT_ref_external (a, 1);
7655 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7657 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7658 dw_attr_ref die_a, abbrev_a;
7659 unsigned ix;
7660 bool ok = true;
7662 if (abbrev->die_tag != die->die_tag)
7663 continue;
7664 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7665 continue;
7667 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7668 continue;
7670 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7672 abbrev_a = &(*abbrev->die_attr)[ix];
7673 if ((abbrev_a->dw_attr != die_a->dw_attr)
7674 || (value_format (abbrev_a) != value_format (die_a)))
7676 ok = false;
7677 break;
7680 if (ok)
7681 break;
7684 if (abbrev_id >= abbrev_die_table_in_use)
7686 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7688 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7689 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7690 n_alloc);
7692 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7693 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7694 abbrev_die_table_allocated = n_alloc;
7697 ++abbrev_die_table_in_use;
7698 abbrev_die_table[abbrev_id] = die;
7701 die->die_abbrev = abbrev_id;
7702 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7705 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7707 static int
7708 constant_size (unsigned HOST_WIDE_INT value)
7710 int log;
7712 if (value == 0)
7713 log = 0;
7714 else
7715 log = floor_log2 (value);
7717 log = log / 8;
7718 log = 1 << (floor_log2 (log) + 1);
7720 return log;
7723 /* Return the size of a DIE as it is represented in the
7724 .debug_info section. */
7726 static unsigned long
7727 size_of_die (dw_die_ref die)
7729 unsigned long size = 0;
7730 dw_attr_ref a;
7731 unsigned ix;
7732 enum dwarf_form form;
7734 size += size_of_uleb128 (die->die_abbrev);
7735 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7737 switch (AT_class (a))
7739 case dw_val_class_addr:
7740 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7742 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7743 size += size_of_uleb128 (AT_index (a));
7745 else
7746 size += DWARF2_ADDR_SIZE;
7747 break;
7748 case dw_val_class_offset:
7749 size += DWARF_OFFSET_SIZE;
7750 break;
7751 case dw_val_class_loc:
7753 unsigned long lsize = size_of_locs (AT_loc (a));
7755 /* Block length. */
7756 if (dwarf_version >= 4)
7757 size += size_of_uleb128 (lsize);
7758 else
7759 size += constant_size (lsize);
7760 size += lsize;
7762 break;
7763 case dw_val_class_loc_list:
7764 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7766 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7767 size += size_of_uleb128 (AT_index (a));
7769 else
7770 size += DWARF_OFFSET_SIZE;
7771 break;
7772 case dw_val_class_range_list:
7773 size += DWARF_OFFSET_SIZE;
7774 break;
7775 case dw_val_class_const:
7776 size += size_of_sleb128 (AT_int (a));
7777 break;
7778 case dw_val_class_unsigned_const:
7780 int csize = constant_size (AT_unsigned (a));
7781 if (dwarf_version == 3
7782 && a->dw_attr == DW_AT_data_member_location
7783 && csize >= 4)
7784 size += size_of_uleb128 (AT_unsigned (a));
7785 else
7786 size += csize;
7788 break;
7789 case dw_val_class_const_double:
7790 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7791 if (HOST_BITS_PER_WIDE_INT >= 64)
7792 size++; /* block */
7793 break;
7794 case dw_val_class_vec:
7795 size += constant_size (a->dw_attr_val.v.val_vec.length
7796 * a->dw_attr_val.v.val_vec.elt_size)
7797 + a->dw_attr_val.v.val_vec.length
7798 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7799 break;
7800 case dw_val_class_flag:
7801 if (dwarf_version >= 4)
7802 /* Currently all add_AT_flag calls pass in 1 as last argument,
7803 so DW_FORM_flag_present can be used. If that ever changes,
7804 we'll need to use DW_FORM_flag and have some optimization
7805 in build_abbrev_table that will change those to
7806 DW_FORM_flag_present if it is set to 1 in all DIEs using
7807 the same abbrev entry. */
7808 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7809 else
7810 size += 1;
7811 break;
7812 case dw_val_class_die_ref:
7813 if (AT_ref_external (a))
7815 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7816 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7817 is sized by target address length, whereas in DWARF3
7818 it's always sized as an offset. */
7819 if (use_debug_types)
7820 size += DWARF_TYPE_SIGNATURE_SIZE;
7821 else if (dwarf_version == 2)
7822 size += DWARF2_ADDR_SIZE;
7823 else
7824 size += DWARF_OFFSET_SIZE;
7826 else
7827 size += DWARF_OFFSET_SIZE;
7828 break;
7829 case dw_val_class_fde_ref:
7830 size += DWARF_OFFSET_SIZE;
7831 break;
7832 case dw_val_class_lbl_id:
7833 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7835 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7836 size += size_of_uleb128 (AT_index (a));
7838 else
7839 size += DWARF2_ADDR_SIZE;
7840 break;
7841 case dw_val_class_lineptr:
7842 case dw_val_class_macptr:
7843 size += DWARF_OFFSET_SIZE;
7844 break;
7845 case dw_val_class_str:
7846 form = AT_string_form (a);
7847 if (form == DW_FORM_strp)
7848 size += DWARF_OFFSET_SIZE;
7849 else if (form == DW_FORM_GNU_str_index)
7850 size += size_of_uleb128 (AT_index (a));
7851 else
7852 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7853 break;
7854 case dw_val_class_file:
7855 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7856 break;
7857 case dw_val_class_data8:
7858 size += 8;
7859 break;
7860 case dw_val_class_vms_delta:
7861 size += DWARF_OFFSET_SIZE;
7862 break;
7863 case dw_val_class_high_pc:
7864 size += DWARF2_ADDR_SIZE;
7865 break;
7866 default:
7867 gcc_unreachable ();
7871 return size;
7874 /* Size the debugging information associated with a given DIE. Visits the
7875 DIE's children recursively. Updates the global variable next_die_offset, on
7876 each time through. Uses the current value of next_die_offset to update the
7877 die_offset field in each DIE. */
7879 static void
7880 calc_die_sizes (dw_die_ref die)
7882 dw_die_ref c;
7884 gcc_assert (die->die_offset == 0
7885 || (unsigned long int) die->die_offset == next_die_offset);
7886 die->die_offset = next_die_offset;
7887 next_die_offset += size_of_die (die);
7889 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7891 if (die->die_child != NULL)
7892 /* Count the null byte used to terminate sibling lists. */
7893 next_die_offset += 1;
7896 /* Size just the base type children at the start of the CU.
7897 This is needed because build_abbrev needs to size locs
7898 and sizing of type based stack ops needs to know die_offset
7899 values for the base types. */
7901 static void
7902 calc_base_type_die_sizes (void)
7904 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7905 unsigned int i;
7906 dw_die_ref base_type;
7907 #if ENABLE_ASSERT_CHECKING
7908 dw_die_ref prev = comp_unit_die ()->die_child;
7909 #endif
7911 die_offset += size_of_die (comp_unit_die ());
7912 for (i = 0; base_types.iterate (i, &base_type); i++)
7914 #if ENABLE_ASSERT_CHECKING
7915 gcc_assert (base_type->die_offset == 0
7916 && prev->die_sib == base_type
7917 && base_type->die_child == NULL
7918 && base_type->die_abbrev);
7919 prev = base_type;
7920 #endif
7921 base_type->die_offset = die_offset;
7922 die_offset += size_of_die (base_type);
7926 /* Set the marks for a die and its children. We do this so
7927 that we know whether or not a reference needs to use FORM_ref_addr; only
7928 DIEs in the same CU will be marked. We used to clear out the offset
7929 and use that as the flag, but ran into ordering problems. */
7931 static void
7932 mark_dies (dw_die_ref die)
7934 dw_die_ref c;
7936 gcc_assert (!die->die_mark);
7938 die->die_mark = 1;
7939 FOR_EACH_CHILD (die, c, mark_dies (c));
7942 /* Clear the marks for a die and its children. */
7944 static void
7945 unmark_dies (dw_die_ref die)
7947 dw_die_ref c;
7949 if (! use_debug_types)
7950 gcc_assert (die->die_mark);
7952 die->die_mark = 0;
7953 FOR_EACH_CHILD (die, c, unmark_dies (c));
7956 /* Clear the marks for a die, its children and referred dies. */
7958 static void
7959 unmark_all_dies (dw_die_ref die)
7961 dw_die_ref c;
7962 dw_attr_ref a;
7963 unsigned ix;
7965 if (!die->die_mark)
7966 return;
7967 die->die_mark = 0;
7969 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7971 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7972 if (AT_class (a) == dw_val_class_die_ref)
7973 unmark_all_dies (AT_ref (a));
7976 /* Calculate if the entry should appear in the final output file. It may be
7977 from a pruned a type. */
7979 static bool
7980 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7982 /* By limiting gnu pubnames to definitions only, gold can generate a
7983 gdb index without entries for declarations, which don't include
7984 enough information to be useful. */
7985 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7986 return false;
7988 if (table == pubname_table)
7990 /* Enumerator names are part of the pubname table, but the
7991 parent DW_TAG_enumeration_type die may have been pruned.
7992 Don't output them if that is the case. */
7993 if (p->die->die_tag == DW_TAG_enumerator &&
7994 (p->die->die_parent == NULL
7995 || !p->die->die_parent->die_perennial_p))
7996 return false;
7998 /* Everything else in the pubname table is included. */
7999 return true;
8002 /* The pubtypes table shouldn't include types that have been
8003 pruned. */
8004 return (p->die->die_offset != 0
8005 || !flag_eliminate_unused_debug_types);
8008 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8009 generated for the compilation unit. */
8011 static unsigned long
8012 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8014 unsigned long size;
8015 unsigned i;
8016 pubname_ref p;
8017 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8019 size = DWARF_PUBNAMES_HEADER_SIZE;
8020 FOR_EACH_VEC_ELT (*names, i, p)
8021 if (include_pubname_in_output (names, p))
8022 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8024 size += DWARF_OFFSET_SIZE;
8025 return size;
8028 /* Return the size of the information in the .debug_aranges section. */
8030 static unsigned long
8031 size_of_aranges (void)
8033 unsigned long size;
8035 size = DWARF_ARANGES_HEADER_SIZE;
8037 /* Count the address/length pair for this compilation unit. */
8038 if (text_section_used)
8039 size += 2 * DWARF2_ADDR_SIZE;
8040 if (cold_text_section_used)
8041 size += 2 * DWARF2_ADDR_SIZE;
8042 if (have_multiple_function_sections)
8044 unsigned fde_idx;
8045 dw_fde_ref fde;
8047 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8049 if (DECL_IGNORED_P (fde->decl))
8050 continue;
8051 if (!fde->in_std_section)
8052 size += 2 * DWARF2_ADDR_SIZE;
8053 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8054 size += 2 * DWARF2_ADDR_SIZE;
8058 /* Count the two zero words used to terminated the address range table. */
8059 size += 2 * DWARF2_ADDR_SIZE;
8060 return size;
8063 /* Select the encoding of an attribute value. */
8065 static enum dwarf_form
8066 value_format (dw_attr_ref a)
8068 switch (AT_class (a))
8070 case dw_val_class_addr:
8071 /* Only very few attributes allow DW_FORM_addr. */
8072 switch (a->dw_attr)
8074 case DW_AT_low_pc:
8075 case DW_AT_high_pc:
8076 case DW_AT_entry_pc:
8077 case DW_AT_trampoline:
8078 return (AT_index (a) == NOT_INDEXED
8079 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8080 default:
8081 break;
8083 switch (DWARF2_ADDR_SIZE)
8085 case 1:
8086 return DW_FORM_data1;
8087 case 2:
8088 return DW_FORM_data2;
8089 case 4:
8090 return DW_FORM_data4;
8091 case 8:
8092 return DW_FORM_data8;
8093 default:
8094 gcc_unreachable ();
8096 case dw_val_class_range_list:
8097 case dw_val_class_loc_list:
8098 if (dwarf_version >= 4)
8099 return DW_FORM_sec_offset;
8100 /* FALLTHRU */
8101 case dw_val_class_vms_delta:
8102 case dw_val_class_offset:
8103 switch (DWARF_OFFSET_SIZE)
8105 case 4:
8106 return DW_FORM_data4;
8107 case 8:
8108 return DW_FORM_data8;
8109 default:
8110 gcc_unreachable ();
8112 case dw_val_class_loc:
8113 if (dwarf_version >= 4)
8114 return DW_FORM_exprloc;
8115 switch (constant_size (size_of_locs (AT_loc (a))))
8117 case 1:
8118 return DW_FORM_block1;
8119 case 2:
8120 return DW_FORM_block2;
8121 case 4:
8122 return DW_FORM_block4;
8123 default:
8124 gcc_unreachable ();
8126 case dw_val_class_const:
8127 return DW_FORM_sdata;
8128 case dw_val_class_unsigned_const:
8129 switch (constant_size (AT_unsigned (a)))
8131 case 1:
8132 return DW_FORM_data1;
8133 case 2:
8134 return DW_FORM_data2;
8135 case 4:
8136 /* In DWARF3 DW_AT_data_member_location with
8137 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8138 constant, so we need to use DW_FORM_udata if we need
8139 a large constant. */
8140 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8141 return DW_FORM_udata;
8142 return DW_FORM_data4;
8143 case 8:
8144 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8145 return DW_FORM_udata;
8146 return DW_FORM_data8;
8147 default:
8148 gcc_unreachable ();
8150 case dw_val_class_const_double:
8151 switch (HOST_BITS_PER_WIDE_INT)
8153 case 8:
8154 return DW_FORM_data2;
8155 case 16:
8156 return DW_FORM_data4;
8157 case 32:
8158 return DW_FORM_data8;
8159 case 64:
8160 default:
8161 return DW_FORM_block1;
8163 case dw_val_class_vec:
8164 switch (constant_size (a->dw_attr_val.v.val_vec.length
8165 * a->dw_attr_val.v.val_vec.elt_size))
8167 case 1:
8168 return DW_FORM_block1;
8169 case 2:
8170 return DW_FORM_block2;
8171 case 4:
8172 return DW_FORM_block4;
8173 default:
8174 gcc_unreachable ();
8176 case dw_val_class_flag:
8177 if (dwarf_version >= 4)
8179 /* Currently all add_AT_flag calls pass in 1 as last argument,
8180 so DW_FORM_flag_present can be used. If that ever changes,
8181 we'll need to use DW_FORM_flag and have some optimization
8182 in build_abbrev_table that will change those to
8183 DW_FORM_flag_present if it is set to 1 in all DIEs using
8184 the same abbrev entry. */
8185 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8186 return DW_FORM_flag_present;
8188 return DW_FORM_flag;
8189 case dw_val_class_die_ref:
8190 if (AT_ref_external (a))
8191 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8192 else
8193 return DW_FORM_ref;
8194 case dw_val_class_fde_ref:
8195 return DW_FORM_data;
8196 case dw_val_class_lbl_id:
8197 return (AT_index (a) == NOT_INDEXED
8198 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8199 case dw_val_class_lineptr:
8200 case dw_val_class_macptr:
8201 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8202 case dw_val_class_str:
8203 return AT_string_form (a);
8204 case dw_val_class_file:
8205 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8207 case 1:
8208 return DW_FORM_data1;
8209 case 2:
8210 return DW_FORM_data2;
8211 case 4:
8212 return DW_FORM_data4;
8213 default:
8214 gcc_unreachable ();
8217 case dw_val_class_data8:
8218 return DW_FORM_data8;
8220 case dw_val_class_high_pc:
8221 switch (DWARF2_ADDR_SIZE)
8223 case 1:
8224 return DW_FORM_data1;
8225 case 2:
8226 return DW_FORM_data2;
8227 case 4:
8228 return DW_FORM_data4;
8229 case 8:
8230 return DW_FORM_data8;
8231 default:
8232 gcc_unreachable ();
8235 default:
8236 gcc_unreachable ();
8240 /* Output the encoding of an attribute value. */
8242 static void
8243 output_value_format (dw_attr_ref a)
8245 enum dwarf_form form = value_format (a);
8247 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8250 /* Given a die and id, produce the appropriate abbreviations. */
8252 static void
8253 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8255 unsigned ix;
8256 dw_attr_ref a_attr;
8258 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8259 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8260 dwarf_tag_name (abbrev->die_tag));
8262 if (abbrev->die_child != NULL)
8263 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8264 else
8265 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8267 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8269 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8270 dwarf_attr_name (a_attr->dw_attr));
8271 output_value_format (a_attr);
8274 dw2_asm_output_data (1, 0, NULL);
8275 dw2_asm_output_data (1, 0, NULL);
8279 /* Output the .debug_abbrev section which defines the DIE abbreviation
8280 table. */
8282 static void
8283 output_abbrev_section (void)
8285 unsigned long abbrev_id;
8287 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8288 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8290 /* Terminate the table. */
8291 dw2_asm_output_data (1, 0, NULL);
8294 /* Output a symbol we can use to refer to this DIE from another CU. */
8296 static inline void
8297 output_die_symbol (dw_die_ref die)
8299 const char *sym = die->die_id.die_symbol;
8301 gcc_assert (!die->comdat_type_p);
8303 if (sym == 0)
8304 return;
8306 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8307 /* We make these global, not weak; if the target doesn't support
8308 .linkonce, it doesn't support combining the sections, so debugging
8309 will break. */
8310 targetm.asm_out.globalize_label (asm_out_file, sym);
8312 ASM_OUTPUT_LABEL (asm_out_file, sym);
8315 /* Return a new location list, given the begin and end range, and the
8316 expression. */
8318 static inline dw_loc_list_ref
8319 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8320 const char *section)
8322 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8324 retlist->begin = begin;
8325 retlist->begin_entry = NULL;
8326 retlist->end = end;
8327 retlist->expr = expr;
8328 retlist->section = section;
8330 return retlist;
8333 /* Generate a new internal symbol for this location list node, if it
8334 hasn't got one yet. */
8336 static inline void
8337 gen_llsym (dw_loc_list_ref list)
8339 gcc_assert (!list->ll_symbol);
8340 list->ll_symbol = gen_internal_sym ("LLST");
8343 /* Output the location list given to us. */
8345 static void
8346 output_loc_list (dw_loc_list_ref list_head)
8348 dw_loc_list_ref curr = list_head;
8350 if (list_head->emitted)
8351 return;
8352 list_head->emitted = true;
8354 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8356 /* Walk the location list, and output each range + expression. */
8357 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8359 unsigned long size;
8360 /* Don't output an entry that starts and ends at the same address. */
8361 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8362 continue;
8363 size = size_of_locs (curr->expr);
8364 /* If the expression is too large, drop it on the floor. We could
8365 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8366 in the expression, but >= 64KB expressions for a single value
8367 in a single range are unlikely very useful. */
8368 if (size > 0xffff)
8369 continue;
8370 if (dwarf_split_debug_info)
8372 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8373 "Location list start/length entry (%s)",
8374 list_head->ll_symbol);
8375 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8376 "Location list range start index (%s)",
8377 curr->begin);
8378 /* The length field is 4 bytes. If we ever need to support
8379 an 8-byte length, we can add a new DW_LLE code or fall back
8380 to DW_LLE_GNU_start_end_entry. */
8381 dw2_asm_output_delta (4, curr->end, curr->begin,
8382 "Location list range length (%s)",
8383 list_head->ll_symbol);
8385 else if (!have_multiple_function_sections)
8387 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8388 "Location list begin address (%s)",
8389 list_head->ll_symbol);
8390 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8391 "Location list end address (%s)",
8392 list_head->ll_symbol);
8394 else
8396 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8397 "Location list begin address (%s)",
8398 list_head->ll_symbol);
8399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8400 "Location list end address (%s)",
8401 list_head->ll_symbol);
8404 /* Output the block length for this list of location operations. */
8405 gcc_assert (size <= 0xffff);
8406 dw2_asm_output_data (2, size, "%s", "Location expression size");
8408 output_loc_sequence (curr->expr, -1);
8411 if (dwarf_split_debug_info)
8412 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8413 "Location list terminator (%s)",
8414 list_head->ll_symbol);
8415 else
8417 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8418 "Location list terminator begin (%s)",
8419 list_head->ll_symbol);
8420 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8421 "Location list terminator end (%s)",
8422 list_head->ll_symbol);
8426 /* Output a range_list offset into the debug_range section. Emit a
8427 relocated reference if val_entry is NULL, otherwise, emit an
8428 indirect reference. */
8430 static void
8431 output_range_list_offset (dw_attr_ref a)
8433 const char *name = dwarf_attr_name (a->dw_attr);
8435 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8437 char *p = strchr (ranges_section_label, '\0');
8438 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8439 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8440 debug_ranges_section, "%s", name);
8441 *p = '\0';
8443 else
8444 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8445 "%s (offset from %s)", name, ranges_section_label);
8448 /* Output the offset into the debug_loc section. */
8450 static void
8451 output_loc_list_offset (dw_attr_ref a)
8453 char *sym = AT_loc_list (a)->ll_symbol;
8455 gcc_assert (sym);
8456 if (dwarf_split_debug_info)
8457 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8458 "%s", dwarf_attr_name (a->dw_attr));
8459 else
8460 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8461 "%s", dwarf_attr_name (a->dw_attr));
8464 /* Output an attribute's index or value appropriately. */
8466 static void
8467 output_attr_index_or_value (dw_attr_ref a)
8469 const char *name = dwarf_attr_name (a->dw_attr);
8471 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8473 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8474 return;
8476 switch (AT_class (a))
8478 case dw_val_class_addr:
8479 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8480 break;
8481 case dw_val_class_high_pc:
8482 case dw_val_class_lbl_id:
8483 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8484 break;
8485 case dw_val_class_loc_list:
8486 output_loc_list_offset (a);
8487 break;
8488 default:
8489 gcc_unreachable ();
8493 /* Output a type signature. */
8495 static inline void
8496 output_signature (const char *sig, const char *name)
8498 int i;
8500 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8501 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8504 /* Output the DIE and its attributes. Called recursively to generate
8505 the definitions of each child DIE. */
8507 static void
8508 output_die (dw_die_ref die)
8510 dw_attr_ref a;
8511 dw_die_ref c;
8512 unsigned long size;
8513 unsigned ix;
8515 /* If someone in another CU might refer to us, set up a symbol for
8516 them to point to. */
8517 if (! die->comdat_type_p && die->die_id.die_symbol)
8518 output_die_symbol (die);
8520 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8521 (unsigned long)die->die_offset,
8522 dwarf_tag_name (die->die_tag));
8524 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8526 const char *name = dwarf_attr_name (a->dw_attr);
8528 switch (AT_class (a))
8530 case dw_val_class_addr:
8531 output_attr_index_or_value (a);
8532 break;
8534 case dw_val_class_offset:
8535 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8536 "%s", name);
8537 break;
8539 case dw_val_class_range_list:
8540 output_range_list_offset (a);
8541 break;
8543 case dw_val_class_loc:
8544 size = size_of_locs (AT_loc (a));
8546 /* Output the block length for this list of location operations. */
8547 if (dwarf_version >= 4)
8548 dw2_asm_output_data_uleb128 (size, "%s", name);
8549 else
8550 dw2_asm_output_data (constant_size (size), size, "%s", name);
8552 output_loc_sequence (AT_loc (a), -1);
8553 break;
8555 case dw_val_class_const:
8556 /* ??? It would be slightly more efficient to use a scheme like is
8557 used for unsigned constants below, but gdb 4.x does not sign
8558 extend. Gdb 5.x does sign extend. */
8559 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8560 break;
8562 case dw_val_class_unsigned_const:
8564 int csize = constant_size (AT_unsigned (a));
8565 if (dwarf_version == 3
8566 && a->dw_attr == DW_AT_data_member_location
8567 && csize >= 4)
8568 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8569 else
8570 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8572 break;
8574 case dw_val_class_const_double:
8576 unsigned HOST_WIDE_INT first, second;
8578 if (HOST_BITS_PER_WIDE_INT >= 64)
8579 dw2_asm_output_data (1,
8580 HOST_BITS_PER_DOUBLE_INT
8581 / HOST_BITS_PER_CHAR,
8582 NULL);
8584 if (WORDS_BIG_ENDIAN)
8586 first = a->dw_attr_val.v.val_double.high;
8587 second = a->dw_attr_val.v.val_double.low;
8589 else
8591 first = a->dw_attr_val.v.val_double.low;
8592 second = a->dw_attr_val.v.val_double.high;
8595 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8596 first, "%s", name);
8597 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8598 second, NULL);
8600 break;
8602 case dw_val_class_vec:
8604 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8605 unsigned int len = a->dw_attr_val.v.val_vec.length;
8606 unsigned int i;
8607 unsigned char *p;
8609 dw2_asm_output_data (constant_size (len * elt_size),
8610 len * elt_size, "%s", name);
8611 if (elt_size > sizeof (HOST_WIDE_INT))
8613 elt_size /= 2;
8614 len *= 2;
8616 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8617 i < len;
8618 i++, p += elt_size)
8619 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8620 "fp or vector constant word %u", i);
8621 break;
8624 case dw_val_class_flag:
8625 if (dwarf_version >= 4)
8627 /* Currently all add_AT_flag calls pass in 1 as last argument,
8628 so DW_FORM_flag_present can be used. If that ever changes,
8629 we'll need to use DW_FORM_flag and have some optimization
8630 in build_abbrev_table that will change those to
8631 DW_FORM_flag_present if it is set to 1 in all DIEs using
8632 the same abbrev entry. */
8633 gcc_assert (AT_flag (a) == 1);
8634 if (flag_debug_asm)
8635 fprintf (asm_out_file, "\t\t\t%s %s\n",
8636 ASM_COMMENT_START, name);
8637 break;
8639 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8640 break;
8642 case dw_val_class_loc_list:
8643 output_attr_index_or_value (a);
8644 break;
8646 case dw_val_class_die_ref:
8647 if (AT_ref_external (a))
8649 if (AT_ref (a)->comdat_type_p)
8651 comdat_type_node_ref type_node =
8652 AT_ref (a)->die_id.die_type_node;
8654 gcc_assert (type_node);
8655 output_signature (type_node->signature, name);
8657 else
8659 const char *sym = AT_ref (a)->die_id.die_symbol;
8660 int size;
8662 gcc_assert (sym);
8663 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8664 length, whereas in DWARF3 it's always sized as an
8665 offset. */
8666 if (dwarf_version == 2)
8667 size = DWARF2_ADDR_SIZE;
8668 else
8669 size = DWARF_OFFSET_SIZE;
8670 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8671 name);
8674 else
8676 gcc_assert (AT_ref (a)->die_offset);
8677 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8678 "%s", name);
8680 break;
8682 case dw_val_class_fde_ref:
8684 char l1[20];
8686 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8687 a->dw_attr_val.v.val_fde_index * 2);
8688 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8689 "%s", name);
8691 break;
8693 case dw_val_class_vms_delta:
8694 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8695 AT_vms_delta2 (a), AT_vms_delta1 (a),
8696 "%s", name);
8697 break;
8699 case dw_val_class_lbl_id:
8700 output_attr_index_or_value (a);
8701 break;
8703 case dw_val_class_lineptr:
8704 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8705 debug_line_section, "%s", name);
8706 break;
8708 case dw_val_class_macptr:
8709 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8710 debug_macinfo_section, "%s", name);
8711 break;
8713 case dw_val_class_str:
8714 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8715 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8716 a->dw_attr_val.v.val_str->label,
8717 debug_str_section,
8718 "%s: \"%s\"", name, AT_string (a));
8719 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8720 dw2_asm_output_data_uleb128 (AT_index (a),
8721 "%s: \"%s\"", name, AT_string (a));
8722 else
8723 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8724 break;
8726 case dw_val_class_file:
8728 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8730 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8731 a->dw_attr_val.v.val_file->filename);
8732 break;
8735 case dw_val_class_data8:
8737 int i;
8739 for (i = 0; i < 8; i++)
8740 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8741 i == 0 ? "%s" : NULL, name);
8742 break;
8745 case dw_val_class_high_pc:
8746 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8747 get_AT_low_pc (die), "DW_AT_high_pc");
8748 break;
8750 default:
8751 gcc_unreachable ();
8755 FOR_EACH_CHILD (die, c, output_die (c));
8757 /* Add null byte to terminate sibling list. */
8758 if (die->die_child != NULL)
8759 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8760 (unsigned long) die->die_offset);
8763 /* Output the compilation unit that appears at the beginning of the
8764 .debug_info section, and precedes the DIE descriptions. */
8766 static void
8767 output_compilation_unit_header (void)
8769 int ver = dwarf_version;
8771 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8772 dw2_asm_output_data (4, 0xffffffff,
8773 "Initial length escape value indicating 64-bit DWARF extension");
8774 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8775 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8776 "Length of Compilation Unit Info");
8777 dw2_asm_output_data (2, ver, "DWARF version number");
8778 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8779 debug_abbrev_section,
8780 "Offset Into Abbrev. Section");
8781 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8784 /* Output the compilation unit DIE and its children. */
8786 static void
8787 output_comp_unit (dw_die_ref die, int output_if_empty)
8789 const char *secname, *oldsym;
8790 char *tmp;
8791 external_ref_hash_type extern_map;
8793 /* Unless we are outputting main CU, we may throw away empty ones. */
8794 if (!output_if_empty && die->die_child == NULL)
8795 return;
8797 /* Even if there are no children of this DIE, we must output the information
8798 about the compilation unit. Otherwise, on an empty translation unit, we
8799 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8800 will then complain when examining the file. First mark all the DIEs in
8801 this CU so we know which get local refs. */
8802 mark_dies (die);
8804 extern_map = optimize_external_refs (die);
8806 build_abbrev_table (die, extern_map);
8808 extern_map.dispose ();
8810 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8811 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8812 calc_die_sizes (die);
8814 oldsym = die->die_id.die_symbol;
8815 if (oldsym)
8817 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8819 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8820 secname = tmp;
8821 die->die_id.die_symbol = NULL;
8822 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8824 else
8826 switch_to_section (debug_info_section);
8827 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8828 info_section_emitted = true;
8831 /* Output debugging information. */
8832 output_compilation_unit_header ();
8833 output_die (die);
8835 /* Leave the marks on the main CU, so we can check them in
8836 output_pubnames. */
8837 if (oldsym)
8839 unmark_dies (die);
8840 die->die_id.die_symbol = oldsym;
8844 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8845 and .debug_pubtypes. This is configured per-target, but can be
8846 overridden by the -gpubnames or -gno-pubnames options. */
8848 static inline bool
8849 want_pubnames (void)
8851 if (debug_info_level <= DINFO_LEVEL_TERSE)
8852 return false;
8853 if (debug_generate_pub_sections != -1)
8854 return debug_generate_pub_sections;
8855 return targetm.want_debug_pub_sections;
8858 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8860 static void
8861 add_AT_pubnames (dw_die_ref die)
8863 if (want_pubnames ())
8864 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8867 /* Add a string attribute value to a skeleton DIE. */
8869 static inline void
8870 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8871 const char *str)
8873 dw_attr_node attr;
8874 struct indirect_string_node *node;
8876 if (! skeleton_debug_str_hash)
8877 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8878 debug_str_eq, NULL);
8880 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8881 find_string_form (node);
8882 if (node->form == DW_FORM_GNU_str_index)
8883 node->form = DW_FORM_strp;
8885 attr.dw_attr = attr_kind;
8886 attr.dw_attr_val.val_class = dw_val_class_str;
8887 attr.dw_attr_val.val_entry = NULL;
8888 attr.dw_attr_val.v.val_str = node;
8889 add_dwarf_attr (die, &attr);
8892 /* Helper function to generate top-level dies for skeleton debug_info and
8893 debug_types. */
8895 static void
8896 add_top_level_skeleton_die_attrs (dw_die_ref die)
8898 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8899 const char *comp_dir = comp_dir_string ();
8901 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8902 if (comp_dir != NULL)
8903 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8904 add_AT_pubnames (die);
8905 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8908 /* Return the single type-unit die for skeleton type units. */
8910 static dw_die_ref
8911 get_skeleton_type_unit (void)
8913 /* For dwarf_split_debug_sections with use_type info, all type units in the
8914 skeleton sections have identical dies (but different headers). This
8915 single die will be output many times. */
8917 static dw_die_ref skeleton_type_unit = NULL;
8919 if (skeleton_type_unit == NULL)
8921 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8922 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8923 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8925 return skeleton_type_unit;
8928 /* Output skeleton debug sections that point to the dwo file. */
8930 static void
8931 output_skeleton_debug_sections (dw_die_ref comp_unit)
8933 /* These attributes will be found in the full debug_info section. */
8934 remove_AT (comp_unit, DW_AT_producer);
8935 remove_AT (comp_unit, DW_AT_language);
8937 switch_to_section (debug_skeleton_info_section);
8938 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8940 /* Produce the skeleton compilation-unit header. This one differs enough from
8941 a normal CU header that it's better not to call output_compilation_unit
8942 header. */
8943 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8944 dw2_asm_output_data (4, 0xffffffff,
8945 "Initial length escape value indicating 64-bit DWARF extension");
8947 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8948 DWARF_COMPILE_UNIT_HEADER_SIZE
8949 - DWARF_INITIAL_LENGTH_SIZE
8950 + size_of_die (comp_unit),
8951 "Length of Compilation Unit Info");
8952 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8953 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8954 debug_abbrev_section,
8955 "Offset Into Abbrev. Section");
8956 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8958 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8959 output_die (comp_unit);
8961 /* Build the skeleton debug_abbrev section. */
8962 switch_to_section (debug_skeleton_abbrev_section);
8963 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8965 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8966 if (use_debug_types)
8967 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8969 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8972 /* Output a comdat type unit DIE and its children. */
8974 static void
8975 output_comdat_type_unit (comdat_type_node *node)
8977 const char *secname;
8978 char *tmp;
8979 int i;
8980 #if defined (OBJECT_FORMAT_ELF)
8981 tree comdat_key;
8982 #endif
8983 external_ref_hash_type extern_map;
8985 /* First mark all the DIEs in this CU so we know which get local refs. */
8986 mark_dies (node->root_die);
8988 extern_map = optimize_external_refs (node->root_die);
8990 build_abbrev_table (node->root_die, extern_map);
8992 extern_map.dispose ();
8994 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8995 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8996 calc_die_sizes (node->root_die);
8998 #if defined (OBJECT_FORMAT_ELF)
8999 if (!dwarf_split_debug_info)
9000 secname = ".debug_types";
9001 else
9002 secname = ".debug_types.dwo";
9004 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9005 sprintf (tmp, "wt.");
9006 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9007 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9008 comdat_key = get_identifier (tmp);
9009 targetm.asm_out.named_section (secname,
9010 SECTION_DEBUG | SECTION_LINKONCE,
9011 comdat_key);
9012 #else
9013 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9014 sprintf (tmp, ".gnu.linkonce.wt.");
9015 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9016 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9017 secname = tmp;
9018 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9019 #endif
9021 /* Output debugging information. */
9022 output_compilation_unit_header ();
9023 output_signature (node->signature, "Type Signature");
9024 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9025 "Offset to Type DIE");
9026 output_die (node->root_die);
9028 unmark_dies (node->root_die);
9030 #if defined (OBJECT_FORMAT_ELF)
9031 if (dwarf_split_debug_info)
9033 /* Produce the skeleton type-unit header. */
9034 const char *secname = ".debug_types";
9036 targetm.asm_out.named_section (secname,
9037 SECTION_DEBUG | SECTION_LINKONCE,
9038 comdat_key);
9039 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9040 dw2_asm_output_data (4, 0xffffffff,
9041 "Initial length escape value indicating 64-bit DWARF extension");
9043 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9044 DWARF_COMPILE_UNIT_HEADER_SIZE
9045 - DWARF_INITIAL_LENGTH_SIZE
9046 + size_of_die (get_skeleton_type_unit ())
9047 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9048 "Length of Type Unit Info");
9049 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9050 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9051 debug_skeleton_abbrev_section_label,
9052 debug_abbrev_section,
9053 "Offset Into Abbrev. Section");
9054 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9055 output_signature (node->signature, "Type Signature");
9056 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9058 output_die (get_skeleton_type_unit ());
9060 #endif
9063 /* Return the DWARF2/3 pubname associated with a decl. */
9065 static const char *
9066 dwarf2_name (tree decl, int scope)
9068 if (DECL_NAMELESS (decl))
9069 return NULL;
9070 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9073 /* Add a new entry to .debug_pubnames if appropriate. */
9075 static void
9076 add_pubname_string (const char *str, dw_die_ref die)
9078 pubname_entry e;
9080 e.die = die;
9081 e.name = xstrdup (str);
9082 vec_safe_push (pubname_table, e);
9085 static void
9086 add_pubname (tree decl, dw_die_ref die)
9088 if (!want_pubnames ())
9089 return;
9091 /* Don't add items to the table when we expect that the consumer will have
9092 just read the enclosing die. For example, if the consumer is looking at a
9093 class_member, it will either be inside the class already, or will have just
9094 looked up the class to find the member. Either way, searching the class is
9095 faster than searching the index. */
9096 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9097 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9099 const char *name = dwarf2_name (decl, 1);
9101 if (name)
9102 add_pubname_string (name, die);
9106 /* Add an enumerator to the pubnames section. */
9108 static void
9109 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9111 pubname_entry e;
9113 gcc_assert (scope_name);
9114 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9115 e.die = die;
9116 vec_safe_push (pubname_table, e);
9119 /* Add a new entry to .debug_pubtypes if appropriate. */
9121 static void
9122 add_pubtype (tree decl, dw_die_ref die)
9124 pubname_entry e;
9126 if (!want_pubnames ())
9127 return;
9129 if ((TREE_PUBLIC (decl)
9130 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9131 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9133 tree scope = NULL;
9134 const char *scope_name = "";
9135 const char *sep = is_cxx () ? "::" : ".";
9136 const char *name;
9138 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9139 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9141 scope_name = lang_hooks.dwarf_name (scope, 1);
9142 if (scope_name != NULL && scope_name[0] != '\0')
9143 scope_name = concat (scope_name, sep, NULL);
9144 else
9145 scope_name = "";
9148 if (TYPE_P (decl))
9149 name = type_tag (decl);
9150 else
9151 name = lang_hooks.dwarf_name (decl, 1);
9153 /* If we don't have a name for the type, there's no point in adding
9154 it to the table. */
9155 if (name != NULL && name[0] != '\0')
9157 e.die = die;
9158 e.name = concat (scope_name, name, NULL);
9159 vec_safe_push (pubtype_table, e);
9162 /* Although it might be more consistent to add the pubinfo for the
9163 enumerators as their dies are created, they should only be added if the
9164 enum type meets the criteria above. So rather than re-check the parent
9165 enum type whenever an enumerator die is created, just output them all
9166 here. This isn't protected by the name conditional because anonymous
9167 enums don't have names. */
9168 if (die->die_tag == DW_TAG_enumeration_type)
9170 dw_die_ref c;
9172 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9177 /* Output a single entry in the pubnames table. */
9179 static void
9180 output_pubname (dw_offset die_offset, pubname_entry *entry)
9182 dw_die_ref die = entry->die;
9183 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9185 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9187 if (debug_generate_pub_sections == 2)
9189 /* This logic follows gdb's method for determining the value of the flag
9190 byte. */
9191 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9192 switch (die->die_tag)
9194 case DW_TAG_typedef:
9195 case DW_TAG_base_type:
9196 case DW_TAG_subrange_type:
9197 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9198 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9199 break;
9200 case DW_TAG_enumerator:
9201 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9202 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9203 if (!is_cxx () && !is_java ())
9204 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9205 break;
9206 case DW_TAG_subprogram:
9207 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9208 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9209 if (!is_ada ())
9210 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9211 break;
9212 case DW_TAG_constant:
9213 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9214 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9215 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9216 break;
9217 case DW_TAG_variable:
9218 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9219 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9220 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9221 break;
9222 case DW_TAG_namespace:
9223 case DW_TAG_imported_declaration:
9224 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9225 break;
9226 case DW_TAG_class_type:
9227 case DW_TAG_interface_type:
9228 case DW_TAG_structure_type:
9229 case DW_TAG_union_type:
9230 case DW_TAG_enumeration_type:
9231 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9232 if (!is_cxx () && !is_java ())
9233 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9234 break;
9235 default:
9236 /* An unusual tag. Leave the flag-byte empty. */
9237 break;
9239 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9240 "GDB-index flags");
9243 dw2_asm_output_nstring (entry->name, -1, "external name");
9247 /* Output the public names table used to speed up access to externally
9248 visible names; or the public types table used to find type definitions. */
9250 static void
9251 output_pubnames (vec<pubname_entry, va_gc> *names)
9253 unsigned i;
9254 unsigned long pubnames_length = size_of_pubnames (names);
9255 pubname_ref pub;
9257 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9258 dw2_asm_output_data (4, 0xffffffff,
9259 "Initial length escape value indicating 64-bit DWARF extension");
9260 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9262 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9263 dw2_asm_output_data (2, 2, "DWARF Version");
9265 if (dwarf_split_debug_info)
9266 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9267 debug_skeleton_info_section,
9268 "Offset of Compilation Unit Info");
9269 else
9270 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9271 debug_info_section,
9272 "Offset of Compilation Unit Info");
9273 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9274 "Compilation Unit Length");
9276 FOR_EACH_VEC_ELT (*names, i, pub)
9278 if (include_pubname_in_output (names, pub))
9280 dw_offset die_offset = pub->die->die_offset;
9282 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9283 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9284 gcc_assert (pub->die->die_mark);
9286 /* If we're putting types in their own .debug_types sections,
9287 the .debug_pubtypes table will still point to the compile
9288 unit (not the type unit), so we want to use the offset of
9289 the skeleton DIE (if there is one). */
9290 if (pub->die->comdat_type_p && names == pubtype_table)
9292 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9294 if (type_node != NULL)
9295 die_offset = (type_node->skeleton_die != NULL
9296 ? type_node->skeleton_die->die_offset
9297 : comp_unit_die ()->die_offset);
9300 output_pubname (die_offset, pub);
9304 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9307 /* Output public names and types tables if necessary. */
9309 static void
9310 output_pubtables (void)
9312 if (!want_pubnames () || !info_section_emitted)
9313 return;
9315 switch_to_section (debug_pubnames_section);
9316 output_pubnames (pubname_table);
9317 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9318 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9319 simply won't look for the section. */
9320 switch_to_section (debug_pubtypes_section);
9321 output_pubnames (pubtype_table);
9325 /* Output the information that goes into the .debug_aranges table.
9326 Namely, define the beginning and ending address range of the
9327 text section generated for this compilation unit. */
9329 static void
9330 output_aranges (unsigned long aranges_length)
9332 unsigned i;
9334 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9335 dw2_asm_output_data (4, 0xffffffff,
9336 "Initial length escape value indicating 64-bit DWARF extension");
9337 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9338 "Length of Address Ranges Info");
9339 /* Version number for aranges is still 2, even in DWARF3. */
9340 dw2_asm_output_data (2, 2, "DWARF Version");
9341 if (dwarf_split_debug_info)
9342 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9343 debug_skeleton_info_section,
9344 "Offset of Compilation Unit Info");
9345 else
9346 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9347 debug_info_section,
9348 "Offset of Compilation Unit Info");
9349 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9350 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9352 /* We need to align to twice the pointer size here. */
9353 if (DWARF_ARANGES_PAD_SIZE)
9355 /* Pad using a 2 byte words so that padding is correct for any
9356 pointer size. */
9357 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9358 2 * DWARF2_ADDR_SIZE);
9359 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9360 dw2_asm_output_data (2, 0, NULL);
9363 /* It is necessary not to output these entries if the sections were
9364 not used; if the sections were not used, the length will be 0 and
9365 the address may end up as 0 if the section is discarded by ld
9366 --gc-sections, leaving an invalid (0, 0) entry that can be
9367 confused with the terminator. */
9368 if (text_section_used)
9370 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9371 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9372 text_section_label, "Length");
9374 if (cold_text_section_used)
9376 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9377 "Address");
9378 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9379 cold_text_section_label, "Length");
9382 if (have_multiple_function_sections)
9384 unsigned fde_idx;
9385 dw_fde_ref fde;
9387 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9389 if (DECL_IGNORED_P (fde->decl))
9390 continue;
9391 if (!fde->in_std_section)
9393 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9394 "Address");
9395 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9396 fde->dw_fde_begin, "Length");
9398 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9400 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9401 "Address");
9402 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9403 fde->dw_fde_second_begin, "Length");
9408 /* Output the terminator words. */
9409 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9410 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9413 /* Add a new entry to .debug_ranges. Return the offset at which it
9414 was placed. */
9416 static unsigned int
9417 add_ranges_num (int num)
9419 unsigned int in_use = ranges_table_in_use;
9421 if (in_use == ranges_table_allocated)
9423 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9424 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9425 ranges_table_allocated);
9426 memset (ranges_table + ranges_table_in_use, 0,
9427 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9430 ranges_table[in_use].num = num;
9431 ranges_table_in_use = in_use + 1;
9433 return in_use * 2 * DWARF2_ADDR_SIZE;
9436 /* Add a new entry to .debug_ranges corresponding to a block, or a
9437 range terminator if BLOCK is NULL. */
9439 static unsigned int
9440 add_ranges (const_tree block)
9442 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9445 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9446 When using dwarf_split_debug_info, address attributes in dies destined
9447 for the final executable should be direct references--setting the
9448 parameter force_direct ensures this behavior. */
9450 static void
9451 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9452 bool *added, bool force_direct)
9454 unsigned int in_use = ranges_by_label_in_use;
9455 unsigned int offset;
9457 if (in_use == ranges_by_label_allocated)
9459 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9460 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9461 ranges_by_label,
9462 ranges_by_label_allocated);
9463 memset (ranges_by_label + ranges_by_label_in_use, 0,
9464 RANGES_TABLE_INCREMENT
9465 * sizeof (struct dw_ranges_by_label_struct));
9468 ranges_by_label[in_use].begin = begin;
9469 ranges_by_label[in_use].end = end;
9470 ranges_by_label_in_use = in_use + 1;
9472 offset = add_ranges_num (-(int)in_use - 1);
9473 if (!*added)
9475 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9476 *added = true;
9480 static void
9481 output_ranges (void)
9483 unsigned i;
9484 static const char *const start_fmt = "Offset %#x";
9485 const char *fmt = start_fmt;
9487 for (i = 0; i < ranges_table_in_use; i++)
9489 int block_num = ranges_table[i].num;
9491 if (block_num > 0)
9493 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9494 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9496 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9497 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9499 /* If all code is in the text section, then the compilation
9500 unit base address defaults to DW_AT_low_pc, which is the
9501 base of the text section. */
9502 if (!have_multiple_function_sections)
9504 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9505 text_section_label,
9506 fmt, i * 2 * DWARF2_ADDR_SIZE);
9507 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9508 text_section_label, NULL);
9511 /* Otherwise, the compilation unit base address is zero,
9512 which allows us to use absolute addresses, and not worry
9513 about whether the target supports cross-section
9514 arithmetic. */
9515 else
9517 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9518 fmt, i * 2 * DWARF2_ADDR_SIZE);
9519 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9522 fmt = NULL;
9525 /* Negative block_num stands for an index into ranges_by_label. */
9526 else if (block_num < 0)
9528 int lab_idx = - block_num - 1;
9530 if (!have_multiple_function_sections)
9532 gcc_unreachable ();
9533 #if 0
9534 /* If we ever use add_ranges_by_labels () for a single
9535 function section, all we have to do is to take out
9536 the #if 0 above. */
9537 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9538 ranges_by_label[lab_idx].begin,
9539 text_section_label,
9540 fmt, i * 2 * DWARF2_ADDR_SIZE);
9541 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9542 ranges_by_label[lab_idx].end,
9543 text_section_label, NULL);
9544 #endif
9546 else
9548 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9549 ranges_by_label[lab_idx].begin,
9550 fmt, i * 2 * DWARF2_ADDR_SIZE);
9551 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9552 ranges_by_label[lab_idx].end,
9553 NULL);
9556 else
9558 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9559 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9560 fmt = start_fmt;
9565 /* Data structure containing information about input files. */
9566 struct file_info
9568 const char *path; /* Complete file name. */
9569 const char *fname; /* File name part. */
9570 int length; /* Length of entire string. */
9571 struct dwarf_file_data * file_idx; /* Index in input file table. */
9572 int dir_idx; /* Index in directory table. */
9575 /* Data structure containing information about directories with source
9576 files. */
9577 struct dir_info
9579 const char *path; /* Path including directory name. */
9580 int length; /* Path length. */
9581 int prefix; /* Index of directory entry which is a prefix. */
9582 int count; /* Number of files in this directory. */
9583 int dir_idx; /* Index of directory used as base. */
9586 /* Callback function for file_info comparison. We sort by looking at
9587 the directories in the path. */
9589 static int
9590 file_info_cmp (const void *p1, const void *p2)
9592 const struct file_info *const s1 = (const struct file_info *) p1;
9593 const struct file_info *const s2 = (const struct file_info *) p2;
9594 const unsigned char *cp1;
9595 const unsigned char *cp2;
9597 /* Take care of file names without directories. We need to make sure that
9598 we return consistent values to qsort since some will get confused if
9599 we return the same value when identical operands are passed in opposite
9600 orders. So if neither has a directory, return 0 and otherwise return
9601 1 or -1 depending on which one has the directory. */
9602 if ((s1->path == s1->fname || s2->path == s2->fname))
9603 return (s2->path == s2->fname) - (s1->path == s1->fname);
9605 cp1 = (const unsigned char *) s1->path;
9606 cp2 = (const unsigned char *) s2->path;
9608 while (1)
9610 ++cp1;
9611 ++cp2;
9612 /* Reached the end of the first path? If so, handle like above. */
9613 if ((cp1 == (const unsigned char *) s1->fname)
9614 || (cp2 == (const unsigned char *) s2->fname))
9615 return ((cp2 == (const unsigned char *) s2->fname)
9616 - (cp1 == (const unsigned char *) s1->fname));
9618 /* Character of current path component the same? */
9619 else if (*cp1 != *cp2)
9620 return *cp1 - *cp2;
9624 struct file_name_acquire_data
9626 struct file_info *files;
9627 int used_files;
9628 int max_files;
9631 /* Traversal function for the hash table. */
9633 static int
9634 file_name_acquire (void ** slot, void *data)
9636 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9637 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9638 struct file_info *fi;
9639 const char *f;
9641 gcc_assert (fnad->max_files >= d->emitted_number);
9643 if (! d->emitted_number)
9644 return 1;
9646 gcc_assert (fnad->max_files != fnad->used_files);
9648 fi = fnad->files + fnad->used_files++;
9650 /* Skip all leading "./". */
9651 f = d->filename;
9652 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9653 f += 2;
9655 /* Create a new array entry. */
9656 fi->path = f;
9657 fi->length = strlen (f);
9658 fi->file_idx = d;
9660 /* Search for the file name part. */
9661 f = strrchr (f, DIR_SEPARATOR);
9662 #if defined (DIR_SEPARATOR_2)
9664 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9666 if (g != NULL)
9668 if (f == NULL || f < g)
9669 f = g;
9672 #endif
9674 fi->fname = f == NULL ? fi->path : f + 1;
9675 return 1;
9678 /* Output the directory table and the file name table. We try to minimize
9679 the total amount of memory needed. A heuristic is used to avoid large
9680 slowdowns with many input files. */
9682 static void
9683 output_file_names (void)
9685 struct file_name_acquire_data fnad;
9686 int numfiles;
9687 struct file_info *files;
9688 struct dir_info *dirs;
9689 int *saved;
9690 int *savehere;
9691 int *backmap;
9692 int ndirs;
9693 int idx_offset;
9694 int i;
9696 if (!last_emitted_file)
9698 dw2_asm_output_data (1, 0, "End directory table");
9699 dw2_asm_output_data (1, 0, "End file name table");
9700 return;
9703 numfiles = last_emitted_file->emitted_number;
9705 /* Allocate the various arrays we need. */
9706 files = XALLOCAVEC (struct file_info, numfiles);
9707 dirs = XALLOCAVEC (struct dir_info, numfiles);
9709 fnad.files = files;
9710 fnad.used_files = 0;
9711 fnad.max_files = numfiles;
9712 htab_traverse (file_table, file_name_acquire, &fnad);
9713 gcc_assert (fnad.used_files == fnad.max_files);
9715 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9717 /* Find all the different directories used. */
9718 dirs[0].path = files[0].path;
9719 dirs[0].length = files[0].fname - files[0].path;
9720 dirs[0].prefix = -1;
9721 dirs[0].count = 1;
9722 dirs[0].dir_idx = 0;
9723 files[0].dir_idx = 0;
9724 ndirs = 1;
9726 for (i = 1; i < numfiles; i++)
9727 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9728 && memcmp (dirs[ndirs - 1].path, files[i].path,
9729 dirs[ndirs - 1].length) == 0)
9731 /* Same directory as last entry. */
9732 files[i].dir_idx = ndirs - 1;
9733 ++dirs[ndirs - 1].count;
9735 else
9737 int j;
9739 /* This is a new directory. */
9740 dirs[ndirs].path = files[i].path;
9741 dirs[ndirs].length = files[i].fname - files[i].path;
9742 dirs[ndirs].count = 1;
9743 dirs[ndirs].dir_idx = ndirs;
9744 files[i].dir_idx = ndirs;
9746 /* Search for a prefix. */
9747 dirs[ndirs].prefix = -1;
9748 for (j = 0; j < ndirs; j++)
9749 if (dirs[j].length < dirs[ndirs].length
9750 && dirs[j].length > 1
9751 && (dirs[ndirs].prefix == -1
9752 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9753 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9754 dirs[ndirs].prefix = j;
9756 ++ndirs;
9759 /* Now to the actual work. We have to find a subset of the directories which
9760 allow expressing the file name using references to the directory table
9761 with the least amount of characters. We do not do an exhaustive search
9762 where we would have to check out every combination of every single
9763 possible prefix. Instead we use a heuristic which provides nearly optimal
9764 results in most cases and never is much off. */
9765 saved = XALLOCAVEC (int, ndirs);
9766 savehere = XALLOCAVEC (int, ndirs);
9768 memset (saved, '\0', ndirs * sizeof (saved[0]));
9769 for (i = 0; i < ndirs; i++)
9771 int j;
9772 int total;
9774 /* We can always save some space for the current directory. But this
9775 does not mean it will be enough to justify adding the directory. */
9776 savehere[i] = dirs[i].length;
9777 total = (savehere[i] - saved[i]) * dirs[i].count;
9779 for (j = i + 1; j < ndirs; j++)
9781 savehere[j] = 0;
9782 if (saved[j] < dirs[i].length)
9784 /* Determine whether the dirs[i] path is a prefix of the
9785 dirs[j] path. */
9786 int k;
9788 k = dirs[j].prefix;
9789 while (k != -1 && k != (int) i)
9790 k = dirs[k].prefix;
9792 if (k == (int) i)
9794 /* Yes it is. We can possibly save some memory by
9795 writing the filenames in dirs[j] relative to
9796 dirs[i]. */
9797 savehere[j] = dirs[i].length;
9798 total += (savehere[j] - saved[j]) * dirs[j].count;
9803 /* Check whether we can save enough to justify adding the dirs[i]
9804 directory. */
9805 if (total > dirs[i].length + 1)
9807 /* It's worthwhile adding. */
9808 for (j = i; j < ndirs; j++)
9809 if (savehere[j] > 0)
9811 /* Remember how much we saved for this directory so far. */
9812 saved[j] = savehere[j];
9814 /* Remember the prefix directory. */
9815 dirs[j].dir_idx = i;
9820 /* Emit the directory name table. */
9821 idx_offset = dirs[0].length > 0 ? 1 : 0;
9822 for (i = 1 - idx_offset; i < ndirs; i++)
9823 dw2_asm_output_nstring (dirs[i].path,
9824 dirs[i].length
9825 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9826 "Directory Entry: %#x", i + idx_offset);
9828 dw2_asm_output_data (1, 0, "End directory table");
9830 /* We have to emit them in the order of emitted_number since that's
9831 used in the debug info generation. To do this efficiently we
9832 generate a back-mapping of the indices first. */
9833 backmap = XALLOCAVEC (int, numfiles);
9834 for (i = 0; i < numfiles; i++)
9835 backmap[files[i].file_idx->emitted_number - 1] = i;
9837 /* Now write all the file names. */
9838 for (i = 0; i < numfiles; i++)
9840 int file_idx = backmap[i];
9841 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9843 #ifdef VMS_DEBUGGING_INFO
9844 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9846 /* Setting these fields can lead to debugger miscomparisons,
9847 but VMS Debug requires them to be set correctly. */
9849 int ver;
9850 long long cdt;
9851 long siz;
9852 int maxfilelen = strlen (files[file_idx].path)
9853 + dirs[dir_idx].length
9854 + MAX_VMS_VERSION_LEN + 1;
9855 char *filebuf = XALLOCAVEC (char, maxfilelen);
9857 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9858 snprintf (filebuf, maxfilelen, "%s;%d",
9859 files[file_idx].path + dirs[dir_idx].length, ver);
9861 dw2_asm_output_nstring
9862 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9864 /* Include directory index. */
9865 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9867 /* Modification time. */
9868 dw2_asm_output_data_uleb128
9869 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9870 ? cdt : 0,
9871 NULL);
9873 /* File length in bytes. */
9874 dw2_asm_output_data_uleb128
9875 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9876 ? siz : 0,
9877 NULL);
9878 #else
9879 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9880 "File Entry: %#x", (unsigned) i + 1);
9882 /* Include directory index. */
9883 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9885 /* Modification time. */
9886 dw2_asm_output_data_uleb128 (0, NULL);
9888 /* File length in bytes. */
9889 dw2_asm_output_data_uleb128 (0, NULL);
9890 #endif /* VMS_DEBUGGING_INFO */
9893 dw2_asm_output_data (1, 0, "End file name table");
9897 /* Output one line number table into the .debug_line section. */
9899 static void
9900 output_one_line_info_table (dw_line_info_table *table)
9902 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9903 unsigned int current_line = 1;
9904 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9905 dw_line_info_entry *ent;
9906 size_t i;
9908 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9910 switch (ent->opcode)
9912 case LI_set_address:
9913 /* ??? Unfortunately, we have little choice here currently, and
9914 must always use the most general form. GCC does not know the
9915 address delta itself, so we can't use DW_LNS_advance_pc. Many
9916 ports do have length attributes which will give an upper bound
9917 on the address range. We could perhaps use length attributes
9918 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9919 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9921 /* This can handle any delta. This takes
9922 4+DWARF2_ADDR_SIZE bytes. */
9923 dw2_asm_output_data (1, 0, "set address %s", line_label);
9924 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9925 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9926 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9927 break;
9929 case LI_set_line:
9930 if (ent->val == current_line)
9932 /* We still need to start a new row, so output a copy insn. */
9933 dw2_asm_output_data (1, DW_LNS_copy,
9934 "copy line %u", current_line);
9936 else
9938 int line_offset = ent->val - current_line;
9939 int line_delta = line_offset - DWARF_LINE_BASE;
9941 current_line = ent->val;
9942 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9944 /* This can handle deltas from -10 to 234, using the current
9945 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9946 This takes 1 byte. */
9947 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9948 "line %u", current_line);
9950 else
9952 /* This can handle any delta. This takes at least 4 bytes,
9953 depending on the value being encoded. */
9954 dw2_asm_output_data (1, DW_LNS_advance_line,
9955 "advance to line %u", current_line);
9956 dw2_asm_output_data_sleb128 (line_offset, NULL);
9957 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9960 break;
9962 case LI_set_file:
9963 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9964 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9965 break;
9967 case LI_set_column:
9968 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9969 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9970 break;
9972 case LI_negate_stmt:
9973 current_is_stmt = !current_is_stmt;
9974 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9975 "is_stmt %d", current_is_stmt);
9976 break;
9978 case LI_set_prologue_end:
9979 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9980 "set prologue end");
9981 break;
9983 case LI_set_epilogue_begin:
9984 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9985 "set epilogue begin");
9986 break;
9988 case LI_set_discriminator:
9989 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9990 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9991 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9992 dw2_asm_output_data_uleb128 (ent->val, NULL);
9993 break;
9997 /* Emit debug info for the address of the end of the table. */
9998 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9999 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10000 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10001 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10003 dw2_asm_output_data (1, 0, "end sequence");
10004 dw2_asm_output_data_uleb128 (1, NULL);
10005 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10008 /* Output the source line number correspondence information. This
10009 information goes into the .debug_line section. */
10011 static void
10012 output_line_info (bool prologue_only)
10014 char l1[20], l2[20], p1[20], p2[20];
10015 int ver = dwarf_version;
10016 bool saw_one = false;
10017 int opc;
10019 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10020 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10021 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10022 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10024 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10025 dw2_asm_output_data (4, 0xffffffff,
10026 "Initial length escape value indicating 64-bit DWARF extension");
10027 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10028 "Length of Source Line Info");
10029 ASM_OUTPUT_LABEL (asm_out_file, l1);
10031 dw2_asm_output_data (2, ver, "DWARF Version");
10032 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10033 ASM_OUTPUT_LABEL (asm_out_file, p1);
10035 /* Define the architecture-dependent minimum instruction length (in bytes).
10036 In this implementation of DWARF, this field is used for information
10037 purposes only. Since GCC generates assembly language, we have no
10038 a priori knowledge of how many instruction bytes are generated for each
10039 source line, and therefore can use only the DW_LNE_set_address and
10040 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10041 this as '1', which is "correct enough" for all architectures,
10042 and don't let the target override. */
10043 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10045 if (ver >= 4)
10046 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10047 "Maximum Operations Per Instruction");
10048 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10049 "Default is_stmt_start flag");
10050 dw2_asm_output_data (1, DWARF_LINE_BASE,
10051 "Line Base Value (Special Opcodes)");
10052 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10053 "Line Range Value (Special Opcodes)");
10054 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10055 "Special Opcode Base");
10057 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10059 int n_op_args;
10060 switch (opc)
10062 case DW_LNS_advance_pc:
10063 case DW_LNS_advance_line:
10064 case DW_LNS_set_file:
10065 case DW_LNS_set_column:
10066 case DW_LNS_fixed_advance_pc:
10067 case DW_LNS_set_isa:
10068 n_op_args = 1;
10069 break;
10070 default:
10071 n_op_args = 0;
10072 break;
10075 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10076 opc, n_op_args);
10079 /* Write out the information about the files we use. */
10080 output_file_names ();
10081 ASM_OUTPUT_LABEL (asm_out_file, p2);
10082 if (prologue_only)
10084 /* Output the marker for the end of the line number info. */
10085 ASM_OUTPUT_LABEL (asm_out_file, l2);
10086 return;
10089 if (separate_line_info)
10091 dw_line_info_table *table;
10092 size_t i;
10094 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10095 if (table->in_use)
10097 output_one_line_info_table (table);
10098 saw_one = true;
10101 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10103 output_one_line_info_table (cold_text_section_line_info);
10104 saw_one = true;
10107 /* ??? Some Darwin linkers crash on a .debug_line section with no
10108 sequences. Further, merely a DW_LNE_end_sequence entry is not
10109 sufficient -- the address column must also be initialized.
10110 Make sure to output at least one set_address/end_sequence pair,
10111 choosing .text since that section is always present. */
10112 if (text_section_line_info->in_use || !saw_one)
10113 output_one_line_info_table (text_section_line_info);
10115 /* Output the marker for the end of the line number info. */
10116 ASM_OUTPUT_LABEL (asm_out_file, l2);
10119 /* Given a pointer to a tree node for some base type, return a pointer to
10120 a DIE that describes the given type.
10122 This routine must only be called for GCC type nodes that correspond to
10123 Dwarf base (fundamental) types. */
10125 static dw_die_ref
10126 base_type_die (tree type)
10128 dw_die_ref base_type_result;
10129 enum dwarf_type encoding;
10131 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10132 return 0;
10134 /* If this is a subtype that should not be emitted as a subrange type,
10135 use the base type. See subrange_type_for_debug_p. */
10136 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10137 type = TREE_TYPE (type);
10139 switch (TREE_CODE (type))
10141 case INTEGER_TYPE:
10142 if ((dwarf_version >= 4 || !dwarf_strict)
10143 && TYPE_NAME (type)
10144 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10145 && DECL_IS_BUILTIN (TYPE_NAME (type))
10146 && DECL_NAME (TYPE_NAME (type)))
10148 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10149 if (strcmp (name, "char16_t") == 0
10150 || strcmp (name, "char32_t") == 0)
10152 encoding = DW_ATE_UTF;
10153 break;
10156 if (TYPE_STRING_FLAG (type))
10158 if (TYPE_UNSIGNED (type))
10159 encoding = DW_ATE_unsigned_char;
10160 else
10161 encoding = DW_ATE_signed_char;
10163 else if (TYPE_UNSIGNED (type))
10164 encoding = DW_ATE_unsigned;
10165 else
10166 encoding = DW_ATE_signed;
10167 break;
10169 case REAL_TYPE:
10170 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10172 if (dwarf_version >= 3 || !dwarf_strict)
10173 encoding = DW_ATE_decimal_float;
10174 else
10175 encoding = DW_ATE_lo_user;
10177 else
10178 encoding = DW_ATE_float;
10179 break;
10181 case FIXED_POINT_TYPE:
10182 if (!(dwarf_version >= 3 || !dwarf_strict))
10183 encoding = DW_ATE_lo_user;
10184 else if (TYPE_UNSIGNED (type))
10185 encoding = DW_ATE_unsigned_fixed;
10186 else
10187 encoding = DW_ATE_signed_fixed;
10188 break;
10190 /* Dwarf2 doesn't know anything about complex ints, so use
10191 a user defined type for it. */
10192 case COMPLEX_TYPE:
10193 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10194 encoding = DW_ATE_complex_float;
10195 else
10196 encoding = DW_ATE_lo_user;
10197 break;
10199 case BOOLEAN_TYPE:
10200 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10201 encoding = DW_ATE_boolean;
10202 break;
10204 default:
10205 /* No other TREE_CODEs are Dwarf fundamental types. */
10206 gcc_unreachable ();
10209 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10211 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10212 int_size_in_bytes (type));
10213 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10214 add_pubtype (type, base_type_result);
10216 return base_type_result;
10219 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10220 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10222 static inline int
10223 is_base_type (tree type)
10225 switch (TREE_CODE (type))
10227 case ERROR_MARK:
10228 case VOID_TYPE:
10229 case INTEGER_TYPE:
10230 case REAL_TYPE:
10231 case FIXED_POINT_TYPE:
10232 case COMPLEX_TYPE:
10233 case BOOLEAN_TYPE:
10234 return 1;
10236 case ARRAY_TYPE:
10237 case RECORD_TYPE:
10238 case UNION_TYPE:
10239 case QUAL_UNION_TYPE:
10240 case ENUMERAL_TYPE:
10241 case FUNCTION_TYPE:
10242 case METHOD_TYPE:
10243 case POINTER_TYPE:
10244 case REFERENCE_TYPE:
10245 case NULLPTR_TYPE:
10246 case OFFSET_TYPE:
10247 case LANG_TYPE:
10248 case VECTOR_TYPE:
10249 return 0;
10251 default:
10252 gcc_unreachable ();
10255 return 0;
10258 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10259 node, return the size in bits for the type if it is a constant, or else
10260 return the alignment for the type if the type's size is not constant, or
10261 else return BITS_PER_WORD if the type actually turns out to be an
10262 ERROR_MARK node. */
10264 static inline unsigned HOST_WIDE_INT
10265 simple_type_size_in_bits (const_tree type)
10267 if (TREE_CODE (type) == ERROR_MARK)
10268 return BITS_PER_WORD;
10269 else if (TYPE_SIZE (type) == NULL_TREE)
10270 return 0;
10271 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10272 return tree_to_uhwi (TYPE_SIZE (type));
10273 else
10274 return TYPE_ALIGN (type);
10277 /* Similarly, but return a double_int instead of UHWI. */
10279 static inline double_int
10280 double_int_type_size_in_bits (const_tree type)
10282 if (TREE_CODE (type) == ERROR_MARK)
10283 return double_int::from_uhwi (BITS_PER_WORD);
10284 else if (TYPE_SIZE (type) == NULL_TREE)
10285 return double_int_zero;
10286 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10287 return tree_to_double_int (TYPE_SIZE (type));
10288 else
10289 return double_int::from_uhwi (TYPE_ALIGN (type));
10292 /* Given a pointer to a tree node for a subrange type, return a pointer
10293 to a DIE that describes the given type. */
10295 static dw_die_ref
10296 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10298 dw_die_ref subrange_die;
10299 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10301 if (context_die == NULL)
10302 context_die = comp_unit_die ();
10304 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10306 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10308 /* The size of the subrange type and its base type do not match,
10309 so we need to generate a size attribute for the subrange type. */
10310 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10313 if (low)
10314 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10315 if (high)
10316 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10318 return subrange_die;
10321 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10322 entry that chains various modifiers in front of the given type. */
10324 static dw_die_ref
10325 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10326 dw_die_ref context_die)
10328 enum tree_code code = TREE_CODE (type);
10329 dw_die_ref mod_type_die;
10330 dw_die_ref sub_die = NULL;
10331 tree item_type = NULL;
10332 tree qualified_type;
10333 tree name, low, high;
10334 dw_die_ref mod_scope;
10336 if (code == ERROR_MARK)
10337 return NULL;
10339 /* See if we already have the appropriately qualified variant of
10340 this type. */
10341 qualified_type
10342 = get_qualified_type (type,
10343 ((is_const_type ? TYPE_QUAL_CONST : 0)
10344 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10346 if (qualified_type == sizetype
10347 && TYPE_NAME (qualified_type)
10348 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10350 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10352 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10353 && TYPE_PRECISION (t)
10354 == TYPE_PRECISION (qualified_type)
10355 && TYPE_UNSIGNED (t)
10356 == TYPE_UNSIGNED (qualified_type));
10357 qualified_type = t;
10360 /* If we do, then we can just use its DIE, if it exists. */
10361 if (qualified_type)
10363 mod_type_die = lookup_type_die (qualified_type);
10364 if (mod_type_die)
10365 return mod_type_die;
10368 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10370 /* Handle C typedef types. */
10371 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10372 && !DECL_ARTIFICIAL (name))
10374 tree dtype = TREE_TYPE (name);
10376 if (qualified_type == dtype)
10378 /* For a named type, use the typedef. */
10379 gen_type_die (qualified_type, context_die);
10380 return lookup_type_die (qualified_type);
10382 else if (is_const_type < TYPE_READONLY (dtype)
10383 || is_volatile_type < TYPE_VOLATILE (dtype)
10384 || (is_const_type <= TYPE_READONLY (dtype)
10385 && is_volatile_type <= TYPE_VOLATILE (dtype)
10386 && DECL_ORIGINAL_TYPE (name) != type))
10387 /* cv-unqualified version of named type. Just use the unnamed
10388 type to which it refers. */
10389 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10390 is_const_type, is_volatile_type,
10391 context_die);
10392 /* Else cv-qualified version of named type; fall through. */
10395 mod_scope = scope_die_for (type, context_die);
10397 if (is_const_type
10398 /* If both is_const_type and is_volatile_type, prefer the path
10399 which leads to a qualified type. */
10400 && (!is_volatile_type
10401 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10402 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10404 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10405 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10407 else if (is_volatile_type)
10409 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10410 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10412 else if (code == POINTER_TYPE)
10414 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10415 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10416 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10417 item_type = TREE_TYPE (type);
10418 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10419 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10420 TYPE_ADDR_SPACE (item_type));
10422 else if (code == REFERENCE_TYPE)
10424 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10425 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10426 type);
10427 else
10428 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10429 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10430 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10431 item_type = TREE_TYPE (type);
10432 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10433 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10434 TYPE_ADDR_SPACE (item_type));
10436 else if (code == INTEGER_TYPE
10437 && TREE_TYPE (type) != NULL_TREE
10438 && subrange_type_for_debug_p (type, &low, &high))
10440 mod_type_die = subrange_type_die (type, low, high, context_die);
10441 item_type = TREE_TYPE (type);
10443 else if (is_base_type (type))
10444 mod_type_die = base_type_die (type);
10445 else
10447 gen_type_die (type, context_die);
10449 /* We have to get the type_main_variant here (and pass that to the
10450 `lookup_type_die' routine) because the ..._TYPE node we have
10451 might simply be a *copy* of some original type node (where the
10452 copy was created to help us keep track of typedef names) and
10453 that copy might have a different TYPE_UID from the original
10454 ..._TYPE node. */
10455 if (TREE_CODE (type) != VECTOR_TYPE)
10456 return lookup_type_die (type_main_variant (type));
10457 else
10458 /* Vectors have the debugging information in the type,
10459 not the main variant. */
10460 return lookup_type_die (type);
10463 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10464 don't output a DW_TAG_typedef, since there isn't one in the
10465 user's program; just attach a DW_AT_name to the type.
10466 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10467 if the base type already has the same name. */
10468 if (name
10469 && ((TREE_CODE (name) != TYPE_DECL
10470 && (qualified_type == TYPE_MAIN_VARIANT (type)
10471 || (!is_const_type && !is_volatile_type)))
10472 || (TREE_CODE (name) == TYPE_DECL
10473 && TREE_TYPE (name) == qualified_type
10474 && DECL_NAME (name))))
10476 if (TREE_CODE (name) == TYPE_DECL)
10477 /* Could just call add_name_and_src_coords_attributes here,
10478 but since this is a builtin type it doesn't have any
10479 useful source coordinates anyway. */
10480 name = DECL_NAME (name);
10481 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10483 /* This probably indicates a bug. */
10484 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10486 name = TYPE_NAME (type);
10487 if (name
10488 && TREE_CODE (name) == TYPE_DECL)
10489 name = DECL_NAME (name);
10490 add_name_attribute (mod_type_die,
10491 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10494 if (qualified_type)
10495 equate_type_number_to_die (qualified_type, mod_type_die);
10497 if (item_type)
10498 /* We must do this after the equate_type_number_to_die call, in case
10499 this is a recursive type. This ensures that the modified_type_die
10500 recursion will terminate even if the type is recursive. Recursive
10501 types are possible in Ada. */
10502 sub_die = modified_type_die (item_type,
10503 TYPE_READONLY (item_type),
10504 TYPE_VOLATILE (item_type),
10505 context_die);
10507 if (sub_die != NULL)
10508 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10510 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10511 if (TYPE_ARTIFICIAL (type))
10512 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10514 return mod_type_die;
10517 /* Generate DIEs for the generic parameters of T.
10518 T must be either a generic type or a generic function.
10519 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10521 static void
10522 gen_generic_params_dies (tree t)
10524 tree parms, args;
10525 int parms_num, i;
10526 dw_die_ref die = NULL;
10527 int non_default;
10529 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10530 return;
10532 if (TYPE_P (t))
10533 die = lookup_type_die (t);
10534 else if (DECL_P (t))
10535 die = lookup_decl_die (t);
10537 gcc_assert (die);
10539 parms = lang_hooks.get_innermost_generic_parms (t);
10540 if (!parms)
10541 /* T has no generic parameter. It means T is neither a generic type
10542 or function. End of story. */
10543 return;
10545 parms_num = TREE_VEC_LENGTH (parms);
10546 args = lang_hooks.get_innermost_generic_args (t);
10547 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10548 non_default = int_cst_value (TREE_CHAIN (args));
10549 else
10550 non_default = TREE_VEC_LENGTH (args);
10551 for (i = 0; i < parms_num; i++)
10553 tree parm, arg, arg_pack_elems;
10554 dw_die_ref parm_die;
10556 parm = TREE_VEC_ELT (parms, i);
10557 arg = TREE_VEC_ELT (args, i);
10558 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10559 gcc_assert (parm && TREE_VALUE (parm) && arg);
10561 if (parm && TREE_VALUE (parm) && arg)
10563 /* If PARM represents a template parameter pack,
10564 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10565 by DW_TAG_template_*_parameter DIEs for the argument
10566 pack elements of ARG. Note that ARG would then be
10567 an argument pack. */
10568 if (arg_pack_elems)
10569 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10570 arg_pack_elems,
10571 die);
10572 else
10573 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10574 true /* emit name */, die);
10575 if (i >= non_default)
10576 add_AT_flag (parm_die, DW_AT_default_value, 1);
10581 /* Create and return a DIE for PARM which should be
10582 the representation of a generic type parameter.
10583 For instance, in the C++ front end, PARM would be a template parameter.
10584 ARG is the argument to PARM.
10585 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10586 name of the PARM.
10587 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10588 as a child node. */
10590 static dw_die_ref
10591 generic_parameter_die (tree parm, tree arg,
10592 bool emit_name_p,
10593 dw_die_ref parent_die)
10595 dw_die_ref tmpl_die = NULL;
10596 const char *name = NULL;
10598 if (!parm || !DECL_NAME (parm) || !arg)
10599 return NULL;
10601 /* We support non-type generic parameters and arguments,
10602 type generic parameters and arguments, as well as
10603 generic generic parameters (a.k.a. template template parameters in C++)
10604 and arguments. */
10605 if (TREE_CODE (parm) == PARM_DECL)
10606 /* PARM is a nontype generic parameter */
10607 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10608 else if (TREE_CODE (parm) == TYPE_DECL)
10609 /* PARM is a type generic parameter. */
10610 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10611 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10612 /* PARM is a generic generic parameter.
10613 Its DIE is a GNU extension. It shall have a
10614 DW_AT_name attribute to represent the name of the template template
10615 parameter, and a DW_AT_GNU_template_name attribute to represent the
10616 name of the template template argument. */
10617 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10618 parent_die, parm);
10619 else
10620 gcc_unreachable ();
10622 if (tmpl_die)
10624 tree tmpl_type;
10626 /* If PARM is a generic parameter pack, it means we are
10627 emitting debug info for a template argument pack element.
10628 In other terms, ARG is a template argument pack element.
10629 In that case, we don't emit any DW_AT_name attribute for
10630 the die. */
10631 if (emit_name_p)
10633 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10634 gcc_assert (name);
10635 add_AT_string (tmpl_die, DW_AT_name, name);
10638 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10640 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10641 TMPL_DIE should have a child DW_AT_type attribute that is set
10642 to the type of the argument to PARM, which is ARG.
10643 If PARM is a type generic parameter, TMPL_DIE should have a
10644 child DW_AT_type that is set to ARG. */
10645 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10646 add_type_attribute (tmpl_die, tmpl_type, 0,
10647 TREE_THIS_VOLATILE (tmpl_type),
10648 parent_die);
10650 else
10652 /* So TMPL_DIE is a DIE representing a
10653 a generic generic template parameter, a.k.a template template
10654 parameter in C++ and arg is a template. */
10656 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10657 to the name of the argument. */
10658 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10659 if (name)
10660 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10663 if (TREE_CODE (parm) == PARM_DECL)
10664 /* So PARM is a non-type generic parameter.
10665 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10666 attribute of TMPL_DIE which value represents the value
10667 of ARG.
10668 We must be careful here:
10669 The value of ARG might reference some function decls.
10670 We might currently be emitting debug info for a generic
10671 type and types are emitted before function decls, we don't
10672 know if the function decls referenced by ARG will actually be
10673 emitted after cgraph computations.
10674 So must defer the generation of the DW_AT_const_value to
10675 after cgraph is ready. */
10676 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10679 return tmpl_die;
10682 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10683 PARM_PACK must be a template parameter pack. The returned DIE
10684 will be child DIE of PARENT_DIE. */
10686 static dw_die_ref
10687 template_parameter_pack_die (tree parm_pack,
10688 tree parm_pack_args,
10689 dw_die_ref parent_die)
10691 dw_die_ref die;
10692 int j;
10694 gcc_assert (parent_die && parm_pack);
10696 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10697 add_name_and_src_coords_attributes (die, parm_pack);
10698 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10699 generic_parameter_die (parm_pack,
10700 TREE_VEC_ELT (parm_pack_args, j),
10701 false /* Don't emit DW_AT_name */,
10702 die);
10703 return die;
10706 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10707 an enumerated type. */
10709 static inline int
10710 type_is_enum (const_tree type)
10712 return TREE_CODE (type) == ENUMERAL_TYPE;
10715 /* Return the DBX register number described by a given RTL node. */
10717 static unsigned int
10718 dbx_reg_number (const_rtx rtl)
10720 unsigned regno = REGNO (rtl);
10722 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10724 #ifdef LEAF_REG_REMAP
10725 if (crtl->uses_only_leaf_regs)
10727 int leaf_reg = LEAF_REG_REMAP (regno);
10728 if (leaf_reg != -1)
10729 regno = (unsigned) leaf_reg;
10731 #endif
10733 regno = DBX_REGISTER_NUMBER (regno);
10734 gcc_assert (regno != INVALID_REGNUM);
10735 return regno;
10738 /* Optionally add a DW_OP_piece term to a location description expression.
10739 DW_OP_piece is only added if the location description expression already
10740 doesn't end with DW_OP_piece. */
10742 static void
10743 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10745 dw_loc_descr_ref loc;
10747 if (*list_head != NULL)
10749 /* Find the end of the chain. */
10750 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10753 if (loc->dw_loc_opc != DW_OP_piece)
10754 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10758 /* Return a location descriptor that designates a machine register or
10759 zero if there is none. */
10761 static dw_loc_descr_ref
10762 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10764 rtx regs;
10766 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10767 return 0;
10769 /* We only use "frame base" when we're sure we're talking about the
10770 post-prologue local stack frame. We do this by *not* running
10771 register elimination until this point, and recognizing the special
10772 argument pointer and soft frame pointer rtx's.
10773 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10774 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10775 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10777 dw_loc_descr_ref result = NULL;
10779 if (dwarf_version >= 4 || !dwarf_strict)
10781 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10782 initialized);
10783 if (result)
10784 add_loc_descr (&result,
10785 new_loc_descr (DW_OP_stack_value, 0, 0));
10787 return result;
10790 regs = targetm.dwarf_register_span (rtl);
10792 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10793 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10794 else
10796 unsigned int dbx_regnum = dbx_reg_number (rtl);
10797 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10798 return 0;
10799 return one_reg_loc_descriptor (dbx_regnum, initialized);
10803 /* Return a location descriptor that designates a machine register for
10804 a given hard register number. */
10806 static dw_loc_descr_ref
10807 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10809 dw_loc_descr_ref reg_loc_descr;
10811 if (regno <= 31)
10812 reg_loc_descr
10813 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10814 else
10815 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10817 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10818 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10820 return reg_loc_descr;
10823 /* Given an RTL of a register, return a location descriptor that
10824 designates a value that spans more than one register. */
10826 static dw_loc_descr_ref
10827 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10828 enum var_init_status initialized)
10830 int size, i;
10831 dw_loc_descr_ref loc_result = NULL;
10833 /* Simple, contiguous registers. */
10834 if (regs == NULL_RTX)
10836 unsigned reg = REGNO (rtl);
10837 int nregs;
10839 #ifdef LEAF_REG_REMAP
10840 if (crtl->uses_only_leaf_regs)
10842 int leaf_reg = LEAF_REG_REMAP (reg);
10843 if (leaf_reg != -1)
10844 reg = (unsigned) leaf_reg;
10846 #endif
10848 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10849 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10851 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10853 loc_result = NULL;
10854 while (nregs--)
10856 dw_loc_descr_ref t;
10858 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10859 VAR_INIT_STATUS_INITIALIZED);
10860 add_loc_descr (&loc_result, t);
10861 add_loc_descr_op_piece (&loc_result, size);
10862 ++reg;
10864 return loc_result;
10867 /* Now onto stupid register sets in non contiguous locations. */
10869 gcc_assert (GET_CODE (regs) == PARALLEL);
10871 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10872 loc_result = NULL;
10874 for (i = 0; i < XVECLEN (regs, 0); ++i)
10876 dw_loc_descr_ref t;
10878 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10879 VAR_INIT_STATUS_INITIALIZED);
10880 add_loc_descr (&loc_result, t);
10881 add_loc_descr_op_piece (&loc_result, size);
10884 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10885 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10886 return loc_result;
10889 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10891 /* Return a location descriptor that designates a constant i,
10892 as a compound operation from constant (i >> shift), constant shift
10893 and DW_OP_shl. */
10895 static dw_loc_descr_ref
10896 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10898 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10899 add_loc_descr (&ret, int_loc_descriptor (shift));
10900 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10901 return ret;
10904 /* Return a location descriptor that designates a constant. */
10906 static dw_loc_descr_ref
10907 int_loc_descriptor (HOST_WIDE_INT i)
10909 enum dwarf_location_atom op;
10911 /* Pick the smallest representation of a constant, rather than just
10912 defaulting to the LEB encoding. */
10913 if (i >= 0)
10915 int clz = clz_hwi (i);
10916 int ctz = ctz_hwi (i);
10917 if (i <= 31)
10918 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10919 else if (i <= 0xff)
10920 op = DW_OP_const1u;
10921 else if (i <= 0xffff)
10922 op = DW_OP_const2u;
10923 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10924 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10925 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10926 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10927 while DW_OP_const4u is 5 bytes. */
10928 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10929 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10930 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10931 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10932 while DW_OP_const4u is 5 bytes. */
10933 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10934 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10935 op = DW_OP_const4u;
10936 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10937 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10938 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10939 while DW_OP_constu of constant >= 0x100000000 takes at least
10940 6 bytes. */
10941 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10942 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10943 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10944 >= HOST_BITS_PER_WIDE_INT)
10945 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10946 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10947 while DW_OP_constu takes in this case at least 6 bytes. */
10948 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10949 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10950 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10951 && size_of_uleb128 (i) > 6)
10952 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10953 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10954 else
10955 op = DW_OP_constu;
10957 else
10959 if (i >= -0x80)
10960 op = DW_OP_const1s;
10961 else if (i >= -0x8000)
10962 op = DW_OP_const2s;
10963 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10965 if (size_of_int_loc_descriptor (i) < 5)
10967 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10968 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10969 return ret;
10971 op = DW_OP_const4s;
10973 else
10975 if (size_of_int_loc_descriptor (i)
10976 < (unsigned long) 1 + size_of_sleb128 (i))
10978 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10979 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10980 return ret;
10982 op = DW_OP_consts;
10986 return new_loc_descr (op, i, 0);
10989 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10990 without actually allocating it. */
10992 static unsigned long
10993 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10995 return size_of_int_loc_descriptor (i >> shift)
10996 + size_of_int_loc_descriptor (shift)
10997 + 1;
11000 /* Return size_of_locs (int_loc_descriptor (i)) without
11001 actually allocating it. */
11003 static unsigned long
11004 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11006 unsigned long s;
11008 if (i >= 0)
11010 int clz, ctz;
11011 if (i <= 31)
11012 return 1;
11013 else if (i <= 0xff)
11014 return 2;
11015 else if (i <= 0xffff)
11016 return 3;
11017 clz = clz_hwi (i);
11018 ctz = ctz_hwi (i);
11019 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11020 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11021 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11022 - clz - 5);
11023 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11024 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11025 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11026 - clz - 8);
11027 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11028 return 5;
11029 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11030 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11031 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11032 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11033 - clz - 8);
11034 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11035 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11036 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11037 - clz - 16);
11038 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11039 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11040 && s > 6)
11041 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11042 - clz - 32);
11043 else
11044 return 1 + s;
11046 else
11048 if (i >= -0x80)
11049 return 2;
11050 else if (i >= -0x8000)
11051 return 3;
11052 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11054 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11056 s = size_of_int_loc_descriptor (-i) + 1;
11057 if (s < 5)
11058 return s;
11060 return 5;
11062 else
11064 unsigned long r = 1 + size_of_sleb128 (i);
11065 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11067 s = size_of_int_loc_descriptor (-i) + 1;
11068 if (s < r)
11069 return s;
11071 return r;
11076 /* Return loc description representing "address" of integer value.
11077 This can appear only as toplevel expression. */
11079 static dw_loc_descr_ref
11080 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11082 int litsize;
11083 dw_loc_descr_ref loc_result = NULL;
11085 if (!(dwarf_version >= 4 || !dwarf_strict))
11086 return NULL;
11088 litsize = size_of_int_loc_descriptor (i);
11089 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11090 is more compact. For DW_OP_stack_value we need:
11091 litsize + 1 (DW_OP_stack_value)
11092 and for DW_OP_implicit_value:
11093 1 (DW_OP_implicit_value) + 1 (length) + size. */
11094 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11096 loc_result = int_loc_descriptor (i);
11097 add_loc_descr (&loc_result,
11098 new_loc_descr (DW_OP_stack_value, 0, 0));
11099 return loc_result;
11102 loc_result = new_loc_descr (DW_OP_implicit_value,
11103 size, 0);
11104 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11105 loc_result->dw_loc_oprnd2.v.val_int = i;
11106 return loc_result;
11109 /* Return a location descriptor that designates a base+offset location. */
11111 static dw_loc_descr_ref
11112 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11113 enum var_init_status initialized)
11115 unsigned int regno;
11116 dw_loc_descr_ref result;
11117 dw_fde_ref fde = cfun->fde;
11119 /* We only use "frame base" when we're sure we're talking about the
11120 post-prologue local stack frame. We do this by *not* running
11121 register elimination until this point, and recognizing the special
11122 argument pointer and soft frame pointer rtx's. */
11123 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11125 rtx elim = (ira_use_lra_p
11126 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11127 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11129 if (elim != reg)
11131 if (GET_CODE (elim) == PLUS)
11133 offset += INTVAL (XEXP (elim, 1));
11134 elim = XEXP (elim, 0);
11136 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11137 && (elim == hard_frame_pointer_rtx
11138 || elim == stack_pointer_rtx))
11139 || elim == (frame_pointer_needed
11140 ? hard_frame_pointer_rtx
11141 : stack_pointer_rtx));
11143 /* If drap register is used to align stack, use frame
11144 pointer + offset to access stack variables. If stack
11145 is aligned without drap, use stack pointer + offset to
11146 access stack variables. */
11147 if (crtl->stack_realign_tried
11148 && reg == frame_pointer_rtx)
11150 int base_reg
11151 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11152 ? HARD_FRAME_POINTER_REGNUM
11153 : REGNO (elim));
11154 return new_reg_loc_descr (base_reg, offset);
11157 gcc_assert (frame_pointer_fb_offset_valid);
11158 offset += frame_pointer_fb_offset;
11159 return new_loc_descr (DW_OP_fbreg, offset, 0);
11163 regno = REGNO (reg);
11164 #ifdef LEAF_REG_REMAP
11165 if (crtl->uses_only_leaf_regs)
11167 int leaf_reg = LEAF_REG_REMAP (regno);
11168 if (leaf_reg != -1)
11169 regno = (unsigned) leaf_reg;
11171 #endif
11172 regno = DWARF_FRAME_REGNUM (regno);
11174 if (!optimize && fde
11175 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11177 /* Use cfa+offset to represent the location of arguments passed
11178 on the stack when drap is used to align stack.
11179 Only do this when not optimizing, for optimized code var-tracking
11180 is supposed to track where the arguments live and the register
11181 used as vdrap or drap in some spot might be used for something
11182 else in other part of the routine. */
11183 return new_loc_descr (DW_OP_fbreg, offset, 0);
11186 if (regno <= 31)
11187 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11188 offset, 0);
11189 else
11190 result = new_loc_descr (DW_OP_bregx, regno, offset);
11192 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11193 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11195 return result;
11198 /* Return true if this RTL expression describes a base+offset calculation. */
11200 static inline int
11201 is_based_loc (const_rtx rtl)
11203 return (GET_CODE (rtl) == PLUS
11204 && ((REG_P (XEXP (rtl, 0))
11205 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11206 && CONST_INT_P (XEXP (rtl, 1)))));
11209 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11210 failed. */
11212 static dw_loc_descr_ref
11213 tls_mem_loc_descriptor (rtx mem)
11215 tree base;
11216 dw_loc_descr_ref loc_result;
11218 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11219 return NULL;
11221 base = get_base_address (MEM_EXPR (mem));
11222 if (base == NULL
11223 || TREE_CODE (base) != VAR_DECL
11224 || !DECL_THREAD_LOCAL_P (base))
11225 return NULL;
11227 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11228 if (loc_result == NULL)
11229 return NULL;
11231 if (MEM_OFFSET (mem))
11232 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11234 return loc_result;
11237 /* Output debug info about reason why we failed to expand expression as dwarf
11238 expression. */
11240 static void
11241 expansion_failed (tree expr, rtx rtl, char const *reason)
11243 if (dump_file && (dump_flags & TDF_DETAILS))
11245 fprintf (dump_file, "Failed to expand as dwarf: ");
11246 if (expr)
11247 print_generic_expr (dump_file, expr, dump_flags);
11248 if (rtl)
11250 fprintf (dump_file, "\n");
11251 print_rtl (dump_file, rtl);
11253 fprintf (dump_file, "\nReason: %s\n", reason);
11257 /* Helper function for const_ok_for_output, called either directly
11258 or via for_each_rtx. */
11260 static int
11261 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11263 rtx rtl = *rtlp;
11265 if (GET_CODE (rtl) == UNSPEC)
11267 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11268 we can't express it in the debug info. */
11269 #ifdef ENABLE_CHECKING
11270 /* Don't complain about TLS UNSPECs, those are just too hard to
11271 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11272 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11273 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11274 if (XVECLEN (rtl, 0) == 0
11275 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11276 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11277 inform (current_function_decl
11278 ? DECL_SOURCE_LOCATION (current_function_decl)
11279 : UNKNOWN_LOCATION,
11280 #if NUM_UNSPEC_VALUES > 0
11281 "non-delegitimized UNSPEC %s (%d) found in variable location",
11282 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11283 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11284 XINT (rtl, 1));
11285 #else
11286 "non-delegitimized UNSPEC %d found in variable location",
11287 XINT (rtl, 1));
11288 #endif
11289 #endif
11290 expansion_failed (NULL_TREE, rtl,
11291 "UNSPEC hasn't been delegitimized.\n");
11292 return 1;
11295 if (targetm.const_not_ok_for_debug_p (rtl))
11297 expansion_failed (NULL_TREE, rtl,
11298 "Expression rejected for debug by the backend.\n");
11299 return 1;
11302 if (GET_CODE (rtl) != SYMBOL_REF)
11303 return 0;
11305 if (CONSTANT_POOL_ADDRESS_P (rtl))
11307 bool marked;
11308 get_pool_constant_mark (rtl, &marked);
11309 /* If all references to this pool constant were optimized away,
11310 it was not output and thus we can't represent it. */
11311 if (!marked)
11313 expansion_failed (NULL_TREE, rtl,
11314 "Constant was removed from constant pool.\n");
11315 return 1;
11319 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11320 return 1;
11322 /* Avoid references to external symbols in debug info, on several targets
11323 the linker might even refuse to link when linking a shared library,
11324 and in many other cases the relocations for .debug_info/.debug_loc are
11325 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11326 to be defined within the same shared library or executable are fine. */
11327 if (SYMBOL_REF_EXTERNAL_P (rtl))
11329 tree decl = SYMBOL_REF_DECL (rtl);
11331 if (decl == NULL || !targetm.binds_local_p (decl))
11333 expansion_failed (NULL_TREE, rtl,
11334 "Symbol not defined in current TU.\n");
11335 return 1;
11339 return 0;
11342 /* Return true if constant RTL can be emitted in DW_OP_addr or
11343 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11344 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11346 static bool
11347 const_ok_for_output (rtx rtl)
11349 if (GET_CODE (rtl) == SYMBOL_REF)
11350 return const_ok_for_output_1 (&rtl, NULL) == 0;
11352 if (GET_CODE (rtl) == CONST)
11353 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11355 return true;
11358 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11359 if possible, NULL otherwise. */
11361 static dw_die_ref
11362 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11364 dw_die_ref type_die;
11365 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11367 if (type == NULL)
11368 return NULL;
11369 switch (TREE_CODE (type))
11371 case INTEGER_TYPE:
11372 case REAL_TYPE:
11373 break;
11374 default:
11375 return NULL;
11377 type_die = lookup_type_die (type);
11378 if (!type_die)
11379 type_die = modified_type_die (type, false, false, comp_unit_die ());
11380 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11381 return NULL;
11382 return type_die;
11385 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11386 type matching MODE, or, if MODE is narrower than or as wide as
11387 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11388 possible. */
11390 static dw_loc_descr_ref
11391 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11393 enum machine_mode outer_mode = mode;
11394 dw_die_ref type_die;
11395 dw_loc_descr_ref cvt;
11397 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11399 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11400 return op;
11402 type_die = base_type_for_mode (outer_mode, 1);
11403 if (type_die == NULL)
11404 return NULL;
11405 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11406 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11407 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11408 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11409 add_loc_descr (&op, cvt);
11410 return op;
11413 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11415 static dw_loc_descr_ref
11416 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11417 dw_loc_descr_ref op1)
11419 dw_loc_descr_ref ret = op0;
11420 add_loc_descr (&ret, op1);
11421 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11422 if (STORE_FLAG_VALUE != 1)
11424 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11425 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11427 return ret;
11430 /* Return location descriptor for signed comparison OP RTL. */
11432 static dw_loc_descr_ref
11433 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11434 enum machine_mode mem_mode)
11436 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11437 dw_loc_descr_ref op0, op1;
11438 int shift;
11440 if (op_mode == VOIDmode)
11441 op_mode = GET_MODE (XEXP (rtl, 1));
11442 if (op_mode == VOIDmode)
11443 return NULL;
11445 if (dwarf_strict
11446 && (GET_MODE_CLASS (op_mode) != MODE_INT
11447 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11448 return NULL;
11450 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11451 VAR_INIT_STATUS_INITIALIZED);
11452 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11453 VAR_INIT_STATUS_INITIALIZED);
11455 if (op0 == NULL || op1 == NULL)
11456 return NULL;
11458 if (GET_MODE_CLASS (op_mode) != MODE_INT
11459 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11460 return compare_loc_descriptor (op, op0, op1);
11462 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11464 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11465 dw_loc_descr_ref cvt;
11467 if (type_die == NULL)
11468 return NULL;
11469 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11470 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11471 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11472 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11473 add_loc_descr (&op0, cvt);
11474 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11475 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11476 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11477 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11478 add_loc_descr (&op1, cvt);
11479 return compare_loc_descriptor (op, op0, op1);
11482 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11483 /* For eq/ne, if the operands are known to be zero-extended,
11484 there is no need to do the fancy shifting up. */
11485 if (op == DW_OP_eq || op == DW_OP_ne)
11487 dw_loc_descr_ref last0, last1;
11488 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11490 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11492 /* deref_size zero extends, and for constants we can check
11493 whether they are zero extended or not. */
11494 if (((last0->dw_loc_opc == DW_OP_deref_size
11495 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11496 || (CONST_INT_P (XEXP (rtl, 0))
11497 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11498 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11499 && ((last1->dw_loc_opc == DW_OP_deref_size
11500 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11501 || (CONST_INT_P (XEXP (rtl, 1))
11502 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11503 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11504 return compare_loc_descriptor (op, op0, op1);
11506 /* EQ/NE comparison against constant in narrower type than
11507 DWARF2_ADDR_SIZE can be performed either as
11508 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11509 DW_OP_{eq,ne}
11511 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11512 DW_OP_{eq,ne}. Pick whatever is shorter. */
11513 if (CONST_INT_P (XEXP (rtl, 1))
11514 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11515 && (size_of_int_loc_descriptor (shift) + 1
11516 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11517 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11518 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11519 & GET_MODE_MASK (op_mode))))
11521 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11522 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11523 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11524 & GET_MODE_MASK (op_mode));
11525 return compare_loc_descriptor (op, op0, op1);
11528 add_loc_descr (&op0, int_loc_descriptor (shift));
11529 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11530 if (CONST_INT_P (XEXP (rtl, 1)))
11531 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11532 else
11534 add_loc_descr (&op1, int_loc_descriptor (shift));
11535 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11537 return compare_loc_descriptor (op, op0, op1);
11540 /* Return location descriptor for unsigned comparison OP RTL. */
11542 static dw_loc_descr_ref
11543 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11544 enum machine_mode mem_mode)
11546 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11547 dw_loc_descr_ref op0, op1;
11549 if (op_mode == VOIDmode)
11550 op_mode = GET_MODE (XEXP (rtl, 1));
11551 if (op_mode == VOIDmode)
11552 return NULL;
11553 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11554 return NULL;
11556 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11557 return NULL;
11559 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11560 VAR_INIT_STATUS_INITIALIZED);
11561 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11562 VAR_INIT_STATUS_INITIALIZED);
11564 if (op0 == NULL || op1 == NULL)
11565 return NULL;
11567 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11569 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11570 dw_loc_descr_ref last0, last1;
11571 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11573 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11575 if (CONST_INT_P (XEXP (rtl, 0)))
11576 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11577 /* deref_size zero extends, so no need to mask it again. */
11578 else if (last0->dw_loc_opc != DW_OP_deref_size
11579 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11581 add_loc_descr (&op0, int_loc_descriptor (mask));
11582 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11584 if (CONST_INT_P (XEXP (rtl, 1)))
11585 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11586 /* deref_size zero extends, so no need to mask it again. */
11587 else if (last1->dw_loc_opc != DW_OP_deref_size
11588 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11590 add_loc_descr (&op1, int_loc_descriptor (mask));
11591 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11594 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11596 HOST_WIDE_INT bias = 1;
11597 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11598 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11599 if (CONST_INT_P (XEXP (rtl, 1)))
11600 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11601 + INTVAL (XEXP (rtl, 1)));
11602 else
11603 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11604 bias, 0));
11606 return compare_loc_descriptor (op, op0, op1);
11609 /* Return location descriptor for {U,S}{MIN,MAX}. */
11611 static dw_loc_descr_ref
11612 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11613 enum machine_mode mem_mode)
11615 enum dwarf_location_atom op;
11616 dw_loc_descr_ref op0, op1, ret;
11617 dw_loc_descr_ref bra_node, drop_node;
11619 if (dwarf_strict
11620 && (GET_MODE_CLASS (mode) != MODE_INT
11621 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11622 return NULL;
11624 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11625 VAR_INIT_STATUS_INITIALIZED);
11626 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11627 VAR_INIT_STATUS_INITIALIZED);
11629 if (op0 == NULL || op1 == NULL)
11630 return NULL;
11632 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11633 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11634 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11635 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11637 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11639 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11640 add_loc_descr (&op0, int_loc_descriptor (mask));
11641 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11642 add_loc_descr (&op1, int_loc_descriptor (mask));
11643 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11645 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11647 HOST_WIDE_INT bias = 1;
11648 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11649 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11650 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11653 else if (GET_MODE_CLASS (mode) == MODE_INT
11654 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11656 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11657 add_loc_descr (&op0, int_loc_descriptor (shift));
11658 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11659 add_loc_descr (&op1, int_loc_descriptor (shift));
11660 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11662 else if (GET_MODE_CLASS (mode) == MODE_INT
11663 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11665 dw_die_ref type_die = base_type_for_mode (mode, 0);
11666 dw_loc_descr_ref cvt;
11667 if (type_die == NULL)
11668 return NULL;
11669 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11670 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11671 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11672 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11673 add_loc_descr (&op0, cvt);
11674 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11675 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11676 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11677 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11678 add_loc_descr (&op1, cvt);
11681 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11682 op = DW_OP_lt;
11683 else
11684 op = DW_OP_gt;
11685 ret = op0;
11686 add_loc_descr (&ret, op1);
11687 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11688 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11689 add_loc_descr (&ret, bra_node);
11690 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11691 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11692 add_loc_descr (&ret, drop_node);
11693 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11694 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11695 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11696 && GET_MODE_CLASS (mode) == MODE_INT
11697 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11698 ret = convert_descriptor_to_mode (mode, ret);
11699 return ret;
11702 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11703 but after converting arguments to type_die, afterwards
11704 convert back to unsigned. */
11706 static dw_loc_descr_ref
11707 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11708 enum machine_mode mode, enum machine_mode mem_mode)
11710 dw_loc_descr_ref cvt, op0, op1;
11712 if (type_die == NULL)
11713 return NULL;
11714 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11715 VAR_INIT_STATUS_INITIALIZED);
11716 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11717 VAR_INIT_STATUS_INITIALIZED);
11718 if (op0 == NULL || op1 == NULL)
11719 return NULL;
11720 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11721 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11722 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11723 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11724 add_loc_descr (&op0, cvt);
11725 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11726 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11727 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11728 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11729 add_loc_descr (&op1, cvt);
11730 add_loc_descr (&op0, op1);
11731 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11732 return convert_descriptor_to_mode (mode, op0);
11735 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11736 const0 is DW_OP_lit0 or corresponding typed constant,
11737 const1 is DW_OP_lit1 or corresponding typed constant
11738 and constMSB is constant with just the MSB bit set
11739 for the mode):
11740 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11741 L1: const0 DW_OP_swap
11742 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11743 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11744 L3: DW_OP_drop
11745 L4: DW_OP_nop
11747 CTZ is similar:
11748 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11749 L1: const0 DW_OP_swap
11750 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11751 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11752 L3: DW_OP_drop
11753 L4: DW_OP_nop
11755 FFS is similar:
11756 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11757 L1: const1 DW_OP_swap
11758 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11759 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11760 L3: DW_OP_drop
11761 L4: DW_OP_nop */
11763 static dw_loc_descr_ref
11764 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11765 enum machine_mode mem_mode)
11767 dw_loc_descr_ref op0, ret, tmp;
11768 HOST_WIDE_INT valv;
11769 dw_loc_descr_ref l1jump, l1label;
11770 dw_loc_descr_ref l2jump, l2label;
11771 dw_loc_descr_ref l3jump, l3label;
11772 dw_loc_descr_ref l4jump, l4label;
11773 rtx msb;
11775 if (GET_MODE_CLASS (mode) != MODE_INT
11776 || GET_MODE (XEXP (rtl, 0)) != mode
11777 || (GET_CODE (rtl) == CLZ
11778 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11779 return NULL;
11781 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11782 VAR_INIT_STATUS_INITIALIZED);
11783 if (op0 == NULL)
11784 return NULL;
11785 ret = op0;
11786 if (GET_CODE (rtl) == CLZ)
11788 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11789 valv = GET_MODE_BITSIZE (mode);
11791 else if (GET_CODE (rtl) == FFS)
11792 valv = 0;
11793 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11794 valv = GET_MODE_BITSIZE (mode);
11795 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11796 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11797 add_loc_descr (&ret, l1jump);
11798 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11799 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11800 VAR_INIT_STATUS_INITIALIZED);
11801 if (tmp == NULL)
11802 return NULL;
11803 add_loc_descr (&ret, tmp);
11804 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11805 add_loc_descr (&ret, l4jump);
11806 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11807 ? const1_rtx : const0_rtx,
11808 mode, mem_mode,
11809 VAR_INIT_STATUS_INITIALIZED);
11810 if (l1label == NULL)
11811 return NULL;
11812 add_loc_descr (&ret, l1label);
11813 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11814 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11815 add_loc_descr (&ret, l2label);
11816 if (GET_CODE (rtl) != CLZ)
11817 msb = const1_rtx;
11818 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11819 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11820 << (GET_MODE_BITSIZE (mode) - 1));
11821 else
11822 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11823 << (GET_MODE_BITSIZE (mode)
11824 - HOST_BITS_PER_WIDE_INT - 1), mode);
11825 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11826 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11827 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11828 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11829 else
11830 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11831 VAR_INIT_STATUS_INITIALIZED);
11832 if (tmp == NULL)
11833 return NULL;
11834 add_loc_descr (&ret, tmp);
11835 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11836 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11837 add_loc_descr (&ret, l3jump);
11838 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11839 VAR_INIT_STATUS_INITIALIZED);
11840 if (tmp == NULL)
11841 return NULL;
11842 add_loc_descr (&ret, tmp);
11843 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11844 ? DW_OP_shl : DW_OP_shr, 0, 0));
11845 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11846 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11847 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11848 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11849 add_loc_descr (&ret, l2jump);
11850 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11851 add_loc_descr (&ret, l3label);
11852 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11853 add_loc_descr (&ret, l4label);
11854 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11855 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11856 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11857 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11858 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11859 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11860 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11861 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11862 return ret;
11865 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11866 const1 is DW_OP_lit1 or corresponding typed constant):
11867 const0 DW_OP_swap
11868 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11869 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11870 L2: DW_OP_drop
11872 PARITY is similar:
11873 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11874 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11875 L2: DW_OP_drop */
11877 static dw_loc_descr_ref
11878 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11879 enum machine_mode mem_mode)
11881 dw_loc_descr_ref op0, ret, tmp;
11882 dw_loc_descr_ref l1jump, l1label;
11883 dw_loc_descr_ref l2jump, l2label;
11885 if (GET_MODE_CLASS (mode) != MODE_INT
11886 || GET_MODE (XEXP (rtl, 0)) != mode)
11887 return NULL;
11889 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11890 VAR_INIT_STATUS_INITIALIZED);
11891 if (op0 == NULL)
11892 return NULL;
11893 ret = op0;
11894 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11895 VAR_INIT_STATUS_INITIALIZED);
11896 if (tmp == NULL)
11897 return NULL;
11898 add_loc_descr (&ret, tmp);
11899 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11900 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11901 add_loc_descr (&ret, l1label);
11902 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11903 add_loc_descr (&ret, l2jump);
11904 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11905 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11906 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11907 VAR_INIT_STATUS_INITIALIZED);
11908 if (tmp == NULL)
11909 return NULL;
11910 add_loc_descr (&ret, tmp);
11911 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11912 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11913 ? DW_OP_plus : DW_OP_xor, 0, 0));
11914 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11915 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11916 VAR_INIT_STATUS_INITIALIZED);
11917 add_loc_descr (&ret, tmp);
11918 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11919 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11920 add_loc_descr (&ret, l1jump);
11921 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11922 add_loc_descr (&ret, l2label);
11923 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11924 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11925 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11926 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11927 return ret;
11930 /* BSWAP (constS is initial shift count, either 56 or 24):
11931 constS const0
11932 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11933 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11934 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11935 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11936 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11938 static dw_loc_descr_ref
11939 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11940 enum machine_mode mem_mode)
11942 dw_loc_descr_ref op0, ret, tmp;
11943 dw_loc_descr_ref l1jump, l1label;
11944 dw_loc_descr_ref l2jump, l2label;
11946 if (GET_MODE_CLASS (mode) != MODE_INT
11947 || BITS_PER_UNIT != 8
11948 || (GET_MODE_BITSIZE (mode) != 32
11949 && GET_MODE_BITSIZE (mode) != 64))
11950 return NULL;
11952 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11953 VAR_INIT_STATUS_INITIALIZED);
11954 if (op0 == NULL)
11955 return NULL;
11957 ret = op0;
11958 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11959 mode, mem_mode,
11960 VAR_INIT_STATUS_INITIALIZED);
11961 if (tmp == NULL)
11962 return NULL;
11963 add_loc_descr (&ret, tmp);
11964 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11965 VAR_INIT_STATUS_INITIALIZED);
11966 if (tmp == NULL)
11967 return NULL;
11968 add_loc_descr (&ret, tmp);
11969 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11970 add_loc_descr (&ret, l1label);
11971 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11972 mode, mem_mode,
11973 VAR_INIT_STATUS_INITIALIZED);
11974 add_loc_descr (&ret, tmp);
11975 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11976 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11977 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11978 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11979 VAR_INIT_STATUS_INITIALIZED);
11980 if (tmp == NULL)
11981 return NULL;
11982 add_loc_descr (&ret, tmp);
11983 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11984 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11985 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11986 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11987 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11988 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11989 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11990 VAR_INIT_STATUS_INITIALIZED);
11991 add_loc_descr (&ret, tmp);
11992 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11993 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11994 add_loc_descr (&ret, l2jump);
11995 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11996 VAR_INIT_STATUS_INITIALIZED);
11997 add_loc_descr (&ret, tmp);
11998 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11999 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12000 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12001 add_loc_descr (&ret, l1jump);
12002 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12003 add_loc_descr (&ret, l2label);
12004 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12005 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12006 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12007 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12008 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12009 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12010 return ret;
12013 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12014 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12015 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12016 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12018 ROTATERT is similar:
12019 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12020 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12021 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12023 static dw_loc_descr_ref
12024 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12025 enum machine_mode mem_mode)
12027 rtx rtlop1 = XEXP (rtl, 1);
12028 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12029 int i;
12031 if (GET_MODE_CLASS (mode) != MODE_INT)
12032 return NULL;
12034 if (GET_MODE (rtlop1) != VOIDmode
12035 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12036 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12037 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12038 VAR_INIT_STATUS_INITIALIZED);
12039 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12040 VAR_INIT_STATUS_INITIALIZED);
12041 if (op0 == NULL || op1 == NULL)
12042 return NULL;
12043 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12044 for (i = 0; i < 2; i++)
12046 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12047 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12048 mode, mem_mode,
12049 VAR_INIT_STATUS_INITIALIZED);
12050 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12051 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12052 ? DW_OP_const4u
12053 : HOST_BITS_PER_WIDE_INT == 64
12054 ? DW_OP_const8u : DW_OP_constu,
12055 GET_MODE_MASK (mode), 0);
12056 else
12057 mask[i] = NULL;
12058 if (mask[i] == NULL)
12059 return NULL;
12060 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12062 ret = op0;
12063 add_loc_descr (&ret, op1);
12064 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12065 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12066 if (GET_CODE (rtl) == ROTATERT)
12068 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12069 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12070 GET_MODE_BITSIZE (mode), 0));
12072 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12073 if (mask[0] != NULL)
12074 add_loc_descr (&ret, mask[0]);
12075 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12076 if (mask[1] != NULL)
12078 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12079 add_loc_descr (&ret, mask[1]);
12080 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12082 if (GET_CODE (rtl) == ROTATE)
12084 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12085 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12086 GET_MODE_BITSIZE (mode), 0));
12088 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12089 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12090 return ret;
12093 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12094 for DEBUG_PARAMETER_REF RTL. */
12096 static dw_loc_descr_ref
12097 parameter_ref_descriptor (rtx rtl)
12099 dw_loc_descr_ref ret;
12100 dw_die_ref ref;
12102 if (dwarf_strict)
12103 return NULL;
12104 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12105 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12106 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12107 if (ref)
12109 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12110 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12111 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12113 else
12115 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12116 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12118 return ret;
12121 /* The following routine converts the RTL for a variable or parameter
12122 (resident in memory) into an equivalent Dwarf representation of a
12123 mechanism for getting the address of that same variable onto the top of a
12124 hypothetical "address evaluation" stack.
12126 When creating memory location descriptors, we are effectively transforming
12127 the RTL for a memory-resident object into its Dwarf postfix expression
12128 equivalent. This routine recursively descends an RTL tree, turning
12129 it into Dwarf postfix code as it goes.
12131 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12133 MEM_MODE is the mode of the memory reference, needed to handle some
12134 autoincrement addressing modes.
12136 Return 0 if we can't represent the location. */
12138 dw_loc_descr_ref
12139 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12140 enum machine_mode mem_mode,
12141 enum var_init_status initialized)
12143 dw_loc_descr_ref mem_loc_result = NULL;
12144 enum dwarf_location_atom op;
12145 dw_loc_descr_ref op0, op1;
12146 rtx inner = NULL_RTX;
12148 if (mode == VOIDmode)
12149 mode = GET_MODE (rtl);
12151 /* Note that for a dynamically sized array, the location we will generate a
12152 description of here will be the lowest numbered location which is
12153 actually within the array. That's *not* necessarily the same as the
12154 zeroth element of the array. */
12156 rtl = targetm.delegitimize_address (rtl);
12158 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12159 return NULL;
12161 switch (GET_CODE (rtl))
12163 case POST_INC:
12164 case POST_DEC:
12165 case POST_MODIFY:
12166 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12168 case SUBREG:
12169 /* The case of a subreg may arise when we have a local (register)
12170 variable or a formal (register) parameter which doesn't quite fill
12171 up an entire register. For now, just assume that it is
12172 legitimate to make the Dwarf info refer to the whole register which
12173 contains the given subreg. */
12174 if (!subreg_lowpart_p (rtl))
12175 break;
12176 inner = SUBREG_REG (rtl);
12177 case TRUNCATE:
12178 if (inner == NULL_RTX)
12179 inner = XEXP (rtl, 0);
12180 if (GET_MODE_CLASS (mode) == MODE_INT
12181 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12182 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12183 #ifdef POINTERS_EXTEND_UNSIGNED
12184 || (mode == Pmode && mem_mode != VOIDmode)
12185 #endif
12187 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12189 mem_loc_result = mem_loc_descriptor (inner,
12190 GET_MODE (inner),
12191 mem_mode, initialized);
12192 break;
12194 if (dwarf_strict)
12195 break;
12196 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12197 break;
12198 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12199 && (GET_MODE_CLASS (mode) != MODE_INT
12200 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12201 break;
12202 else
12204 dw_die_ref type_die;
12205 dw_loc_descr_ref cvt;
12207 mem_loc_result = mem_loc_descriptor (inner,
12208 GET_MODE (inner),
12209 mem_mode, initialized);
12210 if (mem_loc_result == NULL)
12211 break;
12212 type_die = base_type_for_mode (mode,
12213 GET_MODE_CLASS (mode) == MODE_INT);
12214 if (type_die == NULL)
12216 mem_loc_result = NULL;
12217 break;
12219 if (GET_MODE_SIZE (mode)
12220 != GET_MODE_SIZE (GET_MODE (inner)))
12221 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12222 else
12223 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12224 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12225 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12226 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12227 add_loc_descr (&mem_loc_result, cvt);
12229 break;
12231 case REG:
12232 if (GET_MODE_CLASS (mode) != MODE_INT
12233 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12234 && rtl != arg_pointer_rtx
12235 && rtl != frame_pointer_rtx
12236 #ifdef POINTERS_EXTEND_UNSIGNED
12237 && (mode != Pmode || mem_mode == VOIDmode)
12238 #endif
12241 dw_die_ref type_die;
12242 unsigned int dbx_regnum;
12244 if (dwarf_strict)
12245 break;
12246 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12247 break;
12248 type_die = base_type_for_mode (mode,
12249 GET_MODE_CLASS (mode) == MODE_INT);
12250 if (type_die == NULL)
12251 break;
12253 dbx_regnum = dbx_reg_number (rtl);
12254 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12255 break;
12256 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12257 dbx_regnum, 0);
12258 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12259 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12260 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12261 break;
12263 /* Whenever a register number forms a part of the description of the
12264 method for calculating the (dynamic) address of a memory resident
12265 object, DWARF rules require the register number be referred to as
12266 a "base register". This distinction is not based in any way upon
12267 what category of register the hardware believes the given register
12268 belongs to. This is strictly DWARF terminology we're dealing with
12269 here. Note that in cases where the location of a memory-resident
12270 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12271 OP_CONST (0)) the actual DWARF location descriptor that we generate
12272 may just be OP_BASEREG (basereg). This may look deceptively like
12273 the object in question was allocated to a register (rather than in
12274 memory) so DWARF consumers need to be aware of the subtle
12275 distinction between OP_REG and OP_BASEREG. */
12276 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12277 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12278 else if (stack_realign_drap
12279 && crtl->drap_reg
12280 && crtl->args.internal_arg_pointer == rtl
12281 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12283 /* If RTL is internal_arg_pointer, which has been optimized
12284 out, use DRAP instead. */
12285 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12286 VAR_INIT_STATUS_INITIALIZED);
12288 break;
12290 case SIGN_EXTEND:
12291 case ZERO_EXTEND:
12292 if (GET_MODE_CLASS (mode) != MODE_INT)
12293 break;
12294 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12295 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12296 if (op0 == 0)
12297 break;
12298 else if (GET_CODE (rtl) == ZERO_EXTEND
12299 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12300 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12301 < HOST_BITS_PER_WIDE_INT
12302 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12303 to expand zero extend as two shifts instead of
12304 masking. */
12305 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12307 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12308 mem_loc_result = op0;
12309 add_loc_descr (&mem_loc_result,
12310 int_loc_descriptor (GET_MODE_MASK (imode)));
12311 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12313 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12315 int shift = DWARF2_ADDR_SIZE
12316 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12317 shift *= BITS_PER_UNIT;
12318 if (GET_CODE (rtl) == SIGN_EXTEND)
12319 op = DW_OP_shra;
12320 else
12321 op = DW_OP_shr;
12322 mem_loc_result = op0;
12323 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12324 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12325 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12326 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12328 else if (!dwarf_strict)
12330 dw_die_ref type_die1, type_die2;
12331 dw_loc_descr_ref cvt;
12333 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12334 GET_CODE (rtl) == ZERO_EXTEND);
12335 if (type_die1 == NULL)
12336 break;
12337 type_die2 = base_type_for_mode (mode, 1);
12338 if (type_die2 == NULL)
12339 break;
12340 mem_loc_result = op0;
12341 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12342 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12343 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12344 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12345 add_loc_descr (&mem_loc_result, cvt);
12346 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12347 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12348 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12349 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12350 add_loc_descr (&mem_loc_result, cvt);
12352 break;
12354 case MEM:
12356 rtx new_rtl = avoid_constant_pool_reference (rtl);
12357 if (new_rtl != rtl)
12359 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12360 initialized);
12361 if (mem_loc_result != NULL)
12362 return mem_loc_result;
12365 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12366 get_address_mode (rtl), mode,
12367 VAR_INIT_STATUS_INITIALIZED);
12368 if (mem_loc_result == NULL)
12369 mem_loc_result = tls_mem_loc_descriptor (rtl);
12370 if (mem_loc_result != NULL)
12372 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12373 || GET_MODE_CLASS (mode) != MODE_INT)
12375 dw_die_ref type_die;
12376 dw_loc_descr_ref deref;
12378 if (dwarf_strict)
12379 return NULL;
12380 type_die
12381 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12382 if (type_die == NULL)
12383 return NULL;
12384 deref = new_loc_descr (DW_OP_GNU_deref_type,
12385 GET_MODE_SIZE (mode), 0);
12386 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12387 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12388 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12389 add_loc_descr (&mem_loc_result, deref);
12391 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12392 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12393 else
12394 add_loc_descr (&mem_loc_result,
12395 new_loc_descr (DW_OP_deref_size,
12396 GET_MODE_SIZE (mode), 0));
12398 break;
12400 case LO_SUM:
12401 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12403 case LABEL_REF:
12404 /* Some ports can transform a symbol ref into a label ref, because
12405 the symbol ref is too far away and has to be dumped into a constant
12406 pool. */
12407 case CONST:
12408 case SYMBOL_REF:
12409 if (GET_MODE_CLASS (mode) != MODE_INT
12410 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12411 #ifdef POINTERS_EXTEND_UNSIGNED
12412 && (mode != Pmode || mem_mode == VOIDmode)
12413 #endif
12415 break;
12416 if (GET_CODE (rtl) == SYMBOL_REF
12417 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12419 dw_loc_descr_ref temp;
12421 /* If this is not defined, we have no way to emit the data. */
12422 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12423 break;
12425 temp = new_addr_loc_descr (rtl, dtprel_true);
12427 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12428 add_loc_descr (&mem_loc_result, temp);
12430 break;
12433 if (!const_ok_for_output (rtl))
12434 break;
12436 symref:
12437 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12438 vec_safe_push (used_rtx_array, rtl);
12439 break;
12441 case CONCAT:
12442 case CONCATN:
12443 case VAR_LOCATION:
12444 case DEBUG_IMPLICIT_PTR:
12445 expansion_failed (NULL_TREE, rtl,
12446 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12447 return 0;
12449 case ENTRY_VALUE:
12450 if (dwarf_strict)
12451 return NULL;
12452 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12454 if (GET_MODE_CLASS (mode) != MODE_INT
12455 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12456 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12457 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12458 else
12460 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12461 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12462 return NULL;
12463 op0 = one_reg_loc_descriptor (dbx_regnum,
12464 VAR_INIT_STATUS_INITIALIZED);
12467 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12468 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12470 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12471 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12472 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12473 return NULL;
12475 else
12476 gcc_unreachable ();
12477 if (op0 == NULL)
12478 return NULL;
12479 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12480 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12481 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12482 break;
12484 case DEBUG_PARAMETER_REF:
12485 mem_loc_result = parameter_ref_descriptor (rtl);
12486 break;
12488 case PRE_MODIFY:
12489 /* Extract the PLUS expression nested inside and fall into
12490 PLUS code below. */
12491 rtl = XEXP (rtl, 1);
12492 goto plus;
12494 case PRE_INC:
12495 case PRE_DEC:
12496 /* Turn these into a PLUS expression and fall into the PLUS code
12497 below. */
12498 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12499 gen_int_mode (GET_CODE (rtl) == PRE_INC
12500 ? GET_MODE_UNIT_SIZE (mem_mode)
12501 : -GET_MODE_UNIT_SIZE (mem_mode),
12502 mode));
12504 /* ... fall through ... */
12506 case PLUS:
12507 plus:
12508 if (is_based_loc (rtl)
12509 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12510 || XEXP (rtl, 0) == arg_pointer_rtx
12511 || XEXP (rtl, 0) == frame_pointer_rtx)
12512 && GET_MODE_CLASS (mode) == MODE_INT)
12513 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12514 INTVAL (XEXP (rtl, 1)),
12515 VAR_INIT_STATUS_INITIALIZED);
12516 else
12518 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12519 VAR_INIT_STATUS_INITIALIZED);
12520 if (mem_loc_result == 0)
12521 break;
12523 if (CONST_INT_P (XEXP (rtl, 1))
12524 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12525 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12526 else
12528 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12529 VAR_INIT_STATUS_INITIALIZED);
12530 if (op1 == 0)
12531 break;
12532 add_loc_descr (&mem_loc_result, op1);
12533 add_loc_descr (&mem_loc_result,
12534 new_loc_descr (DW_OP_plus, 0, 0));
12537 break;
12539 /* If a pseudo-reg is optimized away, it is possible for it to
12540 be replaced with a MEM containing a multiply or shift. */
12541 case MINUS:
12542 op = DW_OP_minus;
12543 goto do_binop;
12545 case MULT:
12546 op = DW_OP_mul;
12547 goto do_binop;
12549 case DIV:
12550 if (!dwarf_strict
12551 && GET_MODE_CLASS (mode) == MODE_INT
12552 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12554 mem_loc_result = typed_binop (DW_OP_div, rtl,
12555 base_type_for_mode (mode, 0),
12556 mode, mem_mode);
12557 break;
12559 op = DW_OP_div;
12560 goto do_binop;
12562 case UMOD:
12563 op = DW_OP_mod;
12564 goto do_binop;
12566 case ASHIFT:
12567 op = DW_OP_shl;
12568 goto do_shift;
12570 case ASHIFTRT:
12571 op = DW_OP_shra;
12572 goto do_shift;
12574 case LSHIFTRT:
12575 op = DW_OP_shr;
12576 goto do_shift;
12578 do_shift:
12579 if (GET_MODE_CLASS (mode) != MODE_INT)
12580 break;
12581 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12582 VAR_INIT_STATUS_INITIALIZED);
12584 rtx rtlop1 = XEXP (rtl, 1);
12585 if (GET_MODE (rtlop1) != VOIDmode
12586 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12587 < GET_MODE_BITSIZE (mode))
12588 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12589 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12590 VAR_INIT_STATUS_INITIALIZED);
12593 if (op0 == 0 || op1 == 0)
12594 break;
12596 mem_loc_result = op0;
12597 add_loc_descr (&mem_loc_result, op1);
12598 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12599 break;
12601 case AND:
12602 op = DW_OP_and;
12603 goto do_binop;
12605 case IOR:
12606 op = DW_OP_or;
12607 goto do_binop;
12609 case XOR:
12610 op = DW_OP_xor;
12611 goto do_binop;
12613 do_binop:
12614 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12615 VAR_INIT_STATUS_INITIALIZED);
12616 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12617 VAR_INIT_STATUS_INITIALIZED);
12619 if (op0 == 0 || op1 == 0)
12620 break;
12622 mem_loc_result = op0;
12623 add_loc_descr (&mem_loc_result, op1);
12624 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12625 break;
12627 case MOD:
12628 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12630 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12631 base_type_for_mode (mode, 0),
12632 mode, mem_mode);
12633 break;
12636 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12637 VAR_INIT_STATUS_INITIALIZED);
12638 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12639 VAR_INIT_STATUS_INITIALIZED);
12641 if (op0 == 0 || op1 == 0)
12642 break;
12644 mem_loc_result = op0;
12645 add_loc_descr (&mem_loc_result, op1);
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_over, 0, 0));
12648 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12649 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12650 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12651 break;
12653 case UDIV:
12654 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12656 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12658 op = DW_OP_div;
12659 goto do_binop;
12661 mem_loc_result = typed_binop (DW_OP_div, rtl,
12662 base_type_for_mode (mode, 1),
12663 mode, mem_mode);
12665 break;
12667 case NOT:
12668 op = DW_OP_not;
12669 goto do_unop;
12671 case ABS:
12672 op = DW_OP_abs;
12673 goto do_unop;
12675 case NEG:
12676 op = DW_OP_neg;
12677 goto do_unop;
12679 do_unop:
12680 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12681 VAR_INIT_STATUS_INITIALIZED);
12683 if (op0 == 0)
12684 break;
12686 mem_loc_result = op0;
12687 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12688 break;
12690 case CONST_INT:
12691 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12692 #ifdef POINTERS_EXTEND_UNSIGNED
12693 || (mode == Pmode
12694 && mem_mode != VOIDmode
12695 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12696 #endif
12699 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12700 break;
12702 if (!dwarf_strict
12703 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12704 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12706 dw_die_ref type_die = base_type_for_mode (mode, 1);
12707 enum machine_mode amode;
12708 if (type_die == NULL)
12709 return NULL;
12710 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12711 MODE_INT, 0);
12712 if (INTVAL (rtl) >= 0
12713 && amode != BLKmode
12714 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12715 /* const DW_OP_GNU_convert <XXX> vs.
12716 DW_OP_GNU_const_type <XXX, 1, const>. */
12717 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12718 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12720 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12721 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12722 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12723 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12724 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12725 add_loc_descr (&mem_loc_result, op0);
12726 return mem_loc_result;
12728 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12729 INTVAL (rtl));
12730 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12731 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12732 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12733 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12734 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12735 else
12737 mem_loc_result->dw_loc_oprnd2.val_class
12738 = dw_val_class_const_double;
12739 mem_loc_result->dw_loc_oprnd2.v.val_double
12740 = double_int::from_shwi (INTVAL (rtl));
12743 break;
12745 case CONST_DOUBLE:
12746 if (!dwarf_strict)
12748 dw_die_ref type_die;
12750 /* Note that a CONST_DOUBLE rtx could represent either an integer
12751 or a floating-point constant. A CONST_DOUBLE is used whenever
12752 the constant requires more than one word in order to be
12753 adequately represented. We output CONST_DOUBLEs as blocks. */
12754 if (mode == VOIDmode
12755 || (GET_MODE (rtl) == VOIDmode
12756 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12757 break;
12758 type_die = base_type_for_mode (mode,
12759 GET_MODE_CLASS (mode) == MODE_INT);
12760 if (type_die == NULL)
12761 return NULL;
12762 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12763 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12764 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12765 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12766 if (SCALAR_FLOAT_MODE_P (mode))
12768 unsigned int length = GET_MODE_SIZE (mode);
12769 unsigned char *array
12770 = (unsigned char*) ggc_alloc_atomic (length);
12772 insert_float (rtl, array);
12773 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12774 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12775 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12776 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12778 else
12780 mem_loc_result->dw_loc_oprnd2.val_class
12781 = dw_val_class_const_double;
12782 mem_loc_result->dw_loc_oprnd2.v.val_double
12783 = rtx_to_double_int (rtl);
12786 break;
12788 case EQ:
12789 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12790 break;
12792 case GE:
12793 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12794 break;
12796 case GT:
12797 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12798 break;
12800 case LE:
12801 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12802 break;
12804 case LT:
12805 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12806 break;
12808 case NE:
12809 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12810 break;
12812 case GEU:
12813 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12814 break;
12816 case GTU:
12817 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12818 break;
12820 case LEU:
12821 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12822 break;
12824 case LTU:
12825 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12826 break;
12828 case UMIN:
12829 case UMAX:
12830 if (GET_MODE_CLASS (mode) != MODE_INT)
12831 break;
12832 /* FALLTHRU */
12833 case SMIN:
12834 case SMAX:
12835 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12836 break;
12838 case ZERO_EXTRACT:
12839 case SIGN_EXTRACT:
12840 if (CONST_INT_P (XEXP (rtl, 1))
12841 && CONST_INT_P (XEXP (rtl, 2))
12842 && ((unsigned) INTVAL (XEXP (rtl, 1))
12843 + (unsigned) INTVAL (XEXP (rtl, 2))
12844 <= GET_MODE_BITSIZE (mode))
12845 && GET_MODE_CLASS (mode) == MODE_INT
12846 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12847 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12849 int shift, size;
12850 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12851 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12852 if (op0 == 0)
12853 break;
12854 if (GET_CODE (rtl) == SIGN_EXTRACT)
12855 op = DW_OP_shra;
12856 else
12857 op = DW_OP_shr;
12858 mem_loc_result = op0;
12859 size = INTVAL (XEXP (rtl, 1));
12860 shift = INTVAL (XEXP (rtl, 2));
12861 if (BITS_BIG_ENDIAN)
12862 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12863 - shift - size;
12864 if (shift + size != (int) DWARF2_ADDR_SIZE)
12866 add_loc_descr (&mem_loc_result,
12867 int_loc_descriptor (DWARF2_ADDR_SIZE
12868 - shift - size));
12869 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12871 if (size != (int) DWARF2_ADDR_SIZE)
12873 add_loc_descr (&mem_loc_result,
12874 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12875 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12878 break;
12880 case IF_THEN_ELSE:
12882 dw_loc_descr_ref op2, bra_node, drop_node;
12883 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12884 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12885 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12886 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12887 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12888 VAR_INIT_STATUS_INITIALIZED);
12889 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12890 VAR_INIT_STATUS_INITIALIZED);
12891 if (op0 == NULL || op1 == NULL || op2 == NULL)
12892 break;
12894 mem_loc_result = op1;
12895 add_loc_descr (&mem_loc_result, op2);
12896 add_loc_descr (&mem_loc_result, op0);
12897 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12898 add_loc_descr (&mem_loc_result, bra_node);
12899 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12900 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12901 add_loc_descr (&mem_loc_result, drop_node);
12902 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12903 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12905 break;
12907 case FLOAT_EXTEND:
12908 case FLOAT_TRUNCATE:
12909 case FLOAT:
12910 case UNSIGNED_FLOAT:
12911 case FIX:
12912 case UNSIGNED_FIX:
12913 if (!dwarf_strict)
12915 dw_die_ref type_die;
12916 dw_loc_descr_ref cvt;
12918 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12919 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12920 if (op0 == NULL)
12921 break;
12922 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12923 && (GET_CODE (rtl) == FLOAT
12924 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12925 <= DWARF2_ADDR_SIZE))
12927 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12928 GET_CODE (rtl) == UNSIGNED_FLOAT);
12929 if (type_die == NULL)
12930 break;
12931 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12932 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12933 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12934 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12935 add_loc_descr (&op0, cvt);
12937 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12938 if (type_die == NULL)
12939 break;
12940 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12941 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12942 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12943 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12944 add_loc_descr (&op0, cvt);
12945 if (GET_MODE_CLASS (mode) == MODE_INT
12946 && (GET_CODE (rtl) == FIX
12947 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12949 op0 = convert_descriptor_to_mode (mode, op0);
12950 if (op0 == NULL)
12951 break;
12953 mem_loc_result = op0;
12955 break;
12957 case CLZ:
12958 case CTZ:
12959 case FFS:
12960 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12961 break;
12963 case POPCOUNT:
12964 case PARITY:
12965 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12966 break;
12968 case BSWAP:
12969 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12970 break;
12972 case ROTATE:
12973 case ROTATERT:
12974 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12975 break;
12977 case COMPARE:
12978 /* In theory, we could implement the above. */
12979 /* DWARF cannot represent the unsigned compare operations
12980 natively. */
12981 case SS_MULT:
12982 case US_MULT:
12983 case SS_DIV:
12984 case US_DIV:
12985 case SS_PLUS:
12986 case US_PLUS:
12987 case SS_MINUS:
12988 case US_MINUS:
12989 case SS_NEG:
12990 case US_NEG:
12991 case SS_ABS:
12992 case SS_ASHIFT:
12993 case US_ASHIFT:
12994 case SS_TRUNCATE:
12995 case US_TRUNCATE:
12996 case UNORDERED:
12997 case ORDERED:
12998 case UNEQ:
12999 case UNGE:
13000 case UNGT:
13001 case UNLE:
13002 case UNLT:
13003 case LTGT:
13004 case FRACT_CONVERT:
13005 case UNSIGNED_FRACT_CONVERT:
13006 case SAT_FRACT:
13007 case UNSIGNED_SAT_FRACT:
13008 case SQRT:
13009 case ASM_OPERANDS:
13010 case VEC_MERGE:
13011 case VEC_SELECT:
13012 case VEC_CONCAT:
13013 case VEC_DUPLICATE:
13014 case UNSPEC:
13015 case HIGH:
13016 case FMA:
13017 case STRICT_LOW_PART:
13018 case CONST_VECTOR:
13019 case CONST_FIXED:
13020 case CLRSB:
13021 case CLOBBER:
13022 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13023 can't express it in the debug info. This can happen e.g. with some
13024 TLS UNSPECs. */
13025 break;
13027 case CONST_STRING:
13028 resolve_one_addr (&rtl, NULL);
13029 goto symref;
13031 default:
13032 #ifdef ENABLE_CHECKING
13033 print_rtl (stderr, rtl);
13034 gcc_unreachable ();
13035 #else
13036 break;
13037 #endif
13040 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13041 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13043 return mem_loc_result;
13046 /* Return a descriptor that describes the concatenation of two locations.
13047 This is typically a complex variable. */
13049 static dw_loc_descr_ref
13050 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13052 dw_loc_descr_ref cc_loc_result = NULL;
13053 dw_loc_descr_ref x0_ref
13054 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13055 dw_loc_descr_ref x1_ref
13056 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13058 if (x0_ref == 0 || x1_ref == 0)
13059 return 0;
13061 cc_loc_result = x0_ref;
13062 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13064 add_loc_descr (&cc_loc_result, x1_ref);
13065 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13067 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13068 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13070 return cc_loc_result;
13073 /* Return a descriptor that describes the concatenation of N
13074 locations. */
13076 static dw_loc_descr_ref
13077 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13079 unsigned int i;
13080 dw_loc_descr_ref cc_loc_result = NULL;
13081 unsigned int n = XVECLEN (concatn, 0);
13083 for (i = 0; i < n; ++i)
13085 dw_loc_descr_ref ref;
13086 rtx x = XVECEXP (concatn, 0, i);
13088 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13089 if (ref == NULL)
13090 return NULL;
13092 add_loc_descr (&cc_loc_result, ref);
13093 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13096 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13097 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13099 return cc_loc_result;
13102 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13103 for DEBUG_IMPLICIT_PTR RTL. */
13105 static dw_loc_descr_ref
13106 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13108 dw_loc_descr_ref ret;
13109 dw_die_ref ref;
13111 if (dwarf_strict)
13112 return NULL;
13113 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13114 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13115 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13116 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13117 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13118 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13119 if (ref)
13121 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13122 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13123 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13125 else
13127 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13128 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13130 return ret;
13133 /* Output a proper Dwarf location descriptor for a variable or parameter
13134 which is either allocated in a register or in a memory location. For a
13135 register, we just generate an OP_REG and the register number. For a
13136 memory location we provide a Dwarf postfix expression describing how to
13137 generate the (dynamic) address of the object onto the address stack.
13139 MODE is mode of the decl if this loc_descriptor is going to be used in
13140 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13141 allowed, VOIDmode otherwise.
13143 If we don't know how to describe it, return 0. */
13145 static dw_loc_descr_ref
13146 loc_descriptor (rtx rtl, enum machine_mode mode,
13147 enum var_init_status initialized)
13149 dw_loc_descr_ref loc_result = NULL;
13151 switch (GET_CODE (rtl))
13153 case SUBREG:
13154 /* The case of a subreg may arise when we have a local (register)
13155 variable or a formal (register) parameter which doesn't quite fill
13156 up an entire register. For now, just assume that it is
13157 legitimate to make the Dwarf info refer to the whole register which
13158 contains the given subreg. */
13159 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13160 loc_result = loc_descriptor (SUBREG_REG (rtl),
13161 GET_MODE (SUBREG_REG (rtl)), initialized);
13162 else
13163 goto do_default;
13164 break;
13166 case REG:
13167 loc_result = reg_loc_descriptor (rtl, initialized);
13168 break;
13170 case MEM:
13171 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13172 GET_MODE (rtl), initialized);
13173 if (loc_result == NULL)
13174 loc_result = tls_mem_loc_descriptor (rtl);
13175 if (loc_result == NULL)
13177 rtx new_rtl = avoid_constant_pool_reference (rtl);
13178 if (new_rtl != rtl)
13179 loc_result = loc_descriptor (new_rtl, mode, initialized);
13181 break;
13183 case CONCAT:
13184 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13185 initialized);
13186 break;
13188 case CONCATN:
13189 loc_result = concatn_loc_descriptor (rtl, initialized);
13190 break;
13192 case VAR_LOCATION:
13193 /* Single part. */
13194 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13196 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13197 if (GET_CODE (loc) == EXPR_LIST)
13198 loc = XEXP (loc, 0);
13199 loc_result = loc_descriptor (loc, mode, initialized);
13200 break;
13203 rtl = XEXP (rtl, 1);
13204 /* FALLTHRU */
13206 case PARALLEL:
13208 rtvec par_elems = XVEC (rtl, 0);
13209 int num_elem = GET_NUM_ELEM (par_elems);
13210 enum machine_mode mode;
13211 int i;
13213 /* Create the first one, so we have something to add to. */
13214 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13215 VOIDmode, initialized);
13216 if (loc_result == NULL)
13217 return NULL;
13218 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13219 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13220 for (i = 1; i < num_elem; i++)
13222 dw_loc_descr_ref temp;
13224 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13225 VOIDmode, initialized);
13226 if (temp == NULL)
13227 return NULL;
13228 add_loc_descr (&loc_result, temp);
13229 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13230 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13233 break;
13235 case CONST_INT:
13236 if (mode != VOIDmode && mode != BLKmode)
13237 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13238 INTVAL (rtl));
13239 break;
13241 case CONST_DOUBLE:
13242 if (mode == VOIDmode)
13243 mode = GET_MODE (rtl);
13245 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13247 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13249 /* Note that a CONST_DOUBLE rtx could represent either an integer
13250 or a floating-point constant. A CONST_DOUBLE is used whenever
13251 the constant requires more than one word in order to be
13252 adequately represented. We output CONST_DOUBLEs as blocks. */
13253 loc_result = new_loc_descr (DW_OP_implicit_value,
13254 GET_MODE_SIZE (mode), 0);
13255 if (SCALAR_FLOAT_MODE_P (mode))
13257 unsigned int length = GET_MODE_SIZE (mode);
13258 unsigned char *array
13259 = (unsigned char*) ggc_alloc_atomic (length);
13261 insert_float (rtl, array);
13262 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13263 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13264 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13265 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13267 else
13269 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13270 loc_result->dw_loc_oprnd2.v.val_double
13271 = rtx_to_double_int (rtl);
13274 break;
13276 case CONST_VECTOR:
13277 if (mode == VOIDmode)
13278 mode = GET_MODE (rtl);
13280 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13282 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13283 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13284 unsigned char *array = (unsigned char *)
13285 ggc_alloc_atomic (length * elt_size);
13286 unsigned int i;
13287 unsigned char *p;
13289 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13290 switch (GET_MODE_CLASS (mode))
13292 case MODE_VECTOR_INT:
13293 for (i = 0, p = array; i < length; i++, p += elt_size)
13295 rtx elt = CONST_VECTOR_ELT (rtl, i);
13296 double_int val = rtx_to_double_int (elt);
13298 if (elt_size <= sizeof (HOST_WIDE_INT))
13299 insert_int (val.to_shwi (), elt_size, p);
13300 else
13302 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13303 insert_double (val, p);
13306 break;
13308 case MODE_VECTOR_FLOAT:
13309 for (i = 0, p = array; i < length; i++, p += elt_size)
13311 rtx elt = CONST_VECTOR_ELT (rtl, i);
13312 insert_float (elt, p);
13314 break;
13316 default:
13317 gcc_unreachable ();
13320 loc_result = new_loc_descr (DW_OP_implicit_value,
13321 length * elt_size, 0);
13322 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13323 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13324 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13325 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13327 break;
13329 case CONST:
13330 if (mode == VOIDmode
13331 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13332 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13333 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13335 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13336 break;
13338 /* FALLTHROUGH */
13339 case SYMBOL_REF:
13340 if (!const_ok_for_output (rtl))
13341 break;
13342 case LABEL_REF:
13343 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13344 && (dwarf_version >= 4 || !dwarf_strict))
13346 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13347 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13348 vec_safe_push (used_rtx_array, rtl);
13350 break;
13352 case DEBUG_IMPLICIT_PTR:
13353 loc_result = implicit_ptr_descriptor (rtl, 0);
13354 break;
13356 case PLUS:
13357 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13358 && CONST_INT_P (XEXP (rtl, 1)))
13360 loc_result
13361 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13362 break;
13364 /* FALLTHRU */
13365 do_default:
13366 default:
13367 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13368 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13369 && dwarf_version >= 4)
13370 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13372 /* Value expression. */
13373 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13374 if (loc_result)
13375 add_loc_descr (&loc_result,
13376 new_loc_descr (DW_OP_stack_value, 0, 0));
13378 break;
13381 return loc_result;
13384 /* We need to figure out what section we should use as the base for the
13385 address ranges where a given location is valid.
13386 1. If this particular DECL has a section associated with it, use that.
13387 2. If this function has a section associated with it, use that.
13388 3. Otherwise, use the text section.
13389 XXX: If you split a variable across multiple sections, we won't notice. */
13391 static const char *
13392 secname_for_decl (const_tree decl)
13394 const char *secname;
13396 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13398 tree sectree = DECL_SECTION_NAME (decl);
13399 secname = TREE_STRING_POINTER (sectree);
13401 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13403 tree sectree = DECL_SECTION_NAME (current_function_decl);
13404 secname = TREE_STRING_POINTER (sectree);
13406 else if (cfun && in_cold_section_p)
13407 secname = crtl->subsections.cold_section_label;
13408 else
13409 secname = text_section_label;
13411 return secname;
13414 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13416 static bool
13417 decl_by_reference_p (tree decl)
13419 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13420 || TREE_CODE (decl) == VAR_DECL)
13421 && DECL_BY_REFERENCE (decl));
13424 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13425 for VARLOC. */
13427 static dw_loc_descr_ref
13428 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13429 enum var_init_status initialized)
13431 int have_address = 0;
13432 dw_loc_descr_ref descr;
13433 enum machine_mode mode;
13435 if (want_address != 2)
13437 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13438 /* Single part. */
13439 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13441 varloc = PAT_VAR_LOCATION_LOC (varloc);
13442 if (GET_CODE (varloc) == EXPR_LIST)
13443 varloc = XEXP (varloc, 0);
13444 mode = GET_MODE (varloc);
13445 if (MEM_P (varloc))
13447 rtx addr = XEXP (varloc, 0);
13448 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13449 mode, initialized);
13450 if (descr)
13451 have_address = 1;
13452 else
13454 rtx x = avoid_constant_pool_reference (varloc);
13455 if (x != varloc)
13456 descr = mem_loc_descriptor (x, mode, VOIDmode,
13457 initialized);
13460 else
13461 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13463 else
13464 return 0;
13466 else
13468 if (GET_CODE (varloc) == VAR_LOCATION)
13469 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13470 else
13471 mode = DECL_MODE (loc);
13472 descr = loc_descriptor (varloc, mode, initialized);
13473 have_address = 1;
13476 if (!descr)
13477 return 0;
13479 if (want_address == 2 && !have_address
13480 && (dwarf_version >= 4 || !dwarf_strict))
13482 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13484 expansion_failed (loc, NULL_RTX,
13485 "DWARF address size mismatch");
13486 return 0;
13488 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13489 have_address = 1;
13491 /* Show if we can't fill the request for an address. */
13492 if (want_address && !have_address)
13494 expansion_failed (loc, NULL_RTX,
13495 "Want address and only have value");
13496 return 0;
13499 /* If we've got an address and don't want one, dereference. */
13500 if (!want_address && have_address)
13502 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13503 enum dwarf_location_atom op;
13505 if (size > DWARF2_ADDR_SIZE || size == -1)
13507 expansion_failed (loc, NULL_RTX,
13508 "DWARF address size mismatch");
13509 return 0;
13511 else if (size == DWARF2_ADDR_SIZE)
13512 op = DW_OP_deref;
13513 else
13514 op = DW_OP_deref_size;
13516 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13519 return descr;
13522 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13523 if it is not possible. */
13525 static dw_loc_descr_ref
13526 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13528 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13529 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13530 else if (dwarf_version >= 3 || !dwarf_strict)
13531 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13532 else
13533 return NULL;
13536 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13537 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13539 static dw_loc_descr_ref
13540 dw_sra_loc_expr (tree decl, rtx loc)
13542 rtx p;
13543 unsigned int padsize = 0;
13544 dw_loc_descr_ref descr, *descr_tail;
13545 unsigned HOST_WIDE_INT decl_size;
13546 rtx varloc;
13547 enum var_init_status initialized;
13549 if (DECL_SIZE (decl) == NULL
13550 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13551 return NULL;
13553 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13554 descr = NULL;
13555 descr_tail = &descr;
13557 for (p = loc; p; p = XEXP (p, 1))
13559 unsigned int bitsize = decl_piece_bitsize (p);
13560 rtx loc_note = *decl_piece_varloc_ptr (p);
13561 dw_loc_descr_ref cur_descr;
13562 dw_loc_descr_ref *tail, last = NULL;
13563 unsigned int opsize = 0;
13565 if (loc_note == NULL_RTX
13566 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13568 padsize += bitsize;
13569 continue;
13571 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13572 varloc = NOTE_VAR_LOCATION (loc_note);
13573 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13574 if (cur_descr == NULL)
13576 padsize += bitsize;
13577 continue;
13580 /* Check that cur_descr either doesn't use
13581 DW_OP_*piece operations, or their sum is equal
13582 to bitsize. Otherwise we can't embed it. */
13583 for (tail = &cur_descr; *tail != NULL;
13584 tail = &(*tail)->dw_loc_next)
13585 if ((*tail)->dw_loc_opc == DW_OP_piece)
13587 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13588 * BITS_PER_UNIT;
13589 last = *tail;
13591 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13593 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13594 last = *tail;
13597 if (last != NULL && opsize != bitsize)
13599 padsize += bitsize;
13600 /* Discard the current piece of the descriptor and release any
13601 addr_table entries it uses. */
13602 remove_loc_list_addr_table_entries (cur_descr);
13603 continue;
13606 /* If there is a hole, add DW_OP_*piece after empty DWARF
13607 expression, which means that those bits are optimized out. */
13608 if (padsize)
13610 if (padsize > decl_size)
13612 remove_loc_list_addr_table_entries (cur_descr);
13613 goto discard_descr;
13615 decl_size -= padsize;
13616 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13617 if (*descr_tail == NULL)
13619 remove_loc_list_addr_table_entries (cur_descr);
13620 goto discard_descr;
13622 descr_tail = &(*descr_tail)->dw_loc_next;
13623 padsize = 0;
13625 *descr_tail = cur_descr;
13626 descr_tail = tail;
13627 if (bitsize > decl_size)
13628 goto discard_descr;
13629 decl_size -= bitsize;
13630 if (last == NULL)
13632 HOST_WIDE_INT offset = 0;
13633 if (GET_CODE (varloc) == VAR_LOCATION
13634 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13636 varloc = PAT_VAR_LOCATION_LOC (varloc);
13637 if (GET_CODE (varloc) == EXPR_LIST)
13638 varloc = XEXP (varloc, 0);
13642 if (GET_CODE (varloc) == CONST
13643 || GET_CODE (varloc) == SIGN_EXTEND
13644 || GET_CODE (varloc) == ZERO_EXTEND)
13645 varloc = XEXP (varloc, 0);
13646 else if (GET_CODE (varloc) == SUBREG)
13647 varloc = SUBREG_REG (varloc);
13648 else
13649 break;
13651 while (1);
13652 /* DW_OP_bit_size offset should be zero for register
13653 or implicit location descriptions and empty location
13654 descriptions, but for memory addresses needs big endian
13655 adjustment. */
13656 if (MEM_P (varloc))
13658 unsigned HOST_WIDE_INT memsize
13659 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13660 if (memsize != bitsize)
13662 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13663 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13664 goto discard_descr;
13665 if (memsize < bitsize)
13666 goto discard_descr;
13667 if (BITS_BIG_ENDIAN)
13668 offset = memsize - bitsize;
13672 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13673 if (*descr_tail == NULL)
13674 goto discard_descr;
13675 descr_tail = &(*descr_tail)->dw_loc_next;
13679 /* If there were any non-empty expressions, add padding till the end of
13680 the decl. */
13681 if (descr != NULL && decl_size != 0)
13683 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13684 if (*descr_tail == NULL)
13685 goto discard_descr;
13687 return descr;
13689 discard_descr:
13690 /* Discard the descriptor and release any addr_table entries it uses. */
13691 remove_loc_list_addr_table_entries (descr);
13692 return NULL;
13695 /* Return the dwarf representation of the location list LOC_LIST of
13696 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13697 function. */
13699 static dw_loc_list_ref
13700 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13702 const char *endname, *secname;
13703 rtx varloc;
13704 enum var_init_status initialized;
13705 struct var_loc_node *node;
13706 dw_loc_descr_ref descr;
13707 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13708 dw_loc_list_ref list = NULL;
13709 dw_loc_list_ref *listp = &list;
13711 /* Now that we know what section we are using for a base,
13712 actually construct the list of locations.
13713 The first location information is what is passed to the
13714 function that creates the location list, and the remaining
13715 locations just get added on to that list.
13716 Note that we only know the start address for a location
13717 (IE location changes), so to build the range, we use
13718 the range [current location start, next location start].
13719 This means we have to special case the last node, and generate
13720 a range of [last location start, end of function label]. */
13722 secname = secname_for_decl (decl);
13724 for (node = loc_list->first; node; node = node->next)
13725 if (GET_CODE (node->loc) == EXPR_LIST
13726 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13728 if (GET_CODE (node->loc) == EXPR_LIST)
13730 /* This requires DW_OP_{,bit_}piece, which is not usable
13731 inside DWARF expressions. */
13732 if (want_address != 2)
13733 continue;
13734 descr = dw_sra_loc_expr (decl, node->loc);
13735 if (descr == NULL)
13736 continue;
13738 else
13740 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13741 varloc = NOTE_VAR_LOCATION (node->loc);
13742 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13744 if (descr)
13746 bool range_across_switch = false;
13747 /* If section switch happens in between node->label
13748 and node->next->label (or end of function) and
13749 we can't emit it as a single entry list,
13750 emit two ranges, first one ending at the end
13751 of first partition and second one starting at the
13752 beginning of second partition. */
13753 if (node == loc_list->last_before_switch
13754 && (node != loc_list->first || loc_list->first->next)
13755 && current_function_decl)
13757 endname = cfun->fde->dw_fde_end;
13758 range_across_switch = true;
13760 /* The variable has a location between NODE->LABEL and
13761 NODE->NEXT->LABEL. */
13762 else if (node->next)
13763 endname = node->next->label;
13764 /* If the variable has a location at the last label
13765 it keeps its location until the end of function. */
13766 else if (!current_function_decl)
13767 endname = text_end_label;
13768 else
13770 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13771 current_function_funcdef_no);
13772 endname = ggc_strdup (label_id);
13775 *listp = new_loc_list (descr, node->label, endname, secname);
13776 if (TREE_CODE (decl) == PARM_DECL
13777 && node == loc_list->first
13778 && NOTE_P (node->loc)
13779 && strcmp (node->label, endname) == 0)
13780 (*listp)->force = true;
13781 listp = &(*listp)->dw_loc_next;
13783 if (range_across_switch)
13785 if (GET_CODE (node->loc) == EXPR_LIST)
13786 descr = dw_sra_loc_expr (decl, node->loc);
13787 else
13789 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13790 varloc = NOTE_VAR_LOCATION (node->loc);
13791 descr = dw_loc_list_1 (decl, varloc, want_address,
13792 initialized);
13794 gcc_assert (descr);
13795 /* The variable has a location between NODE->LABEL and
13796 NODE->NEXT->LABEL. */
13797 if (node->next)
13798 endname = node->next->label;
13799 else
13800 endname = cfun->fde->dw_fde_second_end;
13801 *listp = new_loc_list (descr,
13802 cfun->fde->dw_fde_second_begin,
13803 endname, secname);
13804 listp = &(*listp)->dw_loc_next;
13809 /* Try to avoid the overhead of a location list emitting a location
13810 expression instead, but only if we didn't have more than one
13811 location entry in the first place. If some entries were not
13812 representable, we don't want to pretend a single entry that was
13813 applies to the entire scope in which the variable is
13814 available. */
13815 if (list && loc_list->first->next)
13816 gen_llsym (list);
13818 return list;
13821 /* Return if the loc_list has only single element and thus can be represented
13822 as location description. */
13824 static bool
13825 single_element_loc_list_p (dw_loc_list_ref list)
13827 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13828 return !list->ll_symbol;
13831 /* To each location in list LIST add loc descr REF. */
13833 static void
13834 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13836 dw_loc_descr_ref copy;
13837 add_loc_descr (&list->expr, ref);
13838 list = list->dw_loc_next;
13839 while (list)
13841 copy = ggc_alloc_dw_loc_descr_node ();
13842 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13843 add_loc_descr (&list->expr, copy);
13844 while (copy->dw_loc_next)
13846 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13847 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13848 copy->dw_loc_next = new_copy;
13849 copy = new_copy;
13851 list = list->dw_loc_next;
13855 /* Given two lists RET and LIST
13856 produce location list that is result of adding expression in LIST
13857 to expression in RET on each position in program.
13858 Might be destructive on both RET and LIST.
13860 TODO: We handle only simple cases of RET or LIST having at most one
13861 element. General case would inolve sorting the lists in program order
13862 and merging them that will need some additional work.
13863 Adding that will improve quality of debug info especially for SRA-ed
13864 structures. */
13866 static void
13867 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13869 if (!list)
13870 return;
13871 if (!*ret)
13873 *ret = list;
13874 return;
13876 if (!list->dw_loc_next)
13878 add_loc_descr_to_each (*ret, list->expr);
13879 return;
13881 if (!(*ret)->dw_loc_next)
13883 add_loc_descr_to_each (list, (*ret)->expr);
13884 *ret = list;
13885 return;
13887 expansion_failed (NULL_TREE, NULL_RTX,
13888 "Don't know how to merge two non-trivial"
13889 " location lists.\n");
13890 *ret = NULL;
13891 return;
13894 /* LOC is constant expression. Try a luck, look it up in constant
13895 pool and return its loc_descr of its address. */
13897 static dw_loc_descr_ref
13898 cst_pool_loc_descr (tree loc)
13900 /* Get an RTL for this, if something has been emitted. */
13901 rtx rtl = lookup_constant_def (loc);
13903 if (!rtl || !MEM_P (rtl))
13905 gcc_assert (!rtl);
13906 return 0;
13908 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13910 /* TODO: We might get more coverage if we was actually delaying expansion
13911 of all expressions till end of compilation when constant pools are fully
13912 populated. */
13913 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13915 expansion_failed (loc, NULL_RTX,
13916 "CST value in contant pool but not marked.");
13917 return 0;
13919 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13920 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13923 /* Return dw_loc_list representing address of addr_expr LOC
13924 by looking for inner INDIRECT_REF expression and turning
13925 it into simple arithmetics. */
13927 static dw_loc_list_ref
13928 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13930 tree obj, offset;
13931 HOST_WIDE_INT bitsize, bitpos, bytepos;
13932 enum machine_mode mode;
13933 int unsignedp, volatilep = 0;
13934 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13936 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13937 &bitsize, &bitpos, &offset, &mode,
13938 &unsignedp, &volatilep, false);
13939 STRIP_NOPS (obj);
13940 if (bitpos % BITS_PER_UNIT)
13942 expansion_failed (loc, NULL_RTX, "bitfield access");
13943 return 0;
13945 if (!INDIRECT_REF_P (obj))
13947 expansion_failed (obj,
13948 NULL_RTX, "no indirect ref in inner refrence");
13949 return 0;
13951 if (!offset && !bitpos)
13952 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13953 else if (toplev
13954 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13955 && (dwarf_version >= 4 || !dwarf_strict))
13957 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13958 if (!list_ret)
13959 return 0;
13960 if (offset)
13962 /* Variable offset. */
13963 list_ret1 = loc_list_from_tree (offset, 0);
13964 if (list_ret1 == 0)
13965 return 0;
13966 add_loc_list (&list_ret, list_ret1);
13967 if (!list_ret)
13968 return 0;
13969 add_loc_descr_to_each (list_ret,
13970 new_loc_descr (DW_OP_plus, 0, 0));
13972 bytepos = bitpos / BITS_PER_UNIT;
13973 if (bytepos > 0)
13974 add_loc_descr_to_each (list_ret,
13975 new_loc_descr (DW_OP_plus_uconst,
13976 bytepos, 0));
13977 else if (bytepos < 0)
13978 loc_list_plus_const (list_ret, bytepos);
13979 add_loc_descr_to_each (list_ret,
13980 new_loc_descr (DW_OP_stack_value, 0, 0));
13982 return list_ret;
13986 /* Generate Dwarf location list representing LOC.
13987 If WANT_ADDRESS is false, expression computing LOC will be computed
13988 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13989 if WANT_ADDRESS is 2, expression computing address useable in location
13990 will be returned (i.e. DW_OP_reg can be used
13991 to refer to register values). */
13993 static dw_loc_list_ref
13994 loc_list_from_tree (tree loc, int want_address)
13996 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13997 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13998 int have_address = 0;
13999 enum dwarf_location_atom op;
14001 /* ??? Most of the time we do not take proper care for sign/zero
14002 extending the values properly. Hopefully this won't be a real
14003 problem... */
14005 switch (TREE_CODE (loc))
14007 case ERROR_MARK:
14008 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14009 return 0;
14011 case PLACEHOLDER_EXPR:
14012 /* This case involves extracting fields from an object to determine the
14013 position of other fields. We don't try to encode this here. The
14014 only user of this is Ada, which encodes the needed information using
14015 the names of types. */
14016 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14017 return 0;
14019 case CALL_EXPR:
14020 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14021 /* There are no opcodes for these operations. */
14022 return 0;
14024 case PREINCREMENT_EXPR:
14025 case PREDECREMENT_EXPR:
14026 case POSTINCREMENT_EXPR:
14027 case POSTDECREMENT_EXPR:
14028 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14029 /* There are no opcodes for these operations. */
14030 return 0;
14032 case ADDR_EXPR:
14033 /* If we already want an address, see if there is INDIRECT_REF inside
14034 e.g. for &this->field. */
14035 if (want_address)
14037 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14038 (loc, want_address == 2);
14039 if (list_ret)
14040 have_address = 1;
14041 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14042 && (ret = cst_pool_loc_descr (loc)))
14043 have_address = 1;
14045 /* Otherwise, process the argument and look for the address. */
14046 if (!list_ret && !ret)
14047 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14048 else
14050 if (want_address)
14051 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14052 return NULL;
14054 break;
14056 case VAR_DECL:
14057 if (DECL_THREAD_LOCAL_P (loc))
14059 rtx rtl;
14060 enum dwarf_location_atom tls_op;
14061 enum dtprel_bool dtprel = dtprel_false;
14063 if (targetm.have_tls)
14065 /* If this is not defined, we have no way to emit the
14066 data. */
14067 if (!targetm.asm_out.output_dwarf_dtprel)
14068 return 0;
14070 /* The way DW_OP_GNU_push_tls_address is specified, we
14071 can only look up addresses of objects in the current
14072 module. We used DW_OP_addr as first op, but that's
14073 wrong, because DW_OP_addr is relocated by the debug
14074 info consumer, while DW_OP_GNU_push_tls_address
14075 operand shouldn't be. */
14076 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14077 return 0;
14078 dtprel = dtprel_true;
14079 tls_op = DW_OP_GNU_push_tls_address;
14081 else
14083 if (!targetm.emutls.debug_form_tls_address
14084 || !(dwarf_version >= 3 || !dwarf_strict))
14085 return 0;
14086 /* We stuffed the control variable into the DECL_VALUE_EXPR
14087 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14088 no longer appear in gimple code. We used the control
14089 variable in specific so that we could pick it up here. */
14090 loc = DECL_VALUE_EXPR (loc);
14091 tls_op = DW_OP_form_tls_address;
14094 rtl = rtl_for_decl_location (loc);
14095 if (rtl == NULL_RTX)
14096 return 0;
14098 if (!MEM_P (rtl))
14099 return 0;
14100 rtl = XEXP (rtl, 0);
14101 if (! CONSTANT_P (rtl))
14102 return 0;
14104 ret = new_addr_loc_descr (rtl, dtprel);
14105 ret1 = new_loc_descr (tls_op, 0, 0);
14106 add_loc_descr (&ret, ret1);
14108 have_address = 1;
14109 break;
14111 /* FALLTHRU */
14113 case PARM_DECL:
14114 case RESULT_DECL:
14115 if (DECL_HAS_VALUE_EXPR_P (loc))
14116 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14117 want_address);
14118 /* FALLTHRU */
14120 case FUNCTION_DECL:
14122 rtx rtl;
14123 var_loc_list *loc_list = lookup_decl_loc (loc);
14125 if (loc_list && loc_list->first)
14127 list_ret = dw_loc_list (loc_list, loc, want_address);
14128 have_address = want_address != 0;
14129 break;
14131 rtl = rtl_for_decl_location (loc);
14132 if (rtl == NULL_RTX)
14134 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14135 return 0;
14137 else if (CONST_INT_P (rtl))
14139 HOST_WIDE_INT val = INTVAL (rtl);
14140 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14141 val &= GET_MODE_MASK (DECL_MODE (loc));
14142 ret = int_loc_descriptor (val);
14144 else if (GET_CODE (rtl) == CONST_STRING)
14146 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14147 return 0;
14149 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14150 ret = new_addr_loc_descr (rtl, dtprel_false);
14151 else
14153 enum machine_mode mode, mem_mode;
14155 /* Certain constructs can only be represented at top-level. */
14156 if (want_address == 2)
14158 ret = loc_descriptor (rtl, VOIDmode,
14159 VAR_INIT_STATUS_INITIALIZED);
14160 have_address = 1;
14162 else
14164 mode = GET_MODE (rtl);
14165 mem_mode = VOIDmode;
14166 if (MEM_P (rtl))
14168 mem_mode = mode;
14169 mode = get_address_mode (rtl);
14170 rtl = XEXP (rtl, 0);
14171 have_address = 1;
14173 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14174 VAR_INIT_STATUS_INITIALIZED);
14176 if (!ret)
14177 expansion_failed (loc, rtl,
14178 "failed to produce loc descriptor for rtl");
14181 break;
14183 case MEM_REF:
14184 /* ??? FIXME. */
14185 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14186 return 0;
14187 /* Fallthru. */
14188 case INDIRECT_REF:
14189 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14190 have_address = 1;
14191 break;
14193 case COMPOUND_EXPR:
14194 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14196 CASE_CONVERT:
14197 case VIEW_CONVERT_EXPR:
14198 case SAVE_EXPR:
14199 case MODIFY_EXPR:
14200 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14202 case COMPONENT_REF:
14203 case BIT_FIELD_REF:
14204 case ARRAY_REF:
14205 case ARRAY_RANGE_REF:
14206 case REALPART_EXPR:
14207 case IMAGPART_EXPR:
14209 tree obj, offset;
14210 HOST_WIDE_INT bitsize, bitpos, bytepos;
14211 enum machine_mode mode;
14212 int unsignedp, volatilep = 0;
14214 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14215 &unsignedp, &volatilep, false);
14217 gcc_assert (obj != loc);
14219 list_ret = loc_list_from_tree (obj,
14220 want_address == 2
14221 && !bitpos && !offset ? 2 : 1);
14222 /* TODO: We can extract value of the small expression via shifting even
14223 for nonzero bitpos. */
14224 if (list_ret == 0)
14225 return 0;
14226 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14228 expansion_failed (loc, NULL_RTX,
14229 "bitfield access");
14230 return 0;
14233 if (offset != NULL_TREE)
14235 /* Variable offset. */
14236 list_ret1 = loc_list_from_tree (offset, 0);
14237 if (list_ret1 == 0)
14238 return 0;
14239 add_loc_list (&list_ret, list_ret1);
14240 if (!list_ret)
14241 return 0;
14242 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14245 bytepos = bitpos / BITS_PER_UNIT;
14246 if (bytepos > 0)
14247 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14248 else if (bytepos < 0)
14249 loc_list_plus_const (list_ret, bytepos);
14251 have_address = 1;
14252 break;
14255 case INTEGER_CST:
14256 if ((want_address || !tree_fits_shwi_p (loc))
14257 && (ret = cst_pool_loc_descr (loc)))
14258 have_address = 1;
14259 else if (want_address == 2
14260 && tree_fits_shwi_p (loc)
14261 && (ret = address_of_int_loc_descriptor
14262 (int_size_in_bytes (TREE_TYPE (loc)),
14263 tree_to_shwi (loc))))
14264 have_address = 1;
14265 else if (tree_fits_shwi_p (loc))
14266 ret = int_loc_descriptor (tree_to_shwi (loc));
14267 else
14269 expansion_failed (loc, NULL_RTX,
14270 "Integer operand is not host integer");
14271 return 0;
14273 break;
14275 case CONSTRUCTOR:
14276 case REAL_CST:
14277 case STRING_CST:
14278 case COMPLEX_CST:
14279 if ((ret = cst_pool_loc_descr (loc)))
14280 have_address = 1;
14281 else
14282 /* We can construct small constants here using int_loc_descriptor. */
14283 expansion_failed (loc, NULL_RTX,
14284 "constructor or constant not in constant pool");
14285 break;
14287 case TRUTH_AND_EXPR:
14288 case TRUTH_ANDIF_EXPR:
14289 case BIT_AND_EXPR:
14290 op = DW_OP_and;
14291 goto do_binop;
14293 case TRUTH_XOR_EXPR:
14294 case BIT_XOR_EXPR:
14295 op = DW_OP_xor;
14296 goto do_binop;
14298 case TRUTH_OR_EXPR:
14299 case TRUTH_ORIF_EXPR:
14300 case BIT_IOR_EXPR:
14301 op = DW_OP_or;
14302 goto do_binop;
14304 case FLOOR_DIV_EXPR:
14305 case CEIL_DIV_EXPR:
14306 case ROUND_DIV_EXPR:
14307 case TRUNC_DIV_EXPR:
14308 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14309 return 0;
14310 op = DW_OP_div;
14311 goto do_binop;
14313 case MINUS_EXPR:
14314 op = DW_OP_minus;
14315 goto do_binop;
14317 case FLOOR_MOD_EXPR:
14318 case CEIL_MOD_EXPR:
14319 case ROUND_MOD_EXPR:
14320 case TRUNC_MOD_EXPR:
14321 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14323 op = DW_OP_mod;
14324 goto do_binop;
14326 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14327 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14328 if (list_ret == 0 || list_ret1 == 0)
14329 return 0;
14331 add_loc_list (&list_ret, list_ret1);
14332 if (list_ret == 0)
14333 return 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_over, 0, 0));
14336 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14337 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14338 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14339 break;
14341 case MULT_EXPR:
14342 op = DW_OP_mul;
14343 goto do_binop;
14345 case LSHIFT_EXPR:
14346 op = DW_OP_shl;
14347 goto do_binop;
14349 case RSHIFT_EXPR:
14350 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14351 goto do_binop;
14353 case POINTER_PLUS_EXPR:
14354 case PLUS_EXPR:
14355 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14357 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14358 if (list_ret == 0)
14359 return 0;
14361 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14362 break;
14365 op = DW_OP_plus;
14366 goto do_binop;
14368 case LE_EXPR:
14369 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14370 return 0;
14372 op = DW_OP_le;
14373 goto do_binop;
14375 case GE_EXPR:
14376 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14377 return 0;
14379 op = DW_OP_ge;
14380 goto do_binop;
14382 case LT_EXPR:
14383 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14384 return 0;
14386 op = DW_OP_lt;
14387 goto do_binop;
14389 case GT_EXPR:
14390 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14391 return 0;
14393 op = DW_OP_gt;
14394 goto do_binop;
14396 case EQ_EXPR:
14397 op = DW_OP_eq;
14398 goto do_binop;
14400 case NE_EXPR:
14401 op = DW_OP_ne;
14402 goto do_binop;
14404 do_binop:
14405 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14406 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14407 if (list_ret == 0 || list_ret1 == 0)
14408 return 0;
14410 add_loc_list (&list_ret, list_ret1);
14411 if (list_ret == 0)
14412 return 0;
14413 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14414 break;
14416 case TRUTH_NOT_EXPR:
14417 case BIT_NOT_EXPR:
14418 op = DW_OP_not;
14419 goto do_unop;
14421 case ABS_EXPR:
14422 op = DW_OP_abs;
14423 goto do_unop;
14425 case NEGATE_EXPR:
14426 op = DW_OP_neg;
14427 goto do_unop;
14429 do_unop:
14430 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14431 if (list_ret == 0)
14432 return 0;
14434 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14435 break;
14437 case MIN_EXPR:
14438 case MAX_EXPR:
14440 const enum tree_code code =
14441 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14443 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14444 build2 (code, integer_type_node,
14445 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14446 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14449 /* ... fall through ... */
14451 case COND_EXPR:
14453 dw_loc_descr_ref lhs
14454 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14455 dw_loc_list_ref rhs
14456 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14457 dw_loc_descr_ref bra_node, jump_node, tmp;
14459 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14460 if (list_ret == 0 || lhs == 0 || rhs == 0)
14461 return 0;
14463 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14464 add_loc_descr_to_each (list_ret, bra_node);
14466 add_loc_list (&list_ret, rhs);
14467 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14468 add_loc_descr_to_each (list_ret, jump_node);
14470 add_loc_descr_to_each (list_ret, lhs);
14471 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14472 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14474 /* ??? Need a node to point the skip at. Use a nop. */
14475 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14476 add_loc_descr_to_each (list_ret, tmp);
14477 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14478 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14480 break;
14482 case FIX_TRUNC_EXPR:
14483 return 0;
14485 default:
14486 /* Leave front-end specific codes as simply unknown. This comes
14487 up, for instance, with the C STMT_EXPR. */
14488 if ((unsigned int) TREE_CODE (loc)
14489 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14491 expansion_failed (loc, NULL_RTX,
14492 "language specific tree node");
14493 return 0;
14496 #ifdef ENABLE_CHECKING
14497 /* Otherwise this is a generic code; we should just lists all of
14498 these explicitly. We forgot one. */
14499 gcc_unreachable ();
14500 #else
14501 /* In a release build, we want to degrade gracefully: better to
14502 generate incomplete debugging information than to crash. */
14503 return NULL;
14504 #endif
14507 if (!ret && !list_ret)
14508 return 0;
14510 if (want_address == 2 && !have_address
14511 && (dwarf_version >= 4 || !dwarf_strict))
14513 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14515 expansion_failed (loc, NULL_RTX,
14516 "DWARF address size mismatch");
14517 return 0;
14519 if (ret)
14520 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14521 else
14522 add_loc_descr_to_each (list_ret,
14523 new_loc_descr (DW_OP_stack_value, 0, 0));
14524 have_address = 1;
14526 /* Show if we can't fill the request for an address. */
14527 if (want_address && !have_address)
14529 expansion_failed (loc, NULL_RTX,
14530 "Want address and only have value");
14531 return 0;
14534 gcc_assert (!ret || !list_ret);
14536 /* If we've got an address and don't want one, dereference. */
14537 if (!want_address && have_address)
14539 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14541 if (size > DWARF2_ADDR_SIZE || size == -1)
14543 expansion_failed (loc, NULL_RTX,
14544 "DWARF address size mismatch");
14545 return 0;
14547 else if (size == DWARF2_ADDR_SIZE)
14548 op = DW_OP_deref;
14549 else
14550 op = DW_OP_deref_size;
14552 if (ret)
14553 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14554 else
14555 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14557 if (ret)
14558 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14560 return list_ret;
14563 /* Same as above but return only single location expression. */
14564 static dw_loc_descr_ref
14565 loc_descriptor_from_tree (tree loc, int want_address)
14567 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14568 if (!ret)
14569 return NULL;
14570 if (ret->dw_loc_next)
14572 expansion_failed (loc, NULL_RTX,
14573 "Location list where only loc descriptor needed");
14574 return NULL;
14576 return ret->expr;
14579 /* Given a value, round it up to the lowest multiple of `boundary'
14580 which is not less than the value itself. */
14582 static inline HOST_WIDE_INT
14583 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14585 return (((value + boundary - 1) / boundary) * boundary);
14588 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14589 pointer to the declared type for the relevant field variable, or return
14590 `integer_type_node' if the given node turns out to be an
14591 ERROR_MARK node. */
14593 static inline tree
14594 field_type (const_tree decl)
14596 tree type;
14598 if (TREE_CODE (decl) == ERROR_MARK)
14599 return integer_type_node;
14601 type = DECL_BIT_FIELD_TYPE (decl);
14602 if (type == NULL_TREE)
14603 type = TREE_TYPE (decl);
14605 return type;
14608 /* Given a pointer to a tree node, return the alignment in bits for
14609 it, or else return BITS_PER_WORD if the node actually turns out to
14610 be an ERROR_MARK node. */
14612 static inline unsigned
14613 simple_type_align_in_bits (const_tree type)
14615 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14618 static inline unsigned
14619 simple_decl_align_in_bits (const_tree decl)
14621 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14624 /* Return the result of rounding T up to ALIGN. */
14626 static inline double_int
14627 round_up_to_align (double_int t, unsigned int align)
14629 double_int alignd = double_int::from_uhwi (align);
14630 t += alignd;
14631 t += double_int_minus_one;
14632 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14633 t *= alignd;
14634 return t;
14637 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14638 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14639 or return 0 if we are unable to determine what that offset is, either
14640 because the argument turns out to be a pointer to an ERROR_MARK node, or
14641 because the offset is actually variable. (We can't handle the latter case
14642 just yet). */
14644 static HOST_WIDE_INT
14645 field_byte_offset (const_tree decl)
14647 double_int object_offset_in_bits;
14648 double_int object_offset_in_bytes;
14649 double_int bitpos_int;
14651 if (TREE_CODE (decl) == ERROR_MARK)
14652 return 0;
14654 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14656 /* We cannot yet cope with fields whose positions are variable, so
14657 for now, when we see such things, we simply return 0. Someday, we may
14658 be able to handle such cases, but it will be damn difficult. */
14659 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14660 return 0;
14662 bitpos_int = tree_to_double_int (bit_position (decl));
14664 #ifdef PCC_BITFIELD_TYPE_MATTERS
14665 if (PCC_BITFIELD_TYPE_MATTERS)
14667 tree type;
14668 tree field_size_tree;
14669 double_int deepest_bitpos;
14670 double_int field_size_in_bits;
14671 unsigned int type_align_in_bits;
14672 unsigned int decl_align_in_bits;
14673 double_int type_size_in_bits;
14675 type = field_type (decl);
14676 type_size_in_bits = double_int_type_size_in_bits (type);
14677 type_align_in_bits = simple_type_align_in_bits (type);
14679 field_size_tree = DECL_SIZE (decl);
14681 /* The size could be unspecified if there was an error, or for
14682 a flexible array member. */
14683 if (!field_size_tree)
14684 field_size_tree = bitsize_zero_node;
14686 /* If the size of the field is not constant, use the type size. */
14687 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14688 field_size_in_bits = tree_to_double_int (field_size_tree);
14689 else
14690 field_size_in_bits = type_size_in_bits;
14692 decl_align_in_bits = simple_decl_align_in_bits (decl);
14694 /* The GCC front-end doesn't make any attempt to keep track of the
14695 starting bit offset (relative to the start of the containing
14696 structure type) of the hypothetical "containing object" for a
14697 bit-field. Thus, when computing the byte offset value for the
14698 start of the "containing object" of a bit-field, we must deduce
14699 this information on our own. This can be rather tricky to do in
14700 some cases. For example, handling the following structure type
14701 definition when compiling for an i386/i486 target (which only
14702 aligns long long's to 32-bit boundaries) can be very tricky:
14704 struct S { int field1; long long field2:31; };
14706 Fortunately, there is a simple rule-of-thumb which can be used
14707 in such cases. When compiling for an i386/i486, GCC will
14708 allocate 8 bytes for the structure shown above. It decides to
14709 do this based upon one simple rule for bit-field allocation.
14710 GCC allocates each "containing object" for each bit-field at
14711 the first (i.e. lowest addressed) legitimate alignment boundary
14712 (based upon the required minimum alignment for the declared
14713 type of the field) which it can possibly use, subject to the
14714 condition that there is still enough available space remaining
14715 in the containing object (when allocated at the selected point)
14716 to fully accommodate all of the bits of the bit-field itself.
14718 This simple rule makes it obvious why GCC allocates 8 bytes for
14719 each object of the structure type shown above. When looking
14720 for a place to allocate the "containing object" for `field2',
14721 the compiler simply tries to allocate a 64-bit "containing
14722 object" at each successive 32-bit boundary (starting at zero)
14723 until it finds a place to allocate that 64- bit field such that
14724 at least 31 contiguous (and previously unallocated) bits remain
14725 within that selected 64 bit field. (As it turns out, for the
14726 example above, the compiler finds it is OK to allocate the
14727 "containing object" 64-bit field at bit-offset zero within the
14728 structure type.)
14730 Here we attempt to work backwards from the limited set of facts
14731 we're given, and we try to deduce from those facts, where GCC
14732 must have believed that the containing object started (within
14733 the structure type). The value we deduce is then used (by the
14734 callers of this routine) to generate DW_AT_location and
14735 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14736 the case of DW_AT_location, regular fields as well). */
14738 /* Figure out the bit-distance from the start of the structure to
14739 the "deepest" bit of the bit-field. */
14740 deepest_bitpos = bitpos_int + field_size_in_bits;
14742 /* This is the tricky part. Use some fancy footwork to deduce
14743 where the lowest addressed bit of the containing object must
14744 be. */
14745 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14747 /* Round up to type_align by default. This works best for
14748 bitfields. */
14749 object_offset_in_bits
14750 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14752 if (object_offset_in_bits.ugt (bitpos_int))
14754 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14756 /* Round up to decl_align instead. */
14757 object_offset_in_bits
14758 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14761 else
14762 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14763 object_offset_in_bits = bitpos_int;
14765 object_offset_in_bytes
14766 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14767 true, TRUNC_DIV_EXPR);
14768 return object_offset_in_bytes.to_shwi ();
14771 /* The following routines define various Dwarf attributes and any data
14772 associated with them. */
14774 /* Add a location description attribute value to a DIE.
14776 This emits location attributes suitable for whole variables and
14777 whole parameters. Note that the location attributes for struct fields are
14778 generated by the routine `data_member_location_attribute' below. */
14780 static inline void
14781 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14782 dw_loc_list_ref descr)
14784 if (descr == 0)
14785 return;
14786 if (single_element_loc_list_p (descr))
14787 add_AT_loc (die, attr_kind, descr->expr);
14788 else
14789 add_AT_loc_list (die, attr_kind, descr);
14792 /* Add DW_AT_accessibility attribute to DIE if needed. */
14794 static void
14795 add_accessibility_attribute (dw_die_ref die, tree decl)
14797 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14798 children, otherwise the default is DW_ACCESS_public. In DWARF2
14799 the default has always been DW_ACCESS_public. */
14800 if (TREE_PROTECTED (decl))
14801 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14802 else if (TREE_PRIVATE (decl))
14804 if (dwarf_version == 2
14805 || die->die_parent == NULL
14806 || die->die_parent->die_tag != DW_TAG_class_type)
14807 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14809 else if (dwarf_version > 2
14810 && die->die_parent
14811 && die->die_parent->die_tag == DW_TAG_class_type)
14812 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14815 /* Attach the specialized form of location attribute used for data members of
14816 struct and union types. In the special case of a FIELD_DECL node which
14817 represents a bit-field, the "offset" part of this special location
14818 descriptor must indicate the distance in bytes from the lowest-addressed
14819 byte of the containing struct or union type to the lowest-addressed byte of
14820 the "containing object" for the bit-field. (See the `field_byte_offset'
14821 function above).
14823 For any given bit-field, the "containing object" is a hypothetical object
14824 (of some integral or enum type) within which the given bit-field lives. The
14825 type of this hypothetical "containing object" is always the same as the
14826 declared type of the individual bit-field itself (for GCC anyway... the
14827 DWARF spec doesn't actually mandate this). Note that it is the size (in
14828 bytes) of the hypothetical "containing object" which will be given in the
14829 DW_AT_byte_size attribute for this bit-field. (See the
14830 `byte_size_attribute' function below.) It is also used when calculating the
14831 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14832 function below.) */
14834 static void
14835 add_data_member_location_attribute (dw_die_ref die, tree decl)
14837 HOST_WIDE_INT offset;
14838 dw_loc_descr_ref loc_descr = 0;
14840 if (TREE_CODE (decl) == TREE_BINFO)
14842 /* We're working on the TAG_inheritance for a base class. */
14843 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14845 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14846 aren't at a fixed offset from all (sub)objects of the same
14847 type. We need to extract the appropriate offset from our
14848 vtable. The following dwarf expression means
14850 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14852 This is specific to the V3 ABI, of course. */
14854 dw_loc_descr_ref tmp;
14856 /* Make a copy of the object address. */
14857 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14858 add_loc_descr (&loc_descr, tmp);
14860 /* Extract the vtable address. */
14861 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14862 add_loc_descr (&loc_descr, tmp);
14864 /* Calculate the address of the offset. */
14865 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14866 gcc_assert (offset < 0);
14868 tmp = int_loc_descriptor (-offset);
14869 add_loc_descr (&loc_descr, tmp);
14870 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14871 add_loc_descr (&loc_descr, tmp);
14873 /* Extract the offset. */
14874 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14875 add_loc_descr (&loc_descr, tmp);
14877 /* Add it to the object address. */
14878 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14879 add_loc_descr (&loc_descr, tmp);
14881 else
14882 offset = tree_to_shwi (BINFO_OFFSET (decl));
14884 else
14885 offset = field_byte_offset (decl);
14887 if (! loc_descr)
14889 if (dwarf_version > 2)
14891 /* Don't need to output a location expression, just the constant. */
14892 if (offset < 0)
14893 add_AT_int (die, DW_AT_data_member_location, offset);
14894 else
14895 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14896 return;
14898 else
14900 enum dwarf_location_atom op;
14902 /* The DWARF2 standard says that we should assume that the structure
14903 address is already on the stack, so we can specify a structure
14904 field address by using DW_OP_plus_uconst. */
14905 op = DW_OP_plus_uconst;
14906 loc_descr = new_loc_descr (op, offset, 0);
14910 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14913 /* Writes integer values to dw_vec_const array. */
14915 static void
14916 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14918 while (size != 0)
14920 *dest++ = val & 0xff;
14921 val >>= 8;
14922 --size;
14926 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14928 static HOST_WIDE_INT
14929 extract_int (const unsigned char *src, unsigned int size)
14931 HOST_WIDE_INT val = 0;
14933 src += size;
14934 while (size != 0)
14936 val <<= 8;
14937 val |= *--src & 0xff;
14938 --size;
14940 return val;
14943 /* Writes double_int values to dw_vec_const array. */
14945 static void
14946 insert_double (double_int val, unsigned char *dest)
14948 unsigned char *p0 = dest;
14949 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14951 if (WORDS_BIG_ENDIAN)
14953 p0 = p1;
14954 p1 = dest;
14957 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14958 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14961 /* Writes floating point values to dw_vec_const array. */
14963 static void
14964 insert_float (const_rtx rtl, unsigned char *array)
14966 REAL_VALUE_TYPE rv;
14967 long val[4];
14968 int i;
14970 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14971 real_to_target (val, &rv, GET_MODE (rtl));
14973 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14974 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14976 insert_int (val[i], 4, array);
14977 array += 4;
14981 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14982 does not have a "location" either in memory or in a register. These
14983 things can arise in GNU C when a constant is passed as an actual parameter
14984 to an inlined function. They can also arise in C++ where declared
14985 constants do not necessarily get memory "homes". */
14987 static bool
14988 add_const_value_attribute (dw_die_ref die, rtx rtl)
14990 switch (GET_CODE (rtl))
14992 case CONST_INT:
14994 HOST_WIDE_INT val = INTVAL (rtl);
14996 if (val < 0)
14997 add_AT_int (die, DW_AT_const_value, val);
14998 else
14999 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15001 return true;
15003 case CONST_DOUBLE:
15004 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15005 floating-point constant. A CONST_DOUBLE is used whenever the
15006 constant requires more than one word in order to be adequately
15007 represented. */
15009 enum machine_mode mode = GET_MODE (rtl);
15011 if (SCALAR_FLOAT_MODE_P (mode))
15013 unsigned int length = GET_MODE_SIZE (mode);
15014 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15016 insert_float (rtl, array);
15017 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15019 else
15020 add_AT_double (die, DW_AT_const_value,
15021 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15023 return true;
15025 case CONST_VECTOR:
15027 enum machine_mode mode = GET_MODE (rtl);
15028 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15029 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15030 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15031 (length * elt_size);
15032 unsigned int i;
15033 unsigned char *p;
15035 switch (GET_MODE_CLASS (mode))
15037 case MODE_VECTOR_INT:
15038 for (i = 0, p = array; i < length; i++, p += elt_size)
15040 rtx elt = CONST_VECTOR_ELT (rtl, i);
15041 double_int val = rtx_to_double_int (elt);
15043 if (elt_size <= sizeof (HOST_WIDE_INT))
15044 insert_int (val.to_shwi (), elt_size, p);
15045 else
15047 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15048 insert_double (val, p);
15051 break;
15053 case MODE_VECTOR_FLOAT:
15054 for (i = 0, p = array; i < length; i++, p += elt_size)
15056 rtx elt = CONST_VECTOR_ELT (rtl, i);
15057 insert_float (elt, p);
15059 break;
15061 default:
15062 gcc_unreachable ();
15065 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15067 return true;
15069 case CONST_STRING:
15070 if (dwarf_version >= 4 || !dwarf_strict)
15072 dw_loc_descr_ref loc_result;
15073 resolve_one_addr (&rtl, NULL);
15074 rtl_addr:
15075 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15076 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15077 add_AT_loc (die, DW_AT_location, loc_result);
15078 vec_safe_push (used_rtx_array, rtl);
15079 return true;
15081 return false;
15083 case CONST:
15084 if (CONSTANT_P (XEXP (rtl, 0)))
15085 return add_const_value_attribute (die, XEXP (rtl, 0));
15086 /* FALLTHROUGH */
15087 case SYMBOL_REF:
15088 if (!const_ok_for_output (rtl))
15089 return false;
15090 case LABEL_REF:
15091 if (dwarf_version >= 4 || !dwarf_strict)
15092 goto rtl_addr;
15093 return false;
15095 case PLUS:
15096 /* In cases where an inlined instance of an inline function is passed
15097 the address of an `auto' variable (which is local to the caller) we
15098 can get a situation where the DECL_RTL of the artificial local
15099 variable (for the inlining) which acts as a stand-in for the
15100 corresponding formal parameter (of the inline function) will look
15101 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15102 exactly a compile-time constant expression, but it isn't the address
15103 of the (artificial) local variable either. Rather, it represents the
15104 *value* which the artificial local variable always has during its
15105 lifetime. We currently have no way to represent such quasi-constant
15106 values in Dwarf, so for now we just punt and generate nothing. */
15107 return false;
15109 case HIGH:
15110 case CONST_FIXED:
15111 return false;
15113 case MEM:
15114 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15115 && MEM_READONLY_P (rtl)
15116 && GET_MODE (rtl) == BLKmode)
15118 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15119 return true;
15121 return false;
15123 default:
15124 /* No other kinds of rtx should be possible here. */
15125 gcc_unreachable ();
15127 return false;
15130 /* Determine whether the evaluation of EXPR references any variables
15131 or functions which aren't otherwise used (and therefore may not be
15132 output). */
15133 static tree
15134 reference_to_unused (tree * tp, int * walk_subtrees,
15135 void * data ATTRIBUTE_UNUSED)
15137 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15138 *walk_subtrees = 0;
15140 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15141 && ! TREE_ASM_WRITTEN (*tp))
15142 return *tp;
15143 /* ??? The C++ FE emits debug information for using decls, so
15144 putting gcc_unreachable here falls over. See PR31899. For now
15145 be conservative. */
15146 else if (!cgraph_global_info_ready
15147 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15148 return *tp;
15149 else if (TREE_CODE (*tp) == VAR_DECL)
15151 varpool_node *node = varpool_get_node (*tp);
15152 if (!node || !node->definition)
15153 return *tp;
15155 else if (TREE_CODE (*tp) == FUNCTION_DECL
15156 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15158 /* The call graph machinery must have finished analyzing,
15159 optimizing and gimplifying the CU by now.
15160 So if *TP has no call graph node associated
15161 to it, it means *TP will not be emitted. */
15162 if (!cgraph_get_node (*tp))
15163 return *tp;
15165 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15166 return *tp;
15168 return NULL_TREE;
15171 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15172 for use in a later add_const_value_attribute call. */
15174 static rtx
15175 rtl_for_decl_init (tree init, tree type)
15177 rtx rtl = NULL_RTX;
15179 STRIP_NOPS (init);
15181 /* If a variable is initialized with a string constant without embedded
15182 zeros, build CONST_STRING. */
15183 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15185 tree enttype = TREE_TYPE (type);
15186 tree domain = TYPE_DOMAIN (type);
15187 enum machine_mode mode = TYPE_MODE (enttype);
15189 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15190 && domain
15191 && integer_zerop (TYPE_MIN_VALUE (domain))
15192 && compare_tree_int (TYPE_MAX_VALUE (domain),
15193 TREE_STRING_LENGTH (init) - 1) == 0
15194 && ((size_t) TREE_STRING_LENGTH (init)
15195 == strlen (TREE_STRING_POINTER (init)) + 1))
15197 rtl = gen_rtx_CONST_STRING (VOIDmode,
15198 ggc_strdup (TREE_STRING_POINTER (init)));
15199 rtl = gen_rtx_MEM (BLKmode, rtl);
15200 MEM_READONLY_P (rtl) = 1;
15203 /* Other aggregates, and complex values, could be represented using
15204 CONCAT: FIXME! */
15205 else if (AGGREGATE_TYPE_P (type)
15206 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15207 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15208 || TREE_CODE (type) == COMPLEX_TYPE)
15210 /* Vectors only work if their mode is supported by the target.
15211 FIXME: generic vectors ought to work too. */
15212 else if (TREE_CODE (type) == VECTOR_TYPE
15213 && !VECTOR_MODE_P (TYPE_MODE (type)))
15215 /* If the initializer is something that we know will expand into an
15216 immediate RTL constant, expand it now. We must be careful not to
15217 reference variables which won't be output. */
15218 else if (initializer_constant_valid_p (init, type)
15219 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15221 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15222 possible. */
15223 if (TREE_CODE (type) == VECTOR_TYPE)
15224 switch (TREE_CODE (init))
15226 case VECTOR_CST:
15227 break;
15228 case CONSTRUCTOR:
15229 if (TREE_CONSTANT (init))
15231 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15232 bool constant_p = true;
15233 tree value;
15234 unsigned HOST_WIDE_INT ix;
15236 /* Even when ctor is constant, it might contain non-*_CST
15237 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15238 belong into VECTOR_CST nodes. */
15239 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15240 if (!CONSTANT_CLASS_P (value))
15242 constant_p = false;
15243 break;
15246 if (constant_p)
15248 init = build_vector_from_ctor (type, elts);
15249 break;
15252 /* FALLTHRU */
15254 default:
15255 return NULL;
15258 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15260 /* If expand_expr returns a MEM, it wasn't immediate. */
15261 gcc_assert (!rtl || !MEM_P (rtl));
15264 return rtl;
15267 /* Generate RTL for the variable DECL to represent its location. */
15269 static rtx
15270 rtl_for_decl_location (tree decl)
15272 rtx rtl;
15274 /* Here we have to decide where we are going to say the parameter "lives"
15275 (as far as the debugger is concerned). We only have a couple of
15276 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15278 DECL_RTL normally indicates where the parameter lives during most of the
15279 activation of the function. If optimization is enabled however, this
15280 could be either NULL or else a pseudo-reg. Both of those cases indicate
15281 that the parameter doesn't really live anywhere (as far as the code
15282 generation parts of GCC are concerned) during most of the function's
15283 activation. That will happen (for example) if the parameter is never
15284 referenced within the function.
15286 We could just generate a location descriptor here for all non-NULL
15287 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15288 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15289 where DECL_RTL is NULL or is a pseudo-reg.
15291 Note however that we can only get away with using DECL_INCOMING_RTL as
15292 a backup substitute for DECL_RTL in certain limited cases. In cases
15293 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15294 we can be sure that the parameter was passed using the same type as it is
15295 declared to have within the function, and that its DECL_INCOMING_RTL
15296 points us to a place where a value of that type is passed.
15298 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15299 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15300 because in these cases DECL_INCOMING_RTL points us to a value of some
15301 type which is *different* from the type of the parameter itself. Thus,
15302 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15303 such cases, the debugger would end up (for example) trying to fetch a
15304 `float' from a place which actually contains the first part of a
15305 `double'. That would lead to really incorrect and confusing
15306 output at debug-time.
15308 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15309 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15310 are a couple of exceptions however. On little-endian machines we can
15311 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15312 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15313 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15314 when (on a little-endian machine) a non-prototyped function has a
15315 parameter declared to be of type `short' or `char'. In such cases,
15316 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15317 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15318 passed `int' value. If the debugger then uses that address to fetch
15319 a `short' or a `char' (on a little-endian machine) the result will be
15320 the correct data, so we allow for such exceptional cases below.
15322 Note that our goal here is to describe the place where the given formal
15323 parameter lives during most of the function's activation (i.e. between the
15324 end of the prologue and the start of the epilogue). We'll do that as best
15325 as we can. Note however that if the given formal parameter is modified
15326 sometime during the execution of the function, then a stack backtrace (at
15327 debug-time) will show the function as having been called with the *new*
15328 value rather than the value which was originally passed in. This happens
15329 rarely enough that it is not a major problem, but it *is* a problem, and
15330 I'd like to fix it.
15332 A future version of dwarf2out.c may generate two additional attributes for
15333 any given DW_TAG_formal_parameter DIE which will describe the "passed
15334 type" and the "passed location" for the given formal parameter in addition
15335 to the attributes we now generate to indicate the "declared type" and the
15336 "active location" for each parameter. This additional set of attributes
15337 could be used by debuggers for stack backtraces. Separately, note that
15338 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15339 This happens (for example) for inlined-instances of inline function formal
15340 parameters which are never referenced. This really shouldn't be
15341 happening. All PARM_DECL nodes should get valid non-NULL
15342 DECL_INCOMING_RTL values. FIXME. */
15344 /* Use DECL_RTL as the "location" unless we find something better. */
15345 rtl = DECL_RTL_IF_SET (decl);
15347 /* When generating abstract instances, ignore everything except
15348 constants, symbols living in memory, and symbols living in
15349 fixed registers. */
15350 if (! reload_completed)
15352 if (rtl
15353 && (CONSTANT_P (rtl)
15354 || (MEM_P (rtl)
15355 && CONSTANT_P (XEXP (rtl, 0)))
15356 || (REG_P (rtl)
15357 && TREE_CODE (decl) == VAR_DECL
15358 && TREE_STATIC (decl))))
15360 rtl = targetm.delegitimize_address (rtl);
15361 return rtl;
15363 rtl = NULL_RTX;
15365 else if (TREE_CODE (decl) == PARM_DECL)
15367 if (rtl == NULL_RTX
15368 || is_pseudo_reg (rtl)
15369 || (MEM_P (rtl)
15370 && is_pseudo_reg (XEXP (rtl, 0))
15371 && DECL_INCOMING_RTL (decl)
15372 && MEM_P (DECL_INCOMING_RTL (decl))
15373 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15375 tree declared_type = TREE_TYPE (decl);
15376 tree passed_type = DECL_ARG_TYPE (decl);
15377 enum machine_mode dmode = TYPE_MODE (declared_type);
15378 enum machine_mode pmode = TYPE_MODE (passed_type);
15380 /* This decl represents a formal parameter which was optimized out.
15381 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15382 all cases where (rtl == NULL_RTX) just below. */
15383 if (dmode == pmode)
15384 rtl = DECL_INCOMING_RTL (decl);
15385 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15386 && SCALAR_INT_MODE_P (dmode)
15387 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15388 && DECL_INCOMING_RTL (decl))
15390 rtx inc = DECL_INCOMING_RTL (decl);
15391 if (REG_P (inc))
15392 rtl = inc;
15393 else if (MEM_P (inc))
15395 if (BYTES_BIG_ENDIAN)
15396 rtl = adjust_address_nv (inc, dmode,
15397 GET_MODE_SIZE (pmode)
15398 - GET_MODE_SIZE (dmode));
15399 else
15400 rtl = inc;
15405 /* If the parm was passed in registers, but lives on the stack, then
15406 make a big endian correction if the mode of the type of the
15407 parameter is not the same as the mode of the rtl. */
15408 /* ??? This is the same series of checks that are made in dbxout.c before
15409 we reach the big endian correction code there. It isn't clear if all
15410 of these checks are necessary here, but keeping them all is the safe
15411 thing to do. */
15412 else if (MEM_P (rtl)
15413 && XEXP (rtl, 0) != const0_rtx
15414 && ! CONSTANT_P (XEXP (rtl, 0))
15415 /* Not passed in memory. */
15416 && !MEM_P (DECL_INCOMING_RTL (decl))
15417 /* Not passed by invisible reference. */
15418 && (!REG_P (XEXP (rtl, 0))
15419 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15420 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15421 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15422 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15423 #endif
15425 /* Big endian correction check. */
15426 && BYTES_BIG_ENDIAN
15427 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15428 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15429 < UNITS_PER_WORD))
15431 enum machine_mode addr_mode = get_address_mode (rtl);
15432 int offset = (UNITS_PER_WORD
15433 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15435 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15436 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15439 else if (TREE_CODE (decl) == VAR_DECL
15440 && rtl
15441 && MEM_P (rtl)
15442 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15443 && BYTES_BIG_ENDIAN)
15445 enum machine_mode addr_mode = get_address_mode (rtl);
15446 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15447 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15449 /* If a variable is declared "register" yet is smaller than
15450 a register, then if we store the variable to memory, it
15451 looks like we're storing a register-sized value, when in
15452 fact we are not. We need to adjust the offset of the
15453 storage location to reflect the actual value's bytes,
15454 else gdb will not be able to display it. */
15455 if (rsize > dsize)
15456 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15457 plus_constant (addr_mode, XEXP (rtl, 0),
15458 rsize - dsize));
15461 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15462 and will have been substituted directly into all expressions that use it.
15463 C does not have such a concept, but C++ and other languages do. */
15464 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15465 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15467 if (rtl)
15468 rtl = targetm.delegitimize_address (rtl);
15470 /* If we don't look past the constant pool, we risk emitting a
15471 reference to a constant pool entry that isn't referenced from
15472 code, and thus is not emitted. */
15473 if (rtl)
15474 rtl = avoid_constant_pool_reference (rtl);
15476 /* Try harder to get a rtl. If this symbol ends up not being emitted
15477 in the current CU, resolve_addr will remove the expression referencing
15478 it. */
15479 if (rtl == NULL_RTX
15480 && TREE_CODE (decl) == VAR_DECL
15481 && !DECL_EXTERNAL (decl)
15482 && TREE_STATIC (decl)
15483 && DECL_NAME (decl)
15484 && !DECL_HARD_REGISTER (decl)
15485 && DECL_MODE (decl) != VOIDmode)
15487 rtl = make_decl_rtl_for_debug (decl);
15488 if (!MEM_P (rtl)
15489 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15490 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15491 rtl = NULL_RTX;
15494 return rtl;
15497 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15498 returned. If so, the decl for the COMMON block is returned, and the
15499 value is the offset into the common block for the symbol. */
15501 static tree
15502 fortran_common (tree decl, HOST_WIDE_INT *value)
15504 tree val_expr, cvar;
15505 enum machine_mode mode;
15506 HOST_WIDE_INT bitsize, bitpos;
15507 tree offset;
15508 int unsignedp, volatilep = 0;
15510 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15511 it does not have a value (the offset into the common area), or if it
15512 is thread local (as opposed to global) then it isn't common, and shouldn't
15513 be handled as such. */
15514 if (TREE_CODE (decl) != VAR_DECL
15515 || !TREE_STATIC (decl)
15516 || !DECL_HAS_VALUE_EXPR_P (decl)
15517 || !is_fortran ())
15518 return NULL_TREE;
15520 val_expr = DECL_VALUE_EXPR (decl);
15521 if (TREE_CODE (val_expr) != COMPONENT_REF)
15522 return NULL_TREE;
15524 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15525 &mode, &unsignedp, &volatilep, true);
15527 if (cvar == NULL_TREE
15528 || TREE_CODE (cvar) != VAR_DECL
15529 || DECL_ARTIFICIAL (cvar)
15530 || !TREE_PUBLIC (cvar))
15531 return NULL_TREE;
15533 *value = 0;
15534 if (offset != NULL)
15536 if (!tree_fits_shwi_p (offset))
15537 return NULL_TREE;
15538 *value = tree_to_shwi (offset);
15540 if (bitpos != 0)
15541 *value += bitpos / BITS_PER_UNIT;
15543 return cvar;
15546 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15547 data attribute for a variable or a parameter. We generate the
15548 DW_AT_const_value attribute only in those cases where the given variable
15549 or parameter does not have a true "location" either in memory or in a
15550 register. This can happen (for example) when a constant is passed as an
15551 actual argument in a call to an inline function. (It's possible that
15552 these things can crop up in other ways also.) Note that one type of
15553 constant value which can be passed into an inlined function is a constant
15554 pointer. This can happen for example if an actual argument in an inlined
15555 function call evaluates to a compile-time constant address.
15557 CACHE_P is true if it is worth caching the location list for DECL,
15558 so that future calls can reuse it rather than regenerate it from scratch.
15559 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15560 since we will need to refer to them each time the function is inlined. */
15562 static bool
15563 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15564 enum dwarf_attribute attr)
15566 rtx rtl;
15567 dw_loc_list_ref list;
15568 var_loc_list *loc_list;
15569 cached_dw_loc_list *cache;
15570 void **slot;
15572 if (TREE_CODE (decl) == ERROR_MARK)
15573 return false;
15575 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15576 || TREE_CODE (decl) == RESULT_DECL);
15578 /* Try to get some constant RTL for this decl, and use that as the value of
15579 the location. */
15581 rtl = rtl_for_decl_location (decl);
15582 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15583 && add_const_value_attribute (die, rtl))
15584 return true;
15586 /* See if we have single element location list that is equivalent to
15587 a constant value. That way we are better to use add_const_value_attribute
15588 rather than expanding constant value equivalent. */
15589 loc_list = lookup_decl_loc (decl);
15590 if (loc_list
15591 && loc_list->first
15592 && loc_list->first->next == NULL
15593 && NOTE_P (loc_list->first->loc)
15594 && NOTE_VAR_LOCATION (loc_list->first->loc)
15595 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15597 struct var_loc_node *node;
15599 node = loc_list->first;
15600 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15601 if (GET_CODE (rtl) == EXPR_LIST)
15602 rtl = XEXP (rtl, 0);
15603 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15604 && add_const_value_attribute (die, rtl))
15605 return true;
15607 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15608 list several times. See if we've already cached the contents. */
15609 list = NULL;
15610 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15611 cache_p = false;
15612 if (cache_p)
15614 cache = (cached_dw_loc_list *)
15615 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15616 if (cache)
15617 list = cache->loc_list;
15619 if (list == NULL)
15621 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15622 /* It is usually worth caching this result if the decl is from
15623 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15624 if (cache_p && list && list->dw_loc_next)
15626 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15627 DECL_UID (decl), INSERT);
15628 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15629 cache->decl_id = DECL_UID (decl);
15630 cache->loc_list = list;
15631 *slot = cache;
15634 if (list)
15636 add_AT_location_description (die, attr, list);
15637 return true;
15639 /* None of that worked, so it must not really have a location;
15640 try adding a constant value attribute from the DECL_INITIAL. */
15641 return tree_add_const_value_attribute_for_decl (die, decl);
15644 /* Add VARIABLE and DIE into deferred locations list. */
15646 static void
15647 defer_location (tree variable, dw_die_ref die)
15649 deferred_locations entry;
15650 entry.variable = variable;
15651 entry.die = die;
15652 vec_safe_push (deferred_locations_list, entry);
15655 /* Helper function for tree_add_const_value_attribute. Natively encode
15656 initializer INIT into an array. Return true if successful. */
15658 static bool
15659 native_encode_initializer (tree init, unsigned char *array, int size)
15661 tree type;
15663 if (init == NULL_TREE)
15664 return false;
15666 STRIP_NOPS (init);
15667 switch (TREE_CODE (init))
15669 case STRING_CST:
15670 type = TREE_TYPE (init);
15671 if (TREE_CODE (type) == ARRAY_TYPE)
15673 tree enttype = TREE_TYPE (type);
15674 enum machine_mode mode = TYPE_MODE (enttype);
15676 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15677 return false;
15678 if (int_size_in_bytes (type) != size)
15679 return false;
15680 if (size > TREE_STRING_LENGTH (init))
15682 memcpy (array, TREE_STRING_POINTER (init),
15683 TREE_STRING_LENGTH (init));
15684 memset (array + TREE_STRING_LENGTH (init),
15685 '\0', size - TREE_STRING_LENGTH (init));
15687 else
15688 memcpy (array, TREE_STRING_POINTER (init), size);
15689 return true;
15691 return false;
15692 case CONSTRUCTOR:
15693 type = TREE_TYPE (init);
15694 if (int_size_in_bytes (type) != size)
15695 return false;
15696 if (TREE_CODE (type) == ARRAY_TYPE)
15698 HOST_WIDE_INT min_index;
15699 unsigned HOST_WIDE_INT cnt;
15700 int curpos = 0, fieldsize;
15701 constructor_elt *ce;
15703 if (TYPE_DOMAIN (type) == NULL_TREE
15704 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15705 return false;
15707 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15708 if (fieldsize <= 0)
15709 return false;
15711 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15712 memset (array, '\0', size);
15713 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15715 tree val = ce->value;
15716 tree index = ce->index;
15717 int pos = curpos;
15718 if (index && TREE_CODE (index) == RANGE_EXPR)
15719 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15720 * fieldsize;
15721 else if (index)
15722 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15724 if (val)
15726 STRIP_NOPS (val);
15727 if (!native_encode_initializer (val, array + pos, fieldsize))
15728 return false;
15730 curpos = pos + fieldsize;
15731 if (index && TREE_CODE (index) == RANGE_EXPR)
15733 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15734 - tree_to_shwi (TREE_OPERAND (index, 0));
15735 while (count-- > 0)
15737 if (val)
15738 memcpy (array + curpos, array + pos, fieldsize);
15739 curpos += fieldsize;
15742 gcc_assert (curpos <= size);
15744 return true;
15746 else if (TREE_CODE (type) == RECORD_TYPE
15747 || TREE_CODE (type) == UNION_TYPE)
15749 tree field = NULL_TREE;
15750 unsigned HOST_WIDE_INT cnt;
15751 constructor_elt *ce;
15753 if (int_size_in_bytes (type) != size)
15754 return false;
15756 if (TREE_CODE (type) == RECORD_TYPE)
15757 field = TYPE_FIELDS (type);
15759 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15761 tree val = ce->value;
15762 int pos, fieldsize;
15764 if (ce->index != 0)
15765 field = ce->index;
15767 if (val)
15768 STRIP_NOPS (val);
15770 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15771 return false;
15773 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15774 && TYPE_DOMAIN (TREE_TYPE (field))
15775 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15776 return false;
15777 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15778 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15779 return false;
15780 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15781 pos = int_byte_position (field);
15782 gcc_assert (pos + fieldsize <= size);
15783 if (val
15784 && !native_encode_initializer (val, array + pos, fieldsize))
15785 return false;
15787 return true;
15789 return false;
15790 case VIEW_CONVERT_EXPR:
15791 case NON_LVALUE_EXPR:
15792 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15793 default:
15794 return native_encode_expr (init, array, size) == size;
15798 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15799 attribute is the const value T. */
15801 static bool
15802 tree_add_const_value_attribute (dw_die_ref die, tree t)
15804 tree init;
15805 tree type = TREE_TYPE (t);
15806 rtx rtl;
15808 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15809 return false;
15811 init = t;
15812 gcc_assert (!DECL_P (init));
15814 rtl = rtl_for_decl_init (init, type);
15815 if (rtl)
15816 return add_const_value_attribute (die, rtl);
15817 /* If the host and target are sane, try harder. */
15818 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15819 && initializer_constant_valid_p (init, type))
15821 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15822 if (size > 0 && (int) size == size)
15824 unsigned char *array = (unsigned char *)
15825 ggc_alloc_cleared_atomic (size);
15827 if (native_encode_initializer (init, array, size))
15829 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15830 return true;
15832 ggc_free (array);
15835 return false;
15838 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15839 attribute is the const value of T, where T is an integral constant
15840 variable with static storage duration
15841 (so it can't be a PARM_DECL or a RESULT_DECL). */
15843 static bool
15844 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15847 if (!decl
15848 || (TREE_CODE (decl) != VAR_DECL
15849 && TREE_CODE (decl) != CONST_DECL)
15850 || (TREE_CODE (decl) == VAR_DECL
15851 && !TREE_STATIC (decl)))
15852 return false;
15854 if (TREE_READONLY (decl)
15855 && ! TREE_THIS_VOLATILE (decl)
15856 && DECL_INITIAL (decl))
15857 /* OK */;
15858 else
15859 return false;
15861 /* Don't add DW_AT_const_value if abstract origin already has one. */
15862 if (get_AT (var_die, DW_AT_const_value))
15863 return false;
15865 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15868 /* Convert the CFI instructions for the current function into a
15869 location list. This is used for DW_AT_frame_base when we targeting
15870 a dwarf2 consumer that does not support the dwarf3
15871 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15872 expressions. */
15874 static dw_loc_list_ref
15875 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15877 int ix;
15878 dw_fde_ref fde;
15879 dw_loc_list_ref list, *list_tail;
15880 dw_cfi_ref cfi;
15881 dw_cfa_location last_cfa, next_cfa;
15882 const char *start_label, *last_label, *section;
15883 dw_cfa_location remember;
15885 fde = cfun->fde;
15886 gcc_assert (fde != NULL);
15888 section = secname_for_decl (current_function_decl);
15889 list_tail = &list;
15890 list = NULL;
15892 memset (&next_cfa, 0, sizeof (next_cfa));
15893 next_cfa.reg = INVALID_REGNUM;
15894 remember = next_cfa;
15896 start_label = fde->dw_fde_begin;
15898 /* ??? Bald assumption that the CIE opcode list does not contain
15899 advance opcodes. */
15900 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15901 lookup_cfa_1 (cfi, &next_cfa, &remember);
15903 last_cfa = next_cfa;
15904 last_label = start_label;
15906 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15908 /* If the first partition contained no CFI adjustments, the
15909 CIE opcodes apply to the whole first partition. */
15910 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15911 fde->dw_fde_begin, fde->dw_fde_end, section);
15912 list_tail =&(*list_tail)->dw_loc_next;
15913 start_label = last_label = fde->dw_fde_second_begin;
15916 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15918 switch (cfi->dw_cfi_opc)
15920 case DW_CFA_set_loc:
15921 case DW_CFA_advance_loc1:
15922 case DW_CFA_advance_loc2:
15923 case DW_CFA_advance_loc4:
15924 if (!cfa_equal_p (&last_cfa, &next_cfa))
15926 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15927 start_label, last_label, section);
15929 list_tail = &(*list_tail)->dw_loc_next;
15930 last_cfa = next_cfa;
15931 start_label = last_label;
15933 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15934 break;
15936 case DW_CFA_advance_loc:
15937 /* The encoding is complex enough that we should never emit this. */
15938 gcc_unreachable ();
15940 default:
15941 lookup_cfa_1 (cfi, &next_cfa, &remember);
15942 break;
15944 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15946 if (!cfa_equal_p (&last_cfa, &next_cfa))
15948 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15949 start_label, last_label, section);
15951 list_tail = &(*list_tail)->dw_loc_next;
15952 last_cfa = next_cfa;
15953 start_label = last_label;
15955 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15956 start_label, fde->dw_fde_end, section);
15957 list_tail = &(*list_tail)->dw_loc_next;
15958 start_label = last_label = fde->dw_fde_second_begin;
15962 if (!cfa_equal_p (&last_cfa, &next_cfa))
15964 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15965 start_label, last_label, section);
15966 list_tail = &(*list_tail)->dw_loc_next;
15967 start_label = last_label;
15970 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15971 start_label,
15972 fde->dw_fde_second_begin
15973 ? fde->dw_fde_second_end : fde->dw_fde_end,
15974 section);
15976 if (list && list->dw_loc_next)
15977 gen_llsym (list);
15979 return list;
15982 /* Compute a displacement from the "steady-state frame pointer" to the
15983 frame base (often the same as the CFA), and store it in
15984 frame_pointer_fb_offset. OFFSET is added to the displacement
15985 before the latter is negated. */
15987 static void
15988 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15990 rtx reg, elim;
15992 #ifdef FRAME_POINTER_CFA_OFFSET
15993 reg = frame_pointer_rtx;
15994 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15995 #else
15996 reg = arg_pointer_rtx;
15997 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15998 #endif
16000 elim = (ira_use_lra_p
16001 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16002 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16003 if (GET_CODE (elim) == PLUS)
16005 offset += INTVAL (XEXP (elim, 1));
16006 elim = XEXP (elim, 0);
16009 frame_pointer_fb_offset = -offset;
16011 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16012 in which to eliminate. This is because it's stack pointer isn't
16013 directly accessible as a register within the ISA. To work around
16014 this, assume that while we cannot provide a proper value for
16015 frame_pointer_fb_offset, we won't need one either. */
16016 frame_pointer_fb_offset_valid
16017 = ((SUPPORTS_STACK_ALIGNMENT
16018 && (elim == hard_frame_pointer_rtx
16019 || elim == stack_pointer_rtx))
16020 || elim == (frame_pointer_needed
16021 ? hard_frame_pointer_rtx
16022 : stack_pointer_rtx));
16025 /* Generate a DW_AT_name attribute given some string value to be included as
16026 the value of the attribute. */
16028 static void
16029 add_name_attribute (dw_die_ref die, const char *name_string)
16031 if (name_string != NULL && *name_string != 0)
16033 if (demangle_name_func)
16034 name_string = (*demangle_name_func) (name_string);
16036 add_AT_string (die, DW_AT_name, name_string);
16040 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16041 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16042 of TYPE accordingly.
16044 ??? This is a temporary measure until after we're able to generate
16045 regular DWARF for the complex Ada type system. */
16047 static void
16048 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16049 dw_die_ref context_die)
16051 tree dtype;
16052 dw_die_ref dtype_die;
16054 if (!lang_hooks.types.descriptive_type)
16055 return;
16057 dtype = lang_hooks.types.descriptive_type (type);
16058 if (!dtype)
16059 return;
16061 dtype_die = lookup_type_die (dtype);
16062 if (!dtype_die)
16064 gen_type_die (dtype, context_die);
16065 dtype_die = lookup_type_die (dtype);
16066 gcc_assert (dtype_die);
16069 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16072 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16074 static const char *
16075 comp_dir_string (void)
16077 const char *wd;
16078 char *wd1;
16079 static const char *cached_wd = NULL;
16081 if (cached_wd != NULL)
16082 return cached_wd;
16084 wd = get_src_pwd ();
16085 if (wd == NULL)
16086 return NULL;
16088 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16090 int wdlen;
16092 wdlen = strlen (wd);
16093 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16094 strcpy (wd1, wd);
16095 wd1 [wdlen] = DIR_SEPARATOR;
16096 wd1 [wdlen + 1] = 0;
16097 wd = wd1;
16100 cached_wd = remap_debug_filename (wd);
16101 return cached_wd;
16104 /* Generate a DW_AT_comp_dir attribute for DIE. */
16106 static void
16107 add_comp_dir_attribute (dw_die_ref die)
16109 const char * wd = comp_dir_string ();
16110 if (wd != NULL)
16111 add_AT_string (die, DW_AT_comp_dir, wd);
16114 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16115 default. */
16117 static int
16118 lower_bound_default (void)
16120 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16122 case DW_LANG_C:
16123 case DW_LANG_C89:
16124 case DW_LANG_C99:
16125 case DW_LANG_C_plus_plus:
16126 case DW_LANG_ObjC:
16127 case DW_LANG_ObjC_plus_plus:
16128 case DW_LANG_Java:
16129 return 0;
16130 case DW_LANG_Fortran77:
16131 case DW_LANG_Fortran90:
16132 case DW_LANG_Fortran95:
16133 return 1;
16134 case DW_LANG_UPC:
16135 case DW_LANG_D:
16136 case DW_LANG_Python:
16137 return dwarf_version >= 4 ? 0 : -1;
16138 case DW_LANG_Ada95:
16139 case DW_LANG_Ada83:
16140 case DW_LANG_Cobol74:
16141 case DW_LANG_Cobol85:
16142 case DW_LANG_Pascal83:
16143 case DW_LANG_Modula2:
16144 case DW_LANG_PLI:
16145 return dwarf_version >= 4 ? 1 : -1;
16146 default:
16147 return -1;
16151 /* Given a tree node describing an array bound (either lower or upper) output
16152 a representation for that bound. */
16154 static void
16155 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16157 switch (TREE_CODE (bound))
16159 case ERROR_MARK:
16160 return;
16162 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16163 case INTEGER_CST:
16165 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16166 int dflt;
16168 /* Use the default if possible. */
16169 if (bound_attr == DW_AT_lower_bound
16170 && tree_fits_shwi_p (bound)
16171 && (dflt = lower_bound_default ()) != -1
16172 && tree_to_shwi (bound) == dflt)
16175 /* Otherwise represent the bound as an unsigned value with the
16176 precision of its type. The precision and signedness of the
16177 type will be necessary to re-interpret it unambiguously. */
16178 else if (prec < HOST_BITS_PER_WIDE_INT)
16180 unsigned HOST_WIDE_INT mask
16181 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16182 add_AT_unsigned (subrange_die, bound_attr,
16183 TREE_INT_CST_LOW (bound) & mask);
16185 else if (prec == HOST_BITS_PER_WIDE_INT
16186 || TREE_INT_CST_HIGH (bound) == 0)
16187 add_AT_unsigned (subrange_die, bound_attr,
16188 TREE_INT_CST_LOW (bound));
16189 else
16190 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16191 TREE_INT_CST_LOW (bound));
16193 break;
16195 CASE_CONVERT:
16196 case VIEW_CONVERT_EXPR:
16197 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16198 break;
16200 case SAVE_EXPR:
16201 break;
16203 case VAR_DECL:
16204 case PARM_DECL:
16205 case RESULT_DECL:
16207 dw_die_ref decl_die = lookup_decl_die (bound);
16209 /* ??? Can this happen, or should the variable have been bound
16210 first? Probably it can, since I imagine that we try to create
16211 the types of parameters in the order in which they exist in
16212 the list, and won't have created a forward reference to a
16213 later parameter. */
16214 if (decl_die != NULL)
16216 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16217 break;
16220 /* FALLTHRU */
16222 default:
16224 /* Otherwise try to create a stack operation procedure to
16225 evaluate the value of the array bound. */
16227 dw_die_ref ctx, decl_die;
16228 dw_loc_list_ref list;
16230 list = loc_list_from_tree (bound, 2);
16231 if (list == NULL || single_element_loc_list_p (list))
16233 /* If DW_AT_*bound is not a reference nor constant, it is
16234 a DWARF expression rather than location description.
16235 For that loc_list_from_tree (bound, 0) is needed.
16236 If that fails to give a single element list,
16237 fall back to outputting this as a reference anyway. */
16238 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16239 if (list2 && single_element_loc_list_p (list2))
16241 add_AT_loc (subrange_die, bound_attr, list2->expr);
16242 break;
16245 if (list == NULL)
16246 break;
16248 if (current_function_decl == 0)
16249 ctx = comp_unit_die ();
16250 else
16251 ctx = lookup_decl_die (current_function_decl);
16253 decl_die = new_die (DW_TAG_variable, ctx, bound);
16254 add_AT_flag (decl_die, DW_AT_artificial, 1);
16255 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16256 add_AT_location_description (decl_die, DW_AT_location, list);
16257 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16258 break;
16263 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16264 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16265 Note that the block of subscript information for an array type also
16266 includes information about the element type of the given array type. */
16268 static void
16269 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16271 unsigned dimension_number;
16272 tree lower, upper;
16273 dw_die_ref subrange_die;
16275 for (dimension_number = 0;
16276 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16277 type = TREE_TYPE (type), dimension_number++)
16279 tree domain = TYPE_DOMAIN (type);
16281 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16282 break;
16284 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16285 and (in GNU C only) variable bounds. Handle all three forms
16286 here. */
16287 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16288 if (domain)
16290 /* We have an array type with specified bounds. */
16291 lower = TYPE_MIN_VALUE (domain);
16292 upper = TYPE_MAX_VALUE (domain);
16294 /* Define the index type. */
16295 if (TREE_TYPE (domain))
16297 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16298 TREE_TYPE field. We can't emit debug info for this
16299 because it is an unnamed integral type. */
16300 if (TREE_CODE (domain) == INTEGER_TYPE
16301 && TYPE_NAME (domain) == NULL_TREE
16302 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16303 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16305 else
16306 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16307 type_die);
16310 /* ??? If upper is NULL, the array has unspecified length,
16311 but it does have a lower bound. This happens with Fortran
16312 dimension arr(N:*)
16313 Since the debugger is definitely going to need to know N
16314 to produce useful results, go ahead and output the lower
16315 bound solo, and hope the debugger can cope. */
16317 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16318 if (upper)
16319 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16322 /* Otherwise we have an array type with an unspecified length. The
16323 DWARF-2 spec does not say how to handle this; let's just leave out the
16324 bounds. */
16328 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16330 static void
16331 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16333 dw_die_ref decl_die;
16334 HOST_WIDE_INT size;
16336 switch (TREE_CODE (tree_node))
16338 case ERROR_MARK:
16339 size = 0;
16340 break;
16341 case ENUMERAL_TYPE:
16342 case RECORD_TYPE:
16343 case UNION_TYPE:
16344 case QUAL_UNION_TYPE:
16345 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16346 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16348 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16349 return;
16351 size = int_size_in_bytes (tree_node);
16352 break;
16353 case FIELD_DECL:
16354 /* For a data member of a struct or union, the DW_AT_byte_size is
16355 generally given as the number of bytes normally allocated for an
16356 object of the *declared* type of the member itself. This is true
16357 even for bit-fields. */
16358 size = int_size_in_bytes (field_type (tree_node));
16359 break;
16360 default:
16361 gcc_unreachable ();
16364 /* Note that `size' might be -1 when we get to this point. If it is, that
16365 indicates that the byte size of the entity in question is variable. We
16366 have no good way of expressing this fact in Dwarf at the present time,
16367 when location description was not used by the caller code instead. */
16368 if (size >= 0)
16369 add_AT_unsigned (die, DW_AT_byte_size, size);
16372 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16373 which specifies the distance in bits from the highest order bit of the
16374 "containing object" for the bit-field to the highest order bit of the
16375 bit-field itself.
16377 For any given bit-field, the "containing object" is a hypothetical object
16378 (of some integral or enum type) within which the given bit-field lives. The
16379 type of this hypothetical "containing object" is always the same as the
16380 declared type of the individual bit-field itself. The determination of the
16381 exact location of the "containing object" for a bit-field is rather
16382 complicated. It's handled by the `field_byte_offset' function (above).
16384 Note that it is the size (in bytes) of the hypothetical "containing object"
16385 which will be given in the DW_AT_byte_size attribute for this bit-field.
16386 (See `byte_size_attribute' above). */
16388 static inline void
16389 add_bit_offset_attribute (dw_die_ref die, tree decl)
16391 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16392 tree type = DECL_BIT_FIELD_TYPE (decl);
16393 HOST_WIDE_INT bitpos_int;
16394 HOST_WIDE_INT highest_order_object_bit_offset;
16395 HOST_WIDE_INT highest_order_field_bit_offset;
16396 HOST_WIDE_INT bit_offset;
16398 /* Must be a field and a bit field. */
16399 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16401 /* We can't yet handle bit-fields whose offsets are variable, so if we
16402 encounter such things, just return without generating any attribute
16403 whatsoever. Likewise for variable or too large size. */
16404 if (! tree_fits_shwi_p (bit_position (decl))
16405 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16406 return;
16408 bitpos_int = int_bit_position (decl);
16410 /* Note that the bit offset is always the distance (in bits) from the
16411 highest-order bit of the "containing object" to the highest-order bit of
16412 the bit-field itself. Since the "high-order end" of any object or field
16413 is different on big-endian and little-endian machines, the computation
16414 below must take account of these differences. */
16415 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16416 highest_order_field_bit_offset = bitpos_int;
16418 if (! BYTES_BIG_ENDIAN)
16420 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16421 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16424 bit_offset
16425 = (! BYTES_BIG_ENDIAN
16426 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16427 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16429 if (bit_offset < 0)
16430 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16431 else
16432 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16435 /* For a FIELD_DECL node which represents a bit field, output an attribute
16436 which specifies the length in bits of the given field. */
16438 static inline void
16439 add_bit_size_attribute (dw_die_ref die, tree decl)
16441 /* Must be a field and a bit field. */
16442 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16443 && DECL_BIT_FIELD_TYPE (decl));
16445 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16446 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16449 /* If the compiled language is ANSI C, then add a 'prototyped'
16450 attribute, if arg types are given for the parameters of a function. */
16452 static inline void
16453 add_prototyped_attribute (dw_die_ref die, tree func_type)
16455 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16456 && prototype_p (func_type))
16457 add_AT_flag (die, DW_AT_prototyped, 1);
16460 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16461 by looking in either the type declaration or object declaration
16462 equate table. */
16464 static inline dw_die_ref
16465 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16467 dw_die_ref origin_die = NULL;
16469 if (TREE_CODE (origin) != FUNCTION_DECL)
16471 /* We may have gotten separated from the block for the inlined
16472 function, if we're in an exception handler or some such; make
16473 sure that the abstract function has been written out.
16475 Doing this for nested functions is wrong, however; functions are
16476 distinct units, and our context might not even be inline. */
16477 tree fn = origin;
16479 if (TYPE_P (fn))
16480 fn = TYPE_STUB_DECL (fn);
16482 fn = decl_function_context (fn);
16483 if (fn)
16484 dwarf2out_abstract_function (fn);
16487 if (DECL_P (origin))
16488 origin_die = lookup_decl_die (origin);
16489 else if (TYPE_P (origin))
16490 origin_die = lookup_type_die (origin);
16492 /* XXX: Functions that are never lowered don't always have correct block
16493 trees (in the case of java, they simply have no block tree, in some other
16494 languages). For these functions, there is nothing we can really do to
16495 output correct debug info for inlined functions in all cases. Rather
16496 than die, we'll just produce deficient debug info now, in that we will
16497 have variables without a proper abstract origin. In the future, when all
16498 functions are lowered, we should re-add a gcc_assert (origin_die)
16499 here. */
16501 if (origin_die)
16502 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16503 return origin_die;
16506 /* We do not currently support the pure_virtual attribute. */
16508 static inline void
16509 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16511 if (DECL_VINDEX (func_decl))
16513 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16515 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16516 add_AT_loc (die, DW_AT_vtable_elem_location,
16517 new_loc_descr (DW_OP_constu,
16518 tree_to_shwi (DECL_VINDEX (func_decl)),
16519 0));
16521 /* GNU extension: Record what type this method came from originally. */
16522 if (debug_info_level > DINFO_LEVEL_TERSE
16523 && DECL_CONTEXT (func_decl))
16524 add_AT_die_ref (die, DW_AT_containing_type,
16525 lookup_type_die (DECL_CONTEXT (func_decl)));
16529 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16530 given decl. This used to be a vendor extension until after DWARF 4
16531 standardized it. */
16533 static void
16534 add_linkage_attr (dw_die_ref die, tree decl)
16536 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16538 /* Mimic what assemble_name_raw does with a leading '*'. */
16539 if (name[0] == '*')
16540 name = &name[1];
16542 if (dwarf_version >= 4)
16543 add_AT_string (die, DW_AT_linkage_name, name);
16544 else
16545 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16548 /* Add source coordinate attributes for the given decl. */
16550 static void
16551 add_src_coords_attributes (dw_die_ref die, tree decl)
16553 expanded_location s;
16555 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16556 return;
16557 s = expand_location (DECL_SOURCE_LOCATION (decl));
16558 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16559 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16562 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16564 static void
16565 add_linkage_name (dw_die_ref die, tree decl)
16567 if (debug_info_level > DINFO_LEVEL_TERSE
16568 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16569 && TREE_PUBLIC (decl)
16570 && !DECL_ABSTRACT (decl)
16571 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16572 && die->die_tag != DW_TAG_member)
16574 /* Defer until we have an assembler name set. */
16575 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16577 limbo_die_node *asm_name;
16579 asm_name = ggc_alloc_cleared_limbo_die_node ();
16580 asm_name->die = die;
16581 asm_name->created_for = decl;
16582 asm_name->next = deferred_asm_name;
16583 deferred_asm_name = asm_name;
16585 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16586 add_linkage_attr (die, decl);
16590 /* Add a DW_AT_name attribute and source coordinate attribute for the
16591 given decl, but only if it actually has a name. */
16593 static void
16594 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16596 tree decl_name;
16598 decl_name = DECL_NAME (decl);
16599 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16601 const char *name = dwarf2_name (decl, 0);
16602 if (name)
16603 add_name_attribute (die, name);
16604 if (! DECL_ARTIFICIAL (decl))
16605 add_src_coords_attributes (die, decl);
16607 add_linkage_name (die, decl);
16610 #ifdef VMS_DEBUGGING_INFO
16611 /* Get the function's name, as described by its RTL. This may be different
16612 from the DECL_NAME name used in the source file. */
16613 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16615 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16616 XEXP (DECL_RTL (decl), 0), false);
16617 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16619 #endif /* VMS_DEBUGGING_INFO */
16622 #ifdef VMS_DEBUGGING_INFO
16623 /* Output the debug main pointer die for VMS */
16625 void
16626 dwarf2out_vms_debug_main_pointer (void)
16628 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16629 dw_die_ref die;
16631 /* Allocate the VMS debug main subprogram die. */
16632 die = ggc_alloc_cleared_die_node ();
16633 die->die_tag = DW_TAG_subprogram;
16634 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16635 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16636 current_function_funcdef_no);
16637 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16639 /* Make it the first child of comp_unit_die (). */
16640 die->die_parent = comp_unit_die ();
16641 if (comp_unit_die ()->die_child)
16643 die->die_sib = comp_unit_die ()->die_child->die_sib;
16644 comp_unit_die ()->die_child->die_sib = die;
16646 else
16648 die->die_sib = die;
16649 comp_unit_die ()->die_child = die;
16652 #endif /* VMS_DEBUGGING_INFO */
16654 /* Push a new declaration scope. */
16656 static void
16657 push_decl_scope (tree scope)
16659 vec_safe_push (decl_scope_table, scope);
16662 /* Pop a declaration scope. */
16664 static inline void
16665 pop_decl_scope (void)
16667 decl_scope_table->pop ();
16670 /* walk_tree helper function for uses_local_type, below. */
16672 static tree
16673 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16675 if (!TYPE_P (*tp))
16676 *walk_subtrees = 0;
16677 else
16679 tree name = TYPE_NAME (*tp);
16680 if (name && DECL_P (name) && decl_function_context (name))
16681 return *tp;
16683 return NULL_TREE;
16686 /* If TYPE involves a function-local type (including a local typedef to a
16687 non-local type), returns that type; otherwise returns NULL_TREE. */
16689 static tree
16690 uses_local_type (tree type)
16692 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16693 return used;
16696 /* Return the DIE for the scope that immediately contains this type.
16697 Non-named types that do not involve a function-local type get global
16698 scope. Named types nested in namespaces or other types get their
16699 containing scope. All other types (i.e. function-local named types) get
16700 the current active scope. */
16702 static dw_die_ref
16703 scope_die_for (tree t, dw_die_ref context_die)
16705 dw_die_ref scope_die = NULL;
16706 tree containing_scope;
16708 /* Non-types always go in the current scope. */
16709 gcc_assert (TYPE_P (t));
16711 /* Use the scope of the typedef, rather than the scope of the type
16712 it refers to. */
16713 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16714 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16715 else
16716 containing_scope = TYPE_CONTEXT (t);
16718 /* Use the containing namespace if there is one. */
16719 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16721 if (context_die == lookup_decl_die (containing_scope))
16722 /* OK */;
16723 else if (debug_info_level > DINFO_LEVEL_TERSE)
16724 context_die = get_context_die (containing_scope);
16725 else
16726 containing_scope = NULL_TREE;
16729 /* Ignore function type "scopes" from the C frontend. They mean that
16730 a tagged type is local to a parmlist of a function declarator, but
16731 that isn't useful to DWARF. */
16732 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16733 containing_scope = NULL_TREE;
16735 if (SCOPE_FILE_SCOPE_P (containing_scope))
16737 /* If T uses a local type keep it local as well, to avoid references
16738 to function-local DIEs from outside the function. */
16739 if (current_function_decl && uses_local_type (t))
16740 scope_die = context_die;
16741 else
16742 scope_die = comp_unit_die ();
16744 else if (TYPE_P (containing_scope))
16746 /* For types, we can just look up the appropriate DIE. */
16747 if (debug_info_level > DINFO_LEVEL_TERSE)
16748 scope_die = get_context_die (containing_scope);
16749 else
16751 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16752 if (scope_die == NULL)
16753 scope_die = comp_unit_die ();
16756 else
16757 scope_die = context_die;
16759 return scope_die;
16762 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16764 static inline int
16765 local_scope_p (dw_die_ref context_die)
16767 for (; context_die; context_die = context_die->die_parent)
16768 if (context_die->die_tag == DW_TAG_inlined_subroutine
16769 || context_die->die_tag == DW_TAG_subprogram)
16770 return 1;
16772 return 0;
16775 /* Returns nonzero if CONTEXT_DIE is a class. */
16777 static inline int
16778 class_scope_p (dw_die_ref context_die)
16780 return (context_die
16781 && (context_die->die_tag == DW_TAG_structure_type
16782 || context_die->die_tag == DW_TAG_class_type
16783 || context_die->die_tag == DW_TAG_interface_type
16784 || context_die->die_tag == DW_TAG_union_type));
16787 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16788 whether or not to treat a DIE in this context as a declaration. */
16790 static inline int
16791 class_or_namespace_scope_p (dw_die_ref context_die)
16793 return (class_scope_p (context_die)
16794 || (context_die && context_die->die_tag == DW_TAG_namespace));
16797 /* Many forms of DIEs require a "type description" attribute. This
16798 routine locates the proper "type descriptor" die for the type given
16799 by 'type', and adds a DW_AT_type attribute below the given die. */
16801 static void
16802 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16803 int decl_volatile, dw_die_ref context_die)
16805 enum tree_code code = TREE_CODE (type);
16806 dw_die_ref type_die = NULL;
16808 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16809 or fixed-point type, use the inner type. This is because we have no
16810 support for unnamed types in base_type_die. This can happen if this is
16811 an Ada subrange type. Correct solution is emit a subrange type die. */
16812 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16813 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16814 type = TREE_TYPE (type), code = TREE_CODE (type);
16816 if (code == ERROR_MARK
16817 /* Handle a special case. For functions whose return type is void, we
16818 generate *no* type attribute. (Note that no object may have type
16819 `void', so this only applies to function return types). */
16820 || code == VOID_TYPE)
16821 return;
16823 type_die = modified_type_die (type,
16824 decl_const || TYPE_READONLY (type),
16825 decl_volatile || TYPE_VOLATILE (type),
16826 context_die);
16828 if (type_die != NULL)
16829 add_AT_die_ref (object_die, DW_AT_type, type_die);
16832 /* Given an object die, add the calling convention attribute for the
16833 function call type. */
16834 static void
16835 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16837 enum dwarf_calling_convention value = DW_CC_normal;
16839 value = ((enum dwarf_calling_convention)
16840 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16842 if (is_fortran ()
16843 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16845 /* DWARF 2 doesn't provide a way to identify a program's source-level
16846 entry point. DW_AT_calling_convention attributes are only meant
16847 to describe functions' calling conventions. However, lacking a
16848 better way to signal the Fortran main program, we used this for
16849 a long time, following existing custom. Now, DWARF 4 has
16850 DW_AT_main_subprogram, which we add below, but some tools still
16851 rely on the old way, which we thus keep. */
16852 value = DW_CC_program;
16854 if (dwarf_version >= 4 || !dwarf_strict)
16855 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16858 /* Only add the attribute if the backend requests it, and
16859 is not DW_CC_normal. */
16860 if (value && (value != DW_CC_normal))
16861 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16864 /* Given a tree pointer to a struct, class, union, or enum type node, return
16865 a pointer to the (string) tag name for the given type, or zero if the type
16866 was declared without a tag. */
16868 static const char *
16869 type_tag (const_tree type)
16871 const char *name = 0;
16873 if (TYPE_NAME (type) != 0)
16875 tree t = 0;
16877 /* Find the IDENTIFIER_NODE for the type name. */
16878 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16879 && !TYPE_NAMELESS (type))
16880 t = TYPE_NAME (type);
16882 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16883 a TYPE_DECL node, regardless of whether or not a `typedef' was
16884 involved. */
16885 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16886 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16888 /* We want to be extra verbose. Don't call dwarf_name if
16889 DECL_NAME isn't set. The default hook for decl_printable_name
16890 doesn't like that, and in this context it's correct to return
16891 0, instead of "<anonymous>" or the like. */
16892 if (DECL_NAME (TYPE_NAME (type))
16893 && !DECL_NAMELESS (TYPE_NAME (type)))
16894 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16897 /* Now get the name as a string, or invent one. */
16898 if (!name && t != 0)
16899 name = IDENTIFIER_POINTER (t);
16902 return (name == 0 || *name == '\0') ? 0 : name;
16905 /* Return the type associated with a data member, make a special check
16906 for bit field types. */
16908 static inline tree
16909 member_declared_type (const_tree member)
16911 return (DECL_BIT_FIELD_TYPE (member)
16912 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16915 /* Get the decl's label, as described by its RTL. This may be different
16916 from the DECL_NAME name used in the source file. */
16918 #if 0
16919 static const char *
16920 decl_start_label (tree decl)
16922 rtx x;
16923 const char *fnname;
16925 x = DECL_RTL (decl);
16926 gcc_assert (MEM_P (x));
16928 x = XEXP (x, 0);
16929 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16931 fnname = XSTR (x, 0);
16932 return fnname;
16934 #endif
16936 /* These routines generate the internal representation of the DIE's for
16937 the compilation unit. Debugging information is collected by walking
16938 the declaration trees passed in from dwarf2out_decl(). */
16940 static void
16941 gen_array_type_die (tree type, dw_die_ref context_die)
16943 dw_die_ref scope_die = scope_die_for (type, context_die);
16944 dw_die_ref array_die;
16946 /* GNU compilers represent multidimensional array types as sequences of one
16947 dimensional array types whose element types are themselves array types.
16948 We sometimes squish that down to a single array_type DIE with multiple
16949 subscripts in the Dwarf debugging info. The draft Dwarf specification
16950 say that we are allowed to do this kind of compression in C, because
16951 there is no difference between an array of arrays and a multidimensional
16952 array. We don't do this for Ada to remain as close as possible to the
16953 actual representation, which is especially important against the language
16954 flexibilty wrt arrays of variable size. */
16956 bool collapse_nested_arrays = !is_ada ();
16957 tree element_type;
16959 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16960 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16961 if (TYPE_STRING_FLAG (type)
16962 && TREE_CODE (type) == ARRAY_TYPE
16963 && is_fortran ()
16964 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16966 HOST_WIDE_INT size;
16968 array_die = new_die (DW_TAG_string_type, scope_die, type);
16969 add_name_attribute (array_die, type_tag (type));
16970 equate_type_number_to_die (type, array_die);
16971 size = int_size_in_bytes (type);
16972 if (size >= 0)
16973 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16974 else if (TYPE_DOMAIN (type) != NULL_TREE
16975 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16976 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16978 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16979 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16981 size = int_size_in_bytes (TREE_TYPE (szdecl));
16982 if (loc && size > 0)
16984 add_AT_location_description (array_die, DW_AT_string_length, loc);
16985 if (size != DWARF2_ADDR_SIZE)
16986 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16989 return;
16992 array_die = new_die (DW_TAG_array_type, scope_die, type);
16993 add_name_attribute (array_die, type_tag (type));
16994 equate_type_number_to_die (type, array_die);
16996 if (TREE_CODE (type) == VECTOR_TYPE)
16997 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16999 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17000 if (is_fortran ()
17001 && TREE_CODE (type) == ARRAY_TYPE
17002 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17003 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17004 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17006 #if 0
17007 /* We default the array ordering. SDB will probably do
17008 the right things even if DW_AT_ordering is not present. It's not even
17009 an issue until we start to get into multidimensional arrays anyway. If
17010 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17011 then we'll have to put the DW_AT_ordering attribute back in. (But if
17012 and when we find out that we need to put these in, we will only do so
17013 for multidimensional arrays. */
17014 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17015 #endif
17017 if (TREE_CODE (type) == VECTOR_TYPE)
17019 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17020 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17021 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17022 add_bound_info (subrange_die, DW_AT_upper_bound,
17023 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17025 else
17026 add_subscript_info (array_die, type, collapse_nested_arrays);
17028 /* Add representation of the type of the elements of this array type and
17029 emit the corresponding DIE if we haven't done it already. */
17030 element_type = TREE_TYPE (type);
17031 if (collapse_nested_arrays)
17032 while (TREE_CODE (element_type) == ARRAY_TYPE)
17034 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17035 break;
17036 element_type = TREE_TYPE (element_type);
17039 add_type_attribute (array_die, element_type, 0, 0, context_die);
17041 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17042 if (TYPE_ARTIFICIAL (type))
17043 add_AT_flag (array_die, DW_AT_artificial, 1);
17045 if (get_AT (array_die, DW_AT_name))
17046 add_pubtype (type, array_die);
17049 static dw_loc_descr_ref
17050 descr_info_loc (tree val, tree base_decl)
17052 HOST_WIDE_INT size;
17053 dw_loc_descr_ref loc, loc2;
17054 enum dwarf_location_atom op;
17056 if (val == base_decl)
17057 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17059 switch (TREE_CODE (val))
17061 CASE_CONVERT:
17062 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17063 case VAR_DECL:
17064 return loc_descriptor_from_tree (val, 0);
17065 case INTEGER_CST:
17066 if (tree_fits_shwi_p (val))
17067 return int_loc_descriptor (tree_to_shwi (val));
17068 break;
17069 case INDIRECT_REF:
17070 size = int_size_in_bytes (TREE_TYPE (val));
17071 if (size < 0)
17072 break;
17073 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17074 if (!loc)
17075 break;
17076 if (size == DWARF2_ADDR_SIZE)
17077 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17078 else
17079 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17080 return loc;
17081 case POINTER_PLUS_EXPR:
17082 case PLUS_EXPR:
17083 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17084 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17086 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17087 if (!loc)
17088 break;
17089 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17091 else
17093 op = DW_OP_plus;
17094 do_binop:
17095 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17096 if (!loc)
17097 break;
17098 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17099 if (!loc2)
17100 break;
17101 add_loc_descr (&loc, loc2);
17102 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17104 return loc;
17105 case MINUS_EXPR:
17106 op = DW_OP_minus;
17107 goto do_binop;
17108 case MULT_EXPR:
17109 op = DW_OP_mul;
17110 goto do_binop;
17111 case EQ_EXPR:
17112 op = DW_OP_eq;
17113 goto do_binop;
17114 case NE_EXPR:
17115 op = DW_OP_ne;
17116 goto do_binop;
17117 default:
17118 break;
17120 return NULL;
17123 static void
17124 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17125 tree val, tree base_decl)
17127 dw_loc_descr_ref loc;
17129 if (tree_fits_shwi_p (val))
17131 add_AT_unsigned (die, attr, tree_to_shwi (val));
17132 return;
17135 loc = descr_info_loc (val, base_decl);
17136 if (!loc)
17137 return;
17139 add_AT_loc (die, attr, loc);
17142 /* This routine generates DIE for array with hidden descriptor, details
17143 are filled into *info by a langhook. */
17145 static void
17146 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17147 dw_die_ref context_die)
17149 dw_die_ref scope_die = scope_die_for (type, context_die);
17150 dw_die_ref array_die;
17151 int dim;
17153 array_die = new_die (DW_TAG_array_type, scope_die, type);
17154 add_name_attribute (array_die, type_tag (type));
17155 equate_type_number_to_die (type, array_die);
17157 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17158 if (is_fortran ()
17159 && info->ndimensions >= 2)
17160 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17162 if (info->data_location)
17163 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17164 info->base_decl);
17165 if (info->associated)
17166 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17167 info->base_decl);
17168 if (info->allocated)
17169 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17170 info->base_decl);
17172 for (dim = 0; dim < info->ndimensions; dim++)
17174 dw_die_ref subrange_die
17175 = new_die (DW_TAG_subrange_type, array_die, NULL);
17177 if (info->dimen[dim].lower_bound)
17179 /* If it is the default value, omit it. */
17180 int dflt;
17182 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17183 && (dflt = lower_bound_default ()) != -1
17184 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17186 else
17187 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17188 info->dimen[dim].lower_bound,
17189 info->base_decl);
17191 if (info->dimen[dim].upper_bound)
17192 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17193 info->dimen[dim].upper_bound,
17194 info->base_decl);
17195 if (info->dimen[dim].stride)
17196 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17197 info->dimen[dim].stride,
17198 info->base_decl);
17201 gen_type_die (info->element_type, context_die);
17202 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17204 if (get_AT (array_die, DW_AT_name))
17205 add_pubtype (type, array_die);
17208 #if 0
17209 static void
17210 gen_entry_point_die (tree decl, dw_die_ref context_die)
17212 tree origin = decl_ultimate_origin (decl);
17213 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17215 if (origin != NULL)
17216 add_abstract_origin_attribute (decl_die, origin);
17217 else
17219 add_name_and_src_coords_attributes (decl_die, decl);
17220 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17221 0, 0, context_die);
17224 if (DECL_ABSTRACT (decl))
17225 equate_decl_number_to_die (decl, decl_die);
17226 else
17227 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17229 #endif
17231 /* Walk through the list of incomplete types again, trying once more to
17232 emit full debugging info for them. */
17234 static void
17235 retry_incomplete_types (void)
17237 int i;
17239 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17240 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17241 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17244 /* Determine what tag to use for a record type. */
17246 static enum dwarf_tag
17247 record_type_tag (tree type)
17249 if (! lang_hooks.types.classify_record)
17250 return DW_TAG_structure_type;
17252 switch (lang_hooks.types.classify_record (type))
17254 case RECORD_IS_STRUCT:
17255 return DW_TAG_structure_type;
17257 case RECORD_IS_CLASS:
17258 return DW_TAG_class_type;
17260 case RECORD_IS_INTERFACE:
17261 if (dwarf_version >= 3 || !dwarf_strict)
17262 return DW_TAG_interface_type;
17263 return DW_TAG_structure_type;
17265 default:
17266 gcc_unreachable ();
17270 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17271 include all of the information about the enumeration values also. Each
17272 enumerated type name/value is listed as a child of the enumerated type
17273 DIE. */
17275 static dw_die_ref
17276 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17278 dw_die_ref type_die = lookup_type_die (type);
17280 if (type_die == NULL)
17282 type_die = new_die (DW_TAG_enumeration_type,
17283 scope_die_for (type, context_die), type);
17284 equate_type_number_to_die (type, type_die);
17285 add_name_attribute (type_die, type_tag (type));
17286 if (dwarf_version >= 4 || !dwarf_strict)
17288 if (ENUM_IS_SCOPED (type))
17289 add_AT_flag (type_die, DW_AT_enum_class, 1);
17290 if (ENUM_IS_OPAQUE (type))
17291 add_AT_flag (type_die, DW_AT_declaration, 1);
17294 else if (! TYPE_SIZE (type))
17295 return type_die;
17296 else
17297 remove_AT (type_die, DW_AT_declaration);
17299 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17300 given enum type is incomplete, do not generate the DW_AT_byte_size
17301 attribute or the DW_AT_element_list attribute. */
17302 if (TYPE_SIZE (type))
17304 tree link;
17306 TREE_ASM_WRITTEN (type) = 1;
17307 add_byte_size_attribute (type_die, type);
17308 if (TYPE_STUB_DECL (type) != NULL_TREE)
17310 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17311 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17314 /* If the first reference to this type was as the return type of an
17315 inline function, then it may not have a parent. Fix this now. */
17316 if (type_die->die_parent == NULL)
17317 add_child_die (scope_die_for (type, context_die), type_die);
17319 for (link = TYPE_VALUES (type);
17320 link != NULL; link = TREE_CHAIN (link))
17322 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17323 tree value = TREE_VALUE (link);
17325 add_name_attribute (enum_die,
17326 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17328 if (TREE_CODE (value) == CONST_DECL)
17329 value = DECL_INITIAL (value);
17331 if (simple_type_size_in_bits (TREE_TYPE (value))
17332 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17333 /* DWARF2 does not provide a way of indicating whether or
17334 not enumeration constants are signed or unsigned. GDB
17335 always assumes the values are signed, so we output all
17336 values as if they were signed. That means that
17337 enumeration constants with very large unsigned values
17338 will appear to have negative values in the debugger.
17340 TODO: the above comment is wrong, DWARF2 does provide
17341 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17342 This should be re-worked to use correct signed/unsigned
17343 int/double tags for all cases, instead of always treating as
17344 signed. */
17345 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17346 else
17347 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17348 that here. */
17349 add_AT_double (enum_die, DW_AT_const_value,
17350 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17353 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17354 if (TYPE_ARTIFICIAL (type))
17355 add_AT_flag (type_die, DW_AT_artificial, 1);
17357 else
17358 add_AT_flag (type_die, DW_AT_declaration, 1);
17360 add_pubtype (type, type_die);
17362 return type_die;
17365 /* Generate a DIE to represent either a real live formal parameter decl or to
17366 represent just the type of some formal parameter position in some function
17367 type.
17369 Note that this routine is a bit unusual because its argument may be a
17370 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17371 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17372 node. If it's the former then this function is being called to output a
17373 DIE to represent a formal parameter object (or some inlining thereof). If
17374 it's the latter, then this function is only being called to output a
17375 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17376 argument type of some subprogram type.
17377 If EMIT_NAME_P is true, name and source coordinate attributes
17378 are emitted. */
17380 static dw_die_ref
17381 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17382 dw_die_ref context_die)
17384 tree node_or_origin = node ? node : origin;
17385 tree ultimate_origin;
17386 dw_die_ref parm_die
17387 = new_die (DW_TAG_formal_parameter, context_die, node);
17389 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17391 case tcc_declaration:
17392 ultimate_origin = decl_ultimate_origin (node_or_origin);
17393 if (node || ultimate_origin)
17394 origin = ultimate_origin;
17395 if (origin != NULL)
17396 add_abstract_origin_attribute (parm_die, origin);
17397 else if (emit_name_p)
17398 add_name_and_src_coords_attributes (parm_die, node);
17399 if (origin == NULL
17400 || (! DECL_ABSTRACT (node_or_origin)
17401 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17402 decl_function_context
17403 (node_or_origin))))
17405 tree type = TREE_TYPE (node_or_origin);
17406 if (decl_by_reference_p (node_or_origin))
17407 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17408 context_die);
17409 else
17410 add_type_attribute (parm_die, type,
17411 TREE_READONLY (node_or_origin),
17412 TREE_THIS_VOLATILE (node_or_origin),
17413 context_die);
17415 if (origin == NULL && DECL_ARTIFICIAL (node))
17416 add_AT_flag (parm_die, DW_AT_artificial, 1);
17418 if (node && node != origin)
17419 equate_decl_number_to_die (node, parm_die);
17420 if (! DECL_ABSTRACT (node_or_origin))
17421 add_location_or_const_value_attribute (parm_die, node_or_origin,
17422 node == NULL, DW_AT_location);
17424 break;
17426 case tcc_type:
17427 /* We were called with some kind of a ..._TYPE node. */
17428 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17429 break;
17431 default:
17432 gcc_unreachable ();
17435 return parm_die;
17438 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17439 children DW_TAG_formal_parameter DIEs representing the arguments of the
17440 parameter pack.
17442 PARM_PACK must be a function parameter pack.
17443 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17444 must point to the subsequent arguments of the function PACK_ARG belongs to.
17445 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17446 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17447 following the last one for which a DIE was generated. */
17449 static dw_die_ref
17450 gen_formal_parameter_pack_die (tree parm_pack,
17451 tree pack_arg,
17452 dw_die_ref subr_die,
17453 tree *next_arg)
17455 tree arg;
17456 dw_die_ref parm_pack_die;
17458 gcc_assert (parm_pack
17459 && lang_hooks.function_parameter_pack_p (parm_pack)
17460 && subr_die);
17462 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17463 add_src_coords_attributes (parm_pack_die, parm_pack);
17465 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17467 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17468 parm_pack))
17469 break;
17470 gen_formal_parameter_die (arg, NULL,
17471 false /* Don't emit name attribute. */,
17472 parm_pack_die);
17474 if (next_arg)
17475 *next_arg = arg;
17476 return parm_pack_die;
17479 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17480 at the end of an (ANSI prototyped) formal parameters list. */
17482 static void
17483 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17485 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17488 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17489 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17490 parameters as specified in some function type specification (except for
17491 those which appear as part of a function *definition*). */
17493 static void
17494 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17496 tree link;
17497 tree formal_type = NULL;
17498 tree first_parm_type;
17499 tree arg;
17501 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17503 arg = DECL_ARGUMENTS (function_or_method_type);
17504 function_or_method_type = TREE_TYPE (function_or_method_type);
17506 else
17507 arg = NULL_TREE;
17509 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17511 /* Make our first pass over the list of formal parameter types and output a
17512 DW_TAG_formal_parameter DIE for each one. */
17513 for (link = first_parm_type; link; )
17515 dw_die_ref parm_die;
17517 formal_type = TREE_VALUE (link);
17518 if (formal_type == void_type_node)
17519 break;
17521 /* Output a (nameless) DIE to represent the formal parameter itself. */
17522 parm_die = gen_formal_parameter_die (formal_type, NULL,
17523 true /* Emit name attribute. */,
17524 context_die);
17525 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17526 && link == first_parm_type)
17528 add_AT_flag (parm_die, DW_AT_artificial, 1);
17529 if (dwarf_version >= 3 || !dwarf_strict)
17530 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17532 else if (arg && DECL_ARTIFICIAL (arg))
17533 add_AT_flag (parm_die, DW_AT_artificial, 1);
17535 link = TREE_CHAIN (link);
17536 if (arg)
17537 arg = DECL_CHAIN (arg);
17540 /* If this function type has an ellipsis, add a
17541 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17542 if (formal_type != void_type_node)
17543 gen_unspecified_parameters_die (function_or_method_type, context_die);
17545 /* Make our second (and final) pass over the list of formal parameter types
17546 and output DIEs to represent those types (as necessary). */
17547 for (link = TYPE_ARG_TYPES (function_or_method_type);
17548 link && TREE_VALUE (link);
17549 link = TREE_CHAIN (link))
17550 gen_type_die (TREE_VALUE (link), context_die);
17553 /* We want to generate the DIE for TYPE so that we can generate the
17554 die for MEMBER, which has been defined; we will need to refer back
17555 to the member declaration nested within TYPE. If we're trying to
17556 generate minimal debug info for TYPE, processing TYPE won't do the
17557 trick; we need to attach the member declaration by hand. */
17559 static void
17560 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17562 gen_type_die (type, context_die);
17564 /* If we're trying to avoid duplicate debug info, we may not have
17565 emitted the member decl for this function. Emit it now. */
17566 if (TYPE_STUB_DECL (type)
17567 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17568 && ! lookup_decl_die (member))
17570 dw_die_ref type_die;
17571 gcc_assert (!decl_ultimate_origin (member));
17573 push_decl_scope (type);
17574 type_die = lookup_type_die_strip_naming_typedef (type);
17575 if (TREE_CODE (member) == FUNCTION_DECL)
17576 gen_subprogram_die (member, type_die);
17577 else if (TREE_CODE (member) == FIELD_DECL)
17579 /* Ignore the nameless fields that are used to skip bits but handle
17580 C++ anonymous unions and structs. */
17581 if (DECL_NAME (member) != NULL_TREE
17582 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17583 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17585 gen_type_die (member_declared_type (member), type_die);
17586 gen_field_die (member, type_die);
17589 else
17590 gen_variable_die (member, NULL_TREE, type_die);
17592 pop_decl_scope ();
17596 /* Forward declare these functions, because they are mutually recursive
17597 with their set_block_* pairing functions. */
17598 static void set_decl_origin_self (tree);
17599 static void set_decl_abstract_flags (tree, int);
17601 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17602 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17603 that it points to the node itself, thus indicating that the node is its
17604 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17605 the given node is NULL, recursively descend the decl/block tree which
17606 it is the root of, and for each other ..._DECL or BLOCK node contained
17607 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17608 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17609 values to point to themselves. */
17611 static void
17612 set_block_origin_self (tree stmt)
17614 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17616 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17619 tree local_decl;
17621 for (local_decl = BLOCK_VARS (stmt);
17622 local_decl != NULL_TREE;
17623 local_decl = DECL_CHAIN (local_decl))
17624 if (! DECL_EXTERNAL (local_decl))
17625 set_decl_origin_self (local_decl); /* Potential recursion. */
17629 tree subblock;
17631 for (subblock = BLOCK_SUBBLOCKS (stmt);
17632 subblock != NULL_TREE;
17633 subblock = BLOCK_CHAIN (subblock))
17634 set_block_origin_self (subblock); /* Recurse. */
17639 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17640 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17641 node to so that it points to the node itself, thus indicating that the
17642 node represents its own (abstract) origin. Additionally, if the
17643 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17644 the decl/block tree of which the given node is the root of, and for
17645 each other ..._DECL or BLOCK node contained therein whose
17646 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17647 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17648 point to themselves. */
17650 static void
17651 set_decl_origin_self (tree decl)
17653 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17655 DECL_ABSTRACT_ORIGIN (decl) = decl;
17656 if (TREE_CODE (decl) == FUNCTION_DECL)
17658 tree arg;
17660 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17661 DECL_ABSTRACT_ORIGIN (arg) = arg;
17662 if (DECL_INITIAL (decl) != NULL_TREE
17663 && DECL_INITIAL (decl) != error_mark_node)
17664 set_block_origin_self (DECL_INITIAL (decl));
17669 /* Given a pointer to some BLOCK node, and a boolean value to set the
17670 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17671 the given block, and for all local decls and all local sub-blocks
17672 (recursively) which are contained therein. */
17674 static void
17675 set_block_abstract_flags (tree stmt, int setting)
17677 tree local_decl;
17678 tree subblock;
17679 unsigned int i;
17681 BLOCK_ABSTRACT (stmt) = setting;
17683 for (local_decl = BLOCK_VARS (stmt);
17684 local_decl != NULL_TREE;
17685 local_decl = DECL_CHAIN (local_decl))
17686 if (! DECL_EXTERNAL (local_decl))
17687 set_decl_abstract_flags (local_decl, setting);
17689 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17691 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17692 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17693 || TREE_CODE (local_decl) == PARM_DECL)
17694 set_decl_abstract_flags (local_decl, setting);
17697 for (subblock = BLOCK_SUBBLOCKS (stmt);
17698 subblock != NULL_TREE;
17699 subblock = BLOCK_CHAIN (subblock))
17700 set_block_abstract_flags (subblock, setting);
17703 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17704 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17705 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17706 set the abstract flags for all of the parameters, local vars, local
17707 blocks and sub-blocks (recursively) to the same setting. */
17709 static void
17710 set_decl_abstract_flags (tree decl, int setting)
17712 DECL_ABSTRACT (decl) = setting;
17713 if (TREE_CODE (decl) == FUNCTION_DECL)
17715 tree arg;
17717 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17718 DECL_ABSTRACT (arg) = setting;
17719 if (DECL_INITIAL (decl) != NULL_TREE
17720 && DECL_INITIAL (decl) != error_mark_node)
17721 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17725 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17726 may later generate inlined and/or out-of-line instances of. */
17728 static void
17729 dwarf2out_abstract_function (tree decl)
17731 dw_die_ref old_die;
17732 tree save_fn;
17733 tree context;
17734 int was_abstract;
17735 htab_t old_decl_loc_table;
17736 htab_t old_cached_dw_loc_list_table;
17737 int old_call_site_count, old_tail_call_site_count;
17738 struct call_arg_loc_node *old_call_arg_locations;
17740 /* Make sure we have the actual abstract inline, not a clone. */
17741 decl = DECL_ORIGIN (decl);
17743 old_die = lookup_decl_die (decl);
17744 if (old_die && get_AT (old_die, DW_AT_inline))
17745 /* We've already generated the abstract instance. */
17746 return;
17748 /* We can be called while recursively when seeing block defining inlined subroutine
17749 DIE. Be sure to not clobber the outer location table nor use it or we would
17750 get locations in abstract instantces. */
17751 old_decl_loc_table = decl_loc_table;
17752 decl_loc_table = NULL;
17753 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17754 cached_dw_loc_list_table = NULL;
17755 old_call_arg_locations = call_arg_locations;
17756 call_arg_locations = NULL;
17757 old_call_site_count = call_site_count;
17758 call_site_count = -1;
17759 old_tail_call_site_count = tail_call_site_count;
17760 tail_call_site_count = -1;
17762 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17763 we don't get confused by DECL_ABSTRACT. */
17764 if (debug_info_level > DINFO_LEVEL_TERSE)
17766 context = decl_class_context (decl);
17767 if (context)
17768 gen_type_die_for_member
17769 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17772 /* Pretend we've just finished compiling this function. */
17773 save_fn = current_function_decl;
17774 current_function_decl = decl;
17776 was_abstract = DECL_ABSTRACT (decl);
17777 set_decl_abstract_flags (decl, 1);
17778 dwarf2out_decl (decl);
17779 if (! was_abstract)
17780 set_decl_abstract_flags (decl, 0);
17782 current_function_decl = save_fn;
17783 decl_loc_table = old_decl_loc_table;
17784 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17785 call_arg_locations = old_call_arg_locations;
17786 call_site_count = old_call_site_count;
17787 tail_call_site_count = old_tail_call_site_count;
17790 /* Helper function of premark_used_types() which gets called through
17791 htab_traverse.
17793 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17794 marked as unused by prune_unused_types. */
17796 static int
17797 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17799 tree type;
17800 dw_die_ref die;
17802 type = (tree) *slot;
17803 die = lookup_type_die (type);
17804 if (die != NULL)
17805 die->die_perennial_p = 1;
17806 return 1;
17809 /* Helper function of premark_types_used_by_global_vars which gets called
17810 through htab_traverse.
17812 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17813 marked as unused by prune_unused_types. The DIE of the type is marked
17814 only if the global variable using the type will actually be emitted. */
17816 static int
17817 premark_types_used_by_global_vars_helper (void **slot,
17818 void *data ATTRIBUTE_UNUSED)
17820 struct types_used_by_vars_entry *entry;
17821 dw_die_ref die;
17823 entry = (struct types_used_by_vars_entry *) *slot;
17824 gcc_assert (entry->type != NULL
17825 && entry->var_decl != NULL);
17826 die = lookup_type_die (entry->type);
17827 if (die)
17829 /* Ask cgraph if the global variable really is to be emitted.
17830 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17831 varpool_node *node = varpool_get_node (entry->var_decl);
17832 if (node && node->definition)
17834 die->die_perennial_p = 1;
17835 /* Keep the parent DIEs as well. */
17836 while ((die = die->die_parent) && die->die_perennial_p == 0)
17837 die->die_perennial_p = 1;
17840 return 1;
17843 /* Mark all members of used_types_hash as perennial. */
17845 static void
17846 premark_used_types (struct function *fun)
17848 if (fun && fun->used_types_hash)
17849 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17852 /* Mark all members of types_used_by_vars_entry as perennial. */
17854 static void
17855 premark_types_used_by_global_vars (void)
17857 if (types_used_by_vars_hash)
17858 htab_traverse (types_used_by_vars_hash,
17859 premark_types_used_by_global_vars_helper, NULL);
17862 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17863 for CA_LOC call arg loc node. */
17865 static dw_die_ref
17866 gen_call_site_die (tree decl, dw_die_ref subr_die,
17867 struct call_arg_loc_node *ca_loc)
17869 dw_die_ref stmt_die = NULL, die;
17870 tree block = ca_loc->block;
17872 while (block
17873 && block != DECL_INITIAL (decl)
17874 && TREE_CODE (block) == BLOCK)
17876 if (block_map.length () > BLOCK_NUMBER (block))
17877 stmt_die = block_map[BLOCK_NUMBER (block)];
17878 if (stmt_die)
17879 break;
17880 block = BLOCK_SUPERCONTEXT (block);
17882 if (stmt_die == NULL)
17883 stmt_die = subr_die;
17884 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17885 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17886 if (ca_loc->tail_call_p)
17887 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17888 if (ca_loc->symbol_ref)
17890 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17891 if (tdie)
17892 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17893 else
17894 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17896 return die;
17899 /* Generate a DIE to represent a declared function (either file-scope or
17900 block-local). */
17902 static void
17903 gen_subprogram_die (tree decl, dw_die_ref context_die)
17905 tree origin = decl_ultimate_origin (decl);
17906 dw_die_ref subr_die;
17907 tree outer_scope;
17908 dw_die_ref old_die = lookup_decl_die (decl);
17909 int declaration = (current_function_decl != decl
17910 || class_or_namespace_scope_p (context_die));
17912 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17914 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17915 started to generate the abstract instance of an inline, decided to output
17916 its containing class, and proceeded to emit the declaration of the inline
17917 from the member list for the class. If so, DECLARATION takes priority;
17918 we'll get back to the abstract instance when done with the class. */
17920 /* The class-scope declaration DIE must be the primary DIE. */
17921 if (origin && declaration && class_or_namespace_scope_p (context_die))
17923 origin = NULL;
17924 gcc_assert (!old_die);
17927 /* Now that the C++ front end lazily declares artificial member fns, we
17928 might need to retrofit the declaration into its class. */
17929 if (!declaration && !origin && !old_die
17930 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17931 && !class_or_namespace_scope_p (context_die)
17932 && debug_info_level > DINFO_LEVEL_TERSE)
17933 old_die = force_decl_die (decl);
17935 if (origin != NULL)
17937 gcc_assert (!declaration || local_scope_p (context_die));
17939 /* Fixup die_parent for the abstract instance of a nested
17940 inline function. */
17941 if (old_die && old_die->die_parent == NULL)
17942 add_child_die (context_die, old_die);
17944 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17945 add_abstract_origin_attribute (subr_die, origin);
17946 /* This is where the actual code for a cloned function is.
17947 Let's emit linkage name attribute for it. This helps
17948 debuggers to e.g, set breakpoints into
17949 constructors/destructors when the user asks "break
17950 K::K". */
17951 add_linkage_name (subr_die, decl);
17953 else if (old_die)
17955 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17956 struct dwarf_file_data * file_index = lookup_filename (s.file);
17958 if (!get_AT_flag (old_die, DW_AT_declaration)
17959 /* We can have a normal definition following an inline one in the
17960 case of redefinition of GNU C extern inlines.
17961 It seems reasonable to use AT_specification in this case. */
17962 && !get_AT (old_die, DW_AT_inline))
17964 /* Detect and ignore this case, where we are trying to output
17965 something we have already output. */
17966 return;
17969 /* If the definition comes from the same place as the declaration,
17970 maybe use the old DIE. We always want the DIE for this function
17971 that has the *_pc attributes to be under comp_unit_die so the
17972 debugger can find it. We also need to do this for abstract
17973 instances of inlines, since the spec requires the out-of-line copy
17974 to have the same parent. For local class methods, this doesn't
17975 apply; we just use the old DIE. */
17976 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17977 && (DECL_ARTIFICIAL (decl)
17978 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17979 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17980 == (unsigned) s.line))))
17982 subr_die = old_die;
17984 /* Clear out the declaration attribute and the formal parameters.
17985 Do not remove all children, because it is possible that this
17986 declaration die was forced using force_decl_die(). In such
17987 cases die that forced declaration die (e.g. TAG_imported_module)
17988 is one of the children that we do not want to remove. */
17989 remove_AT (subr_die, DW_AT_declaration);
17990 remove_AT (subr_die, DW_AT_object_pointer);
17991 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17993 else
17995 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17996 add_AT_specification (subr_die, old_die);
17997 add_pubname (decl, subr_die);
17998 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17999 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18000 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18001 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18004 else
18006 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18008 if (TREE_PUBLIC (decl))
18009 add_AT_flag (subr_die, DW_AT_external, 1);
18011 add_name_and_src_coords_attributes (subr_die, decl);
18012 add_pubname (decl, subr_die);
18013 if (debug_info_level > DINFO_LEVEL_TERSE)
18015 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18016 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18017 0, 0, context_die);
18020 add_pure_or_virtual_attribute (subr_die, decl);
18021 if (DECL_ARTIFICIAL (decl))
18022 add_AT_flag (subr_die, DW_AT_artificial, 1);
18024 add_accessibility_attribute (subr_die, decl);
18027 if (declaration)
18029 if (!old_die || !get_AT (old_die, DW_AT_inline))
18031 add_AT_flag (subr_die, DW_AT_declaration, 1);
18033 /* If this is an explicit function declaration then generate
18034 a DW_AT_explicit attribute. */
18035 if (lang_hooks.decls.function_decl_explicit_p (decl)
18036 && (dwarf_version >= 3 || !dwarf_strict))
18037 add_AT_flag (subr_die, DW_AT_explicit, 1);
18039 /* The first time we see a member function, it is in the context of
18040 the class to which it belongs. We make sure of this by emitting
18041 the class first. The next time is the definition, which is
18042 handled above. The two may come from the same source text.
18044 Note that force_decl_die() forces function declaration die. It is
18045 later reused to represent definition. */
18046 equate_decl_number_to_die (decl, subr_die);
18049 else if (DECL_ABSTRACT (decl))
18051 if (DECL_DECLARED_INLINE_P (decl))
18053 if (cgraph_function_possibly_inlined_p (decl))
18054 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18055 else
18056 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18058 else
18060 if (cgraph_function_possibly_inlined_p (decl))
18061 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18062 else
18063 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18066 if (DECL_DECLARED_INLINE_P (decl)
18067 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18068 add_AT_flag (subr_die, DW_AT_artificial, 1);
18070 equate_decl_number_to_die (decl, subr_die);
18072 else if (!DECL_EXTERNAL (decl))
18074 HOST_WIDE_INT cfa_fb_offset;
18075 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18077 if (!old_die || !get_AT (old_die, DW_AT_inline))
18078 equate_decl_number_to_die (decl, subr_die);
18080 gcc_checking_assert (fun);
18081 if (!flag_reorder_blocks_and_partition)
18083 dw_fde_ref fde = fun->fde;
18084 if (fde->dw_fde_begin)
18086 /* We have already generated the labels. */
18087 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18088 fde->dw_fde_end, false);
18090 else
18092 /* Create start/end labels and add the range. */
18093 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18094 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18095 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18096 current_function_funcdef_no);
18097 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18098 current_function_funcdef_no);
18099 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18100 false);
18103 #if VMS_DEBUGGING_INFO
18104 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18105 Section 2.3 Prologue and Epilogue Attributes:
18106 When a breakpoint is set on entry to a function, it is generally
18107 desirable for execution to be suspended, not on the very first
18108 instruction of the function, but rather at a point after the
18109 function's frame has been set up, after any language defined local
18110 declaration processing has been completed, and before execution of
18111 the first statement of the function begins. Debuggers generally
18112 cannot properly determine where this point is. Similarly for a
18113 breakpoint set on exit from a function. The prologue and epilogue
18114 attributes allow a compiler to communicate the location(s) to use. */
18117 if (fde->dw_fde_vms_end_prologue)
18118 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18119 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18121 if (fde->dw_fde_vms_begin_epilogue)
18122 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18123 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18125 #endif
18128 else
18130 /* Generate pubnames entries for the split function code ranges. */
18131 dw_fde_ref fde = fun->fde;
18133 if (fde->dw_fde_second_begin)
18135 if (dwarf_version >= 3 || !dwarf_strict)
18137 /* We should use ranges for non-contiguous code section
18138 addresses. Use the actual code range for the initial
18139 section, since the HOT/COLD labels might precede an
18140 alignment offset. */
18141 bool range_list_added = false;
18142 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18143 fde->dw_fde_end, &range_list_added,
18144 false);
18145 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18146 fde->dw_fde_second_end,
18147 &range_list_added, false);
18148 if (range_list_added)
18149 add_ranges (NULL);
18151 else
18153 /* There is no real support in DW2 for this .. so we make
18154 a work-around. First, emit the pub name for the segment
18155 containing the function label. Then make and emit a
18156 simplified subprogram DIE for the second segment with the
18157 name pre-fixed by __hot/cold_sect_of_. We use the same
18158 linkage name for the second die so that gdb will find both
18159 sections when given "b foo". */
18160 const char *name = NULL;
18161 tree decl_name = DECL_NAME (decl);
18162 dw_die_ref seg_die;
18164 /* Do the 'primary' section. */
18165 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18166 fde->dw_fde_end, false);
18168 /* Build a minimal DIE for the secondary section. */
18169 seg_die = new_die (DW_TAG_subprogram,
18170 subr_die->die_parent, decl);
18172 if (TREE_PUBLIC (decl))
18173 add_AT_flag (seg_die, DW_AT_external, 1);
18175 if (decl_name != NULL
18176 && IDENTIFIER_POINTER (decl_name) != NULL)
18178 name = dwarf2_name (decl, 1);
18179 if (! DECL_ARTIFICIAL (decl))
18180 add_src_coords_attributes (seg_die, decl);
18182 add_linkage_name (seg_die, decl);
18184 gcc_assert (name != NULL);
18185 add_pure_or_virtual_attribute (seg_die, decl);
18186 if (DECL_ARTIFICIAL (decl))
18187 add_AT_flag (seg_die, DW_AT_artificial, 1);
18189 name = concat ("__second_sect_of_", name, NULL);
18190 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18191 fde->dw_fde_second_end, false);
18192 add_name_attribute (seg_die, name);
18193 if (want_pubnames ())
18194 add_pubname_string (name, seg_die);
18197 else
18198 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18199 false);
18202 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18204 /* We define the "frame base" as the function's CFA. This is more
18205 convenient for several reasons: (1) It's stable across the prologue
18206 and epilogue, which makes it better than just a frame pointer,
18207 (2) With dwarf3, there exists a one-byte encoding that allows us
18208 to reference the .debug_frame data by proxy, but failing that,
18209 (3) We can at least reuse the code inspection and interpretation
18210 code that determines the CFA position at various points in the
18211 function. */
18212 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18214 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18215 add_AT_loc (subr_die, DW_AT_frame_base, op);
18217 else
18219 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18220 if (list->dw_loc_next)
18221 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18222 else
18223 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18226 /* Compute a displacement from the "steady-state frame pointer" to
18227 the CFA. The former is what all stack slots and argument slots
18228 will reference in the rtl; the latter is what we've told the
18229 debugger about. We'll need to adjust all frame_base references
18230 by this displacement. */
18231 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18233 if (fun->static_chain_decl)
18234 add_AT_location_description (subr_die, DW_AT_static_link,
18235 loc_list_from_tree (fun->static_chain_decl, 2));
18238 /* Generate child dies for template paramaters. */
18239 if (debug_info_level > DINFO_LEVEL_TERSE)
18240 gen_generic_params_dies (decl);
18242 /* Now output descriptions of the arguments for this function. This gets
18243 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18244 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18245 `...' at the end of the formal parameter list. In order to find out if
18246 there was a trailing ellipsis or not, we must instead look at the type
18247 associated with the FUNCTION_DECL. This will be a node of type
18248 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18249 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18250 an ellipsis at the end. */
18252 /* In the case where we are describing a mere function declaration, all we
18253 need to do here (and all we *can* do here) is to describe the *types* of
18254 its formal parameters. */
18255 if (debug_info_level <= DINFO_LEVEL_TERSE)
18257 else if (declaration)
18258 gen_formal_types_die (decl, subr_die);
18259 else
18261 /* Generate DIEs to represent all known formal parameters. */
18262 tree parm = DECL_ARGUMENTS (decl);
18263 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18264 tree generic_decl_parm = generic_decl
18265 ? DECL_ARGUMENTS (generic_decl)
18266 : NULL;
18268 /* Now we want to walk the list of parameters of the function and
18269 emit their relevant DIEs.
18271 We consider the case of DECL being an instance of a generic function
18272 as well as it being a normal function.
18274 If DECL is an instance of a generic function we walk the
18275 parameters of the generic function declaration _and_ the parameters of
18276 DECL itself. This is useful because we want to emit specific DIEs for
18277 function parameter packs and those are declared as part of the
18278 generic function declaration. In that particular case,
18279 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18280 That DIE has children DIEs representing the set of arguments
18281 of the pack. Note that the set of pack arguments can be empty.
18282 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18283 children DIE.
18285 Otherwise, we just consider the parameters of DECL. */
18286 while (generic_decl_parm || parm)
18288 if (generic_decl_parm
18289 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18290 gen_formal_parameter_pack_die (generic_decl_parm,
18291 parm, subr_die,
18292 &parm);
18293 else if (parm)
18295 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18297 if (parm == DECL_ARGUMENTS (decl)
18298 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18299 && parm_die
18300 && (dwarf_version >= 3 || !dwarf_strict))
18301 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18303 parm = DECL_CHAIN (parm);
18306 if (generic_decl_parm)
18307 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18310 /* Decide whether we need an unspecified_parameters DIE at the end.
18311 There are 2 more cases to do this for: 1) the ansi ... declaration -
18312 this is detectable when the end of the arg list is not a
18313 void_type_node 2) an unprototyped function declaration (not a
18314 definition). This just means that we have no info about the
18315 parameters at all. */
18316 if (prototype_p (TREE_TYPE (decl)))
18318 /* This is the prototyped case, check for.... */
18319 if (stdarg_p (TREE_TYPE (decl)))
18320 gen_unspecified_parameters_die (decl, subr_die);
18322 else if (DECL_INITIAL (decl) == NULL_TREE)
18323 gen_unspecified_parameters_die (decl, subr_die);
18326 /* Output Dwarf info for all of the stuff within the body of the function
18327 (if it has one - it may be just a declaration). */
18328 outer_scope = DECL_INITIAL (decl);
18330 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18331 a function. This BLOCK actually represents the outermost binding contour
18332 for the function, i.e. the contour in which the function's formal
18333 parameters and labels get declared. Curiously, it appears that the front
18334 end doesn't actually put the PARM_DECL nodes for the current function onto
18335 the BLOCK_VARS list for this outer scope, but are strung off of the
18336 DECL_ARGUMENTS list for the function instead.
18338 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18339 the LABEL_DECL nodes for the function however, and we output DWARF info
18340 for those in decls_for_scope. Just within the `outer_scope' there will be
18341 a BLOCK node representing the function's outermost pair of curly braces,
18342 and any blocks used for the base and member initializers of a C++
18343 constructor function. */
18344 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18346 int call_site_note_count = 0;
18347 int tail_call_site_note_count = 0;
18349 /* Emit a DW_TAG_variable DIE for a named return value. */
18350 if (DECL_NAME (DECL_RESULT (decl)))
18351 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18353 current_function_has_inlines = 0;
18354 decls_for_scope (outer_scope, subr_die, 0);
18356 if (call_arg_locations && !dwarf_strict)
18358 struct call_arg_loc_node *ca_loc;
18359 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18361 dw_die_ref die = NULL;
18362 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18363 rtx arg, next_arg;
18365 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18366 arg; arg = next_arg)
18368 dw_loc_descr_ref reg, val;
18369 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18370 dw_die_ref cdie, tdie = NULL;
18372 next_arg = XEXP (arg, 1);
18373 if (REG_P (XEXP (XEXP (arg, 0), 0))
18374 && next_arg
18375 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18376 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18377 && REGNO (XEXP (XEXP (arg, 0), 0))
18378 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18379 next_arg = XEXP (next_arg, 1);
18380 if (mode == VOIDmode)
18382 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18383 if (mode == VOIDmode)
18384 mode = GET_MODE (XEXP (arg, 0));
18386 if (mode == VOIDmode || mode == BLKmode)
18387 continue;
18388 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18390 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18391 tloc = XEXP (XEXP (arg, 0), 1);
18392 continue;
18394 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18395 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18397 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18398 tlocc = XEXP (XEXP (arg, 0), 1);
18399 continue;
18401 reg = NULL;
18402 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18403 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18404 VAR_INIT_STATUS_INITIALIZED);
18405 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18407 rtx mem = XEXP (XEXP (arg, 0), 0);
18408 reg = mem_loc_descriptor (XEXP (mem, 0),
18409 get_address_mode (mem),
18410 GET_MODE (mem),
18411 VAR_INIT_STATUS_INITIALIZED);
18413 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18414 == DEBUG_PARAMETER_REF)
18416 tree tdecl
18417 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18418 tdie = lookup_decl_die (tdecl);
18419 if (tdie == NULL)
18420 continue;
18422 else
18423 continue;
18424 if (reg == NULL
18425 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18426 != DEBUG_PARAMETER_REF)
18427 continue;
18428 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18429 VOIDmode,
18430 VAR_INIT_STATUS_INITIALIZED);
18431 if (val == NULL)
18432 continue;
18433 if (die == NULL)
18434 die = gen_call_site_die (decl, subr_die, ca_loc);
18435 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18436 NULL_TREE);
18437 if (reg != NULL)
18438 add_AT_loc (cdie, DW_AT_location, reg);
18439 else if (tdie != NULL)
18440 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18441 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18442 if (next_arg != XEXP (arg, 1))
18444 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18445 if (mode == VOIDmode)
18446 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18447 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18448 0), 1),
18449 mode, VOIDmode,
18450 VAR_INIT_STATUS_INITIALIZED);
18451 if (val != NULL)
18452 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18455 if (die == NULL
18456 && (ca_loc->symbol_ref || tloc))
18457 die = gen_call_site_die (decl, subr_die, ca_loc);
18458 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18460 dw_loc_descr_ref tval = NULL;
18462 if (tloc != NULL_RTX)
18463 tval = mem_loc_descriptor (tloc,
18464 GET_MODE (tloc) == VOIDmode
18465 ? Pmode : GET_MODE (tloc),
18466 VOIDmode,
18467 VAR_INIT_STATUS_INITIALIZED);
18468 if (tval)
18469 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18470 else if (tlocc != NULL_RTX)
18472 tval = mem_loc_descriptor (tlocc,
18473 GET_MODE (tlocc) == VOIDmode
18474 ? Pmode : GET_MODE (tlocc),
18475 VOIDmode,
18476 VAR_INIT_STATUS_INITIALIZED);
18477 if (tval)
18478 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18479 tval);
18482 if (die != NULL)
18484 call_site_note_count++;
18485 if (ca_loc->tail_call_p)
18486 tail_call_site_note_count++;
18490 call_arg_locations = NULL;
18491 call_arg_loc_last = NULL;
18492 if (tail_call_site_count >= 0
18493 && tail_call_site_count == tail_call_site_note_count
18494 && !dwarf_strict)
18496 if (call_site_count >= 0
18497 && call_site_count == call_site_note_count)
18498 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18499 else
18500 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18502 call_site_count = -1;
18503 tail_call_site_count = -1;
18505 /* Add the calling convention attribute if requested. */
18506 add_calling_convention_attribute (subr_die, decl);
18510 /* Returns a hash value for X (which really is a die_struct). */
18512 static hashval_t
18513 common_block_die_table_hash (const void *x)
18515 const_dw_die_ref d = (const_dw_die_ref) x;
18516 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18519 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18520 as decl_id and die_parent of die_struct Y. */
18522 static int
18523 common_block_die_table_eq (const void *x, const void *y)
18525 const_dw_die_ref d = (const_dw_die_ref) x;
18526 const_dw_die_ref e = (const_dw_die_ref) y;
18527 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18530 /* Generate a DIE to represent a declared data object.
18531 Either DECL or ORIGIN must be non-null. */
18533 static void
18534 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18536 HOST_WIDE_INT off = 0;
18537 tree com_decl;
18538 tree decl_or_origin = decl ? decl : origin;
18539 tree ultimate_origin;
18540 dw_die_ref var_die;
18541 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18542 dw_die_ref origin_die;
18543 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18544 || class_or_namespace_scope_p (context_die));
18545 bool specialization_p = false;
18547 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18548 if (decl || ultimate_origin)
18549 origin = ultimate_origin;
18550 com_decl = fortran_common (decl_or_origin, &off);
18552 /* Symbol in common gets emitted as a child of the common block, in the form
18553 of a data member. */
18554 if (com_decl)
18556 dw_die_ref com_die;
18557 dw_loc_list_ref loc;
18558 die_node com_die_arg;
18560 var_die = lookup_decl_die (decl_or_origin);
18561 if (var_die)
18563 if (get_AT (var_die, DW_AT_location) == NULL)
18565 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18566 if (loc)
18568 if (off)
18570 /* Optimize the common case. */
18571 if (single_element_loc_list_p (loc)
18572 && loc->expr->dw_loc_opc == DW_OP_addr
18573 && loc->expr->dw_loc_next == NULL
18574 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18575 == SYMBOL_REF)
18577 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18578 loc->expr->dw_loc_oprnd1.v.val_addr
18579 = plus_constant (GET_MODE (x), x , off);
18581 else
18582 loc_list_plus_const (loc, off);
18584 add_AT_location_description (var_die, DW_AT_location, loc);
18585 remove_AT (var_die, DW_AT_declaration);
18588 return;
18591 if (common_block_die_table == NULL)
18592 common_block_die_table
18593 = htab_create_ggc (10, common_block_die_table_hash,
18594 common_block_die_table_eq, NULL);
18596 com_die_arg.decl_id = DECL_UID (com_decl);
18597 com_die_arg.die_parent = context_die;
18598 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18599 loc = loc_list_from_tree (com_decl, 2);
18600 if (com_die == NULL)
18602 const char *cnam
18603 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18604 void **slot;
18606 com_die = new_die (DW_TAG_common_block, context_die, decl);
18607 add_name_and_src_coords_attributes (com_die, com_decl);
18608 if (loc)
18610 add_AT_location_description (com_die, DW_AT_location, loc);
18611 /* Avoid sharing the same loc descriptor between
18612 DW_TAG_common_block and DW_TAG_variable. */
18613 loc = loc_list_from_tree (com_decl, 2);
18615 else if (DECL_EXTERNAL (decl))
18616 add_AT_flag (com_die, DW_AT_declaration, 1);
18617 if (want_pubnames ())
18618 add_pubname_string (cnam, com_die); /* ??? needed? */
18619 com_die->decl_id = DECL_UID (com_decl);
18620 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18621 *slot = (void *) com_die;
18623 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18625 add_AT_location_description (com_die, DW_AT_location, loc);
18626 loc = loc_list_from_tree (com_decl, 2);
18627 remove_AT (com_die, DW_AT_declaration);
18629 var_die = new_die (DW_TAG_variable, com_die, decl);
18630 add_name_and_src_coords_attributes (var_die, decl);
18631 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18632 TREE_THIS_VOLATILE (decl), context_die);
18633 add_AT_flag (var_die, DW_AT_external, 1);
18634 if (loc)
18636 if (off)
18638 /* Optimize the common case. */
18639 if (single_element_loc_list_p (loc)
18640 && loc->expr->dw_loc_opc == DW_OP_addr
18641 && loc->expr->dw_loc_next == NULL
18642 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18644 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18645 loc->expr->dw_loc_oprnd1.v.val_addr
18646 = plus_constant (GET_MODE (x), x, off);
18648 else
18649 loc_list_plus_const (loc, off);
18651 add_AT_location_description (var_die, DW_AT_location, loc);
18653 else if (DECL_EXTERNAL (decl))
18654 add_AT_flag (var_die, DW_AT_declaration, 1);
18655 equate_decl_number_to_die (decl, var_die);
18656 return;
18659 /* If the compiler emitted a definition for the DECL declaration
18660 and if we already emitted a DIE for it, don't emit a second
18661 DIE for it again. Allow re-declarations of DECLs that are
18662 inside functions, though. */
18663 if (old_die && declaration && !local_scope_p (context_die))
18664 return;
18666 /* For static data members, the declaration in the class is supposed
18667 to have DW_TAG_member tag; the specification should still be
18668 DW_TAG_variable referencing the DW_TAG_member DIE. */
18669 if (declaration && class_scope_p (context_die))
18670 var_die = new_die (DW_TAG_member, context_die, decl);
18671 else
18672 var_die = new_die (DW_TAG_variable, context_die, decl);
18674 origin_die = NULL;
18675 if (origin != NULL)
18676 origin_die = add_abstract_origin_attribute (var_die, origin);
18678 /* Loop unrolling can create multiple blocks that refer to the same
18679 static variable, so we must test for the DW_AT_declaration flag.
18681 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18682 copy decls and set the DECL_ABSTRACT flag on them instead of
18683 sharing them.
18685 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18687 ??? The declare_in_namespace support causes us to get two DIEs for one
18688 variable, both of which are declarations. We want to avoid considering
18689 one to be a specification, so we must test that this DIE is not a
18690 declaration. */
18691 else if (old_die && TREE_STATIC (decl) && ! declaration
18692 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18694 /* This is a definition of a C++ class level static. */
18695 add_AT_specification (var_die, old_die);
18696 specialization_p = true;
18697 if (DECL_NAME (decl))
18699 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18700 struct dwarf_file_data * file_index = lookup_filename (s.file);
18702 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18703 add_AT_file (var_die, DW_AT_decl_file, file_index);
18705 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18706 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18708 if (old_die->die_tag == DW_TAG_member)
18709 add_linkage_name (var_die, decl);
18712 else
18713 add_name_and_src_coords_attributes (var_die, decl);
18715 if ((origin == NULL && !specialization_p)
18716 || (origin != NULL
18717 && !DECL_ABSTRACT (decl_or_origin)
18718 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18719 decl_function_context
18720 (decl_or_origin))))
18722 tree type = TREE_TYPE (decl_or_origin);
18724 if (decl_by_reference_p (decl_or_origin))
18725 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18726 else
18727 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18728 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18731 if (origin == NULL && !specialization_p)
18733 if (TREE_PUBLIC (decl))
18734 add_AT_flag (var_die, DW_AT_external, 1);
18736 if (DECL_ARTIFICIAL (decl))
18737 add_AT_flag (var_die, DW_AT_artificial, 1);
18739 add_accessibility_attribute (var_die, decl);
18742 if (declaration)
18743 add_AT_flag (var_die, DW_AT_declaration, 1);
18745 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18746 equate_decl_number_to_die (decl, var_die);
18748 if (! declaration
18749 && (! DECL_ABSTRACT (decl_or_origin)
18750 /* Local static vars are shared between all clones/inlines,
18751 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18752 already set. */
18753 || (TREE_CODE (decl_or_origin) == VAR_DECL
18754 && TREE_STATIC (decl_or_origin)
18755 && DECL_RTL_SET_P (decl_or_origin)))
18756 /* When abstract origin already has DW_AT_location attribute, no need
18757 to add it again. */
18758 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18760 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18761 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18762 defer_location (decl_or_origin, var_die);
18763 else
18764 add_location_or_const_value_attribute (var_die, decl_or_origin,
18765 decl == NULL, DW_AT_location);
18766 add_pubname (decl_or_origin, var_die);
18768 else
18769 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18772 /* Generate a DIE to represent a named constant. */
18774 static void
18775 gen_const_die (tree decl, dw_die_ref context_die)
18777 dw_die_ref const_die;
18778 tree type = TREE_TYPE (decl);
18780 const_die = new_die (DW_TAG_constant, context_die, decl);
18781 add_name_and_src_coords_attributes (const_die, decl);
18782 add_type_attribute (const_die, type, 1, 0, context_die);
18783 if (TREE_PUBLIC (decl))
18784 add_AT_flag (const_die, DW_AT_external, 1);
18785 if (DECL_ARTIFICIAL (decl))
18786 add_AT_flag (const_die, DW_AT_artificial, 1);
18787 tree_add_const_value_attribute_for_decl (const_die, decl);
18790 /* Generate a DIE to represent a label identifier. */
18792 static void
18793 gen_label_die (tree decl, dw_die_ref context_die)
18795 tree origin = decl_ultimate_origin (decl);
18796 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18797 rtx insn;
18798 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18800 if (origin != NULL)
18801 add_abstract_origin_attribute (lbl_die, origin);
18802 else
18803 add_name_and_src_coords_attributes (lbl_die, decl);
18805 if (DECL_ABSTRACT (decl))
18806 equate_decl_number_to_die (decl, lbl_die);
18807 else
18809 insn = DECL_RTL_IF_SET (decl);
18811 /* Deleted labels are programmer specified labels which have been
18812 eliminated because of various optimizations. We still emit them
18813 here so that it is possible to put breakpoints on them. */
18814 if (insn
18815 && (LABEL_P (insn)
18816 || ((NOTE_P (insn)
18817 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18819 /* When optimization is enabled (via -O) some parts of the compiler
18820 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18821 represent source-level labels which were explicitly declared by
18822 the user. This really shouldn't be happening though, so catch
18823 it if it ever does happen. */
18824 gcc_assert (!INSN_DELETED_P (insn));
18826 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18827 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18829 else if (insn
18830 && NOTE_P (insn)
18831 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18832 && CODE_LABEL_NUMBER (insn) != -1)
18834 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18835 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18840 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18841 attributes to the DIE for a block STMT, to describe where the inlined
18842 function was called from. This is similar to add_src_coords_attributes. */
18844 static inline void
18845 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18847 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18849 if (dwarf_version >= 3 || !dwarf_strict)
18851 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18852 add_AT_unsigned (die, DW_AT_call_line, s.line);
18857 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18858 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18860 static inline void
18861 add_high_low_attributes (tree stmt, dw_die_ref die)
18863 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18865 if (BLOCK_FRAGMENT_CHAIN (stmt)
18866 && (dwarf_version >= 3 || !dwarf_strict))
18868 tree chain, superblock = NULL_TREE;
18869 dw_die_ref pdie;
18870 dw_attr_ref attr = NULL;
18872 if (inlined_function_outer_scope_p (stmt))
18874 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18875 BLOCK_NUMBER (stmt));
18876 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18879 /* Optimize duplicate .debug_ranges lists or even tails of
18880 lists. If this BLOCK has same ranges as its supercontext,
18881 lookup DW_AT_ranges attribute in the supercontext (and
18882 recursively so), verify that the ranges_table contains the
18883 right values and use it instead of adding a new .debug_range. */
18884 for (chain = stmt, pdie = die;
18885 BLOCK_SAME_RANGE (chain);
18886 chain = BLOCK_SUPERCONTEXT (chain))
18888 dw_attr_ref new_attr;
18890 pdie = pdie->die_parent;
18891 if (pdie == NULL)
18892 break;
18893 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18894 break;
18895 new_attr = get_AT (pdie, DW_AT_ranges);
18896 if (new_attr == NULL
18897 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18898 break;
18899 attr = new_attr;
18900 superblock = BLOCK_SUPERCONTEXT (chain);
18902 if (attr != NULL
18903 && (ranges_table[attr->dw_attr_val.v.val_offset
18904 / 2 / DWARF2_ADDR_SIZE].num
18905 == BLOCK_NUMBER (superblock))
18906 && BLOCK_FRAGMENT_CHAIN (superblock))
18908 unsigned long off = attr->dw_attr_val.v.val_offset
18909 / 2 / DWARF2_ADDR_SIZE;
18910 unsigned long supercnt = 0, thiscnt = 0;
18911 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18912 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18914 ++supercnt;
18915 gcc_checking_assert (ranges_table[off + supercnt].num
18916 == BLOCK_NUMBER (chain));
18918 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18919 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18920 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18921 ++thiscnt;
18922 gcc_assert (supercnt >= thiscnt);
18923 add_AT_range_list (die, DW_AT_ranges,
18924 ((off + supercnt - thiscnt)
18925 * 2 * DWARF2_ADDR_SIZE),
18926 false);
18927 return;
18930 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18932 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18935 add_ranges (chain);
18936 chain = BLOCK_FRAGMENT_CHAIN (chain);
18938 while (chain);
18939 add_ranges (NULL);
18941 else
18943 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18944 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18945 BLOCK_NUMBER (stmt));
18946 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18947 BLOCK_NUMBER (stmt));
18948 add_AT_low_high_pc (die, label, label_high, false);
18952 /* Generate a DIE for a lexical block. */
18954 static void
18955 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18957 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18959 if (call_arg_locations)
18961 if (block_map.length () <= BLOCK_NUMBER (stmt))
18962 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18963 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18966 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18967 add_high_low_attributes (stmt, stmt_die);
18969 decls_for_scope (stmt, stmt_die, depth);
18972 /* Generate a DIE for an inlined subprogram. */
18974 static void
18975 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18977 tree decl;
18979 /* The instance of function that is effectively being inlined shall not
18980 be abstract. */
18981 gcc_assert (! BLOCK_ABSTRACT (stmt));
18983 decl = block_ultimate_origin (stmt);
18985 /* Emit info for the abstract instance first, if we haven't yet. We
18986 must emit this even if the block is abstract, otherwise when we
18987 emit the block below (or elsewhere), we may end up trying to emit
18988 a die whose origin die hasn't been emitted, and crashing. */
18989 dwarf2out_abstract_function (decl);
18991 if (! BLOCK_ABSTRACT (stmt))
18993 dw_die_ref subr_die
18994 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18996 if (call_arg_locations)
18998 if (block_map.length () <= BLOCK_NUMBER (stmt))
18999 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19000 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19002 add_abstract_origin_attribute (subr_die, decl);
19003 if (TREE_ASM_WRITTEN (stmt))
19004 add_high_low_attributes (stmt, subr_die);
19005 add_call_src_coords_attributes (stmt, subr_die);
19007 decls_for_scope (stmt, subr_die, depth);
19008 current_function_has_inlines = 1;
19012 /* Generate a DIE for a field in a record, or structure. */
19014 static void
19015 gen_field_die (tree decl, dw_die_ref context_die)
19017 dw_die_ref decl_die;
19019 if (TREE_TYPE (decl) == error_mark_node)
19020 return;
19022 decl_die = new_die (DW_TAG_member, context_die, decl);
19023 add_name_and_src_coords_attributes (decl_die, decl);
19024 add_type_attribute (decl_die, member_declared_type (decl),
19025 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19026 context_die);
19028 if (DECL_BIT_FIELD_TYPE (decl))
19030 add_byte_size_attribute (decl_die, decl);
19031 add_bit_size_attribute (decl_die, decl);
19032 add_bit_offset_attribute (decl_die, decl);
19035 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19036 add_data_member_location_attribute (decl_die, decl);
19038 if (DECL_ARTIFICIAL (decl))
19039 add_AT_flag (decl_die, DW_AT_artificial, 1);
19041 add_accessibility_attribute (decl_die, decl);
19043 /* Equate decl number to die, so that we can look up this decl later on. */
19044 equate_decl_number_to_die (decl, decl_die);
19047 #if 0
19048 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19049 Use modified_type_die instead.
19050 We keep this code here just in case these types of DIEs may be needed to
19051 represent certain things in other languages (e.g. Pascal) someday. */
19053 static void
19054 gen_pointer_type_die (tree type, dw_die_ref context_die)
19056 dw_die_ref ptr_die
19057 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19059 equate_type_number_to_die (type, ptr_die);
19060 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19061 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19064 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19065 Use modified_type_die instead.
19066 We keep this code here just in case these types of DIEs may be needed to
19067 represent certain things in other languages (e.g. Pascal) someday. */
19069 static void
19070 gen_reference_type_die (tree type, dw_die_ref context_die)
19072 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19074 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19075 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19076 else
19077 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19079 equate_type_number_to_die (type, ref_die);
19080 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19081 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19083 #endif
19085 /* Generate a DIE for a pointer to a member type. */
19087 static void
19088 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19090 dw_die_ref ptr_die
19091 = new_die (DW_TAG_ptr_to_member_type,
19092 scope_die_for (type, context_die), type);
19094 equate_type_number_to_die (type, ptr_die);
19095 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19096 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19097 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19100 typedef const char *dchar_p; /* For DEF_VEC_P. */
19102 static char *producer_string;
19104 /* Return a heap allocated producer string including command line options
19105 if -grecord-gcc-switches. */
19107 static char *
19108 gen_producer_string (void)
19110 size_t j;
19111 auto_vec<dchar_p> switches;
19112 const char *language_string = lang_hooks.name;
19113 char *producer, *tail;
19114 const char *p;
19115 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19116 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19118 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19119 switch (save_decoded_options[j].opt_index)
19121 case OPT_o:
19122 case OPT_d:
19123 case OPT_dumpbase:
19124 case OPT_dumpdir:
19125 case OPT_auxbase:
19126 case OPT_auxbase_strip:
19127 case OPT_quiet:
19128 case OPT_version:
19129 case OPT_v:
19130 case OPT_w:
19131 case OPT_L:
19132 case OPT_D:
19133 case OPT_I:
19134 case OPT_U:
19135 case OPT_SPECIAL_unknown:
19136 case OPT_SPECIAL_ignore:
19137 case OPT_SPECIAL_program_name:
19138 case OPT_SPECIAL_input_file:
19139 case OPT_grecord_gcc_switches:
19140 case OPT_gno_record_gcc_switches:
19141 case OPT__output_pch_:
19142 case OPT_fdiagnostics_show_location_:
19143 case OPT_fdiagnostics_show_option:
19144 case OPT_fdiagnostics_show_caret:
19145 case OPT_fdiagnostics_color_:
19146 case OPT_fverbose_asm:
19147 case OPT____:
19148 case OPT__sysroot_:
19149 case OPT_nostdinc:
19150 case OPT_nostdinc__:
19151 /* Ignore these. */
19152 continue;
19153 default:
19154 if (cl_options[save_decoded_options[j].opt_index].flags
19155 & CL_NO_DWARF_RECORD)
19156 continue;
19157 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19158 == '-');
19159 switch (save_decoded_options[j].canonical_option[0][1])
19161 case 'M':
19162 case 'i':
19163 case 'W':
19164 continue;
19165 case 'f':
19166 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19167 "dump", 4) == 0)
19168 continue;
19169 break;
19170 default:
19171 break;
19173 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19174 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19175 break;
19178 producer = XNEWVEC (char, plen + 1 + len + 1);
19179 tail = producer;
19180 sprintf (tail, "%s %s", language_string, version_string);
19181 tail += plen;
19183 FOR_EACH_VEC_ELT (switches, j, p)
19185 len = strlen (p);
19186 *tail = ' ';
19187 memcpy (tail + 1, p, len);
19188 tail += len + 1;
19191 *tail = '\0';
19192 return producer;
19195 /* Generate the DIE for the compilation unit. */
19197 static dw_die_ref
19198 gen_compile_unit_die (const char *filename)
19200 dw_die_ref die;
19201 const char *language_string = lang_hooks.name;
19202 int language;
19204 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19206 if (filename)
19208 add_name_attribute (die, filename);
19209 /* Don't add cwd for <built-in>. */
19210 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19211 add_comp_dir_attribute (die);
19214 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19216 /* If our producer is LTO try to figure out a common language to use
19217 from the global list of translation units. */
19218 if (strcmp (language_string, "GNU GIMPLE") == 0)
19220 unsigned i;
19221 tree t;
19222 const char *common_lang = NULL;
19224 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19226 if (!TRANSLATION_UNIT_LANGUAGE (t))
19227 continue;
19228 if (!common_lang)
19229 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19230 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19232 else if (strncmp (common_lang, "GNU C", 5) == 0
19233 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19234 /* Mixing C and C++ is ok, use C++ in that case. */
19235 common_lang = "GNU C++";
19236 else
19238 /* Fall back to C. */
19239 common_lang = NULL;
19240 break;
19244 if (common_lang)
19245 language_string = common_lang;
19248 language = DW_LANG_C89;
19249 if (strcmp (language_string, "GNU C++") == 0)
19250 language = DW_LANG_C_plus_plus;
19251 else if (strcmp (language_string, "GNU F77") == 0)
19252 language = DW_LANG_Fortran77;
19253 else if (strcmp (language_string, "GNU Pascal") == 0)
19254 language = DW_LANG_Pascal83;
19255 else if (dwarf_version >= 3 || !dwarf_strict)
19257 if (strcmp (language_string, "GNU Ada") == 0)
19258 language = DW_LANG_Ada95;
19259 else if (strcmp (language_string, "GNU Fortran") == 0)
19260 language = DW_LANG_Fortran95;
19261 else if (strcmp (language_string, "GNU Java") == 0)
19262 language = DW_LANG_Java;
19263 else if (strcmp (language_string, "GNU Objective-C") == 0)
19264 language = DW_LANG_ObjC;
19265 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19266 language = DW_LANG_ObjC_plus_plus;
19267 else if (dwarf_version >= 5 || !dwarf_strict)
19269 if (strcmp (language_string, "GNU Go") == 0)
19270 language = DW_LANG_Go;
19273 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19274 else if (strcmp (language_string, "GNU Fortran") == 0)
19275 language = DW_LANG_Fortran90;
19277 add_AT_unsigned (die, DW_AT_language, language);
19279 switch (language)
19281 case DW_LANG_Fortran77:
19282 case DW_LANG_Fortran90:
19283 case DW_LANG_Fortran95:
19284 /* Fortran has case insensitive identifiers and the front-end
19285 lowercases everything. */
19286 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19287 break;
19288 default:
19289 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19290 break;
19292 return die;
19295 /* Generate the DIE for a base class. */
19297 static void
19298 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19300 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19302 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19303 add_data_member_location_attribute (die, binfo);
19305 if (BINFO_VIRTUAL_P (binfo))
19306 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19308 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19309 children, otherwise the default is DW_ACCESS_public. In DWARF2
19310 the default has always been DW_ACCESS_private. */
19311 if (access == access_public_node)
19313 if (dwarf_version == 2
19314 || context_die->die_tag == DW_TAG_class_type)
19315 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19317 else if (access == access_protected_node)
19318 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19319 else if (dwarf_version > 2
19320 && context_die->die_tag != DW_TAG_class_type)
19321 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19324 /* Generate a DIE for a class member. */
19326 static void
19327 gen_member_die (tree type, dw_die_ref context_die)
19329 tree member;
19330 tree binfo = TYPE_BINFO (type);
19331 dw_die_ref child;
19333 /* If this is not an incomplete type, output descriptions of each of its
19334 members. Note that as we output the DIEs necessary to represent the
19335 members of this record or union type, we will also be trying to output
19336 DIEs to represent the *types* of those members. However the `type'
19337 function (above) will specifically avoid generating type DIEs for member
19338 types *within* the list of member DIEs for this (containing) type except
19339 for those types (of members) which are explicitly marked as also being
19340 members of this (containing) type themselves. The g++ front- end can
19341 force any given type to be treated as a member of some other (containing)
19342 type by setting the TYPE_CONTEXT of the given (member) type to point to
19343 the TREE node representing the appropriate (containing) type. */
19345 /* First output info about the base classes. */
19346 if (binfo)
19348 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19349 int i;
19350 tree base;
19352 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19353 gen_inheritance_die (base,
19354 (accesses ? (*accesses)[i] : access_public_node),
19355 context_die);
19358 /* Now output info about the data members and type members. */
19359 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19361 /* If we thought we were generating minimal debug info for TYPE
19362 and then changed our minds, some of the member declarations
19363 may have already been defined. Don't define them again, but
19364 do put them in the right order. */
19366 child = lookup_decl_die (member);
19367 if (child)
19368 splice_child_die (context_die, child);
19369 else
19370 gen_decl_die (member, NULL, context_die);
19373 /* Now output info about the function members (if any). */
19374 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19376 /* Don't include clones in the member list. */
19377 if (DECL_ABSTRACT_ORIGIN (member))
19378 continue;
19380 child = lookup_decl_die (member);
19381 if (child)
19382 splice_child_die (context_die, child);
19383 else
19384 gen_decl_die (member, NULL, context_die);
19388 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19389 is set, we pretend that the type was never defined, so we only get the
19390 member DIEs needed by later specification DIEs. */
19392 static void
19393 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19394 enum debug_info_usage usage)
19396 dw_die_ref type_die = lookup_type_die (type);
19397 dw_die_ref scope_die = 0;
19398 int nested = 0;
19399 int complete = (TYPE_SIZE (type)
19400 && (! TYPE_STUB_DECL (type)
19401 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19402 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19403 complete = complete && should_emit_struct_debug (type, usage);
19405 if (type_die && ! complete)
19406 return;
19408 if (TYPE_CONTEXT (type) != NULL_TREE
19409 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19410 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19411 nested = 1;
19413 scope_die = scope_die_for (type, context_die);
19415 /* Generate child dies for template paramaters. */
19416 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19417 schedule_generic_params_dies_gen (type);
19419 if (! type_die || (nested && is_cu_die (scope_die)))
19420 /* First occurrence of type or toplevel definition of nested class. */
19422 dw_die_ref old_die = type_die;
19424 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19425 ? record_type_tag (type) : DW_TAG_union_type,
19426 scope_die, type);
19427 equate_type_number_to_die (type, type_die);
19428 if (old_die)
19429 add_AT_specification (type_die, old_die);
19430 else
19431 add_name_attribute (type_die, type_tag (type));
19433 else
19434 remove_AT (type_die, DW_AT_declaration);
19436 /* If this type has been completed, then give it a byte_size attribute and
19437 then give a list of members. */
19438 if (complete && !ns_decl)
19440 /* Prevent infinite recursion in cases where the type of some member of
19441 this type is expressed in terms of this type itself. */
19442 TREE_ASM_WRITTEN (type) = 1;
19443 add_byte_size_attribute (type_die, type);
19444 if (TYPE_STUB_DECL (type) != NULL_TREE)
19446 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19447 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19450 /* If the first reference to this type was as the return type of an
19451 inline function, then it may not have a parent. Fix this now. */
19452 if (type_die->die_parent == NULL)
19453 add_child_die (scope_die, type_die);
19455 push_decl_scope (type);
19456 gen_member_die (type, type_die);
19457 pop_decl_scope ();
19459 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19460 if (TYPE_ARTIFICIAL (type))
19461 add_AT_flag (type_die, DW_AT_artificial, 1);
19463 /* GNU extension: Record what type our vtable lives in. */
19464 if (TYPE_VFIELD (type))
19466 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19468 gen_type_die (vtype, context_die);
19469 add_AT_die_ref (type_die, DW_AT_containing_type,
19470 lookup_type_die (vtype));
19473 else
19475 add_AT_flag (type_die, DW_AT_declaration, 1);
19477 /* We don't need to do this for function-local types. */
19478 if (TYPE_STUB_DECL (type)
19479 && ! decl_function_context (TYPE_STUB_DECL (type)))
19480 vec_safe_push (incomplete_types, type);
19483 if (get_AT (type_die, DW_AT_name))
19484 add_pubtype (type, type_die);
19487 /* Generate a DIE for a subroutine _type_. */
19489 static void
19490 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19492 tree return_type = TREE_TYPE (type);
19493 dw_die_ref subr_die
19494 = new_die (DW_TAG_subroutine_type,
19495 scope_die_for (type, context_die), type);
19497 equate_type_number_to_die (type, subr_die);
19498 add_prototyped_attribute (subr_die, type);
19499 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19500 gen_formal_types_die (type, subr_die);
19502 if (get_AT (subr_die, DW_AT_name))
19503 add_pubtype (type, subr_die);
19506 /* Generate a DIE for a type definition. */
19508 static void
19509 gen_typedef_die (tree decl, dw_die_ref context_die)
19511 dw_die_ref type_die;
19512 tree origin;
19514 if (TREE_ASM_WRITTEN (decl))
19515 return;
19517 TREE_ASM_WRITTEN (decl) = 1;
19518 type_die = new_die (DW_TAG_typedef, context_die, decl);
19519 origin = decl_ultimate_origin (decl);
19520 if (origin != NULL)
19521 add_abstract_origin_attribute (type_die, origin);
19522 else
19524 tree type;
19526 add_name_and_src_coords_attributes (type_die, decl);
19527 if (DECL_ORIGINAL_TYPE (decl))
19529 type = DECL_ORIGINAL_TYPE (decl);
19531 gcc_assert (type != TREE_TYPE (decl));
19532 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19534 else
19536 type = TREE_TYPE (decl);
19538 if (is_naming_typedef_decl (TYPE_NAME (type)))
19540 /* Here, we are in the case of decl being a typedef naming
19541 an anonymous type, e.g:
19542 typedef struct {...} foo;
19543 In that case TREE_TYPE (decl) is not a typedef variant
19544 type and TYPE_NAME of the anonymous type is set to the
19545 TYPE_DECL of the typedef. This construct is emitted by
19546 the C++ FE.
19548 TYPE is the anonymous struct named by the typedef
19549 DECL. As we need the DW_AT_type attribute of the
19550 DW_TAG_typedef to point to the DIE of TYPE, let's
19551 generate that DIE right away. add_type_attribute
19552 called below will then pick (via lookup_type_die) that
19553 anonymous struct DIE. */
19554 if (!TREE_ASM_WRITTEN (type))
19555 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19557 /* This is a GNU Extension. We are adding a
19558 DW_AT_linkage_name attribute to the DIE of the
19559 anonymous struct TYPE. The value of that attribute
19560 is the name of the typedef decl naming the anonymous
19561 struct. This greatly eases the work of consumers of
19562 this debug info. */
19563 add_linkage_attr (lookup_type_die (type), decl);
19567 add_type_attribute (type_die, type, TREE_READONLY (decl),
19568 TREE_THIS_VOLATILE (decl), context_die);
19570 if (is_naming_typedef_decl (decl))
19571 /* We want that all subsequent calls to lookup_type_die with
19572 TYPE in argument yield the DW_TAG_typedef we have just
19573 created. */
19574 equate_type_number_to_die (type, type_die);
19576 add_accessibility_attribute (type_die, decl);
19579 if (DECL_ABSTRACT (decl))
19580 equate_decl_number_to_die (decl, type_die);
19582 if (get_AT (type_die, DW_AT_name))
19583 add_pubtype (decl, type_die);
19586 /* Generate a DIE for a struct, class, enum or union type. */
19588 static void
19589 gen_tagged_type_die (tree type,
19590 dw_die_ref context_die,
19591 enum debug_info_usage usage)
19593 int need_pop;
19595 if (type == NULL_TREE
19596 || !is_tagged_type (type))
19597 return;
19599 /* If this is a nested type whose containing class hasn't been written
19600 out yet, writing it out will cover this one, too. This does not apply
19601 to instantiations of member class templates; they need to be added to
19602 the containing class as they are generated. FIXME: This hurts the
19603 idea of combining type decls from multiple TUs, since we can't predict
19604 what set of template instantiations we'll get. */
19605 if (TYPE_CONTEXT (type)
19606 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19607 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19609 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19611 if (TREE_ASM_WRITTEN (type))
19612 return;
19614 /* If that failed, attach ourselves to the stub. */
19615 push_decl_scope (TYPE_CONTEXT (type));
19616 context_die = lookup_type_die (TYPE_CONTEXT (type));
19617 need_pop = 1;
19619 else if (TYPE_CONTEXT (type) != NULL_TREE
19620 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19622 /* If this type is local to a function that hasn't been written
19623 out yet, use a NULL context for now; it will be fixed up in
19624 decls_for_scope. */
19625 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19626 /* A declaration DIE doesn't count; nested types need to go in the
19627 specification. */
19628 if (context_die && is_declaration_die (context_die))
19629 context_die = NULL;
19630 need_pop = 0;
19632 else
19634 context_die = declare_in_namespace (type, context_die);
19635 need_pop = 0;
19638 if (TREE_CODE (type) == ENUMERAL_TYPE)
19640 /* This might have been written out by the call to
19641 declare_in_namespace. */
19642 if (!TREE_ASM_WRITTEN (type))
19643 gen_enumeration_type_die (type, context_die);
19645 else
19646 gen_struct_or_union_type_die (type, context_die, usage);
19648 if (need_pop)
19649 pop_decl_scope ();
19651 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19652 it up if it is ever completed. gen_*_type_die will set it for us
19653 when appropriate. */
19656 /* Generate a type description DIE. */
19658 static void
19659 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19660 enum debug_info_usage usage)
19662 struct array_descr_info info;
19664 if (type == NULL_TREE || type == error_mark_node)
19665 return;
19667 if (TYPE_NAME (type) != NULL_TREE
19668 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19669 && is_redundant_typedef (TYPE_NAME (type))
19670 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19671 /* The DECL of this type is a typedef we don't want to emit debug
19672 info for but we want debug info for its underlying typedef.
19673 This can happen for e.g, the injected-class-name of a C++
19674 type. */
19675 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19677 /* If TYPE is a typedef type variant, let's generate debug info
19678 for the parent typedef which TYPE is a type of. */
19679 if (typedef_variant_p (type))
19681 if (TREE_ASM_WRITTEN (type))
19682 return;
19684 /* Prevent broken recursion; we can't hand off to the same type. */
19685 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19687 /* Give typedefs the right scope. */
19688 context_die = scope_die_for (type, context_die);
19690 TREE_ASM_WRITTEN (type) = 1;
19692 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19693 return;
19696 /* If type is an anonymous tagged type named by a typedef, let's
19697 generate debug info for the typedef. */
19698 if (is_naming_typedef_decl (TYPE_NAME (type)))
19700 /* Use the DIE of the containing namespace as the parent DIE of
19701 the type description DIE we want to generate. */
19702 if (DECL_CONTEXT (TYPE_NAME (type))
19703 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19704 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19706 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19707 return;
19710 /* If this is an array type with hidden descriptor, handle it first. */
19711 if (!TREE_ASM_WRITTEN (type)
19712 && lang_hooks.types.get_array_descr_info
19713 && lang_hooks.types.get_array_descr_info (type, &info)
19714 && (dwarf_version >= 3 || !dwarf_strict))
19716 gen_descr_array_type_die (type, &info, context_die);
19717 TREE_ASM_WRITTEN (type) = 1;
19718 return;
19721 /* We are going to output a DIE to represent the unqualified version
19722 of this type (i.e. without any const or volatile qualifiers) so
19723 get the main variant (i.e. the unqualified version) of this type
19724 now. (Vectors are special because the debugging info is in the
19725 cloned type itself). */
19726 if (TREE_CODE (type) != VECTOR_TYPE)
19727 type = type_main_variant (type);
19729 if (TREE_ASM_WRITTEN (type))
19730 return;
19732 switch (TREE_CODE (type))
19734 case ERROR_MARK:
19735 break;
19737 case POINTER_TYPE:
19738 case REFERENCE_TYPE:
19739 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19740 ensures that the gen_type_die recursion will terminate even if the
19741 type is recursive. Recursive types are possible in Ada. */
19742 /* ??? We could perhaps do this for all types before the switch
19743 statement. */
19744 TREE_ASM_WRITTEN (type) = 1;
19746 /* For these types, all that is required is that we output a DIE (or a
19747 set of DIEs) to represent the "basis" type. */
19748 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19749 DINFO_USAGE_IND_USE);
19750 break;
19752 case OFFSET_TYPE:
19753 /* This code is used for C++ pointer-to-data-member types.
19754 Output a description of the relevant class type. */
19755 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19756 DINFO_USAGE_IND_USE);
19758 /* Output a description of the type of the object pointed to. */
19759 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19760 DINFO_USAGE_IND_USE);
19762 /* Now output a DIE to represent this pointer-to-data-member type
19763 itself. */
19764 gen_ptr_to_mbr_type_die (type, context_die);
19765 break;
19767 case FUNCTION_TYPE:
19768 /* Force out return type (in case it wasn't forced out already). */
19769 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19770 DINFO_USAGE_DIR_USE);
19771 gen_subroutine_type_die (type, context_die);
19772 break;
19774 case METHOD_TYPE:
19775 /* Force out return type (in case it wasn't forced out already). */
19776 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19777 DINFO_USAGE_DIR_USE);
19778 gen_subroutine_type_die (type, context_die);
19779 break;
19781 case ARRAY_TYPE:
19782 gen_array_type_die (type, context_die);
19783 break;
19785 case VECTOR_TYPE:
19786 gen_array_type_die (type, context_die);
19787 break;
19789 case ENUMERAL_TYPE:
19790 case RECORD_TYPE:
19791 case UNION_TYPE:
19792 case QUAL_UNION_TYPE:
19793 gen_tagged_type_die (type, context_die, usage);
19794 return;
19796 case VOID_TYPE:
19797 case INTEGER_TYPE:
19798 case REAL_TYPE:
19799 case FIXED_POINT_TYPE:
19800 case COMPLEX_TYPE:
19801 case BOOLEAN_TYPE:
19802 /* No DIEs needed for fundamental types. */
19803 break;
19805 case NULLPTR_TYPE:
19806 case LANG_TYPE:
19807 /* Just use DW_TAG_unspecified_type. */
19809 dw_die_ref type_die = lookup_type_die (type);
19810 if (type_die == NULL)
19812 tree name = TYPE_NAME (type);
19813 if (TREE_CODE (name) == TYPE_DECL)
19814 name = DECL_NAME (name);
19815 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19816 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19817 equate_type_number_to_die (type, type_die);
19820 break;
19822 default:
19823 gcc_unreachable ();
19826 TREE_ASM_WRITTEN (type) = 1;
19829 static void
19830 gen_type_die (tree type, dw_die_ref context_die)
19832 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19835 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19836 things which are local to the given block. */
19838 static void
19839 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19841 int must_output_die = 0;
19842 bool inlined_func;
19844 /* Ignore blocks that are NULL. */
19845 if (stmt == NULL_TREE)
19846 return;
19848 inlined_func = inlined_function_outer_scope_p (stmt);
19850 /* If the block is one fragment of a non-contiguous block, do not
19851 process the variables, since they will have been done by the
19852 origin block. Do process subblocks. */
19853 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19855 tree sub;
19857 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19858 gen_block_die (sub, context_die, depth + 1);
19860 return;
19863 /* Determine if we need to output any Dwarf DIEs at all to represent this
19864 block. */
19865 if (inlined_func)
19866 /* The outer scopes for inlinings *must* always be represented. We
19867 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19868 must_output_die = 1;
19869 else
19871 /* Determine if this block directly contains any "significant"
19872 local declarations which we will need to output DIEs for. */
19873 if (debug_info_level > DINFO_LEVEL_TERSE)
19874 /* We are not in terse mode so *any* local declaration counts
19875 as being a "significant" one. */
19876 must_output_die = ((BLOCK_VARS (stmt) != NULL
19877 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19878 && (TREE_USED (stmt)
19879 || TREE_ASM_WRITTEN (stmt)
19880 || BLOCK_ABSTRACT (stmt)));
19881 else if ((TREE_USED (stmt)
19882 || TREE_ASM_WRITTEN (stmt)
19883 || BLOCK_ABSTRACT (stmt))
19884 && !dwarf2out_ignore_block (stmt))
19885 must_output_die = 1;
19888 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19889 DIE for any block which contains no significant local declarations at
19890 all. Rather, in such cases we just call `decls_for_scope' so that any
19891 needed Dwarf info for any sub-blocks will get properly generated. Note
19892 that in terse mode, our definition of what constitutes a "significant"
19893 local declaration gets restricted to include only inlined function
19894 instances and local (nested) function definitions. */
19895 if (must_output_die)
19897 if (inlined_func)
19899 /* If STMT block is abstract, that means we have been called
19900 indirectly from dwarf2out_abstract_function.
19901 That function rightfully marks the descendent blocks (of
19902 the abstract function it is dealing with) as being abstract,
19903 precisely to prevent us from emitting any
19904 DW_TAG_inlined_subroutine DIE as a descendent
19905 of an abstract function instance. So in that case, we should
19906 not call gen_inlined_subroutine_die.
19908 Later though, when cgraph asks dwarf2out to emit info
19909 for the concrete instance of the function decl into which
19910 the concrete instance of STMT got inlined, the later will lead
19911 to the generation of a DW_TAG_inlined_subroutine DIE. */
19912 if (! BLOCK_ABSTRACT (stmt))
19913 gen_inlined_subroutine_die (stmt, context_die, depth);
19915 else
19916 gen_lexical_block_die (stmt, context_die, depth);
19918 else
19919 decls_for_scope (stmt, context_die, depth);
19922 /* Process variable DECL (or variable with origin ORIGIN) within
19923 block STMT and add it to CONTEXT_DIE. */
19924 static void
19925 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19927 dw_die_ref die;
19928 tree decl_or_origin = decl ? decl : origin;
19930 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19931 die = lookup_decl_die (decl_or_origin);
19932 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19933 && TYPE_DECL_IS_STUB (decl_or_origin))
19934 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19935 else
19936 die = NULL;
19938 if (die != NULL && die->die_parent == NULL)
19939 add_child_die (context_die, die);
19940 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19941 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19942 stmt, context_die);
19943 else
19944 gen_decl_die (decl, origin, context_die);
19947 /* Generate all of the decls declared within a given scope and (recursively)
19948 all of its sub-blocks. */
19950 static void
19951 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19953 tree decl;
19954 unsigned int i;
19955 tree subblocks;
19957 /* Ignore NULL blocks. */
19958 if (stmt == NULL_TREE)
19959 return;
19961 /* Output the DIEs to represent all of the data objects and typedefs
19962 declared directly within this block but not within any nested
19963 sub-blocks. Also, nested function and tag DIEs have been
19964 generated with a parent of NULL; fix that up now. We don't
19965 have to do this if we're at -g1. */
19966 if (debug_info_level > DINFO_LEVEL_TERSE)
19968 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19969 process_scope_var (stmt, decl, NULL_TREE, context_die);
19970 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19971 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19972 context_die);
19975 /* Even if we're at -g1, we need to process the subblocks in order to get
19976 inlined call information. */
19978 /* Output the DIEs to represent all sub-blocks (and the items declared
19979 therein) of this block. */
19980 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19981 subblocks != NULL;
19982 subblocks = BLOCK_CHAIN (subblocks))
19983 gen_block_die (subblocks, context_die, depth + 1);
19986 /* Is this a typedef we can avoid emitting? */
19988 static inline int
19989 is_redundant_typedef (const_tree decl)
19991 if (TYPE_DECL_IS_STUB (decl))
19992 return 1;
19994 if (DECL_ARTIFICIAL (decl)
19995 && DECL_CONTEXT (decl)
19996 && is_tagged_type (DECL_CONTEXT (decl))
19997 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19998 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19999 /* Also ignore the artificial member typedef for the class name. */
20000 return 1;
20002 return 0;
20005 /* Return TRUE if TYPE is a typedef that names a type for linkage
20006 purposes. This kind of typedefs is produced by the C++ FE for
20007 constructs like:
20009 typedef struct {...} foo;
20011 In that case, there is no typedef variant type produced for foo.
20012 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20013 struct type. */
20015 static bool
20016 is_naming_typedef_decl (const_tree decl)
20018 if (decl == NULL_TREE
20019 || TREE_CODE (decl) != TYPE_DECL
20020 || !is_tagged_type (TREE_TYPE (decl))
20021 || DECL_IS_BUILTIN (decl)
20022 || is_redundant_typedef (decl)
20023 /* It looks like Ada produces TYPE_DECLs that are very similar
20024 to C++ naming typedefs but that have different
20025 semantics. Let's be specific to c++ for now. */
20026 || !is_cxx ())
20027 return FALSE;
20029 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20030 && TYPE_NAME (TREE_TYPE (decl)) == decl
20031 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20032 != TYPE_NAME (TREE_TYPE (decl))));
20035 /* Returns the DIE for a context. */
20037 static inline dw_die_ref
20038 get_context_die (tree context)
20040 if (context)
20042 /* Find die that represents this context. */
20043 if (TYPE_P (context))
20045 context = TYPE_MAIN_VARIANT (context);
20046 return strip_naming_typedef (context, force_type_die (context));
20048 else
20049 return force_decl_die (context);
20051 return comp_unit_die ();
20054 /* Returns the DIE for decl. A DIE will always be returned. */
20056 static dw_die_ref
20057 force_decl_die (tree decl)
20059 dw_die_ref decl_die;
20060 unsigned saved_external_flag;
20061 tree save_fn = NULL_TREE;
20062 decl_die = lookup_decl_die (decl);
20063 if (!decl_die)
20065 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20067 decl_die = lookup_decl_die (decl);
20068 if (decl_die)
20069 return decl_die;
20071 switch (TREE_CODE (decl))
20073 case FUNCTION_DECL:
20074 /* Clear current_function_decl, so that gen_subprogram_die thinks
20075 that this is a declaration. At this point, we just want to force
20076 declaration die. */
20077 save_fn = current_function_decl;
20078 current_function_decl = NULL_TREE;
20079 gen_subprogram_die (decl, context_die);
20080 current_function_decl = save_fn;
20081 break;
20083 case VAR_DECL:
20084 /* Set external flag to force declaration die. Restore it after
20085 gen_decl_die() call. */
20086 saved_external_flag = DECL_EXTERNAL (decl);
20087 DECL_EXTERNAL (decl) = 1;
20088 gen_decl_die (decl, NULL, context_die);
20089 DECL_EXTERNAL (decl) = saved_external_flag;
20090 break;
20092 case NAMESPACE_DECL:
20093 if (dwarf_version >= 3 || !dwarf_strict)
20094 dwarf2out_decl (decl);
20095 else
20096 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20097 decl_die = comp_unit_die ();
20098 break;
20100 case TRANSLATION_UNIT_DECL:
20101 decl_die = comp_unit_die ();
20102 break;
20104 default:
20105 gcc_unreachable ();
20108 /* We should be able to find the DIE now. */
20109 if (!decl_die)
20110 decl_die = lookup_decl_die (decl);
20111 gcc_assert (decl_die);
20114 return decl_die;
20117 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20118 always returned. */
20120 static dw_die_ref
20121 force_type_die (tree type)
20123 dw_die_ref type_die;
20125 type_die = lookup_type_die (type);
20126 if (!type_die)
20128 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20130 type_die = modified_type_die (type, TYPE_READONLY (type),
20131 TYPE_VOLATILE (type), context_die);
20132 gcc_assert (type_die);
20134 return type_die;
20137 /* Force out any required namespaces to be able to output DECL,
20138 and return the new context_die for it, if it's changed. */
20140 static dw_die_ref
20141 setup_namespace_context (tree thing, dw_die_ref context_die)
20143 tree context = (DECL_P (thing)
20144 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20145 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20146 /* Force out the namespace. */
20147 context_die = force_decl_die (context);
20149 return context_die;
20152 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20153 type) within its namespace, if appropriate.
20155 For compatibility with older debuggers, namespace DIEs only contain
20156 declarations; all definitions are emitted at CU scope. */
20158 static dw_die_ref
20159 declare_in_namespace (tree thing, dw_die_ref context_die)
20161 dw_die_ref ns_context;
20163 if (debug_info_level <= DINFO_LEVEL_TERSE)
20164 return context_die;
20166 /* If this decl is from an inlined function, then don't try to emit it in its
20167 namespace, as we will get confused. It would have already been emitted
20168 when the abstract instance of the inline function was emitted anyways. */
20169 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20170 return context_die;
20172 ns_context = setup_namespace_context (thing, context_die);
20174 if (ns_context != context_die)
20176 if (is_fortran ())
20177 return ns_context;
20178 if (DECL_P (thing))
20179 gen_decl_die (thing, NULL, ns_context);
20180 else
20181 gen_type_die (thing, ns_context);
20183 return context_die;
20186 /* Generate a DIE for a namespace or namespace alias. */
20188 static void
20189 gen_namespace_die (tree decl, dw_die_ref context_die)
20191 dw_die_ref namespace_die;
20193 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20194 they are an alias of. */
20195 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20197 /* Output a real namespace or module. */
20198 context_die = setup_namespace_context (decl, comp_unit_die ());
20199 namespace_die = new_die (is_fortran ()
20200 ? DW_TAG_module : DW_TAG_namespace,
20201 context_die, decl);
20202 /* For Fortran modules defined in different CU don't add src coords. */
20203 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20205 const char *name = dwarf2_name (decl, 0);
20206 if (name)
20207 add_name_attribute (namespace_die, name);
20209 else
20210 add_name_and_src_coords_attributes (namespace_die, decl);
20211 if (DECL_EXTERNAL (decl))
20212 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20213 equate_decl_number_to_die (decl, namespace_die);
20215 else
20217 /* Output a namespace alias. */
20219 /* Force out the namespace we are an alias of, if necessary. */
20220 dw_die_ref origin_die
20221 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20223 if (DECL_FILE_SCOPE_P (decl)
20224 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20225 context_die = setup_namespace_context (decl, comp_unit_die ());
20226 /* Now create the namespace alias DIE. */
20227 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20228 add_name_and_src_coords_attributes (namespace_die, decl);
20229 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20230 equate_decl_number_to_die (decl, namespace_die);
20232 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20233 if (want_pubnames ())
20234 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20237 /* Generate Dwarf debug information for a decl described by DECL.
20238 The return value is currently only meaningful for PARM_DECLs,
20239 for all other decls it returns NULL. */
20241 static dw_die_ref
20242 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20244 tree decl_or_origin = decl ? decl : origin;
20245 tree class_origin = NULL, ultimate_origin;
20247 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20248 return NULL;
20250 switch (TREE_CODE (decl_or_origin))
20252 case ERROR_MARK:
20253 break;
20255 case CONST_DECL:
20256 if (!is_fortran () && !is_ada ())
20258 /* The individual enumerators of an enum type get output when we output
20259 the Dwarf representation of the relevant enum type itself. */
20260 break;
20263 /* Emit its type. */
20264 gen_type_die (TREE_TYPE (decl), context_die);
20266 /* And its containing namespace. */
20267 context_die = declare_in_namespace (decl, context_die);
20269 gen_const_die (decl, context_die);
20270 break;
20272 case FUNCTION_DECL:
20273 /* Don't output any DIEs to represent mere function declarations,
20274 unless they are class members or explicit block externs. */
20275 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20276 && DECL_FILE_SCOPE_P (decl_or_origin)
20277 && (current_function_decl == NULL_TREE
20278 || DECL_ARTIFICIAL (decl_or_origin)))
20279 break;
20281 #if 0
20282 /* FIXME */
20283 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20284 on local redeclarations of global functions. That seems broken. */
20285 if (current_function_decl != decl)
20286 /* This is only a declaration. */;
20287 #endif
20289 /* If we're emitting a clone, emit info for the abstract instance. */
20290 if (origin || DECL_ORIGIN (decl) != decl)
20291 dwarf2out_abstract_function (origin
20292 ? DECL_ORIGIN (origin)
20293 : DECL_ABSTRACT_ORIGIN (decl));
20295 /* If we're emitting an out-of-line copy of an inline function,
20296 emit info for the abstract instance and set up to refer to it. */
20297 else if (cgraph_function_possibly_inlined_p (decl)
20298 && ! DECL_ABSTRACT (decl)
20299 && ! class_or_namespace_scope_p (context_die)
20300 /* dwarf2out_abstract_function won't emit a die if this is just
20301 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20302 that case, because that works only if we have a die. */
20303 && DECL_INITIAL (decl) != NULL_TREE)
20305 dwarf2out_abstract_function (decl);
20306 set_decl_origin_self (decl);
20309 /* Otherwise we're emitting the primary DIE for this decl. */
20310 else if (debug_info_level > DINFO_LEVEL_TERSE)
20312 /* Before we describe the FUNCTION_DECL itself, make sure that we
20313 have its containing type. */
20314 if (!origin)
20315 origin = decl_class_context (decl);
20316 if (origin != NULL_TREE)
20317 gen_type_die (origin, context_die);
20319 /* And its return type. */
20320 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20322 /* And its virtual context. */
20323 if (DECL_VINDEX (decl) != NULL_TREE)
20324 gen_type_die (DECL_CONTEXT (decl), context_die);
20326 /* Make sure we have a member DIE for decl. */
20327 if (origin != NULL_TREE)
20328 gen_type_die_for_member (origin, decl, context_die);
20330 /* And its containing namespace. */
20331 context_die = declare_in_namespace (decl, context_die);
20334 /* Now output a DIE to represent the function itself. */
20335 if (decl)
20336 gen_subprogram_die (decl, context_die);
20337 break;
20339 case TYPE_DECL:
20340 /* If we are in terse mode, don't generate any DIEs to represent any
20341 actual typedefs. */
20342 if (debug_info_level <= DINFO_LEVEL_TERSE)
20343 break;
20345 /* In the special case of a TYPE_DECL node representing the declaration
20346 of some type tag, if the given TYPE_DECL is marked as having been
20347 instantiated from some other (original) TYPE_DECL node (e.g. one which
20348 was generated within the original definition of an inline function) we
20349 used to generate a special (abbreviated) DW_TAG_structure_type,
20350 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20351 should be actually referencing those DIEs, as variable DIEs with that
20352 type would be emitted already in the abstract origin, so it was always
20353 removed during unused type prunning. Don't add anything in this
20354 case. */
20355 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20356 break;
20358 if (is_redundant_typedef (decl))
20359 gen_type_die (TREE_TYPE (decl), context_die);
20360 else
20361 /* Output a DIE to represent the typedef itself. */
20362 gen_typedef_die (decl, context_die);
20363 break;
20365 case LABEL_DECL:
20366 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20367 gen_label_die (decl, context_die);
20368 break;
20370 case VAR_DECL:
20371 case RESULT_DECL:
20372 /* If we are in terse mode, don't generate any DIEs to represent any
20373 variable declarations or definitions. */
20374 if (debug_info_level <= DINFO_LEVEL_TERSE)
20375 break;
20377 /* Output any DIEs that are needed to specify the type of this data
20378 object. */
20379 if (decl_by_reference_p (decl_or_origin))
20380 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20381 else
20382 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20384 /* And its containing type. */
20385 class_origin = decl_class_context (decl_or_origin);
20386 if (class_origin != NULL_TREE)
20387 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20389 /* And its containing namespace. */
20390 context_die = declare_in_namespace (decl_or_origin, context_die);
20392 /* Now output the DIE to represent the data object itself. This gets
20393 complicated because of the possibility that the VAR_DECL really
20394 represents an inlined instance of a formal parameter for an inline
20395 function. */
20396 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20397 if (ultimate_origin != NULL_TREE
20398 && TREE_CODE (ultimate_origin) == PARM_DECL)
20399 gen_formal_parameter_die (decl, origin,
20400 true /* Emit name attribute. */,
20401 context_die);
20402 else
20403 gen_variable_die (decl, origin, context_die);
20404 break;
20406 case FIELD_DECL:
20407 /* Ignore the nameless fields that are used to skip bits but handle C++
20408 anonymous unions and structs. */
20409 if (DECL_NAME (decl) != NULL_TREE
20410 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20411 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20413 gen_type_die (member_declared_type (decl), context_die);
20414 gen_field_die (decl, context_die);
20416 break;
20418 case PARM_DECL:
20419 if (DECL_BY_REFERENCE (decl_or_origin))
20420 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20421 else
20422 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20423 return gen_formal_parameter_die (decl, origin,
20424 true /* Emit name attribute. */,
20425 context_die);
20427 case NAMESPACE_DECL:
20428 case IMPORTED_DECL:
20429 if (dwarf_version >= 3 || !dwarf_strict)
20430 gen_namespace_die (decl, context_die);
20431 break;
20433 case NAMELIST_DECL:
20434 gen_namelist_decl (DECL_NAME (decl), context_die,
20435 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20436 break;
20438 default:
20439 /* Probably some frontend-internal decl. Assume we don't care. */
20440 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20441 break;
20444 return NULL;
20447 /* Output debug information for global decl DECL. Called from toplev.c after
20448 compilation proper has finished. */
20450 static void
20451 dwarf2out_global_decl (tree decl)
20453 /* Output DWARF2 information for file-scope tentative data object
20454 declarations, file-scope (extern) function declarations (which
20455 had no corresponding body) and file-scope tagged type declarations
20456 and definitions which have not yet been forced out. */
20457 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20458 dwarf2out_decl (decl);
20461 /* Output debug information for type decl DECL. Called from toplev.c
20462 and from language front ends (to record built-in types). */
20463 static void
20464 dwarf2out_type_decl (tree decl, int local)
20466 if (!local)
20467 dwarf2out_decl (decl);
20470 /* Output debug information for imported module or decl DECL.
20471 NAME is non-NULL name in the lexical block if the decl has been renamed.
20472 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20473 that DECL belongs to.
20474 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20475 static void
20476 dwarf2out_imported_module_or_decl_1 (tree decl,
20477 tree name,
20478 tree lexical_block,
20479 dw_die_ref lexical_block_die)
20481 expanded_location xloc;
20482 dw_die_ref imported_die = NULL;
20483 dw_die_ref at_import_die;
20485 if (TREE_CODE (decl) == IMPORTED_DECL)
20487 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20488 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20489 gcc_assert (decl);
20491 else
20492 xloc = expand_location (input_location);
20494 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20496 at_import_die = force_type_die (TREE_TYPE (decl));
20497 /* For namespace N { typedef void T; } using N::T; base_type_die
20498 returns NULL, but DW_TAG_imported_declaration requires
20499 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20500 if (!at_import_die)
20502 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20503 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20504 at_import_die = lookup_type_die (TREE_TYPE (decl));
20505 gcc_assert (at_import_die);
20508 else
20510 at_import_die = lookup_decl_die (decl);
20511 if (!at_import_die)
20513 /* If we're trying to avoid duplicate debug info, we may not have
20514 emitted the member decl for this field. Emit it now. */
20515 if (TREE_CODE (decl) == FIELD_DECL)
20517 tree type = DECL_CONTEXT (decl);
20519 if (TYPE_CONTEXT (type)
20520 && TYPE_P (TYPE_CONTEXT (type))
20521 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20522 DINFO_USAGE_DIR_USE))
20523 return;
20524 gen_type_die_for_member (type, decl,
20525 get_context_die (TYPE_CONTEXT (type)));
20527 if (TREE_CODE (decl) == NAMELIST_DECL)
20528 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20529 get_context_die (DECL_CONTEXT (decl)),
20530 NULL_TREE);
20531 else
20532 at_import_die = force_decl_die (decl);
20536 if (TREE_CODE (decl) == NAMESPACE_DECL)
20538 if (dwarf_version >= 3 || !dwarf_strict)
20539 imported_die = new_die (DW_TAG_imported_module,
20540 lexical_block_die,
20541 lexical_block);
20542 else
20543 return;
20545 else
20546 imported_die = new_die (DW_TAG_imported_declaration,
20547 lexical_block_die,
20548 lexical_block);
20550 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20551 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20552 if (name)
20553 add_AT_string (imported_die, DW_AT_name,
20554 IDENTIFIER_POINTER (name));
20555 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20558 /* Output debug information for imported module or decl DECL.
20559 NAME is non-NULL name in context if the decl has been renamed.
20560 CHILD is true if decl is one of the renamed decls as part of
20561 importing whole module. */
20563 static void
20564 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20565 bool child)
20567 /* dw_die_ref at_import_die; */
20568 dw_die_ref scope_die;
20570 if (debug_info_level <= DINFO_LEVEL_TERSE)
20571 return;
20573 gcc_assert (decl);
20575 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20576 We need decl DIE for reference and scope die. First, get DIE for the decl
20577 itself. */
20579 /* Get the scope die for decl context. Use comp_unit_die for global module
20580 or decl. If die is not found for non globals, force new die. */
20581 if (context
20582 && TYPE_P (context)
20583 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20584 return;
20586 if (!(dwarf_version >= 3 || !dwarf_strict))
20587 return;
20589 scope_die = get_context_die (context);
20591 if (child)
20593 gcc_assert (scope_die->die_child);
20594 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20595 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20596 scope_die = scope_die->die_child;
20599 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20600 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20604 /* Output debug information for namelists. */
20606 static dw_die_ref
20607 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20609 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20610 tree value;
20611 unsigned i;
20613 if (debug_info_level <= DINFO_LEVEL_TERSE)
20614 return NULL;
20616 gcc_assert (scope_die != NULL);
20617 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20618 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20620 /* If there are no item_decls, we have a nondefining namelist, e.g.
20621 with USE association; hence, set DW_AT_declaration. */
20622 if (item_decls == NULL_TREE)
20624 add_AT_flag (nml_die, DW_AT_declaration, 1);
20625 return nml_die;
20628 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20630 nml_item_ref_die = lookup_decl_die (value);
20631 if (!nml_item_ref_die)
20632 nml_item_ref_die = force_decl_die (value);
20634 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20635 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20637 return nml_die;
20641 /* Write the debugging output for DECL. */
20643 void
20644 dwarf2out_decl (tree decl)
20646 dw_die_ref context_die = comp_unit_die ();
20648 switch (TREE_CODE (decl))
20650 case ERROR_MARK:
20651 return;
20653 case FUNCTION_DECL:
20654 /* What we would really like to do here is to filter out all mere
20655 file-scope declarations of file-scope functions which are never
20656 referenced later within this translation unit (and keep all of ones
20657 that *are* referenced later on) but we aren't clairvoyant, so we have
20658 no idea which functions will be referenced in the future (i.e. later
20659 on within the current translation unit). So here we just ignore all
20660 file-scope function declarations which are not also definitions. If
20661 and when the debugger needs to know something about these functions,
20662 it will have to hunt around and find the DWARF information associated
20663 with the definition of the function.
20665 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20666 nodes represent definitions and which ones represent mere
20667 declarations. We have to check DECL_INITIAL instead. That's because
20668 the C front-end supports some weird semantics for "extern inline"
20669 function definitions. These can get inlined within the current
20670 translation unit (and thus, we need to generate Dwarf info for their
20671 abstract instances so that the Dwarf info for the concrete inlined
20672 instances can have something to refer to) but the compiler never
20673 generates any out-of-lines instances of such things (despite the fact
20674 that they *are* definitions).
20676 The important point is that the C front-end marks these "extern
20677 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20678 them anyway. Note that the C++ front-end also plays some similar games
20679 for inline function definitions appearing within include files which
20680 also contain `#pragma interface' pragmas.
20682 If we are called from dwarf2out_abstract_function output a DIE
20683 anyway. We can end up here this way with early inlining and LTO
20684 where the inlined function is output in a different LTRANS unit
20685 or not at all. */
20686 if (DECL_INITIAL (decl) == NULL_TREE
20687 && ! DECL_ABSTRACT (decl))
20688 return;
20690 /* If we're a nested function, initially use a parent of NULL; if we're
20691 a plain function, this will be fixed up in decls_for_scope. If
20692 we're a method, it will be ignored, since we already have a DIE. */
20693 if (decl_function_context (decl)
20694 /* But if we're in terse mode, we don't care about scope. */
20695 && debug_info_level > DINFO_LEVEL_TERSE)
20696 context_die = NULL;
20697 break;
20699 case VAR_DECL:
20700 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20701 declaration and if the declaration was never even referenced from
20702 within this entire compilation unit. We suppress these DIEs in
20703 order to save space in the .debug section (by eliminating entries
20704 which are probably useless). Note that we must not suppress
20705 block-local extern declarations (whether used or not) because that
20706 would screw-up the debugger's name lookup mechanism and cause it to
20707 miss things which really ought to be in scope at a given point. */
20708 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20709 return;
20711 /* For local statics lookup proper context die. */
20712 if (TREE_STATIC (decl)
20713 && DECL_CONTEXT (decl)
20714 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20715 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20717 /* If we are in terse mode, don't generate any DIEs to represent any
20718 variable declarations or definitions. */
20719 if (debug_info_level <= DINFO_LEVEL_TERSE)
20720 return;
20721 break;
20723 case CONST_DECL:
20724 if (debug_info_level <= DINFO_LEVEL_TERSE)
20725 return;
20726 if (!is_fortran () && !is_ada ())
20727 return;
20728 if (TREE_STATIC (decl) && decl_function_context (decl))
20729 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20730 break;
20732 case NAMESPACE_DECL:
20733 case IMPORTED_DECL:
20734 if (debug_info_level <= DINFO_LEVEL_TERSE)
20735 return;
20736 if (lookup_decl_die (decl) != NULL)
20737 return;
20738 break;
20740 case TYPE_DECL:
20741 /* Don't emit stubs for types unless they are needed by other DIEs. */
20742 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20743 return;
20745 /* Don't bother trying to generate any DIEs to represent any of the
20746 normal built-in types for the language we are compiling. */
20747 if (DECL_IS_BUILTIN (decl))
20748 return;
20750 /* If we are in terse mode, don't generate any DIEs for types. */
20751 if (debug_info_level <= DINFO_LEVEL_TERSE)
20752 return;
20754 /* If we're a function-scope tag, initially use a parent of NULL;
20755 this will be fixed up in decls_for_scope. */
20756 if (decl_function_context (decl))
20757 context_die = NULL;
20759 break;
20761 case NAMELIST_DECL:
20762 break;
20764 default:
20765 return;
20768 gen_decl_die (decl, NULL, context_die);
20771 /* Write the debugging output for DECL. */
20773 static void
20774 dwarf2out_function_decl (tree decl)
20776 dwarf2out_decl (decl);
20777 call_arg_locations = NULL;
20778 call_arg_loc_last = NULL;
20779 call_site_count = -1;
20780 tail_call_site_count = -1;
20781 block_map.release ();
20782 htab_empty (decl_loc_table);
20783 htab_empty (cached_dw_loc_list_table);
20786 /* Output a marker (i.e. a label) for the beginning of the generated code for
20787 a lexical block. */
20789 static void
20790 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20791 unsigned int blocknum)
20793 switch_to_section (current_function_section ());
20794 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20797 /* Output a marker (i.e. a label) for the end of the generated code for a
20798 lexical block. */
20800 static void
20801 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20803 switch_to_section (current_function_section ());
20804 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20807 /* Returns nonzero if it is appropriate not to emit any debugging
20808 information for BLOCK, because it doesn't contain any instructions.
20810 Don't allow this for blocks with nested functions or local classes
20811 as we would end up with orphans, and in the presence of scheduling
20812 we may end up calling them anyway. */
20814 static bool
20815 dwarf2out_ignore_block (const_tree block)
20817 tree decl;
20818 unsigned int i;
20820 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20821 if (TREE_CODE (decl) == FUNCTION_DECL
20822 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20823 return 0;
20824 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20826 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20827 if (TREE_CODE (decl) == FUNCTION_DECL
20828 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20829 return 0;
20832 return 1;
20835 /* Hash table routines for file_hash. */
20837 static int
20838 file_table_eq (const void *p1_p, const void *p2_p)
20840 const struct dwarf_file_data *const p1 =
20841 (const struct dwarf_file_data *) p1_p;
20842 const char *const p2 = (const char *) p2_p;
20843 return filename_cmp (p1->filename, p2) == 0;
20846 static hashval_t
20847 file_table_hash (const void *p_p)
20849 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20850 return htab_hash_string (p->filename);
20853 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20854 dwarf2out.c) and return its "index". The index of each (known) filename is
20855 just a unique number which is associated with only that one filename. We
20856 need such numbers for the sake of generating labels (in the .debug_sfnames
20857 section) and references to those files numbers (in the .debug_srcinfo
20858 and.debug_macinfo sections). If the filename given as an argument is not
20859 found in our current list, add it to the list and assign it the next
20860 available unique index number. In order to speed up searches, we remember
20861 the index of the filename was looked up last. This handles the majority of
20862 all searches. */
20864 static struct dwarf_file_data *
20865 lookup_filename (const char *file_name)
20867 void ** slot;
20868 struct dwarf_file_data * created;
20870 /* Check to see if the file name that was searched on the previous
20871 call matches this file name. If so, return the index. */
20872 if (file_table_last_lookup
20873 && (file_name == file_table_last_lookup->filename
20874 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20875 return file_table_last_lookup;
20877 /* Didn't match the previous lookup, search the table. */
20878 slot = htab_find_slot_with_hash (file_table, file_name,
20879 htab_hash_string (file_name), INSERT);
20880 if (*slot)
20881 return (struct dwarf_file_data *) *slot;
20883 created = ggc_alloc_dwarf_file_data ();
20884 created->filename = file_name;
20885 created->emitted_number = 0;
20886 *slot = created;
20887 return created;
20890 /* If the assembler will construct the file table, then translate the compiler
20891 internal file table number into the assembler file table number, and emit
20892 a .file directive if we haven't already emitted one yet. The file table
20893 numbers are different because we prune debug info for unused variables and
20894 types, which may include filenames. */
20896 static int
20897 maybe_emit_file (struct dwarf_file_data * fd)
20899 if (! fd->emitted_number)
20901 if (last_emitted_file)
20902 fd->emitted_number = last_emitted_file->emitted_number + 1;
20903 else
20904 fd->emitted_number = 1;
20905 last_emitted_file = fd;
20907 if (DWARF2_ASM_LINE_DEBUG_INFO)
20909 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20910 output_quoted_string (asm_out_file,
20911 remap_debug_filename (fd->filename));
20912 fputc ('\n', asm_out_file);
20916 return fd->emitted_number;
20919 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20920 That generation should happen after function debug info has been
20921 generated. The value of the attribute is the constant value of ARG. */
20923 static void
20924 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20926 die_arg_entry entry;
20928 if (!die || !arg)
20929 return;
20931 if (!tmpl_value_parm_die_table)
20932 vec_alloc (tmpl_value_parm_die_table, 32);
20934 entry.die = die;
20935 entry.arg = arg;
20936 vec_safe_push (tmpl_value_parm_die_table, entry);
20939 /* Return TRUE if T is an instance of generic type, FALSE
20940 otherwise. */
20942 static bool
20943 generic_type_p (tree t)
20945 if (t == NULL_TREE || !TYPE_P (t))
20946 return false;
20947 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20950 /* Schedule the generation of the generic parameter dies for the
20951 instance of generic type T. The proper generation itself is later
20952 done by gen_scheduled_generic_parms_dies. */
20954 static void
20955 schedule_generic_params_dies_gen (tree t)
20957 if (!generic_type_p (t))
20958 return;
20960 if (!generic_type_instances)
20961 vec_alloc (generic_type_instances, 256);
20963 vec_safe_push (generic_type_instances, t);
20966 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20967 by append_entry_to_tmpl_value_parm_die_table. This function must
20968 be called after function DIEs have been generated. */
20970 static void
20971 gen_remaining_tmpl_value_param_die_attribute (void)
20973 if (tmpl_value_parm_die_table)
20975 unsigned i;
20976 die_arg_entry *e;
20978 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20979 tree_add_const_value_attribute (e->die, e->arg);
20983 /* Generate generic parameters DIEs for instances of generic types
20984 that have been previously scheduled by
20985 schedule_generic_params_dies_gen. This function must be called
20986 after all the types of the CU have been laid out. */
20988 static void
20989 gen_scheduled_generic_parms_dies (void)
20991 unsigned i;
20992 tree t;
20994 if (!generic_type_instances)
20995 return;
20997 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20998 if (COMPLETE_TYPE_P (t))
20999 gen_generic_params_dies (t);
21003 /* Replace DW_AT_name for the decl with name. */
21005 static void
21006 dwarf2out_set_name (tree decl, tree name)
21008 dw_die_ref die;
21009 dw_attr_ref attr;
21010 const char *dname;
21012 die = TYPE_SYMTAB_DIE (decl);
21013 if (!die)
21014 return;
21016 dname = dwarf2_name (name, 0);
21017 if (!dname)
21018 return;
21020 attr = get_AT (die, DW_AT_name);
21021 if (attr)
21023 struct indirect_string_node *node;
21025 node = find_AT_string (dname);
21026 /* replace the string. */
21027 attr->dw_attr_val.v.val_str = node;
21030 else
21031 add_name_attribute (die, dname);
21034 /* True if before or during processing of the first function being emitted. */
21035 static bool in_first_function_p = true;
21036 /* True if loc_note during dwarf2out_var_location call might still be
21037 before first real instruction at address equal to .Ltext0. */
21038 static bool maybe_at_text_label_p = true;
21039 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21040 static unsigned int first_loclabel_num_not_at_text_label;
21042 /* Called by the final INSN scan whenever we see a var location. We
21043 use it to drop labels in the right places, and throw the location in
21044 our lookup table. */
21046 static void
21047 dwarf2out_var_location (rtx loc_note)
21049 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21050 struct var_loc_node *newloc;
21051 rtx next_real, next_note;
21052 static const char *last_label;
21053 static const char *last_postcall_label;
21054 static bool last_in_cold_section_p;
21055 static rtx expected_next_loc_note;
21056 tree decl;
21057 bool var_loc_p;
21059 if (!NOTE_P (loc_note))
21061 if (CALL_P (loc_note))
21063 call_site_count++;
21064 if (SIBLING_CALL_P (loc_note))
21065 tail_call_site_count++;
21067 return;
21070 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21071 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21072 return;
21074 /* Optimize processing a large consecutive sequence of location
21075 notes so we don't spend too much time in next_real_insn. If the
21076 next insn is another location note, remember the next_real_insn
21077 calculation for next time. */
21078 next_real = cached_next_real_insn;
21079 if (next_real)
21081 if (expected_next_loc_note != loc_note)
21082 next_real = NULL_RTX;
21085 next_note = NEXT_INSN (loc_note);
21086 if (! next_note
21087 || INSN_DELETED_P (next_note)
21088 || ! NOTE_P (next_note)
21089 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21090 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21091 next_note = NULL_RTX;
21093 if (! next_real)
21094 next_real = next_real_insn (loc_note);
21096 if (next_note)
21098 expected_next_loc_note = next_note;
21099 cached_next_real_insn = next_real;
21101 else
21102 cached_next_real_insn = NULL_RTX;
21104 /* If there are no instructions which would be affected by this note,
21105 don't do anything. */
21106 if (var_loc_p
21107 && next_real == NULL_RTX
21108 && !NOTE_DURING_CALL_P (loc_note))
21109 return;
21111 if (next_real == NULL_RTX)
21112 next_real = get_last_insn ();
21114 /* If there were any real insns between note we processed last time
21115 and this note (or if it is the first note), clear
21116 last_{,postcall_}label so that they are not reused this time. */
21117 if (last_var_location_insn == NULL_RTX
21118 || last_var_location_insn != next_real
21119 || last_in_cold_section_p != in_cold_section_p)
21121 last_label = NULL;
21122 last_postcall_label = NULL;
21125 if (var_loc_p)
21127 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21128 newloc = add_var_loc_to_decl (decl, loc_note,
21129 NOTE_DURING_CALL_P (loc_note)
21130 ? last_postcall_label : last_label);
21131 if (newloc == NULL)
21132 return;
21134 else
21136 decl = NULL_TREE;
21137 newloc = NULL;
21140 /* If there were no real insns between note we processed last time
21141 and this note, use the label we emitted last time. Otherwise
21142 create a new label and emit it. */
21143 if (last_label == NULL)
21145 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21146 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21147 loclabel_num++;
21148 last_label = ggc_strdup (loclabel);
21149 /* See if loclabel might be equal to .Ltext0. If yes,
21150 bump first_loclabel_num_not_at_text_label. */
21151 if (!have_multiple_function_sections
21152 && in_first_function_p
21153 && maybe_at_text_label_p)
21155 static rtx last_start;
21156 rtx insn;
21157 for (insn = loc_note; insn; insn = previous_insn (insn))
21158 if (insn == last_start)
21159 break;
21160 else if (!NONDEBUG_INSN_P (insn))
21161 continue;
21162 else
21164 rtx body = PATTERN (insn);
21165 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21166 continue;
21167 /* Inline asm could occupy zero bytes. */
21168 else if (GET_CODE (body) == ASM_INPUT
21169 || asm_noperands (body) >= 0)
21170 continue;
21171 #ifdef HAVE_attr_length
21172 else if (get_attr_min_length (insn) == 0)
21173 continue;
21174 #endif
21175 else
21177 /* Assume insn has non-zero length. */
21178 maybe_at_text_label_p = false;
21179 break;
21182 if (maybe_at_text_label_p)
21184 last_start = loc_note;
21185 first_loclabel_num_not_at_text_label = loclabel_num;
21190 if (!var_loc_p)
21192 struct call_arg_loc_node *ca_loc
21193 = ggc_alloc_cleared_call_arg_loc_node ();
21194 rtx prev = prev_real_insn (loc_note), x;
21195 ca_loc->call_arg_loc_note = loc_note;
21196 ca_loc->next = NULL;
21197 ca_loc->label = last_label;
21198 gcc_assert (prev
21199 && (CALL_P (prev)
21200 || (NONJUMP_INSN_P (prev)
21201 && GET_CODE (PATTERN (prev)) == SEQUENCE
21202 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21203 if (!CALL_P (prev))
21204 prev = XVECEXP (PATTERN (prev), 0, 0);
21205 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21206 x = get_call_rtx_from (PATTERN (prev));
21207 if (x)
21209 x = XEXP (XEXP (x, 0), 0);
21210 if (GET_CODE (x) == SYMBOL_REF
21211 && SYMBOL_REF_DECL (x)
21212 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21213 ca_loc->symbol_ref = x;
21215 ca_loc->block = insn_scope (prev);
21216 if (call_arg_locations)
21217 call_arg_loc_last->next = ca_loc;
21218 else
21219 call_arg_locations = ca_loc;
21220 call_arg_loc_last = ca_loc;
21222 else if (!NOTE_DURING_CALL_P (loc_note))
21223 newloc->label = last_label;
21224 else
21226 if (!last_postcall_label)
21228 sprintf (loclabel, "%s-1", last_label);
21229 last_postcall_label = ggc_strdup (loclabel);
21231 newloc->label = last_postcall_label;
21234 last_var_location_insn = next_real;
21235 last_in_cold_section_p = in_cold_section_p;
21238 /* Note in one location list that text section has changed. */
21240 static int
21241 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21243 var_loc_list *list = (var_loc_list *) *slot;
21244 if (list->first)
21245 list->last_before_switch
21246 = list->last->next ? list->last->next : list->last;
21247 return 1;
21250 /* Note in all location lists that text section has changed. */
21252 static void
21253 var_location_switch_text_section (void)
21255 if (decl_loc_table == NULL)
21256 return;
21258 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21261 /* Create a new line number table. */
21263 static dw_line_info_table *
21264 new_line_info_table (void)
21266 dw_line_info_table *table;
21268 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21269 table->file_num = 1;
21270 table->line_num = 1;
21271 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21273 return table;
21276 /* Lookup the "current" table into which we emit line info, so
21277 that we don't have to do it for every source line. */
21279 static void
21280 set_cur_line_info_table (section *sec)
21282 dw_line_info_table *table;
21284 if (sec == text_section)
21285 table = text_section_line_info;
21286 else if (sec == cold_text_section)
21288 table = cold_text_section_line_info;
21289 if (!table)
21291 cold_text_section_line_info = table = new_line_info_table ();
21292 table->end_label = cold_end_label;
21295 else
21297 const char *end_label;
21299 if (flag_reorder_blocks_and_partition)
21301 if (in_cold_section_p)
21302 end_label = crtl->subsections.cold_section_end_label;
21303 else
21304 end_label = crtl->subsections.hot_section_end_label;
21306 else
21308 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21309 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21310 current_function_funcdef_no);
21311 end_label = ggc_strdup (label);
21314 table = new_line_info_table ();
21315 table->end_label = end_label;
21317 vec_safe_push (separate_line_info, table);
21320 if (DWARF2_ASM_LINE_DEBUG_INFO)
21321 table->is_stmt = (cur_line_info_table
21322 ? cur_line_info_table->is_stmt
21323 : DWARF_LINE_DEFAULT_IS_STMT_START);
21324 cur_line_info_table = table;
21328 /* We need to reset the locations at the beginning of each
21329 function. We can't do this in the end_function hook, because the
21330 declarations that use the locations won't have been output when
21331 that hook is called. Also compute have_multiple_function_sections here. */
21333 static void
21334 dwarf2out_begin_function (tree fun)
21336 section *sec = function_section (fun);
21338 if (sec != text_section)
21339 have_multiple_function_sections = true;
21341 if (flag_reorder_blocks_and_partition && !cold_text_section)
21343 gcc_assert (current_function_decl == fun);
21344 cold_text_section = unlikely_text_section ();
21345 switch_to_section (cold_text_section);
21346 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21347 switch_to_section (sec);
21350 dwarf2out_note_section_used ();
21351 call_site_count = 0;
21352 tail_call_site_count = 0;
21354 set_cur_line_info_table (sec);
21357 /* Helper function of dwarf2out_end_function, called only after emitting
21358 the very first function into assembly. Check if some .debug_loc range
21359 might end with a .LVL* label that could be equal to .Ltext0.
21360 In that case we must force using absolute addresses in .debug_loc ranges,
21361 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21362 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21363 list terminator.
21364 Set have_multiple_function_sections to true in that case and
21365 terminate htab traversal. */
21367 static int
21368 find_empty_loc_ranges_at_text_label (void **slot, void *)
21370 var_loc_list *entry;
21371 struct var_loc_node *node;
21373 entry = (var_loc_list *) *slot;
21374 node = entry->first;
21375 if (node && node->next && node->next->label)
21377 unsigned int i;
21378 const char *label = node->next->label;
21379 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21381 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21383 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21384 if (strcmp (label, loclabel) == 0)
21386 have_multiple_function_sections = true;
21387 return 0;
21391 return 1;
21394 /* Hook called after emitting a function into assembly.
21395 This does something only for the very first function emitted. */
21397 static void
21398 dwarf2out_end_function (unsigned int)
21400 if (in_first_function_p
21401 && !have_multiple_function_sections
21402 && first_loclabel_num_not_at_text_label
21403 && decl_loc_table)
21404 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21405 NULL);
21406 in_first_function_p = false;
21407 maybe_at_text_label_p = false;
21410 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21412 static void
21413 push_dw_line_info_entry (dw_line_info_table *table,
21414 enum dw_line_info_opcode opcode, unsigned int val)
21416 dw_line_info_entry e;
21417 e.opcode = opcode;
21418 e.val = val;
21419 vec_safe_push (table->entries, e);
21422 /* Output a label to mark the beginning of a source code line entry
21423 and record information relating to this source line, in
21424 'line_info_table' for later output of the .debug_line section. */
21425 /* ??? The discriminator parameter ought to be unsigned. */
21427 static void
21428 dwarf2out_source_line (unsigned int line, const char *filename,
21429 int discriminator, bool is_stmt)
21431 unsigned int file_num;
21432 dw_line_info_table *table;
21434 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21435 return;
21437 /* The discriminator column was added in dwarf4. Simplify the below
21438 by simply removing it if we're not supposed to output it. */
21439 if (dwarf_version < 4 && dwarf_strict)
21440 discriminator = 0;
21442 table = cur_line_info_table;
21443 file_num = maybe_emit_file (lookup_filename (filename));
21445 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21446 the debugger has used the second (possibly duplicate) line number
21447 at the beginning of the function to mark the end of the prologue.
21448 We could eliminate any other duplicates within the function. For
21449 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21450 that second line number entry. */
21451 /* Recall that this end-of-prologue indication is *not* the same thing
21452 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21453 to which the hook corresponds, follows the last insn that was
21454 emitted by gen_prologue. What we need is to precede the first insn
21455 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21456 insn that corresponds to something the user wrote. These may be
21457 very different locations once scheduling is enabled. */
21459 if (0 && file_num == table->file_num
21460 && line == table->line_num
21461 && discriminator == table->discrim_num
21462 && is_stmt == table->is_stmt)
21463 return;
21465 switch_to_section (current_function_section ());
21467 /* If requested, emit something human-readable. */
21468 if (flag_debug_asm)
21469 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21471 if (DWARF2_ASM_LINE_DEBUG_INFO)
21473 /* Emit the .loc directive understood by GNU as. */
21474 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21475 file_num, line, is_stmt, discriminator */
21476 fputs ("\t.loc ", asm_out_file);
21477 fprint_ul (asm_out_file, file_num);
21478 putc (' ', asm_out_file);
21479 fprint_ul (asm_out_file, line);
21480 putc (' ', asm_out_file);
21481 putc ('0', asm_out_file);
21483 if (is_stmt != table->is_stmt)
21485 fputs (" is_stmt ", asm_out_file);
21486 putc (is_stmt ? '1' : '0', asm_out_file);
21488 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21490 gcc_assert (discriminator > 0);
21491 fputs (" discriminator ", asm_out_file);
21492 fprint_ul (asm_out_file, (unsigned long) discriminator);
21494 putc ('\n', asm_out_file);
21496 else
21498 unsigned int label_num = ++line_info_label_num;
21500 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21502 push_dw_line_info_entry (table, LI_set_address, label_num);
21503 if (file_num != table->file_num)
21504 push_dw_line_info_entry (table, LI_set_file, file_num);
21505 if (discriminator != table->discrim_num)
21506 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21507 if (is_stmt != table->is_stmt)
21508 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21509 push_dw_line_info_entry (table, LI_set_line, line);
21512 table->file_num = file_num;
21513 table->line_num = line;
21514 table->discrim_num = discriminator;
21515 table->is_stmt = is_stmt;
21516 table->in_use = true;
21519 /* Record the beginning of a new source file. */
21521 static void
21522 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21524 if (flag_eliminate_dwarf2_dups)
21526 /* Record the beginning of the file for break_out_includes. */
21527 dw_die_ref bincl_die;
21529 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21530 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21533 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21535 macinfo_entry e;
21536 e.code = DW_MACINFO_start_file;
21537 e.lineno = lineno;
21538 e.info = ggc_strdup (filename);
21539 vec_safe_push (macinfo_table, e);
21543 /* Record the end of a source file. */
21545 static void
21546 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21548 if (flag_eliminate_dwarf2_dups)
21549 /* Record the end of the file for break_out_includes. */
21550 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21552 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21554 macinfo_entry e;
21555 e.code = DW_MACINFO_end_file;
21556 e.lineno = lineno;
21557 e.info = NULL;
21558 vec_safe_push (macinfo_table, e);
21562 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21563 the tail part of the directive line, i.e. the part which is past the
21564 initial whitespace, #, whitespace, directive-name, whitespace part. */
21566 static void
21567 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21568 const char *buffer ATTRIBUTE_UNUSED)
21570 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21572 macinfo_entry e;
21573 /* Insert a dummy first entry to be able to optimize the whole
21574 predefined macro block using DW_MACRO_GNU_transparent_include. */
21575 if (macinfo_table->is_empty () && lineno <= 1)
21577 e.code = 0;
21578 e.lineno = 0;
21579 e.info = NULL;
21580 vec_safe_push (macinfo_table, e);
21582 e.code = DW_MACINFO_define;
21583 e.lineno = lineno;
21584 e.info = ggc_strdup (buffer);
21585 vec_safe_push (macinfo_table, e);
21589 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21590 the tail part of the directive line, i.e. the part which is past the
21591 initial whitespace, #, whitespace, directive-name, whitespace part. */
21593 static void
21594 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21595 const char *buffer ATTRIBUTE_UNUSED)
21597 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21599 macinfo_entry e;
21600 /* Insert a dummy first entry to be able to optimize the whole
21601 predefined macro block using DW_MACRO_GNU_transparent_include. */
21602 if (macinfo_table->is_empty () && lineno <= 1)
21604 e.code = 0;
21605 e.lineno = 0;
21606 e.info = NULL;
21607 vec_safe_push (macinfo_table, e);
21609 e.code = DW_MACINFO_undef;
21610 e.lineno = lineno;
21611 e.info = ggc_strdup (buffer);
21612 vec_safe_push (macinfo_table, e);
21616 /* Helpers to manipulate hash table of CUs. */
21618 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21620 typedef macinfo_entry value_type;
21621 typedef macinfo_entry compare_type;
21622 static inline hashval_t hash (const value_type *);
21623 static inline bool equal (const value_type *, const compare_type *);
21626 inline hashval_t
21627 macinfo_entry_hasher::hash (const value_type *entry)
21629 return htab_hash_string (entry->info);
21632 inline bool
21633 macinfo_entry_hasher::equal (const value_type *entry1,
21634 const compare_type *entry2)
21636 return !strcmp (entry1->info, entry2->info);
21639 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21641 /* Output a single .debug_macinfo entry. */
21643 static void
21644 output_macinfo_op (macinfo_entry *ref)
21646 int file_num;
21647 size_t len;
21648 struct indirect_string_node *node;
21649 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21650 struct dwarf_file_data *fd;
21652 switch (ref->code)
21654 case DW_MACINFO_start_file:
21655 fd = lookup_filename (ref->info);
21656 file_num = maybe_emit_file (fd);
21657 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21658 dw2_asm_output_data_uleb128 (ref->lineno,
21659 "Included from line number %lu",
21660 (unsigned long) ref->lineno);
21661 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21662 break;
21663 case DW_MACINFO_end_file:
21664 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21665 break;
21666 case DW_MACINFO_define:
21667 case DW_MACINFO_undef:
21668 len = strlen (ref->info) + 1;
21669 if (!dwarf_strict
21670 && len > DWARF_OFFSET_SIZE
21671 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21672 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21674 ref->code = ref->code == DW_MACINFO_define
21675 ? DW_MACRO_GNU_define_indirect
21676 : DW_MACRO_GNU_undef_indirect;
21677 output_macinfo_op (ref);
21678 return;
21680 dw2_asm_output_data (1, ref->code,
21681 ref->code == DW_MACINFO_define
21682 ? "Define macro" : "Undefine macro");
21683 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21684 (unsigned long) ref->lineno);
21685 dw2_asm_output_nstring (ref->info, -1, "The macro");
21686 break;
21687 case DW_MACRO_GNU_define_indirect:
21688 case DW_MACRO_GNU_undef_indirect:
21689 node = find_AT_string (ref->info);
21690 gcc_assert (node
21691 && ((node->form == DW_FORM_strp)
21692 || (node->form == DW_FORM_GNU_str_index)));
21693 dw2_asm_output_data (1, ref->code,
21694 ref->code == DW_MACRO_GNU_define_indirect
21695 ? "Define macro indirect"
21696 : "Undefine macro indirect");
21697 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21698 (unsigned long) ref->lineno);
21699 if (node->form == DW_FORM_strp)
21700 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21701 debug_str_section, "The macro: \"%s\"",
21702 ref->info);
21703 else
21704 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21705 ref->info);
21706 break;
21707 case DW_MACRO_GNU_transparent_include:
21708 dw2_asm_output_data (1, ref->code, "Transparent include");
21709 ASM_GENERATE_INTERNAL_LABEL (label,
21710 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21711 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21712 break;
21713 default:
21714 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21715 ASM_COMMENT_START, (unsigned long) ref->code);
21716 break;
21720 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21721 other compilation unit .debug_macinfo sections. IDX is the first
21722 index of a define/undef, return the number of ops that should be
21723 emitted in a comdat .debug_macinfo section and emit
21724 a DW_MACRO_GNU_transparent_include entry referencing it.
21725 If the define/undef entry should be emitted normally, return 0. */
21727 static unsigned
21728 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21729 macinfo_hash_type *macinfo_htab)
21731 macinfo_entry *first, *second, *cur, *inc;
21732 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21733 unsigned char checksum[16];
21734 struct md5_ctx ctx;
21735 char *grp_name, *tail;
21736 const char *base;
21737 unsigned int i, count, encoded_filename_len, linebuf_len;
21738 macinfo_entry **slot;
21740 first = &(*macinfo_table)[idx];
21741 second = &(*macinfo_table)[idx + 1];
21743 /* Optimize only if there are at least two consecutive define/undef ops,
21744 and either all of them are before first DW_MACINFO_start_file
21745 with lineno {0,1} (i.e. predefined macro block), or all of them are
21746 in some included header file. */
21747 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21748 return 0;
21749 if (vec_safe_is_empty (files))
21751 if (first->lineno > 1 || second->lineno > 1)
21752 return 0;
21754 else if (first->lineno == 0)
21755 return 0;
21757 /* Find the last define/undef entry that can be grouped together
21758 with first and at the same time compute md5 checksum of their
21759 codes, linenumbers and strings. */
21760 md5_init_ctx (&ctx);
21761 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21762 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21763 break;
21764 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21765 break;
21766 else
21768 unsigned char code = cur->code;
21769 md5_process_bytes (&code, 1, &ctx);
21770 checksum_uleb128 (cur->lineno, &ctx);
21771 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21773 md5_finish_ctx (&ctx, checksum);
21774 count = i - idx;
21776 /* From the containing include filename (if any) pick up just
21777 usable characters from its basename. */
21778 if (vec_safe_is_empty (files))
21779 base = "";
21780 else
21781 base = lbasename (files->last ().info);
21782 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21783 if (ISIDNUM (base[i]) || base[i] == '.')
21784 encoded_filename_len++;
21785 /* Count . at the end. */
21786 if (encoded_filename_len)
21787 encoded_filename_len++;
21789 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21790 linebuf_len = strlen (linebuf);
21792 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21793 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21794 + 16 * 2 + 1);
21795 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21796 tail = grp_name + 4;
21797 if (encoded_filename_len)
21799 for (i = 0; base[i]; i++)
21800 if (ISIDNUM (base[i]) || base[i] == '.')
21801 *tail++ = base[i];
21802 *tail++ = '.';
21804 memcpy (tail, linebuf, linebuf_len);
21805 tail += linebuf_len;
21806 *tail++ = '.';
21807 for (i = 0; i < 16; i++)
21808 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21810 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21811 in the empty vector entry before the first define/undef. */
21812 inc = &(*macinfo_table)[idx - 1];
21813 inc->code = DW_MACRO_GNU_transparent_include;
21814 inc->lineno = 0;
21815 inc->info = ggc_strdup (grp_name);
21816 if (!macinfo_htab->is_created ())
21817 macinfo_htab->create (10);
21818 /* Avoid emitting duplicates. */
21819 slot = macinfo_htab->find_slot (inc, INSERT);
21820 if (*slot != NULL)
21822 inc->code = 0;
21823 inc->info = NULL;
21824 /* If such an entry has been used before, just emit
21825 a DW_MACRO_GNU_transparent_include op. */
21826 inc = *slot;
21827 output_macinfo_op (inc);
21828 /* And clear all macinfo_entry in the range to avoid emitting them
21829 in the second pass. */
21830 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21832 cur->code = 0;
21833 cur->info = NULL;
21836 else
21838 *slot = inc;
21839 inc->lineno = macinfo_htab->elements ();
21840 output_macinfo_op (inc);
21842 return count;
21845 /* Save any strings needed by the macinfo table in the debug str
21846 table. All strings must be collected into the table by the time
21847 index_string is called. */
21849 static void
21850 save_macinfo_strings (void)
21852 unsigned len;
21853 unsigned i;
21854 macinfo_entry *ref;
21856 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21858 switch (ref->code)
21860 /* Match the logic in output_macinfo_op to decide on
21861 indirect strings. */
21862 case DW_MACINFO_define:
21863 case DW_MACINFO_undef:
21864 len = strlen (ref->info) + 1;
21865 if (!dwarf_strict
21866 && len > DWARF_OFFSET_SIZE
21867 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21868 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21869 set_indirect_string (find_AT_string (ref->info));
21870 break;
21871 case DW_MACRO_GNU_define_indirect:
21872 case DW_MACRO_GNU_undef_indirect:
21873 set_indirect_string (find_AT_string (ref->info));
21874 break;
21875 default:
21876 break;
21881 /* Output macinfo section(s). */
21883 static void
21884 output_macinfo (void)
21886 unsigned i;
21887 unsigned long length = vec_safe_length (macinfo_table);
21888 macinfo_entry *ref;
21889 vec<macinfo_entry, va_gc> *files = NULL;
21890 macinfo_hash_type macinfo_htab;
21892 if (! length)
21893 return;
21895 /* output_macinfo* uses these interchangeably. */
21896 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21897 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21898 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21899 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21901 /* For .debug_macro emit the section header. */
21902 if (!dwarf_strict)
21904 dw2_asm_output_data (2, 4, "DWARF macro version number");
21905 if (DWARF_OFFSET_SIZE == 8)
21906 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21907 else
21908 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21909 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21910 (!dwarf_split_debug_info ? debug_line_section_label
21911 : debug_skeleton_line_section_label),
21912 debug_line_section, NULL);
21915 /* In the first loop, it emits the primary .debug_macinfo section
21916 and after each emitted op the macinfo_entry is cleared.
21917 If a longer range of define/undef ops can be optimized using
21918 DW_MACRO_GNU_transparent_include, the
21919 DW_MACRO_GNU_transparent_include op is emitted and kept in
21920 the vector before the first define/undef in the range and the
21921 whole range of define/undef ops is not emitted and kept. */
21922 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21924 switch (ref->code)
21926 case DW_MACINFO_start_file:
21927 vec_safe_push (files, *ref);
21928 break;
21929 case DW_MACINFO_end_file:
21930 if (!vec_safe_is_empty (files))
21931 files->pop ();
21932 break;
21933 case DW_MACINFO_define:
21934 case DW_MACINFO_undef:
21935 if (!dwarf_strict
21936 && HAVE_COMDAT_GROUP
21937 && vec_safe_length (files) != 1
21938 && i > 0
21939 && i + 1 < length
21940 && (*macinfo_table)[i - 1].code == 0)
21942 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21943 if (count)
21945 i += count - 1;
21946 continue;
21949 break;
21950 case 0:
21951 /* A dummy entry may be inserted at the beginning to be able
21952 to optimize the whole block of predefined macros. */
21953 if (i == 0)
21954 continue;
21955 default:
21956 break;
21958 output_macinfo_op (ref);
21959 ref->info = NULL;
21960 ref->code = 0;
21963 if (!macinfo_htab.is_created ())
21964 return;
21966 macinfo_htab.dispose ();
21968 /* If any DW_MACRO_GNU_transparent_include were used, on those
21969 DW_MACRO_GNU_transparent_include entries terminate the
21970 current chain and switch to a new comdat .debug_macinfo
21971 section and emit the define/undef entries within it. */
21972 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21973 switch (ref->code)
21975 case 0:
21976 continue;
21977 case DW_MACRO_GNU_transparent_include:
21979 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21980 tree comdat_key = get_identifier (ref->info);
21981 /* Terminate the previous .debug_macinfo section. */
21982 dw2_asm_output_data (1, 0, "End compilation unit");
21983 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21984 SECTION_DEBUG
21985 | SECTION_LINKONCE,
21986 comdat_key);
21987 ASM_GENERATE_INTERNAL_LABEL (label,
21988 DEBUG_MACRO_SECTION_LABEL,
21989 ref->lineno);
21990 ASM_OUTPUT_LABEL (asm_out_file, label);
21991 ref->code = 0;
21992 ref->info = NULL;
21993 dw2_asm_output_data (2, 4, "DWARF macro version number");
21994 if (DWARF_OFFSET_SIZE == 8)
21995 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21996 else
21997 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21999 break;
22000 case DW_MACINFO_define:
22001 case DW_MACINFO_undef:
22002 output_macinfo_op (ref);
22003 ref->code = 0;
22004 ref->info = NULL;
22005 break;
22006 default:
22007 gcc_unreachable ();
22011 /* Set up for Dwarf output at the start of compilation. */
22013 static void
22014 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22016 /* Allocate the file_table. */
22017 file_table = htab_create_ggc (50, file_table_hash,
22018 file_table_eq, NULL);
22020 /* Allocate the decl_die_table. */
22021 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22022 decl_die_table_eq, NULL);
22024 /* Allocate the decl_loc_table. */
22025 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22026 decl_loc_table_eq, NULL);
22028 /* Allocate the cached_dw_loc_list_table. */
22029 cached_dw_loc_list_table
22030 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22031 cached_dw_loc_list_table_eq, NULL);
22033 /* Allocate the initial hunk of the decl_scope_table. */
22034 vec_alloc (decl_scope_table, 256);
22036 /* Allocate the initial hunk of the abbrev_die_table. */
22037 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22038 (ABBREV_DIE_TABLE_INCREMENT);
22039 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22040 /* Zero-th entry is allocated, but unused. */
22041 abbrev_die_table_in_use = 1;
22043 /* Allocate the pubtypes and pubnames vectors. */
22044 vec_alloc (pubname_table, 32);
22045 vec_alloc (pubtype_table, 32);
22047 vec_alloc (incomplete_types, 64);
22049 vec_alloc (used_rtx_array, 32);
22051 if (!dwarf_split_debug_info)
22053 debug_info_section = get_section (DEBUG_INFO_SECTION,
22054 SECTION_DEBUG, NULL);
22055 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22056 SECTION_DEBUG, NULL);
22057 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22058 SECTION_DEBUG, NULL);
22060 else
22062 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22063 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22064 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22065 SECTION_DEBUG | SECTION_EXCLUDE,
22066 NULL);
22067 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22068 SECTION_DEBUG, NULL);
22069 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22070 SECTION_DEBUG, NULL);
22071 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22072 SECTION_DEBUG, NULL);
22073 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22074 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22076 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22077 the main .o, but the skeleton_line goes into the split off dwo. */
22078 debug_skeleton_line_section
22079 = get_section (DEBUG_DWO_LINE_SECTION,
22080 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22081 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22082 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22083 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22084 SECTION_DEBUG | SECTION_EXCLUDE,
22085 NULL);
22086 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22087 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22088 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22089 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22090 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22091 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22093 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22094 SECTION_DEBUG, NULL);
22095 debug_macinfo_section = get_section (dwarf_strict
22096 ? DEBUG_MACINFO_SECTION
22097 : DEBUG_MACRO_SECTION,
22098 DEBUG_MACRO_SECTION_FLAGS, NULL);
22099 debug_line_section = get_section (DEBUG_LINE_SECTION,
22100 SECTION_DEBUG, NULL);
22101 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22102 SECTION_DEBUG, NULL);
22103 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22104 SECTION_DEBUG, NULL);
22105 debug_str_section = get_section (DEBUG_STR_SECTION,
22106 DEBUG_STR_SECTION_FLAGS, NULL);
22107 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22108 SECTION_DEBUG, NULL);
22109 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22110 SECTION_DEBUG, NULL);
22112 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22113 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22114 DEBUG_ABBREV_SECTION_LABEL, 0);
22115 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22116 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22117 COLD_TEXT_SECTION_LABEL, 0);
22118 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22120 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22121 DEBUG_INFO_SECTION_LABEL, 0);
22122 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22123 DEBUG_LINE_SECTION_LABEL, 0);
22124 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22125 DEBUG_RANGES_SECTION_LABEL, 0);
22126 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22127 DEBUG_ADDR_SECTION_LABEL, 0);
22128 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22129 dwarf_strict
22130 ? DEBUG_MACINFO_SECTION_LABEL
22131 : DEBUG_MACRO_SECTION_LABEL, 0);
22132 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22134 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22135 vec_alloc (macinfo_table, 64);
22137 switch_to_section (text_section);
22138 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22140 /* Make sure the line number table for .text always exists. */
22141 text_section_line_info = new_line_info_table ();
22142 text_section_line_info->end_label = text_end_label;
22145 /* Called before compile () starts outputtting functions, variables
22146 and toplevel asms into assembly. */
22148 static void
22149 dwarf2out_assembly_start (void)
22151 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22152 && dwarf2out_do_cfi_asm ()
22153 && (!(flag_unwind_tables || flag_exceptions)
22154 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22155 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22158 /* A helper function for dwarf2out_finish called through
22159 htab_traverse. Assign a string its index. All strings must be
22160 collected into the table by the time index_string is called,
22161 because the indexing code relies on htab_traverse to traverse nodes
22162 in the same order for each run. */
22164 static int
22165 index_string (void **h, void *v)
22167 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22168 unsigned int *index = (unsigned int *) v;
22170 find_string_form (node);
22171 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22173 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22174 node->index = *index;
22175 *index += 1;
22177 return 1;
22180 /* A helper function for output_indirect_strings called through
22181 htab_traverse. Output the offset to a string and update the
22182 current offset. */
22184 static int
22185 output_index_string_offset (void **h, void *v)
22187 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22188 unsigned int *offset = (unsigned int *) v;
22190 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22192 /* Assert that this node has been assigned an index. */
22193 gcc_assert (node->index != NO_INDEX_ASSIGNED
22194 && node->index != NOT_INDEXED);
22195 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22196 "indexed string 0x%x: %s", node->index, node->str);
22197 *offset += strlen (node->str) + 1;
22199 return 1;
22202 /* A helper function for dwarf2out_finish called through
22203 htab_traverse. Output the indexed string. */
22205 static int
22206 output_index_string (void **h, void *v)
22208 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22209 unsigned int *cur_idx = (unsigned int *) v;
22211 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22213 /* Assert that the strings are output in the same order as their
22214 indexes were assigned. */
22215 gcc_assert (*cur_idx == node->index);
22216 assemble_string (node->str, strlen (node->str) + 1);
22217 *cur_idx += 1;
22219 return 1;
22222 /* A helper function for dwarf2out_finish called through
22223 htab_traverse. Emit one queued .debug_str string. */
22225 static int
22226 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22228 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22230 node->form = find_string_form (node);
22231 if (node->form == DW_FORM_strp && node->refcount > 0)
22233 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22234 assemble_string (node->str, strlen (node->str) + 1);
22237 return 1;
22240 /* Output the indexed string table. */
22242 static void
22243 output_indirect_strings (void)
22245 switch_to_section (debug_str_section);
22246 if (!dwarf_split_debug_info)
22247 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22248 else
22250 unsigned int offset = 0;
22251 unsigned int cur_idx = 0;
22253 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22255 switch_to_section (debug_str_offsets_section);
22256 htab_traverse_noresize (debug_str_hash,
22257 output_index_string_offset,
22258 &offset);
22259 switch_to_section (debug_str_dwo_section);
22260 htab_traverse_noresize (debug_str_hash,
22261 output_index_string,
22262 &cur_idx);
22266 /* Callback for htab_traverse to assign an index to an entry in the
22267 table, and to write that entry to the .debug_addr section. */
22269 static int
22270 output_addr_table_entry (void **slot, void *data)
22272 addr_table_entry *entry = (addr_table_entry *) *slot;
22273 unsigned int *cur_index = (unsigned int *)data;
22275 if (entry->refcount == 0)
22277 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22278 || entry->index == NOT_INDEXED);
22279 return 1;
22282 gcc_assert (entry->index == *cur_index);
22283 (*cur_index)++;
22285 switch (entry->kind)
22287 case ate_kind_rtx:
22288 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22289 "0x%x", entry->index);
22290 break;
22291 case ate_kind_rtx_dtprel:
22292 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22293 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22294 DWARF2_ADDR_SIZE,
22295 entry->addr.rtl);
22296 fputc ('\n', asm_out_file);
22297 break;
22298 case ate_kind_label:
22299 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22300 "0x%x", entry->index);
22301 break;
22302 default:
22303 gcc_unreachable ();
22305 return 1;
22308 /* Produce the .debug_addr section. */
22310 static void
22311 output_addr_table (void)
22313 unsigned int index = 0;
22314 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22315 return;
22317 switch_to_section (debug_addr_section);
22318 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22321 #if ENABLE_ASSERT_CHECKING
22322 /* Verify that all marks are clear. */
22324 static void
22325 verify_marks_clear (dw_die_ref die)
22327 dw_die_ref c;
22329 gcc_assert (! die->die_mark);
22330 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22332 #endif /* ENABLE_ASSERT_CHECKING */
22334 /* Clear the marks for a die and its children.
22335 Be cool if the mark isn't set. */
22337 static void
22338 prune_unmark_dies (dw_die_ref die)
22340 dw_die_ref c;
22342 if (die->die_mark)
22343 die->die_mark = 0;
22344 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22347 /* Given DIE that we're marking as used, find any other dies
22348 it references as attributes and mark them as used. */
22350 static void
22351 prune_unused_types_walk_attribs (dw_die_ref die)
22353 dw_attr_ref a;
22354 unsigned ix;
22356 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22358 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22360 /* A reference to another DIE.
22361 Make sure that it will get emitted.
22362 If it was broken out into a comdat group, don't follow it. */
22363 if (! AT_ref (a)->comdat_type_p
22364 || a->dw_attr == DW_AT_specification)
22365 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22367 /* Set the string's refcount to 0 so that prune_unused_types_mark
22368 accounts properly for it. */
22369 if (AT_class (a) == dw_val_class_str)
22370 a->dw_attr_val.v.val_str->refcount = 0;
22374 /* Mark the generic parameters and arguments children DIEs of DIE. */
22376 static void
22377 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22379 dw_die_ref c;
22381 if (die == NULL || die->die_child == NULL)
22382 return;
22383 c = die->die_child;
22386 if (is_template_parameter (c))
22387 prune_unused_types_mark (c, 1);
22388 c = c->die_sib;
22389 } while (c && c != die->die_child);
22392 /* Mark DIE as being used. If DOKIDS is true, then walk down
22393 to DIE's children. */
22395 static void
22396 prune_unused_types_mark (dw_die_ref die, int dokids)
22398 dw_die_ref c;
22400 if (die->die_mark == 0)
22402 /* We haven't done this node yet. Mark it as used. */
22403 die->die_mark = 1;
22404 /* If this is the DIE of a generic type instantiation,
22405 mark the children DIEs that describe its generic parms and
22406 args. */
22407 prune_unused_types_mark_generic_parms_dies (die);
22409 /* We also have to mark its parents as used.
22410 (But we don't want to mark our parent's kids due to this,
22411 unless it is a class.) */
22412 if (die->die_parent)
22413 prune_unused_types_mark (die->die_parent,
22414 class_scope_p (die->die_parent));
22416 /* Mark any referenced nodes. */
22417 prune_unused_types_walk_attribs (die);
22419 /* If this node is a specification,
22420 also mark the definition, if it exists. */
22421 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22422 prune_unused_types_mark (die->die_definition, 1);
22425 if (dokids && die->die_mark != 2)
22427 /* We need to walk the children, but haven't done so yet.
22428 Remember that we've walked the kids. */
22429 die->die_mark = 2;
22431 /* If this is an array type, we need to make sure our
22432 kids get marked, even if they're types. If we're
22433 breaking out types into comdat sections, do this
22434 for all type definitions. */
22435 if (die->die_tag == DW_TAG_array_type
22436 || (use_debug_types
22437 && is_type_die (die) && ! is_declaration_die (die)))
22438 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22439 else
22440 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22444 /* For local classes, look if any static member functions were emitted
22445 and if so, mark them. */
22447 static void
22448 prune_unused_types_walk_local_classes (dw_die_ref die)
22450 dw_die_ref c;
22452 if (die->die_mark == 2)
22453 return;
22455 switch (die->die_tag)
22457 case DW_TAG_structure_type:
22458 case DW_TAG_union_type:
22459 case DW_TAG_class_type:
22460 break;
22462 case DW_TAG_subprogram:
22463 if (!get_AT_flag (die, DW_AT_declaration)
22464 || die->die_definition != NULL)
22465 prune_unused_types_mark (die, 1);
22466 return;
22468 default:
22469 return;
22472 /* Mark children. */
22473 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22476 /* Walk the tree DIE and mark types that we actually use. */
22478 static void
22479 prune_unused_types_walk (dw_die_ref die)
22481 dw_die_ref c;
22483 /* Don't do anything if this node is already marked and
22484 children have been marked as well. */
22485 if (die->die_mark == 2)
22486 return;
22488 switch (die->die_tag)
22490 case DW_TAG_structure_type:
22491 case DW_TAG_union_type:
22492 case DW_TAG_class_type:
22493 if (die->die_perennial_p)
22494 break;
22496 for (c = die->die_parent; c; c = c->die_parent)
22497 if (c->die_tag == DW_TAG_subprogram)
22498 break;
22500 /* Finding used static member functions inside of classes
22501 is needed just for local classes, because for other classes
22502 static member function DIEs with DW_AT_specification
22503 are emitted outside of the DW_TAG_*_type. If we ever change
22504 it, we'd need to call this even for non-local classes. */
22505 if (c)
22506 prune_unused_types_walk_local_classes (die);
22508 /* It's a type node --- don't mark it. */
22509 return;
22511 case DW_TAG_const_type:
22512 case DW_TAG_packed_type:
22513 case DW_TAG_pointer_type:
22514 case DW_TAG_reference_type:
22515 case DW_TAG_rvalue_reference_type:
22516 case DW_TAG_volatile_type:
22517 case DW_TAG_typedef:
22518 case DW_TAG_array_type:
22519 case DW_TAG_interface_type:
22520 case DW_TAG_friend:
22521 case DW_TAG_variant_part:
22522 case DW_TAG_enumeration_type:
22523 case DW_TAG_subroutine_type:
22524 case DW_TAG_string_type:
22525 case DW_TAG_set_type:
22526 case DW_TAG_subrange_type:
22527 case DW_TAG_ptr_to_member_type:
22528 case DW_TAG_file_type:
22529 if (die->die_perennial_p)
22530 break;
22532 /* It's a type node --- don't mark it. */
22533 return;
22535 default:
22536 /* Mark everything else. */
22537 break;
22540 if (die->die_mark == 0)
22542 die->die_mark = 1;
22544 /* Now, mark any dies referenced from here. */
22545 prune_unused_types_walk_attribs (die);
22548 die->die_mark = 2;
22550 /* Mark children. */
22551 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22554 /* Increment the string counts on strings referred to from DIE's
22555 attributes. */
22557 static void
22558 prune_unused_types_update_strings (dw_die_ref die)
22560 dw_attr_ref a;
22561 unsigned ix;
22563 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22564 if (AT_class (a) == dw_val_class_str)
22566 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22567 s->refcount++;
22568 /* Avoid unnecessarily putting strings that are used less than
22569 twice in the hash table. */
22570 if (s->refcount
22571 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22573 void ** slot;
22574 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22575 htab_hash_string (s->str),
22576 INSERT);
22577 gcc_assert (*slot == NULL);
22578 *slot = s;
22583 /* Remove from the tree DIE any dies that aren't marked. */
22585 static void
22586 prune_unused_types_prune (dw_die_ref die)
22588 dw_die_ref c;
22590 gcc_assert (die->die_mark);
22591 prune_unused_types_update_strings (die);
22593 if (! die->die_child)
22594 return;
22596 c = die->die_child;
22597 do {
22598 dw_die_ref prev = c;
22599 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22600 if (c == die->die_child)
22602 /* No marked children between 'prev' and the end of the list. */
22603 if (prev == c)
22604 /* No marked children at all. */
22605 die->die_child = NULL;
22606 else
22608 prev->die_sib = c->die_sib;
22609 die->die_child = prev;
22611 return;
22614 if (c != prev->die_sib)
22615 prev->die_sib = c;
22616 prune_unused_types_prune (c);
22617 } while (c != die->die_child);
22620 /* Remove dies representing declarations that we never use. */
22622 static void
22623 prune_unused_types (void)
22625 unsigned int i;
22626 limbo_die_node *node;
22627 comdat_type_node *ctnode;
22628 pubname_ref pub;
22629 dw_die_ref base_type;
22631 #if ENABLE_ASSERT_CHECKING
22632 /* All the marks should already be clear. */
22633 verify_marks_clear (comp_unit_die ());
22634 for (node = limbo_die_list; node; node = node->next)
22635 verify_marks_clear (node->die);
22636 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22637 verify_marks_clear (ctnode->root_die);
22638 #endif /* ENABLE_ASSERT_CHECKING */
22640 /* Mark types that are used in global variables. */
22641 premark_types_used_by_global_vars ();
22643 /* Set the mark on nodes that are actually used. */
22644 prune_unused_types_walk (comp_unit_die ());
22645 for (node = limbo_die_list; node; node = node->next)
22646 prune_unused_types_walk (node->die);
22647 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22649 prune_unused_types_walk (ctnode->root_die);
22650 prune_unused_types_mark (ctnode->type_die, 1);
22653 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22654 are unusual in that they are pubnames that are the children of pubtypes.
22655 They should only be marked via their parent DW_TAG_enumeration_type die,
22656 not as roots in themselves. */
22657 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22658 if (pub->die->die_tag != DW_TAG_enumerator)
22659 prune_unused_types_mark (pub->die, 1);
22660 for (i = 0; base_types.iterate (i, &base_type); i++)
22661 prune_unused_types_mark (base_type, 1);
22663 if (debug_str_hash)
22664 htab_empty (debug_str_hash);
22665 if (skeleton_debug_str_hash)
22666 htab_empty (skeleton_debug_str_hash);
22667 prune_unused_types_prune (comp_unit_die ());
22668 for (node = limbo_die_list; node; node = node->next)
22669 prune_unused_types_prune (node->die);
22670 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22671 prune_unused_types_prune (ctnode->root_die);
22673 /* Leave the marks clear. */
22674 prune_unmark_dies (comp_unit_die ());
22675 for (node = limbo_die_list; node; node = node->next)
22676 prune_unmark_dies (node->die);
22677 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22678 prune_unmark_dies (ctnode->root_die);
22681 /* Set the parameter to true if there are any relative pathnames in
22682 the file table. */
22683 static int
22684 file_table_relative_p (void ** slot, void *param)
22686 bool *p = (bool *) param;
22687 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22688 if (!IS_ABSOLUTE_PATH (d->filename))
22690 *p = true;
22691 return 0;
22693 return 1;
22696 /* Helpers to manipulate hash table of comdat type units. */
22698 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22700 typedef comdat_type_node value_type;
22701 typedef comdat_type_node compare_type;
22702 static inline hashval_t hash (const value_type *);
22703 static inline bool equal (const value_type *, const compare_type *);
22706 inline hashval_t
22707 comdat_type_hasher::hash (const value_type *type_node)
22709 hashval_t h;
22710 memcpy (&h, type_node->signature, sizeof (h));
22711 return h;
22714 inline bool
22715 comdat_type_hasher::equal (const value_type *type_node_1,
22716 const compare_type *type_node_2)
22718 return (! memcmp (type_node_1->signature, type_node_2->signature,
22719 DWARF_TYPE_SIGNATURE_SIZE));
22722 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22723 to the location it would have been added, should we know its
22724 DECL_ASSEMBLER_NAME when we added other attributes. This will
22725 probably improve compactness of debug info, removing equivalent
22726 abbrevs, and hide any differences caused by deferring the
22727 computation of the assembler name, triggered by e.g. PCH. */
22729 static inline void
22730 move_linkage_attr (dw_die_ref die)
22732 unsigned ix = vec_safe_length (die->die_attr);
22733 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22735 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22736 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22738 while (--ix > 0)
22740 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22742 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22743 break;
22746 if (ix != vec_safe_length (die->die_attr) - 1)
22748 die->die_attr->pop ();
22749 die->die_attr->quick_insert (ix, linkage);
22753 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22754 referenced from typed stack ops and count how often they are used. */
22756 static void
22757 mark_base_types (dw_loc_descr_ref loc)
22759 dw_die_ref base_type = NULL;
22761 for (; loc; loc = loc->dw_loc_next)
22763 switch (loc->dw_loc_opc)
22765 case DW_OP_GNU_regval_type:
22766 case DW_OP_GNU_deref_type:
22767 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22768 break;
22769 case DW_OP_GNU_convert:
22770 case DW_OP_GNU_reinterpret:
22771 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22772 continue;
22773 /* FALLTHRU */
22774 case DW_OP_GNU_const_type:
22775 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22776 break;
22777 case DW_OP_GNU_entry_value:
22778 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22779 continue;
22780 default:
22781 continue;
22783 gcc_assert (base_type->die_parent == comp_unit_die ());
22784 if (base_type->die_mark)
22785 base_type->die_mark++;
22786 else
22788 base_types.safe_push (base_type);
22789 base_type->die_mark = 1;
22794 /* Comparison function for sorting marked base types. */
22796 static int
22797 base_type_cmp (const void *x, const void *y)
22799 dw_die_ref dx = *(const dw_die_ref *) x;
22800 dw_die_ref dy = *(const dw_die_ref *) y;
22801 unsigned int byte_size1, byte_size2;
22802 unsigned int encoding1, encoding2;
22803 if (dx->die_mark > dy->die_mark)
22804 return -1;
22805 if (dx->die_mark < dy->die_mark)
22806 return 1;
22807 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22808 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22809 if (byte_size1 < byte_size2)
22810 return 1;
22811 if (byte_size1 > byte_size2)
22812 return -1;
22813 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22814 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22815 if (encoding1 < encoding2)
22816 return 1;
22817 if (encoding1 > encoding2)
22818 return -1;
22819 return 0;
22822 /* Move base types marked by mark_base_types as early as possible
22823 in the CU, sorted by decreasing usage count both to make the
22824 uleb128 references as small as possible and to make sure they
22825 will have die_offset already computed by calc_die_sizes when
22826 sizes of typed stack loc ops is computed. */
22828 static void
22829 move_marked_base_types (void)
22831 unsigned int i;
22832 dw_die_ref base_type, die, c;
22834 if (base_types.is_empty ())
22835 return;
22837 /* Sort by decreasing usage count, they will be added again in that
22838 order later on. */
22839 base_types.qsort (base_type_cmp);
22840 die = comp_unit_die ();
22841 c = die->die_child;
22844 dw_die_ref prev = c;
22845 c = c->die_sib;
22846 while (c->die_mark)
22848 remove_child_with_prev (c, prev);
22849 /* As base types got marked, there must be at least
22850 one node other than DW_TAG_base_type. */
22851 gcc_assert (c != c->die_sib);
22852 c = c->die_sib;
22855 while (c != die->die_child);
22856 gcc_assert (die->die_child);
22857 c = die->die_child;
22858 for (i = 0; base_types.iterate (i, &base_type); i++)
22860 base_type->die_mark = 0;
22861 base_type->die_sib = c->die_sib;
22862 c->die_sib = base_type;
22863 c = base_type;
22867 /* Helper function for resolve_addr, attempt to resolve
22868 one CONST_STRING, return non-zero if not successful. Similarly verify that
22869 SYMBOL_REFs refer to variables emitted in the current CU. */
22871 static int
22872 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22874 rtx rtl = *addr;
22876 if (GET_CODE (rtl) == CONST_STRING)
22878 size_t len = strlen (XSTR (rtl, 0)) + 1;
22879 tree t = build_string (len, XSTR (rtl, 0));
22880 tree tlen = size_int (len - 1);
22881 TREE_TYPE (t)
22882 = build_array_type (char_type_node, build_index_type (tlen));
22883 rtl = lookup_constant_def (t);
22884 if (!rtl || !MEM_P (rtl))
22885 return 1;
22886 rtl = XEXP (rtl, 0);
22887 if (GET_CODE (rtl) == SYMBOL_REF
22888 && SYMBOL_REF_DECL (rtl)
22889 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22890 return 1;
22891 vec_safe_push (used_rtx_array, rtl);
22892 *addr = rtl;
22893 return 0;
22896 if (GET_CODE (rtl) == SYMBOL_REF
22897 && SYMBOL_REF_DECL (rtl))
22899 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22901 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22902 return 1;
22904 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22905 return 1;
22908 if (GET_CODE (rtl) == CONST
22909 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22910 return 1;
22912 return 0;
22915 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22916 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22917 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22919 static rtx
22920 string_cst_pool_decl (tree t)
22922 rtx rtl = output_constant_def (t, 1);
22923 unsigned char *array;
22924 dw_loc_descr_ref l;
22925 tree decl;
22926 size_t len;
22927 dw_die_ref ref;
22929 if (!rtl || !MEM_P (rtl))
22930 return NULL_RTX;
22931 rtl = XEXP (rtl, 0);
22932 if (GET_CODE (rtl) != SYMBOL_REF
22933 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22934 return NULL_RTX;
22936 decl = SYMBOL_REF_DECL (rtl);
22937 if (!lookup_decl_die (decl))
22939 len = TREE_STRING_LENGTH (t);
22940 vec_safe_push (used_rtx_array, rtl);
22941 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22942 array = (unsigned char *) ggc_alloc_atomic (len);
22943 memcpy (array, TREE_STRING_POINTER (t), len);
22944 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22945 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22946 l->dw_loc_oprnd2.v.val_vec.length = len;
22947 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22948 l->dw_loc_oprnd2.v.val_vec.array = array;
22949 add_AT_loc (ref, DW_AT_location, l);
22950 equate_decl_number_to_die (decl, ref);
22952 return rtl;
22955 /* Helper function of resolve_addr_in_expr. LOC is
22956 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22957 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22958 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22959 with DW_OP_GNU_implicit_pointer if possible
22960 and return true, if unsuccessful, return false. */
22962 static bool
22963 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22965 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22966 HOST_WIDE_INT offset = 0;
22967 dw_die_ref ref = NULL;
22968 tree decl;
22970 if (GET_CODE (rtl) == CONST
22971 && GET_CODE (XEXP (rtl, 0)) == PLUS
22972 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22974 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22975 rtl = XEXP (XEXP (rtl, 0), 0);
22977 if (GET_CODE (rtl) == CONST_STRING)
22979 size_t len = strlen (XSTR (rtl, 0)) + 1;
22980 tree t = build_string (len, XSTR (rtl, 0));
22981 tree tlen = size_int (len - 1);
22983 TREE_TYPE (t)
22984 = build_array_type (char_type_node, build_index_type (tlen));
22985 rtl = string_cst_pool_decl (t);
22986 if (!rtl)
22987 return false;
22989 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22991 decl = SYMBOL_REF_DECL (rtl);
22992 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22994 ref = lookup_decl_die (decl);
22995 if (ref && (get_AT (ref, DW_AT_location)
22996 || get_AT (ref, DW_AT_const_value)))
22998 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22999 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23000 loc->dw_loc_oprnd1.val_entry = NULL;
23001 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23002 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23003 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23004 loc->dw_loc_oprnd2.v.val_int = offset;
23005 return true;
23009 return false;
23012 /* Helper function for resolve_addr, handle one location
23013 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23014 the location list couldn't be resolved. */
23016 static bool
23017 resolve_addr_in_expr (dw_loc_descr_ref loc)
23019 dw_loc_descr_ref keep = NULL;
23020 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23021 switch (loc->dw_loc_opc)
23023 case DW_OP_addr:
23024 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23026 if ((prev == NULL
23027 || prev->dw_loc_opc == DW_OP_piece
23028 || prev->dw_loc_opc == DW_OP_bit_piece)
23029 && loc->dw_loc_next
23030 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23031 && !dwarf_strict
23032 && optimize_one_addr_into_implicit_ptr (loc))
23033 break;
23034 return false;
23036 break;
23037 case DW_OP_GNU_addr_index:
23038 case DW_OP_GNU_const_index:
23039 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23040 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23041 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23042 NULL))
23043 return false;
23044 break;
23045 case DW_OP_const4u:
23046 case DW_OP_const8u:
23047 if (loc->dtprel
23048 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23049 return false;
23050 break;
23051 case DW_OP_plus_uconst:
23052 if (size_of_loc_descr (loc)
23053 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23055 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23057 dw_loc_descr_ref repl
23058 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23059 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23060 add_loc_descr (&repl, loc->dw_loc_next);
23061 *loc = *repl;
23063 break;
23064 case DW_OP_implicit_value:
23065 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23066 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23067 return false;
23068 break;
23069 case DW_OP_GNU_implicit_pointer:
23070 case DW_OP_GNU_parameter_ref:
23071 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23073 dw_die_ref ref
23074 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23075 if (ref == NULL)
23076 return false;
23077 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23078 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23079 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23081 break;
23082 case DW_OP_GNU_const_type:
23083 case DW_OP_GNU_regval_type:
23084 case DW_OP_GNU_deref_type:
23085 case DW_OP_GNU_convert:
23086 case DW_OP_GNU_reinterpret:
23087 while (loc->dw_loc_next
23088 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23090 dw_die_ref base1, base2;
23091 unsigned enc1, enc2, size1, size2;
23092 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23093 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23094 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23095 else if (loc->dw_loc_oprnd1.val_class
23096 == dw_val_class_unsigned_const)
23097 break;
23098 else
23099 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23100 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23101 == dw_val_class_unsigned_const)
23102 break;
23103 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23104 gcc_assert (base1->die_tag == DW_TAG_base_type
23105 && base2->die_tag == DW_TAG_base_type);
23106 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23107 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23108 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23109 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23110 if (size1 == size2
23111 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23112 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23113 && loc != keep)
23114 || enc1 == enc2))
23116 /* Optimize away next DW_OP_GNU_convert after
23117 adjusting LOC's base type die reference. */
23118 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23119 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23120 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23121 else
23122 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23123 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23124 continue;
23126 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23127 point typed stack entry. */
23128 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23129 keep = loc->dw_loc_next;
23130 break;
23132 break;
23133 default:
23134 break;
23136 return true;
23139 /* Helper function of resolve_addr. DIE had DW_AT_location of
23140 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23141 and DW_OP_addr couldn't be resolved. resolve_addr has already
23142 removed the DW_AT_location attribute. This function attempts to
23143 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23144 to it or DW_AT_const_value attribute, if possible. */
23146 static void
23147 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23149 if (TREE_CODE (decl) != VAR_DECL
23150 || lookup_decl_die (decl) != die
23151 || DECL_EXTERNAL (decl)
23152 || !TREE_STATIC (decl)
23153 || DECL_INITIAL (decl) == NULL_TREE
23154 || DECL_P (DECL_INITIAL (decl))
23155 || get_AT (die, DW_AT_const_value))
23156 return;
23158 tree init = DECL_INITIAL (decl);
23159 HOST_WIDE_INT offset = 0;
23160 /* For variables that have been optimized away and thus
23161 don't have a memory location, see if we can emit
23162 DW_AT_const_value instead. */
23163 if (tree_add_const_value_attribute (die, init))
23164 return;
23165 if (dwarf_strict)
23166 return;
23167 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23168 and ADDR_EXPR refers to a decl that has DW_AT_location or
23169 DW_AT_const_value (but isn't addressable, otherwise
23170 resolving the original DW_OP_addr wouldn't fail), see if
23171 we can add DW_OP_GNU_implicit_pointer. */
23172 STRIP_NOPS (init);
23173 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23174 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23176 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23177 init = TREE_OPERAND (init, 0);
23178 STRIP_NOPS (init);
23180 if (TREE_CODE (init) != ADDR_EXPR)
23181 return;
23182 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23183 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23184 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23185 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23186 && TREE_OPERAND (init, 0) != decl))
23188 dw_die_ref ref;
23189 dw_loc_descr_ref l;
23191 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23193 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23194 if (!rtl)
23195 return;
23196 decl = SYMBOL_REF_DECL (rtl);
23198 else
23199 decl = TREE_OPERAND (init, 0);
23200 ref = lookup_decl_die (decl);
23201 if (ref == NULL
23202 || (!get_AT (ref, DW_AT_location)
23203 && !get_AT (ref, DW_AT_const_value)))
23204 return;
23205 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23206 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23207 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23208 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23209 add_AT_loc (die, DW_AT_location, l);
23213 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23214 an address in .rodata section if the string literal is emitted there,
23215 or remove the containing location list or replace DW_AT_const_value
23216 with DW_AT_location and empty location expression, if it isn't found
23217 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23218 to something that has been emitted in the current CU. */
23220 static void
23221 resolve_addr (dw_die_ref die)
23223 dw_die_ref c;
23224 dw_attr_ref a;
23225 dw_loc_list_ref *curr, *start, loc;
23226 unsigned ix;
23228 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23229 switch (AT_class (a))
23231 case dw_val_class_loc_list:
23232 start = curr = AT_loc_list_ptr (a);
23233 loc = *curr;
23234 gcc_assert (loc);
23235 /* The same list can be referenced more than once. See if we have
23236 already recorded the result from a previous pass. */
23237 if (loc->replaced)
23238 *curr = loc->dw_loc_next;
23239 else if (!loc->resolved_addr)
23241 /* As things stand, we do not expect or allow one die to
23242 reference a suffix of another die's location list chain.
23243 References must be identical or completely separate.
23244 There is therefore no need to cache the result of this
23245 pass on any list other than the first; doing so
23246 would lead to unnecessary writes. */
23247 while (*curr)
23249 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23250 if (!resolve_addr_in_expr ((*curr)->expr))
23252 dw_loc_list_ref next = (*curr)->dw_loc_next;
23253 dw_loc_descr_ref l = (*curr)->expr;
23255 if (next && (*curr)->ll_symbol)
23257 gcc_assert (!next->ll_symbol);
23258 next->ll_symbol = (*curr)->ll_symbol;
23260 if (dwarf_split_debug_info)
23261 remove_loc_list_addr_table_entries (l);
23262 *curr = next;
23264 else
23266 mark_base_types ((*curr)->expr);
23267 curr = &(*curr)->dw_loc_next;
23270 if (loc == *start)
23271 loc->resolved_addr = 1;
23272 else
23274 loc->replaced = 1;
23275 loc->dw_loc_next = *start;
23278 if (!*start)
23280 remove_AT (die, a->dw_attr);
23281 ix--;
23283 break;
23284 case dw_val_class_loc:
23286 dw_loc_descr_ref l = AT_loc (a);
23287 /* For -gdwarf-2 don't attempt to optimize
23288 DW_AT_data_member_location containing
23289 DW_OP_plus_uconst - older consumers might
23290 rely on it being that op instead of a more complex,
23291 but shorter, location description. */
23292 if ((dwarf_version > 2
23293 || a->dw_attr != DW_AT_data_member_location
23294 || l == NULL
23295 || l->dw_loc_opc != DW_OP_plus_uconst
23296 || l->dw_loc_next != NULL)
23297 && !resolve_addr_in_expr (l))
23299 if (dwarf_split_debug_info)
23300 remove_loc_list_addr_table_entries (l);
23301 if (l != NULL
23302 && l->dw_loc_next == NULL
23303 && l->dw_loc_opc == DW_OP_addr
23304 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23305 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23306 && a->dw_attr == DW_AT_location)
23308 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23309 remove_AT (die, a->dw_attr);
23310 ix--;
23311 optimize_location_into_implicit_ptr (die, decl);
23312 break;
23314 remove_AT (die, a->dw_attr);
23315 ix--;
23317 else
23318 mark_base_types (l);
23320 break;
23321 case dw_val_class_addr:
23322 if (a->dw_attr == DW_AT_const_value
23323 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23325 if (AT_index (a) != NOT_INDEXED)
23326 remove_addr_table_entry (a->dw_attr_val.val_entry);
23327 remove_AT (die, a->dw_attr);
23328 ix--;
23330 if (die->die_tag == DW_TAG_GNU_call_site
23331 && a->dw_attr == DW_AT_abstract_origin)
23333 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23334 dw_die_ref tdie = lookup_decl_die (tdecl);
23335 if (tdie == NULL
23336 && DECL_EXTERNAL (tdecl)
23337 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23339 force_decl_die (tdecl);
23340 tdie = lookup_decl_die (tdecl);
23342 if (tdie)
23344 a->dw_attr_val.val_class = dw_val_class_die_ref;
23345 a->dw_attr_val.v.val_die_ref.die = tdie;
23346 a->dw_attr_val.v.val_die_ref.external = 0;
23348 else
23350 if (AT_index (a) != NOT_INDEXED)
23351 remove_addr_table_entry (a->dw_attr_val.val_entry);
23352 remove_AT (die, a->dw_attr);
23353 ix--;
23356 break;
23357 default:
23358 break;
23361 FOR_EACH_CHILD (die, c, resolve_addr (c));
23364 /* Helper routines for optimize_location_lists.
23365 This pass tries to share identical local lists in .debug_loc
23366 section. */
23368 /* Iteratively hash operands of LOC opcode. */
23370 static hashval_t
23371 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23373 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23374 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23376 switch (loc->dw_loc_opc)
23378 case DW_OP_const4u:
23379 case DW_OP_const8u:
23380 if (loc->dtprel)
23381 goto hash_addr;
23382 /* FALLTHRU */
23383 case DW_OP_const1u:
23384 case DW_OP_const1s:
23385 case DW_OP_const2u:
23386 case DW_OP_const2s:
23387 case DW_OP_const4s:
23388 case DW_OP_const8s:
23389 case DW_OP_constu:
23390 case DW_OP_consts:
23391 case DW_OP_pick:
23392 case DW_OP_plus_uconst:
23393 case DW_OP_breg0:
23394 case DW_OP_breg1:
23395 case DW_OP_breg2:
23396 case DW_OP_breg3:
23397 case DW_OP_breg4:
23398 case DW_OP_breg5:
23399 case DW_OP_breg6:
23400 case DW_OP_breg7:
23401 case DW_OP_breg8:
23402 case DW_OP_breg9:
23403 case DW_OP_breg10:
23404 case DW_OP_breg11:
23405 case DW_OP_breg12:
23406 case DW_OP_breg13:
23407 case DW_OP_breg14:
23408 case DW_OP_breg15:
23409 case DW_OP_breg16:
23410 case DW_OP_breg17:
23411 case DW_OP_breg18:
23412 case DW_OP_breg19:
23413 case DW_OP_breg20:
23414 case DW_OP_breg21:
23415 case DW_OP_breg22:
23416 case DW_OP_breg23:
23417 case DW_OP_breg24:
23418 case DW_OP_breg25:
23419 case DW_OP_breg26:
23420 case DW_OP_breg27:
23421 case DW_OP_breg28:
23422 case DW_OP_breg29:
23423 case DW_OP_breg30:
23424 case DW_OP_breg31:
23425 case DW_OP_regx:
23426 case DW_OP_fbreg:
23427 case DW_OP_piece:
23428 case DW_OP_deref_size:
23429 case DW_OP_xderef_size:
23430 hash = iterative_hash_object (val1->v.val_int, hash);
23431 break;
23432 case DW_OP_skip:
23433 case DW_OP_bra:
23435 int offset;
23437 gcc_assert (val1->val_class == dw_val_class_loc);
23438 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23439 hash = iterative_hash_object (offset, hash);
23441 break;
23442 case DW_OP_implicit_value:
23443 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23444 switch (val2->val_class)
23446 case dw_val_class_const:
23447 hash = iterative_hash_object (val2->v.val_int, hash);
23448 break;
23449 case dw_val_class_vec:
23451 unsigned int elt_size = val2->v.val_vec.elt_size;
23452 unsigned int len = val2->v.val_vec.length;
23454 hash = iterative_hash_object (elt_size, hash);
23455 hash = iterative_hash_object (len, hash);
23456 hash = iterative_hash (val2->v.val_vec.array,
23457 len * elt_size, hash);
23459 break;
23460 case dw_val_class_const_double:
23461 hash = iterative_hash_object (val2->v.val_double.low, hash);
23462 hash = iterative_hash_object (val2->v.val_double.high, hash);
23463 break;
23464 case dw_val_class_addr:
23465 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23466 break;
23467 default:
23468 gcc_unreachable ();
23470 break;
23471 case DW_OP_bregx:
23472 case DW_OP_bit_piece:
23473 hash = iterative_hash_object (val1->v.val_int, hash);
23474 hash = iterative_hash_object (val2->v.val_int, hash);
23475 break;
23476 case DW_OP_addr:
23477 hash_addr:
23478 if (loc->dtprel)
23480 unsigned char dtprel = 0xd1;
23481 hash = iterative_hash_object (dtprel, hash);
23483 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23484 break;
23485 case DW_OP_GNU_addr_index:
23486 case DW_OP_GNU_const_index:
23488 if (loc->dtprel)
23490 unsigned char dtprel = 0xd1;
23491 hash = iterative_hash_object (dtprel, hash);
23493 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23495 break;
23496 case DW_OP_GNU_implicit_pointer:
23497 hash = iterative_hash_object (val2->v.val_int, hash);
23498 break;
23499 case DW_OP_GNU_entry_value:
23500 hash = hash_loc_operands (val1->v.val_loc, hash);
23501 break;
23502 case DW_OP_GNU_regval_type:
23503 case DW_OP_GNU_deref_type:
23505 unsigned int byte_size
23506 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23507 unsigned int encoding
23508 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23509 hash = iterative_hash_object (val1->v.val_int, hash);
23510 hash = iterative_hash_object (byte_size, hash);
23511 hash = iterative_hash_object (encoding, hash);
23513 break;
23514 case DW_OP_GNU_convert:
23515 case DW_OP_GNU_reinterpret:
23516 if (val1->val_class == dw_val_class_unsigned_const)
23518 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23519 break;
23521 /* FALLTHRU */
23522 case DW_OP_GNU_const_type:
23524 unsigned int byte_size
23525 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23526 unsigned int encoding
23527 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23528 hash = iterative_hash_object (byte_size, hash);
23529 hash = iterative_hash_object (encoding, hash);
23530 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23531 break;
23532 hash = iterative_hash_object (val2->val_class, hash);
23533 switch (val2->val_class)
23535 case dw_val_class_const:
23536 hash = iterative_hash_object (val2->v.val_int, hash);
23537 break;
23538 case dw_val_class_vec:
23540 unsigned int elt_size = val2->v.val_vec.elt_size;
23541 unsigned int len = val2->v.val_vec.length;
23543 hash = iterative_hash_object (elt_size, hash);
23544 hash = iterative_hash_object (len, hash);
23545 hash = iterative_hash (val2->v.val_vec.array,
23546 len * elt_size, hash);
23548 break;
23549 case dw_val_class_const_double:
23550 hash = iterative_hash_object (val2->v.val_double.low, hash);
23551 hash = iterative_hash_object (val2->v.val_double.high, hash);
23552 break;
23553 default:
23554 gcc_unreachable ();
23557 break;
23559 default:
23560 /* Other codes have no operands. */
23561 break;
23563 return hash;
23566 /* Iteratively hash the whole DWARF location expression LOC. */
23568 static inline hashval_t
23569 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23571 dw_loc_descr_ref l;
23572 bool sizes_computed = false;
23573 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23574 size_of_locs (loc);
23576 for (l = loc; l != NULL; l = l->dw_loc_next)
23578 enum dwarf_location_atom opc = l->dw_loc_opc;
23579 hash = iterative_hash_object (opc, hash);
23580 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23582 size_of_locs (loc);
23583 sizes_computed = true;
23585 hash = hash_loc_operands (l, hash);
23587 return hash;
23590 /* Compute hash of the whole location list LIST_HEAD. */
23592 static inline void
23593 hash_loc_list (dw_loc_list_ref list_head)
23595 dw_loc_list_ref curr = list_head;
23596 hashval_t hash = 0;
23598 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23600 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23601 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23602 if (curr->section)
23603 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23604 hash);
23605 hash = hash_locs (curr->expr, hash);
23607 list_head->hash = hash;
23610 /* Return true if X and Y opcodes have the same operands. */
23612 static inline bool
23613 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23615 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23616 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23617 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23618 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23620 switch (x->dw_loc_opc)
23622 case DW_OP_const4u:
23623 case DW_OP_const8u:
23624 if (x->dtprel)
23625 goto hash_addr;
23626 /* FALLTHRU */
23627 case DW_OP_const1u:
23628 case DW_OP_const1s:
23629 case DW_OP_const2u:
23630 case DW_OP_const2s:
23631 case DW_OP_const4s:
23632 case DW_OP_const8s:
23633 case DW_OP_constu:
23634 case DW_OP_consts:
23635 case DW_OP_pick:
23636 case DW_OP_plus_uconst:
23637 case DW_OP_breg0:
23638 case DW_OP_breg1:
23639 case DW_OP_breg2:
23640 case DW_OP_breg3:
23641 case DW_OP_breg4:
23642 case DW_OP_breg5:
23643 case DW_OP_breg6:
23644 case DW_OP_breg7:
23645 case DW_OP_breg8:
23646 case DW_OP_breg9:
23647 case DW_OP_breg10:
23648 case DW_OP_breg11:
23649 case DW_OP_breg12:
23650 case DW_OP_breg13:
23651 case DW_OP_breg14:
23652 case DW_OP_breg15:
23653 case DW_OP_breg16:
23654 case DW_OP_breg17:
23655 case DW_OP_breg18:
23656 case DW_OP_breg19:
23657 case DW_OP_breg20:
23658 case DW_OP_breg21:
23659 case DW_OP_breg22:
23660 case DW_OP_breg23:
23661 case DW_OP_breg24:
23662 case DW_OP_breg25:
23663 case DW_OP_breg26:
23664 case DW_OP_breg27:
23665 case DW_OP_breg28:
23666 case DW_OP_breg29:
23667 case DW_OP_breg30:
23668 case DW_OP_breg31:
23669 case DW_OP_regx:
23670 case DW_OP_fbreg:
23671 case DW_OP_piece:
23672 case DW_OP_deref_size:
23673 case DW_OP_xderef_size:
23674 return valx1->v.val_int == valy1->v.val_int;
23675 case DW_OP_skip:
23676 case DW_OP_bra:
23677 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23678 can cause irrelevant differences in dw_loc_addr. */
23679 gcc_assert (valx1->val_class == dw_val_class_loc
23680 && valy1->val_class == dw_val_class_loc
23681 && (dwarf_split_debug_info
23682 || x->dw_loc_addr == y->dw_loc_addr));
23683 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23684 case DW_OP_implicit_value:
23685 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23686 || valx2->val_class != valy2->val_class)
23687 return false;
23688 switch (valx2->val_class)
23690 case dw_val_class_const:
23691 return valx2->v.val_int == valy2->v.val_int;
23692 case dw_val_class_vec:
23693 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23694 && valx2->v.val_vec.length == valy2->v.val_vec.length
23695 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23696 valx2->v.val_vec.elt_size
23697 * valx2->v.val_vec.length) == 0;
23698 case dw_val_class_const_double:
23699 return valx2->v.val_double.low == valy2->v.val_double.low
23700 && valx2->v.val_double.high == valy2->v.val_double.high;
23701 case dw_val_class_addr:
23702 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23703 default:
23704 gcc_unreachable ();
23706 case DW_OP_bregx:
23707 case DW_OP_bit_piece:
23708 return valx1->v.val_int == valy1->v.val_int
23709 && valx2->v.val_int == valy2->v.val_int;
23710 case DW_OP_addr:
23711 hash_addr:
23712 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23713 case DW_OP_GNU_addr_index:
23714 case DW_OP_GNU_const_index:
23716 rtx ax1 = valx1->val_entry->addr.rtl;
23717 rtx ay1 = valy1->val_entry->addr.rtl;
23718 return rtx_equal_p (ax1, ay1);
23720 case DW_OP_GNU_implicit_pointer:
23721 return valx1->val_class == dw_val_class_die_ref
23722 && valx1->val_class == valy1->val_class
23723 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23724 && valx2->v.val_int == valy2->v.val_int;
23725 case DW_OP_GNU_entry_value:
23726 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23727 case DW_OP_GNU_const_type:
23728 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23729 || valx2->val_class != valy2->val_class)
23730 return false;
23731 switch (valx2->val_class)
23733 case dw_val_class_const:
23734 return valx2->v.val_int == valy2->v.val_int;
23735 case dw_val_class_vec:
23736 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23737 && valx2->v.val_vec.length == valy2->v.val_vec.length
23738 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23739 valx2->v.val_vec.elt_size
23740 * valx2->v.val_vec.length) == 0;
23741 case dw_val_class_const_double:
23742 return valx2->v.val_double.low == valy2->v.val_double.low
23743 && valx2->v.val_double.high == valy2->v.val_double.high;
23744 default:
23745 gcc_unreachable ();
23747 case DW_OP_GNU_regval_type:
23748 case DW_OP_GNU_deref_type:
23749 return valx1->v.val_int == valy1->v.val_int
23750 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23751 case DW_OP_GNU_convert:
23752 case DW_OP_GNU_reinterpret:
23753 if (valx1->val_class != valy1->val_class)
23754 return false;
23755 if (valx1->val_class == dw_val_class_unsigned_const)
23756 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23757 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23758 case DW_OP_GNU_parameter_ref:
23759 return valx1->val_class == dw_val_class_die_ref
23760 && valx1->val_class == valy1->val_class
23761 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23762 default:
23763 /* Other codes have no operands. */
23764 return true;
23768 /* Return true if DWARF location expressions X and Y are the same. */
23770 static inline bool
23771 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23773 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23774 if (x->dw_loc_opc != y->dw_loc_opc
23775 || x->dtprel != y->dtprel
23776 || !compare_loc_operands (x, y))
23777 break;
23778 return x == NULL && y == NULL;
23781 /* Hashtable helpers. */
23783 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23785 typedef dw_loc_list_struct value_type;
23786 typedef dw_loc_list_struct compare_type;
23787 static inline hashval_t hash (const value_type *);
23788 static inline bool equal (const value_type *, const compare_type *);
23791 /* Return precomputed hash of location list X. */
23793 inline hashval_t
23794 loc_list_hasher::hash (const value_type *x)
23796 return x->hash;
23799 /* Return true if location lists A and B are the same. */
23801 inline bool
23802 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23804 if (a == b)
23805 return 1;
23806 if (a->hash != b->hash)
23807 return 0;
23808 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23809 if (strcmp (a->begin, b->begin) != 0
23810 || strcmp (a->end, b->end) != 0
23811 || (a->section == NULL) != (b->section == NULL)
23812 || (a->section && strcmp (a->section, b->section) != 0)
23813 || !compare_locs (a->expr, b->expr))
23814 break;
23815 return a == NULL && b == NULL;
23818 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23821 /* Recursively optimize location lists referenced from DIE
23822 children and share them whenever possible. */
23824 static void
23825 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23827 dw_die_ref c;
23828 dw_attr_ref a;
23829 unsigned ix;
23830 dw_loc_list_struct **slot;
23832 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23833 if (AT_class (a) == dw_val_class_loc_list)
23835 dw_loc_list_ref list = AT_loc_list (a);
23836 /* TODO: perform some optimizations here, before hashing
23837 it and storing into the hash table. */
23838 hash_loc_list (list);
23839 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23840 if (*slot == NULL)
23841 *slot = list;
23842 else
23843 a->dw_attr_val.v.val_loc_list = *slot;
23846 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23850 /* Recursively assign each location list a unique index into the debug_addr
23851 section. */
23853 static void
23854 index_location_lists (dw_die_ref die)
23856 dw_die_ref c;
23857 dw_attr_ref a;
23858 unsigned ix;
23860 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23861 if (AT_class (a) == dw_val_class_loc_list)
23863 dw_loc_list_ref list = AT_loc_list (a);
23864 dw_loc_list_ref curr;
23865 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23867 /* Don't index an entry that has already been indexed
23868 or won't be output. */
23869 if (curr->begin_entry != NULL
23870 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23871 continue;
23873 curr->begin_entry
23874 = add_addr_table_entry (xstrdup (curr->begin),
23875 ate_kind_label);
23879 FOR_EACH_CHILD (die, c, index_location_lists (c));
23882 /* Optimize location lists referenced from DIE
23883 children and share them whenever possible. */
23885 static void
23886 optimize_location_lists (dw_die_ref die)
23888 loc_list_hash_type htab;
23889 htab.create (500);
23890 optimize_location_lists_1 (die, htab);
23891 htab.dispose ();
23894 /* Output stuff that dwarf requires at the end of every file,
23895 and generate the DWARF-2 debugging info. */
23897 static void
23898 dwarf2out_finish (const char *filename)
23900 limbo_die_node *node, *next_node;
23901 comdat_type_node *ctnode;
23902 hash_table <comdat_type_hasher> comdat_type_table;
23903 unsigned int i;
23904 dw_die_ref main_comp_unit_die;
23906 /* PCH might result in DW_AT_producer string being restored from the
23907 header compilation, so always fill it with empty string initially
23908 and overwrite only here. */
23909 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23910 producer_string = gen_producer_string ();
23911 producer->dw_attr_val.v.val_str->refcount--;
23912 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23914 gen_scheduled_generic_parms_dies ();
23915 gen_remaining_tmpl_value_param_die_attribute ();
23917 /* Add the name for the main input file now. We delayed this from
23918 dwarf2out_init to avoid complications with PCH. */
23919 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23920 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23921 add_comp_dir_attribute (comp_unit_die ());
23922 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23924 bool p = false;
23925 htab_traverse (file_table, file_table_relative_p, &p);
23926 if (p)
23927 add_comp_dir_attribute (comp_unit_die ());
23930 if (deferred_locations_list)
23931 for (i = 0; i < deferred_locations_list->length (); i++)
23933 add_location_or_const_value_attribute (
23934 (*deferred_locations_list)[i].die,
23935 (*deferred_locations_list)[i].variable,
23936 false,
23937 DW_AT_location);
23940 /* Traverse the limbo die list, and add parent/child links. The only
23941 dies without parents that should be here are concrete instances of
23942 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23943 For concrete instances, we can get the parent die from the abstract
23944 instance. */
23945 for (node = limbo_die_list; node; node = next_node)
23947 dw_die_ref die = node->die;
23948 next_node = node->next;
23950 if (die->die_parent == NULL)
23952 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23954 if (origin && origin->die_parent)
23955 add_child_die (origin->die_parent, die);
23956 else if (is_cu_die (die))
23958 else if (seen_error ())
23959 /* It's OK to be confused by errors in the input. */
23960 add_child_die (comp_unit_die (), die);
23961 else
23963 /* In certain situations, the lexical block containing a
23964 nested function can be optimized away, which results
23965 in the nested function die being orphaned. Likewise
23966 with the return type of that nested function. Force
23967 this to be a child of the containing function.
23969 It may happen that even the containing function got fully
23970 inlined and optimized out. In that case we are lost and
23971 assign the empty child. This should not be big issue as
23972 the function is likely unreachable too. */
23973 gcc_assert (node->created_for);
23975 if (DECL_P (node->created_for))
23976 origin = get_context_die (DECL_CONTEXT (node->created_for));
23977 else if (TYPE_P (node->created_for))
23978 origin = scope_die_for (node->created_for, comp_unit_die ());
23979 else
23980 origin = comp_unit_die ();
23982 add_child_die (origin, die);
23987 limbo_die_list = NULL;
23989 #if ENABLE_ASSERT_CHECKING
23991 dw_die_ref die = comp_unit_die (), c;
23992 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23994 #endif
23995 resolve_addr (comp_unit_die ());
23996 move_marked_base_types ();
23998 for (node = deferred_asm_name; node; node = node->next)
24000 tree decl = node->created_for;
24001 /* When generating LTO bytecode we can not generate new assembler
24002 names at this point and all important decls got theirs via
24003 free-lang-data. */
24004 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24005 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24007 add_linkage_attr (node->die, decl);
24008 move_linkage_attr (node->die);
24012 deferred_asm_name = NULL;
24014 /* Walk through the list of incomplete types again, trying once more to
24015 emit full debugging info for them. */
24016 retry_incomplete_types ();
24018 if (flag_eliminate_unused_debug_types)
24019 prune_unused_types ();
24021 /* Generate separate COMDAT sections for type DIEs. */
24022 if (use_debug_types)
24024 break_out_comdat_types (comp_unit_die ());
24026 /* Each new type_unit DIE was added to the limbo die list when created.
24027 Since these have all been added to comdat_type_list, clear the
24028 limbo die list. */
24029 limbo_die_list = NULL;
24031 /* For each new comdat type unit, copy declarations for incomplete
24032 types to make the new unit self-contained (i.e., no direct
24033 references to the main compile unit). */
24034 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24035 copy_decls_for_unworthy_types (ctnode->root_die);
24036 copy_decls_for_unworthy_types (comp_unit_die ());
24038 /* In the process of copying declarations from one unit to another,
24039 we may have left some declarations behind that are no longer
24040 referenced. Prune them. */
24041 prune_unused_types ();
24044 /* Generate separate CUs for each of the include files we've seen.
24045 They will go into limbo_die_list. */
24046 if (flag_eliminate_dwarf2_dups)
24047 break_out_includes (comp_unit_die ());
24049 /* Traverse the DIE's and add add sibling attributes to those DIE's
24050 that have children. */
24051 add_sibling_attributes (comp_unit_die ());
24052 for (node = limbo_die_list; node; node = node->next)
24053 add_sibling_attributes (node->die);
24054 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24055 add_sibling_attributes (ctnode->root_die);
24057 /* When splitting DWARF info, we put some attributes in the
24058 skeleton compile_unit DIE that remains in the .o, while
24059 most attributes go in the DWO compile_unit_die. */
24060 if (dwarf_split_debug_info)
24061 main_comp_unit_die = gen_compile_unit_die (NULL);
24062 else
24063 main_comp_unit_die = comp_unit_die ();
24065 /* Output a terminator label for the .text section. */
24066 switch_to_section (text_section);
24067 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24068 if (cold_text_section)
24070 switch_to_section (cold_text_section);
24071 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24074 /* We can only use the low/high_pc attributes if all of the code was
24075 in .text. */
24076 if (!have_multiple_function_sections
24077 || (dwarf_version < 3 && dwarf_strict))
24079 /* Don't add if the CU has no associated code. */
24080 if (text_section_used)
24081 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24082 text_end_label, true);
24084 else
24086 unsigned fde_idx;
24087 dw_fde_ref fde;
24088 bool range_list_added = false;
24090 if (text_section_used)
24091 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24092 text_end_label, &range_list_added, true);
24093 if (cold_text_section_used)
24094 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24095 cold_end_label, &range_list_added, true);
24097 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24099 if (DECL_IGNORED_P (fde->decl))
24100 continue;
24101 if (!fde->in_std_section)
24102 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24103 fde->dw_fde_end, &range_list_added,
24104 true);
24105 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24106 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24107 fde->dw_fde_second_end, &range_list_added,
24108 true);
24111 if (range_list_added)
24113 /* We need to give .debug_loc and .debug_ranges an appropriate
24114 "base address". Use zero so that these addresses become
24115 absolute. Historically, we've emitted the unexpected
24116 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24117 Emit both to give time for other tools to adapt. */
24118 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24119 if (! dwarf_strict && dwarf_version < 4)
24120 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24122 add_ranges (NULL);
24126 if (debug_info_level >= DINFO_LEVEL_TERSE)
24127 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24128 debug_line_section_label);
24130 if (have_macinfo)
24131 add_AT_macptr (comp_unit_die (),
24132 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24133 macinfo_section_label);
24135 if (dwarf_split_debug_info && addr_index_table != NULL)
24137 /* optimize_location_lists calculates the size of the lists,
24138 so index them first, and assign indices to the entries.
24139 Although optimize_location_lists will remove entries from
24140 the table, it only does so for duplicates, and therefore
24141 only reduces ref_counts to 1. */
24142 unsigned int index = 0;
24143 index_location_lists (comp_unit_die ());
24144 htab_traverse_noresize (addr_index_table,
24145 index_addr_table_entry, &index);
24147 if (have_location_lists)
24148 optimize_location_lists (comp_unit_die ());
24150 save_macinfo_strings ();
24152 if (dwarf_split_debug_info)
24154 unsigned int index = 0;
24156 /* Add attributes common to skeleton compile_units and
24157 type_units. Because these attributes include strings, it
24158 must be done before freezing the string table. Top-level
24159 skeleton die attrs are added when the skeleton type unit is
24160 created, so ensure it is created by this point. */
24161 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24162 (void) get_skeleton_type_unit ();
24163 htab_traverse_noresize (debug_str_hash, index_string, &index);
24166 /* Output all of the compilation units. We put the main one last so that
24167 the offsets are available to output_pubnames. */
24168 for (node = limbo_die_list; node; node = node->next)
24169 output_comp_unit (node->die, 0);
24171 comdat_type_table.create (100);
24172 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24174 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24176 /* Don't output duplicate types. */
24177 if (*slot != HTAB_EMPTY_ENTRY)
24178 continue;
24180 /* Add a pointer to the line table for the main compilation unit
24181 so that the debugger can make sense of DW_AT_decl_file
24182 attributes. */
24183 if (debug_info_level >= DINFO_LEVEL_TERSE)
24184 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24185 (!dwarf_split_debug_info
24186 ? debug_line_section_label
24187 : debug_skeleton_line_section_label));
24189 output_comdat_type_unit (ctnode);
24190 *slot = ctnode;
24192 comdat_type_table.dispose ();
24194 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24195 both the main_cu and all skeleton TUs. Making this call unconditional
24196 would end up either adding a second copy of the AT_pubnames attribute, or
24197 requiring a special case in add_top_level_skeleton_die_attrs. */
24198 if (!dwarf_split_debug_info)
24199 add_AT_pubnames (comp_unit_die ());
24201 if (dwarf_split_debug_info)
24203 int mark;
24204 unsigned char checksum[16];
24205 struct md5_ctx ctx;
24207 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24208 md5_init_ctx (&ctx);
24209 mark = 0;
24210 die_checksum (comp_unit_die (), &ctx, &mark);
24211 unmark_all_dies (comp_unit_die ());
24212 md5_finish_ctx (&ctx, checksum);
24214 /* Use the first 8 bytes of the checksum as the dwo_id,
24215 and add it to both comp-unit DIEs. */
24216 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24217 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24219 /* Add the base offset of the ranges table to the skeleton
24220 comp-unit DIE. */
24221 if (ranges_table_in_use)
24222 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24223 ranges_section_label);
24225 switch_to_section (debug_addr_section);
24226 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24227 output_addr_table ();
24230 /* Output the main compilation unit if non-empty or if .debug_macinfo
24231 or .debug_macro will be emitted. */
24232 output_comp_unit (comp_unit_die (), have_macinfo);
24234 if (dwarf_split_debug_info && info_section_emitted)
24235 output_skeleton_debug_sections (main_comp_unit_die);
24237 /* Output the abbreviation table. */
24238 if (abbrev_die_table_in_use != 1)
24240 switch_to_section (debug_abbrev_section);
24241 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24242 output_abbrev_section ();
24245 /* Output location list section if necessary. */
24246 if (have_location_lists)
24248 /* Output the location lists info. */
24249 switch_to_section (debug_loc_section);
24250 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24251 output_location_lists (comp_unit_die ());
24254 output_pubtables ();
24256 /* Output the address range information if a CU (.debug_info section)
24257 was emitted. We output an empty table even if we had no functions
24258 to put in it. This because the consumer has no way to tell the
24259 difference between an empty table that we omitted and failure to
24260 generate a table that would have contained data. */
24261 if (info_section_emitted)
24263 unsigned long aranges_length = size_of_aranges ();
24265 switch_to_section (debug_aranges_section);
24266 output_aranges (aranges_length);
24269 /* Output ranges section if necessary. */
24270 if (ranges_table_in_use)
24272 switch_to_section (debug_ranges_section);
24273 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24274 output_ranges ();
24277 /* Have to end the macro section. */
24278 if (have_macinfo)
24280 switch_to_section (debug_macinfo_section);
24281 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24282 output_macinfo ();
24283 dw2_asm_output_data (1, 0, "End compilation unit");
24286 /* Output the source line correspondence table. We must do this
24287 even if there is no line information. Otherwise, on an empty
24288 translation unit, we will generate a present, but empty,
24289 .debug_info section. IRIX 6.5 `nm' will then complain when
24290 examining the file. This is done late so that any filenames
24291 used by the debug_info section are marked as 'used'. */
24292 switch_to_section (debug_line_section);
24293 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24294 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24295 output_line_info (false);
24297 if (dwarf_split_debug_info && info_section_emitted)
24299 switch_to_section (debug_skeleton_line_section);
24300 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24301 output_line_info (true);
24304 /* If we emitted any indirect strings, output the string table too. */
24305 if (debug_str_hash || skeleton_debug_str_hash)
24306 output_indirect_strings ();
24309 #include "gt-dwarf2out.h"