hash-traits.h (free_ptr_hash): New class.
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2015 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).
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
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. */
28
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. */
34
35 /* DWARF2 Abbreviation Glossary:
36
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.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "alias.h"
64 #include "symtab.h"
65 #include "tree.h"
66 #include "fold-const.h"
67 #include "stringpool.h"
68 #include "stor-layout.h"
69 #include "varasm.h"
70 #include "hard-reg-set.h"
71 #include "function.h"
72 #include "emit-rtl.h"
73 #include "version.h"
74 #include "flags.h"
75 #include "regs.h"
76 #include "rtlhash.h"
77 #include "insn-config.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expmed.h"
81 #include "dojump.h"
82 #include "explow.h"
83 #include "calls.h"
84 #include "stmt.h"
85 #include "expr.h"
86 #include "except.h"
87 #include "dwarf2.h"
88 #include "dwarf2out.h"
89 #include "dwarf2asm.h"
90 #include "toplev.h"
91 #include "md5.h"
92 #include "tm_p.h"
93 #include "diagnostic.h"
94 #include "tree-pretty-print.h"
95 #include "debug.h"
96 #include "target.h"
97 #include "common/common-target.h"
98 #include "langhooks.h"
99 #include "plugin-api.h"
100 #include "ipa-ref.h"
101 #include "cgraph.h"
102 #include "ira.h"
103 #include "lra.h"
104 #include "dumpfile.h"
105 #include "opts.h"
106 #include "tree-dfa.h"
107 #include "gdb/gdb-index.h"
108 #include "rtl-iter.h"
109
110 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
111 static rtx_insn *last_var_location_insn;
112 static rtx_insn *cached_next_real_insn;
113 static void dwarf2out_decl (tree);
114
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
117
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
130
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
138
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
143
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
147
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
153
154 /* A pointer to the base of a table of references to declaration
155 scopes. This table is a display which tracks the nesting
156 of declaration scopes at the current scope and containing
157 scopes. This table is used to find the proper place to
158 define type declaration DIE's. */
159 static GTY(()) vec<tree, va_gc> *decl_scope_table;
160
161 /* Pointers to various DWARF2 sections. */
162 static GTY(()) section *debug_info_section;
163 static GTY(()) section *debug_skeleton_info_section;
164 static GTY(()) section *debug_abbrev_section;
165 static GTY(()) section *debug_skeleton_abbrev_section;
166 static GTY(()) section *debug_aranges_section;
167 static GTY(()) section *debug_addr_section;
168 static GTY(()) section *debug_macinfo_section;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
179
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 30
182
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
187
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
190
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
194
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
198
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
206
207
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
212
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
214
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
221 };
222
223 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
224 {
225 typedef const char *compare_type;
226
227 static hashval_t hash (indirect_string_node *);
228 static bool equal (indirect_string_node *, const char *);
229 };
230
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
232
233 /* With split_debug_info, both the comp_dir and dwo_name go in the
234 main object file, rather than the dwo, similar to the force_direct
235 parameter elsewhere but with additional complications:
236
237 1) The string is needed in both the main object file and the dwo.
238 That is, the comp_dir and dwo_name will appear in both places.
239
240 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
241 DW_FORM_GNU_str_index.
242
243 3) GCC chooses the form to use late, depending on the size and
244 reference count.
245
246 Rather than forcing the all debug string handling functions and
247 callers to deal with these complications, simply use a separate,
248 special-cased string table for any attribute that should go in the
249 main object file. This limits the complexity to just the places
250 that need it. */
251
252 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
253
254 static GTY(()) int dw2_string_counter;
255
256 /* True if the compilation unit places functions in more than one section. */
257 static GTY(()) bool have_multiple_function_sections = false;
258
259 /* Whether the default text and cold text sections have been used at all. */
260
261 static GTY(()) bool text_section_used = false;
262 static GTY(()) bool cold_text_section_used = false;
263
264 /* The default cold text section. */
265 static GTY(()) section *cold_text_section;
266
267 /* The DIE for C++14 'auto' in a function return type. */
268 static GTY(()) dw_die_ref auto_die;
269
270 /* The DIE for C++14 'decltype(auto)' in a function return type. */
271 static GTY(()) dw_die_ref decltype_auto_die;
272
273 /* Forward declarations for functions defined in this file. */
274
275 static char *stripattributes (const char *);
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
278
279 /* Personality decl of current unit. Used only when assembler does not support
280 personality CFI. */
281 static GTY(()) rtx current_unit_personality;
282
283 /* Data and reference forms for relocatable data. */
284 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
285 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
286
287 #ifndef DEBUG_FRAME_SECTION
288 #define DEBUG_FRAME_SECTION ".debug_frame"
289 #endif
290
291 #ifndef FUNC_BEGIN_LABEL
292 #define FUNC_BEGIN_LABEL "LFB"
293 #endif
294
295 #ifndef FUNC_END_LABEL
296 #define FUNC_END_LABEL "LFE"
297 #endif
298
299 #ifndef PROLOGUE_END_LABEL
300 #define PROLOGUE_END_LABEL "LPE"
301 #endif
302
303 #ifndef EPILOGUE_BEGIN_LABEL
304 #define EPILOGUE_BEGIN_LABEL "LEB"
305 #endif
306
307 #ifndef FRAME_BEGIN_LABEL
308 #define FRAME_BEGIN_LABEL "Lframe"
309 #endif
310 #define CIE_AFTER_SIZE_LABEL "LSCIE"
311 #define CIE_END_LABEL "LECIE"
312 #define FDE_LABEL "LSFDE"
313 #define FDE_AFTER_SIZE_LABEL "LASFDE"
314 #define FDE_END_LABEL "LEFDE"
315 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
316 #define LINE_NUMBER_END_LABEL "LELT"
317 #define LN_PROLOG_AS_LABEL "LASLTP"
318 #define LN_PROLOG_END_LABEL "LELTP"
319 #define DIE_LABEL_PREFIX "DW"
320 \f
321 /* Match the base name of a file to the base name of a compilation unit. */
322
323 static int
324 matches_main_base (const char *path)
325 {
326 /* Cache the last query. */
327 static const char *last_path = NULL;
328 static int last_match = 0;
329 if (path != last_path)
330 {
331 const char *base;
332 int length = base_of_path (path, &base);
333 last_path = path;
334 last_match = (length == main_input_baselength
335 && memcmp (base, main_input_basename, length) == 0);
336 }
337 return last_match;
338 }
339
340 #ifdef DEBUG_DEBUG_STRUCT
341
342 static int
343 dump_struct_debug (tree type, enum debug_info_usage usage,
344 enum debug_struct_file criterion, int generic,
345 int matches, int result)
346 {
347 /* Find the type name. */
348 tree type_decl = TYPE_STUB_DECL (type);
349 tree t = type_decl;
350 const char *name = 0;
351 if (TREE_CODE (t) == TYPE_DECL)
352 t = DECL_NAME (t);
353 if (t)
354 name = IDENTIFIER_POINTER (t);
355
356 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
357 criterion,
358 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
359 matches ? "bas" : "hdr",
360 generic ? "gen" : "ord",
361 usage == DINFO_USAGE_DFN ? ";" :
362 usage == DINFO_USAGE_DIR_USE ? "." : "*",
363 result,
364 (void*) type_decl, name);
365 return result;
366 }
367 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
368 dump_struct_debug (type, usage, criterion, generic, matches, result)
369
370 #else
371
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 (result)
374
375 #endif
376
377 /* Get the number of HOST_WIDE_INTs needed to represent the precision
378 of the number. */
379
380 static unsigned int
381 get_full_len (const wide_int &op)
382 {
383 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
384 / HOST_BITS_PER_WIDE_INT);
385 }
386
387 static bool
388 should_emit_struct_debug (tree type, enum debug_info_usage usage)
389 {
390 enum debug_struct_file criterion;
391 tree type_decl;
392 bool generic = lang_hooks.types.generic_p (type);
393
394 if (generic)
395 criterion = debug_struct_generic[usage];
396 else
397 criterion = debug_struct_ordinary[usage];
398
399 if (criterion == DINFO_STRUCT_FILE_NONE)
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
401 if (criterion == DINFO_STRUCT_FILE_ANY)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
403
404 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
405
406 if (type_decl != NULL)
407 {
408 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
410
411 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
413 }
414
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
416 }
417 \f
418 /* Return a pointer to a copy of the section string name S with all
419 attributes stripped off, and an asterisk prepended (for assemble_name). */
420
421 static inline char *
422 stripattributes (const char *s)
423 {
424 char *stripped = XNEWVEC (char, strlen (s) + 2);
425 char *p = stripped;
426
427 *p++ = '*';
428
429 while (*s && *s != ',')
430 *p++ = *s++;
431
432 *p = '\0';
433 return stripped;
434 }
435
436 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
437 switch to the data section instead, and write out a synthetic start label
438 for collect2 the first time around. */
439
440 static void
441 switch_to_eh_frame_section (bool back)
442 {
443 tree label;
444
445 #ifdef EH_FRAME_SECTION_NAME
446 if (eh_frame_section == 0)
447 {
448 int flags;
449
450 if (EH_TABLES_CAN_BE_READ_ONLY)
451 {
452 int fde_encoding;
453 int per_encoding;
454 int lsda_encoding;
455
456 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
457 /*global=*/0);
458 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
459 /*global=*/1);
460 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
461 /*global=*/0);
462 flags = ((! flag_pic
463 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
464 && (fde_encoding & 0x70) != DW_EH_PE_aligned
465 && (per_encoding & 0x70) != DW_EH_PE_absptr
466 && (per_encoding & 0x70) != DW_EH_PE_aligned
467 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
468 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
469 ? 0 : SECTION_WRITE);
470 }
471 else
472 flags = SECTION_WRITE;
473 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
474 }
475 #endif /* EH_FRAME_SECTION_NAME */
476
477 if (eh_frame_section)
478 switch_to_section (eh_frame_section);
479 else
480 {
481 /* We have no special eh_frame section. Put the information in
482 the data section and emit special labels to guide collect2. */
483 switch_to_section (data_section);
484
485 if (!back)
486 {
487 label = get_file_function_name ("F");
488 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
489 targetm.asm_out.globalize_label (asm_out_file,
490 IDENTIFIER_POINTER (label));
491 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
492 }
493 }
494 }
495
496 /* Switch [BACK] to the eh or debug frame table section, depending on
497 FOR_EH. */
498
499 static void
500 switch_to_frame_table_section (int for_eh, bool back)
501 {
502 if (for_eh)
503 switch_to_eh_frame_section (back);
504 else
505 {
506 if (!debug_frame_section)
507 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
508 SECTION_DEBUG, NULL);
509 switch_to_section (debug_frame_section);
510 }
511 }
512
513 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
514
515 enum dw_cfi_oprnd_type
516 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
517 {
518 switch (cfi)
519 {
520 case DW_CFA_nop:
521 case DW_CFA_GNU_window_save:
522 case DW_CFA_remember_state:
523 case DW_CFA_restore_state:
524 return dw_cfi_oprnd_unused;
525
526 case DW_CFA_set_loc:
527 case DW_CFA_advance_loc1:
528 case DW_CFA_advance_loc2:
529 case DW_CFA_advance_loc4:
530 case DW_CFA_MIPS_advance_loc8:
531 return dw_cfi_oprnd_addr;
532
533 case DW_CFA_offset:
534 case DW_CFA_offset_extended:
535 case DW_CFA_def_cfa:
536 case DW_CFA_offset_extended_sf:
537 case DW_CFA_def_cfa_sf:
538 case DW_CFA_restore:
539 case DW_CFA_restore_extended:
540 case DW_CFA_undefined:
541 case DW_CFA_same_value:
542 case DW_CFA_def_cfa_register:
543 case DW_CFA_register:
544 case DW_CFA_expression:
545 return dw_cfi_oprnd_reg_num;
546
547 case DW_CFA_def_cfa_offset:
548 case DW_CFA_GNU_args_size:
549 case DW_CFA_def_cfa_offset_sf:
550 return dw_cfi_oprnd_offset;
551
552 case DW_CFA_def_cfa_expression:
553 return dw_cfi_oprnd_loc;
554
555 default:
556 gcc_unreachable ();
557 }
558 }
559
560 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
561
562 enum dw_cfi_oprnd_type
563 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
564 {
565 switch (cfi)
566 {
567 case DW_CFA_def_cfa:
568 case DW_CFA_def_cfa_sf:
569 case DW_CFA_offset:
570 case DW_CFA_offset_extended_sf:
571 case DW_CFA_offset_extended:
572 return dw_cfi_oprnd_offset;
573
574 case DW_CFA_register:
575 return dw_cfi_oprnd_reg_num;
576
577 case DW_CFA_expression:
578 return dw_cfi_oprnd_loc;
579
580 default:
581 return dw_cfi_oprnd_unused;
582 }
583 }
584
585 /* Output one FDE. */
586
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
591 {
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[20], l2[20];
595
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
603 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
604 " indicating 64-bit DWARF extension");
605 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
606 "FDE Length");
607 ASM_OUTPUT_LABEL (asm_out_file, l1);
608
609 if (for_eh)
610 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
611 else
612 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
613 debug_frame_section, "FDE CIE offset");
614
615 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
616 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
617
618 if (for_eh)
619 {
620 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
621 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
622 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
623 "FDE initial location");
624 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
625 end, begin, "FDE address range");
626 }
627 else
628 {
629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
630 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
631 }
632
633 if (augmentation[0])
634 {
635 if (any_lsda_needed)
636 {
637 int size = size_of_encoded_value (lsda_encoding);
638
639 if (lsda_encoding == DW_EH_PE_aligned)
640 {
641 int offset = ( 4 /* Length */
642 + 4 /* CIE offset */
643 + 2 * size_of_encoded_value (fde_encoding)
644 + 1 /* Augmentation size */ );
645 int pad = -offset & (PTR_SIZE - 1);
646
647 size += pad;
648 gcc_assert (size_of_uleb128 (size) == 1);
649 }
650
651 dw2_asm_output_data_uleb128 (size, "Augmentation size");
652
653 if (fde->uses_eh_lsda)
654 {
655 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
656 fde->funcdef_number);
657 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
658 gen_rtx_SYMBOL_REF (Pmode, l1),
659 false,
660 "Language Specific Data Area");
661 }
662 else
663 {
664 if (lsda_encoding == DW_EH_PE_aligned)
665 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
666 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
667 "Language Specific Data Area (none)");
668 }
669 }
670 else
671 dw2_asm_output_data_uleb128 (0, "Augmentation size");
672 }
673
674 /* Loop through the Call Frame Instructions associated with this FDE. */
675 fde->dw_fde_current_label = begin;
676 {
677 size_t from, until, i;
678
679 from = 0;
680 until = vec_safe_length (fde->dw_fde_cfi);
681
682 if (fde->dw_fde_second_begin == NULL)
683 ;
684 else if (!second)
685 until = fde->dw_fde_switch_cfi_index;
686 else
687 from = fde->dw_fde_switch_cfi_index;
688
689 for (i = from; i < until; i++)
690 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
691 }
692
693 /* If we are to emit a ref/link from function bodies to their frame tables,
694 do it now. This is typically performed to make sure that tables
695 associated with functions are dragged with them and not discarded in
696 garbage collecting links. We need to do this on a per function basis to
697 cope with -ffunction-sections. */
698
699 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
700 /* Switch to the function section, emit the ref to the tables, and
701 switch *back* into the table section. */
702 switch_to_section (function_section (fde->decl));
703 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
704 switch_to_frame_table_section (for_eh, true);
705 #endif
706
707 /* Pad the FDE out to an address sized boundary. */
708 ASM_OUTPUT_ALIGN (asm_out_file,
709 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
710 ASM_OUTPUT_LABEL (asm_out_file, l2);
711
712 j += 2;
713 }
714
715 /* Return true if frame description entry FDE is needed for EH. */
716
717 static bool
718 fde_needed_for_eh_p (dw_fde_ref fde)
719 {
720 if (flag_asynchronous_unwind_tables)
721 return true;
722
723 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
724 return true;
725
726 if (fde->uses_eh_lsda)
727 return true;
728
729 /* If exceptions are enabled, we have collected nothrow info. */
730 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
731 return false;
732
733 return true;
734 }
735
736 /* Output the call frame information used to record information
737 that relates to calculating the frame pointer, and records the
738 location of saved registers. */
739
740 static void
741 output_call_frame_info (int for_eh)
742 {
743 unsigned int i;
744 dw_fde_ref fde;
745 dw_cfi_ref cfi;
746 char l1[20], l2[20], section_start_label[20];
747 bool any_lsda_needed = false;
748 char augmentation[6];
749 int augmentation_size;
750 int fde_encoding = DW_EH_PE_absptr;
751 int per_encoding = DW_EH_PE_absptr;
752 int lsda_encoding = DW_EH_PE_absptr;
753 int return_reg;
754 rtx personality = NULL;
755 int dw_cie_version;
756
757 /* Don't emit a CIE if there won't be any FDEs. */
758 if (!fde_vec)
759 return;
760
761 /* Nothing to do if the assembler's doing it all. */
762 if (dwarf2out_do_cfi_asm ())
763 return;
764
765 /* If we don't have any functions we'll want to unwind out of, don't emit
766 any EH unwind information. If we make FDEs linkonce, we may have to
767 emit an empty label for an FDE that wouldn't otherwise be emitted. We
768 want to avoid having an FDE kept around when the function it refers to
769 is discarded. Example where this matters: a primary function template
770 in C++ requires EH information, an explicit specialization doesn't. */
771 if (for_eh)
772 {
773 bool any_eh_needed = false;
774
775 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
776 {
777 if (fde->uses_eh_lsda)
778 any_eh_needed = any_lsda_needed = true;
779 else if (fde_needed_for_eh_p (fde))
780 any_eh_needed = true;
781 else if (TARGET_USES_WEAK_UNWIND_INFO)
782 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
783 }
784
785 if (!any_eh_needed)
786 return;
787 }
788
789 /* We're going to be generating comments, so turn on app. */
790 if (flag_debug_asm)
791 app_enable ();
792
793 /* Switch to the proper frame section, first time. */
794 switch_to_frame_table_section (for_eh, false);
795
796 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
797 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
798
799 /* Output the CIE. */
800 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
801 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
802 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
803 dw2_asm_output_data (4, 0xffffffff,
804 "Initial length escape value indicating 64-bit DWARF extension");
805 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
806 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
808
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
814
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
823
824 augmentation[0] = 0;
825 augmentation_size = 0;
826
827 personality = current_unit_personality;
828 if (for_eh)
829 {
830 char *p;
831
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
841
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
845
846 p = augmentation + 1;
847 if (personality)
848 {
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
852 }
853 if (any_lsda_needed)
854 {
855 *p++ = 'L';
856 augmentation_size += 1;
857 }
858 if (fde_encoding != DW_EH_PE_absptr)
859 {
860 *p++ = 'R';
861 augmentation_size += 1;
862 }
863 if (p > augmentation + 1)
864 {
865 augmentation[0] = 'z';
866 *p = '\0';
867 }
868
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
871 {
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
882
883 augmentation_size += pad;
884
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
888 }
889 }
890
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
893 {
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
896 }
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
900
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
905
906 if (augmentation[0])
907 {
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
910 {
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
916 }
917
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
921
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
925 }
926
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
929
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
934
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
937 {
938 unsigned int k;
939
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
943
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
947 }
948
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
951
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
955 }
956
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
958
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
961 {
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
965
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
967
968 if (personality)
969 {
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
972
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
979
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
983 }
984
985 if (crtl->uses_eh_lsda)
986 {
987 char lab[20];
988
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
994
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
997
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1001 }
1002 }
1003
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1006
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1009 {
1010 dw_fde_ref fde;
1011
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1021
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1025
1026 return fde;
1027 }
1028
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1031
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1035 {
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1041
1042 current_function_func_begin_label = NULL;
1043
1044 do_frame = dwarf2out_do_frame ();
1045
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1052
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1061
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1065
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1073
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1079
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1086
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1090 {
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1094
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1101 }
1102 }
1103
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1107
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1111 {
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1113
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1121 }
1122
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1126
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1130 {
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1133
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1136
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1144 }
1145
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1149
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1153 {
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1156
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1159
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1162
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1172 }
1173
1174 void
1175 dwarf2out_frame_finish (void)
1176 {
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1180
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1185 }
1186
1187 /* Note that the current function section is being used for code. */
1188
1189 static void
1190 dwarf2out_note_section_used (void)
1191 {
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1197 }
1198
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1201
1202 void
1203 dwarf2out_switch_text_section (void)
1204 {
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1207
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1209
1210 if (!in_cold_section_p)
1211 {
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1215 }
1216 else
1217 {
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1221 }
1222 have_multiple_function_sections = true;
1223
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1227
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1230
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1234
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1238
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1241
1242 var_location_switch_text_section ();
1243
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1246 }
1247 \f
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1250
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1255 };
1256
1257 /* Describe an entry into the .debug_addr section. */
1258
1259 enum ate_kind {
1260 ate_kind_rtx,
1261 ate_kind_rtx_dtprel,
1262 ate_kind_label
1263 };
1264
1265 typedef struct GTY((for_user)) addr_table_entry_struct {
1266 enum ate_kind kind;
1267 unsigned int refcount;
1268 unsigned int index;
1269 union addr_table_entry_struct_union
1270 {
1271 rtx GTY ((tag ("0"))) rtl;
1272 char * GTY ((tag ("1"))) label;
1273 }
1274 GTY ((desc ("%1.kind"))) addr;
1275 }
1276 addr_table_entry;
1277
1278 /* Location lists are ranges + location descriptions for that range,
1279 so you can track variables that are in different places over
1280 their entire life. */
1281 typedef struct GTY(()) dw_loc_list_struct {
1282 dw_loc_list_ref dw_loc_next;
1283 const char *begin; /* Label and addr_entry for start of range */
1284 addr_table_entry *begin_entry;
1285 const char *end; /* Label for end of range */
1286 char *ll_symbol; /* Label for beginning of location list.
1287 Only on head of list */
1288 const char *section; /* Section this loclist is relative to */
1289 dw_loc_descr_ref expr;
1290 hashval_t hash;
1291 /* True if all addresses in this and subsequent lists are known to be
1292 resolved. */
1293 bool resolved_addr;
1294 /* True if this list has been replaced by dw_loc_next. */
1295 bool replaced;
1296 bool emitted;
1297 /* True if the range should be emitted even if begin and end
1298 are the same. */
1299 bool force;
1300 } dw_loc_list_node;
1301
1302 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1303
1304 /* Convert a DWARF stack opcode into its string name. */
1305
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1308 {
1309 const char *name = get_DW_OP_name (op);
1310
1311 if (name != NULL)
1312 return name;
1313
1314 return "OP_<unknown>";
1315 }
1316
1317 /* Return a pointer to a newly allocated location description. Location
1318 descriptions are simple expression terms that can be strung
1319 together to form more complicated location (address) descriptions. */
1320
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323 unsigned HOST_WIDE_INT oprnd2)
1324 {
1325 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1326
1327 descr->dw_loc_opc = op;
1328 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1329 descr->dw_loc_oprnd1.val_entry = NULL;
1330 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1331 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1332 descr->dw_loc_oprnd2.val_entry = NULL;
1333 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1334
1335 return descr;
1336 }
1337
1338 /* Return a pointer to a newly allocated location description for
1339 REG and OFFSET. */
1340
1341 static inline dw_loc_descr_ref
1342 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1343 {
1344 if (reg <= 31)
1345 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1346 offset, 0);
1347 else
1348 return new_loc_descr (DW_OP_bregx, reg, offset);
1349 }
1350
1351 /* Add a location description term to a location description expression. */
1352
1353 static inline void
1354 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1355 {
1356 dw_loc_descr_ref *d;
1357
1358 /* Find the end of the chain. */
1359 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1360 ;
1361
1362 *d = descr;
1363 }
1364
1365 /* Compare two location operands for exact equality. */
1366
1367 static bool
1368 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1369 {
1370 if (a->val_class != b->val_class)
1371 return false;
1372 switch (a->val_class)
1373 {
1374 case dw_val_class_none:
1375 return true;
1376 case dw_val_class_addr:
1377 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1378
1379 case dw_val_class_offset:
1380 case dw_val_class_unsigned_const:
1381 case dw_val_class_const:
1382 case dw_val_class_range_list:
1383 case dw_val_class_lineptr:
1384 case dw_val_class_macptr:
1385 /* These are all HOST_WIDE_INT, signed or unsigned. */
1386 return a->v.val_unsigned == b->v.val_unsigned;
1387
1388 case dw_val_class_loc:
1389 return a->v.val_loc == b->v.val_loc;
1390 case dw_val_class_loc_list:
1391 return a->v.val_loc_list == b->v.val_loc_list;
1392 case dw_val_class_die_ref:
1393 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1394 case dw_val_class_fde_ref:
1395 return a->v.val_fde_index == b->v.val_fde_index;
1396 case dw_val_class_lbl_id:
1397 case dw_val_class_high_pc:
1398 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1399 case dw_val_class_str:
1400 return a->v.val_str == b->v.val_str;
1401 case dw_val_class_flag:
1402 return a->v.val_flag == b->v.val_flag;
1403 case dw_val_class_file:
1404 return a->v.val_file == b->v.val_file;
1405 case dw_val_class_decl_ref:
1406 return a->v.val_decl_ref == b->v.val_decl_ref;
1407
1408 case dw_val_class_const_double:
1409 return (a->v.val_double.high == b->v.val_double.high
1410 && a->v.val_double.low == b->v.val_double.low);
1411
1412 case dw_val_class_wide_int:
1413 return *a->v.val_wide == *b->v.val_wide;
1414
1415 case dw_val_class_vec:
1416 {
1417 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1418 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1419
1420 return (a_len == b_len
1421 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1422 }
1423
1424 case dw_val_class_data8:
1425 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1426
1427 case dw_val_class_vms_delta:
1428 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1429 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1430 }
1431 gcc_unreachable ();
1432 }
1433
1434 /* Compare two location atoms for exact equality. */
1435
1436 static bool
1437 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1438 {
1439 if (a->dw_loc_opc != b->dw_loc_opc)
1440 return false;
1441
1442 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1443 address size, but since we always allocate cleared storage it
1444 should be zero for other types of locations. */
1445 if (a->dtprel != b->dtprel)
1446 return false;
1447
1448 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1449 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1450 }
1451
1452 /* Compare two complete location expressions for exact equality. */
1453
1454 bool
1455 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1456 {
1457 while (1)
1458 {
1459 if (a == b)
1460 return true;
1461 if (a == NULL || b == NULL)
1462 return false;
1463 if (!loc_descr_equal_p_1 (a, b))
1464 return false;
1465
1466 a = a->dw_loc_next;
1467 b = b->dw_loc_next;
1468 }
1469 }
1470
1471
1472 /* Add a constant OFFSET to a location expression. */
1473
1474 static void
1475 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1476 {
1477 dw_loc_descr_ref loc;
1478 HOST_WIDE_INT *p;
1479
1480 gcc_assert (*list_head != NULL);
1481
1482 if (!offset)
1483 return;
1484
1485 /* Find the end of the chain. */
1486 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1487 ;
1488
1489 p = NULL;
1490 if (loc->dw_loc_opc == DW_OP_fbreg
1491 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1492 p = &loc->dw_loc_oprnd1.v.val_int;
1493 else if (loc->dw_loc_opc == DW_OP_bregx)
1494 p = &loc->dw_loc_oprnd2.v.val_int;
1495
1496 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1497 offset. Don't optimize if an signed integer overflow would happen. */
1498 if (p != NULL
1499 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1500 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1501 *p += offset;
1502
1503 else if (offset > 0)
1504 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1505
1506 else
1507 {
1508 loc->dw_loc_next = int_loc_descriptor (-offset);
1509 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1510 }
1511 }
1512
1513 /* Add a constant OFFSET to a location list. */
1514
1515 static void
1516 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1517 {
1518 dw_loc_list_ref d;
1519 for (d = list_head; d != NULL; d = d->dw_loc_next)
1520 loc_descr_plus_const (&d->expr, offset);
1521 }
1522
1523 #define DWARF_REF_SIZE \
1524 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1525
1526 static unsigned long int get_base_type_offset (dw_die_ref);
1527
1528 /* Return the size of a location descriptor. */
1529
1530 static unsigned long
1531 size_of_loc_descr (dw_loc_descr_ref loc)
1532 {
1533 unsigned long size = 1;
1534
1535 switch (loc->dw_loc_opc)
1536 {
1537 case DW_OP_addr:
1538 size += DWARF2_ADDR_SIZE;
1539 break;
1540 case DW_OP_GNU_addr_index:
1541 case DW_OP_GNU_const_index:
1542 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1543 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1544 break;
1545 case DW_OP_const1u:
1546 case DW_OP_const1s:
1547 size += 1;
1548 break;
1549 case DW_OP_const2u:
1550 case DW_OP_const2s:
1551 size += 2;
1552 break;
1553 case DW_OP_const4u:
1554 case DW_OP_const4s:
1555 size += 4;
1556 break;
1557 case DW_OP_const8u:
1558 case DW_OP_const8s:
1559 size += 8;
1560 break;
1561 case DW_OP_constu:
1562 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1563 break;
1564 case DW_OP_consts:
1565 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1566 break;
1567 case DW_OP_pick:
1568 size += 1;
1569 break;
1570 case DW_OP_plus_uconst:
1571 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1572 break;
1573 case DW_OP_skip:
1574 case DW_OP_bra:
1575 size += 2;
1576 break;
1577 case DW_OP_breg0:
1578 case DW_OP_breg1:
1579 case DW_OP_breg2:
1580 case DW_OP_breg3:
1581 case DW_OP_breg4:
1582 case DW_OP_breg5:
1583 case DW_OP_breg6:
1584 case DW_OP_breg7:
1585 case DW_OP_breg8:
1586 case DW_OP_breg9:
1587 case DW_OP_breg10:
1588 case DW_OP_breg11:
1589 case DW_OP_breg12:
1590 case DW_OP_breg13:
1591 case DW_OP_breg14:
1592 case DW_OP_breg15:
1593 case DW_OP_breg16:
1594 case DW_OP_breg17:
1595 case DW_OP_breg18:
1596 case DW_OP_breg19:
1597 case DW_OP_breg20:
1598 case DW_OP_breg21:
1599 case DW_OP_breg22:
1600 case DW_OP_breg23:
1601 case DW_OP_breg24:
1602 case DW_OP_breg25:
1603 case DW_OP_breg26:
1604 case DW_OP_breg27:
1605 case DW_OP_breg28:
1606 case DW_OP_breg29:
1607 case DW_OP_breg30:
1608 case DW_OP_breg31:
1609 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1610 break;
1611 case DW_OP_regx:
1612 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1613 break;
1614 case DW_OP_fbreg:
1615 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1616 break;
1617 case DW_OP_bregx:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1620 break;
1621 case DW_OP_piece:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 break;
1624 case DW_OP_bit_piece:
1625 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1626 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1627 break;
1628 case DW_OP_deref_size:
1629 case DW_OP_xderef_size:
1630 size += 1;
1631 break;
1632 case DW_OP_call2:
1633 size += 2;
1634 break;
1635 case DW_OP_call4:
1636 size += 4;
1637 break;
1638 case DW_OP_call_ref:
1639 size += DWARF_REF_SIZE;
1640 break;
1641 case DW_OP_implicit_value:
1642 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1643 + loc->dw_loc_oprnd1.v.val_unsigned;
1644 break;
1645 case DW_OP_GNU_implicit_pointer:
1646 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1647 break;
1648 case DW_OP_GNU_entry_value:
1649 {
1650 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1651 size += size_of_uleb128 (op_size) + op_size;
1652 break;
1653 }
1654 case DW_OP_GNU_const_type:
1655 {
1656 unsigned long o
1657 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1658 size += size_of_uleb128 (o) + 1;
1659 switch (loc->dw_loc_oprnd2.val_class)
1660 {
1661 case dw_val_class_vec:
1662 size += loc->dw_loc_oprnd2.v.val_vec.length
1663 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1664 break;
1665 case dw_val_class_const:
1666 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1667 break;
1668 case dw_val_class_const_double:
1669 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1670 break;
1671 case dw_val_class_wide_int:
1672 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1673 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1674 break;
1675 default:
1676 gcc_unreachable ();
1677 }
1678 break;
1679 }
1680 case DW_OP_GNU_regval_type:
1681 {
1682 unsigned long o
1683 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1684 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1685 + size_of_uleb128 (o);
1686 }
1687 break;
1688 case DW_OP_GNU_deref_type:
1689 {
1690 unsigned long o
1691 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1692 size += 1 + size_of_uleb128 (o);
1693 }
1694 break;
1695 case DW_OP_GNU_convert:
1696 case DW_OP_GNU_reinterpret:
1697 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1698 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1699 else
1700 {
1701 unsigned long o
1702 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1703 size += size_of_uleb128 (o);
1704 }
1705 break;
1706 case DW_OP_GNU_parameter_ref:
1707 size += 4;
1708 break;
1709 default:
1710 break;
1711 }
1712
1713 return size;
1714 }
1715
1716 /* Return the size of a series of location descriptors. */
1717
1718 unsigned long
1719 size_of_locs (dw_loc_descr_ref loc)
1720 {
1721 dw_loc_descr_ref l;
1722 unsigned long size;
1723
1724 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1725 field, to avoid writing to a PCH file. */
1726 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1727 {
1728 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1729 break;
1730 size += size_of_loc_descr (l);
1731 }
1732 if (! l)
1733 return size;
1734
1735 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1736 {
1737 l->dw_loc_addr = size;
1738 size += size_of_loc_descr (l);
1739 }
1740
1741 return size;
1742 }
1743
1744 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1745 static void get_ref_die_offset_label (char *, dw_die_ref);
1746 static unsigned long int get_ref_die_offset (dw_die_ref);
1747
1748 /* Output location description stack opcode's operands (if any).
1749 The for_eh_or_skip parameter controls whether register numbers are
1750 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1751 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1752 info). This should be suppressed for the cases that have not been converted
1753 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1754
1755 static void
1756 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1757 {
1758 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1759 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1760
1761 switch (loc->dw_loc_opc)
1762 {
1763 #ifdef DWARF2_DEBUGGING_INFO
1764 case DW_OP_const2u:
1765 case DW_OP_const2s:
1766 dw2_asm_output_data (2, val1->v.val_int, NULL);
1767 break;
1768 case DW_OP_const4u:
1769 if (loc->dtprel)
1770 {
1771 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1772 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1773 val1->v.val_addr);
1774 fputc ('\n', asm_out_file);
1775 break;
1776 }
1777 /* FALLTHRU */
1778 case DW_OP_const4s:
1779 dw2_asm_output_data (4, val1->v.val_int, NULL);
1780 break;
1781 case DW_OP_const8u:
1782 if (loc->dtprel)
1783 {
1784 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1785 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1786 val1->v.val_addr);
1787 fputc ('\n', asm_out_file);
1788 break;
1789 }
1790 /* FALLTHRU */
1791 case DW_OP_const8s:
1792 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1793 dw2_asm_output_data (8, val1->v.val_int, NULL);
1794 break;
1795 case DW_OP_skip:
1796 case DW_OP_bra:
1797 {
1798 int offset;
1799
1800 gcc_assert (val1->val_class == dw_val_class_loc);
1801 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1802
1803 dw2_asm_output_data (2, offset, NULL);
1804 }
1805 break;
1806 case DW_OP_implicit_value:
1807 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1808 switch (val2->val_class)
1809 {
1810 case dw_val_class_const:
1811 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1812 break;
1813 case dw_val_class_vec:
1814 {
1815 unsigned int elt_size = val2->v.val_vec.elt_size;
1816 unsigned int len = val2->v.val_vec.length;
1817 unsigned int i;
1818 unsigned char *p;
1819
1820 if (elt_size > sizeof (HOST_WIDE_INT))
1821 {
1822 elt_size /= 2;
1823 len *= 2;
1824 }
1825 for (i = 0, p = val2->v.val_vec.array;
1826 i < len;
1827 i++, p += elt_size)
1828 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1829 "fp or vector constant word %u", i);
1830 }
1831 break;
1832 case dw_val_class_const_double:
1833 {
1834 unsigned HOST_WIDE_INT first, second;
1835
1836 if (WORDS_BIG_ENDIAN)
1837 {
1838 first = val2->v.val_double.high;
1839 second = val2->v.val_double.low;
1840 }
1841 else
1842 {
1843 first = val2->v.val_double.low;
1844 second = val2->v.val_double.high;
1845 }
1846 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1847 first, NULL);
1848 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1849 second, NULL);
1850 }
1851 break;
1852 case dw_val_class_wide_int:
1853 {
1854 int i;
1855 int len = get_full_len (*val2->v.val_wide);
1856 if (WORDS_BIG_ENDIAN)
1857 for (i = len - 1; i >= 0; --i)
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 val2->v.val_wide->elt (i), NULL);
1860 else
1861 for (i = 0; i < len; ++i)
1862 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1863 val2->v.val_wide->elt (i), NULL);
1864 }
1865 break;
1866 case dw_val_class_addr:
1867 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1868 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1869 break;
1870 default:
1871 gcc_unreachable ();
1872 }
1873 break;
1874 #else
1875 case DW_OP_const2u:
1876 case DW_OP_const2s:
1877 case DW_OP_const4u:
1878 case DW_OP_const4s:
1879 case DW_OP_const8u:
1880 case DW_OP_const8s:
1881 case DW_OP_skip:
1882 case DW_OP_bra:
1883 case DW_OP_implicit_value:
1884 /* We currently don't make any attempt to make sure these are
1885 aligned properly like we do for the main unwind info, so
1886 don't support emitting things larger than a byte if we're
1887 only doing unwinding. */
1888 gcc_unreachable ();
1889 #endif
1890 case DW_OP_const1u:
1891 case DW_OP_const1s:
1892 dw2_asm_output_data (1, val1->v.val_int, NULL);
1893 break;
1894 case DW_OP_constu:
1895 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1896 break;
1897 case DW_OP_consts:
1898 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_pick:
1901 dw2_asm_output_data (1, val1->v.val_int, NULL);
1902 break;
1903 case DW_OP_plus_uconst:
1904 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1905 break;
1906 case DW_OP_breg0:
1907 case DW_OP_breg1:
1908 case DW_OP_breg2:
1909 case DW_OP_breg3:
1910 case DW_OP_breg4:
1911 case DW_OP_breg5:
1912 case DW_OP_breg6:
1913 case DW_OP_breg7:
1914 case DW_OP_breg8:
1915 case DW_OP_breg9:
1916 case DW_OP_breg10:
1917 case DW_OP_breg11:
1918 case DW_OP_breg12:
1919 case DW_OP_breg13:
1920 case DW_OP_breg14:
1921 case DW_OP_breg15:
1922 case DW_OP_breg16:
1923 case DW_OP_breg17:
1924 case DW_OP_breg18:
1925 case DW_OP_breg19:
1926 case DW_OP_breg20:
1927 case DW_OP_breg21:
1928 case DW_OP_breg22:
1929 case DW_OP_breg23:
1930 case DW_OP_breg24:
1931 case DW_OP_breg25:
1932 case DW_OP_breg26:
1933 case DW_OP_breg27:
1934 case DW_OP_breg28:
1935 case DW_OP_breg29:
1936 case DW_OP_breg30:
1937 case DW_OP_breg31:
1938 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1939 break;
1940 case DW_OP_regx:
1941 {
1942 unsigned r = val1->v.val_unsigned;
1943 if (for_eh_or_skip >= 0)
1944 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1945 gcc_assert (size_of_uleb128 (r)
1946 == size_of_uleb128 (val1->v.val_unsigned));
1947 dw2_asm_output_data_uleb128 (r, NULL);
1948 }
1949 break;
1950 case DW_OP_fbreg:
1951 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1952 break;
1953 case DW_OP_bregx:
1954 {
1955 unsigned r = val1->v.val_unsigned;
1956 if (for_eh_or_skip >= 0)
1957 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1958 gcc_assert (size_of_uleb128 (r)
1959 == size_of_uleb128 (val1->v.val_unsigned));
1960 dw2_asm_output_data_uleb128 (r, NULL);
1961 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1962 }
1963 break;
1964 case DW_OP_piece:
1965 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1966 break;
1967 case DW_OP_bit_piece:
1968 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1969 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1970 break;
1971 case DW_OP_deref_size:
1972 case DW_OP_xderef_size:
1973 dw2_asm_output_data (1, val1->v.val_int, NULL);
1974 break;
1975
1976 case DW_OP_addr:
1977 if (loc->dtprel)
1978 {
1979 if (targetm.asm_out.output_dwarf_dtprel)
1980 {
1981 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1982 DWARF2_ADDR_SIZE,
1983 val1->v.val_addr);
1984 fputc ('\n', asm_out_file);
1985 }
1986 else
1987 gcc_unreachable ();
1988 }
1989 else
1990 {
1991 #ifdef DWARF2_DEBUGGING_INFO
1992 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1993 #else
1994 gcc_unreachable ();
1995 #endif
1996 }
1997 break;
1998
1999 case DW_OP_GNU_addr_index:
2000 case DW_OP_GNU_const_index:
2001 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2002 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2003 "(index into .debug_addr)");
2004 break;
2005
2006 case DW_OP_GNU_implicit_pointer:
2007 {
2008 char label[MAX_ARTIFICIAL_LABEL_BYTES
2009 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2010 gcc_assert (val1->val_class == dw_val_class_die_ref);
2011 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2012 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2013 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2014 }
2015 break;
2016
2017 case DW_OP_GNU_entry_value:
2018 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2019 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2020 break;
2021
2022 case DW_OP_GNU_const_type:
2023 {
2024 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2025 gcc_assert (o);
2026 dw2_asm_output_data_uleb128 (o, NULL);
2027 switch (val2->val_class)
2028 {
2029 case dw_val_class_const:
2030 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2031 dw2_asm_output_data (1, l, NULL);
2032 dw2_asm_output_data (l, val2->v.val_int, NULL);
2033 break;
2034 case dw_val_class_vec:
2035 {
2036 unsigned int elt_size = val2->v.val_vec.elt_size;
2037 unsigned int len = val2->v.val_vec.length;
2038 unsigned int i;
2039 unsigned char *p;
2040
2041 l = len * elt_size;
2042 dw2_asm_output_data (1, l, NULL);
2043 if (elt_size > sizeof (HOST_WIDE_INT))
2044 {
2045 elt_size /= 2;
2046 len *= 2;
2047 }
2048 for (i = 0, p = val2->v.val_vec.array;
2049 i < len;
2050 i++, p += elt_size)
2051 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2052 "fp or vector constant word %u", i);
2053 }
2054 break;
2055 case dw_val_class_const_double:
2056 {
2057 unsigned HOST_WIDE_INT first, second;
2058 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2059
2060 dw2_asm_output_data (1, 2 * l, NULL);
2061 if (WORDS_BIG_ENDIAN)
2062 {
2063 first = val2->v.val_double.high;
2064 second = val2->v.val_double.low;
2065 }
2066 else
2067 {
2068 first = val2->v.val_double.low;
2069 second = val2->v.val_double.high;
2070 }
2071 dw2_asm_output_data (l, first, NULL);
2072 dw2_asm_output_data (l, second, NULL);
2073 }
2074 break;
2075 case dw_val_class_wide_int:
2076 {
2077 int i;
2078 int len = get_full_len (*val2->v.val_wide);
2079 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2080
2081 dw2_asm_output_data (1, len * l, NULL);
2082 if (WORDS_BIG_ENDIAN)
2083 for (i = len - 1; i >= 0; --i)
2084 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2085 else
2086 for (i = 0; i < len; ++i)
2087 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2088 }
2089 break;
2090 default:
2091 gcc_unreachable ();
2092 }
2093 }
2094 break;
2095 case DW_OP_GNU_regval_type:
2096 {
2097 unsigned r = val1->v.val_unsigned;
2098 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2099 gcc_assert (o);
2100 if (for_eh_or_skip >= 0)
2101 {
2102 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2103 gcc_assert (size_of_uleb128 (r)
2104 == size_of_uleb128 (val1->v.val_unsigned));
2105 }
2106 dw2_asm_output_data_uleb128 (r, NULL);
2107 dw2_asm_output_data_uleb128 (o, NULL);
2108 }
2109 break;
2110 case DW_OP_GNU_deref_type:
2111 {
2112 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2113 gcc_assert (o);
2114 dw2_asm_output_data (1, val1->v.val_int, NULL);
2115 dw2_asm_output_data_uleb128 (o, NULL);
2116 }
2117 break;
2118 case DW_OP_GNU_convert:
2119 case DW_OP_GNU_reinterpret:
2120 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2121 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2122 else
2123 {
2124 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2125 gcc_assert (o);
2126 dw2_asm_output_data_uleb128 (o, NULL);
2127 }
2128 break;
2129
2130 case DW_OP_GNU_parameter_ref:
2131 {
2132 unsigned long o;
2133 gcc_assert (val1->val_class == dw_val_class_die_ref);
2134 o = get_ref_die_offset (val1->v.val_die_ref.die);
2135 dw2_asm_output_data (4, o, NULL);
2136 }
2137 break;
2138
2139 default:
2140 /* Other codes have no operands. */
2141 break;
2142 }
2143 }
2144
2145 /* Output a sequence of location operations.
2146 The for_eh_or_skip parameter controls whether register numbers are
2147 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2148 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2149 info). This should be suppressed for the cases that have not been converted
2150 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2151
2152 void
2153 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2154 {
2155 for (; loc != NULL; loc = loc->dw_loc_next)
2156 {
2157 enum dwarf_location_atom opc = loc->dw_loc_opc;
2158 /* Output the opcode. */
2159 if (for_eh_or_skip >= 0
2160 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2161 {
2162 unsigned r = (opc - DW_OP_breg0);
2163 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2164 gcc_assert (r <= 31);
2165 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2166 }
2167 else if (for_eh_or_skip >= 0
2168 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2169 {
2170 unsigned r = (opc - DW_OP_reg0);
2171 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2172 gcc_assert (r <= 31);
2173 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2174 }
2175
2176 dw2_asm_output_data (1, opc,
2177 "%s", dwarf_stack_op_name (opc));
2178
2179 /* Output the operand(s) (if any). */
2180 output_loc_operands (loc, for_eh_or_skip);
2181 }
2182 }
2183
2184 /* Output location description stack opcode's operands (if any).
2185 The output is single bytes on a line, suitable for .cfi_escape. */
2186
2187 static void
2188 output_loc_operands_raw (dw_loc_descr_ref loc)
2189 {
2190 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2191 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2192
2193 switch (loc->dw_loc_opc)
2194 {
2195 case DW_OP_addr:
2196 case DW_OP_GNU_addr_index:
2197 case DW_OP_GNU_const_index:
2198 case DW_OP_implicit_value:
2199 /* We cannot output addresses in .cfi_escape, only bytes. */
2200 gcc_unreachable ();
2201
2202 case DW_OP_const1u:
2203 case DW_OP_const1s:
2204 case DW_OP_pick:
2205 case DW_OP_deref_size:
2206 case DW_OP_xderef_size:
2207 fputc (',', asm_out_file);
2208 dw2_asm_output_data_raw (1, val1->v.val_int);
2209 break;
2210
2211 case DW_OP_const2u:
2212 case DW_OP_const2s:
2213 fputc (',', asm_out_file);
2214 dw2_asm_output_data_raw (2, val1->v.val_int);
2215 break;
2216
2217 case DW_OP_const4u:
2218 case DW_OP_const4s:
2219 fputc (',', asm_out_file);
2220 dw2_asm_output_data_raw (4, val1->v.val_int);
2221 break;
2222
2223 case DW_OP_const8u:
2224 case DW_OP_const8s:
2225 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2226 fputc (',', asm_out_file);
2227 dw2_asm_output_data_raw (8, val1->v.val_int);
2228 break;
2229
2230 case DW_OP_skip:
2231 case DW_OP_bra:
2232 {
2233 int offset;
2234
2235 gcc_assert (val1->val_class == dw_val_class_loc);
2236 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2237
2238 fputc (',', asm_out_file);
2239 dw2_asm_output_data_raw (2, offset);
2240 }
2241 break;
2242
2243 case DW_OP_regx:
2244 {
2245 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2246 gcc_assert (size_of_uleb128 (r)
2247 == size_of_uleb128 (val1->v.val_unsigned));
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_uleb128_raw (r);
2250 }
2251 break;
2252
2253 case DW_OP_constu:
2254 case DW_OP_plus_uconst:
2255 case DW_OP_piece:
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2258 break;
2259
2260 case DW_OP_bit_piece:
2261 fputc (',', asm_out_file);
2262 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2263 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2264 break;
2265
2266 case DW_OP_consts:
2267 case DW_OP_breg0:
2268 case DW_OP_breg1:
2269 case DW_OP_breg2:
2270 case DW_OP_breg3:
2271 case DW_OP_breg4:
2272 case DW_OP_breg5:
2273 case DW_OP_breg6:
2274 case DW_OP_breg7:
2275 case DW_OP_breg8:
2276 case DW_OP_breg9:
2277 case DW_OP_breg10:
2278 case DW_OP_breg11:
2279 case DW_OP_breg12:
2280 case DW_OP_breg13:
2281 case DW_OP_breg14:
2282 case DW_OP_breg15:
2283 case DW_OP_breg16:
2284 case DW_OP_breg17:
2285 case DW_OP_breg18:
2286 case DW_OP_breg19:
2287 case DW_OP_breg20:
2288 case DW_OP_breg21:
2289 case DW_OP_breg22:
2290 case DW_OP_breg23:
2291 case DW_OP_breg24:
2292 case DW_OP_breg25:
2293 case DW_OP_breg26:
2294 case DW_OP_breg27:
2295 case DW_OP_breg28:
2296 case DW_OP_breg29:
2297 case DW_OP_breg30:
2298 case DW_OP_breg31:
2299 case DW_OP_fbreg:
2300 fputc (',', asm_out_file);
2301 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2302 break;
2303
2304 case DW_OP_bregx:
2305 {
2306 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2307 gcc_assert (size_of_uleb128 (r)
2308 == size_of_uleb128 (val1->v.val_unsigned));
2309 fputc (',', asm_out_file);
2310 dw2_asm_output_data_uleb128_raw (r);
2311 fputc (',', asm_out_file);
2312 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2313 }
2314 break;
2315
2316 case DW_OP_GNU_implicit_pointer:
2317 case DW_OP_GNU_entry_value:
2318 case DW_OP_GNU_const_type:
2319 case DW_OP_GNU_regval_type:
2320 case DW_OP_GNU_deref_type:
2321 case DW_OP_GNU_convert:
2322 case DW_OP_GNU_reinterpret:
2323 case DW_OP_GNU_parameter_ref:
2324 gcc_unreachable ();
2325 break;
2326
2327 default:
2328 /* Other codes have no operands. */
2329 break;
2330 }
2331 }
2332
2333 void
2334 output_loc_sequence_raw (dw_loc_descr_ref loc)
2335 {
2336 while (1)
2337 {
2338 enum dwarf_location_atom opc = loc->dw_loc_opc;
2339 /* Output the opcode. */
2340 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2341 {
2342 unsigned r = (opc - DW_OP_breg0);
2343 r = DWARF2_FRAME_REG_OUT (r, 1);
2344 gcc_assert (r <= 31);
2345 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2346 }
2347 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2348 {
2349 unsigned r = (opc - DW_OP_reg0);
2350 r = DWARF2_FRAME_REG_OUT (r, 1);
2351 gcc_assert (r <= 31);
2352 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2353 }
2354 /* Output the opcode. */
2355 fprintf (asm_out_file, "%#x", opc);
2356 output_loc_operands_raw (loc);
2357
2358 if (!loc->dw_loc_next)
2359 break;
2360 loc = loc->dw_loc_next;
2361
2362 fputc (',', asm_out_file);
2363 }
2364 }
2365
2366 /* This function builds a dwarf location descriptor sequence from a
2367 dw_cfa_location, adding the given OFFSET to the result of the
2368 expression. */
2369
2370 struct dw_loc_descr_node *
2371 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2372 {
2373 struct dw_loc_descr_node *head, *tmp;
2374
2375 offset += cfa->offset;
2376
2377 if (cfa->indirect)
2378 {
2379 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2380 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2381 head->dw_loc_oprnd1.val_entry = NULL;
2382 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2383 add_loc_descr (&head, tmp);
2384 if (offset != 0)
2385 {
2386 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2387 add_loc_descr (&head, tmp);
2388 }
2389 }
2390 else
2391 head = new_reg_loc_descr (cfa->reg, offset);
2392
2393 return head;
2394 }
2395
2396 /* This function builds a dwarf location descriptor sequence for
2397 the address at OFFSET from the CFA when stack is aligned to
2398 ALIGNMENT byte. */
2399
2400 struct dw_loc_descr_node *
2401 build_cfa_aligned_loc (dw_cfa_location *cfa,
2402 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2403 {
2404 struct dw_loc_descr_node *head;
2405 unsigned int dwarf_fp
2406 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2407
2408 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2409 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2410 {
2411 head = new_reg_loc_descr (dwarf_fp, 0);
2412 add_loc_descr (&head, int_loc_descriptor (alignment));
2413 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2414 loc_descr_plus_const (&head, offset);
2415 }
2416 else
2417 head = new_reg_loc_descr (dwarf_fp, offset);
2418 return head;
2419 }
2420 \f
2421 /* And now, the support for symbolic debugging information. */
2422
2423 /* .debug_str support. */
2424
2425 static void dwarf2out_init (const char *);
2426 static void dwarf2out_finish (const char *);
2427 static void dwarf2out_early_finish (void);
2428 static void dwarf2out_assembly_start (void);
2429 static void dwarf2out_define (unsigned int, const char *);
2430 static void dwarf2out_undef (unsigned int, const char *);
2431 static void dwarf2out_start_source_file (unsigned, const char *);
2432 static void dwarf2out_end_source_file (unsigned);
2433 static void dwarf2out_function_decl (tree);
2434 static void dwarf2out_begin_block (unsigned, unsigned);
2435 static void dwarf2out_end_block (unsigned, unsigned);
2436 static bool dwarf2out_ignore_block (const_tree);
2437 static void dwarf2out_early_global_decl (tree);
2438 static void dwarf2out_late_global_decl (tree);
2439 static void dwarf2out_type_decl (tree, int);
2440 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2441 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2442 dw_die_ref);
2443 static void dwarf2out_abstract_function (tree);
2444 static void dwarf2out_var_location (rtx_insn *);
2445 static void dwarf2out_begin_function (tree);
2446 static void dwarf2out_end_function (unsigned int);
2447 static void dwarf2out_register_main_translation_unit (tree unit);
2448 static void dwarf2out_set_name (tree, tree);
2449
2450 /* The debug hooks structure. */
2451
2452 const struct gcc_debug_hooks dwarf2_debug_hooks =
2453 {
2454 dwarf2out_init,
2455 dwarf2out_finish,
2456 dwarf2out_early_finish,
2457 dwarf2out_assembly_start,
2458 dwarf2out_define,
2459 dwarf2out_undef,
2460 dwarf2out_start_source_file,
2461 dwarf2out_end_source_file,
2462 dwarf2out_begin_block,
2463 dwarf2out_end_block,
2464 dwarf2out_ignore_block,
2465 dwarf2out_source_line,
2466 dwarf2out_begin_prologue,
2467 #if VMS_DEBUGGING_INFO
2468 dwarf2out_vms_end_prologue,
2469 dwarf2out_vms_begin_epilogue,
2470 #else
2471 debug_nothing_int_charstar,
2472 debug_nothing_int_charstar,
2473 #endif
2474 dwarf2out_end_epilogue,
2475 dwarf2out_begin_function,
2476 dwarf2out_end_function, /* end_function */
2477 dwarf2out_register_main_translation_unit,
2478 dwarf2out_function_decl, /* function_decl */
2479 dwarf2out_early_global_decl,
2480 dwarf2out_late_global_decl,
2481 dwarf2out_type_decl, /* type_decl */
2482 dwarf2out_imported_module_or_decl,
2483 debug_nothing_tree, /* deferred_inline_function */
2484 /* The DWARF 2 backend tries to reduce debugging bloat by not
2485 emitting the abstract description of inline functions until
2486 something tries to reference them. */
2487 dwarf2out_abstract_function, /* outlining_inline_function */
2488 debug_nothing_rtx_code_label, /* label */
2489 debug_nothing_int, /* handle_pch */
2490 dwarf2out_var_location,
2491 dwarf2out_switch_text_section,
2492 dwarf2out_set_name,
2493 1, /* start_end_main_source_file */
2494 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2495 };
2496 \f
2497 /* NOTE: In the comments in this file, many references are made to
2498 "Debugging Information Entries". This term is abbreviated as `DIE'
2499 throughout the remainder of this file. */
2500
2501 /* An internal representation of the DWARF output is built, and then
2502 walked to generate the DWARF debugging info. The walk of the internal
2503 representation is done after the entire program has been compiled.
2504 The types below are used to describe the internal representation. */
2505
2506 /* Whether to put type DIEs into their own section .debug_types instead
2507 of making them part of the .debug_info section. Only supported for
2508 Dwarf V4 or higher and the user didn't disable them through
2509 -fno-debug-types-section. It is more efficient to put them in a
2510 separate comdat sections since the linker will then be able to
2511 remove duplicates. But not all tools support .debug_types sections
2512 yet. */
2513
2514 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2515
2516 /* Various DIE's use offsets relative to the beginning of the
2517 .debug_info section to refer to each other. */
2518
2519 typedef long int dw_offset;
2520
2521 /* Define typedefs here to avoid circular dependencies. */
2522
2523 typedef struct dw_attr_struct *dw_attr_ref;
2524 typedef struct dw_line_info_struct *dw_line_info_ref;
2525 typedef struct pubname_struct *pubname_ref;
2526 typedef struct dw_ranges_struct *dw_ranges_ref;
2527 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2528 typedef struct comdat_type_struct *comdat_type_node_ref;
2529
2530 /* The entries in the line_info table more-or-less mirror the opcodes
2531 that are used in the real dwarf line table. Arrays of these entries
2532 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2533 supported. */
2534
2535 enum dw_line_info_opcode {
2536 /* Emit DW_LNE_set_address; the operand is the label index. */
2537 LI_set_address,
2538
2539 /* Emit a row to the matrix with the given line. This may be done
2540 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2541 special opcodes. */
2542 LI_set_line,
2543
2544 /* Emit a DW_LNS_set_file. */
2545 LI_set_file,
2546
2547 /* Emit a DW_LNS_set_column. */
2548 LI_set_column,
2549
2550 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2551 LI_negate_stmt,
2552
2553 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2554 LI_set_prologue_end,
2555 LI_set_epilogue_begin,
2556
2557 /* Emit a DW_LNE_set_discriminator. */
2558 LI_set_discriminator
2559 };
2560
2561 typedef struct GTY(()) dw_line_info_struct {
2562 enum dw_line_info_opcode opcode;
2563 unsigned int val;
2564 } dw_line_info_entry;
2565
2566
2567 typedef struct GTY(()) dw_line_info_table_struct {
2568 /* The label that marks the end of this section. */
2569 const char *end_label;
2570
2571 /* The values for the last row of the matrix, as collected in the table.
2572 These are used to minimize the changes to the next row. */
2573 unsigned int file_num;
2574 unsigned int line_num;
2575 unsigned int column_num;
2576 int discrim_num;
2577 bool is_stmt;
2578 bool in_use;
2579
2580 vec<dw_line_info_entry, va_gc> *entries;
2581 } dw_line_info_table;
2582
2583 typedef dw_line_info_table *dw_line_info_table_p;
2584
2585
2586 /* Each DIE attribute has a field specifying the attribute kind,
2587 a link to the next attribute in the chain, and an attribute value.
2588 Attributes are typically linked below the DIE they modify. */
2589
2590 typedef struct GTY(()) dw_attr_struct {
2591 enum dwarf_attribute dw_attr;
2592 dw_val_node dw_attr_val;
2593 }
2594 dw_attr_node;
2595
2596
2597 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2598 The children of each node form a circular list linked by
2599 die_sib. die_child points to the node *before* the "first" child node. */
2600
2601 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2602 union die_symbol_or_type_node
2603 {
2604 const char * GTY ((tag ("0"))) die_symbol;
2605 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2606 }
2607 GTY ((desc ("%0.comdat_type_p"))) die_id;
2608 vec<dw_attr_node, va_gc> *die_attr;
2609 dw_die_ref die_parent;
2610 dw_die_ref die_child;
2611 dw_die_ref die_sib;
2612 dw_die_ref die_definition; /* ref from a specification to its definition */
2613 dw_offset die_offset;
2614 unsigned long die_abbrev;
2615 int die_mark;
2616 unsigned int decl_id;
2617 enum dwarf_tag die_tag;
2618 /* Die is used and must not be pruned as unused. */
2619 BOOL_BITFIELD die_perennial_p : 1;
2620 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2621 /* Lots of spare bits. */
2622 }
2623 die_node;
2624
2625 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2626 static bool early_dwarf;
2627 struct set_early_dwarf {
2628 bool saved;
2629 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2630 ~set_early_dwarf () { early_dwarf = saved; }
2631 };
2632
2633 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2634 #define FOR_EACH_CHILD(die, c, expr) do { \
2635 c = die->die_child; \
2636 if (c) do { \
2637 c = c->die_sib; \
2638 expr; \
2639 } while (c != die->die_child); \
2640 } while (0)
2641
2642 /* The pubname structure */
2643
2644 typedef struct GTY(()) pubname_struct {
2645 dw_die_ref die;
2646 const char *name;
2647 }
2648 pubname_entry;
2649
2650
2651 struct GTY(()) dw_ranges_struct {
2652 /* If this is positive, it's a block number, otherwise it's a
2653 bitwise-negated index into dw_ranges_by_label. */
2654 int num;
2655 };
2656
2657 /* A structure to hold a macinfo entry. */
2658
2659 typedef struct GTY(()) macinfo_struct {
2660 unsigned char code;
2661 unsigned HOST_WIDE_INT lineno;
2662 const char *info;
2663 }
2664 macinfo_entry;
2665
2666
2667 struct GTY(()) dw_ranges_by_label_struct {
2668 const char *begin;
2669 const char *end;
2670 };
2671
2672 /* The comdat type node structure. */
2673 typedef struct GTY(()) comdat_type_struct
2674 {
2675 dw_die_ref root_die;
2676 dw_die_ref type_die;
2677 dw_die_ref skeleton_die;
2678 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2679 struct comdat_type_struct *next;
2680 }
2681 comdat_type_node;
2682
2683 /* A list of DIEs for which we can't determine ancestry (parent_die
2684 field) just yet. Later in dwarf2out_finish we will fill in the
2685 missing bits. */
2686 typedef struct GTY(()) limbo_die_struct {
2687 dw_die_ref die;
2688 /* The tree for which this DIE was created. We use this to
2689 determine ancestry later. */
2690 tree created_for;
2691 struct limbo_die_struct *next;
2692 }
2693 limbo_die_node;
2694
2695 typedef struct skeleton_chain_struct
2696 {
2697 dw_die_ref old_die;
2698 dw_die_ref new_die;
2699 struct skeleton_chain_struct *parent;
2700 }
2701 skeleton_chain_node;
2702
2703 /* Define a macro which returns nonzero for a TYPE_DECL which was
2704 implicitly generated for a type.
2705
2706 Note that, unlike the C front-end (which generates a NULL named
2707 TYPE_DECL node for each complete tagged type, each array type,
2708 and each function type node created) the C++ front-end generates
2709 a _named_ TYPE_DECL node for each tagged type node created.
2710 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2711 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2712 front-end, but for each type, tagged or not. */
2713
2714 #define TYPE_DECL_IS_STUB(decl) \
2715 (DECL_NAME (decl) == NULL_TREE \
2716 || (DECL_ARTIFICIAL (decl) \
2717 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2718 /* This is necessary for stub decls that \
2719 appear in nested inline functions. */ \
2720 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2721 && (decl_ultimate_origin (decl) \
2722 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2723
2724 /* Information concerning the compilation unit's programming
2725 language, and compiler version. */
2726
2727 /* Fixed size portion of the DWARF compilation unit header. */
2728 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2729 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2730
2731 /* Fixed size portion of the DWARF comdat type unit header. */
2732 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2733 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2734 + DWARF_OFFSET_SIZE)
2735
2736 /* Fixed size portion of public names info. */
2737 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2738
2739 /* Fixed size portion of the address range info. */
2740 #define DWARF_ARANGES_HEADER_SIZE \
2741 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2742 DWARF2_ADDR_SIZE * 2) \
2743 - DWARF_INITIAL_LENGTH_SIZE)
2744
2745 /* Size of padding portion in the address range info. It must be
2746 aligned to twice the pointer size. */
2747 #define DWARF_ARANGES_PAD_SIZE \
2748 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2749 DWARF2_ADDR_SIZE * 2) \
2750 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2751
2752 /* Use assembler line directives if available. */
2753 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2754 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2755 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2756 #else
2757 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2758 #endif
2759 #endif
2760
2761 /* Minimum line offset in a special line info. opcode.
2762 This value was chosen to give a reasonable range of values. */
2763 #define DWARF_LINE_BASE -10
2764
2765 /* First special line opcode - leave room for the standard opcodes. */
2766 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2767
2768 /* Range of line offsets in a special line info. opcode. */
2769 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2770
2771 /* Flag that indicates the initial value of the is_stmt_start flag.
2772 In the present implementation, we do not mark any lines as
2773 the beginning of a source statement, because that information
2774 is not made available by the GCC front-end. */
2775 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2776
2777 /* Maximum number of operations per instruction bundle. */
2778 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2779 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2780 #endif
2781
2782 /* This location is used by calc_die_sizes() to keep track
2783 the offset of each DIE within the .debug_info section. */
2784 static unsigned long next_die_offset;
2785
2786 /* Record the root of the DIE's built for the current compilation unit. */
2787 static GTY(()) dw_die_ref single_comp_unit_die;
2788
2789 /* A list of type DIEs that have been separated into comdat sections. */
2790 static GTY(()) comdat_type_node *comdat_type_list;
2791
2792 /* A list of DIEs with a NULL parent waiting to be relocated. */
2793 static GTY(()) limbo_die_node *limbo_die_list;
2794
2795 /* A list of DIEs for which we may have to generate
2796 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2797 static GTY(()) limbo_die_node *deferred_asm_name;
2798
2799 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2800 {
2801 typedef const char *compare_type;
2802
2803 static hashval_t hash (dwarf_file_data *);
2804 static bool equal (dwarf_file_data *, const char *);
2805 };
2806
2807 /* Filenames referenced by this compilation unit. */
2808 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2809
2810 struct decl_die_hasher : ggc_hasher<die_node *>
2811 {
2812 typedef tree compare_type;
2813
2814 static hashval_t hash (die_node *);
2815 static bool equal (die_node *, tree);
2816 };
2817 /* A hash table of references to DIE's that describe declarations.
2818 The key is a DECL_UID() which is a unique number identifying each decl. */
2819 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2820
2821 struct block_die_hasher : ggc_hasher<die_struct *>
2822 {
2823 static hashval_t hash (die_struct *);
2824 static bool equal (die_struct *, die_struct *);
2825 };
2826
2827 /* A hash table of references to DIE's that describe COMMON blocks.
2828 The key is DECL_UID() ^ die_parent. */
2829 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2830
2831 typedef struct GTY(()) die_arg_entry_struct {
2832 dw_die_ref die;
2833 tree arg;
2834 } die_arg_entry;
2835
2836
2837 /* Node of the variable location list. */
2838 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2839 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2840 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2841 in mode of the EXPR_LIST node and first EXPR_LIST operand
2842 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2843 location or NULL for padding. For larger bitsizes,
2844 mode is 0 and first operand is a CONCAT with bitsize
2845 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2846 NULL as second operand. */
2847 rtx GTY (()) loc;
2848 const char * GTY (()) label;
2849 struct var_loc_node * GTY (()) next;
2850 };
2851
2852 /* Variable location list. */
2853 struct GTY ((for_user)) var_loc_list_def {
2854 struct var_loc_node * GTY (()) first;
2855
2856 /* Pointer to the last but one or last element of the
2857 chained list. If the list is empty, both first and
2858 last are NULL, if the list contains just one node
2859 or the last node certainly is not redundant, it points
2860 to the last node, otherwise points to the last but one.
2861 Do not mark it for GC because it is marked through the chain. */
2862 struct var_loc_node * GTY ((skip ("%h"))) last;
2863
2864 /* Pointer to the last element before section switch,
2865 if NULL, either sections weren't switched or first
2866 is after section switch. */
2867 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2868
2869 /* DECL_UID of the variable decl. */
2870 unsigned int decl_id;
2871 };
2872 typedef struct var_loc_list_def var_loc_list;
2873
2874 /* Call argument location list. */
2875 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2876 rtx GTY (()) call_arg_loc_note;
2877 const char * GTY (()) label;
2878 tree GTY (()) block;
2879 bool tail_call_p;
2880 rtx GTY (()) symbol_ref;
2881 struct call_arg_loc_node * GTY (()) next;
2882 };
2883
2884
2885 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2886 {
2887 typedef const_tree compare_type;
2888
2889 static hashval_t hash (var_loc_list *);
2890 static bool equal (var_loc_list *, const_tree);
2891 };
2892
2893 /* Table of decl location linked lists. */
2894 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2895
2896 /* Head and tail of call_arg_loc chain. */
2897 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2898 static struct call_arg_loc_node *call_arg_loc_last;
2899
2900 /* Number of call sites in the current function. */
2901 static int call_site_count = -1;
2902 /* Number of tail call sites in the current function. */
2903 static int tail_call_site_count = -1;
2904
2905 /* A cached location list. */
2906 struct GTY ((for_user)) cached_dw_loc_list_def {
2907 /* The DECL_UID of the decl that this entry describes. */
2908 unsigned int decl_id;
2909
2910 /* The cached location list. */
2911 dw_loc_list_ref loc_list;
2912 };
2913 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2914
2915 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2916 {
2917
2918 typedef const_tree compare_type;
2919
2920 static hashval_t hash (cached_dw_loc_list *);
2921 static bool equal (cached_dw_loc_list *, const_tree);
2922 };
2923
2924 /* Table of cached location lists. */
2925 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2926
2927 /* A pointer to the base of a list of references to DIE's that
2928 are uniquely identified by their tag, presence/absence of
2929 children DIE's, and list of attribute/value pairs. */
2930 static GTY((length ("abbrev_die_table_allocated")))
2931 dw_die_ref *abbrev_die_table;
2932
2933 /* Number of elements currently allocated for abbrev_die_table. */
2934 static GTY(()) unsigned abbrev_die_table_allocated;
2935
2936 /* Number of elements in abbrev_die_table currently in use. */
2937 static GTY(()) unsigned abbrev_die_table_in_use;
2938
2939 /* Size (in elements) of increments by which we may expand the
2940 abbrev_die_table. */
2941 #define ABBREV_DIE_TABLE_INCREMENT 256
2942
2943 /* A global counter for generating labels for line number data. */
2944 static unsigned int line_info_label_num;
2945
2946 /* The current table to which we should emit line number information
2947 for the current function. This will be set up at the beginning of
2948 assembly for the function. */
2949 static dw_line_info_table *cur_line_info_table;
2950
2951 /* The two default tables of line number info. */
2952 static GTY(()) dw_line_info_table *text_section_line_info;
2953 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2954
2955 /* The set of all non-default tables of line number info. */
2956 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2957
2958 /* A flag to tell pubnames/types export if there is an info section to
2959 refer to. */
2960 static bool info_section_emitted;
2961
2962 /* A pointer to the base of a table that contains a list of publicly
2963 accessible names. */
2964 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2965
2966 /* A pointer to the base of a table that contains a list of publicly
2967 accessible types. */
2968 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2969
2970 /* A pointer to the base of a table that contains a list of macro
2971 defines/undefines (and file start/end markers). */
2972 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2973
2974 /* True if .debug_macinfo or .debug_macros section is going to be
2975 emitted. */
2976 #define have_macinfo \
2977 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2978 && !macinfo_table->is_empty ())
2979
2980 /* Array of dies for which we should generate .debug_ranges info. */
2981 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2982
2983 /* Number of elements currently allocated for ranges_table. */
2984 static GTY(()) unsigned ranges_table_allocated;
2985
2986 /* Number of elements in ranges_table currently in use. */
2987 static GTY(()) unsigned ranges_table_in_use;
2988
2989 /* Array of pairs of labels referenced in ranges_table. */
2990 static GTY ((length ("ranges_by_label_allocated")))
2991 dw_ranges_by_label_ref ranges_by_label;
2992
2993 /* Number of elements currently allocated for ranges_by_label. */
2994 static GTY(()) unsigned ranges_by_label_allocated;
2995
2996 /* Number of elements in ranges_by_label currently in use. */
2997 static GTY(()) unsigned ranges_by_label_in_use;
2998
2999 /* Size (in elements) of increments by which we may expand the
3000 ranges_table. */
3001 #define RANGES_TABLE_INCREMENT 64
3002
3003 /* Whether we have location lists that need outputting */
3004 static GTY(()) bool have_location_lists;
3005
3006 /* Unique label counter. */
3007 static GTY(()) unsigned int loclabel_num;
3008
3009 /* Unique label counter for point-of-call tables. */
3010 static GTY(()) unsigned int poc_label_num;
3011
3012 /* The last file entry emitted by maybe_emit_file(). */
3013 static GTY(()) struct dwarf_file_data * last_emitted_file;
3014
3015 /* Number of internal labels generated by gen_internal_sym(). */
3016 static GTY(()) int label_num;
3017
3018 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3019
3020 /* Instances of generic types for which we need to generate debug
3021 info that describe their generic parameters and arguments. That
3022 generation needs to happen once all types are properly laid out so
3023 we do it at the end of compilation. */
3024 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3025
3026 /* Offset from the "steady-state frame pointer" to the frame base,
3027 within the current function. */
3028 static HOST_WIDE_INT frame_pointer_fb_offset;
3029 static bool frame_pointer_fb_offset_valid;
3030
3031 static vec<dw_die_ref> base_types;
3032
3033 /* Flags to represent a set of attribute classes for attributes that represent
3034 a scalar value (bounds, pointers, ...). */
3035 enum dw_scalar_form
3036 {
3037 dw_scalar_form_constant = 0x01,
3038 dw_scalar_form_exprloc = 0x02,
3039 dw_scalar_form_reference = 0x04
3040 };
3041
3042 /* Forward declarations for functions defined in this file. */
3043
3044 static int is_pseudo_reg (const_rtx);
3045 static tree type_main_variant (tree);
3046 static int is_tagged_type (const_tree);
3047 static const char *dwarf_tag_name (unsigned);
3048 static const char *dwarf_attr_name (unsigned);
3049 static const char *dwarf_form_name (unsigned);
3050 static tree decl_ultimate_origin (const_tree);
3051 static tree decl_class_context (tree);
3052 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3053 static inline enum dw_val_class AT_class (dw_attr_ref);
3054 static inline unsigned int AT_index (dw_attr_ref);
3055 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3056 static inline unsigned AT_flag (dw_attr_ref);
3057 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3058 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3059 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3060 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3061 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3062 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3063 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3064 unsigned int, unsigned char *);
3065 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3066 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3067 static inline const char *AT_string (dw_attr_ref);
3068 static enum dwarf_form AT_string_form (dw_attr_ref);
3069 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3070 static void add_AT_specification (dw_die_ref, dw_die_ref);
3071 static inline dw_die_ref AT_ref (dw_attr_ref);
3072 static inline int AT_ref_external (dw_attr_ref);
3073 static inline void set_AT_ref_external (dw_attr_ref, int);
3074 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3075 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3076 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3077 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3078 dw_loc_list_ref);
3079 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3080 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3081 static void remove_addr_table_entry (addr_table_entry *);
3082 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3083 static inline rtx AT_addr (dw_attr_ref);
3084 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3085 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3086 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3087 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3088 unsigned HOST_WIDE_INT);
3089 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3090 unsigned long, bool);
3091 static inline const char *AT_lbl (dw_attr_ref);
3092 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3093 static const char *get_AT_low_pc (dw_die_ref);
3094 static const char *get_AT_hi_pc (dw_die_ref);
3095 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3096 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3097 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3098 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3099 static bool is_cxx (void);
3100 static bool is_fortran (void);
3101 static bool is_ada (void);
3102 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3103 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3104 static void add_child_die (dw_die_ref, dw_die_ref);
3105 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3106 static dw_die_ref lookup_type_die (tree);
3107 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3108 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3109 static void equate_type_number_to_die (tree, dw_die_ref);
3110 static dw_die_ref lookup_decl_die (tree);
3111 static var_loc_list *lookup_decl_loc (const_tree);
3112 static void equate_decl_number_to_die (tree, dw_die_ref);
3113 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3114 static void print_spaces (FILE *);
3115 static void print_die (dw_die_ref, FILE *);
3116 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3117 static dw_die_ref pop_compile_unit (dw_die_ref);
3118 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3119 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3120 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3121 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3122 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3123 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3124 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3125 struct md5_ctx *, int *);
3126 struct checksum_attributes;
3127 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3128 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3129 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3130 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3131 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3132 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3133 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3134 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3135 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3136 static void compute_section_prefix (dw_die_ref);
3137 static int is_type_die (dw_die_ref);
3138 static int is_comdat_die (dw_die_ref);
3139 static int is_symbol_die (dw_die_ref);
3140 static inline bool is_template_instantiation (dw_die_ref);
3141 static void assign_symbol_names (dw_die_ref);
3142 static void break_out_includes (dw_die_ref);
3143 static int is_declaration_die (dw_die_ref);
3144 static int should_move_die_to_comdat (dw_die_ref);
3145 static dw_die_ref clone_as_declaration (dw_die_ref);
3146 static dw_die_ref clone_die (dw_die_ref);
3147 static dw_die_ref clone_tree (dw_die_ref);
3148 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3149 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3150 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3151 static dw_die_ref generate_skeleton (dw_die_ref);
3152 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3153 dw_die_ref,
3154 dw_die_ref);
3155 static void break_out_comdat_types (dw_die_ref);
3156 static void copy_decls_for_unworthy_types (dw_die_ref);
3157
3158 static void add_sibling_attributes (dw_die_ref);
3159 static void output_location_lists (dw_die_ref);
3160 static int constant_size (unsigned HOST_WIDE_INT);
3161 static unsigned long size_of_die (dw_die_ref);
3162 static void calc_die_sizes (dw_die_ref);
3163 static void calc_base_type_die_sizes (void);
3164 static void mark_dies (dw_die_ref);
3165 static void unmark_dies (dw_die_ref);
3166 static void unmark_all_dies (dw_die_ref);
3167 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3168 static unsigned long size_of_aranges (void);
3169 static enum dwarf_form value_format (dw_attr_ref);
3170 static void output_value_format (dw_attr_ref);
3171 static void output_abbrev_section (void);
3172 static void output_die_abbrevs (unsigned long, dw_die_ref);
3173 static void output_die_symbol (dw_die_ref);
3174 static void output_die (dw_die_ref);
3175 static void output_compilation_unit_header (void);
3176 static void output_comp_unit (dw_die_ref, int);
3177 static void output_comdat_type_unit (comdat_type_node *);
3178 static const char *dwarf2_name (tree, int);
3179 static void add_pubname (tree, dw_die_ref);
3180 static void add_enumerator_pubname (const char *, dw_die_ref);
3181 static void add_pubname_string (const char *, dw_die_ref);
3182 static void add_pubtype (tree, dw_die_ref);
3183 static void output_pubnames (vec<pubname_entry, va_gc> *);
3184 static void output_aranges (unsigned long);
3185 static unsigned int add_ranges_num (int);
3186 static unsigned int add_ranges (const_tree);
3187 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3188 bool *, bool);
3189 static void output_ranges (void);
3190 static dw_line_info_table *new_line_info_table (void);
3191 static void output_line_info (bool);
3192 static void output_file_names (void);
3193 static dw_die_ref base_type_die (tree);
3194 static int is_base_type (tree);
3195 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3196 static int decl_quals (const_tree);
3197 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3198 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3199 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3200 static int type_is_enum (const_tree);
3201 static unsigned int dbx_reg_number (const_rtx);
3202 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3203 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3204 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3205 enum var_init_status);
3206 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3207 enum var_init_status);
3208 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3209 enum var_init_status);
3210 static int is_based_loc (const_rtx);
3211 static bool resolve_one_addr (rtx *);
3212 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3213 enum var_init_status);
3214 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3215 enum var_init_status);
3216 struct loc_descr_context;
3217 static dw_loc_list_ref loc_list_from_tree (tree, int,
3218 const struct loc_descr_context *);
3219 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3220 const struct loc_descr_context *);
3221 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3222 static tree field_type (const_tree);
3223 static unsigned int simple_type_align_in_bits (const_tree);
3224 static unsigned int simple_decl_align_in_bits (const_tree);
3225 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3226 static HOST_WIDE_INT field_byte_offset (const_tree);
3227 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3228 dw_loc_list_ref);
3229 static void add_data_member_location_attribute (dw_die_ref, tree);
3230 static bool add_const_value_attribute (dw_die_ref, rtx);
3231 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3232 static void insert_wide_int (const wide_int &, unsigned char *, int);
3233 static void insert_float (const_rtx, unsigned char *);
3234 static rtx rtl_for_decl_location (tree);
3235 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3236 enum dwarf_attribute);
3237 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3238 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3239 static void add_name_attribute (dw_die_ref, const char *);
3240 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3241 static void add_comp_dir_attribute (dw_die_ref);
3242 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3243 const struct loc_descr_context *);
3244 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3245 const struct loc_descr_context *);
3246 static void add_subscript_info (dw_die_ref, tree, bool);
3247 static void add_byte_size_attribute (dw_die_ref, tree);
3248 static void add_bit_offset_attribute (dw_die_ref, tree);
3249 static void add_bit_size_attribute (dw_die_ref, tree);
3250 static void add_prototyped_attribute (dw_die_ref, tree);
3251 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3252 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3253 static void add_src_coords_attributes (dw_die_ref, tree);
3254 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3255 static void push_decl_scope (tree);
3256 static void pop_decl_scope (void);
3257 static dw_die_ref scope_die_for (tree, dw_die_ref);
3258 static inline int local_scope_p (dw_die_ref);
3259 static inline int class_scope_p (dw_die_ref);
3260 static inline int class_or_namespace_scope_p (dw_die_ref);
3261 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3262 static void add_calling_convention_attribute (dw_die_ref, tree);
3263 static const char *type_tag (const_tree);
3264 static tree member_declared_type (const_tree);
3265 #if 0
3266 static const char *decl_start_label (tree);
3267 #endif
3268 static void gen_array_type_die (tree, dw_die_ref);
3269 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3270 #if 0
3271 static void gen_entry_point_die (tree, dw_die_ref);
3272 #endif
3273 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3274 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3275 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3276 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3277 static void gen_formal_types_die (tree, dw_die_ref);
3278 static void gen_subprogram_die (tree, dw_die_ref);
3279 static void gen_variable_die (tree, tree, dw_die_ref);
3280 static void gen_const_die (tree, dw_die_ref);
3281 static void gen_label_die (tree, dw_die_ref);
3282 static void gen_lexical_block_die (tree, dw_die_ref);
3283 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3284 static void gen_field_die (tree, dw_die_ref);
3285 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3286 static dw_die_ref gen_compile_unit_die (const char *);
3287 static void gen_inheritance_die (tree, tree, dw_die_ref);
3288 static void gen_member_die (tree, dw_die_ref);
3289 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3290 enum debug_info_usage);
3291 static void gen_subroutine_type_die (tree, dw_die_ref);
3292 static void gen_typedef_die (tree, dw_die_ref);
3293 static void gen_type_die (tree, dw_die_ref);
3294 static void gen_block_die (tree, dw_die_ref);
3295 static void decls_for_scope (tree, dw_die_ref);
3296 static inline int is_redundant_typedef (const_tree);
3297 static bool is_naming_typedef_decl (const_tree);
3298 static inline dw_die_ref get_context_die (tree);
3299 static void gen_namespace_die (tree, dw_die_ref);
3300 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3301 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3302 static dw_die_ref force_decl_die (tree);
3303 static dw_die_ref force_type_die (tree);
3304 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3305 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3306 static struct dwarf_file_data * lookup_filename (const char *);
3307 static void retry_incomplete_types (void);
3308 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3309 static void gen_generic_params_dies (tree);
3310 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3311 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3312 static void splice_child_die (dw_die_ref, dw_die_ref);
3313 static int file_info_cmp (const void *, const void *);
3314 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3315 const char *, const char *);
3316 static void output_loc_list (dw_loc_list_ref);
3317 static char *gen_internal_sym (const char *);
3318 static bool want_pubnames (void);
3319
3320 static void prune_unmark_dies (dw_die_ref);
3321 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3322 static void prune_unused_types_mark (dw_die_ref, int);
3323 static void prune_unused_types_walk (dw_die_ref);
3324 static void prune_unused_types_walk_attribs (dw_die_ref);
3325 static void prune_unused_types_prune (dw_die_ref);
3326 static void prune_unused_types (void);
3327 static int maybe_emit_file (struct dwarf_file_data *fd);
3328 static inline const char *AT_vms_delta1 (dw_attr_ref);
3329 static inline const char *AT_vms_delta2 (dw_attr_ref);
3330 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3331 const char *, const char *);
3332 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3333 static void gen_remaining_tmpl_value_param_die_attribute (void);
3334 static bool generic_type_p (tree);
3335 static void schedule_generic_params_dies_gen (tree t);
3336 static void gen_scheduled_generic_parms_dies (void);
3337
3338 static const char *comp_dir_string (void);
3339
3340 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3341
3342 /* enum for tracking thread-local variables whose address is really an offset
3343 relative to the TLS pointer, which will need link-time relocation, but will
3344 not need relocation by the DWARF consumer. */
3345
3346 enum dtprel_bool
3347 {
3348 dtprel_false = 0,
3349 dtprel_true = 1
3350 };
3351
3352 /* Return the operator to use for an address of a variable. For dtprel_true, we
3353 use DW_OP_const*. For regular variables, which need both link-time
3354 relocation and consumer-level relocation (e.g., to account for shared objects
3355 loaded at a random address), we use DW_OP_addr*. */
3356
3357 static inline enum dwarf_location_atom
3358 dw_addr_op (enum dtprel_bool dtprel)
3359 {
3360 if (dtprel == dtprel_true)
3361 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3362 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3363 else
3364 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3365 }
3366
3367 /* Return a pointer to a newly allocated address location description. If
3368 dwarf_split_debug_info is true, then record the address with the appropriate
3369 relocation. */
3370 static inline dw_loc_descr_ref
3371 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3372 {
3373 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3374
3375 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3376 ref->dw_loc_oprnd1.v.val_addr = addr;
3377 ref->dtprel = dtprel;
3378 if (dwarf_split_debug_info)
3379 ref->dw_loc_oprnd1.val_entry
3380 = add_addr_table_entry (addr,
3381 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3382 else
3383 ref->dw_loc_oprnd1.val_entry = NULL;
3384
3385 return ref;
3386 }
3387
3388 /* Section names used to hold DWARF debugging information. */
3389
3390 #ifndef DEBUG_INFO_SECTION
3391 #define DEBUG_INFO_SECTION ".debug_info"
3392 #endif
3393 #ifndef DEBUG_DWO_INFO_SECTION
3394 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3395 #endif
3396 #ifndef DEBUG_ABBREV_SECTION
3397 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3398 #endif
3399 #ifndef DEBUG_DWO_ABBREV_SECTION
3400 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3401 #endif
3402 #ifndef DEBUG_ARANGES_SECTION
3403 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3404 #endif
3405 #ifndef DEBUG_ADDR_SECTION
3406 #define DEBUG_ADDR_SECTION ".debug_addr"
3407 #endif
3408 #ifndef DEBUG_NORM_MACINFO_SECTION
3409 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3410 #endif
3411 #ifndef DEBUG_DWO_MACINFO_SECTION
3412 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3413 #endif
3414 #ifndef DEBUG_MACINFO_SECTION
3415 #define DEBUG_MACINFO_SECTION \
3416 (!dwarf_split_debug_info \
3417 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3418 #endif
3419 #ifndef DEBUG_NORM_MACRO_SECTION
3420 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3421 #endif
3422 #ifndef DEBUG_DWO_MACRO_SECTION
3423 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3424 #endif
3425 #ifndef DEBUG_MACRO_SECTION
3426 #define DEBUG_MACRO_SECTION \
3427 (!dwarf_split_debug_info \
3428 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3429 #endif
3430 #ifndef DEBUG_LINE_SECTION
3431 #define DEBUG_LINE_SECTION ".debug_line"
3432 #endif
3433 #ifndef DEBUG_DWO_LINE_SECTION
3434 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3435 #endif
3436 #ifndef DEBUG_LOC_SECTION
3437 #define DEBUG_LOC_SECTION ".debug_loc"
3438 #endif
3439 #ifndef DEBUG_DWO_LOC_SECTION
3440 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3441 #endif
3442 #ifndef DEBUG_PUBNAMES_SECTION
3443 #define DEBUG_PUBNAMES_SECTION \
3444 ((debug_generate_pub_sections == 2) \
3445 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3446 #endif
3447 #ifndef DEBUG_PUBTYPES_SECTION
3448 #define DEBUG_PUBTYPES_SECTION \
3449 ((debug_generate_pub_sections == 2) \
3450 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3451 #endif
3452 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3453 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3454 #ifndef DEBUG_STR_OFFSETS_SECTION
3455 #define DEBUG_STR_OFFSETS_SECTION \
3456 (!dwarf_split_debug_info \
3457 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3458 #endif
3459 #ifndef DEBUG_STR_DWO_SECTION
3460 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3461 #endif
3462 #ifndef DEBUG_STR_SECTION
3463 #define DEBUG_STR_SECTION ".debug_str"
3464 #endif
3465 #ifndef DEBUG_RANGES_SECTION
3466 #define DEBUG_RANGES_SECTION ".debug_ranges"
3467 #endif
3468
3469 /* Standard ELF section names for compiled code and data. */
3470 #ifndef TEXT_SECTION_NAME
3471 #define TEXT_SECTION_NAME ".text"
3472 #endif
3473
3474 /* Section flags for .debug_macinfo/.debug_macro section. */
3475 #define DEBUG_MACRO_SECTION_FLAGS \
3476 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3477
3478 /* Section flags for .debug_str section. */
3479 #define DEBUG_STR_SECTION_FLAGS \
3480 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3481 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3482 : SECTION_DEBUG)
3483
3484 /* Section flags for .debug_str.dwo section. */
3485 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3486
3487 /* Labels we insert at beginning sections we can reference instead of
3488 the section names themselves. */
3489
3490 #ifndef TEXT_SECTION_LABEL
3491 #define TEXT_SECTION_LABEL "Ltext"
3492 #endif
3493 #ifndef COLD_TEXT_SECTION_LABEL
3494 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3495 #endif
3496 #ifndef DEBUG_LINE_SECTION_LABEL
3497 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3498 #endif
3499 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3500 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3501 #endif
3502 #ifndef DEBUG_INFO_SECTION_LABEL
3503 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3504 #endif
3505 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3506 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3507 #endif
3508 #ifndef DEBUG_ABBREV_SECTION_LABEL
3509 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3510 #endif
3511 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3512 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3513 #endif
3514 #ifndef DEBUG_ADDR_SECTION_LABEL
3515 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3516 #endif
3517 #ifndef DEBUG_LOC_SECTION_LABEL
3518 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3519 #endif
3520 #ifndef DEBUG_RANGES_SECTION_LABEL
3521 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3522 #endif
3523 #ifndef DEBUG_MACINFO_SECTION_LABEL
3524 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3525 #endif
3526 #ifndef DEBUG_MACRO_SECTION_LABEL
3527 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3528 #endif
3529 #define SKELETON_COMP_DIE_ABBREV 1
3530 #define SKELETON_TYPE_DIE_ABBREV 2
3531
3532 /* Definitions of defaults for formats and names of various special
3533 (artificial) labels which may be generated within this file (when the -g
3534 options is used and DWARF2_DEBUGGING_INFO is in effect.
3535 If necessary, these may be overridden from within the tm.h file, but
3536 typically, overriding these defaults is unnecessary. */
3537
3538 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3539 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3540 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3541 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3542 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3543 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3544 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3545 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3546 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3547 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3551 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3552
3553 #ifndef TEXT_END_LABEL
3554 #define TEXT_END_LABEL "Letext"
3555 #endif
3556 #ifndef COLD_END_LABEL
3557 #define COLD_END_LABEL "Letext_cold"
3558 #endif
3559 #ifndef BLOCK_BEGIN_LABEL
3560 #define BLOCK_BEGIN_LABEL "LBB"
3561 #endif
3562 #ifndef BLOCK_END_LABEL
3563 #define BLOCK_END_LABEL "LBE"
3564 #endif
3565 #ifndef LINE_CODE_LABEL
3566 #define LINE_CODE_LABEL "LM"
3567 #endif
3568
3569 \f
3570 /* Return the root of the DIE's built for the current compilation unit. */
3571 static dw_die_ref
3572 comp_unit_die (void)
3573 {
3574 if (!single_comp_unit_die)
3575 single_comp_unit_die = gen_compile_unit_die (NULL);
3576 return single_comp_unit_die;
3577 }
3578
3579 /* We allow a language front-end to designate a function that is to be
3580 called to "demangle" any name before it is put into a DIE. */
3581
3582 static const char *(*demangle_name_func) (const char *);
3583
3584 void
3585 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3586 {
3587 demangle_name_func = func;
3588 }
3589
3590 /* Test if rtl node points to a pseudo register. */
3591
3592 static inline int
3593 is_pseudo_reg (const_rtx rtl)
3594 {
3595 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3596 || (GET_CODE (rtl) == SUBREG
3597 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3598 }
3599
3600 /* Return a reference to a type, with its const and volatile qualifiers
3601 removed. */
3602
3603 static inline tree
3604 type_main_variant (tree type)
3605 {
3606 type = TYPE_MAIN_VARIANT (type);
3607
3608 /* ??? There really should be only one main variant among any group of
3609 variants of a given type (and all of the MAIN_VARIANT values for all
3610 members of the group should point to that one type) but sometimes the C
3611 front-end messes this up for array types, so we work around that bug
3612 here. */
3613 if (TREE_CODE (type) == ARRAY_TYPE)
3614 while (type != TYPE_MAIN_VARIANT (type))
3615 type = TYPE_MAIN_VARIANT (type);
3616
3617 return type;
3618 }
3619
3620 /* Return nonzero if the given type node represents a tagged type. */
3621
3622 static inline int
3623 is_tagged_type (const_tree type)
3624 {
3625 enum tree_code code = TREE_CODE (type);
3626
3627 return (code == RECORD_TYPE || code == UNION_TYPE
3628 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3629 }
3630
3631 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3632
3633 static void
3634 get_ref_die_offset_label (char *label, dw_die_ref ref)
3635 {
3636 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3637 }
3638
3639 /* Return die_offset of a DIE reference to a base type. */
3640
3641 static unsigned long int
3642 get_base_type_offset (dw_die_ref ref)
3643 {
3644 if (ref->die_offset)
3645 return ref->die_offset;
3646 if (comp_unit_die ()->die_abbrev)
3647 {
3648 calc_base_type_die_sizes ();
3649 gcc_assert (ref->die_offset);
3650 }
3651 return ref->die_offset;
3652 }
3653
3654 /* Return die_offset of a DIE reference other than base type. */
3655
3656 static unsigned long int
3657 get_ref_die_offset (dw_die_ref ref)
3658 {
3659 gcc_assert (ref->die_offset);
3660 return ref->die_offset;
3661 }
3662
3663 /* Convert a DIE tag into its string name. */
3664
3665 static const char *
3666 dwarf_tag_name (unsigned int tag)
3667 {
3668 const char *name = get_DW_TAG_name (tag);
3669
3670 if (name != NULL)
3671 return name;
3672
3673 return "DW_TAG_<unknown>";
3674 }
3675
3676 /* Convert a DWARF attribute code into its string name. */
3677
3678 static const char *
3679 dwarf_attr_name (unsigned int attr)
3680 {
3681 const char *name;
3682
3683 switch (attr)
3684 {
3685 #if VMS_DEBUGGING_INFO
3686 case DW_AT_HP_prologue:
3687 return "DW_AT_HP_prologue";
3688 #else
3689 case DW_AT_MIPS_loop_unroll_factor:
3690 return "DW_AT_MIPS_loop_unroll_factor";
3691 #endif
3692
3693 #if VMS_DEBUGGING_INFO
3694 case DW_AT_HP_epilogue:
3695 return "DW_AT_HP_epilogue";
3696 #else
3697 case DW_AT_MIPS_stride:
3698 return "DW_AT_MIPS_stride";
3699 #endif
3700 }
3701
3702 name = get_DW_AT_name (attr);
3703
3704 if (name != NULL)
3705 return name;
3706
3707 return "DW_AT_<unknown>";
3708 }
3709
3710 /* Convert a DWARF value form code into its string name. */
3711
3712 static const char *
3713 dwarf_form_name (unsigned int form)
3714 {
3715 const char *name = get_DW_FORM_name (form);
3716
3717 if (name != NULL)
3718 return name;
3719
3720 return "DW_FORM_<unknown>";
3721 }
3722 \f
3723 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3724 instance of an inlined instance of a decl which is local to an inline
3725 function, so we have to trace all of the way back through the origin chain
3726 to find out what sort of node actually served as the original seed for the
3727 given block. */
3728
3729 static tree
3730 decl_ultimate_origin (const_tree decl)
3731 {
3732 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3733 return NULL_TREE;
3734
3735 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3736 we're trying to output the abstract instance of this function. */
3737 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3738 return NULL_TREE;
3739
3740 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3741 most distant ancestor, this should never happen. */
3742 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3743
3744 return DECL_ABSTRACT_ORIGIN (decl);
3745 }
3746
3747 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3748 of a virtual function may refer to a base class, so we check the 'this'
3749 parameter. */
3750
3751 static tree
3752 decl_class_context (tree decl)
3753 {
3754 tree context = NULL_TREE;
3755
3756 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3757 context = DECL_CONTEXT (decl);
3758 else
3759 context = TYPE_MAIN_VARIANT
3760 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3761
3762 if (context && !TYPE_P (context))
3763 context = NULL_TREE;
3764
3765 return context;
3766 }
3767 \f
3768 /* Add an attribute/value pair to a DIE. */
3769
3770 static inline void
3771 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3772 {
3773 /* Maybe this should be an assert? */
3774 if (die == NULL)
3775 return;
3776
3777 vec_safe_reserve (die->die_attr, 1);
3778 vec_safe_push (die->die_attr, *attr);
3779 }
3780
3781 static inline enum dw_val_class
3782 AT_class (dw_attr_ref a)
3783 {
3784 return a->dw_attr_val.val_class;
3785 }
3786
3787 /* Return the index for any attribute that will be referenced with a
3788 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3789 are stored in dw_attr_val.v.val_str for reference counting
3790 pruning. */
3791
3792 static inline unsigned int
3793 AT_index (dw_attr_ref a)
3794 {
3795 if (AT_class (a) == dw_val_class_str)
3796 return a->dw_attr_val.v.val_str->index;
3797 else if (a->dw_attr_val.val_entry != NULL)
3798 return a->dw_attr_val.val_entry->index;
3799 return NOT_INDEXED;
3800 }
3801
3802 /* Add a flag value attribute to a DIE. */
3803
3804 static inline void
3805 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3806 {
3807 dw_attr_node attr;
3808
3809 attr.dw_attr = attr_kind;
3810 attr.dw_attr_val.val_class = dw_val_class_flag;
3811 attr.dw_attr_val.val_entry = NULL;
3812 attr.dw_attr_val.v.val_flag = flag;
3813 add_dwarf_attr (die, &attr);
3814 }
3815
3816 static inline unsigned
3817 AT_flag (dw_attr_ref a)
3818 {
3819 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3820 return a->dw_attr_val.v.val_flag;
3821 }
3822
3823 /* Add a signed integer attribute value to a DIE. */
3824
3825 static inline void
3826 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3827 {
3828 dw_attr_node attr;
3829
3830 attr.dw_attr = attr_kind;
3831 attr.dw_attr_val.val_class = dw_val_class_const;
3832 attr.dw_attr_val.val_entry = NULL;
3833 attr.dw_attr_val.v.val_int = int_val;
3834 add_dwarf_attr (die, &attr);
3835 }
3836
3837 static inline HOST_WIDE_INT
3838 AT_int (dw_attr_ref a)
3839 {
3840 gcc_assert (a && AT_class (a) == dw_val_class_const);
3841 return a->dw_attr_val.v.val_int;
3842 }
3843
3844 /* Add an unsigned integer attribute value to a DIE. */
3845
3846 static inline void
3847 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3848 unsigned HOST_WIDE_INT unsigned_val)
3849 {
3850 dw_attr_node attr;
3851
3852 attr.dw_attr = attr_kind;
3853 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3854 attr.dw_attr_val.val_entry = NULL;
3855 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3856 add_dwarf_attr (die, &attr);
3857 }
3858
3859 static inline unsigned HOST_WIDE_INT
3860 AT_unsigned (dw_attr_ref a)
3861 {
3862 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3863 return a->dw_attr_val.v.val_unsigned;
3864 }
3865
3866 /* Add an unsigned wide integer attribute value to a DIE. */
3867
3868 static inline void
3869 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3870 const wide_int& w)
3871 {
3872 dw_attr_node attr;
3873
3874 attr.dw_attr = attr_kind;
3875 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3876 attr.dw_attr_val.val_entry = NULL;
3877 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3878 *attr.dw_attr_val.v.val_wide = w;
3879 add_dwarf_attr (die, &attr);
3880 }
3881
3882 /* Add an unsigned double integer attribute value to a DIE. */
3883
3884 static inline void
3885 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3886 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3887 {
3888 dw_attr_node attr;
3889
3890 attr.dw_attr = attr_kind;
3891 attr.dw_attr_val.val_class = dw_val_class_const_double;
3892 attr.dw_attr_val.val_entry = NULL;
3893 attr.dw_attr_val.v.val_double.high = high;
3894 attr.dw_attr_val.v.val_double.low = low;
3895 add_dwarf_attr (die, &attr);
3896 }
3897
3898 /* Add a floating point attribute value to a DIE and return it. */
3899
3900 static inline void
3901 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 unsigned int length, unsigned int elt_size, unsigned char *array)
3903 {
3904 dw_attr_node attr;
3905
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_vec;
3908 attr.dw_attr_val.val_entry = NULL;
3909 attr.dw_attr_val.v.val_vec.length = length;
3910 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3911 attr.dw_attr_val.v.val_vec.array = array;
3912 add_dwarf_attr (die, &attr);
3913 }
3914
3915 /* Add an 8-byte data attribute value to a DIE. */
3916
3917 static inline void
3918 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3919 unsigned char data8[8])
3920 {
3921 dw_attr_node attr;
3922
3923 attr.dw_attr = attr_kind;
3924 attr.dw_attr_val.val_class = dw_val_class_data8;
3925 attr.dw_attr_val.val_entry = NULL;
3926 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3927 add_dwarf_attr (die, &attr);
3928 }
3929
3930 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3931 dwarf_split_debug_info, address attributes in dies destined for the
3932 final executable have force_direct set to avoid using indexed
3933 references. */
3934
3935 static inline void
3936 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3937 bool force_direct)
3938 {
3939 dw_attr_node attr;
3940 char * lbl_id;
3941
3942 lbl_id = xstrdup (lbl_low);
3943 attr.dw_attr = DW_AT_low_pc;
3944 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3945 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3946 if (dwarf_split_debug_info && !force_direct)
3947 attr.dw_attr_val.val_entry
3948 = add_addr_table_entry (lbl_id, ate_kind_label);
3949 else
3950 attr.dw_attr_val.val_entry = NULL;
3951 add_dwarf_attr (die, &attr);
3952
3953 attr.dw_attr = DW_AT_high_pc;
3954 if (dwarf_version < 4)
3955 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3956 else
3957 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3958 lbl_id = xstrdup (lbl_high);
3959 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3960 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3961 && dwarf_split_debug_info && !force_direct)
3962 attr.dw_attr_val.val_entry
3963 = add_addr_table_entry (lbl_id, ate_kind_label);
3964 else
3965 attr.dw_attr_val.val_entry = NULL;
3966 add_dwarf_attr (die, &attr);
3967 }
3968
3969 /* Hash and equality functions for debug_str_hash. */
3970
3971 hashval_t
3972 indirect_string_hasher::hash (indirect_string_node *x)
3973 {
3974 return htab_hash_string (x->str);
3975 }
3976
3977 bool
3978 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3979 {
3980 return strcmp (x1->str, x2) == 0;
3981 }
3982
3983 /* Add STR to the given string hash table. */
3984
3985 static struct indirect_string_node *
3986 find_AT_string_in_table (const char *str,
3987 hash_table<indirect_string_hasher> *table)
3988 {
3989 struct indirect_string_node *node;
3990
3991 indirect_string_node **slot
3992 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3993 if (*slot == NULL)
3994 {
3995 node = ggc_cleared_alloc<indirect_string_node> ();
3996 node->str = ggc_strdup (str);
3997 *slot = node;
3998 }
3999 else
4000 node = *slot;
4001
4002 node->refcount++;
4003 return node;
4004 }
4005
4006 /* Add STR to the indirect string hash table. */
4007
4008 static struct indirect_string_node *
4009 find_AT_string (const char *str)
4010 {
4011 if (! debug_str_hash)
4012 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4013
4014 return find_AT_string_in_table (str, debug_str_hash);
4015 }
4016
4017 /* Add a string attribute value to a DIE. */
4018
4019 static inline void
4020 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4021 {
4022 dw_attr_node attr;
4023 struct indirect_string_node *node;
4024
4025 node = find_AT_string (str);
4026
4027 attr.dw_attr = attr_kind;
4028 attr.dw_attr_val.val_class = dw_val_class_str;
4029 attr.dw_attr_val.val_entry = NULL;
4030 attr.dw_attr_val.v.val_str = node;
4031 add_dwarf_attr (die, &attr);
4032 }
4033
4034 static inline const char *
4035 AT_string (dw_attr_ref a)
4036 {
4037 gcc_assert (a && AT_class (a) == dw_val_class_str);
4038 return a->dw_attr_val.v.val_str->str;
4039 }
4040
4041 /* Call this function directly to bypass AT_string_form's logic to put
4042 the string inline in the die. */
4043
4044 static void
4045 set_indirect_string (struct indirect_string_node *node)
4046 {
4047 char label[32];
4048 /* Already indirect is a no op. */
4049 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4050 {
4051 gcc_assert (node->label);
4052 return;
4053 }
4054 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4055 ++dw2_string_counter;
4056 node->label = xstrdup (label);
4057
4058 if (!dwarf_split_debug_info)
4059 {
4060 node->form = DW_FORM_strp;
4061 node->index = NOT_INDEXED;
4062 }
4063 else
4064 {
4065 node->form = DW_FORM_GNU_str_index;
4066 node->index = NO_INDEX_ASSIGNED;
4067 }
4068 }
4069
4070 /* Find out whether a string should be output inline in DIE
4071 or out-of-line in .debug_str section. */
4072
4073 static enum dwarf_form
4074 find_string_form (struct indirect_string_node *node)
4075 {
4076 unsigned int len;
4077
4078 if (node->form)
4079 return node->form;
4080
4081 len = strlen (node->str) + 1;
4082
4083 /* If the string is shorter or equal to the size of the reference, it is
4084 always better to put it inline. */
4085 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4086 return node->form = DW_FORM_string;
4087
4088 /* If we cannot expect the linker to merge strings in .debug_str
4089 section, only put it into .debug_str if it is worth even in this
4090 single module. */
4091 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4092 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4093 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4094 return node->form = DW_FORM_string;
4095
4096 set_indirect_string (node);
4097
4098 return node->form;
4099 }
4100
4101 /* Find out whether the string referenced from the attribute should be
4102 output inline in DIE or out-of-line in .debug_str section. */
4103
4104 static enum dwarf_form
4105 AT_string_form (dw_attr_ref a)
4106 {
4107 gcc_assert (a && AT_class (a) == dw_val_class_str);
4108 return find_string_form (a->dw_attr_val.v.val_str);
4109 }
4110
4111 /* Add a DIE reference attribute value to a DIE. */
4112
4113 static inline void
4114 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4115 {
4116 dw_attr_node attr;
4117
4118 #ifdef ENABLE_CHECKING
4119 gcc_assert (targ_die != NULL);
4120 #else
4121 /* With LTO we can end up trying to reference something we didn't create
4122 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4123 if (targ_die == NULL)
4124 return;
4125 #endif
4126
4127 attr.dw_attr = attr_kind;
4128 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4129 attr.dw_attr_val.val_entry = NULL;
4130 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4131 attr.dw_attr_val.v.val_die_ref.external = 0;
4132 add_dwarf_attr (die, &attr);
4133 }
4134
4135 /* Change DIE reference REF to point to NEW_DIE instead. */
4136
4137 static inline void
4138 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4139 {
4140 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4141 ref->dw_attr_val.v.val_die_ref.die = new_die;
4142 ref->dw_attr_val.v.val_die_ref.external = 0;
4143 }
4144
4145 /* Add an AT_specification attribute to a DIE, and also make the back
4146 pointer from the specification to the definition. */
4147
4148 static inline void
4149 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4150 {
4151 add_AT_die_ref (die, DW_AT_specification, targ_die);
4152 gcc_assert (!targ_die->die_definition);
4153 targ_die->die_definition = die;
4154 }
4155
4156 static inline dw_die_ref
4157 AT_ref (dw_attr_ref a)
4158 {
4159 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4160 return a->dw_attr_val.v.val_die_ref.die;
4161 }
4162
4163 static inline int
4164 AT_ref_external (dw_attr_ref a)
4165 {
4166 if (a && AT_class (a) == dw_val_class_die_ref)
4167 return a->dw_attr_val.v.val_die_ref.external;
4168
4169 return 0;
4170 }
4171
4172 static inline void
4173 set_AT_ref_external (dw_attr_ref a, int i)
4174 {
4175 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4176 a->dw_attr_val.v.val_die_ref.external = i;
4177 }
4178
4179 /* Add an FDE reference attribute value to a DIE. */
4180
4181 static inline void
4182 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4183 {
4184 dw_attr_node attr;
4185
4186 attr.dw_attr = attr_kind;
4187 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4188 attr.dw_attr_val.val_entry = NULL;
4189 attr.dw_attr_val.v.val_fde_index = targ_fde;
4190 add_dwarf_attr (die, &attr);
4191 }
4192
4193 /* Add a location description attribute value to a DIE. */
4194
4195 static inline void
4196 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4197 {
4198 dw_attr_node attr;
4199
4200 attr.dw_attr = attr_kind;
4201 attr.dw_attr_val.val_class = dw_val_class_loc;
4202 attr.dw_attr_val.val_entry = NULL;
4203 attr.dw_attr_val.v.val_loc = loc;
4204 add_dwarf_attr (die, &attr);
4205 }
4206
4207 static inline dw_loc_descr_ref
4208 AT_loc (dw_attr_ref a)
4209 {
4210 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4211 return a->dw_attr_val.v.val_loc;
4212 }
4213
4214 static inline void
4215 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4216 {
4217 dw_attr_node attr;
4218
4219 attr.dw_attr = attr_kind;
4220 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4221 attr.dw_attr_val.val_entry = NULL;
4222 attr.dw_attr_val.v.val_loc_list = loc_list;
4223 add_dwarf_attr (die, &attr);
4224 have_location_lists = true;
4225 }
4226
4227 static inline dw_loc_list_ref
4228 AT_loc_list (dw_attr_ref a)
4229 {
4230 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4231 return a->dw_attr_val.v.val_loc_list;
4232 }
4233
4234 static inline dw_loc_list_ref *
4235 AT_loc_list_ptr (dw_attr_ref a)
4236 {
4237 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4238 return &a->dw_attr_val.v.val_loc_list;
4239 }
4240
4241 struct addr_hasher : ggc_hasher<addr_table_entry *>
4242 {
4243 static hashval_t hash (addr_table_entry *);
4244 static bool equal (addr_table_entry *, addr_table_entry *);
4245 };
4246
4247 /* Table of entries into the .debug_addr section. */
4248
4249 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4250
4251 /* Hash an address_table_entry. */
4252
4253 hashval_t
4254 addr_hasher::hash (addr_table_entry *a)
4255 {
4256 inchash::hash hstate;
4257 switch (a->kind)
4258 {
4259 case ate_kind_rtx:
4260 hstate.add_int (0);
4261 break;
4262 case ate_kind_rtx_dtprel:
4263 hstate.add_int (1);
4264 break;
4265 case ate_kind_label:
4266 return htab_hash_string (a->addr.label);
4267 default:
4268 gcc_unreachable ();
4269 }
4270 inchash::add_rtx (a->addr.rtl, hstate);
4271 return hstate.end ();
4272 }
4273
4274 /* Determine equality for two address_table_entries. */
4275
4276 bool
4277 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4278 {
4279 if (a1->kind != a2->kind)
4280 return 0;
4281 switch (a1->kind)
4282 {
4283 case ate_kind_rtx:
4284 case ate_kind_rtx_dtprel:
4285 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4286 case ate_kind_label:
4287 return strcmp (a1->addr.label, a2->addr.label) == 0;
4288 default:
4289 gcc_unreachable ();
4290 }
4291 }
4292
4293 /* Initialize an addr_table_entry. */
4294
4295 void
4296 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4297 {
4298 e->kind = kind;
4299 switch (kind)
4300 {
4301 case ate_kind_rtx:
4302 case ate_kind_rtx_dtprel:
4303 e->addr.rtl = (rtx) addr;
4304 break;
4305 case ate_kind_label:
4306 e->addr.label = (char *) addr;
4307 break;
4308 }
4309 e->refcount = 0;
4310 e->index = NO_INDEX_ASSIGNED;
4311 }
4312
4313 /* Add attr to the address table entry to the table. Defer setting an
4314 index until output time. */
4315
4316 static addr_table_entry *
4317 add_addr_table_entry (void *addr, enum ate_kind kind)
4318 {
4319 addr_table_entry *node;
4320 addr_table_entry finder;
4321
4322 gcc_assert (dwarf_split_debug_info);
4323 if (! addr_index_table)
4324 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4325 init_addr_table_entry (&finder, kind, addr);
4326 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4327
4328 if (*slot == HTAB_EMPTY_ENTRY)
4329 {
4330 node = ggc_cleared_alloc<addr_table_entry> ();
4331 init_addr_table_entry (node, kind, addr);
4332 *slot = node;
4333 }
4334 else
4335 node = *slot;
4336
4337 node->refcount++;
4338 return node;
4339 }
4340
4341 /* Remove an entry from the addr table by decrementing its refcount.
4342 Strictly, decrementing the refcount would be enough, but the
4343 assertion that the entry is actually in the table has found
4344 bugs. */
4345
4346 static void
4347 remove_addr_table_entry (addr_table_entry *entry)
4348 {
4349 gcc_assert (dwarf_split_debug_info && addr_index_table);
4350 /* After an index is assigned, the table is frozen. */
4351 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4352 entry->refcount--;
4353 }
4354
4355 /* Given a location list, remove all addresses it refers to from the
4356 address_table. */
4357
4358 static void
4359 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4360 {
4361 for (; descr; descr = descr->dw_loc_next)
4362 if (descr->dw_loc_oprnd1.val_entry != NULL)
4363 {
4364 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4365 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4366 }
4367 }
4368
4369 /* A helper function for dwarf2out_finish called through
4370 htab_traverse. Assign an addr_table_entry its index. All entries
4371 must be collected into the table when this function is called,
4372 because the indexing code relies on htab_traverse to traverse nodes
4373 in the same order for each run. */
4374
4375 int
4376 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4377 {
4378 addr_table_entry *node = *h;
4379
4380 /* Don't index unreferenced nodes. */
4381 if (node->refcount == 0)
4382 return 1;
4383
4384 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4385 node->index = *index;
4386 *index += 1;
4387
4388 return 1;
4389 }
4390
4391 /* Add an address constant attribute value to a DIE. When using
4392 dwarf_split_debug_info, address attributes in dies destined for the
4393 final executable should be direct references--setting the parameter
4394 force_direct ensures this behavior. */
4395
4396 static inline void
4397 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4398 bool force_direct)
4399 {
4400 dw_attr_node attr;
4401
4402 attr.dw_attr = attr_kind;
4403 attr.dw_attr_val.val_class = dw_val_class_addr;
4404 attr.dw_attr_val.v.val_addr = addr;
4405 if (dwarf_split_debug_info && !force_direct)
4406 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4407 else
4408 attr.dw_attr_val.val_entry = NULL;
4409 add_dwarf_attr (die, &attr);
4410 }
4411
4412 /* Get the RTX from to an address DIE attribute. */
4413
4414 static inline rtx
4415 AT_addr (dw_attr_ref a)
4416 {
4417 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4418 return a->dw_attr_val.v.val_addr;
4419 }
4420
4421 /* Add a file attribute value to a DIE. */
4422
4423 static inline void
4424 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4425 struct dwarf_file_data *fd)
4426 {
4427 dw_attr_node attr;
4428
4429 attr.dw_attr = attr_kind;
4430 attr.dw_attr_val.val_class = dw_val_class_file;
4431 attr.dw_attr_val.val_entry = NULL;
4432 attr.dw_attr_val.v.val_file = fd;
4433 add_dwarf_attr (die, &attr);
4434 }
4435
4436 /* Get the dwarf_file_data from a file DIE attribute. */
4437
4438 static inline struct dwarf_file_data *
4439 AT_file (dw_attr_ref a)
4440 {
4441 gcc_assert (a && AT_class (a) == dw_val_class_file);
4442 return a->dw_attr_val.v.val_file;
4443 }
4444
4445 /* Add a vms delta attribute value to a DIE. */
4446
4447 static inline void
4448 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4449 const char *lbl1, const char *lbl2)
4450 {
4451 dw_attr_node attr;
4452
4453 attr.dw_attr = attr_kind;
4454 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4455 attr.dw_attr_val.val_entry = NULL;
4456 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4457 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4458 add_dwarf_attr (die, &attr);
4459 }
4460
4461 /* Add a label identifier attribute value to a DIE. */
4462
4463 static inline void
4464 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4465 const char *lbl_id)
4466 {
4467 dw_attr_node attr;
4468
4469 attr.dw_attr = attr_kind;
4470 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4471 attr.dw_attr_val.val_entry = NULL;
4472 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4473 if (dwarf_split_debug_info)
4474 attr.dw_attr_val.val_entry
4475 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4476 ate_kind_label);
4477 add_dwarf_attr (die, &attr);
4478 }
4479
4480 /* Add a section offset attribute value to a DIE, an offset into the
4481 debug_line section. */
4482
4483 static inline void
4484 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4485 const char *label)
4486 {
4487 dw_attr_node attr;
4488
4489 attr.dw_attr = attr_kind;
4490 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4491 attr.dw_attr_val.val_entry = NULL;
4492 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4493 add_dwarf_attr (die, &attr);
4494 }
4495
4496 /* Add a section offset attribute value to a DIE, an offset into the
4497 debug_macinfo section. */
4498
4499 static inline void
4500 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4501 const char *label)
4502 {
4503 dw_attr_node attr;
4504
4505 attr.dw_attr = attr_kind;
4506 attr.dw_attr_val.val_class = dw_val_class_macptr;
4507 attr.dw_attr_val.val_entry = NULL;
4508 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4509 add_dwarf_attr (die, &attr);
4510 }
4511
4512 /* Add an offset attribute value to a DIE. */
4513
4514 static inline void
4515 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4516 unsigned HOST_WIDE_INT offset)
4517 {
4518 dw_attr_node attr;
4519
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_offset;
4522 attr.dw_attr_val.val_entry = NULL;
4523 attr.dw_attr_val.v.val_offset = offset;
4524 add_dwarf_attr (die, &attr);
4525 }
4526
4527 /* Add a range_list attribute value to a DIE. When using
4528 dwarf_split_debug_info, address attributes in dies destined for the
4529 final executable should be direct references--setting the parameter
4530 force_direct ensures this behavior. */
4531
4532 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4533 #define RELOCATED_OFFSET (NULL)
4534
4535 static void
4536 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4537 long unsigned int offset, bool force_direct)
4538 {
4539 dw_attr_node attr;
4540
4541 attr.dw_attr = attr_kind;
4542 attr.dw_attr_val.val_class = dw_val_class_range_list;
4543 /* For the range_list attribute, use val_entry to store whether the
4544 offset should follow split-debug-info or normal semantics. This
4545 value is read in output_range_list_offset. */
4546 if (dwarf_split_debug_info && !force_direct)
4547 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4548 else
4549 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4550 attr.dw_attr_val.v.val_offset = offset;
4551 add_dwarf_attr (die, &attr);
4552 }
4553
4554 /* Return the start label of a delta attribute. */
4555
4556 static inline const char *
4557 AT_vms_delta1 (dw_attr_ref a)
4558 {
4559 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4560 return a->dw_attr_val.v.val_vms_delta.lbl1;
4561 }
4562
4563 /* Return the end label of a delta attribute. */
4564
4565 static inline const char *
4566 AT_vms_delta2 (dw_attr_ref a)
4567 {
4568 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4569 return a->dw_attr_val.v.val_vms_delta.lbl2;
4570 }
4571
4572 static inline const char *
4573 AT_lbl (dw_attr_ref a)
4574 {
4575 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4576 || AT_class (a) == dw_val_class_lineptr
4577 || AT_class (a) == dw_val_class_macptr
4578 || AT_class (a) == dw_val_class_high_pc));
4579 return a->dw_attr_val.v.val_lbl_id;
4580 }
4581
4582 /* Get the attribute of type attr_kind. */
4583
4584 static dw_attr_ref
4585 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4586 {
4587 dw_attr_ref a;
4588 unsigned ix;
4589 dw_die_ref spec = NULL;
4590
4591 if (! die)
4592 return NULL;
4593
4594 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4595 if (a->dw_attr == attr_kind)
4596 return a;
4597 else if (a->dw_attr == DW_AT_specification
4598 || a->dw_attr == DW_AT_abstract_origin)
4599 spec = AT_ref (a);
4600
4601 if (spec)
4602 return get_AT (spec, attr_kind);
4603
4604 return NULL;
4605 }
4606
4607 /* Returns the parent of the declaration of DIE. */
4608
4609 static dw_die_ref
4610 get_die_parent (dw_die_ref die)
4611 {
4612 dw_die_ref t;
4613
4614 if (!die)
4615 return NULL;
4616
4617 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4618 || (t = get_AT_ref (die, DW_AT_specification)))
4619 die = t;
4620
4621 return die->die_parent;
4622 }
4623
4624 /* Return the "low pc" attribute value, typically associated with a subprogram
4625 DIE. Return null if the "low pc" attribute is either not present, or if it
4626 cannot be represented as an assembler label identifier. */
4627
4628 static inline const char *
4629 get_AT_low_pc (dw_die_ref die)
4630 {
4631 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4632
4633 return a ? AT_lbl (a) : NULL;
4634 }
4635
4636 /* Return the "high pc" attribute value, typically associated with a subprogram
4637 DIE. Return null if the "high pc" attribute is either not present, or if it
4638 cannot be represented as an assembler label identifier. */
4639
4640 static inline const char *
4641 get_AT_hi_pc (dw_die_ref die)
4642 {
4643 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4644
4645 return a ? AT_lbl (a) : NULL;
4646 }
4647
4648 /* Return the value of the string attribute designated by ATTR_KIND, or
4649 NULL if it is not present. */
4650
4651 static inline const char *
4652 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4653 {
4654 dw_attr_ref a = get_AT (die, attr_kind);
4655
4656 return a ? AT_string (a) : NULL;
4657 }
4658
4659 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4660 if it is not present. */
4661
4662 static inline int
4663 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4664 {
4665 dw_attr_ref a = get_AT (die, attr_kind);
4666
4667 return a ? AT_flag (a) : 0;
4668 }
4669
4670 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4671 if it is not present. */
4672
4673 static inline unsigned
4674 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4675 {
4676 dw_attr_ref a = get_AT (die, attr_kind);
4677
4678 return a ? AT_unsigned (a) : 0;
4679 }
4680
4681 static inline dw_die_ref
4682 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4683 {
4684 dw_attr_ref a = get_AT (die, attr_kind);
4685
4686 return a ? AT_ref (a) : NULL;
4687 }
4688
4689 static inline struct dwarf_file_data *
4690 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4691 {
4692 dw_attr_ref a = get_AT (die, attr_kind);
4693
4694 return a ? AT_file (a) : NULL;
4695 }
4696
4697 /* Return TRUE if the language is C++. */
4698
4699 static inline bool
4700 is_cxx (void)
4701 {
4702 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4703
4704 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4705 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4706 }
4707
4708 /* Return TRUE if the language is Java. */
4709
4710 static inline bool
4711 is_java (void)
4712 {
4713 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4714
4715 return lang == DW_LANG_Java;
4716 }
4717
4718 /* Return TRUE if the language is Fortran. */
4719
4720 static inline bool
4721 is_fortran (void)
4722 {
4723 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4724
4725 return (lang == DW_LANG_Fortran77
4726 || lang == DW_LANG_Fortran90
4727 || lang == DW_LANG_Fortran95
4728 || lang == DW_LANG_Fortran03
4729 || lang == DW_LANG_Fortran08);
4730 }
4731
4732 /* Return TRUE if the language is Ada. */
4733
4734 static inline bool
4735 is_ada (void)
4736 {
4737 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4738
4739 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4740 }
4741
4742 /* Remove the specified attribute if present. Return TRUE if removal
4743 was successful. */
4744
4745 static bool
4746 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4747 {
4748 dw_attr_ref a;
4749 unsigned ix;
4750
4751 if (! die)
4752 return false;
4753
4754 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4755 if (a->dw_attr == attr_kind)
4756 {
4757 if (AT_class (a) == dw_val_class_str)
4758 if (a->dw_attr_val.v.val_str->refcount)
4759 a->dw_attr_val.v.val_str->refcount--;
4760
4761 /* vec::ordered_remove should help reduce the number of abbrevs
4762 that are needed. */
4763 die->die_attr->ordered_remove (ix);
4764 return true;
4765 }
4766 return false;
4767 }
4768
4769 /* Remove CHILD from its parent. PREV must have the property that
4770 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4771
4772 static void
4773 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4774 {
4775 gcc_assert (child->die_parent == prev->die_parent);
4776 gcc_assert (prev->die_sib == child);
4777 if (prev == child)
4778 {
4779 gcc_assert (child->die_parent->die_child == child);
4780 prev = NULL;
4781 }
4782 else
4783 prev->die_sib = child->die_sib;
4784 if (child->die_parent->die_child == child)
4785 child->die_parent->die_child = prev;
4786 }
4787
4788 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4789 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4790
4791 static void
4792 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4793 {
4794 dw_die_ref parent = old_child->die_parent;
4795
4796 gcc_assert (parent == prev->die_parent);
4797 gcc_assert (prev->die_sib == old_child);
4798
4799 new_child->die_parent = parent;
4800 if (prev == old_child)
4801 {
4802 gcc_assert (parent->die_child == old_child);
4803 new_child->die_sib = new_child;
4804 }
4805 else
4806 {
4807 prev->die_sib = new_child;
4808 new_child->die_sib = old_child->die_sib;
4809 }
4810 if (old_child->die_parent->die_child == old_child)
4811 old_child->die_parent->die_child = new_child;
4812 }
4813
4814 /* Move all children from OLD_PARENT to NEW_PARENT. */
4815
4816 static void
4817 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4818 {
4819 dw_die_ref c;
4820 new_parent->die_child = old_parent->die_child;
4821 old_parent->die_child = NULL;
4822 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4823 }
4824
4825 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4826 matches TAG. */
4827
4828 static void
4829 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4830 {
4831 dw_die_ref c;
4832
4833 c = die->die_child;
4834 if (c) do {
4835 dw_die_ref prev = c;
4836 c = c->die_sib;
4837 while (c->die_tag == tag)
4838 {
4839 remove_child_with_prev (c, prev);
4840 c->die_parent = NULL;
4841 /* Might have removed every child. */
4842 if (c == c->die_sib)
4843 return;
4844 c = c->die_sib;
4845 }
4846 } while (c != die->die_child);
4847 }
4848
4849 /* Add a CHILD_DIE as the last child of DIE. */
4850
4851 static void
4852 add_child_die (dw_die_ref die, dw_die_ref child_die)
4853 {
4854 /* FIXME this should probably be an assert. */
4855 if (! die || ! child_die)
4856 return;
4857 gcc_assert (die != child_die);
4858
4859 child_die->die_parent = die;
4860 if (die->die_child)
4861 {
4862 child_die->die_sib = die->die_child->die_sib;
4863 die->die_child->die_sib = child_die;
4864 }
4865 else
4866 child_die->die_sib = child_die;
4867 die->die_child = child_die;
4868 }
4869
4870 /* Unassociate CHILD from its parent, and make its parent be
4871 NEW_PARENT. */
4872
4873 static void
4874 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4875 {
4876 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4877 if (p->die_sib == child)
4878 {
4879 remove_child_with_prev (child, p);
4880 break;
4881 }
4882 add_child_die (new_parent, child);
4883 }
4884
4885 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4886 is the specification, to the end of PARENT's list of children.
4887 This is done by removing and re-adding it. */
4888
4889 static void
4890 splice_child_die (dw_die_ref parent, dw_die_ref child)
4891 {
4892 /* We want the declaration DIE from inside the class, not the
4893 specification DIE at toplevel. */
4894 if (child->die_parent != parent)
4895 {
4896 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4897
4898 if (tmp)
4899 child = tmp;
4900 }
4901
4902 gcc_assert (child->die_parent == parent
4903 || (child->die_parent
4904 == get_AT_ref (parent, DW_AT_specification)));
4905
4906 reparent_child (child, parent);
4907 }
4908
4909 /* Create and return a new die with a parent of PARENT_DIE. If
4910 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4911 associated tree T must be supplied to determine parenthood
4912 later. */
4913
4914 static inline dw_die_ref
4915 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4916 {
4917 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4918
4919 die->die_tag = tag_value;
4920
4921 if (parent_die != NULL)
4922 add_child_die (parent_die, die);
4923 else
4924 {
4925 limbo_die_node *limbo_node;
4926
4927 /* No DIEs created after early dwarf should end up in limbo,
4928 because the limbo list should not persist past LTO
4929 streaming. */
4930 if (tag_value != DW_TAG_compile_unit
4931 /* These are allowed because they're generated while
4932 breaking out COMDAT units late. */
4933 && tag_value != DW_TAG_type_unit
4934 && !early_dwarf
4935 /* Allow nested functions to live in limbo because they will
4936 only temporarily live there, as decls_for_scope will fix
4937 them up. */
4938 && (TREE_CODE (t) != FUNCTION_DECL
4939 || !decl_function_context (t))
4940 /* Same as nested functions above but for types. Types that
4941 are local to a function will be fixed in
4942 decls_for_scope. */
4943 && (!RECORD_OR_UNION_TYPE_P (t)
4944 || !TYPE_CONTEXT (t)
4945 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4946 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4947 especially in the ltrans stage, but once we implement LTO
4948 dwarf streaming, we should remove this exception. */
4949 && !in_lto_p)
4950 {
4951 fprintf (stderr, "symbol ended up in limbo too late:");
4952 debug_generic_stmt (t);
4953 gcc_unreachable ();
4954 }
4955
4956 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4957 limbo_node->die = die;
4958 limbo_node->created_for = t;
4959 limbo_node->next = limbo_die_list;
4960 limbo_die_list = limbo_node;
4961 }
4962
4963 return die;
4964 }
4965
4966 /* Return the DIE associated with the given type specifier. */
4967
4968 static inline dw_die_ref
4969 lookup_type_die (tree type)
4970 {
4971 return TYPE_SYMTAB_DIE (type);
4972 }
4973
4974 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4975 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4976 anonymous type instead the one of the naming typedef. */
4977
4978 static inline dw_die_ref
4979 strip_naming_typedef (tree type, dw_die_ref type_die)
4980 {
4981 if (type
4982 && TREE_CODE (type) == RECORD_TYPE
4983 && type_die
4984 && type_die->die_tag == DW_TAG_typedef
4985 && is_naming_typedef_decl (TYPE_NAME (type)))
4986 type_die = get_AT_ref (type_die, DW_AT_type);
4987 return type_die;
4988 }
4989
4990 /* Like lookup_type_die, but if type is an anonymous type named by a
4991 typedef[1], return the DIE of the anonymous type instead the one of
4992 the naming typedef. This is because in gen_typedef_die, we did
4993 equate the anonymous struct named by the typedef with the DIE of
4994 the naming typedef. So by default, lookup_type_die on an anonymous
4995 struct yields the DIE of the naming typedef.
4996
4997 [1]: Read the comment of is_naming_typedef_decl to learn about what
4998 a naming typedef is. */
4999
5000 static inline dw_die_ref
5001 lookup_type_die_strip_naming_typedef (tree type)
5002 {
5003 dw_die_ref die = lookup_type_die (type);
5004 return strip_naming_typedef (type, die);
5005 }
5006
5007 /* Equate a DIE to a given type specifier. */
5008
5009 static inline void
5010 equate_type_number_to_die (tree type, dw_die_ref type_die)
5011 {
5012 TYPE_SYMTAB_DIE (type) = type_die;
5013 }
5014
5015 /* Returns a hash value for X (which really is a die_struct). */
5016
5017 inline hashval_t
5018 decl_die_hasher::hash (die_node *x)
5019 {
5020 return (hashval_t) x->decl_id;
5021 }
5022
5023 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5024
5025 inline bool
5026 decl_die_hasher::equal (die_node *x, tree y)
5027 {
5028 return (x->decl_id == DECL_UID (y));
5029 }
5030
5031 /* Return the DIE associated with a given declaration. */
5032
5033 static inline dw_die_ref
5034 lookup_decl_die (tree decl)
5035 {
5036 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5037 }
5038
5039 /* Returns a hash value for X (which really is a var_loc_list). */
5040
5041 inline hashval_t
5042 decl_loc_hasher::hash (var_loc_list *x)
5043 {
5044 return (hashval_t) x->decl_id;
5045 }
5046
5047 /* Return nonzero if decl_id of var_loc_list X is the same as
5048 UID of decl *Y. */
5049
5050 inline bool
5051 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5052 {
5053 return (x->decl_id == DECL_UID (y));
5054 }
5055
5056 /* Return the var_loc list associated with a given declaration. */
5057
5058 static inline var_loc_list *
5059 lookup_decl_loc (const_tree decl)
5060 {
5061 if (!decl_loc_table)
5062 return NULL;
5063 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5064 }
5065
5066 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5067
5068 inline hashval_t
5069 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5070 {
5071 return (hashval_t) x->decl_id;
5072 }
5073
5074 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5075 UID of decl *Y. */
5076
5077 inline bool
5078 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5079 {
5080 return (x->decl_id == DECL_UID (y));
5081 }
5082
5083 /* Equate a DIE to a particular declaration. */
5084
5085 static void
5086 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5087 {
5088 unsigned int decl_id = DECL_UID (decl);
5089
5090 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5091 decl_die->decl_id = decl_id;
5092 }
5093
5094 /* Return how many bits covers PIECE EXPR_LIST. */
5095
5096 static HOST_WIDE_INT
5097 decl_piece_bitsize (rtx piece)
5098 {
5099 int ret = (int) GET_MODE (piece);
5100 if (ret)
5101 return ret;
5102 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5103 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5104 return INTVAL (XEXP (XEXP (piece, 0), 0));
5105 }
5106
5107 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5108
5109 static rtx *
5110 decl_piece_varloc_ptr (rtx piece)
5111 {
5112 if ((int) GET_MODE (piece))
5113 return &XEXP (piece, 0);
5114 else
5115 return &XEXP (XEXP (piece, 0), 1);
5116 }
5117
5118 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5119 Next is the chain of following piece nodes. */
5120
5121 static rtx_expr_list *
5122 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5123 {
5124 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5125 return alloc_EXPR_LIST (bitsize, loc_note, next);
5126 else
5127 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5128 GEN_INT (bitsize),
5129 loc_note), next);
5130 }
5131
5132 /* Return rtx that should be stored into loc field for
5133 LOC_NOTE and BITPOS/BITSIZE. */
5134
5135 static rtx
5136 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5137 HOST_WIDE_INT bitsize)
5138 {
5139 if (bitsize != -1)
5140 {
5141 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5142 if (bitpos != 0)
5143 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5144 }
5145 return loc_note;
5146 }
5147
5148 /* This function either modifies location piece list *DEST in
5149 place (if SRC and INNER is NULL), or copies location piece list
5150 *SRC to *DEST while modifying it. Location BITPOS is modified
5151 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5152 not copied and if needed some padding around it is added.
5153 When modifying in place, DEST should point to EXPR_LIST where
5154 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5155 to the start of the whole list and INNER points to the EXPR_LIST
5156 where earlier pieces cover PIECE_BITPOS bits. */
5157
5158 static void
5159 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5160 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5161 HOST_WIDE_INT bitsize, rtx loc_note)
5162 {
5163 HOST_WIDE_INT diff;
5164 bool copy = inner != NULL;
5165
5166 if (copy)
5167 {
5168 /* First copy all nodes preceding the current bitpos. */
5169 while (src != inner)
5170 {
5171 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5172 decl_piece_bitsize (*src), NULL_RTX);
5173 dest = &XEXP (*dest, 1);
5174 src = &XEXP (*src, 1);
5175 }
5176 }
5177 /* Add padding if needed. */
5178 if (bitpos != piece_bitpos)
5179 {
5180 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5181 copy ? NULL_RTX : *dest);
5182 dest = &XEXP (*dest, 1);
5183 }
5184 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5185 {
5186 gcc_assert (!copy);
5187 /* A piece with correct bitpos and bitsize already exist,
5188 just update the location for it and return. */
5189 *decl_piece_varloc_ptr (*dest) = loc_note;
5190 return;
5191 }
5192 /* Add the piece that changed. */
5193 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5194 dest = &XEXP (*dest, 1);
5195 /* Skip over pieces that overlap it. */
5196 diff = bitpos - piece_bitpos + bitsize;
5197 if (!copy)
5198 src = dest;
5199 while (diff > 0 && *src)
5200 {
5201 rtx piece = *src;
5202 diff -= decl_piece_bitsize (piece);
5203 if (copy)
5204 src = &XEXP (piece, 1);
5205 else
5206 {
5207 *src = XEXP (piece, 1);
5208 free_EXPR_LIST_node (piece);
5209 }
5210 }
5211 /* Add padding if needed. */
5212 if (diff < 0 && *src)
5213 {
5214 if (!copy)
5215 dest = src;
5216 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5217 dest = &XEXP (*dest, 1);
5218 }
5219 if (!copy)
5220 return;
5221 /* Finally copy all nodes following it. */
5222 while (*src)
5223 {
5224 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5225 decl_piece_bitsize (*src), NULL_RTX);
5226 dest = &XEXP (*dest, 1);
5227 src = &XEXP (*src, 1);
5228 }
5229 }
5230
5231 /* Add a variable location node to the linked list for DECL. */
5232
5233 static struct var_loc_node *
5234 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5235 {
5236 unsigned int decl_id;
5237 var_loc_list *temp;
5238 struct var_loc_node *loc = NULL;
5239 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5240
5241 if (TREE_CODE (decl) == VAR_DECL
5242 && DECL_HAS_DEBUG_EXPR_P (decl))
5243 {
5244 tree realdecl = DECL_DEBUG_EXPR (decl);
5245 if (handled_component_p (realdecl)
5246 || (TREE_CODE (realdecl) == MEM_REF
5247 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5248 {
5249 HOST_WIDE_INT maxsize;
5250 tree innerdecl;
5251 innerdecl
5252 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5253 if (!DECL_P (innerdecl)
5254 || DECL_IGNORED_P (innerdecl)
5255 || TREE_STATIC (innerdecl)
5256 || bitsize <= 0
5257 || bitpos + bitsize > 256
5258 || bitsize != maxsize)
5259 return NULL;
5260 decl = innerdecl;
5261 }
5262 }
5263
5264 decl_id = DECL_UID (decl);
5265 var_loc_list **slot
5266 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5267 if (*slot == NULL)
5268 {
5269 temp = ggc_cleared_alloc<var_loc_list> ();
5270 temp->decl_id = decl_id;
5271 *slot = temp;
5272 }
5273 else
5274 temp = *slot;
5275
5276 /* For PARM_DECLs try to keep around the original incoming value,
5277 even if that means we'll emit a zero-range .debug_loc entry. */
5278 if (temp->last
5279 && temp->first == temp->last
5280 && TREE_CODE (decl) == PARM_DECL
5281 && NOTE_P (temp->first->loc)
5282 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5283 && DECL_INCOMING_RTL (decl)
5284 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5285 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5286 == GET_CODE (DECL_INCOMING_RTL (decl))
5287 && prev_real_insn (temp->first->loc) == NULL_RTX
5288 && (bitsize != -1
5289 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5290 NOTE_VAR_LOCATION_LOC (loc_note))
5291 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5292 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5293 {
5294 loc = ggc_cleared_alloc<var_loc_node> ();
5295 temp->first->next = loc;
5296 temp->last = loc;
5297 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5298 }
5299 else if (temp->last)
5300 {
5301 struct var_loc_node *last = temp->last, *unused = NULL;
5302 rtx *piece_loc = NULL, last_loc_note;
5303 HOST_WIDE_INT piece_bitpos = 0;
5304 if (last->next)
5305 {
5306 last = last->next;
5307 gcc_assert (last->next == NULL);
5308 }
5309 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5310 {
5311 piece_loc = &last->loc;
5312 do
5313 {
5314 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5315 if (piece_bitpos + cur_bitsize > bitpos)
5316 break;
5317 piece_bitpos += cur_bitsize;
5318 piece_loc = &XEXP (*piece_loc, 1);
5319 }
5320 while (*piece_loc);
5321 }
5322 /* TEMP->LAST here is either pointer to the last but one or
5323 last element in the chained list, LAST is pointer to the
5324 last element. */
5325 if (label && strcmp (last->label, label) == 0)
5326 {
5327 /* For SRA optimized variables if there weren't any real
5328 insns since last note, just modify the last node. */
5329 if (piece_loc != NULL)
5330 {
5331 adjust_piece_list (piece_loc, NULL, NULL,
5332 bitpos, piece_bitpos, bitsize, loc_note);
5333 return NULL;
5334 }
5335 /* If the last note doesn't cover any instructions, remove it. */
5336 if (temp->last != last)
5337 {
5338 temp->last->next = NULL;
5339 unused = last;
5340 last = temp->last;
5341 gcc_assert (strcmp (last->label, label) != 0);
5342 }
5343 else
5344 {
5345 gcc_assert (temp->first == temp->last
5346 || (temp->first->next == temp->last
5347 && TREE_CODE (decl) == PARM_DECL));
5348 memset (temp->last, '\0', sizeof (*temp->last));
5349 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5350 return temp->last;
5351 }
5352 }
5353 if (bitsize == -1 && NOTE_P (last->loc))
5354 last_loc_note = last->loc;
5355 else if (piece_loc != NULL
5356 && *piece_loc != NULL_RTX
5357 && piece_bitpos == bitpos
5358 && decl_piece_bitsize (*piece_loc) == bitsize)
5359 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5360 else
5361 last_loc_note = NULL_RTX;
5362 /* If the current location is the same as the end of the list,
5363 and either both or neither of the locations is uninitialized,
5364 we have nothing to do. */
5365 if (last_loc_note == NULL_RTX
5366 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5367 NOTE_VAR_LOCATION_LOC (loc_note)))
5368 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5369 != NOTE_VAR_LOCATION_STATUS (loc_note))
5370 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5371 == VAR_INIT_STATUS_UNINITIALIZED)
5372 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5373 == VAR_INIT_STATUS_UNINITIALIZED))))
5374 {
5375 /* Add LOC to the end of list and update LAST. If the last
5376 element of the list has been removed above, reuse its
5377 memory for the new node, otherwise allocate a new one. */
5378 if (unused)
5379 {
5380 loc = unused;
5381 memset (loc, '\0', sizeof (*loc));
5382 }
5383 else
5384 loc = ggc_cleared_alloc<var_loc_node> ();
5385 if (bitsize == -1 || piece_loc == NULL)
5386 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5387 else
5388 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5389 bitpos, piece_bitpos, bitsize, loc_note);
5390 last->next = loc;
5391 /* Ensure TEMP->LAST will point either to the new last but one
5392 element of the chain, or to the last element in it. */
5393 if (last != temp->last)
5394 temp->last = last;
5395 }
5396 else if (unused)
5397 ggc_free (unused);
5398 }
5399 else
5400 {
5401 loc = ggc_cleared_alloc<var_loc_node> ();
5402 temp->first = loc;
5403 temp->last = loc;
5404 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5405 }
5406 return loc;
5407 }
5408 \f
5409 /* Keep track of the number of spaces used to indent the
5410 output of the debugging routines that print the structure of
5411 the DIE internal representation. */
5412 static int print_indent;
5413
5414 /* Indent the line the number of spaces given by print_indent. */
5415
5416 static inline void
5417 print_spaces (FILE *outfile)
5418 {
5419 fprintf (outfile, "%*s", print_indent, "");
5420 }
5421
5422 /* Print a type signature in hex. */
5423
5424 static inline void
5425 print_signature (FILE *outfile, char *sig)
5426 {
5427 int i;
5428
5429 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5430 fprintf (outfile, "%02x", sig[i] & 0xff);
5431 }
5432
5433 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5434
5435 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5436 RECURSE, output location descriptor operations. */
5437
5438 static void
5439 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5440 {
5441 switch (val->val_class)
5442 {
5443 case dw_val_class_addr:
5444 fprintf (outfile, "address");
5445 break;
5446 case dw_val_class_offset:
5447 fprintf (outfile, "offset");
5448 break;
5449 case dw_val_class_loc:
5450 fprintf (outfile, "location descriptor");
5451 if (val->v.val_loc == NULL)
5452 fprintf (outfile, " -> <null>\n");
5453 else if (recurse)
5454 {
5455 fprintf (outfile, ":\n");
5456 print_indent += 4;
5457 print_loc_descr (val->v.val_loc, outfile);
5458 print_indent -= 4;
5459 }
5460 else
5461 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5462 break;
5463 case dw_val_class_loc_list:
5464 fprintf (outfile, "location list -> label:%s",
5465 val->v.val_loc_list->ll_symbol);
5466 break;
5467 case dw_val_class_range_list:
5468 fprintf (outfile, "range list");
5469 break;
5470 case dw_val_class_const:
5471 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5472 break;
5473 case dw_val_class_unsigned_const:
5474 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5475 break;
5476 case dw_val_class_const_double:
5477 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5478 HOST_WIDE_INT_PRINT_UNSIGNED")",
5479 val->v.val_double.high,
5480 val->v.val_double.low);
5481 break;
5482 case dw_val_class_wide_int:
5483 {
5484 int i = val->v.val_wide->get_len ();
5485 fprintf (outfile, "constant (");
5486 gcc_assert (i > 0);
5487 if (val->v.val_wide->elt (i - 1) == 0)
5488 fprintf (outfile, "0x");
5489 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5490 val->v.val_wide->elt (--i));
5491 while (--i >= 0)
5492 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5493 val->v.val_wide->elt (i));
5494 fprintf (outfile, ")");
5495 break;
5496 }
5497 case dw_val_class_vec:
5498 fprintf (outfile, "floating-point or vector constant");
5499 break;
5500 case dw_val_class_flag:
5501 fprintf (outfile, "%u", val->v.val_flag);
5502 break;
5503 case dw_val_class_die_ref:
5504 if (val->v.val_die_ref.die != NULL)
5505 {
5506 dw_die_ref die = val->v.val_die_ref.die;
5507
5508 if (die->comdat_type_p)
5509 {
5510 fprintf (outfile, "die -> signature: ");
5511 print_signature (outfile,
5512 die->die_id.die_type_node->signature);
5513 }
5514 else if (die->die_id.die_symbol)
5515 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5516 else
5517 fprintf (outfile, "die -> %ld", die->die_offset);
5518 fprintf (outfile, " (%p)", (void *) die);
5519 }
5520 else
5521 fprintf (outfile, "die -> <null>");
5522 break;
5523 case dw_val_class_vms_delta:
5524 fprintf (outfile, "delta: @slotcount(%s-%s)",
5525 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5526 break;
5527 case dw_val_class_lbl_id:
5528 case dw_val_class_lineptr:
5529 case dw_val_class_macptr:
5530 case dw_val_class_high_pc:
5531 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5532 break;
5533 case dw_val_class_str:
5534 if (val->v.val_str->str != NULL)
5535 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5536 else
5537 fprintf (outfile, "<null>");
5538 break;
5539 case dw_val_class_file:
5540 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5541 val->v.val_file->emitted_number);
5542 break;
5543 case dw_val_class_data8:
5544 {
5545 int i;
5546
5547 for (i = 0; i < 8; i++)
5548 fprintf (outfile, "%02x", val->v.val_data8[i]);
5549 break;
5550 }
5551 default:
5552 break;
5553 }
5554 }
5555
5556 /* Likewise, for a DIE attribute. */
5557
5558 static void
5559 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5560 {
5561 print_dw_val (&a->dw_attr_val, recurse, outfile);
5562 }
5563
5564
5565 /* Print the list of operands in the LOC location description to OUTFILE. This
5566 routine is a debugging aid only. */
5567
5568 static void
5569 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5570 {
5571 dw_loc_descr_ref l = loc;
5572
5573 if (loc == NULL)
5574 {
5575 print_spaces (outfile);
5576 fprintf (outfile, "<null>\n");
5577 return;
5578 }
5579
5580 for (l = loc; l != NULL; l = l->dw_loc_next)
5581 {
5582 print_spaces (outfile);
5583 fprintf (outfile, "(%p) %s",
5584 (void *) l,
5585 dwarf_stack_op_name (l->dw_loc_opc));
5586 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5587 {
5588 fprintf (outfile, " ");
5589 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5590 }
5591 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5592 {
5593 fprintf (outfile, ", ");
5594 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5595 }
5596 fprintf (outfile, "\n");
5597 }
5598 }
5599
5600 /* Print the information associated with a given DIE, and its children.
5601 This routine is a debugging aid only. */
5602
5603 static void
5604 print_die (dw_die_ref die, FILE *outfile)
5605 {
5606 dw_attr_ref a;
5607 dw_die_ref c;
5608 unsigned ix;
5609
5610 print_spaces (outfile);
5611 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5612 die->die_offset, dwarf_tag_name (die->die_tag),
5613 (void*) die);
5614 print_spaces (outfile);
5615 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5616 fprintf (outfile, " offset: %ld", die->die_offset);
5617 fprintf (outfile, " mark: %d\n", die->die_mark);
5618
5619 if (die->comdat_type_p)
5620 {
5621 print_spaces (outfile);
5622 fprintf (outfile, " signature: ");
5623 print_signature (outfile, die->die_id.die_type_node->signature);
5624 fprintf (outfile, "\n");
5625 }
5626
5627 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5628 {
5629 print_spaces (outfile);
5630 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5631
5632 print_attribute (a, true, outfile);
5633 fprintf (outfile, "\n");
5634 }
5635
5636 if (die->die_child != NULL)
5637 {
5638 print_indent += 4;
5639 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5640 print_indent -= 4;
5641 }
5642 if (print_indent == 0)
5643 fprintf (outfile, "\n");
5644 }
5645
5646 /* Print the list of operations in the LOC location description. */
5647
5648 DEBUG_FUNCTION void
5649 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5650 {
5651 print_loc_descr (loc, stderr);
5652 }
5653
5654 /* Print the information collected for a given DIE. */
5655
5656 DEBUG_FUNCTION void
5657 debug_dwarf_die (dw_die_ref die)
5658 {
5659 print_die (die, stderr);
5660 }
5661
5662 DEBUG_FUNCTION void
5663 debug (die_struct &ref)
5664 {
5665 print_die (&ref, stderr);
5666 }
5667
5668 DEBUG_FUNCTION void
5669 debug (die_struct *ptr)
5670 {
5671 if (ptr)
5672 debug (*ptr);
5673 else
5674 fprintf (stderr, "<nil>\n");
5675 }
5676
5677
5678 /* Print all DWARF information collected for the compilation unit.
5679 This routine is a debugging aid only. */
5680
5681 DEBUG_FUNCTION void
5682 debug_dwarf (void)
5683 {
5684 print_indent = 0;
5685 print_die (comp_unit_die (), stderr);
5686 }
5687
5688 #ifdef ENABLE_CHECKING
5689 /* Sanity checks on DIEs. */
5690
5691 static void
5692 check_die (dw_die_ref die)
5693 {
5694 unsigned ix;
5695 dw_attr_ref a;
5696 bool inline_found = false;
5697 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5698 int n_decl_line = 0, n_decl_file = 0;
5699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5700 {
5701 switch (a->dw_attr)
5702 {
5703 case DW_AT_inline:
5704 if (a->dw_attr_val.v.val_unsigned)
5705 inline_found = true;
5706 break;
5707 case DW_AT_location:
5708 ++n_location;
5709 break;
5710 case DW_AT_low_pc:
5711 ++n_low_pc;
5712 break;
5713 case DW_AT_high_pc:
5714 ++n_high_pc;
5715 break;
5716 case DW_AT_artificial:
5717 ++n_artificial;
5718 break;
5719 case DW_AT_decl_line:
5720 ++n_decl_line;
5721 break;
5722 case DW_AT_decl_file:
5723 ++n_decl_file;
5724 break;
5725 default:
5726 break;
5727 }
5728 }
5729 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5730 || n_decl_line > 1 || n_decl_file > 1)
5731 {
5732 fprintf (stderr, "Duplicate attributes in DIE:\n");
5733 debug_dwarf_die (die);
5734 gcc_unreachable ();
5735 }
5736 if (inline_found)
5737 {
5738 /* A debugging information entry that is a member of an abstract
5739 instance tree [that has DW_AT_inline] should not contain any
5740 attributes which describe aspects of the subroutine which vary
5741 between distinct inlined expansions or distinct out-of-line
5742 expansions. */
5743 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5744 gcc_assert (a->dw_attr != DW_AT_low_pc
5745 && a->dw_attr != DW_AT_high_pc
5746 && a->dw_attr != DW_AT_location
5747 && a->dw_attr != DW_AT_frame_base
5748 && a->dw_attr != DW_AT_GNU_all_call_sites);
5749 }
5750 }
5751 #endif
5752 \f
5753 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5754 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5755 DIE that marks the start of the DIEs for this include file. */
5756
5757 static dw_die_ref
5758 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5759 {
5760 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5761 dw_die_ref new_unit = gen_compile_unit_die (filename);
5762
5763 new_unit->die_sib = old_unit;
5764 return new_unit;
5765 }
5766
5767 /* Close an include-file CU and reopen the enclosing one. */
5768
5769 static dw_die_ref
5770 pop_compile_unit (dw_die_ref old_unit)
5771 {
5772 dw_die_ref new_unit = old_unit->die_sib;
5773
5774 old_unit->die_sib = NULL;
5775 return new_unit;
5776 }
5777
5778 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5779 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5780 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5781
5782 /* Calculate the checksum of a location expression. */
5783
5784 static inline void
5785 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5786 {
5787 int tem;
5788 inchash::hash hstate;
5789 hashval_t hash;
5790
5791 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5792 CHECKSUM (tem);
5793 hash_loc_operands (loc, hstate);
5794 hash = hstate.end();
5795 CHECKSUM (hash);
5796 }
5797
5798 /* Calculate the checksum of an attribute. */
5799
5800 static void
5801 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5802 {
5803 dw_loc_descr_ref loc;
5804 rtx r;
5805
5806 CHECKSUM (at->dw_attr);
5807
5808 /* We don't care that this was compiled with a different compiler
5809 snapshot; if the output is the same, that's what matters. */
5810 if (at->dw_attr == DW_AT_producer)
5811 return;
5812
5813 switch (AT_class (at))
5814 {
5815 case dw_val_class_const:
5816 CHECKSUM (at->dw_attr_val.v.val_int);
5817 break;
5818 case dw_val_class_unsigned_const:
5819 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5820 break;
5821 case dw_val_class_const_double:
5822 CHECKSUM (at->dw_attr_val.v.val_double);
5823 break;
5824 case dw_val_class_wide_int:
5825 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5826 get_full_len (*at->dw_attr_val.v.val_wide)
5827 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5828 break;
5829 case dw_val_class_vec:
5830 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5831 (at->dw_attr_val.v.val_vec.length
5832 * at->dw_attr_val.v.val_vec.elt_size));
5833 break;
5834 case dw_val_class_flag:
5835 CHECKSUM (at->dw_attr_val.v.val_flag);
5836 break;
5837 case dw_val_class_str:
5838 CHECKSUM_STRING (AT_string (at));
5839 break;
5840
5841 case dw_val_class_addr:
5842 r = AT_addr (at);
5843 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5844 CHECKSUM_STRING (XSTR (r, 0));
5845 break;
5846
5847 case dw_val_class_offset:
5848 CHECKSUM (at->dw_attr_val.v.val_offset);
5849 break;
5850
5851 case dw_val_class_loc:
5852 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5853 loc_checksum (loc, ctx);
5854 break;
5855
5856 case dw_val_class_die_ref:
5857 die_checksum (AT_ref (at), ctx, mark);
5858 break;
5859
5860 case dw_val_class_fde_ref:
5861 case dw_val_class_vms_delta:
5862 case dw_val_class_lbl_id:
5863 case dw_val_class_lineptr:
5864 case dw_val_class_macptr:
5865 case dw_val_class_high_pc:
5866 break;
5867
5868 case dw_val_class_file:
5869 CHECKSUM_STRING (AT_file (at)->filename);
5870 break;
5871
5872 case dw_val_class_data8:
5873 CHECKSUM (at->dw_attr_val.v.val_data8);
5874 break;
5875
5876 default:
5877 break;
5878 }
5879 }
5880
5881 /* Calculate the checksum of a DIE. */
5882
5883 static void
5884 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5885 {
5886 dw_die_ref c;
5887 dw_attr_ref a;
5888 unsigned ix;
5889
5890 /* To avoid infinite recursion. */
5891 if (die->die_mark)
5892 {
5893 CHECKSUM (die->die_mark);
5894 return;
5895 }
5896 die->die_mark = ++(*mark);
5897
5898 CHECKSUM (die->die_tag);
5899
5900 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5901 attr_checksum (a, ctx, mark);
5902
5903 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5904 }
5905
5906 #undef CHECKSUM
5907 #undef CHECKSUM_BLOCK
5908 #undef CHECKSUM_STRING
5909
5910 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5911 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5912 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5913 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5914 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5915 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5916 #define CHECKSUM_ATTR(FOO) \
5917 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5918
5919 /* Calculate the checksum of a number in signed LEB128 format. */
5920
5921 static void
5922 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5923 {
5924 unsigned char byte;
5925 bool more;
5926
5927 while (1)
5928 {
5929 byte = (value & 0x7f);
5930 value >>= 7;
5931 more = !((value == 0 && (byte & 0x40) == 0)
5932 || (value == -1 && (byte & 0x40) != 0));
5933 if (more)
5934 byte |= 0x80;
5935 CHECKSUM (byte);
5936 if (!more)
5937 break;
5938 }
5939 }
5940
5941 /* Calculate the checksum of a number in unsigned LEB128 format. */
5942
5943 static void
5944 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5945 {
5946 while (1)
5947 {
5948 unsigned char byte = (value & 0x7f);
5949 value >>= 7;
5950 if (value != 0)
5951 /* More bytes to follow. */
5952 byte |= 0x80;
5953 CHECKSUM (byte);
5954 if (value == 0)
5955 break;
5956 }
5957 }
5958
5959 /* Checksum the context of the DIE. This adds the names of any
5960 surrounding namespaces or structures to the checksum. */
5961
5962 static void
5963 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5964 {
5965 const char *name;
5966 dw_die_ref spec;
5967 int tag = die->die_tag;
5968
5969 if (tag != DW_TAG_namespace
5970 && tag != DW_TAG_structure_type
5971 && tag != DW_TAG_class_type)
5972 return;
5973
5974 name = get_AT_string (die, DW_AT_name);
5975
5976 spec = get_AT_ref (die, DW_AT_specification);
5977 if (spec != NULL)
5978 die = spec;
5979
5980 if (die->die_parent != NULL)
5981 checksum_die_context (die->die_parent, ctx);
5982
5983 CHECKSUM_ULEB128 ('C');
5984 CHECKSUM_ULEB128 (tag);
5985 if (name != NULL)
5986 CHECKSUM_STRING (name);
5987 }
5988
5989 /* Calculate the checksum of a location expression. */
5990
5991 static inline void
5992 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5993 {
5994 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5995 were emitted as a DW_FORM_sdata instead of a location expression. */
5996 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5997 {
5998 CHECKSUM_ULEB128 (DW_FORM_sdata);
5999 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6000 return;
6001 }
6002
6003 /* Otherwise, just checksum the raw location expression. */
6004 while (loc != NULL)
6005 {
6006 inchash::hash hstate;
6007 hashval_t hash;
6008
6009 CHECKSUM_ULEB128 (loc->dtprel);
6010 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6011 hash_loc_operands (loc, hstate);
6012 hash = hstate.end ();
6013 CHECKSUM (hash);
6014 loc = loc->dw_loc_next;
6015 }
6016 }
6017
6018 /* Calculate the checksum of an attribute. */
6019
6020 static void
6021 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6022 struct md5_ctx *ctx, int *mark)
6023 {
6024 dw_loc_descr_ref loc;
6025 rtx r;
6026
6027 if (AT_class (at) == dw_val_class_die_ref)
6028 {
6029 dw_die_ref target_die = AT_ref (at);
6030
6031 /* For pointer and reference types, we checksum only the (qualified)
6032 name of the target type (if there is a name). For friend entries,
6033 we checksum only the (qualified) name of the target type or function.
6034 This allows the checksum to remain the same whether the target type
6035 is complete or not. */
6036 if ((at->dw_attr == DW_AT_type
6037 && (tag == DW_TAG_pointer_type
6038 || tag == DW_TAG_reference_type
6039 || tag == DW_TAG_rvalue_reference_type
6040 || tag == DW_TAG_ptr_to_member_type))
6041 || (at->dw_attr == DW_AT_friend
6042 && tag == DW_TAG_friend))
6043 {
6044 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6045
6046 if (name_attr != NULL)
6047 {
6048 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6049
6050 if (decl == NULL)
6051 decl = target_die;
6052 CHECKSUM_ULEB128 ('N');
6053 CHECKSUM_ULEB128 (at->dw_attr);
6054 if (decl->die_parent != NULL)
6055 checksum_die_context (decl->die_parent, ctx);
6056 CHECKSUM_ULEB128 ('E');
6057 CHECKSUM_STRING (AT_string (name_attr));
6058 return;
6059 }
6060 }
6061
6062 /* For all other references to another DIE, we check to see if the
6063 target DIE has already been visited. If it has, we emit a
6064 backward reference; if not, we descend recursively. */
6065 if (target_die->die_mark > 0)
6066 {
6067 CHECKSUM_ULEB128 ('R');
6068 CHECKSUM_ULEB128 (at->dw_attr);
6069 CHECKSUM_ULEB128 (target_die->die_mark);
6070 }
6071 else
6072 {
6073 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6074
6075 if (decl == NULL)
6076 decl = target_die;
6077 target_die->die_mark = ++(*mark);
6078 CHECKSUM_ULEB128 ('T');
6079 CHECKSUM_ULEB128 (at->dw_attr);
6080 if (decl->die_parent != NULL)
6081 checksum_die_context (decl->die_parent, ctx);
6082 die_checksum_ordered (target_die, ctx, mark);
6083 }
6084 return;
6085 }
6086
6087 CHECKSUM_ULEB128 ('A');
6088 CHECKSUM_ULEB128 (at->dw_attr);
6089
6090 switch (AT_class (at))
6091 {
6092 case dw_val_class_const:
6093 CHECKSUM_ULEB128 (DW_FORM_sdata);
6094 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6095 break;
6096
6097 case dw_val_class_unsigned_const:
6098 CHECKSUM_ULEB128 (DW_FORM_sdata);
6099 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6100 break;
6101
6102 case dw_val_class_const_double:
6103 CHECKSUM_ULEB128 (DW_FORM_block);
6104 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6105 CHECKSUM (at->dw_attr_val.v.val_double);
6106 break;
6107
6108 case dw_val_class_wide_int:
6109 CHECKSUM_ULEB128 (DW_FORM_block);
6110 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6111 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6112 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6113 get_full_len (*at->dw_attr_val.v.val_wide)
6114 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6115 break;
6116
6117 case dw_val_class_vec:
6118 CHECKSUM_ULEB128 (DW_FORM_block);
6119 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6120 * at->dw_attr_val.v.val_vec.elt_size);
6121 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6122 (at->dw_attr_val.v.val_vec.length
6123 * at->dw_attr_val.v.val_vec.elt_size));
6124 break;
6125
6126 case dw_val_class_flag:
6127 CHECKSUM_ULEB128 (DW_FORM_flag);
6128 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6129 break;
6130
6131 case dw_val_class_str:
6132 CHECKSUM_ULEB128 (DW_FORM_string);
6133 CHECKSUM_STRING (AT_string (at));
6134 break;
6135
6136 case dw_val_class_addr:
6137 r = AT_addr (at);
6138 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6139 CHECKSUM_ULEB128 (DW_FORM_string);
6140 CHECKSUM_STRING (XSTR (r, 0));
6141 break;
6142
6143 case dw_val_class_offset:
6144 CHECKSUM_ULEB128 (DW_FORM_sdata);
6145 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6146 break;
6147
6148 case dw_val_class_loc:
6149 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6150 loc_checksum_ordered (loc, ctx);
6151 break;
6152
6153 case dw_val_class_fde_ref:
6154 case dw_val_class_lbl_id:
6155 case dw_val_class_lineptr:
6156 case dw_val_class_macptr:
6157 case dw_val_class_high_pc:
6158 break;
6159
6160 case dw_val_class_file:
6161 CHECKSUM_ULEB128 (DW_FORM_string);
6162 CHECKSUM_STRING (AT_file (at)->filename);
6163 break;
6164
6165 case dw_val_class_data8:
6166 CHECKSUM (at->dw_attr_val.v.val_data8);
6167 break;
6168
6169 default:
6170 break;
6171 }
6172 }
6173
6174 struct checksum_attributes
6175 {
6176 dw_attr_ref at_name;
6177 dw_attr_ref at_type;
6178 dw_attr_ref at_friend;
6179 dw_attr_ref at_accessibility;
6180 dw_attr_ref at_address_class;
6181 dw_attr_ref at_allocated;
6182 dw_attr_ref at_artificial;
6183 dw_attr_ref at_associated;
6184 dw_attr_ref at_binary_scale;
6185 dw_attr_ref at_bit_offset;
6186 dw_attr_ref at_bit_size;
6187 dw_attr_ref at_bit_stride;
6188 dw_attr_ref at_byte_size;
6189 dw_attr_ref at_byte_stride;
6190 dw_attr_ref at_const_value;
6191 dw_attr_ref at_containing_type;
6192 dw_attr_ref at_count;
6193 dw_attr_ref at_data_location;
6194 dw_attr_ref at_data_member_location;
6195 dw_attr_ref at_decimal_scale;
6196 dw_attr_ref at_decimal_sign;
6197 dw_attr_ref at_default_value;
6198 dw_attr_ref at_digit_count;
6199 dw_attr_ref at_discr;
6200 dw_attr_ref at_discr_list;
6201 dw_attr_ref at_discr_value;
6202 dw_attr_ref at_encoding;
6203 dw_attr_ref at_endianity;
6204 dw_attr_ref at_explicit;
6205 dw_attr_ref at_is_optional;
6206 dw_attr_ref at_location;
6207 dw_attr_ref at_lower_bound;
6208 dw_attr_ref at_mutable;
6209 dw_attr_ref at_ordering;
6210 dw_attr_ref at_picture_string;
6211 dw_attr_ref at_prototyped;
6212 dw_attr_ref at_small;
6213 dw_attr_ref at_segment;
6214 dw_attr_ref at_string_length;
6215 dw_attr_ref at_threads_scaled;
6216 dw_attr_ref at_upper_bound;
6217 dw_attr_ref at_use_location;
6218 dw_attr_ref at_use_UTF8;
6219 dw_attr_ref at_variable_parameter;
6220 dw_attr_ref at_virtuality;
6221 dw_attr_ref at_visibility;
6222 dw_attr_ref at_vtable_elem_location;
6223 };
6224
6225 /* Collect the attributes that we will want to use for the checksum. */
6226
6227 static void
6228 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6229 {
6230 dw_attr_ref a;
6231 unsigned ix;
6232
6233 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6234 {
6235 switch (a->dw_attr)
6236 {
6237 case DW_AT_name:
6238 attrs->at_name = a;
6239 break;
6240 case DW_AT_type:
6241 attrs->at_type = a;
6242 break;
6243 case DW_AT_friend:
6244 attrs->at_friend = a;
6245 break;
6246 case DW_AT_accessibility:
6247 attrs->at_accessibility = a;
6248 break;
6249 case DW_AT_address_class:
6250 attrs->at_address_class = a;
6251 break;
6252 case DW_AT_allocated:
6253 attrs->at_allocated = a;
6254 break;
6255 case DW_AT_artificial:
6256 attrs->at_artificial = a;
6257 break;
6258 case DW_AT_associated:
6259 attrs->at_associated = a;
6260 break;
6261 case DW_AT_binary_scale:
6262 attrs->at_binary_scale = a;
6263 break;
6264 case DW_AT_bit_offset:
6265 attrs->at_bit_offset = a;
6266 break;
6267 case DW_AT_bit_size:
6268 attrs->at_bit_size = a;
6269 break;
6270 case DW_AT_bit_stride:
6271 attrs->at_bit_stride = a;
6272 break;
6273 case DW_AT_byte_size:
6274 attrs->at_byte_size = a;
6275 break;
6276 case DW_AT_byte_stride:
6277 attrs->at_byte_stride = a;
6278 break;
6279 case DW_AT_const_value:
6280 attrs->at_const_value = a;
6281 break;
6282 case DW_AT_containing_type:
6283 attrs->at_containing_type = a;
6284 break;
6285 case DW_AT_count:
6286 attrs->at_count = a;
6287 break;
6288 case DW_AT_data_location:
6289 attrs->at_data_location = a;
6290 break;
6291 case DW_AT_data_member_location:
6292 attrs->at_data_member_location = a;
6293 break;
6294 case DW_AT_decimal_scale:
6295 attrs->at_decimal_scale = a;
6296 break;
6297 case DW_AT_decimal_sign:
6298 attrs->at_decimal_sign = a;
6299 break;
6300 case DW_AT_default_value:
6301 attrs->at_default_value = a;
6302 break;
6303 case DW_AT_digit_count:
6304 attrs->at_digit_count = a;
6305 break;
6306 case DW_AT_discr:
6307 attrs->at_discr = a;
6308 break;
6309 case DW_AT_discr_list:
6310 attrs->at_discr_list = a;
6311 break;
6312 case DW_AT_discr_value:
6313 attrs->at_discr_value = a;
6314 break;
6315 case DW_AT_encoding:
6316 attrs->at_encoding = a;
6317 break;
6318 case DW_AT_endianity:
6319 attrs->at_endianity = a;
6320 break;
6321 case DW_AT_explicit:
6322 attrs->at_explicit = a;
6323 break;
6324 case DW_AT_is_optional:
6325 attrs->at_is_optional = a;
6326 break;
6327 case DW_AT_location:
6328 attrs->at_location = a;
6329 break;
6330 case DW_AT_lower_bound:
6331 attrs->at_lower_bound = a;
6332 break;
6333 case DW_AT_mutable:
6334 attrs->at_mutable = a;
6335 break;
6336 case DW_AT_ordering:
6337 attrs->at_ordering = a;
6338 break;
6339 case DW_AT_picture_string:
6340 attrs->at_picture_string = a;
6341 break;
6342 case DW_AT_prototyped:
6343 attrs->at_prototyped = a;
6344 break;
6345 case DW_AT_small:
6346 attrs->at_small = a;
6347 break;
6348 case DW_AT_segment:
6349 attrs->at_segment = a;
6350 break;
6351 case DW_AT_string_length:
6352 attrs->at_string_length = a;
6353 break;
6354 case DW_AT_threads_scaled:
6355 attrs->at_threads_scaled = a;
6356 break;
6357 case DW_AT_upper_bound:
6358 attrs->at_upper_bound = a;
6359 break;
6360 case DW_AT_use_location:
6361 attrs->at_use_location = a;
6362 break;
6363 case DW_AT_use_UTF8:
6364 attrs->at_use_UTF8 = a;
6365 break;
6366 case DW_AT_variable_parameter:
6367 attrs->at_variable_parameter = a;
6368 break;
6369 case DW_AT_virtuality:
6370 attrs->at_virtuality = a;
6371 break;
6372 case DW_AT_visibility:
6373 attrs->at_visibility = a;
6374 break;
6375 case DW_AT_vtable_elem_location:
6376 attrs->at_vtable_elem_location = a;
6377 break;
6378 default:
6379 break;
6380 }
6381 }
6382 }
6383
6384 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6385
6386 static void
6387 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6388 {
6389 dw_die_ref c;
6390 dw_die_ref decl;
6391 struct checksum_attributes attrs;
6392
6393 CHECKSUM_ULEB128 ('D');
6394 CHECKSUM_ULEB128 (die->die_tag);
6395
6396 memset (&attrs, 0, sizeof (attrs));
6397
6398 decl = get_AT_ref (die, DW_AT_specification);
6399 if (decl != NULL)
6400 collect_checksum_attributes (&attrs, decl);
6401 collect_checksum_attributes (&attrs, die);
6402
6403 CHECKSUM_ATTR (attrs.at_name);
6404 CHECKSUM_ATTR (attrs.at_accessibility);
6405 CHECKSUM_ATTR (attrs.at_address_class);
6406 CHECKSUM_ATTR (attrs.at_allocated);
6407 CHECKSUM_ATTR (attrs.at_artificial);
6408 CHECKSUM_ATTR (attrs.at_associated);
6409 CHECKSUM_ATTR (attrs.at_binary_scale);
6410 CHECKSUM_ATTR (attrs.at_bit_offset);
6411 CHECKSUM_ATTR (attrs.at_bit_size);
6412 CHECKSUM_ATTR (attrs.at_bit_stride);
6413 CHECKSUM_ATTR (attrs.at_byte_size);
6414 CHECKSUM_ATTR (attrs.at_byte_stride);
6415 CHECKSUM_ATTR (attrs.at_const_value);
6416 CHECKSUM_ATTR (attrs.at_containing_type);
6417 CHECKSUM_ATTR (attrs.at_count);
6418 CHECKSUM_ATTR (attrs.at_data_location);
6419 CHECKSUM_ATTR (attrs.at_data_member_location);
6420 CHECKSUM_ATTR (attrs.at_decimal_scale);
6421 CHECKSUM_ATTR (attrs.at_decimal_sign);
6422 CHECKSUM_ATTR (attrs.at_default_value);
6423 CHECKSUM_ATTR (attrs.at_digit_count);
6424 CHECKSUM_ATTR (attrs.at_discr);
6425 CHECKSUM_ATTR (attrs.at_discr_list);
6426 CHECKSUM_ATTR (attrs.at_discr_value);
6427 CHECKSUM_ATTR (attrs.at_encoding);
6428 CHECKSUM_ATTR (attrs.at_endianity);
6429 CHECKSUM_ATTR (attrs.at_explicit);
6430 CHECKSUM_ATTR (attrs.at_is_optional);
6431 CHECKSUM_ATTR (attrs.at_location);
6432 CHECKSUM_ATTR (attrs.at_lower_bound);
6433 CHECKSUM_ATTR (attrs.at_mutable);
6434 CHECKSUM_ATTR (attrs.at_ordering);
6435 CHECKSUM_ATTR (attrs.at_picture_string);
6436 CHECKSUM_ATTR (attrs.at_prototyped);
6437 CHECKSUM_ATTR (attrs.at_small);
6438 CHECKSUM_ATTR (attrs.at_segment);
6439 CHECKSUM_ATTR (attrs.at_string_length);
6440 CHECKSUM_ATTR (attrs.at_threads_scaled);
6441 CHECKSUM_ATTR (attrs.at_upper_bound);
6442 CHECKSUM_ATTR (attrs.at_use_location);
6443 CHECKSUM_ATTR (attrs.at_use_UTF8);
6444 CHECKSUM_ATTR (attrs.at_variable_parameter);
6445 CHECKSUM_ATTR (attrs.at_virtuality);
6446 CHECKSUM_ATTR (attrs.at_visibility);
6447 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6448 CHECKSUM_ATTR (attrs.at_type);
6449 CHECKSUM_ATTR (attrs.at_friend);
6450
6451 /* Checksum the child DIEs. */
6452 c = die->die_child;
6453 if (c) do {
6454 dw_attr_ref name_attr;
6455
6456 c = c->die_sib;
6457 name_attr = get_AT (c, DW_AT_name);
6458 if (is_template_instantiation (c))
6459 {
6460 /* Ignore instantiations of member type and function templates. */
6461 }
6462 else if (name_attr != NULL
6463 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6464 {
6465 /* Use a shallow checksum for named nested types and member
6466 functions. */
6467 CHECKSUM_ULEB128 ('S');
6468 CHECKSUM_ULEB128 (c->die_tag);
6469 CHECKSUM_STRING (AT_string (name_attr));
6470 }
6471 else
6472 {
6473 /* Use a deep checksum for other children. */
6474 /* Mark this DIE so it gets processed when unmarking. */
6475 if (c->die_mark == 0)
6476 c->die_mark = -1;
6477 die_checksum_ordered (c, ctx, mark);
6478 }
6479 } while (c != die->die_child);
6480
6481 CHECKSUM_ULEB128 (0);
6482 }
6483
6484 /* Add a type name and tag to a hash. */
6485 static void
6486 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6487 {
6488 CHECKSUM_ULEB128 (tag);
6489 CHECKSUM_STRING (name);
6490 }
6491
6492 #undef CHECKSUM
6493 #undef CHECKSUM_STRING
6494 #undef CHECKSUM_ATTR
6495 #undef CHECKSUM_LEB128
6496 #undef CHECKSUM_ULEB128
6497
6498 /* Generate the type signature for DIE. This is computed by generating an
6499 MD5 checksum over the DIE's tag, its relevant attributes, and its
6500 children. Attributes that are references to other DIEs are processed
6501 by recursion, using the MARK field to prevent infinite recursion.
6502 If the DIE is nested inside a namespace or another type, we also
6503 need to include that context in the signature. The lower 64 bits
6504 of the resulting MD5 checksum comprise the signature. */
6505
6506 static void
6507 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6508 {
6509 int mark;
6510 const char *name;
6511 unsigned char checksum[16];
6512 struct md5_ctx ctx;
6513 dw_die_ref decl;
6514 dw_die_ref parent;
6515
6516 name = get_AT_string (die, DW_AT_name);
6517 decl = get_AT_ref (die, DW_AT_specification);
6518 parent = get_die_parent (die);
6519
6520 /* First, compute a signature for just the type name (and its surrounding
6521 context, if any. This is stored in the type unit DIE for link-time
6522 ODR (one-definition rule) checking. */
6523
6524 if (is_cxx () && name != NULL)
6525 {
6526 md5_init_ctx (&ctx);
6527
6528 /* Checksum the names of surrounding namespaces and structures. */
6529 if (parent != NULL)
6530 checksum_die_context (parent, &ctx);
6531
6532 /* Checksum the current DIE. */
6533 die_odr_checksum (die->die_tag, name, &ctx);
6534 md5_finish_ctx (&ctx, checksum);
6535
6536 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6537 }
6538
6539 /* Next, compute the complete type signature. */
6540
6541 md5_init_ctx (&ctx);
6542 mark = 1;
6543 die->die_mark = mark;
6544
6545 /* Checksum the names of surrounding namespaces and structures. */
6546 if (parent != NULL)
6547 checksum_die_context (parent, &ctx);
6548
6549 /* Checksum the DIE and its children. */
6550 die_checksum_ordered (die, &ctx, &mark);
6551 unmark_all_dies (die);
6552 md5_finish_ctx (&ctx, checksum);
6553
6554 /* Store the signature in the type node and link the type DIE and the
6555 type node together. */
6556 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6557 DWARF_TYPE_SIGNATURE_SIZE);
6558 die->comdat_type_p = true;
6559 die->die_id.die_type_node = type_node;
6560 type_node->type_die = die;
6561
6562 /* If the DIE is a specification, link its declaration to the type node
6563 as well. */
6564 if (decl != NULL)
6565 {
6566 decl->comdat_type_p = true;
6567 decl->die_id.die_type_node = type_node;
6568 }
6569 }
6570
6571 /* Do the location expressions look same? */
6572 static inline int
6573 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6574 {
6575 return loc1->dw_loc_opc == loc2->dw_loc_opc
6576 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6577 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6578 }
6579
6580 /* Do the values look the same? */
6581 static int
6582 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6583 {
6584 dw_loc_descr_ref loc1, loc2;
6585 rtx r1, r2;
6586
6587 if (v1->val_class != v2->val_class)
6588 return 0;
6589
6590 switch (v1->val_class)
6591 {
6592 case dw_val_class_const:
6593 return v1->v.val_int == v2->v.val_int;
6594 case dw_val_class_unsigned_const:
6595 return v1->v.val_unsigned == v2->v.val_unsigned;
6596 case dw_val_class_const_double:
6597 return v1->v.val_double.high == v2->v.val_double.high
6598 && v1->v.val_double.low == v2->v.val_double.low;
6599 case dw_val_class_wide_int:
6600 return *v1->v.val_wide == *v2->v.val_wide;
6601 case dw_val_class_vec:
6602 if (v1->v.val_vec.length != v2->v.val_vec.length
6603 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6604 return 0;
6605 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6606 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6607 return 0;
6608 return 1;
6609 case dw_val_class_flag:
6610 return v1->v.val_flag == v2->v.val_flag;
6611 case dw_val_class_str:
6612 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6613
6614 case dw_val_class_addr:
6615 r1 = v1->v.val_addr;
6616 r2 = v2->v.val_addr;
6617 if (GET_CODE (r1) != GET_CODE (r2))
6618 return 0;
6619 return !rtx_equal_p (r1, r2);
6620
6621 case dw_val_class_offset:
6622 return v1->v.val_offset == v2->v.val_offset;
6623
6624 case dw_val_class_loc:
6625 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6626 loc1 && loc2;
6627 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6628 if (!same_loc_p (loc1, loc2, mark))
6629 return 0;
6630 return !loc1 && !loc2;
6631
6632 case dw_val_class_die_ref:
6633 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6634
6635 case dw_val_class_fde_ref:
6636 case dw_val_class_vms_delta:
6637 case dw_val_class_lbl_id:
6638 case dw_val_class_lineptr:
6639 case dw_val_class_macptr:
6640 case dw_val_class_high_pc:
6641 return 1;
6642
6643 case dw_val_class_file:
6644 return v1->v.val_file == v2->v.val_file;
6645
6646 case dw_val_class_data8:
6647 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6648
6649 default:
6650 return 1;
6651 }
6652 }
6653
6654 /* Do the attributes look the same? */
6655
6656 static int
6657 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6658 {
6659 if (at1->dw_attr != at2->dw_attr)
6660 return 0;
6661
6662 /* We don't care that this was compiled with a different compiler
6663 snapshot; if the output is the same, that's what matters. */
6664 if (at1->dw_attr == DW_AT_producer)
6665 return 1;
6666
6667 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6668 }
6669
6670 /* Do the dies look the same? */
6671
6672 static int
6673 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6674 {
6675 dw_die_ref c1, c2;
6676 dw_attr_ref a1;
6677 unsigned ix;
6678
6679 /* To avoid infinite recursion. */
6680 if (die1->die_mark)
6681 return die1->die_mark == die2->die_mark;
6682 die1->die_mark = die2->die_mark = ++(*mark);
6683
6684 if (die1->die_tag != die2->die_tag)
6685 return 0;
6686
6687 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6688 return 0;
6689
6690 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6691 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6692 return 0;
6693
6694 c1 = die1->die_child;
6695 c2 = die2->die_child;
6696 if (! c1)
6697 {
6698 if (c2)
6699 return 0;
6700 }
6701 else
6702 for (;;)
6703 {
6704 if (!same_die_p (c1, c2, mark))
6705 return 0;
6706 c1 = c1->die_sib;
6707 c2 = c2->die_sib;
6708 if (c1 == die1->die_child)
6709 {
6710 if (c2 == die2->die_child)
6711 break;
6712 else
6713 return 0;
6714 }
6715 }
6716
6717 return 1;
6718 }
6719
6720 /* Do the dies look the same? Wrapper around same_die_p. */
6721
6722 static int
6723 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6724 {
6725 int mark = 0;
6726 int ret = same_die_p (die1, die2, &mark);
6727
6728 unmark_all_dies (die1);
6729 unmark_all_dies (die2);
6730
6731 return ret;
6732 }
6733
6734 /* The prefix to attach to symbols on DIEs in the current comdat debug
6735 info section. */
6736 static const char *comdat_symbol_id;
6737
6738 /* The index of the current symbol within the current comdat CU. */
6739 static unsigned int comdat_symbol_number;
6740
6741 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6742 children, and set comdat_symbol_id accordingly. */
6743
6744 static void
6745 compute_section_prefix (dw_die_ref unit_die)
6746 {
6747 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6748 const char *base = die_name ? lbasename (die_name) : "anonymous";
6749 char *name = XALLOCAVEC (char, strlen (base) + 64);
6750 char *p;
6751 int i, mark;
6752 unsigned char checksum[16];
6753 struct md5_ctx ctx;
6754
6755 /* Compute the checksum of the DIE, then append part of it as hex digits to
6756 the name filename of the unit. */
6757
6758 md5_init_ctx (&ctx);
6759 mark = 0;
6760 die_checksum (unit_die, &ctx, &mark);
6761 unmark_all_dies (unit_die);
6762 md5_finish_ctx (&ctx, checksum);
6763
6764 sprintf (name, "%s.", base);
6765 clean_symbol_name (name);
6766
6767 p = name + strlen (name);
6768 for (i = 0; i < 4; i++)
6769 {
6770 sprintf (p, "%.2x", checksum[i]);
6771 p += 2;
6772 }
6773
6774 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6775 comdat_symbol_number = 0;
6776 }
6777
6778 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6779
6780 static int
6781 is_type_die (dw_die_ref die)
6782 {
6783 switch (die->die_tag)
6784 {
6785 case DW_TAG_array_type:
6786 case DW_TAG_class_type:
6787 case DW_TAG_interface_type:
6788 case DW_TAG_enumeration_type:
6789 case DW_TAG_pointer_type:
6790 case DW_TAG_reference_type:
6791 case DW_TAG_rvalue_reference_type:
6792 case DW_TAG_string_type:
6793 case DW_TAG_structure_type:
6794 case DW_TAG_subroutine_type:
6795 case DW_TAG_union_type:
6796 case DW_TAG_ptr_to_member_type:
6797 case DW_TAG_set_type:
6798 case DW_TAG_subrange_type:
6799 case DW_TAG_base_type:
6800 case DW_TAG_const_type:
6801 case DW_TAG_file_type:
6802 case DW_TAG_packed_type:
6803 case DW_TAG_volatile_type:
6804 case DW_TAG_typedef:
6805 return 1;
6806 default:
6807 return 0;
6808 }
6809 }
6810
6811 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6812 Basically, we want to choose the bits that are likely to be shared between
6813 compilations (types) and leave out the bits that are specific to individual
6814 compilations (functions). */
6815
6816 static int
6817 is_comdat_die (dw_die_ref c)
6818 {
6819 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6820 we do for stabs. The advantage is a greater likelihood of sharing between
6821 objects that don't include headers in the same order (and therefore would
6822 put the base types in a different comdat). jason 8/28/00 */
6823
6824 if (c->die_tag == DW_TAG_base_type)
6825 return 0;
6826
6827 if (c->die_tag == DW_TAG_pointer_type
6828 || c->die_tag == DW_TAG_reference_type
6829 || c->die_tag == DW_TAG_rvalue_reference_type
6830 || c->die_tag == DW_TAG_const_type
6831 || c->die_tag == DW_TAG_volatile_type)
6832 {
6833 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6834
6835 return t ? is_comdat_die (t) : 0;
6836 }
6837
6838 return is_type_die (c);
6839 }
6840
6841 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6842 compilation unit. */
6843
6844 static int
6845 is_symbol_die (dw_die_ref c)
6846 {
6847 return (is_type_die (c)
6848 || is_declaration_die (c)
6849 || c->die_tag == DW_TAG_namespace
6850 || c->die_tag == DW_TAG_module);
6851 }
6852
6853 /* Returns true iff C is a compile-unit DIE. */
6854
6855 static inline bool
6856 is_cu_die (dw_die_ref c)
6857 {
6858 return c && c->die_tag == DW_TAG_compile_unit;
6859 }
6860
6861 /* Returns true iff C is a unit DIE of some sort. */
6862
6863 static inline bool
6864 is_unit_die (dw_die_ref c)
6865 {
6866 return c && (c->die_tag == DW_TAG_compile_unit
6867 || c->die_tag == DW_TAG_partial_unit
6868 || c->die_tag == DW_TAG_type_unit);
6869 }
6870
6871 /* Returns true iff C is a namespace DIE. */
6872
6873 static inline bool
6874 is_namespace_die (dw_die_ref c)
6875 {
6876 return c && c->die_tag == DW_TAG_namespace;
6877 }
6878
6879 /* Returns true iff C is a class or structure DIE. */
6880
6881 static inline bool
6882 is_class_die (dw_die_ref c)
6883 {
6884 return c && (c->die_tag == DW_TAG_class_type
6885 || c->die_tag == DW_TAG_structure_type);
6886 }
6887
6888 /* Return non-zero if this DIE is a template parameter. */
6889
6890 static inline bool
6891 is_template_parameter (dw_die_ref die)
6892 {
6893 switch (die->die_tag)
6894 {
6895 case DW_TAG_template_type_param:
6896 case DW_TAG_template_value_param:
6897 case DW_TAG_GNU_template_template_param:
6898 case DW_TAG_GNU_template_parameter_pack:
6899 return true;
6900 default:
6901 return false;
6902 }
6903 }
6904
6905 /* Return non-zero if this DIE represents a template instantiation. */
6906
6907 static inline bool
6908 is_template_instantiation (dw_die_ref die)
6909 {
6910 dw_die_ref c;
6911
6912 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6913 return false;
6914 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6915 return false;
6916 }
6917
6918 static char *
6919 gen_internal_sym (const char *prefix)
6920 {
6921 char buf[256];
6922
6923 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6924 return xstrdup (buf);
6925 }
6926
6927 /* Assign symbols to all worthy DIEs under DIE. */
6928
6929 static void
6930 assign_symbol_names (dw_die_ref die)
6931 {
6932 dw_die_ref c;
6933
6934 if (is_symbol_die (die) && !die->comdat_type_p)
6935 {
6936 if (comdat_symbol_id)
6937 {
6938 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6939
6940 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6941 comdat_symbol_id, comdat_symbol_number++);
6942 die->die_id.die_symbol = xstrdup (p);
6943 }
6944 else
6945 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6946 }
6947
6948 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6949 }
6950
6951 struct cu_hash_table_entry
6952 {
6953 dw_die_ref cu;
6954 unsigned min_comdat_num, max_comdat_num;
6955 struct cu_hash_table_entry *next;
6956 };
6957
6958 /* Helpers to manipulate hash table of CUs. */
6959
6960 struct cu_hash_table_entry_hasher
6961 {
6962 typedef cu_hash_table_entry *value_type;
6963 typedef die_struct *compare_type;
6964 static inline hashval_t hash (const cu_hash_table_entry *);
6965 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6966 static inline void remove (cu_hash_table_entry *);
6967 };
6968
6969 inline hashval_t
6970 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6971 {
6972 return htab_hash_string (entry->cu->die_id.die_symbol);
6973 }
6974
6975 inline bool
6976 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6977 const die_struct *entry2)
6978 {
6979 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6980 }
6981
6982 inline void
6983 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6984 {
6985 struct cu_hash_table_entry *next;
6986
6987 while (entry)
6988 {
6989 next = entry->next;
6990 free (entry);
6991 entry = next;
6992 }
6993 }
6994
6995 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6996
6997 /* Check whether we have already seen this CU and set up SYM_NUM
6998 accordingly. */
6999 static int
7000 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7001 {
7002 struct cu_hash_table_entry dummy;
7003 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7004
7005 dummy.max_comdat_num = 0;
7006
7007 slot = htable->find_slot_with_hash (cu,
7008 htab_hash_string (cu->die_id.die_symbol),
7009 INSERT);
7010 entry = *slot;
7011
7012 for (; entry; last = entry, entry = entry->next)
7013 {
7014 if (same_die_p_wrap (cu, entry->cu))
7015 break;
7016 }
7017
7018 if (entry)
7019 {
7020 *sym_num = entry->min_comdat_num;
7021 return 1;
7022 }
7023
7024 entry = XCNEW (struct cu_hash_table_entry);
7025 entry->cu = cu;
7026 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7027 entry->next = *slot;
7028 *slot = entry;
7029
7030 return 0;
7031 }
7032
7033 /* Record SYM_NUM to record of CU in HTABLE. */
7034 static void
7035 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7036 unsigned int sym_num)
7037 {
7038 struct cu_hash_table_entry **slot, *entry;
7039
7040 slot = htable->find_slot_with_hash (cu,
7041 htab_hash_string (cu->die_id.die_symbol),
7042 NO_INSERT);
7043 entry = *slot;
7044
7045 entry->max_comdat_num = sym_num;
7046 }
7047
7048 /* Traverse the DIE (which is always comp_unit_die), and set up
7049 additional compilation units for each of the include files we see
7050 bracketed by BINCL/EINCL. */
7051
7052 static void
7053 break_out_includes (dw_die_ref die)
7054 {
7055 dw_die_ref c;
7056 dw_die_ref unit = NULL;
7057 limbo_die_node *node, **pnode;
7058
7059 c = die->die_child;
7060 if (c) do {
7061 dw_die_ref prev = c;
7062 c = c->die_sib;
7063 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7064 || (unit && is_comdat_die (c)))
7065 {
7066 dw_die_ref next = c->die_sib;
7067
7068 /* This DIE is for a secondary CU; remove it from the main one. */
7069 remove_child_with_prev (c, prev);
7070
7071 if (c->die_tag == DW_TAG_GNU_BINCL)
7072 unit = push_new_compile_unit (unit, c);
7073 else if (c->die_tag == DW_TAG_GNU_EINCL)
7074 unit = pop_compile_unit (unit);
7075 else
7076 add_child_die (unit, c);
7077 c = next;
7078 if (c == die->die_child)
7079 break;
7080 }
7081 } while (c != die->die_child);
7082
7083 #if 0
7084 /* We can only use this in debugging, since the frontend doesn't check
7085 to make sure that we leave every include file we enter. */
7086 gcc_assert (!unit);
7087 #endif
7088
7089 assign_symbol_names (die);
7090 cu_hash_type cu_hash_table (10);
7091 for (node = limbo_die_list, pnode = &limbo_die_list;
7092 node;
7093 node = node->next)
7094 {
7095 int is_dupl;
7096
7097 compute_section_prefix (node->die);
7098 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7099 &comdat_symbol_number);
7100 assign_symbol_names (node->die);
7101 if (is_dupl)
7102 *pnode = node->next;
7103 else
7104 {
7105 pnode = &node->next;
7106 record_comdat_symbol_number (node->die, &cu_hash_table,
7107 comdat_symbol_number);
7108 }
7109 }
7110 }
7111
7112 /* Return non-zero if this DIE is a declaration. */
7113
7114 static int
7115 is_declaration_die (dw_die_ref die)
7116 {
7117 dw_attr_ref a;
7118 unsigned ix;
7119
7120 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7121 if (a->dw_attr == DW_AT_declaration)
7122 return 1;
7123
7124 return 0;
7125 }
7126
7127 /* Return non-zero if this DIE is nested inside a subprogram. */
7128
7129 static int
7130 is_nested_in_subprogram (dw_die_ref die)
7131 {
7132 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7133
7134 if (decl == NULL)
7135 decl = die;
7136 return local_scope_p (decl);
7137 }
7138
7139 /* Return non-zero if this DIE contains a defining declaration of a
7140 subprogram. */
7141
7142 static int
7143 contains_subprogram_definition (dw_die_ref die)
7144 {
7145 dw_die_ref c;
7146
7147 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7148 return 1;
7149 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7150 return 0;
7151 }
7152
7153 /* Return non-zero if this is a type DIE that should be moved to a
7154 COMDAT .debug_types section. */
7155
7156 static int
7157 should_move_die_to_comdat (dw_die_ref die)
7158 {
7159 switch (die->die_tag)
7160 {
7161 case DW_TAG_class_type:
7162 case DW_TAG_structure_type:
7163 case DW_TAG_enumeration_type:
7164 case DW_TAG_union_type:
7165 /* Don't move declarations, inlined instances, types nested in a
7166 subprogram, or types that contain subprogram definitions. */
7167 if (is_declaration_die (die)
7168 || get_AT (die, DW_AT_abstract_origin)
7169 || is_nested_in_subprogram (die)
7170 || contains_subprogram_definition (die))
7171 return 0;
7172 return 1;
7173 case DW_TAG_array_type:
7174 case DW_TAG_interface_type:
7175 case DW_TAG_pointer_type:
7176 case DW_TAG_reference_type:
7177 case DW_TAG_rvalue_reference_type:
7178 case DW_TAG_string_type:
7179 case DW_TAG_subroutine_type:
7180 case DW_TAG_ptr_to_member_type:
7181 case DW_TAG_set_type:
7182 case DW_TAG_subrange_type:
7183 case DW_TAG_base_type:
7184 case DW_TAG_const_type:
7185 case DW_TAG_file_type:
7186 case DW_TAG_packed_type:
7187 case DW_TAG_volatile_type:
7188 case DW_TAG_typedef:
7189 default:
7190 return 0;
7191 }
7192 }
7193
7194 /* Make a clone of DIE. */
7195
7196 static dw_die_ref
7197 clone_die (dw_die_ref die)
7198 {
7199 dw_die_ref clone;
7200 dw_attr_ref a;
7201 unsigned ix;
7202
7203 clone = ggc_cleared_alloc<die_node> ();
7204 clone->die_tag = die->die_tag;
7205
7206 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7207 add_dwarf_attr (clone, a);
7208
7209 return clone;
7210 }
7211
7212 /* Make a clone of the tree rooted at DIE. */
7213
7214 static dw_die_ref
7215 clone_tree (dw_die_ref die)
7216 {
7217 dw_die_ref c;
7218 dw_die_ref clone = clone_die (die);
7219
7220 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7221
7222 return clone;
7223 }
7224
7225 /* Make a clone of DIE as a declaration. */
7226
7227 static dw_die_ref
7228 clone_as_declaration (dw_die_ref die)
7229 {
7230 dw_die_ref clone;
7231 dw_die_ref decl;
7232 dw_attr_ref a;
7233 unsigned ix;
7234
7235 /* If the DIE is already a declaration, just clone it. */
7236 if (is_declaration_die (die))
7237 return clone_die (die);
7238
7239 /* If the DIE is a specification, just clone its declaration DIE. */
7240 decl = get_AT_ref (die, DW_AT_specification);
7241 if (decl != NULL)
7242 {
7243 clone = clone_die (decl);
7244 if (die->comdat_type_p)
7245 add_AT_die_ref (clone, DW_AT_signature, die);
7246 return clone;
7247 }
7248
7249 clone = ggc_cleared_alloc<die_node> ();
7250 clone->die_tag = die->die_tag;
7251
7252 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7253 {
7254 /* We don't want to copy over all attributes.
7255 For example we don't want DW_AT_byte_size because otherwise we will no
7256 longer have a declaration and GDB will treat it as a definition. */
7257
7258 switch (a->dw_attr)
7259 {
7260 case DW_AT_abstract_origin:
7261 case DW_AT_artificial:
7262 case DW_AT_containing_type:
7263 case DW_AT_external:
7264 case DW_AT_name:
7265 case DW_AT_type:
7266 case DW_AT_virtuality:
7267 case DW_AT_linkage_name:
7268 case DW_AT_MIPS_linkage_name:
7269 add_dwarf_attr (clone, a);
7270 break;
7271 case DW_AT_byte_size:
7272 default:
7273 break;
7274 }
7275 }
7276
7277 if (die->comdat_type_p)
7278 add_AT_die_ref (clone, DW_AT_signature, die);
7279
7280 add_AT_flag (clone, DW_AT_declaration, 1);
7281 return clone;
7282 }
7283
7284
7285 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7286
7287 struct decl_table_entry
7288 {
7289 dw_die_ref orig;
7290 dw_die_ref copy;
7291 };
7292
7293 /* Helpers to manipulate hash table of copied declarations. */
7294
7295 /* Hashtable helpers. */
7296
7297 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7298 {
7299 typedef die_struct *compare_type;
7300 static inline hashval_t hash (const decl_table_entry *);
7301 static inline bool equal (const decl_table_entry *, const die_struct *);
7302 };
7303
7304 inline hashval_t
7305 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7306 {
7307 return htab_hash_pointer (entry->orig);
7308 }
7309
7310 inline bool
7311 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7312 const die_struct *entry2)
7313 {
7314 return entry1->orig == entry2;
7315 }
7316
7317 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7318
7319 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7320 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7321 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7322 to check if the ancestor has already been copied into UNIT. */
7323
7324 static dw_die_ref
7325 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7326 decl_hash_type *decl_table)
7327 {
7328 dw_die_ref parent = die->die_parent;
7329 dw_die_ref new_parent = unit;
7330 dw_die_ref copy;
7331 decl_table_entry **slot = NULL;
7332 struct decl_table_entry *entry = NULL;
7333
7334 if (decl_table)
7335 {
7336 /* Check if the entry has already been copied to UNIT. */
7337 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7338 INSERT);
7339 if (*slot != HTAB_EMPTY_ENTRY)
7340 {
7341 entry = *slot;
7342 return entry->copy;
7343 }
7344
7345 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7346 entry = XCNEW (struct decl_table_entry);
7347 entry->orig = die;
7348 entry->copy = NULL;
7349 *slot = entry;
7350 }
7351
7352 if (parent != NULL)
7353 {
7354 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7355 if (spec != NULL)
7356 parent = spec;
7357 if (!is_unit_die (parent))
7358 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7359 }
7360
7361 copy = clone_as_declaration (die);
7362 add_child_die (new_parent, copy);
7363
7364 if (decl_table)
7365 {
7366 /* Record the pointer to the copy. */
7367 entry->copy = copy;
7368 }
7369
7370 return copy;
7371 }
7372 /* Copy the declaration context to the new type unit DIE. This includes
7373 any surrounding namespace or type declarations. If the DIE has an
7374 AT_specification attribute, it also includes attributes and children
7375 attached to the specification, and returns a pointer to the original
7376 parent of the declaration DIE. Returns NULL otherwise. */
7377
7378 static dw_die_ref
7379 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7380 {
7381 dw_die_ref decl;
7382 dw_die_ref new_decl;
7383 dw_die_ref orig_parent = NULL;
7384
7385 decl = get_AT_ref (die, DW_AT_specification);
7386 if (decl == NULL)
7387 decl = die;
7388 else
7389 {
7390 unsigned ix;
7391 dw_die_ref c;
7392 dw_attr_ref a;
7393
7394 /* The original DIE will be changed to a declaration, and must
7395 be moved to be a child of the original declaration DIE. */
7396 orig_parent = decl->die_parent;
7397
7398 /* Copy the type node pointer from the new DIE to the original
7399 declaration DIE so we can forward references later. */
7400 decl->comdat_type_p = true;
7401 decl->die_id.die_type_node = die->die_id.die_type_node;
7402
7403 remove_AT (die, DW_AT_specification);
7404
7405 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7406 {
7407 if (a->dw_attr != DW_AT_name
7408 && a->dw_attr != DW_AT_declaration
7409 && a->dw_attr != DW_AT_external)
7410 add_dwarf_attr (die, a);
7411 }
7412
7413 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7414 }
7415
7416 if (decl->die_parent != NULL
7417 && !is_unit_die (decl->die_parent))
7418 {
7419 new_decl = copy_ancestor_tree (unit, decl, NULL);
7420 if (new_decl != NULL)
7421 {
7422 remove_AT (new_decl, DW_AT_signature);
7423 add_AT_specification (die, new_decl);
7424 }
7425 }
7426
7427 return orig_parent;
7428 }
7429
7430 /* Generate the skeleton ancestor tree for the given NODE, then clone
7431 the DIE and add the clone into the tree. */
7432
7433 static void
7434 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7435 {
7436 if (node->new_die != NULL)
7437 return;
7438
7439 node->new_die = clone_as_declaration (node->old_die);
7440
7441 if (node->parent != NULL)
7442 {
7443 generate_skeleton_ancestor_tree (node->parent);
7444 add_child_die (node->parent->new_die, node->new_die);
7445 }
7446 }
7447
7448 /* Generate a skeleton tree of DIEs containing any declarations that are
7449 found in the original tree. We traverse the tree looking for declaration
7450 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7451
7452 static void
7453 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7454 {
7455 skeleton_chain_node node;
7456 dw_die_ref c;
7457 dw_die_ref first;
7458 dw_die_ref prev = NULL;
7459 dw_die_ref next = NULL;
7460
7461 node.parent = parent;
7462
7463 first = c = parent->old_die->die_child;
7464 if (c)
7465 next = c->die_sib;
7466 if (c) do {
7467 if (prev == NULL || prev->die_sib == c)
7468 prev = c;
7469 c = next;
7470 next = (c == first ? NULL : c->die_sib);
7471 node.old_die = c;
7472 node.new_die = NULL;
7473 if (is_declaration_die (c))
7474 {
7475 if (is_template_instantiation (c))
7476 {
7477 /* Instantiated templates do not need to be cloned into the
7478 type unit. Just move the DIE and its children back to
7479 the skeleton tree (in the main CU). */
7480 remove_child_with_prev (c, prev);
7481 add_child_die (parent->new_die, c);
7482 c = prev;
7483 }
7484 else
7485 {
7486 /* Clone the existing DIE, move the original to the skeleton
7487 tree (which is in the main CU), and put the clone, with
7488 all the original's children, where the original came from
7489 (which is about to be moved to the type unit). */
7490 dw_die_ref clone = clone_die (c);
7491 move_all_children (c, clone);
7492
7493 /* If the original has a DW_AT_object_pointer attribute,
7494 it would now point to a child DIE just moved to the
7495 cloned tree, so we need to remove that attribute from
7496 the original. */
7497 remove_AT (c, DW_AT_object_pointer);
7498
7499 replace_child (c, clone, prev);
7500 generate_skeleton_ancestor_tree (parent);
7501 add_child_die (parent->new_die, c);
7502 node.new_die = c;
7503 c = clone;
7504 }
7505 }
7506 generate_skeleton_bottom_up (&node);
7507 } while (next != NULL);
7508 }
7509
7510 /* Wrapper function for generate_skeleton_bottom_up. */
7511
7512 static dw_die_ref
7513 generate_skeleton (dw_die_ref die)
7514 {
7515 skeleton_chain_node node;
7516
7517 node.old_die = die;
7518 node.new_die = NULL;
7519 node.parent = NULL;
7520
7521 /* If this type definition is nested inside another type,
7522 and is not an instantiation of a template, always leave
7523 at least a declaration in its place. */
7524 if (die->die_parent != NULL
7525 && is_type_die (die->die_parent)
7526 && !is_template_instantiation (die))
7527 node.new_die = clone_as_declaration (die);
7528
7529 generate_skeleton_bottom_up (&node);
7530 return node.new_die;
7531 }
7532
7533 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7534 declaration. The original DIE is moved to a new compile unit so that
7535 existing references to it follow it to the new location. If any of the
7536 original DIE's descendants is a declaration, we need to replace the
7537 original DIE with a skeleton tree and move the declarations back into the
7538 skeleton tree. */
7539
7540 static dw_die_ref
7541 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7542 dw_die_ref prev)
7543 {
7544 dw_die_ref skeleton, orig_parent;
7545
7546 /* Copy the declaration context to the type unit DIE. If the returned
7547 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7548 that DIE. */
7549 orig_parent = copy_declaration_context (unit, child);
7550
7551 skeleton = generate_skeleton (child);
7552 if (skeleton == NULL)
7553 remove_child_with_prev (child, prev);
7554 else
7555 {
7556 skeleton->comdat_type_p = true;
7557 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7558
7559 /* If the original DIE was a specification, we need to put
7560 the skeleton under the parent DIE of the declaration.
7561 This leaves the original declaration in the tree, but
7562 it will be pruned later since there are no longer any
7563 references to it. */
7564 if (orig_parent != NULL)
7565 {
7566 remove_child_with_prev (child, prev);
7567 add_child_die (orig_parent, skeleton);
7568 }
7569 else
7570 replace_child (child, skeleton, prev);
7571 }
7572
7573 return skeleton;
7574 }
7575
7576 /* Traverse the DIE and set up additional .debug_types sections for each
7577 type worthy of being placed in a COMDAT section. */
7578
7579 static void
7580 break_out_comdat_types (dw_die_ref die)
7581 {
7582 dw_die_ref c;
7583 dw_die_ref first;
7584 dw_die_ref prev = NULL;
7585 dw_die_ref next = NULL;
7586 dw_die_ref unit = NULL;
7587
7588 first = c = die->die_child;
7589 if (c)
7590 next = c->die_sib;
7591 if (c) do {
7592 if (prev == NULL || prev->die_sib == c)
7593 prev = c;
7594 c = next;
7595 next = (c == first ? NULL : c->die_sib);
7596 if (should_move_die_to_comdat (c))
7597 {
7598 dw_die_ref replacement;
7599 comdat_type_node_ref type_node;
7600
7601 /* Break out nested types into their own type units. */
7602 break_out_comdat_types (c);
7603
7604 /* Create a new type unit DIE as the root for the new tree, and
7605 add it to the list of comdat types. */
7606 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7607 add_AT_unsigned (unit, DW_AT_language,
7608 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7609 type_node = ggc_cleared_alloc<comdat_type_node> ();
7610 type_node->root_die = unit;
7611 type_node->next = comdat_type_list;
7612 comdat_type_list = type_node;
7613
7614 /* Generate the type signature. */
7615 generate_type_signature (c, type_node);
7616
7617 /* Copy the declaration context, attributes, and children of the
7618 declaration into the new type unit DIE, then remove this DIE
7619 from the main CU (or replace it with a skeleton if necessary). */
7620 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7621 type_node->skeleton_die = replacement;
7622
7623 /* Add the DIE to the new compunit. */
7624 add_child_die (unit, c);
7625
7626 if (replacement != NULL)
7627 c = replacement;
7628 }
7629 else if (c->die_tag == DW_TAG_namespace
7630 || c->die_tag == DW_TAG_class_type
7631 || c->die_tag == DW_TAG_structure_type
7632 || c->die_tag == DW_TAG_union_type)
7633 {
7634 /* Look for nested types that can be broken out. */
7635 break_out_comdat_types (c);
7636 }
7637 } while (next != NULL);
7638 }
7639
7640 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7641 Enter all the cloned children into the hash table decl_table. */
7642
7643 static dw_die_ref
7644 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7645 {
7646 dw_die_ref c;
7647 dw_die_ref clone;
7648 struct decl_table_entry *entry;
7649 decl_table_entry **slot;
7650
7651 if (die->die_tag == DW_TAG_subprogram)
7652 clone = clone_as_declaration (die);
7653 else
7654 clone = clone_die (die);
7655
7656 slot = decl_table->find_slot_with_hash (die,
7657 htab_hash_pointer (die), INSERT);
7658
7659 /* Assert that DIE isn't in the hash table yet. If it would be there
7660 before, the ancestors would be necessarily there as well, therefore
7661 clone_tree_partial wouldn't be called. */
7662 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7663
7664 entry = XCNEW (struct decl_table_entry);
7665 entry->orig = die;
7666 entry->copy = clone;
7667 *slot = entry;
7668
7669 if (die->die_tag != DW_TAG_subprogram)
7670 FOR_EACH_CHILD (die, c,
7671 add_child_die (clone, clone_tree_partial (c, decl_table)));
7672
7673 return clone;
7674 }
7675
7676 /* Walk the DIE and its children, looking for references to incomplete
7677 or trivial types that are unmarked (i.e., that are not in the current
7678 type_unit). */
7679
7680 static void
7681 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7682 {
7683 dw_die_ref c;
7684 dw_attr_ref a;
7685 unsigned ix;
7686
7687 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7688 {
7689 if (AT_class (a) == dw_val_class_die_ref)
7690 {
7691 dw_die_ref targ = AT_ref (a);
7692 decl_table_entry **slot;
7693 struct decl_table_entry *entry;
7694
7695 if (targ->die_mark != 0 || targ->comdat_type_p)
7696 continue;
7697
7698 slot = decl_table->find_slot_with_hash (targ,
7699 htab_hash_pointer (targ),
7700 INSERT);
7701
7702 if (*slot != HTAB_EMPTY_ENTRY)
7703 {
7704 /* TARG has already been copied, so we just need to
7705 modify the reference to point to the copy. */
7706 entry = *slot;
7707 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7708 }
7709 else
7710 {
7711 dw_die_ref parent = unit;
7712 dw_die_ref copy = clone_die (targ);
7713
7714 /* Record in DECL_TABLE that TARG has been copied.
7715 Need to do this now, before the recursive call,
7716 because DECL_TABLE may be expanded and SLOT
7717 would no longer be a valid pointer. */
7718 entry = XCNEW (struct decl_table_entry);
7719 entry->orig = targ;
7720 entry->copy = copy;
7721 *slot = entry;
7722
7723 /* If TARG is not a declaration DIE, we need to copy its
7724 children. */
7725 if (!is_declaration_die (targ))
7726 {
7727 FOR_EACH_CHILD (
7728 targ, c,
7729 add_child_die (copy,
7730 clone_tree_partial (c, decl_table)));
7731 }
7732
7733 /* Make sure the cloned tree is marked as part of the
7734 type unit. */
7735 mark_dies (copy);
7736
7737 /* If TARG has surrounding context, copy its ancestor tree
7738 into the new type unit. */
7739 if (targ->die_parent != NULL
7740 && !is_unit_die (targ->die_parent))
7741 parent = copy_ancestor_tree (unit, targ->die_parent,
7742 decl_table);
7743
7744 add_child_die (parent, copy);
7745 a->dw_attr_val.v.val_die_ref.die = copy;
7746
7747 /* Make sure the newly-copied DIE is walked. If it was
7748 installed in a previously-added context, it won't
7749 get visited otherwise. */
7750 if (parent != unit)
7751 {
7752 /* Find the highest point of the newly-added tree,
7753 mark each node along the way, and walk from there. */
7754 parent->die_mark = 1;
7755 while (parent->die_parent
7756 && parent->die_parent->die_mark == 0)
7757 {
7758 parent = parent->die_parent;
7759 parent->die_mark = 1;
7760 }
7761 copy_decls_walk (unit, parent, decl_table);
7762 }
7763 }
7764 }
7765 }
7766
7767 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7768 }
7769
7770 /* Copy declarations for "unworthy" types into the new comdat section.
7771 Incomplete types, modified types, and certain other types aren't broken
7772 out into comdat sections of their own, so they don't have a signature,
7773 and we need to copy the declaration into the same section so that we
7774 don't have an external reference. */
7775
7776 static void
7777 copy_decls_for_unworthy_types (dw_die_ref unit)
7778 {
7779 mark_dies (unit);
7780 decl_hash_type decl_table (10);
7781 copy_decls_walk (unit, unit, &decl_table);
7782 unmark_dies (unit);
7783 }
7784
7785 /* Traverse the DIE and add a sibling attribute if it may have the
7786 effect of speeding up access to siblings. To save some space,
7787 avoid generating sibling attributes for DIE's without children. */
7788
7789 static void
7790 add_sibling_attributes (dw_die_ref die)
7791 {
7792 dw_die_ref c;
7793
7794 if (! die->die_child)
7795 return;
7796
7797 if (die->die_parent && die != die->die_parent->die_child)
7798 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7799
7800 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7801 }
7802
7803 /* Output all location lists for the DIE and its children. */
7804
7805 static void
7806 output_location_lists (dw_die_ref die)
7807 {
7808 dw_die_ref c;
7809 dw_attr_ref a;
7810 unsigned ix;
7811
7812 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7813 if (AT_class (a) == dw_val_class_loc_list)
7814 output_loc_list (AT_loc_list (a));
7815
7816 FOR_EACH_CHILD (die, c, output_location_lists (c));
7817 }
7818
7819 /* We want to limit the number of external references, because they are
7820 larger than local references: a relocation takes multiple words, and
7821 even a sig8 reference is always eight bytes, whereas a local reference
7822 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7823 So if we encounter multiple external references to the same type DIE, we
7824 make a local typedef stub for it and redirect all references there.
7825
7826 This is the element of the hash table for keeping track of these
7827 references. */
7828
7829 struct external_ref
7830 {
7831 dw_die_ref type;
7832 dw_die_ref stub;
7833 unsigned n_refs;
7834 };
7835
7836 /* Hashtable helpers. */
7837
7838 struct external_ref_hasher : free_ptr_hash <external_ref>
7839 {
7840 static inline hashval_t hash (const external_ref *);
7841 static inline bool equal (const external_ref *, const external_ref *);
7842 };
7843
7844 inline hashval_t
7845 external_ref_hasher::hash (const external_ref *r)
7846 {
7847 dw_die_ref die = r->type;
7848 hashval_t h = 0;
7849
7850 /* We can't use the address of the DIE for hashing, because
7851 that will make the order of the stub DIEs non-deterministic. */
7852 if (! die->comdat_type_p)
7853 /* We have a symbol; use it to compute a hash. */
7854 h = htab_hash_string (die->die_id.die_symbol);
7855 else
7856 {
7857 /* We have a type signature; use a subset of the bits as the hash.
7858 The 8-byte signature is at least as large as hashval_t. */
7859 comdat_type_node_ref type_node = die->die_id.die_type_node;
7860 memcpy (&h, type_node->signature, sizeof (h));
7861 }
7862 return h;
7863 }
7864
7865 inline bool
7866 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7867 {
7868 return r1->type == r2->type;
7869 }
7870
7871 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7872
7873 /* Return a pointer to the external_ref for references to DIE. */
7874
7875 static struct external_ref *
7876 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7877 {
7878 struct external_ref ref, *ref_p;
7879 external_ref **slot;
7880
7881 ref.type = die;
7882 slot = map->find_slot (&ref, INSERT);
7883 if (*slot != HTAB_EMPTY_ENTRY)
7884 return *slot;
7885
7886 ref_p = XCNEW (struct external_ref);
7887 ref_p->type = die;
7888 *slot = ref_p;
7889 return ref_p;
7890 }
7891
7892 /* Subroutine of optimize_external_refs, below.
7893
7894 If we see a type skeleton, record it as our stub. If we see external
7895 references, remember how many we've seen. */
7896
7897 static void
7898 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7899 {
7900 dw_die_ref c;
7901 dw_attr_ref a;
7902 unsigned ix;
7903 struct external_ref *ref_p;
7904
7905 if (is_type_die (die)
7906 && (c = get_AT_ref (die, DW_AT_signature)))
7907 {
7908 /* This is a local skeleton; use it for local references. */
7909 ref_p = lookup_external_ref (map, c);
7910 ref_p->stub = die;
7911 }
7912
7913 /* Scan the DIE references, and remember any that refer to DIEs from
7914 other CUs (i.e. those which are not marked). */
7915 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7916 if (AT_class (a) == dw_val_class_die_ref
7917 && (c = AT_ref (a))->die_mark == 0
7918 && is_type_die (c))
7919 {
7920 ref_p = lookup_external_ref (map, c);
7921 ref_p->n_refs++;
7922 }
7923
7924 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7925 }
7926
7927 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7928 points to an external_ref, DATA is the CU we're processing. If we don't
7929 already have a local stub, and we have multiple refs, build a stub. */
7930
7931 int
7932 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7933 {
7934 struct external_ref *ref_p = *slot;
7935
7936 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7937 {
7938 /* We have multiple references to this type, so build a small stub.
7939 Both of these forms are a bit dodgy from the perspective of the
7940 DWARF standard, since technically they should have names. */
7941 dw_die_ref cu = data;
7942 dw_die_ref type = ref_p->type;
7943 dw_die_ref stub = NULL;
7944
7945 if (type->comdat_type_p)
7946 {
7947 /* If we refer to this type via sig8, use AT_signature. */
7948 stub = new_die (type->die_tag, cu, NULL_TREE);
7949 add_AT_die_ref (stub, DW_AT_signature, type);
7950 }
7951 else
7952 {
7953 /* Otherwise, use a typedef with no name. */
7954 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7955 add_AT_die_ref (stub, DW_AT_type, type);
7956 }
7957
7958 stub->die_mark++;
7959 ref_p->stub = stub;
7960 }
7961 return 1;
7962 }
7963
7964 /* DIE is a unit; look through all the DIE references to see if there are
7965 any external references to types, and if so, create local stubs for
7966 them which will be applied in build_abbrev_table. This is useful because
7967 references to local DIEs are smaller. */
7968
7969 static external_ref_hash_type *
7970 optimize_external_refs (dw_die_ref die)
7971 {
7972 external_ref_hash_type *map = new external_ref_hash_type (10);
7973 optimize_external_refs_1 (die, map);
7974 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7975 return map;
7976 }
7977
7978 /* The format of each DIE (and its attribute value pairs) is encoded in an
7979 abbreviation table. This routine builds the abbreviation table and assigns
7980 a unique abbreviation id for each abbreviation entry. The children of each
7981 die are visited recursively. */
7982
7983 static void
7984 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7985 {
7986 unsigned long abbrev_id;
7987 unsigned int n_alloc;
7988 dw_die_ref c;
7989 dw_attr_ref a;
7990 unsigned ix;
7991
7992 /* Scan the DIE references, and replace any that refer to
7993 DIEs from other CUs (i.e. those which are not marked) with
7994 the local stubs we built in optimize_external_refs. */
7995 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7996 if (AT_class (a) == dw_val_class_die_ref
7997 && (c = AT_ref (a))->die_mark == 0)
7998 {
7999 struct external_ref *ref_p;
8000 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8001
8002 ref_p = lookup_external_ref (extern_map, c);
8003 if (ref_p->stub && ref_p->stub != die)
8004 change_AT_die_ref (a, ref_p->stub);
8005 else
8006 /* We aren't changing this reference, so mark it external. */
8007 set_AT_ref_external (a, 1);
8008 }
8009
8010 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8011 {
8012 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8013 dw_attr_ref die_a, abbrev_a;
8014 unsigned ix;
8015 bool ok = true;
8016
8017 if (abbrev->die_tag != die->die_tag)
8018 continue;
8019 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8020 continue;
8021
8022 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8023 continue;
8024
8025 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8026 {
8027 abbrev_a = &(*abbrev->die_attr)[ix];
8028 if ((abbrev_a->dw_attr != die_a->dw_attr)
8029 || (value_format (abbrev_a) != value_format (die_a)))
8030 {
8031 ok = false;
8032 break;
8033 }
8034 }
8035 if (ok)
8036 break;
8037 }
8038
8039 if (abbrev_id >= abbrev_die_table_in_use)
8040 {
8041 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8042 {
8043 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8044 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8045 n_alloc);
8046
8047 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8048 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8049 abbrev_die_table_allocated = n_alloc;
8050 }
8051
8052 ++abbrev_die_table_in_use;
8053 abbrev_die_table[abbrev_id] = die;
8054 }
8055
8056 die->die_abbrev = abbrev_id;
8057 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8058 }
8059 \f
8060 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8061
8062 static int
8063 constant_size (unsigned HOST_WIDE_INT value)
8064 {
8065 int log;
8066
8067 if (value == 0)
8068 log = 0;
8069 else
8070 log = floor_log2 (value);
8071
8072 log = log / 8;
8073 log = 1 << (floor_log2 (log) + 1);
8074
8075 return log;
8076 }
8077
8078 /* Return the size of a DIE as it is represented in the
8079 .debug_info section. */
8080
8081 static unsigned long
8082 size_of_die (dw_die_ref die)
8083 {
8084 unsigned long size = 0;
8085 dw_attr_ref a;
8086 unsigned ix;
8087 enum dwarf_form form;
8088
8089 size += size_of_uleb128 (die->die_abbrev);
8090 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8091 {
8092 switch (AT_class (a))
8093 {
8094 case dw_val_class_addr:
8095 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8096 {
8097 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8098 size += size_of_uleb128 (AT_index (a));
8099 }
8100 else
8101 size += DWARF2_ADDR_SIZE;
8102 break;
8103 case dw_val_class_offset:
8104 size += DWARF_OFFSET_SIZE;
8105 break;
8106 case dw_val_class_loc:
8107 {
8108 unsigned long lsize = size_of_locs (AT_loc (a));
8109
8110 /* Block length. */
8111 if (dwarf_version >= 4)
8112 size += size_of_uleb128 (lsize);
8113 else
8114 size += constant_size (lsize);
8115 size += lsize;
8116 }
8117 break;
8118 case dw_val_class_loc_list:
8119 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8120 {
8121 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8122 size += size_of_uleb128 (AT_index (a));
8123 }
8124 else
8125 size += DWARF_OFFSET_SIZE;
8126 break;
8127 case dw_val_class_range_list:
8128 size += DWARF_OFFSET_SIZE;
8129 break;
8130 case dw_val_class_const:
8131 size += size_of_sleb128 (AT_int (a));
8132 break;
8133 case dw_val_class_unsigned_const:
8134 {
8135 int csize = constant_size (AT_unsigned (a));
8136 if (dwarf_version == 3
8137 && a->dw_attr == DW_AT_data_member_location
8138 && csize >= 4)
8139 size += size_of_uleb128 (AT_unsigned (a));
8140 else
8141 size += csize;
8142 }
8143 break;
8144 case dw_val_class_const_double:
8145 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8146 if (HOST_BITS_PER_WIDE_INT >= 64)
8147 size++; /* block */
8148 break;
8149 case dw_val_class_wide_int:
8150 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8151 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8152 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8153 > 64)
8154 size++; /* block */
8155 break;
8156 case dw_val_class_vec:
8157 size += constant_size (a->dw_attr_val.v.val_vec.length
8158 * a->dw_attr_val.v.val_vec.elt_size)
8159 + a->dw_attr_val.v.val_vec.length
8160 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8161 break;
8162 case dw_val_class_flag:
8163 if (dwarf_version >= 4)
8164 /* Currently all add_AT_flag calls pass in 1 as last argument,
8165 so DW_FORM_flag_present can be used. If that ever changes,
8166 we'll need to use DW_FORM_flag and have some optimization
8167 in build_abbrev_table that will change those to
8168 DW_FORM_flag_present if it is set to 1 in all DIEs using
8169 the same abbrev entry. */
8170 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8171 else
8172 size += 1;
8173 break;
8174 case dw_val_class_die_ref:
8175 if (AT_ref_external (a))
8176 {
8177 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8178 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8179 is sized by target address length, whereas in DWARF3
8180 it's always sized as an offset. */
8181 if (use_debug_types)
8182 size += DWARF_TYPE_SIGNATURE_SIZE;
8183 else if (dwarf_version == 2)
8184 size += DWARF2_ADDR_SIZE;
8185 else
8186 size += DWARF_OFFSET_SIZE;
8187 }
8188 else
8189 size += DWARF_OFFSET_SIZE;
8190 break;
8191 case dw_val_class_fde_ref:
8192 size += DWARF_OFFSET_SIZE;
8193 break;
8194 case dw_val_class_lbl_id:
8195 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8196 {
8197 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8198 size += size_of_uleb128 (AT_index (a));
8199 }
8200 else
8201 size += DWARF2_ADDR_SIZE;
8202 break;
8203 case dw_val_class_lineptr:
8204 case dw_val_class_macptr:
8205 size += DWARF_OFFSET_SIZE;
8206 break;
8207 case dw_val_class_str:
8208 form = AT_string_form (a);
8209 if (form == DW_FORM_strp)
8210 size += DWARF_OFFSET_SIZE;
8211 else if (form == DW_FORM_GNU_str_index)
8212 size += size_of_uleb128 (AT_index (a));
8213 else
8214 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8215 break;
8216 case dw_val_class_file:
8217 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8218 break;
8219 case dw_val_class_data8:
8220 size += 8;
8221 break;
8222 case dw_val_class_vms_delta:
8223 size += DWARF_OFFSET_SIZE;
8224 break;
8225 case dw_val_class_high_pc:
8226 size += DWARF2_ADDR_SIZE;
8227 break;
8228 default:
8229 gcc_unreachable ();
8230 }
8231 }
8232
8233 return size;
8234 }
8235
8236 /* Size the debugging information associated with a given DIE. Visits the
8237 DIE's children recursively. Updates the global variable next_die_offset, on
8238 each time through. Uses the current value of next_die_offset to update the
8239 die_offset field in each DIE. */
8240
8241 static void
8242 calc_die_sizes (dw_die_ref die)
8243 {
8244 dw_die_ref c;
8245
8246 gcc_assert (die->die_offset == 0
8247 || (unsigned long int) die->die_offset == next_die_offset);
8248 die->die_offset = next_die_offset;
8249 next_die_offset += size_of_die (die);
8250
8251 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8252
8253 if (die->die_child != NULL)
8254 /* Count the null byte used to terminate sibling lists. */
8255 next_die_offset += 1;
8256 }
8257
8258 /* Size just the base type children at the start of the CU.
8259 This is needed because build_abbrev needs to size locs
8260 and sizing of type based stack ops needs to know die_offset
8261 values for the base types. */
8262
8263 static void
8264 calc_base_type_die_sizes (void)
8265 {
8266 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8267 unsigned int i;
8268 dw_die_ref base_type;
8269 #if ENABLE_ASSERT_CHECKING
8270 dw_die_ref prev = comp_unit_die ()->die_child;
8271 #endif
8272
8273 die_offset += size_of_die (comp_unit_die ());
8274 for (i = 0; base_types.iterate (i, &base_type); i++)
8275 {
8276 #if ENABLE_ASSERT_CHECKING
8277 gcc_assert (base_type->die_offset == 0
8278 && prev->die_sib == base_type
8279 && base_type->die_child == NULL
8280 && base_type->die_abbrev);
8281 prev = base_type;
8282 #endif
8283 base_type->die_offset = die_offset;
8284 die_offset += size_of_die (base_type);
8285 }
8286 }
8287
8288 /* Set the marks for a die and its children. We do this so
8289 that we know whether or not a reference needs to use FORM_ref_addr; only
8290 DIEs in the same CU will be marked. We used to clear out the offset
8291 and use that as the flag, but ran into ordering problems. */
8292
8293 static void
8294 mark_dies (dw_die_ref die)
8295 {
8296 dw_die_ref c;
8297
8298 gcc_assert (!die->die_mark);
8299
8300 die->die_mark = 1;
8301 FOR_EACH_CHILD (die, c, mark_dies (c));
8302 }
8303
8304 /* Clear the marks for a die and its children. */
8305
8306 static void
8307 unmark_dies (dw_die_ref die)
8308 {
8309 dw_die_ref c;
8310
8311 if (! use_debug_types)
8312 gcc_assert (die->die_mark);
8313
8314 die->die_mark = 0;
8315 FOR_EACH_CHILD (die, c, unmark_dies (c));
8316 }
8317
8318 /* Clear the marks for a die, its children and referred dies. */
8319
8320 static void
8321 unmark_all_dies (dw_die_ref die)
8322 {
8323 dw_die_ref c;
8324 dw_attr_ref a;
8325 unsigned ix;
8326
8327 if (!die->die_mark)
8328 return;
8329 die->die_mark = 0;
8330
8331 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8332
8333 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8334 if (AT_class (a) == dw_val_class_die_ref)
8335 unmark_all_dies (AT_ref (a));
8336 }
8337
8338 /* Calculate if the entry should appear in the final output file. It may be
8339 from a pruned a type. */
8340
8341 static bool
8342 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8343 {
8344 /* By limiting gnu pubnames to definitions only, gold can generate a
8345 gdb index without entries for declarations, which don't include
8346 enough information to be useful. */
8347 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8348 return false;
8349
8350 if (table == pubname_table)
8351 {
8352 /* Enumerator names are part of the pubname table, but the
8353 parent DW_TAG_enumeration_type die may have been pruned.
8354 Don't output them if that is the case. */
8355 if (p->die->die_tag == DW_TAG_enumerator &&
8356 (p->die->die_parent == NULL
8357 || !p->die->die_parent->die_perennial_p))
8358 return false;
8359
8360 /* Everything else in the pubname table is included. */
8361 return true;
8362 }
8363
8364 /* The pubtypes table shouldn't include types that have been
8365 pruned. */
8366 return (p->die->die_offset != 0
8367 || !flag_eliminate_unused_debug_types);
8368 }
8369
8370 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8371 generated for the compilation unit. */
8372
8373 static unsigned long
8374 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8375 {
8376 unsigned long size;
8377 unsigned i;
8378 pubname_ref p;
8379 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8380
8381 size = DWARF_PUBNAMES_HEADER_SIZE;
8382 FOR_EACH_VEC_ELT (*names, i, p)
8383 if (include_pubname_in_output (names, p))
8384 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8385
8386 size += DWARF_OFFSET_SIZE;
8387 return size;
8388 }
8389
8390 /* Return the size of the information in the .debug_aranges section. */
8391
8392 static unsigned long
8393 size_of_aranges (void)
8394 {
8395 unsigned long size;
8396
8397 size = DWARF_ARANGES_HEADER_SIZE;
8398
8399 /* Count the address/length pair for this compilation unit. */
8400 if (text_section_used)
8401 size += 2 * DWARF2_ADDR_SIZE;
8402 if (cold_text_section_used)
8403 size += 2 * DWARF2_ADDR_SIZE;
8404 if (have_multiple_function_sections)
8405 {
8406 unsigned fde_idx;
8407 dw_fde_ref fde;
8408
8409 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8410 {
8411 if (DECL_IGNORED_P (fde->decl))
8412 continue;
8413 if (!fde->in_std_section)
8414 size += 2 * DWARF2_ADDR_SIZE;
8415 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8416 size += 2 * DWARF2_ADDR_SIZE;
8417 }
8418 }
8419
8420 /* Count the two zero words used to terminated the address range table. */
8421 size += 2 * DWARF2_ADDR_SIZE;
8422 return size;
8423 }
8424 \f
8425 /* Select the encoding of an attribute value. */
8426
8427 static enum dwarf_form
8428 value_format (dw_attr_ref a)
8429 {
8430 switch (AT_class (a))
8431 {
8432 case dw_val_class_addr:
8433 /* Only very few attributes allow DW_FORM_addr. */
8434 switch (a->dw_attr)
8435 {
8436 case DW_AT_low_pc:
8437 case DW_AT_high_pc:
8438 case DW_AT_entry_pc:
8439 case DW_AT_trampoline:
8440 return (AT_index (a) == NOT_INDEXED
8441 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8442 default:
8443 break;
8444 }
8445 switch (DWARF2_ADDR_SIZE)
8446 {
8447 case 1:
8448 return DW_FORM_data1;
8449 case 2:
8450 return DW_FORM_data2;
8451 case 4:
8452 return DW_FORM_data4;
8453 case 8:
8454 return DW_FORM_data8;
8455 default:
8456 gcc_unreachable ();
8457 }
8458 case dw_val_class_range_list:
8459 case dw_val_class_loc_list:
8460 if (dwarf_version >= 4)
8461 return DW_FORM_sec_offset;
8462 /* FALLTHRU */
8463 case dw_val_class_vms_delta:
8464 case dw_val_class_offset:
8465 switch (DWARF_OFFSET_SIZE)
8466 {
8467 case 4:
8468 return DW_FORM_data4;
8469 case 8:
8470 return DW_FORM_data8;
8471 default:
8472 gcc_unreachable ();
8473 }
8474 case dw_val_class_loc:
8475 if (dwarf_version >= 4)
8476 return DW_FORM_exprloc;
8477 switch (constant_size (size_of_locs (AT_loc (a))))
8478 {
8479 case 1:
8480 return DW_FORM_block1;
8481 case 2:
8482 return DW_FORM_block2;
8483 case 4:
8484 return DW_FORM_block4;
8485 default:
8486 gcc_unreachable ();
8487 }
8488 case dw_val_class_const:
8489 return DW_FORM_sdata;
8490 case dw_val_class_unsigned_const:
8491 switch (constant_size (AT_unsigned (a)))
8492 {
8493 case 1:
8494 return DW_FORM_data1;
8495 case 2:
8496 return DW_FORM_data2;
8497 case 4:
8498 /* In DWARF3 DW_AT_data_member_location with
8499 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8500 constant, so we need to use DW_FORM_udata if we need
8501 a large constant. */
8502 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8503 return DW_FORM_udata;
8504 return DW_FORM_data4;
8505 case 8:
8506 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8507 return DW_FORM_udata;
8508 return DW_FORM_data8;
8509 default:
8510 gcc_unreachable ();
8511 }
8512 case dw_val_class_const_double:
8513 switch (HOST_BITS_PER_WIDE_INT)
8514 {
8515 case 8:
8516 return DW_FORM_data2;
8517 case 16:
8518 return DW_FORM_data4;
8519 case 32:
8520 return DW_FORM_data8;
8521 case 64:
8522 default:
8523 return DW_FORM_block1;
8524 }
8525 case dw_val_class_wide_int:
8526 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8527 {
8528 case 8:
8529 return DW_FORM_data1;
8530 case 16:
8531 return DW_FORM_data2;
8532 case 32:
8533 return DW_FORM_data4;
8534 case 64:
8535 return DW_FORM_data8;
8536 default:
8537 return DW_FORM_block1;
8538 }
8539 case dw_val_class_vec:
8540 switch (constant_size (a->dw_attr_val.v.val_vec.length
8541 * a->dw_attr_val.v.val_vec.elt_size))
8542 {
8543 case 1:
8544 return DW_FORM_block1;
8545 case 2:
8546 return DW_FORM_block2;
8547 case 4:
8548 return DW_FORM_block4;
8549 default:
8550 gcc_unreachable ();
8551 }
8552 case dw_val_class_flag:
8553 if (dwarf_version >= 4)
8554 {
8555 /* Currently all add_AT_flag calls pass in 1 as last argument,
8556 so DW_FORM_flag_present can be used. If that ever changes,
8557 we'll need to use DW_FORM_flag and have some optimization
8558 in build_abbrev_table that will change those to
8559 DW_FORM_flag_present if it is set to 1 in all DIEs using
8560 the same abbrev entry. */
8561 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8562 return DW_FORM_flag_present;
8563 }
8564 return DW_FORM_flag;
8565 case dw_val_class_die_ref:
8566 if (AT_ref_external (a))
8567 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8568 else
8569 return DW_FORM_ref;
8570 case dw_val_class_fde_ref:
8571 return DW_FORM_data;
8572 case dw_val_class_lbl_id:
8573 return (AT_index (a) == NOT_INDEXED
8574 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8575 case dw_val_class_lineptr:
8576 case dw_val_class_macptr:
8577 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8578 case dw_val_class_str:
8579 return AT_string_form (a);
8580 case dw_val_class_file:
8581 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8582 {
8583 case 1:
8584 return DW_FORM_data1;
8585 case 2:
8586 return DW_FORM_data2;
8587 case 4:
8588 return DW_FORM_data4;
8589 default:
8590 gcc_unreachable ();
8591 }
8592
8593 case dw_val_class_data8:
8594 return DW_FORM_data8;
8595
8596 case dw_val_class_high_pc:
8597 switch (DWARF2_ADDR_SIZE)
8598 {
8599 case 1:
8600 return DW_FORM_data1;
8601 case 2:
8602 return DW_FORM_data2;
8603 case 4:
8604 return DW_FORM_data4;
8605 case 8:
8606 return DW_FORM_data8;
8607 default:
8608 gcc_unreachable ();
8609 }
8610
8611 default:
8612 gcc_unreachable ();
8613 }
8614 }
8615
8616 /* Output the encoding of an attribute value. */
8617
8618 static void
8619 output_value_format (dw_attr_ref a)
8620 {
8621 enum dwarf_form form = value_format (a);
8622
8623 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8624 }
8625
8626 /* Given a die and id, produce the appropriate abbreviations. */
8627
8628 static void
8629 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8630 {
8631 unsigned ix;
8632 dw_attr_ref a_attr;
8633
8634 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8635 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8636 dwarf_tag_name (abbrev->die_tag));
8637
8638 if (abbrev->die_child != NULL)
8639 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8640 else
8641 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8642
8643 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8644 {
8645 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8646 dwarf_attr_name (a_attr->dw_attr));
8647 output_value_format (a_attr);
8648 }
8649
8650 dw2_asm_output_data (1, 0, NULL);
8651 dw2_asm_output_data (1, 0, NULL);
8652 }
8653
8654
8655 /* Output the .debug_abbrev section which defines the DIE abbreviation
8656 table. */
8657
8658 static void
8659 output_abbrev_section (void)
8660 {
8661 unsigned long abbrev_id;
8662
8663 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8664 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8665
8666 /* Terminate the table. */
8667 dw2_asm_output_data (1, 0, NULL);
8668 }
8669
8670 /* Output a symbol we can use to refer to this DIE from another CU. */
8671
8672 static inline void
8673 output_die_symbol (dw_die_ref die)
8674 {
8675 const char *sym = die->die_id.die_symbol;
8676
8677 gcc_assert (!die->comdat_type_p);
8678
8679 if (sym == 0)
8680 return;
8681
8682 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8683 /* We make these global, not weak; if the target doesn't support
8684 .linkonce, it doesn't support combining the sections, so debugging
8685 will break. */
8686 targetm.asm_out.globalize_label (asm_out_file, sym);
8687
8688 ASM_OUTPUT_LABEL (asm_out_file, sym);
8689 }
8690
8691 /* Return a new location list, given the begin and end range, and the
8692 expression. */
8693
8694 static inline dw_loc_list_ref
8695 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8696 const char *section)
8697 {
8698 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8699
8700 retlist->begin = begin;
8701 retlist->begin_entry = NULL;
8702 retlist->end = end;
8703 retlist->expr = expr;
8704 retlist->section = section;
8705
8706 return retlist;
8707 }
8708
8709 /* Generate a new internal symbol for this location list node, if it
8710 hasn't got one yet. */
8711
8712 static inline void
8713 gen_llsym (dw_loc_list_ref list)
8714 {
8715 gcc_assert (!list->ll_symbol);
8716 list->ll_symbol = gen_internal_sym ("LLST");
8717 }
8718
8719 /* Output the location list given to us. */
8720
8721 static void
8722 output_loc_list (dw_loc_list_ref list_head)
8723 {
8724 dw_loc_list_ref curr = list_head;
8725
8726 if (list_head->emitted)
8727 return;
8728 list_head->emitted = true;
8729
8730 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8731
8732 /* Walk the location list, and output each range + expression. */
8733 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8734 {
8735 unsigned long size;
8736 /* Don't output an entry that starts and ends at the same address. */
8737 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8738 continue;
8739 size = size_of_locs (curr->expr);
8740 /* If the expression is too large, drop it on the floor. We could
8741 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8742 in the expression, but >= 64KB expressions for a single value
8743 in a single range are unlikely very useful. */
8744 if (size > 0xffff)
8745 continue;
8746 if (dwarf_split_debug_info)
8747 {
8748 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8749 "Location list start/length entry (%s)",
8750 list_head->ll_symbol);
8751 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8752 "Location list range start index (%s)",
8753 curr->begin);
8754 /* The length field is 4 bytes. If we ever need to support
8755 an 8-byte length, we can add a new DW_LLE code or fall back
8756 to DW_LLE_GNU_start_end_entry. */
8757 dw2_asm_output_delta (4, curr->end, curr->begin,
8758 "Location list range length (%s)",
8759 list_head->ll_symbol);
8760 }
8761 else if (!have_multiple_function_sections)
8762 {
8763 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8764 "Location list begin address (%s)",
8765 list_head->ll_symbol);
8766 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8767 "Location list end address (%s)",
8768 list_head->ll_symbol);
8769 }
8770 else
8771 {
8772 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8773 "Location list begin address (%s)",
8774 list_head->ll_symbol);
8775 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8776 "Location list end address (%s)",
8777 list_head->ll_symbol);
8778 }
8779
8780 /* Output the block length for this list of location operations. */
8781 gcc_assert (size <= 0xffff);
8782 dw2_asm_output_data (2, size, "%s", "Location expression size");
8783
8784 output_loc_sequence (curr->expr, -1);
8785 }
8786
8787 if (dwarf_split_debug_info)
8788 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8789 "Location list terminator (%s)",
8790 list_head->ll_symbol);
8791 else
8792 {
8793 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8794 "Location list terminator begin (%s)",
8795 list_head->ll_symbol);
8796 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8797 "Location list terminator end (%s)",
8798 list_head->ll_symbol);
8799 }
8800 }
8801
8802 /* Output a range_list offset into the debug_range section. Emit a
8803 relocated reference if val_entry is NULL, otherwise, emit an
8804 indirect reference. */
8805
8806 static void
8807 output_range_list_offset (dw_attr_ref a)
8808 {
8809 const char *name = dwarf_attr_name (a->dw_attr);
8810
8811 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8812 {
8813 char *p = strchr (ranges_section_label, '\0');
8814 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8815 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8816 debug_ranges_section, "%s", name);
8817 *p = '\0';
8818 }
8819 else
8820 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8821 "%s (offset from %s)", name, ranges_section_label);
8822 }
8823
8824 /* Output the offset into the debug_loc section. */
8825
8826 static void
8827 output_loc_list_offset (dw_attr_ref a)
8828 {
8829 char *sym = AT_loc_list (a)->ll_symbol;
8830
8831 gcc_assert (sym);
8832 if (dwarf_split_debug_info)
8833 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8834 "%s", dwarf_attr_name (a->dw_attr));
8835 else
8836 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8837 "%s", dwarf_attr_name (a->dw_attr));
8838 }
8839
8840 /* Output an attribute's index or value appropriately. */
8841
8842 static void
8843 output_attr_index_or_value (dw_attr_ref a)
8844 {
8845 const char *name = dwarf_attr_name (a->dw_attr);
8846
8847 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8848 {
8849 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8850 return;
8851 }
8852 switch (AT_class (a))
8853 {
8854 case dw_val_class_addr:
8855 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8856 break;
8857 case dw_val_class_high_pc:
8858 case dw_val_class_lbl_id:
8859 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8860 break;
8861 case dw_val_class_loc_list:
8862 output_loc_list_offset (a);
8863 break;
8864 default:
8865 gcc_unreachable ();
8866 }
8867 }
8868
8869 /* Output a type signature. */
8870
8871 static inline void
8872 output_signature (const char *sig, const char *name)
8873 {
8874 int i;
8875
8876 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8877 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8878 }
8879
8880 /* Output the DIE and its attributes. Called recursively to generate
8881 the definitions of each child DIE. */
8882
8883 static void
8884 output_die (dw_die_ref die)
8885 {
8886 dw_attr_ref a;
8887 dw_die_ref c;
8888 unsigned long size;
8889 unsigned ix;
8890
8891 /* If someone in another CU might refer to us, set up a symbol for
8892 them to point to. */
8893 if (! die->comdat_type_p && die->die_id.die_symbol)
8894 output_die_symbol (die);
8895
8896 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8897 (unsigned long)die->die_offset,
8898 dwarf_tag_name (die->die_tag));
8899
8900 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8901 {
8902 const char *name = dwarf_attr_name (a->dw_attr);
8903
8904 switch (AT_class (a))
8905 {
8906 case dw_val_class_addr:
8907 output_attr_index_or_value (a);
8908 break;
8909
8910 case dw_val_class_offset:
8911 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8912 "%s", name);
8913 break;
8914
8915 case dw_val_class_range_list:
8916 output_range_list_offset (a);
8917 break;
8918
8919 case dw_val_class_loc:
8920 size = size_of_locs (AT_loc (a));
8921
8922 /* Output the block length for this list of location operations. */
8923 if (dwarf_version >= 4)
8924 dw2_asm_output_data_uleb128 (size, "%s", name);
8925 else
8926 dw2_asm_output_data (constant_size (size), size, "%s", name);
8927
8928 output_loc_sequence (AT_loc (a), -1);
8929 break;
8930
8931 case dw_val_class_const:
8932 /* ??? It would be slightly more efficient to use a scheme like is
8933 used for unsigned constants below, but gdb 4.x does not sign
8934 extend. Gdb 5.x does sign extend. */
8935 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8936 break;
8937
8938 case dw_val_class_unsigned_const:
8939 {
8940 int csize = constant_size (AT_unsigned (a));
8941 if (dwarf_version == 3
8942 && a->dw_attr == DW_AT_data_member_location
8943 && csize >= 4)
8944 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8945 else
8946 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8947 }
8948 break;
8949
8950 case dw_val_class_const_double:
8951 {
8952 unsigned HOST_WIDE_INT first, second;
8953
8954 if (HOST_BITS_PER_WIDE_INT >= 64)
8955 dw2_asm_output_data (1,
8956 HOST_BITS_PER_DOUBLE_INT
8957 / HOST_BITS_PER_CHAR,
8958 NULL);
8959
8960 if (WORDS_BIG_ENDIAN)
8961 {
8962 first = a->dw_attr_val.v.val_double.high;
8963 second = a->dw_attr_val.v.val_double.low;
8964 }
8965 else
8966 {
8967 first = a->dw_attr_val.v.val_double.low;
8968 second = a->dw_attr_val.v.val_double.high;
8969 }
8970
8971 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8972 first, "%s", name);
8973 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8974 second, NULL);
8975 }
8976 break;
8977
8978 case dw_val_class_wide_int:
8979 {
8980 int i;
8981 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8982 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8983 if (len * HOST_BITS_PER_WIDE_INT > 64)
8984 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8985 NULL);
8986
8987 if (WORDS_BIG_ENDIAN)
8988 for (i = len - 1; i >= 0; --i)
8989 {
8990 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8991 "%s", name);
8992 name = NULL;
8993 }
8994 else
8995 for (i = 0; i < len; ++i)
8996 {
8997 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8998 "%s", name);
8999 name = NULL;
9000 }
9001 }
9002 break;
9003
9004 case dw_val_class_vec:
9005 {
9006 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9007 unsigned int len = a->dw_attr_val.v.val_vec.length;
9008 unsigned int i;
9009 unsigned char *p;
9010
9011 dw2_asm_output_data (constant_size (len * elt_size),
9012 len * elt_size, "%s", name);
9013 if (elt_size > sizeof (HOST_WIDE_INT))
9014 {
9015 elt_size /= 2;
9016 len *= 2;
9017 }
9018 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9019 i < len;
9020 i++, p += elt_size)
9021 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9022 "fp or vector constant word %u", i);
9023 break;
9024 }
9025
9026 case dw_val_class_flag:
9027 if (dwarf_version >= 4)
9028 {
9029 /* Currently all add_AT_flag calls pass in 1 as last argument,
9030 so DW_FORM_flag_present can be used. If that ever changes,
9031 we'll need to use DW_FORM_flag and have some optimization
9032 in build_abbrev_table that will change those to
9033 DW_FORM_flag_present if it is set to 1 in all DIEs using
9034 the same abbrev entry. */
9035 gcc_assert (AT_flag (a) == 1);
9036 if (flag_debug_asm)
9037 fprintf (asm_out_file, "\t\t\t%s %s\n",
9038 ASM_COMMENT_START, name);
9039 break;
9040 }
9041 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9042 break;
9043
9044 case dw_val_class_loc_list:
9045 output_attr_index_or_value (a);
9046 break;
9047
9048 case dw_val_class_die_ref:
9049 if (AT_ref_external (a))
9050 {
9051 if (AT_ref (a)->comdat_type_p)
9052 {
9053 comdat_type_node_ref type_node =
9054 AT_ref (a)->die_id.die_type_node;
9055
9056 gcc_assert (type_node);
9057 output_signature (type_node->signature, name);
9058 }
9059 else
9060 {
9061 const char *sym = AT_ref (a)->die_id.die_symbol;
9062 int size;
9063
9064 gcc_assert (sym);
9065 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9066 length, whereas in DWARF3 it's always sized as an
9067 offset. */
9068 if (dwarf_version == 2)
9069 size = DWARF2_ADDR_SIZE;
9070 else
9071 size = DWARF_OFFSET_SIZE;
9072 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9073 name);
9074 }
9075 }
9076 else
9077 {
9078 gcc_assert (AT_ref (a)->die_offset);
9079 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9080 "%s", name);
9081 }
9082 break;
9083
9084 case dw_val_class_fde_ref:
9085 {
9086 char l1[20];
9087
9088 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9089 a->dw_attr_val.v.val_fde_index * 2);
9090 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9091 "%s", name);
9092 }
9093 break;
9094
9095 case dw_val_class_vms_delta:
9096 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9097 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9098 AT_vms_delta2 (a), AT_vms_delta1 (a),
9099 "%s", name);
9100 #else
9101 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9102 AT_vms_delta2 (a), AT_vms_delta1 (a),
9103 "%s", name);
9104 #endif
9105 break;
9106
9107 case dw_val_class_lbl_id:
9108 output_attr_index_or_value (a);
9109 break;
9110
9111 case dw_val_class_lineptr:
9112 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9113 debug_line_section, "%s", name);
9114 break;
9115
9116 case dw_val_class_macptr:
9117 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9118 debug_macinfo_section, "%s", name);
9119 break;
9120
9121 case dw_val_class_str:
9122 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9123 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9124 a->dw_attr_val.v.val_str->label,
9125 debug_str_section,
9126 "%s: \"%s\"", name, AT_string (a));
9127 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9128 dw2_asm_output_data_uleb128 (AT_index (a),
9129 "%s: \"%s\"", name, AT_string (a));
9130 else
9131 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9132 break;
9133
9134 case dw_val_class_file:
9135 {
9136 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9137
9138 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9139 a->dw_attr_val.v.val_file->filename);
9140 break;
9141 }
9142
9143 case dw_val_class_data8:
9144 {
9145 int i;
9146
9147 for (i = 0; i < 8; i++)
9148 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9149 i == 0 ? "%s" : NULL, name);
9150 break;
9151 }
9152
9153 case dw_val_class_high_pc:
9154 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9155 get_AT_low_pc (die), "DW_AT_high_pc");
9156 break;
9157
9158 default:
9159 gcc_unreachable ();
9160 }
9161 }
9162
9163 FOR_EACH_CHILD (die, c, output_die (c));
9164
9165 /* Add null byte to terminate sibling list. */
9166 if (die->die_child != NULL)
9167 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9168 (unsigned long) die->die_offset);
9169 }
9170
9171 /* Output the compilation unit that appears at the beginning of the
9172 .debug_info section, and precedes the DIE descriptions. */
9173
9174 static void
9175 output_compilation_unit_header (void)
9176 {
9177 /* We don't support actual DWARFv5 units yet, we just use some
9178 DWARFv5 draft DIE tags in DWARFv4 format. */
9179 int ver = dwarf_version < 5 ? dwarf_version : 4;
9180
9181 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9182 dw2_asm_output_data (4, 0xffffffff,
9183 "Initial length escape value indicating 64-bit DWARF extension");
9184 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9185 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9186 "Length of Compilation Unit Info");
9187 dw2_asm_output_data (2, ver, "DWARF version number");
9188 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9189 debug_abbrev_section,
9190 "Offset Into Abbrev. Section");
9191 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9192 }
9193
9194 /* Output the compilation unit DIE and its children. */
9195
9196 static void
9197 output_comp_unit (dw_die_ref die, int output_if_empty)
9198 {
9199 const char *secname, *oldsym;
9200 char *tmp;
9201
9202 /* Unless we are outputting main CU, we may throw away empty ones. */
9203 if (!output_if_empty && die->die_child == NULL)
9204 return;
9205
9206 /* Even if there are no children of this DIE, we must output the information
9207 about the compilation unit. Otherwise, on an empty translation unit, we
9208 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9209 will then complain when examining the file. First mark all the DIEs in
9210 this CU so we know which get local refs. */
9211 mark_dies (die);
9212
9213 external_ref_hash_type *extern_map = optimize_external_refs (die);
9214
9215 build_abbrev_table (die, extern_map);
9216
9217 delete extern_map;
9218
9219 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9220 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9221 calc_die_sizes (die);
9222
9223 oldsym = die->die_id.die_symbol;
9224 if (oldsym)
9225 {
9226 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9227
9228 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9229 secname = tmp;
9230 die->die_id.die_symbol = NULL;
9231 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9232 }
9233 else
9234 {
9235 switch_to_section (debug_info_section);
9236 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9237 info_section_emitted = true;
9238 }
9239
9240 /* Output debugging information. */
9241 output_compilation_unit_header ();
9242 output_die (die);
9243
9244 /* Leave the marks on the main CU, so we can check them in
9245 output_pubnames. */
9246 if (oldsym)
9247 {
9248 unmark_dies (die);
9249 die->die_id.die_symbol = oldsym;
9250 }
9251 }
9252
9253 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9254 and .debug_pubtypes. This is configured per-target, but can be
9255 overridden by the -gpubnames or -gno-pubnames options. */
9256
9257 static inline bool
9258 want_pubnames (void)
9259 {
9260 if (debug_info_level <= DINFO_LEVEL_TERSE)
9261 return false;
9262 if (debug_generate_pub_sections != -1)
9263 return debug_generate_pub_sections;
9264 return targetm.want_debug_pub_sections;
9265 }
9266
9267 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9268
9269 static void
9270 add_AT_pubnames (dw_die_ref die)
9271 {
9272 if (want_pubnames ())
9273 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9274 }
9275
9276 /* Add a string attribute value to a skeleton DIE. */
9277
9278 static inline void
9279 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9280 const char *str)
9281 {
9282 dw_attr_node attr;
9283 struct indirect_string_node *node;
9284
9285 if (! skeleton_debug_str_hash)
9286 skeleton_debug_str_hash
9287 = hash_table<indirect_string_hasher>::create_ggc (10);
9288
9289 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9290 find_string_form (node);
9291 if (node->form == DW_FORM_GNU_str_index)
9292 node->form = DW_FORM_strp;
9293
9294 attr.dw_attr = attr_kind;
9295 attr.dw_attr_val.val_class = dw_val_class_str;
9296 attr.dw_attr_val.val_entry = NULL;
9297 attr.dw_attr_val.v.val_str = node;
9298 add_dwarf_attr (die, &attr);
9299 }
9300
9301 /* Helper function to generate top-level dies for skeleton debug_info and
9302 debug_types. */
9303
9304 static void
9305 add_top_level_skeleton_die_attrs (dw_die_ref die)
9306 {
9307 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9308 const char *comp_dir = comp_dir_string ();
9309
9310 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9311 if (comp_dir != NULL)
9312 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9313 add_AT_pubnames (die);
9314 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9315 }
9316
9317 /* Output skeleton debug sections that point to the dwo file. */
9318
9319 static void
9320 output_skeleton_debug_sections (dw_die_ref comp_unit)
9321 {
9322 /* We don't support actual DWARFv5 units yet, we just use some
9323 DWARFv5 draft DIE tags in DWARFv4 format. */
9324 int ver = dwarf_version < 5 ? dwarf_version : 4;
9325
9326 /* These attributes will be found in the full debug_info section. */
9327 remove_AT (comp_unit, DW_AT_producer);
9328 remove_AT (comp_unit, DW_AT_language);
9329
9330 switch_to_section (debug_skeleton_info_section);
9331 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9332
9333 /* Produce the skeleton compilation-unit header. This one differs enough from
9334 a normal CU header that it's better not to call output_compilation_unit
9335 header. */
9336 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9337 dw2_asm_output_data (4, 0xffffffff,
9338 "Initial length escape value indicating 64-bit DWARF extension");
9339
9340 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9341 DWARF_COMPILE_UNIT_HEADER_SIZE
9342 - DWARF_INITIAL_LENGTH_SIZE
9343 + size_of_die (comp_unit),
9344 "Length of Compilation Unit Info");
9345 dw2_asm_output_data (2, ver, "DWARF version number");
9346 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9347 debug_abbrev_section,
9348 "Offset Into Abbrev. Section");
9349 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9350
9351 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9352 output_die (comp_unit);
9353
9354 /* Build the skeleton debug_abbrev section. */
9355 switch_to_section (debug_skeleton_abbrev_section);
9356 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9357
9358 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9359
9360 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9361 }
9362
9363 /* Output a comdat type unit DIE and its children. */
9364
9365 static void
9366 output_comdat_type_unit (comdat_type_node *node)
9367 {
9368 const char *secname;
9369 char *tmp;
9370 int i;
9371 #if defined (OBJECT_FORMAT_ELF)
9372 tree comdat_key;
9373 #endif
9374
9375 /* First mark all the DIEs in this CU so we know which get local refs. */
9376 mark_dies (node->root_die);
9377
9378 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9379
9380 build_abbrev_table (node->root_die, extern_map);
9381
9382 delete extern_map;
9383 extern_map = NULL;
9384
9385 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9386 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9387 calc_die_sizes (node->root_die);
9388
9389 #if defined (OBJECT_FORMAT_ELF)
9390 if (!dwarf_split_debug_info)
9391 secname = ".debug_types";
9392 else
9393 secname = ".debug_types.dwo";
9394
9395 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9396 sprintf (tmp, "wt.");
9397 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9398 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9399 comdat_key = get_identifier (tmp);
9400 targetm.asm_out.named_section (secname,
9401 SECTION_DEBUG | SECTION_LINKONCE,
9402 comdat_key);
9403 #else
9404 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9405 sprintf (tmp, ".gnu.linkonce.wt.");
9406 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9407 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9408 secname = tmp;
9409 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9410 #endif
9411
9412 /* Output debugging information. */
9413 output_compilation_unit_header ();
9414 output_signature (node->signature, "Type Signature");
9415 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9416 "Offset to Type DIE");
9417 output_die (node->root_die);
9418
9419 unmark_dies (node->root_die);
9420 }
9421
9422 /* Return the DWARF2/3 pubname associated with a decl. */
9423
9424 static const char *
9425 dwarf2_name (tree decl, int scope)
9426 {
9427 if (DECL_NAMELESS (decl))
9428 return NULL;
9429 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9430 }
9431
9432 /* Add a new entry to .debug_pubnames if appropriate. */
9433
9434 static void
9435 add_pubname_string (const char *str, dw_die_ref die)
9436 {
9437 pubname_entry e;
9438
9439 e.die = die;
9440 e.name = xstrdup (str);
9441 vec_safe_push (pubname_table, e);
9442 }
9443
9444 static void
9445 add_pubname (tree decl, dw_die_ref die)
9446 {
9447 if (!want_pubnames ())
9448 return;
9449
9450 /* Don't add items to the table when we expect that the consumer will have
9451 just read the enclosing die. For example, if the consumer is looking at a
9452 class_member, it will either be inside the class already, or will have just
9453 looked up the class to find the member. Either way, searching the class is
9454 faster than searching the index. */
9455 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9456 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9457 {
9458 const char *name = dwarf2_name (decl, 1);
9459
9460 if (name)
9461 add_pubname_string (name, die);
9462 }
9463 }
9464
9465 /* Add an enumerator to the pubnames section. */
9466
9467 static void
9468 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9469 {
9470 pubname_entry e;
9471
9472 gcc_assert (scope_name);
9473 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9474 e.die = die;
9475 vec_safe_push (pubname_table, e);
9476 }
9477
9478 /* Add a new entry to .debug_pubtypes if appropriate. */
9479
9480 static void
9481 add_pubtype (tree decl, dw_die_ref die)
9482 {
9483 pubname_entry e;
9484
9485 if (!want_pubnames ())
9486 return;
9487
9488 if ((TREE_PUBLIC (decl)
9489 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9490 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9491 {
9492 tree scope = NULL;
9493 const char *scope_name = "";
9494 const char *sep = is_cxx () ? "::" : ".";
9495 const char *name;
9496
9497 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9498 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9499 {
9500 scope_name = lang_hooks.dwarf_name (scope, 1);
9501 if (scope_name != NULL && scope_name[0] != '\0')
9502 scope_name = concat (scope_name, sep, NULL);
9503 else
9504 scope_name = "";
9505 }
9506
9507 if (TYPE_P (decl))
9508 name = type_tag (decl);
9509 else
9510 name = lang_hooks.dwarf_name (decl, 1);
9511
9512 /* If we don't have a name for the type, there's no point in adding
9513 it to the table. */
9514 if (name != NULL && name[0] != '\0')
9515 {
9516 e.die = die;
9517 e.name = concat (scope_name, name, NULL);
9518 vec_safe_push (pubtype_table, e);
9519 }
9520
9521 /* Although it might be more consistent to add the pubinfo for the
9522 enumerators as their dies are created, they should only be added if the
9523 enum type meets the criteria above. So rather than re-check the parent
9524 enum type whenever an enumerator die is created, just output them all
9525 here. This isn't protected by the name conditional because anonymous
9526 enums don't have names. */
9527 if (die->die_tag == DW_TAG_enumeration_type)
9528 {
9529 dw_die_ref c;
9530
9531 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9532 }
9533 }
9534 }
9535
9536 /* Output a single entry in the pubnames table. */
9537
9538 static void
9539 output_pubname (dw_offset die_offset, pubname_entry *entry)
9540 {
9541 dw_die_ref die = entry->die;
9542 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9543
9544 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9545
9546 if (debug_generate_pub_sections == 2)
9547 {
9548 /* This logic follows gdb's method for determining the value of the flag
9549 byte. */
9550 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9551 switch (die->die_tag)
9552 {
9553 case DW_TAG_typedef:
9554 case DW_TAG_base_type:
9555 case DW_TAG_subrange_type:
9556 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9557 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9558 break;
9559 case DW_TAG_enumerator:
9560 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9561 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9562 if (!is_cxx () && !is_java ())
9563 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9564 break;
9565 case DW_TAG_subprogram:
9566 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9567 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9568 if (!is_ada ())
9569 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9570 break;
9571 case DW_TAG_constant:
9572 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9573 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9574 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9575 break;
9576 case DW_TAG_variable:
9577 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9578 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9579 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9580 break;
9581 case DW_TAG_namespace:
9582 case DW_TAG_imported_declaration:
9583 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9584 break;
9585 case DW_TAG_class_type:
9586 case DW_TAG_interface_type:
9587 case DW_TAG_structure_type:
9588 case DW_TAG_union_type:
9589 case DW_TAG_enumeration_type:
9590 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9591 if (!is_cxx () && !is_java ())
9592 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9593 break;
9594 default:
9595 /* An unusual tag. Leave the flag-byte empty. */
9596 break;
9597 }
9598 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9599 "GDB-index flags");
9600 }
9601
9602 dw2_asm_output_nstring (entry->name, -1, "external name");
9603 }
9604
9605
9606 /* Output the public names table used to speed up access to externally
9607 visible names; or the public types table used to find type definitions. */
9608
9609 static void
9610 output_pubnames (vec<pubname_entry, va_gc> *names)
9611 {
9612 unsigned i;
9613 unsigned long pubnames_length = size_of_pubnames (names);
9614 pubname_ref pub;
9615
9616 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9617 dw2_asm_output_data (4, 0xffffffff,
9618 "Initial length escape value indicating 64-bit DWARF extension");
9619 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9620
9621 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9622 dw2_asm_output_data (2, 2, "DWARF Version");
9623
9624 if (dwarf_split_debug_info)
9625 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9626 debug_skeleton_info_section,
9627 "Offset of Compilation Unit Info");
9628 else
9629 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9630 debug_info_section,
9631 "Offset of Compilation Unit Info");
9632 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9633 "Compilation Unit Length");
9634
9635 FOR_EACH_VEC_ELT (*names, i, pub)
9636 {
9637 if (include_pubname_in_output (names, pub))
9638 {
9639 dw_offset die_offset = pub->die->die_offset;
9640
9641 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9642 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9643 gcc_assert (pub->die->die_mark);
9644
9645 /* If we're putting types in their own .debug_types sections,
9646 the .debug_pubtypes table will still point to the compile
9647 unit (not the type unit), so we want to use the offset of
9648 the skeleton DIE (if there is one). */
9649 if (pub->die->comdat_type_p && names == pubtype_table)
9650 {
9651 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9652
9653 if (type_node != NULL)
9654 die_offset = (type_node->skeleton_die != NULL
9655 ? type_node->skeleton_die->die_offset
9656 : comp_unit_die ()->die_offset);
9657 }
9658
9659 output_pubname (die_offset, pub);
9660 }
9661 }
9662
9663 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9664 }
9665
9666 /* Output public names and types tables if necessary. */
9667
9668 static void
9669 output_pubtables (void)
9670 {
9671 if (!want_pubnames () || !info_section_emitted)
9672 return;
9673
9674 switch_to_section (debug_pubnames_section);
9675 output_pubnames (pubname_table);
9676 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9677 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9678 simply won't look for the section. */
9679 switch_to_section (debug_pubtypes_section);
9680 output_pubnames (pubtype_table);
9681 }
9682
9683
9684 /* Output the information that goes into the .debug_aranges table.
9685 Namely, define the beginning and ending address range of the
9686 text section generated for this compilation unit. */
9687
9688 static void
9689 output_aranges (unsigned long aranges_length)
9690 {
9691 unsigned i;
9692
9693 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9694 dw2_asm_output_data (4, 0xffffffff,
9695 "Initial length escape value indicating 64-bit DWARF extension");
9696 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9697 "Length of Address Ranges Info");
9698 /* Version number for aranges is still 2, even up to DWARF5. */
9699 dw2_asm_output_data (2, 2, "DWARF Version");
9700 if (dwarf_split_debug_info)
9701 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9702 debug_skeleton_info_section,
9703 "Offset of Compilation Unit Info");
9704 else
9705 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9706 debug_info_section,
9707 "Offset of Compilation Unit Info");
9708 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9709 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9710
9711 /* We need to align to twice the pointer size here. */
9712 if (DWARF_ARANGES_PAD_SIZE)
9713 {
9714 /* Pad using a 2 byte words so that padding is correct for any
9715 pointer size. */
9716 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9717 2 * DWARF2_ADDR_SIZE);
9718 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9719 dw2_asm_output_data (2, 0, NULL);
9720 }
9721
9722 /* It is necessary not to output these entries if the sections were
9723 not used; if the sections were not used, the length will be 0 and
9724 the address may end up as 0 if the section is discarded by ld
9725 --gc-sections, leaving an invalid (0, 0) entry that can be
9726 confused with the terminator. */
9727 if (text_section_used)
9728 {
9729 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9730 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9731 text_section_label, "Length");
9732 }
9733 if (cold_text_section_used)
9734 {
9735 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9736 "Address");
9737 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9738 cold_text_section_label, "Length");
9739 }
9740
9741 if (have_multiple_function_sections)
9742 {
9743 unsigned fde_idx;
9744 dw_fde_ref fde;
9745
9746 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9747 {
9748 if (DECL_IGNORED_P (fde->decl))
9749 continue;
9750 if (!fde->in_std_section)
9751 {
9752 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9753 "Address");
9754 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9755 fde->dw_fde_begin, "Length");
9756 }
9757 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9758 {
9759 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9760 "Address");
9761 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9762 fde->dw_fde_second_begin, "Length");
9763 }
9764 }
9765 }
9766
9767 /* Output the terminator words. */
9768 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9769 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9770 }
9771
9772 /* Add a new entry to .debug_ranges. Return the offset at which it
9773 was placed. */
9774
9775 static unsigned int
9776 add_ranges_num (int num)
9777 {
9778 unsigned int in_use = ranges_table_in_use;
9779
9780 if (in_use == ranges_table_allocated)
9781 {
9782 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9783 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9784 ranges_table_allocated);
9785 memset (ranges_table + ranges_table_in_use, 0,
9786 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9787 }
9788
9789 ranges_table[in_use].num = num;
9790 ranges_table_in_use = in_use + 1;
9791
9792 return in_use * 2 * DWARF2_ADDR_SIZE;
9793 }
9794
9795 /* Add a new entry to .debug_ranges corresponding to a block, or a
9796 range terminator if BLOCK is NULL. */
9797
9798 static unsigned int
9799 add_ranges (const_tree block)
9800 {
9801 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9802 }
9803
9804 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9805 When using dwarf_split_debug_info, address attributes in dies destined
9806 for the final executable should be direct references--setting the
9807 parameter force_direct ensures this behavior. */
9808
9809 static void
9810 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9811 bool *added, bool force_direct)
9812 {
9813 unsigned int in_use = ranges_by_label_in_use;
9814 unsigned int offset;
9815
9816 if (in_use == ranges_by_label_allocated)
9817 {
9818 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9819 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9820 ranges_by_label,
9821 ranges_by_label_allocated);
9822 memset (ranges_by_label + ranges_by_label_in_use, 0,
9823 RANGES_TABLE_INCREMENT
9824 * sizeof (struct dw_ranges_by_label_struct));
9825 }
9826
9827 ranges_by_label[in_use].begin = begin;
9828 ranges_by_label[in_use].end = end;
9829 ranges_by_label_in_use = in_use + 1;
9830
9831 offset = add_ranges_num (-(int)in_use - 1);
9832 if (!*added)
9833 {
9834 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9835 *added = true;
9836 }
9837 }
9838
9839 static void
9840 output_ranges (void)
9841 {
9842 unsigned i;
9843 static const char *const start_fmt = "Offset %#x";
9844 const char *fmt = start_fmt;
9845
9846 for (i = 0; i < ranges_table_in_use; i++)
9847 {
9848 int block_num = ranges_table[i].num;
9849
9850 if (block_num > 0)
9851 {
9852 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9853 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9854
9855 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9856 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9857
9858 /* If all code is in the text section, then the compilation
9859 unit base address defaults to DW_AT_low_pc, which is the
9860 base of the text section. */
9861 if (!have_multiple_function_sections)
9862 {
9863 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9864 text_section_label,
9865 fmt, i * 2 * DWARF2_ADDR_SIZE);
9866 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9867 text_section_label, NULL);
9868 }
9869
9870 /* Otherwise, the compilation unit base address is zero,
9871 which allows us to use absolute addresses, and not worry
9872 about whether the target supports cross-section
9873 arithmetic. */
9874 else
9875 {
9876 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9877 fmt, i * 2 * DWARF2_ADDR_SIZE);
9878 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9879 }
9880
9881 fmt = NULL;
9882 }
9883
9884 /* Negative block_num stands for an index into ranges_by_label. */
9885 else if (block_num < 0)
9886 {
9887 int lab_idx = - block_num - 1;
9888
9889 if (!have_multiple_function_sections)
9890 {
9891 gcc_unreachable ();
9892 #if 0
9893 /* If we ever use add_ranges_by_labels () for a single
9894 function section, all we have to do is to take out
9895 the #if 0 above. */
9896 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9897 ranges_by_label[lab_idx].begin,
9898 text_section_label,
9899 fmt, i * 2 * DWARF2_ADDR_SIZE);
9900 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9901 ranges_by_label[lab_idx].end,
9902 text_section_label, NULL);
9903 #endif
9904 }
9905 else
9906 {
9907 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9908 ranges_by_label[lab_idx].begin,
9909 fmt, i * 2 * DWARF2_ADDR_SIZE);
9910 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9911 ranges_by_label[lab_idx].end,
9912 NULL);
9913 }
9914 }
9915 else
9916 {
9917 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9918 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9919 fmt = start_fmt;
9920 }
9921 }
9922 }
9923
9924 /* Data structure containing information about input files. */
9925 struct file_info
9926 {
9927 const char *path; /* Complete file name. */
9928 const char *fname; /* File name part. */
9929 int length; /* Length of entire string. */
9930 struct dwarf_file_data * file_idx; /* Index in input file table. */
9931 int dir_idx; /* Index in directory table. */
9932 };
9933
9934 /* Data structure containing information about directories with source
9935 files. */
9936 struct dir_info
9937 {
9938 const char *path; /* Path including directory name. */
9939 int length; /* Path length. */
9940 int prefix; /* Index of directory entry which is a prefix. */
9941 int count; /* Number of files in this directory. */
9942 int dir_idx; /* Index of directory used as base. */
9943 };
9944
9945 /* Callback function for file_info comparison. We sort by looking at
9946 the directories in the path. */
9947
9948 static int
9949 file_info_cmp (const void *p1, const void *p2)
9950 {
9951 const struct file_info *const s1 = (const struct file_info *) p1;
9952 const struct file_info *const s2 = (const struct file_info *) p2;
9953 const unsigned char *cp1;
9954 const unsigned char *cp2;
9955
9956 /* Take care of file names without directories. We need to make sure that
9957 we return consistent values to qsort since some will get confused if
9958 we return the same value when identical operands are passed in opposite
9959 orders. So if neither has a directory, return 0 and otherwise return
9960 1 or -1 depending on which one has the directory. */
9961 if ((s1->path == s1->fname || s2->path == s2->fname))
9962 return (s2->path == s2->fname) - (s1->path == s1->fname);
9963
9964 cp1 = (const unsigned char *) s1->path;
9965 cp2 = (const unsigned char *) s2->path;
9966
9967 while (1)
9968 {
9969 ++cp1;
9970 ++cp2;
9971 /* Reached the end of the first path? If so, handle like above. */
9972 if ((cp1 == (const unsigned char *) s1->fname)
9973 || (cp2 == (const unsigned char *) s2->fname))
9974 return ((cp2 == (const unsigned char *) s2->fname)
9975 - (cp1 == (const unsigned char *) s1->fname));
9976
9977 /* Character of current path component the same? */
9978 else if (*cp1 != *cp2)
9979 return *cp1 - *cp2;
9980 }
9981 }
9982
9983 struct file_name_acquire_data
9984 {
9985 struct file_info *files;
9986 int used_files;
9987 int max_files;
9988 };
9989
9990 /* Traversal function for the hash table. */
9991
9992 int
9993 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9994 {
9995 struct dwarf_file_data *d = *slot;
9996 struct file_info *fi;
9997 const char *f;
9998
9999 gcc_assert (fnad->max_files >= d->emitted_number);
10000
10001 if (! d->emitted_number)
10002 return 1;
10003
10004 gcc_assert (fnad->max_files != fnad->used_files);
10005
10006 fi = fnad->files + fnad->used_files++;
10007
10008 /* Skip all leading "./". */
10009 f = d->filename;
10010 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10011 f += 2;
10012
10013 /* Create a new array entry. */
10014 fi->path = f;
10015 fi->length = strlen (f);
10016 fi->file_idx = d;
10017
10018 /* Search for the file name part. */
10019 f = strrchr (f, DIR_SEPARATOR);
10020 #if defined (DIR_SEPARATOR_2)
10021 {
10022 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10023
10024 if (g != NULL)
10025 {
10026 if (f == NULL || f < g)
10027 f = g;
10028 }
10029 }
10030 #endif
10031
10032 fi->fname = f == NULL ? fi->path : f + 1;
10033 return 1;
10034 }
10035
10036 /* Output the directory table and the file name table. We try to minimize
10037 the total amount of memory needed. A heuristic is used to avoid large
10038 slowdowns with many input files. */
10039
10040 static void
10041 output_file_names (void)
10042 {
10043 struct file_name_acquire_data fnad;
10044 int numfiles;
10045 struct file_info *files;
10046 struct dir_info *dirs;
10047 int *saved;
10048 int *savehere;
10049 int *backmap;
10050 int ndirs;
10051 int idx_offset;
10052 int i;
10053
10054 if (!last_emitted_file)
10055 {
10056 dw2_asm_output_data (1, 0, "End directory table");
10057 dw2_asm_output_data (1, 0, "End file name table");
10058 return;
10059 }
10060
10061 numfiles = last_emitted_file->emitted_number;
10062
10063 /* Allocate the various arrays we need. */
10064 files = XALLOCAVEC (struct file_info, numfiles);
10065 dirs = XALLOCAVEC (struct dir_info, numfiles);
10066
10067 fnad.files = files;
10068 fnad.used_files = 0;
10069 fnad.max_files = numfiles;
10070 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10071 gcc_assert (fnad.used_files == fnad.max_files);
10072
10073 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10074
10075 /* Find all the different directories used. */
10076 dirs[0].path = files[0].path;
10077 dirs[0].length = files[0].fname - files[0].path;
10078 dirs[0].prefix = -1;
10079 dirs[0].count = 1;
10080 dirs[0].dir_idx = 0;
10081 files[0].dir_idx = 0;
10082 ndirs = 1;
10083
10084 for (i = 1; i < numfiles; i++)
10085 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10086 && memcmp (dirs[ndirs - 1].path, files[i].path,
10087 dirs[ndirs - 1].length) == 0)
10088 {
10089 /* Same directory as last entry. */
10090 files[i].dir_idx = ndirs - 1;
10091 ++dirs[ndirs - 1].count;
10092 }
10093 else
10094 {
10095 int j;
10096
10097 /* This is a new directory. */
10098 dirs[ndirs].path = files[i].path;
10099 dirs[ndirs].length = files[i].fname - files[i].path;
10100 dirs[ndirs].count = 1;
10101 dirs[ndirs].dir_idx = ndirs;
10102 files[i].dir_idx = ndirs;
10103
10104 /* Search for a prefix. */
10105 dirs[ndirs].prefix = -1;
10106 for (j = 0; j < ndirs; j++)
10107 if (dirs[j].length < dirs[ndirs].length
10108 && dirs[j].length > 1
10109 && (dirs[ndirs].prefix == -1
10110 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10111 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10112 dirs[ndirs].prefix = j;
10113
10114 ++ndirs;
10115 }
10116
10117 /* Now to the actual work. We have to find a subset of the directories which
10118 allow expressing the file name using references to the directory table
10119 with the least amount of characters. We do not do an exhaustive search
10120 where we would have to check out every combination of every single
10121 possible prefix. Instead we use a heuristic which provides nearly optimal
10122 results in most cases and never is much off. */
10123 saved = XALLOCAVEC (int, ndirs);
10124 savehere = XALLOCAVEC (int, ndirs);
10125
10126 memset (saved, '\0', ndirs * sizeof (saved[0]));
10127 for (i = 0; i < ndirs; i++)
10128 {
10129 int j;
10130 int total;
10131
10132 /* We can always save some space for the current directory. But this
10133 does not mean it will be enough to justify adding the directory. */
10134 savehere[i] = dirs[i].length;
10135 total = (savehere[i] - saved[i]) * dirs[i].count;
10136
10137 for (j = i + 1; j < ndirs; j++)
10138 {
10139 savehere[j] = 0;
10140 if (saved[j] < dirs[i].length)
10141 {
10142 /* Determine whether the dirs[i] path is a prefix of the
10143 dirs[j] path. */
10144 int k;
10145
10146 k = dirs[j].prefix;
10147 while (k != -1 && k != (int) i)
10148 k = dirs[k].prefix;
10149
10150 if (k == (int) i)
10151 {
10152 /* Yes it is. We can possibly save some memory by
10153 writing the filenames in dirs[j] relative to
10154 dirs[i]. */
10155 savehere[j] = dirs[i].length;
10156 total += (savehere[j] - saved[j]) * dirs[j].count;
10157 }
10158 }
10159 }
10160
10161 /* Check whether we can save enough to justify adding the dirs[i]
10162 directory. */
10163 if (total > dirs[i].length + 1)
10164 {
10165 /* It's worthwhile adding. */
10166 for (j = i; j < ndirs; j++)
10167 if (savehere[j] > 0)
10168 {
10169 /* Remember how much we saved for this directory so far. */
10170 saved[j] = savehere[j];
10171
10172 /* Remember the prefix directory. */
10173 dirs[j].dir_idx = i;
10174 }
10175 }
10176 }
10177
10178 /* Emit the directory name table. */
10179 idx_offset = dirs[0].length > 0 ? 1 : 0;
10180 for (i = 1 - idx_offset; i < ndirs; i++)
10181 dw2_asm_output_nstring (dirs[i].path,
10182 dirs[i].length
10183 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10184 "Directory Entry: %#x", i + idx_offset);
10185
10186 dw2_asm_output_data (1, 0, "End directory table");
10187
10188 /* We have to emit them in the order of emitted_number since that's
10189 used in the debug info generation. To do this efficiently we
10190 generate a back-mapping of the indices first. */
10191 backmap = XALLOCAVEC (int, numfiles);
10192 for (i = 0; i < numfiles; i++)
10193 backmap[files[i].file_idx->emitted_number - 1] = i;
10194
10195 /* Now write all the file names. */
10196 for (i = 0; i < numfiles; i++)
10197 {
10198 int file_idx = backmap[i];
10199 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10200
10201 #ifdef VMS_DEBUGGING_INFO
10202 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10203
10204 /* Setting these fields can lead to debugger miscomparisons,
10205 but VMS Debug requires them to be set correctly. */
10206
10207 int ver;
10208 long long cdt;
10209 long siz;
10210 int maxfilelen = strlen (files[file_idx].path)
10211 + dirs[dir_idx].length
10212 + MAX_VMS_VERSION_LEN + 1;
10213 char *filebuf = XALLOCAVEC (char, maxfilelen);
10214
10215 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10216 snprintf (filebuf, maxfilelen, "%s;%d",
10217 files[file_idx].path + dirs[dir_idx].length, ver);
10218
10219 dw2_asm_output_nstring
10220 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10221
10222 /* Include directory index. */
10223 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10224
10225 /* Modification time. */
10226 dw2_asm_output_data_uleb128
10227 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10228 ? cdt : 0,
10229 NULL);
10230
10231 /* File length in bytes. */
10232 dw2_asm_output_data_uleb128
10233 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10234 ? siz : 0,
10235 NULL);
10236 #else
10237 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10238 "File Entry: %#x", (unsigned) i + 1);
10239
10240 /* Include directory index. */
10241 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10242
10243 /* Modification time. */
10244 dw2_asm_output_data_uleb128 (0, NULL);
10245
10246 /* File length in bytes. */
10247 dw2_asm_output_data_uleb128 (0, NULL);
10248 #endif /* VMS_DEBUGGING_INFO */
10249 }
10250
10251 dw2_asm_output_data (1, 0, "End file name table");
10252 }
10253
10254
10255 /* Output one line number table into the .debug_line section. */
10256
10257 static void
10258 output_one_line_info_table (dw_line_info_table *table)
10259 {
10260 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10261 unsigned int current_line = 1;
10262 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10263 dw_line_info_entry *ent;
10264 size_t i;
10265
10266 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10267 {
10268 switch (ent->opcode)
10269 {
10270 case LI_set_address:
10271 /* ??? Unfortunately, we have little choice here currently, and
10272 must always use the most general form. GCC does not know the
10273 address delta itself, so we can't use DW_LNS_advance_pc. Many
10274 ports do have length attributes which will give an upper bound
10275 on the address range. We could perhaps use length attributes
10276 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10277 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10278
10279 /* This can handle any delta. This takes
10280 4+DWARF2_ADDR_SIZE bytes. */
10281 dw2_asm_output_data (1, 0, "set address %s", line_label);
10282 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10283 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10284 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10285 break;
10286
10287 case LI_set_line:
10288 if (ent->val == current_line)
10289 {
10290 /* We still need to start a new row, so output a copy insn. */
10291 dw2_asm_output_data (1, DW_LNS_copy,
10292 "copy line %u", current_line);
10293 }
10294 else
10295 {
10296 int line_offset = ent->val - current_line;
10297 int line_delta = line_offset - DWARF_LINE_BASE;
10298
10299 current_line = ent->val;
10300 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10301 {
10302 /* This can handle deltas from -10 to 234, using the current
10303 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10304 This takes 1 byte. */
10305 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10306 "line %u", current_line);
10307 }
10308 else
10309 {
10310 /* This can handle any delta. This takes at least 4 bytes,
10311 depending on the value being encoded. */
10312 dw2_asm_output_data (1, DW_LNS_advance_line,
10313 "advance to line %u", current_line);
10314 dw2_asm_output_data_sleb128 (line_offset, NULL);
10315 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10316 }
10317 }
10318 break;
10319
10320 case LI_set_file:
10321 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10322 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10323 break;
10324
10325 case LI_set_column:
10326 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10327 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10328 break;
10329
10330 case LI_negate_stmt:
10331 current_is_stmt = !current_is_stmt;
10332 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10333 "is_stmt %d", current_is_stmt);
10334 break;
10335
10336 case LI_set_prologue_end:
10337 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10338 "set prologue end");
10339 break;
10340
10341 case LI_set_epilogue_begin:
10342 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10343 "set epilogue begin");
10344 break;
10345
10346 case LI_set_discriminator:
10347 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10348 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10349 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10350 dw2_asm_output_data_uleb128 (ent->val, NULL);
10351 break;
10352 }
10353 }
10354
10355 /* Emit debug info for the address of the end of the table. */
10356 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10357 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10358 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10359 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10360
10361 dw2_asm_output_data (1, 0, "end sequence");
10362 dw2_asm_output_data_uleb128 (1, NULL);
10363 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10364 }
10365
10366 /* Output the source line number correspondence information. This
10367 information goes into the .debug_line section. */
10368
10369 static void
10370 output_line_info (bool prologue_only)
10371 {
10372 char l1[20], l2[20], p1[20], p2[20];
10373 /* We don't support DWARFv5 line tables yet. */
10374 int ver = dwarf_version < 5 ? dwarf_version : 4;
10375 bool saw_one = false;
10376 int opc;
10377
10378 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10379 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10380 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10381 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10382
10383 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10384 dw2_asm_output_data (4, 0xffffffff,
10385 "Initial length escape value indicating 64-bit DWARF extension");
10386 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10387 "Length of Source Line Info");
10388 ASM_OUTPUT_LABEL (asm_out_file, l1);
10389
10390 dw2_asm_output_data (2, ver, "DWARF Version");
10391 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10392 ASM_OUTPUT_LABEL (asm_out_file, p1);
10393
10394 /* Define the architecture-dependent minimum instruction length (in bytes).
10395 In this implementation of DWARF, this field is used for information
10396 purposes only. Since GCC generates assembly language, we have no
10397 a priori knowledge of how many instruction bytes are generated for each
10398 source line, and therefore can use only the DW_LNE_set_address and
10399 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10400 this as '1', which is "correct enough" for all architectures,
10401 and don't let the target override. */
10402 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10403
10404 if (ver >= 4)
10405 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10406 "Maximum Operations Per Instruction");
10407 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10408 "Default is_stmt_start flag");
10409 dw2_asm_output_data (1, DWARF_LINE_BASE,
10410 "Line Base Value (Special Opcodes)");
10411 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10412 "Line Range Value (Special Opcodes)");
10413 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10414 "Special Opcode Base");
10415
10416 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10417 {
10418 int n_op_args;
10419 switch (opc)
10420 {
10421 case DW_LNS_advance_pc:
10422 case DW_LNS_advance_line:
10423 case DW_LNS_set_file:
10424 case DW_LNS_set_column:
10425 case DW_LNS_fixed_advance_pc:
10426 case DW_LNS_set_isa:
10427 n_op_args = 1;
10428 break;
10429 default:
10430 n_op_args = 0;
10431 break;
10432 }
10433
10434 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10435 opc, n_op_args);
10436 }
10437
10438 /* Write out the information about the files we use. */
10439 output_file_names ();
10440 ASM_OUTPUT_LABEL (asm_out_file, p2);
10441 if (prologue_only)
10442 {
10443 /* Output the marker for the end of the line number info. */
10444 ASM_OUTPUT_LABEL (asm_out_file, l2);
10445 return;
10446 }
10447
10448 if (separate_line_info)
10449 {
10450 dw_line_info_table *table;
10451 size_t i;
10452
10453 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10454 if (table->in_use)
10455 {
10456 output_one_line_info_table (table);
10457 saw_one = true;
10458 }
10459 }
10460 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10461 {
10462 output_one_line_info_table (cold_text_section_line_info);
10463 saw_one = true;
10464 }
10465
10466 /* ??? Some Darwin linkers crash on a .debug_line section with no
10467 sequences. Further, merely a DW_LNE_end_sequence entry is not
10468 sufficient -- the address column must also be initialized.
10469 Make sure to output at least one set_address/end_sequence pair,
10470 choosing .text since that section is always present. */
10471 if (text_section_line_info->in_use || !saw_one)
10472 output_one_line_info_table (text_section_line_info);
10473
10474 /* Output the marker for the end of the line number info. */
10475 ASM_OUTPUT_LABEL (asm_out_file, l2);
10476 }
10477 \f
10478 /* Given a pointer to a tree node for some base type, return a pointer to
10479 a DIE that describes the given type.
10480
10481 This routine must only be called for GCC type nodes that correspond to
10482 Dwarf base (fundamental) types. */
10483
10484 static dw_die_ref
10485 base_type_die (tree type)
10486 {
10487 dw_die_ref base_type_result;
10488 enum dwarf_type encoding;
10489
10490 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10491 return 0;
10492
10493 /* If this is a subtype that should not be emitted as a subrange type,
10494 use the base type. See subrange_type_for_debug_p. */
10495 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10496 type = TREE_TYPE (type);
10497
10498 switch (TREE_CODE (type))
10499 {
10500 case INTEGER_TYPE:
10501 if ((dwarf_version >= 4 || !dwarf_strict)
10502 && TYPE_NAME (type)
10503 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10504 && DECL_IS_BUILTIN (TYPE_NAME (type))
10505 && DECL_NAME (TYPE_NAME (type)))
10506 {
10507 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10508 if (strcmp (name, "char16_t") == 0
10509 || strcmp (name, "char32_t") == 0)
10510 {
10511 encoding = DW_ATE_UTF;
10512 break;
10513 }
10514 }
10515 if (TYPE_STRING_FLAG (type))
10516 {
10517 if (TYPE_UNSIGNED (type))
10518 encoding = DW_ATE_unsigned_char;
10519 else
10520 encoding = DW_ATE_signed_char;
10521 }
10522 else if (TYPE_UNSIGNED (type))
10523 encoding = DW_ATE_unsigned;
10524 else
10525 encoding = DW_ATE_signed;
10526 break;
10527
10528 case REAL_TYPE:
10529 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10530 {
10531 if (dwarf_version >= 3 || !dwarf_strict)
10532 encoding = DW_ATE_decimal_float;
10533 else
10534 encoding = DW_ATE_lo_user;
10535 }
10536 else
10537 encoding = DW_ATE_float;
10538 break;
10539
10540 case FIXED_POINT_TYPE:
10541 if (!(dwarf_version >= 3 || !dwarf_strict))
10542 encoding = DW_ATE_lo_user;
10543 else if (TYPE_UNSIGNED (type))
10544 encoding = DW_ATE_unsigned_fixed;
10545 else
10546 encoding = DW_ATE_signed_fixed;
10547 break;
10548
10549 /* Dwarf2 doesn't know anything about complex ints, so use
10550 a user defined type for it. */
10551 case COMPLEX_TYPE:
10552 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10553 encoding = DW_ATE_complex_float;
10554 else
10555 encoding = DW_ATE_lo_user;
10556 break;
10557
10558 case BOOLEAN_TYPE:
10559 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10560 encoding = DW_ATE_boolean;
10561 break;
10562
10563 default:
10564 /* No other TREE_CODEs are Dwarf fundamental types. */
10565 gcc_unreachable ();
10566 }
10567
10568 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10569
10570 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10571 int_size_in_bytes (type));
10572 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10573 add_pubtype (type, base_type_result);
10574
10575 return base_type_result;
10576 }
10577
10578 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10579 named 'auto' in its type: return true for it, false otherwise. */
10580
10581 static inline bool
10582 is_cxx_auto (tree type)
10583 {
10584 if (is_cxx ())
10585 {
10586 tree name = TYPE_IDENTIFIER (type);
10587 if (name == get_identifier ("auto")
10588 || name == get_identifier ("decltype(auto)"))
10589 return true;
10590 }
10591 return false;
10592 }
10593
10594 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10595 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10596
10597 static inline int
10598 is_base_type (tree type)
10599 {
10600 switch (TREE_CODE (type))
10601 {
10602 case ERROR_MARK:
10603 case VOID_TYPE:
10604 case INTEGER_TYPE:
10605 case REAL_TYPE:
10606 case FIXED_POINT_TYPE:
10607 case COMPLEX_TYPE:
10608 case BOOLEAN_TYPE:
10609 case POINTER_BOUNDS_TYPE:
10610 return 1;
10611
10612 case ARRAY_TYPE:
10613 case RECORD_TYPE:
10614 case UNION_TYPE:
10615 case QUAL_UNION_TYPE:
10616 case ENUMERAL_TYPE:
10617 case FUNCTION_TYPE:
10618 case METHOD_TYPE:
10619 case POINTER_TYPE:
10620 case REFERENCE_TYPE:
10621 case NULLPTR_TYPE:
10622 case OFFSET_TYPE:
10623 case LANG_TYPE:
10624 case VECTOR_TYPE:
10625 return 0;
10626
10627 default:
10628 if (is_cxx_auto (type))
10629 return 0;
10630 gcc_unreachable ();
10631 }
10632
10633 return 0;
10634 }
10635
10636 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10637 node, return the size in bits for the type if it is a constant, or else
10638 return the alignment for the type if the type's size is not constant, or
10639 else return BITS_PER_WORD if the type actually turns out to be an
10640 ERROR_MARK node. */
10641
10642 static inline unsigned HOST_WIDE_INT
10643 simple_type_size_in_bits (const_tree type)
10644 {
10645 if (TREE_CODE (type) == ERROR_MARK)
10646 return BITS_PER_WORD;
10647 else if (TYPE_SIZE (type) == NULL_TREE)
10648 return 0;
10649 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10650 return tree_to_uhwi (TYPE_SIZE (type));
10651 else
10652 return TYPE_ALIGN (type);
10653 }
10654
10655 /* Similarly, but return an offset_int instead of UHWI. */
10656
10657 static inline offset_int
10658 offset_int_type_size_in_bits (const_tree type)
10659 {
10660 if (TREE_CODE (type) == ERROR_MARK)
10661 return BITS_PER_WORD;
10662 else if (TYPE_SIZE (type) == NULL_TREE)
10663 return 0;
10664 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10665 return wi::to_offset (TYPE_SIZE (type));
10666 else
10667 return TYPE_ALIGN (type);
10668 }
10669
10670 /* Given a pointer to a tree node for a subrange type, return a pointer
10671 to a DIE that describes the given type. */
10672
10673 static dw_die_ref
10674 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10675 {
10676 dw_die_ref subrange_die;
10677 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10678
10679 if (context_die == NULL)
10680 context_die = comp_unit_die ();
10681
10682 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10683
10684 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10685 {
10686 /* The size of the subrange type and its base type do not match,
10687 so we need to generate a size attribute for the subrange type. */
10688 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10689 }
10690
10691 if (low)
10692 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10693 if (high)
10694 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10695
10696 return subrange_die;
10697 }
10698
10699 /* Returns the (const and/or volatile) cv_qualifiers associated with
10700 the decl node. This will normally be augmented with the
10701 cv_qualifiers of the underlying type in add_type_attribute. */
10702
10703 static int
10704 decl_quals (const_tree decl)
10705 {
10706 return ((TREE_READONLY (decl)
10707 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10708 | (TREE_THIS_VOLATILE (decl)
10709 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10710 }
10711
10712 /* Determine the TYPE whose qualifiers match the largest strict subset
10713 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10714 qualifiers outside QUAL_MASK. */
10715
10716 static int
10717 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10718 {
10719 tree t;
10720 int best_rank = 0, best_qual = 0, max_rank;
10721
10722 type_quals &= qual_mask;
10723 max_rank = popcount_hwi (type_quals) - 1;
10724
10725 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10726 t = TYPE_NEXT_VARIANT (t))
10727 {
10728 int q = TYPE_QUALS (t) & qual_mask;
10729
10730 if ((q & type_quals) == q && q != type_quals
10731 && check_base_type (t, type))
10732 {
10733 int rank = popcount_hwi (q);
10734
10735 if (rank > best_rank)
10736 {
10737 best_rank = rank;
10738 best_qual = q;
10739 }
10740 }
10741 }
10742
10743 return best_qual;
10744 }
10745
10746 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10747 entry that chains various modifiers in front of the given type. */
10748
10749 static dw_die_ref
10750 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10751 {
10752 enum tree_code code = TREE_CODE (type);
10753 dw_die_ref mod_type_die;
10754 dw_die_ref sub_die = NULL;
10755 tree item_type = NULL;
10756 tree qualified_type;
10757 tree name, low, high;
10758 dw_die_ref mod_scope;
10759 /* Only these cv-qualifiers are currently handled. */
10760 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10761 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10762
10763 if (code == ERROR_MARK)
10764 return NULL;
10765
10766 cv_quals &= cv_qual_mask;
10767
10768 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10769 tag modifier (and not an attribute) old consumers won't be able
10770 to handle it. */
10771 if (dwarf_version < 3)
10772 cv_quals &= ~TYPE_QUAL_RESTRICT;
10773
10774 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10775 if (dwarf_version < 5)
10776 cv_quals &= ~TYPE_QUAL_ATOMIC;
10777
10778 /* See if we already have the appropriately qualified variant of
10779 this type. */
10780 qualified_type = get_qualified_type (type, cv_quals);
10781
10782 if (qualified_type == sizetype
10783 && TYPE_NAME (qualified_type)
10784 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10785 {
10786 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10787
10788 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10789 && TYPE_PRECISION (t)
10790 == TYPE_PRECISION (qualified_type)
10791 && TYPE_UNSIGNED (t)
10792 == TYPE_UNSIGNED (qualified_type));
10793 qualified_type = t;
10794 }
10795
10796 /* If we do, then we can just use its DIE, if it exists. */
10797 if (qualified_type)
10798 {
10799 mod_type_die = lookup_type_die (qualified_type);
10800 if (mod_type_die)
10801 return mod_type_die;
10802 }
10803
10804 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10805
10806 /* Handle C typedef types. */
10807 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10808 && !DECL_ARTIFICIAL (name))
10809 {
10810 tree dtype = TREE_TYPE (name);
10811
10812 if (qualified_type == dtype)
10813 {
10814 /* For a named type, use the typedef. */
10815 gen_type_die (qualified_type, context_die);
10816 return lookup_type_die (qualified_type);
10817 }
10818 else
10819 {
10820 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10821 dquals &= cv_qual_mask;
10822 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10823 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10824 /* cv-unqualified version of named type. Just use
10825 the unnamed type to which it refers. */
10826 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10827 cv_quals, context_die);
10828 /* Else cv-qualified version of named type; fall through. */
10829 }
10830 }
10831
10832 mod_scope = scope_die_for (type, context_die);
10833
10834 if (cv_quals)
10835 {
10836 struct qual_info { int q; enum dwarf_tag t; };
10837 static const struct qual_info qual_info[] =
10838 {
10839 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10840 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10841 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10842 { TYPE_QUAL_CONST, DW_TAG_const_type },
10843 };
10844 int sub_quals;
10845 unsigned i;
10846
10847 /* Determine a lesser qualified type that most closely matches
10848 this one. Then generate DW_TAG_* entries for the remaining
10849 qualifiers. */
10850 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10851 cv_qual_mask);
10852 mod_type_die = modified_type_die (type, sub_quals, context_die);
10853
10854 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10855 if (qual_info[i].q & cv_quals & ~sub_quals)
10856 {
10857 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10858 if (mod_type_die)
10859 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10860 mod_type_die = d;
10861 }
10862 }
10863 else if (code == POINTER_TYPE)
10864 {
10865 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10866 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10867 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10868 item_type = TREE_TYPE (type);
10869 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10870 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10871 TYPE_ADDR_SPACE (item_type));
10872 }
10873 else if (code == REFERENCE_TYPE)
10874 {
10875 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10876 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10877 type);
10878 else
10879 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10880 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10881 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10882 item_type = TREE_TYPE (type);
10883 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10884 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10885 TYPE_ADDR_SPACE (item_type));
10886 }
10887 else if (code == INTEGER_TYPE
10888 && TREE_TYPE (type) != NULL_TREE
10889 && subrange_type_for_debug_p (type, &low, &high))
10890 {
10891 mod_type_die = subrange_type_die (type, low, high, context_die);
10892 item_type = TREE_TYPE (type);
10893 }
10894 else if (is_base_type (type))
10895 mod_type_die = base_type_die (type);
10896 else
10897 {
10898 gen_type_die (type, context_die);
10899
10900 /* We have to get the type_main_variant here (and pass that to the
10901 `lookup_type_die' routine) because the ..._TYPE node we have
10902 might simply be a *copy* of some original type node (where the
10903 copy was created to help us keep track of typedef names) and
10904 that copy might have a different TYPE_UID from the original
10905 ..._TYPE node. */
10906 if (TREE_CODE (type) != VECTOR_TYPE)
10907 return lookup_type_die (type_main_variant (type));
10908 else
10909 /* Vectors have the debugging information in the type,
10910 not the main variant. */
10911 return lookup_type_die (type);
10912 }
10913
10914 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10915 don't output a DW_TAG_typedef, since there isn't one in the
10916 user's program; just attach a DW_AT_name to the type.
10917 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10918 if the base type already has the same name. */
10919 if (name
10920 && ((TREE_CODE (name) != TYPE_DECL
10921 && (qualified_type == TYPE_MAIN_VARIANT (type)
10922 || (cv_quals == TYPE_UNQUALIFIED)))
10923 || (TREE_CODE (name) == TYPE_DECL
10924 && TREE_TYPE (name) == qualified_type
10925 && DECL_NAME (name))))
10926 {
10927 if (TREE_CODE (name) == TYPE_DECL)
10928 /* Could just call add_name_and_src_coords_attributes here,
10929 but since this is a builtin type it doesn't have any
10930 useful source coordinates anyway. */
10931 name = DECL_NAME (name);
10932 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10933 }
10934 /* This probably indicates a bug. */
10935 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10936 {
10937 name = TYPE_IDENTIFIER (type);
10938 add_name_attribute (mod_type_die,
10939 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10940 }
10941
10942 if (qualified_type)
10943 equate_type_number_to_die (qualified_type, mod_type_die);
10944
10945 if (item_type)
10946 /* We must do this after the equate_type_number_to_die call, in case
10947 this is a recursive type. This ensures that the modified_type_die
10948 recursion will terminate even if the type is recursive. Recursive
10949 types are possible in Ada. */
10950 sub_die = modified_type_die (item_type,
10951 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10952 context_die);
10953
10954 if (sub_die != NULL)
10955 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10956
10957 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10958 if (TYPE_ARTIFICIAL (type))
10959 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10960
10961 return mod_type_die;
10962 }
10963
10964 /* Generate DIEs for the generic parameters of T.
10965 T must be either a generic type or a generic function.
10966 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10967
10968 static void
10969 gen_generic_params_dies (tree t)
10970 {
10971 tree parms, args;
10972 int parms_num, i;
10973 dw_die_ref die = NULL;
10974 int non_default;
10975
10976 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10977 return;
10978
10979 if (TYPE_P (t))
10980 die = lookup_type_die (t);
10981 else if (DECL_P (t))
10982 die = lookup_decl_die (t);
10983
10984 gcc_assert (die);
10985
10986 parms = lang_hooks.get_innermost_generic_parms (t);
10987 if (!parms)
10988 /* T has no generic parameter. It means T is neither a generic type
10989 or function. End of story. */
10990 return;
10991
10992 parms_num = TREE_VEC_LENGTH (parms);
10993 args = lang_hooks.get_innermost_generic_args (t);
10994 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10995 non_default = int_cst_value (TREE_CHAIN (args));
10996 else
10997 non_default = TREE_VEC_LENGTH (args);
10998 for (i = 0; i < parms_num; i++)
10999 {
11000 tree parm, arg, arg_pack_elems;
11001 dw_die_ref parm_die;
11002
11003 parm = TREE_VEC_ELT (parms, i);
11004 arg = TREE_VEC_ELT (args, i);
11005 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11006 gcc_assert (parm && TREE_VALUE (parm) && arg);
11007
11008 if (parm && TREE_VALUE (parm) && arg)
11009 {
11010 /* If PARM represents a template parameter pack,
11011 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11012 by DW_TAG_template_*_parameter DIEs for the argument
11013 pack elements of ARG. Note that ARG would then be
11014 an argument pack. */
11015 if (arg_pack_elems)
11016 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11017 arg_pack_elems,
11018 die);
11019 else
11020 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11021 true /* emit name */, die);
11022 if (i >= non_default)
11023 add_AT_flag (parm_die, DW_AT_default_value, 1);
11024 }
11025 }
11026 }
11027
11028 /* Create and return a DIE for PARM which should be
11029 the representation of a generic type parameter.
11030 For instance, in the C++ front end, PARM would be a template parameter.
11031 ARG is the argument to PARM.
11032 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11033 name of the PARM.
11034 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11035 as a child node. */
11036
11037 static dw_die_ref
11038 generic_parameter_die (tree parm, tree arg,
11039 bool emit_name_p,
11040 dw_die_ref parent_die)
11041 {
11042 dw_die_ref tmpl_die = NULL;
11043 const char *name = NULL;
11044
11045 if (!parm || !DECL_NAME (parm) || !arg)
11046 return NULL;
11047
11048 /* We support non-type generic parameters and arguments,
11049 type generic parameters and arguments, as well as
11050 generic generic parameters (a.k.a. template template parameters in C++)
11051 and arguments. */
11052 if (TREE_CODE (parm) == PARM_DECL)
11053 /* PARM is a nontype generic parameter */
11054 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11055 else if (TREE_CODE (parm) == TYPE_DECL)
11056 /* PARM is a type generic parameter. */
11057 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11058 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11059 /* PARM is a generic generic parameter.
11060 Its DIE is a GNU extension. It shall have a
11061 DW_AT_name attribute to represent the name of the template template
11062 parameter, and a DW_AT_GNU_template_name attribute to represent the
11063 name of the template template argument. */
11064 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11065 parent_die, parm);
11066 else
11067 gcc_unreachable ();
11068
11069 if (tmpl_die)
11070 {
11071 tree tmpl_type;
11072
11073 /* If PARM is a generic parameter pack, it means we are
11074 emitting debug info for a template argument pack element.
11075 In other terms, ARG is a template argument pack element.
11076 In that case, we don't emit any DW_AT_name attribute for
11077 the die. */
11078 if (emit_name_p)
11079 {
11080 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11081 gcc_assert (name);
11082 add_AT_string (tmpl_die, DW_AT_name, name);
11083 }
11084
11085 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11086 {
11087 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11088 TMPL_DIE should have a child DW_AT_type attribute that is set
11089 to the type of the argument to PARM, which is ARG.
11090 If PARM is a type generic parameter, TMPL_DIE should have a
11091 child DW_AT_type that is set to ARG. */
11092 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11093 add_type_attribute (tmpl_die, tmpl_type,
11094 (TREE_THIS_VOLATILE (tmpl_type)
11095 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11096 parent_die);
11097 }
11098 else
11099 {
11100 /* So TMPL_DIE is a DIE representing a
11101 a generic generic template parameter, a.k.a template template
11102 parameter in C++ and arg is a template. */
11103
11104 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11105 to the name of the argument. */
11106 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11107 if (name)
11108 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11109 }
11110
11111 if (TREE_CODE (parm) == PARM_DECL)
11112 /* So PARM is a non-type generic parameter.
11113 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11114 attribute of TMPL_DIE which value represents the value
11115 of ARG.
11116 We must be careful here:
11117 The value of ARG might reference some function decls.
11118 We might currently be emitting debug info for a generic
11119 type and types are emitted before function decls, we don't
11120 know if the function decls referenced by ARG will actually be
11121 emitted after cgraph computations.
11122 So must defer the generation of the DW_AT_const_value to
11123 after cgraph is ready. */
11124 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11125 }
11126
11127 return tmpl_die;
11128 }
11129
11130 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11131 PARM_PACK must be a template parameter pack. The returned DIE
11132 will be child DIE of PARENT_DIE. */
11133
11134 static dw_die_ref
11135 template_parameter_pack_die (tree parm_pack,
11136 tree parm_pack_args,
11137 dw_die_ref parent_die)
11138 {
11139 dw_die_ref die;
11140 int j;
11141
11142 gcc_assert (parent_die && parm_pack);
11143
11144 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11145 add_name_and_src_coords_attributes (die, parm_pack);
11146 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11147 generic_parameter_die (parm_pack,
11148 TREE_VEC_ELT (parm_pack_args, j),
11149 false /* Don't emit DW_AT_name */,
11150 die);
11151 return die;
11152 }
11153
11154 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11155 an enumerated type. */
11156
11157 static inline int
11158 type_is_enum (const_tree type)
11159 {
11160 return TREE_CODE (type) == ENUMERAL_TYPE;
11161 }
11162
11163 /* Return the DBX register number described by a given RTL node. */
11164
11165 static unsigned int
11166 dbx_reg_number (const_rtx rtl)
11167 {
11168 unsigned regno = REGNO (rtl);
11169
11170 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11171
11172 #ifdef LEAF_REG_REMAP
11173 if (crtl->uses_only_leaf_regs)
11174 {
11175 int leaf_reg = LEAF_REG_REMAP (regno);
11176 if (leaf_reg != -1)
11177 regno = (unsigned) leaf_reg;
11178 }
11179 #endif
11180
11181 regno = DBX_REGISTER_NUMBER (regno);
11182 gcc_assert (regno != INVALID_REGNUM);
11183 return regno;
11184 }
11185
11186 /* Optionally add a DW_OP_piece term to a location description expression.
11187 DW_OP_piece is only added if the location description expression already
11188 doesn't end with DW_OP_piece. */
11189
11190 static void
11191 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11192 {
11193 dw_loc_descr_ref loc;
11194
11195 if (*list_head != NULL)
11196 {
11197 /* Find the end of the chain. */
11198 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11199 ;
11200
11201 if (loc->dw_loc_opc != DW_OP_piece)
11202 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11203 }
11204 }
11205
11206 /* Return a location descriptor that designates a machine register or
11207 zero if there is none. */
11208
11209 static dw_loc_descr_ref
11210 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11211 {
11212 rtx regs;
11213
11214 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11215 return 0;
11216
11217 /* We only use "frame base" when we're sure we're talking about the
11218 post-prologue local stack frame. We do this by *not* running
11219 register elimination until this point, and recognizing the special
11220 argument pointer and soft frame pointer rtx's.
11221 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11222 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11223 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11224 {
11225 dw_loc_descr_ref result = NULL;
11226
11227 if (dwarf_version >= 4 || !dwarf_strict)
11228 {
11229 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11230 initialized);
11231 if (result)
11232 add_loc_descr (&result,
11233 new_loc_descr (DW_OP_stack_value, 0, 0));
11234 }
11235 return result;
11236 }
11237
11238 regs = targetm.dwarf_register_span (rtl);
11239
11240 if (REG_NREGS (rtl) > 1 || regs)
11241 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11242 else
11243 {
11244 unsigned int dbx_regnum = dbx_reg_number (rtl);
11245 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11246 return 0;
11247 return one_reg_loc_descriptor (dbx_regnum, initialized);
11248 }
11249 }
11250
11251 /* Return a location descriptor that designates a machine register for
11252 a given hard register number. */
11253
11254 static dw_loc_descr_ref
11255 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11256 {
11257 dw_loc_descr_ref reg_loc_descr;
11258
11259 if (regno <= 31)
11260 reg_loc_descr
11261 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11262 else
11263 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11264
11265 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11266 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11267
11268 return reg_loc_descr;
11269 }
11270
11271 /* Given an RTL of a register, return a location descriptor that
11272 designates a value that spans more than one register. */
11273
11274 static dw_loc_descr_ref
11275 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11276 enum var_init_status initialized)
11277 {
11278 int size, i;
11279 dw_loc_descr_ref loc_result = NULL;
11280
11281 /* Simple, contiguous registers. */
11282 if (regs == NULL_RTX)
11283 {
11284 unsigned reg = REGNO (rtl);
11285 int nregs;
11286
11287 #ifdef LEAF_REG_REMAP
11288 if (crtl->uses_only_leaf_regs)
11289 {
11290 int leaf_reg = LEAF_REG_REMAP (reg);
11291 if (leaf_reg != -1)
11292 reg = (unsigned) leaf_reg;
11293 }
11294 #endif
11295
11296 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11297 nregs = REG_NREGS (rtl);
11298
11299 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11300
11301 loc_result = NULL;
11302 while (nregs--)
11303 {
11304 dw_loc_descr_ref t;
11305
11306 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11307 VAR_INIT_STATUS_INITIALIZED);
11308 add_loc_descr (&loc_result, t);
11309 add_loc_descr_op_piece (&loc_result, size);
11310 ++reg;
11311 }
11312 return loc_result;
11313 }
11314
11315 /* Now onto stupid register sets in non contiguous locations. */
11316
11317 gcc_assert (GET_CODE (regs) == PARALLEL);
11318
11319 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11320 loc_result = NULL;
11321
11322 for (i = 0; i < XVECLEN (regs, 0); ++i)
11323 {
11324 dw_loc_descr_ref t;
11325
11326 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11327 VAR_INIT_STATUS_INITIALIZED);
11328 add_loc_descr (&loc_result, t);
11329 add_loc_descr_op_piece (&loc_result, size);
11330 }
11331
11332 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11333 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11334 return loc_result;
11335 }
11336
11337 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11338
11339 /* Return a location descriptor that designates a constant i,
11340 as a compound operation from constant (i >> shift), constant shift
11341 and DW_OP_shl. */
11342
11343 static dw_loc_descr_ref
11344 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11345 {
11346 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11347 add_loc_descr (&ret, int_loc_descriptor (shift));
11348 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11349 return ret;
11350 }
11351
11352 /* Return a location descriptor that designates a constant. */
11353
11354 static dw_loc_descr_ref
11355 int_loc_descriptor (HOST_WIDE_INT i)
11356 {
11357 enum dwarf_location_atom op;
11358
11359 /* Pick the smallest representation of a constant, rather than just
11360 defaulting to the LEB encoding. */
11361 if (i >= 0)
11362 {
11363 int clz = clz_hwi (i);
11364 int ctz = ctz_hwi (i);
11365 if (i <= 31)
11366 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11367 else if (i <= 0xff)
11368 op = DW_OP_const1u;
11369 else if (i <= 0xffff)
11370 op = DW_OP_const2u;
11371 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11372 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11373 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11374 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11375 while DW_OP_const4u is 5 bytes. */
11376 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11377 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11378 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11379 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11380 while DW_OP_const4u is 5 bytes. */
11381 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11382 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11383 op = DW_OP_const4u;
11384 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11385 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11386 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11387 while DW_OP_constu of constant >= 0x100000000 takes at least
11388 6 bytes. */
11389 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11390 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11391 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11392 >= HOST_BITS_PER_WIDE_INT)
11393 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11394 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11395 while DW_OP_constu takes in this case at least 6 bytes. */
11396 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11397 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11398 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11399 && size_of_uleb128 (i) > 6)
11400 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11401 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11402 else
11403 op = DW_OP_constu;
11404 }
11405 else
11406 {
11407 if (i >= -0x80)
11408 op = DW_OP_const1s;
11409 else if (i >= -0x8000)
11410 op = DW_OP_const2s;
11411 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11412 {
11413 if (size_of_int_loc_descriptor (i) < 5)
11414 {
11415 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11416 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11417 return ret;
11418 }
11419 op = DW_OP_const4s;
11420 }
11421 else
11422 {
11423 if (size_of_int_loc_descriptor (i)
11424 < (unsigned long) 1 + size_of_sleb128 (i))
11425 {
11426 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11427 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11428 return ret;
11429 }
11430 op = DW_OP_consts;
11431 }
11432 }
11433
11434 return new_loc_descr (op, i, 0);
11435 }
11436
11437 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11438 without actually allocating it. */
11439
11440 static unsigned long
11441 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11442 {
11443 return size_of_int_loc_descriptor (i >> shift)
11444 + size_of_int_loc_descriptor (shift)
11445 + 1;
11446 }
11447
11448 /* Return size_of_locs (int_loc_descriptor (i)) without
11449 actually allocating it. */
11450
11451 static unsigned long
11452 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11453 {
11454 unsigned long s;
11455
11456 if (i >= 0)
11457 {
11458 int clz, ctz;
11459 if (i <= 31)
11460 return 1;
11461 else if (i <= 0xff)
11462 return 2;
11463 else if (i <= 0xffff)
11464 return 3;
11465 clz = clz_hwi (i);
11466 ctz = ctz_hwi (i);
11467 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11468 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11469 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11470 - clz - 5);
11471 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11472 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11473 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11474 - clz - 8);
11475 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11476 return 5;
11477 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11478 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11479 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11480 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11481 - clz - 8);
11482 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11483 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11484 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11485 - clz - 16);
11486 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11487 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11488 && s > 6)
11489 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11490 - clz - 32);
11491 else
11492 return 1 + s;
11493 }
11494 else
11495 {
11496 if (i >= -0x80)
11497 return 2;
11498 else if (i >= -0x8000)
11499 return 3;
11500 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11501 {
11502 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11503 {
11504 s = size_of_int_loc_descriptor (-i) + 1;
11505 if (s < 5)
11506 return s;
11507 }
11508 return 5;
11509 }
11510 else
11511 {
11512 unsigned long r = 1 + size_of_sleb128 (i);
11513 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11514 {
11515 s = size_of_int_loc_descriptor (-i) + 1;
11516 if (s < r)
11517 return s;
11518 }
11519 return r;
11520 }
11521 }
11522 }
11523
11524 /* Return loc description representing "address" of integer value.
11525 This can appear only as toplevel expression. */
11526
11527 static dw_loc_descr_ref
11528 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11529 {
11530 int litsize;
11531 dw_loc_descr_ref loc_result = NULL;
11532
11533 if (!(dwarf_version >= 4 || !dwarf_strict))
11534 return NULL;
11535
11536 litsize = size_of_int_loc_descriptor (i);
11537 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11538 is more compact. For DW_OP_stack_value we need:
11539 litsize + 1 (DW_OP_stack_value)
11540 and for DW_OP_implicit_value:
11541 1 (DW_OP_implicit_value) + 1 (length) + size. */
11542 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11543 {
11544 loc_result = int_loc_descriptor (i);
11545 add_loc_descr (&loc_result,
11546 new_loc_descr (DW_OP_stack_value, 0, 0));
11547 return loc_result;
11548 }
11549
11550 loc_result = new_loc_descr (DW_OP_implicit_value,
11551 size, 0);
11552 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11553 loc_result->dw_loc_oprnd2.v.val_int = i;
11554 return loc_result;
11555 }
11556
11557 /* Return a location descriptor that designates a base+offset location. */
11558
11559 static dw_loc_descr_ref
11560 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11561 enum var_init_status initialized)
11562 {
11563 unsigned int regno;
11564 dw_loc_descr_ref result;
11565 dw_fde_ref fde = cfun->fde;
11566
11567 /* We only use "frame base" when we're sure we're talking about the
11568 post-prologue local stack frame. We do this by *not* running
11569 register elimination until this point, and recognizing the special
11570 argument pointer and soft frame pointer rtx's. */
11571 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11572 {
11573 rtx elim = (ira_use_lra_p
11574 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11575 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11576
11577 if (elim != reg)
11578 {
11579 if (GET_CODE (elim) == PLUS)
11580 {
11581 offset += INTVAL (XEXP (elim, 1));
11582 elim = XEXP (elim, 0);
11583 }
11584 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11585 && (elim == hard_frame_pointer_rtx
11586 || elim == stack_pointer_rtx))
11587 || elim == (frame_pointer_needed
11588 ? hard_frame_pointer_rtx
11589 : stack_pointer_rtx));
11590
11591 /* If drap register is used to align stack, use frame
11592 pointer + offset to access stack variables. If stack
11593 is aligned without drap, use stack pointer + offset to
11594 access stack variables. */
11595 if (crtl->stack_realign_tried
11596 && reg == frame_pointer_rtx)
11597 {
11598 int base_reg
11599 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11600 ? HARD_FRAME_POINTER_REGNUM
11601 : REGNO (elim));
11602 return new_reg_loc_descr (base_reg, offset);
11603 }
11604
11605 gcc_assert (frame_pointer_fb_offset_valid);
11606 offset += frame_pointer_fb_offset;
11607 return new_loc_descr (DW_OP_fbreg, offset, 0);
11608 }
11609 }
11610
11611 regno = REGNO (reg);
11612 #ifdef LEAF_REG_REMAP
11613 if (crtl->uses_only_leaf_regs)
11614 {
11615 int leaf_reg = LEAF_REG_REMAP (regno);
11616 if (leaf_reg != -1)
11617 regno = (unsigned) leaf_reg;
11618 }
11619 #endif
11620 regno = DWARF_FRAME_REGNUM (regno);
11621
11622 if (!optimize && fde
11623 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11624 {
11625 /* Use cfa+offset to represent the location of arguments passed
11626 on the stack when drap is used to align stack.
11627 Only do this when not optimizing, for optimized code var-tracking
11628 is supposed to track where the arguments live and the register
11629 used as vdrap or drap in some spot might be used for something
11630 else in other part of the routine. */
11631 return new_loc_descr (DW_OP_fbreg, offset, 0);
11632 }
11633
11634 if (regno <= 31)
11635 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11636 offset, 0);
11637 else
11638 result = new_loc_descr (DW_OP_bregx, regno, offset);
11639
11640 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11641 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11642
11643 return result;
11644 }
11645
11646 /* Return true if this RTL expression describes a base+offset calculation. */
11647
11648 static inline int
11649 is_based_loc (const_rtx rtl)
11650 {
11651 return (GET_CODE (rtl) == PLUS
11652 && ((REG_P (XEXP (rtl, 0))
11653 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11654 && CONST_INT_P (XEXP (rtl, 1)))));
11655 }
11656
11657 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11658 failed. */
11659
11660 static dw_loc_descr_ref
11661 tls_mem_loc_descriptor (rtx mem)
11662 {
11663 tree base;
11664 dw_loc_descr_ref loc_result;
11665
11666 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11667 return NULL;
11668
11669 base = get_base_address (MEM_EXPR (mem));
11670 if (base == NULL
11671 || TREE_CODE (base) != VAR_DECL
11672 || !DECL_THREAD_LOCAL_P (base))
11673 return NULL;
11674
11675 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11676 if (loc_result == NULL)
11677 return NULL;
11678
11679 if (MEM_OFFSET (mem))
11680 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11681
11682 return loc_result;
11683 }
11684
11685 /* Output debug info about reason why we failed to expand expression as dwarf
11686 expression. */
11687
11688 static void
11689 expansion_failed (tree expr, rtx rtl, char const *reason)
11690 {
11691 if (dump_file && (dump_flags & TDF_DETAILS))
11692 {
11693 fprintf (dump_file, "Failed to expand as dwarf: ");
11694 if (expr)
11695 print_generic_expr (dump_file, expr, dump_flags);
11696 if (rtl)
11697 {
11698 fprintf (dump_file, "\n");
11699 print_rtl (dump_file, rtl);
11700 }
11701 fprintf (dump_file, "\nReason: %s\n", reason);
11702 }
11703 }
11704
11705 /* Helper function for const_ok_for_output. */
11706
11707 static bool
11708 const_ok_for_output_1 (rtx rtl)
11709 {
11710 if (GET_CODE (rtl) == UNSPEC)
11711 {
11712 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11713 we can't express it in the debug info. */
11714 #ifdef ENABLE_CHECKING
11715 /* Don't complain about TLS UNSPECs, those are just too hard to
11716 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11717 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11718 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11719 if (XVECLEN (rtl, 0) == 0
11720 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11721 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11722 inform (current_function_decl
11723 ? DECL_SOURCE_LOCATION (current_function_decl)
11724 : UNKNOWN_LOCATION,
11725 #if NUM_UNSPEC_VALUES > 0
11726 "non-delegitimized UNSPEC %s (%d) found in variable location",
11727 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11728 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11729 XINT (rtl, 1));
11730 #else
11731 "non-delegitimized UNSPEC %d found in variable location",
11732 XINT (rtl, 1));
11733 #endif
11734 #endif
11735 expansion_failed (NULL_TREE, rtl,
11736 "UNSPEC hasn't been delegitimized.\n");
11737 return false;
11738 }
11739
11740 if (targetm.const_not_ok_for_debug_p (rtl))
11741 {
11742 expansion_failed (NULL_TREE, rtl,
11743 "Expression rejected for debug by the backend.\n");
11744 return false;
11745 }
11746
11747 /* FIXME: Refer to PR60655. It is possible for simplification
11748 of rtl expressions in var tracking to produce such expressions.
11749 We should really identify / validate expressions
11750 enclosed in CONST that can be handled by assemblers on various
11751 targets and only handle legitimate cases here. */
11752 if (GET_CODE (rtl) != SYMBOL_REF)
11753 {
11754 if (GET_CODE (rtl) == NOT)
11755 return false;
11756 return true;
11757 }
11758
11759 if (CONSTANT_POOL_ADDRESS_P (rtl))
11760 {
11761 bool marked;
11762 get_pool_constant_mark (rtl, &marked);
11763 /* If all references to this pool constant were optimized away,
11764 it was not output and thus we can't represent it. */
11765 if (!marked)
11766 {
11767 expansion_failed (NULL_TREE, rtl,
11768 "Constant was removed from constant pool.\n");
11769 return false;
11770 }
11771 }
11772
11773 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11774 return false;
11775
11776 /* Avoid references to external symbols in debug info, on several targets
11777 the linker might even refuse to link when linking a shared library,
11778 and in many other cases the relocations for .debug_info/.debug_loc are
11779 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11780 to be defined within the same shared library or executable are fine. */
11781 if (SYMBOL_REF_EXTERNAL_P (rtl))
11782 {
11783 tree decl = SYMBOL_REF_DECL (rtl);
11784
11785 if (decl == NULL || !targetm.binds_local_p (decl))
11786 {
11787 expansion_failed (NULL_TREE, rtl,
11788 "Symbol not defined in current TU.\n");
11789 return false;
11790 }
11791 }
11792
11793 return true;
11794 }
11795
11796 /* Return true if constant RTL can be emitted in DW_OP_addr or
11797 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11798 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11799
11800 static bool
11801 const_ok_for_output (rtx rtl)
11802 {
11803 if (GET_CODE (rtl) == SYMBOL_REF)
11804 return const_ok_for_output_1 (rtl);
11805
11806 if (GET_CODE (rtl) == CONST)
11807 {
11808 subrtx_var_iterator::array_type array;
11809 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11810 if (!const_ok_for_output_1 (*iter))
11811 return false;
11812 return true;
11813 }
11814
11815 return true;
11816 }
11817
11818 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11819 if possible, NULL otherwise. */
11820
11821 static dw_die_ref
11822 base_type_for_mode (machine_mode mode, bool unsignedp)
11823 {
11824 dw_die_ref type_die;
11825 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11826
11827 if (type == NULL)
11828 return NULL;
11829 switch (TREE_CODE (type))
11830 {
11831 case INTEGER_TYPE:
11832 case REAL_TYPE:
11833 break;
11834 default:
11835 return NULL;
11836 }
11837 type_die = lookup_type_die (type);
11838 if (!type_die)
11839 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11840 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11841 return NULL;
11842 return type_die;
11843 }
11844
11845 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11846 type matching MODE, or, if MODE is narrower than or as wide as
11847 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11848 possible. */
11849
11850 static dw_loc_descr_ref
11851 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11852 {
11853 machine_mode outer_mode = mode;
11854 dw_die_ref type_die;
11855 dw_loc_descr_ref cvt;
11856
11857 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11858 {
11859 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11860 return op;
11861 }
11862 type_die = base_type_for_mode (outer_mode, 1);
11863 if (type_die == NULL)
11864 return NULL;
11865 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11866 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11867 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11868 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11869 add_loc_descr (&op, cvt);
11870 return op;
11871 }
11872
11873 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11874
11875 static dw_loc_descr_ref
11876 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11877 dw_loc_descr_ref op1)
11878 {
11879 dw_loc_descr_ref ret = op0;
11880 add_loc_descr (&ret, op1);
11881 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11882 if (STORE_FLAG_VALUE != 1)
11883 {
11884 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11885 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11886 }
11887 return ret;
11888 }
11889
11890 /* Return location descriptor for signed comparison OP RTL. */
11891
11892 static dw_loc_descr_ref
11893 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11894 machine_mode mem_mode)
11895 {
11896 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11897 dw_loc_descr_ref op0, op1;
11898 int shift;
11899
11900 if (op_mode == VOIDmode)
11901 op_mode = GET_MODE (XEXP (rtl, 1));
11902 if (op_mode == VOIDmode)
11903 return NULL;
11904
11905 if (dwarf_strict
11906 && (GET_MODE_CLASS (op_mode) != MODE_INT
11907 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11908 return NULL;
11909
11910 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11911 VAR_INIT_STATUS_INITIALIZED);
11912 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11913 VAR_INIT_STATUS_INITIALIZED);
11914
11915 if (op0 == NULL || op1 == NULL)
11916 return NULL;
11917
11918 if (GET_MODE_CLASS (op_mode) != MODE_INT
11919 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11920 return compare_loc_descriptor (op, op0, op1);
11921
11922 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11923 {
11924 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11925 dw_loc_descr_ref cvt;
11926
11927 if (type_die == NULL)
11928 return NULL;
11929 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11930 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11931 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11932 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11933 add_loc_descr (&op0, cvt);
11934 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11935 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11936 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11937 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11938 add_loc_descr (&op1, cvt);
11939 return compare_loc_descriptor (op, op0, op1);
11940 }
11941
11942 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11943 /* For eq/ne, if the operands are known to be zero-extended,
11944 there is no need to do the fancy shifting up. */
11945 if (op == DW_OP_eq || op == DW_OP_ne)
11946 {
11947 dw_loc_descr_ref last0, last1;
11948 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11949 ;
11950 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11951 ;
11952 /* deref_size zero extends, and for constants we can check
11953 whether they are zero extended or not. */
11954 if (((last0->dw_loc_opc == DW_OP_deref_size
11955 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11956 || (CONST_INT_P (XEXP (rtl, 0))
11957 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11958 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11959 && ((last1->dw_loc_opc == DW_OP_deref_size
11960 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11961 || (CONST_INT_P (XEXP (rtl, 1))
11962 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11963 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11964 return compare_loc_descriptor (op, op0, op1);
11965
11966 /* EQ/NE comparison against constant in narrower type than
11967 DWARF2_ADDR_SIZE can be performed either as
11968 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11969 DW_OP_{eq,ne}
11970 or
11971 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11972 DW_OP_{eq,ne}. Pick whatever is shorter. */
11973 if (CONST_INT_P (XEXP (rtl, 1))
11974 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11975 && (size_of_int_loc_descriptor (shift) + 1
11976 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11977 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11978 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11979 & GET_MODE_MASK (op_mode))))
11980 {
11981 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11982 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11983 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11984 & GET_MODE_MASK (op_mode));
11985 return compare_loc_descriptor (op, op0, op1);
11986 }
11987 }
11988 add_loc_descr (&op0, int_loc_descriptor (shift));
11989 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11990 if (CONST_INT_P (XEXP (rtl, 1)))
11991 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11992 else
11993 {
11994 add_loc_descr (&op1, int_loc_descriptor (shift));
11995 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11996 }
11997 return compare_loc_descriptor (op, op0, op1);
11998 }
11999
12000 /* Return location descriptor for unsigned comparison OP RTL. */
12001
12002 static dw_loc_descr_ref
12003 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12004 machine_mode mem_mode)
12005 {
12006 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12007 dw_loc_descr_ref op0, op1;
12008
12009 if (op_mode == VOIDmode)
12010 op_mode = GET_MODE (XEXP (rtl, 1));
12011 if (op_mode == VOIDmode)
12012 return NULL;
12013 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12014 return NULL;
12015
12016 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12017 return NULL;
12018
12019 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12020 VAR_INIT_STATUS_INITIALIZED);
12021 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12022 VAR_INIT_STATUS_INITIALIZED);
12023
12024 if (op0 == NULL || op1 == NULL)
12025 return NULL;
12026
12027 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12028 {
12029 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12030 dw_loc_descr_ref last0, last1;
12031 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12032 ;
12033 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12034 ;
12035 if (CONST_INT_P (XEXP (rtl, 0)))
12036 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12037 /* deref_size zero extends, so no need to mask it again. */
12038 else if (last0->dw_loc_opc != DW_OP_deref_size
12039 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12040 {
12041 add_loc_descr (&op0, int_loc_descriptor (mask));
12042 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12043 }
12044 if (CONST_INT_P (XEXP (rtl, 1)))
12045 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12046 /* deref_size zero extends, so no need to mask it again. */
12047 else if (last1->dw_loc_opc != DW_OP_deref_size
12048 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12049 {
12050 add_loc_descr (&op1, int_loc_descriptor (mask));
12051 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12052 }
12053 }
12054 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12055 {
12056 HOST_WIDE_INT bias = 1;
12057 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12058 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12059 if (CONST_INT_P (XEXP (rtl, 1)))
12060 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12061 + INTVAL (XEXP (rtl, 1)));
12062 else
12063 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12064 bias, 0));
12065 }
12066 return compare_loc_descriptor (op, op0, op1);
12067 }
12068
12069 /* Return location descriptor for {U,S}{MIN,MAX}. */
12070
12071 static dw_loc_descr_ref
12072 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12073 machine_mode mem_mode)
12074 {
12075 enum dwarf_location_atom op;
12076 dw_loc_descr_ref op0, op1, ret;
12077 dw_loc_descr_ref bra_node, drop_node;
12078
12079 if (dwarf_strict
12080 && (GET_MODE_CLASS (mode) != MODE_INT
12081 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12082 return NULL;
12083
12084 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12085 VAR_INIT_STATUS_INITIALIZED);
12086 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12087 VAR_INIT_STATUS_INITIALIZED);
12088
12089 if (op0 == NULL || op1 == NULL)
12090 return NULL;
12091
12092 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12093 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12094 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12095 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12096 {
12097 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12098 {
12099 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12100 add_loc_descr (&op0, int_loc_descriptor (mask));
12101 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12102 add_loc_descr (&op1, int_loc_descriptor (mask));
12103 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12104 }
12105 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12106 {
12107 HOST_WIDE_INT bias = 1;
12108 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12109 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12110 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12111 }
12112 }
12113 else if (GET_MODE_CLASS (mode) == MODE_INT
12114 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12115 {
12116 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12117 add_loc_descr (&op0, int_loc_descriptor (shift));
12118 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12119 add_loc_descr (&op1, int_loc_descriptor (shift));
12120 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12121 }
12122 else if (GET_MODE_CLASS (mode) == MODE_INT
12123 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12124 {
12125 dw_die_ref type_die = base_type_for_mode (mode, 0);
12126 dw_loc_descr_ref cvt;
12127 if (type_die == NULL)
12128 return NULL;
12129 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12130 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12131 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12132 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12133 add_loc_descr (&op0, cvt);
12134 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12135 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12136 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12137 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12138 add_loc_descr (&op1, cvt);
12139 }
12140
12141 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12142 op = DW_OP_lt;
12143 else
12144 op = DW_OP_gt;
12145 ret = op0;
12146 add_loc_descr (&ret, op1);
12147 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12148 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12149 add_loc_descr (&ret, bra_node);
12150 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12151 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12152 add_loc_descr (&ret, drop_node);
12153 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12154 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12155 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12156 && GET_MODE_CLASS (mode) == MODE_INT
12157 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12158 ret = convert_descriptor_to_mode (mode, ret);
12159 return ret;
12160 }
12161
12162 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12163 but after converting arguments to type_die, afterwards
12164 convert back to unsigned. */
12165
12166 static dw_loc_descr_ref
12167 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12168 machine_mode mode, machine_mode mem_mode)
12169 {
12170 dw_loc_descr_ref cvt, op0, op1;
12171
12172 if (type_die == NULL)
12173 return NULL;
12174 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12175 VAR_INIT_STATUS_INITIALIZED);
12176 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12177 VAR_INIT_STATUS_INITIALIZED);
12178 if (op0 == NULL || op1 == NULL)
12179 return NULL;
12180 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12181 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12182 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12183 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12184 add_loc_descr (&op0, cvt);
12185 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12186 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12187 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12188 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12189 add_loc_descr (&op1, cvt);
12190 add_loc_descr (&op0, op1);
12191 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12192 return convert_descriptor_to_mode (mode, op0);
12193 }
12194
12195 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12196 const0 is DW_OP_lit0 or corresponding typed constant,
12197 const1 is DW_OP_lit1 or corresponding typed constant
12198 and constMSB is constant with just the MSB bit set
12199 for the mode):
12200 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12201 L1: const0 DW_OP_swap
12202 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12203 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12204 L3: DW_OP_drop
12205 L4: DW_OP_nop
12206
12207 CTZ is similar:
12208 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12209 L1: const0 DW_OP_swap
12210 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12211 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12212 L3: DW_OP_drop
12213 L4: DW_OP_nop
12214
12215 FFS is similar:
12216 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12217 L1: const1 DW_OP_swap
12218 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12219 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12220 L3: DW_OP_drop
12221 L4: DW_OP_nop */
12222
12223 static dw_loc_descr_ref
12224 clz_loc_descriptor (rtx rtl, machine_mode mode,
12225 machine_mode mem_mode)
12226 {
12227 dw_loc_descr_ref op0, ret, tmp;
12228 HOST_WIDE_INT valv;
12229 dw_loc_descr_ref l1jump, l1label;
12230 dw_loc_descr_ref l2jump, l2label;
12231 dw_loc_descr_ref l3jump, l3label;
12232 dw_loc_descr_ref l4jump, l4label;
12233 rtx msb;
12234
12235 if (GET_MODE_CLASS (mode) != MODE_INT
12236 || GET_MODE (XEXP (rtl, 0)) != mode)
12237 return NULL;
12238
12239 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12240 VAR_INIT_STATUS_INITIALIZED);
12241 if (op0 == NULL)
12242 return NULL;
12243 ret = op0;
12244 if (GET_CODE (rtl) == CLZ)
12245 {
12246 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12247 valv = GET_MODE_BITSIZE (mode);
12248 }
12249 else if (GET_CODE (rtl) == FFS)
12250 valv = 0;
12251 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12252 valv = GET_MODE_BITSIZE (mode);
12253 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12254 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12255 add_loc_descr (&ret, l1jump);
12256 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12257 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12258 VAR_INIT_STATUS_INITIALIZED);
12259 if (tmp == NULL)
12260 return NULL;
12261 add_loc_descr (&ret, tmp);
12262 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12263 add_loc_descr (&ret, l4jump);
12264 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12265 ? const1_rtx : const0_rtx,
12266 mode, mem_mode,
12267 VAR_INIT_STATUS_INITIALIZED);
12268 if (l1label == NULL)
12269 return NULL;
12270 add_loc_descr (&ret, l1label);
12271 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12272 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12273 add_loc_descr (&ret, l2label);
12274 if (GET_CODE (rtl) != CLZ)
12275 msb = const1_rtx;
12276 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12277 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12278 << (GET_MODE_BITSIZE (mode) - 1));
12279 else
12280 msb = immed_wide_int_const
12281 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12282 GET_MODE_PRECISION (mode)), mode);
12283 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12284 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12285 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12286 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12287 else
12288 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12289 VAR_INIT_STATUS_INITIALIZED);
12290 if (tmp == NULL)
12291 return NULL;
12292 add_loc_descr (&ret, tmp);
12293 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12294 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12295 add_loc_descr (&ret, l3jump);
12296 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12297 VAR_INIT_STATUS_INITIALIZED);
12298 if (tmp == NULL)
12299 return NULL;
12300 add_loc_descr (&ret, tmp);
12301 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12302 ? DW_OP_shl : DW_OP_shr, 0, 0));
12303 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12304 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12305 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12306 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12307 add_loc_descr (&ret, l2jump);
12308 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12309 add_loc_descr (&ret, l3label);
12310 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12311 add_loc_descr (&ret, l4label);
12312 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12313 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12314 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12315 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12316 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12317 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12318 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12319 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12320 return ret;
12321 }
12322
12323 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12324 const1 is DW_OP_lit1 or corresponding typed constant):
12325 const0 DW_OP_swap
12326 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12327 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12328 L2: DW_OP_drop
12329
12330 PARITY is similar:
12331 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12332 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12333 L2: DW_OP_drop */
12334
12335 static dw_loc_descr_ref
12336 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12337 machine_mode mem_mode)
12338 {
12339 dw_loc_descr_ref op0, ret, tmp;
12340 dw_loc_descr_ref l1jump, l1label;
12341 dw_loc_descr_ref l2jump, l2label;
12342
12343 if (GET_MODE_CLASS (mode) != MODE_INT
12344 || GET_MODE (XEXP (rtl, 0)) != mode)
12345 return NULL;
12346
12347 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12348 VAR_INIT_STATUS_INITIALIZED);
12349 if (op0 == NULL)
12350 return NULL;
12351 ret = op0;
12352 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12353 VAR_INIT_STATUS_INITIALIZED);
12354 if (tmp == NULL)
12355 return NULL;
12356 add_loc_descr (&ret, tmp);
12357 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12358 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12359 add_loc_descr (&ret, l1label);
12360 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12361 add_loc_descr (&ret, l2jump);
12362 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12363 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12364 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12365 VAR_INIT_STATUS_INITIALIZED);
12366 if (tmp == NULL)
12367 return NULL;
12368 add_loc_descr (&ret, tmp);
12369 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12370 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12371 ? DW_OP_plus : DW_OP_xor, 0, 0));
12372 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12373 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12374 VAR_INIT_STATUS_INITIALIZED);
12375 add_loc_descr (&ret, tmp);
12376 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12377 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12378 add_loc_descr (&ret, l1jump);
12379 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12380 add_loc_descr (&ret, l2label);
12381 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12382 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12383 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12384 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12385 return ret;
12386 }
12387
12388 /* BSWAP (constS is initial shift count, either 56 or 24):
12389 constS const0
12390 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12391 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12392 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12393 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12394 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12395
12396 static dw_loc_descr_ref
12397 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12398 machine_mode mem_mode)
12399 {
12400 dw_loc_descr_ref op0, ret, tmp;
12401 dw_loc_descr_ref l1jump, l1label;
12402 dw_loc_descr_ref l2jump, l2label;
12403
12404 if (GET_MODE_CLASS (mode) != MODE_INT
12405 || BITS_PER_UNIT != 8
12406 || (GET_MODE_BITSIZE (mode) != 32
12407 && GET_MODE_BITSIZE (mode) != 64))
12408 return NULL;
12409
12410 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12411 VAR_INIT_STATUS_INITIALIZED);
12412 if (op0 == NULL)
12413 return NULL;
12414
12415 ret = op0;
12416 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12417 mode, mem_mode,
12418 VAR_INIT_STATUS_INITIALIZED);
12419 if (tmp == NULL)
12420 return NULL;
12421 add_loc_descr (&ret, tmp);
12422 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12423 VAR_INIT_STATUS_INITIALIZED);
12424 if (tmp == NULL)
12425 return NULL;
12426 add_loc_descr (&ret, tmp);
12427 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12428 add_loc_descr (&ret, l1label);
12429 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12430 mode, mem_mode,
12431 VAR_INIT_STATUS_INITIALIZED);
12432 add_loc_descr (&ret, tmp);
12433 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12434 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12435 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12436 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12437 VAR_INIT_STATUS_INITIALIZED);
12438 if (tmp == NULL)
12439 return NULL;
12440 add_loc_descr (&ret, tmp);
12441 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12442 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12443 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12444 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12445 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12446 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12447 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12448 VAR_INIT_STATUS_INITIALIZED);
12449 add_loc_descr (&ret, tmp);
12450 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12451 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12452 add_loc_descr (&ret, l2jump);
12453 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12454 VAR_INIT_STATUS_INITIALIZED);
12455 add_loc_descr (&ret, tmp);
12456 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12457 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12458 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12459 add_loc_descr (&ret, l1jump);
12460 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12461 add_loc_descr (&ret, l2label);
12462 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12463 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12464 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12465 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12466 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12467 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12468 return ret;
12469 }
12470
12471 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12472 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12473 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12474 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12475
12476 ROTATERT is similar:
12477 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12478 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12479 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12480
12481 static dw_loc_descr_ref
12482 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12483 machine_mode mem_mode)
12484 {
12485 rtx rtlop1 = XEXP (rtl, 1);
12486 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12487 int i;
12488
12489 if (GET_MODE_CLASS (mode) != MODE_INT)
12490 return NULL;
12491
12492 if (GET_MODE (rtlop1) != VOIDmode
12493 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12494 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12495 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12496 VAR_INIT_STATUS_INITIALIZED);
12497 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12498 VAR_INIT_STATUS_INITIALIZED);
12499 if (op0 == NULL || op1 == NULL)
12500 return NULL;
12501 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12502 for (i = 0; i < 2; i++)
12503 {
12504 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12505 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12506 mode, mem_mode,
12507 VAR_INIT_STATUS_INITIALIZED);
12508 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12509 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12510 ? DW_OP_const4u
12511 : HOST_BITS_PER_WIDE_INT == 64
12512 ? DW_OP_const8u : DW_OP_constu,
12513 GET_MODE_MASK (mode), 0);
12514 else
12515 mask[i] = NULL;
12516 if (mask[i] == NULL)
12517 return NULL;
12518 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12519 }
12520 ret = op0;
12521 add_loc_descr (&ret, op1);
12522 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12523 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12524 if (GET_CODE (rtl) == ROTATERT)
12525 {
12526 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12527 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12528 GET_MODE_BITSIZE (mode), 0));
12529 }
12530 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12531 if (mask[0] != NULL)
12532 add_loc_descr (&ret, mask[0]);
12533 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12534 if (mask[1] != NULL)
12535 {
12536 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12537 add_loc_descr (&ret, mask[1]);
12538 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12539 }
12540 if (GET_CODE (rtl) == ROTATE)
12541 {
12542 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12543 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12544 GET_MODE_BITSIZE (mode), 0));
12545 }
12546 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12547 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12548 return ret;
12549 }
12550
12551 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12552 for DEBUG_PARAMETER_REF RTL. */
12553
12554 static dw_loc_descr_ref
12555 parameter_ref_descriptor (rtx rtl)
12556 {
12557 dw_loc_descr_ref ret;
12558 dw_die_ref ref;
12559
12560 if (dwarf_strict)
12561 return NULL;
12562 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12563 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12564 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12565 if (ref)
12566 {
12567 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12568 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12569 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12570 }
12571 else
12572 {
12573 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12574 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12575 }
12576 return ret;
12577 }
12578
12579 /* The following routine converts the RTL for a variable or parameter
12580 (resident in memory) into an equivalent Dwarf representation of a
12581 mechanism for getting the address of that same variable onto the top of a
12582 hypothetical "address evaluation" stack.
12583
12584 When creating memory location descriptors, we are effectively transforming
12585 the RTL for a memory-resident object into its Dwarf postfix expression
12586 equivalent. This routine recursively descends an RTL tree, turning
12587 it into Dwarf postfix code as it goes.
12588
12589 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12590
12591 MEM_MODE is the mode of the memory reference, needed to handle some
12592 autoincrement addressing modes.
12593
12594 Return 0 if we can't represent the location. */
12595
12596 dw_loc_descr_ref
12597 mem_loc_descriptor (rtx rtl, machine_mode mode,
12598 machine_mode mem_mode,
12599 enum var_init_status initialized)
12600 {
12601 dw_loc_descr_ref mem_loc_result = NULL;
12602 enum dwarf_location_atom op;
12603 dw_loc_descr_ref op0, op1;
12604 rtx inner = NULL_RTX;
12605
12606 if (mode == VOIDmode)
12607 mode = GET_MODE (rtl);
12608
12609 /* Note that for a dynamically sized array, the location we will generate a
12610 description of here will be the lowest numbered location which is
12611 actually within the array. That's *not* necessarily the same as the
12612 zeroth element of the array. */
12613
12614 rtl = targetm.delegitimize_address (rtl);
12615
12616 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12617 return NULL;
12618
12619 switch (GET_CODE (rtl))
12620 {
12621 case POST_INC:
12622 case POST_DEC:
12623 case POST_MODIFY:
12624 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12625
12626 case SUBREG:
12627 /* The case of a subreg may arise when we have a local (register)
12628 variable or a formal (register) parameter which doesn't quite fill
12629 up an entire register. For now, just assume that it is
12630 legitimate to make the Dwarf info refer to the whole register which
12631 contains the given subreg. */
12632 if (!subreg_lowpart_p (rtl))
12633 break;
12634 inner = SUBREG_REG (rtl);
12635 case TRUNCATE:
12636 if (inner == NULL_RTX)
12637 inner = XEXP (rtl, 0);
12638 if (GET_MODE_CLASS (mode) == MODE_INT
12639 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12640 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12641 #ifdef POINTERS_EXTEND_UNSIGNED
12642 || (mode == Pmode && mem_mode != VOIDmode)
12643 #endif
12644 )
12645 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12646 {
12647 mem_loc_result = mem_loc_descriptor (inner,
12648 GET_MODE (inner),
12649 mem_mode, initialized);
12650 break;
12651 }
12652 if (dwarf_strict)
12653 break;
12654 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12655 break;
12656 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12657 && (GET_MODE_CLASS (mode) != MODE_INT
12658 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12659 break;
12660 else
12661 {
12662 dw_die_ref type_die;
12663 dw_loc_descr_ref cvt;
12664
12665 mem_loc_result = mem_loc_descriptor (inner,
12666 GET_MODE (inner),
12667 mem_mode, initialized);
12668 if (mem_loc_result == NULL)
12669 break;
12670 type_die = base_type_for_mode (mode,
12671 GET_MODE_CLASS (mode) == MODE_INT);
12672 if (type_die == NULL)
12673 {
12674 mem_loc_result = NULL;
12675 break;
12676 }
12677 if (GET_MODE_SIZE (mode)
12678 != GET_MODE_SIZE (GET_MODE (inner)))
12679 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12680 else
12681 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12682 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12683 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12684 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12685 add_loc_descr (&mem_loc_result, cvt);
12686 }
12687 break;
12688
12689 case REG:
12690 if (GET_MODE_CLASS (mode) != MODE_INT
12691 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12692 && rtl != arg_pointer_rtx
12693 && rtl != frame_pointer_rtx
12694 #ifdef POINTERS_EXTEND_UNSIGNED
12695 && (mode != Pmode || mem_mode == VOIDmode)
12696 #endif
12697 ))
12698 {
12699 dw_die_ref type_die;
12700 unsigned int dbx_regnum;
12701
12702 if (dwarf_strict)
12703 break;
12704 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12705 break;
12706 type_die = base_type_for_mode (mode,
12707 GET_MODE_CLASS (mode) == MODE_INT);
12708 if (type_die == NULL)
12709 break;
12710
12711 dbx_regnum = dbx_reg_number (rtl);
12712 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12713 break;
12714 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12715 dbx_regnum, 0);
12716 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12717 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12718 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12719 break;
12720 }
12721 /* Whenever a register number forms a part of the description of the
12722 method for calculating the (dynamic) address of a memory resident
12723 object, DWARF rules require the register number be referred to as
12724 a "base register". This distinction is not based in any way upon
12725 what category of register the hardware believes the given register
12726 belongs to. This is strictly DWARF terminology we're dealing with
12727 here. Note that in cases where the location of a memory-resident
12728 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12729 OP_CONST (0)) the actual DWARF location descriptor that we generate
12730 may just be OP_BASEREG (basereg). This may look deceptively like
12731 the object in question was allocated to a register (rather than in
12732 memory) so DWARF consumers need to be aware of the subtle
12733 distinction between OP_REG and OP_BASEREG. */
12734 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12735 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12736 else if (stack_realign_drap
12737 && crtl->drap_reg
12738 && crtl->args.internal_arg_pointer == rtl
12739 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12740 {
12741 /* If RTL is internal_arg_pointer, which has been optimized
12742 out, use DRAP instead. */
12743 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12744 VAR_INIT_STATUS_INITIALIZED);
12745 }
12746 break;
12747
12748 case SIGN_EXTEND:
12749 case ZERO_EXTEND:
12750 if (GET_MODE_CLASS (mode) != MODE_INT)
12751 break;
12752 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12753 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12754 if (op0 == 0)
12755 break;
12756 else if (GET_CODE (rtl) == ZERO_EXTEND
12757 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12758 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12759 < HOST_BITS_PER_WIDE_INT
12760 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12761 to expand zero extend as two shifts instead of
12762 masking. */
12763 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12764 {
12765 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12766 mem_loc_result = op0;
12767 add_loc_descr (&mem_loc_result,
12768 int_loc_descriptor (GET_MODE_MASK (imode)));
12769 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12770 }
12771 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12772 {
12773 int shift = DWARF2_ADDR_SIZE
12774 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12775 shift *= BITS_PER_UNIT;
12776 if (GET_CODE (rtl) == SIGN_EXTEND)
12777 op = DW_OP_shra;
12778 else
12779 op = DW_OP_shr;
12780 mem_loc_result = op0;
12781 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12782 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12783 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12784 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12785 }
12786 else if (!dwarf_strict)
12787 {
12788 dw_die_ref type_die1, type_die2;
12789 dw_loc_descr_ref cvt;
12790
12791 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12792 GET_CODE (rtl) == ZERO_EXTEND);
12793 if (type_die1 == NULL)
12794 break;
12795 type_die2 = base_type_for_mode (mode, 1);
12796 if (type_die2 == NULL)
12797 break;
12798 mem_loc_result = op0;
12799 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12800 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12801 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12802 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12803 add_loc_descr (&mem_loc_result, cvt);
12804 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12805 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12806 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12807 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12808 add_loc_descr (&mem_loc_result, cvt);
12809 }
12810 break;
12811
12812 case MEM:
12813 {
12814 rtx new_rtl = avoid_constant_pool_reference (rtl);
12815 if (new_rtl != rtl)
12816 {
12817 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12818 initialized);
12819 if (mem_loc_result != NULL)
12820 return mem_loc_result;
12821 }
12822 }
12823 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12824 get_address_mode (rtl), mode,
12825 VAR_INIT_STATUS_INITIALIZED);
12826 if (mem_loc_result == NULL)
12827 mem_loc_result = tls_mem_loc_descriptor (rtl);
12828 if (mem_loc_result != NULL)
12829 {
12830 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12831 || GET_MODE_CLASS (mode) != MODE_INT)
12832 {
12833 dw_die_ref type_die;
12834 dw_loc_descr_ref deref;
12835
12836 if (dwarf_strict)
12837 return NULL;
12838 type_die
12839 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12840 if (type_die == NULL)
12841 return NULL;
12842 deref = new_loc_descr (DW_OP_GNU_deref_type,
12843 GET_MODE_SIZE (mode), 0);
12844 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12845 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12846 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12847 add_loc_descr (&mem_loc_result, deref);
12848 }
12849 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12850 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12851 else
12852 add_loc_descr (&mem_loc_result,
12853 new_loc_descr (DW_OP_deref_size,
12854 GET_MODE_SIZE (mode), 0));
12855 }
12856 break;
12857
12858 case LO_SUM:
12859 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12860
12861 case LABEL_REF:
12862 /* Some ports can transform a symbol ref into a label ref, because
12863 the symbol ref is too far away and has to be dumped into a constant
12864 pool. */
12865 case CONST:
12866 case SYMBOL_REF:
12867 if ((GET_MODE_CLASS (mode) != MODE_INT
12868 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12869 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12870 #ifdef POINTERS_EXTEND_UNSIGNED
12871 && (mode != Pmode || mem_mode == VOIDmode)
12872 #endif
12873 ))
12874 break;
12875 if (GET_CODE (rtl) == SYMBOL_REF
12876 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12877 {
12878 dw_loc_descr_ref temp;
12879
12880 /* If this is not defined, we have no way to emit the data. */
12881 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12882 break;
12883
12884 temp = new_addr_loc_descr (rtl, dtprel_true);
12885
12886 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12887 add_loc_descr (&mem_loc_result, temp);
12888
12889 break;
12890 }
12891
12892 if (!const_ok_for_output (rtl))
12893 {
12894 if (GET_CODE (rtl) == CONST)
12895 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12896 initialized);
12897 break;
12898 }
12899
12900 symref:
12901 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12902 vec_safe_push (used_rtx_array, rtl);
12903 break;
12904
12905 case CONCAT:
12906 case CONCATN:
12907 case VAR_LOCATION:
12908 case DEBUG_IMPLICIT_PTR:
12909 expansion_failed (NULL_TREE, rtl,
12910 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12911 return 0;
12912
12913 case ENTRY_VALUE:
12914 if (dwarf_strict)
12915 return NULL;
12916 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12917 {
12918 if (GET_MODE_CLASS (mode) != MODE_INT
12919 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12920 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12921 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12922 else
12923 {
12924 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12925 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12926 return NULL;
12927 op0 = one_reg_loc_descriptor (dbx_regnum,
12928 VAR_INIT_STATUS_INITIALIZED);
12929 }
12930 }
12931 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12932 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12933 {
12934 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12935 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12936 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12937 return NULL;
12938 }
12939 else
12940 gcc_unreachable ();
12941 if (op0 == NULL)
12942 return NULL;
12943 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12944 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12945 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12946 break;
12947
12948 case DEBUG_PARAMETER_REF:
12949 mem_loc_result = parameter_ref_descriptor (rtl);
12950 break;
12951
12952 case PRE_MODIFY:
12953 /* Extract the PLUS expression nested inside and fall into
12954 PLUS code below. */
12955 rtl = XEXP (rtl, 1);
12956 goto plus;
12957
12958 case PRE_INC:
12959 case PRE_DEC:
12960 /* Turn these into a PLUS expression and fall into the PLUS code
12961 below. */
12962 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12963 gen_int_mode (GET_CODE (rtl) == PRE_INC
12964 ? GET_MODE_UNIT_SIZE (mem_mode)
12965 : -GET_MODE_UNIT_SIZE (mem_mode),
12966 mode));
12967
12968 /* ... fall through ... */
12969
12970 case PLUS:
12971 plus:
12972 if (is_based_loc (rtl)
12973 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12974 || XEXP (rtl, 0) == arg_pointer_rtx
12975 || XEXP (rtl, 0) == frame_pointer_rtx)
12976 && GET_MODE_CLASS (mode) == MODE_INT)
12977 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12978 INTVAL (XEXP (rtl, 1)),
12979 VAR_INIT_STATUS_INITIALIZED);
12980 else
12981 {
12982 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12983 VAR_INIT_STATUS_INITIALIZED);
12984 if (mem_loc_result == 0)
12985 break;
12986
12987 if (CONST_INT_P (XEXP (rtl, 1))
12988 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12989 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12990 else
12991 {
12992 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12993 VAR_INIT_STATUS_INITIALIZED);
12994 if (op1 == 0)
12995 return NULL;
12996 add_loc_descr (&mem_loc_result, op1);
12997 add_loc_descr (&mem_loc_result,
12998 new_loc_descr (DW_OP_plus, 0, 0));
12999 }
13000 }
13001 break;
13002
13003 /* If a pseudo-reg is optimized away, it is possible for it to
13004 be replaced with a MEM containing a multiply or shift. */
13005 case MINUS:
13006 op = DW_OP_minus;
13007 goto do_binop;
13008
13009 case MULT:
13010 op = DW_OP_mul;
13011 goto do_binop;
13012
13013 case DIV:
13014 if (!dwarf_strict
13015 && GET_MODE_CLASS (mode) == MODE_INT
13016 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13017 {
13018 mem_loc_result = typed_binop (DW_OP_div, rtl,
13019 base_type_for_mode (mode, 0),
13020 mode, mem_mode);
13021 break;
13022 }
13023 op = DW_OP_div;
13024 goto do_binop;
13025
13026 case UMOD:
13027 op = DW_OP_mod;
13028 goto do_binop;
13029
13030 case ASHIFT:
13031 op = DW_OP_shl;
13032 goto do_shift;
13033
13034 case ASHIFTRT:
13035 op = DW_OP_shra;
13036 goto do_shift;
13037
13038 case LSHIFTRT:
13039 op = DW_OP_shr;
13040 goto do_shift;
13041
13042 do_shift:
13043 if (GET_MODE_CLASS (mode) != MODE_INT)
13044 break;
13045 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13046 VAR_INIT_STATUS_INITIALIZED);
13047 {
13048 rtx rtlop1 = XEXP (rtl, 1);
13049 if (GET_MODE (rtlop1) != VOIDmode
13050 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13051 < GET_MODE_BITSIZE (mode))
13052 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13053 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13054 VAR_INIT_STATUS_INITIALIZED);
13055 }
13056
13057 if (op0 == 0 || op1 == 0)
13058 break;
13059
13060 mem_loc_result = op0;
13061 add_loc_descr (&mem_loc_result, op1);
13062 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13063 break;
13064
13065 case AND:
13066 op = DW_OP_and;
13067 goto do_binop;
13068
13069 case IOR:
13070 op = DW_OP_or;
13071 goto do_binop;
13072
13073 case XOR:
13074 op = DW_OP_xor;
13075 goto do_binop;
13076
13077 do_binop:
13078 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13079 VAR_INIT_STATUS_INITIALIZED);
13080 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13081 VAR_INIT_STATUS_INITIALIZED);
13082
13083 if (op0 == 0 || op1 == 0)
13084 break;
13085
13086 mem_loc_result = op0;
13087 add_loc_descr (&mem_loc_result, op1);
13088 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13089 break;
13090
13091 case MOD:
13092 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13093 {
13094 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13095 base_type_for_mode (mode, 0),
13096 mode, mem_mode);
13097 break;
13098 }
13099
13100 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13101 VAR_INIT_STATUS_INITIALIZED);
13102 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13103 VAR_INIT_STATUS_INITIALIZED);
13104
13105 if (op0 == 0 || op1 == 0)
13106 break;
13107
13108 mem_loc_result = op0;
13109 add_loc_descr (&mem_loc_result, op1);
13110 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13111 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13112 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13113 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13114 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13115 break;
13116
13117 case UDIV:
13118 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13119 {
13120 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13121 {
13122 op = DW_OP_div;
13123 goto do_binop;
13124 }
13125 mem_loc_result = typed_binop (DW_OP_div, rtl,
13126 base_type_for_mode (mode, 1),
13127 mode, mem_mode);
13128 }
13129 break;
13130
13131 case NOT:
13132 op = DW_OP_not;
13133 goto do_unop;
13134
13135 case ABS:
13136 op = DW_OP_abs;
13137 goto do_unop;
13138
13139 case NEG:
13140 op = DW_OP_neg;
13141 goto do_unop;
13142
13143 do_unop:
13144 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13145 VAR_INIT_STATUS_INITIALIZED);
13146
13147 if (op0 == 0)
13148 break;
13149
13150 mem_loc_result = op0;
13151 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13152 break;
13153
13154 case CONST_INT:
13155 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13156 #ifdef POINTERS_EXTEND_UNSIGNED
13157 || (mode == Pmode
13158 && mem_mode != VOIDmode
13159 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13160 #endif
13161 )
13162 {
13163 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13164 break;
13165 }
13166 if (!dwarf_strict
13167 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13168 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13169 {
13170 dw_die_ref type_die = base_type_for_mode (mode, 1);
13171 machine_mode amode;
13172 if (type_die == NULL)
13173 return NULL;
13174 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13175 MODE_INT, 0);
13176 if (INTVAL (rtl) >= 0
13177 && amode != BLKmode
13178 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13179 /* const DW_OP_GNU_convert <XXX> vs.
13180 DW_OP_GNU_const_type <XXX, 1, const>. */
13181 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13182 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13183 {
13184 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13185 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13186 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13187 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13188 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13189 add_loc_descr (&mem_loc_result, op0);
13190 return mem_loc_result;
13191 }
13192 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13193 INTVAL (rtl));
13194 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13195 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13196 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13197 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13198 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13199 else
13200 {
13201 mem_loc_result->dw_loc_oprnd2.val_class
13202 = dw_val_class_const_double;
13203 mem_loc_result->dw_loc_oprnd2.v.val_double
13204 = double_int::from_shwi (INTVAL (rtl));
13205 }
13206 }
13207 break;
13208
13209 case CONST_DOUBLE:
13210 if (!dwarf_strict)
13211 {
13212 dw_die_ref type_die;
13213
13214 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13215 CONST_DOUBLE rtx could represent either a large integer
13216 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13217 the value is always a floating point constant.
13218
13219 When it is an integer, a CONST_DOUBLE is used whenever
13220 the constant requires 2 HWIs to be adequately represented.
13221 We output CONST_DOUBLEs as blocks. */
13222 if (mode == VOIDmode
13223 || (GET_MODE (rtl) == VOIDmode
13224 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13225 break;
13226 type_die = base_type_for_mode (mode,
13227 GET_MODE_CLASS (mode) == MODE_INT);
13228 if (type_die == NULL)
13229 return NULL;
13230 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13231 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13232 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13233 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13234 #if TARGET_SUPPORTS_WIDE_INT == 0
13235 if (!SCALAR_FLOAT_MODE_P (mode))
13236 {
13237 mem_loc_result->dw_loc_oprnd2.val_class
13238 = dw_val_class_const_double;
13239 mem_loc_result->dw_loc_oprnd2.v.val_double
13240 = rtx_to_double_int (rtl);
13241 }
13242 else
13243 #endif
13244 {
13245 unsigned int length = GET_MODE_SIZE (mode);
13246 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13247
13248 insert_float (rtl, array);
13249 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13250 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13251 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13252 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13253 }
13254 }
13255 break;
13256
13257 case CONST_WIDE_INT:
13258 if (!dwarf_strict)
13259 {
13260 dw_die_ref type_die;
13261
13262 type_die = base_type_for_mode (mode,
13263 GET_MODE_CLASS (mode) == MODE_INT);
13264 if (type_die == NULL)
13265 return NULL;
13266 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13267 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13268 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13269 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13270 mem_loc_result->dw_loc_oprnd2.val_class
13271 = dw_val_class_wide_int;
13272 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13273 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13274 }
13275 break;
13276
13277 case EQ:
13278 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13279 break;
13280
13281 case GE:
13282 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13283 break;
13284
13285 case GT:
13286 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13287 break;
13288
13289 case LE:
13290 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13291 break;
13292
13293 case LT:
13294 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13295 break;
13296
13297 case NE:
13298 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13299 break;
13300
13301 case GEU:
13302 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13303 break;
13304
13305 case GTU:
13306 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13307 break;
13308
13309 case LEU:
13310 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13311 break;
13312
13313 case LTU:
13314 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13315 break;
13316
13317 case UMIN:
13318 case UMAX:
13319 if (GET_MODE_CLASS (mode) != MODE_INT)
13320 break;
13321 /* FALLTHRU */
13322 case SMIN:
13323 case SMAX:
13324 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13325 break;
13326
13327 case ZERO_EXTRACT:
13328 case SIGN_EXTRACT:
13329 if (CONST_INT_P (XEXP (rtl, 1))
13330 && CONST_INT_P (XEXP (rtl, 2))
13331 && ((unsigned) INTVAL (XEXP (rtl, 1))
13332 + (unsigned) INTVAL (XEXP (rtl, 2))
13333 <= GET_MODE_BITSIZE (mode))
13334 && GET_MODE_CLASS (mode) == MODE_INT
13335 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13336 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13337 {
13338 int shift, size;
13339 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13340 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13341 if (op0 == 0)
13342 break;
13343 if (GET_CODE (rtl) == SIGN_EXTRACT)
13344 op = DW_OP_shra;
13345 else
13346 op = DW_OP_shr;
13347 mem_loc_result = op0;
13348 size = INTVAL (XEXP (rtl, 1));
13349 shift = INTVAL (XEXP (rtl, 2));
13350 if (BITS_BIG_ENDIAN)
13351 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13352 - shift - size;
13353 if (shift + size != (int) DWARF2_ADDR_SIZE)
13354 {
13355 add_loc_descr (&mem_loc_result,
13356 int_loc_descriptor (DWARF2_ADDR_SIZE
13357 - shift - size));
13358 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13359 }
13360 if (size != (int) DWARF2_ADDR_SIZE)
13361 {
13362 add_loc_descr (&mem_loc_result,
13363 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13364 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13365 }
13366 }
13367 break;
13368
13369 case IF_THEN_ELSE:
13370 {
13371 dw_loc_descr_ref op2, bra_node, drop_node;
13372 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13373 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13374 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13375 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13376 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13377 VAR_INIT_STATUS_INITIALIZED);
13378 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13379 VAR_INIT_STATUS_INITIALIZED);
13380 if (op0 == NULL || op1 == NULL || op2 == NULL)
13381 break;
13382
13383 mem_loc_result = op1;
13384 add_loc_descr (&mem_loc_result, op2);
13385 add_loc_descr (&mem_loc_result, op0);
13386 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13387 add_loc_descr (&mem_loc_result, bra_node);
13388 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13389 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13390 add_loc_descr (&mem_loc_result, drop_node);
13391 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13392 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13393 }
13394 break;
13395
13396 case FLOAT_EXTEND:
13397 case FLOAT_TRUNCATE:
13398 case FLOAT:
13399 case UNSIGNED_FLOAT:
13400 case FIX:
13401 case UNSIGNED_FIX:
13402 if (!dwarf_strict)
13403 {
13404 dw_die_ref type_die;
13405 dw_loc_descr_ref cvt;
13406
13407 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13408 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13409 if (op0 == NULL)
13410 break;
13411 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13412 && (GET_CODE (rtl) == FLOAT
13413 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13414 <= DWARF2_ADDR_SIZE))
13415 {
13416 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13417 GET_CODE (rtl) == UNSIGNED_FLOAT);
13418 if (type_die == NULL)
13419 break;
13420 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13421 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13422 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13423 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13424 add_loc_descr (&op0, cvt);
13425 }
13426 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13427 if (type_die == NULL)
13428 break;
13429 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13430 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13431 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13432 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13433 add_loc_descr (&op0, cvt);
13434 if (GET_MODE_CLASS (mode) == MODE_INT
13435 && (GET_CODE (rtl) == FIX
13436 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13437 {
13438 op0 = convert_descriptor_to_mode (mode, op0);
13439 if (op0 == NULL)
13440 break;
13441 }
13442 mem_loc_result = op0;
13443 }
13444 break;
13445
13446 case CLZ:
13447 case CTZ:
13448 case FFS:
13449 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13450 break;
13451
13452 case POPCOUNT:
13453 case PARITY:
13454 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13455 break;
13456
13457 case BSWAP:
13458 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13459 break;
13460
13461 case ROTATE:
13462 case ROTATERT:
13463 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13464 break;
13465
13466 case COMPARE:
13467 /* In theory, we could implement the above. */
13468 /* DWARF cannot represent the unsigned compare operations
13469 natively. */
13470 case SS_MULT:
13471 case US_MULT:
13472 case SS_DIV:
13473 case US_DIV:
13474 case SS_PLUS:
13475 case US_PLUS:
13476 case SS_MINUS:
13477 case US_MINUS:
13478 case SS_NEG:
13479 case US_NEG:
13480 case SS_ABS:
13481 case SS_ASHIFT:
13482 case US_ASHIFT:
13483 case SS_TRUNCATE:
13484 case US_TRUNCATE:
13485 case UNORDERED:
13486 case ORDERED:
13487 case UNEQ:
13488 case UNGE:
13489 case UNGT:
13490 case UNLE:
13491 case UNLT:
13492 case LTGT:
13493 case FRACT_CONVERT:
13494 case UNSIGNED_FRACT_CONVERT:
13495 case SAT_FRACT:
13496 case UNSIGNED_SAT_FRACT:
13497 case SQRT:
13498 case ASM_OPERANDS:
13499 case VEC_MERGE:
13500 case VEC_SELECT:
13501 case VEC_CONCAT:
13502 case VEC_DUPLICATE:
13503 case UNSPEC:
13504 case HIGH:
13505 case FMA:
13506 case STRICT_LOW_PART:
13507 case CONST_VECTOR:
13508 case CONST_FIXED:
13509 case CLRSB:
13510 case CLOBBER:
13511 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13512 can't express it in the debug info. This can happen e.g. with some
13513 TLS UNSPECs. */
13514 break;
13515
13516 case CONST_STRING:
13517 resolve_one_addr (&rtl);
13518 goto symref;
13519
13520 default:
13521 #ifdef ENABLE_CHECKING
13522 print_rtl (stderr, rtl);
13523 gcc_unreachable ();
13524 #else
13525 break;
13526 #endif
13527 }
13528
13529 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13530 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13531
13532 return mem_loc_result;
13533 }
13534
13535 /* Return a descriptor that describes the concatenation of two locations.
13536 This is typically a complex variable. */
13537
13538 static dw_loc_descr_ref
13539 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13540 {
13541 dw_loc_descr_ref cc_loc_result = NULL;
13542 dw_loc_descr_ref x0_ref
13543 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13544 dw_loc_descr_ref x1_ref
13545 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13546
13547 if (x0_ref == 0 || x1_ref == 0)
13548 return 0;
13549
13550 cc_loc_result = x0_ref;
13551 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13552
13553 add_loc_descr (&cc_loc_result, x1_ref);
13554 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13555
13556 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13557 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13558
13559 return cc_loc_result;
13560 }
13561
13562 /* Return a descriptor that describes the concatenation of N
13563 locations. */
13564
13565 static dw_loc_descr_ref
13566 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13567 {
13568 unsigned int i;
13569 dw_loc_descr_ref cc_loc_result = NULL;
13570 unsigned int n = XVECLEN (concatn, 0);
13571
13572 for (i = 0; i < n; ++i)
13573 {
13574 dw_loc_descr_ref ref;
13575 rtx x = XVECEXP (concatn, 0, i);
13576
13577 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13578 if (ref == NULL)
13579 return NULL;
13580
13581 add_loc_descr (&cc_loc_result, ref);
13582 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13583 }
13584
13585 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13586 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13587
13588 return cc_loc_result;
13589 }
13590
13591 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13592 for DEBUG_IMPLICIT_PTR RTL. */
13593
13594 static dw_loc_descr_ref
13595 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13596 {
13597 dw_loc_descr_ref ret;
13598 dw_die_ref ref;
13599
13600 if (dwarf_strict)
13601 return NULL;
13602 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13603 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13604 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13605 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13606 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13607 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13608 if (ref)
13609 {
13610 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13611 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13612 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13613 }
13614 else
13615 {
13616 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13617 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13618 }
13619 return ret;
13620 }
13621
13622 /* Output a proper Dwarf location descriptor for a variable or parameter
13623 which is either allocated in a register or in a memory location. For a
13624 register, we just generate an OP_REG and the register number. For a
13625 memory location we provide a Dwarf postfix expression describing how to
13626 generate the (dynamic) address of the object onto the address stack.
13627
13628 MODE is mode of the decl if this loc_descriptor is going to be used in
13629 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13630 allowed, VOIDmode otherwise.
13631
13632 If we don't know how to describe it, return 0. */
13633
13634 static dw_loc_descr_ref
13635 loc_descriptor (rtx rtl, machine_mode mode,
13636 enum var_init_status initialized)
13637 {
13638 dw_loc_descr_ref loc_result = NULL;
13639
13640 switch (GET_CODE (rtl))
13641 {
13642 case SUBREG:
13643 /* The case of a subreg may arise when we have a local (register)
13644 variable or a formal (register) parameter which doesn't quite fill
13645 up an entire register. For now, just assume that it is
13646 legitimate to make the Dwarf info refer to the whole register which
13647 contains the given subreg. */
13648 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13649 loc_result = loc_descriptor (SUBREG_REG (rtl),
13650 GET_MODE (SUBREG_REG (rtl)), initialized);
13651 else
13652 goto do_default;
13653 break;
13654
13655 case REG:
13656 loc_result = reg_loc_descriptor (rtl, initialized);
13657 break;
13658
13659 case MEM:
13660 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13661 GET_MODE (rtl), initialized);
13662 if (loc_result == NULL)
13663 loc_result = tls_mem_loc_descriptor (rtl);
13664 if (loc_result == NULL)
13665 {
13666 rtx new_rtl = avoid_constant_pool_reference (rtl);
13667 if (new_rtl != rtl)
13668 loc_result = loc_descriptor (new_rtl, mode, initialized);
13669 }
13670 break;
13671
13672 case CONCAT:
13673 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13674 initialized);
13675 break;
13676
13677 case CONCATN:
13678 loc_result = concatn_loc_descriptor (rtl, initialized);
13679 break;
13680
13681 case VAR_LOCATION:
13682 /* Single part. */
13683 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13684 {
13685 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13686 if (GET_CODE (loc) == EXPR_LIST)
13687 loc = XEXP (loc, 0);
13688 loc_result = loc_descriptor (loc, mode, initialized);
13689 break;
13690 }
13691
13692 rtl = XEXP (rtl, 1);
13693 /* FALLTHRU */
13694
13695 case PARALLEL:
13696 {
13697 rtvec par_elems = XVEC (rtl, 0);
13698 int num_elem = GET_NUM_ELEM (par_elems);
13699 machine_mode mode;
13700 int i;
13701
13702 /* Create the first one, so we have something to add to. */
13703 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13704 VOIDmode, initialized);
13705 if (loc_result == NULL)
13706 return NULL;
13707 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13708 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13709 for (i = 1; i < num_elem; i++)
13710 {
13711 dw_loc_descr_ref temp;
13712
13713 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13714 VOIDmode, initialized);
13715 if (temp == NULL)
13716 return NULL;
13717 add_loc_descr (&loc_result, temp);
13718 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13719 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13720 }
13721 }
13722 break;
13723
13724 case CONST_INT:
13725 if (mode != VOIDmode && mode != BLKmode)
13726 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13727 INTVAL (rtl));
13728 break;
13729
13730 case CONST_DOUBLE:
13731 if (mode == VOIDmode)
13732 mode = GET_MODE (rtl);
13733
13734 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13735 {
13736 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13737
13738 /* Note that a CONST_DOUBLE rtx could represent either an integer
13739 or a floating-point constant. A CONST_DOUBLE is used whenever
13740 the constant requires more than one word in order to be
13741 adequately represented. We output CONST_DOUBLEs as blocks. */
13742 loc_result = new_loc_descr (DW_OP_implicit_value,
13743 GET_MODE_SIZE (mode), 0);
13744 #if TARGET_SUPPORTS_WIDE_INT == 0
13745 if (!SCALAR_FLOAT_MODE_P (mode))
13746 {
13747 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13748 loc_result->dw_loc_oprnd2.v.val_double
13749 = rtx_to_double_int (rtl);
13750 }
13751 else
13752 #endif
13753 {
13754 unsigned int length = GET_MODE_SIZE (mode);
13755 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13756
13757 insert_float (rtl, array);
13758 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13759 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13760 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13761 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13762 }
13763 }
13764 break;
13765
13766 case CONST_WIDE_INT:
13767 if (mode == VOIDmode)
13768 mode = GET_MODE (rtl);
13769
13770 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13771 {
13772 loc_result = new_loc_descr (DW_OP_implicit_value,
13773 GET_MODE_SIZE (mode), 0);
13774 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13775 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13776 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13777 }
13778 break;
13779
13780 case CONST_VECTOR:
13781 if (mode == VOIDmode)
13782 mode = GET_MODE (rtl);
13783
13784 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13785 {
13786 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13787 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13788 unsigned char *array
13789 = ggc_vec_alloc<unsigned char> (length * elt_size);
13790 unsigned int i;
13791 unsigned char *p;
13792 machine_mode imode = GET_MODE_INNER (mode);
13793
13794 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13795 switch (GET_MODE_CLASS (mode))
13796 {
13797 case MODE_VECTOR_INT:
13798 for (i = 0, p = array; i < length; i++, p += elt_size)
13799 {
13800 rtx elt = CONST_VECTOR_ELT (rtl, i);
13801 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13802 }
13803 break;
13804
13805 case MODE_VECTOR_FLOAT:
13806 for (i = 0, p = array; i < length; i++, p += elt_size)
13807 {
13808 rtx elt = CONST_VECTOR_ELT (rtl, i);
13809 insert_float (elt, p);
13810 }
13811 break;
13812
13813 default:
13814 gcc_unreachable ();
13815 }
13816
13817 loc_result = new_loc_descr (DW_OP_implicit_value,
13818 length * elt_size, 0);
13819 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13820 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13821 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13822 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13823 }
13824 break;
13825
13826 case CONST:
13827 if (mode == VOIDmode
13828 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13829 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13830 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13831 {
13832 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13833 break;
13834 }
13835 /* FALLTHROUGH */
13836 case SYMBOL_REF:
13837 if (!const_ok_for_output (rtl))
13838 break;
13839 case LABEL_REF:
13840 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13841 && (dwarf_version >= 4 || !dwarf_strict))
13842 {
13843 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13844 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13845 vec_safe_push (used_rtx_array, rtl);
13846 }
13847 break;
13848
13849 case DEBUG_IMPLICIT_PTR:
13850 loc_result = implicit_ptr_descriptor (rtl, 0);
13851 break;
13852
13853 case PLUS:
13854 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13855 && CONST_INT_P (XEXP (rtl, 1)))
13856 {
13857 loc_result
13858 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13859 break;
13860 }
13861 /* FALLTHRU */
13862 do_default:
13863 default:
13864 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13865 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13866 && dwarf_version >= 4)
13867 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13868 {
13869 /* Value expression. */
13870 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13871 if (loc_result)
13872 add_loc_descr (&loc_result,
13873 new_loc_descr (DW_OP_stack_value, 0, 0));
13874 }
13875 break;
13876 }
13877
13878 return loc_result;
13879 }
13880
13881 /* We need to figure out what section we should use as the base for the
13882 address ranges where a given location is valid.
13883 1. If this particular DECL has a section associated with it, use that.
13884 2. If this function has a section associated with it, use that.
13885 3. Otherwise, use the text section.
13886 XXX: If you split a variable across multiple sections, we won't notice. */
13887
13888 static const char *
13889 secname_for_decl (const_tree decl)
13890 {
13891 const char *secname;
13892
13893 if (VAR_OR_FUNCTION_DECL_P (decl)
13894 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13895 && DECL_SECTION_NAME (decl))
13896 secname = DECL_SECTION_NAME (decl);
13897 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13898 secname = DECL_SECTION_NAME (current_function_decl);
13899 else if (cfun && in_cold_section_p)
13900 secname = crtl->subsections.cold_section_label;
13901 else
13902 secname = text_section_label;
13903
13904 return secname;
13905 }
13906
13907 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13908
13909 static bool
13910 decl_by_reference_p (tree decl)
13911 {
13912 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13913 || TREE_CODE (decl) == VAR_DECL)
13914 && DECL_BY_REFERENCE (decl));
13915 }
13916
13917 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13918 for VARLOC. */
13919
13920 static dw_loc_descr_ref
13921 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13922 enum var_init_status initialized)
13923 {
13924 int have_address = 0;
13925 dw_loc_descr_ref descr;
13926 machine_mode mode;
13927
13928 if (want_address != 2)
13929 {
13930 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13931 /* Single part. */
13932 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13933 {
13934 varloc = PAT_VAR_LOCATION_LOC (varloc);
13935 if (GET_CODE (varloc) == EXPR_LIST)
13936 varloc = XEXP (varloc, 0);
13937 mode = GET_MODE (varloc);
13938 if (MEM_P (varloc))
13939 {
13940 rtx addr = XEXP (varloc, 0);
13941 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13942 mode, initialized);
13943 if (descr)
13944 have_address = 1;
13945 else
13946 {
13947 rtx x = avoid_constant_pool_reference (varloc);
13948 if (x != varloc)
13949 descr = mem_loc_descriptor (x, mode, VOIDmode,
13950 initialized);
13951 }
13952 }
13953 else
13954 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13955 }
13956 else
13957 return 0;
13958 }
13959 else
13960 {
13961 if (GET_CODE (varloc) == VAR_LOCATION)
13962 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13963 else
13964 mode = DECL_MODE (loc);
13965 descr = loc_descriptor (varloc, mode, initialized);
13966 have_address = 1;
13967 }
13968
13969 if (!descr)
13970 return 0;
13971
13972 if (want_address == 2 && !have_address
13973 && (dwarf_version >= 4 || !dwarf_strict))
13974 {
13975 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13976 {
13977 expansion_failed (loc, NULL_RTX,
13978 "DWARF address size mismatch");
13979 return 0;
13980 }
13981 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13982 have_address = 1;
13983 }
13984 /* Show if we can't fill the request for an address. */
13985 if (want_address && !have_address)
13986 {
13987 expansion_failed (loc, NULL_RTX,
13988 "Want address and only have value");
13989 return 0;
13990 }
13991
13992 /* If we've got an address and don't want one, dereference. */
13993 if (!want_address && have_address)
13994 {
13995 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13996 enum dwarf_location_atom op;
13997
13998 if (size > DWARF2_ADDR_SIZE || size == -1)
13999 {
14000 expansion_failed (loc, NULL_RTX,
14001 "DWARF address size mismatch");
14002 return 0;
14003 }
14004 else if (size == DWARF2_ADDR_SIZE)
14005 op = DW_OP_deref;
14006 else
14007 op = DW_OP_deref_size;
14008
14009 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14010 }
14011
14012 return descr;
14013 }
14014
14015 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14016 if it is not possible. */
14017
14018 static dw_loc_descr_ref
14019 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14020 {
14021 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14022 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14023 else if (dwarf_version >= 3 || !dwarf_strict)
14024 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14025 else
14026 return NULL;
14027 }
14028
14029 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14030 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14031
14032 static dw_loc_descr_ref
14033 dw_sra_loc_expr (tree decl, rtx loc)
14034 {
14035 rtx p;
14036 unsigned HOST_WIDE_INT padsize = 0;
14037 dw_loc_descr_ref descr, *descr_tail;
14038 unsigned HOST_WIDE_INT decl_size;
14039 rtx varloc;
14040 enum var_init_status initialized;
14041
14042 if (DECL_SIZE (decl) == NULL
14043 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14044 return NULL;
14045
14046 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14047 descr = NULL;
14048 descr_tail = &descr;
14049
14050 for (p = loc; p; p = XEXP (p, 1))
14051 {
14052 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14053 rtx loc_note = *decl_piece_varloc_ptr (p);
14054 dw_loc_descr_ref cur_descr;
14055 dw_loc_descr_ref *tail, last = NULL;
14056 unsigned HOST_WIDE_INT opsize = 0;
14057
14058 if (loc_note == NULL_RTX
14059 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14060 {
14061 padsize += bitsize;
14062 continue;
14063 }
14064 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14065 varloc = NOTE_VAR_LOCATION (loc_note);
14066 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14067 if (cur_descr == NULL)
14068 {
14069 padsize += bitsize;
14070 continue;
14071 }
14072
14073 /* Check that cur_descr either doesn't use
14074 DW_OP_*piece operations, or their sum is equal
14075 to bitsize. Otherwise we can't embed it. */
14076 for (tail = &cur_descr; *tail != NULL;
14077 tail = &(*tail)->dw_loc_next)
14078 if ((*tail)->dw_loc_opc == DW_OP_piece)
14079 {
14080 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14081 * BITS_PER_UNIT;
14082 last = *tail;
14083 }
14084 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14085 {
14086 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14087 last = *tail;
14088 }
14089
14090 if (last != NULL && opsize != bitsize)
14091 {
14092 padsize += bitsize;
14093 /* Discard the current piece of the descriptor and release any
14094 addr_table entries it uses. */
14095 remove_loc_list_addr_table_entries (cur_descr);
14096 continue;
14097 }
14098
14099 /* If there is a hole, add DW_OP_*piece after empty DWARF
14100 expression, which means that those bits are optimized out. */
14101 if (padsize)
14102 {
14103 if (padsize > decl_size)
14104 {
14105 remove_loc_list_addr_table_entries (cur_descr);
14106 goto discard_descr;
14107 }
14108 decl_size -= padsize;
14109 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14110 if (*descr_tail == NULL)
14111 {
14112 remove_loc_list_addr_table_entries (cur_descr);
14113 goto discard_descr;
14114 }
14115 descr_tail = &(*descr_tail)->dw_loc_next;
14116 padsize = 0;
14117 }
14118 *descr_tail = cur_descr;
14119 descr_tail = tail;
14120 if (bitsize > decl_size)
14121 goto discard_descr;
14122 decl_size -= bitsize;
14123 if (last == NULL)
14124 {
14125 HOST_WIDE_INT offset = 0;
14126 if (GET_CODE (varloc) == VAR_LOCATION
14127 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14128 {
14129 varloc = PAT_VAR_LOCATION_LOC (varloc);
14130 if (GET_CODE (varloc) == EXPR_LIST)
14131 varloc = XEXP (varloc, 0);
14132 }
14133 do
14134 {
14135 if (GET_CODE (varloc) == CONST
14136 || GET_CODE (varloc) == SIGN_EXTEND
14137 || GET_CODE (varloc) == ZERO_EXTEND)
14138 varloc = XEXP (varloc, 0);
14139 else if (GET_CODE (varloc) == SUBREG)
14140 varloc = SUBREG_REG (varloc);
14141 else
14142 break;
14143 }
14144 while (1);
14145 /* DW_OP_bit_size offset should be zero for register
14146 or implicit location descriptions and empty location
14147 descriptions, but for memory addresses needs big endian
14148 adjustment. */
14149 if (MEM_P (varloc))
14150 {
14151 unsigned HOST_WIDE_INT memsize
14152 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14153 if (memsize != bitsize)
14154 {
14155 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14156 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14157 goto discard_descr;
14158 if (memsize < bitsize)
14159 goto discard_descr;
14160 if (BITS_BIG_ENDIAN)
14161 offset = memsize - bitsize;
14162 }
14163 }
14164
14165 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14166 if (*descr_tail == NULL)
14167 goto discard_descr;
14168 descr_tail = &(*descr_tail)->dw_loc_next;
14169 }
14170 }
14171
14172 /* If there were any non-empty expressions, add padding till the end of
14173 the decl. */
14174 if (descr != NULL && decl_size != 0)
14175 {
14176 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14177 if (*descr_tail == NULL)
14178 goto discard_descr;
14179 }
14180 return descr;
14181
14182 discard_descr:
14183 /* Discard the descriptor and release any addr_table entries it uses. */
14184 remove_loc_list_addr_table_entries (descr);
14185 return NULL;
14186 }
14187
14188 /* Return the dwarf representation of the location list LOC_LIST of
14189 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14190 function. */
14191
14192 static dw_loc_list_ref
14193 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14194 {
14195 const char *endname, *secname;
14196 rtx varloc;
14197 enum var_init_status initialized;
14198 struct var_loc_node *node;
14199 dw_loc_descr_ref descr;
14200 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14201 dw_loc_list_ref list = NULL;
14202 dw_loc_list_ref *listp = &list;
14203
14204 /* Now that we know what section we are using for a base,
14205 actually construct the list of locations.
14206 The first location information is what is passed to the
14207 function that creates the location list, and the remaining
14208 locations just get added on to that list.
14209 Note that we only know the start address for a location
14210 (IE location changes), so to build the range, we use
14211 the range [current location start, next location start].
14212 This means we have to special case the last node, and generate
14213 a range of [last location start, end of function label]. */
14214
14215 secname = secname_for_decl (decl);
14216
14217 for (node = loc_list->first; node; node = node->next)
14218 if (GET_CODE (node->loc) == EXPR_LIST
14219 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14220 {
14221 if (GET_CODE (node->loc) == EXPR_LIST)
14222 {
14223 /* This requires DW_OP_{,bit_}piece, which is not usable
14224 inside DWARF expressions. */
14225 if (want_address != 2)
14226 continue;
14227 descr = dw_sra_loc_expr (decl, node->loc);
14228 if (descr == NULL)
14229 continue;
14230 }
14231 else
14232 {
14233 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14234 varloc = NOTE_VAR_LOCATION (node->loc);
14235 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14236 }
14237 if (descr)
14238 {
14239 bool range_across_switch = false;
14240 /* If section switch happens in between node->label
14241 and node->next->label (or end of function) and
14242 we can't emit it as a single entry list,
14243 emit two ranges, first one ending at the end
14244 of first partition and second one starting at the
14245 beginning of second partition. */
14246 if (node == loc_list->last_before_switch
14247 && (node != loc_list->first || loc_list->first->next)
14248 && current_function_decl)
14249 {
14250 endname = cfun->fde->dw_fde_end;
14251 range_across_switch = true;
14252 }
14253 /* The variable has a location between NODE->LABEL and
14254 NODE->NEXT->LABEL. */
14255 else if (node->next)
14256 endname = node->next->label;
14257 /* If the variable has a location at the last label
14258 it keeps its location until the end of function. */
14259 else if (!current_function_decl)
14260 endname = text_end_label;
14261 else
14262 {
14263 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14264 current_function_funcdef_no);
14265 endname = ggc_strdup (label_id);
14266 }
14267
14268 *listp = new_loc_list (descr, node->label, endname, secname);
14269 if (TREE_CODE (decl) == PARM_DECL
14270 && node == loc_list->first
14271 && NOTE_P (node->loc)
14272 && strcmp (node->label, endname) == 0)
14273 (*listp)->force = true;
14274 listp = &(*listp)->dw_loc_next;
14275
14276 if (range_across_switch)
14277 {
14278 if (GET_CODE (node->loc) == EXPR_LIST)
14279 descr = dw_sra_loc_expr (decl, node->loc);
14280 else
14281 {
14282 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14283 varloc = NOTE_VAR_LOCATION (node->loc);
14284 descr = dw_loc_list_1 (decl, varloc, want_address,
14285 initialized);
14286 }
14287 gcc_assert (descr);
14288 /* The variable has a location between NODE->LABEL and
14289 NODE->NEXT->LABEL. */
14290 if (node->next)
14291 endname = node->next->label;
14292 else
14293 endname = cfun->fde->dw_fde_second_end;
14294 *listp = new_loc_list (descr,
14295 cfun->fde->dw_fde_second_begin,
14296 endname, secname);
14297 listp = &(*listp)->dw_loc_next;
14298 }
14299 }
14300 }
14301
14302 /* Try to avoid the overhead of a location list emitting a location
14303 expression instead, but only if we didn't have more than one
14304 location entry in the first place. If some entries were not
14305 representable, we don't want to pretend a single entry that was
14306 applies to the entire scope in which the variable is
14307 available. */
14308 if (list && loc_list->first->next)
14309 gen_llsym (list);
14310
14311 return list;
14312 }
14313
14314 /* Return if the loc_list has only single element and thus can be represented
14315 as location description. */
14316
14317 static bool
14318 single_element_loc_list_p (dw_loc_list_ref list)
14319 {
14320 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14321 return !list->ll_symbol;
14322 }
14323
14324 /* To each location in list LIST add loc descr REF. */
14325
14326 static void
14327 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14328 {
14329 dw_loc_descr_ref copy;
14330 add_loc_descr (&list->expr, ref);
14331 list = list->dw_loc_next;
14332 while (list)
14333 {
14334 copy = ggc_alloc<dw_loc_descr_node> ();
14335 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14336 add_loc_descr (&list->expr, copy);
14337 while (copy->dw_loc_next)
14338 {
14339 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14340 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14341 copy->dw_loc_next = new_copy;
14342 copy = new_copy;
14343 }
14344 list = list->dw_loc_next;
14345 }
14346 }
14347
14348 /* Given two lists RET and LIST
14349 produce location list that is result of adding expression in LIST
14350 to expression in RET on each position in program.
14351 Might be destructive on both RET and LIST.
14352
14353 TODO: We handle only simple cases of RET or LIST having at most one
14354 element. General case would inolve sorting the lists in program order
14355 and merging them that will need some additional work.
14356 Adding that will improve quality of debug info especially for SRA-ed
14357 structures. */
14358
14359 static void
14360 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14361 {
14362 if (!list)
14363 return;
14364 if (!*ret)
14365 {
14366 *ret = list;
14367 return;
14368 }
14369 if (!list->dw_loc_next)
14370 {
14371 add_loc_descr_to_each (*ret, list->expr);
14372 return;
14373 }
14374 if (!(*ret)->dw_loc_next)
14375 {
14376 add_loc_descr_to_each (list, (*ret)->expr);
14377 *ret = list;
14378 return;
14379 }
14380 expansion_failed (NULL_TREE, NULL_RTX,
14381 "Don't know how to merge two non-trivial"
14382 " location lists.\n");
14383 *ret = NULL;
14384 return;
14385 }
14386
14387 /* LOC is constant expression. Try a luck, look it up in constant
14388 pool and return its loc_descr of its address. */
14389
14390 static dw_loc_descr_ref
14391 cst_pool_loc_descr (tree loc)
14392 {
14393 /* Get an RTL for this, if something has been emitted. */
14394 rtx rtl = lookup_constant_def (loc);
14395
14396 if (!rtl || !MEM_P (rtl))
14397 {
14398 gcc_assert (!rtl);
14399 return 0;
14400 }
14401 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14402
14403 /* TODO: We might get more coverage if we was actually delaying expansion
14404 of all expressions till end of compilation when constant pools are fully
14405 populated. */
14406 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14407 {
14408 expansion_failed (loc, NULL_RTX,
14409 "CST value in contant pool but not marked.");
14410 return 0;
14411 }
14412 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14413 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14414 }
14415
14416 /* Return dw_loc_list representing address of addr_expr LOC
14417 by looking for inner INDIRECT_REF expression and turning
14418 it into simple arithmetics.
14419
14420 See loc_list_from_tree for the meaning of CONTEXT. */
14421
14422 static dw_loc_list_ref
14423 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14424 const loc_descr_context *context)
14425 {
14426 tree obj, offset;
14427 HOST_WIDE_INT bitsize, bitpos, bytepos;
14428 machine_mode mode;
14429 int unsignedp, volatilep = 0;
14430 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14431
14432 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14433 &bitsize, &bitpos, &offset, &mode,
14434 &unsignedp, &volatilep, false);
14435 STRIP_NOPS (obj);
14436 if (bitpos % BITS_PER_UNIT)
14437 {
14438 expansion_failed (loc, NULL_RTX, "bitfield access");
14439 return 0;
14440 }
14441 if (!INDIRECT_REF_P (obj))
14442 {
14443 expansion_failed (obj,
14444 NULL_RTX, "no indirect ref in inner refrence");
14445 return 0;
14446 }
14447 if (!offset && !bitpos)
14448 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14449 context);
14450 else if (toplev
14451 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14452 && (dwarf_version >= 4 || !dwarf_strict))
14453 {
14454 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14455 if (!list_ret)
14456 return 0;
14457 if (offset)
14458 {
14459 /* Variable offset. */
14460 list_ret1 = loc_list_from_tree (offset, 0, context);
14461 if (list_ret1 == 0)
14462 return 0;
14463 add_loc_list (&list_ret, list_ret1);
14464 if (!list_ret)
14465 return 0;
14466 add_loc_descr_to_each (list_ret,
14467 new_loc_descr (DW_OP_plus, 0, 0));
14468 }
14469 bytepos = bitpos / BITS_PER_UNIT;
14470 if (bytepos > 0)
14471 add_loc_descr_to_each (list_ret,
14472 new_loc_descr (DW_OP_plus_uconst,
14473 bytepos, 0));
14474 else if (bytepos < 0)
14475 loc_list_plus_const (list_ret, bytepos);
14476 add_loc_descr_to_each (list_ret,
14477 new_loc_descr (DW_OP_stack_value, 0, 0));
14478 }
14479 return list_ret;
14480 }
14481
14482
14483 /* Helper structure for location descriptions generation. */
14484 struct loc_descr_context
14485 {
14486 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14487 NULL_TREE if DW_OP_push_object_address in invalid for this location
14488 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14489 tree context_type;
14490 /* The ..._DECL node that should be translated as a
14491 DW_OP_push_object_address operation. */
14492 tree base_decl;
14493 };
14494
14495 /* Generate Dwarf location list representing LOC.
14496 If WANT_ADDRESS is false, expression computing LOC will be computed
14497 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14498 if WANT_ADDRESS is 2, expression computing address useable in location
14499 will be returned (i.e. DW_OP_reg can be used
14500 to refer to register values).
14501
14502 CONTEXT provides information to customize the location descriptions
14503 generation. Its context_type field specifies what type is implicitly
14504 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14505 will not be generated.
14506
14507 If CONTEXT is NULL, the behavior is the same as if both context_type and
14508 base_decl fields were NULL_TREE. */
14509
14510 static dw_loc_list_ref
14511 loc_list_from_tree (tree loc, int want_address,
14512 const struct loc_descr_context *context)
14513 {
14514 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14515 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14516 int have_address = 0;
14517 enum dwarf_location_atom op;
14518
14519 /* ??? Most of the time we do not take proper care for sign/zero
14520 extending the values properly. Hopefully this won't be a real
14521 problem... */
14522
14523 if (context != NULL
14524 && context->base_decl == loc
14525 && want_address == 0)
14526 {
14527 if (dwarf_version >= 3 || !dwarf_strict)
14528 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14529 NULL, NULL, NULL);
14530 else
14531 return NULL;
14532 }
14533
14534 switch (TREE_CODE (loc))
14535 {
14536 case ERROR_MARK:
14537 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14538 return 0;
14539
14540 case PLACEHOLDER_EXPR:
14541 /* This case involves extracting fields from an object to determine the
14542 position of other fields. It is supposed to appear only as the first
14543 operand of COMPONENT_REF nodes and to reference precisely the type
14544 that the context allows. */
14545 if (context != NULL
14546 && TREE_TYPE (loc) == context->context_type
14547 && want_address >= 1)
14548 {
14549 if (dwarf_version >= 3 || !dwarf_strict)
14550 {
14551 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14552 have_address = 1;
14553 break;
14554 }
14555 else
14556 return NULL;
14557 }
14558 else
14559 expansion_failed (loc, NULL_RTX,
14560 "PLACEHOLDER_EXPR for an unexpected type");
14561 break;
14562
14563 case CALL_EXPR:
14564 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14565 /* There are no opcodes for these operations. */
14566 return 0;
14567
14568 case PREINCREMENT_EXPR:
14569 case PREDECREMENT_EXPR:
14570 case POSTINCREMENT_EXPR:
14571 case POSTDECREMENT_EXPR:
14572 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14573 /* There are no opcodes for these operations. */
14574 return 0;
14575
14576 case ADDR_EXPR:
14577 /* If we already want an address, see if there is INDIRECT_REF inside
14578 e.g. for &this->field. */
14579 if (want_address)
14580 {
14581 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14582 (loc, want_address == 2, context);
14583 if (list_ret)
14584 have_address = 1;
14585 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14586 && (ret = cst_pool_loc_descr (loc)))
14587 have_address = 1;
14588 }
14589 /* Otherwise, process the argument and look for the address. */
14590 if (!list_ret && !ret)
14591 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14592 else
14593 {
14594 if (want_address)
14595 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14596 return NULL;
14597 }
14598 break;
14599
14600 case VAR_DECL:
14601 if (DECL_THREAD_LOCAL_P (loc))
14602 {
14603 rtx rtl;
14604 enum dwarf_location_atom tls_op;
14605 enum dtprel_bool dtprel = dtprel_false;
14606
14607 if (targetm.have_tls)
14608 {
14609 /* If this is not defined, we have no way to emit the
14610 data. */
14611 if (!targetm.asm_out.output_dwarf_dtprel)
14612 return 0;
14613
14614 /* The way DW_OP_GNU_push_tls_address is specified, we
14615 can only look up addresses of objects in the current
14616 module. We used DW_OP_addr as first op, but that's
14617 wrong, because DW_OP_addr is relocated by the debug
14618 info consumer, while DW_OP_GNU_push_tls_address
14619 operand shouldn't be. */
14620 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14621 return 0;
14622 dtprel = dtprel_true;
14623 tls_op = DW_OP_GNU_push_tls_address;
14624 }
14625 else
14626 {
14627 if (!targetm.emutls.debug_form_tls_address
14628 || !(dwarf_version >= 3 || !dwarf_strict))
14629 return 0;
14630 /* We stuffed the control variable into the DECL_VALUE_EXPR
14631 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14632 no longer appear in gimple code. We used the control
14633 variable in specific so that we could pick it up here. */
14634 loc = DECL_VALUE_EXPR (loc);
14635 tls_op = DW_OP_form_tls_address;
14636 }
14637
14638 rtl = rtl_for_decl_location (loc);
14639 if (rtl == NULL_RTX)
14640 return 0;
14641
14642 if (!MEM_P (rtl))
14643 return 0;
14644 rtl = XEXP (rtl, 0);
14645 if (! CONSTANT_P (rtl))
14646 return 0;
14647
14648 ret = new_addr_loc_descr (rtl, dtprel);
14649 ret1 = new_loc_descr (tls_op, 0, 0);
14650 add_loc_descr (&ret, ret1);
14651
14652 have_address = 1;
14653 break;
14654 }
14655 /* FALLTHRU */
14656
14657 case PARM_DECL:
14658 case RESULT_DECL:
14659 if (DECL_HAS_VALUE_EXPR_P (loc))
14660 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14661 want_address, context);
14662 /* FALLTHRU */
14663
14664 case FUNCTION_DECL:
14665 {
14666 rtx rtl;
14667 var_loc_list *loc_list = lookup_decl_loc (loc);
14668
14669 if (loc_list && loc_list->first)
14670 {
14671 list_ret = dw_loc_list (loc_list, loc, want_address);
14672 have_address = want_address != 0;
14673 break;
14674 }
14675 rtl = rtl_for_decl_location (loc);
14676 if (rtl == NULL_RTX)
14677 {
14678 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14679 return 0;
14680 }
14681 else if (CONST_INT_P (rtl))
14682 {
14683 HOST_WIDE_INT val = INTVAL (rtl);
14684 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14685 val &= GET_MODE_MASK (DECL_MODE (loc));
14686 ret = int_loc_descriptor (val);
14687 }
14688 else if (GET_CODE (rtl) == CONST_STRING)
14689 {
14690 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14691 return 0;
14692 }
14693 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14694 ret = new_addr_loc_descr (rtl, dtprel_false);
14695 else
14696 {
14697 machine_mode mode, mem_mode;
14698
14699 /* Certain constructs can only be represented at top-level. */
14700 if (want_address == 2)
14701 {
14702 ret = loc_descriptor (rtl, VOIDmode,
14703 VAR_INIT_STATUS_INITIALIZED);
14704 have_address = 1;
14705 }
14706 else
14707 {
14708 mode = GET_MODE (rtl);
14709 mem_mode = VOIDmode;
14710 if (MEM_P (rtl))
14711 {
14712 mem_mode = mode;
14713 mode = get_address_mode (rtl);
14714 rtl = XEXP (rtl, 0);
14715 have_address = 1;
14716 }
14717 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14718 VAR_INIT_STATUS_INITIALIZED);
14719 }
14720 if (!ret)
14721 expansion_failed (loc, rtl,
14722 "failed to produce loc descriptor for rtl");
14723 }
14724 }
14725 break;
14726
14727 case MEM_REF:
14728 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14729 {
14730 have_address = 1;
14731 goto do_plus;
14732 }
14733 /* Fallthru. */
14734 case INDIRECT_REF:
14735 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14736 have_address = 1;
14737 break;
14738
14739 case TARGET_MEM_REF:
14740 case SSA_NAME:
14741 case DEBUG_EXPR_DECL:
14742 return NULL;
14743
14744 case COMPOUND_EXPR:
14745 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14746
14747 CASE_CONVERT:
14748 case VIEW_CONVERT_EXPR:
14749 case SAVE_EXPR:
14750 case MODIFY_EXPR:
14751 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14752
14753 case COMPONENT_REF:
14754 case BIT_FIELD_REF:
14755 case ARRAY_REF:
14756 case ARRAY_RANGE_REF:
14757 case REALPART_EXPR:
14758 case IMAGPART_EXPR:
14759 {
14760 tree obj, offset;
14761 HOST_WIDE_INT bitsize, bitpos, bytepos;
14762 machine_mode mode;
14763 int unsignedp, volatilep = 0;
14764
14765 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14766 &unsignedp, &volatilep, false);
14767
14768 gcc_assert (obj != loc);
14769
14770 list_ret = loc_list_from_tree (obj,
14771 want_address == 2
14772 && !bitpos && !offset ? 2 : 1,
14773 context);
14774 /* TODO: We can extract value of the small expression via shifting even
14775 for nonzero bitpos. */
14776 if (list_ret == 0)
14777 return 0;
14778 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14779 {
14780 expansion_failed (loc, NULL_RTX,
14781 "bitfield access");
14782 return 0;
14783 }
14784
14785 if (offset != NULL_TREE)
14786 {
14787 /* Variable offset. */
14788 list_ret1 = loc_list_from_tree (offset, 0, context);
14789 if (list_ret1 == 0)
14790 return 0;
14791 add_loc_list (&list_ret, list_ret1);
14792 if (!list_ret)
14793 return 0;
14794 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14795 }
14796
14797 bytepos = bitpos / BITS_PER_UNIT;
14798 if (bytepos > 0)
14799 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14800 else if (bytepos < 0)
14801 loc_list_plus_const (list_ret, bytepos);
14802
14803 have_address = 1;
14804 break;
14805 }
14806
14807 case INTEGER_CST:
14808 if ((want_address || !tree_fits_shwi_p (loc))
14809 && (ret = cst_pool_loc_descr (loc)))
14810 have_address = 1;
14811 else if (want_address == 2
14812 && tree_fits_shwi_p (loc)
14813 && (ret = address_of_int_loc_descriptor
14814 (int_size_in_bytes (TREE_TYPE (loc)),
14815 tree_to_shwi (loc))))
14816 have_address = 1;
14817 else if (tree_fits_shwi_p (loc))
14818 ret = int_loc_descriptor (tree_to_shwi (loc));
14819 else
14820 {
14821 expansion_failed (loc, NULL_RTX,
14822 "Integer operand is not host integer");
14823 return 0;
14824 }
14825 break;
14826
14827 case CONSTRUCTOR:
14828 case REAL_CST:
14829 case STRING_CST:
14830 case COMPLEX_CST:
14831 if ((ret = cst_pool_loc_descr (loc)))
14832 have_address = 1;
14833 else
14834 /* We can construct small constants here using int_loc_descriptor. */
14835 expansion_failed (loc, NULL_RTX,
14836 "constructor or constant not in constant pool");
14837 break;
14838
14839 case TRUTH_AND_EXPR:
14840 case TRUTH_ANDIF_EXPR:
14841 case BIT_AND_EXPR:
14842 op = DW_OP_and;
14843 goto do_binop;
14844
14845 case TRUTH_XOR_EXPR:
14846 case BIT_XOR_EXPR:
14847 op = DW_OP_xor;
14848 goto do_binop;
14849
14850 case TRUTH_OR_EXPR:
14851 case TRUTH_ORIF_EXPR:
14852 case BIT_IOR_EXPR:
14853 op = DW_OP_or;
14854 goto do_binop;
14855
14856 case FLOOR_DIV_EXPR:
14857 case CEIL_DIV_EXPR:
14858 case ROUND_DIV_EXPR:
14859 case TRUNC_DIV_EXPR:
14860 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14861 return 0;
14862 op = DW_OP_div;
14863 goto do_binop;
14864
14865 case MINUS_EXPR:
14866 op = DW_OP_minus;
14867 goto do_binop;
14868
14869 case FLOOR_MOD_EXPR:
14870 case CEIL_MOD_EXPR:
14871 case ROUND_MOD_EXPR:
14872 case TRUNC_MOD_EXPR:
14873 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14874 {
14875 op = DW_OP_mod;
14876 goto do_binop;
14877 }
14878 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14879 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14880 if (list_ret == 0 || list_ret1 == 0)
14881 return 0;
14882
14883 add_loc_list (&list_ret, list_ret1);
14884 if (list_ret == 0)
14885 return 0;
14886 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14887 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14888 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14889 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14890 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14891 break;
14892
14893 case MULT_EXPR:
14894 op = DW_OP_mul;
14895 goto do_binop;
14896
14897 case LSHIFT_EXPR:
14898 op = DW_OP_shl;
14899 goto do_binop;
14900
14901 case RSHIFT_EXPR:
14902 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14903 goto do_binop;
14904
14905 case POINTER_PLUS_EXPR:
14906 case PLUS_EXPR:
14907 do_plus:
14908 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14909 {
14910 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14911 if (list_ret == 0)
14912 return 0;
14913
14914 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14915 break;
14916 }
14917
14918 op = DW_OP_plus;
14919 goto do_binop;
14920
14921 case LE_EXPR:
14922 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14923 return 0;
14924
14925 op = DW_OP_le;
14926 goto do_binop;
14927
14928 case GE_EXPR:
14929 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14930 return 0;
14931
14932 op = DW_OP_ge;
14933 goto do_binop;
14934
14935 case LT_EXPR:
14936 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14937 return 0;
14938
14939 op = DW_OP_lt;
14940 goto do_binop;
14941
14942 case GT_EXPR:
14943 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14944 return 0;
14945
14946 op = DW_OP_gt;
14947 goto do_binop;
14948
14949 case EQ_EXPR:
14950 op = DW_OP_eq;
14951 goto do_binop;
14952
14953 case NE_EXPR:
14954 op = DW_OP_ne;
14955 goto do_binop;
14956
14957 do_binop:
14958 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14959 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14960 if (list_ret == 0 || list_ret1 == 0)
14961 return 0;
14962
14963 add_loc_list (&list_ret, list_ret1);
14964 if (list_ret == 0)
14965 return 0;
14966 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14967 break;
14968
14969 case TRUTH_NOT_EXPR:
14970 case BIT_NOT_EXPR:
14971 op = DW_OP_not;
14972 goto do_unop;
14973
14974 case ABS_EXPR:
14975 op = DW_OP_abs;
14976 goto do_unop;
14977
14978 case NEGATE_EXPR:
14979 op = DW_OP_neg;
14980 goto do_unop;
14981
14982 do_unop:
14983 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14984 if (list_ret == 0)
14985 return 0;
14986
14987 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14988 break;
14989
14990 case MIN_EXPR:
14991 case MAX_EXPR:
14992 {
14993 const enum tree_code code =
14994 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14995
14996 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14997 build2 (code, integer_type_node,
14998 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14999 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15000 }
15001
15002 /* ... fall through ... */
15003
15004 case COND_EXPR:
15005 {
15006 dw_loc_descr_ref lhs
15007 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15008 dw_loc_list_ref rhs
15009 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15010 dw_loc_descr_ref bra_node, jump_node, tmp;
15011
15012 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15013 if (list_ret == 0 || lhs == 0 || rhs == 0)
15014 return 0;
15015
15016 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15017 add_loc_descr_to_each (list_ret, bra_node);
15018
15019 add_loc_list (&list_ret, rhs);
15020 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15021 add_loc_descr_to_each (list_ret, jump_node);
15022
15023 add_loc_descr_to_each (list_ret, lhs);
15024 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15025 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15026
15027 /* ??? Need a node to point the skip at. Use a nop. */
15028 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15029 add_loc_descr_to_each (list_ret, tmp);
15030 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15031 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15032 }
15033 break;
15034
15035 case FIX_TRUNC_EXPR:
15036 return 0;
15037
15038 default:
15039 /* Leave front-end specific codes as simply unknown. This comes
15040 up, for instance, with the C STMT_EXPR. */
15041 if ((unsigned int) TREE_CODE (loc)
15042 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15043 {
15044 expansion_failed (loc, NULL_RTX,
15045 "language specific tree node");
15046 return 0;
15047 }
15048
15049 #ifdef ENABLE_CHECKING
15050 /* Otherwise this is a generic code; we should just lists all of
15051 these explicitly. We forgot one. */
15052 gcc_unreachable ();
15053 #else
15054 /* In a release build, we want to degrade gracefully: better to
15055 generate incomplete debugging information than to crash. */
15056 return NULL;
15057 #endif
15058 }
15059
15060 if (!ret && !list_ret)
15061 return 0;
15062
15063 if (want_address == 2 && !have_address
15064 && (dwarf_version >= 4 || !dwarf_strict))
15065 {
15066 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15067 {
15068 expansion_failed (loc, NULL_RTX,
15069 "DWARF address size mismatch");
15070 return 0;
15071 }
15072 if (ret)
15073 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15074 else
15075 add_loc_descr_to_each (list_ret,
15076 new_loc_descr (DW_OP_stack_value, 0, 0));
15077 have_address = 1;
15078 }
15079 /* Show if we can't fill the request for an address. */
15080 if (want_address && !have_address)
15081 {
15082 expansion_failed (loc, NULL_RTX,
15083 "Want address and only have value");
15084 return 0;
15085 }
15086
15087 gcc_assert (!ret || !list_ret);
15088
15089 /* If we've got an address and don't want one, dereference. */
15090 if (!want_address && have_address)
15091 {
15092 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15093
15094 if (size > DWARF2_ADDR_SIZE || size == -1)
15095 {
15096 expansion_failed (loc, NULL_RTX,
15097 "DWARF address size mismatch");
15098 return 0;
15099 }
15100 else if (size == DWARF2_ADDR_SIZE)
15101 op = DW_OP_deref;
15102 else
15103 op = DW_OP_deref_size;
15104
15105 if (ret)
15106 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15107 else
15108 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15109 }
15110 if (ret)
15111 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15112
15113 return list_ret;
15114 }
15115
15116 /* Same as above but return only single location expression. */
15117 static dw_loc_descr_ref
15118 loc_descriptor_from_tree (tree loc, int want_address,
15119 const struct loc_descr_context *context)
15120 {
15121 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15122 if (!ret)
15123 return NULL;
15124 if (ret->dw_loc_next)
15125 {
15126 expansion_failed (loc, NULL_RTX,
15127 "Location list where only loc descriptor needed");
15128 return NULL;
15129 }
15130 return ret->expr;
15131 }
15132
15133 /* Given a value, round it up to the lowest multiple of `boundary'
15134 which is not less than the value itself. */
15135
15136 static inline HOST_WIDE_INT
15137 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15138 {
15139 return (((value + boundary - 1) / boundary) * boundary);
15140 }
15141
15142 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15143 pointer to the declared type for the relevant field variable, or return
15144 `integer_type_node' if the given node turns out to be an
15145 ERROR_MARK node. */
15146
15147 static inline tree
15148 field_type (const_tree decl)
15149 {
15150 tree type;
15151
15152 if (TREE_CODE (decl) == ERROR_MARK)
15153 return integer_type_node;
15154
15155 type = DECL_BIT_FIELD_TYPE (decl);
15156 if (type == NULL_TREE)
15157 type = TREE_TYPE (decl);
15158
15159 return type;
15160 }
15161
15162 /* Given a pointer to a tree node, return the alignment in bits for
15163 it, or else return BITS_PER_WORD if the node actually turns out to
15164 be an ERROR_MARK node. */
15165
15166 static inline unsigned
15167 simple_type_align_in_bits (const_tree type)
15168 {
15169 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15170 }
15171
15172 static inline unsigned
15173 simple_decl_align_in_bits (const_tree decl)
15174 {
15175 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15176 }
15177
15178 /* Return the result of rounding T up to ALIGN. */
15179
15180 static inline offset_int
15181 round_up_to_align (const offset_int &t, unsigned int align)
15182 {
15183 return wi::udiv_trunc (t + align - 1, align) * align;
15184 }
15185
15186 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15187 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15188 or return 0 if we are unable to determine what that offset is, either
15189 because the argument turns out to be a pointer to an ERROR_MARK node, or
15190 because the offset is actually variable. (We can't handle the latter case
15191 just yet). */
15192
15193 static HOST_WIDE_INT
15194 field_byte_offset (const_tree decl)
15195 {
15196 offset_int object_offset_in_bits;
15197 offset_int object_offset_in_bytes;
15198 offset_int bitpos_int;
15199
15200 if (TREE_CODE (decl) == ERROR_MARK)
15201 return 0;
15202
15203 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15204
15205 /* We cannot yet cope with fields whose positions are variable, so
15206 for now, when we see such things, we simply return 0. Someday, we may
15207 be able to handle such cases, but it will be damn difficult. */
15208 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15209 return 0;
15210
15211 bitpos_int = wi::to_offset (bit_position (decl));
15212
15213 if (PCC_BITFIELD_TYPE_MATTERS)
15214 {
15215 tree type;
15216 tree field_size_tree;
15217 offset_int deepest_bitpos;
15218 offset_int field_size_in_bits;
15219 unsigned int type_align_in_bits;
15220 unsigned int decl_align_in_bits;
15221 offset_int type_size_in_bits;
15222
15223 type = field_type (decl);
15224 type_size_in_bits = offset_int_type_size_in_bits (type);
15225 type_align_in_bits = simple_type_align_in_bits (type);
15226
15227 field_size_tree = DECL_SIZE (decl);
15228
15229 /* The size could be unspecified if there was an error, or for
15230 a flexible array member. */
15231 if (!field_size_tree)
15232 field_size_tree = bitsize_zero_node;
15233
15234 /* If the size of the field is not constant, use the type size. */
15235 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15236 field_size_in_bits = wi::to_offset (field_size_tree);
15237 else
15238 field_size_in_bits = type_size_in_bits;
15239
15240 decl_align_in_bits = simple_decl_align_in_bits (decl);
15241
15242 /* The GCC front-end doesn't make any attempt to keep track of the
15243 starting bit offset (relative to the start of the containing
15244 structure type) of the hypothetical "containing object" for a
15245 bit-field. Thus, when computing the byte offset value for the
15246 start of the "containing object" of a bit-field, we must deduce
15247 this information on our own. This can be rather tricky to do in
15248 some cases. For example, handling the following structure type
15249 definition when compiling for an i386/i486 target (which only
15250 aligns long long's to 32-bit boundaries) can be very tricky:
15251
15252 struct S { int field1; long long field2:31; };
15253
15254 Fortunately, there is a simple rule-of-thumb which can be used
15255 in such cases. When compiling for an i386/i486, GCC will
15256 allocate 8 bytes for the structure shown above. It decides to
15257 do this based upon one simple rule for bit-field allocation.
15258 GCC allocates each "containing object" for each bit-field at
15259 the first (i.e. lowest addressed) legitimate alignment boundary
15260 (based upon the required minimum alignment for the declared
15261 type of the field) which it can possibly use, subject to the
15262 condition that there is still enough available space remaining
15263 in the containing object (when allocated at the selected point)
15264 to fully accommodate all of the bits of the bit-field itself.
15265
15266 This simple rule makes it obvious why GCC allocates 8 bytes for
15267 each object of the structure type shown above. When looking
15268 for a place to allocate the "containing object" for `field2',
15269 the compiler simply tries to allocate a 64-bit "containing
15270 object" at each successive 32-bit boundary (starting at zero)
15271 until it finds a place to allocate that 64- bit field such that
15272 at least 31 contiguous (and previously unallocated) bits remain
15273 within that selected 64 bit field. (As it turns out, for the
15274 example above, the compiler finds it is OK to allocate the
15275 "containing object" 64-bit field at bit-offset zero within the
15276 structure type.)
15277
15278 Here we attempt to work backwards from the limited set of facts
15279 we're given, and we try to deduce from those facts, where GCC
15280 must have believed that the containing object started (within
15281 the structure type). The value we deduce is then used (by the
15282 callers of this routine) to generate DW_AT_location and
15283 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15284 the case of DW_AT_location, regular fields as well). */
15285
15286 /* Figure out the bit-distance from the start of the structure to
15287 the "deepest" bit of the bit-field. */
15288 deepest_bitpos = bitpos_int + field_size_in_bits;
15289
15290 /* This is the tricky part. Use some fancy footwork to deduce
15291 where the lowest addressed bit of the containing object must
15292 be. */
15293 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15294
15295 /* Round up to type_align by default. This works best for
15296 bitfields. */
15297 object_offset_in_bits
15298 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15299
15300 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15301 {
15302 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15303
15304 /* Round up to decl_align instead. */
15305 object_offset_in_bits
15306 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15307 }
15308 }
15309 else
15310 object_offset_in_bits = bitpos_int;
15311
15312 object_offset_in_bytes
15313 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15314 return object_offset_in_bytes.to_shwi ();
15315 }
15316 \f
15317 /* The following routines define various Dwarf attributes and any data
15318 associated with them. */
15319
15320 /* Add a location description attribute value to a DIE.
15321
15322 This emits location attributes suitable for whole variables and
15323 whole parameters. Note that the location attributes for struct fields are
15324 generated by the routine `data_member_location_attribute' below. */
15325
15326 static inline void
15327 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15328 dw_loc_list_ref descr)
15329 {
15330 if (descr == 0)
15331 return;
15332 if (single_element_loc_list_p (descr))
15333 add_AT_loc (die, attr_kind, descr->expr);
15334 else
15335 add_AT_loc_list (die, attr_kind, descr);
15336 }
15337
15338 /* Add DW_AT_accessibility attribute to DIE if needed. */
15339
15340 static void
15341 add_accessibility_attribute (dw_die_ref die, tree decl)
15342 {
15343 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15344 children, otherwise the default is DW_ACCESS_public. In DWARF2
15345 the default has always been DW_ACCESS_public. */
15346 if (TREE_PROTECTED (decl))
15347 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15348 else if (TREE_PRIVATE (decl))
15349 {
15350 if (dwarf_version == 2
15351 || die->die_parent == NULL
15352 || die->die_parent->die_tag != DW_TAG_class_type)
15353 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15354 }
15355 else if (dwarf_version > 2
15356 && die->die_parent
15357 && die->die_parent->die_tag == DW_TAG_class_type)
15358 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15359 }
15360
15361 /* Attach the specialized form of location attribute used for data members of
15362 struct and union types. In the special case of a FIELD_DECL node which
15363 represents a bit-field, the "offset" part of this special location
15364 descriptor must indicate the distance in bytes from the lowest-addressed
15365 byte of the containing struct or union type to the lowest-addressed byte of
15366 the "containing object" for the bit-field. (See the `field_byte_offset'
15367 function above).
15368
15369 For any given bit-field, the "containing object" is a hypothetical object
15370 (of some integral or enum type) within which the given bit-field lives. The
15371 type of this hypothetical "containing object" is always the same as the
15372 declared type of the individual bit-field itself (for GCC anyway... the
15373 DWARF spec doesn't actually mandate this). Note that it is the size (in
15374 bytes) of the hypothetical "containing object" which will be given in the
15375 DW_AT_byte_size attribute for this bit-field. (See the
15376 `byte_size_attribute' function below.) It is also used when calculating the
15377 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15378 function below.) */
15379
15380 static void
15381 add_data_member_location_attribute (dw_die_ref die, tree decl)
15382 {
15383 HOST_WIDE_INT offset;
15384 dw_loc_descr_ref loc_descr = 0;
15385
15386 if (TREE_CODE (decl) == TREE_BINFO)
15387 {
15388 /* We're working on the TAG_inheritance for a base class. */
15389 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15390 {
15391 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15392 aren't at a fixed offset from all (sub)objects of the same
15393 type. We need to extract the appropriate offset from our
15394 vtable. The following dwarf expression means
15395
15396 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15397
15398 This is specific to the V3 ABI, of course. */
15399
15400 dw_loc_descr_ref tmp;
15401
15402 /* Make a copy of the object address. */
15403 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15404 add_loc_descr (&loc_descr, tmp);
15405
15406 /* Extract the vtable address. */
15407 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15408 add_loc_descr (&loc_descr, tmp);
15409
15410 /* Calculate the address of the offset. */
15411 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15412 gcc_assert (offset < 0);
15413
15414 tmp = int_loc_descriptor (-offset);
15415 add_loc_descr (&loc_descr, tmp);
15416 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15417 add_loc_descr (&loc_descr, tmp);
15418
15419 /* Extract the offset. */
15420 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15421 add_loc_descr (&loc_descr, tmp);
15422
15423 /* Add it to the object address. */
15424 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15425 add_loc_descr (&loc_descr, tmp);
15426 }
15427 else
15428 offset = tree_to_shwi (BINFO_OFFSET (decl));
15429 }
15430 else
15431 offset = field_byte_offset (decl);
15432
15433 if (! loc_descr)
15434 {
15435 if (dwarf_version > 2)
15436 {
15437 /* Don't need to output a location expression, just the constant. */
15438 if (offset < 0)
15439 add_AT_int (die, DW_AT_data_member_location, offset);
15440 else
15441 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15442 return;
15443 }
15444 else
15445 {
15446 enum dwarf_location_atom op;
15447
15448 /* The DWARF2 standard says that we should assume that the structure
15449 address is already on the stack, so we can specify a structure
15450 field address by using DW_OP_plus_uconst. */
15451 op = DW_OP_plus_uconst;
15452 loc_descr = new_loc_descr (op, offset, 0);
15453 }
15454 }
15455
15456 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15457 }
15458
15459 /* Writes integer values to dw_vec_const array. */
15460
15461 static void
15462 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15463 {
15464 while (size != 0)
15465 {
15466 *dest++ = val & 0xff;
15467 val >>= 8;
15468 --size;
15469 }
15470 }
15471
15472 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15473
15474 static HOST_WIDE_INT
15475 extract_int (const unsigned char *src, unsigned int size)
15476 {
15477 HOST_WIDE_INT val = 0;
15478
15479 src += size;
15480 while (size != 0)
15481 {
15482 val <<= 8;
15483 val |= *--src & 0xff;
15484 --size;
15485 }
15486 return val;
15487 }
15488
15489 /* Writes wide_int values to dw_vec_const array. */
15490
15491 static void
15492 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15493 {
15494 int i;
15495
15496 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15497 {
15498 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15499 return;
15500 }
15501
15502 /* We'd have to extend this code to support odd sizes. */
15503 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15504
15505 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15506
15507 if (WORDS_BIG_ENDIAN)
15508 for (i = n - 1; i >= 0; i--)
15509 {
15510 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15511 dest += sizeof (HOST_WIDE_INT);
15512 }
15513 else
15514 for (i = 0; i < n; i++)
15515 {
15516 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15517 dest += sizeof (HOST_WIDE_INT);
15518 }
15519 }
15520
15521 /* Writes floating point values to dw_vec_const array. */
15522
15523 static void
15524 insert_float (const_rtx rtl, unsigned char *array)
15525 {
15526 REAL_VALUE_TYPE rv;
15527 long val[4];
15528 int i;
15529
15530 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15531 real_to_target (val, &rv, GET_MODE (rtl));
15532
15533 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15534 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15535 {
15536 insert_int (val[i], 4, array);
15537 array += 4;
15538 }
15539 }
15540
15541 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15542 does not have a "location" either in memory or in a register. These
15543 things can arise in GNU C when a constant is passed as an actual parameter
15544 to an inlined function. They can also arise in C++ where declared
15545 constants do not necessarily get memory "homes". */
15546
15547 static bool
15548 add_const_value_attribute (dw_die_ref die, rtx rtl)
15549 {
15550 switch (GET_CODE (rtl))
15551 {
15552 case CONST_INT:
15553 {
15554 HOST_WIDE_INT val = INTVAL (rtl);
15555
15556 if (val < 0)
15557 add_AT_int (die, DW_AT_const_value, val);
15558 else
15559 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15560 }
15561 return true;
15562
15563 case CONST_WIDE_INT:
15564 add_AT_wide (die, DW_AT_const_value,
15565 std::make_pair (rtl, GET_MODE (rtl)));
15566 return true;
15567
15568 case CONST_DOUBLE:
15569 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15570 floating-point constant. A CONST_DOUBLE is used whenever the
15571 constant requires more than one word in order to be adequately
15572 represented. */
15573 {
15574 machine_mode mode = GET_MODE (rtl);
15575
15576 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15577 add_AT_double (die, DW_AT_const_value,
15578 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15579 else
15580 {
15581 unsigned int length = GET_MODE_SIZE (mode);
15582 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15583
15584 insert_float (rtl, array);
15585 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15586 }
15587 }
15588 return true;
15589
15590 case CONST_VECTOR:
15591 {
15592 machine_mode mode = GET_MODE (rtl);
15593 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15594 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15595 unsigned char *array
15596 = ggc_vec_alloc<unsigned char> (length * elt_size);
15597 unsigned int i;
15598 unsigned char *p;
15599 machine_mode imode = GET_MODE_INNER (mode);
15600
15601 switch (GET_MODE_CLASS (mode))
15602 {
15603 case MODE_VECTOR_INT:
15604 for (i = 0, p = array; i < length; i++, p += elt_size)
15605 {
15606 rtx elt = CONST_VECTOR_ELT (rtl, i);
15607 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15608 }
15609 break;
15610
15611 case MODE_VECTOR_FLOAT:
15612 for (i = 0, p = array; i < length; i++, p += elt_size)
15613 {
15614 rtx elt = CONST_VECTOR_ELT (rtl, i);
15615 insert_float (elt, p);
15616 }
15617 break;
15618
15619 default:
15620 gcc_unreachable ();
15621 }
15622
15623 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15624 }
15625 return true;
15626
15627 case CONST_STRING:
15628 if (dwarf_version >= 4 || !dwarf_strict)
15629 {
15630 dw_loc_descr_ref loc_result;
15631 resolve_one_addr (&rtl);
15632 rtl_addr:
15633 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15634 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15635 add_AT_loc (die, DW_AT_location, loc_result);
15636 vec_safe_push (used_rtx_array, rtl);
15637 return true;
15638 }
15639 return false;
15640
15641 case CONST:
15642 if (CONSTANT_P (XEXP (rtl, 0)))
15643 return add_const_value_attribute (die, XEXP (rtl, 0));
15644 /* FALLTHROUGH */
15645 case SYMBOL_REF:
15646 if (!const_ok_for_output (rtl))
15647 return false;
15648 case LABEL_REF:
15649 if (dwarf_version >= 4 || !dwarf_strict)
15650 goto rtl_addr;
15651 return false;
15652
15653 case PLUS:
15654 /* In cases where an inlined instance of an inline function is passed
15655 the address of an `auto' variable (which is local to the caller) we
15656 can get a situation where the DECL_RTL of the artificial local
15657 variable (for the inlining) which acts as a stand-in for the
15658 corresponding formal parameter (of the inline function) will look
15659 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15660 exactly a compile-time constant expression, but it isn't the address
15661 of the (artificial) local variable either. Rather, it represents the
15662 *value* which the artificial local variable always has during its
15663 lifetime. We currently have no way to represent such quasi-constant
15664 values in Dwarf, so for now we just punt and generate nothing. */
15665 return false;
15666
15667 case HIGH:
15668 case CONST_FIXED:
15669 return false;
15670
15671 case MEM:
15672 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15673 && MEM_READONLY_P (rtl)
15674 && GET_MODE (rtl) == BLKmode)
15675 {
15676 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15677 return true;
15678 }
15679 return false;
15680
15681 default:
15682 /* No other kinds of rtx should be possible here. */
15683 gcc_unreachable ();
15684 }
15685 return false;
15686 }
15687
15688 /* Determine whether the evaluation of EXPR references any variables
15689 or functions which aren't otherwise used (and therefore may not be
15690 output). */
15691 static tree
15692 reference_to_unused (tree * tp, int * walk_subtrees,
15693 void * data ATTRIBUTE_UNUSED)
15694 {
15695 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15696 *walk_subtrees = 0;
15697
15698 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15699 && ! TREE_ASM_WRITTEN (*tp))
15700 return *tp;
15701 /* ??? The C++ FE emits debug information for using decls, so
15702 putting gcc_unreachable here falls over. See PR31899. For now
15703 be conservative. */
15704 else if (!symtab->global_info_ready
15705 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15706 return *tp;
15707 else if (TREE_CODE (*tp) == VAR_DECL)
15708 {
15709 varpool_node *node = varpool_node::get (*tp);
15710 if (!node || !node->definition)
15711 return *tp;
15712 }
15713 else if (TREE_CODE (*tp) == FUNCTION_DECL
15714 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15715 {
15716 /* The call graph machinery must have finished analyzing,
15717 optimizing and gimplifying the CU by now.
15718 So if *TP has no call graph node associated
15719 to it, it means *TP will not be emitted. */
15720 if (!cgraph_node::get (*tp))
15721 return *tp;
15722 }
15723 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15724 return *tp;
15725
15726 return NULL_TREE;
15727 }
15728
15729 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15730 for use in a later add_const_value_attribute call. */
15731
15732 static rtx
15733 rtl_for_decl_init (tree init, tree type)
15734 {
15735 rtx rtl = NULL_RTX;
15736
15737 STRIP_NOPS (init);
15738
15739 /* If a variable is initialized with a string constant without embedded
15740 zeros, build CONST_STRING. */
15741 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15742 {
15743 tree enttype = TREE_TYPE (type);
15744 tree domain = TYPE_DOMAIN (type);
15745 machine_mode mode = TYPE_MODE (enttype);
15746
15747 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15748 && domain
15749 && integer_zerop (TYPE_MIN_VALUE (domain))
15750 && compare_tree_int (TYPE_MAX_VALUE (domain),
15751 TREE_STRING_LENGTH (init) - 1) == 0
15752 && ((size_t) TREE_STRING_LENGTH (init)
15753 == strlen (TREE_STRING_POINTER (init)) + 1))
15754 {
15755 rtl = gen_rtx_CONST_STRING (VOIDmode,
15756 ggc_strdup (TREE_STRING_POINTER (init)));
15757 rtl = gen_rtx_MEM (BLKmode, rtl);
15758 MEM_READONLY_P (rtl) = 1;
15759 }
15760 }
15761 /* Other aggregates, and complex values, could be represented using
15762 CONCAT: FIXME! */
15763 else if (AGGREGATE_TYPE_P (type)
15764 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15765 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15766 || TREE_CODE (type) == COMPLEX_TYPE)
15767 ;
15768 /* Vectors only work if their mode is supported by the target.
15769 FIXME: generic vectors ought to work too. */
15770 else if (TREE_CODE (type) == VECTOR_TYPE
15771 && !VECTOR_MODE_P (TYPE_MODE (type)))
15772 ;
15773 /* If the initializer is something that we know will expand into an
15774 immediate RTL constant, expand it now. We must be careful not to
15775 reference variables which won't be output. */
15776 else if (initializer_constant_valid_p (init, type)
15777 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15778 {
15779 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15780 possible. */
15781 if (TREE_CODE (type) == VECTOR_TYPE)
15782 switch (TREE_CODE (init))
15783 {
15784 case VECTOR_CST:
15785 break;
15786 case CONSTRUCTOR:
15787 if (TREE_CONSTANT (init))
15788 {
15789 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15790 bool constant_p = true;
15791 tree value;
15792 unsigned HOST_WIDE_INT ix;
15793
15794 /* Even when ctor is constant, it might contain non-*_CST
15795 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15796 belong into VECTOR_CST nodes. */
15797 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15798 if (!CONSTANT_CLASS_P (value))
15799 {
15800 constant_p = false;
15801 break;
15802 }
15803
15804 if (constant_p)
15805 {
15806 init = build_vector_from_ctor (type, elts);
15807 break;
15808 }
15809 }
15810 /* FALLTHRU */
15811
15812 default:
15813 return NULL;
15814 }
15815
15816 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15817
15818 /* If expand_expr returns a MEM, it wasn't immediate. */
15819 gcc_assert (!rtl || !MEM_P (rtl));
15820 }
15821
15822 return rtl;
15823 }
15824
15825 /* Generate RTL for the variable DECL to represent its location. */
15826
15827 static rtx
15828 rtl_for_decl_location (tree decl)
15829 {
15830 rtx rtl;
15831
15832 /* Here we have to decide where we are going to say the parameter "lives"
15833 (as far as the debugger is concerned). We only have a couple of
15834 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15835
15836 DECL_RTL normally indicates where the parameter lives during most of the
15837 activation of the function. If optimization is enabled however, this
15838 could be either NULL or else a pseudo-reg. Both of those cases indicate
15839 that the parameter doesn't really live anywhere (as far as the code
15840 generation parts of GCC are concerned) during most of the function's
15841 activation. That will happen (for example) if the parameter is never
15842 referenced within the function.
15843
15844 We could just generate a location descriptor here for all non-NULL
15845 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15846 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15847 where DECL_RTL is NULL or is a pseudo-reg.
15848
15849 Note however that we can only get away with using DECL_INCOMING_RTL as
15850 a backup substitute for DECL_RTL in certain limited cases. In cases
15851 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15852 we can be sure that the parameter was passed using the same type as it is
15853 declared to have within the function, and that its DECL_INCOMING_RTL
15854 points us to a place where a value of that type is passed.
15855
15856 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15857 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15858 because in these cases DECL_INCOMING_RTL points us to a value of some
15859 type which is *different* from the type of the parameter itself. Thus,
15860 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15861 such cases, the debugger would end up (for example) trying to fetch a
15862 `float' from a place which actually contains the first part of a
15863 `double'. That would lead to really incorrect and confusing
15864 output at debug-time.
15865
15866 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15867 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15868 are a couple of exceptions however. On little-endian machines we can
15869 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15870 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15871 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15872 when (on a little-endian machine) a non-prototyped function has a
15873 parameter declared to be of type `short' or `char'. In such cases,
15874 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15875 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15876 passed `int' value. If the debugger then uses that address to fetch
15877 a `short' or a `char' (on a little-endian machine) the result will be
15878 the correct data, so we allow for such exceptional cases below.
15879
15880 Note that our goal here is to describe the place where the given formal
15881 parameter lives during most of the function's activation (i.e. between the
15882 end of the prologue and the start of the epilogue). We'll do that as best
15883 as we can. Note however that if the given formal parameter is modified
15884 sometime during the execution of the function, then a stack backtrace (at
15885 debug-time) will show the function as having been called with the *new*
15886 value rather than the value which was originally passed in. This happens
15887 rarely enough that it is not a major problem, but it *is* a problem, and
15888 I'd like to fix it.
15889
15890 A future version of dwarf2out.c may generate two additional attributes for
15891 any given DW_TAG_formal_parameter DIE which will describe the "passed
15892 type" and the "passed location" for the given formal parameter in addition
15893 to the attributes we now generate to indicate the "declared type" and the
15894 "active location" for each parameter. This additional set of attributes
15895 could be used by debuggers for stack backtraces. Separately, note that
15896 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15897 This happens (for example) for inlined-instances of inline function formal
15898 parameters which are never referenced. This really shouldn't be
15899 happening. All PARM_DECL nodes should get valid non-NULL
15900 DECL_INCOMING_RTL values. FIXME. */
15901
15902 /* Use DECL_RTL as the "location" unless we find something better. */
15903 rtl = DECL_RTL_IF_SET (decl);
15904
15905 /* When generating abstract instances, ignore everything except
15906 constants, symbols living in memory, and symbols living in
15907 fixed registers. */
15908 if (! reload_completed)
15909 {
15910 if (rtl
15911 && (CONSTANT_P (rtl)
15912 || (MEM_P (rtl)
15913 && CONSTANT_P (XEXP (rtl, 0)))
15914 || (REG_P (rtl)
15915 && TREE_CODE (decl) == VAR_DECL
15916 && TREE_STATIC (decl))))
15917 {
15918 rtl = targetm.delegitimize_address (rtl);
15919 return rtl;
15920 }
15921 rtl = NULL_RTX;
15922 }
15923 else if (TREE_CODE (decl) == PARM_DECL)
15924 {
15925 if (rtl == NULL_RTX
15926 || is_pseudo_reg (rtl)
15927 || (MEM_P (rtl)
15928 && is_pseudo_reg (XEXP (rtl, 0))
15929 && DECL_INCOMING_RTL (decl)
15930 && MEM_P (DECL_INCOMING_RTL (decl))
15931 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15932 {
15933 tree declared_type = TREE_TYPE (decl);
15934 tree passed_type = DECL_ARG_TYPE (decl);
15935 machine_mode dmode = TYPE_MODE (declared_type);
15936 machine_mode pmode = TYPE_MODE (passed_type);
15937
15938 /* This decl represents a formal parameter which was optimized out.
15939 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15940 all cases where (rtl == NULL_RTX) just below. */
15941 if (dmode == pmode)
15942 rtl = DECL_INCOMING_RTL (decl);
15943 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15944 && SCALAR_INT_MODE_P (dmode)
15945 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15946 && DECL_INCOMING_RTL (decl))
15947 {
15948 rtx inc = DECL_INCOMING_RTL (decl);
15949 if (REG_P (inc))
15950 rtl = inc;
15951 else if (MEM_P (inc))
15952 {
15953 if (BYTES_BIG_ENDIAN)
15954 rtl = adjust_address_nv (inc, dmode,
15955 GET_MODE_SIZE (pmode)
15956 - GET_MODE_SIZE (dmode));
15957 else
15958 rtl = inc;
15959 }
15960 }
15961 }
15962
15963 /* If the parm was passed in registers, but lives on the stack, then
15964 make a big endian correction if the mode of the type of the
15965 parameter is not the same as the mode of the rtl. */
15966 /* ??? This is the same series of checks that are made in dbxout.c before
15967 we reach the big endian correction code there. It isn't clear if all
15968 of these checks are necessary here, but keeping them all is the safe
15969 thing to do. */
15970 else if (MEM_P (rtl)
15971 && XEXP (rtl, 0) != const0_rtx
15972 && ! CONSTANT_P (XEXP (rtl, 0))
15973 /* Not passed in memory. */
15974 && !MEM_P (DECL_INCOMING_RTL (decl))
15975 /* Not passed by invisible reference. */
15976 && (!REG_P (XEXP (rtl, 0))
15977 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15978 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15979 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15980 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15981 #endif
15982 )
15983 /* Big endian correction check. */
15984 && BYTES_BIG_ENDIAN
15985 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15986 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15987 < UNITS_PER_WORD))
15988 {
15989 machine_mode addr_mode = get_address_mode (rtl);
15990 int offset = (UNITS_PER_WORD
15991 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15992
15993 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15994 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15995 }
15996 }
15997 else if (TREE_CODE (decl) == VAR_DECL
15998 && rtl
15999 && MEM_P (rtl)
16000 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16001 && BYTES_BIG_ENDIAN)
16002 {
16003 machine_mode addr_mode = get_address_mode (rtl);
16004 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16005 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16006
16007 /* If a variable is declared "register" yet is smaller than
16008 a register, then if we store the variable to memory, it
16009 looks like we're storing a register-sized value, when in
16010 fact we are not. We need to adjust the offset of the
16011 storage location to reflect the actual value's bytes,
16012 else gdb will not be able to display it. */
16013 if (rsize > dsize)
16014 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16015 plus_constant (addr_mode, XEXP (rtl, 0),
16016 rsize - dsize));
16017 }
16018
16019 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16020 and will have been substituted directly into all expressions that use it.
16021 C does not have such a concept, but C++ and other languages do. */
16022 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16023 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16024
16025 if (rtl)
16026 rtl = targetm.delegitimize_address (rtl);
16027
16028 /* If we don't look past the constant pool, we risk emitting a
16029 reference to a constant pool entry that isn't referenced from
16030 code, and thus is not emitted. */
16031 if (rtl)
16032 rtl = avoid_constant_pool_reference (rtl);
16033
16034 /* Try harder to get a rtl. If this symbol ends up not being emitted
16035 in the current CU, resolve_addr will remove the expression referencing
16036 it. */
16037 if (rtl == NULL_RTX
16038 && TREE_CODE (decl) == VAR_DECL
16039 && !DECL_EXTERNAL (decl)
16040 && TREE_STATIC (decl)
16041 && DECL_NAME (decl)
16042 && !DECL_HARD_REGISTER (decl)
16043 && DECL_MODE (decl) != VOIDmode)
16044 {
16045 rtl = make_decl_rtl_for_debug (decl);
16046 if (!MEM_P (rtl)
16047 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16048 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16049 rtl = NULL_RTX;
16050 }
16051
16052 return rtl;
16053 }
16054
16055 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16056 returned. If so, the decl for the COMMON block is returned, and the
16057 value is the offset into the common block for the symbol. */
16058
16059 static tree
16060 fortran_common (tree decl, HOST_WIDE_INT *value)
16061 {
16062 tree val_expr, cvar;
16063 machine_mode mode;
16064 HOST_WIDE_INT bitsize, bitpos;
16065 tree offset;
16066 int unsignedp, volatilep = 0;
16067
16068 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16069 it does not have a value (the offset into the common area), or if it
16070 is thread local (as opposed to global) then it isn't common, and shouldn't
16071 be handled as such. */
16072 if (TREE_CODE (decl) != VAR_DECL
16073 || !TREE_STATIC (decl)
16074 || !DECL_HAS_VALUE_EXPR_P (decl)
16075 || !is_fortran ())
16076 return NULL_TREE;
16077
16078 val_expr = DECL_VALUE_EXPR (decl);
16079 if (TREE_CODE (val_expr) != COMPONENT_REF)
16080 return NULL_TREE;
16081
16082 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16083 &mode, &unsignedp, &volatilep, true);
16084
16085 if (cvar == NULL_TREE
16086 || TREE_CODE (cvar) != VAR_DECL
16087 || DECL_ARTIFICIAL (cvar)
16088 || !TREE_PUBLIC (cvar))
16089 return NULL_TREE;
16090
16091 *value = 0;
16092 if (offset != NULL)
16093 {
16094 if (!tree_fits_shwi_p (offset))
16095 return NULL_TREE;
16096 *value = tree_to_shwi (offset);
16097 }
16098 if (bitpos != 0)
16099 *value += bitpos / BITS_PER_UNIT;
16100
16101 return cvar;
16102 }
16103
16104 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16105 data attribute for a variable or a parameter. We generate the
16106 DW_AT_const_value attribute only in those cases where the given variable
16107 or parameter does not have a true "location" either in memory or in a
16108 register. This can happen (for example) when a constant is passed as an
16109 actual argument in a call to an inline function. (It's possible that
16110 these things can crop up in other ways also.) Note that one type of
16111 constant value which can be passed into an inlined function is a constant
16112 pointer. This can happen for example if an actual argument in an inlined
16113 function call evaluates to a compile-time constant address.
16114
16115 CACHE_P is true if it is worth caching the location list for DECL,
16116 so that future calls can reuse it rather than regenerate it from scratch.
16117 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16118 since we will need to refer to them each time the function is inlined. */
16119
16120 static bool
16121 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16122 enum dwarf_attribute attr)
16123 {
16124 rtx rtl;
16125 dw_loc_list_ref list;
16126 var_loc_list *loc_list;
16127 cached_dw_loc_list *cache;
16128
16129 if (TREE_CODE (decl) == ERROR_MARK)
16130 return false;
16131
16132 if (get_AT (die, attr))
16133 return true;
16134
16135 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16136 || TREE_CODE (decl) == RESULT_DECL);
16137
16138 /* Try to get some constant RTL for this decl, and use that as the value of
16139 the location. */
16140
16141 rtl = rtl_for_decl_location (decl);
16142 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16143 && add_const_value_attribute (die, rtl))
16144 return true;
16145
16146 /* See if we have single element location list that is equivalent to
16147 a constant value. That way we are better to use add_const_value_attribute
16148 rather than expanding constant value equivalent. */
16149 loc_list = lookup_decl_loc (decl);
16150 if (loc_list
16151 && loc_list->first
16152 && loc_list->first->next == NULL
16153 && NOTE_P (loc_list->first->loc)
16154 && NOTE_VAR_LOCATION (loc_list->first->loc)
16155 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16156 {
16157 struct var_loc_node *node;
16158
16159 node = loc_list->first;
16160 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16161 if (GET_CODE (rtl) == EXPR_LIST)
16162 rtl = XEXP (rtl, 0);
16163 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16164 && add_const_value_attribute (die, rtl))
16165 return true;
16166 }
16167 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16168 list several times. See if we've already cached the contents. */
16169 list = NULL;
16170 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16171 cache_p = false;
16172 if (cache_p)
16173 {
16174 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16175 if (cache)
16176 list = cache->loc_list;
16177 }
16178 if (list == NULL)
16179 {
16180 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16181 NULL);
16182 /* It is usually worth caching this result if the decl is from
16183 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16184 if (cache_p && list && list->dw_loc_next)
16185 {
16186 cached_dw_loc_list **slot
16187 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16188 DECL_UID (decl),
16189 INSERT);
16190 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16191 cache->decl_id = DECL_UID (decl);
16192 cache->loc_list = list;
16193 *slot = cache;
16194 }
16195 }
16196 if (list)
16197 {
16198 add_AT_location_description (die, attr, list);
16199 return true;
16200 }
16201 /* None of that worked, so it must not really have a location;
16202 try adding a constant value attribute from the DECL_INITIAL. */
16203 return tree_add_const_value_attribute_for_decl (die, decl);
16204 }
16205
16206 /* Helper function for tree_add_const_value_attribute. Natively encode
16207 initializer INIT into an array. Return true if successful. */
16208
16209 static bool
16210 native_encode_initializer (tree init, unsigned char *array, int size)
16211 {
16212 tree type;
16213
16214 if (init == NULL_TREE)
16215 return false;
16216
16217 STRIP_NOPS (init);
16218 switch (TREE_CODE (init))
16219 {
16220 case STRING_CST:
16221 type = TREE_TYPE (init);
16222 if (TREE_CODE (type) == ARRAY_TYPE)
16223 {
16224 tree enttype = TREE_TYPE (type);
16225 machine_mode mode = TYPE_MODE (enttype);
16226
16227 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16228 return false;
16229 if (int_size_in_bytes (type) != size)
16230 return false;
16231 if (size > TREE_STRING_LENGTH (init))
16232 {
16233 memcpy (array, TREE_STRING_POINTER (init),
16234 TREE_STRING_LENGTH (init));
16235 memset (array + TREE_STRING_LENGTH (init),
16236 '\0', size - TREE_STRING_LENGTH (init));
16237 }
16238 else
16239 memcpy (array, TREE_STRING_POINTER (init), size);
16240 return true;
16241 }
16242 return false;
16243 case CONSTRUCTOR:
16244 type = TREE_TYPE (init);
16245 if (int_size_in_bytes (type) != size)
16246 return false;
16247 if (TREE_CODE (type) == ARRAY_TYPE)
16248 {
16249 HOST_WIDE_INT min_index;
16250 unsigned HOST_WIDE_INT cnt;
16251 int curpos = 0, fieldsize;
16252 constructor_elt *ce;
16253
16254 if (TYPE_DOMAIN (type) == NULL_TREE
16255 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16256 return false;
16257
16258 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16259 if (fieldsize <= 0)
16260 return false;
16261
16262 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16263 memset (array, '\0', size);
16264 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16265 {
16266 tree val = ce->value;
16267 tree index = ce->index;
16268 int pos = curpos;
16269 if (index && TREE_CODE (index) == RANGE_EXPR)
16270 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16271 * fieldsize;
16272 else if (index)
16273 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16274
16275 if (val)
16276 {
16277 STRIP_NOPS (val);
16278 if (!native_encode_initializer (val, array + pos, fieldsize))
16279 return false;
16280 }
16281 curpos = pos + fieldsize;
16282 if (index && TREE_CODE (index) == RANGE_EXPR)
16283 {
16284 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16285 - tree_to_shwi (TREE_OPERAND (index, 0));
16286 while (count-- > 0)
16287 {
16288 if (val)
16289 memcpy (array + curpos, array + pos, fieldsize);
16290 curpos += fieldsize;
16291 }
16292 }
16293 gcc_assert (curpos <= size);
16294 }
16295 return true;
16296 }
16297 else if (TREE_CODE (type) == RECORD_TYPE
16298 || TREE_CODE (type) == UNION_TYPE)
16299 {
16300 tree field = NULL_TREE;
16301 unsigned HOST_WIDE_INT cnt;
16302 constructor_elt *ce;
16303
16304 if (int_size_in_bytes (type) != size)
16305 return false;
16306
16307 if (TREE_CODE (type) == RECORD_TYPE)
16308 field = TYPE_FIELDS (type);
16309
16310 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16311 {
16312 tree val = ce->value;
16313 int pos, fieldsize;
16314
16315 if (ce->index != 0)
16316 field = ce->index;
16317
16318 if (val)
16319 STRIP_NOPS (val);
16320
16321 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16322 return false;
16323
16324 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16325 && TYPE_DOMAIN (TREE_TYPE (field))
16326 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16327 return false;
16328 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16329 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16330 return false;
16331 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16332 pos = int_byte_position (field);
16333 gcc_assert (pos + fieldsize <= size);
16334 if (val
16335 && !native_encode_initializer (val, array + pos, fieldsize))
16336 return false;
16337 }
16338 return true;
16339 }
16340 return false;
16341 case VIEW_CONVERT_EXPR:
16342 case NON_LVALUE_EXPR:
16343 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16344 default:
16345 return native_encode_expr (init, array, size) == size;
16346 }
16347 }
16348
16349 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16350 attribute is the const value T. */
16351
16352 static bool
16353 tree_add_const_value_attribute (dw_die_ref die, tree t)
16354 {
16355 tree init;
16356 tree type = TREE_TYPE (t);
16357 rtx rtl;
16358
16359 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16360 return false;
16361
16362 init = t;
16363 gcc_assert (!DECL_P (init));
16364
16365 rtl = rtl_for_decl_init (init, type);
16366 if (rtl)
16367 return add_const_value_attribute (die, rtl);
16368 /* If the host and target are sane, try harder. */
16369 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16370 && initializer_constant_valid_p (init, type))
16371 {
16372 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16373 if (size > 0 && (int) size == size)
16374 {
16375 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16376
16377 if (native_encode_initializer (init, array, size))
16378 {
16379 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16380 return true;
16381 }
16382 ggc_free (array);
16383 }
16384 }
16385 return false;
16386 }
16387
16388 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16389 attribute is the const value of T, where T is an integral constant
16390 variable with static storage duration
16391 (so it can't be a PARM_DECL or a RESULT_DECL). */
16392
16393 static bool
16394 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16395 {
16396
16397 if (!decl
16398 || (TREE_CODE (decl) != VAR_DECL
16399 && TREE_CODE (decl) != CONST_DECL)
16400 || (TREE_CODE (decl) == VAR_DECL
16401 && !TREE_STATIC (decl)))
16402 return false;
16403
16404 if (TREE_READONLY (decl)
16405 && ! TREE_THIS_VOLATILE (decl)
16406 && DECL_INITIAL (decl))
16407 /* OK */;
16408 else
16409 return false;
16410
16411 /* Don't add DW_AT_const_value if abstract origin already has one. */
16412 if (get_AT (var_die, DW_AT_const_value))
16413 return false;
16414
16415 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16416 }
16417
16418 /* Convert the CFI instructions for the current function into a
16419 location list. This is used for DW_AT_frame_base when we targeting
16420 a dwarf2 consumer that does not support the dwarf3
16421 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16422 expressions. */
16423
16424 static dw_loc_list_ref
16425 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16426 {
16427 int ix;
16428 dw_fde_ref fde;
16429 dw_loc_list_ref list, *list_tail;
16430 dw_cfi_ref cfi;
16431 dw_cfa_location last_cfa, next_cfa;
16432 const char *start_label, *last_label, *section;
16433 dw_cfa_location remember;
16434
16435 fde = cfun->fde;
16436 gcc_assert (fde != NULL);
16437
16438 section = secname_for_decl (current_function_decl);
16439 list_tail = &list;
16440 list = NULL;
16441
16442 memset (&next_cfa, 0, sizeof (next_cfa));
16443 next_cfa.reg = INVALID_REGNUM;
16444 remember = next_cfa;
16445
16446 start_label = fde->dw_fde_begin;
16447
16448 /* ??? Bald assumption that the CIE opcode list does not contain
16449 advance opcodes. */
16450 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16451 lookup_cfa_1 (cfi, &next_cfa, &remember);
16452
16453 last_cfa = next_cfa;
16454 last_label = start_label;
16455
16456 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16457 {
16458 /* If the first partition contained no CFI adjustments, the
16459 CIE opcodes apply to the whole first partition. */
16460 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16461 fde->dw_fde_begin, fde->dw_fde_end, section);
16462 list_tail =&(*list_tail)->dw_loc_next;
16463 start_label = last_label = fde->dw_fde_second_begin;
16464 }
16465
16466 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16467 {
16468 switch (cfi->dw_cfi_opc)
16469 {
16470 case DW_CFA_set_loc:
16471 case DW_CFA_advance_loc1:
16472 case DW_CFA_advance_loc2:
16473 case DW_CFA_advance_loc4:
16474 if (!cfa_equal_p (&last_cfa, &next_cfa))
16475 {
16476 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16477 start_label, last_label, section);
16478
16479 list_tail = &(*list_tail)->dw_loc_next;
16480 last_cfa = next_cfa;
16481 start_label = last_label;
16482 }
16483 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16484 break;
16485
16486 case DW_CFA_advance_loc:
16487 /* The encoding is complex enough that we should never emit this. */
16488 gcc_unreachable ();
16489
16490 default:
16491 lookup_cfa_1 (cfi, &next_cfa, &remember);
16492 break;
16493 }
16494 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16495 {
16496 if (!cfa_equal_p (&last_cfa, &next_cfa))
16497 {
16498 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16499 start_label, last_label, section);
16500
16501 list_tail = &(*list_tail)->dw_loc_next;
16502 last_cfa = next_cfa;
16503 start_label = last_label;
16504 }
16505 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16506 start_label, fde->dw_fde_end, section);
16507 list_tail = &(*list_tail)->dw_loc_next;
16508 start_label = last_label = fde->dw_fde_second_begin;
16509 }
16510 }
16511
16512 if (!cfa_equal_p (&last_cfa, &next_cfa))
16513 {
16514 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16515 start_label, last_label, section);
16516 list_tail = &(*list_tail)->dw_loc_next;
16517 start_label = last_label;
16518 }
16519
16520 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16521 start_label,
16522 fde->dw_fde_second_begin
16523 ? fde->dw_fde_second_end : fde->dw_fde_end,
16524 section);
16525
16526 if (list && list->dw_loc_next)
16527 gen_llsym (list);
16528
16529 return list;
16530 }
16531
16532 /* Compute a displacement from the "steady-state frame pointer" to the
16533 frame base (often the same as the CFA), and store it in
16534 frame_pointer_fb_offset. OFFSET is added to the displacement
16535 before the latter is negated. */
16536
16537 static void
16538 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16539 {
16540 rtx reg, elim;
16541
16542 #ifdef FRAME_POINTER_CFA_OFFSET
16543 reg = frame_pointer_rtx;
16544 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16545 #else
16546 reg = arg_pointer_rtx;
16547 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16548 #endif
16549
16550 elim = (ira_use_lra_p
16551 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16552 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16553 if (GET_CODE (elim) == PLUS)
16554 {
16555 offset += INTVAL (XEXP (elim, 1));
16556 elim = XEXP (elim, 0);
16557 }
16558
16559 frame_pointer_fb_offset = -offset;
16560
16561 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16562 in which to eliminate. This is because it's stack pointer isn't
16563 directly accessible as a register within the ISA. To work around
16564 this, assume that while we cannot provide a proper value for
16565 frame_pointer_fb_offset, we won't need one either. */
16566 frame_pointer_fb_offset_valid
16567 = ((SUPPORTS_STACK_ALIGNMENT
16568 && (elim == hard_frame_pointer_rtx
16569 || elim == stack_pointer_rtx))
16570 || elim == (frame_pointer_needed
16571 ? hard_frame_pointer_rtx
16572 : stack_pointer_rtx));
16573 }
16574
16575 /* Generate a DW_AT_name attribute given some string value to be included as
16576 the value of the attribute. */
16577
16578 static void
16579 add_name_attribute (dw_die_ref die, const char *name_string)
16580 {
16581 if (name_string != NULL && *name_string != 0)
16582 {
16583 if (demangle_name_func)
16584 name_string = (*demangle_name_func) (name_string);
16585
16586 add_AT_string (die, DW_AT_name, name_string);
16587 }
16588 }
16589
16590 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16591 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16592 of TYPE accordingly.
16593
16594 ??? This is a temporary measure until after we're able to generate
16595 regular DWARF for the complex Ada type system. */
16596
16597 static void
16598 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16599 dw_die_ref context_die)
16600 {
16601 tree dtype;
16602 dw_die_ref dtype_die;
16603
16604 if (!lang_hooks.types.descriptive_type)
16605 return;
16606
16607 dtype = lang_hooks.types.descriptive_type (type);
16608 if (!dtype)
16609 return;
16610
16611 dtype_die = lookup_type_die (dtype);
16612 if (!dtype_die)
16613 {
16614 gen_type_die (dtype, context_die);
16615 dtype_die = lookup_type_die (dtype);
16616 gcc_assert (dtype_die);
16617 }
16618
16619 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16620 }
16621
16622 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16623
16624 static const char *
16625 comp_dir_string (void)
16626 {
16627 const char *wd;
16628 char *wd1;
16629 static const char *cached_wd = NULL;
16630
16631 if (cached_wd != NULL)
16632 return cached_wd;
16633
16634 wd = get_src_pwd ();
16635 if (wd == NULL)
16636 return NULL;
16637
16638 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16639 {
16640 int wdlen;
16641
16642 wdlen = strlen (wd);
16643 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16644 strcpy (wd1, wd);
16645 wd1 [wdlen] = DIR_SEPARATOR;
16646 wd1 [wdlen + 1] = 0;
16647 wd = wd1;
16648 }
16649
16650 cached_wd = remap_debug_filename (wd);
16651 return cached_wd;
16652 }
16653
16654 /* Generate a DW_AT_comp_dir attribute for DIE. */
16655
16656 static void
16657 add_comp_dir_attribute (dw_die_ref die)
16658 {
16659 const char * wd = comp_dir_string ();
16660 if (wd != NULL)
16661 add_AT_string (die, DW_AT_comp_dir, wd);
16662 }
16663
16664 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16665 pointer computation, ...), output a representation for that bound according
16666 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16667 loc_list_from_tree for the meaning of CONTEXT. */
16668
16669 static void
16670 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16671 int forms, const struct loc_descr_context *context)
16672 {
16673 dw_die_ref ctx, decl_die;
16674 dw_loc_list_ref list;
16675
16676 bool strip_conversions = true;
16677
16678 while (strip_conversions)
16679 switch (TREE_CODE (value))
16680 {
16681 case ERROR_MARK:
16682 case SAVE_EXPR:
16683 return;
16684
16685 CASE_CONVERT:
16686 case VIEW_CONVERT_EXPR:
16687 value = TREE_OPERAND (value, 0);
16688 break;
16689
16690 default:
16691 strip_conversions = false;
16692 break;
16693 }
16694
16695 /* If possible and permitted, output the attribute as a constant. */
16696 if ((forms & dw_scalar_form_constant) != 0
16697 && TREE_CODE (value) == INTEGER_CST)
16698 {
16699 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16700
16701 /* If HOST_WIDE_INT is big enough then represent the bound as
16702 a constant value. We need to choose a form based on
16703 whether the type is signed or unsigned. We cannot just
16704 call add_AT_unsigned if the value itself is positive
16705 (add_AT_unsigned might add the unsigned value encoded as
16706 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16707 bounds type and then sign extend any unsigned values found
16708 for signed types. This is needed only for
16709 DW_AT_{lower,upper}_bound, since for most other attributes,
16710 consumers will treat DW_FORM_data[1248] as unsigned values,
16711 regardless of the underlying type. */
16712 if (prec <= HOST_BITS_PER_WIDE_INT
16713 || tree_fits_uhwi_p (value))
16714 {
16715 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16716 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16717 else
16718 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16719 }
16720 else
16721 /* Otherwise represent the bound as an unsigned value with
16722 the precision of its type. The precision and signedness
16723 of the type will be necessary to re-interpret it
16724 unambiguously. */
16725 add_AT_wide (die, attr, value);
16726 return;
16727 }
16728
16729 /* Otherwise, if it's possible and permitted too, output a reference to
16730 another DIE. */
16731 if ((forms & dw_scalar_form_reference) != 0)
16732 {
16733 tree decl = NULL_TREE;
16734
16735 /* Some type attributes reference an outer type. For instance, the upper
16736 bound of an array may reference an embedding record (this happens in
16737 Ada). */
16738 if (TREE_CODE (value) == COMPONENT_REF
16739 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16740 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16741 decl = TREE_OPERAND (value, 1);
16742
16743 else if (TREE_CODE (value) == VAR_DECL
16744 || TREE_CODE (value) == PARM_DECL
16745 || TREE_CODE (value) == RESULT_DECL)
16746 decl = value;
16747
16748 if (decl != NULL_TREE)
16749 {
16750 dw_die_ref decl_die = lookup_decl_die (decl);
16751
16752 /* ??? Can this happen, or should the variable have been bound
16753 first? Probably it can, since I imagine that we try to create
16754 the types of parameters in the order in which they exist in
16755 the list, and won't have created a forward reference to a
16756 later parameter. */
16757 if (decl_die != NULL)
16758 {
16759 add_AT_die_ref (die, attr, decl_die);
16760 return;
16761 }
16762 }
16763 }
16764
16765 /* Last chance: try to create a stack operation procedure to evaluate the
16766 value. Do nothing if even that is not possible or permitted. */
16767 if ((forms & dw_scalar_form_exprloc) == 0)
16768 return;
16769
16770 list = loc_list_from_tree (value, 2, context);
16771 if (list == NULL || single_element_loc_list_p (list))
16772 {
16773 /* If this attribute is not a reference nor constant, it is
16774 a DWARF expression rather than location description. For that
16775 loc_list_from_tree (value, 0, &context) is needed. */
16776 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16777 if (list2 && single_element_loc_list_p (list2))
16778 {
16779 add_AT_loc (die, attr, list2->expr);
16780 return;
16781 }
16782 }
16783
16784 /* If that failed to give a single element location list, fall back to
16785 outputting this as a reference... still if permitted. */
16786 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16787 return;
16788
16789 if (current_function_decl == 0)
16790 ctx = comp_unit_die ();
16791 else
16792 ctx = lookup_decl_die (current_function_decl);
16793
16794 decl_die = new_die (DW_TAG_variable, ctx, value);
16795 add_AT_flag (decl_die, DW_AT_artificial, 1);
16796 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16797 add_AT_location_description (decl_die, DW_AT_location, list);
16798 add_AT_die_ref (die, attr, decl_die);
16799 }
16800
16801 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16802 default. */
16803
16804 static int
16805 lower_bound_default (void)
16806 {
16807 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16808 {
16809 case DW_LANG_C:
16810 case DW_LANG_C89:
16811 case DW_LANG_C99:
16812 case DW_LANG_C11:
16813 case DW_LANG_C_plus_plus:
16814 case DW_LANG_C_plus_plus_11:
16815 case DW_LANG_C_plus_plus_14:
16816 case DW_LANG_ObjC:
16817 case DW_LANG_ObjC_plus_plus:
16818 case DW_LANG_Java:
16819 return 0;
16820 case DW_LANG_Fortran77:
16821 case DW_LANG_Fortran90:
16822 case DW_LANG_Fortran95:
16823 case DW_LANG_Fortran03:
16824 case DW_LANG_Fortran08:
16825 return 1;
16826 case DW_LANG_UPC:
16827 case DW_LANG_D:
16828 case DW_LANG_Python:
16829 return dwarf_version >= 4 ? 0 : -1;
16830 case DW_LANG_Ada95:
16831 case DW_LANG_Ada83:
16832 case DW_LANG_Cobol74:
16833 case DW_LANG_Cobol85:
16834 case DW_LANG_Pascal83:
16835 case DW_LANG_Modula2:
16836 case DW_LANG_PLI:
16837 return dwarf_version >= 4 ? 1 : -1;
16838 default:
16839 return -1;
16840 }
16841 }
16842
16843 /* Given a tree node describing an array bound (either lower or upper) output
16844 a representation for that bound. */
16845
16846 static void
16847 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16848 tree bound, const struct loc_descr_context *context)
16849 {
16850 int dflt;
16851
16852 while (1)
16853 switch (TREE_CODE (bound))
16854 {
16855 /* Strip all conversions. */
16856 CASE_CONVERT:
16857 case VIEW_CONVERT_EXPR:
16858 bound = TREE_OPERAND (bound, 0);
16859 break;
16860
16861 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16862 are even omitted when they are the default. */
16863 case INTEGER_CST:
16864 /* If the value for this bound is the default one, we can even omit the
16865 attribute. */
16866 if (bound_attr == DW_AT_lower_bound
16867 && tree_fits_shwi_p (bound)
16868 && (dflt = lower_bound_default ()) != -1
16869 && tree_to_shwi (bound) == dflt)
16870 return;
16871
16872 /* FALLTHRU */
16873
16874 default:
16875 add_scalar_info (subrange_die, bound_attr, bound,
16876 dw_scalar_form_constant
16877 | dw_scalar_form_exprloc
16878 | dw_scalar_form_reference,
16879 context);
16880 return;
16881 }
16882 }
16883
16884 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16885 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16886 Note that the block of subscript information for an array type also
16887 includes information about the element type of the given array type.
16888
16889 This function reuses previously set type and bound information if
16890 available. */
16891
16892 static void
16893 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16894 {
16895 unsigned dimension_number;
16896 tree lower, upper;
16897 dw_die_ref child = type_die->die_child;
16898
16899 for (dimension_number = 0;
16900 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16901 type = TREE_TYPE (type), dimension_number++)
16902 {
16903 tree domain = TYPE_DOMAIN (type);
16904
16905 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16906 break;
16907
16908 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16909 and (in GNU C only) variable bounds. Handle all three forms
16910 here. */
16911
16912 /* Find and reuse a previously generated DW_TAG_subrange_type if
16913 available.
16914
16915 For multi-dimensional arrays, as we iterate through the
16916 various dimensions in the enclosing for loop above, we also
16917 iterate through the DIE children and pick at each
16918 DW_TAG_subrange_type previously generated (if available).
16919 Each child DW_TAG_subrange_type DIE describes the range of
16920 the current dimension. At this point we should have as many
16921 DW_TAG_subrange_type's as we have dimensions in the
16922 array. */
16923 dw_die_ref subrange_die = NULL;
16924 if (child)
16925 while (1)
16926 {
16927 child = child->die_sib;
16928 if (child->die_tag == DW_TAG_subrange_type)
16929 subrange_die = child;
16930 if (child == type_die->die_child)
16931 {
16932 /* If we wrapped around, stop looking next time. */
16933 child = NULL;
16934 break;
16935 }
16936 if (child->die_tag == DW_TAG_subrange_type)
16937 break;
16938 }
16939 if (!subrange_die)
16940 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16941
16942 if (domain)
16943 {
16944 /* We have an array type with specified bounds. */
16945 lower = TYPE_MIN_VALUE (domain);
16946 upper = TYPE_MAX_VALUE (domain);
16947
16948 /* Define the index type. */
16949 if (TREE_TYPE (domain)
16950 && !get_AT (subrange_die, DW_AT_type))
16951 {
16952 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16953 TREE_TYPE field. We can't emit debug info for this
16954 because it is an unnamed integral type. */
16955 if (TREE_CODE (domain) == INTEGER_TYPE
16956 && TYPE_NAME (domain) == NULL_TREE
16957 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16958 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16959 ;
16960 else
16961 add_type_attribute (subrange_die, TREE_TYPE (domain),
16962 TYPE_UNQUALIFIED, type_die);
16963 }
16964
16965 /* ??? If upper is NULL, the array has unspecified length,
16966 but it does have a lower bound. This happens with Fortran
16967 dimension arr(N:*)
16968 Since the debugger is definitely going to need to know N
16969 to produce useful results, go ahead and output the lower
16970 bound solo, and hope the debugger can cope. */
16971
16972 if (!get_AT (subrange_die, DW_AT_lower_bound))
16973 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16974 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16975 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16976 }
16977
16978 /* Otherwise we have an array type with an unspecified length. The
16979 DWARF-2 spec does not say how to handle this; let's just leave out the
16980 bounds. */
16981 }
16982 }
16983
16984 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16985
16986 static void
16987 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16988 {
16989 dw_die_ref decl_die;
16990 HOST_WIDE_INT size;
16991
16992 switch (TREE_CODE (tree_node))
16993 {
16994 case ERROR_MARK:
16995 size = 0;
16996 break;
16997 case ENUMERAL_TYPE:
16998 case RECORD_TYPE:
16999 case UNION_TYPE:
17000 case QUAL_UNION_TYPE:
17001 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17002 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17003 {
17004 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17005 return;
17006 }
17007 size = int_size_in_bytes (tree_node);
17008 break;
17009 case FIELD_DECL:
17010 /* For a data member of a struct or union, the DW_AT_byte_size is
17011 generally given as the number of bytes normally allocated for an
17012 object of the *declared* type of the member itself. This is true
17013 even for bit-fields. */
17014 size = int_size_in_bytes (field_type (tree_node));
17015 break;
17016 default:
17017 gcc_unreachable ();
17018 }
17019
17020 /* Note that `size' might be -1 when we get to this point. If it is, that
17021 indicates that the byte size of the entity in question is variable. We
17022 have no good way of expressing this fact in Dwarf at the present time,
17023 when location description was not used by the caller code instead. */
17024 if (size >= 0)
17025 add_AT_unsigned (die, DW_AT_byte_size, size);
17026 }
17027
17028 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17029 which specifies the distance in bits from the highest order bit of the
17030 "containing object" for the bit-field to the highest order bit of the
17031 bit-field itself.
17032
17033 For any given bit-field, the "containing object" is a hypothetical object
17034 (of some integral or enum type) within which the given bit-field lives. The
17035 type of this hypothetical "containing object" is always the same as the
17036 declared type of the individual bit-field itself. The determination of the
17037 exact location of the "containing object" for a bit-field is rather
17038 complicated. It's handled by the `field_byte_offset' function (above).
17039
17040 Note that it is the size (in bytes) of the hypothetical "containing object"
17041 which will be given in the DW_AT_byte_size attribute for this bit-field.
17042 (See `byte_size_attribute' above). */
17043
17044 static inline void
17045 add_bit_offset_attribute (dw_die_ref die, tree decl)
17046 {
17047 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17048 tree type = DECL_BIT_FIELD_TYPE (decl);
17049 HOST_WIDE_INT bitpos_int;
17050 HOST_WIDE_INT highest_order_object_bit_offset;
17051 HOST_WIDE_INT highest_order_field_bit_offset;
17052 HOST_WIDE_INT bit_offset;
17053
17054 /* Must be a field and a bit field. */
17055 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17056
17057 /* We can't yet handle bit-fields whose offsets are variable, so if we
17058 encounter such things, just return without generating any attribute
17059 whatsoever. Likewise for variable or too large size. */
17060 if (! tree_fits_shwi_p (bit_position (decl))
17061 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17062 return;
17063
17064 bitpos_int = int_bit_position (decl);
17065
17066 /* Note that the bit offset is always the distance (in bits) from the
17067 highest-order bit of the "containing object" to the highest-order bit of
17068 the bit-field itself. Since the "high-order end" of any object or field
17069 is different on big-endian and little-endian machines, the computation
17070 below must take account of these differences. */
17071 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17072 highest_order_field_bit_offset = bitpos_int;
17073
17074 if (! BYTES_BIG_ENDIAN)
17075 {
17076 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17077 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17078 }
17079
17080 bit_offset
17081 = (! BYTES_BIG_ENDIAN
17082 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17083 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17084
17085 if (bit_offset < 0)
17086 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17087 else
17088 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17089 }
17090
17091 /* For a FIELD_DECL node which represents a bit field, output an attribute
17092 which specifies the length in bits of the given field. */
17093
17094 static inline void
17095 add_bit_size_attribute (dw_die_ref die, tree decl)
17096 {
17097 /* Must be a field and a bit field. */
17098 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17099 && DECL_BIT_FIELD_TYPE (decl));
17100
17101 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17102 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17103 }
17104
17105 /* If the compiled language is ANSI C, then add a 'prototyped'
17106 attribute, if arg types are given for the parameters of a function. */
17107
17108 static inline void
17109 add_prototyped_attribute (dw_die_ref die, tree func_type)
17110 {
17111 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17112 {
17113 case DW_LANG_C:
17114 case DW_LANG_C89:
17115 case DW_LANG_C99:
17116 case DW_LANG_C11:
17117 case DW_LANG_ObjC:
17118 if (prototype_p (func_type))
17119 add_AT_flag (die, DW_AT_prototyped, 1);
17120 break;
17121 default:
17122 break;
17123 }
17124 }
17125
17126 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17127 by looking in either the type declaration or object declaration
17128 equate table. */
17129
17130 static inline dw_die_ref
17131 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17132 {
17133 dw_die_ref origin_die = NULL;
17134
17135 if (TREE_CODE (origin) != FUNCTION_DECL)
17136 {
17137 /* We may have gotten separated from the block for the inlined
17138 function, if we're in an exception handler or some such; make
17139 sure that the abstract function has been written out.
17140
17141 Doing this for nested functions is wrong, however; functions are
17142 distinct units, and our context might not even be inline. */
17143 tree fn = origin;
17144
17145 if (TYPE_P (fn))
17146 fn = TYPE_STUB_DECL (fn);
17147
17148 fn = decl_function_context (fn);
17149 if (fn)
17150 dwarf2out_abstract_function (fn);
17151 }
17152
17153 if (DECL_P (origin))
17154 origin_die = lookup_decl_die (origin);
17155 else if (TYPE_P (origin))
17156 origin_die = lookup_type_die (origin);
17157
17158 /* XXX: Functions that are never lowered don't always have correct block
17159 trees (in the case of java, they simply have no block tree, in some other
17160 languages). For these functions, there is nothing we can really do to
17161 output correct debug info for inlined functions in all cases. Rather
17162 than die, we'll just produce deficient debug info now, in that we will
17163 have variables without a proper abstract origin. In the future, when all
17164 functions are lowered, we should re-add a gcc_assert (origin_die)
17165 here. */
17166
17167 if (origin_die)
17168 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17169 return origin_die;
17170 }
17171
17172 /* We do not currently support the pure_virtual attribute. */
17173
17174 static inline void
17175 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17176 {
17177 if (DECL_VINDEX (func_decl))
17178 {
17179 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17180
17181 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17182 add_AT_loc (die, DW_AT_vtable_elem_location,
17183 new_loc_descr (DW_OP_constu,
17184 tree_to_shwi (DECL_VINDEX (func_decl)),
17185 0));
17186
17187 /* GNU extension: Record what type this method came from originally. */
17188 if (debug_info_level > DINFO_LEVEL_TERSE
17189 && DECL_CONTEXT (func_decl))
17190 add_AT_die_ref (die, DW_AT_containing_type,
17191 lookup_type_die (DECL_CONTEXT (func_decl)));
17192 }
17193 }
17194 \f
17195 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17196 given decl. This used to be a vendor extension until after DWARF 4
17197 standardized it. */
17198
17199 static void
17200 add_linkage_attr (dw_die_ref die, tree decl)
17201 {
17202 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17203
17204 /* Mimic what assemble_name_raw does with a leading '*'. */
17205 if (name[0] == '*')
17206 name = &name[1];
17207
17208 if (dwarf_version >= 4)
17209 add_AT_string (die, DW_AT_linkage_name, name);
17210 else
17211 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17212 }
17213
17214 /* Add source coordinate attributes for the given decl. */
17215
17216 static void
17217 add_src_coords_attributes (dw_die_ref die, tree decl)
17218 {
17219 expanded_location s;
17220
17221 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17222 return;
17223 s = expand_location (DECL_SOURCE_LOCATION (decl));
17224 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17225 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17226 }
17227
17228 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17229
17230 static void
17231 add_linkage_name (dw_die_ref die, tree decl)
17232 {
17233 if (debug_info_level > DINFO_LEVEL_NONE
17234 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17235 && TREE_PUBLIC (decl)
17236 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17237 && die->die_tag != DW_TAG_member)
17238 {
17239 /* Defer until we have an assembler name set. */
17240 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17241 {
17242 limbo_die_node *asm_name;
17243
17244 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17245 asm_name->die = die;
17246 asm_name->created_for = decl;
17247 asm_name->next = deferred_asm_name;
17248 deferred_asm_name = asm_name;
17249 }
17250 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17251 add_linkage_attr (die, decl);
17252 }
17253 }
17254
17255 /* Add a DW_AT_name attribute and source coordinate attribute for the
17256 given decl, but only if it actually has a name. */
17257
17258 static void
17259 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17260 {
17261 tree decl_name;
17262
17263 decl_name = DECL_NAME (decl);
17264 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17265 {
17266 const char *name = dwarf2_name (decl, 0);
17267 if (name)
17268 add_name_attribute (die, name);
17269 if (! DECL_ARTIFICIAL (decl))
17270 add_src_coords_attributes (die, decl);
17271
17272 add_linkage_name (die, decl);
17273 }
17274
17275 #ifdef VMS_DEBUGGING_INFO
17276 /* Get the function's name, as described by its RTL. This may be different
17277 from the DECL_NAME name used in the source file. */
17278 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17279 {
17280 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17281 XEXP (DECL_RTL (decl), 0), false);
17282 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17283 }
17284 #endif /* VMS_DEBUGGING_INFO */
17285 }
17286
17287 #ifdef VMS_DEBUGGING_INFO
17288 /* Output the debug main pointer die for VMS */
17289
17290 void
17291 dwarf2out_vms_debug_main_pointer (void)
17292 {
17293 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17294 dw_die_ref die;
17295
17296 /* Allocate the VMS debug main subprogram die. */
17297 die = ggc_cleared_alloc<die_node> ();
17298 die->die_tag = DW_TAG_subprogram;
17299 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17300 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17301 current_function_funcdef_no);
17302 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17303
17304 /* Make it the first child of comp_unit_die (). */
17305 die->die_parent = comp_unit_die ();
17306 if (comp_unit_die ()->die_child)
17307 {
17308 die->die_sib = comp_unit_die ()->die_child->die_sib;
17309 comp_unit_die ()->die_child->die_sib = die;
17310 }
17311 else
17312 {
17313 die->die_sib = die;
17314 comp_unit_die ()->die_child = die;
17315 }
17316 }
17317 #endif /* VMS_DEBUGGING_INFO */
17318
17319 /* Push a new declaration scope. */
17320
17321 static void
17322 push_decl_scope (tree scope)
17323 {
17324 vec_safe_push (decl_scope_table, scope);
17325 }
17326
17327 /* Pop a declaration scope. */
17328
17329 static inline void
17330 pop_decl_scope (void)
17331 {
17332 decl_scope_table->pop ();
17333 }
17334
17335 /* walk_tree helper function for uses_local_type, below. */
17336
17337 static tree
17338 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17339 {
17340 if (!TYPE_P (*tp))
17341 *walk_subtrees = 0;
17342 else
17343 {
17344 tree name = TYPE_NAME (*tp);
17345 if (name && DECL_P (name) && decl_function_context (name))
17346 return *tp;
17347 }
17348 return NULL_TREE;
17349 }
17350
17351 /* If TYPE involves a function-local type (including a local typedef to a
17352 non-local type), returns that type; otherwise returns NULL_TREE. */
17353
17354 static tree
17355 uses_local_type (tree type)
17356 {
17357 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17358 return used;
17359 }
17360
17361 /* Return the DIE for the scope that immediately contains this type.
17362 Non-named types that do not involve a function-local type get global
17363 scope. Named types nested in namespaces or other types get their
17364 containing scope. All other types (i.e. function-local named types) get
17365 the current active scope. */
17366
17367 static dw_die_ref
17368 scope_die_for (tree t, dw_die_ref context_die)
17369 {
17370 dw_die_ref scope_die = NULL;
17371 tree containing_scope;
17372
17373 /* Non-types always go in the current scope. */
17374 gcc_assert (TYPE_P (t));
17375
17376 /* Use the scope of the typedef, rather than the scope of the type
17377 it refers to. */
17378 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17379 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17380 else
17381 containing_scope = TYPE_CONTEXT (t);
17382
17383 /* Use the containing namespace if there is one. */
17384 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17385 {
17386 if (context_die == lookup_decl_die (containing_scope))
17387 /* OK */;
17388 else if (debug_info_level > DINFO_LEVEL_TERSE)
17389 context_die = get_context_die (containing_scope);
17390 else
17391 containing_scope = NULL_TREE;
17392 }
17393
17394 /* Ignore function type "scopes" from the C frontend. They mean that
17395 a tagged type is local to a parmlist of a function declarator, but
17396 that isn't useful to DWARF. */
17397 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17398 containing_scope = NULL_TREE;
17399
17400 if (SCOPE_FILE_SCOPE_P (containing_scope))
17401 {
17402 /* If T uses a local type keep it local as well, to avoid references
17403 to function-local DIEs from outside the function. */
17404 if (current_function_decl && uses_local_type (t))
17405 scope_die = context_die;
17406 else
17407 scope_die = comp_unit_die ();
17408 }
17409 else if (TYPE_P (containing_scope))
17410 {
17411 /* For types, we can just look up the appropriate DIE. */
17412 if (debug_info_level > DINFO_LEVEL_TERSE)
17413 scope_die = get_context_die (containing_scope);
17414 else
17415 {
17416 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17417 if (scope_die == NULL)
17418 scope_die = comp_unit_die ();
17419 }
17420 }
17421 else
17422 scope_die = context_die;
17423
17424 return scope_die;
17425 }
17426
17427 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17428
17429 static inline int
17430 local_scope_p (dw_die_ref context_die)
17431 {
17432 for (; context_die; context_die = context_die->die_parent)
17433 if (context_die->die_tag == DW_TAG_inlined_subroutine
17434 || context_die->die_tag == DW_TAG_subprogram)
17435 return 1;
17436
17437 return 0;
17438 }
17439
17440 /* Returns nonzero if CONTEXT_DIE is a class. */
17441
17442 static inline int
17443 class_scope_p (dw_die_ref context_die)
17444 {
17445 return (context_die
17446 && (context_die->die_tag == DW_TAG_structure_type
17447 || context_die->die_tag == DW_TAG_class_type
17448 || context_die->die_tag == DW_TAG_interface_type
17449 || context_die->die_tag == DW_TAG_union_type));
17450 }
17451
17452 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17453 whether or not to treat a DIE in this context as a declaration. */
17454
17455 static inline int
17456 class_or_namespace_scope_p (dw_die_ref context_die)
17457 {
17458 return (class_scope_p (context_die)
17459 || (context_die && context_die->die_tag == DW_TAG_namespace));
17460 }
17461
17462 /* Many forms of DIEs require a "type description" attribute. This
17463 routine locates the proper "type descriptor" die for the type given
17464 by 'type' plus any additional qualifiers given by 'cv_quals', and
17465 adds a DW_AT_type attribute below the given die. */
17466
17467 static void
17468 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17469 dw_die_ref context_die)
17470 {
17471 enum tree_code code = TREE_CODE (type);
17472 dw_die_ref type_die = NULL;
17473
17474 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17475 or fixed-point type, use the inner type. This is because we have no
17476 support for unnamed types in base_type_die. This can happen if this is
17477 an Ada subrange type. Correct solution is emit a subrange type die. */
17478 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17479 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17480 type = TREE_TYPE (type), code = TREE_CODE (type);
17481
17482 if (code == ERROR_MARK
17483 /* Handle a special case. For functions whose return type is void, we
17484 generate *no* type attribute. (Note that no object may have type
17485 `void', so this only applies to function return types). */
17486 || code == VOID_TYPE)
17487 return;
17488
17489 type_die = modified_type_die (type,
17490 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17491 context_die);
17492
17493 if (type_die != NULL)
17494 add_AT_die_ref (object_die, DW_AT_type, type_die);
17495 }
17496
17497 /* Given an object die, add the calling convention attribute for the
17498 function call type. */
17499 static void
17500 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17501 {
17502 enum dwarf_calling_convention value = DW_CC_normal;
17503
17504 value = ((enum dwarf_calling_convention)
17505 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17506
17507 if (is_fortran ()
17508 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17509 {
17510 /* DWARF 2 doesn't provide a way to identify a program's source-level
17511 entry point. DW_AT_calling_convention attributes are only meant
17512 to describe functions' calling conventions. However, lacking a
17513 better way to signal the Fortran main program, we used this for
17514 a long time, following existing custom. Now, DWARF 4 has
17515 DW_AT_main_subprogram, which we add below, but some tools still
17516 rely on the old way, which we thus keep. */
17517 value = DW_CC_program;
17518
17519 if (dwarf_version >= 4 || !dwarf_strict)
17520 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17521 }
17522
17523 /* Only add the attribute if the backend requests it, and
17524 is not DW_CC_normal. */
17525 if (value && (value != DW_CC_normal))
17526 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17527 }
17528
17529 /* Given a tree pointer to a struct, class, union, or enum type node, return
17530 a pointer to the (string) tag name for the given type, or zero if the type
17531 was declared without a tag. */
17532
17533 static const char *
17534 type_tag (const_tree type)
17535 {
17536 const char *name = 0;
17537
17538 if (TYPE_NAME (type) != 0)
17539 {
17540 tree t = 0;
17541
17542 /* Find the IDENTIFIER_NODE for the type name. */
17543 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17544 && !TYPE_NAMELESS (type))
17545 t = TYPE_NAME (type);
17546
17547 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17548 a TYPE_DECL node, regardless of whether or not a `typedef' was
17549 involved. */
17550 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17551 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17552 {
17553 /* We want to be extra verbose. Don't call dwarf_name if
17554 DECL_NAME isn't set. The default hook for decl_printable_name
17555 doesn't like that, and in this context it's correct to return
17556 0, instead of "<anonymous>" or the like. */
17557 if (DECL_NAME (TYPE_NAME (type))
17558 && !DECL_NAMELESS (TYPE_NAME (type)))
17559 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17560 }
17561
17562 /* Now get the name as a string, or invent one. */
17563 if (!name && t != 0)
17564 name = IDENTIFIER_POINTER (t);
17565 }
17566
17567 return (name == 0 || *name == '\0') ? 0 : name;
17568 }
17569
17570 /* Return the type associated with a data member, make a special check
17571 for bit field types. */
17572
17573 static inline tree
17574 member_declared_type (const_tree member)
17575 {
17576 return (DECL_BIT_FIELD_TYPE (member)
17577 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17578 }
17579
17580 /* Get the decl's label, as described by its RTL. This may be different
17581 from the DECL_NAME name used in the source file. */
17582
17583 #if 0
17584 static const char *
17585 decl_start_label (tree decl)
17586 {
17587 rtx x;
17588 const char *fnname;
17589
17590 x = DECL_RTL (decl);
17591 gcc_assert (MEM_P (x));
17592
17593 x = XEXP (x, 0);
17594 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17595
17596 fnname = XSTR (x, 0);
17597 return fnname;
17598 }
17599 #endif
17600 \f
17601 /* For variable-length arrays that have been previously generated, but
17602 may be incomplete due to missing subscript info, fill the subscript
17603 info. Return TRUE if this is one of those cases. */
17604 static bool
17605 fill_variable_array_bounds (tree type)
17606 {
17607 if (TREE_ASM_WRITTEN (type)
17608 && TREE_CODE (type) == ARRAY_TYPE
17609 && variably_modified_type_p (type, NULL))
17610 {
17611 dw_die_ref array_die = lookup_type_die (type);
17612 if (!array_die)
17613 return false;
17614 add_subscript_info (array_die, type, !is_ada ());
17615 return true;
17616 }
17617 return false;
17618 }
17619
17620 /* These routines generate the internal representation of the DIE's for
17621 the compilation unit. Debugging information is collected by walking
17622 the declaration trees passed in from dwarf2out_decl(). */
17623
17624 static void
17625 gen_array_type_die (tree type, dw_die_ref context_die)
17626 {
17627 dw_die_ref array_die;
17628
17629 /* GNU compilers represent multidimensional array types as sequences of one
17630 dimensional array types whose element types are themselves array types.
17631 We sometimes squish that down to a single array_type DIE with multiple
17632 subscripts in the Dwarf debugging info. The draft Dwarf specification
17633 say that we are allowed to do this kind of compression in C, because
17634 there is no difference between an array of arrays and a multidimensional
17635 array. We don't do this for Ada to remain as close as possible to the
17636 actual representation, which is especially important against the language
17637 flexibilty wrt arrays of variable size. */
17638
17639 bool collapse_nested_arrays = !is_ada ();
17640
17641 if (fill_variable_array_bounds (type))
17642 return;
17643
17644 dw_die_ref scope_die = scope_die_for (type, context_die);
17645 tree element_type;
17646
17647 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17648 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17649 if (TYPE_STRING_FLAG (type)
17650 && TREE_CODE (type) == ARRAY_TYPE
17651 && is_fortran ()
17652 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17653 {
17654 HOST_WIDE_INT size;
17655
17656 array_die = new_die (DW_TAG_string_type, scope_die, type);
17657 add_name_attribute (array_die, type_tag (type));
17658 equate_type_number_to_die (type, array_die);
17659 size = int_size_in_bytes (type);
17660 if (size >= 0)
17661 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17662 else if (TYPE_DOMAIN (type) != NULL_TREE
17663 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17664 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17665 {
17666 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17667 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17668
17669 size = int_size_in_bytes (TREE_TYPE (szdecl));
17670 if (loc && size > 0)
17671 {
17672 add_AT_location_description (array_die, DW_AT_string_length, loc);
17673 if (size != DWARF2_ADDR_SIZE)
17674 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17675 }
17676 }
17677 return;
17678 }
17679
17680 array_die = new_die (DW_TAG_array_type, scope_die, type);
17681 add_name_attribute (array_die, type_tag (type));
17682 equate_type_number_to_die (type, array_die);
17683
17684 if (TREE_CODE (type) == VECTOR_TYPE)
17685 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17686
17687 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17688 if (is_fortran ()
17689 && TREE_CODE (type) == ARRAY_TYPE
17690 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17691 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17692 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17693
17694 #if 0
17695 /* We default the array ordering. SDB will probably do
17696 the right things even if DW_AT_ordering is not present. It's not even
17697 an issue until we start to get into multidimensional arrays anyway. If
17698 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17699 then we'll have to put the DW_AT_ordering attribute back in. (But if
17700 and when we find out that we need to put these in, we will only do so
17701 for multidimensional arrays. */
17702 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17703 #endif
17704
17705 if (TREE_CODE (type) == VECTOR_TYPE)
17706 {
17707 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17708 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17709 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17710 add_bound_info (subrange_die, DW_AT_upper_bound,
17711 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17712 }
17713 else
17714 add_subscript_info (array_die, type, collapse_nested_arrays);
17715
17716 /* Add representation of the type of the elements of this array type and
17717 emit the corresponding DIE if we haven't done it already. */
17718 element_type = TREE_TYPE (type);
17719 if (collapse_nested_arrays)
17720 while (TREE_CODE (element_type) == ARRAY_TYPE)
17721 {
17722 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17723 break;
17724 element_type = TREE_TYPE (element_type);
17725 }
17726
17727 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17728
17729 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17730 if (TYPE_ARTIFICIAL (type))
17731 add_AT_flag (array_die, DW_AT_artificial, 1);
17732
17733 if (get_AT (array_die, DW_AT_name))
17734 add_pubtype (type, array_die);
17735 }
17736
17737 /* This routine generates DIE for array with hidden descriptor, details
17738 are filled into *info by a langhook. */
17739
17740 static void
17741 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17742 dw_die_ref context_die)
17743 {
17744 const dw_die_ref scope_die = scope_die_for (type, context_die);
17745 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17746 const struct loc_descr_context context = { type, info->base_decl };
17747 int dim;
17748
17749 add_name_attribute (array_die, type_tag (type));
17750 equate_type_number_to_die (type, array_die);
17751
17752 if (info->ndimensions > 1)
17753 switch (info->ordering)
17754 {
17755 case array_descr_ordering_row_major:
17756 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17757 break;
17758 case array_descr_ordering_column_major:
17759 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17760 break;
17761 default:
17762 break;
17763 }
17764
17765 if (dwarf_version >= 3 || !dwarf_strict)
17766 {
17767 if (info->data_location)
17768 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17769 dw_scalar_form_exprloc, &context);
17770 if (info->associated)
17771 add_scalar_info (array_die, DW_AT_associated, info->associated,
17772 dw_scalar_form_constant
17773 | dw_scalar_form_exprloc
17774 | dw_scalar_form_reference, &context);
17775 if (info->allocated)
17776 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17777 dw_scalar_form_constant
17778 | dw_scalar_form_exprloc
17779 | dw_scalar_form_reference, &context);
17780 }
17781
17782 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17783
17784 for (dim = 0; dim < info->ndimensions; dim++)
17785 {
17786 dw_die_ref subrange_die
17787 = new_die (DW_TAG_subrange_type, array_die, NULL);
17788
17789 if (info->dimen[dim].bounds_type)
17790 add_type_attribute (subrange_die,
17791 info->dimen[dim].bounds_type, 0,
17792 context_die);
17793 if (info->dimen[dim].lower_bound)
17794 add_bound_info (subrange_die, DW_AT_lower_bound,
17795 info->dimen[dim].lower_bound, &context);
17796 if (info->dimen[dim].upper_bound)
17797 add_bound_info (subrange_die, DW_AT_upper_bound,
17798 info->dimen[dim].upper_bound, &context);
17799 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17800 add_scalar_info (subrange_die, DW_AT_byte_stride,
17801 info->dimen[dim].stride,
17802 dw_scalar_form_constant
17803 | dw_scalar_form_exprloc
17804 | dw_scalar_form_reference,
17805 &context);
17806 }
17807
17808 gen_type_die (info->element_type, context_die);
17809 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17810 context_die);
17811
17812 if (get_AT (array_die, DW_AT_name))
17813 add_pubtype (type, array_die);
17814 }
17815
17816 #if 0
17817 static void
17818 gen_entry_point_die (tree decl, dw_die_ref context_die)
17819 {
17820 tree origin = decl_ultimate_origin (decl);
17821 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17822
17823 if (origin != NULL)
17824 add_abstract_origin_attribute (decl_die, origin);
17825 else
17826 {
17827 add_name_and_src_coords_attributes (decl_die, decl);
17828 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17829 TYPE_UNQUALIFIED, context_die);
17830 }
17831
17832 if (DECL_ABSTRACT_P (decl))
17833 equate_decl_number_to_die (decl, decl_die);
17834 else
17835 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17836 }
17837 #endif
17838
17839 /* Walk through the list of incomplete types again, trying once more to
17840 emit full debugging info for them. */
17841
17842 static void
17843 retry_incomplete_types (void)
17844 {
17845 int i;
17846
17847 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17848 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17849 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17850 }
17851
17852 /* Determine what tag to use for a record type. */
17853
17854 static enum dwarf_tag
17855 record_type_tag (tree type)
17856 {
17857 if (! lang_hooks.types.classify_record)
17858 return DW_TAG_structure_type;
17859
17860 switch (lang_hooks.types.classify_record (type))
17861 {
17862 case RECORD_IS_STRUCT:
17863 return DW_TAG_structure_type;
17864
17865 case RECORD_IS_CLASS:
17866 return DW_TAG_class_type;
17867
17868 case RECORD_IS_INTERFACE:
17869 if (dwarf_version >= 3 || !dwarf_strict)
17870 return DW_TAG_interface_type;
17871 return DW_TAG_structure_type;
17872
17873 default:
17874 gcc_unreachable ();
17875 }
17876 }
17877
17878 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17879 include all of the information about the enumeration values also. Each
17880 enumerated type name/value is listed as a child of the enumerated type
17881 DIE. */
17882
17883 static dw_die_ref
17884 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17885 {
17886 dw_die_ref type_die = lookup_type_die (type);
17887
17888 if (type_die == NULL)
17889 {
17890 type_die = new_die (DW_TAG_enumeration_type,
17891 scope_die_for (type, context_die), type);
17892 equate_type_number_to_die (type, type_die);
17893 add_name_attribute (type_die, type_tag (type));
17894 if (dwarf_version >= 4 || !dwarf_strict)
17895 {
17896 if (ENUM_IS_SCOPED (type))
17897 add_AT_flag (type_die, DW_AT_enum_class, 1);
17898 if (ENUM_IS_OPAQUE (type))
17899 add_AT_flag (type_die, DW_AT_declaration, 1);
17900 }
17901 }
17902 else if (! TYPE_SIZE (type))
17903 return type_die;
17904 else
17905 remove_AT (type_die, DW_AT_declaration);
17906
17907 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17908 given enum type is incomplete, do not generate the DW_AT_byte_size
17909 attribute or the DW_AT_element_list attribute. */
17910 if (TYPE_SIZE (type))
17911 {
17912 tree link;
17913
17914 TREE_ASM_WRITTEN (type) = 1;
17915 add_byte_size_attribute (type_die, type);
17916 if (dwarf_version >= 3 || !dwarf_strict)
17917 {
17918 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17919 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17920 context_die);
17921 }
17922 if (TYPE_STUB_DECL (type) != NULL_TREE)
17923 {
17924 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17925 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17926 }
17927
17928 /* If the first reference to this type was as the return type of an
17929 inline function, then it may not have a parent. Fix this now. */
17930 if (type_die->die_parent == NULL)
17931 add_child_die (scope_die_for (type, context_die), type_die);
17932
17933 for (link = TYPE_VALUES (type);
17934 link != NULL; link = TREE_CHAIN (link))
17935 {
17936 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17937 tree value = TREE_VALUE (link);
17938
17939 add_name_attribute (enum_die,
17940 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17941
17942 if (TREE_CODE (value) == CONST_DECL)
17943 value = DECL_INITIAL (value);
17944
17945 if (simple_type_size_in_bits (TREE_TYPE (value))
17946 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17947 {
17948 /* For constant forms created by add_AT_unsigned DWARF
17949 consumers (GDB, elfutils, etc.) always zero extend
17950 the value. Only when the actual value is negative
17951 do we need to use add_AT_int to generate a constant
17952 form that can represent negative values. */
17953 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17954 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17955 add_AT_unsigned (enum_die, DW_AT_const_value,
17956 (unsigned HOST_WIDE_INT) val);
17957 else
17958 add_AT_int (enum_die, DW_AT_const_value, val);
17959 }
17960 else
17961 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17962 that here. TODO: This should be re-worked to use correct
17963 signed/unsigned double tags for all cases. */
17964 add_AT_wide (enum_die, DW_AT_const_value, value);
17965 }
17966
17967 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17968 if (TYPE_ARTIFICIAL (type))
17969 add_AT_flag (type_die, DW_AT_artificial, 1);
17970 }
17971 else
17972 add_AT_flag (type_die, DW_AT_declaration, 1);
17973
17974 add_pubtype (type, type_die);
17975
17976 return type_die;
17977 }
17978
17979 /* Generate a DIE to represent either a real live formal parameter decl or to
17980 represent just the type of some formal parameter position in some function
17981 type.
17982
17983 Note that this routine is a bit unusual because its argument may be a
17984 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17985 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17986 node. If it's the former then this function is being called to output a
17987 DIE to represent a formal parameter object (or some inlining thereof). If
17988 it's the latter, then this function is only being called to output a
17989 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17990 argument type of some subprogram type.
17991 If EMIT_NAME_P is true, name and source coordinate attributes
17992 are emitted. */
17993
17994 static dw_die_ref
17995 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17996 dw_die_ref context_die)
17997 {
17998 tree node_or_origin = node ? node : origin;
17999 tree ultimate_origin;
18000 dw_die_ref parm_die = NULL;
18001
18002 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18003 {
18004 parm_die = lookup_decl_die (node);
18005
18006 /* If the contexts differ, we may not be talking about the same
18007 thing. */
18008 if (parm_die && parm_die->die_parent != context_die)
18009 {
18010 if (!DECL_ABSTRACT_P (node))
18011 {
18012 /* This can happen when creating an inlined instance, in
18013 which case we need to create a new DIE that will get
18014 annotated with DW_AT_abstract_origin. */
18015 parm_die = NULL;
18016 }
18017 else
18018 {
18019 /* FIXME: Reuse DIE even with a differing context.
18020
18021 This can happen when calling
18022 dwarf2out_abstract_function to build debug info for
18023 the abstract instance of a function for which we have
18024 already generated a DIE in
18025 dwarf2out_early_global_decl.
18026
18027 Once we remove dwarf2out_abstract_function, we should
18028 have a call to gcc_unreachable here. */
18029 }
18030 }
18031
18032 if (parm_die && parm_die->die_parent == NULL)
18033 {
18034 /* Check that parm_die already has the right attributes that
18035 we would have added below. If any attributes are
18036 missing, fall through to add them. */
18037 if (! DECL_ABSTRACT_P (node_or_origin)
18038 && !get_AT (parm_die, DW_AT_location)
18039 && !get_AT (parm_die, DW_AT_const_value))
18040 /* We are missing location info, and are about to add it. */
18041 ;
18042 else
18043 {
18044 add_child_die (context_die, parm_die);
18045 return parm_die;
18046 }
18047 }
18048 }
18049
18050 /* If we have a previously generated DIE, use it, unless this is an
18051 concrete instance (origin != NULL), in which case we need a new
18052 DIE with a corresponding DW_AT_abstract_origin. */
18053 bool reusing_die;
18054 if (parm_die && origin == NULL)
18055 reusing_die = true;
18056 else
18057 {
18058 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18059 reusing_die = false;
18060 }
18061
18062 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18063 {
18064 case tcc_declaration:
18065 ultimate_origin = decl_ultimate_origin (node_or_origin);
18066 if (node || ultimate_origin)
18067 origin = ultimate_origin;
18068
18069 if (reusing_die)
18070 goto add_location;
18071
18072 if (origin != NULL)
18073 add_abstract_origin_attribute (parm_die, origin);
18074 else if (emit_name_p)
18075 add_name_and_src_coords_attributes (parm_die, node);
18076 if (origin == NULL
18077 || (! DECL_ABSTRACT_P (node_or_origin)
18078 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18079 decl_function_context
18080 (node_or_origin))))
18081 {
18082 tree type = TREE_TYPE (node_or_origin);
18083 if (decl_by_reference_p (node_or_origin))
18084 add_type_attribute (parm_die, TREE_TYPE (type),
18085 TYPE_UNQUALIFIED, context_die);
18086 else
18087 add_type_attribute (parm_die, type,
18088 decl_quals (node_or_origin),
18089 context_die);
18090 }
18091 if (origin == NULL && DECL_ARTIFICIAL (node))
18092 add_AT_flag (parm_die, DW_AT_artificial, 1);
18093 add_location:
18094 if (node && node != origin)
18095 equate_decl_number_to_die (node, parm_die);
18096 if (! DECL_ABSTRACT_P (node_or_origin))
18097 add_location_or_const_value_attribute (parm_die, node_or_origin,
18098 node == NULL, DW_AT_location);
18099
18100 break;
18101
18102 case tcc_type:
18103 /* We were called with some kind of a ..._TYPE node. */
18104 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18105 context_die);
18106 break;
18107
18108 default:
18109 gcc_unreachable ();
18110 }
18111
18112 return parm_die;
18113 }
18114
18115 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18116 children DW_TAG_formal_parameter DIEs representing the arguments of the
18117 parameter pack.
18118
18119 PARM_PACK must be a function parameter pack.
18120 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18121 must point to the subsequent arguments of the function PACK_ARG belongs to.
18122 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18123 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18124 following the last one for which a DIE was generated. */
18125
18126 static dw_die_ref
18127 gen_formal_parameter_pack_die (tree parm_pack,
18128 tree pack_arg,
18129 dw_die_ref subr_die,
18130 tree *next_arg)
18131 {
18132 tree arg;
18133 dw_die_ref parm_pack_die;
18134
18135 gcc_assert (parm_pack
18136 && lang_hooks.function_parameter_pack_p (parm_pack)
18137 && subr_die);
18138
18139 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18140 add_src_coords_attributes (parm_pack_die, parm_pack);
18141
18142 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18143 {
18144 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18145 parm_pack))
18146 break;
18147 gen_formal_parameter_die (arg, NULL,
18148 false /* Don't emit name attribute. */,
18149 parm_pack_die);
18150 }
18151 if (next_arg)
18152 *next_arg = arg;
18153 return parm_pack_die;
18154 }
18155
18156 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18157 at the end of an (ANSI prototyped) formal parameters list. */
18158
18159 static void
18160 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18161 {
18162 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18163 }
18164
18165 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18166 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18167 parameters as specified in some function type specification (except for
18168 those which appear as part of a function *definition*). */
18169
18170 static void
18171 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18172 {
18173 tree link;
18174 tree formal_type = NULL;
18175 tree first_parm_type;
18176 tree arg;
18177
18178 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18179 {
18180 arg = DECL_ARGUMENTS (function_or_method_type);
18181 function_or_method_type = TREE_TYPE (function_or_method_type);
18182 }
18183 else
18184 arg = NULL_TREE;
18185
18186 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18187
18188 /* Make our first pass over the list of formal parameter types and output a
18189 DW_TAG_formal_parameter DIE for each one. */
18190 for (link = first_parm_type; link; )
18191 {
18192 dw_die_ref parm_die;
18193
18194 formal_type = TREE_VALUE (link);
18195 if (formal_type == void_type_node)
18196 break;
18197
18198 /* Output a (nameless) DIE to represent the formal parameter itself. */
18199 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18200 {
18201 parm_die = gen_formal_parameter_die (formal_type, NULL,
18202 true /* Emit name attribute. */,
18203 context_die);
18204 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18205 && link == first_parm_type)
18206 {
18207 add_AT_flag (parm_die, DW_AT_artificial, 1);
18208 if (dwarf_version >= 3 || !dwarf_strict)
18209 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18210 }
18211 else if (arg && DECL_ARTIFICIAL (arg))
18212 add_AT_flag (parm_die, DW_AT_artificial, 1);
18213 }
18214
18215 link = TREE_CHAIN (link);
18216 if (arg)
18217 arg = DECL_CHAIN (arg);
18218 }
18219
18220 /* If this function type has an ellipsis, add a
18221 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18222 if (formal_type != void_type_node)
18223 gen_unspecified_parameters_die (function_or_method_type, context_die);
18224
18225 /* Make our second (and final) pass over the list of formal parameter types
18226 and output DIEs to represent those types (as necessary). */
18227 for (link = TYPE_ARG_TYPES (function_or_method_type);
18228 link && TREE_VALUE (link);
18229 link = TREE_CHAIN (link))
18230 gen_type_die (TREE_VALUE (link), context_die);
18231 }
18232
18233 /* We want to generate the DIE for TYPE so that we can generate the
18234 die for MEMBER, which has been defined; we will need to refer back
18235 to the member declaration nested within TYPE. If we're trying to
18236 generate minimal debug info for TYPE, processing TYPE won't do the
18237 trick; we need to attach the member declaration by hand. */
18238
18239 static void
18240 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18241 {
18242 gen_type_die (type, context_die);
18243
18244 /* If we're trying to avoid duplicate debug info, we may not have
18245 emitted the member decl for this function. Emit it now. */
18246 if (TYPE_STUB_DECL (type)
18247 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18248 && ! lookup_decl_die (member))
18249 {
18250 dw_die_ref type_die;
18251 gcc_assert (!decl_ultimate_origin (member));
18252
18253 push_decl_scope (type);
18254 type_die = lookup_type_die_strip_naming_typedef (type);
18255 if (TREE_CODE (member) == FUNCTION_DECL)
18256 gen_subprogram_die (member, type_die);
18257 else if (TREE_CODE (member) == FIELD_DECL)
18258 {
18259 /* Ignore the nameless fields that are used to skip bits but handle
18260 C++ anonymous unions and structs. */
18261 if (DECL_NAME (member) != NULL_TREE
18262 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18263 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18264 {
18265 gen_type_die (member_declared_type (member), type_die);
18266 gen_field_die (member, type_die);
18267 }
18268 }
18269 else
18270 gen_variable_die (member, NULL_TREE, type_die);
18271
18272 pop_decl_scope ();
18273 }
18274 }
18275 \f
18276 /* Forward declare these functions, because they are mutually recursive
18277 with their set_block_* pairing functions. */
18278 static void set_decl_origin_self (tree);
18279 static void set_decl_abstract_flags (tree, vec<tree> &);
18280
18281 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18282 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18283 that it points to the node itself, thus indicating that the node is its
18284 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18285 the given node is NULL, recursively descend the decl/block tree which
18286 it is the root of, and for each other ..._DECL or BLOCK node contained
18287 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18288 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18289 values to point to themselves. */
18290
18291 static void
18292 set_block_origin_self (tree stmt)
18293 {
18294 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18295 {
18296 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18297
18298 {
18299 tree local_decl;
18300
18301 for (local_decl = BLOCK_VARS (stmt);
18302 local_decl != NULL_TREE;
18303 local_decl = DECL_CHAIN (local_decl))
18304 /* Do not recurse on nested functions since the inlining status
18305 of parent and child can be different as per the DWARF spec. */
18306 if (TREE_CODE (local_decl) != FUNCTION_DECL
18307 && !DECL_EXTERNAL (local_decl))
18308 set_decl_origin_self (local_decl);
18309 }
18310
18311 {
18312 tree subblock;
18313
18314 for (subblock = BLOCK_SUBBLOCKS (stmt);
18315 subblock != NULL_TREE;
18316 subblock = BLOCK_CHAIN (subblock))
18317 set_block_origin_self (subblock); /* Recurse. */
18318 }
18319 }
18320 }
18321
18322 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18323 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18324 node to so that it points to the node itself, thus indicating that the
18325 node represents its own (abstract) origin. Additionally, if the
18326 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18327 the decl/block tree of which the given node is the root of, and for
18328 each other ..._DECL or BLOCK node contained therein whose
18329 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18330 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18331 point to themselves. */
18332
18333 static void
18334 set_decl_origin_self (tree decl)
18335 {
18336 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18337 {
18338 DECL_ABSTRACT_ORIGIN (decl) = decl;
18339 if (TREE_CODE (decl) == FUNCTION_DECL)
18340 {
18341 tree arg;
18342
18343 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18344 DECL_ABSTRACT_ORIGIN (arg) = arg;
18345 if (DECL_INITIAL (decl) != NULL_TREE
18346 && DECL_INITIAL (decl) != error_mark_node)
18347 set_block_origin_self (DECL_INITIAL (decl));
18348 }
18349 }
18350 }
18351 \f
18352 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18353 and if it wasn't 1 before, push it to abstract_vec vector.
18354 For all local decls and all local sub-blocks (recursively) do it
18355 too. */
18356
18357 static void
18358 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18359 {
18360 tree local_decl;
18361 tree subblock;
18362 unsigned int i;
18363
18364 if (!BLOCK_ABSTRACT (stmt))
18365 {
18366 abstract_vec.safe_push (stmt);
18367 BLOCK_ABSTRACT (stmt) = 1;
18368 }
18369
18370 for (local_decl = BLOCK_VARS (stmt);
18371 local_decl != NULL_TREE;
18372 local_decl = DECL_CHAIN (local_decl))
18373 if (! DECL_EXTERNAL (local_decl))
18374 set_decl_abstract_flags (local_decl, abstract_vec);
18375
18376 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18377 {
18378 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18379 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18380 || TREE_CODE (local_decl) == PARM_DECL)
18381 set_decl_abstract_flags (local_decl, abstract_vec);
18382 }
18383
18384 for (subblock = BLOCK_SUBBLOCKS (stmt);
18385 subblock != NULL_TREE;
18386 subblock = BLOCK_CHAIN (subblock))
18387 set_block_abstract_flags (subblock, abstract_vec);
18388 }
18389
18390 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18391 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18392 In the case where the decl is a FUNCTION_DECL also set the abstract
18393 flags for all of the parameters, local vars, local
18394 blocks and sub-blocks (recursively). */
18395
18396 static void
18397 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18398 {
18399 if (!DECL_ABSTRACT_P (decl))
18400 {
18401 abstract_vec.safe_push (decl);
18402 DECL_ABSTRACT_P (decl) = 1;
18403 }
18404
18405 if (TREE_CODE (decl) == FUNCTION_DECL)
18406 {
18407 tree arg;
18408
18409 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18410 if (!DECL_ABSTRACT_P (arg))
18411 {
18412 abstract_vec.safe_push (arg);
18413 DECL_ABSTRACT_P (arg) = 1;
18414 }
18415 if (DECL_INITIAL (decl) != NULL_TREE
18416 && DECL_INITIAL (decl) != error_mark_node)
18417 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18418 }
18419 }
18420
18421 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18422 may later generate inlined and/or out-of-line instances of.
18423
18424 FIXME: In the early-dwarf world, this function, and most of the
18425 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18426 the abstract instance. All we would need to do is annotate
18427 the early DIE with the appropriate DW_AT_inline in late
18428 dwarf (perhaps in gen_inlined_subroutine_die).
18429
18430 However, we can't do this yet, because LTO streaming of DIEs
18431 has not been implemented yet. */
18432
18433 static void
18434 dwarf2out_abstract_function (tree decl)
18435 {
18436 dw_die_ref old_die;
18437 tree save_fn;
18438 tree context;
18439 hash_table<decl_loc_hasher> *old_decl_loc_table;
18440 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18441 int old_call_site_count, old_tail_call_site_count;
18442 struct call_arg_loc_node *old_call_arg_locations;
18443
18444 /* Make sure we have the actual abstract inline, not a clone. */
18445 decl = DECL_ORIGIN (decl);
18446
18447 old_die = lookup_decl_die (decl);
18448 if (old_die && get_AT (old_die, DW_AT_inline))
18449 /* We've already generated the abstract instance. */
18450 return;
18451
18452 /* We can be called while recursively when seeing block defining inlined subroutine
18453 DIE. Be sure to not clobber the outer location table nor use it or we would
18454 get locations in abstract instantces. */
18455 old_decl_loc_table = decl_loc_table;
18456 decl_loc_table = NULL;
18457 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18458 cached_dw_loc_list_table = NULL;
18459 old_call_arg_locations = call_arg_locations;
18460 call_arg_locations = NULL;
18461 old_call_site_count = call_site_count;
18462 call_site_count = -1;
18463 old_tail_call_site_count = tail_call_site_count;
18464 tail_call_site_count = -1;
18465
18466 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18467 we don't get confused by DECL_ABSTRACT_P. */
18468 if (debug_info_level > DINFO_LEVEL_TERSE)
18469 {
18470 context = decl_class_context (decl);
18471 if (context)
18472 gen_type_die_for_member
18473 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18474 }
18475
18476 /* Pretend we've just finished compiling this function. */
18477 save_fn = current_function_decl;
18478 current_function_decl = decl;
18479
18480 auto_vec<tree, 64> abstract_vec;
18481 set_decl_abstract_flags (decl, abstract_vec);
18482 dwarf2out_decl (decl);
18483 unsigned int i;
18484 tree t;
18485 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18486 if (TREE_CODE (t) == BLOCK)
18487 BLOCK_ABSTRACT (t) = 0;
18488 else
18489 DECL_ABSTRACT_P (t) = 0;
18490
18491 current_function_decl = save_fn;
18492 decl_loc_table = old_decl_loc_table;
18493 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18494 call_arg_locations = old_call_arg_locations;
18495 call_site_count = old_call_site_count;
18496 tail_call_site_count = old_tail_call_site_count;
18497 }
18498
18499 /* Helper function of premark_used_types() which gets called through
18500 htab_traverse.
18501
18502 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18503 marked as unused by prune_unused_types. */
18504
18505 bool
18506 premark_used_types_helper (tree const &type, void *)
18507 {
18508 dw_die_ref die;
18509
18510 die = lookup_type_die (type);
18511 if (die != NULL)
18512 die->die_perennial_p = 1;
18513 return true;
18514 }
18515
18516 /* Helper function of premark_types_used_by_global_vars which gets called
18517 through htab_traverse.
18518
18519 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18520 marked as unused by prune_unused_types. The DIE of the type is marked
18521 only if the global variable using the type will actually be emitted. */
18522
18523 int
18524 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18525 void *)
18526 {
18527 struct types_used_by_vars_entry *entry;
18528 dw_die_ref die;
18529
18530 entry = (struct types_used_by_vars_entry *) *slot;
18531 gcc_assert (entry->type != NULL
18532 && entry->var_decl != NULL);
18533 die = lookup_type_die (entry->type);
18534 if (die)
18535 {
18536 /* Ask cgraph if the global variable really is to be emitted.
18537 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18538 varpool_node *node = varpool_node::get (entry->var_decl);
18539 if (node && node->definition)
18540 {
18541 die->die_perennial_p = 1;
18542 /* Keep the parent DIEs as well. */
18543 while ((die = die->die_parent) && die->die_perennial_p == 0)
18544 die->die_perennial_p = 1;
18545 }
18546 }
18547 return 1;
18548 }
18549
18550 /* Mark all members of used_types_hash as perennial. */
18551
18552 static void
18553 premark_used_types (struct function *fun)
18554 {
18555 if (fun && fun->used_types_hash)
18556 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18557 }
18558
18559 /* Mark all members of types_used_by_vars_entry as perennial. */
18560
18561 static void
18562 premark_types_used_by_global_vars (void)
18563 {
18564 if (types_used_by_vars_hash)
18565 types_used_by_vars_hash
18566 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18567 }
18568
18569 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18570 for CA_LOC call arg loc node. */
18571
18572 static dw_die_ref
18573 gen_call_site_die (tree decl, dw_die_ref subr_die,
18574 struct call_arg_loc_node *ca_loc)
18575 {
18576 dw_die_ref stmt_die = NULL, die;
18577 tree block = ca_loc->block;
18578
18579 while (block
18580 && block != DECL_INITIAL (decl)
18581 && TREE_CODE (block) == BLOCK)
18582 {
18583 stmt_die = BLOCK_DIE (block);
18584 if (stmt_die)
18585 break;
18586 block = BLOCK_SUPERCONTEXT (block);
18587 }
18588 if (stmt_die == NULL)
18589 stmt_die = subr_die;
18590 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18591 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18592 if (ca_loc->tail_call_p)
18593 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18594 if (ca_loc->symbol_ref)
18595 {
18596 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18597 if (tdie)
18598 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18599 else
18600 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18601 }
18602 return die;
18603 }
18604
18605 /* Generate a DIE to represent a declared function (either file-scope or
18606 block-local). */
18607
18608 static void
18609 gen_subprogram_die (tree decl, dw_die_ref context_die)
18610 {
18611 tree origin = decl_ultimate_origin (decl);
18612 dw_die_ref subr_die;
18613 dw_die_ref old_die = lookup_decl_die (decl);
18614
18615 /* This function gets called multiple times for different stages of
18616 the debug process. For example, for func() in this code:
18617
18618 namespace S
18619 {
18620 void func() { ... }
18621 }
18622
18623 ...we get called 4 times. Twice in early debug and twice in
18624 late debug:
18625
18626 Early debug
18627 -----------
18628
18629 1. Once while generating func() within the namespace. This is
18630 the declaration. The declaration bit below is set, as the
18631 context is the namespace.
18632
18633 A new DIE will be generated with DW_AT_declaration set.
18634
18635 2. Once for func() itself. This is the specification. The
18636 declaration bit below is clear as the context is the CU.
18637
18638 We will use the cached DIE from (1) to create a new DIE with
18639 DW_AT_specification pointing to the declaration in (1).
18640
18641 Late debug via rest_of_handle_final()
18642 -------------------------------------
18643
18644 3. Once generating func() within the namespace. This is also the
18645 declaration, as in (1), but this time we will early exit below
18646 as we have a cached DIE and a declaration needs no additional
18647 annotations (no locations), as the source declaration line
18648 info is enough.
18649
18650 4. Once for func() itself. As in (2), this is the specification,
18651 but this time we will re-use the cached DIE, and just annotate
18652 it with the location information that should now be available.
18653
18654 For something without namespaces, but with abstract instances, we
18655 are also called a multiple times:
18656
18657 class Base
18658 {
18659 public:
18660 Base (); // constructor declaration (1)
18661 };
18662
18663 Base::Base () { } // constructor specification (2)
18664
18665 Early debug
18666 -----------
18667
18668 1. Once for the Base() constructor by virtue of it being a
18669 member of the Base class. This is done via
18670 rest_of_type_compilation.
18671
18672 This is a declaration, so a new DIE will be created with
18673 DW_AT_declaration.
18674
18675 2. Once for the Base() constructor definition, but this time
18676 while generating the abstract instance of the base
18677 constructor (__base_ctor) which is being generated via early
18678 debug of reachable functions.
18679
18680 Even though we have a cached version of the declaration (1),
18681 we will create a DW_AT_specification of the declaration DIE
18682 in (1).
18683
18684 3. Once for the __base_ctor itself, but this time, we generate
18685 an DW_AT_abstract_origin version of the DW_AT_specification in
18686 (2).
18687
18688 Late debug via rest_of_handle_final
18689 -----------------------------------
18690
18691 4. One final time for the __base_ctor (which will have a cached
18692 DIE with DW_AT_abstract_origin created in (3). This time,
18693 we will just annotate the location information now
18694 available.
18695 */
18696 int declaration = (current_function_decl != decl
18697 || class_or_namespace_scope_p (context_die));
18698
18699 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18700
18701 /* Now that the C++ front end lazily declares artificial member fns, we
18702 might need to retrofit the declaration into its class. */
18703 if (!declaration && !origin && !old_die
18704 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18705 && !class_or_namespace_scope_p (context_die)
18706 && debug_info_level > DINFO_LEVEL_TERSE)
18707 old_die = force_decl_die (decl);
18708
18709 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18710 if (origin != NULL)
18711 {
18712 gcc_assert (!declaration || local_scope_p (context_die));
18713
18714 /* Fixup die_parent for the abstract instance of a nested
18715 inline function. */
18716 if (old_die && old_die->die_parent == NULL)
18717 add_child_die (context_die, old_die);
18718
18719 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18720 {
18721 /* If we have a DW_AT_abstract_origin we have a working
18722 cached version. */
18723 subr_die = old_die;
18724 }
18725 else
18726 {
18727 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18728 add_abstract_origin_attribute (subr_die, origin);
18729 /* This is where the actual code for a cloned function is.
18730 Let's emit linkage name attribute for it. This helps
18731 debuggers to e.g, set breakpoints into
18732 constructors/destructors when the user asks "break
18733 K::K". */
18734 add_linkage_name (subr_die, decl);
18735 }
18736 }
18737 /* A cached copy, possibly from early dwarf generation. Reuse as
18738 much as possible. */
18739 else if (old_die)
18740 {
18741 /* A declaration that has been previously dumped needs no
18742 additional information. */
18743 if (declaration)
18744 return;
18745
18746 if (!get_AT_flag (old_die, DW_AT_declaration)
18747 /* We can have a normal definition following an inline one in the
18748 case of redefinition of GNU C extern inlines.
18749 It seems reasonable to use AT_specification in this case. */
18750 && !get_AT (old_die, DW_AT_inline))
18751 {
18752 /* Detect and ignore this case, where we are trying to output
18753 something we have already output. */
18754 if (get_AT (old_die, DW_AT_low_pc)
18755 || get_AT (old_die, DW_AT_ranges))
18756 return;
18757
18758 /* If we have no location information, this must be a
18759 partially generated DIE from early dwarf generation.
18760 Fall through and generate it. */
18761 }
18762
18763 /* If the definition comes from the same place as the declaration,
18764 maybe use the old DIE. We always want the DIE for this function
18765 that has the *_pc attributes to be under comp_unit_die so the
18766 debugger can find it. We also need to do this for abstract
18767 instances of inlines, since the spec requires the out-of-line copy
18768 to have the same parent. For local class methods, this doesn't
18769 apply; we just use the old DIE. */
18770 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18771 struct dwarf_file_data * file_index = lookup_filename (s.file);
18772 if ((is_cu_die (old_die->die_parent)
18773 /* This condition fixes the inconsistency/ICE with the
18774 following Fortran test (or some derivative thereof) while
18775 building libgfortran:
18776
18777 module some_m
18778 contains
18779 logical function funky (FLAG)
18780 funky = .true.
18781 end function
18782 end module
18783 */
18784 || (old_die->die_parent
18785 && old_die->die_parent->die_tag == DW_TAG_module)
18786 || context_die == NULL)
18787 && (DECL_ARTIFICIAL (decl)
18788 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18789 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18790 == (unsigned) s.line))))
18791 {
18792 subr_die = old_die;
18793
18794 /* Clear out the declaration attribute, but leave the
18795 parameters so they can be augmented with location
18796 information later. Unless this was a declaration, in
18797 which case, wipe out the nameless parameters and recreate
18798 them further down. */
18799 if (remove_AT (subr_die, DW_AT_declaration))
18800 {
18801
18802 remove_AT (subr_die, DW_AT_object_pointer);
18803 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18804 }
18805 }
18806 /* Make a specification pointing to the previously built
18807 declaration. */
18808 else
18809 {
18810 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18811 add_AT_specification (subr_die, old_die);
18812 add_pubname (decl, subr_die);
18813 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18814 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18815 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18816 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18817
18818 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18819 emit the real type on the definition die. */
18820 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18821 {
18822 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18823 if (die == auto_die || die == decltype_auto_die)
18824 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18825 TYPE_UNQUALIFIED, context_die);
18826 }
18827 }
18828 }
18829 /* Create a fresh DIE for anything else. */
18830 else
18831 {
18832 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18833
18834 if (TREE_PUBLIC (decl))
18835 add_AT_flag (subr_die, DW_AT_external, 1);
18836
18837 add_name_and_src_coords_attributes (subr_die, decl);
18838 add_pubname (decl, subr_die);
18839 if (debug_info_level > DINFO_LEVEL_TERSE)
18840 {
18841 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18842 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18843 TYPE_UNQUALIFIED, context_die);
18844 }
18845
18846 add_pure_or_virtual_attribute (subr_die, decl);
18847 if (DECL_ARTIFICIAL (decl))
18848 add_AT_flag (subr_die, DW_AT_artificial, 1);
18849
18850 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18851 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18852
18853 add_accessibility_attribute (subr_die, decl);
18854 }
18855
18856 /* Unless we have an existing non-declaration DIE, equate the new
18857 DIE. */
18858 if (!old_die || is_declaration_die (old_die))
18859 equate_decl_number_to_die (decl, subr_die);
18860
18861 if (declaration)
18862 {
18863 if (!old_die || !get_AT (old_die, DW_AT_inline))
18864 {
18865 add_AT_flag (subr_die, DW_AT_declaration, 1);
18866
18867 /* If this is an explicit function declaration then generate
18868 a DW_AT_explicit attribute. */
18869 if (lang_hooks.decls.function_decl_explicit_p (decl)
18870 && (dwarf_version >= 3 || !dwarf_strict))
18871 add_AT_flag (subr_die, DW_AT_explicit, 1);
18872
18873 /* If this is a C++11 deleted special function member then generate
18874 a DW_AT_GNU_deleted attribute. */
18875 if (lang_hooks.decls.function_decl_deleted_p (decl)
18876 && (! dwarf_strict))
18877 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18878 }
18879 }
18880 /* Tag abstract instances with DW_AT_inline. */
18881 else if (DECL_ABSTRACT_P (decl))
18882 {
18883 if (DECL_DECLARED_INLINE_P (decl))
18884 {
18885 if (cgraph_function_possibly_inlined_p (decl))
18886 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18887 else
18888 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18889 }
18890 else
18891 {
18892 if (cgraph_function_possibly_inlined_p (decl))
18893 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18894 else
18895 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18896 }
18897
18898 if (DECL_DECLARED_INLINE_P (decl)
18899 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18900 add_AT_flag (subr_die, DW_AT_artificial, 1);
18901 }
18902 /* For non DECL_EXTERNALs, if range information is available, fill
18903 the DIE with it. */
18904 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18905 {
18906 HOST_WIDE_INT cfa_fb_offset;
18907
18908 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18909
18910 if (!flag_reorder_blocks_and_partition)
18911 {
18912 dw_fde_ref fde = fun->fde;
18913 if (fde->dw_fde_begin)
18914 {
18915 /* We have already generated the labels. */
18916 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18917 fde->dw_fde_end, false);
18918 }
18919 else
18920 {
18921 /* Create start/end labels and add the range. */
18922 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18923 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18924 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18925 current_function_funcdef_no);
18926 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18927 current_function_funcdef_no);
18928 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18929 false);
18930 }
18931
18932 #if VMS_DEBUGGING_INFO
18933 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18934 Section 2.3 Prologue and Epilogue Attributes:
18935 When a breakpoint is set on entry to a function, it is generally
18936 desirable for execution to be suspended, not on the very first
18937 instruction of the function, but rather at a point after the
18938 function's frame has been set up, after any language defined local
18939 declaration processing has been completed, and before execution of
18940 the first statement of the function begins. Debuggers generally
18941 cannot properly determine where this point is. Similarly for a
18942 breakpoint set on exit from a function. The prologue and epilogue
18943 attributes allow a compiler to communicate the location(s) to use. */
18944
18945 {
18946 if (fde->dw_fde_vms_end_prologue)
18947 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18948 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18949
18950 if (fde->dw_fde_vms_begin_epilogue)
18951 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18952 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18953 }
18954 #endif
18955
18956 }
18957 else
18958 {
18959 /* Generate pubnames entries for the split function code ranges. */
18960 dw_fde_ref fde = fun->fde;
18961
18962 if (fde->dw_fde_second_begin)
18963 {
18964 if (dwarf_version >= 3 || !dwarf_strict)
18965 {
18966 /* We should use ranges for non-contiguous code section
18967 addresses. Use the actual code range for the initial
18968 section, since the HOT/COLD labels might precede an
18969 alignment offset. */
18970 bool range_list_added = false;
18971 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18972 fde->dw_fde_end, &range_list_added,
18973 false);
18974 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18975 fde->dw_fde_second_end,
18976 &range_list_added, false);
18977 if (range_list_added)
18978 add_ranges (NULL);
18979 }
18980 else
18981 {
18982 /* There is no real support in DW2 for this .. so we make
18983 a work-around. First, emit the pub name for the segment
18984 containing the function label. Then make and emit a
18985 simplified subprogram DIE for the second segment with the
18986 name pre-fixed by __hot/cold_sect_of_. We use the same
18987 linkage name for the second die so that gdb will find both
18988 sections when given "b foo". */
18989 const char *name = NULL;
18990 tree decl_name = DECL_NAME (decl);
18991 dw_die_ref seg_die;
18992
18993 /* Do the 'primary' section. */
18994 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18995 fde->dw_fde_end, false);
18996
18997 /* Build a minimal DIE for the secondary section. */
18998 seg_die = new_die (DW_TAG_subprogram,
18999 subr_die->die_parent, decl);
19000
19001 if (TREE_PUBLIC (decl))
19002 add_AT_flag (seg_die, DW_AT_external, 1);
19003
19004 if (decl_name != NULL
19005 && IDENTIFIER_POINTER (decl_name) != NULL)
19006 {
19007 name = dwarf2_name (decl, 1);
19008 if (! DECL_ARTIFICIAL (decl))
19009 add_src_coords_attributes (seg_die, decl);
19010
19011 add_linkage_name (seg_die, decl);
19012 }
19013 gcc_assert (name != NULL);
19014 add_pure_or_virtual_attribute (seg_die, decl);
19015 if (DECL_ARTIFICIAL (decl))
19016 add_AT_flag (seg_die, DW_AT_artificial, 1);
19017
19018 name = concat ("__second_sect_of_", name, NULL);
19019 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19020 fde->dw_fde_second_end, false);
19021 add_name_attribute (seg_die, name);
19022 if (want_pubnames ())
19023 add_pubname_string (name, seg_die);
19024 }
19025 }
19026 else
19027 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19028 false);
19029 }
19030
19031 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19032
19033 /* We define the "frame base" as the function's CFA. This is more
19034 convenient for several reasons: (1) It's stable across the prologue
19035 and epilogue, which makes it better than just a frame pointer,
19036 (2) With dwarf3, there exists a one-byte encoding that allows us
19037 to reference the .debug_frame data by proxy, but failing that,
19038 (3) We can at least reuse the code inspection and interpretation
19039 code that determines the CFA position at various points in the
19040 function. */
19041 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19042 {
19043 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19044 add_AT_loc (subr_die, DW_AT_frame_base, op);
19045 }
19046 else
19047 {
19048 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19049 if (list->dw_loc_next)
19050 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19051 else
19052 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19053 }
19054
19055 /* Compute a displacement from the "steady-state frame pointer" to
19056 the CFA. The former is what all stack slots and argument slots
19057 will reference in the rtl; the latter is what we've told the
19058 debugger about. We'll need to adjust all frame_base references
19059 by this displacement. */
19060 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19061
19062 if (fun->static_chain_decl)
19063 add_AT_location_description
19064 (subr_die, DW_AT_static_link,
19065 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19066 }
19067
19068 /* Generate child dies for template paramaters. */
19069 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19070 gen_generic_params_dies (decl);
19071
19072 /* Now output descriptions of the arguments for this function. This gets
19073 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19074 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19075 `...' at the end of the formal parameter list. In order to find out if
19076 there was a trailing ellipsis or not, we must instead look at the type
19077 associated with the FUNCTION_DECL. This will be a node of type
19078 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19079 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19080 an ellipsis at the end. */
19081
19082 /* In the case where we are describing a mere function declaration, all we
19083 need to do here (and all we *can* do here) is to describe the *types* of
19084 its formal parameters. */
19085 if (debug_info_level <= DINFO_LEVEL_TERSE)
19086 ;
19087 else if (declaration)
19088 gen_formal_types_die (decl, subr_die);
19089 else
19090 {
19091 /* Generate DIEs to represent all known formal parameters. */
19092 tree parm = DECL_ARGUMENTS (decl);
19093 tree generic_decl = early_dwarf
19094 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19095 tree generic_decl_parm = generic_decl
19096 ? DECL_ARGUMENTS (generic_decl)
19097 : NULL;
19098
19099 /* Now we want to walk the list of parameters of the function and
19100 emit their relevant DIEs.
19101
19102 We consider the case of DECL being an instance of a generic function
19103 as well as it being a normal function.
19104
19105 If DECL is an instance of a generic function we walk the
19106 parameters of the generic function declaration _and_ the parameters of
19107 DECL itself. This is useful because we want to emit specific DIEs for
19108 function parameter packs and those are declared as part of the
19109 generic function declaration. In that particular case,
19110 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19111 That DIE has children DIEs representing the set of arguments
19112 of the pack. Note that the set of pack arguments can be empty.
19113 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19114 children DIE.
19115
19116 Otherwise, we just consider the parameters of DECL. */
19117 while (generic_decl_parm || parm)
19118 {
19119 if (generic_decl_parm
19120 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19121 gen_formal_parameter_pack_die (generic_decl_parm,
19122 parm, subr_die,
19123 &parm);
19124 else if (parm && !POINTER_BOUNDS_P (parm))
19125 {
19126 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19127
19128 if (parm == DECL_ARGUMENTS (decl)
19129 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19130 && parm_die
19131 && (dwarf_version >= 3 || !dwarf_strict))
19132 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19133
19134 parm = DECL_CHAIN (parm);
19135 }
19136 else if (parm)
19137 parm = DECL_CHAIN (parm);
19138
19139 if (generic_decl_parm)
19140 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19141 }
19142
19143 /* Decide whether we need an unspecified_parameters DIE at the end.
19144 There are 2 more cases to do this for: 1) the ansi ... declaration -
19145 this is detectable when the end of the arg list is not a
19146 void_type_node 2) an unprototyped function declaration (not a
19147 definition). This just means that we have no info about the
19148 parameters at all. */
19149 if (prototype_p (TREE_TYPE (decl)))
19150 {
19151 /* This is the prototyped case, check for.... */
19152 if (stdarg_p (TREE_TYPE (decl)))
19153 gen_unspecified_parameters_die (decl, subr_die);
19154 }
19155 else if (DECL_INITIAL (decl) == NULL_TREE)
19156 gen_unspecified_parameters_die (decl, subr_die);
19157 }
19158
19159 if (subr_die != old_die)
19160 /* Add the calling convention attribute if requested. */
19161 add_calling_convention_attribute (subr_die, decl);
19162
19163 /* Output Dwarf info for all of the stuff within the body of the function
19164 (if it has one - it may be just a declaration).
19165
19166 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19167 a function. This BLOCK actually represents the outermost binding contour
19168 for the function, i.e. the contour in which the function's formal
19169 parameters and labels get declared. Curiously, it appears that the front
19170 end doesn't actually put the PARM_DECL nodes for the current function onto
19171 the BLOCK_VARS list for this outer scope, but are strung off of the
19172 DECL_ARGUMENTS list for the function instead.
19173
19174 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19175 the LABEL_DECL nodes for the function however, and we output DWARF info
19176 for those in decls_for_scope. Just within the `outer_scope' there will be
19177 a BLOCK node representing the function's outermost pair of curly braces,
19178 and any blocks used for the base and member initializers of a C++
19179 constructor function. */
19180 tree outer_scope = DECL_INITIAL (decl);
19181 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19182 {
19183 int call_site_note_count = 0;
19184 int tail_call_site_note_count = 0;
19185
19186 /* Emit a DW_TAG_variable DIE for a named return value. */
19187 if (DECL_NAME (DECL_RESULT (decl)))
19188 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19189
19190 /* The first time through decls_for_scope we will generate the
19191 DIEs for the locals. The second time, we fill in the
19192 location info. */
19193 decls_for_scope (outer_scope, subr_die);
19194
19195 if (call_arg_locations && !dwarf_strict)
19196 {
19197 struct call_arg_loc_node *ca_loc;
19198 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19199 {
19200 dw_die_ref die = NULL;
19201 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19202 rtx arg, next_arg;
19203
19204 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19205 arg; arg = next_arg)
19206 {
19207 dw_loc_descr_ref reg, val;
19208 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19209 dw_die_ref cdie, tdie = NULL;
19210
19211 next_arg = XEXP (arg, 1);
19212 if (REG_P (XEXP (XEXP (arg, 0), 0))
19213 && next_arg
19214 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19215 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19216 && REGNO (XEXP (XEXP (arg, 0), 0))
19217 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19218 next_arg = XEXP (next_arg, 1);
19219 if (mode == VOIDmode)
19220 {
19221 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19222 if (mode == VOIDmode)
19223 mode = GET_MODE (XEXP (arg, 0));
19224 }
19225 if (mode == VOIDmode || mode == BLKmode)
19226 continue;
19227 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19228 {
19229 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19230 tloc = XEXP (XEXP (arg, 0), 1);
19231 continue;
19232 }
19233 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19234 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19235 {
19236 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19237 tlocc = XEXP (XEXP (arg, 0), 1);
19238 continue;
19239 }
19240 reg = NULL;
19241 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19242 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19243 VAR_INIT_STATUS_INITIALIZED);
19244 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19245 {
19246 rtx mem = XEXP (XEXP (arg, 0), 0);
19247 reg = mem_loc_descriptor (XEXP (mem, 0),
19248 get_address_mode (mem),
19249 GET_MODE (mem),
19250 VAR_INIT_STATUS_INITIALIZED);
19251 }
19252 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19253 == DEBUG_PARAMETER_REF)
19254 {
19255 tree tdecl
19256 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19257 tdie = lookup_decl_die (tdecl);
19258 if (tdie == NULL)
19259 continue;
19260 }
19261 else
19262 continue;
19263 if (reg == NULL
19264 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19265 != DEBUG_PARAMETER_REF)
19266 continue;
19267 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19268 VOIDmode,
19269 VAR_INIT_STATUS_INITIALIZED);
19270 if (val == NULL)
19271 continue;
19272 if (die == NULL)
19273 die = gen_call_site_die (decl, subr_die, ca_loc);
19274 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19275 NULL_TREE);
19276 if (reg != NULL)
19277 add_AT_loc (cdie, DW_AT_location, reg);
19278 else if (tdie != NULL)
19279 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19280 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19281 if (next_arg != XEXP (arg, 1))
19282 {
19283 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19284 if (mode == VOIDmode)
19285 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19286 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19287 0), 1),
19288 mode, VOIDmode,
19289 VAR_INIT_STATUS_INITIALIZED);
19290 if (val != NULL)
19291 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19292 }
19293 }
19294 if (die == NULL
19295 && (ca_loc->symbol_ref || tloc))
19296 die = gen_call_site_die (decl, subr_die, ca_loc);
19297 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19298 {
19299 dw_loc_descr_ref tval = NULL;
19300
19301 if (tloc != NULL_RTX)
19302 tval = mem_loc_descriptor (tloc,
19303 GET_MODE (tloc) == VOIDmode
19304 ? Pmode : GET_MODE (tloc),
19305 VOIDmode,
19306 VAR_INIT_STATUS_INITIALIZED);
19307 if (tval)
19308 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19309 else if (tlocc != NULL_RTX)
19310 {
19311 tval = mem_loc_descriptor (tlocc,
19312 GET_MODE (tlocc) == VOIDmode
19313 ? Pmode : GET_MODE (tlocc),
19314 VOIDmode,
19315 VAR_INIT_STATUS_INITIALIZED);
19316 if (tval)
19317 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19318 tval);
19319 }
19320 }
19321 if (die != NULL)
19322 {
19323 call_site_note_count++;
19324 if (ca_loc->tail_call_p)
19325 tail_call_site_note_count++;
19326 }
19327 }
19328 }
19329 call_arg_locations = NULL;
19330 call_arg_loc_last = NULL;
19331 if (tail_call_site_count >= 0
19332 && tail_call_site_count == tail_call_site_note_count
19333 && !dwarf_strict)
19334 {
19335 if (call_site_count >= 0
19336 && call_site_count == call_site_note_count)
19337 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19338 else
19339 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19340 }
19341 call_site_count = -1;
19342 tail_call_site_count = -1;
19343 }
19344 }
19345
19346 /* Returns a hash value for X (which really is a die_struct). */
19347
19348 hashval_t
19349 block_die_hasher::hash (die_struct *d)
19350 {
19351 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19352 }
19353
19354 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19355 as decl_id and die_parent of die_struct Y. */
19356
19357 bool
19358 block_die_hasher::equal (die_struct *x, die_struct *y)
19359 {
19360 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19361 }
19362
19363 /* Return TRUE if DECL, which may have been previously generated as
19364 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19365 true if decl (or its origin) is either an extern declaration or a
19366 class/namespace scoped declaration.
19367
19368 The declare_in_namespace support causes us to get two DIEs for one
19369 variable, both of which are declarations. We want to avoid
19370 considering one to be a specification, so we must test for
19371 DECLARATION and DW_AT_declaration. */
19372 static inline bool
19373 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19374 {
19375 return (old_die && TREE_STATIC (decl) && !declaration
19376 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19377 }
19378
19379 /* Return true if DECL is a local static. */
19380
19381 static inline bool
19382 local_function_static (tree decl)
19383 {
19384 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19385 return TREE_STATIC (decl)
19386 && DECL_CONTEXT (decl)
19387 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19388 }
19389
19390 /* Generate a DIE to represent a declared data object.
19391 Either DECL or ORIGIN must be non-null. */
19392
19393 static void
19394 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19395 {
19396 HOST_WIDE_INT off = 0;
19397 tree com_decl;
19398 tree decl_or_origin = decl ? decl : origin;
19399 tree ultimate_origin;
19400 dw_die_ref var_die;
19401 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19402 dw_die_ref origin_die = NULL;
19403 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19404 || class_or_namespace_scope_p (context_die));
19405 bool specialization_p = false;
19406
19407 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19408 if (decl || ultimate_origin)
19409 origin = ultimate_origin;
19410 com_decl = fortran_common (decl_or_origin, &off);
19411
19412 /* Symbol in common gets emitted as a child of the common block, in the form
19413 of a data member. */
19414 if (com_decl)
19415 {
19416 dw_die_ref com_die;
19417 dw_loc_list_ref loc;
19418 die_node com_die_arg;
19419
19420 var_die = lookup_decl_die (decl_or_origin);
19421 if (var_die)
19422 {
19423 if (get_AT (var_die, DW_AT_location) == NULL)
19424 {
19425 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19426 if (loc)
19427 {
19428 if (off)
19429 {
19430 /* Optimize the common case. */
19431 if (single_element_loc_list_p (loc)
19432 && loc->expr->dw_loc_opc == DW_OP_addr
19433 && loc->expr->dw_loc_next == NULL
19434 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19435 == SYMBOL_REF)
19436 {
19437 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19438 loc->expr->dw_loc_oprnd1.v.val_addr
19439 = plus_constant (GET_MODE (x), x , off);
19440 }
19441 else
19442 loc_list_plus_const (loc, off);
19443 }
19444 add_AT_location_description (var_die, DW_AT_location, loc);
19445 remove_AT (var_die, DW_AT_declaration);
19446 }
19447 }
19448 return;
19449 }
19450
19451 if (common_block_die_table == NULL)
19452 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19453
19454 com_die_arg.decl_id = DECL_UID (com_decl);
19455 com_die_arg.die_parent = context_die;
19456 com_die = common_block_die_table->find (&com_die_arg);
19457 loc = loc_list_from_tree (com_decl, 2, NULL);
19458 if (com_die == NULL)
19459 {
19460 const char *cnam
19461 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19462 die_node **slot;
19463
19464 com_die = new_die (DW_TAG_common_block, context_die, decl);
19465 add_name_and_src_coords_attributes (com_die, com_decl);
19466 if (loc)
19467 {
19468 add_AT_location_description (com_die, DW_AT_location, loc);
19469 /* Avoid sharing the same loc descriptor between
19470 DW_TAG_common_block and DW_TAG_variable. */
19471 loc = loc_list_from_tree (com_decl, 2, NULL);
19472 }
19473 else if (DECL_EXTERNAL (decl))
19474 add_AT_flag (com_die, DW_AT_declaration, 1);
19475 if (want_pubnames ())
19476 add_pubname_string (cnam, com_die); /* ??? needed? */
19477 com_die->decl_id = DECL_UID (com_decl);
19478 slot = common_block_die_table->find_slot (com_die, INSERT);
19479 *slot = com_die;
19480 }
19481 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19482 {
19483 add_AT_location_description (com_die, DW_AT_location, loc);
19484 loc = loc_list_from_tree (com_decl, 2, NULL);
19485 remove_AT (com_die, DW_AT_declaration);
19486 }
19487 var_die = new_die (DW_TAG_variable, com_die, decl);
19488 add_name_and_src_coords_attributes (var_die, decl);
19489 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19490 context_die);
19491 add_AT_flag (var_die, DW_AT_external, 1);
19492 if (loc)
19493 {
19494 if (off)
19495 {
19496 /* Optimize the common case. */
19497 if (single_element_loc_list_p (loc)
19498 && loc->expr->dw_loc_opc == DW_OP_addr
19499 && loc->expr->dw_loc_next == NULL
19500 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19501 {
19502 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19503 loc->expr->dw_loc_oprnd1.v.val_addr
19504 = plus_constant (GET_MODE (x), x, off);
19505 }
19506 else
19507 loc_list_plus_const (loc, off);
19508 }
19509 add_AT_location_description (var_die, DW_AT_location, loc);
19510 }
19511 else if (DECL_EXTERNAL (decl))
19512 add_AT_flag (var_die, DW_AT_declaration, 1);
19513 equate_decl_number_to_die (decl, var_die);
19514 return;
19515 }
19516
19517 if (old_die)
19518 {
19519 if (declaration)
19520 {
19521 /* A declaration that has been previously dumped, needs no
19522 further annotations, since it doesn't need location on
19523 the second pass. */
19524 return;
19525 }
19526 else if (decl_will_get_specification_p (old_die, decl, declaration)
19527 && !get_AT (old_die, DW_AT_specification))
19528 {
19529 /* Fall-thru so we can make a new variable die along with a
19530 DW_AT_specification. */
19531 }
19532 else if (origin && old_die->die_parent != context_die)
19533 {
19534 /* If we will be creating an inlined instance, we need a
19535 new DIE that will get annotated with
19536 DW_AT_abstract_origin. Clear things so we can get a
19537 new DIE. */
19538 gcc_assert (!DECL_ABSTRACT_P (decl));
19539 old_die = NULL;
19540 }
19541 else
19542 {
19543 /* If a DIE was dumped early, it still needs location info.
19544 Skip to where we fill the location bits. */
19545 var_die = old_die;
19546 goto gen_variable_die_location;
19547 }
19548 }
19549
19550 /* For static data members, the declaration in the class is supposed
19551 to have DW_TAG_member tag; the specification should still be
19552 DW_TAG_variable referencing the DW_TAG_member DIE. */
19553 if (declaration && class_scope_p (context_die))
19554 var_die = new_die (DW_TAG_member, context_die, decl);
19555 else
19556 var_die = new_die (DW_TAG_variable, context_die, decl);
19557
19558 if (origin != NULL)
19559 origin_die = add_abstract_origin_attribute (var_die, origin);
19560
19561 /* Loop unrolling can create multiple blocks that refer to the same
19562 static variable, so we must test for the DW_AT_declaration flag.
19563
19564 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19565 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19566 sharing them.
19567
19568 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19569 else if (decl_will_get_specification_p (old_die, decl, declaration))
19570 {
19571 /* This is a definition of a C++ class level static. */
19572 add_AT_specification (var_die, old_die);
19573 specialization_p = true;
19574 if (DECL_NAME (decl))
19575 {
19576 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19577 struct dwarf_file_data * file_index = lookup_filename (s.file);
19578
19579 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19580 add_AT_file (var_die, DW_AT_decl_file, file_index);
19581
19582 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19583 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19584
19585 if (old_die->die_tag == DW_TAG_member)
19586 add_linkage_name (var_die, decl);
19587 }
19588 }
19589 else
19590 add_name_and_src_coords_attributes (var_die, decl);
19591
19592 if ((origin == NULL && !specialization_p)
19593 || (origin != NULL
19594 && !DECL_ABSTRACT_P (decl_or_origin)
19595 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19596 decl_function_context
19597 (decl_or_origin))))
19598 {
19599 tree type = TREE_TYPE (decl_or_origin);
19600
19601 if (decl_by_reference_p (decl_or_origin))
19602 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19603 context_die);
19604 else
19605 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19606 context_die);
19607 }
19608
19609 if (origin == NULL && !specialization_p)
19610 {
19611 if (TREE_PUBLIC (decl))
19612 add_AT_flag (var_die, DW_AT_external, 1);
19613
19614 if (DECL_ARTIFICIAL (decl))
19615 add_AT_flag (var_die, DW_AT_artificial, 1);
19616
19617 add_accessibility_attribute (var_die, decl);
19618 }
19619
19620 if (declaration)
19621 add_AT_flag (var_die, DW_AT_declaration, 1);
19622
19623 if (decl && (DECL_ABSTRACT_P (decl)
19624 || !old_die || is_declaration_die (old_die)))
19625 equate_decl_number_to_die (decl, var_die);
19626
19627 gen_variable_die_location:
19628 if (! declaration
19629 && (! DECL_ABSTRACT_P (decl_or_origin)
19630 /* Local static vars are shared between all clones/inlines,
19631 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19632 already set. */
19633 || (TREE_CODE (decl_or_origin) == VAR_DECL
19634 && TREE_STATIC (decl_or_origin)
19635 && DECL_RTL_SET_P (decl_or_origin)))
19636 /* When abstract origin already has DW_AT_location attribute, no need
19637 to add it again. */
19638 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19639 {
19640 if (early_dwarf)
19641 add_pubname (decl_or_origin, var_die);
19642 else
19643 add_location_or_const_value_attribute (var_die, decl_or_origin,
19644 decl == NULL, DW_AT_location);
19645 }
19646 else
19647 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19648 }
19649
19650 /* Generate a DIE to represent a named constant. */
19651
19652 static void
19653 gen_const_die (tree decl, dw_die_ref context_die)
19654 {
19655 dw_die_ref const_die;
19656 tree type = TREE_TYPE (decl);
19657
19658 const_die = lookup_decl_die (decl);
19659 if (const_die)
19660 return;
19661
19662 const_die = new_die (DW_TAG_constant, context_die, decl);
19663 equate_decl_number_to_die (decl, const_die);
19664 add_name_and_src_coords_attributes (const_die, decl);
19665 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19666 if (TREE_PUBLIC (decl))
19667 add_AT_flag (const_die, DW_AT_external, 1);
19668 if (DECL_ARTIFICIAL (decl))
19669 add_AT_flag (const_die, DW_AT_artificial, 1);
19670 tree_add_const_value_attribute_for_decl (const_die, decl);
19671 }
19672
19673 /* Generate a DIE to represent a label identifier. */
19674
19675 static void
19676 gen_label_die (tree decl, dw_die_ref context_die)
19677 {
19678 tree origin = decl_ultimate_origin (decl);
19679 dw_die_ref lbl_die = lookup_decl_die (decl);
19680 rtx insn;
19681 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19682
19683 if (!lbl_die)
19684 {
19685 lbl_die = new_die (DW_TAG_label, context_die, decl);
19686 equate_decl_number_to_die (decl, lbl_die);
19687
19688 if (origin != NULL)
19689 add_abstract_origin_attribute (lbl_die, origin);
19690 else
19691 add_name_and_src_coords_attributes (lbl_die, decl);
19692 }
19693
19694 if (DECL_ABSTRACT_P (decl))
19695 equate_decl_number_to_die (decl, lbl_die);
19696 else
19697 {
19698 insn = DECL_RTL_IF_SET (decl);
19699
19700 /* Deleted labels are programmer specified labels which have been
19701 eliminated because of various optimizations. We still emit them
19702 here so that it is possible to put breakpoints on them. */
19703 if (insn
19704 && (LABEL_P (insn)
19705 || ((NOTE_P (insn)
19706 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19707 {
19708 /* When optimization is enabled (via -O) some parts of the compiler
19709 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19710 represent source-level labels which were explicitly declared by
19711 the user. This really shouldn't be happening though, so catch
19712 it if it ever does happen. */
19713 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19714
19715 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19716 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19717 }
19718 else if (insn
19719 && NOTE_P (insn)
19720 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19721 && CODE_LABEL_NUMBER (insn) != -1)
19722 {
19723 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19724 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19725 }
19726 }
19727 }
19728
19729 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19730 attributes to the DIE for a block STMT, to describe where the inlined
19731 function was called from. This is similar to add_src_coords_attributes. */
19732
19733 static inline void
19734 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19735 {
19736 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19737
19738 if (dwarf_version >= 3 || !dwarf_strict)
19739 {
19740 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19741 add_AT_unsigned (die, DW_AT_call_line, s.line);
19742 }
19743 }
19744
19745
19746 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19747 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19748
19749 static inline void
19750 add_high_low_attributes (tree stmt, dw_die_ref die)
19751 {
19752 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19753
19754 if (BLOCK_FRAGMENT_CHAIN (stmt)
19755 && (dwarf_version >= 3 || !dwarf_strict))
19756 {
19757 tree chain, superblock = NULL_TREE;
19758 dw_die_ref pdie;
19759 dw_attr_ref attr = NULL;
19760
19761 if (inlined_function_outer_scope_p (stmt))
19762 {
19763 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19764 BLOCK_NUMBER (stmt));
19765 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19766 }
19767
19768 /* Optimize duplicate .debug_ranges lists or even tails of
19769 lists. If this BLOCK has same ranges as its supercontext,
19770 lookup DW_AT_ranges attribute in the supercontext (and
19771 recursively so), verify that the ranges_table contains the
19772 right values and use it instead of adding a new .debug_range. */
19773 for (chain = stmt, pdie = die;
19774 BLOCK_SAME_RANGE (chain);
19775 chain = BLOCK_SUPERCONTEXT (chain))
19776 {
19777 dw_attr_ref new_attr;
19778
19779 pdie = pdie->die_parent;
19780 if (pdie == NULL)
19781 break;
19782 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19783 break;
19784 new_attr = get_AT (pdie, DW_AT_ranges);
19785 if (new_attr == NULL
19786 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19787 break;
19788 attr = new_attr;
19789 superblock = BLOCK_SUPERCONTEXT (chain);
19790 }
19791 if (attr != NULL
19792 && (ranges_table[attr->dw_attr_val.v.val_offset
19793 / 2 / DWARF2_ADDR_SIZE].num
19794 == BLOCK_NUMBER (superblock))
19795 && BLOCK_FRAGMENT_CHAIN (superblock))
19796 {
19797 unsigned long off = attr->dw_attr_val.v.val_offset
19798 / 2 / DWARF2_ADDR_SIZE;
19799 unsigned long supercnt = 0, thiscnt = 0;
19800 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19801 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19802 {
19803 ++supercnt;
19804 gcc_checking_assert (ranges_table[off + supercnt].num
19805 == BLOCK_NUMBER (chain));
19806 }
19807 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19808 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19809 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19810 ++thiscnt;
19811 gcc_assert (supercnt >= thiscnt);
19812 add_AT_range_list (die, DW_AT_ranges,
19813 ((off + supercnt - thiscnt)
19814 * 2 * DWARF2_ADDR_SIZE),
19815 false);
19816 return;
19817 }
19818
19819 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19820
19821 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19822 do
19823 {
19824 add_ranges (chain);
19825 chain = BLOCK_FRAGMENT_CHAIN (chain);
19826 }
19827 while (chain);
19828 add_ranges (NULL);
19829 }
19830 else
19831 {
19832 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19833 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19834 BLOCK_NUMBER (stmt));
19835 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19836 BLOCK_NUMBER (stmt));
19837 add_AT_low_high_pc (die, label, label_high, false);
19838 }
19839 }
19840
19841 /* Generate a DIE for a lexical block. */
19842
19843 static void
19844 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19845 {
19846 dw_die_ref old_die = BLOCK_DIE (stmt);
19847 dw_die_ref stmt_die = NULL;
19848 if (!old_die)
19849 {
19850 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19851 BLOCK_DIE (stmt) = stmt_die;
19852 }
19853
19854 if (BLOCK_ABSTRACT (stmt))
19855 {
19856 if (old_die)
19857 {
19858 #ifdef ENABLE_CHECKING
19859 /* This must have been generated early and it won't even
19860 need location information since it's a DW_AT_inline
19861 function. */
19862 for (dw_die_ref c = context_die; c; c = c->die_parent)
19863 if (c->die_tag == DW_TAG_inlined_subroutine
19864 || c->die_tag == DW_TAG_subprogram)
19865 {
19866 gcc_assert (get_AT (c, DW_AT_inline));
19867 break;
19868 }
19869 #endif
19870 return;
19871 }
19872 }
19873 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19874 {
19875 /* If this is an inlined instance, create a new lexical die for
19876 anything below to attach DW_AT_abstract_origin to. */
19877 if (old_die)
19878 {
19879 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19880 BLOCK_DIE (stmt) = stmt_die;
19881 old_die = NULL;
19882 }
19883 }
19884
19885 if (old_die)
19886 stmt_die = old_die;
19887
19888 /* A non abstract block whose blocks have already been reordered
19889 should have the instruction range for this block. If so, set the
19890 high/low attributes. */
19891 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19892 {
19893 gcc_assert (stmt_die);
19894 add_high_low_attributes (stmt, stmt_die);
19895 }
19896
19897 decls_for_scope (stmt, stmt_die);
19898 }
19899
19900 /* Generate a DIE for an inlined subprogram. */
19901
19902 static void
19903 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19904 {
19905 tree decl;
19906
19907 /* The instance of function that is effectively being inlined shall not
19908 be abstract. */
19909 gcc_assert (! BLOCK_ABSTRACT (stmt));
19910
19911 decl = block_ultimate_origin (stmt);
19912
19913 /* Emit info for the abstract instance first, if we haven't yet. We
19914 must emit this even if the block is abstract, otherwise when we
19915 emit the block below (or elsewhere), we may end up trying to emit
19916 a die whose origin die hasn't been emitted, and crashing. */
19917 dwarf2out_abstract_function (decl);
19918
19919 if (! BLOCK_ABSTRACT (stmt))
19920 {
19921 dw_die_ref subr_die
19922 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19923
19924 if (call_arg_locations)
19925 BLOCK_DIE (stmt) = subr_die;
19926 add_abstract_origin_attribute (subr_die, decl);
19927 if (TREE_ASM_WRITTEN (stmt))
19928 add_high_low_attributes (stmt, subr_die);
19929 add_call_src_coords_attributes (stmt, subr_die);
19930
19931 decls_for_scope (stmt, subr_die);
19932 }
19933 }
19934
19935 /* Generate a DIE for a field in a record, or structure. */
19936
19937 static void
19938 gen_field_die (tree decl, dw_die_ref context_die)
19939 {
19940 dw_die_ref decl_die;
19941
19942 if (TREE_TYPE (decl) == error_mark_node)
19943 return;
19944
19945 decl_die = new_die (DW_TAG_member, context_die, decl);
19946 add_name_and_src_coords_attributes (decl_die, decl);
19947 add_type_attribute (decl_die, member_declared_type (decl),
19948 decl_quals (decl), context_die);
19949
19950 if (DECL_BIT_FIELD_TYPE (decl))
19951 {
19952 add_byte_size_attribute (decl_die, decl);
19953 add_bit_size_attribute (decl_die, decl);
19954 add_bit_offset_attribute (decl_die, decl);
19955 }
19956
19957 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19958 add_data_member_location_attribute (decl_die, decl);
19959
19960 if (DECL_ARTIFICIAL (decl))
19961 add_AT_flag (decl_die, DW_AT_artificial, 1);
19962
19963 add_accessibility_attribute (decl_die, decl);
19964
19965 /* Equate decl number to die, so that we can look up this decl later on. */
19966 equate_decl_number_to_die (decl, decl_die);
19967 }
19968
19969 #if 0
19970 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19971 Use modified_type_die instead.
19972 We keep this code here just in case these types of DIEs may be needed to
19973 represent certain things in other languages (e.g. Pascal) someday. */
19974
19975 static void
19976 gen_pointer_type_die (tree type, dw_die_ref context_die)
19977 {
19978 dw_die_ref ptr_die
19979 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19980
19981 equate_type_number_to_die (type, ptr_die);
19982 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19983 context_die);
19984 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19985 }
19986
19987 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19988 Use modified_type_die instead.
19989 We keep this code here just in case these types of DIEs may be needed to
19990 represent certain things in other languages (e.g. Pascal) someday. */
19991
19992 static void
19993 gen_reference_type_die (tree type, dw_die_ref context_die)
19994 {
19995 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19996
19997 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19998 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19999 else
20000 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20001
20002 equate_type_number_to_die (type, ref_die);
20003 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20004 context_die);
20005 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20006 }
20007 #endif
20008
20009 /* Generate a DIE for a pointer to a member type. */
20010
20011 static void
20012 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20013 {
20014 dw_die_ref ptr_die
20015 = new_die (DW_TAG_ptr_to_member_type,
20016 scope_die_for (type, context_die), type);
20017
20018 equate_type_number_to_die (type, ptr_die);
20019 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20020 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20021 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20022 context_die);
20023 }
20024
20025 typedef const char *dchar_p; /* For DEF_VEC_P. */
20026
20027 static char *producer_string;
20028
20029 /* Return a heap allocated producer string including command line options
20030 if -grecord-gcc-switches. */
20031
20032 static char *
20033 gen_producer_string (void)
20034 {
20035 size_t j;
20036 auto_vec<dchar_p> switches;
20037 const char *language_string = lang_hooks.name;
20038 char *producer, *tail;
20039 const char *p;
20040 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20041 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20042
20043 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20044 switch (save_decoded_options[j].opt_index)
20045 {
20046 case OPT_o:
20047 case OPT_d:
20048 case OPT_dumpbase:
20049 case OPT_dumpdir:
20050 case OPT_auxbase:
20051 case OPT_auxbase_strip:
20052 case OPT_quiet:
20053 case OPT_version:
20054 case OPT_v:
20055 case OPT_w:
20056 case OPT_L:
20057 case OPT_D:
20058 case OPT_I:
20059 case OPT_U:
20060 case OPT_SPECIAL_unknown:
20061 case OPT_SPECIAL_ignore:
20062 case OPT_SPECIAL_program_name:
20063 case OPT_SPECIAL_input_file:
20064 case OPT_grecord_gcc_switches:
20065 case OPT_gno_record_gcc_switches:
20066 case OPT__output_pch_:
20067 case OPT_fdiagnostics_show_location_:
20068 case OPT_fdiagnostics_show_option:
20069 case OPT_fdiagnostics_show_caret:
20070 case OPT_fdiagnostics_color_:
20071 case OPT_fverbose_asm:
20072 case OPT____:
20073 case OPT__sysroot_:
20074 case OPT_nostdinc:
20075 case OPT_nostdinc__:
20076 case OPT_fpreprocessed:
20077 case OPT_fltrans_output_list_:
20078 case OPT_fresolution_:
20079 /* Ignore these. */
20080 continue;
20081 default:
20082 if (cl_options[save_decoded_options[j].opt_index].flags
20083 & CL_NO_DWARF_RECORD)
20084 continue;
20085 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20086 == '-');
20087 switch (save_decoded_options[j].canonical_option[0][1])
20088 {
20089 case 'M':
20090 case 'i':
20091 case 'W':
20092 continue;
20093 case 'f':
20094 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20095 "dump", 4) == 0)
20096 continue;
20097 break;
20098 default:
20099 break;
20100 }
20101 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20102 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20103 break;
20104 }
20105
20106 producer = XNEWVEC (char, plen + 1 + len + 1);
20107 tail = producer;
20108 sprintf (tail, "%s %s", language_string, version_string);
20109 tail += plen;
20110
20111 FOR_EACH_VEC_ELT (switches, j, p)
20112 {
20113 len = strlen (p);
20114 *tail = ' ';
20115 memcpy (tail + 1, p, len);
20116 tail += len + 1;
20117 }
20118
20119 *tail = '\0';
20120 return producer;
20121 }
20122
20123 /* Given a C and/or C++ language/version string return the "highest".
20124 C++ is assumed to be "higher" than C in this case. Used for merging
20125 LTO translation unit languages. */
20126 static const char *
20127 highest_c_language (const char *lang1, const char *lang2)
20128 {
20129 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20130 return "GNU C++14";
20131 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20132 return "GNU C++11";
20133 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20134 return "GNU C++98";
20135
20136 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20137 return "GNU C11";
20138 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20139 return "GNU C99";
20140 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20141 return "GNU C89";
20142
20143 gcc_unreachable ();
20144 }
20145
20146
20147 /* Generate the DIE for the compilation unit. */
20148
20149 static dw_die_ref
20150 gen_compile_unit_die (const char *filename)
20151 {
20152 dw_die_ref die;
20153 const char *language_string = lang_hooks.name;
20154 int language;
20155
20156 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20157
20158 if (filename)
20159 {
20160 add_name_attribute (die, filename);
20161 /* Don't add cwd for <built-in>. */
20162 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20163 add_comp_dir_attribute (die);
20164 }
20165
20166 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20167
20168 /* If our producer is LTO try to figure out a common language to use
20169 from the global list of translation units. */
20170 if (strcmp (language_string, "GNU GIMPLE") == 0)
20171 {
20172 unsigned i;
20173 tree t;
20174 const char *common_lang = NULL;
20175
20176 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20177 {
20178 if (!TRANSLATION_UNIT_LANGUAGE (t))
20179 continue;
20180 if (!common_lang)
20181 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20182 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20183 ;
20184 else if (strncmp (common_lang, "GNU C", 5) == 0
20185 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20186 /* Mixing C and C++ is ok, use C++ in that case. */
20187 common_lang = highest_c_language (common_lang,
20188 TRANSLATION_UNIT_LANGUAGE (t));
20189 else
20190 {
20191 /* Fall back to C. */
20192 common_lang = NULL;
20193 break;
20194 }
20195 }
20196
20197 if (common_lang)
20198 language_string = common_lang;
20199 }
20200
20201 language = DW_LANG_C;
20202 if (strncmp (language_string, "GNU C", 5) == 0
20203 && ISDIGIT (language_string[5]))
20204 {
20205 language = DW_LANG_C89;
20206 if (dwarf_version >= 3 || !dwarf_strict)
20207 {
20208 if (strcmp (language_string, "GNU C89") != 0)
20209 language = DW_LANG_C99;
20210
20211 if (dwarf_version >= 5 /* || !dwarf_strict */)
20212 if (strcmp (language_string, "GNU C11") == 0)
20213 language = DW_LANG_C11;
20214 }
20215 }
20216 else if (strncmp (language_string, "GNU C++", 7) == 0)
20217 {
20218 language = DW_LANG_C_plus_plus;
20219 if (dwarf_version >= 5 /* || !dwarf_strict */)
20220 {
20221 if (strcmp (language_string, "GNU C++11") == 0)
20222 language = DW_LANG_C_plus_plus_11;
20223 else if (strcmp (language_string, "GNU C++14") == 0)
20224 language = DW_LANG_C_plus_plus_14;
20225 }
20226 }
20227 else if (strcmp (language_string, "GNU F77") == 0)
20228 language = DW_LANG_Fortran77;
20229 else if (strcmp (language_string, "GNU Pascal") == 0)
20230 language = DW_LANG_Pascal83;
20231 else if (dwarf_version >= 3 || !dwarf_strict)
20232 {
20233 if (strcmp (language_string, "GNU Ada") == 0)
20234 language = DW_LANG_Ada95;
20235 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20236 {
20237 language = DW_LANG_Fortran95;
20238 if (dwarf_version >= 5 /* || !dwarf_strict */)
20239 {
20240 if (strcmp (language_string, "GNU Fortran2003") == 0)
20241 language = DW_LANG_Fortran03;
20242 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20243 language = DW_LANG_Fortran08;
20244 }
20245 }
20246 else if (strcmp (language_string, "GNU Java") == 0)
20247 language = DW_LANG_Java;
20248 else if (strcmp (language_string, "GNU Objective-C") == 0)
20249 language = DW_LANG_ObjC;
20250 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20251 language = DW_LANG_ObjC_plus_plus;
20252 else if (dwarf_version >= 5 || !dwarf_strict)
20253 {
20254 if (strcmp (language_string, "GNU Go") == 0)
20255 language = DW_LANG_Go;
20256 }
20257 }
20258 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20259 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20260 language = DW_LANG_Fortran90;
20261
20262 add_AT_unsigned (die, DW_AT_language, language);
20263
20264 switch (language)
20265 {
20266 case DW_LANG_Fortran77:
20267 case DW_LANG_Fortran90:
20268 case DW_LANG_Fortran95:
20269 case DW_LANG_Fortran03:
20270 case DW_LANG_Fortran08:
20271 /* Fortran has case insensitive identifiers and the front-end
20272 lowercases everything. */
20273 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20274 break;
20275 default:
20276 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20277 break;
20278 }
20279 return die;
20280 }
20281
20282 /* Generate the DIE for a base class. */
20283
20284 static void
20285 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20286 {
20287 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20288
20289 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20290 add_data_member_location_attribute (die, binfo);
20291
20292 if (BINFO_VIRTUAL_P (binfo))
20293 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20294
20295 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20296 children, otherwise the default is DW_ACCESS_public. In DWARF2
20297 the default has always been DW_ACCESS_private. */
20298 if (access == access_public_node)
20299 {
20300 if (dwarf_version == 2
20301 || context_die->die_tag == DW_TAG_class_type)
20302 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20303 }
20304 else if (access == access_protected_node)
20305 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20306 else if (dwarf_version > 2
20307 && context_die->die_tag != DW_TAG_class_type)
20308 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20309 }
20310
20311 /* Generate a DIE for a class member. */
20312
20313 static void
20314 gen_member_die (tree type, dw_die_ref context_die)
20315 {
20316 tree member;
20317 tree binfo = TYPE_BINFO (type);
20318 dw_die_ref child;
20319
20320 /* If this is not an incomplete type, output descriptions of each of its
20321 members. Note that as we output the DIEs necessary to represent the
20322 members of this record or union type, we will also be trying to output
20323 DIEs to represent the *types* of those members. However the `type'
20324 function (above) will specifically avoid generating type DIEs for member
20325 types *within* the list of member DIEs for this (containing) type except
20326 for those types (of members) which are explicitly marked as also being
20327 members of this (containing) type themselves. The g++ front- end can
20328 force any given type to be treated as a member of some other (containing)
20329 type by setting the TYPE_CONTEXT of the given (member) type to point to
20330 the TREE node representing the appropriate (containing) type. */
20331
20332 /* First output info about the base classes. */
20333 if (binfo)
20334 {
20335 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20336 int i;
20337 tree base;
20338
20339 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20340 gen_inheritance_die (base,
20341 (accesses ? (*accesses)[i] : access_public_node),
20342 context_die);
20343 }
20344
20345 /* Now output info about the data members and type members. */
20346 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20347 {
20348 /* If we thought we were generating minimal debug info for TYPE
20349 and then changed our minds, some of the member declarations
20350 may have already been defined. Don't define them again, but
20351 do put them in the right order. */
20352
20353 child = lookup_decl_die (member);
20354 if (child)
20355 splice_child_die (context_die, child);
20356 else
20357 gen_decl_die (member, NULL, context_die);
20358 }
20359
20360 /* We do not keep type methods in type variants. */
20361 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20362 /* Now output info about the function members (if any). */
20363 if (TYPE_METHODS (type) != error_mark_node)
20364 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20365 {
20366 /* Don't include clones in the member list. */
20367 if (DECL_ABSTRACT_ORIGIN (member))
20368 continue;
20369 /* Nor constructors for anonymous classes. */
20370 if (DECL_ARTIFICIAL (member)
20371 && dwarf2_name (member, 0) == NULL)
20372 continue;
20373
20374 child = lookup_decl_die (member);
20375 if (child)
20376 splice_child_die (context_die, child);
20377 else
20378 gen_decl_die (member, NULL, context_die);
20379 }
20380 }
20381
20382 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20383 is set, we pretend that the type was never defined, so we only get the
20384 member DIEs needed by later specification DIEs. */
20385
20386 static void
20387 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20388 enum debug_info_usage usage)
20389 {
20390 if (TREE_ASM_WRITTEN (type))
20391 {
20392 /* Fill in the bound of variable-length fields in late dwarf if
20393 still incomplete. */
20394 if (!early_dwarf && variably_modified_type_p (type, NULL))
20395 for (tree member = TYPE_FIELDS (type);
20396 member;
20397 member = DECL_CHAIN (member))
20398 fill_variable_array_bounds (TREE_TYPE (member));
20399 return;
20400 }
20401
20402 dw_die_ref type_die = lookup_type_die (type);
20403 dw_die_ref scope_die = 0;
20404 int nested = 0;
20405 int complete = (TYPE_SIZE (type)
20406 && (! TYPE_STUB_DECL (type)
20407 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20408 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20409 complete = complete && should_emit_struct_debug (type, usage);
20410
20411 if (type_die && ! complete)
20412 return;
20413
20414 if (TYPE_CONTEXT (type) != NULL_TREE
20415 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20416 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20417 nested = 1;
20418
20419 scope_die = scope_die_for (type, context_die);
20420
20421 /* Generate child dies for template paramaters. */
20422 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20423 schedule_generic_params_dies_gen (type);
20424
20425 if (! type_die || (nested && is_cu_die (scope_die)))
20426 /* First occurrence of type or toplevel definition of nested class. */
20427 {
20428 dw_die_ref old_die = type_die;
20429
20430 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20431 ? record_type_tag (type) : DW_TAG_union_type,
20432 scope_die, type);
20433 equate_type_number_to_die (type, type_die);
20434 if (old_die)
20435 add_AT_specification (type_die, old_die);
20436 else
20437 add_name_attribute (type_die, type_tag (type));
20438 }
20439 else
20440 remove_AT (type_die, DW_AT_declaration);
20441
20442 /* If this type has been completed, then give it a byte_size attribute and
20443 then give a list of members. */
20444 if (complete && !ns_decl)
20445 {
20446 /* Prevent infinite recursion in cases where the type of some member of
20447 this type is expressed in terms of this type itself. */
20448 TREE_ASM_WRITTEN (type) = 1;
20449 add_byte_size_attribute (type_die, type);
20450 if (TYPE_STUB_DECL (type) != NULL_TREE)
20451 {
20452 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20453 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20454 }
20455
20456 /* If the first reference to this type was as the return type of an
20457 inline function, then it may not have a parent. Fix this now. */
20458 if (type_die->die_parent == NULL)
20459 add_child_die (scope_die, type_die);
20460
20461 push_decl_scope (type);
20462 gen_member_die (type, type_die);
20463 pop_decl_scope ();
20464
20465 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20466 if (TYPE_ARTIFICIAL (type))
20467 add_AT_flag (type_die, DW_AT_artificial, 1);
20468
20469 /* GNU extension: Record what type our vtable lives in. */
20470 if (TYPE_VFIELD (type))
20471 {
20472 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20473
20474 gen_type_die (vtype, context_die);
20475 add_AT_die_ref (type_die, DW_AT_containing_type,
20476 lookup_type_die (vtype));
20477 }
20478 }
20479 else
20480 {
20481 add_AT_flag (type_die, DW_AT_declaration, 1);
20482
20483 /* We don't need to do this for function-local types. */
20484 if (TYPE_STUB_DECL (type)
20485 && ! decl_function_context (TYPE_STUB_DECL (type)))
20486 vec_safe_push (incomplete_types, type);
20487 }
20488
20489 if (get_AT (type_die, DW_AT_name))
20490 add_pubtype (type, type_die);
20491 }
20492
20493 /* Generate a DIE for a subroutine _type_. */
20494
20495 static void
20496 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20497 {
20498 tree return_type = TREE_TYPE (type);
20499 dw_die_ref subr_die
20500 = new_die (DW_TAG_subroutine_type,
20501 scope_die_for (type, context_die), type);
20502
20503 equate_type_number_to_die (type, subr_die);
20504 add_prototyped_attribute (subr_die, type);
20505 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20506 gen_formal_types_die (type, subr_die);
20507
20508 if (get_AT (subr_die, DW_AT_name))
20509 add_pubtype (type, subr_die);
20510 }
20511
20512 /* Generate a DIE for a type definition. */
20513
20514 static void
20515 gen_typedef_die (tree decl, dw_die_ref context_die)
20516 {
20517 dw_die_ref type_die;
20518 tree origin;
20519
20520 if (TREE_ASM_WRITTEN (decl))
20521 {
20522 if (DECL_ORIGINAL_TYPE (decl))
20523 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20524 return;
20525 }
20526
20527 TREE_ASM_WRITTEN (decl) = 1;
20528 type_die = new_die (DW_TAG_typedef, context_die, decl);
20529 origin = decl_ultimate_origin (decl);
20530 if (origin != NULL)
20531 add_abstract_origin_attribute (type_die, origin);
20532 else
20533 {
20534 tree type;
20535
20536 add_name_and_src_coords_attributes (type_die, decl);
20537 if (DECL_ORIGINAL_TYPE (decl))
20538 {
20539 type = DECL_ORIGINAL_TYPE (decl);
20540
20541 if (type == error_mark_node)
20542 return;
20543
20544 gcc_assert (type != TREE_TYPE (decl));
20545 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20546 }
20547 else
20548 {
20549 type = TREE_TYPE (decl);
20550
20551 if (type == error_mark_node)
20552 return;
20553
20554 if (is_naming_typedef_decl (TYPE_NAME (type)))
20555 {
20556 /* Here, we are in the case of decl being a typedef naming
20557 an anonymous type, e.g:
20558 typedef struct {...} foo;
20559 In that case TREE_TYPE (decl) is not a typedef variant
20560 type and TYPE_NAME of the anonymous type is set to the
20561 TYPE_DECL of the typedef. This construct is emitted by
20562 the C++ FE.
20563
20564 TYPE is the anonymous struct named by the typedef
20565 DECL. As we need the DW_AT_type attribute of the
20566 DW_TAG_typedef to point to the DIE of TYPE, let's
20567 generate that DIE right away. add_type_attribute
20568 called below will then pick (via lookup_type_die) that
20569 anonymous struct DIE. */
20570 if (!TREE_ASM_WRITTEN (type))
20571 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20572
20573 /* This is a GNU Extension. We are adding a
20574 DW_AT_linkage_name attribute to the DIE of the
20575 anonymous struct TYPE. The value of that attribute
20576 is the name of the typedef decl naming the anonymous
20577 struct. This greatly eases the work of consumers of
20578 this debug info. */
20579 add_linkage_attr (lookup_type_die (type), decl);
20580 }
20581 }
20582
20583 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20584
20585 if (is_naming_typedef_decl (decl))
20586 /* We want that all subsequent calls to lookup_type_die with
20587 TYPE in argument yield the DW_TAG_typedef we have just
20588 created. */
20589 equate_type_number_to_die (type, type_die);
20590
20591 add_accessibility_attribute (type_die, decl);
20592 }
20593
20594 if (DECL_ABSTRACT_P (decl))
20595 equate_decl_number_to_die (decl, type_die);
20596
20597 if (get_AT (type_die, DW_AT_name))
20598 add_pubtype (decl, type_die);
20599 }
20600
20601 /* Generate a DIE for a struct, class, enum or union type. */
20602
20603 static void
20604 gen_tagged_type_die (tree type,
20605 dw_die_ref context_die,
20606 enum debug_info_usage usage)
20607 {
20608 int need_pop;
20609
20610 if (type == NULL_TREE
20611 || !is_tagged_type (type))
20612 return;
20613
20614 if (TREE_ASM_WRITTEN (type))
20615 need_pop = 0;
20616 /* If this is a nested type whose containing class hasn't been written
20617 out yet, writing it out will cover this one, too. This does not apply
20618 to instantiations of member class templates; they need to be added to
20619 the containing class as they are generated. FIXME: This hurts the
20620 idea of combining type decls from multiple TUs, since we can't predict
20621 what set of template instantiations we'll get. */
20622 else if (TYPE_CONTEXT (type)
20623 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20624 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20625 {
20626 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20627
20628 if (TREE_ASM_WRITTEN (type))
20629 return;
20630
20631 /* If that failed, attach ourselves to the stub. */
20632 push_decl_scope (TYPE_CONTEXT (type));
20633 context_die = lookup_type_die (TYPE_CONTEXT (type));
20634 need_pop = 1;
20635 }
20636 else if (TYPE_CONTEXT (type) != NULL_TREE
20637 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20638 {
20639 /* If this type is local to a function that hasn't been written
20640 out yet, use a NULL context for now; it will be fixed up in
20641 decls_for_scope. */
20642 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20643 /* A declaration DIE doesn't count; nested types need to go in the
20644 specification. */
20645 if (context_die && is_declaration_die (context_die))
20646 context_die = NULL;
20647 need_pop = 0;
20648 }
20649 else
20650 {
20651 context_die = declare_in_namespace (type, context_die);
20652 need_pop = 0;
20653 }
20654
20655 if (TREE_CODE (type) == ENUMERAL_TYPE)
20656 {
20657 /* This might have been written out by the call to
20658 declare_in_namespace. */
20659 if (!TREE_ASM_WRITTEN (type))
20660 gen_enumeration_type_die (type, context_die);
20661 }
20662 else
20663 gen_struct_or_union_type_die (type, context_die, usage);
20664
20665 if (need_pop)
20666 pop_decl_scope ();
20667
20668 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20669 it up if it is ever completed. gen_*_type_die will set it for us
20670 when appropriate. */
20671 }
20672
20673 /* Generate a type description DIE. */
20674
20675 static void
20676 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20677 enum debug_info_usage usage)
20678 {
20679 struct array_descr_info info;
20680
20681 if (type == NULL_TREE || type == error_mark_node)
20682 return;
20683
20684 #ifdef ENABLE_CHECKING
20685 if (type)
20686 verify_type (type);
20687 #endif
20688
20689 if (TYPE_NAME (type) != NULL_TREE
20690 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20691 && is_redundant_typedef (TYPE_NAME (type))
20692 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20693 /* The DECL of this type is a typedef we don't want to emit debug
20694 info for but we want debug info for its underlying typedef.
20695 This can happen for e.g, the injected-class-name of a C++
20696 type. */
20697 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20698
20699 /* If TYPE is a typedef type variant, let's generate debug info
20700 for the parent typedef which TYPE is a type of. */
20701 if (typedef_variant_p (type))
20702 {
20703 if (TREE_ASM_WRITTEN (type))
20704 return;
20705
20706 /* Prevent broken recursion; we can't hand off to the same type. */
20707 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20708
20709 /* Give typedefs the right scope. */
20710 context_die = scope_die_for (type, context_die);
20711
20712 TREE_ASM_WRITTEN (type) = 1;
20713
20714 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20715 return;
20716 }
20717
20718 /* If type is an anonymous tagged type named by a typedef, let's
20719 generate debug info for the typedef. */
20720 if (is_naming_typedef_decl (TYPE_NAME (type)))
20721 {
20722 /* Use the DIE of the containing namespace as the parent DIE of
20723 the type description DIE we want to generate. */
20724 if (DECL_CONTEXT (TYPE_NAME (type))
20725 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20726 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20727
20728 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20729 return;
20730 }
20731
20732 /* We are going to output a DIE to represent the unqualified version
20733 of this type (i.e. without any const or volatile qualifiers) so
20734 get the main variant (i.e. the unqualified version) of this type
20735 now. (Vectors are special because the debugging info is in the
20736 cloned type itself). */
20737 if (TREE_CODE (type) != VECTOR_TYPE)
20738 type = type_main_variant (type);
20739
20740 /* If this is an array type with hidden descriptor, handle it first. */
20741 if (!TREE_ASM_WRITTEN (type)
20742 && lang_hooks.types.get_array_descr_info)
20743 {
20744 memset (&info, 0, sizeof (info));
20745 if (lang_hooks.types.get_array_descr_info (type, &info))
20746 {
20747 gen_descr_array_type_die (type, &info, context_die);
20748 TREE_ASM_WRITTEN (type) = 1;
20749 return;
20750 }
20751 }
20752
20753 if (TREE_ASM_WRITTEN (type))
20754 {
20755 /* Variable-length types may be incomplete even if
20756 TREE_ASM_WRITTEN. For such types, fall through to
20757 gen_array_type_die() and possibly fill in
20758 DW_AT_{upper,lower}_bound attributes. */
20759 if ((TREE_CODE (type) != ARRAY_TYPE
20760 && TREE_CODE (type) != RECORD_TYPE
20761 && TREE_CODE (type) != UNION_TYPE
20762 && TREE_CODE (type) != QUAL_UNION_TYPE)
20763 || !variably_modified_type_p (type, NULL))
20764 return;
20765 }
20766
20767 switch (TREE_CODE (type))
20768 {
20769 case ERROR_MARK:
20770 break;
20771
20772 case POINTER_TYPE:
20773 case REFERENCE_TYPE:
20774 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20775 ensures that the gen_type_die recursion will terminate even if the
20776 type is recursive. Recursive types are possible in Ada. */
20777 /* ??? We could perhaps do this for all types before the switch
20778 statement. */
20779 TREE_ASM_WRITTEN (type) = 1;
20780
20781 /* For these types, all that is required is that we output a DIE (or a
20782 set of DIEs) to represent the "basis" type. */
20783 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20784 DINFO_USAGE_IND_USE);
20785 break;
20786
20787 case OFFSET_TYPE:
20788 /* This code is used for C++ pointer-to-data-member types.
20789 Output a description of the relevant class type. */
20790 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20791 DINFO_USAGE_IND_USE);
20792
20793 /* Output a description of the type of the object pointed to. */
20794 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20795 DINFO_USAGE_IND_USE);
20796
20797 /* Now output a DIE to represent this pointer-to-data-member type
20798 itself. */
20799 gen_ptr_to_mbr_type_die (type, context_die);
20800 break;
20801
20802 case FUNCTION_TYPE:
20803 /* Force out return type (in case it wasn't forced out already). */
20804 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20805 DINFO_USAGE_DIR_USE);
20806 gen_subroutine_type_die (type, context_die);
20807 break;
20808
20809 case METHOD_TYPE:
20810 /* Force out return type (in case it wasn't forced out already). */
20811 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20812 DINFO_USAGE_DIR_USE);
20813 gen_subroutine_type_die (type, context_die);
20814 break;
20815
20816 case ARRAY_TYPE:
20817 case VECTOR_TYPE:
20818 gen_array_type_die (type, context_die);
20819 break;
20820
20821 case ENUMERAL_TYPE:
20822 case RECORD_TYPE:
20823 case UNION_TYPE:
20824 case QUAL_UNION_TYPE:
20825 gen_tagged_type_die (type, context_die, usage);
20826 return;
20827
20828 case VOID_TYPE:
20829 case INTEGER_TYPE:
20830 case REAL_TYPE:
20831 case FIXED_POINT_TYPE:
20832 case COMPLEX_TYPE:
20833 case BOOLEAN_TYPE:
20834 case POINTER_BOUNDS_TYPE:
20835 /* No DIEs needed for fundamental types. */
20836 break;
20837
20838 case NULLPTR_TYPE:
20839 case LANG_TYPE:
20840 /* Just use DW_TAG_unspecified_type. */
20841 {
20842 dw_die_ref type_die = lookup_type_die (type);
20843 if (type_die == NULL)
20844 {
20845 tree name = TYPE_IDENTIFIER (type);
20846 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20847 type);
20848 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20849 equate_type_number_to_die (type, type_die);
20850 }
20851 }
20852 break;
20853
20854 default:
20855 if (is_cxx_auto (type))
20856 {
20857 tree name = TYPE_IDENTIFIER (type);
20858 dw_die_ref *die = (name == get_identifier ("auto")
20859 ? &auto_die : &decltype_auto_die);
20860 if (!*die)
20861 {
20862 *die = new_die (DW_TAG_unspecified_type,
20863 comp_unit_die (), NULL_TREE);
20864 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20865 }
20866 equate_type_number_to_die (type, *die);
20867 break;
20868 }
20869 gcc_unreachable ();
20870 }
20871
20872 TREE_ASM_WRITTEN (type) = 1;
20873 }
20874
20875 static void
20876 gen_type_die (tree type, dw_die_ref context_die)
20877 {
20878 if (type != error_mark_node)
20879 {
20880 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20881 #ifdef ENABLE_CHECKING
20882 dw_die_ref die = lookup_type_die (type);
20883 if (die)
20884 check_die (die);
20885 #endif
20886 }
20887 }
20888
20889 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20890 things which are local to the given block. */
20891
20892 static void
20893 gen_block_die (tree stmt, dw_die_ref context_die)
20894 {
20895 int must_output_die = 0;
20896 bool inlined_func;
20897
20898 /* Ignore blocks that are NULL. */
20899 if (stmt == NULL_TREE)
20900 return;
20901
20902 inlined_func = inlined_function_outer_scope_p (stmt);
20903
20904 /* If the block is one fragment of a non-contiguous block, do not
20905 process the variables, since they will have been done by the
20906 origin block. Do process subblocks. */
20907 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20908 {
20909 tree sub;
20910
20911 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20912 gen_block_die (sub, context_die);
20913
20914 return;
20915 }
20916
20917 /* Determine if we need to output any Dwarf DIEs at all to represent this
20918 block. */
20919 if (inlined_func)
20920 /* The outer scopes for inlinings *must* always be represented. We
20921 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20922 must_output_die = 1;
20923 else
20924 {
20925 /* Determine if this block directly contains any "significant"
20926 local declarations which we will need to output DIEs for. */
20927 if (debug_info_level > DINFO_LEVEL_TERSE)
20928 /* We are not in terse mode so *any* local declaration counts
20929 as being a "significant" one. */
20930 must_output_die = ((BLOCK_VARS (stmt) != NULL
20931 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20932 && (TREE_USED (stmt)
20933 || TREE_ASM_WRITTEN (stmt)
20934 || BLOCK_ABSTRACT (stmt)));
20935 else if ((TREE_USED (stmt)
20936 || TREE_ASM_WRITTEN (stmt)
20937 || BLOCK_ABSTRACT (stmt))
20938 && !dwarf2out_ignore_block (stmt))
20939 must_output_die = 1;
20940 }
20941
20942 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20943 DIE for any block which contains no significant local declarations at
20944 all. Rather, in such cases we just call `decls_for_scope' so that any
20945 needed Dwarf info for any sub-blocks will get properly generated. Note
20946 that in terse mode, our definition of what constitutes a "significant"
20947 local declaration gets restricted to include only inlined function
20948 instances and local (nested) function definitions. */
20949 if (must_output_die)
20950 {
20951 if (inlined_func)
20952 {
20953 /* If STMT block is abstract, that means we have been called
20954 indirectly from dwarf2out_abstract_function.
20955 That function rightfully marks the descendent blocks (of
20956 the abstract function it is dealing with) as being abstract,
20957 precisely to prevent us from emitting any
20958 DW_TAG_inlined_subroutine DIE as a descendent
20959 of an abstract function instance. So in that case, we should
20960 not call gen_inlined_subroutine_die.
20961
20962 Later though, when cgraph asks dwarf2out to emit info
20963 for the concrete instance of the function decl into which
20964 the concrete instance of STMT got inlined, the later will lead
20965 to the generation of a DW_TAG_inlined_subroutine DIE. */
20966 if (! BLOCK_ABSTRACT (stmt))
20967 gen_inlined_subroutine_die (stmt, context_die);
20968 }
20969 else
20970 gen_lexical_block_die (stmt, context_die);
20971 }
20972 else
20973 decls_for_scope (stmt, context_die);
20974 }
20975
20976 /* Process variable DECL (or variable with origin ORIGIN) within
20977 block STMT and add it to CONTEXT_DIE. */
20978 static void
20979 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20980 {
20981 dw_die_ref die;
20982 tree decl_or_origin = decl ? decl : origin;
20983
20984 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20985 die = lookup_decl_die (decl_or_origin);
20986 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20987 && TYPE_DECL_IS_STUB (decl_or_origin))
20988 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20989 else
20990 die = NULL;
20991
20992 if (die != NULL && die->die_parent == NULL)
20993 add_child_die (context_die, die);
20994 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20995 {
20996 if (early_dwarf)
20997 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20998 stmt, context_die);
20999 }
21000 else
21001 gen_decl_die (decl, origin, context_die);
21002 }
21003
21004 /* Generate all of the decls declared within a given scope and (recursively)
21005 all of its sub-blocks. */
21006
21007 static void
21008 decls_for_scope (tree stmt, dw_die_ref context_die)
21009 {
21010 tree decl;
21011 unsigned int i;
21012 tree subblocks;
21013
21014 /* Ignore NULL blocks. */
21015 if (stmt == NULL_TREE)
21016 return;
21017
21018 /* Output the DIEs to represent all of the data objects and typedefs
21019 declared directly within this block but not within any nested
21020 sub-blocks. Also, nested function and tag DIEs have been
21021 generated with a parent of NULL; fix that up now. We don't
21022 have to do this if we're at -g1. */
21023 if (debug_info_level > DINFO_LEVEL_TERSE)
21024 {
21025 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21026 process_scope_var (stmt, decl, NULL_TREE, context_die);
21027 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21028 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21029 context_die);
21030 }
21031
21032 /* Even if we're at -g1, we need to process the subblocks in order to get
21033 inlined call information. */
21034
21035 /* Output the DIEs to represent all sub-blocks (and the items declared
21036 therein) of this block. */
21037 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21038 subblocks != NULL;
21039 subblocks = BLOCK_CHAIN (subblocks))
21040 gen_block_die (subblocks, context_die);
21041 }
21042
21043 /* Is this a typedef we can avoid emitting? */
21044
21045 static inline int
21046 is_redundant_typedef (const_tree decl)
21047 {
21048 if (TYPE_DECL_IS_STUB (decl))
21049 return 1;
21050
21051 if (DECL_ARTIFICIAL (decl)
21052 && DECL_CONTEXT (decl)
21053 && is_tagged_type (DECL_CONTEXT (decl))
21054 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21055 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21056 /* Also ignore the artificial member typedef for the class name. */
21057 return 1;
21058
21059 return 0;
21060 }
21061
21062 /* Return TRUE if TYPE is a typedef that names a type for linkage
21063 purposes. This kind of typedefs is produced by the C++ FE for
21064 constructs like:
21065
21066 typedef struct {...} foo;
21067
21068 In that case, there is no typedef variant type produced for foo.
21069 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21070 struct type. */
21071
21072 static bool
21073 is_naming_typedef_decl (const_tree decl)
21074 {
21075 if (decl == NULL_TREE
21076 || TREE_CODE (decl) != TYPE_DECL
21077 || !is_tagged_type (TREE_TYPE (decl))
21078 || DECL_IS_BUILTIN (decl)
21079 || is_redundant_typedef (decl)
21080 /* It looks like Ada produces TYPE_DECLs that are very similar
21081 to C++ naming typedefs but that have different
21082 semantics. Let's be specific to c++ for now. */
21083 || !is_cxx ())
21084 return FALSE;
21085
21086 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21087 && TYPE_NAME (TREE_TYPE (decl)) == decl
21088 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21089 != TYPE_NAME (TREE_TYPE (decl))));
21090 }
21091
21092 /* Looks up the DIE for a context. */
21093
21094 static inline dw_die_ref
21095 lookup_context_die (tree context)
21096 {
21097 if (context)
21098 {
21099 /* Find die that represents this context. */
21100 if (TYPE_P (context))
21101 {
21102 context = TYPE_MAIN_VARIANT (context);
21103 dw_die_ref ctx = lookup_type_die (context);
21104 if (!ctx)
21105 return NULL;
21106 return strip_naming_typedef (context, ctx);
21107 }
21108 else
21109 return lookup_decl_die (context);
21110 }
21111 return comp_unit_die ();
21112 }
21113
21114 /* Returns the DIE for a context. */
21115
21116 static inline dw_die_ref
21117 get_context_die (tree context)
21118 {
21119 if (context)
21120 {
21121 /* Find die that represents this context. */
21122 if (TYPE_P (context))
21123 {
21124 context = TYPE_MAIN_VARIANT (context);
21125 return strip_naming_typedef (context, force_type_die (context));
21126 }
21127 else
21128 return force_decl_die (context);
21129 }
21130 return comp_unit_die ();
21131 }
21132
21133 /* Returns the DIE for decl. A DIE will always be returned. */
21134
21135 static dw_die_ref
21136 force_decl_die (tree decl)
21137 {
21138 dw_die_ref decl_die;
21139 unsigned saved_external_flag;
21140 tree save_fn = NULL_TREE;
21141 decl_die = lookup_decl_die (decl);
21142 if (!decl_die)
21143 {
21144 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21145
21146 decl_die = lookup_decl_die (decl);
21147 if (decl_die)
21148 return decl_die;
21149
21150 switch (TREE_CODE (decl))
21151 {
21152 case FUNCTION_DECL:
21153 /* Clear current_function_decl, so that gen_subprogram_die thinks
21154 that this is a declaration. At this point, we just want to force
21155 declaration die. */
21156 save_fn = current_function_decl;
21157 current_function_decl = NULL_TREE;
21158 gen_subprogram_die (decl, context_die);
21159 current_function_decl = save_fn;
21160 break;
21161
21162 case VAR_DECL:
21163 /* Set external flag to force declaration die. Restore it after
21164 gen_decl_die() call. */
21165 saved_external_flag = DECL_EXTERNAL (decl);
21166 DECL_EXTERNAL (decl) = 1;
21167 gen_decl_die (decl, NULL, context_die);
21168 DECL_EXTERNAL (decl) = saved_external_flag;
21169 break;
21170
21171 case NAMESPACE_DECL:
21172 if (dwarf_version >= 3 || !dwarf_strict)
21173 dwarf2out_decl (decl);
21174 else
21175 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21176 decl_die = comp_unit_die ();
21177 break;
21178
21179 case TRANSLATION_UNIT_DECL:
21180 decl_die = comp_unit_die ();
21181 break;
21182
21183 default:
21184 gcc_unreachable ();
21185 }
21186
21187 /* We should be able to find the DIE now. */
21188 if (!decl_die)
21189 decl_die = lookup_decl_die (decl);
21190 gcc_assert (decl_die);
21191 }
21192
21193 return decl_die;
21194 }
21195
21196 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21197 always returned. */
21198
21199 static dw_die_ref
21200 force_type_die (tree type)
21201 {
21202 dw_die_ref type_die;
21203
21204 type_die = lookup_type_die (type);
21205 if (!type_die)
21206 {
21207 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21208
21209 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21210 context_die);
21211 gcc_assert (type_die);
21212 }
21213 return type_die;
21214 }
21215
21216 /* Force out any required namespaces to be able to output DECL,
21217 and return the new context_die for it, if it's changed. */
21218
21219 static dw_die_ref
21220 setup_namespace_context (tree thing, dw_die_ref context_die)
21221 {
21222 tree context = (DECL_P (thing)
21223 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21224 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21225 /* Force out the namespace. */
21226 context_die = force_decl_die (context);
21227
21228 return context_die;
21229 }
21230
21231 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21232 type) within its namespace, if appropriate.
21233
21234 For compatibility with older debuggers, namespace DIEs only contain
21235 declarations; all definitions are emitted at CU scope, with
21236 DW_AT_specification pointing to the declaration (like with class
21237 members). */
21238
21239 static dw_die_ref
21240 declare_in_namespace (tree thing, dw_die_ref context_die)
21241 {
21242 dw_die_ref ns_context;
21243
21244 if (debug_info_level <= DINFO_LEVEL_TERSE)
21245 return context_die;
21246
21247 /* External declarations in the local scope only need to be emitted
21248 once, not once in the namespace and once in the scope.
21249
21250 This avoids declaring the `extern' below in the
21251 namespace DIE as well as in the innermost scope:
21252
21253 namespace S
21254 {
21255 int i=5;
21256 int foo()
21257 {
21258 int i=8;
21259 extern int i;
21260 return i;
21261 }
21262 }
21263 */
21264 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21265 return context_die;
21266
21267 /* If this decl is from an inlined function, then don't try to emit it in its
21268 namespace, as we will get confused. It would have already been emitted
21269 when the abstract instance of the inline function was emitted anyways. */
21270 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21271 return context_die;
21272
21273 ns_context = setup_namespace_context (thing, context_die);
21274
21275 if (ns_context != context_die)
21276 {
21277 if (is_fortran ())
21278 return ns_context;
21279 if (DECL_P (thing))
21280 gen_decl_die (thing, NULL, ns_context);
21281 else
21282 gen_type_die (thing, ns_context);
21283 }
21284 return context_die;
21285 }
21286
21287 /* Generate a DIE for a namespace or namespace alias. */
21288
21289 static void
21290 gen_namespace_die (tree decl, dw_die_ref context_die)
21291 {
21292 dw_die_ref namespace_die;
21293
21294 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21295 they are an alias of. */
21296 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21297 {
21298 /* Output a real namespace or module. */
21299 context_die = setup_namespace_context (decl, comp_unit_die ());
21300 namespace_die = new_die (is_fortran ()
21301 ? DW_TAG_module : DW_TAG_namespace,
21302 context_die, decl);
21303 /* For Fortran modules defined in different CU don't add src coords. */
21304 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21305 {
21306 const char *name = dwarf2_name (decl, 0);
21307 if (name)
21308 add_name_attribute (namespace_die, name);
21309 }
21310 else
21311 add_name_and_src_coords_attributes (namespace_die, decl);
21312 if (DECL_EXTERNAL (decl))
21313 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21314 equate_decl_number_to_die (decl, namespace_die);
21315 }
21316 else
21317 {
21318 /* Output a namespace alias. */
21319
21320 /* Force out the namespace we are an alias of, if necessary. */
21321 dw_die_ref origin_die
21322 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21323
21324 if (DECL_FILE_SCOPE_P (decl)
21325 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21326 context_die = setup_namespace_context (decl, comp_unit_die ());
21327 /* Now create the namespace alias DIE. */
21328 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21329 add_name_and_src_coords_attributes (namespace_die, decl);
21330 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21331 equate_decl_number_to_die (decl, namespace_die);
21332 }
21333 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21334 if (want_pubnames ())
21335 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21336 }
21337
21338 /* Generate Dwarf debug information for a decl described by DECL.
21339 The return value is currently only meaningful for PARM_DECLs,
21340 for all other decls it returns NULL. */
21341
21342 static dw_die_ref
21343 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21344 {
21345 tree decl_or_origin = decl ? decl : origin;
21346 tree class_origin = NULL, ultimate_origin;
21347
21348 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21349 return NULL;
21350
21351 /* Ignore pointer bounds decls. */
21352 if (DECL_P (decl_or_origin)
21353 && TREE_TYPE (decl_or_origin)
21354 && POINTER_BOUNDS_P (decl_or_origin))
21355 return NULL;
21356
21357 switch (TREE_CODE (decl_or_origin))
21358 {
21359 case ERROR_MARK:
21360 break;
21361
21362 case CONST_DECL:
21363 if (!is_fortran () && !is_ada ())
21364 {
21365 /* The individual enumerators of an enum type get output when we output
21366 the Dwarf representation of the relevant enum type itself. */
21367 break;
21368 }
21369
21370 /* Emit its type. */
21371 gen_type_die (TREE_TYPE (decl), context_die);
21372
21373 /* And its containing namespace. */
21374 context_die = declare_in_namespace (decl, context_die);
21375
21376 gen_const_die (decl, context_die);
21377 break;
21378
21379 case FUNCTION_DECL:
21380 /* Don't output any DIEs to represent mere function declarations,
21381 unless they are class members or explicit block externs. */
21382 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21383 && DECL_FILE_SCOPE_P (decl_or_origin)
21384 && (current_function_decl == NULL_TREE
21385 || DECL_ARTIFICIAL (decl_or_origin)))
21386 break;
21387
21388 #if 0
21389 /* FIXME */
21390 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21391 on local redeclarations of global functions. That seems broken. */
21392 if (current_function_decl != decl)
21393 /* This is only a declaration. */;
21394 #endif
21395
21396 /* If we're emitting a clone, emit info for the abstract instance. */
21397 if (origin || DECL_ORIGIN (decl) != decl)
21398 dwarf2out_abstract_function (origin
21399 ? DECL_ORIGIN (origin)
21400 : DECL_ABSTRACT_ORIGIN (decl));
21401
21402 /* If we're emitting an out-of-line copy of an inline function,
21403 emit info for the abstract instance and set up to refer to it. */
21404 else if (cgraph_function_possibly_inlined_p (decl)
21405 && ! DECL_ABSTRACT_P (decl)
21406 && ! class_or_namespace_scope_p (context_die)
21407 /* dwarf2out_abstract_function won't emit a die if this is just
21408 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21409 that case, because that works only if we have a die. */
21410 && DECL_INITIAL (decl) != NULL_TREE)
21411 {
21412 dwarf2out_abstract_function (decl);
21413 set_decl_origin_self (decl);
21414 }
21415
21416 /* Otherwise we're emitting the primary DIE for this decl. */
21417 else if (debug_info_level > DINFO_LEVEL_TERSE)
21418 {
21419 /* Before we describe the FUNCTION_DECL itself, make sure that we
21420 have its containing type. */
21421 if (!origin)
21422 origin = decl_class_context (decl);
21423 if (origin != NULL_TREE)
21424 gen_type_die (origin, context_die);
21425
21426 /* And its return type. */
21427 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21428
21429 /* And its virtual context. */
21430 if (DECL_VINDEX (decl) != NULL_TREE)
21431 gen_type_die (DECL_CONTEXT (decl), context_die);
21432
21433 /* Make sure we have a member DIE for decl. */
21434 if (origin != NULL_TREE)
21435 gen_type_die_for_member (origin, decl, context_die);
21436
21437 /* And its containing namespace. */
21438 context_die = declare_in_namespace (decl, context_die);
21439 }
21440
21441 /* Now output a DIE to represent the function itself. */
21442 if (decl)
21443 gen_subprogram_die (decl, context_die);
21444 break;
21445
21446 case TYPE_DECL:
21447 /* If we are in terse mode, don't generate any DIEs to represent any
21448 actual typedefs. */
21449 if (debug_info_level <= DINFO_LEVEL_TERSE)
21450 break;
21451
21452 /* In the special case of a TYPE_DECL node representing the declaration
21453 of some type tag, if the given TYPE_DECL is marked as having been
21454 instantiated from some other (original) TYPE_DECL node (e.g. one which
21455 was generated within the original definition of an inline function) we
21456 used to generate a special (abbreviated) DW_TAG_structure_type,
21457 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21458 should be actually referencing those DIEs, as variable DIEs with that
21459 type would be emitted already in the abstract origin, so it was always
21460 removed during unused type prunning. Don't add anything in this
21461 case. */
21462 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21463 break;
21464
21465 if (is_redundant_typedef (decl))
21466 gen_type_die (TREE_TYPE (decl), context_die);
21467 else
21468 /* Output a DIE to represent the typedef itself. */
21469 gen_typedef_die (decl, context_die);
21470 break;
21471
21472 case LABEL_DECL:
21473 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21474 gen_label_die (decl, context_die);
21475 break;
21476
21477 case VAR_DECL:
21478 case RESULT_DECL:
21479 /* If we are in terse mode, don't generate any DIEs to represent any
21480 variable declarations or definitions. */
21481 if (debug_info_level <= DINFO_LEVEL_TERSE)
21482 break;
21483
21484 /* Output any DIEs that are needed to specify the type of this data
21485 object. */
21486 if (decl_by_reference_p (decl_or_origin))
21487 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21488 else
21489 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21490
21491 /* And its containing type. */
21492 class_origin = decl_class_context (decl_or_origin);
21493 if (class_origin != NULL_TREE)
21494 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21495
21496 /* And its containing namespace. */
21497 context_die = declare_in_namespace (decl_or_origin, context_die);
21498
21499 /* Now output the DIE to represent the data object itself. This gets
21500 complicated because of the possibility that the VAR_DECL really
21501 represents an inlined instance of a formal parameter for an inline
21502 function. */
21503 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21504 if (ultimate_origin != NULL_TREE
21505 && TREE_CODE (ultimate_origin) == PARM_DECL)
21506 gen_formal_parameter_die (decl, origin,
21507 true /* Emit name attribute. */,
21508 context_die);
21509 else
21510 gen_variable_die (decl, origin, context_die);
21511 break;
21512
21513 case FIELD_DECL:
21514 /* Ignore the nameless fields that are used to skip bits but handle C++
21515 anonymous unions and structs. */
21516 if (DECL_NAME (decl) != NULL_TREE
21517 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21518 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21519 {
21520 gen_type_die (member_declared_type (decl), context_die);
21521 gen_field_die (decl, context_die);
21522 }
21523 break;
21524
21525 case PARM_DECL:
21526 if (DECL_BY_REFERENCE (decl_or_origin))
21527 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21528 else
21529 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21530 return gen_formal_parameter_die (decl, origin,
21531 true /* Emit name attribute. */,
21532 context_die);
21533
21534 case NAMESPACE_DECL:
21535 case IMPORTED_DECL:
21536 if (dwarf_version >= 3 || !dwarf_strict)
21537 gen_namespace_die (decl, context_die);
21538 break;
21539
21540 case NAMELIST_DECL:
21541 gen_namelist_decl (DECL_NAME (decl), context_die,
21542 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21543 break;
21544
21545 default:
21546 /* Probably some frontend-internal decl. Assume we don't care. */
21547 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21548 break;
21549 }
21550
21551 return NULL;
21552 }
21553 \f
21554 /* Output initial debug information for global DECL. Called at the
21555 end of the parsing process.
21556
21557 This is the initial debug generation process. As such, the DIEs
21558 generated may be incomplete. A later debug generation pass
21559 (dwarf2out_late_global_decl) will augment the information generated
21560 in this pass (e.g., with complete location info). */
21561
21562 static void
21563 dwarf2out_early_global_decl (tree decl)
21564 {
21565 set_early_dwarf s;
21566
21567 /* gen_decl_die() will set DECL_ABSTRACT because
21568 cgraph_function_possibly_inlined_p() returns true. This is in
21569 turn will cause DW_AT_inline attributes to be set.
21570
21571 This happens because at early dwarf generation, there is no
21572 cgraph information, causing cgraph_function_possibly_inlined_p()
21573 to return true. Trick cgraph_function_possibly_inlined_p()
21574 while we generate dwarf early. */
21575 bool save = symtab->global_info_ready;
21576 symtab->global_info_ready = true;
21577
21578 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21579 other DECLs and they can point to template types or other things
21580 that dwarf2out can't handle when done via dwarf2out_decl. */
21581 if (TREE_CODE (decl) != TYPE_DECL
21582 && TREE_CODE (decl) != PARM_DECL)
21583 {
21584 tree save_fndecl = current_function_decl;
21585 if (TREE_CODE (decl) == FUNCTION_DECL)
21586 {
21587 /* No cfun means the symbol has no body, so there's nothing
21588 to emit. */
21589 if (!DECL_STRUCT_FUNCTION (decl))
21590 goto early_decl_exit;
21591
21592 current_function_decl = decl;
21593 }
21594 dwarf2out_decl (decl);
21595 if (TREE_CODE (decl) == FUNCTION_DECL)
21596 current_function_decl = save_fndecl;
21597 }
21598 early_decl_exit:
21599 symtab->global_info_ready = save;
21600 }
21601
21602 /* Output debug information for global decl DECL. Called from
21603 toplev.c after compilation proper has finished. */
21604
21605 static void
21606 dwarf2out_late_global_decl (tree decl)
21607 {
21608 /* Output any global decls we missed or fill-in any location
21609 information we were unable to determine on the first pass.
21610
21611 Skip over functions because they were handled by the
21612 debug_hooks->function_decl() call in rest_of_handle_final. */
21613 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21614 && !POINTER_BOUNDS_P (decl))
21615 dwarf2out_decl (decl);
21616 }
21617
21618 /* Output debug information for type decl DECL. Called from toplev.c
21619 and from language front ends (to record built-in types). */
21620 static void
21621 dwarf2out_type_decl (tree decl, int local)
21622 {
21623 if (!local)
21624 {
21625 set_early_dwarf s;
21626 dwarf2out_decl (decl);
21627 }
21628 }
21629
21630 /* Output debug information for imported module or decl DECL.
21631 NAME is non-NULL name in the lexical block if the decl has been renamed.
21632 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21633 that DECL belongs to.
21634 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21635 static void
21636 dwarf2out_imported_module_or_decl_1 (tree decl,
21637 tree name,
21638 tree lexical_block,
21639 dw_die_ref lexical_block_die)
21640 {
21641 expanded_location xloc;
21642 dw_die_ref imported_die = NULL;
21643 dw_die_ref at_import_die;
21644
21645 if (TREE_CODE (decl) == IMPORTED_DECL)
21646 {
21647 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21648 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21649 gcc_assert (decl);
21650 }
21651 else
21652 xloc = expand_location (input_location);
21653
21654 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21655 {
21656 at_import_die = force_type_die (TREE_TYPE (decl));
21657 /* For namespace N { typedef void T; } using N::T; base_type_die
21658 returns NULL, but DW_TAG_imported_declaration requires
21659 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21660 if (!at_import_die)
21661 {
21662 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21663 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21664 at_import_die = lookup_type_die (TREE_TYPE (decl));
21665 gcc_assert (at_import_die);
21666 }
21667 }
21668 else
21669 {
21670 at_import_die = lookup_decl_die (decl);
21671 if (!at_import_die)
21672 {
21673 /* If we're trying to avoid duplicate debug info, we may not have
21674 emitted the member decl for this field. Emit it now. */
21675 if (TREE_CODE (decl) == FIELD_DECL)
21676 {
21677 tree type = DECL_CONTEXT (decl);
21678
21679 if (TYPE_CONTEXT (type)
21680 && TYPE_P (TYPE_CONTEXT (type))
21681 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21682 DINFO_USAGE_DIR_USE))
21683 return;
21684 gen_type_die_for_member (type, decl,
21685 get_context_die (TYPE_CONTEXT (type)));
21686 }
21687 if (TREE_CODE (decl) == NAMELIST_DECL)
21688 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21689 get_context_die (DECL_CONTEXT (decl)),
21690 NULL_TREE);
21691 else
21692 at_import_die = force_decl_die (decl);
21693 }
21694 }
21695
21696 if (TREE_CODE (decl) == NAMESPACE_DECL)
21697 {
21698 if (dwarf_version >= 3 || !dwarf_strict)
21699 imported_die = new_die (DW_TAG_imported_module,
21700 lexical_block_die,
21701 lexical_block);
21702 else
21703 return;
21704 }
21705 else
21706 imported_die = new_die (DW_TAG_imported_declaration,
21707 lexical_block_die,
21708 lexical_block);
21709
21710 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21711 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21712 if (name)
21713 add_AT_string (imported_die, DW_AT_name,
21714 IDENTIFIER_POINTER (name));
21715 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21716 }
21717
21718 /* Output debug information for imported module or decl DECL.
21719 NAME is non-NULL name in context if the decl has been renamed.
21720 CHILD is true if decl is one of the renamed decls as part of
21721 importing whole module. */
21722
21723 static void
21724 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21725 bool child)
21726 {
21727 /* dw_die_ref at_import_die; */
21728 dw_die_ref scope_die;
21729
21730 if (debug_info_level <= DINFO_LEVEL_TERSE)
21731 return;
21732
21733 gcc_assert (decl);
21734
21735 set_early_dwarf s;
21736
21737 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21738 We need decl DIE for reference and scope die. First, get DIE for the decl
21739 itself. */
21740
21741 /* Get the scope die for decl context. Use comp_unit_die for global module
21742 or decl. If die is not found for non globals, force new die. */
21743 if (context
21744 && TYPE_P (context)
21745 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21746 return;
21747
21748 if (!(dwarf_version >= 3 || !dwarf_strict))
21749 return;
21750
21751 scope_die = get_context_die (context);
21752
21753 if (child)
21754 {
21755 gcc_assert (scope_die->die_child);
21756 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21757 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21758 scope_die = scope_die->die_child;
21759 }
21760
21761 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21762 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21763 }
21764
21765 /* Output debug information for namelists. */
21766
21767 static dw_die_ref
21768 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21769 {
21770 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21771 tree value;
21772 unsigned i;
21773
21774 if (debug_info_level <= DINFO_LEVEL_TERSE)
21775 return NULL;
21776
21777 gcc_assert (scope_die != NULL);
21778 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21779 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21780
21781 /* If there are no item_decls, we have a nondefining namelist, e.g.
21782 with USE association; hence, set DW_AT_declaration. */
21783 if (item_decls == NULL_TREE)
21784 {
21785 add_AT_flag (nml_die, DW_AT_declaration, 1);
21786 return nml_die;
21787 }
21788
21789 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21790 {
21791 nml_item_ref_die = lookup_decl_die (value);
21792 if (!nml_item_ref_die)
21793 nml_item_ref_die = force_decl_die (value);
21794
21795 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21796 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21797 }
21798 return nml_die;
21799 }
21800
21801
21802 /* Write the debugging output for DECL and return the DIE. */
21803
21804 static void
21805 dwarf2out_decl (tree decl)
21806 {
21807 dw_die_ref context_die = comp_unit_die ();
21808
21809 switch (TREE_CODE (decl))
21810 {
21811 case ERROR_MARK:
21812 return;
21813
21814 case FUNCTION_DECL:
21815 /* What we would really like to do here is to filter out all mere
21816 file-scope declarations of file-scope functions which are never
21817 referenced later within this translation unit (and keep all of ones
21818 that *are* referenced later on) but we aren't clairvoyant, so we have
21819 no idea which functions will be referenced in the future (i.e. later
21820 on within the current translation unit). So here we just ignore all
21821 file-scope function declarations which are not also definitions. If
21822 and when the debugger needs to know something about these functions,
21823 it will have to hunt around and find the DWARF information associated
21824 with the definition of the function.
21825
21826 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21827 nodes represent definitions and which ones represent mere
21828 declarations. We have to check DECL_INITIAL instead. That's because
21829 the C front-end supports some weird semantics for "extern inline"
21830 function definitions. These can get inlined within the current
21831 translation unit (and thus, we need to generate Dwarf info for their
21832 abstract instances so that the Dwarf info for the concrete inlined
21833 instances can have something to refer to) but the compiler never
21834 generates any out-of-lines instances of such things (despite the fact
21835 that they *are* definitions).
21836
21837 The important point is that the C front-end marks these "extern
21838 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21839 them anyway. Note that the C++ front-end also plays some similar games
21840 for inline function definitions appearing within include files which
21841 also contain `#pragma interface' pragmas.
21842
21843 If we are called from dwarf2out_abstract_function output a DIE
21844 anyway. We can end up here this way with early inlining and LTO
21845 where the inlined function is output in a different LTRANS unit
21846 or not at all. */
21847 if (DECL_INITIAL (decl) == NULL_TREE
21848 && ! DECL_ABSTRACT_P (decl))
21849 return;
21850
21851 /* If we're a nested function, initially use a parent of NULL; if we're
21852 a plain function, this will be fixed up in decls_for_scope. If
21853 we're a method, it will be ignored, since we already have a DIE. */
21854 if (decl_function_context (decl)
21855 /* But if we're in terse mode, we don't care about scope. */
21856 && debug_info_level > DINFO_LEVEL_TERSE)
21857 context_die = NULL;
21858 break;
21859
21860 case VAR_DECL:
21861 /* For local statics lookup proper context die. */
21862 if (local_function_static (decl))
21863 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21864
21865 /* If we are in terse mode, don't generate any DIEs to represent any
21866 variable declarations or definitions. */
21867 if (debug_info_level <= DINFO_LEVEL_TERSE)
21868 return;
21869 break;
21870
21871 case CONST_DECL:
21872 if (debug_info_level <= DINFO_LEVEL_TERSE)
21873 return;
21874 if (!is_fortran () && !is_ada ())
21875 return;
21876 if (TREE_STATIC (decl) && decl_function_context (decl))
21877 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21878 break;
21879
21880 case NAMESPACE_DECL:
21881 case IMPORTED_DECL:
21882 if (debug_info_level <= DINFO_LEVEL_TERSE)
21883 return;
21884 if (lookup_decl_die (decl) != NULL)
21885 return;
21886 break;
21887
21888 case TYPE_DECL:
21889 /* Don't emit stubs for types unless they are needed by other DIEs. */
21890 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21891 return;
21892
21893 /* Don't bother trying to generate any DIEs to represent any of the
21894 normal built-in types for the language we are compiling. */
21895 if (DECL_IS_BUILTIN (decl))
21896 return;
21897
21898 /* If we are in terse mode, don't generate any DIEs for types. */
21899 if (debug_info_level <= DINFO_LEVEL_TERSE)
21900 return;
21901
21902 /* If we're a function-scope tag, initially use a parent of NULL;
21903 this will be fixed up in decls_for_scope. */
21904 if (decl_function_context (decl))
21905 context_die = NULL;
21906
21907 break;
21908
21909 case NAMELIST_DECL:
21910 break;
21911
21912 default:
21913 return;
21914 }
21915
21916 gen_decl_die (decl, NULL, context_die);
21917
21918 #ifdef ENABLE_CHECKING
21919 dw_die_ref die = lookup_decl_die (decl);
21920 if (die)
21921 check_die (die);
21922 #endif
21923 }
21924
21925 /* Write the debugging output for DECL. */
21926
21927 static void
21928 dwarf2out_function_decl (tree decl)
21929 {
21930 dwarf2out_decl (decl);
21931 call_arg_locations = NULL;
21932 call_arg_loc_last = NULL;
21933 call_site_count = -1;
21934 tail_call_site_count = -1;
21935 decl_loc_table->empty ();
21936 cached_dw_loc_list_table->empty ();
21937 }
21938
21939 /* Output a marker (i.e. a label) for the beginning of the generated code for
21940 a lexical block. */
21941
21942 static void
21943 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21944 unsigned int blocknum)
21945 {
21946 switch_to_section (current_function_section ());
21947 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21948 }
21949
21950 /* Output a marker (i.e. a label) for the end of the generated code for a
21951 lexical block. */
21952
21953 static void
21954 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21955 {
21956 switch_to_section (current_function_section ());
21957 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21958 }
21959
21960 /* Returns nonzero if it is appropriate not to emit any debugging
21961 information for BLOCK, because it doesn't contain any instructions.
21962
21963 Don't allow this for blocks with nested functions or local classes
21964 as we would end up with orphans, and in the presence of scheduling
21965 we may end up calling them anyway. */
21966
21967 static bool
21968 dwarf2out_ignore_block (const_tree block)
21969 {
21970 tree decl;
21971 unsigned int i;
21972
21973 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21974 if (TREE_CODE (decl) == FUNCTION_DECL
21975 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21976 return 0;
21977 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21978 {
21979 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21980 if (TREE_CODE (decl) == FUNCTION_DECL
21981 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21982 return 0;
21983 }
21984
21985 return 1;
21986 }
21987
21988 /* Hash table routines for file_hash. */
21989
21990 bool
21991 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21992 {
21993 return filename_cmp (p1->filename, p2) == 0;
21994 }
21995
21996 hashval_t
21997 dwarf_file_hasher::hash (dwarf_file_data *p)
21998 {
21999 return htab_hash_string (p->filename);
22000 }
22001
22002 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22003 dwarf2out.c) and return its "index". The index of each (known) filename is
22004 just a unique number which is associated with only that one filename. We
22005 need such numbers for the sake of generating labels (in the .debug_sfnames
22006 section) and references to those files numbers (in the .debug_srcinfo
22007 and.debug_macinfo sections). If the filename given as an argument is not
22008 found in our current list, add it to the list and assign it the next
22009 available unique index number. */
22010
22011 static struct dwarf_file_data *
22012 lookup_filename (const char *file_name)
22013 {
22014 struct dwarf_file_data * created;
22015
22016 if (!file_name)
22017 return NULL;
22018
22019 dwarf_file_data **slot
22020 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22021 INSERT);
22022 if (*slot)
22023 return *slot;
22024
22025 created = ggc_alloc<dwarf_file_data> ();
22026 created->filename = file_name;
22027 created->emitted_number = 0;
22028 *slot = created;
22029 return created;
22030 }
22031
22032 /* If the assembler will construct the file table, then translate the compiler
22033 internal file table number into the assembler file table number, and emit
22034 a .file directive if we haven't already emitted one yet. The file table
22035 numbers are different because we prune debug info for unused variables and
22036 types, which may include filenames. */
22037
22038 static int
22039 maybe_emit_file (struct dwarf_file_data * fd)
22040 {
22041 if (! fd->emitted_number)
22042 {
22043 if (last_emitted_file)
22044 fd->emitted_number = last_emitted_file->emitted_number + 1;
22045 else
22046 fd->emitted_number = 1;
22047 last_emitted_file = fd;
22048
22049 if (DWARF2_ASM_LINE_DEBUG_INFO)
22050 {
22051 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22052 output_quoted_string (asm_out_file,
22053 remap_debug_filename (fd->filename));
22054 fputc ('\n', asm_out_file);
22055 }
22056 }
22057
22058 return fd->emitted_number;
22059 }
22060
22061 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22062 That generation should happen after function debug info has been
22063 generated. The value of the attribute is the constant value of ARG. */
22064
22065 static void
22066 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22067 {
22068 die_arg_entry entry;
22069
22070 if (!die || !arg)
22071 return;
22072
22073 if (!tmpl_value_parm_die_table)
22074 vec_alloc (tmpl_value_parm_die_table, 32);
22075
22076 entry.die = die;
22077 entry.arg = arg;
22078 vec_safe_push (tmpl_value_parm_die_table, entry);
22079 }
22080
22081 /* Return TRUE if T is an instance of generic type, FALSE
22082 otherwise. */
22083
22084 static bool
22085 generic_type_p (tree t)
22086 {
22087 if (t == NULL_TREE || !TYPE_P (t))
22088 return false;
22089 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22090 }
22091
22092 /* Schedule the generation of the generic parameter dies for the
22093 instance of generic type T. The proper generation itself is later
22094 done by gen_scheduled_generic_parms_dies. */
22095
22096 static void
22097 schedule_generic_params_dies_gen (tree t)
22098 {
22099 if (!generic_type_p (t))
22100 return;
22101
22102 if (!generic_type_instances)
22103 vec_alloc (generic_type_instances, 256);
22104
22105 vec_safe_push (generic_type_instances, t);
22106 }
22107
22108 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22109 by append_entry_to_tmpl_value_parm_die_table. This function must
22110 be called after function DIEs have been generated. */
22111
22112 static void
22113 gen_remaining_tmpl_value_param_die_attribute (void)
22114 {
22115 if (tmpl_value_parm_die_table)
22116 {
22117 unsigned i;
22118 die_arg_entry *e;
22119
22120 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22121 tree_add_const_value_attribute (e->die, e->arg);
22122 }
22123 }
22124
22125 /* Generate generic parameters DIEs for instances of generic types
22126 that have been previously scheduled by
22127 schedule_generic_params_dies_gen. This function must be called
22128 after all the types of the CU have been laid out. */
22129
22130 static void
22131 gen_scheduled_generic_parms_dies (void)
22132 {
22133 unsigned i;
22134 tree t;
22135
22136 if (!generic_type_instances)
22137 return;
22138
22139 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22140 if (COMPLETE_TYPE_P (t))
22141 gen_generic_params_dies (t);
22142 }
22143
22144
22145 /* Replace DW_AT_name for the decl with name. */
22146
22147 static void
22148 dwarf2out_set_name (tree decl, tree name)
22149 {
22150 dw_die_ref die;
22151 dw_attr_ref attr;
22152 const char *dname;
22153
22154 die = TYPE_SYMTAB_DIE (decl);
22155 if (!die)
22156 return;
22157
22158 dname = dwarf2_name (name, 0);
22159 if (!dname)
22160 return;
22161
22162 attr = get_AT (die, DW_AT_name);
22163 if (attr)
22164 {
22165 struct indirect_string_node *node;
22166
22167 node = find_AT_string (dname);
22168 /* replace the string. */
22169 attr->dw_attr_val.v.val_str = node;
22170 }
22171
22172 else
22173 add_name_attribute (die, dname);
22174 }
22175
22176 /* True if before or during processing of the first function being emitted. */
22177 static bool in_first_function_p = true;
22178 /* True if loc_note during dwarf2out_var_location call might still be
22179 before first real instruction at address equal to .Ltext0. */
22180 static bool maybe_at_text_label_p = true;
22181 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22182 static unsigned int first_loclabel_num_not_at_text_label;
22183
22184 /* Called by the final INSN scan whenever we see a var location. We
22185 use it to drop labels in the right places, and throw the location in
22186 our lookup table. */
22187
22188 static void
22189 dwarf2out_var_location (rtx_insn *loc_note)
22190 {
22191 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22192 struct var_loc_node *newloc;
22193 rtx_insn *next_real, *next_note;
22194 static const char *last_label;
22195 static const char *last_postcall_label;
22196 static bool last_in_cold_section_p;
22197 static rtx_insn *expected_next_loc_note;
22198 tree decl;
22199 bool var_loc_p;
22200
22201 if (!NOTE_P (loc_note))
22202 {
22203 if (CALL_P (loc_note))
22204 {
22205 call_site_count++;
22206 if (SIBLING_CALL_P (loc_note))
22207 tail_call_site_count++;
22208 }
22209 return;
22210 }
22211
22212 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22213 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22214 return;
22215
22216 /* Optimize processing a large consecutive sequence of location
22217 notes so we don't spend too much time in next_real_insn. If the
22218 next insn is another location note, remember the next_real_insn
22219 calculation for next time. */
22220 next_real = cached_next_real_insn;
22221 if (next_real)
22222 {
22223 if (expected_next_loc_note != loc_note)
22224 next_real = NULL;
22225 }
22226
22227 next_note = NEXT_INSN (loc_note);
22228 if (! next_note
22229 || next_note->deleted ()
22230 || ! NOTE_P (next_note)
22231 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22232 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22233 next_note = NULL;
22234
22235 if (! next_real)
22236 next_real = next_real_insn (loc_note);
22237
22238 if (next_note)
22239 {
22240 expected_next_loc_note = next_note;
22241 cached_next_real_insn = next_real;
22242 }
22243 else
22244 cached_next_real_insn = NULL;
22245
22246 /* If there are no instructions which would be affected by this note,
22247 don't do anything. */
22248 if (var_loc_p
22249 && next_real == NULL_RTX
22250 && !NOTE_DURING_CALL_P (loc_note))
22251 return;
22252
22253 if (next_real == NULL_RTX)
22254 next_real = get_last_insn ();
22255
22256 /* If there were any real insns between note we processed last time
22257 and this note (or if it is the first note), clear
22258 last_{,postcall_}label so that they are not reused this time. */
22259 if (last_var_location_insn == NULL_RTX
22260 || last_var_location_insn != next_real
22261 || last_in_cold_section_p != in_cold_section_p)
22262 {
22263 last_label = NULL;
22264 last_postcall_label = NULL;
22265 }
22266
22267 if (var_loc_p)
22268 {
22269 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22270 newloc = add_var_loc_to_decl (decl, loc_note,
22271 NOTE_DURING_CALL_P (loc_note)
22272 ? last_postcall_label : last_label);
22273 if (newloc == NULL)
22274 return;
22275 }
22276 else
22277 {
22278 decl = NULL_TREE;
22279 newloc = NULL;
22280 }
22281
22282 /* If there were no real insns between note we processed last time
22283 and this note, use the label we emitted last time. Otherwise
22284 create a new label and emit it. */
22285 if (last_label == NULL)
22286 {
22287 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22288 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22289 loclabel_num++;
22290 last_label = ggc_strdup (loclabel);
22291 /* See if loclabel might be equal to .Ltext0. If yes,
22292 bump first_loclabel_num_not_at_text_label. */
22293 if (!have_multiple_function_sections
22294 && in_first_function_p
22295 && maybe_at_text_label_p)
22296 {
22297 static rtx_insn *last_start;
22298 rtx_insn *insn;
22299 for (insn = loc_note; insn; insn = previous_insn (insn))
22300 if (insn == last_start)
22301 break;
22302 else if (!NONDEBUG_INSN_P (insn))
22303 continue;
22304 else
22305 {
22306 rtx body = PATTERN (insn);
22307 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22308 continue;
22309 /* Inline asm could occupy zero bytes. */
22310 else if (GET_CODE (body) == ASM_INPUT
22311 || asm_noperands (body) >= 0)
22312 continue;
22313 #ifdef HAVE_attr_length
22314 else if (get_attr_min_length (insn) == 0)
22315 continue;
22316 #endif
22317 else
22318 {
22319 /* Assume insn has non-zero length. */
22320 maybe_at_text_label_p = false;
22321 break;
22322 }
22323 }
22324 if (maybe_at_text_label_p)
22325 {
22326 last_start = loc_note;
22327 first_loclabel_num_not_at_text_label = loclabel_num;
22328 }
22329 }
22330 }
22331
22332 if (!var_loc_p)
22333 {
22334 struct call_arg_loc_node *ca_loc
22335 = ggc_cleared_alloc<call_arg_loc_node> ();
22336 rtx_insn *prev = prev_real_insn (loc_note);
22337 rtx x;
22338 ca_loc->call_arg_loc_note = loc_note;
22339 ca_loc->next = NULL;
22340 ca_loc->label = last_label;
22341 gcc_assert (prev
22342 && (CALL_P (prev)
22343 || (NONJUMP_INSN_P (prev)
22344 && GET_CODE (PATTERN (prev)) == SEQUENCE
22345 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22346 if (!CALL_P (prev))
22347 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22348 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22349 x = get_call_rtx_from (PATTERN (prev));
22350 if (x)
22351 {
22352 x = XEXP (XEXP (x, 0), 0);
22353 if (GET_CODE (x) == SYMBOL_REF
22354 && SYMBOL_REF_DECL (x)
22355 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22356 ca_loc->symbol_ref = x;
22357 }
22358 ca_loc->block = insn_scope (prev);
22359 if (call_arg_locations)
22360 call_arg_loc_last->next = ca_loc;
22361 else
22362 call_arg_locations = ca_loc;
22363 call_arg_loc_last = ca_loc;
22364 }
22365 else if (!NOTE_DURING_CALL_P (loc_note))
22366 newloc->label = last_label;
22367 else
22368 {
22369 if (!last_postcall_label)
22370 {
22371 sprintf (loclabel, "%s-1", last_label);
22372 last_postcall_label = ggc_strdup (loclabel);
22373 }
22374 newloc->label = last_postcall_label;
22375 }
22376
22377 last_var_location_insn = next_real;
22378 last_in_cold_section_p = in_cold_section_p;
22379 }
22380
22381 /* Note in one location list that text section has changed. */
22382
22383 int
22384 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22385 {
22386 var_loc_list *list = *slot;
22387 if (list->first)
22388 list->last_before_switch
22389 = list->last->next ? list->last->next : list->last;
22390 return 1;
22391 }
22392
22393 /* Note in all location lists that text section has changed. */
22394
22395 static void
22396 var_location_switch_text_section (void)
22397 {
22398 if (decl_loc_table == NULL)
22399 return;
22400
22401 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22402 }
22403
22404 /* Create a new line number table. */
22405
22406 static dw_line_info_table *
22407 new_line_info_table (void)
22408 {
22409 dw_line_info_table *table;
22410
22411 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
22412 table->file_num = 1;
22413 table->line_num = 1;
22414 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22415
22416 return table;
22417 }
22418
22419 /* Lookup the "current" table into which we emit line info, so
22420 that we don't have to do it for every source line. */
22421
22422 static void
22423 set_cur_line_info_table (section *sec)
22424 {
22425 dw_line_info_table *table;
22426
22427 if (sec == text_section)
22428 table = text_section_line_info;
22429 else if (sec == cold_text_section)
22430 {
22431 table = cold_text_section_line_info;
22432 if (!table)
22433 {
22434 cold_text_section_line_info = table = new_line_info_table ();
22435 table->end_label = cold_end_label;
22436 }
22437 }
22438 else
22439 {
22440 const char *end_label;
22441
22442 if (flag_reorder_blocks_and_partition)
22443 {
22444 if (in_cold_section_p)
22445 end_label = crtl->subsections.cold_section_end_label;
22446 else
22447 end_label = crtl->subsections.hot_section_end_label;
22448 }
22449 else
22450 {
22451 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22452 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22453 current_function_funcdef_no);
22454 end_label = ggc_strdup (label);
22455 }
22456
22457 table = new_line_info_table ();
22458 table->end_label = end_label;
22459
22460 vec_safe_push (separate_line_info, table);
22461 }
22462
22463 if (DWARF2_ASM_LINE_DEBUG_INFO)
22464 table->is_stmt = (cur_line_info_table
22465 ? cur_line_info_table->is_stmt
22466 : DWARF_LINE_DEFAULT_IS_STMT_START);
22467 cur_line_info_table = table;
22468 }
22469
22470
22471 /* We need to reset the locations at the beginning of each
22472 function. We can't do this in the end_function hook, because the
22473 declarations that use the locations won't have been output when
22474 that hook is called. Also compute have_multiple_function_sections here. */
22475
22476 static void
22477 dwarf2out_begin_function (tree fun)
22478 {
22479 section *sec = function_section (fun);
22480
22481 if (sec != text_section)
22482 have_multiple_function_sections = true;
22483
22484 if (flag_reorder_blocks_and_partition && !cold_text_section)
22485 {
22486 gcc_assert (current_function_decl == fun);
22487 cold_text_section = unlikely_text_section ();
22488 switch_to_section (cold_text_section);
22489 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22490 switch_to_section (sec);
22491 }
22492
22493 dwarf2out_note_section_used ();
22494 call_site_count = 0;
22495 tail_call_site_count = 0;
22496
22497 set_cur_line_info_table (sec);
22498 }
22499
22500 /* Helper function of dwarf2out_end_function, called only after emitting
22501 the very first function into assembly. Check if some .debug_loc range
22502 might end with a .LVL* label that could be equal to .Ltext0.
22503 In that case we must force using absolute addresses in .debug_loc ranges,
22504 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22505 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22506 list terminator.
22507 Set have_multiple_function_sections to true in that case and
22508 terminate htab traversal. */
22509
22510 int
22511 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22512 {
22513 var_loc_list *entry = *slot;
22514 struct var_loc_node *node;
22515
22516 node = entry->first;
22517 if (node && node->next && node->next->label)
22518 {
22519 unsigned int i;
22520 const char *label = node->next->label;
22521 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22522
22523 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22524 {
22525 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22526 if (strcmp (label, loclabel) == 0)
22527 {
22528 have_multiple_function_sections = true;
22529 return 0;
22530 }
22531 }
22532 }
22533 return 1;
22534 }
22535
22536 /* Hook called after emitting a function into assembly.
22537 This does something only for the very first function emitted. */
22538
22539 static void
22540 dwarf2out_end_function (unsigned int)
22541 {
22542 if (in_first_function_p
22543 && !have_multiple_function_sections
22544 && first_loclabel_num_not_at_text_label
22545 && decl_loc_table)
22546 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22547 in_first_function_p = false;
22548 maybe_at_text_label_p = false;
22549 }
22550
22551 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22552 front-ends register a translation unit even before dwarf2out_init is
22553 called. */
22554 static tree main_translation_unit = NULL_TREE;
22555
22556 /* Hook called by front-ends after they built their main translation unit.
22557 Associate comp_unit_die to UNIT. */
22558
22559 static void
22560 dwarf2out_register_main_translation_unit (tree unit)
22561 {
22562 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22563 && main_translation_unit == NULL_TREE);
22564 main_translation_unit = unit;
22565 /* If dwarf2out_init has not been called yet, it will perform the association
22566 itself looking at main_translation_unit. */
22567 if (decl_die_table != NULL)
22568 equate_decl_number_to_die (unit, comp_unit_die ());
22569 }
22570
22571 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22572
22573 static void
22574 push_dw_line_info_entry (dw_line_info_table *table,
22575 enum dw_line_info_opcode opcode, unsigned int val)
22576 {
22577 dw_line_info_entry e;
22578 e.opcode = opcode;
22579 e.val = val;
22580 vec_safe_push (table->entries, e);
22581 }
22582
22583 /* Output a label to mark the beginning of a source code line entry
22584 and record information relating to this source line, in
22585 'line_info_table' for later output of the .debug_line section. */
22586 /* ??? The discriminator parameter ought to be unsigned. */
22587
22588 static void
22589 dwarf2out_source_line (unsigned int line, const char *filename,
22590 int discriminator, bool is_stmt)
22591 {
22592 unsigned int file_num;
22593 dw_line_info_table *table;
22594
22595 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22596 return;
22597
22598 /* The discriminator column was added in dwarf4. Simplify the below
22599 by simply removing it if we're not supposed to output it. */
22600 if (dwarf_version < 4 && dwarf_strict)
22601 discriminator = 0;
22602
22603 table = cur_line_info_table;
22604 file_num = maybe_emit_file (lookup_filename (filename));
22605
22606 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22607 the debugger has used the second (possibly duplicate) line number
22608 at the beginning of the function to mark the end of the prologue.
22609 We could eliminate any other duplicates within the function. For
22610 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22611 that second line number entry. */
22612 /* Recall that this end-of-prologue indication is *not* the same thing
22613 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22614 to which the hook corresponds, follows the last insn that was
22615 emitted by gen_prologue. What we need is to precede the first insn
22616 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22617 insn that corresponds to something the user wrote. These may be
22618 very different locations once scheduling is enabled. */
22619
22620 if (0 && file_num == table->file_num
22621 && line == table->line_num
22622 && discriminator == table->discrim_num
22623 && is_stmt == table->is_stmt)
22624 return;
22625
22626 switch_to_section (current_function_section ());
22627
22628 /* If requested, emit something human-readable. */
22629 if (flag_debug_asm)
22630 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22631
22632 if (DWARF2_ASM_LINE_DEBUG_INFO)
22633 {
22634 /* Emit the .loc directive understood by GNU as. */
22635 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22636 file_num, line, is_stmt, discriminator */
22637 fputs ("\t.loc ", asm_out_file);
22638 fprint_ul (asm_out_file, file_num);
22639 putc (' ', asm_out_file);
22640 fprint_ul (asm_out_file, line);
22641 putc (' ', asm_out_file);
22642 putc ('0', asm_out_file);
22643
22644 if (is_stmt != table->is_stmt)
22645 {
22646 fputs (" is_stmt ", asm_out_file);
22647 putc (is_stmt ? '1' : '0', asm_out_file);
22648 }
22649 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22650 {
22651 gcc_assert (discriminator > 0);
22652 fputs (" discriminator ", asm_out_file);
22653 fprint_ul (asm_out_file, (unsigned long) discriminator);
22654 }
22655 putc ('\n', asm_out_file);
22656 }
22657 else
22658 {
22659 unsigned int label_num = ++line_info_label_num;
22660
22661 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22662
22663 push_dw_line_info_entry (table, LI_set_address, label_num);
22664 if (file_num != table->file_num)
22665 push_dw_line_info_entry (table, LI_set_file, file_num);
22666 if (discriminator != table->discrim_num)
22667 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22668 if (is_stmt != table->is_stmt)
22669 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22670 push_dw_line_info_entry (table, LI_set_line, line);
22671 }
22672
22673 table->file_num = file_num;
22674 table->line_num = line;
22675 table->discrim_num = discriminator;
22676 table->is_stmt = is_stmt;
22677 table->in_use = true;
22678 }
22679
22680 /* Record the beginning of a new source file. */
22681
22682 static void
22683 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22684 {
22685 if (flag_eliminate_dwarf2_dups)
22686 {
22687 /* Record the beginning of the file for break_out_includes. */
22688 dw_die_ref bincl_die;
22689
22690 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22691 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22692 }
22693
22694 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22695 {
22696 macinfo_entry e;
22697 e.code = DW_MACINFO_start_file;
22698 e.lineno = lineno;
22699 e.info = ggc_strdup (filename);
22700 vec_safe_push (macinfo_table, e);
22701 }
22702 }
22703
22704 /* Record the end of a source file. */
22705
22706 static void
22707 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22708 {
22709 if (flag_eliminate_dwarf2_dups)
22710 /* Record the end of the file for break_out_includes. */
22711 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22712
22713 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22714 {
22715 macinfo_entry e;
22716 e.code = DW_MACINFO_end_file;
22717 e.lineno = lineno;
22718 e.info = NULL;
22719 vec_safe_push (macinfo_table, e);
22720 }
22721 }
22722
22723 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22724 the tail part of the directive line, i.e. the part which is past the
22725 initial whitespace, #, whitespace, directive-name, whitespace part. */
22726
22727 static void
22728 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22729 const char *buffer ATTRIBUTE_UNUSED)
22730 {
22731 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22732 {
22733 macinfo_entry e;
22734 /* Insert a dummy first entry to be able to optimize the whole
22735 predefined macro block using DW_MACRO_GNU_transparent_include. */
22736 if (macinfo_table->is_empty () && lineno <= 1)
22737 {
22738 e.code = 0;
22739 e.lineno = 0;
22740 e.info = NULL;
22741 vec_safe_push (macinfo_table, e);
22742 }
22743 e.code = DW_MACINFO_define;
22744 e.lineno = lineno;
22745 e.info = ggc_strdup (buffer);
22746 vec_safe_push (macinfo_table, e);
22747 }
22748 }
22749
22750 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22751 the tail part of the directive line, i.e. the part which is past the
22752 initial whitespace, #, whitespace, directive-name, whitespace part. */
22753
22754 static void
22755 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22756 const char *buffer ATTRIBUTE_UNUSED)
22757 {
22758 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22759 {
22760 macinfo_entry e;
22761 /* Insert a dummy first entry to be able to optimize the whole
22762 predefined macro block using DW_MACRO_GNU_transparent_include. */
22763 if (macinfo_table->is_empty () && lineno <= 1)
22764 {
22765 e.code = 0;
22766 e.lineno = 0;
22767 e.info = NULL;
22768 vec_safe_push (macinfo_table, e);
22769 }
22770 e.code = DW_MACINFO_undef;
22771 e.lineno = lineno;
22772 e.info = ggc_strdup (buffer);
22773 vec_safe_push (macinfo_table, e);
22774 }
22775 }
22776
22777 /* Helpers to manipulate hash table of CUs. */
22778
22779 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22780 {
22781 static inline hashval_t hash (const macinfo_entry *);
22782 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22783 };
22784
22785 inline hashval_t
22786 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22787 {
22788 return htab_hash_string (entry->info);
22789 }
22790
22791 inline bool
22792 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22793 const macinfo_entry *entry2)
22794 {
22795 return !strcmp (entry1->info, entry2->info);
22796 }
22797
22798 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22799
22800 /* Output a single .debug_macinfo entry. */
22801
22802 static void
22803 output_macinfo_op (macinfo_entry *ref)
22804 {
22805 int file_num;
22806 size_t len;
22807 struct indirect_string_node *node;
22808 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22809 struct dwarf_file_data *fd;
22810
22811 switch (ref->code)
22812 {
22813 case DW_MACINFO_start_file:
22814 fd = lookup_filename (ref->info);
22815 file_num = maybe_emit_file (fd);
22816 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22817 dw2_asm_output_data_uleb128 (ref->lineno,
22818 "Included from line number %lu",
22819 (unsigned long) ref->lineno);
22820 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22821 break;
22822 case DW_MACINFO_end_file:
22823 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22824 break;
22825 case DW_MACINFO_define:
22826 case DW_MACINFO_undef:
22827 len = strlen (ref->info) + 1;
22828 if (!dwarf_strict
22829 && len > DWARF_OFFSET_SIZE
22830 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22831 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22832 {
22833 ref->code = ref->code == DW_MACINFO_define
22834 ? DW_MACRO_GNU_define_indirect
22835 : DW_MACRO_GNU_undef_indirect;
22836 output_macinfo_op (ref);
22837 return;
22838 }
22839 dw2_asm_output_data (1, ref->code,
22840 ref->code == DW_MACINFO_define
22841 ? "Define macro" : "Undefine macro");
22842 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22843 (unsigned long) ref->lineno);
22844 dw2_asm_output_nstring (ref->info, -1, "The macro");
22845 break;
22846 case DW_MACRO_GNU_define_indirect:
22847 case DW_MACRO_GNU_undef_indirect:
22848 node = find_AT_string (ref->info);
22849 gcc_assert (node
22850 && ((node->form == DW_FORM_strp)
22851 || (node->form == DW_FORM_GNU_str_index)));
22852 dw2_asm_output_data (1, ref->code,
22853 ref->code == DW_MACRO_GNU_define_indirect
22854 ? "Define macro indirect"
22855 : "Undefine macro indirect");
22856 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22857 (unsigned long) ref->lineno);
22858 if (node->form == DW_FORM_strp)
22859 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22860 debug_str_section, "The macro: \"%s\"",
22861 ref->info);
22862 else
22863 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22864 ref->info);
22865 break;
22866 case DW_MACRO_GNU_transparent_include:
22867 dw2_asm_output_data (1, ref->code, "Transparent include");
22868 ASM_GENERATE_INTERNAL_LABEL (label,
22869 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22870 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22871 break;
22872 default:
22873 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22874 ASM_COMMENT_START, (unsigned long) ref->code);
22875 break;
22876 }
22877 }
22878
22879 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22880 other compilation unit .debug_macinfo sections. IDX is the first
22881 index of a define/undef, return the number of ops that should be
22882 emitted in a comdat .debug_macinfo section and emit
22883 a DW_MACRO_GNU_transparent_include entry referencing it.
22884 If the define/undef entry should be emitted normally, return 0. */
22885
22886 static unsigned
22887 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22888 macinfo_hash_type **macinfo_htab)
22889 {
22890 macinfo_entry *first, *second, *cur, *inc;
22891 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22892 unsigned char checksum[16];
22893 struct md5_ctx ctx;
22894 char *grp_name, *tail;
22895 const char *base;
22896 unsigned int i, count, encoded_filename_len, linebuf_len;
22897 macinfo_entry **slot;
22898
22899 first = &(*macinfo_table)[idx];
22900 second = &(*macinfo_table)[idx + 1];
22901
22902 /* Optimize only if there are at least two consecutive define/undef ops,
22903 and either all of them are before first DW_MACINFO_start_file
22904 with lineno {0,1} (i.e. predefined macro block), or all of them are
22905 in some included header file. */
22906 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22907 return 0;
22908 if (vec_safe_is_empty (files))
22909 {
22910 if (first->lineno > 1 || second->lineno > 1)
22911 return 0;
22912 }
22913 else if (first->lineno == 0)
22914 return 0;
22915
22916 /* Find the last define/undef entry that can be grouped together
22917 with first and at the same time compute md5 checksum of their
22918 codes, linenumbers and strings. */
22919 md5_init_ctx (&ctx);
22920 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22921 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22922 break;
22923 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22924 break;
22925 else
22926 {
22927 unsigned char code = cur->code;
22928 md5_process_bytes (&code, 1, &ctx);
22929 checksum_uleb128 (cur->lineno, &ctx);
22930 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22931 }
22932 md5_finish_ctx (&ctx, checksum);
22933 count = i - idx;
22934
22935 /* From the containing include filename (if any) pick up just
22936 usable characters from its basename. */
22937 if (vec_safe_is_empty (files))
22938 base = "";
22939 else
22940 base = lbasename (files->last ().info);
22941 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22942 if (ISIDNUM (base[i]) || base[i] == '.')
22943 encoded_filename_len++;
22944 /* Count . at the end. */
22945 if (encoded_filename_len)
22946 encoded_filename_len++;
22947
22948 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22949 linebuf_len = strlen (linebuf);
22950
22951 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22952 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22953 + 16 * 2 + 1);
22954 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22955 tail = grp_name + 4;
22956 if (encoded_filename_len)
22957 {
22958 for (i = 0; base[i]; i++)
22959 if (ISIDNUM (base[i]) || base[i] == '.')
22960 *tail++ = base[i];
22961 *tail++ = '.';
22962 }
22963 memcpy (tail, linebuf, linebuf_len);
22964 tail += linebuf_len;
22965 *tail++ = '.';
22966 for (i = 0; i < 16; i++)
22967 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22968
22969 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22970 in the empty vector entry before the first define/undef. */
22971 inc = &(*macinfo_table)[idx - 1];
22972 inc->code = DW_MACRO_GNU_transparent_include;
22973 inc->lineno = 0;
22974 inc->info = ggc_strdup (grp_name);
22975 if (!*macinfo_htab)
22976 *macinfo_htab = new macinfo_hash_type (10);
22977 /* Avoid emitting duplicates. */
22978 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22979 if (*slot != NULL)
22980 {
22981 inc->code = 0;
22982 inc->info = NULL;
22983 /* If such an entry has been used before, just emit
22984 a DW_MACRO_GNU_transparent_include op. */
22985 inc = *slot;
22986 output_macinfo_op (inc);
22987 /* And clear all macinfo_entry in the range to avoid emitting them
22988 in the second pass. */
22989 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22990 {
22991 cur->code = 0;
22992 cur->info = NULL;
22993 }
22994 }
22995 else
22996 {
22997 *slot = inc;
22998 inc->lineno = (*macinfo_htab)->elements ();
22999 output_macinfo_op (inc);
23000 }
23001 return count;
23002 }
23003
23004 /* Save any strings needed by the macinfo table in the debug str
23005 table. All strings must be collected into the table by the time
23006 index_string is called. */
23007
23008 static void
23009 save_macinfo_strings (void)
23010 {
23011 unsigned len;
23012 unsigned i;
23013 macinfo_entry *ref;
23014
23015 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23016 {
23017 switch (ref->code)
23018 {
23019 /* Match the logic in output_macinfo_op to decide on
23020 indirect strings. */
23021 case DW_MACINFO_define:
23022 case DW_MACINFO_undef:
23023 len = strlen (ref->info) + 1;
23024 if (!dwarf_strict
23025 && len > DWARF_OFFSET_SIZE
23026 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23027 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23028 set_indirect_string (find_AT_string (ref->info));
23029 break;
23030 case DW_MACRO_GNU_define_indirect:
23031 case DW_MACRO_GNU_undef_indirect:
23032 set_indirect_string (find_AT_string (ref->info));
23033 break;
23034 default:
23035 break;
23036 }
23037 }
23038 }
23039
23040 /* Output macinfo section(s). */
23041
23042 static void
23043 output_macinfo (void)
23044 {
23045 unsigned i;
23046 unsigned long length = vec_safe_length (macinfo_table);
23047 macinfo_entry *ref;
23048 vec<macinfo_entry, va_gc> *files = NULL;
23049 macinfo_hash_type *macinfo_htab = NULL;
23050
23051 if (! length)
23052 return;
23053
23054 /* output_macinfo* uses these interchangeably. */
23055 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23056 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23057 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23058 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23059
23060 /* For .debug_macro emit the section header. */
23061 if (!dwarf_strict)
23062 {
23063 dw2_asm_output_data (2, 4, "DWARF macro version number");
23064 if (DWARF_OFFSET_SIZE == 8)
23065 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23066 else
23067 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23068 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23069 (!dwarf_split_debug_info ? debug_line_section_label
23070 : debug_skeleton_line_section_label),
23071 debug_line_section, NULL);
23072 }
23073
23074 /* In the first loop, it emits the primary .debug_macinfo section
23075 and after each emitted op the macinfo_entry is cleared.
23076 If a longer range of define/undef ops can be optimized using
23077 DW_MACRO_GNU_transparent_include, the
23078 DW_MACRO_GNU_transparent_include op is emitted and kept in
23079 the vector before the first define/undef in the range and the
23080 whole range of define/undef ops is not emitted and kept. */
23081 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23082 {
23083 switch (ref->code)
23084 {
23085 case DW_MACINFO_start_file:
23086 vec_safe_push (files, *ref);
23087 break;
23088 case DW_MACINFO_end_file:
23089 if (!vec_safe_is_empty (files))
23090 files->pop ();
23091 break;
23092 case DW_MACINFO_define:
23093 case DW_MACINFO_undef:
23094 if (!dwarf_strict
23095 && HAVE_COMDAT_GROUP
23096 && vec_safe_length (files) != 1
23097 && i > 0
23098 && i + 1 < length
23099 && (*macinfo_table)[i - 1].code == 0)
23100 {
23101 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23102 if (count)
23103 {
23104 i += count - 1;
23105 continue;
23106 }
23107 }
23108 break;
23109 case 0:
23110 /* A dummy entry may be inserted at the beginning to be able
23111 to optimize the whole block of predefined macros. */
23112 if (i == 0)
23113 continue;
23114 default:
23115 break;
23116 }
23117 output_macinfo_op (ref);
23118 ref->info = NULL;
23119 ref->code = 0;
23120 }
23121
23122 if (!macinfo_htab)
23123 return;
23124
23125 delete macinfo_htab;
23126 macinfo_htab = NULL;
23127
23128 /* If any DW_MACRO_GNU_transparent_include were used, on those
23129 DW_MACRO_GNU_transparent_include entries terminate the
23130 current chain and switch to a new comdat .debug_macinfo
23131 section and emit the define/undef entries within it. */
23132 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23133 switch (ref->code)
23134 {
23135 case 0:
23136 continue;
23137 case DW_MACRO_GNU_transparent_include:
23138 {
23139 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23140 tree comdat_key = get_identifier (ref->info);
23141 /* Terminate the previous .debug_macinfo section. */
23142 dw2_asm_output_data (1, 0, "End compilation unit");
23143 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23144 SECTION_DEBUG
23145 | SECTION_LINKONCE,
23146 comdat_key);
23147 ASM_GENERATE_INTERNAL_LABEL (label,
23148 DEBUG_MACRO_SECTION_LABEL,
23149 ref->lineno);
23150 ASM_OUTPUT_LABEL (asm_out_file, label);
23151 ref->code = 0;
23152 ref->info = NULL;
23153 dw2_asm_output_data (2, 4, "DWARF macro version number");
23154 if (DWARF_OFFSET_SIZE == 8)
23155 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23156 else
23157 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23158 }
23159 break;
23160 case DW_MACINFO_define:
23161 case DW_MACINFO_undef:
23162 output_macinfo_op (ref);
23163 ref->code = 0;
23164 ref->info = NULL;
23165 break;
23166 default:
23167 gcc_unreachable ();
23168 }
23169 }
23170
23171 /* Set up for Dwarf output at the start of compilation. */
23172
23173 static void
23174 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23175 {
23176 /* This option is currently broken, see (PR53118 and PR46102). */
23177 if (flag_eliminate_dwarf2_dups
23178 && strstr (lang_hooks.name, "C++"))
23179 {
23180 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23181 flag_eliminate_dwarf2_dups = 0;
23182 }
23183
23184 /* Allocate the file_table. */
23185 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23186
23187 /* Allocate the decl_die_table. */
23188 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23189
23190 /* Allocate the decl_loc_table. */
23191 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23192
23193 /* Allocate the cached_dw_loc_list_table. */
23194 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23195
23196 /* Allocate the initial hunk of the decl_scope_table. */
23197 vec_alloc (decl_scope_table, 256);
23198
23199 /* Allocate the initial hunk of the abbrev_die_table. */
23200 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23201 (ABBREV_DIE_TABLE_INCREMENT);
23202 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23203 /* Zero-th entry is allocated, but unused. */
23204 abbrev_die_table_in_use = 1;
23205
23206 /* Allocate the pubtypes and pubnames vectors. */
23207 vec_alloc (pubname_table, 32);
23208 vec_alloc (pubtype_table, 32);
23209
23210 vec_alloc (incomplete_types, 64);
23211
23212 vec_alloc (used_rtx_array, 32);
23213
23214 if (!dwarf_split_debug_info)
23215 {
23216 debug_info_section = get_section (DEBUG_INFO_SECTION,
23217 SECTION_DEBUG, NULL);
23218 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23219 SECTION_DEBUG, NULL);
23220 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23221 SECTION_DEBUG, NULL);
23222 }
23223 else
23224 {
23225 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23226 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23227 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23228 SECTION_DEBUG | SECTION_EXCLUDE,
23229 NULL);
23230 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23231 SECTION_DEBUG, NULL);
23232 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23233 SECTION_DEBUG, NULL);
23234 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23235 SECTION_DEBUG, NULL);
23236 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23237 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23238
23239 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23240 the main .o, but the skeleton_line goes into the split off dwo. */
23241 debug_skeleton_line_section
23242 = get_section (DEBUG_DWO_LINE_SECTION,
23243 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23244 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23245 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23246 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23247 SECTION_DEBUG | SECTION_EXCLUDE,
23248 NULL);
23249 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23250 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23251 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23252 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23253 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23254 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23255 }
23256 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23257 SECTION_DEBUG, NULL);
23258 debug_macinfo_section = get_section (dwarf_strict
23259 ? DEBUG_MACINFO_SECTION
23260 : DEBUG_MACRO_SECTION,
23261 DEBUG_MACRO_SECTION_FLAGS, NULL);
23262 debug_line_section = get_section (DEBUG_LINE_SECTION,
23263 SECTION_DEBUG, NULL);
23264 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23265 SECTION_DEBUG, NULL);
23266 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23267 SECTION_DEBUG, NULL);
23268 debug_str_section = get_section (DEBUG_STR_SECTION,
23269 DEBUG_STR_SECTION_FLAGS, NULL);
23270 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23271 SECTION_DEBUG, NULL);
23272 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23273 SECTION_DEBUG, NULL);
23274
23275 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23276 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23277 DEBUG_ABBREV_SECTION_LABEL, 0);
23278 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23279 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23280 COLD_TEXT_SECTION_LABEL, 0);
23281 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23282
23283 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23284 DEBUG_INFO_SECTION_LABEL, 0);
23285 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23286 DEBUG_LINE_SECTION_LABEL, 0);
23287 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23288 DEBUG_RANGES_SECTION_LABEL, 0);
23289 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23290 DEBUG_ADDR_SECTION_LABEL, 0);
23291 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23292 dwarf_strict
23293 ? DEBUG_MACINFO_SECTION_LABEL
23294 : DEBUG_MACRO_SECTION_LABEL, 0);
23295 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23296
23297 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23298 vec_alloc (macinfo_table, 64);
23299
23300 switch_to_section (text_section);
23301 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23302
23303 /* Make sure the line number table for .text always exists. */
23304 text_section_line_info = new_line_info_table ();
23305 text_section_line_info->end_label = text_end_label;
23306
23307 /* If front-ends already registered a main translation unit but we were not
23308 ready to perform the association, do this now. */
23309 if (main_translation_unit != NULL_TREE)
23310 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23311 }
23312
23313 /* Called before compile () starts outputtting functions, variables
23314 and toplevel asms into assembly. */
23315
23316 static void
23317 dwarf2out_assembly_start (void)
23318 {
23319 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23320 && dwarf2out_do_cfi_asm ()
23321 && (!(flag_unwind_tables || flag_exceptions)
23322 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23323 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23324 }
23325
23326 /* A helper function for dwarf2out_finish called through
23327 htab_traverse. Assign a string its index. All strings must be
23328 collected into the table by the time index_string is called,
23329 because the indexing code relies on htab_traverse to traverse nodes
23330 in the same order for each run. */
23331
23332 int
23333 index_string (indirect_string_node **h, unsigned int *index)
23334 {
23335 indirect_string_node *node = *h;
23336
23337 find_string_form (node);
23338 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23339 {
23340 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23341 node->index = *index;
23342 *index += 1;
23343 }
23344 return 1;
23345 }
23346
23347 /* A helper function for output_indirect_strings called through
23348 htab_traverse. Output the offset to a string and update the
23349 current offset. */
23350
23351 int
23352 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23353 {
23354 indirect_string_node *node = *h;
23355
23356 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23357 {
23358 /* Assert that this node has been assigned an index. */
23359 gcc_assert (node->index != NO_INDEX_ASSIGNED
23360 && node->index != NOT_INDEXED);
23361 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23362 "indexed string 0x%x: %s", node->index, node->str);
23363 *offset += strlen (node->str) + 1;
23364 }
23365 return 1;
23366 }
23367
23368 /* A helper function for dwarf2out_finish called through
23369 htab_traverse. Output the indexed string. */
23370
23371 int
23372 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23373 {
23374 struct indirect_string_node *node = *h;
23375
23376 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23377 {
23378 /* Assert that the strings are output in the same order as their
23379 indexes were assigned. */
23380 gcc_assert (*cur_idx == node->index);
23381 assemble_string (node->str, strlen (node->str) + 1);
23382 *cur_idx += 1;
23383 }
23384 return 1;
23385 }
23386
23387 /* A helper function for dwarf2out_finish called through
23388 htab_traverse. Emit one queued .debug_str string. */
23389
23390 int
23391 output_indirect_string (indirect_string_node **h, void *)
23392 {
23393 struct indirect_string_node *node = *h;
23394
23395 node->form = find_string_form (node);
23396 if (node->form == DW_FORM_strp && node->refcount > 0)
23397 {
23398 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23399 assemble_string (node->str, strlen (node->str) + 1);
23400 }
23401
23402 return 1;
23403 }
23404
23405 /* Output the indexed string table. */
23406
23407 static void
23408 output_indirect_strings (void)
23409 {
23410 switch_to_section (debug_str_section);
23411 if (!dwarf_split_debug_info)
23412 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23413 else
23414 {
23415 unsigned int offset = 0;
23416 unsigned int cur_idx = 0;
23417
23418 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23419
23420 switch_to_section (debug_str_offsets_section);
23421 debug_str_hash->traverse_noresize
23422 <unsigned int *, output_index_string_offset> (&offset);
23423 switch_to_section (debug_str_dwo_section);
23424 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23425 (&cur_idx);
23426 }
23427 }
23428
23429 /* Callback for htab_traverse to assign an index to an entry in the
23430 table, and to write that entry to the .debug_addr section. */
23431
23432 int
23433 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23434 {
23435 addr_table_entry *entry = *slot;
23436
23437 if (entry->refcount == 0)
23438 {
23439 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23440 || entry->index == NOT_INDEXED);
23441 return 1;
23442 }
23443
23444 gcc_assert (entry->index == *cur_index);
23445 (*cur_index)++;
23446
23447 switch (entry->kind)
23448 {
23449 case ate_kind_rtx:
23450 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23451 "0x%x", entry->index);
23452 break;
23453 case ate_kind_rtx_dtprel:
23454 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23455 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23456 DWARF2_ADDR_SIZE,
23457 entry->addr.rtl);
23458 fputc ('\n', asm_out_file);
23459 break;
23460 case ate_kind_label:
23461 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23462 "0x%x", entry->index);
23463 break;
23464 default:
23465 gcc_unreachable ();
23466 }
23467 return 1;
23468 }
23469
23470 /* Produce the .debug_addr section. */
23471
23472 static void
23473 output_addr_table (void)
23474 {
23475 unsigned int index = 0;
23476 if (addr_index_table == NULL || addr_index_table->size () == 0)
23477 return;
23478
23479 switch_to_section (debug_addr_section);
23480 addr_index_table
23481 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23482 }
23483
23484 #if ENABLE_ASSERT_CHECKING
23485 /* Verify that all marks are clear. */
23486
23487 static void
23488 verify_marks_clear (dw_die_ref die)
23489 {
23490 dw_die_ref c;
23491
23492 gcc_assert (! die->die_mark);
23493 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23494 }
23495 #endif /* ENABLE_ASSERT_CHECKING */
23496
23497 /* Clear the marks for a die and its children.
23498 Be cool if the mark isn't set. */
23499
23500 static void
23501 prune_unmark_dies (dw_die_ref die)
23502 {
23503 dw_die_ref c;
23504
23505 if (die->die_mark)
23506 die->die_mark = 0;
23507 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23508 }
23509
23510 /* Given DIE that we're marking as used, find any other dies
23511 it references as attributes and mark them as used. */
23512
23513 static void
23514 prune_unused_types_walk_attribs (dw_die_ref die)
23515 {
23516 dw_attr_ref a;
23517 unsigned ix;
23518
23519 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23520 {
23521 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23522 {
23523 /* A reference to another DIE.
23524 Make sure that it will get emitted.
23525 If it was broken out into a comdat group, don't follow it. */
23526 if (! AT_ref (a)->comdat_type_p
23527 || a->dw_attr == DW_AT_specification)
23528 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23529 }
23530 /* Set the string's refcount to 0 so that prune_unused_types_mark
23531 accounts properly for it. */
23532 if (AT_class (a) == dw_val_class_str)
23533 a->dw_attr_val.v.val_str->refcount = 0;
23534 }
23535 }
23536
23537 /* Mark the generic parameters and arguments children DIEs of DIE. */
23538
23539 static void
23540 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23541 {
23542 dw_die_ref c;
23543
23544 if (die == NULL || die->die_child == NULL)
23545 return;
23546 c = die->die_child;
23547 do
23548 {
23549 if (is_template_parameter (c))
23550 prune_unused_types_mark (c, 1);
23551 c = c->die_sib;
23552 } while (c && c != die->die_child);
23553 }
23554
23555 /* Mark DIE as being used. If DOKIDS is true, then walk down
23556 to DIE's children. */
23557
23558 static void
23559 prune_unused_types_mark (dw_die_ref die, int dokids)
23560 {
23561 dw_die_ref c;
23562
23563 if (die->die_mark == 0)
23564 {
23565 /* We haven't done this node yet. Mark it as used. */
23566 die->die_mark = 1;
23567 /* If this is the DIE of a generic type instantiation,
23568 mark the children DIEs that describe its generic parms and
23569 args. */
23570 prune_unused_types_mark_generic_parms_dies (die);
23571
23572 /* We also have to mark its parents as used.
23573 (But we don't want to mark our parent's kids due to this,
23574 unless it is a class.) */
23575 if (die->die_parent)
23576 prune_unused_types_mark (die->die_parent,
23577 class_scope_p (die->die_parent));
23578
23579 /* Mark any referenced nodes. */
23580 prune_unused_types_walk_attribs (die);
23581
23582 /* If this node is a specification,
23583 also mark the definition, if it exists. */
23584 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23585 prune_unused_types_mark (die->die_definition, 1);
23586 }
23587
23588 if (dokids && die->die_mark != 2)
23589 {
23590 /* We need to walk the children, but haven't done so yet.
23591 Remember that we've walked the kids. */
23592 die->die_mark = 2;
23593
23594 /* If this is an array type, we need to make sure our
23595 kids get marked, even if they're types. If we're
23596 breaking out types into comdat sections, do this
23597 for all type definitions. */
23598 if (die->die_tag == DW_TAG_array_type
23599 || (use_debug_types
23600 && is_type_die (die) && ! is_declaration_die (die)))
23601 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23602 else
23603 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23604 }
23605 }
23606
23607 /* For local classes, look if any static member functions were emitted
23608 and if so, mark them. */
23609
23610 static void
23611 prune_unused_types_walk_local_classes (dw_die_ref die)
23612 {
23613 dw_die_ref c;
23614
23615 if (die->die_mark == 2)
23616 return;
23617
23618 switch (die->die_tag)
23619 {
23620 case DW_TAG_structure_type:
23621 case DW_TAG_union_type:
23622 case DW_TAG_class_type:
23623 break;
23624
23625 case DW_TAG_subprogram:
23626 if (!get_AT_flag (die, DW_AT_declaration)
23627 || die->die_definition != NULL)
23628 prune_unused_types_mark (die, 1);
23629 return;
23630
23631 default:
23632 return;
23633 }
23634
23635 /* Mark children. */
23636 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23637 }
23638
23639 /* Walk the tree DIE and mark types that we actually use. */
23640
23641 static void
23642 prune_unused_types_walk (dw_die_ref die)
23643 {
23644 dw_die_ref c;
23645
23646 /* Don't do anything if this node is already marked and
23647 children have been marked as well. */
23648 if (die->die_mark == 2)
23649 return;
23650
23651 switch (die->die_tag)
23652 {
23653 case DW_TAG_structure_type:
23654 case DW_TAG_union_type:
23655 case DW_TAG_class_type:
23656 if (die->die_perennial_p)
23657 break;
23658
23659 for (c = die->die_parent; c; c = c->die_parent)
23660 if (c->die_tag == DW_TAG_subprogram)
23661 break;
23662
23663 /* Finding used static member functions inside of classes
23664 is needed just for local classes, because for other classes
23665 static member function DIEs with DW_AT_specification
23666 are emitted outside of the DW_TAG_*_type. If we ever change
23667 it, we'd need to call this even for non-local classes. */
23668 if (c)
23669 prune_unused_types_walk_local_classes (die);
23670
23671 /* It's a type node --- don't mark it. */
23672 return;
23673
23674 case DW_TAG_const_type:
23675 case DW_TAG_packed_type:
23676 case DW_TAG_pointer_type:
23677 case DW_TAG_reference_type:
23678 case DW_TAG_rvalue_reference_type:
23679 case DW_TAG_volatile_type:
23680 case DW_TAG_typedef:
23681 case DW_TAG_array_type:
23682 case DW_TAG_interface_type:
23683 case DW_TAG_friend:
23684 case DW_TAG_variant_part:
23685 case DW_TAG_enumeration_type:
23686 case DW_TAG_subroutine_type:
23687 case DW_TAG_string_type:
23688 case DW_TAG_set_type:
23689 case DW_TAG_subrange_type:
23690 case DW_TAG_ptr_to_member_type:
23691 case DW_TAG_file_type:
23692 if (die->die_perennial_p)
23693 break;
23694
23695 /* It's a type node --- don't mark it. */
23696 return;
23697
23698 default:
23699 /* Mark everything else. */
23700 break;
23701 }
23702
23703 if (die->die_mark == 0)
23704 {
23705 die->die_mark = 1;
23706
23707 /* Now, mark any dies referenced from here. */
23708 prune_unused_types_walk_attribs (die);
23709 }
23710
23711 die->die_mark = 2;
23712
23713 /* Mark children. */
23714 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23715 }
23716
23717 /* Increment the string counts on strings referred to from DIE's
23718 attributes. */
23719
23720 static void
23721 prune_unused_types_update_strings (dw_die_ref die)
23722 {
23723 dw_attr_ref a;
23724 unsigned ix;
23725
23726 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23727 if (AT_class (a) == dw_val_class_str)
23728 {
23729 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23730 s->refcount++;
23731 /* Avoid unnecessarily putting strings that are used less than
23732 twice in the hash table. */
23733 if (s->refcount
23734 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23735 {
23736 indirect_string_node **slot
23737 = debug_str_hash->find_slot_with_hash (s->str,
23738 htab_hash_string (s->str),
23739 INSERT);
23740 gcc_assert (*slot == NULL);
23741 *slot = s;
23742 }
23743 }
23744 }
23745
23746 /* Remove from the tree DIE any dies that aren't marked. */
23747
23748 static void
23749 prune_unused_types_prune (dw_die_ref die)
23750 {
23751 dw_die_ref c;
23752
23753 gcc_assert (die->die_mark);
23754 prune_unused_types_update_strings (die);
23755
23756 if (! die->die_child)
23757 return;
23758
23759 c = die->die_child;
23760 do {
23761 dw_die_ref prev = c;
23762 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23763 if (c == die->die_child)
23764 {
23765 /* No marked children between 'prev' and the end of the list. */
23766 if (prev == c)
23767 /* No marked children at all. */
23768 die->die_child = NULL;
23769 else
23770 {
23771 prev->die_sib = c->die_sib;
23772 die->die_child = prev;
23773 }
23774 return;
23775 }
23776
23777 if (c != prev->die_sib)
23778 prev->die_sib = c;
23779 prune_unused_types_prune (c);
23780 } while (c != die->die_child);
23781 }
23782
23783 /* Remove dies representing declarations that we never use. */
23784
23785 static void
23786 prune_unused_types (void)
23787 {
23788 unsigned int i;
23789 limbo_die_node *node;
23790 comdat_type_node *ctnode;
23791 pubname_ref pub;
23792 dw_die_ref base_type;
23793
23794 #if ENABLE_ASSERT_CHECKING
23795 /* All the marks should already be clear. */
23796 verify_marks_clear (comp_unit_die ());
23797 for (node = limbo_die_list; node; node = node->next)
23798 verify_marks_clear (node->die);
23799 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23800 verify_marks_clear (ctnode->root_die);
23801 #endif /* ENABLE_ASSERT_CHECKING */
23802
23803 /* Mark types that are used in global variables. */
23804 premark_types_used_by_global_vars ();
23805
23806 /* Set the mark on nodes that are actually used. */
23807 prune_unused_types_walk (comp_unit_die ());
23808 for (node = limbo_die_list; node; node = node->next)
23809 prune_unused_types_walk (node->die);
23810 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23811 {
23812 prune_unused_types_walk (ctnode->root_die);
23813 prune_unused_types_mark (ctnode->type_die, 1);
23814 }
23815
23816 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23817 are unusual in that they are pubnames that are the children of pubtypes.
23818 They should only be marked via their parent DW_TAG_enumeration_type die,
23819 not as roots in themselves. */
23820 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23821 if (pub->die->die_tag != DW_TAG_enumerator)
23822 prune_unused_types_mark (pub->die, 1);
23823 for (i = 0; base_types.iterate (i, &base_type); i++)
23824 prune_unused_types_mark (base_type, 1);
23825
23826 if (debug_str_hash)
23827 debug_str_hash->empty ();
23828 if (skeleton_debug_str_hash)
23829 skeleton_debug_str_hash->empty ();
23830 prune_unused_types_prune (comp_unit_die ());
23831 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23832 {
23833 node = *pnode;
23834 if (!node->die->die_mark)
23835 *pnode = node->next;
23836 else
23837 {
23838 prune_unused_types_prune (node->die);
23839 pnode = &node->next;
23840 }
23841 }
23842 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23843 prune_unused_types_prune (ctnode->root_die);
23844
23845 /* Leave the marks clear. */
23846 prune_unmark_dies (comp_unit_die ());
23847 for (node = limbo_die_list; node; node = node->next)
23848 prune_unmark_dies (node->die);
23849 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23850 prune_unmark_dies (ctnode->root_die);
23851 }
23852
23853 /* Set the parameter to true if there are any relative pathnames in
23854 the file table. */
23855 int
23856 file_table_relative_p (dwarf_file_data **slot, bool *p)
23857 {
23858 struct dwarf_file_data *d = *slot;
23859 if (!IS_ABSOLUTE_PATH (d->filename))
23860 {
23861 *p = true;
23862 return 0;
23863 }
23864 return 1;
23865 }
23866
23867 /* Helpers to manipulate hash table of comdat type units. */
23868
23869 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23870 {
23871 static inline hashval_t hash (const comdat_type_node *);
23872 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23873 };
23874
23875 inline hashval_t
23876 comdat_type_hasher::hash (const comdat_type_node *type_node)
23877 {
23878 hashval_t h;
23879 memcpy (&h, type_node->signature, sizeof (h));
23880 return h;
23881 }
23882
23883 inline bool
23884 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23885 const comdat_type_node *type_node_2)
23886 {
23887 return (! memcmp (type_node_1->signature, type_node_2->signature,
23888 DWARF_TYPE_SIGNATURE_SIZE));
23889 }
23890
23891 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23892 to the location it would have been added, should we know its
23893 DECL_ASSEMBLER_NAME when we added other attributes. This will
23894 probably improve compactness of debug info, removing equivalent
23895 abbrevs, and hide any differences caused by deferring the
23896 computation of the assembler name, triggered by e.g. PCH. */
23897
23898 static inline void
23899 move_linkage_attr (dw_die_ref die)
23900 {
23901 unsigned ix = vec_safe_length (die->die_attr);
23902 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23903
23904 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23905 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23906
23907 while (--ix > 0)
23908 {
23909 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23910
23911 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23912 break;
23913 }
23914
23915 if (ix != vec_safe_length (die->die_attr) - 1)
23916 {
23917 die->die_attr->pop ();
23918 die->die_attr->quick_insert (ix, linkage);
23919 }
23920 }
23921
23922 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23923 referenced from typed stack ops and count how often they are used. */
23924
23925 static void
23926 mark_base_types (dw_loc_descr_ref loc)
23927 {
23928 dw_die_ref base_type = NULL;
23929
23930 for (; loc; loc = loc->dw_loc_next)
23931 {
23932 switch (loc->dw_loc_opc)
23933 {
23934 case DW_OP_GNU_regval_type:
23935 case DW_OP_GNU_deref_type:
23936 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23937 break;
23938 case DW_OP_GNU_convert:
23939 case DW_OP_GNU_reinterpret:
23940 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23941 continue;
23942 /* FALLTHRU */
23943 case DW_OP_GNU_const_type:
23944 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23945 break;
23946 case DW_OP_GNU_entry_value:
23947 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23948 continue;
23949 default:
23950 continue;
23951 }
23952 gcc_assert (base_type->die_parent == comp_unit_die ());
23953 if (base_type->die_mark)
23954 base_type->die_mark++;
23955 else
23956 {
23957 base_types.safe_push (base_type);
23958 base_type->die_mark = 1;
23959 }
23960 }
23961 }
23962
23963 /* Comparison function for sorting marked base types. */
23964
23965 static int
23966 base_type_cmp (const void *x, const void *y)
23967 {
23968 dw_die_ref dx = *(const dw_die_ref *) x;
23969 dw_die_ref dy = *(const dw_die_ref *) y;
23970 unsigned int byte_size1, byte_size2;
23971 unsigned int encoding1, encoding2;
23972 if (dx->die_mark > dy->die_mark)
23973 return -1;
23974 if (dx->die_mark < dy->die_mark)
23975 return 1;
23976 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23977 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23978 if (byte_size1 < byte_size2)
23979 return 1;
23980 if (byte_size1 > byte_size2)
23981 return -1;
23982 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23983 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23984 if (encoding1 < encoding2)
23985 return 1;
23986 if (encoding1 > encoding2)
23987 return -1;
23988 return 0;
23989 }
23990
23991 /* Move base types marked by mark_base_types as early as possible
23992 in the CU, sorted by decreasing usage count both to make the
23993 uleb128 references as small as possible and to make sure they
23994 will have die_offset already computed by calc_die_sizes when
23995 sizes of typed stack loc ops is computed. */
23996
23997 static void
23998 move_marked_base_types (void)
23999 {
24000 unsigned int i;
24001 dw_die_ref base_type, die, c;
24002
24003 if (base_types.is_empty ())
24004 return;
24005
24006 /* Sort by decreasing usage count, they will be added again in that
24007 order later on. */
24008 base_types.qsort (base_type_cmp);
24009 die = comp_unit_die ();
24010 c = die->die_child;
24011 do
24012 {
24013 dw_die_ref prev = c;
24014 c = c->die_sib;
24015 while (c->die_mark)
24016 {
24017 remove_child_with_prev (c, prev);
24018 /* As base types got marked, there must be at least
24019 one node other than DW_TAG_base_type. */
24020 gcc_assert (c != c->die_sib);
24021 c = c->die_sib;
24022 }
24023 }
24024 while (c != die->die_child);
24025 gcc_assert (die->die_child);
24026 c = die->die_child;
24027 for (i = 0; base_types.iterate (i, &base_type); i++)
24028 {
24029 base_type->die_mark = 0;
24030 base_type->die_sib = c->die_sib;
24031 c->die_sib = base_type;
24032 c = base_type;
24033 }
24034 }
24035
24036 /* Helper function for resolve_addr, attempt to resolve
24037 one CONST_STRING, return true if successful. Similarly verify that
24038 SYMBOL_REFs refer to variables emitted in the current CU. */
24039
24040 static bool
24041 resolve_one_addr (rtx *addr)
24042 {
24043 rtx rtl = *addr;
24044
24045 if (GET_CODE (rtl) == CONST_STRING)
24046 {
24047 size_t len = strlen (XSTR (rtl, 0)) + 1;
24048 tree t = build_string (len, XSTR (rtl, 0));
24049 tree tlen = size_int (len - 1);
24050 TREE_TYPE (t)
24051 = build_array_type (char_type_node, build_index_type (tlen));
24052 rtl = lookup_constant_def (t);
24053 if (!rtl || !MEM_P (rtl))
24054 return false;
24055 rtl = XEXP (rtl, 0);
24056 if (GET_CODE (rtl) == SYMBOL_REF
24057 && SYMBOL_REF_DECL (rtl)
24058 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24059 return false;
24060 vec_safe_push (used_rtx_array, rtl);
24061 *addr = rtl;
24062 return true;
24063 }
24064
24065 if (GET_CODE (rtl) == SYMBOL_REF
24066 && SYMBOL_REF_DECL (rtl))
24067 {
24068 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24069 {
24070 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24071 return false;
24072 }
24073 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24074 return false;
24075 }
24076
24077 if (GET_CODE (rtl) == CONST)
24078 {
24079 subrtx_ptr_iterator::array_type array;
24080 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24081 if (!resolve_one_addr (*iter))
24082 return false;
24083 }
24084
24085 return true;
24086 }
24087
24088 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24089 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24090 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24091
24092 static rtx
24093 string_cst_pool_decl (tree t)
24094 {
24095 rtx rtl = output_constant_def (t, 1);
24096 unsigned char *array;
24097 dw_loc_descr_ref l;
24098 tree decl;
24099 size_t len;
24100 dw_die_ref ref;
24101
24102 if (!rtl || !MEM_P (rtl))
24103 return NULL_RTX;
24104 rtl = XEXP (rtl, 0);
24105 if (GET_CODE (rtl) != SYMBOL_REF
24106 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24107 return NULL_RTX;
24108
24109 decl = SYMBOL_REF_DECL (rtl);
24110 if (!lookup_decl_die (decl))
24111 {
24112 len = TREE_STRING_LENGTH (t);
24113 vec_safe_push (used_rtx_array, rtl);
24114 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24115 array = ggc_vec_alloc<unsigned char> (len);
24116 memcpy (array, TREE_STRING_POINTER (t), len);
24117 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24118 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24119 l->dw_loc_oprnd2.v.val_vec.length = len;
24120 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24121 l->dw_loc_oprnd2.v.val_vec.array = array;
24122 add_AT_loc (ref, DW_AT_location, l);
24123 equate_decl_number_to_die (decl, ref);
24124 }
24125 return rtl;
24126 }
24127
24128 /* Helper function of resolve_addr_in_expr. LOC is
24129 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24130 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24131 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24132 with DW_OP_GNU_implicit_pointer if possible
24133 and return true, if unsuccessful, return false. */
24134
24135 static bool
24136 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24137 {
24138 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24139 HOST_WIDE_INT offset = 0;
24140 dw_die_ref ref = NULL;
24141 tree decl;
24142
24143 if (GET_CODE (rtl) == CONST
24144 && GET_CODE (XEXP (rtl, 0)) == PLUS
24145 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24146 {
24147 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24148 rtl = XEXP (XEXP (rtl, 0), 0);
24149 }
24150 if (GET_CODE (rtl) == CONST_STRING)
24151 {
24152 size_t len = strlen (XSTR (rtl, 0)) + 1;
24153 tree t = build_string (len, XSTR (rtl, 0));
24154 tree tlen = size_int (len - 1);
24155
24156 TREE_TYPE (t)
24157 = build_array_type (char_type_node, build_index_type (tlen));
24158 rtl = string_cst_pool_decl (t);
24159 if (!rtl)
24160 return false;
24161 }
24162 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24163 {
24164 decl = SYMBOL_REF_DECL (rtl);
24165 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24166 {
24167 ref = lookup_decl_die (decl);
24168 if (ref && (get_AT (ref, DW_AT_location)
24169 || get_AT (ref, DW_AT_const_value)))
24170 {
24171 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24172 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24173 loc->dw_loc_oprnd1.val_entry = NULL;
24174 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24175 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24176 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24177 loc->dw_loc_oprnd2.v.val_int = offset;
24178 return true;
24179 }
24180 }
24181 }
24182 return false;
24183 }
24184
24185 /* Helper function for resolve_addr, handle one location
24186 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24187 the location list couldn't be resolved. */
24188
24189 static bool
24190 resolve_addr_in_expr (dw_loc_descr_ref loc)
24191 {
24192 dw_loc_descr_ref keep = NULL;
24193 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24194 switch (loc->dw_loc_opc)
24195 {
24196 case DW_OP_addr:
24197 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24198 {
24199 if ((prev == NULL
24200 || prev->dw_loc_opc == DW_OP_piece
24201 || prev->dw_loc_opc == DW_OP_bit_piece)
24202 && loc->dw_loc_next
24203 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24204 && !dwarf_strict
24205 && optimize_one_addr_into_implicit_ptr (loc))
24206 break;
24207 return false;
24208 }
24209 break;
24210 case DW_OP_GNU_addr_index:
24211 case DW_OP_GNU_const_index:
24212 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24213 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24214 {
24215 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24216 if (!resolve_one_addr (&rtl))
24217 return false;
24218 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24219 loc->dw_loc_oprnd1.val_entry =
24220 add_addr_table_entry (rtl, ate_kind_rtx);
24221 }
24222 break;
24223 case DW_OP_const4u:
24224 case DW_OP_const8u:
24225 if (loc->dtprel
24226 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24227 return false;
24228 break;
24229 case DW_OP_plus_uconst:
24230 if (size_of_loc_descr (loc)
24231 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24232 + 1
24233 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24234 {
24235 dw_loc_descr_ref repl
24236 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24237 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24238 add_loc_descr (&repl, loc->dw_loc_next);
24239 *loc = *repl;
24240 }
24241 break;
24242 case DW_OP_implicit_value:
24243 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24244 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24245 return false;
24246 break;
24247 case DW_OP_GNU_implicit_pointer:
24248 case DW_OP_GNU_parameter_ref:
24249 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24250 {
24251 dw_die_ref ref
24252 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24253 if (ref == NULL)
24254 return false;
24255 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24256 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24257 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24258 }
24259 break;
24260 case DW_OP_GNU_const_type:
24261 case DW_OP_GNU_regval_type:
24262 case DW_OP_GNU_deref_type:
24263 case DW_OP_GNU_convert:
24264 case DW_OP_GNU_reinterpret:
24265 while (loc->dw_loc_next
24266 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24267 {
24268 dw_die_ref base1, base2;
24269 unsigned enc1, enc2, size1, size2;
24270 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24271 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24272 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24273 else if (loc->dw_loc_oprnd1.val_class
24274 == dw_val_class_unsigned_const)
24275 break;
24276 else
24277 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24278 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24279 == dw_val_class_unsigned_const)
24280 break;
24281 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24282 gcc_assert (base1->die_tag == DW_TAG_base_type
24283 && base2->die_tag == DW_TAG_base_type);
24284 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24285 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24286 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24287 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24288 if (size1 == size2
24289 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24290 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24291 && loc != keep)
24292 || enc1 == enc2))
24293 {
24294 /* Optimize away next DW_OP_GNU_convert after
24295 adjusting LOC's base type die reference. */
24296 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24297 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24298 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24299 else
24300 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24301 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24302 continue;
24303 }
24304 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24305 point typed stack entry. */
24306 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24307 keep = loc->dw_loc_next;
24308 break;
24309 }
24310 break;
24311 default:
24312 break;
24313 }
24314 return true;
24315 }
24316
24317 /* Helper function of resolve_addr. DIE had DW_AT_location of
24318 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24319 and DW_OP_addr couldn't be resolved. resolve_addr has already
24320 removed the DW_AT_location attribute. This function attempts to
24321 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24322 to it or DW_AT_const_value attribute, if possible. */
24323
24324 static void
24325 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24326 {
24327 if (TREE_CODE (decl) != VAR_DECL
24328 || lookup_decl_die (decl) != die
24329 || DECL_EXTERNAL (decl)
24330 || !TREE_STATIC (decl)
24331 || DECL_INITIAL (decl) == NULL_TREE
24332 || DECL_P (DECL_INITIAL (decl))
24333 || get_AT (die, DW_AT_const_value))
24334 return;
24335
24336 tree init = DECL_INITIAL (decl);
24337 HOST_WIDE_INT offset = 0;
24338 /* For variables that have been optimized away and thus
24339 don't have a memory location, see if we can emit
24340 DW_AT_const_value instead. */
24341 if (tree_add_const_value_attribute (die, init))
24342 return;
24343 if (dwarf_strict)
24344 return;
24345 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24346 and ADDR_EXPR refers to a decl that has DW_AT_location or
24347 DW_AT_const_value (but isn't addressable, otherwise
24348 resolving the original DW_OP_addr wouldn't fail), see if
24349 we can add DW_OP_GNU_implicit_pointer. */
24350 STRIP_NOPS (init);
24351 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24352 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24353 {
24354 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24355 init = TREE_OPERAND (init, 0);
24356 STRIP_NOPS (init);
24357 }
24358 if (TREE_CODE (init) != ADDR_EXPR)
24359 return;
24360 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24361 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24362 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24363 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24364 && TREE_OPERAND (init, 0) != decl))
24365 {
24366 dw_die_ref ref;
24367 dw_loc_descr_ref l;
24368
24369 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24370 {
24371 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24372 if (!rtl)
24373 return;
24374 decl = SYMBOL_REF_DECL (rtl);
24375 }
24376 else
24377 decl = TREE_OPERAND (init, 0);
24378 ref = lookup_decl_die (decl);
24379 if (ref == NULL
24380 || (!get_AT (ref, DW_AT_location)
24381 && !get_AT (ref, DW_AT_const_value)))
24382 return;
24383 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24384 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24385 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24386 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24387 add_AT_loc (die, DW_AT_location, l);
24388 }
24389 }
24390
24391 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24392 an address in .rodata section if the string literal is emitted there,
24393 or remove the containing location list or replace DW_AT_const_value
24394 with DW_AT_location and empty location expression, if it isn't found
24395 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24396 to something that has been emitted in the current CU. */
24397
24398 static void
24399 resolve_addr (dw_die_ref die)
24400 {
24401 dw_die_ref c;
24402 dw_attr_ref a;
24403 dw_loc_list_ref *curr, *start, loc;
24404 unsigned ix;
24405
24406 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24407 switch (AT_class (a))
24408 {
24409 case dw_val_class_loc_list:
24410 start = curr = AT_loc_list_ptr (a);
24411 loc = *curr;
24412 gcc_assert (loc);
24413 /* The same list can be referenced more than once. See if we have
24414 already recorded the result from a previous pass. */
24415 if (loc->replaced)
24416 *curr = loc->dw_loc_next;
24417 else if (!loc->resolved_addr)
24418 {
24419 /* As things stand, we do not expect or allow one die to
24420 reference a suffix of another die's location list chain.
24421 References must be identical or completely separate.
24422 There is therefore no need to cache the result of this
24423 pass on any list other than the first; doing so
24424 would lead to unnecessary writes. */
24425 while (*curr)
24426 {
24427 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24428 if (!resolve_addr_in_expr ((*curr)->expr))
24429 {
24430 dw_loc_list_ref next = (*curr)->dw_loc_next;
24431 dw_loc_descr_ref l = (*curr)->expr;
24432
24433 if (next && (*curr)->ll_symbol)
24434 {
24435 gcc_assert (!next->ll_symbol);
24436 next->ll_symbol = (*curr)->ll_symbol;
24437 }
24438 if (dwarf_split_debug_info)
24439 remove_loc_list_addr_table_entries (l);
24440 *curr = next;
24441 }
24442 else
24443 {
24444 mark_base_types ((*curr)->expr);
24445 curr = &(*curr)->dw_loc_next;
24446 }
24447 }
24448 if (loc == *start)
24449 loc->resolved_addr = 1;
24450 else
24451 {
24452 loc->replaced = 1;
24453 loc->dw_loc_next = *start;
24454 }
24455 }
24456 if (!*start)
24457 {
24458 remove_AT (die, a->dw_attr);
24459 ix--;
24460 }
24461 break;
24462 case dw_val_class_loc:
24463 {
24464 dw_loc_descr_ref l = AT_loc (a);
24465 /* For -gdwarf-2 don't attempt to optimize
24466 DW_AT_data_member_location containing
24467 DW_OP_plus_uconst - older consumers might
24468 rely on it being that op instead of a more complex,
24469 but shorter, location description. */
24470 if ((dwarf_version > 2
24471 || a->dw_attr != DW_AT_data_member_location
24472 || l == NULL
24473 || l->dw_loc_opc != DW_OP_plus_uconst
24474 || l->dw_loc_next != NULL)
24475 && !resolve_addr_in_expr (l))
24476 {
24477 if (dwarf_split_debug_info)
24478 remove_loc_list_addr_table_entries (l);
24479 if (l != NULL
24480 && l->dw_loc_next == NULL
24481 && l->dw_loc_opc == DW_OP_addr
24482 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24483 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24484 && a->dw_attr == DW_AT_location)
24485 {
24486 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24487 remove_AT (die, a->dw_attr);
24488 ix--;
24489 optimize_location_into_implicit_ptr (die, decl);
24490 break;
24491 }
24492 remove_AT (die, a->dw_attr);
24493 ix--;
24494 }
24495 else
24496 mark_base_types (l);
24497 }
24498 break;
24499 case dw_val_class_addr:
24500 if (a->dw_attr == DW_AT_const_value
24501 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24502 {
24503 if (AT_index (a) != NOT_INDEXED)
24504 remove_addr_table_entry (a->dw_attr_val.val_entry);
24505 remove_AT (die, a->dw_attr);
24506 ix--;
24507 }
24508 if (die->die_tag == DW_TAG_GNU_call_site
24509 && a->dw_attr == DW_AT_abstract_origin)
24510 {
24511 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24512 dw_die_ref tdie = lookup_decl_die (tdecl);
24513 dw_die_ref cdie;
24514 if (tdie == NULL
24515 && DECL_EXTERNAL (tdecl)
24516 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24517 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24518 {
24519 /* Creating a full DIE for tdecl is overly expensive and
24520 at this point even wrong when in the LTO phase
24521 as it can end up generating new type DIEs we didn't
24522 output and thus optimize_external_refs will crash. */
24523 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24524 add_AT_flag (tdie, DW_AT_external, 1);
24525 add_AT_flag (tdie, DW_AT_declaration, 1);
24526 add_linkage_attr (tdie, tdecl);
24527 add_name_and_src_coords_attributes (tdie, tdecl);
24528 equate_decl_number_to_die (tdecl, tdie);
24529 }
24530 if (tdie)
24531 {
24532 a->dw_attr_val.val_class = dw_val_class_die_ref;
24533 a->dw_attr_val.v.val_die_ref.die = tdie;
24534 a->dw_attr_val.v.val_die_ref.external = 0;
24535 }
24536 else
24537 {
24538 if (AT_index (a) != NOT_INDEXED)
24539 remove_addr_table_entry (a->dw_attr_val.val_entry);
24540 remove_AT (die, a->dw_attr);
24541 ix--;
24542 }
24543 }
24544 break;
24545 default:
24546 break;
24547 }
24548
24549 FOR_EACH_CHILD (die, c, resolve_addr (c));
24550 }
24551 \f
24552 /* Helper routines for optimize_location_lists.
24553 This pass tries to share identical local lists in .debug_loc
24554 section. */
24555
24556 /* Iteratively hash operands of LOC opcode into HSTATE. */
24557
24558 static void
24559 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24560 {
24561 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24562 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24563
24564 switch (loc->dw_loc_opc)
24565 {
24566 case DW_OP_const4u:
24567 case DW_OP_const8u:
24568 if (loc->dtprel)
24569 goto hash_addr;
24570 /* FALLTHRU */
24571 case DW_OP_const1u:
24572 case DW_OP_const1s:
24573 case DW_OP_const2u:
24574 case DW_OP_const2s:
24575 case DW_OP_const4s:
24576 case DW_OP_const8s:
24577 case DW_OP_constu:
24578 case DW_OP_consts:
24579 case DW_OP_pick:
24580 case DW_OP_plus_uconst:
24581 case DW_OP_breg0:
24582 case DW_OP_breg1:
24583 case DW_OP_breg2:
24584 case DW_OP_breg3:
24585 case DW_OP_breg4:
24586 case DW_OP_breg5:
24587 case DW_OP_breg6:
24588 case DW_OP_breg7:
24589 case DW_OP_breg8:
24590 case DW_OP_breg9:
24591 case DW_OP_breg10:
24592 case DW_OP_breg11:
24593 case DW_OP_breg12:
24594 case DW_OP_breg13:
24595 case DW_OP_breg14:
24596 case DW_OP_breg15:
24597 case DW_OP_breg16:
24598 case DW_OP_breg17:
24599 case DW_OP_breg18:
24600 case DW_OP_breg19:
24601 case DW_OP_breg20:
24602 case DW_OP_breg21:
24603 case DW_OP_breg22:
24604 case DW_OP_breg23:
24605 case DW_OP_breg24:
24606 case DW_OP_breg25:
24607 case DW_OP_breg26:
24608 case DW_OP_breg27:
24609 case DW_OP_breg28:
24610 case DW_OP_breg29:
24611 case DW_OP_breg30:
24612 case DW_OP_breg31:
24613 case DW_OP_regx:
24614 case DW_OP_fbreg:
24615 case DW_OP_piece:
24616 case DW_OP_deref_size:
24617 case DW_OP_xderef_size:
24618 hstate.add_object (val1->v.val_int);
24619 break;
24620 case DW_OP_skip:
24621 case DW_OP_bra:
24622 {
24623 int offset;
24624
24625 gcc_assert (val1->val_class == dw_val_class_loc);
24626 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24627 hstate.add_object (offset);
24628 }
24629 break;
24630 case DW_OP_implicit_value:
24631 hstate.add_object (val1->v.val_unsigned);
24632 switch (val2->val_class)
24633 {
24634 case dw_val_class_const:
24635 hstate.add_object (val2->v.val_int);
24636 break;
24637 case dw_val_class_vec:
24638 {
24639 unsigned int elt_size = val2->v.val_vec.elt_size;
24640 unsigned int len = val2->v.val_vec.length;
24641
24642 hstate.add_int (elt_size);
24643 hstate.add_int (len);
24644 hstate.add (val2->v.val_vec.array, len * elt_size);
24645 }
24646 break;
24647 case dw_val_class_const_double:
24648 hstate.add_object (val2->v.val_double.low);
24649 hstate.add_object (val2->v.val_double.high);
24650 break;
24651 case dw_val_class_wide_int:
24652 hstate.add (val2->v.val_wide->get_val (),
24653 get_full_len (*val2->v.val_wide)
24654 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24655 break;
24656 case dw_val_class_addr:
24657 inchash::add_rtx (val2->v.val_addr, hstate);
24658 break;
24659 default:
24660 gcc_unreachable ();
24661 }
24662 break;
24663 case DW_OP_bregx:
24664 case DW_OP_bit_piece:
24665 hstate.add_object (val1->v.val_int);
24666 hstate.add_object (val2->v.val_int);
24667 break;
24668 case DW_OP_addr:
24669 hash_addr:
24670 if (loc->dtprel)
24671 {
24672 unsigned char dtprel = 0xd1;
24673 hstate.add_object (dtprel);
24674 }
24675 inchash::add_rtx (val1->v.val_addr, hstate);
24676 break;
24677 case DW_OP_GNU_addr_index:
24678 case DW_OP_GNU_const_index:
24679 {
24680 if (loc->dtprel)
24681 {
24682 unsigned char dtprel = 0xd1;
24683 hstate.add_object (dtprel);
24684 }
24685 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24686 }
24687 break;
24688 case DW_OP_GNU_implicit_pointer:
24689 hstate.add_int (val2->v.val_int);
24690 break;
24691 case DW_OP_GNU_entry_value:
24692 hstate.add_object (val1->v.val_loc);
24693 break;
24694 case DW_OP_GNU_regval_type:
24695 case DW_OP_GNU_deref_type:
24696 {
24697 unsigned int byte_size
24698 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24699 unsigned int encoding
24700 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24701 hstate.add_object (val1->v.val_int);
24702 hstate.add_object (byte_size);
24703 hstate.add_object (encoding);
24704 }
24705 break;
24706 case DW_OP_GNU_convert:
24707 case DW_OP_GNU_reinterpret:
24708 if (val1->val_class == dw_val_class_unsigned_const)
24709 {
24710 hstate.add_object (val1->v.val_unsigned);
24711 break;
24712 }
24713 /* FALLTHRU */
24714 case DW_OP_GNU_const_type:
24715 {
24716 unsigned int byte_size
24717 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24718 unsigned int encoding
24719 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24720 hstate.add_object (byte_size);
24721 hstate.add_object (encoding);
24722 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24723 break;
24724 hstate.add_object (val2->val_class);
24725 switch (val2->val_class)
24726 {
24727 case dw_val_class_const:
24728 hstate.add_object (val2->v.val_int);
24729 break;
24730 case dw_val_class_vec:
24731 {
24732 unsigned int elt_size = val2->v.val_vec.elt_size;
24733 unsigned int len = val2->v.val_vec.length;
24734
24735 hstate.add_object (elt_size);
24736 hstate.add_object (len);
24737 hstate.add (val2->v.val_vec.array, len * elt_size);
24738 }
24739 break;
24740 case dw_val_class_const_double:
24741 hstate.add_object (val2->v.val_double.low);
24742 hstate.add_object (val2->v.val_double.high);
24743 break;
24744 case dw_val_class_wide_int:
24745 hstate.add (val2->v.val_wide->get_val (),
24746 get_full_len (*val2->v.val_wide)
24747 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24748 break;
24749 default:
24750 gcc_unreachable ();
24751 }
24752 }
24753 break;
24754
24755 default:
24756 /* Other codes have no operands. */
24757 break;
24758 }
24759 }
24760
24761 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24762
24763 static inline void
24764 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24765 {
24766 dw_loc_descr_ref l;
24767 bool sizes_computed = false;
24768 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24769 size_of_locs (loc);
24770
24771 for (l = loc; l != NULL; l = l->dw_loc_next)
24772 {
24773 enum dwarf_location_atom opc = l->dw_loc_opc;
24774 hstate.add_object (opc);
24775 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24776 {
24777 size_of_locs (loc);
24778 sizes_computed = true;
24779 }
24780 hash_loc_operands (l, hstate);
24781 }
24782 }
24783
24784 /* Compute hash of the whole location list LIST_HEAD. */
24785
24786 static inline void
24787 hash_loc_list (dw_loc_list_ref list_head)
24788 {
24789 dw_loc_list_ref curr = list_head;
24790 inchash::hash hstate;
24791
24792 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24793 {
24794 hstate.add (curr->begin, strlen (curr->begin) + 1);
24795 hstate.add (curr->end, strlen (curr->end) + 1);
24796 if (curr->section)
24797 hstate.add (curr->section, strlen (curr->section) + 1);
24798 hash_locs (curr->expr, hstate);
24799 }
24800 list_head->hash = hstate.end ();
24801 }
24802
24803 /* Return true if X and Y opcodes have the same operands. */
24804
24805 static inline bool
24806 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24807 {
24808 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24809 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24810 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24811 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24812
24813 switch (x->dw_loc_opc)
24814 {
24815 case DW_OP_const4u:
24816 case DW_OP_const8u:
24817 if (x->dtprel)
24818 goto hash_addr;
24819 /* FALLTHRU */
24820 case DW_OP_const1u:
24821 case DW_OP_const1s:
24822 case DW_OP_const2u:
24823 case DW_OP_const2s:
24824 case DW_OP_const4s:
24825 case DW_OP_const8s:
24826 case DW_OP_constu:
24827 case DW_OP_consts:
24828 case DW_OP_pick:
24829 case DW_OP_plus_uconst:
24830 case DW_OP_breg0:
24831 case DW_OP_breg1:
24832 case DW_OP_breg2:
24833 case DW_OP_breg3:
24834 case DW_OP_breg4:
24835 case DW_OP_breg5:
24836 case DW_OP_breg6:
24837 case DW_OP_breg7:
24838 case DW_OP_breg8:
24839 case DW_OP_breg9:
24840 case DW_OP_breg10:
24841 case DW_OP_breg11:
24842 case DW_OP_breg12:
24843 case DW_OP_breg13:
24844 case DW_OP_breg14:
24845 case DW_OP_breg15:
24846 case DW_OP_breg16:
24847 case DW_OP_breg17:
24848 case DW_OP_breg18:
24849 case DW_OP_breg19:
24850 case DW_OP_breg20:
24851 case DW_OP_breg21:
24852 case DW_OP_breg22:
24853 case DW_OP_breg23:
24854 case DW_OP_breg24:
24855 case DW_OP_breg25:
24856 case DW_OP_breg26:
24857 case DW_OP_breg27:
24858 case DW_OP_breg28:
24859 case DW_OP_breg29:
24860 case DW_OP_breg30:
24861 case DW_OP_breg31:
24862 case DW_OP_regx:
24863 case DW_OP_fbreg:
24864 case DW_OP_piece:
24865 case DW_OP_deref_size:
24866 case DW_OP_xderef_size:
24867 return valx1->v.val_int == valy1->v.val_int;
24868 case DW_OP_skip:
24869 case DW_OP_bra:
24870 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24871 can cause irrelevant differences in dw_loc_addr. */
24872 gcc_assert (valx1->val_class == dw_val_class_loc
24873 && valy1->val_class == dw_val_class_loc
24874 && (dwarf_split_debug_info
24875 || x->dw_loc_addr == y->dw_loc_addr));
24876 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24877 case DW_OP_implicit_value:
24878 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24879 || valx2->val_class != valy2->val_class)
24880 return false;
24881 switch (valx2->val_class)
24882 {
24883 case dw_val_class_const:
24884 return valx2->v.val_int == valy2->v.val_int;
24885 case dw_val_class_vec:
24886 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24887 && valx2->v.val_vec.length == valy2->v.val_vec.length
24888 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24889 valx2->v.val_vec.elt_size
24890 * valx2->v.val_vec.length) == 0;
24891 case dw_val_class_const_double:
24892 return valx2->v.val_double.low == valy2->v.val_double.low
24893 && valx2->v.val_double.high == valy2->v.val_double.high;
24894 case dw_val_class_wide_int:
24895 return *valx2->v.val_wide == *valy2->v.val_wide;
24896 case dw_val_class_addr:
24897 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24898 default:
24899 gcc_unreachable ();
24900 }
24901 case DW_OP_bregx:
24902 case DW_OP_bit_piece:
24903 return valx1->v.val_int == valy1->v.val_int
24904 && valx2->v.val_int == valy2->v.val_int;
24905 case DW_OP_addr:
24906 hash_addr:
24907 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24908 case DW_OP_GNU_addr_index:
24909 case DW_OP_GNU_const_index:
24910 {
24911 rtx ax1 = valx1->val_entry->addr.rtl;
24912 rtx ay1 = valy1->val_entry->addr.rtl;
24913 return rtx_equal_p (ax1, ay1);
24914 }
24915 case DW_OP_GNU_implicit_pointer:
24916 return valx1->val_class == dw_val_class_die_ref
24917 && valx1->val_class == valy1->val_class
24918 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24919 && valx2->v.val_int == valy2->v.val_int;
24920 case DW_OP_GNU_entry_value:
24921 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24922 case DW_OP_GNU_const_type:
24923 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24924 || valx2->val_class != valy2->val_class)
24925 return false;
24926 switch (valx2->val_class)
24927 {
24928 case dw_val_class_const:
24929 return valx2->v.val_int == valy2->v.val_int;
24930 case dw_val_class_vec:
24931 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24932 && valx2->v.val_vec.length == valy2->v.val_vec.length
24933 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24934 valx2->v.val_vec.elt_size
24935 * valx2->v.val_vec.length) == 0;
24936 case dw_val_class_const_double:
24937 return valx2->v.val_double.low == valy2->v.val_double.low
24938 && valx2->v.val_double.high == valy2->v.val_double.high;
24939 case dw_val_class_wide_int:
24940 return *valx2->v.val_wide == *valy2->v.val_wide;
24941 default:
24942 gcc_unreachable ();
24943 }
24944 case DW_OP_GNU_regval_type:
24945 case DW_OP_GNU_deref_type:
24946 return valx1->v.val_int == valy1->v.val_int
24947 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24948 case DW_OP_GNU_convert:
24949 case DW_OP_GNU_reinterpret:
24950 if (valx1->val_class != valy1->val_class)
24951 return false;
24952 if (valx1->val_class == dw_val_class_unsigned_const)
24953 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24954 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24955 case DW_OP_GNU_parameter_ref:
24956 return valx1->val_class == dw_val_class_die_ref
24957 && valx1->val_class == valy1->val_class
24958 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24959 default:
24960 /* Other codes have no operands. */
24961 return true;
24962 }
24963 }
24964
24965 /* Return true if DWARF location expressions X and Y are the same. */
24966
24967 static inline bool
24968 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24969 {
24970 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24971 if (x->dw_loc_opc != y->dw_loc_opc
24972 || x->dtprel != y->dtprel
24973 || !compare_loc_operands (x, y))
24974 break;
24975 return x == NULL && y == NULL;
24976 }
24977
24978 /* Hashtable helpers. */
24979
24980 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
24981 {
24982 static inline hashval_t hash (const dw_loc_list_struct *);
24983 static inline bool equal (const dw_loc_list_struct *,
24984 const dw_loc_list_struct *);
24985 };
24986
24987 /* Return precomputed hash of location list X. */
24988
24989 inline hashval_t
24990 loc_list_hasher::hash (const dw_loc_list_struct *x)
24991 {
24992 return x->hash;
24993 }
24994
24995 /* Return true if location lists A and B are the same. */
24996
24997 inline bool
24998 loc_list_hasher::equal (const dw_loc_list_struct *a,
24999 const dw_loc_list_struct *b)
25000 {
25001 if (a == b)
25002 return 1;
25003 if (a->hash != b->hash)
25004 return 0;
25005 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25006 if (strcmp (a->begin, b->begin) != 0
25007 || strcmp (a->end, b->end) != 0
25008 || (a->section == NULL) != (b->section == NULL)
25009 || (a->section && strcmp (a->section, b->section) != 0)
25010 || !compare_locs (a->expr, b->expr))
25011 break;
25012 return a == NULL && b == NULL;
25013 }
25014
25015 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25016
25017
25018 /* Recursively optimize location lists referenced from DIE
25019 children and share them whenever possible. */
25020
25021 static void
25022 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25023 {
25024 dw_die_ref c;
25025 dw_attr_ref a;
25026 unsigned ix;
25027 dw_loc_list_struct **slot;
25028
25029 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25030 if (AT_class (a) == dw_val_class_loc_list)
25031 {
25032 dw_loc_list_ref list = AT_loc_list (a);
25033 /* TODO: perform some optimizations here, before hashing
25034 it and storing into the hash table. */
25035 hash_loc_list (list);
25036 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25037 if (*slot == NULL)
25038 *slot = list;
25039 else
25040 a->dw_attr_val.v.val_loc_list = *slot;
25041 }
25042
25043 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25044 }
25045
25046
25047 /* Recursively assign each location list a unique index into the debug_addr
25048 section. */
25049
25050 static void
25051 index_location_lists (dw_die_ref die)
25052 {
25053 dw_die_ref c;
25054 dw_attr_ref a;
25055 unsigned ix;
25056
25057 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25058 if (AT_class (a) == dw_val_class_loc_list)
25059 {
25060 dw_loc_list_ref list = AT_loc_list (a);
25061 dw_loc_list_ref curr;
25062 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25063 {
25064 /* Don't index an entry that has already been indexed
25065 or won't be output. */
25066 if (curr->begin_entry != NULL
25067 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25068 continue;
25069
25070 curr->begin_entry
25071 = add_addr_table_entry (xstrdup (curr->begin),
25072 ate_kind_label);
25073 }
25074 }
25075
25076 FOR_EACH_CHILD (die, c, index_location_lists (c));
25077 }
25078
25079 /* Optimize location lists referenced from DIE
25080 children and share them whenever possible. */
25081
25082 static void
25083 optimize_location_lists (dw_die_ref die)
25084 {
25085 loc_list_hash_type htab (500);
25086 optimize_location_lists_1 (die, &htab);
25087 }
25088 \f
25089 /* Output stuff that dwarf requires at the end of every file,
25090 and generate the DWARF-2 debugging info. */
25091
25092 static void
25093 dwarf2out_finish (const char *filename)
25094 {
25095 comdat_type_node *ctnode;
25096 dw_die_ref main_comp_unit_die;
25097
25098 /* Flush out any latecomers to the limbo party. */
25099 dwarf2out_early_finish ();
25100
25101 /* PCH might result in DW_AT_producer string being restored from the
25102 header compilation, so always fill it with empty string initially
25103 and overwrite only here. */
25104 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
25105 producer_string = gen_producer_string ();
25106 producer->dw_attr_val.v.val_str->refcount--;
25107 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25108
25109 gen_scheduled_generic_parms_dies ();
25110 gen_remaining_tmpl_value_param_die_attribute ();
25111
25112 /* Add the name for the main input file now. We delayed this from
25113 dwarf2out_init to avoid complications with PCH.
25114 For LTO produced units use a fixed artificial name to avoid
25115 leaking tempfile names into the dwarf. */
25116 if (!in_lto_p)
25117 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25118 else
25119 add_name_attribute (comp_unit_die (), "<artificial>");
25120 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25121 add_comp_dir_attribute (comp_unit_die ());
25122 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25123 {
25124 bool p = false;
25125 file_table->traverse<bool *, file_table_relative_p> (&p);
25126 if (p)
25127 add_comp_dir_attribute (comp_unit_die ());
25128 }
25129
25130 #if ENABLE_ASSERT_CHECKING
25131 {
25132 dw_die_ref die = comp_unit_die (), c;
25133 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25134 }
25135 #endif
25136 resolve_addr (comp_unit_die ());
25137 move_marked_base_types ();
25138
25139 /* Walk through the list of incomplete types again, trying once more to
25140 emit full debugging info for them. */
25141 retry_incomplete_types ();
25142
25143 if (flag_eliminate_unused_debug_types)
25144 prune_unused_types ();
25145
25146 /* Generate separate COMDAT sections for type DIEs. */
25147 if (use_debug_types)
25148 {
25149 break_out_comdat_types (comp_unit_die ());
25150
25151 /* Each new type_unit DIE was added to the limbo die list when created.
25152 Since these have all been added to comdat_type_list, clear the
25153 limbo die list. */
25154 limbo_die_list = NULL;
25155
25156 /* For each new comdat type unit, copy declarations for incomplete
25157 types to make the new unit self-contained (i.e., no direct
25158 references to the main compile unit). */
25159 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25160 copy_decls_for_unworthy_types (ctnode->root_die);
25161 copy_decls_for_unworthy_types (comp_unit_die ());
25162
25163 /* In the process of copying declarations from one unit to another,
25164 we may have left some declarations behind that are no longer
25165 referenced. Prune them. */
25166 prune_unused_types ();
25167 }
25168
25169 /* Generate separate CUs for each of the include files we've seen.
25170 They will go into limbo_die_list. */
25171 if (flag_eliminate_dwarf2_dups)
25172 break_out_includes (comp_unit_die ());
25173
25174 /* Traverse the DIE's and add add sibling attributes to those DIE's
25175 that have children. */
25176 add_sibling_attributes (comp_unit_die ());
25177 limbo_die_node *node;
25178 for (node = limbo_die_list; node; node = node->next)
25179 add_sibling_attributes (node->die);
25180 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25181 add_sibling_attributes (ctnode->root_die);
25182
25183 /* When splitting DWARF info, we put some attributes in the
25184 skeleton compile_unit DIE that remains in the .o, while
25185 most attributes go in the DWO compile_unit_die. */
25186 if (dwarf_split_debug_info)
25187 main_comp_unit_die = gen_compile_unit_die (NULL);
25188 else
25189 main_comp_unit_die = comp_unit_die ();
25190
25191 /* Output a terminator label for the .text section. */
25192 switch_to_section (text_section);
25193 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25194 if (cold_text_section)
25195 {
25196 switch_to_section (cold_text_section);
25197 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25198 }
25199
25200 /* We can only use the low/high_pc attributes if all of the code was
25201 in .text. */
25202 if (!have_multiple_function_sections
25203 || (dwarf_version < 3 && dwarf_strict))
25204 {
25205 /* Don't add if the CU has no associated code. */
25206 if (text_section_used)
25207 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25208 text_end_label, true);
25209 }
25210 else
25211 {
25212 unsigned fde_idx;
25213 dw_fde_ref fde;
25214 bool range_list_added = false;
25215
25216 if (text_section_used)
25217 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25218 text_end_label, &range_list_added, true);
25219 if (cold_text_section_used)
25220 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25221 cold_end_label, &range_list_added, true);
25222
25223 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25224 {
25225 if (DECL_IGNORED_P (fde->decl))
25226 continue;
25227 if (!fde->in_std_section)
25228 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25229 fde->dw_fde_end, &range_list_added,
25230 true);
25231 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25232 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25233 fde->dw_fde_second_end, &range_list_added,
25234 true);
25235 }
25236
25237 if (range_list_added)
25238 {
25239 /* We need to give .debug_loc and .debug_ranges an appropriate
25240 "base address". Use zero so that these addresses become
25241 absolute. Historically, we've emitted the unexpected
25242 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25243 Emit both to give time for other tools to adapt. */
25244 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25245 if (! dwarf_strict && dwarf_version < 4)
25246 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25247
25248 add_ranges (NULL);
25249 }
25250 }
25251
25252 if (debug_info_level >= DINFO_LEVEL_TERSE)
25253 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25254 debug_line_section_label);
25255
25256 if (have_macinfo)
25257 add_AT_macptr (comp_unit_die (),
25258 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25259 macinfo_section_label);
25260
25261 if (dwarf_split_debug_info)
25262 {
25263 /* optimize_location_lists calculates the size of the lists,
25264 so index them first, and assign indices to the entries.
25265 Although optimize_location_lists will remove entries from
25266 the table, it only does so for duplicates, and therefore
25267 only reduces ref_counts to 1. */
25268 index_location_lists (comp_unit_die ());
25269
25270 if (addr_index_table != NULL)
25271 {
25272 unsigned int index = 0;
25273 addr_index_table
25274 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25275 (&index);
25276 }
25277 }
25278
25279 if (have_location_lists)
25280 optimize_location_lists (comp_unit_die ());
25281
25282 save_macinfo_strings ();
25283
25284 if (dwarf_split_debug_info)
25285 {
25286 unsigned int index = 0;
25287
25288 /* Add attributes common to skeleton compile_units and
25289 type_units. Because these attributes include strings, it
25290 must be done before freezing the string table. Top-level
25291 skeleton die attrs are added when the skeleton type unit is
25292 created, so ensure it is created by this point. */
25293 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25294 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25295 }
25296
25297 /* Output all of the compilation units. We put the main one last so that
25298 the offsets are available to output_pubnames. */
25299 for (node = limbo_die_list; node; node = node->next)
25300 output_comp_unit (node->die, 0);
25301
25302 hash_table<comdat_type_hasher> comdat_type_table (100);
25303 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25304 {
25305 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25306
25307 /* Don't output duplicate types. */
25308 if (*slot != HTAB_EMPTY_ENTRY)
25309 continue;
25310
25311 /* Add a pointer to the line table for the main compilation unit
25312 so that the debugger can make sense of DW_AT_decl_file
25313 attributes. */
25314 if (debug_info_level >= DINFO_LEVEL_TERSE)
25315 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25316 (!dwarf_split_debug_info
25317 ? debug_line_section_label
25318 : debug_skeleton_line_section_label));
25319
25320 output_comdat_type_unit (ctnode);
25321 *slot = ctnode;
25322 }
25323
25324 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25325 both the main_cu and all skeleton TUs. Making this call unconditional
25326 would end up either adding a second copy of the AT_pubnames attribute, or
25327 requiring a special case in add_top_level_skeleton_die_attrs. */
25328 if (!dwarf_split_debug_info)
25329 add_AT_pubnames (comp_unit_die ());
25330
25331 if (dwarf_split_debug_info)
25332 {
25333 int mark;
25334 unsigned char checksum[16];
25335 struct md5_ctx ctx;
25336
25337 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25338 md5_init_ctx (&ctx);
25339 mark = 0;
25340 die_checksum (comp_unit_die (), &ctx, &mark);
25341 unmark_all_dies (comp_unit_die ());
25342 md5_finish_ctx (&ctx, checksum);
25343
25344 /* Use the first 8 bytes of the checksum as the dwo_id,
25345 and add it to both comp-unit DIEs. */
25346 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25347 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25348
25349 /* Add the base offset of the ranges table to the skeleton
25350 comp-unit DIE. */
25351 if (ranges_table_in_use)
25352 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25353 ranges_section_label);
25354
25355 switch_to_section (debug_addr_section);
25356 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25357 output_addr_table ();
25358 }
25359
25360 /* Output the main compilation unit if non-empty or if .debug_macinfo
25361 or .debug_macro will be emitted. */
25362 output_comp_unit (comp_unit_die (), have_macinfo);
25363
25364 if (dwarf_split_debug_info && info_section_emitted)
25365 output_skeleton_debug_sections (main_comp_unit_die);
25366
25367 /* Output the abbreviation table. */
25368 if (abbrev_die_table_in_use != 1)
25369 {
25370 switch_to_section (debug_abbrev_section);
25371 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25372 output_abbrev_section ();
25373 }
25374
25375 /* Output location list section if necessary. */
25376 if (have_location_lists)
25377 {
25378 /* Output the location lists info. */
25379 switch_to_section (debug_loc_section);
25380 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25381 output_location_lists (comp_unit_die ());
25382 }
25383
25384 output_pubtables ();
25385
25386 /* Output the address range information if a CU (.debug_info section)
25387 was emitted. We output an empty table even if we had no functions
25388 to put in it. This because the consumer has no way to tell the
25389 difference between an empty table that we omitted and failure to
25390 generate a table that would have contained data. */
25391 if (info_section_emitted)
25392 {
25393 unsigned long aranges_length = size_of_aranges ();
25394
25395 switch_to_section (debug_aranges_section);
25396 output_aranges (aranges_length);
25397 }
25398
25399 /* Output ranges section if necessary. */
25400 if (ranges_table_in_use)
25401 {
25402 switch_to_section (debug_ranges_section);
25403 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25404 output_ranges ();
25405 }
25406
25407 /* Have to end the macro section. */
25408 if (have_macinfo)
25409 {
25410 switch_to_section (debug_macinfo_section);
25411 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25412 output_macinfo ();
25413 dw2_asm_output_data (1, 0, "End compilation unit");
25414 }
25415
25416 /* Output the source line correspondence table. We must do this
25417 even if there is no line information. Otherwise, on an empty
25418 translation unit, we will generate a present, but empty,
25419 .debug_info section. IRIX 6.5 `nm' will then complain when
25420 examining the file. This is done late so that any filenames
25421 used by the debug_info section are marked as 'used'. */
25422 switch_to_section (debug_line_section);
25423 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25424 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25425 output_line_info (false);
25426
25427 if (dwarf_split_debug_info && info_section_emitted)
25428 {
25429 switch_to_section (debug_skeleton_line_section);
25430 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25431 output_line_info (true);
25432 }
25433
25434 /* If we emitted any indirect strings, output the string table too. */
25435 if (debug_str_hash || skeleton_debug_str_hash)
25436 output_indirect_strings ();
25437 }
25438
25439 /* Perform any cleanups needed after the early debug generation pass
25440 has run. */
25441
25442 static void
25443 dwarf2out_early_finish (void)
25444 {
25445 limbo_die_node *node, *next_node;
25446
25447 /* Add DW_AT_linkage_name for all deferred DIEs. */
25448 for (node = deferred_asm_name; node; node = node->next)
25449 {
25450 tree decl = node->created_for;
25451 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25452 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25453 ended up in in deferred_asm_name before we knew it was
25454 constant and never written to disk. */
25455 && DECL_ASSEMBLER_NAME (decl))
25456 {
25457 add_linkage_attr (node->die, decl);
25458 move_linkage_attr (node->die);
25459 }
25460 }
25461 deferred_asm_name = NULL;
25462
25463 /* Traverse the limbo die list, and add parent/child links. The only
25464 dies without parents that should be here are concrete instances of
25465 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25466 For concrete instances, we can get the parent die from the abstract
25467 instance.
25468
25469 The point here is to flush out the limbo list so that it is empty
25470 and we don't need to stream it for LTO. */
25471 for (node = limbo_die_list; node; node = next_node)
25472 {
25473 dw_die_ref die = node->die;
25474 next_node = node->next;
25475
25476 if (die->die_parent == NULL)
25477 {
25478 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25479
25480 if (origin && origin->die_parent)
25481 add_child_die (origin->die_parent, die);
25482 else if (is_cu_die (die))
25483 ;
25484 else if (seen_error ())
25485 /* It's OK to be confused by errors in the input. */
25486 add_child_die (comp_unit_die (), die);
25487 else
25488 {
25489 /* In certain situations, the lexical block containing a
25490 nested function can be optimized away, which results
25491 in the nested function die being orphaned. Likewise
25492 with the return type of that nested function. Force
25493 this to be a child of the containing function.
25494
25495 It may happen that even the containing function got fully
25496 inlined and optimized out. In that case we are lost and
25497 assign the empty child. This should not be big issue as
25498 the function is likely unreachable too. */
25499 gcc_assert (node->created_for);
25500
25501 if (DECL_P (node->created_for))
25502 origin = get_context_die (DECL_CONTEXT (node->created_for));
25503 else if (TYPE_P (node->created_for))
25504 origin = scope_die_for (node->created_for, comp_unit_die ());
25505 else
25506 origin = comp_unit_die ();
25507
25508 add_child_die (origin, die);
25509 }
25510 }
25511 }
25512
25513 limbo_die_list = NULL;
25514 }
25515
25516 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25517 within the same process. For use by toplev::finalize. */
25518
25519 void
25520 dwarf2out_c_finalize (void)
25521 {
25522 last_var_location_insn = NULL;
25523 cached_next_real_insn = NULL;
25524 used_rtx_array = NULL;
25525 incomplete_types = NULL;
25526 decl_scope_table = NULL;
25527 debug_info_section = NULL;
25528 debug_skeleton_info_section = NULL;
25529 debug_abbrev_section = NULL;
25530 debug_skeleton_abbrev_section = NULL;
25531 debug_aranges_section = NULL;
25532 debug_addr_section = NULL;
25533 debug_macinfo_section = NULL;
25534 debug_line_section = NULL;
25535 debug_skeleton_line_section = NULL;
25536 debug_loc_section = NULL;
25537 debug_pubnames_section = NULL;
25538 debug_pubtypes_section = NULL;
25539 debug_str_section = NULL;
25540 debug_str_dwo_section = NULL;
25541 debug_str_offsets_section = NULL;
25542 debug_ranges_section = NULL;
25543 debug_frame_section = NULL;
25544 fde_vec = NULL;
25545 debug_str_hash = NULL;
25546 skeleton_debug_str_hash = NULL;
25547 dw2_string_counter = 0;
25548 have_multiple_function_sections = false;
25549 text_section_used = false;
25550 cold_text_section_used = false;
25551 cold_text_section = NULL;
25552 current_unit_personality = NULL;
25553
25554 next_die_offset = 0;
25555 single_comp_unit_die = NULL;
25556 comdat_type_list = NULL;
25557 limbo_die_list = NULL;
25558 file_table = NULL;
25559 decl_die_table = NULL;
25560 common_block_die_table = NULL;
25561 decl_loc_table = NULL;
25562 call_arg_locations = NULL;
25563 call_arg_loc_last = NULL;
25564 call_site_count = -1;
25565 tail_call_site_count = -1;
25566 cached_dw_loc_list_table = NULL;
25567 abbrev_die_table = NULL;
25568 abbrev_die_table_allocated = 0;
25569 abbrev_die_table_in_use = 0;
25570 line_info_label_num = 0;
25571 cur_line_info_table = NULL;
25572 text_section_line_info = NULL;
25573 cold_text_section_line_info = NULL;
25574 separate_line_info = NULL;
25575 info_section_emitted = false;
25576 pubname_table = NULL;
25577 pubtype_table = NULL;
25578 macinfo_table = NULL;
25579 ranges_table = NULL;
25580 ranges_table_allocated = 0;
25581 ranges_table_in_use = 0;
25582 ranges_by_label = 0;
25583 ranges_by_label_allocated = 0;
25584 ranges_by_label_in_use = 0;
25585 have_location_lists = false;
25586 loclabel_num = 0;
25587 poc_label_num = 0;
25588 last_emitted_file = NULL;
25589 label_num = 0;
25590 tmpl_value_parm_die_table = NULL;
25591 generic_type_instances = NULL;
25592 frame_pointer_fb_offset = 0;
25593 frame_pointer_fb_offset_valid = false;
25594 base_types.release ();
25595 XDELETEVEC (producer_string);
25596 producer_string = NULL;
25597 }
25598
25599 #include "gt-dwarf2out.h"