Debian Codegraph @

gcc/dwarf2out.cc

raw
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2025 Free Software Foundation, Inc.
3 Contributed by Gary Funck ([email protected]).
4 Derived from DWARF 1 implementation of Ron Guilmette ([email protected]).
5 Extensively modified by Jason Merrill ([email protected]).
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 "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2ctf.h"
83 #include "dwarf2codeview.h"
84 #include "dwarf2asm.h"
85 #include "toplev.h"
86 #include "md5.h"
87 #include "tree-pretty-print.h"
88 #include "print-rtl.h"
89 #include "debug.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
95 #include "tree-dfa.h"
96 #include "gdb/gdb-index.h"
97 #include "rtl-iter.h"
98 #include "stringpool.h"
99 #include "attribs.h"
100 #include "file-prefix-map.h" /* remap_debug_filename() */
101
102 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
103 int, bool);
104 static rtx_insn *last_var_location_insn;
105 static rtx_insn *cached_next_real_insn;
106 static void dwarf2out_decl (tree);
107 static bool is_redundant_typedef (const_tree);
108
109 #ifndef XCOFF_DEBUGGING_INFO
110 #define XCOFF_DEBUGGING_INFO 0
111 #endif
112
113 #ifndef HAVE_XCOFF_DWARF_EXTRAS
114 #define HAVE_XCOFF_DWARF_EXTRAS 0
115 #endif
116
117 #ifdef VMS_DEBUGGING_INFO
118 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119
120 /* Define this macro to be a nonzero value if the directory specifications
121 which are output in the debug info should end with a separator. */
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
123 /* Define this macro to evaluate to a nonzero value if GCC should refrain
124 from generating indirect strings in DWARF2 debug information, for instance
125 if your target is stuck with an old version of GDB that is unable to
126 process them properly or uses VMS Debug. */
127 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
128 #else
129 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
130 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
131 #endif
132
133 /* ??? Poison these here until it can be done generically. They've been
134 totally replaced in this file; make sure it stays that way. */
135 #undef DWARF2_UNWIND_INFO
136 #undef DWARF2_FRAME_INFO
137 #if (GCC_VERSION >= 3000)
138 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
139 #endif
140
141 /* The size of the target's pointer type. */
142 #ifndef PTR_SIZE
143 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
144 #endif
145
146 /* Array of RTXes referenced by the debugging information, which therefore
147 must be kept around forever. */
148 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149
150 /* A pointer to the base of a list of incomplete types which might be
151 completed at some later time. incomplete_types_list needs to be a
152 vec<tree, va_gc> *because we want to tell the garbage collector about
153 it. */
154 static GTY(()) vec<tree, va_gc> *incomplete_types;
155
156 /* Pointers to various DWARF2 sections. */
157 static GTY(()) section *debug_info_section;
158 static GTY(()) section *debug_skeleton_info_section;
159 static GTY(()) section *debug_abbrev_section;
160 static GTY(()) section *debug_skeleton_abbrev_section;
161 static GTY(()) section *debug_aranges_section;
162 static GTY(()) section *debug_addr_section;
163 static GTY(()) section *debug_macinfo_section;
164 static const char *debug_macinfo_section_name;
165 static unsigned macinfo_label_base = 1;
166 static GTY(()) section *debug_line_section;
167 static GTY(()) section *debug_skeleton_line_section;
168 static GTY(()) section *debug_loc_section;
169 static GTY(()) section *debug_pubnames_section;
170 static GTY(()) section *debug_pubtypes_section;
171 static GTY(()) section *debug_str_section;
172 static GTY(()) section *debug_line_str_section;
173 static GTY(()) section *debug_str_dwo_section;
174 static GTY(()) section *debug_str_offsets_section;
175 static GTY(()) section *debug_ranges_section;
176 static GTY(()) section *debug_ranges_dwo_section;
177 static GTY(()) section *debug_frame_section;
178
179 /* Maximum size (in bytes) of an artificially generated label. */
180 #define MAX_ARTIFICIAL_LABEL_BYTES 40
181
182 /* According to the (draft) DWARF 3 specification, the initial length
183 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
184 bytes are 0xffffffff, followed by the length stored in the next 8
185 bytes.
186
187 However, the SGI/MIPS ABI uses an initial length which is equal to
188 dwarf_offset_size. It is defined (elsewhere) accordingly. */
189
190 #ifndef DWARF_INITIAL_LENGTH_SIZE
191 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
192 #endif
193
194 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
195 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
196 #endif
197
198 /* Round SIZE up to the nearest BOUNDARY. */
199 #define DWARF_ROUND(SIZE,BOUNDARY) \
200 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
201
202 /* CIE identifier. */
203 #if HOST_BITS_PER_WIDE_INT >= 64
204 #define DWARF_CIE_ID \
205 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
206 #else
207 #define DWARF_CIE_ID DW_CIE_ID
208 #endif
209
210
211 /* A vector for a table that contains frame description
212 information for each routine. */
213 #define NOT_INDEXED (-1U)
214 #define NO_INDEX_ASSIGNED (-2U)
215
216 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
217
218 struct GTY((for_user)) indirect_string_node {
219 const char *str;
220 unsigned int refcount;
221 enum dwarf_form form;
222 char *label;
223 unsigned int index;
224 };
225
226 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
227 {
228 typedef const char *compare_type;
229
230 static hashval_t hash (indirect_string_node *);
231 static bool equal (indirect_string_node *, const char *);
232 };
233
234 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
235
236 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
237
238 /* With split_debug_info, both the comp_dir and dwo_name go in the
239 main object file, rather than the dwo, similar to the force_direct
240 parameter elsewhere but with additional complications:
241
242 1) The string is needed in both the main object file and the dwo.
243 That is, the comp_dir and dwo_name will appear in both places.
244
245 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
246 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
247
248 3) GCC chooses the form to use late, depending on the size and
249 reference count.
250
251 Rather than forcing the all debug string handling functions and
252 callers to deal with these complications, simply use a separate,
253 special-cased string table for any attribute that should go in the
254 main object file. This limits the complexity to just the places
255 that need it. */
256
257 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
258
259 static GTY(()) int dw2_string_counter;
260
261 /* True if the compilation unit places functions in more than one section. */
262 static GTY(()) bool have_multiple_function_sections = false;
263
264 /* The default cold text section. */
265 static GTY(()) section *cold_text_section;
266
267 /* True if currently in text section. */
268 static GTY(()) bool in_text_section_p = false;
269
270 /* Last debug-on location in corresponding section. */
271 static GTY(()) const char *last_text_label;
272 static GTY(()) const char *last_cold_label;
273
274 /* Mark debug-on/off locations per section.
275 NULL means the section is not used at all. */
276 static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
277 static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
278
279 /* The DIE for C++14 'auto' in a function return type. */
280 static GTY(()) dw_die_ref auto_die;
281
282 /* The DIE for C++14 'decltype(auto)' in a function return type. */
283 static GTY(()) dw_die_ref decltype_auto_die;
284
285 /* Forward declarations for functions defined in this file. */
286
287 static void output_call_frame_info (int);
288
289 /* Personality decl of current unit. Used only when assembler does not support
290 personality CFI. */
291 static GTY(()) rtx current_unit_personality;
292
293 /* Whether an eh_frame section is required. */
294 static GTY(()) bool do_eh_frame = false;
295
296 /* .debug_rnglists next index. */
297 static unsigned int rnglist_idx;
298
299 /* Data and reference forms for relocatable data. */
300 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
301 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
302
303 #ifndef DEBUG_FRAME_SECTION
304 #define DEBUG_FRAME_SECTION ".debug_frame"
305 #endif
306
307 #ifndef FUNC_BEGIN_LABEL
308 #define FUNC_BEGIN_LABEL "LFB"
309 #endif
310
311 #ifndef FUNC_SECOND_SECT_LABEL
312 #define FUNC_SECOND_SECT_LABEL "LFSB"
313 #endif
314
315 #ifndef FUNC_END_LABEL
316 #define FUNC_END_LABEL "LFE"
317 #endif
318
319 #ifndef PROLOGUE_END_LABEL
320 #define PROLOGUE_END_LABEL "LPE"
321 #endif
322
323 #ifndef EPILOGUE_BEGIN_LABEL
324 #define EPILOGUE_BEGIN_LABEL "LEB"
325 #endif
326
327 #ifndef FRAME_BEGIN_LABEL
328 #define FRAME_BEGIN_LABEL "Lframe"
329 #endif
330 #define CIE_AFTER_SIZE_LABEL "LSCIE"
331 #define CIE_END_LABEL "LECIE"
332 #define FDE_LABEL "LSFDE"
333 #define FDE_AFTER_SIZE_LABEL "LASFDE"
334 #define FDE_END_LABEL "LEFDE"
335 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
336 #define LINE_NUMBER_END_LABEL "LELT"
337 #define LN_PROLOG_AS_LABEL "LASLTP"
338 #define LN_PROLOG_END_LABEL "LELTP"
339 #define DIE_LABEL_PREFIX "DW"
340
341 /* Match the base name of a file to the base name of a compilation unit. */
342
343 static bool
344 matches_main_base (const char *path)
345 {
346 /* Cache the last query. */
347 static const char *last_path = NULL;
348 static bool last_match = false;
349 if (path != last_path)
350 {
351 const char *base;
352 int length = base_of_path (path, &base);
353 last_path = path;
354 last_match = (length == main_input_baselength
355 && memcmp (base, main_input_basename, length) == 0);
356 }
357 return last_match;
358 }
359
360 #ifdef DEBUG_DEBUG_STRUCT
361
362 static bool
363 dump_struct_debug (tree type, enum debug_info_usage usage,
364 enum debug_struct_file criterion, int generic,
365 bool matches, bool result)
366 {
367 /* Find the type name. */
368 tree type_decl = TYPE_STUB_DECL (type);
369 tree t = type_decl;
370 const char *name = 0;
371 if (TREE_CODE (t) == TYPE_DECL)
372 t = DECL_NAME (t);
373 if (t)
374 name = IDENTIFIER_POINTER (t);
375
376 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
377 criterion,
378 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
379 matches ? "bas" : "hdr",
380 generic ? "gen" : "ord",
381 usage == DINFO_USAGE_DFN ? ";" :
382 usage == DINFO_USAGE_DIR_USE ? "." : "*",
383 result,
384 (void*) type_decl, name);
385 return result;
386 }
387 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
388 dump_struct_debug (type, usage, criterion, generic, matches, result)
389
390 #else
391
392 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
393 (result)
394
395 #endif
396
397 /* Get the number of HOST_WIDE_INTs needed to represent the precision
398 of the number. */
399
400 static unsigned int
401 get_full_len (const dw_wide_int &op)
402 {
403 return CEIL (op.get_precision (), HOST_BITS_PER_WIDE_INT);
404 }
405
406 static bool
407 should_emit_struct_debug (tree type, enum debug_info_usage usage)
408 {
409 if (debug_info_level <= DINFO_LEVEL_TERSE)
410 return false;
411
412 enum debug_struct_file criterion;
413 tree type_decl;
414 bool generic = lang_hooks.types.generic_p (type);
415
416 if (generic)
417 criterion = debug_struct_generic[usage];
418 else
419 criterion = debug_struct_ordinary[usage];
420
421 if (criterion == DINFO_STRUCT_FILE_NONE)
422 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
423 if (criterion == DINFO_STRUCT_FILE_ANY)
424 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425
426 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
427
428 if (type_decl != NULL)
429 {
430 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
431 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
432
433 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
434 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
435 }
436
437 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
438 }
439
440 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
441 switch to the data section instead, and write out a synthetic start label
442 for collect2 the first time around. */
443
444 static void
445 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
446 {
447 if (eh_frame_section == 0)
448 {
449 int flags;
450
451 if (EH_TABLES_CAN_BE_READ_ONLY)
452 {
453 int fde_encoding;
454 int per_encoding;
455 int lsda_encoding;
456
457 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
458 /*global=*/0);
459 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
460 /*global=*/1);
461 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
462 /*global=*/0);
463 flags = ((! flag_pic
464 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
465 && (fde_encoding & 0x70) != DW_EH_PE_aligned
466 && (per_encoding & 0x70) != DW_EH_PE_absptr
467 && (per_encoding & 0x70) != DW_EH_PE_aligned
468 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
469 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
470 ? 0 : SECTION_WRITE);
471 }
472 else
473 flags = SECTION_WRITE;
474
475 #ifdef EH_FRAME_SECTION_NAME
476 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
477 #else
478 eh_frame_section = ((flags == SECTION_WRITE)
479 ? data_section : readonly_data_section);
480 #endif /* EH_FRAME_SECTION_NAME */
481 }
482
483 switch_to_section (eh_frame_section);
484
485 #ifdef EH_FRAME_THROUGH_COLLECT2
486 /* We have no special eh_frame section. Emit special labels to guide
487 collect2. */
488 if (!back)
489 {
490 tree label = get_file_function_name ("F");
491 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
492 targetm.asm_out.globalize_label (asm_out_file,
493 IDENTIFIER_POINTER (label));
494 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
495 }
496 #endif
497 }
498
499 /* Switch [BACK] to the eh or debug frame table section, depending on
500 FOR_EH. */
501
502 static void
503 switch_to_frame_table_section (int for_eh, bool back)
504 {
505 if (for_eh)
506 switch_to_eh_frame_section (back);
507 else
508 {
509 if (!debug_frame_section)
510 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
511 SECTION_DEBUG, NULL);
512 switch_to_section (debug_frame_section);
513 }
514 }
515
516 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
517
518 enum dw_cfi_oprnd_type
519 dw_cfi_oprnd1_desc (dwarf_call_frame_info cfi)
520 {
521 switch (cfi)
522 {
523 case DW_CFA_nop:
524 case DW_CFA_remember_state:
525 case DW_CFA_restore_state:
526 return dw_cfi_oprnd_unused;
527
528 case DW_CFA_set_loc:
529 case DW_CFA_advance_loc1:
530 case DW_CFA_advance_loc2:
531 case DW_CFA_advance_loc4:
532 case DW_CFA_MIPS_advance_loc8:
533 return dw_cfi_oprnd_addr;
534
535 case DW_CFA_offset:
536 case DW_CFA_offset_extended:
537 case DW_CFA_def_cfa:
538 case DW_CFA_offset_extended_sf:
539 case DW_CFA_def_cfa_sf:
540 case DW_CFA_restore:
541 case DW_CFA_restore_extended:
542 case DW_CFA_undefined:
543 case DW_CFA_same_value:
544 case DW_CFA_def_cfa_register:
545 case DW_CFA_register:
546 case DW_CFA_expression:
547 case DW_CFA_val_expression:
548 return dw_cfi_oprnd_reg_num;
549
550 case DW_CFA_def_cfa_offset:
551 case DW_CFA_GNU_args_size:
552 case DW_CFA_def_cfa_offset_sf:
553 return dw_cfi_oprnd_offset;
554
555 case DW_CFA_def_cfa_expression:
556 return dw_cfi_oprnd_loc;
557
558 default:
559 {
560 dw_cfi_oprnd_type oprnd_type;
561 if (targetm.dw_cfi_oprnd1_desc (cfi, oprnd_type))
562 return oprnd_type;
563 else
564 gcc_unreachable ();
565 }
566 }
567 }
568
569 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
570
571 enum dw_cfi_oprnd_type
572 dw_cfi_oprnd2_desc (dwarf_call_frame_info cfi)
573 {
574 switch (cfi)
575 {
576 case DW_CFA_def_cfa:
577 case DW_CFA_def_cfa_sf:
578 case DW_CFA_offset:
579 case DW_CFA_offset_extended_sf:
580 case DW_CFA_offset_extended:
581 return dw_cfi_oprnd_offset;
582
583 case DW_CFA_register:
584 return dw_cfi_oprnd_reg_num;
585
586 case DW_CFA_expression:
587 case DW_CFA_val_expression:
588 return dw_cfi_oprnd_loc;
589
590 case DW_CFA_def_cfa_expression:
591 return dw_cfi_oprnd_cfa_loc;
592
593 default:
594 return dw_cfi_oprnd_unused;
595 }
596 }
597
598 /* Output one FDE. */
599
600 static void
601 output_fde (dw_fde_ref fde, bool for_eh, bool second,
602 char *section_start_label, int fde_encoding, char *augmentation,
603 bool any_lsda_needed, int lsda_encoding)
604 {
605 const char *begin, *end;
606 static unsigned int j;
607 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
608
609 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
610 /* empty */ 0);
611 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
612 for_eh + j);
613 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
614 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
615 if (!XCOFF_DEBUGGING_INFO || for_eh)
616 {
617 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
618 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
619 " indicating 64-bit DWARF extension");
620 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
621 "FDE Length");
622 }
623 ASM_OUTPUT_LABEL (asm_out_file, l1);
624
625 if (for_eh)
626 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
627 else
628 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
629 debug_frame_section, "FDE CIE offset");
630
631 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
632 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
633
634 if (for_eh)
635 {
636 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
637 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
638 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
639 "FDE initial location");
640 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
641 end, begin, "FDE address range");
642 }
643 else
644 {
645 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
646 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
647 }
648
649 if (augmentation[0])
650 {
651 if (any_lsda_needed)
652 {
653 int size = size_of_encoded_value (lsda_encoding);
654
655 if (lsda_encoding == DW_EH_PE_aligned)
656 {
657 int offset = ( 4 /* Length */
658 + 4 /* CIE offset */
659 + 2 * size_of_encoded_value (fde_encoding)
660 + 1 /* Augmentation size */ );
661 int pad = -offset & (PTR_SIZE - 1);
662
663 size += pad;
664 gcc_assert (size_of_uleb128 (size) == 1);
665 }
666
667 dw2_asm_output_data_uleb128 (size, "Augmentation size");
668
669 if (fde->uses_eh_lsda)
670 {
671 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
672 fde->funcdef_number);
673 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
674 gen_rtx_SYMBOL_REF (Pmode, l1),
675 false,
676 "Language Specific Data Area");
677 }
678 else
679 {
680 if (lsda_encoding == DW_EH_PE_aligned)
681 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
682 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
683 "Language Specific Data Area (none)");
684 }
685 }
686 else
687 dw2_asm_output_data_uleb128 (0, "Augmentation size");
688 }
689
690 /* Loop through the Call Frame Instructions associated with this FDE. */
691 fde->dw_fde_current_label = begin;
692 {
693 size_t from, until, i;
694
695 from = 0;
696 until = vec_safe_length (fde->dw_fde_cfi);
697
698 if (fde->dw_fde_second_begin == NULL)
699 ;
700 else if (!second)
701 until = fde->dw_fde_switch_cfi_index;
702 else
703 from = fde->dw_fde_switch_cfi_index;
704
705 for (i = from; i < until; i++)
706 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
707 }
708
709 /* If we are to emit a ref/link from function bodies to their frame tables,
710 do it now. This is typically performed to make sure that tables
711 associated with functions are dragged with them and not discarded in
712 garbage collecting links. We need to do this on a per function basis to
713 cope with -ffunction-sections. */
714
715 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
716 /* Switch to the function section, emit the ref to the tables, and
717 switch *back* into the table section. */
718 switch_to_section (function_section (fde->decl));
719 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
720 switch_to_frame_table_section (for_eh, true);
721 #endif
722
723 /* Pad the FDE out to an address sized boundary. */
724 ASM_OUTPUT_ALIGN (asm_out_file,
725 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
726 ASM_OUTPUT_LABEL (asm_out_file, l2);
727
728 j += 2;
729 }
730
731 /* Return true if frame description entry FDE is needed for EH. */
732
733 static bool
734 fde_needed_for_eh_p (dw_fde_ref fde)
735 {
736 if (flag_asynchronous_unwind_tables)
737 return true;
738
739 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
740 return true;
741
742 if (fde->uses_eh_lsda)
743 return true;
744
745 /* If exceptions are enabled, we have collected nothrow info. */
746 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
747 return false;
748
749 return true;
750 }
751
752 /* Output the call frame information used to record information
753 that relates to calculating the frame pointer, and records the
754 location of saved registers. */
755
756 static void
757 output_call_frame_info (int for_eh)
758 {
759 unsigned int i;
760 dw_fde_ref fde;
761 dw_cfi_ref cfi;
762 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
763 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
764 bool any_lsda_needed = false;
765 char augmentation[6];
766 int augmentation_size;
767 int fde_encoding = DW_EH_PE_absptr;
768 int per_encoding = DW_EH_PE_absptr;
769 int lsda_encoding = DW_EH_PE_absptr;
770 int return_reg;
771 rtx personality = NULL;
772 int dw_cie_version;
773
774 /* Don't emit a CIE if there won't be any FDEs. */
775 if (!fde_vec)
776 return;
777
778 /* Nothing to do if the assembler's doing it all. */
779 if (dwarf2out_do_cfi_asm ())
780 return;
781
782 /* If we don't have any functions we'll want to unwind out of, don't emit
783 any EH unwind information. If we make FDEs linkonce, we may have to
784 emit an empty label for an FDE that wouldn't otherwise be emitted. We
785 want to avoid having an FDE kept around when the function it refers to
786 is discarded. Example where this matters: a primary function template
787 in C++ requires EH information, an explicit specialization doesn't. */
788 if (for_eh)
789 {
790 bool any_eh_needed = false;
791
792 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
793 {
794 if (fde->uses_eh_lsda)
795 any_eh_needed = any_lsda_needed = true;
796 else if (fde_needed_for_eh_p (fde))
797 any_eh_needed = true;
798 else if (TARGET_USES_WEAK_UNWIND_INFO)
799 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
800 }
801
802 if (!any_eh_needed)
803 return;
804 }
805
806 /* We're going to be generating comments, so turn on app. */
807 if (flag_debug_asm)
808 app_enable ();
809
810 /* Switch to the proper frame section, first time. */
811 switch_to_frame_table_section (for_eh, false);
812
813 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
814 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
815
816 /* Output the CIE. */
817 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
818 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
819 if (!XCOFF_DEBUGGING_INFO || for_eh)
820 {
821 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
822 dw2_asm_output_data (4, 0xffffffff,
823 "Initial length escape value indicating 64-bit DWARF extension");
824 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
825 "Length of Common Information Entry");
826 }
827 ASM_OUTPUT_LABEL (asm_out_file, l1);
828
829 /* Now that the CIE pointer is PC-relative for EH,
830 use 0 to identify the CIE. */
831 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
832 (for_eh ? 0 : DWARF_CIE_ID),
833 "CIE Identifier Tag");
834
835 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
836 use CIE version 1, unless that would produce incorrect results
837 due to overflowing the return register column. */
838 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
839 dw_cie_version = 1;
840 if (return_reg >= 256 || dwarf_version > 2)
841 dw_cie_version = 3;
842 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
843
844 augmentation[0] = 0;
845 augmentation_size = 0;
846
847 personality = current_unit_personality;
848 if (for_eh)
849 {
850 char *p;
851
852 /* Augmentation:
853 z Indicates that a uleb128 is present to size the
854 augmentation section.
855 L Indicates the encoding (and thus presence) of
856 an LSDA pointer in the FDE augmentation.
857 R Indicates a non-default pointer encoding for
858 FDE code pointers.
859 P Indicates the presence of an encoding + language
860 personality routine in the CIE augmentation. */
861
862 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
863 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
864 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
865
866 p = augmentation + 1;
867 if (personality)
868 {
869 *p++ = 'P';
870 augmentation_size += 1 + size_of_encoded_value (per_encoding);
871 assemble_external_libcall (personality);
872 }
873 if (any_lsda_needed)
874 {
875 *p++ = 'L';
876 augmentation_size += 1;
877 }
878 if (fde_encoding != DW_EH_PE_absptr)
879 {
880 *p++ = 'R';
881 augmentation_size += 1;
882 }
883 if (p > augmentation + 1)
884 {
885 augmentation[0] = 'z';
886 *p = '\0';
887 }
888
889 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
890 if (personality && per_encoding == DW_EH_PE_aligned)
891 {
892 int offset = ( 4 /* Length */
893 + 4 /* CIE Id */
894 + 1 /* CIE version */
895 + strlen (augmentation) + 1 /* Augmentation */
896 + size_of_uleb128 (1) /* Code alignment */
897 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
898 + 1 /* RA column */
899 + 1 /* Augmentation size */
900 + 1 /* Personality encoding */ );
901 int pad = -offset & (PTR_SIZE - 1);
902
903 augmentation_size += pad;
904
905 /* Augmentations should be small, so there's scarce need to
906 iterate for a solution. Die if we exceed one uleb128 byte. */
907 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
908 }
909 }
910
911 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
912 if (dw_cie_version >= 4)
913 {
914 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
915 dw2_asm_output_data (1, 0, "CIE Segment Size");
916 }
917 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
918 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
919 "CIE Data Alignment Factor");
920
921 if (dw_cie_version == 1)
922 dw2_asm_output_data (1, return_reg, "CIE RA Column");
923 else
924 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
925
926 if (augmentation[0])
927 {
928 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
929 if (personality)
930 {
931 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
932 eh_data_format_name (per_encoding));
933 dw2_asm_output_encoded_addr_rtx (per_encoding,
934 personality,
935 true, NULL);
936 }
937
938 if (any_lsda_needed)
939 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
940 eh_data_format_name (lsda_encoding));
941
942 if (fde_encoding != DW_EH_PE_absptr)
943 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
944 eh_data_format_name (fde_encoding));
945 }
946
947 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
948 output_cfi (cfi, NULL, for_eh);
949
950 /* Pad the CIE out to an address sized boundary. */
951 ASM_OUTPUT_ALIGN (asm_out_file,
952 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
953 ASM_OUTPUT_LABEL (asm_out_file, l2);
954
955 /* Loop through all of the FDE's. */
956 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
957 {
958 unsigned int k;
959
960 /* Don't emit EH unwind info for leaf functions that don't need it. */
961 if (for_eh && !fde_needed_for_eh_p (fde))
962 continue;
963
964 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
965 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
966 augmentation, any_lsda_needed, lsda_encoding);
967 }
968
969 if (for_eh && targetm.terminate_dw2_eh_frame_info)
970 dw2_asm_output_data (4, 0, "End of Table");
971
972 /* Turn off app to make assembly quicker. */
973 if (flag_debug_asm)
974 app_disable ();
975 }
976
977 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
978
979 static void
980 dwarf2out_do_cfi_startproc (bool second)
981 {
982 int enc;
983 rtx ref;
984
985 fprintf (asm_out_file, "\t.cfi_startproc\n");
986
987 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
988
989 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
990 eh unwinders. */
991 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
992 return;
993
994 rtx personality = get_personality_function (current_function_decl);
995
996 if (personality)
997 {
998 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
999 ref = personality;
1000
1001 /* ??? The GAS support isn't entirely consistent. We have to
1002 handle indirect support ourselves, but PC-relative is done
1003 in the assembler. Further, the assembler can't handle any
1004 of the weirder relocation types. */
1005 if (enc & DW_EH_PE_indirect)
1006 {
1007 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1008 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1009 else
1010 ref = dw2_force_const_mem (ref, true);
1011 }
1012
1013 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1014 output_addr_const (asm_out_file, ref);
1015 fputc ('\n', asm_out_file);
1016 }
1017
1018 if (crtl->uses_eh_lsda)
1019 {
1020 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1021
1022 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1023 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1024 current_function_funcdef_no);
1025 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1026 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1027
1028 if (enc & DW_EH_PE_indirect)
1029 {
1030 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1031 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1032 else
1033 ref = dw2_force_const_mem (ref, true);
1034 }
1035
1036 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1037 output_addr_const (asm_out_file, ref);
1038 fputc ('\n', asm_out_file);
1039 }
1040 }
1041
1042 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1043 this allocation may be done before pass_final. */
1044
1045 dw_fde_ref
1046 dwarf2out_alloc_current_fde (void)
1047 {
1048 dw_fde_ref fde;
1049
1050 fde = ggc_cleared_alloc<dw_fde_node> ();
1051 fde->decl = current_function_decl;
1052 fde->funcdef_number = current_function_funcdef_no;
1053 fde->fde_index = vec_safe_length (fde_vec);
1054 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1055 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1056 fde->nothrow = crtl->nothrow;
1057 fde->drap_reg = INVALID_REGNUM;
1058 fde->vdrap_reg = INVALID_REGNUM;
1059
1060 /* Record the FDE associated with this function. */
1061 cfun->fde = fde;
1062 vec_safe_push (fde_vec, fde);
1063
1064 return fde;
1065 }
1066
1067 /* Output a marker (i.e. a label) for the beginning of a function, before
1068 the prologue. */
1069
1070 void
1071 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1072 unsigned int column ATTRIBUTE_UNUSED,
1073 const char *file ATTRIBUTE_UNUSED)
1074 {
1075 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1076 char * dup_label;
1077 dw_fde_ref fde;
1078 section *fnsec;
1079 bool do_frame;
1080
1081 current_function_func_begin_label = NULL;
1082
1083 do_frame = dwarf2out_do_frame ();
1084
1085 /* ??? current_function_func_begin_label is also used by except.cc for
1086 call-site information. We must emit this label if it might be used. */
1087 if (!do_frame
1088 && (!flag_exceptions
1089 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1090 return;
1091
1092 fnsec = function_section (current_function_decl);
1093 switch_to_section (fnsec);
1094 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1095 current_function_funcdef_no);
1096 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1097 current_function_funcdef_no);
1098 dup_label = xstrdup (label);
1099 current_function_func_begin_label = dup_label;
1100
1101 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1102 if (!do_frame)
1103 return;
1104
1105 /* Unlike the debug version, the EH version of frame unwind info is a per-
1106 function setting so we need to record whether we need it for the unit. */
1107 do_eh_frame |= dwarf2out_do_eh_frame ();
1108
1109 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1110 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1111 would include pass_dwarf2_frame. If we've not created the FDE yet,
1112 do so now. */
1113 fde = cfun->fde;
1114 if (fde == NULL)
1115 fde = dwarf2out_alloc_current_fde ();
1116
1117 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1118 fde->dw_fde_begin = dup_label;
1119 fde->dw_fde_current_label = dup_label;
1120 fde->in_std_section = (fnsec == text_section
1121 || (cold_text_section && fnsec == cold_text_section));
1122 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1123 in_text_section_p = fnsec == text_section;
1124
1125 /* We only want to output line number information for the genuine dwarf2
1126 prologue case, not the eh frame case. */
1127 #ifdef DWARF2_DEBUGGING_INFO
1128 if (file)
1129 dwarf2out_source_line (line, column, file, 0, true);
1130 #endif
1131
1132 if (dwarf2out_do_cfi_asm ())
1133 dwarf2out_do_cfi_startproc (false);
1134 else
1135 {
1136 rtx personality = get_personality_function (current_function_decl);
1137 if (!current_unit_personality)
1138 current_unit_personality = personality;
1139
1140 /* We cannot keep a current personality per function as without CFI
1141 asm, at the point where we emit the CFI data, there is no current
1142 function anymore. */
1143 if (personality && current_unit_personality != personality)
1144 sorry ("multiple EH personalities are supported only with assemblers "
1145 "supporting %<.cfi_personality%> directive");
1146 }
1147 }
1148
1149 /* Output a marker (i.e. a label) for the end of the generated code
1150 for a function prologue. This gets called *after* the prologue code has
1151 been generated. */
1152
1153 void
1154 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1155 const char *file ATTRIBUTE_UNUSED)
1156 {
1157 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1158
1159 /* Output a label to mark the endpoint of the code generated for this
1160 function. */
1161 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1162 current_function_funcdef_no);
1163 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1164 current_function_funcdef_no);
1165 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1166 }
1167
1168 /* Output a marker (i.e. a label) for the beginning of the generated code
1169 for a function epilogue. This gets called *before* the prologue code has
1170 been generated. */
1171
1172 void
1173 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1174 const char *file ATTRIBUTE_UNUSED)
1175 {
1176 dw_fde_ref fde = cfun->fde;
1177 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1178
1179 if (fde->dw_fde_vms_begin_epilogue)
1180 return;
1181
1182 /* Output a label to mark the endpoint of the code generated for this
1183 function. */
1184 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1185 current_function_funcdef_no);
1186 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1187 current_function_funcdef_no);
1188 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1189 }
1190
1191 /* Mark the ranges of non-debug subsections in the std text sections. */
1192
1193 static void
1194 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1195 {
1196 bool std_section;
1197 const char *begin_label, *end_label;
1198 const char **last_end_label;
1199 vec<const char *, va_gc> **switch_ranges;
1200
1201 if (second)
1202 {
1203 std_section = fde->second_in_std_section;
1204 begin_label = fde->dw_fde_second_begin;
1205 end_label = fde->dw_fde_second_end;
1206 }
1207 else
1208 {
1209 std_section = fde->in_std_section;
1210 begin_label = fde->dw_fde_begin;
1211 end_label = fde->dw_fde_end;
1212 }
1213
1214 if (!std_section)
1215 return;
1216
1217 if (in_text_section_p)
1218 {
1219 last_end_label = &last_text_label;
1220 switch_ranges = &switch_text_ranges;
1221 }
1222 else
1223 {
1224 last_end_label = &last_cold_label;
1225 switch_ranges = &switch_cold_ranges;
1226 }
1227
1228 if (fde->ignored_debug)
1229 {
1230 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1231 vec_safe_push (*switch_ranges, *last_end_label);
1232 }
1233 else
1234 {
1235 *last_end_label = end_label;
1236
1237 if (!*switch_ranges)
1238 vec_alloc (*switch_ranges, 16);
1239 else if (vec_safe_length (*switch_ranges) & 1)
1240 vec_safe_push (*switch_ranges, begin_label);
1241 }
1242 }
1243
1244 /* Output a marker (i.e. a label) for the absolute end of the generated code
1245 for a function definition. This gets called *after* the epilogue code has
1246 been generated. */
1247
1248 void
1249 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1250 const char *file ATTRIBUTE_UNUSED)
1251 {
1252 dw_fde_ref fde;
1253 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1254
1255 last_var_location_insn = NULL;
1256 cached_next_real_insn = NULL;
1257
1258 if (dwarf2out_do_cfi_asm ())
1259 fprintf (asm_out_file, "\t.cfi_endproc\n");
1260
1261 #ifdef CODEVIEW_DEBUGGING_INFO
1262 if (codeview_debuginfo_p ())
1263 codeview_end_epilogue ();
1264 #endif
1265
1266 /* Output a label to mark the endpoint of the code generated for this
1267 function. */
1268 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1269 current_function_funcdef_no);
1270 ASM_OUTPUT_LABEL (asm_out_file, label);
1271 fde = cfun->fde;
1272 gcc_assert (fde != NULL);
1273 if (fde->dw_fde_second_begin == NULL)
1274 fde->dw_fde_end = xstrdup (label);
1275
1276 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1277 }
1278
1279 void
1280 dwarf2out_frame_finish (void)
1281 {
1282 /* Output call frame information. */
1283 if (targetm.debug_unwind_info () == UI_DWARF2)
1284 output_call_frame_info (0);
1285
1286 /* Output another copy for the unwinder. */
1287 if (do_eh_frame)
1288 output_call_frame_info (1);
1289 }
1290
1291 static void var_location_switch_text_section (void);
1292 static void set_cur_line_info_table (section *);
1293
1294 void
1295 dwarf2out_switch_text_section (void)
1296 {
1297 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1298 section *sect;
1299 dw_fde_ref fde = cfun->fde;
1300
1301 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1302
1303 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1304 current_function_funcdef_no);
1305
1306 fde->dw_fde_second_begin = ggc_strdup (label);
1307 if (!in_cold_section_p)
1308 {
1309 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1310 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1311 }
1312 else
1313 {
1314 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1315 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1316 }
1317 have_multiple_function_sections = true;
1318
1319 #ifdef CODEVIEW_DEBUGGING_INFO
1320 if (codeview_debuginfo_p ())
1321 codeview_switch_text_section ();
1322 #endif
1323
1324 if (dwarf2out_do_cfi_asm ())
1325 fprintf (asm_out_file, "\t.cfi_endproc\n");
1326
1327 mark_ignored_debug_section (fde, false);
1328
1329 /* Now do the real section switch. */
1330 sect = current_function_section ();
1331 switch_to_section (sect);
1332
1333 fde->second_in_std_section
1334 = (sect == text_section
1335 || (cold_text_section && sect == cold_text_section));
1336 in_text_section_p = sect == text_section;
1337
1338 if (dwarf2out_do_cfi_asm ())
1339 dwarf2out_do_cfi_startproc (true);
1340
1341 var_location_switch_text_section ();
1342
1343 if (cold_text_section != NULL)
1344 set_cur_line_info_table (sect);
1345 }
1346
1347 /* And now, the subset of the debugging information support code necessary
1348 for emitting location expressions. */
1349
1350 /* Describe an entry into the .debug_addr section. */
1351
1352 enum ate_kind {
1353 ate_kind_rtx,
1354 ate_kind_rtx_dtprel,
1355 ate_kind_label
1356 };
1357
1358 struct GTY((for_user)) addr_table_entry {
1359 enum ate_kind kind;
1360 unsigned int refcount;
1361 unsigned int index;
1362 union addr_table_entry_struct_union
1363 {
1364 rtx GTY ((tag ("0"))) rtl;
1365 char * GTY ((tag ("1"))) label;
1366 }
1367 GTY ((desc ("%1.kind"))) addr;
1368 };
1369
1370 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1371 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1372
1373 /* Convert a DWARF stack opcode into its string name. */
1374
1375 static const char *
1376 dwarf_stack_op_name (unsigned int op)
1377 {
1378 const char *name = get_DW_OP_name (op);
1379
1380 if (name != NULL)
1381 return name;
1382
1383 return "OP_<unknown>";
1384 }
1385
1386 /* Return TRUE iff we're to output location view lists as a separate
1387 attribute next to the location lists, as an extension compatible
1388 with DWARF 2 and above. */
1389
1390 static inline bool
1391 dwarf2out_locviews_in_attribute ()
1392 {
1393 return debug_variable_location_views == 1;
1394 }
1395
1396 /* Return TRUE iff we're to output location view lists as part of the
1397 location lists, as proposed for standardization after DWARF 5. */
1398
1399 static inline bool
1400 dwarf2out_locviews_in_loclist ()
1401 {
1402 #ifndef DW_LLE_view_pair
1403 return false;
1404 #else
1405 return debug_variable_location_views == -1;
1406 #endif
1407 }
1408
1409 /* Return a pointer to a newly allocated location description. Location
1410 descriptions are simple expression terms that can be strung
1411 together to form more complicated location (address) descriptions. */
1412
1413 static inline dw_loc_descr_ref
1414 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1415 unsigned HOST_WIDE_INT oprnd2)
1416 {
1417 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1418
1419 descr->dw_loc_opc = op;
1420 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1421 descr->dw_loc_oprnd1.val_entry = NULL;
1422 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1423 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1424 descr->dw_loc_oprnd2.val_entry = NULL;
1425 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1426
1427 return descr;
1428 }
1429
1430 /* Add a location description term to a location description expression. */
1431
1432 static inline void
1433 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1434 {
1435 dw_loc_descr_ref *d;
1436
1437 /* Find the end of the chain. */
1438 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1439 ;
1440
1441 *d = descr;
1442 }
1443
1444 /* Compare two location operands for exact equality. */
1445
1446 static bool
1447 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1448 {
1449 if (a->val_class != b->val_class)
1450 return false;
1451 switch (a->val_class)
1452 {
1453 case dw_val_class_none:
1454 return true;
1455 case dw_val_class_addr:
1456 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1457
1458 case dw_val_class_offset:
1459 case dw_val_class_unsigned_const:
1460 case dw_val_class_const:
1461 case dw_val_class_unsigned_const_implicit:
1462 case dw_val_class_const_implicit:
1463 case dw_val_class_range_list:
1464 /* These are all HOST_WIDE_INT, signed or unsigned. */
1465 return a->v.val_unsigned == b->v.val_unsigned;
1466
1467 case dw_val_class_loc:
1468 return a->v.val_loc == b->v.val_loc;
1469 case dw_val_class_loc_list:
1470 return a->v.val_loc_list == b->v.val_loc_list;
1471 case dw_val_class_view_list:
1472 return a->v.val_view_list == b->v.val_view_list;
1473 case dw_val_class_die_ref:
1474 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1475 case dw_val_class_fde_ref:
1476 return a->v.val_fde_index == b->v.val_fde_index;
1477 case dw_val_class_symview:
1478 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1479 case dw_val_class_lbl_id:
1480 case dw_val_class_lineptr:
1481 case dw_val_class_macptr:
1482 case dw_val_class_loclistsptr:
1483 case dw_val_class_high_pc:
1484 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1485 case dw_val_class_str:
1486 return a->v.val_str == b->v.val_str;
1487 case dw_val_class_flag:
1488 return a->v.val_flag == b->v.val_flag;
1489 case dw_val_class_file:
1490 case dw_val_class_file_implicit:
1491 return a->v.val_file == b->v.val_file;
1492 case dw_val_class_decl_ref:
1493 return a->v.val_decl_ref == b->v.val_decl_ref;
1494
1495 case dw_val_class_const_double:
1496 return (a->v.val_double.high == b->v.val_double.high
1497 && a->v.val_double.low == b->v.val_double.low);
1498
1499 case dw_val_class_wide_int:
1500 return *a->v.val_wide == *b->v.val_wide;
1501
1502 case dw_val_class_vec:
1503 {
1504 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1505 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1506
1507 return (a_len == b_len
1508 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1509 }
1510
1511 case dw_val_class_data8:
1512 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1513
1514 case dw_val_class_vms_delta:
1515 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1516 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1517
1518 case dw_val_class_discr_value:
1519 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1520 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1521 case dw_val_class_discr_list:
1522 /* It makes no sense comparing two discriminant value lists. */
1523 return false;
1524 }
1525 gcc_unreachable ();
1526 }
1527
1528 /* Compare two location atoms for exact equality. */
1529
1530 static bool
1531 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1532 {
1533 if (a->dw_loc_opc != b->dw_loc_opc)
1534 return false;
1535
1536 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1537 address size, but since we always allocate cleared storage it
1538 should be zero for other types of locations. */
1539 if (a->dtprel != b->dtprel)
1540 return false;
1541
1542 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1543 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1544 }
1545
1546 /* Compare two complete location expressions for exact equality. */
1547
1548 bool
1549 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1550 {
1551 while (1)
1552 {
1553 if (a == b)
1554 return true;
1555 if (a == NULL || b == NULL)
1556 return false;
1557 if (!loc_descr_equal_p_1 (a, b))
1558 return false;
1559
1560 a = a->dw_loc_next;
1561 b = b->dw_loc_next;
1562 }
1563 }
1564
1565
1566 /* Add a constant POLY_OFFSET to a location expression. */
1567
1568 static void
1569 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1570 {
1571 dw_loc_descr_ref loc;
1572 HOST_WIDE_INT *p;
1573
1574 gcc_assert (*list_head != NULL);
1575
1576 if (known_eq (poly_offset, 0))
1577 return;
1578
1579 /* Find the end of the chain. */
1580 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1581 ;
1582
1583 HOST_WIDE_INT offset;
1584 if (!poly_offset.is_constant (&offset))
1585 {
1586 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1587 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1588 return;
1589 }
1590
1591 p = NULL;
1592 if (loc->dw_loc_opc == DW_OP_fbreg
1593 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1594 p = &loc->dw_loc_oprnd1.v.val_int;
1595 else if (loc->dw_loc_opc == DW_OP_bregx)
1596 p = &loc->dw_loc_oprnd2.v.val_int;
1597
1598 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1599 offset. Don't optimize if an signed integer overflow would happen. */
1600 if (p != NULL
1601 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1602 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1603 *p += offset;
1604
1605 else if (offset > 0)
1606 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1607
1608 else
1609 {
1610 loc->dw_loc_next
1611 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1612 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1613 }
1614 }
1615
1616 /* Return a pointer to a newly allocated location description for
1617 REG and OFFSET. */
1618
1619 static inline dw_loc_descr_ref
1620 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1621 {
1622 HOST_WIDE_INT const_offset;
1623 if (offset.is_constant (&const_offset))
1624 {
1625 if (reg <= 31)
1626 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1627 const_offset, 0);
1628 else
1629 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1630 }
1631 else
1632 {
1633 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1634 loc_descr_plus_const (&ret, offset);
1635 return ret;
1636 }
1637 }
1638
1639 /* Add a constant OFFSET to a location list. */
1640
1641 static void
1642 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1643 {
1644 dw_loc_list_ref d;
1645 for (d = list_head; d != NULL; d = d->dw_loc_next)
1646 loc_descr_plus_const (&d->expr, offset);
1647 }
1648
1649 #define DWARF_REF_SIZE \
1650 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1651
1652 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1653 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1654 DW_FORM_data16 with 128 bits. */
1655 #define DWARF_LARGEST_DATA_FORM_BITS \
1656 (dwarf_version >= 5 ? 128 : 64)
1657
1658 /* Utility inline function for construction of ops that were GNU extension
1659 before DWARF 5. */
1660 static inline enum dwarf_location_atom
1661 dwarf_OP (enum dwarf_location_atom op)
1662 {
1663 switch (op)
1664 {
1665 case DW_OP_implicit_pointer:
1666 if (dwarf_version < 5)
1667 return DW_OP_GNU_implicit_pointer;
1668 break;
1669
1670 case DW_OP_entry_value:
1671 if (dwarf_version < 5)
1672 return DW_OP_GNU_entry_value;
1673 break;
1674
1675 case DW_OP_const_type:
1676 if (dwarf_version < 5)
1677 return DW_OP_GNU_const_type;
1678 break;
1679
1680 case DW_OP_regval_type:
1681 if (dwarf_version < 5)
1682 return DW_OP_GNU_regval_type;
1683 break;
1684
1685 case DW_OP_deref_type:
1686 if (dwarf_version < 5)
1687 return DW_OP_GNU_deref_type;
1688 break;
1689
1690 case DW_OP_convert:
1691 if (dwarf_version < 5)
1692 return DW_OP_GNU_convert;
1693 break;
1694
1695 case DW_OP_reinterpret:
1696 if (dwarf_version < 5)
1697 return DW_OP_GNU_reinterpret;
1698 break;
1699
1700 case DW_OP_addrx:
1701 if (dwarf_version < 5)
1702 return DW_OP_GNU_addr_index;
1703 break;
1704
1705 case DW_OP_constx:
1706 if (dwarf_version < 5)
1707 return DW_OP_GNU_const_index;
1708 break;
1709
1710 default:
1711 break;
1712 }
1713 return op;
1714 }
1715
1716 /* Similarly for attributes. */
1717 static inline enum dwarf_attribute
1718 dwarf_AT (enum dwarf_attribute at)
1719 {
1720 switch (at)
1721 {
1722 case DW_AT_call_return_pc:
1723 if (dwarf_version < 5)
1724 return DW_AT_low_pc;
1725 break;
1726
1727 case DW_AT_call_tail_call:
1728 if (dwarf_version < 5)
1729 return DW_AT_GNU_tail_call;
1730 break;
1731
1732 case DW_AT_call_origin:
1733 if (dwarf_version < 5)
1734 return DW_AT_abstract_origin;
1735 break;
1736
1737 case DW_AT_call_target:
1738 if (dwarf_version < 5)
1739 return DW_AT_GNU_call_site_target;
1740 break;
1741
1742 case DW_AT_call_target_clobbered:
1743 if (dwarf_version < 5)
1744 return DW_AT_GNU_call_site_target_clobbered;
1745 break;
1746
1747 case DW_AT_call_parameter:
1748 if (dwarf_version < 5)
1749 return DW_AT_abstract_origin;
1750 break;
1751
1752 case DW_AT_call_value:
1753 if (dwarf_version < 5)
1754 return DW_AT_GNU_call_site_value;
1755 break;
1756
1757 case DW_AT_call_data_value:
1758 if (dwarf_version < 5)
1759 return DW_AT_GNU_call_site_data_value;
1760 break;
1761
1762 case DW_AT_call_all_calls:
1763 if (dwarf_version < 5)
1764 return DW_AT_GNU_all_call_sites;
1765 break;
1766
1767 case DW_AT_call_all_tail_calls:
1768 if (dwarf_version < 5)
1769 return DW_AT_GNU_all_tail_call_sites;
1770 break;
1771
1772 case DW_AT_dwo_name:
1773 if (dwarf_version < 5)
1774 return DW_AT_GNU_dwo_name;
1775 break;
1776
1777 case DW_AT_addr_base:
1778 if (dwarf_version < 5)
1779 return DW_AT_GNU_addr_base;
1780 break;
1781
1782 default:
1783 break;
1784 }
1785 return at;
1786 }
1787
1788 /* And similarly for tags. */
1789 static inline enum dwarf_tag
1790 dwarf_TAG (enum dwarf_tag tag)
1791 {
1792 switch (tag)
1793 {
1794 case DW_TAG_call_site:
1795 if (dwarf_version < 5)
1796 return DW_TAG_GNU_call_site;
1797 break;
1798
1799 case DW_TAG_call_site_parameter:
1800 if (dwarf_version < 5)
1801 return DW_TAG_GNU_call_site_parameter;
1802 break;
1803
1804 default:
1805 break;
1806 }
1807 return tag;
1808 }
1809
1810 /* And similarly for forms. */
1811 static inline enum dwarf_form
1812 dwarf_FORM (enum dwarf_form form)
1813 {
1814 switch (form)
1815 {
1816 case DW_FORM_addrx:
1817 if (dwarf_version < 5)
1818 return DW_FORM_GNU_addr_index;
1819 break;
1820
1821 case DW_FORM_strx:
1822 if (dwarf_version < 5)
1823 return DW_FORM_GNU_str_index;
1824 break;
1825
1826 default:
1827 break;
1828 }
1829 return form;
1830 }
1831
1832 static unsigned long int get_base_type_offset (dw_die_ref);
1833
1834 /* Return the size of a location descriptor. */
1835
1836 static unsigned long
1837 size_of_loc_descr (dw_loc_descr_ref loc)
1838 {
1839 unsigned long size = 1;
1840
1841 switch (loc->dw_loc_opc)
1842 {
1843 case DW_OP_addr:
1844 size += DWARF2_ADDR_SIZE;
1845 break;
1846 case DW_OP_GNU_addr_index:
1847 case DW_OP_addrx:
1848 case DW_OP_GNU_const_index:
1849 case DW_OP_constx:
1850 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1851 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1852 break;
1853 case DW_OP_const1u:
1854 case DW_OP_const1s:
1855 size += 1;
1856 break;
1857 case DW_OP_const2u:
1858 case DW_OP_const2s:
1859 size += 2;
1860 break;
1861 case DW_OP_const4u:
1862 case DW_OP_const4s:
1863 size += 4;
1864 break;
1865 case DW_OP_const8u:
1866 case DW_OP_const8s:
1867 size += 8;
1868 break;
1869 case DW_OP_constu:
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1871 break;
1872 case DW_OP_consts:
1873 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1874 break;
1875 case DW_OP_pick:
1876 size += 1;
1877 break;
1878 case DW_OP_plus_uconst:
1879 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1880 break;
1881 case DW_OP_skip:
1882 case DW_OP_bra:
1883 size += 2;
1884 break;
1885 case DW_OP_breg0:
1886 case DW_OP_breg1:
1887 case DW_OP_breg2:
1888 case DW_OP_breg3:
1889 case DW_OP_breg4:
1890 case DW_OP_breg5:
1891 case DW_OP_breg6:
1892 case DW_OP_breg7:
1893 case DW_OP_breg8:
1894 case DW_OP_breg9:
1895 case DW_OP_breg10:
1896 case DW_OP_breg11:
1897 case DW_OP_breg12:
1898 case DW_OP_breg13:
1899 case DW_OP_breg14:
1900 case DW_OP_breg15:
1901 case DW_OP_breg16:
1902 case DW_OP_breg17:
1903 case DW_OP_breg18:
1904 case DW_OP_breg19:
1905 case DW_OP_breg20:
1906 case DW_OP_breg21:
1907 case DW_OP_breg22:
1908 case DW_OP_breg23:
1909 case DW_OP_breg24:
1910 case DW_OP_breg25:
1911 case DW_OP_breg26:
1912 case DW_OP_breg27:
1913 case DW_OP_breg28:
1914 case DW_OP_breg29:
1915 case DW_OP_breg30:
1916 case DW_OP_breg31:
1917 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1918 break;
1919 case DW_OP_regx:
1920 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1921 break;
1922 case DW_OP_fbreg:
1923 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1924 break;
1925 case DW_OP_bregx:
1926 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1927 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1928 break;
1929 case DW_OP_piece:
1930 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1931 break;
1932 case DW_OP_bit_piece:
1933 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1934 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1935 break;
1936 case DW_OP_deref_size:
1937 case DW_OP_xderef_size:
1938 size += 1;
1939 break;
1940 case DW_OP_call2:
1941 size += 2;
1942 break;
1943 case DW_OP_call4:
1944 size += 4;
1945 break;
1946 case DW_OP_call_ref:
1947 case DW_OP_GNU_variable_value:
1948 size += DWARF_REF_SIZE;
1949 break;
1950 case DW_OP_implicit_value:
1951 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1952 + loc->dw_loc_oprnd1.v.val_unsigned;
1953 break;
1954 case DW_OP_implicit_pointer:
1955 case DW_OP_GNU_implicit_pointer:
1956 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1957 break;
1958 case DW_OP_entry_value:
1959 case DW_OP_GNU_entry_value:
1960 {
1961 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1962 size += size_of_uleb128 (op_size) + op_size;
1963 break;
1964 }
1965 case DW_OP_const_type:
1966 case DW_OP_GNU_const_type:
1967 {
1968 unsigned long o
1969 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1970 size += size_of_uleb128 (o) + 1;
1971 switch (loc->dw_loc_oprnd2.val_class)
1972 {
1973 case dw_val_class_vec:
1974 size += loc->dw_loc_oprnd2.v.val_vec.length
1975 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1976 break;
1977 case dw_val_class_const:
1978 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1979 break;
1980 case dw_val_class_const_double:
1981 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1982 break;
1983 case dw_val_class_wide_int:
1984 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1985 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1986 break;
1987 default:
1988 gcc_unreachable ();
1989 }
1990 break;
1991 }
1992 case DW_OP_regval_type:
1993 case DW_OP_GNU_regval_type:
1994 {
1995 unsigned long o
1996 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1997 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1998 + size_of_uleb128 (o);
1999 }
2000 break;
2001 case DW_OP_deref_type:
2002 case DW_OP_GNU_deref_type:
2003 {
2004 unsigned long o
2005 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2006 size += 1 + size_of_uleb128 (o);
2007 }
2008 break;
2009 case DW_OP_convert:
2010 case DW_OP_reinterpret:
2011 case DW_OP_GNU_convert:
2012 case DW_OP_GNU_reinterpret:
2013 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2014 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2015 else
2016 {
2017 unsigned long o
2018 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2019 size += size_of_uleb128 (o);
2020 }
2021 break;
2022 case DW_OP_GNU_parameter_ref:
2023 size += 4;
2024 break;
2025 default:
2026 break;
2027 }
2028
2029 return size;
2030 }
2031
2032 /* Return the size of a series of location descriptors. */
2033
2034 unsigned long
2035 size_of_locs (dw_loc_descr_ref loc)
2036 {
2037 dw_loc_descr_ref l;
2038 unsigned long size;
2039
2040 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2041 field, to avoid writing to a PCH file. */
2042 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2043 {
2044 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2045 break;
2046 size += size_of_loc_descr (l);
2047 }
2048 if (! l)
2049 return size;
2050
2051 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2052 {
2053 l->dw_loc_addr = size;
2054 size += size_of_loc_descr (l);
2055 }
2056
2057 return size;
2058 }
2059
2060 /* Return the size of the value in a DW_AT_discr_value attribute. */
2061
2062 static int
2063 size_of_discr_value (dw_discr_value *discr_value)
2064 {
2065 if (discr_value->pos)
2066 return size_of_uleb128 (discr_value->v.uval);
2067 else
2068 return size_of_sleb128 (discr_value->v.sval);
2069 }
2070
2071 /* Return the size of the value in a DW_AT_discr_list attribute. */
2072
2073 static int
2074 size_of_discr_list (dw_discr_list_ref discr_list)
2075 {
2076 int size = 0;
2077
2078 for (dw_discr_list_ref list = discr_list;
2079 list != NULL;
2080 list = list->dw_discr_next)
2081 {
2082 /* One byte for the discriminant value descriptor, and then one or two
2083 LEB128 numbers, depending on whether it's a single case label or a
2084 range label. */
2085 size += 1;
2086 size += size_of_discr_value (&list->dw_discr_lower_bound);
2087 if (list->dw_discr_range != 0)
2088 size += size_of_discr_value (&list->dw_discr_upper_bound);
2089 }
2090 return size;
2091 }
2092
2093 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2094 static void get_ref_die_offset_label (char *, dw_die_ref);
2095 static unsigned long int get_ref_die_offset (dw_die_ref);
2096
2097 /* Output location description stack opcode's operands (if any).
2098 The for_eh_or_skip parameter controls whether register numbers are
2099 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2100 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2101 info). This should be suppressed for the cases that have not been converted
2102 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2103
2104 static void
2105 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2106 {
2107 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2108 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2109
2110 switch (loc->dw_loc_opc)
2111 {
2112 #ifdef DWARF2_DEBUGGING_INFO
2113 case DW_OP_const2u:
2114 case DW_OP_const2s:
2115 dw2_asm_output_data (2, val1->v.val_int, NULL);
2116 break;
2117 case DW_OP_const4u:
2118 if (loc->dtprel)
2119 {
2120 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2121 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2122 val1->v.val_addr);
2123 fputc ('\n', asm_out_file);
2124 break;
2125 }
2126 /* FALLTHRU */
2127 case DW_OP_const4s:
2128 dw2_asm_output_data (4, val1->v.val_int, NULL);
2129 break;
2130 case DW_OP_const8u:
2131 if (loc->dtprel)
2132 {
2133 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2134 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2135 val1->v.val_addr);
2136 fputc ('\n', asm_out_file);
2137 break;
2138 }
2139 /* FALLTHRU */
2140 case DW_OP_const8s:
2141 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2142 dw2_asm_output_data (8, val1->v.val_int, NULL);
2143 break;
2144 case DW_OP_skip:
2145 case DW_OP_bra:
2146 {
2147 int offset;
2148
2149 gcc_assert (val1->val_class == dw_val_class_loc);
2150 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2151
2152 dw2_asm_output_data (2, offset, NULL);
2153 }
2154 break;
2155 case DW_OP_implicit_value:
2156 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2157 switch (val2->val_class)
2158 {
2159 case dw_val_class_const:
2160 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2161 break;
2162 case dw_val_class_vec:
2163 {
2164 unsigned int elt_size = val2->v.val_vec.elt_size;
2165 unsigned int len = val2->v.val_vec.length;
2166 unsigned int i;
2167 unsigned char *p;
2168
2169 if (elt_size > sizeof (HOST_WIDE_INT))
2170 {
2171 elt_size /= 2;
2172 len *= 2;
2173 }
2174 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2175 i < len;
2176 i++, p += elt_size)
2177 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2178 "fp or vector constant word %u", i);
2179 }
2180 break;
2181 case dw_val_class_const_double:
2182 {
2183 unsigned HOST_WIDE_INT first, second;
2184
2185 if (WORDS_BIG_ENDIAN)
2186 {
2187 first = val2->v.val_double.high;
2188 second = val2->v.val_double.low;
2189 }
2190 else
2191 {
2192 first = val2->v.val_double.low;
2193 second = val2->v.val_double.high;
2194 }
2195 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2196 first, NULL);
2197 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2198 second, NULL);
2199 }
2200 break;
2201 case dw_val_class_wide_int:
2202 {
2203 int i;
2204 int len = get_full_len (*val2->v.val_wide);
2205 if (WORDS_BIG_ENDIAN)
2206 for (i = len - 1; i >= 0; --i)
2207 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2208 val2->v.val_wide->elt (i), NULL);
2209 else
2210 for (i = 0; i < len; ++i)
2211 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2212 val2->v.val_wide->elt (i), NULL);
2213 }
2214 break;
2215 case dw_val_class_addr:
2216 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2217 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2218 break;
2219 default:
2220 gcc_unreachable ();
2221 }
2222 break;
2223 #else
2224 case DW_OP_const2u:
2225 case DW_OP_const2s:
2226 case DW_OP_const4u:
2227 case DW_OP_const4s:
2228 case DW_OP_const8u:
2229 case DW_OP_const8s:
2230 case DW_OP_skip:
2231 case DW_OP_bra:
2232 case DW_OP_implicit_value:
2233 /* We currently don't make any attempt to make sure these are
2234 aligned properly like we do for the main unwind info, so
2235 don't support emitting things larger than a byte if we're
2236 only doing unwinding. */
2237 gcc_unreachable ();
2238 #endif
2239 case DW_OP_const1u:
2240 case DW_OP_const1s:
2241 dw2_asm_output_data (1, val1->v.val_int, NULL);
2242 break;
2243 case DW_OP_constu:
2244 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2245 break;
2246 case DW_OP_consts:
2247 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2248 break;
2249 case DW_OP_pick:
2250 dw2_asm_output_data (1, val1->v.val_int, NULL);
2251 break;
2252 case DW_OP_plus_uconst:
2253 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2254 break;
2255 case DW_OP_breg0:
2256 case DW_OP_breg1:
2257 case DW_OP_breg2:
2258 case DW_OP_breg3:
2259 case DW_OP_breg4:
2260 case DW_OP_breg5:
2261 case DW_OP_breg6:
2262 case DW_OP_breg7:
2263 case DW_OP_breg8:
2264 case DW_OP_breg9:
2265 case DW_OP_breg10:
2266 case DW_OP_breg11:
2267 case DW_OP_breg12:
2268 case DW_OP_breg13:
2269 case DW_OP_breg14:
2270 case DW_OP_breg15:
2271 case DW_OP_breg16:
2272 case DW_OP_breg17:
2273 case DW_OP_breg18:
2274 case DW_OP_breg19:
2275 case DW_OP_breg20:
2276 case DW_OP_breg21:
2277 case DW_OP_breg22:
2278 case DW_OP_breg23:
2279 case DW_OP_breg24:
2280 case DW_OP_breg25:
2281 case DW_OP_breg26:
2282 case DW_OP_breg27:
2283 case DW_OP_breg28:
2284 case DW_OP_breg29:
2285 case DW_OP_breg30:
2286 case DW_OP_breg31:
2287 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2288 break;
2289 case DW_OP_regx:
2290 {
2291 unsigned r = val1->v.val_unsigned;
2292 if (for_eh_or_skip >= 0)
2293 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2294 gcc_assert (size_of_uleb128 (r)
2295 == size_of_uleb128 (val1->v.val_unsigned));
2296 dw2_asm_output_data_uleb128 (r, NULL);
2297 }
2298 break;
2299 case DW_OP_fbreg:
2300 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2301 break;
2302 case DW_OP_bregx:
2303 {
2304 unsigned r = val1->v.val_unsigned;
2305 if (for_eh_or_skip >= 0)
2306 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2307 gcc_assert (size_of_uleb128 (r)
2308 == size_of_uleb128 (val1->v.val_unsigned));
2309 dw2_asm_output_data_uleb128 (r, NULL);
2310 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2311 }
2312 break;
2313 case DW_OP_piece:
2314 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2315 break;
2316 case DW_OP_bit_piece:
2317 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2318 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2319 break;
2320 case DW_OP_deref_size:
2321 case DW_OP_xderef_size:
2322 dw2_asm_output_data (1, val1->v.val_int, NULL);
2323 break;
2324
2325 case DW_OP_addr:
2326 if (loc->dtprel)
2327 {
2328 if (targetm.asm_out.output_dwarf_dtprel)
2329 {
2330 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2331 DWARF2_ADDR_SIZE,
2332 val1->v.val_addr);
2333 fputc ('\n', asm_out_file);
2334 }
2335 else
2336 gcc_unreachable ();
2337 }
2338 else
2339 {
2340 #ifdef DWARF2_DEBUGGING_INFO
2341 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2342 #else
2343 gcc_unreachable ();
2344 #endif
2345 }
2346 break;
2347
2348 case DW_OP_GNU_addr_index:
2349 case DW_OP_addrx:
2350 case DW_OP_GNU_const_index:
2351 case DW_OP_constx:
2352 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2353 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2354 "(index into .debug_addr)");
2355 break;
2356
2357 case DW_OP_call2:
2358 case DW_OP_call4:
2359 {
2360 unsigned long die_offset
2361 = get_ref_die_offset (val1->v.val_die_ref.die);
2362 /* Make sure the offset has been computed and that we can encode it as
2363 an operand. */
2364 gcc_assert (die_offset > 0
2365 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2366 ? 0xffff
2367 : 0xffffffff));
2368 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2369 die_offset, NULL);
2370 }
2371 break;
2372
2373 case DW_OP_call_ref:
2374 case DW_OP_GNU_variable_value:
2375 {
2376 char label[MAX_ARTIFICIAL_LABEL_BYTES
2377 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2378 gcc_assert (val1->val_class == dw_val_class_die_ref);
2379 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2380 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2381 }
2382 break;
2383
2384 case DW_OP_implicit_pointer:
2385 case DW_OP_GNU_implicit_pointer:
2386 {
2387 char label[MAX_ARTIFICIAL_LABEL_BYTES
2388 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2389 gcc_assert (val1->val_class == dw_val_class_die_ref);
2390 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2391 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2392 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2393 }
2394 break;
2395
2396 case DW_OP_entry_value:
2397 case DW_OP_GNU_entry_value:
2398 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2399 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2400 break;
2401
2402 case DW_OP_const_type:
2403 case DW_OP_GNU_const_type:
2404 {
2405 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2406 gcc_assert (o);
2407 dw2_asm_output_data_uleb128 (o, NULL);
2408 switch (val2->val_class)
2409 {
2410 case dw_val_class_const:
2411 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2412 dw2_asm_output_data (1, l, NULL);
2413 dw2_asm_output_data (l, val2->v.val_int, NULL);
2414 break;
2415 case dw_val_class_vec:
2416 {
2417 unsigned int elt_size = val2->v.val_vec.elt_size;
2418 unsigned int len = val2->v.val_vec.length;
2419 unsigned int i;
2420 unsigned char *p;
2421
2422 l = len * elt_size;
2423 dw2_asm_output_data (1, l, NULL);
2424 if (elt_size > sizeof (HOST_WIDE_INT))
2425 {
2426 elt_size /= 2;
2427 len *= 2;
2428 }
2429 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2430 i < len;
2431 i++, p += elt_size)
2432 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2433 "fp or vector constant word %u", i);
2434 }
2435 break;
2436 case dw_val_class_const_double:
2437 {
2438 unsigned HOST_WIDE_INT first, second;
2439 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2440
2441 dw2_asm_output_data (1, 2 * l, NULL);
2442 if (WORDS_BIG_ENDIAN)
2443 {
2444 first = val2->v.val_double.high;
2445 second = val2->v.val_double.low;
2446 }
2447 else
2448 {
2449 first = val2->v.val_double.low;
2450 second = val2->v.val_double.high;
2451 }
2452 dw2_asm_output_data (l, first, NULL);
2453 dw2_asm_output_data (l, second, NULL);
2454 }
2455 break;
2456 case dw_val_class_wide_int:
2457 {
2458 int i;
2459 int len = get_full_len (*val2->v.val_wide);
2460 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2461
2462 dw2_asm_output_data (1, len * l, NULL);
2463 if (WORDS_BIG_ENDIAN)
2464 for (i = len - 1; i >= 0; --i)
2465 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2466 else
2467 for (i = 0; i < len; ++i)
2468 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2469 }
2470 break;
2471 default:
2472 gcc_unreachable ();
2473 }
2474 }
2475 break;
2476 case DW_OP_regval_type:
2477 case DW_OP_GNU_regval_type:
2478 {
2479 unsigned r = val1->v.val_unsigned;
2480 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2481 gcc_assert (o);
2482 if (for_eh_or_skip >= 0)
2483 {
2484 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2485 gcc_assert (size_of_uleb128 (r)
2486 == size_of_uleb128 (val1->v.val_unsigned));
2487 }
2488 dw2_asm_output_data_uleb128 (r, NULL);
2489 dw2_asm_output_data_uleb128 (o, NULL);
2490 }
2491 break;
2492 case DW_OP_deref_type:
2493 case DW_OP_GNU_deref_type:
2494 {
2495 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2496 gcc_assert (o);
2497 dw2_asm_output_data (1, val1->v.val_int, NULL);
2498 dw2_asm_output_data_uleb128 (o, NULL);
2499 }
2500 break;
2501 case DW_OP_convert:
2502 case DW_OP_reinterpret:
2503 case DW_OP_GNU_convert:
2504 case DW_OP_GNU_reinterpret:
2505 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2506 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2507 else
2508 {
2509 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2510 gcc_assert (o);
2511 dw2_asm_output_data_uleb128 (o, NULL);
2512 }
2513 break;
2514
2515 case DW_OP_GNU_parameter_ref:
2516 {
2517 unsigned long o;
2518 gcc_assert (val1->val_class == dw_val_class_die_ref);
2519 o = get_ref_die_offset (val1->v.val_die_ref.die);
2520 dw2_asm_output_data (4, o, NULL);
2521 }
2522 break;
2523
2524 default:
2525 /* Other codes have no operands. */
2526 break;
2527 }
2528 }
2529
2530 /* Output a sequence of location operations.
2531 The for_eh_or_skip parameter controls whether register numbers are
2532 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2533 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2534 info). This should be suppressed for the cases that have not been converted
2535 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2536
2537 void
2538 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2539 {
2540 for (; loc != NULL; loc = loc->dw_loc_next)
2541 {
2542 enum dwarf_location_atom opc = loc->dw_loc_opc;
2543 /* Output the opcode. */
2544 if (for_eh_or_skip >= 0
2545 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2546 {
2547 unsigned r = (opc - DW_OP_breg0);
2548 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2549 gcc_assert (r <= 31);
2550 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2551 }
2552 else if (for_eh_or_skip >= 0
2553 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2554 {
2555 unsigned r = (opc - DW_OP_reg0);
2556 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2557 gcc_assert (r <= 31);
2558 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2559 }
2560
2561 dw2_asm_output_data (1, opc,
2562 "%s", dwarf_stack_op_name (opc));
2563
2564 /* Output the operand(s) (if any). */
2565 output_loc_operands (loc, for_eh_or_skip);
2566 }
2567 }
2568
2569 /* Output location description stack opcode's operands (if any).
2570 The output is single bytes on a line, suitable for .cfi_escape. */
2571
2572 static void
2573 output_loc_operands_raw (dw_loc_descr_ref loc)
2574 {
2575 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2576 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2577
2578 switch (loc->dw_loc_opc)
2579 {
2580 case DW_OP_addr:
2581 case DW_OP_GNU_addr_index:
2582 case DW_OP_addrx:
2583 case DW_OP_GNU_const_index:
2584 case DW_OP_constx:
2585 case DW_OP_implicit_value:
2586 /* We cannot output addresses in .cfi_escape, only bytes. */
2587 gcc_unreachable ();
2588
2589 case DW_OP_const1u:
2590 case DW_OP_const1s:
2591 case DW_OP_pick:
2592 case DW_OP_deref_size:
2593 case DW_OP_xderef_size:
2594 fputc (',', asm_out_file);
2595 dw2_asm_output_data_raw (1, val1->v.val_int);
2596 break;
2597
2598 case DW_OP_const2u:
2599 case DW_OP_const2s:
2600 fputc (',', asm_out_file);
2601 dw2_asm_output_data_raw (2, val1->v.val_int);
2602 break;
2603
2604 case DW_OP_const4u:
2605 case DW_OP_const4s:
2606 fputc (',', asm_out_file);
2607 dw2_asm_output_data_raw (4, val1->v.val_int);
2608 break;
2609
2610 case DW_OP_const8u:
2611 case DW_OP_const8s:
2612 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2613 fputc (',', asm_out_file);
2614 dw2_asm_output_data_raw (8, val1->v.val_int);
2615 break;
2616
2617 case DW_OP_skip:
2618 case DW_OP_bra:
2619 {
2620 int offset;
2621
2622 gcc_assert (val1->val_class == dw_val_class_loc);
2623 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2624
2625 fputc (',', asm_out_file);
2626 dw2_asm_output_data_raw (2, offset);
2627 }
2628 break;
2629
2630 case DW_OP_regx:
2631 {
2632 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2633 gcc_assert (size_of_uleb128 (r)
2634 == size_of_uleb128 (val1->v.val_unsigned));
2635 fputc (',', asm_out_file);
2636 dw2_asm_output_data_uleb128_raw (r);
2637 }
2638 break;
2639
2640 case DW_OP_constu:
2641 case DW_OP_plus_uconst:
2642 case DW_OP_piece:
2643 fputc (',', asm_out_file);
2644 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2645 break;
2646
2647 case DW_OP_bit_piece:
2648 fputc (',', asm_out_file);
2649 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2650 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2651 break;
2652
2653 case DW_OP_consts:
2654 case DW_OP_breg0:
2655 case DW_OP_breg1:
2656 case DW_OP_breg2:
2657 case DW_OP_breg3:
2658 case DW_OP_breg4:
2659 case DW_OP_breg5:
2660 case DW_OP_breg6:
2661 case DW_OP_breg7:
2662 case DW_OP_breg8:
2663 case DW_OP_breg9:
2664 case DW_OP_breg10:
2665 case DW_OP_breg11:
2666 case DW_OP_breg12:
2667 case DW_OP_breg13:
2668 case DW_OP_breg14:
2669 case DW_OP_breg15:
2670 case DW_OP_breg16:
2671 case DW_OP_breg17:
2672 case DW_OP_breg18:
2673 case DW_OP_breg19:
2674 case DW_OP_breg20:
2675 case DW_OP_breg21:
2676 case DW_OP_breg22:
2677 case DW_OP_breg23:
2678 case DW_OP_breg24:
2679 case DW_OP_breg25:
2680 case DW_OP_breg26:
2681 case DW_OP_breg27:
2682 case DW_OP_breg28:
2683 case DW_OP_breg29:
2684 case DW_OP_breg30:
2685 case DW_OP_breg31:
2686 case DW_OP_fbreg:
2687 fputc (',', asm_out_file);
2688 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2689 break;
2690
2691 case DW_OP_bregx:
2692 {
2693 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2694 gcc_assert (size_of_uleb128 (r)
2695 == size_of_uleb128 (val1->v.val_unsigned));
2696 fputc (',', asm_out_file);
2697 dw2_asm_output_data_uleb128_raw (r);
2698 fputc (',', asm_out_file);
2699 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2700 }
2701 break;
2702
2703 case DW_OP_implicit_pointer:
2704 case DW_OP_entry_value:
2705 case DW_OP_const_type:
2706 case DW_OP_regval_type:
2707 case DW_OP_deref_type:
2708 case DW_OP_convert:
2709 case DW_OP_reinterpret:
2710 case DW_OP_GNU_implicit_pointer:
2711 case DW_OP_GNU_entry_value:
2712 case DW_OP_GNU_const_type:
2713 case DW_OP_GNU_regval_type:
2714 case DW_OP_GNU_deref_type:
2715 case DW_OP_GNU_convert:
2716 case DW_OP_GNU_reinterpret:
2717 case DW_OP_GNU_parameter_ref:
2718 gcc_unreachable ();
2719 break;
2720
2721 default:
2722 /* Other codes have no operands. */
2723 break;
2724 }
2725 }
2726
2727 void
2728 output_loc_sequence_raw (dw_loc_descr_ref loc)
2729 {
2730 while (1)
2731 {
2732 enum dwarf_location_atom opc = loc->dw_loc_opc;
2733 /* Output the opcode. */
2734 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2735 {
2736 unsigned r = (opc - DW_OP_breg0);
2737 r = DWARF2_FRAME_REG_OUT (r, 1);
2738 gcc_assert (r <= 31);
2739 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2740 }
2741 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2742 {
2743 unsigned r = (opc - DW_OP_reg0);
2744 r = DWARF2_FRAME_REG_OUT (r, 1);
2745 gcc_assert (r <= 31);
2746 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2747 }
2748 /* Output the opcode. */
2749 fprintf (asm_out_file, "%#x", opc);
2750 output_loc_operands_raw (loc);
2751
2752 if (!loc->dw_loc_next)
2753 break;
2754 loc = loc->dw_loc_next;
2755
2756 fputc (',', asm_out_file);
2757 }
2758 }
2759
2760 static void
2761 build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2762 {
2763 if (regno <= 31)
2764 add_loc_descr (head, new_loc_descr ((enum dwarf_location_atom)
2765 (DW_OP_breg0 + regno), 0, 0));
2766 else
2767 add_loc_descr (head, new_loc_descr (DW_OP_bregx, regno, 0));
2768 }
2769
2770 /* Build a dwarf location for a cfa_reg spanning multiple
2771 consecutive registers. */
2772
2773 struct dw_loc_descr_node *
2774 build_span_loc (struct cfa_reg reg)
2775 {
2776 struct dw_loc_descr_node *head = NULL;
2777
2778 gcc_assert (reg.span_width > 0);
2779 gcc_assert (reg.span > 1);
2780
2781 /* Start from the highest number register as it goes in the upper bits. */
2782 unsigned int regno = reg.reg + reg.span - 1;
2783 build_breg_loc (&head, regno);
2784
2785 /* Deal with the remaining registers in the span. */
2786 for (int i = reg.span - 2; i >= 0; i--)
2787 {
2788 add_loc_descr (&head, int_loc_descriptor (reg.span_width * 8));
2789 add_loc_descr (&head, new_loc_descr (DW_OP_shl, 0, 0));
2790 regno--;
2791 build_breg_loc (&head, regno);
2792 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
2793 }
2794 return head;
2795 }
2796
2797 /* This function builds a dwarf location descriptor sequence from a
2798 dw_cfa_location, adding the given OFFSET to the result of the
2799 expression. */
2800
2801 struct dw_loc_descr_node *
2802 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2803 {
2804 struct dw_loc_descr_node *head, *tmp;
2805
2806 offset += cfa->offset;
2807
2808 if (cfa->reg.span > 1)
2809 {
2810 head = build_span_loc (cfa->reg);
2811
2812 if (maybe_ne (offset, 0))
2813 loc_descr_plus_const (&head, offset);
2814 }
2815 else if (cfa->indirect)
2816 {
2817 head = new_reg_loc_descr (cfa->reg.reg, cfa->base_offset);
2818 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2819 head->dw_loc_oprnd1.val_entry = NULL;
2820 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2821 add_loc_descr (&head, tmp);
2822 loc_descr_plus_const (&head, offset);
2823 }
2824 else
2825 head = new_reg_loc_descr (cfa->reg.reg, offset);
2826
2827 return head;
2828 }
2829
2830 /* This function builds a dwarf location descriptor sequence for
2831 the address at OFFSET from the CFA when stack is aligned to
2832 ALIGNMENT byte. */
2833
2834 struct dw_loc_descr_node *
2835 build_cfa_aligned_loc (dw_cfa_location *cfa,
2836 poly_int64 offset, HOST_WIDE_INT alignment)
2837 {
2838 struct dw_loc_descr_node *head;
2839 unsigned int dwarf_fp
2840 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2841
2842 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2843 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2844 {
2845 head = new_reg_loc_descr (dwarf_fp, 0);
2846 add_loc_descr (&head, int_loc_descriptor (alignment));
2847 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2848 loc_descr_plus_const (&head, offset);
2849 }
2850 else
2851 head = new_reg_loc_descr (dwarf_fp, offset);
2852 return head;
2853 }
2854
2855 /* And now, the support for symbolic debugging information. */
2856
2857 /* .debug_str support. */
2858
2859 static void dwarf2out_init (const char *);
2860 static void dwarf2out_finish (const char *);
2861 static void dwarf2out_early_finish (const char *);
2862 static void dwarf2out_assembly_start (void);
2863 static void dwarf2out_define (unsigned int, const char *);
2864 static void dwarf2out_undef (unsigned int, const char *);
2865 static void dwarf2out_start_source_file (unsigned, const char *);
2866 static void dwarf2out_end_source_file (unsigned);
2867 static void dwarf2out_function_decl (tree);
2868 static void dwarf2out_begin_block (unsigned, unsigned, tree);
2869 static void dwarf2out_end_block (unsigned, unsigned);
2870 static bool dwarf2out_ignore_block (const_tree);
2871 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2872 static void dwarf2out_early_global_decl (tree);
2873 static void dwarf2out_late_global_decl (tree);
2874 static void dwarf2out_type_decl (tree, int);
2875 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2876 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2877 dw_die_ref);
2878 static void dwarf2out_abstract_function (tree);
2879 static void dwarf2out_var_location (rtx_insn *);
2880 static void dwarf2out_inline_entry (tree);
2881 static void dwarf2out_size_function (tree);
2882 static void dwarf2out_begin_function (tree);
2883 static void dwarf2out_end_function (unsigned int);
2884 static void dwarf2out_register_main_translation_unit (tree unit);
2885 static void dwarf2out_set_name (tree, tree);
2886 static void dwarf2out_register_external_die (tree decl, const char *sym,
2887 unsigned HOST_WIDE_INT off);
2888 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2889 unsigned HOST_WIDE_INT *off);
2890
2891 /* The debug hooks structure. */
2892
2893 const struct gcc_debug_hooks dwarf2_debug_hooks =
2894 {
2895 dwarf2out_init,
2896 dwarf2out_finish,
2897 dwarf2out_early_finish,
2898 dwarf2out_assembly_start,
2899 dwarf2out_define,
2900 dwarf2out_undef,
2901 dwarf2out_start_source_file,
2902 dwarf2out_end_source_file,
2903 dwarf2out_begin_block,
2904 dwarf2out_end_block,
2905 dwarf2out_ignore_block,
2906 dwarf2out_source_line,
2907 dwarf2out_set_ignored_loc,
2908 dwarf2out_begin_prologue,
2909 #if VMS_DEBUGGING_INFO
2910 dwarf2out_vms_end_prologue,
2911 dwarf2out_vms_begin_epilogue,
2912 #else
2913 debug_nothing_int_charstar,
2914 debug_nothing_int_charstar,
2915 #endif
2916 dwarf2out_end_epilogue,
2917 dwarf2out_begin_function,
2918 dwarf2out_end_function, /* end_function */
2919 dwarf2out_register_main_translation_unit,
2920 dwarf2out_function_decl, /* function_decl */
2921 dwarf2out_early_global_decl,
2922 dwarf2out_late_global_decl,
2923 dwarf2out_type_decl, /* type_decl */
2924 dwarf2out_imported_module_or_decl,
2925 dwarf2out_die_ref_for_decl,
2926 dwarf2out_register_external_die,
2927 debug_nothing_tree, /* deferred_inline_function */
2928 /* The DWARF 2 backend tries to reduce debugging bloat by not
2929 emitting the abstract description of inline functions until
2930 something tries to reference them. */
2931 dwarf2out_abstract_function, /* outlining_inline_function */
2932 debug_nothing_rtx_code_label, /* label */
2933 debug_nothing_int, /* handle_pch */
2934 dwarf2out_var_location,
2935 dwarf2out_inline_entry, /* inline_entry */
2936 dwarf2out_size_function, /* size_function */
2937 dwarf2out_switch_text_section,
2938 dwarf2out_set_name,
2939 1, /* start_end_main_source_file */
2940 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2941 };
2942
2943 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2944 {
2945 dwarf2out_init,
2946 debug_nothing_charstar,
2947 debug_nothing_charstar,
2948 dwarf2out_assembly_start,
2949 debug_nothing_int_charstar,
2950 debug_nothing_int_charstar,
2951 debug_nothing_int_charstar,
2952 debug_nothing_int,
2953 debug_nothing_int_int_tree, /* begin_block */
2954 debug_nothing_int_int, /* end_block */
2955 debug_true_const_tree, /* ignore_block */
2956 dwarf2out_source_line, /* source_line */
2957 debug_nothing_int_int_charstar, /* set_ignored_loc */
2958 debug_nothing_int_int_charstar, /* begin_prologue */
2959 debug_nothing_int_charstar, /* end_prologue */
2960 debug_nothing_int_charstar, /* begin_epilogue */
2961 debug_nothing_int_charstar, /* end_epilogue */
2962 debug_nothing_tree, /* begin_function */
2963 debug_nothing_int, /* end_function */
2964 debug_nothing_tree, /* register_main_translation_unit */
2965 debug_nothing_tree, /* function_decl */
2966 debug_nothing_tree, /* early_global_decl */
2967 debug_nothing_tree, /* late_global_decl */
2968 debug_nothing_tree_int, /* type_decl */
2969 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2970 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2971 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2972 debug_nothing_tree, /* deferred_inline_function */
2973 debug_nothing_tree, /* outlining_inline_function */
2974 debug_nothing_rtx_code_label, /* label */
2975 debug_nothing_int, /* handle_pch */
2976 debug_nothing_rtx_insn, /* var_location */
2977 debug_nothing_tree, /* inline_entry */
2978 debug_nothing_tree, /* size_function */
2979 debug_nothing_void, /* switch_text_section */
2980 debug_nothing_tree_tree, /* set_name */
2981 0, /* start_end_main_source_file */
2982 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2983 };
2984
2985 /* NOTE: In the comments in this file, many references are made to
2986 "Debugging Information Entries". This term is abbreviated as `DIE'
2987 throughout the remainder of this file. */
2988
2989 /* An internal representation of the DWARF output is built, and then
2990 walked to generate the DWARF debugging info. The walk of the internal
2991 representation is done after the entire program has been compiled.
2992 The types below are used to describe the internal representation. */
2993
2994 /* Whether to put type DIEs into their own section .debug_types instead
2995 of making them part of the .debug_info section. Only supported for
2996 Dwarf V4 or higher and the user didn't disable them through
2997 -fno-debug-types-section. It is more efficient to put them in a
2998 separate comdat sections since the linker will then be able to
2999 remove duplicates. But not all tools support .debug_types sections
3000 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3001 it is DW_UT_type unit type in .debug_info section. For late LTO
3002 debug there should be almost no types emitted so avoid enabling
3003 -fdebug-types-section there. */
3004
3005 #define use_debug_types (dwarf_version >= 4 \
3006 && flag_debug_types_section \
3007 && !in_lto_p)
3008
3009 /* Various DIE's use offsets relative to the beginning of the
3010 .debug_info section to refer to each other. */
3011
3012 typedef long int dw_offset;
3013
3014 struct comdat_type_node;
3015
3016 /* The entries in the line_info table more-or-less mirror the opcodes
3017 that are used in the real dwarf line table. Arrays of these entries
3018 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3019 supported. */
3020
3021 enum dw_line_info_opcode {
3022 /* Emit DW_LNE_set_address; the operand is the label index. */
3023 LI_set_address,
3024
3025 /* Emit a row to the matrix with the given line. This may be done
3026 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3027 special opcodes. */
3028 LI_set_line,
3029
3030 /* Emit a DW_LNS_set_file. */
3031 LI_set_file,
3032
3033 /* Emit a DW_LNS_set_column. */
3034 LI_set_column,
3035
3036 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3037 LI_negate_stmt,
3038
3039 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3040 LI_set_prologue_end,
3041 LI_set_epilogue_begin,
3042
3043 /* Emit a DW_LNE_set_discriminator. */
3044 LI_set_discriminator,
3045
3046 /* Output a Fixed Advance PC; the target PC is the label index; the
3047 base PC is the previous LI_adv_address or LI_set_address entry.
3048 We only use this when emitting debug views without assembler
3049 support, at explicit user request. Ideally, we should only use
3050 it when the offset might be zero but we can't tell: it's the only
3051 way to maybe change the PC without resetting the view number. */
3052 LI_adv_address
3053 };
3054
3055 typedef struct GTY(()) dw_line_info_struct {
3056 enum dw_line_info_opcode opcode;
3057 unsigned int val;
3058 } dw_line_info_entry;
3059
3060
3061 struct GTY(()) dw_line_info_table {
3062 /* The label that marks the end of this section. */
3063 const char *end_label;
3064
3065 /* The values for the last row of the matrix, as collected in the table.
3066 These are used to minimize the changes to the next row. */
3067 unsigned int file_num;
3068 unsigned int line_num;
3069 unsigned int column_num;
3070 int discrim_num;
3071 bool is_stmt;
3072 bool in_use;
3073
3074 /* This denotes the NEXT view number.
3075
3076 If it is 0, it is known that the NEXT view will be the first view
3077 at the given PC.
3078
3079 If it is -1, we're forcing the view number to be reset, e.g. at a
3080 function entry.
3081
3082 The meaning of other nonzero values depends on whether we're
3083 computing views internally or leaving it for the assembler to do
3084 so. If we're emitting them internally, view denotes the view
3085 number since the last known advance of PC. If we're leaving it
3086 for the assembler, it denotes the LVU label number that we're
3087 going to ask the assembler to assign. */
3088 var_loc_view view;
3089
3090 /* This counts the number of symbolic views emitted in this table
3091 since the latest view reset. Its max value, over all tables,
3092 sets symview_upper_bound. */
3093 var_loc_view symviews_since_reset;
3094
3095 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3096 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3097 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3098 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3099
3100 vec<dw_line_info_entry, va_gc> *entries;
3101 };
3102
3103 /* This is an upper bound for view numbers that the assembler may
3104 assign to symbolic views output in this translation. It is used to
3105 decide how big a field to use to represent view numbers in
3106 symview-classed attributes. */
3107
3108 static var_loc_view symview_upper_bound;
3109
3110 /* If we're keep track of location views and their reset points, and
3111 INSN is a reset point (i.e., it necessarily advances the PC), mark
3112 the next view in TABLE as reset. */
3113
3114 static void
3115 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3116 {
3117 if (!debug_internal_reset_location_views)
3118 return;
3119
3120 /* Maybe turn (part of?) this test into a default target hook. */
3121 int reset = 0;
3122
3123 if (targetm.reset_location_view)
3124 reset = targetm.reset_location_view (insn);
3125
3126 if (reset)
3127 ;
3128 else if (JUMP_TABLE_DATA_P (insn))
3129 reset = 1;
3130 else if (GET_CODE (insn) == USE
3131 || GET_CODE (insn) == CLOBBER
3132 || GET_CODE (insn) == ASM_INPUT
3133 || asm_noperands (insn) >= 0)
3134 ;
3135 else if (get_attr_min_length (insn) > 0)
3136 reset = 1;
3137
3138 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3139 RESET_NEXT_VIEW (table->view);
3140 }
3141
3142 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3143 The children of each node form a circular list linked by
3144 die_sib. die_child points to the node *before* the "first" child node. */
3145
3146 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3147 union die_symbol_or_type_node
3148 {
3149 const char * GTY ((tag ("0"))) die_symbol;
3150 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3151 }
3152 GTY ((desc ("%0.comdat_type_p"))) die_id;
3153 vec<dw_attr_node, va_gc> *die_attr;
3154 dw_die_ref die_parent;
3155 dw_die_ref die_child;
3156 dw_die_ref die_sib;
3157 dw_die_ref die_definition; /* ref from a specification to its definition */
3158 dw_offset die_offset;
3159 unsigned long die_abbrev;
3160 int die_mark;
3161 unsigned int decl_id;
3162 enum dwarf_tag die_tag;
3163 /* Die is used and must not be pruned as unused. */
3164 BOOL_BITFIELD die_perennial_p : 1;
3165 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3166 /* For an external ref to die_symbol if die_offset contains an extra
3167 offset to that symbol. */
3168 BOOL_BITFIELD with_offset : 1;
3169 /* Whether this DIE was removed from the DIE tree, for example via
3170 prune_unused_types. We don't consider those present from the
3171 DIE lookup routines. */
3172 BOOL_BITFIELD removed : 1;
3173 /* Lots of spare bits. */
3174 }
3175 die_node;
3176
3177 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3178 static bool early_dwarf;
3179 static bool early_dwarf_finished;
3180 class set_early_dwarf {
3181 public:
3182 bool saved;
3183 set_early_dwarf () : saved(early_dwarf)
3184 {
3185 gcc_assert (! early_dwarf_finished);
3186 early_dwarf = true;
3187 }
3188 ~set_early_dwarf () { early_dwarf = saved; }
3189 };
3190
3191 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3192 #define FOR_EACH_CHILD(die, c, expr) do { \
3193 c = die->die_child; \
3194 if (c) do { \
3195 c = c->die_sib; \
3196 expr; \
3197 } while (c != die->die_child); \
3198 } while (0)
3199
3200 /* The pubname structure */
3201
3202 typedef struct GTY(()) pubname_struct {
3203 dw_die_ref die;
3204 const char *name;
3205 }
3206 pubname_entry;
3207
3208
3209 struct GTY(()) dw_ranges {
3210 const char *label;
3211 /* If this is positive, it's a block number, otherwise it's a
3212 bitwise-negated index into dw_ranges_by_label. */
3213 int num;
3214 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3215 into .debug_rnglists section rather than .debug_rnglists.dwo
3216 for -gsplit-dwarf and DWARF >= 5. */
3217 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3218 /* Index for the range list for DW_FORM_rnglistx. */
3219 unsigned int idx : 31;
3220 /* True if this range might be possibly in a different section
3221 from previous entry. */
3222 unsigned int maybe_new_sec : 1;
3223 addr_table_entry *begin_entry;
3224 addr_table_entry *end_entry;
3225 };
3226
3227 /* A structure to hold a macinfo entry. */
3228
3229 typedef struct GTY(()) macinfo_struct {
3230 unsigned char code;
3231 unsigned HOST_WIDE_INT lineno;
3232 const char *info;
3233 }
3234 macinfo_entry;
3235
3236
3237 struct GTY(()) dw_ranges_by_label {
3238 const char *begin;
3239 const char *end;
3240 };
3241
3242 /* The comdat type node structure. */
3243 struct GTY(()) comdat_type_node
3244 {
3245 dw_die_ref root_die;
3246 dw_die_ref type_die;
3247 dw_die_ref skeleton_die;
3248 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3249 comdat_type_node *next;
3250 };
3251
3252 /* A list of DIEs for which we can't determine ancestry (parent_die
3253 field) just yet. Later in dwarf2out_finish we will fill in the
3254 missing bits. */
3255 typedef struct GTY(()) limbo_die_struct {
3256 dw_die_ref die;
3257 /* The tree for which this DIE was created. We use this to
3258 determine ancestry later. */
3259 tree created_for;
3260 struct limbo_die_struct *next;
3261 }
3262 limbo_die_node;
3263
3264 typedef struct skeleton_chain_struct
3265 {
3266 dw_die_ref old_die;
3267 dw_die_ref new_die;
3268 struct skeleton_chain_struct *parent;
3269 }
3270 skeleton_chain_node;
3271
3272 /* Define a macro which returns nonzero for a TYPE_DECL which was
3273 implicitly generated for a type.
3274
3275 Note that, unlike the C front-end (which generates a NULL named
3276 TYPE_DECL node for each complete tagged type, each array type,
3277 and each function type node created) the C++ front-end generates
3278 a _named_ TYPE_DECL node for each tagged type node created.
3279 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3280 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3281 front-end, but for each type, tagged or not. */
3282
3283 #define TYPE_DECL_IS_STUB(decl) \
3284 (DECL_NAME (decl) == NULL_TREE \
3285 || (DECL_ARTIFICIAL (decl) \
3286 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3287 /* This is necessary for stub decls that \
3288 appear in nested inline functions. */ \
3289 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3290 && (decl_ultimate_origin (decl) \
3291 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3292
3293 /* Information concerning the compilation unit's programming
3294 language, and compiler version. */
3295
3296 /* Fixed size portion of the DWARF compilation unit header. */
3297 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3298 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3299 + (dwarf_version >= 5 ? 4 : 3))
3300
3301 /* Fixed size portion of the DWARF comdat type unit header. */
3302 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3303 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3304 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3305
3306 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3307 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3308 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3309
3310 /* Fixed size portion of public names info. */
3311 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3312
3313 /* Fixed size portion of the address range info. */
3314 #define DWARF_ARANGES_HEADER_SIZE \
3315 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3316 DWARF2_ADDR_SIZE * 2) \
3317 - DWARF_INITIAL_LENGTH_SIZE)
3318
3319 /* Size of padding portion in the address range info. It must be
3320 aligned to twice the pointer size. */
3321 #define DWARF_ARANGES_PAD_SIZE \
3322 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3323 DWARF2_ADDR_SIZE * 2) \
3324 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3325
3326 /* Use assembler line directives if available. */
3327 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3328 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3329 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3330 #else
3331 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3332 #endif
3333 #endif
3334
3335 /* Use assembler views in line directives if available. */
3336 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3337 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3338 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3339 #else
3340 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3341 #endif
3342 #endif
3343
3344 /* Return true if GCC configure detected assembler support for .loc. */
3345
3346 bool
3347 dwarf2out_default_as_loc_support (void)
3348 {
3349 return DWARF2_ASM_LINE_DEBUG_INFO;
3350 #if (GCC_VERSION >= 3000)
3351 # undef DWARF2_ASM_LINE_DEBUG_INFO
3352 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3353 #endif
3354 }
3355
3356 /* Return true if GCC configure detected assembler support for views
3357 in .loc directives. */
3358
3359 bool
3360 dwarf2out_default_as_locview_support (void)
3361 {
3362 return DWARF2_ASM_VIEW_DEBUG_INFO;
3363 #if (GCC_VERSION >= 3000)
3364 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3365 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3366 #endif
3367 }
3368
3369 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3370 view computation, and it refers to a view identifier for which we
3371 will not emit a label because it is known to map to a view number
3372 zero. We won't allocate the bitmap if we're not using assembler
3373 support for location views, but we have to make the variable
3374 visible for GGC and for code that will be optimized out for lack of
3375 support but that's still parsed and compiled. We could abstract it
3376 out with macros, but it's not worth it. */
3377 static GTY(()) bitmap zero_view_p;
3378
3379 /* Evaluate to TRUE iff N is known to identify the first location view
3380 at its PC. When not using assembler location view computation,
3381 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3382 and views label numbers recorded in it are the ones known to be
3383 zero. */
3384 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3385 || (N) == (var_loc_view)-1 \
3386 || (zero_view_p \
3387 && bitmap_bit_p (zero_view_p, (N))))
3388
3389 /* Return true iff we're to emit .loc directives for the assembler to
3390 generate line number sections.
3391
3392 When we're not emitting views, all we need from the assembler is
3393 support for .loc directives.
3394
3395 If we are emitting views, we can only use the assembler's .loc
3396 support if it also supports views.
3397
3398 When the compiler is emitting the line number programs and
3399 computing view numbers itself, it resets view numbers at known PC
3400 changes and counts from that, and then it emits view numbers as
3401 literal constants in locviewlists. There are cases in which the
3402 compiler is not sure about PC changes, e.g. when extra alignment is
3403 requested for a label. In these cases, the compiler may not reset
3404 the view counter, and the potential PC advance in the line number
3405 program will use an opcode that does not reset the view counter
3406 even if the PC actually changes, so that compiler and debug info
3407 consumer can keep view numbers in sync.
3408
3409 When the compiler defers view computation to the assembler, it
3410 emits symbolic view numbers in locviewlists, with the exception of
3411 views known to be zero (forced resets, or reset after
3412 compiler-visible PC changes): instead of emitting symbols for
3413 these, we emit literal zero and assert the assembler agrees with
3414 the compiler's assessment. We could use symbolic views everywhere,
3415 instead of special-casing zero views, but then we'd be unable to
3416 optimize out locviewlists that contain only zeros. */
3417
3418 static bool
3419 output_asm_line_debug_info (void)
3420 {
3421 return (dwarf2out_as_loc_support
3422 && (dwarf2out_as_locview_support
3423 || !debug_variable_location_views));
3424 }
3425
3426 static bool asm_outputs_debug_line_str (void);
3427
3428 /* Minimum line offset in a special line info. opcode.
3429 This value was chosen to give a reasonable range of values. */
3430 #define DWARF_LINE_BASE -10
3431
3432 /* First special line opcode - leave room for the standard opcodes. */
3433 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3434
3435 /* Range of line offsets in a special line info. opcode. */
3436 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3437
3438 /* Flag that indicates the initial value of the is_stmt_start flag.
3439 In the present implementation, we do not mark any lines as
3440 the beginning of a source statement, because that information
3441 is not made available by the GCC front-end. */
3442 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3443
3444 /* Maximum number of operations per instruction bundle. */
3445 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3446 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3447 #endif
3448
3449 /* This location is used by calc_die_sizes() to keep track
3450 the offset of each DIE within the .debug_info section. */
3451 static unsigned long next_die_offset;
3452
3453 /* Record the root of the DIE's built for the current compilation unit. */
3454 static GTY(()) dw_die_ref single_comp_unit_die;
3455
3456 /* A list of type DIEs that have been separated into comdat sections. */
3457 static GTY(()) comdat_type_node *comdat_type_list;
3458
3459 /* A list of CU DIEs that have been separated. */
3460 static GTY(()) limbo_die_node *cu_die_list;
3461
3462 /* A list of DIEs with a NULL parent waiting to be relocated. */
3463 static GTY(()) limbo_die_node *limbo_die_list;
3464
3465 /* A list of DIEs for which we may have to generate
3466 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3467 static GTY(()) limbo_die_node *deferred_asm_name;
3468
3469 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3470 {
3471 typedef const char *compare_type;
3472
3473 static hashval_t hash (dwarf_file_data *);
3474 static bool equal (dwarf_file_data *, const char *);
3475 };
3476
3477 /* Filenames referenced by this compilation unit. */
3478 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3479
3480 struct decl_die_hasher : ggc_ptr_hash<die_node>
3481 {
3482 typedef tree compare_type;
3483
3484 static hashval_t hash (die_node *);
3485 static bool equal (die_node *, tree);
3486 };
3487 /* A hash table of references to DIE's that describe declarations.
3488 The key is a DECL_UID() which is a unique number identifying each decl. */
3489 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3490
3491 struct GTY ((for_user)) variable_value_struct {
3492 unsigned int decl_id;
3493 vec<dw_die_ref, va_gc> *dies;
3494 };
3495
3496 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3497 {
3498 typedef tree compare_type;
3499
3500 static hashval_t hash (variable_value_struct *);
3501 static bool equal (variable_value_struct *, tree);
3502 };
3503 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3504 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3505 DECL_CONTEXT of the referenced VAR_DECLs. */
3506 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3507
3508 struct block_die_hasher : ggc_ptr_hash<die_struct>
3509 {
3510 static hashval_t hash (die_struct *);
3511 static bool equal (die_struct *, die_struct *);
3512 };
3513
3514 /* A hash table of references to DIE's that describe COMMON blocks.
3515 The key is DECL_UID() ^ die_parent. */
3516 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3517
3518 typedef struct GTY(()) die_arg_entry_struct {
3519 dw_die_ref die;
3520 tree arg;
3521 } die_arg_entry;
3522
3523
3524 /* Node of the variable location list. */
3525 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3526 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3527 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3528 in mode of the EXPR_LIST node and first EXPR_LIST operand
3529 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3530 location or NULL for padding. For larger bitsizes,
3531 mode is 0 and first operand is a CONCAT with bitsize
3532 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3533 NULL as second operand. */
3534 rtx GTY (()) loc;
3535 const char * GTY (()) label;
3536 struct var_loc_node * GTY (()) next;
3537 var_loc_view view;
3538 };
3539
3540 /* Variable location list. */
3541 struct GTY ((for_user)) var_loc_list_def {
3542 struct var_loc_node * GTY (()) first;
3543
3544 /* Pointer to the last but one or last element of the
3545 chained list. If the list is empty, both first and
3546 last are NULL, if the list contains just one node
3547 or the last node certainly is not redundant, it points
3548 to the last node, otherwise points to the last but one.
3549 Do not mark it for GC because it is marked through the chain. */
3550 struct var_loc_node * GTY ((skip ("%h"))) last;
3551
3552 /* Pointer to the last element before section switch,
3553 if NULL, either sections weren't switched or first
3554 is after section switch. */
3555 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3556
3557 /* DECL_UID of the variable decl. */
3558 unsigned int decl_id;
3559 };
3560 typedef struct var_loc_list_def var_loc_list;
3561
3562 /* Call argument location list. */
3563 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3564 rtx_insn * GTY (()) call_insn;
3565 const char * GTY (()) label;
3566 tree GTY (()) block;
3567 bool tail_call_p;
3568 rtx GTY (()) symbol_ref;
3569 struct call_arg_loc_node * GTY (()) next;
3570 };
3571
3572
3573 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3574 {
3575 typedef const_tree compare_type;
3576
3577 static hashval_t hash (var_loc_list *);
3578 static bool equal (var_loc_list *, const_tree);
3579 };
3580
3581 /* Table of decl location linked lists. */
3582 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3583
3584 /* Head and tail of call_arg_loc chain. */
3585 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3586 static struct call_arg_loc_node *call_arg_loc_last;
3587
3588 /* Number of call sites in the current function. */
3589 static int call_site_count = -1;
3590 /* Number of tail call sites in the current function. */
3591 static int tail_call_site_count = -1;
3592
3593 /* A cached location list. */
3594 struct GTY ((for_user)) cached_dw_loc_list_def {
3595 /* The DECL_UID of the decl that this entry describes. */
3596 unsigned int decl_id;
3597
3598 /* The cached location list. */
3599 dw_loc_list_ref loc_list;
3600 };
3601 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3602
3603 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3604 {
3605
3606 typedef const_tree compare_type;
3607
3608 static hashval_t hash (cached_dw_loc_list *);
3609 static bool equal (cached_dw_loc_list *, const_tree);
3610 };
3611
3612 /* Table of cached location lists. */
3613 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3614
3615 /* A vector of references to DIE's that are uniquely identified by their tag,
3616 presence/absence of children DIE's, and list of attribute/value pairs. */
3617 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3618
3619 /* A hash map to remember the stack usage for DWARF procedures. The value
3620 stored is the stack size difference between before the DWARF procedure
3621 invokation and after it returned. In other words, for a DWARF procedure
3622 that consumes N stack slots and that pushes M ones, this stores M - N. */
3623 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3624
3625 /* A global counter for generating labels for line number data. */
3626 static unsigned int line_info_label_num;
3627
3628 /* The current table to which we should emit line number information
3629 for the current function. This will be set up at the beginning of
3630 assembly for the function. */
3631 static GTY(()) dw_line_info_table *cur_line_info_table;
3632
3633 /* The two default tables of line number info. */
3634 static GTY(()) dw_line_info_table *text_section_line_info;
3635 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3636
3637 /* The set of all non-default tables of line number info. */
3638 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3639
3640 /* A flag to tell pubnames/types export if there is an info section to
3641 refer to. */
3642 static bool info_section_emitted;
3643
3644 /* A pointer to the base of a table that contains a list of publicly
3645 accessible names. */
3646 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3647
3648 /* A pointer to the base of a table that contains a list of publicly
3649 accessible types. */
3650 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3651
3652 /* A pointer to the base of a table that contains a list of macro
3653 defines/undefines (and file start/end markers). */
3654 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3655
3656 /* True if .debug_macinfo or .debug_macros section is going to be
3657 emitted. */
3658 #define have_macinfo \
3659 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3660 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3661 && !macinfo_table->is_empty ())
3662
3663 /* Vector of dies for which we should generate .debug_ranges info. */
3664 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3665
3666 /* Vector of pairs of labels referenced in ranges_table. */
3667 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3668
3669 /* Whether we have location lists that need outputting */
3670 static GTY(()) bool have_location_lists;
3671
3672 /* Unique label counter. */
3673 static GTY(()) unsigned int loclabel_num;
3674
3675 /* Unique label counter for point-of-call tables. */
3676 static GTY(()) unsigned int poc_label_num;
3677
3678 /* The last file entry emitted by maybe_emit_file(). */
3679 static GTY(()) struct dwarf_file_data * last_emitted_file;
3680
3681 /* Number of internal labels generated by gen_internal_sym(). */
3682 static GTY(()) int label_num;
3683
3684 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3685
3686 /* Instances of generic types for which we need to generate debug
3687 info that describe their generic parameters and arguments. That
3688 generation needs to happen once all types are properly laid out so
3689 we do it at the end of compilation. */
3690 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3691
3692 /* Offset from the "steady-state frame pointer" to the frame base,
3693 within the current function. */
3694 static poly_int64 frame_pointer_fb_offset;
3695 static bool frame_pointer_fb_offset_valid;
3696
3697 static vec<dw_die_ref> base_types;
3698
3699 /* Flags to represent a set of attribute classes for attributes that represent
3700 a scalar value (bounds, pointers, ...). */
3701 enum dw_scalar_form
3702 {
3703 dw_scalar_form_constant = 0x01,
3704 dw_scalar_form_exprloc = 0x02,
3705 dw_scalar_form_reference = 0x04
3706 };
3707
3708 /* Forward declarations for functions defined in this file. */
3709
3710 static bool is_pseudo_reg (const_rtx);
3711 static tree type_main_variant (tree);
3712 static bool is_tagged_type (const_tree);
3713 static const char *dwarf_tag_name (unsigned);
3714 static const char *dwarf_attr_name (unsigned);
3715 static const char *dwarf_form_name (unsigned);
3716 static tree decl_ultimate_origin (const_tree);
3717 static tree decl_class_context (tree);
3718 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3719 static inline unsigned int AT_index (dw_attr_node *);
3720 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3721 static inline unsigned AT_flag (dw_attr_node *);
3722 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3723 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3724 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3725 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3726 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3727 unsigned int, unsigned char *);
3728 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3729 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3730 static inline const char *AT_string (dw_attr_node *);
3731 static enum dwarf_form AT_string_form (dw_attr_node *);
3732 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3733 static void add_AT_specification (dw_die_ref, dw_die_ref);
3734 static inline dw_die_ref AT_ref (dw_attr_node *);
3735 static inline int AT_ref_external (dw_attr_node *);
3736 static inline void set_AT_ref_external (dw_attr_node *, int);
3737 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3738 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3739 dw_loc_list_ref);
3740 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3741 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3742 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3743 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3744 static void remove_addr_table_entry (addr_table_entry *);
3745 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3746 static inline rtx AT_addr (dw_attr_node *);
3747 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3748 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *,
3749 int = 0);
3750 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3751 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3752 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3753 unsigned long, bool);
3754 static inline const char *AT_lbl (dw_attr_node *);
3755 static const char *get_AT_low_pc (dw_die_ref);
3756 static bool is_c (void);
3757 static bool is_cxx (void);
3758 static bool is_cxx (const_tree);
3759 static bool is_fortran (void);
3760 static bool is_ada (void);
3761 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3762 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3763 static void add_child_die (dw_die_ref, dw_die_ref);
3764 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3765 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3766 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3767 static void equate_type_number_to_die (tree, dw_die_ref);
3768 static var_loc_list *lookup_decl_loc (const_tree);
3769 static void equate_decl_number_to_die (tree, dw_die_ref);
3770 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3771 static void print_spaces (FILE *);
3772 static void print_die (dw_die_ref, FILE *);
3773 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3774 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3775 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3776 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3777 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3778 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3779 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3780 struct md5_ctx *, int *);
3781 struct checksum_attributes;
3782 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3783 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3784 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3785 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3786 static bool same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3787 static bool same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3788 static bool same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3789 static bool same_die_p (dw_die_ref, dw_die_ref, int *);
3790 static bool is_type_die (dw_die_ref);
3791 static inline bool is_template_instantiation (dw_die_ref);
3792 static bool is_declaration_die (dw_die_ref);
3793 static bool should_move_die_to_comdat (dw_die_ref);
3794 static dw_die_ref clone_as_declaration (dw_die_ref);
3795 static dw_die_ref clone_die (dw_die_ref);
3796 static dw_die_ref clone_tree (dw_die_ref);
3797 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3798 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3799 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3800 static dw_die_ref generate_skeleton (dw_die_ref);
3801 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3802 dw_die_ref,
3803 dw_die_ref);
3804 static void break_out_comdat_types (dw_die_ref);
3805 static void copy_decls_for_unworthy_types (dw_die_ref);
3806
3807 static void add_sibling_attributes (dw_die_ref);
3808 static void output_location_lists (dw_die_ref);
3809 static int constant_size (unsigned HOST_WIDE_INT);
3810 static unsigned long size_of_die (dw_die_ref);
3811 static void calc_die_sizes (dw_die_ref);
3812 static void calc_base_type_die_sizes (void);
3813 static void mark_dies (dw_die_ref);
3814 static void unmark_dies (dw_die_ref);
3815 static void unmark_all_dies (dw_die_ref);
3816 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3817 static unsigned long size_of_aranges (void);
3818 static enum dwarf_form value_format (dw_attr_node *);
3819 static void output_value_format (dw_attr_node *);
3820 static void output_abbrev_section (void);
3821 static void output_die_abbrevs (unsigned long, dw_die_ref);
3822 static void output_die (dw_die_ref);
3823 static void output_compilation_unit_header (enum dwarf_unit_type);
3824 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3825 static void output_comdat_type_unit (comdat_type_node *, bool);
3826 static const char *dwarf2_name (tree, int);
3827 static void add_pubname (tree, dw_die_ref);
3828 static void add_enumerator_pubname (const char *, dw_die_ref);
3829 static void add_pubname_string (const char *, dw_die_ref);
3830 static void add_pubtype (tree, dw_die_ref);
3831 static void output_pubnames (vec<pubname_entry, va_gc> *);
3832 static void output_aranges (void);
3833 static unsigned int add_ranges (const_tree, bool = false);
3834 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3835 bool *, bool);
3836 static void output_ranges (void);
3837 static dw_line_info_table *new_line_info_table (void);
3838 static void output_line_info (bool);
3839 static void output_file_names (void);
3840 static bool is_base_type (tree);
3841 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3842 static int decl_quals (const_tree);
3843 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3844 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3845 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3846 static unsigned int debugger_reg_number (const_rtx);
3847 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3848 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3849 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3850 enum var_init_status);
3851 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3852 enum var_init_status);
3853 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3854 enum var_init_status);
3855 static bool is_based_loc (const_rtx);
3856 static bool resolve_one_addr (rtx *);
3857 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3858 enum var_init_status);
3859 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3860 enum var_init_status);
3861 struct loc_descr_context;
3862 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3863 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3864 static dw_loc_list_ref loc_list_from_tree (tree, int,
3865 struct loc_descr_context *);
3866 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3867 struct loc_descr_context *);
3868 static tree field_type (const_tree);
3869 static unsigned int simple_type_align_in_bits (const_tree);
3870 static unsigned int simple_decl_align_in_bits (const_tree);
3871 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3872 struct vlr_context;
3873 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3874 HOST_WIDE_INT *);
3875 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3876 dw_loc_list_ref);
3877 static void add_data_member_location_attribute (dw_die_ref, tree,
3878 struct vlr_context *);
3879 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3880 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3881 static void insert_wide_int (const wide_int_ref &, unsigned char *, int);
3882 static unsigned insert_float (const_rtx, unsigned char *);
3883 static rtx rtl_for_decl_location (tree);
3884 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3885 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3886 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3887 static void add_desc_attribute (dw_die_ref, tree);
3888 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3889 static void add_comp_dir_attribute (dw_die_ref);
3890 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3891 struct loc_descr_context *);
3892 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3893 struct loc_descr_context *);
3894 static void add_subscript_info (dw_die_ref, tree, bool);
3895 static void add_byte_size_attribute (dw_die_ref, tree);
3896 static void add_alignment_attribute (dw_die_ref, tree);
3897 static void add_bit_offset_attribute (dw_die_ref, tree);
3898 static void add_bit_size_attribute (dw_die_ref, tree);
3899 static void add_prototyped_attribute (dw_die_ref, tree);
3900 static void add_abstract_origin_attribute (dw_die_ref, tree);
3901 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3902 static void add_src_coords_attributes (dw_die_ref, tree);
3903 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3904 static void add_discr_value (dw_die_ref, dw_discr_value *);
3905 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3906 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3907 static dw_die_ref scope_die_for (tree, dw_die_ref);
3908 static inline bool local_scope_p (dw_die_ref);
3909 static inline bool class_scope_p (dw_die_ref);
3910 static inline bool class_or_namespace_scope_p (dw_die_ref);
3911 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3912 static void add_calling_convention_attribute (dw_die_ref, tree);
3913 static const char *type_tag (const_tree);
3914 static tree member_declared_type (const_tree);
3915 #if 0
3916 static const char *decl_start_label (tree);
3917 #endif
3918 static void gen_array_type_die (tree, dw_die_ref);
3919 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3920 #if 0
3921 static void gen_entry_point_die (tree, dw_die_ref);
3922 #endif
3923 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref, bool);
3924 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3925 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3926 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3927 static void gen_formal_types_die (tree, dw_die_ref);
3928 static void gen_subprogram_die (tree, dw_die_ref);
3929 static void gen_variable_die (tree, tree, dw_die_ref);
3930 static void gen_const_die (tree, dw_die_ref);
3931 static void gen_label_die (tree, dw_die_ref);
3932 static void gen_lexical_block_die (tree, dw_die_ref);
3933 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3934 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3935 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3936 static dw_die_ref gen_compile_unit_die (const char *);
3937 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3938 static void gen_member_die (tree, dw_die_ref);
3939 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3940 enum debug_info_usage);
3941 static void gen_subroutine_type_die (tree, dw_die_ref);
3942 static void gen_typedef_die (tree, dw_die_ref);
3943 static void gen_type_die (tree, dw_die_ref, bool = false);
3944 static void gen_block_die (tree, dw_die_ref);
3945 static void decls_for_scope (tree, dw_die_ref, bool = true);
3946 static bool is_naming_typedef_decl (const_tree);
3947 static inline dw_die_ref get_context_die (tree);
3948 static void gen_namespace_die (tree, dw_die_ref);
3949 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3950 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3951 static dw_die_ref force_decl_die (tree);
3952 static dw_die_ref force_type_die (tree);
3953 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3954 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3955 static struct dwarf_file_data * lookup_filename (const char *);
3956 static void retry_incomplete_types (void);
3957 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3958 static void gen_generic_params_dies (tree);
3959 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage,
3960 bool = false);
3961 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage,
3962 bool = false);
3963 static void splice_child_die (dw_die_ref, dw_die_ref);
3964 static int file_info_cmp (const void *, const void *);
3965 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3966 const char *, var_loc_view, const char *);
3967 static void output_loc_list (dw_loc_list_ref);
3968 static char *gen_internal_sym (const char *);
3969 static bool want_pubnames (void);
3970
3971 static void prune_unmark_dies (dw_die_ref);
3972 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3973 static void prune_unused_types_mark (dw_die_ref, int);
3974 static void prune_unused_types_walk (dw_die_ref);
3975 static void prune_unused_types_walk_attribs (dw_die_ref);
3976 static void prune_unused_types_prune (dw_die_ref);
3977 static void prune_unused_types (void);
3978 static int maybe_emit_file (struct dwarf_file_data *fd);
3979 static inline const char *AT_vms_delta1 (dw_attr_node *);
3980 static inline const char *AT_vms_delta2 (dw_attr_node *);
3981 #if VMS_DEBUGGING_INFO
3982 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3983 const char *, const char *);
3984 #endif
3985 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3986 static void gen_remaining_tmpl_value_param_die_attribute (void);
3987 static bool generic_type_p (tree);
3988 static void schedule_generic_params_dies_gen (tree t);
3989 static void gen_scheduled_generic_parms_dies (void);
3990 static void resolve_variable_values (void);
3991
3992 static const char *comp_dir_string (void);
3993
3994 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3995
3996 /* enum for tracking thread-local variables whose address is really an offset
3997 relative to the TLS pointer, which will need link-time relocation, but will
3998 not need relocation by the DWARF consumer. */
3999
4000 enum dtprel_bool
4001 {
4002 dtprel_false = 0,
4003 dtprel_true = 1
4004 };
4005
4006 /* Return the operator to use for an address of a variable. For dtprel_true, we
4007 use DW_OP_const*. For regular variables, which need both link-time
4008 relocation and consumer-level relocation (e.g., to account for shared objects
4009 loaded at a random address), we use DW_OP_addr*. */
4010
4011 static inline enum dwarf_location_atom
4012 dw_addr_op (enum dtprel_bool dtprel)
4013 {
4014 if (dtprel == dtprel_true)
4015 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
4016 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4017 else
4018 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
4019 }
4020
4021 /* Return a pointer to a newly allocated address location description. If
4022 dwarf_split_debug_info is true, then record the address with the appropriate
4023 relocation. */
4024 static inline dw_loc_descr_ref
4025 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4026 {
4027 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4028
4029 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4030 ref->dw_loc_oprnd1.v.val_addr = addr;
4031 ref->dtprel = dtprel;
4032 if (dwarf_split_debug_info)
4033 ref->dw_loc_oprnd1.val_entry
4034 = add_addr_table_entry (addr,
4035 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4036 else
4037 ref->dw_loc_oprnd1.val_entry = NULL;
4038
4039 return ref;
4040 }
4041
4042 /* Section names used to hold DWARF debugging information. */
4043
4044 #ifndef DEBUG_INFO_SECTION
4045 #define DEBUG_INFO_SECTION ".debug_info"
4046 #endif
4047 #ifndef DEBUG_DWO_INFO_SECTION
4048 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4049 #endif
4050 #ifndef DEBUG_LTO_INFO_SECTION
4051 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4052 #endif
4053 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4054 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4055 #endif
4056 #ifndef DEBUG_ABBREV_SECTION
4057 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4058 #endif
4059 #ifndef DEBUG_LTO_ABBREV_SECTION
4060 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4061 #endif
4062 #ifndef DEBUG_DWO_ABBREV_SECTION
4063 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4064 #endif
4065 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4066 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4067 #endif
4068 #ifndef DEBUG_ARANGES_SECTION
4069 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4070 #endif
4071 #ifndef DEBUG_ADDR_SECTION
4072 #define DEBUG_ADDR_SECTION ".debug_addr"
4073 #endif
4074 #ifndef DEBUG_MACINFO_SECTION
4075 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4076 #endif
4077 #ifndef DEBUG_LTO_MACINFO_SECTION
4078 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4079 #endif
4080 #ifndef DEBUG_DWO_MACINFO_SECTION
4081 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4082 #endif
4083 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4084 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4085 #endif
4086 #ifndef DEBUG_MACRO_SECTION
4087 #define DEBUG_MACRO_SECTION ".debug_macro"
4088 #endif
4089 #ifndef DEBUG_LTO_MACRO_SECTION
4090 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4091 #endif
4092 #ifndef DEBUG_DWO_MACRO_SECTION
4093 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4094 #endif
4095 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4096 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4097 #endif
4098 #ifndef DEBUG_LINE_SECTION
4099 #define DEBUG_LINE_SECTION ".debug_line"
4100 #endif
4101 #ifndef DEBUG_LTO_LINE_SECTION
4102 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4103 #endif
4104 #ifndef DEBUG_DWO_LINE_SECTION
4105 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4106 #endif
4107 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4108 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4109 #endif
4110 #ifndef DEBUG_LOC_SECTION
4111 #define DEBUG_LOC_SECTION ".debug_loc"
4112 #endif
4113 #ifndef DEBUG_DWO_LOC_SECTION
4114 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4115 #endif
4116 #ifndef DEBUG_LOCLISTS_SECTION
4117 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4118 #endif
4119 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4120 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4121 #endif
4122 #ifndef DEBUG_PUBNAMES_SECTION
4123 #define DEBUG_PUBNAMES_SECTION \
4124 ((debug_generate_pub_sections == 2) \
4125 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4126 #endif
4127 #ifndef DEBUG_PUBTYPES_SECTION
4128 #define DEBUG_PUBTYPES_SECTION \
4129 ((debug_generate_pub_sections == 2) \
4130 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4131 #endif
4132 #ifndef DEBUG_STR_OFFSETS_SECTION
4133 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4134 #endif
4135 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4136 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4137 #endif
4138 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4139 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4140 #endif
4141 #ifndef DEBUG_STR_SECTION
4142 #define DEBUG_STR_SECTION ".debug_str"
4143 #endif
4144 #ifndef DEBUG_LTO_STR_SECTION
4145 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4146 #endif
4147 #ifndef DEBUG_STR_DWO_SECTION
4148 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4149 #endif
4150 #ifndef DEBUG_LTO_STR_DWO_SECTION
4151 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4152 #endif
4153 #ifndef DEBUG_RANGES_SECTION
4154 #define DEBUG_RANGES_SECTION ".debug_ranges"
4155 #endif
4156 #ifndef DEBUG_RNGLISTS_SECTION
4157 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4158 #endif
4159 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4160 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4161 #endif
4162 #ifndef DEBUG_LINE_STR_SECTION
4163 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4164 #endif
4165 #ifndef DEBUG_LTO_LINE_STR_SECTION
4166 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4167 #endif
4168
4169 /* Section flags for .debug_str section. */
4170 #define DEBUG_STR_SECTION_FLAGS \
4171 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4172 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4173 : SECTION_DEBUG)
4174
4175 /* Section flags for .debug_str.dwo section. */
4176 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4177
4178 /* Attribute used to refer to the macro section. */
4179 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4180 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4181
4182 /* Labels we insert at beginning sections we can reference instead of
4183 the section names themselves. */
4184
4185 #ifndef TEXT_SECTION_LABEL
4186 #define TEXT_SECTION_LABEL "Ltext"
4187 #endif
4188 #ifndef COLD_TEXT_SECTION_LABEL
4189 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4190 #endif
4191 #ifndef DEBUG_LINE_SECTION_LABEL
4192 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4193 #endif
4194 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4195 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4196 #endif
4197 #ifndef DEBUG_INFO_SECTION_LABEL
4198 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4199 #endif
4200 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4201 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4202 #endif
4203 #ifndef DEBUG_ABBREV_SECTION_LABEL
4204 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4205 #endif
4206 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4207 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4208 #endif
4209 #ifndef DEBUG_ADDR_SECTION_LABEL
4210 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4211 #endif
4212 #ifndef DEBUG_LOC_SECTION_LABEL
4213 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4214 #endif
4215 #ifndef DEBUG_RANGES_SECTION_LABEL
4216 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4217 #endif
4218 #ifndef DEBUG_MACINFO_SECTION_LABEL
4219 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4220 #endif
4221 #ifndef DEBUG_MACRO_SECTION_LABEL
4222 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4223 #endif
4224 #define SKELETON_COMP_DIE_ABBREV 1
4225 #define SKELETON_TYPE_DIE_ABBREV 2
4226
4227 /* Definitions of defaults for formats and names of various special
4228 (artificial) labels which may be generated within this file (when the -g
4229 options is used and DWARF2_DEBUGGING_INFO is in effect.
4230 If necessary, these may be overridden from within the tm.h file, but
4231 typically, overriding these defaults is unnecessary. */
4232
4233 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4234 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4235 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4236 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4237 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4238 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4239 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4240 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4241 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4242 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4243 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4244 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4245 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4246 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4247 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4248
4249 #ifndef TEXT_END_LABEL
4250 #define TEXT_END_LABEL "Letext"
4251 #endif
4252 #ifndef COLD_END_LABEL
4253 #define COLD_END_LABEL "Letext_cold"
4254 #endif
4255 #ifndef BLOCK_BEGIN_LABEL
4256 #define BLOCK_BEGIN_LABEL "LBB"
4257 #endif
4258 #ifndef BLOCK_INLINE_ENTRY_LABEL
4259 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4260 #endif
4261 #ifndef BLOCK_END_LABEL
4262 #define BLOCK_END_LABEL "LBE"
4263 #endif
4264 #ifndef LINE_CODE_LABEL
4265 #define LINE_CODE_LABEL "LM"
4266 #endif
4267
4268
4269 /* Return the root of the DIE's built for the current compilation unit. */
4270 static dw_die_ref
4271 comp_unit_die (void)
4272 {
4273 if (!single_comp_unit_die)
4274 single_comp_unit_die = gen_compile_unit_die (NULL);
4275 return single_comp_unit_die;
4276 }
4277
4278 /* We allow a language front-end to designate a function that is to be
4279 called to "demangle" any name before it is put into a DIE. */
4280
4281 static const char *(*demangle_name_func) (const char *);
4282
4283 void
4284 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4285 {
4286 demangle_name_func = func;
4287 }
4288
4289 /* Test if rtl node points to a pseudo register. */
4290
4291 static inline bool
4292 is_pseudo_reg (const_rtx rtl)
4293 {
4294 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4295 || (GET_CODE (rtl) == SUBREG
4296 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4297 }
4298
4299 /* Return a reference to a type, with its const and volatile qualifiers
4300 removed. */
4301
4302 static inline tree
4303 type_main_variant (tree type)
4304 {
4305 type = TYPE_MAIN_VARIANT (type);
4306
4307 /* ??? There really should be only one main variant among any group of
4308 variants of a given type (and all of the MAIN_VARIANT values for all
4309 members of the group should point to that one type) but sometimes the C
4310 front-end messes this up for array types, so we work around that bug
4311 here. */
4312 if (TREE_CODE (type) == ARRAY_TYPE)
4313 while (type != TYPE_MAIN_VARIANT (type))
4314 type = TYPE_MAIN_VARIANT (type);
4315
4316 return type;
4317 }
4318
4319 /* Return true if the given type node represents a tagged type. */
4320
4321 static inline bool
4322 is_tagged_type (const_tree type)
4323 {
4324 enum tree_code code = TREE_CODE (type);
4325
4326 return (code == RECORD_TYPE || code == UNION_TYPE
4327 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4328 }
4329
4330 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4331
4332 static void
4333 get_ref_die_offset_label (char *label, dw_die_ref ref)
4334 {
4335 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4336 }
4337
4338 /* Return die_offset of a DIE reference to a base type. */
4339
4340 static unsigned long int
4341 get_base_type_offset (dw_die_ref ref)
4342 {
4343 if (ref->die_offset)
4344 return ref->die_offset;
4345 if (comp_unit_die ()->die_abbrev)
4346 {
4347 calc_base_type_die_sizes ();
4348 gcc_assert (ref->die_offset);
4349 }
4350 return ref->die_offset;
4351 }
4352
4353 /* Return die_offset of a DIE reference other than base type. */
4354
4355 static unsigned long int
4356 get_ref_die_offset (dw_die_ref ref)
4357 {
4358 gcc_assert (ref->die_offset);
4359 return ref->die_offset;
4360 }
4361
4362 /* Convert a DIE tag into its string name. */
4363
4364 static const char *
4365 dwarf_tag_name (unsigned int tag)
4366 {
4367 const char *name = get_DW_TAG_name (tag);
4368
4369 if (name != NULL)
4370 return name;
4371
4372 return "DW_TAG_<unknown>";
4373 }
4374
4375 /* Convert a DWARF attribute code into its string name. */
4376
4377 static const char *
4378 dwarf_attr_name (unsigned int attr)
4379 {
4380 const char *name;
4381
4382 switch (attr)
4383 {
4384 #if VMS_DEBUGGING_INFO
4385 case DW_AT_HP_prologue:
4386 return "DW_AT_HP_prologue";
4387 #else
4388 case DW_AT_MIPS_loop_unroll_factor:
4389 return "DW_AT_MIPS_loop_unroll_factor";
4390 #endif
4391
4392 #if VMS_DEBUGGING_INFO
4393 case DW_AT_HP_epilogue:
4394 return "DW_AT_HP_epilogue";
4395 #else
4396 case DW_AT_MIPS_stride:
4397 return "DW_AT_MIPS_stride";
4398 #endif
4399 }
4400
4401 name = get_DW_AT_name (attr);
4402
4403 if (name != NULL)
4404 return name;
4405
4406 return "DW_AT_<unknown>";
4407 }
4408
4409 /* Convert a DWARF value form code into its string name. */
4410
4411 static const char *
4412 dwarf_form_name (unsigned int form)
4413 {
4414 const char *name = get_DW_FORM_name (form);
4415
4416 if (name != NULL)
4417 return name;
4418
4419 return "DW_FORM_<unknown>";
4420 }
4421
4422 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4423 instance of an inlined instance of a decl which is local to an inline
4424 function, so we have to trace all of the way back through the origin chain
4425 to find out what sort of node actually served as the original seed for the
4426 given block. */
4427
4428 static tree
4429 decl_ultimate_origin (const_tree decl)
4430 {
4431 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4432 return NULL_TREE;
4433
4434 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4435 we're trying to output the abstract instance of this function. */
4436 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4437 return NULL_TREE;
4438
4439 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4440 most distant ancestor, this should never happen. */
4441 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4442
4443 return DECL_ABSTRACT_ORIGIN (decl);
4444 }
4445
4446 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4447 of a virtual function may refer to a base class, so we check the 'this'
4448 parameter. */
4449
4450 static tree
4451 decl_class_context (tree decl)
4452 {
4453 tree context = NULL_TREE;
4454
4455 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4456 context = DECL_CONTEXT (decl);
4457 else
4458 context = TYPE_MAIN_VARIANT
4459 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4460
4461 if (context && !TYPE_P (context))
4462 context = NULL_TREE;
4463
4464 return context;
4465 }
4466
4467 /* Add an attribute/value pair to a DIE. */
4468
4469 static inline void
4470 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4471 {
4472 /* Maybe this should be an assert? */
4473 if (die == NULL)
4474 return;
4475
4476 if (flag_checking)
4477 {
4478 /* Check we do not add duplicate attrs. Can't use get_AT here
4479 because that recurses to the specification/abstract origin DIE. */
4480 dw_attr_node *a;
4481 unsigned ix;
4482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4483 gcc_assert (a->dw_attr != attr->dw_attr);
4484 }
4485
4486 vec_safe_reserve (die->die_attr, 1);
4487 vec_safe_push (die->die_attr, *attr);
4488 }
4489
4490 enum dw_val_class
4491 AT_class (dw_attr_node *a)
4492 {
4493 return a->dw_attr_val.val_class;
4494 }
4495
4496 /* Return the index for any attribute that will be referenced with a
4497 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4498 indices are stored in dw_attr_val.v.val_str for reference counting
4499 pruning. */
4500
4501 static inline unsigned int
4502 AT_index (dw_attr_node *a)
4503 {
4504 if (AT_class (a) == dw_val_class_str)
4505 return a->dw_attr_val.v.val_str->index;
4506 else if (a->dw_attr_val.val_entry != NULL)
4507 return a->dw_attr_val.val_entry->index;
4508 return NOT_INDEXED;
4509 }
4510
4511 /* Add a flag value attribute to a DIE. */
4512
4513 static inline void
4514 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4515 {
4516 dw_attr_node attr;
4517
4518 attr.dw_attr = attr_kind;
4519 attr.dw_attr_val.val_class = dw_val_class_flag;
4520 attr.dw_attr_val.val_entry = NULL;
4521 attr.dw_attr_val.v.val_flag = flag;
4522 add_dwarf_attr (die, &attr);
4523 }
4524
4525 static inline unsigned
4526 AT_flag (dw_attr_node *a)
4527 {
4528 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4529 return a->dw_attr_val.v.val_flag;
4530 }
4531
4532 /* Add a signed integer attribute value to a DIE. */
4533
4534 static inline void
4535 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4536 {
4537 dw_attr_node attr;
4538
4539 attr.dw_attr = attr_kind;
4540 attr.dw_attr_val.val_class = dw_val_class_const;
4541 attr.dw_attr_val.val_entry = NULL;
4542 attr.dw_attr_val.v.val_int = int_val;
4543 add_dwarf_attr (die, &attr);
4544 }
4545
4546 HOST_WIDE_INT
4547 AT_int (dw_attr_node *a)
4548 {
4549 gcc_assert (a && (AT_class (a) == dw_val_class_const
4550 || AT_class (a) == dw_val_class_const_implicit));
4551 return a->dw_attr_val.v.val_int;
4552 }
4553
4554 /* Add an unsigned integer attribute value to a DIE. */
4555
4556 static inline void
4557 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4558 unsigned HOST_WIDE_INT unsigned_val)
4559 {
4560 dw_attr_node attr;
4561
4562 attr.dw_attr = attr_kind;
4563 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4564 attr.dw_attr_val.val_entry = NULL;
4565 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4566 add_dwarf_attr (die, &attr);
4567 }
4568
4569 unsigned HOST_WIDE_INT
4570 AT_unsigned (dw_attr_node *a)
4571 {
4572 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4573 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4574 return a->dw_attr_val.v.val_unsigned;
4575 }
4576
4577 dw_wide_int *
4578 alloc_dw_wide_int (const wide_int_ref &w)
4579 {
4580 dw_wide_int *p
4581 = (dw_wide_int *) ggc_internal_alloc (sizeof (dw_wide_int)
4582 + ((w.get_len () - 1)
4583 * sizeof (HOST_WIDE_INT)));
4584 p->precision = w.get_precision ();
4585 p->len = w.get_len ();
4586 memcpy (p->val, w.get_val (), p->len * sizeof (HOST_WIDE_INT));
4587 return p;
4588 }
4589
4590 /* Add an unsigned wide integer attribute value to a DIE. */
4591
4592 static inline void
4593 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4594 const wide_int_ref &w)
4595 {
4596 dw_attr_node attr;
4597
4598 attr.dw_attr = attr_kind;
4599 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4600 attr.dw_attr_val.val_entry = NULL;
4601 attr.dw_attr_val.v.val_wide = alloc_dw_wide_int (w);
4602 add_dwarf_attr (die, &attr);
4603 }
4604
4605 /* Add an unsigned double integer attribute value to a DIE. */
4606
4607 static inline void
4608 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4609 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4610 {
4611 dw_attr_node attr;
4612
4613 attr.dw_attr = attr_kind;
4614 attr.dw_attr_val.val_class = dw_val_class_const_double;
4615 attr.dw_attr_val.val_entry = NULL;
4616 attr.dw_attr_val.v.val_double.high = high;
4617 attr.dw_attr_val.v.val_double.low = low;
4618 add_dwarf_attr (die, &attr);
4619 }
4620
4621 /* Add a floating point attribute value to a DIE and return it. */
4622
4623 static inline void
4624 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4625 unsigned int length, unsigned int elt_size, unsigned char *array)
4626 {
4627 dw_attr_node attr;
4628
4629 attr.dw_attr = attr_kind;
4630 attr.dw_attr_val.val_class = dw_val_class_vec;
4631 attr.dw_attr_val.val_entry = NULL;
4632 attr.dw_attr_val.v.val_vec.length = length;
4633 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4634 attr.dw_attr_val.v.val_vec.array = array;
4635 add_dwarf_attr (die, &attr);
4636 }
4637
4638 /* Add an 8-byte data attribute value to a DIE. */
4639
4640 static inline void
4641 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4642 unsigned char data8[8])
4643 {
4644 dw_attr_node attr;
4645
4646 attr.dw_attr = attr_kind;
4647 attr.dw_attr_val.val_class = dw_val_class_data8;
4648 attr.dw_attr_val.val_entry = NULL;
4649 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4650 add_dwarf_attr (die, &attr);
4651 }
4652
4653 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4654 dwarf_split_debug_info, address attributes in dies destined for the
4655 final executable have force_direct set to avoid using indexed
4656 references. */
4657
4658 static inline void
4659 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4660 bool force_direct)
4661 {
4662 dw_attr_node attr;
4663 char * lbl_id;
4664
4665 lbl_id = xstrdup (lbl_low);
4666 attr.dw_attr = DW_AT_low_pc;
4667 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4668 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4669 if (dwarf_split_debug_info && !force_direct)
4670 attr.dw_attr_val.val_entry
4671 = add_addr_table_entry (lbl_id, ate_kind_label);
4672 else
4673 attr.dw_attr_val.val_entry = NULL;
4674 add_dwarf_attr (die, &attr);
4675
4676 attr.dw_attr = DW_AT_high_pc;
4677 if (dwarf_version < 4)
4678 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4679 else
4680 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4681 lbl_id = xstrdup (lbl_high);
4682 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4683 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4684 && dwarf_split_debug_info && !force_direct)
4685 attr.dw_attr_val.val_entry
4686 = add_addr_table_entry (lbl_id, ate_kind_label);
4687 else
4688 attr.dw_attr_val.val_entry = NULL;
4689 add_dwarf_attr (die, &attr);
4690 }
4691
4692 /* Hash and equality functions for debug_str_hash. */
4693
4694 hashval_t
4695 indirect_string_hasher::hash (indirect_string_node *x)
4696 {
4697 return htab_hash_string (x->str);
4698 }
4699
4700 bool
4701 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4702 {
4703 return strcmp (x1->str, x2) == 0;
4704 }
4705
4706 /* Add STR to the given string hash table. */
4707
4708 static struct indirect_string_node *
4709 find_AT_string_in_table (const char *str,
4710 hash_table<indirect_string_hasher> *table,
4711 enum insert_option insert = INSERT)
4712 {
4713 struct indirect_string_node *node;
4714
4715 indirect_string_node **slot
4716 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4717 if (*slot == NULL)
4718 {
4719 node = ggc_cleared_alloc<indirect_string_node> ();
4720 node->str = ggc_strdup (str);
4721 *slot = node;
4722 }
4723 else
4724 node = *slot;
4725
4726 node->refcount++;
4727 return node;
4728 }
4729
4730 /* Add STR to the indirect string hash table. */
4731
4732 static struct indirect_string_node *
4733 find_AT_string (const char *str, enum insert_option insert = INSERT)
4734 {
4735 if (! debug_str_hash)
4736 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4737
4738 return find_AT_string_in_table (str, debug_str_hash, insert);
4739 }
4740
4741 /* Add a string attribute value to a DIE. */
4742
4743 static inline void
4744 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4745 {
4746 dw_attr_node attr;
4747 struct indirect_string_node *node;
4748
4749 node = find_AT_string (str);
4750
4751 attr.dw_attr = attr_kind;
4752 attr.dw_attr_val.val_class = dw_val_class_str;
4753 attr.dw_attr_val.val_entry = NULL;
4754 attr.dw_attr_val.v.val_str = node;
4755 add_dwarf_attr (die, &attr);
4756 }
4757
4758 static inline const char *
4759 AT_string (dw_attr_node *a)
4760 {
4761 gcc_assert (a && AT_class (a) == dw_val_class_str);
4762 return a->dw_attr_val.v.val_str->str;
4763 }
4764
4765 /* Call this function directly to bypass AT_string_form's logic to put
4766 the string inline in the die. */
4767
4768 static void
4769 set_indirect_string (struct indirect_string_node *node)
4770 {
4771 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4772 /* Already indirect is a no op. */
4773 if (node->form == DW_FORM_strp
4774 || node->form == DW_FORM_line_strp
4775 || node->form == dwarf_FORM (DW_FORM_strx))
4776 {
4777 gcc_assert (node->label);
4778 return;
4779 }
4780 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4781 ++dw2_string_counter;
4782 node->label = xstrdup (label);
4783
4784 if (!dwarf_split_debug_info)
4785 {
4786 node->form = DW_FORM_strp;
4787 node->index = NOT_INDEXED;
4788 }
4789 else
4790 {
4791 node->form = dwarf_FORM (DW_FORM_strx);
4792 node->index = NO_INDEX_ASSIGNED;
4793 }
4794 }
4795
4796 /* A helper function for dwarf2out_finish, called to reset indirect
4797 string decisions done for early LTO dwarf output before fat object
4798 dwarf output. */
4799
4800 int
4801 reset_indirect_string (indirect_string_node **h, void *)
4802 {
4803 struct indirect_string_node *node = *h;
4804 if (node->form == DW_FORM_strp
4805 || node->form == DW_FORM_line_strp
4806 || node->form == dwarf_FORM (DW_FORM_strx))
4807 {
4808 free (node->label);
4809 node->label = NULL;
4810 node->form = (dwarf_form) 0;
4811 node->index = 0;
4812 }
4813 return 1;
4814 }
4815
4816 /* Add a string representing a file or filepath attribute value to a DIE. */
4817
4818 static inline void
4819 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4820 const char *str)
4821 {
4822 if (! asm_outputs_debug_line_str ())
4823 add_AT_string (die, attr_kind, str);
4824 else
4825 {
4826 dw_attr_node attr;
4827 struct indirect_string_node *node;
4828
4829 if (!debug_line_str_hash)
4830 debug_line_str_hash
4831 = hash_table<indirect_string_hasher>::create_ggc (10);
4832
4833 node = find_AT_string_in_table (str, debug_line_str_hash);
4834 set_indirect_string (node);
4835 node->form = DW_FORM_line_strp;
4836
4837 attr.dw_attr = attr_kind;
4838 attr.dw_attr_val.val_class = dw_val_class_str;
4839 attr.dw_attr_val.val_entry = NULL;
4840 attr.dw_attr_val.v.val_str = node;
4841 add_dwarf_attr (die, &attr);
4842 }
4843 }
4844
4845 /* Find out whether a string should be output inline in DIE
4846 or out-of-line in .debug_str section. */
4847
4848 static enum dwarf_form
4849 find_string_form (struct indirect_string_node *node)
4850 {
4851 unsigned int len;
4852
4853 if (node->form)
4854 return node->form;
4855
4856 len = strlen (node->str) + 1;
4857
4858 /* If the string is shorter or equal to the size of the reference, it is
4859 always better to put it inline. */
4860 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4861 return node->form = DW_FORM_string;
4862
4863 /* If we cannot expect the linker to merge strings in .debug_str
4864 section, only put it into .debug_str if it is worth even in this
4865 single module. */
4866 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4867 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4868 && (len - dwarf_offset_size) * node->refcount <= len))
4869 return node->form = DW_FORM_string;
4870
4871 set_indirect_string (node);
4872
4873 return node->form;
4874 }
4875
4876 /* Find out whether the string referenced from the attribute should be
4877 output inline in DIE or out-of-line in .debug_str section. */
4878
4879 static enum dwarf_form
4880 AT_string_form (dw_attr_node *a)
4881 {
4882 gcc_assert (a && AT_class (a) == dw_val_class_str);
4883 return find_string_form (a->dw_attr_val.v.val_str);
4884 }
4885
4886 /* Add a DIE reference attribute value to a DIE. */
4887
4888 static inline void
4889 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4890 {
4891 dw_attr_node attr;
4892 gcc_checking_assert (targ_die != NULL);
4893 gcc_assert (targ_die != die
4894 || (attr_kind != DW_AT_abstract_origin
4895 && attr_kind != DW_AT_specification));
4896
4897 /* With LTO we can end up trying to reference something we didn't create
4898 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4899 if (targ_die == NULL)
4900 return;
4901
4902 attr.dw_attr = attr_kind;
4903 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4904 attr.dw_attr_val.val_entry = NULL;
4905 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4906 attr.dw_attr_val.v.val_die_ref.external = 0;
4907 add_dwarf_attr (die, &attr);
4908 }
4909
4910 /* Change DIE reference REF to point to NEW_DIE instead. */
4911
4912 static inline void
4913 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4914 {
4915 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4916 ref->dw_attr_val.v.val_die_ref.die = new_die;
4917 ref->dw_attr_val.v.val_die_ref.external = 0;
4918 }
4919
4920 /* Add an AT_specification attribute to a DIE, and also make the back
4921 pointer from the specification to the definition. */
4922
4923 static inline void
4924 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4925 {
4926 add_AT_die_ref (die, DW_AT_specification, targ_die);
4927 gcc_assert (!targ_die->die_definition);
4928 targ_die->die_definition = die;
4929 }
4930
4931 static inline dw_die_ref
4932 AT_ref (dw_attr_node *a)
4933 {
4934 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4935 return a->dw_attr_val.v.val_die_ref.die;
4936 }
4937
4938 static inline int
4939 AT_ref_external (dw_attr_node *a)
4940 {
4941 if (a && AT_class (a) == dw_val_class_die_ref)
4942 return a->dw_attr_val.v.val_die_ref.external;
4943
4944 return 0;
4945 }
4946
4947 static inline void
4948 set_AT_ref_external (dw_attr_node *a, int i)
4949 {
4950 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4951 a->dw_attr_val.v.val_die_ref.external = i;
4952 }
4953
4954 /* Add a location description attribute value to a DIE. */
4955
4956 static inline void
4957 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4958 {
4959 dw_attr_node attr;
4960
4961 attr.dw_attr = attr_kind;
4962 attr.dw_attr_val.val_class = dw_val_class_loc;
4963 attr.dw_attr_val.val_entry = NULL;
4964 attr.dw_attr_val.v.val_loc = loc;
4965 add_dwarf_attr (die, &attr);
4966 }
4967
4968 dw_loc_descr_ref
4969 AT_loc (dw_attr_node *a)
4970 {
4971 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4972 return a->dw_attr_val.v.val_loc;
4973 }
4974
4975 static inline void
4976 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4977 {
4978 dw_attr_node attr;
4979
4980 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4981 return;
4982
4983 attr.dw_attr = attr_kind;
4984 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4985 attr.dw_attr_val.val_entry = NULL;
4986 attr.dw_attr_val.v.val_loc_list = loc_list;
4987 add_dwarf_attr (die, &attr);
4988 have_location_lists = true;
4989 }
4990
4991 static inline dw_loc_list_ref
4992 AT_loc_list (dw_attr_node *a)
4993 {
4994 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4995 return a->dw_attr_val.v.val_loc_list;
4996 }
4997
4998 /* Add a view list attribute to DIE. It must have a DW_AT_location
4999 attribute, because the view list complements the location list. */
5000
5001 static inline void
5002 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5003 {
5004 dw_attr_node attr;
5005
5006 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5007 return;
5008
5009 attr.dw_attr = attr_kind;
5010 attr.dw_attr_val.val_class = dw_val_class_view_list;
5011 attr.dw_attr_val.val_entry = NULL;
5012 attr.dw_attr_val.v.val_view_list = die;
5013 add_dwarf_attr (die, &attr);
5014 gcc_checking_assert (get_AT (die, DW_AT_location));
5015 gcc_assert (have_location_lists);
5016 }
5017
5018 /* Return a pointer to the location list referenced by the attribute.
5019 If the named attribute is a view list, look up the corresponding
5020 DW_AT_location attribute and return its location list. */
5021
5022 static inline dw_loc_list_ref *
5023 AT_loc_list_ptr (dw_attr_node *a)
5024 {
5025 gcc_assert (a);
5026 switch (AT_class (a))
5027 {
5028 case dw_val_class_loc_list:
5029 return &a->dw_attr_val.v.val_loc_list;
5030 case dw_val_class_view_list:
5031 {
5032 dw_attr_node *l;
5033 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5034 if (!l)
5035 return NULL;
5036 gcc_checking_assert (l + 1 == a);
5037 return AT_loc_list_ptr (l);
5038 }
5039 default:
5040 gcc_unreachable ();
5041 }
5042 }
5043
5044 /* Return the location attribute value associated with a view list
5045 attribute value. */
5046
5047 static inline dw_val_node *
5048 view_list_to_loc_list_val_node (dw_val_node *val)
5049 {
5050 gcc_assert (val->val_class == dw_val_class_view_list);
5051 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5052 if (!loc)
5053 return NULL;
5054 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5055 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5056 return &loc->dw_attr_val;
5057 }
5058
5059 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5060 {
5061 static hashval_t hash (addr_table_entry *);
5062 static bool equal (addr_table_entry *, addr_table_entry *);
5063 };
5064
5065 /* Table of entries into the .debug_addr section. */
5066
5067 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5068
5069 /* Hash an address_table_entry. */
5070
5071 hashval_t
5072 addr_hasher::hash (addr_table_entry *a)
5073 {
5074 inchash::hash hstate;
5075 switch (a->kind)
5076 {
5077 case ate_kind_rtx:
5078 hstate.add_int (0);
5079 break;
5080 case ate_kind_rtx_dtprel:
5081 hstate.add_int (1);
5082 break;
5083 case ate_kind_label:
5084 return htab_hash_string (a->addr.label);
5085 default:
5086 gcc_unreachable ();
5087 }
5088 inchash::add_rtx (a->addr.rtl, hstate);
5089 return hstate.end ();
5090 }
5091
5092 /* Determine equality for two address_table_entries. */
5093
5094 bool
5095 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5096 {
5097 if (a1->kind != a2->kind)
5098 return false;
5099 switch (a1->kind)
5100 {
5101 case ate_kind_rtx:
5102 case ate_kind_rtx_dtprel:
5103 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5104 case ate_kind_label:
5105 return strcmp (a1->addr.label, a2->addr.label) == 0;
5106 default:
5107 gcc_unreachable ();
5108 }
5109 }
5110
5111 /* Initialize an addr_table_entry. */
5112
5113 void
5114 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5115 {
5116 e->kind = kind;
5117 switch (kind)
5118 {
5119 case ate_kind_rtx:
5120 case ate_kind_rtx_dtprel:
5121 e->addr.rtl = (rtx) addr;
5122 break;
5123 case ate_kind_label:
5124 e->addr.label = (char *) addr;
5125 break;
5126 }
5127 e->refcount = 0;
5128 e->index = NO_INDEX_ASSIGNED;
5129 }
5130
5131 /* Add attr to the address table entry to the table. Defer setting an
5132 index until output time. */
5133
5134 static addr_table_entry *
5135 add_addr_table_entry (void *addr, enum ate_kind kind)
5136 {
5137 addr_table_entry *node;
5138 addr_table_entry finder;
5139
5140 gcc_assert (dwarf_split_debug_info);
5141 if (! addr_index_table)
5142 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5143 init_addr_table_entry (&finder, kind, addr);
5144 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5145
5146 if (*slot == HTAB_EMPTY_ENTRY)
5147 {
5148 node = ggc_cleared_alloc<addr_table_entry> ();
5149 init_addr_table_entry (node, kind, addr);
5150 *slot = node;
5151 }
5152 else
5153 node = *slot;
5154
5155 node->refcount++;
5156 return node;
5157 }
5158
5159 /* Remove an entry from the addr table by decrementing its refcount.
5160 Strictly, decrementing the refcount would be enough, but the
5161 assertion that the entry is actually in the table has found
5162 bugs. */
5163
5164 static void
5165 remove_addr_table_entry (addr_table_entry *entry)
5166 {
5167 gcc_assert (dwarf_split_debug_info && addr_index_table);
5168 /* After an index is assigned, the table is frozen. */
5169 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5170 entry->refcount--;
5171 }
5172
5173 /* Given a location list, remove all addresses it refers to from the
5174 address_table. */
5175
5176 static void
5177 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5178 {
5179 for (; descr; descr = descr->dw_loc_next)
5180 if (descr->dw_loc_oprnd1.val_entry != NULL)
5181 {
5182 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5183 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5184 }
5185 }
5186
5187 /* A helper function for dwarf2out_finish called through
5188 htab_traverse. Assign an addr_table_entry its index. All entries
5189 must be collected into the table when this function is called,
5190 because the indexing code relies on htab_traverse to traverse nodes
5191 in the same order for each run. */
5192
5193 int
5194 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5195 {
5196 addr_table_entry *node = *h;
5197
5198 /* Don't index unreferenced nodes. */
5199 if (node->refcount == 0)
5200 return 1;
5201
5202 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5203 node->index = *index;
5204 *index += 1;
5205
5206 return 1;
5207 }
5208
5209 /* Return the tag of a given DIE. */
5210
5211 enum dwarf_tag
5212 dw_get_die_tag (dw_die_ref die)
5213 {
5214 return die->die_tag;
5215 }
5216
5217 /* Return a reference to the children list of a given DIE. */
5218
5219 dw_die_ref
5220 dw_get_die_child (dw_die_ref die)
5221 {
5222 return die->die_child;
5223 }
5224
5225 /* Return a reference to the sibling of a given DIE. */
5226
5227 dw_die_ref
5228 dw_get_die_sib (dw_die_ref die)
5229 {
5230 return die->die_sib;
5231 }
5232
5233 /* Add an address constant attribute value to a DIE. When using
5234 dwarf_split_debug_info, address attributes in dies destined for the
5235 final executable should be direct references--setting the parameter
5236 force_direct ensures this behavior. */
5237
5238 static inline void
5239 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5240 bool force_direct)
5241 {
5242 dw_attr_node attr;
5243
5244 attr.dw_attr = attr_kind;
5245 attr.dw_attr_val.val_class = dw_val_class_addr;
5246 attr.dw_attr_val.v.val_addr = addr;
5247 if (dwarf_split_debug_info && !force_direct)
5248 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5249 else
5250 attr.dw_attr_val.val_entry = NULL;
5251 add_dwarf_attr (die, &attr);
5252 }
5253
5254 /* Get the RTX from to an address DIE attribute. */
5255
5256 static inline rtx
5257 AT_addr (dw_attr_node *a)
5258 {
5259 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5260 return a->dw_attr_val.v.val_addr;
5261 }
5262
5263 /* Add a file attribute value to a DIE. */
5264
5265 static inline void
5266 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5267 struct dwarf_file_data *fd)
5268 {
5269 dw_attr_node attr;
5270
5271 attr.dw_attr = attr_kind;
5272 attr.dw_attr_val.val_class = dw_val_class_file;
5273 attr.dw_attr_val.val_entry = NULL;
5274 attr.dw_attr_val.v.val_file = fd;
5275 add_dwarf_attr (die, &attr);
5276 }
5277
5278 /* Get the dwarf_file_data from a file DIE attribute. */
5279
5280 static inline struct dwarf_file_data *
5281 AT_file (dw_attr_node *a)
5282 {
5283 gcc_assert (a && (AT_class (a) == dw_val_class_file
5284 || AT_class (a) == dw_val_class_file_implicit));
5285 return a->dw_attr_val.v.val_file;
5286 }
5287
5288 #if VMS_DEBUGGING_INFO
5289 /* Add a vms delta attribute value to a DIE. */
5290
5291 static inline void
5292 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5293 const char *lbl1, const char *lbl2)
5294 {
5295 dw_attr_node attr;
5296
5297 attr.dw_attr = attr_kind;
5298 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5299 attr.dw_attr_val.val_entry = NULL;
5300 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5301 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5302 add_dwarf_attr (die, &attr);
5303 }
5304 #endif
5305
5306 /* Add a symbolic view identifier attribute value to a DIE. */
5307
5308 static inline void
5309 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5310 const char *view_label)
5311 {
5312 dw_attr_node attr;
5313
5314 attr.dw_attr = attr_kind;
5315 attr.dw_attr_val.val_class = dw_val_class_symview;
5316 attr.dw_attr_val.val_entry = NULL;
5317 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5318 add_dwarf_attr (die, &attr);
5319 }
5320
5321 /* Add a label identifier attribute value to a DIE. */
5322
5323 static inline void
5324 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5325 const char *lbl_id, int offset)
5326 {
5327 dw_attr_node attr;
5328
5329 attr.dw_attr = attr_kind;
5330 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5331 attr.dw_attr_val.val_entry = NULL;
5332 if (!offset)
5333 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5334 else
5335 attr.dw_attr_val.v.val_lbl_id = xasprintf ("%s%+i", lbl_id, offset);
5336 if (dwarf_split_debug_info)
5337 attr.dw_attr_val.val_entry
5338 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5339 ate_kind_label);
5340 add_dwarf_attr (die, &attr);
5341 }
5342
5343 /* Add a section offset attribute value to a DIE, an offset into the
5344 debug_line section. */
5345
5346 static inline void
5347 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5348 const char *label)
5349 {
5350 dw_attr_node attr;
5351
5352 attr.dw_attr = attr_kind;
5353 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5354 attr.dw_attr_val.val_entry = NULL;
5355 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5356 add_dwarf_attr (die, &attr);
5357 }
5358
5359 /* Add a section offset attribute value to a DIE, an offset into the
5360 debug_macinfo section. */
5361
5362 static inline void
5363 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5364 const char *label)
5365 {
5366 dw_attr_node attr;
5367
5368 attr.dw_attr = attr_kind;
5369 attr.dw_attr_val.val_class = dw_val_class_macptr;
5370 attr.dw_attr_val.val_entry = NULL;
5371 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5372 add_dwarf_attr (die, &attr);
5373 }
5374
5375 /* Add a range_list attribute value to a DIE. When using
5376 dwarf_split_debug_info, address attributes in dies destined for the
5377 final executable should be direct references--setting the parameter
5378 force_direct ensures this behavior. */
5379
5380 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5381 #define RELOCATED_OFFSET (NULL)
5382
5383 static void
5384 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5385 long unsigned int offset, bool force_direct)
5386 {
5387 dw_attr_node attr;
5388
5389 attr.dw_attr = attr_kind;
5390 attr.dw_attr_val.val_class = dw_val_class_range_list;
5391 /* For the range_list attribute, use val_entry to store whether the
5392 offset should follow split-debug-info or normal semantics. This
5393 value is read in output_range_list_offset. */
5394 if (dwarf_split_debug_info && !force_direct)
5395 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5396 else
5397 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5398 attr.dw_attr_val.v.val_offset = offset;
5399 add_dwarf_attr (die, &attr);
5400 }
5401
5402 /* Return the start label of a delta attribute. */
5403
5404 static inline const char *
5405 AT_vms_delta1 (dw_attr_node *a)
5406 {
5407 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5408 return a->dw_attr_val.v.val_vms_delta.lbl1;
5409 }
5410
5411 /* Return the end label of a delta attribute. */
5412
5413 static inline const char *
5414 AT_vms_delta2 (dw_attr_node *a)
5415 {
5416 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5417 return a->dw_attr_val.v.val_vms_delta.lbl2;
5418 }
5419
5420 static inline const char *
5421 AT_lbl (dw_attr_node *a)
5422 {
5423 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5424 || AT_class (a) == dw_val_class_lineptr
5425 || AT_class (a) == dw_val_class_macptr
5426 || AT_class (a) == dw_val_class_loclistsptr
5427 || AT_class (a) == dw_val_class_high_pc));
5428 return a->dw_attr_val.v.val_lbl_id;
5429 }
5430
5431 /* Get the attribute of type attr_kind. */
5432
5433 dw_attr_node *
5434 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5435 {
5436 dw_attr_node *a;
5437 unsigned ix;
5438 dw_die_ref spec = NULL;
5439
5440 if (! die)
5441 return NULL;
5442
5443 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5444 if (a->dw_attr == attr_kind)
5445 return a;
5446 else if (a->dw_attr == DW_AT_specification
5447 || a->dw_attr == DW_AT_abstract_origin)
5448 spec = AT_ref (a);
5449
5450 if (spec)
5451 return get_AT (spec, attr_kind);
5452
5453 return NULL;
5454 }
5455
5456 /* Returns the parent of the declaration of DIE. */
5457
5458 dw_die_ref
5459 dw_get_die_parent (dw_die_ref die)
5460 {
5461 dw_die_ref t;
5462
5463 if (!die)
5464 return NULL;
5465
5466 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5467 || (t = get_AT_ref (die, DW_AT_specification)))
5468 die = t;
5469
5470 return die->die_parent;
5471 }
5472
5473 /* Return the "low pc" attribute value, typically associated with a subprogram
5474 DIE. Return null if the "low pc" attribute is either not present, or if it
5475 cannot be represented as an assembler label identifier. */
5476
5477 static inline const char *
5478 get_AT_low_pc (dw_die_ref die)
5479 {
5480 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5481
5482 return a ? AT_lbl (a) : NULL;
5483 }
5484
5485 /* Return the value of the string attribute designated by ATTR_KIND, or
5486 NULL if it is not present. */
5487
5488 const char *
5489 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5490 {
5491 dw_attr_node *a = get_AT (die, attr_kind);
5492
5493 return a ? AT_string (a) : NULL;
5494 }
5495
5496 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5497 if it is not present. */
5498
5499 int
5500 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5501 {
5502 dw_attr_node *a = get_AT (die, attr_kind);
5503
5504 return a ? AT_flag (a) : 0;
5505 }
5506
5507 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5508 if it is not present. */
5509
5510 unsigned
5511 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5512 {
5513 dw_attr_node *a = get_AT (die, attr_kind);
5514
5515 return a ? AT_unsigned (a) : 0;
5516 }
5517
5518 dw_die_ref
5519 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5520 {
5521 dw_attr_node *a = get_AT (die, attr_kind);
5522
5523 return a ? AT_ref (a) : NULL;
5524 }
5525
5526 struct dwarf_file_data *
5527 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5528 {
5529 dw_attr_node *a = get_AT (die, attr_kind);
5530
5531 return a ? AT_file (a) : NULL;
5532 }
5533
5534 /* Return TRUE if the language is C. */
5535
5536 static inline bool
5537 is_c (void)
5538 {
5539 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5540
5541 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5542 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5543
5544
5545 }
5546
5547 /* Return TRUE if the language is C++. */
5548
5549 static inline bool
5550 is_cxx (void)
5551 {
5552 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5553
5554 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5555 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5556 }
5557
5558 /* Return TRUE if DECL was created by the C++ frontend. */
5559
5560 static bool
5561 is_cxx (const_tree decl)
5562 {
5563 if (in_lto_p)
5564 {
5565 const_tree context = get_ultimate_context (decl);
5566 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5567 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5568 }
5569 return is_cxx ();
5570 }
5571
5572 /* Return TRUE if the language is Fortran. */
5573
5574 static inline bool
5575 is_fortran (void)
5576 {
5577 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5578
5579 return (lang == DW_LANG_Fortran77
5580 || lang == DW_LANG_Fortran90
5581 || lang == DW_LANG_Fortran95
5582 || lang == DW_LANG_Fortran03
5583 || lang == DW_LANG_Fortran08);
5584 }
5585
5586 static inline bool
5587 is_fortran (const_tree decl)
5588 {
5589 if (in_lto_p)
5590 {
5591 const_tree context = get_ultimate_context (decl);
5592 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5593 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5594 "GNU Fortran", 11) == 0
5595 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5596 "GNU F77") == 0);
5597 }
5598 return is_fortran ();
5599 }
5600
5601 /* Return TRUE if the language is Rust.
5602 Note, returns FALSE for dwarf_version < 5 && dwarf_strict. */
5603
5604 static inline bool
5605 is_rust (void)
5606 {
5607 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5608
5609 return lang == DW_LANG_Rust;
5610 }
5611
5612 /* Return TRUE if the language is Ada. */
5613
5614 static inline bool
5615 is_ada (void)
5616 {
5617 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5618
5619 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5620 }
5621
5622 /* Return TRUE if the language is D. */
5623
5624 static inline bool
5625 is_dlang (void)
5626 {
5627 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5628
5629 return lang == DW_LANG_D;
5630 }
5631
5632 /* Remove the specified attribute if present. Return TRUE if removal
5633 was successful. */
5634
5635 static bool
5636 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5637 {
5638 dw_attr_node *a;
5639 unsigned ix;
5640
5641 if (! die)
5642 return false;
5643
5644 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5645 if (a->dw_attr == attr_kind)
5646 {
5647 if (AT_class (a) == dw_val_class_str)
5648 if (a->dw_attr_val.v.val_str->refcount)
5649 a->dw_attr_val.v.val_str->refcount--;
5650
5651 /* vec::ordered_remove should help reduce the number of abbrevs
5652 that are needed. */
5653 die->die_attr->ordered_remove (ix);
5654 return true;
5655 }
5656 return false;
5657 }
5658
5659 /* Remove CHILD from its parent. PREV must have the property that
5660 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5661
5662 static void
5663 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5664 {
5665 gcc_assert (child->die_parent == prev->die_parent);
5666 gcc_assert (prev->die_sib == child);
5667 if (prev == child)
5668 {
5669 gcc_assert (child->die_parent->die_child == child);
5670 prev = NULL;
5671 }
5672 else
5673 prev->die_sib = child->die_sib;
5674 if (child->die_parent->die_child == child)
5675 child->die_parent->die_child = prev;
5676 child->die_sib = NULL;
5677 }
5678
5679 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5680 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5681
5682 static void
5683 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5684 {
5685 dw_die_ref parent = old_child->die_parent;
5686
5687 gcc_assert (parent == prev->die_parent);
5688 gcc_assert (prev->die_sib == old_child);
5689
5690 new_child->die_parent = parent;
5691 if (prev == old_child)
5692 {
5693 gcc_assert (parent->die_child == old_child);
5694 new_child->die_sib = new_child;
5695 }
5696 else
5697 {
5698 prev->die_sib = new_child;
5699 new_child->die_sib = old_child->die_sib;
5700 }
5701 if (old_child->die_parent->die_child == old_child)
5702 old_child->die_parent->die_child = new_child;
5703 old_child->die_sib = NULL;
5704 }
5705
5706 /* Move all children from OLD_PARENT to NEW_PARENT. */
5707
5708 static void
5709 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5710 {
5711 dw_die_ref c;
5712 new_parent->die_child = old_parent->die_child;
5713 old_parent->die_child = NULL;
5714 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5715 }
5716
5717 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5718 matches TAG. */
5719
5720 static void
5721 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5722 {
5723 dw_die_ref c;
5724
5725 c = die->die_child;
5726 if (c) do {
5727 dw_die_ref prev = c;
5728 c = c->die_sib;
5729 while (c->die_tag == tag)
5730 {
5731 remove_child_with_prev (c, prev);
5732 c->die_parent = NULL;
5733 /* Might have removed every child. */
5734 if (die->die_child == NULL)
5735 return;
5736 c = prev->die_sib;
5737 }
5738 } while (c != die->die_child);
5739 }
5740
5741 /* Add a CHILD_DIE as the last child of DIE. */
5742
5743 static void
5744 add_child_die (dw_die_ref die, dw_die_ref child_die)
5745 {
5746 /* FIXME this should probably be an assert. */
5747 if (! die || ! child_die)
5748 return;
5749 gcc_assert (die != child_die);
5750
5751 child_die->die_parent = die;
5752 if (die->die_child)
5753 {
5754 child_die->die_sib = die->die_child->die_sib;
5755 die->die_child->die_sib = child_die;
5756 }
5757 else
5758 child_die->die_sib = child_die;
5759 die->die_child = child_die;
5760 }
5761
5762 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5763
5764 static void
5765 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5766 dw_die_ref after_die)
5767 {
5768 gcc_assert (die
5769 && child_die
5770 && after_die
5771 && die->die_child
5772 && die != child_die);
5773
5774 child_die->die_parent = die;
5775 child_die->die_sib = after_die->die_sib;
5776 after_die->die_sib = child_die;
5777 if (die->die_child == after_die)
5778 die->die_child = child_die;
5779 }
5780
5781 /* Unassociate CHILD from its parent, and make its parent be
5782 NEW_PARENT. */
5783
5784 static void
5785 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5786 {
5787 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5788 if (p->die_sib == child)
5789 {
5790 remove_child_with_prev (child, p);
5791 break;
5792 }
5793 add_child_die (new_parent, child);
5794 }
5795
5796 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5797 is the specification, to the end of PARENT's list of children.
5798 This is done by removing and re-adding it. */
5799
5800 static void
5801 splice_child_die (dw_die_ref parent, dw_die_ref child)
5802 {
5803 /* We want the declaration DIE from inside the class, not the
5804 specification DIE at toplevel. */
5805 if (child->die_parent != parent)
5806 {
5807 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5808
5809 if (tmp)
5810 child = tmp;
5811 }
5812
5813 gcc_assert (child->die_parent == parent
5814 || (child->die_parent
5815 == get_AT_ref (parent, DW_AT_specification)));
5816
5817 reparent_child (child, parent);
5818 }
5819
5820 /* Create and return a new die with TAG_VALUE as tag. */
5821
5822 dw_die_ref
5823 new_die_raw (enum dwarf_tag tag_value)
5824 {
5825 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5826 die->die_tag = tag_value;
5827 return die;
5828 }
5829
5830 /* Create and return a new die with a parent of PARENT_DIE. If
5831 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5832 associated tree T must be supplied to determine parenthood
5833 later. */
5834
5835 static inline dw_die_ref
5836 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5837 {
5838 dw_die_ref die = new_die_raw (tag_value);
5839
5840 if (parent_die != NULL)
5841 add_child_die (parent_die, die);
5842 else
5843 {
5844 limbo_die_node *limbo_node;
5845
5846 /* No DIEs created after early dwarf should end up in limbo,
5847 because the limbo list should not persist past LTO
5848 streaming. */
5849 if (tag_value != DW_TAG_compile_unit
5850 /* These are allowed because they're generated while
5851 breaking out COMDAT units late. */
5852 && tag_value != DW_TAG_type_unit
5853 && tag_value != DW_TAG_skeleton_unit
5854 && !early_dwarf
5855 /* Allow nested functions to live in limbo because they will
5856 only temporarily live there, as decls_for_scope will fix
5857 them up. */
5858 && (TREE_CODE (t) != FUNCTION_DECL
5859 || !decl_function_context (t))
5860 /* Same as nested functions above but for types. Types that
5861 are local to a function will be fixed in
5862 decls_for_scope. */
5863 && (!RECORD_OR_UNION_TYPE_P (t)
5864 || !TYPE_CONTEXT (t)
5865 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5866 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5867 especially in the ltrans stage, but once we implement LTO
5868 dwarf streaming, we should remove this exception. */
5869 && !in_lto_p)
5870 {
5871 fprintf (stderr, "symbol ended up in limbo too late:");
5872 debug_generic_stmt (t);
5873 gcc_unreachable ();
5874 }
5875
5876 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5877 limbo_node->die = die;
5878 limbo_node->created_for = t;
5879 limbo_node->next = limbo_die_list;
5880 limbo_die_list = limbo_node;
5881 }
5882
5883 return die;
5884 }
5885
5886 /* Return the DIE associated with the given type specifier. */
5887
5888 dw_die_ref
5889 lookup_type_die (tree type)
5890 {
5891 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5892 if (die && die->removed)
5893 {
5894 TYPE_SYMTAB_DIE (type) = NULL;
5895 TREE_ASM_WRITTEN (type) = 0;
5896 return NULL;
5897 }
5898 return die;
5899 }
5900
5901 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5902 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5903 anonymous type instead the one of the naming typedef. */
5904
5905 static inline dw_die_ref
5906 strip_naming_typedef (tree type, dw_die_ref type_die)
5907 {
5908 if (type
5909 && TREE_CODE (type) == RECORD_TYPE
5910 && type_die
5911 && type_die->die_tag == DW_TAG_typedef
5912 && is_naming_typedef_decl (TYPE_NAME (type)))
5913 type_die = get_AT_ref (type_die, DW_AT_type);
5914 return type_die;
5915 }
5916
5917 /* Like lookup_type_die, but if type is an anonymous type named by a
5918 typedef[1], return the DIE of the anonymous type instead the one of
5919 the naming typedef. This is because in gen_typedef_die, we did
5920 equate the anonymous struct named by the typedef with the DIE of
5921 the naming typedef. So by default, lookup_type_die on an anonymous
5922 struct yields the DIE of the naming typedef.
5923
5924 [1]: Read the comment of is_naming_typedef_decl to learn about what
5925 a naming typedef is. */
5926
5927 static inline dw_die_ref
5928 lookup_type_die_strip_naming_typedef (tree type)
5929 {
5930 dw_die_ref die = lookup_type_die (type);
5931 return strip_naming_typedef (type, die);
5932 }
5933
5934 /* Equate a DIE to a given type specifier. */
5935
5936 static inline void
5937 equate_type_number_to_die (tree type, dw_die_ref type_die)
5938 {
5939 TYPE_SYMTAB_DIE (type) = type_die;
5940 }
5941
5942 static dw_die_ref maybe_create_die_with_external_ref (tree);
5943 struct GTY(()) sym_off_pair
5944 {
5945 const char *sym;
5946 unsigned HOST_WIDE_INT off;
5947 };
5948 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5949
5950 /* Returns a hash value for X (which really is a die_struct). */
5951
5952 inline hashval_t
5953 decl_die_hasher::hash (die_node *x)
5954 {
5955 return (hashval_t) x->decl_id;
5956 }
5957
5958 /* Return true if decl_id of die_struct X is the same as UID of decl *Y. */
5959
5960 inline bool
5961 decl_die_hasher::equal (die_node *x, tree y)
5962 {
5963 return (x->decl_id == DECL_UID (y));
5964 }
5965
5966 /* Return the DIE associated with a given declaration. */
5967
5968 dw_die_ref
5969 lookup_decl_die (tree decl)
5970 {
5971 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5972 NO_INSERT);
5973 if (!die)
5974 {
5975 if (in_lto_p)
5976 return maybe_create_die_with_external_ref (decl);
5977 return NULL;
5978 }
5979 if ((*die)->removed)
5980 {
5981 decl_die_table->clear_slot (die);
5982 return NULL;
5983 }
5984 return *die;
5985 }
5986
5987
5988 /* Return the DIE associated with BLOCK. */
5989
5990 static inline dw_die_ref
5991 lookup_block_die (tree block)
5992 {
5993 dw_die_ref die = BLOCK_DIE (block);
5994 if (!die && in_lto_p)
5995 return maybe_create_die_with_external_ref (block);
5996 return die;
5997 }
5998
5999 /* Associate DIE with BLOCK. */
6000
6001 static inline void
6002 equate_block_to_die (tree block, dw_die_ref die)
6003 {
6004 BLOCK_DIE (block) = die;
6005 }
6006 #undef BLOCK_DIE
6007
6008
6009 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6010 style reference. Return true if we found one refering to a DIE for
6011 DECL, otherwise return false. */
6012
6013 static bool
6014 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6015 unsigned HOST_WIDE_INT *off)
6016 {
6017 dw_die_ref die;
6018
6019 if (in_lto_p)
6020 {
6021 /* During WPA stage and incremental linking we use a hash-map
6022 to store the decl <-> label + offset map. */
6023 if (!external_die_map)
6024 return false;
6025 sym_off_pair *desc = external_die_map->get (decl);
6026 if (!desc)
6027 return false;
6028 *sym = desc->sym;
6029 *off = desc->off;
6030 return true;
6031 }
6032
6033 if (TREE_CODE (decl) == BLOCK)
6034 die = lookup_block_die (decl);
6035 else
6036 die = lookup_decl_die (decl);
6037 if (!die)
6038 return false;
6039
6040 /* Similar to get_ref_die_offset_label, but using the "correct"
6041 label. */
6042 *off = die->die_offset;
6043 while (die->die_parent)
6044 die = die->die_parent;
6045 /* For the containing CU DIE we compute a die_symbol in
6046 compute_comp_unit_symbol. */
6047 if (die->die_tag == DW_TAG_compile_unit)
6048 {
6049 gcc_assert (die->die_id.die_symbol != NULL);
6050 *sym = die->die_id.die_symbol;
6051 return true;
6052 }
6053 /* While we can gracefully handle running into say a type unit
6054 we don't really want and consider this a bug. */
6055 if (flag_checking)
6056 gcc_unreachable ();
6057 return false;
6058 }
6059
6060 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6061
6062 static void
6063 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6064 const char *symbol, HOST_WIDE_INT offset)
6065 {
6066 /* Create a fake DIE that contains the reference. Don't use
6067 new_die because we don't want to end up in the limbo list. */
6068 /* ??? We probably want to share these, thus put a ref to the DIE
6069 we create here to the external_die_map entry. */
6070 dw_die_ref ref = new_die_raw (die->die_tag);
6071 ref->die_id.die_symbol = symbol;
6072 ref->die_offset = offset;
6073 ref->with_offset = 1;
6074 add_AT_die_ref (die, attr_kind, ref);
6075 }
6076
6077 /* Create a DIE for DECL if required and add a reference to a DIE
6078 at SYMBOL + OFFSET which contains attributes dumped early. */
6079
6080 static void
6081 dwarf2out_register_external_die (tree decl, const char *sym,
6082 unsigned HOST_WIDE_INT off)
6083 {
6084 if (debug_info_level == DINFO_LEVEL_NONE)
6085 return;
6086
6087 if (!external_die_map)
6088 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6089 gcc_checking_assert (!external_die_map->get (decl));
6090 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6091 external_die_map->put (decl, p);
6092 }
6093
6094 /* If we have a registered external DIE for DECL return a new DIE for
6095 the concrete instance with an appropriate abstract origin. */
6096
6097 static dw_die_ref
6098 maybe_create_die_with_external_ref (tree decl)
6099 {
6100 if (!external_die_map)
6101 return NULL;
6102 sym_off_pair *desc = external_die_map->get (decl);
6103 if (!desc)
6104 return NULL;
6105
6106 const char *sym = desc->sym;
6107 unsigned HOST_WIDE_INT off = desc->off;
6108 external_die_map->remove (decl);
6109
6110 in_lto_p = false;
6111 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6112 ? lookup_block_die (decl) : lookup_decl_die (decl));
6113 gcc_assert (!die);
6114 in_lto_p = true;
6115
6116 tree ctx;
6117 dw_die_ref parent = NULL;
6118 /* Need to lookup a DIE for the decls context - the containing
6119 function or translation unit. */
6120 if (TREE_CODE (decl) == BLOCK)
6121 {
6122 ctx = BLOCK_SUPERCONTEXT (decl);
6123 /* ??? We do not output DIEs for all scopes thus skip as
6124 many DIEs as needed. */
6125 while (TREE_CODE (ctx) == BLOCK
6126 && !lookup_block_die (ctx))
6127 ctx = BLOCK_SUPERCONTEXT (ctx);
6128 }
6129 else
6130 ctx = DECL_CONTEXT (decl);
6131 /* Peel types in the context stack. */
6132 while (ctx && TYPE_P (ctx))
6133 ctx = TYPE_CONTEXT (ctx);
6134 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6135 if (debug_info_level <= DINFO_LEVEL_TERSE)
6136 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6137 ctx = DECL_CONTEXT (ctx);
6138 if (ctx)
6139 {
6140 if (TREE_CODE (ctx) == BLOCK)
6141 parent = lookup_block_die (ctx);
6142 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6143 /* Keep the 1:1 association during WPA. */
6144 && !flag_wpa
6145 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6146 /* Otherwise all late annotations go to the main CU which
6147 imports the original CUs. */
6148 parent = comp_unit_die ();
6149 else if (TREE_CODE (ctx) == FUNCTION_DECL
6150 && TREE_CODE (decl) != FUNCTION_DECL
6151 && TREE_CODE (decl) != PARM_DECL
6152 && TREE_CODE (decl) != RESULT_DECL
6153 && TREE_CODE (decl) != BLOCK)
6154 /* Leave function local entities parent determination to when
6155 we process scope vars. */
6156 ;
6157 else
6158 parent = lookup_decl_die (ctx);
6159 }
6160 else
6161 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6162 Handle this case gracefully by globalizing stuff. */
6163 parent = comp_unit_die ();
6164 /* Create a DIE "stub". */
6165 switch (TREE_CODE (decl))
6166 {
6167 case TRANSLATION_UNIT_DECL:
6168 {
6169 die = comp_unit_die ();
6170 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6171 to create a DIE for the original CUs. */
6172 return die;
6173 }
6174 case NAMESPACE_DECL:
6175 if (is_fortran (decl))
6176 die = new_die (DW_TAG_module, parent, decl);
6177 else
6178 die = new_die (DW_TAG_namespace, parent, decl);
6179 break;
6180 case FUNCTION_DECL:
6181 die = new_die (DW_TAG_subprogram, parent, decl);
6182 break;
6183 case VAR_DECL:
6184 die = new_die (DW_TAG_variable, parent, decl);
6185 break;
6186 case RESULT_DECL:
6187 die = new_die (DW_TAG_variable, parent, decl);
6188 break;
6189 case PARM_DECL:
6190 die = new_die (DW_TAG_formal_parameter, parent, decl);
6191 break;
6192 case CONST_DECL:
6193 die = new_die (DW_TAG_constant, parent, decl);
6194 break;
6195 case LABEL_DECL:
6196 die = new_die (DW_TAG_label, parent, decl);
6197 break;
6198 case BLOCK:
6199 die = new_die (DW_TAG_lexical_block, parent, decl);
6200 break;
6201 default:
6202 gcc_unreachable ();
6203 }
6204 if (TREE_CODE (decl) == BLOCK)
6205 equate_block_to_die (decl, die);
6206 else
6207 equate_decl_number_to_die (decl, die);
6208
6209 add_desc_attribute (die, decl);
6210
6211 /* Add a reference to the DIE providing early debug at $sym + off. */
6212 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6213
6214 return die;
6215 }
6216
6217 /* Returns a hash value for X (which really is a var_loc_list). */
6218
6219 inline hashval_t
6220 decl_loc_hasher::hash (var_loc_list *x)
6221 {
6222 return (hashval_t) x->decl_id;
6223 }
6224
6225 /* Return true if decl_id of var_loc_list X is the same as
6226 UID of decl *Y. */
6227
6228 inline bool
6229 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6230 {
6231 return (x->decl_id == DECL_UID (y));
6232 }
6233
6234 /* Return the var_loc list associated with a given declaration. */
6235
6236 static inline var_loc_list *
6237 lookup_decl_loc (const_tree decl)
6238 {
6239 if (!decl_loc_table)
6240 return NULL;
6241 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6242 }
6243
6244 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6245
6246 inline hashval_t
6247 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6248 {
6249 return (hashval_t) x->decl_id;
6250 }
6251
6252 /* Return true if decl_id of cached_dw_loc_list X is the same as
6253 UID of decl *Y. */
6254
6255 inline bool
6256 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6257 {
6258 return (x->decl_id == DECL_UID (y));
6259 }
6260
6261 /* Equate a DIE to a particular declaration. */
6262
6263 static void
6264 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6265 {
6266 unsigned int decl_id = DECL_UID (decl);
6267
6268 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6269 decl_die->decl_id = decl_id;
6270 }
6271
6272 /* Return how many bits covers PIECE EXPR_LIST. */
6273
6274 static HOST_WIDE_INT
6275 decl_piece_bitsize (rtx piece)
6276 {
6277 int ret = (int) GET_MODE (piece);
6278 if (ret)
6279 return ret;
6280 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6281 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6282 return INTVAL (XEXP (XEXP (piece, 0), 0));
6283 }
6284
6285 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6286
6287 static rtx *
6288 decl_piece_varloc_ptr (rtx piece)
6289 {
6290 if ((int) GET_MODE (piece))
6291 return &XEXP (piece, 0);
6292 else
6293 return &XEXP (XEXP (piece, 0), 1);
6294 }
6295
6296 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6297 Next is the chain of following piece nodes. */
6298
6299 static rtx_expr_list *
6300 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6301 {
6302 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6303 return alloc_EXPR_LIST (bitsize, loc_note, next);
6304 else
6305 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6306 GEN_INT (bitsize),
6307 loc_note), next);
6308 }
6309
6310 /* Return rtx that should be stored into loc field for
6311 LOC_NOTE and BITPOS/BITSIZE. */
6312
6313 static rtx
6314 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6315 HOST_WIDE_INT bitsize)
6316 {
6317 if (bitsize != -1)
6318 {
6319 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6320 if (bitpos != 0)
6321 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6322 }
6323 return loc_note;
6324 }
6325
6326 /* This function either modifies location piece list *DEST in
6327 place (if SRC and INNER is NULL), or copies location piece list
6328 *SRC to *DEST while modifying it. Location BITPOS is modified
6329 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6330 not copied and if needed some padding around it is added.
6331 When modifying in place, DEST should point to EXPR_LIST where
6332 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6333 to the start of the whole list and INNER points to the EXPR_LIST
6334 where earlier pieces cover PIECE_BITPOS bits. */
6335
6336 static void
6337 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6338 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6339 HOST_WIDE_INT bitsize, rtx loc_note)
6340 {
6341 HOST_WIDE_INT diff;
6342 bool copy = inner != NULL;
6343
6344 if (copy)
6345 {
6346 /* First copy all nodes preceding the current bitpos. */
6347 while (src != inner)
6348 {
6349 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6350 decl_piece_bitsize (*src), NULL_RTX);
6351 dest = &XEXP (*dest, 1);
6352 src = &XEXP (*src, 1);
6353 }
6354 }
6355 /* Add padding if needed. */
6356 if (bitpos != piece_bitpos)
6357 {
6358 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6359 copy ? NULL_RTX : *dest);
6360 dest = &XEXP (*dest, 1);
6361 }
6362 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6363 {
6364 gcc_assert (!copy);
6365 /* A piece with correct bitpos and bitsize already exist,
6366 just update the location for it and return. */
6367 *decl_piece_varloc_ptr (*dest) = loc_note;
6368 return;
6369 }
6370 /* Add the piece that changed. */
6371 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6372 dest = &XEXP (*dest, 1);
6373 /* Skip over pieces that overlap it. */
6374 diff = bitpos - piece_bitpos + bitsize;
6375 if (!copy)
6376 src = dest;
6377 while (diff > 0 && *src)
6378 {
6379 rtx piece = *src;
6380 diff -= decl_piece_bitsize (piece);
6381 if (copy)
6382 src = &XEXP (piece, 1);
6383 else
6384 {
6385 *src = XEXP (piece, 1);
6386 free_EXPR_LIST_node (piece);
6387 }
6388 }
6389 /* Add padding if needed. */
6390 if (diff < 0 && *src)
6391 {
6392 if (!copy)
6393 dest = src;
6394 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6395 dest = &XEXP (*dest, 1);
6396 }
6397 if (!copy)
6398 return;
6399 /* Finally copy all nodes following it. */
6400 while (*src)
6401 {
6402 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6403 decl_piece_bitsize (*src), NULL_RTX);
6404 dest = &XEXP (*dest, 1);
6405 src = &XEXP (*src, 1);
6406 }
6407 }
6408
6409 /* Add a variable location node to the linked list for DECL. */
6410
6411 static struct var_loc_node *
6412 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6413 {
6414 unsigned int decl_id;
6415 var_loc_list *temp;
6416 struct var_loc_node *loc = NULL;
6417 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6418
6419 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6420 {
6421 tree realdecl = DECL_DEBUG_EXPR (decl);
6422 if (handled_component_p (realdecl)
6423 || (TREE_CODE (realdecl) == MEM_REF
6424 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6425 {
6426 bool reverse;
6427 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6428 &bitsize, &reverse);
6429 if (!innerdecl
6430 || !DECL_P (innerdecl)
6431 || DECL_IGNORED_P (innerdecl)
6432 || TREE_STATIC (innerdecl)
6433 || bitsize == 0
6434 || bitpos + bitsize > 256)
6435 return NULL;
6436 decl = innerdecl;
6437 }
6438 }
6439
6440 decl_id = DECL_UID (decl);
6441 var_loc_list **slot
6442 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6443 if (*slot == NULL)
6444 {
6445 temp = ggc_cleared_alloc<var_loc_list> ();
6446 temp->decl_id = decl_id;
6447 *slot = temp;
6448 }
6449 else
6450 temp = *slot;
6451
6452 /* For PARM_DECLs try to keep around the original incoming value,
6453 even if that means we'll emit a zero-range .debug_loc entry. */
6454 if (temp->last
6455 && temp->first == temp->last
6456 && TREE_CODE (decl) == PARM_DECL
6457 && NOTE_P (temp->first->loc)
6458 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6459 && DECL_INCOMING_RTL (decl)
6460 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6461 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6462 == GET_CODE (DECL_INCOMING_RTL (decl))
6463 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6464 && (bitsize != -1
6465 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6466 NOTE_VAR_LOCATION_LOC (loc_note))
6467 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6468 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6469 {
6470 loc = ggc_cleared_alloc<var_loc_node> ();
6471 temp->first->next = loc;
6472 temp->last = loc;
6473 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6474 }
6475 else if (temp->last)
6476 {
6477 struct var_loc_node *last = temp->last, *unused = NULL;
6478 rtx *piece_loc = NULL, last_loc_note;
6479 HOST_WIDE_INT piece_bitpos = 0;
6480 if (last->next)
6481 {
6482 last = last->next;
6483 gcc_assert (last->next == NULL);
6484 }
6485 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6486 {
6487 piece_loc = &last->loc;
6488 do
6489 {
6490 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6491 if (piece_bitpos + cur_bitsize > bitpos)
6492 break;
6493 piece_bitpos += cur_bitsize;
6494 piece_loc = &XEXP (*piece_loc, 1);
6495 }
6496 while (*piece_loc);
6497 }
6498 /* TEMP->LAST here is either pointer to the last but one or
6499 last element in the chained list, LAST is pointer to the
6500 last element. */
6501 if (label && strcmp (last->label, label) == 0 && last->view == view)
6502 {
6503 /* For SRA optimized variables if there weren't any real
6504 insns since last note, just modify the last node. */
6505 if (piece_loc != NULL)
6506 {
6507 adjust_piece_list (piece_loc, NULL, NULL,
6508 bitpos, piece_bitpos, bitsize, loc_note);
6509 return NULL;
6510 }
6511 /* If the last note doesn't cover any instructions, remove it. */
6512 if (temp->last != last)
6513 {
6514 temp->last->next = NULL;
6515 unused = last;
6516 last = temp->last;
6517 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6518 }
6519 else
6520 {
6521 gcc_assert (temp->first == temp->last
6522 || (temp->first->next == temp->last
6523 && TREE_CODE (decl) == PARM_DECL));
6524 memset (temp->last, '\0', sizeof (*temp->last));
6525 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6526 return temp->last;
6527 }
6528 }
6529 if (bitsize == -1 && NOTE_P (last->loc))
6530 last_loc_note = last->loc;
6531 else if (piece_loc != NULL
6532 && *piece_loc != NULL_RTX
6533 && piece_bitpos == bitpos
6534 && decl_piece_bitsize (*piece_loc) == bitsize)
6535 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6536 else
6537 last_loc_note = NULL_RTX;
6538 /* If the current location is the same as the end of the list,
6539 and either both or neither of the locations is uninitialized,
6540 we have nothing to do. */
6541 if (last_loc_note == NULL_RTX
6542 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6543 NOTE_VAR_LOCATION_LOC (loc_note)))
6544 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6545 != NOTE_VAR_LOCATION_STATUS (loc_note))
6546 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6547 == VAR_INIT_STATUS_UNINITIALIZED)
6548 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6549 == VAR_INIT_STATUS_UNINITIALIZED))))
6550 {
6551 /* Add LOC to the end of list and update LAST. If the last
6552 element of the list has been removed above, reuse its
6553 memory for the new node, otherwise allocate a new one. */
6554 if (unused)
6555 {
6556 loc = unused;
6557 memset (loc, '\0', sizeof (*loc));
6558 }
6559 else
6560 loc = ggc_cleared_alloc<var_loc_node> ();
6561 if (bitsize == -1 || piece_loc == NULL)
6562 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6563 else
6564 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6565 bitpos, piece_bitpos, bitsize, loc_note);
6566 last->next = loc;
6567 /* Ensure TEMP->LAST will point either to the new last but one
6568 element of the chain, or to the last element in it. */
6569 if (last != temp->last)
6570 temp->last = last;
6571 }
6572 else if (unused)
6573 ggc_free (unused);
6574 }
6575 else
6576 {
6577 loc = ggc_cleared_alloc<var_loc_node> ();
6578 temp->first = loc;
6579 temp->last = loc;
6580 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6581 }
6582 return loc;
6583 }
6584
6585 /* Keep track of the number of spaces used to indent the
6586 output of the debugging routines that print the structure of
6587 the DIE internal representation. */
6588 static int print_indent;
6589
6590 /* Indent the line the number of spaces given by print_indent. */
6591
6592 static inline void
6593 print_spaces (FILE *outfile)
6594 {
6595 fprintf (outfile, "%*s", print_indent, "");
6596 }
6597
6598 /* Print a type signature in hex. */
6599
6600 static inline void
6601 print_signature (FILE *outfile, char *sig)
6602 {
6603 int i;
6604
6605 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6606 fprintf (outfile, "%02x", sig[i] & 0xff);
6607 }
6608
6609 static inline void
6610 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6611 {
6612 if (discr_value->pos)
6613 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6614 else
6615 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6616 }
6617
6618 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6619
6620 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6621 RECURSE, output location descriptor operations. */
6622
6623 static void
6624 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6625 {
6626 switch (val->val_class)
6627 {
6628 case dw_val_class_addr:
6629 fprintf (outfile, "address");
6630 break;
6631 case dw_val_class_offset:
6632 fprintf (outfile, "offset");
6633 break;
6634 case dw_val_class_loc:
6635 fprintf (outfile, "location descriptor");
6636 if (val->v.val_loc == NULL)
6637 fprintf (outfile, " -> <null>");
6638 else if (recurse)
6639 {
6640 fprintf (outfile, ":\n");
6641 print_indent += 4;
6642 print_loc_descr (val->v.val_loc, outfile);
6643 print_indent -= 4;
6644 }
6645 else
6646 {
6647 if (flag_dump_noaddr || flag_dump_unnumbered)
6648 fprintf (outfile, " #");
6649 else
6650 fprintf (outfile, " (%p)", (void *) val->v.val_loc);
6651 }
6652 break;
6653 case dw_val_class_loc_list:
6654 fprintf (outfile, "location list -> label:%s",
6655 val->v.val_loc_list->ll_symbol);
6656 break;
6657 case dw_val_class_view_list:
6658 val = view_list_to_loc_list_val_node (val);
6659 fprintf (outfile, "location list with views -> labels:%s and %s",
6660 val->v.val_loc_list->ll_symbol,
6661 val->v.val_loc_list->vl_symbol);
6662 break;
6663 case dw_val_class_range_list:
6664 fprintf (outfile, "range list");
6665 break;
6666 case dw_val_class_const:
6667 case dw_val_class_const_implicit:
6668 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6669 break;
6670 case dw_val_class_unsigned_const:
6671 case dw_val_class_unsigned_const_implicit:
6672 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6673 break;
6674 case dw_val_class_const_double:
6675 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6676 HOST_WIDE_INT_PRINT_UNSIGNED")",
6677 val->v.val_double.high,
6678 val->v.val_double.low);
6679 break;
6680 case dw_val_class_wide_int:
6681 {
6682 int i = val->v.val_wide->get_len ();
6683 fprintf (outfile, "constant (");
6684 gcc_assert (i > 0);
6685 if (val->v.val_wide->elt (i - 1) == 0)
6686 fprintf (outfile, "0x");
6687 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6688 val->v.val_wide->elt (--i));
6689 while (--i >= 0)
6690 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6691 val->v.val_wide->elt (i));
6692 fprintf (outfile, ")");
6693 break;
6694 }
6695 case dw_val_class_vec:
6696 fprintf (outfile, "floating-point or vector constant");
6697 break;
6698 case dw_val_class_flag:
6699 fprintf (outfile, "%u", val->v.val_flag);
6700 break;
6701 case dw_val_class_die_ref:
6702 if (val->v.val_die_ref.die != NULL)
6703 {
6704 dw_die_ref die = val->v.val_die_ref.die;
6705
6706 if (die->comdat_type_p)
6707 {
6708 fprintf (outfile, "die -> signature: ");
6709 print_signature (outfile,
6710 die->die_id.die_type_node->signature);
6711 }
6712 else if (die->die_id.die_symbol)
6713 {
6714 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6715 if (die->with_offset)
6716 fprintf (outfile, " + %ld", die->die_offset);
6717 }
6718 else
6719 fprintf (outfile, "die -> %ld", die->die_offset);
6720 if (flag_dump_noaddr || flag_dump_unnumbered)
6721 fprintf (outfile, " #");
6722 else
6723 fprintf (outfile, " (%p)", (void *) die);
6724 }
6725 else
6726 fprintf (outfile, "die -> <null>");
6727 break;
6728 case dw_val_class_vms_delta:
6729 fprintf (outfile, "delta: @slotcount(%s-%s)",
6730 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6731 break;
6732 case dw_val_class_symview:
6733 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6734 break;
6735 case dw_val_class_lbl_id:
6736 case dw_val_class_lineptr:
6737 case dw_val_class_macptr:
6738 case dw_val_class_loclistsptr:
6739 case dw_val_class_high_pc:
6740 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6741 break;
6742 case dw_val_class_str:
6743 if (val->v.val_str->str != NULL)
6744 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6745 else
6746 fprintf (outfile, "<null>");
6747 break;
6748 case dw_val_class_file:
6749 case dw_val_class_file_implicit:
6750 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6751 val->v.val_file->emitted_number);
6752 break;
6753 case dw_val_class_data8:
6754 {
6755 int i;
6756
6757 for (i = 0; i < 8; i++)
6758 fprintf (outfile, "%02x", val->v.val_data8[i]);
6759 break;
6760 }
6761 case dw_val_class_discr_value:
6762 print_discr_value (outfile, &val->v.val_discr_value);
6763 break;
6764 case dw_val_class_discr_list:
6765 for (dw_discr_list_ref node = val->v.val_discr_list;
6766 node != NULL;
6767 node = node->dw_discr_next)
6768 {
6769 if (node->dw_discr_range)
6770 {
6771 fprintf (outfile, " .. ");
6772 print_discr_value (outfile, &node->dw_discr_lower_bound);
6773 print_discr_value (outfile, &node->dw_discr_upper_bound);
6774 }
6775 else
6776 print_discr_value (outfile, &node->dw_discr_lower_bound);
6777
6778 if (node->dw_discr_next != NULL)
6779 fprintf (outfile, " | ");
6780 }
6781 default:
6782 break;
6783 }
6784 }
6785
6786 /* Likewise, for a DIE attribute. */
6787
6788 static void
6789 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6790 {
6791 print_dw_val (&a->dw_attr_val, recurse, outfile);
6792 }
6793
6794
6795 /* Print the list of operands in the LOC location description to OUTFILE. This
6796 routine is a debugging aid only. */
6797
6798 static void
6799 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6800 {
6801 dw_loc_descr_ref l = loc;
6802
6803 if (loc == NULL)
6804 {
6805 print_spaces (outfile);
6806 fprintf (outfile, "<null>\n");
6807 return;
6808 }
6809
6810 for (l = loc; l != NULL; l = l->dw_loc_next)
6811 {
6812 print_spaces (outfile);
6813 if (flag_dump_noaddr || flag_dump_unnumbered)
6814 fprintf (outfile, "#");
6815 else
6816 fprintf (outfile, "(%p)", (void *) l);
6817 fprintf (outfile, " %s",
6818 dwarf_stack_op_name (l->dw_loc_opc));
6819 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6820 {
6821 fprintf (outfile, " ");
6822 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6823 }
6824 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6825 {
6826 fprintf (outfile, ", ");
6827 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6828 }
6829 fprintf (outfile, "\n");
6830 }
6831 }
6832
6833 /* Print the information associated with a given DIE, and its children.
6834 This routine is a debugging aid only. */
6835
6836 static void
6837 print_die (dw_die_ref die, FILE *outfile)
6838 {
6839 dw_attr_node *a;
6840 dw_die_ref c;
6841 unsigned ix;
6842
6843 print_spaces (outfile);
6844 fprintf (outfile, "DIE %4ld: %s ",
6845 die->die_offset, dwarf_tag_name (die->die_tag));
6846 if (flag_dump_noaddr || flag_dump_unnumbered)
6847 fprintf (outfile, "#\n");
6848 else
6849 fprintf (outfile, "(%p)\n", (void*) die);
6850 print_spaces (outfile);
6851 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6852 fprintf (outfile, " offset: %ld", die->die_offset);
6853 fprintf (outfile, " mark: %d\n", die->die_mark);
6854
6855 if (die->comdat_type_p)
6856 {
6857 print_spaces (outfile);
6858 fprintf (outfile, " signature: ");
6859 print_signature (outfile, die->die_id.die_type_node->signature);
6860 fprintf (outfile, "\n");
6861 }
6862
6863 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6864 {
6865 print_spaces (outfile);
6866 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6867
6868 print_attribute (a, true, outfile);
6869 fprintf (outfile, "\n");
6870 }
6871
6872 if (die->die_child != NULL)
6873 {
6874 print_indent += 4;
6875 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6876 print_indent -= 4;
6877 }
6878 if (print_indent == 0)
6879 fprintf (outfile, "\n");
6880 }
6881
6882 /* Print the list of operations in the LOC location description. */
6883
6884 DEBUG_FUNCTION void
6885 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6886 {
6887 print_loc_descr (loc, stderr);
6888 }
6889
6890 /* Print the information collected for a given DIE. */
6891
6892 DEBUG_FUNCTION void
6893 debug_dwarf_die (dw_die_ref die)
6894 {
6895 print_die (die, stderr);
6896 }
6897
6898 DEBUG_FUNCTION void
6899 debug (die_struct &ref)
6900 {
6901 print_die (&ref, stderr);
6902 }
6903
6904 DEBUG_FUNCTION void
6905 debug (die_struct *ptr)
6906 {
6907 if (ptr)
6908 debug (*ptr);
6909 else
6910 fprintf (stderr, "<nil>\n");
6911 }
6912
6913
6914 /* Print all DWARF information collected for the compilation unit.
6915 This routine is a debugging aid only. */
6916
6917 DEBUG_FUNCTION void
6918 debug_dwarf (void)
6919 {
6920 print_indent = 0;
6921 print_die (comp_unit_die (), stderr);
6922 }
6923
6924 /* Verify the DIE tree structure. */
6925
6926 DEBUG_FUNCTION void
6927 verify_die (dw_die_ref die)
6928 {
6929 gcc_assert (!die->die_mark);
6930 if (die->die_parent == NULL
6931 && die->die_sib == NULL)
6932 return;
6933 /* Verify the die_sib list is cyclic. */
6934 dw_die_ref x = die;
6935 do
6936 {
6937 x->die_mark = 1;
6938 x = x->die_sib;
6939 }
6940 while (x && !x->die_mark);
6941 gcc_assert (x == die);
6942 x = die;
6943 do
6944 {
6945 /* Verify all dies have the same parent. */
6946 gcc_assert (x->die_parent == die->die_parent);
6947 if (x->die_child)
6948 {
6949 /* Verify the child has the proper parent and recurse. */
6950 gcc_assert (x->die_child->die_parent == x);
6951 verify_die (x->die_child);
6952 }
6953 x->die_mark = 0;
6954 x = x->die_sib;
6955 }
6956 while (x && x->die_mark);
6957 }
6958
6959 /* Sanity checks on DIEs. */
6960
6961 static void
6962 check_die (dw_die_ref die)
6963 {
6964 unsigned ix;
6965 dw_attr_node *a;
6966 bool inline_found = false;
6967 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6968 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6969 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6970 {
6971 switch (a->dw_attr)
6972 {
6973 case DW_AT_inline:
6974 if (a->dw_attr_val.v.val_unsigned)
6975 inline_found = true;
6976 break;
6977 case DW_AT_location:
6978 ++n_location;
6979 break;
6980 case DW_AT_low_pc:
6981 ++n_low_pc;
6982 break;
6983 case DW_AT_high_pc:
6984 ++n_high_pc;
6985 break;
6986 case DW_AT_artificial:
6987 ++n_artificial;
6988 break;
6989 case DW_AT_decl_column:
6990 ++n_decl_column;
6991 break;
6992 case DW_AT_decl_line:
6993 ++n_decl_line;
6994 break;
6995 case DW_AT_decl_file:
6996 ++n_decl_file;
6997 break;
6998 default:
6999 break;
7000 }
7001 }
7002 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
7003 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
7004 {
7005 fprintf (stderr, "Duplicate attributes in DIE:\n");
7006 debug_dwarf_die (die);
7007 gcc_unreachable ();
7008 }
7009 if (inline_found)
7010 {
7011 /* A debugging information entry that is a member of an abstract
7012 instance tree [that has DW_AT_inline] should not contain any
7013 attributes which describe aspects of the subroutine which vary
7014 between distinct inlined expansions or distinct out-of-line
7015 expansions. */
7016 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7017 gcc_assert (a->dw_attr != DW_AT_low_pc
7018 && a->dw_attr != DW_AT_high_pc
7019 && a->dw_attr != DW_AT_location
7020 && a->dw_attr != DW_AT_frame_base
7021 && a->dw_attr != DW_AT_call_all_calls
7022 && a->dw_attr != DW_AT_GNU_all_call_sites);
7023 }
7024 }
7025
7026 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7027 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7028 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7029
7030 /* Calculate the checksum of a location expression. */
7031
7032 static inline void
7033 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7034 {
7035 int tem;
7036 inchash::hash hstate;
7037 hashval_t hash;
7038
7039 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7040 CHECKSUM (tem);
7041 hash_loc_operands (loc, hstate);
7042 hash = hstate.end();
7043 CHECKSUM (hash);
7044 }
7045
7046 /* Calculate the checksum of an attribute. */
7047
7048 static void
7049 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7050 {
7051 dw_loc_descr_ref loc;
7052 rtx r;
7053
7054 CHECKSUM (at->dw_attr);
7055
7056 /* We don't care that this was compiled with a different compiler
7057 snapshot; if the output is the same, that's what matters. */
7058 if (at->dw_attr == DW_AT_producer)
7059 return;
7060
7061 switch (AT_class (at))
7062 {
7063 case dw_val_class_const:
7064 case dw_val_class_const_implicit:
7065 CHECKSUM (at->dw_attr_val.v.val_int);
7066 break;
7067 case dw_val_class_unsigned_const:
7068 case dw_val_class_unsigned_const_implicit:
7069 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7070 break;
7071 case dw_val_class_const_double:
7072 CHECKSUM (at->dw_attr_val.v.val_double);
7073 break;
7074 case dw_val_class_wide_int:
7075 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7076 get_full_len (*at->dw_attr_val.v.val_wide)
7077 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7078 break;
7079 case dw_val_class_vec:
7080 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7081 (at->dw_attr_val.v.val_vec.length
7082 * at->dw_attr_val.v.val_vec.elt_size));
7083 break;
7084 case dw_val_class_flag:
7085 CHECKSUM (at->dw_attr_val.v.val_flag);
7086 break;
7087 case dw_val_class_str:
7088 CHECKSUM_STRING (AT_string (at));
7089 break;
7090
7091 case dw_val_class_addr:
7092 r = AT_addr (at);
7093 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7094 CHECKSUM_STRING (XSTR (r, 0));
7095 break;
7096
7097 case dw_val_class_offset:
7098 CHECKSUM (at->dw_attr_val.v.val_offset);
7099 break;
7100
7101 case dw_val_class_loc:
7102 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7103 loc_checksum (loc, ctx);
7104 break;
7105
7106 case dw_val_class_die_ref:
7107 die_checksum (AT_ref (at), ctx, mark);
7108 break;
7109
7110 case dw_val_class_fde_ref:
7111 case dw_val_class_vms_delta:
7112 case dw_val_class_symview:
7113 case dw_val_class_lbl_id:
7114 case dw_val_class_lineptr:
7115 case dw_val_class_macptr:
7116 case dw_val_class_loclistsptr:
7117 case dw_val_class_high_pc:
7118 break;
7119
7120 case dw_val_class_file:
7121 case dw_val_class_file_implicit:
7122 CHECKSUM_STRING (AT_file (at)->filename);
7123 break;
7124
7125 case dw_val_class_data8:
7126 CHECKSUM (at->dw_attr_val.v.val_data8);
7127 break;
7128
7129 default:
7130 break;
7131 }
7132 }
7133
7134 /* Calculate the checksum of a DIE. */
7135
7136 static void
7137 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7138 {
7139 dw_die_ref c;
7140 dw_attr_node *a;
7141 unsigned ix;
7142
7143 /* To avoid infinite recursion. */
7144 if (die->die_mark)
7145 {
7146 CHECKSUM (die->die_mark);
7147 return;
7148 }
7149 die->die_mark = ++(*mark);
7150
7151 CHECKSUM (die->die_tag);
7152
7153 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7154 attr_checksum (a, ctx, mark);
7155
7156 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7157 }
7158
7159 #undef CHECKSUM
7160 #undef CHECKSUM_BLOCK
7161 #undef CHECKSUM_STRING
7162
7163 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7164 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7165 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7166 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7167 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7168 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7169 #define CHECKSUM_ATTR(FOO) \
7170 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7171
7172 /* Calculate the checksum of a number in signed LEB128 format. */
7173
7174 static void
7175 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7176 {
7177 unsigned char byte;
7178 bool more;
7179
7180 while (1)
7181 {
7182 byte = (value & 0x7f);
7183 value >>= 7;
7184 more = !((value == 0 && (byte & 0x40) == 0)
7185 || (value == -1 && (byte & 0x40) != 0));
7186 if (more)
7187 byte |= 0x80;
7188 CHECKSUM (byte);
7189 if (!more)
7190 break;
7191 }
7192 }
7193
7194 /* Calculate the checksum of a number in unsigned LEB128 format. */
7195
7196 static void
7197 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7198 {
7199 while (1)
7200 {
7201 unsigned char byte = (value & 0x7f);
7202 value >>= 7;
7203 if (value != 0)
7204 /* More bytes to follow. */
7205 byte |= 0x80;
7206 CHECKSUM (byte);
7207 if (value == 0)
7208 break;
7209 }
7210 }
7211
7212 /* Checksum the context of the DIE. This adds the names of any
7213 surrounding namespaces or structures to the checksum. */
7214
7215 static void
7216 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7217 {
7218 const char *name;
7219 dw_die_ref spec;
7220 int tag = die->die_tag;
7221
7222 if (tag != DW_TAG_namespace
7223 && tag != DW_TAG_structure_type
7224 && tag != DW_TAG_class_type)
7225 return;
7226
7227 name = get_AT_string (die, DW_AT_name);
7228
7229 spec = get_AT_ref (die, DW_AT_specification);
7230 if (spec != NULL)
7231 die = spec;
7232
7233 if (die->die_parent != NULL)
7234 checksum_die_context (die->die_parent, ctx);
7235
7236 CHECKSUM_ULEB128 ('C');
7237 CHECKSUM_ULEB128 (tag);
7238 if (name != NULL)
7239 CHECKSUM_STRING (name);
7240 }
7241
7242 /* Calculate the checksum of a location expression. */
7243
7244 static inline void
7245 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7246 {
7247 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7248 were emitted as a DW_FORM_sdata instead of a location expression. */
7249 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7250 {
7251 CHECKSUM_ULEB128 (DW_FORM_sdata);
7252 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7253 return;
7254 }
7255
7256 /* Otherwise, just checksum the raw location expression. */
7257 while (loc != NULL)
7258 {
7259 inchash::hash hstate;
7260 hashval_t hash;
7261
7262 CHECKSUM_ULEB128 (loc->dtprel);
7263 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7264 hash_loc_operands (loc, hstate);
7265 hash = hstate.end ();
7266 CHECKSUM (hash);
7267 loc = loc->dw_loc_next;
7268 }
7269 }
7270
7271 /* Calculate the checksum of an attribute. */
7272
7273 static void
7274 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7275 struct md5_ctx *ctx, int *mark)
7276 {
7277 dw_loc_descr_ref loc;
7278 rtx r;
7279
7280 if (AT_class (at) == dw_val_class_die_ref)
7281 {
7282 dw_die_ref target_die = AT_ref (at);
7283
7284 /* For pointer and reference types, we checksum only the (qualified)
7285 name of the target type (if there is a name). For friend entries,
7286 we checksum only the (qualified) name of the target type or function.
7287 This allows the checksum to remain the same whether the target type
7288 is complete or not. */
7289 if ((at->dw_attr == DW_AT_type
7290 && (tag == DW_TAG_pointer_type
7291 || tag == DW_TAG_reference_type
7292 || tag == DW_TAG_rvalue_reference_type
7293 || tag == DW_TAG_ptr_to_member_type))
7294 || (at->dw_attr == DW_AT_friend
7295 && tag == DW_TAG_friend))
7296 {
7297 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7298
7299 if (name_attr != NULL)
7300 {
7301 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7302
7303 if (decl == NULL)
7304 decl = target_die;
7305 CHECKSUM_ULEB128 ('N');
7306 CHECKSUM_ULEB128 (at->dw_attr);
7307 if (decl->die_parent != NULL)
7308 checksum_die_context (decl->die_parent, ctx);
7309 CHECKSUM_ULEB128 ('E');
7310 CHECKSUM_STRING (AT_string (name_attr));
7311 return;
7312 }
7313 }
7314
7315 /* For all other references to another DIE, we check to see if the
7316 target DIE has already been visited. If it has, we emit a
7317 backward reference; if not, we descend recursively. */
7318 if (target_die->die_mark > 0)
7319 {
7320 CHECKSUM_ULEB128 ('R');
7321 CHECKSUM_ULEB128 (at->dw_attr);
7322 CHECKSUM_ULEB128 (target_die->die_mark);
7323 }
7324 else
7325 {
7326 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7327
7328 if (decl == NULL)
7329 decl = target_die;
7330 target_die->die_mark = ++(*mark);
7331 CHECKSUM_ULEB128 ('T');
7332 CHECKSUM_ULEB128 (at->dw_attr);
7333 if (decl->die_parent != NULL)
7334 checksum_die_context (decl->die_parent, ctx);
7335 die_checksum_ordered (target_die, ctx, mark);
7336 }
7337 return;
7338 }
7339
7340 CHECKSUM_ULEB128 ('A');
7341 CHECKSUM_ULEB128 (at->dw_attr);
7342
7343 switch (AT_class (at))
7344 {
7345 case dw_val_class_const:
7346 case dw_val_class_const_implicit:
7347 CHECKSUM_ULEB128 (DW_FORM_sdata);
7348 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7349 break;
7350
7351 case dw_val_class_unsigned_const:
7352 case dw_val_class_unsigned_const_implicit:
7353 CHECKSUM_ULEB128 (DW_FORM_sdata);
7354 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7355 break;
7356
7357 case dw_val_class_const_double:
7358 CHECKSUM_ULEB128 (DW_FORM_block);
7359 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7360 CHECKSUM (at->dw_attr_val.v.val_double);
7361 break;
7362
7363 case dw_val_class_wide_int:
7364 CHECKSUM_ULEB128 (DW_FORM_block);
7365 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7366 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7367 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7368 get_full_len (*at->dw_attr_val.v.val_wide)
7369 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7370 break;
7371
7372 case dw_val_class_vec:
7373 CHECKSUM_ULEB128 (DW_FORM_block);
7374 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7375 * at->dw_attr_val.v.val_vec.elt_size);
7376 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7377 (at->dw_attr_val.v.val_vec.length
7378 * at->dw_attr_val.v.val_vec.elt_size));
7379 break;
7380
7381 case dw_val_class_flag:
7382 CHECKSUM_ULEB128 (DW_FORM_flag);
7383 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7384 break;
7385
7386 case dw_val_class_str:
7387 CHECKSUM_ULEB128 (DW_FORM_string);
7388 CHECKSUM_STRING (AT_string (at));
7389 break;
7390
7391 case dw_val_class_addr:
7392 r = AT_addr (at);
7393 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7394 CHECKSUM_ULEB128 (DW_FORM_string);
7395 CHECKSUM_STRING (XSTR (r, 0));
7396 break;
7397
7398 case dw_val_class_offset:
7399 CHECKSUM_ULEB128 (DW_FORM_sdata);
7400 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7401 break;
7402
7403 case dw_val_class_loc:
7404 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7405 loc_checksum_ordered (loc, ctx);
7406 break;
7407
7408 case dw_val_class_fde_ref:
7409 case dw_val_class_symview:
7410 case dw_val_class_lbl_id:
7411 case dw_val_class_lineptr:
7412 case dw_val_class_macptr:
7413 case dw_val_class_loclistsptr:
7414 case dw_val_class_high_pc:
7415 break;
7416
7417 case dw_val_class_file:
7418 case dw_val_class_file_implicit:
7419 CHECKSUM_ULEB128 (DW_FORM_string);
7420 CHECKSUM_STRING (AT_file (at)->filename);
7421 break;
7422
7423 case dw_val_class_data8:
7424 CHECKSUM (at->dw_attr_val.v.val_data8);
7425 break;
7426
7427 default:
7428 break;
7429 }
7430 }
7431
7432 struct checksum_attributes
7433 {
7434 dw_attr_node *at_name;
7435 dw_attr_node *at_type;
7436 dw_attr_node *at_friend;
7437 dw_attr_node *at_accessibility;
7438 dw_attr_node *at_address_class;
7439 dw_attr_node *at_alignment;
7440 dw_attr_node *at_allocated;
7441 dw_attr_node *at_artificial;
7442 dw_attr_node *at_associated;
7443 dw_attr_node *at_binary_scale;
7444 dw_attr_node *at_bit_offset;
7445 dw_attr_node *at_bit_size;
7446 dw_attr_node *at_bit_stride;
7447 dw_attr_node *at_byte_size;
7448 dw_attr_node *at_byte_stride;
7449 dw_attr_node *at_const_value;
7450 dw_attr_node *at_containing_type;
7451 dw_attr_node *at_count;
7452 dw_attr_node *at_data_location;
7453 dw_attr_node *at_data_member_location;
7454 dw_attr_node *at_decimal_scale;
7455 dw_attr_node *at_decimal_sign;
7456 dw_attr_node *at_default_value;
7457 dw_attr_node *at_digit_count;
7458 dw_attr_node *at_discr;
7459 dw_attr_node *at_discr_list;
7460 dw_attr_node *at_discr_value;
7461 dw_attr_node *at_encoding;
7462 dw_attr_node *at_endianity;
7463 dw_attr_node *at_explicit;
7464 dw_attr_node *at_is_optional;
7465 dw_attr_node *at_location;
7466 dw_attr_node *at_lower_bound;
7467 dw_attr_node *at_mutable;
7468 dw_attr_node *at_ordering;
7469 dw_attr_node *at_picture_string;
7470 dw_attr_node *at_prototyped;
7471 dw_attr_node *at_small;
7472 dw_attr_node *at_segment;
7473 dw_attr_node *at_string_length;
7474 dw_attr_node *at_string_length_bit_size;
7475 dw_attr_node *at_string_length_byte_size;
7476 dw_attr_node *at_threads_scaled;
7477 dw_attr_node *at_upper_bound;
7478 dw_attr_node *at_use_location;
7479 dw_attr_node *at_use_UTF8;
7480 dw_attr_node *at_variable_parameter;
7481 dw_attr_node *at_virtuality;
7482 dw_attr_node *at_visibility;
7483 dw_attr_node *at_vtable_elem_location;
7484 };
7485
7486 /* Collect the attributes that we will want to use for the checksum. */
7487
7488 static void
7489 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7490 {
7491 dw_attr_node *a;
7492 unsigned ix;
7493
7494 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7495 {
7496 switch (a->dw_attr)
7497 {
7498 case DW_AT_name:
7499 attrs->at_name = a;
7500 break;
7501 case DW_AT_type:
7502 attrs->at_type = a;
7503 break;
7504 case DW_AT_friend:
7505 attrs->at_friend = a;
7506 break;
7507 case DW_AT_accessibility:
7508 attrs->at_accessibility = a;
7509 break;
7510 case DW_AT_address_class:
7511 attrs->at_address_class = a;
7512 break;
7513 case DW_AT_alignment:
7514 attrs->at_alignment = a;
7515 break;
7516 case DW_AT_allocated:
7517 attrs->at_allocated = a;
7518 break;
7519 case DW_AT_artificial:
7520 attrs->at_artificial = a;
7521 break;
7522 case DW_AT_associated:
7523 attrs->at_associated = a;
7524 break;
7525 case DW_AT_binary_scale:
7526 attrs->at_binary_scale = a;
7527 break;
7528 case DW_AT_bit_offset:
7529 attrs->at_bit_offset = a;
7530 break;
7531 case DW_AT_bit_size:
7532 attrs->at_bit_size = a;
7533 break;
7534 case DW_AT_bit_stride:
7535 attrs->at_bit_stride = a;
7536 break;
7537 case DW_AT_byte_size:
7538 attrs->at_byte_size = a;
7539 break;
7540 case DW_AT_byte_stride:
7541 attrs->at_byte_stride = a;
7542 break;
7543 case DW_AT_const_value:
7544 attrs->at_const_value = a;
7545 break;
7546 case DW_AT_containing_type:
7547 attrs->at_containing_type = a;
7548 break;
7549 case DW_AT_count:
7550 attrs->at_count = a;
7551 break;
7552 case DW_AT_data_location:
7553 attrs->at_data_location = a;
7554 break;
7555 case DW_AT_data_member_location:
7556 attrs->at_data_member_location = a;
7557 break;
7558 case DW_AT_decimal_scale:
7559 attrs->at_decimal_scale = a;
7560 break;
7561 case DW_AT_decimal_sign:
7562 attrs->at_decimal_sign = a;
7563 break;
7564 case DW_AT_default_value:
7565 attrs->at_default_value = a;
7566 break;
7567 case DW_AT_digit_count:
7568 attrs->at_digit_count = a;
7569 break;
7570 case DW_AT_discr:
7571 attrs->at_discr = a;
7572 break;
7573 case DW_AT_discr_list:
7574 attrs->at_discr_list = a;
7575 break;
7576 case DW_AT_discr_value:
7577 attrs->at_discr_value = a;
7578 break;
7579 case DW_AT_encoding:
7580 attrs->at_encoding = a;
7581 break;
7582 case DW_AT_endianity:
7583 attrs->at_endianity = a;
7584 break;
7585 case DW_AT_explicit:
7586 attrs->at_explicit = a;
7587 break;
7588 case DW_AT_is_optional:
7589 attrs->at_is_optional = a;
7590 break;
7591 case DW_AT_location:
7592 attrs->at_location = a;
7593 break;
7594 case DW_AT_lower_bound:
7595 attrs->at_lower_bound = a;
7596 break;
7597 case DW_AT_mutable:
7598 attrs->at_mutable = a;
7599 break;
7600 case DW_AT_ordering:
7601 attrs->at_ordering = a;
7602 break;
7603 case DW_AT_picture_string:
7604 attrs->at_picture_string = a;
7605 break;
7606 case DW_AT_prototyped:
7607 attrs->at_prototyped = a;
7608 break;
7609 case DW_AT_small:
7610 attrs->at_small = a;
7611 break;
7612 case DW_AT_segment:
7613 attrs->at_segment = a;
7614 break;
7615 case DW_AT_string_length:
7616 attrs->at_string_length = a;
7617 break;
7618 case DW_AT_string_length_bit_size:
7619 attrs->at_string_length_bit_size = a;
7620 break;
7621 case DW_AT_string_length_byte_size:
7622 attrs->at_string_length_byte_size = a;
7623 break;
7624 case DW_AT_threads_scaled:
7625 attrs->at_threads_scaled = a;
7626 break;
7627 case DW_AT_upper_bound:
7628 attrs->at_upper_bound = a;
7629 break;
7630 case DW_AT_use_location:
7631 attrs->at_use_location = a;
7632 break;
7633 case DW_AT_use_UTF8:
7634 attrs->at_use_UTF8 = a;
7635 break;
7636 case DW_AT_variable_parameter:
7637 attrs->at_variable_parameter = a;
7638 break;
7639 case DW_AT_virtuality:
7640 attrs->at_virtuality = a;
7641 break;
7642 case DW_AT_visibility:
7643 attrs->at_visibility = a;
7644 break;
7645 case DW_AT_vtable_elem_location:
7646 attrs->at_vtable_elem_location = a;
7647 break;
7648 default:
7649 break;
7650 }
7651 }
7652 }
7653
7654 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7655
7656 static void
7657 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7658 {
7659 dw_die_ref c;
7660 dw_die_ref decl;
7661 struct checksum_attributes attrs;
7662
7663 CHECKSUM_ULEB128 ('D');
7664 CHECKSUM_ULEB128 (die->die_tag);
7665
7666 memset (&attrs, 0, sizeof (attrs));
7667
7668 decl = get_AT_ref (die, DW_AT_specification);
7669 if (decl != NULL)
7670 collect_checksum_attributes (&attrs, decl);
7671 collect_checksum_attributes (&attrs, die);
7672
7673 CHECKSUM_ATTR (attrs.at_name);
7674 CHECKSUM_ATTR (attrs.at_accessibility);
7675 CHECKSUM_ATTR (attrs.at_address_class);
7676 CHECKSUM_ATTR (attrs.at_allocated);
7677 CHECKSUM_ATTR (attrs.at_artificial);
7678 CHECKSUM_ATTR (attrs.at_associated);
7679 CHECKSUM_ATTR (attrs.at_binary_scale);
7680 CHECKSUM_ATTR (attrs.at_bit_offset);
7681 CHECKSUM_ATTR (attrs.at_bit_size);
7682 CHECKSUM_ATTR (attrs.at_bit_stride);
7683 CHECKSUM_ATTR (attrs.at_byte_size);
7684 CHECKSUM_ATTR (attrs.at_byte_stride);
7685 CHECKSUM_ATTR (attrs.at_const_value);
7686 CHECKSUM_ATTR (attrs.at_containing_type);
7687 CHECKSUM_ATTR (attrs.at_count);
7688 CHECKSUM_ATTR (attrs.at_data_location);
7689 CHECKSUM_ATTR (attrs.at_data_member_location);
7690 CHECKSUM_ATTR (attrs.at_decimal_scale);
7691 CHECKSUM_ATTR (attrs.at_decimal_sign);
7692 CHECKSUM_ATTR (attrs.at_default_value);
7693 CHECKSUM_ATTR (attrs.at_digit_count);
7694 CHECKSUM_ATTR (attrs.at_discr);
7695 CHECKSUM_ATTR (attrs.at_discr_list);
7696 CHECKSUM_ATTR (attrs.at_discr_value);
7697 CHECKSUM_ATTR (attrs.at_encoding);
7698 CHECKSUM_ATTR (attrs.at_endianity);
7699 CHECKSUM_ATTR (attrs.at_explicit);
7700 CHECKSUM_ATTR (attrs.at_is_optional);
7701 CHECKSUM_ATTR (attrs.at_location);
7702 CHECKSUM_ATTR (attrs.at_lower_bound);
7703 CHECKSUM_ATTR (attrs.at_mutable);
7704 CHECKSUM_ATTR (attrs.at_ordering);
7705 CHECKSUM_ATTR (attrs.at_picture_string);
7706 CHECKSUM_ATTR (attrs.at_prototyped);
7707 CHECKSUM_ATTR (attrs.at_small);
7708 CHECKSUM_ATTR (attrs.at_segment);
7709 CHECKSUM_ATTR (attrs.at_string_length);
7710 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7711 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7712 CHECKSUM_ATTR (attrs.at_threads_scaled);
7713 CHECKSUM_ATTR (attrs.at_upper_bound);
7714 CHECKSUM_ATTR (attrs.at_use_location);
7715 CHECKSUM_ATTR (attrs.at_use_UTF8);
7716 CHECKSUM_ATTR (attrs.at_variable_parameter);
7717 CHECKSUM_ATTR (attrs.at_virtuality);
7718 CHECKSUM_ATTR (attrs.at_visibility);
7719 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7720 CHECKSUM_ATTR (attrs.at_type);
7721 CHECKSUM_ATTR (attrs.at_friend);
7722 CHECKSUM_ATTR (attrs.at_alignment);
7723
7724 /* Checksum the child DIEs. */
7725 c = die->die_child;
7726 if (c) do {
7727 dw_attr_node *name_attr;
7728
7729 c = c->die_sib;
7730 name_attr = get_AT (c, DW_AT_name);
7731 if (is_template_instantiation (c))
7732 {
7733 /* Ignore instantiations of member type and function templates. */
7734 }
7735 else if (name_attr != NULL
7736 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7737 {
7738 /* Use a shallow checksum for named nested types and member
7739 functions. */
7740 CHECKSUM_ULEB128 ('S');
7741 CHECKSUM_ULEB128 (c->die_tag);
7742 CHECKSUM_STRING (AT_string (name_attr));
7743 }
7744 else
7745 {
7746 /* Use a deep checksum for other children. */
7747 /* Mark this DIE so it gets processed when unmarking. */
7748 if (c->die_mark == 0)
7749 c->die_mark = -1;
7750 die_checksum_ordered (c, ctx, mark);
7751 }
7752 } while (c != die->die_child);
7753
7754 CHECKSUM_ULEB128 (0);
7755 }
7756
7757 /* Add a type name and tag to a hash. */
7758 static void
7759 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7760 {
7761 CHECKSUM_ULEB128 (tag);
7762 CHECKSUM_STRING (name);
7763 }
7764
7765 #undef CHECKSUM
7766 #undef CHECKSUM_STRING
7767 #undef CHECKSUM_ATTR
7768 #undef CHECKSUM_LEB128
7769 #undef CHECKSUM_ULEB128
7770
7771 /* Generate the type signature for DIE. This is computed by generating an
7772 MD5 checksum over the DIE's tag, its relevant attributes, and its
7773 children. Attributes that are references to other DIEs are processed
7774 by recursion, using the MARK field to prevent infinite recursion.
7775 If the DIE is nested inside a namespace or another type, we also
7776 need to include that context in the signature. The lower 64 bits
7777 of the resulting MD5 checksum comprise the signature. */
7778
7779 static void
7780 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7781 {
7782 int mark;
7783 const char *name;
7784 unsigned char checksum[16];
7785 struct md5_ctx ctx;
7786 dw_die_ref decl;
7787 dw_die_ref parent;
7788
7789 name = get_AT_string (die, DW_AT_name);
7790 decl = get_AT_ref (die, DW_AT_specification);
7791 parent = dw_get_die_parent (die);
7792
7793 /* First, compute a signature for just the type name (and its surrounding
7794 context, if any. This is stored in the type unit DIE for link-time
7795 ODR (one-definition rule) checking. */
7796
7797 if (is_cxx () && name != NULL)
7798 {
7799 md5_init_ctx (&ctx);
7800
7801 /* Checksum the names of surrounding namespaces and structures. */
7802 if (parent != NULL)
7803 checksum_die_context (parent, &ctx);
7804
7805 /* Checksum the current DIE. */
7806 die_odr_checksum (die->die_tag, name, &ctx);
7807 md5_finish_ctx (&ctx, checksum);
7808
7809 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7810 }
7811
7812 /* Next, compute the complete type signature. */
7813
7814 md5_init_ctx (&ctx);
7815 mark = 1;
7816 die->die_mark = mark;
7817
7818 /* Checksum the names of surrounding namespaces and structures. */
7819 if (parent != NULL)
7820 checksum_die_context (parent, &ctx);
7821
7822 /* Checksum the DIE and its children. */
7823 die_checksum_ordered (die, &ctx, &mark);
7824 unmark_all_dies (die);
7825 md5_finish_ctx (&ctx, checksum);
7826
7827 /* Store the signature in the type node and link the type DIE and the
7828 type node together. */
7829 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7830 DWARF_TYPE_SIGNATURE_SIZE);
7831 die->comdat_type_p = true;
7832 die->die_id.die_type_node = type_node;
7833 type_node->type_die = die;
7834
7835 /* If the DIE is a specification, link its declaration to the type node
7836 as well. */
7837 if (decl != NULL)
7838 {
7839 decl->comdat_type_p = true;
7840 decl->die_id.die_type_node = type_node;
7841 }
7842 }
7843
7844 /* Do the location expressions look same? */
7845 static inline bool
7846 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7847 {
7848 return loc1->dw_loc_opc == loc2->dw_loc_opc
7849 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7850 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7851 }
7852
7853 /* Do the values look the same? */
7854 static bool
7855 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7856 {
7857 dw_loc_descr_ref loc1, loc2;
7858 rtx r1, r2;
7859
7860 if (v1->val_class != v2->val_class)
7861 return false;
7862
7863 switch (v1->val_class)
7864 {
7865 case dw_val_class_const:
7866 case dw_val_class_const_implicit:
7867 return v1->v.val_int == v2->v.val_int;
7868 case dw_val_class_unsigned_const:
7869 case dw_val_class_unsigned_const_implicit:
7870 return v1->v.val_unsigned == v2->v.val_unsigned;
7871 case dw_val_class_const_double:
7872 return v1->v.val_double.high == v2->v.val_double.high
7873 && v1->v.val_double.low == v2->v.val_double.low;
7874 case dw_val_class_wide_int:
7875 return *v1->v.val_wide == *v2->v.val_wide;
7876 case dw_val_class_vec:
7877 if (v1->v.val_vec.length != v2->v.val_vec.length
7878 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7879 return false;
7880 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7881 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7882 return false;
7883 return true;
7884 case dw_val_class_flag:
7885 return v1->v.val_flag == v2->v.val_flag;
7886 case dw_val_class_str:
7887 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7888
7889 case dw_val_class_addr:
7890 r1 = v1->v.val_addr;
7891 r2 = v2->v.val_addr;
7892 if (GET_CODE (r1) != GET_CODE (r2))
7893 return false;
7894 return !rtx_equal_p (r1, r2);
7895
7896 case dw_val_class_offset:
7897 return v1->v.val_offset == v2->v.val_offset;
7898
7899 case dw_val_class_loc:
7900 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7901 loc1 && loc2;
7902 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7903 if (!same_loc_p (loc1, loc2, mark))
7904 return false;
7905 return !loc1 && !loc2;
7906
7907 case dw_val_class_die_ref:
7908 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7909
7910 case dw_val_class_symview:
7911 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7912
7913 case dw_val_class_fde_ref:
7914 case dw_val_class_vms_delta:
7915 case dw_val_class_lbl_id:
7916 case dw_val_class_lineptr:
7917 case dw_val_class_macptr:
7918 case dw_val_class_loclistsptr:
7919 case dw_val_class_high_pc:
7920 return true;
7921
7922 case dw_val_class_file:
7923 case dw_val_class_file_implicit:
7924 return v1->v.val_file == v2->v.val_file;
7925
7926 case dw_val_class_data8:
7927 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7928
7929 default:
7930 return true;
7931 }
7932 }
7933
7934 /* Do the attributes look the same? */
7935
7936 static bool
7937 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7938 {
7939 if (at1->dw_attr != at2->dw_attr)
7940 return false;
7941
7942 /* We don't care that this was compiled with a different compiler
7943 snapshot; if the output is the same, that's what matters. */
7944 if (at1->dw_attr == DW_AT_producer)
7945 return true;
7946
7947 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7948 }
7949
7950 /* Do the dies look the same? */
7951
7952 static bool
7953 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7954 {
7955 dw_die_ref c1, c2;
7956 dw_attr_node *a1;
7957 unsigned ix;
7958
7959 /* To avoid infinite recursion. */
7960 if (die1->die_mark)
7961 return die1->die_mark == die2->die_mark;
7962 die1->die_mark = die2->die_mark = ++(*mark);
7963
7964 if (die1->die_tag != die2->die_tag)
7965 return false;
7966
7967 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7968 return false;
7969
7970 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7971 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7972 return false;
7973
7974 c1 = die1->die_child;
7975 c2 = die2->die_child;
7976 if (! c1)
7977 {
7978 if (c2)
7979 return false;
7980 }
7981 else
7982 for (;;)
7983 {
7984 if (!same_die_p (c1, c2, mark))
7985 return false;
7986 c1 = c1->die_sib;
7987 c2 = c2->die_sib;
7988 if (c1 == die1->die_child)
7989 {
7990 if (c2 == die2->die_child)
7991 break;
7992 else
7993 return false;
7994 }
7995 }
7996
7997 return true;
7998 }
7999
8000 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8001 children, and set die_symbol. */
8002
8003 static void
8004 compute_comp_unit_symbol (dw_die_ref unit_die)
8005 {
8006 const char *die_name = get_AT_string (unit_die, DW_AT_name);
8007 const char *base = die_name ? lbasename (die_name) : "anonymous";
8008 char *name = XALLOCAVEC (char, strlen (base) + 64);
8009 char *p;
8010 int i, mark;
8011 unsigned char checksum[16];
8012 struct md5_ctx ctx;
8013
8014 /* Compute the checksum of the DIE, then append part of it as hex digits to
8015 the name filename of the unit. */
8016
8017 md5_init_ctx (&ctx);
8018 mark = 0;
8019 die_checksum (unit_die, &ctx, &mark);
8020 unmark_all_dies (unit_die);
8021 md5_finish_ctx (&ctx, checksum);
8022
8023 /* When we this for comp_unit_die () we have a DW_AT_name that might
8024 not start with a letter but with anything valid for filenames and
8025 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8026 character is not a letter. */
8027 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8028 clean_symbol_name (name);
8029
8030 p = name + strlen (name);
8031 for (i = 0; i < 4; i++)
8032 {
8033 sprintf (p, "%.2x", checksum[i]);
8034 p += 2;
8035 }
8036
8037 unit_die->die_id.die_symbol = xstrdup (name);
8038 }
8039
8040 /* Returns true if DIE represents a type, in the sense of TYPE_P. */
8041
8042 static bool
8043 is_type_die (dw_die_ref die)
8044 {
8045 switch (die->die_tag)
8046 {
8047 case DW_TAG_array_type:
8048 case DW_TAG_class_type:
8049 case DW_TAG_interface_type:
8050 case DW_TAG_enumeration_type:
8051 case DW_TAG_pointer_type:
8052 case DW_TAG_reference_type:
8053 case DW_TAG_rvalue_reference_type:
8054 case DW_TAG_string_type:
8055 case DW_TAG_structure_type:
8056 case DW_TAG_subroutine_type:
8057 case DW_TAG_union_type:
8058 case DW_TAG_ptr_to_member_type:
8059 case DW_TAG_set_type:
8060 case DW_TAG_subrange_type:
8061 case DW_TAG_base_type:
8062 case DW_TAG_const_type:
8063 case DW_TAG_file_type:
8064 case DW_TAG_packed_type:
8065 case DW_TAG_volatile_type:
8066 case DW_TAG_typedef:
8067 return true;
8068 default:
8069 return false;
8070 }
8071 }
8072
8073 /* Returns true iff C is a compile-unit DIE. */
8074
8075 static inline bool
8076 is_cu_die (dw_die_ref c)
8077 {
8078 return c && (c->die_tag == DW_TAG_compile_unit
8079 || c->die_tag == DW_TAG_skeleton_unit);
8080 }
8081
8082 /* Returns true iff C is a unit DIE of some sort. */
8083
8084 static inline bool
8085 is_unit_die (dw_die_ref c)
8086 {
8087 return c && (c->die_tag == DW_TAG_compile_unit
8088 || c->die_tag == DW_TAG_partial_unit
8089 || c->die_tag == DW_TAG_type_unit
8090 || c->die_tag == DW_TAG_skeleton_unit);
8091 }
8092
8093 /* Returns true iff C is a namespace DIE. */
8094
8095 static inline bool
8096 is_namespace_die (dw_die_ref c)
8097 {
8098 return c && c->die_tag == DW_TAG_namespace;
8099 }
8100
8101 /* Return true if this DIE is a template parameter. */
8102
8103 static inline bool
8104 is_template_parameter (dw_die_ref die)
8105 {
8106 switch (die->die_tag)
8107 {
8108 case DW_TAG_template_type_param:
8109 case DW_TAG_template_value_param:
8110 case DW_TAG_GNU_template_template_param:
8111 case DW_TAG_GNU_template_parameter_pack:
8112 return true;
8113 default:
8114 return false;
8115 }
8116 }
8117
8118 /* Return true if this DIE represents a template instantiation. */
8119
8120 static inline bool
8121 is_template_instantiation (dw_die_ref die)
8122 {
8123 dw_die_ref c;
8124
8125 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8126 return false;
8127 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8128 return false;
8129 }
8130
8131 static char *
8132 gen_internal_sym (const char *prefix)
8133 {
8134 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8135
8136 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8137 return xstrdup (buf);
8138 }
8139
8140 /* Return true if this DIE is a declaration. */
8141
8142 static bool
8143 is_declaration_die (dw_die_ref die)
8144 {
8145 dw_attr_node *a;
8146 unsigned ix;
8147
8148 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8149 if (a->dw_attr == DW_AT_declaration)
8150 return true;
8151
8152 return false;
8153 }
8154
8155 /* Return true if this DIE is nested inside a subprogram. */
8156
8157 static bool
8158 is_nested_in_subprogram (dw_die_ref die)
8159 {
8160 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8161
8162 if (decl == NULL)
8163 decl = die;
8164 return local_scope_p (decl);
8165 }
8166
8167 /* Return true if this DIE contains a defining declaration of a
8168 subprogram. */
8169
8170 static bool
8171 contains_subprogram_definition (dw_die_ref die)
8172 {
8173 dw_die_ref c;
8174
8175 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8176 return true;
8177 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8178 return false;
8179 }
8180
8181 /* Return true if this is a type DIE that should be moved to a
8182 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8183 unit type. */
8184
8185 static bool
8186 should_move_die_to_comdat (dw_die_ref die)
8187 {
8188 switch (die->die_tag)
8189 {
8190 case DW_TAG_class_type:
8191 case DW_TAG_structure_type:
8192 case DW_TAG_enumeration_type:
8193 case DW_TAG_union_type:
8194 /* Don't move declarations, inlined instances, types nested in a
8195 subprogram, or types that contain subprogram definitions. */
8196 if (is_declaration_die (die)
8197 || get_AT (die, DW_AT_abstract_origin)
8198 || is_nested_in_subprogram (die)
8199 || contains_subprogram_definition (die))
8200 return false;
8201 if (die->die_tag != DW_TAG_enumeration_type)
8202 {
8203 /* Don't move non-constant size aggregates. */
8204 dw_attr_node *sz = get_AT (die, DW_AT_byte_size);
8205 if (sz == NULL
8206 || (AT_class (sz) != dw_val_class_unsigned_const
8207 && AT_class (sz) != dw_val_class_unsigned_const_implicit))
8208 return false;
8209 }
8210 return true;
8211 case DW_TAG_array_type:
8212 case DW_TAG_interface_type:
8213 case DW_TAG_pointer_type:
8214 case DW_TAG_reference_type:
8215 case DW_TAG_rvalue_reference_type:
8216 case DW_TAG_string_type:
8217 case DW_TAG_subroutine_type:
8218 case DW_TAG_ptr_to_member_type:
8219 case DW_TAG_set_type:
8220 case DW_TAG_subrange_type:
8221 case DW_TAG_base_type:
8222 case DW_TAG_const_type:
8223 case DW_TAG_file_type:
8224 case DW_TAG_packed_type:
8225 case DW_TAG_volatile_type:
8226 case DW_TAG_typedef:
8227 default:
8228 return false;
8229 }
8230 }
8231
8232 /* Make a clone of DIE. */
8233
8234 static dw_die_ref
8235 clone_die (dw_die_ref die)
8236 {
8237 dw_die_ref clone = new_die_raw (die->die_tag);
8238 dw_attr_node *a;
8239 unsigned ix;
8240
8241 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8242 add_dwarf_attr (clone, a);
8243
8244 return clone;
8245 }
8246
8247 /* Make a clone of the tree rooted at DIE. */
8248
8249 static dw_die_ref
8250 clone_tree (dw_die_ref die)
8251 {
8252 dw_die_ref c;
8253 dw_die_ref clone = clone_die (die);
8254
8255 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8256
8257 return clone;
8258 }
8259
8260 /* Make a clone of DIE as a declaration. */
8261
8262 static dw_die_ref
8263 clone_as_declaration (dw_die_ref die)
8264 {
8265 dw_die_ref clone;
8266 dw_die_ref decl;
8267 dw_attr_node *a;
8268 unsigned ix;
8269
8270 /* If the DIE is already a declaration, just clone it. */
8271 if (is_declaration_die (die))
8272 return clone_die (die);
8273
8274 /* If the DIE is a specification, just clone its declaration DIE. */
8275 decl = get_AT_ref (die, DW_AT_specification);
8276 if (decl != NULL)
8277 {
8278 clone = clone_die (decl);
8279 if (die->comdat_type_p)
8280 add_AT_die_ref (clone, DW_AT_signature, die);
8281 return clone;
8282 }
8283
8284 clone = new_die_raw (die->die_tag);
8285
8286 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8287 {
8288 /* We don't want to copy over all attributes.
8289 For example we don't want DW_AT_byte_size because otherwise we will no
8290 longer have a declaration and GDB will treat it as a definition. */
8291
8292 switch (a->dw_attr)
8293 {
8294 case DW_AT_abstract_origin:
8295 case DW_AT_artificial:
8296 case DW_AT_containing_type:
8297 case DW_AT_external:
8298 case DW_AT_name:
8299 case DW_AT_type:
8300 case DW_AT_virtuality:
8301 case DW_AT_linkage_name:
8302 case DW_AT_MIPS_linkage_name:
8303 add_dwarf_attr (clone, a);
8304 break;
8305 case DW_AT_byte_size:
8306 case DW_AT_alignment:
8307 default:
8308 break;
8309 }
8310 }
8311
8312 if (die->comdat_type_p)
8313 add_AT_die_ref (clone, DW_AT_signature, die);
8314
8315 add_AT_flag (clone, DW_AT_declaration, 1);
8316 return clone;
8317 }
8318
8319
8320 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8321
8322 struct decl_table_entry
8323 {
8324 dw_die_ref orig;
8325 dw_die_ref copy;
8326 };
8327
8328 /* Helpers to manipulate hash table of copied declarations. */
8329
8330 /* Hashtable helpers. */
8331
8332 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8333 {
8334 typedef die_struct *compare_type;
8335 static inline hashval_t hash (const decl_table_entry *);
8336 static inline bool equal (const decl_table_entry *, const die_struct *);
8337 };
8338
8339 inline hashval_t
8340 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8341 {
8342 return htab_hash_pointer (entry->orig);
8343 }
8344
8345 inline bool
8346 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8347 const die_struct *entry2)
8348 {
8349 return entry1->orig == entry2;
8350 }
8351
8352 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8353
8354 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8355 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8356 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8357 to check if the ancestor has already been copied into UNIT. */
8358
8359 static dw_die_ref
8360 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8361 decl_hash_type *decl_table)
8362 {
8363 dw_die_ref parent = die->die_parent;
8364 dw_die_ref new_parent = unit;
8365 dw_die_ref copy;
8366 decl_table_entry **slot = NULL;
8367 struct decl_table_entry *entry = NULL;
8368
8369 /* If DIE refers to a stub unfold that so we get the appropriate
8370 DIE registered as orig in decl_table. */
8371 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8372 die = c;
8373
8374 if (decl_table)
8375 {
8376 /* Check if the entry has already been copied to UNIT. */
8377 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8378 INSERT);
8379 if (*slot != HTAB_EMPTY_ENTRY)
8380 {
8381 entry = *slot;
8382 return entry->copy;
8383 }
8384
8385 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8386 entry = XCNEW (struct decl_table_entry);
8387 entry->orig = die;
8388 entry->copy = NULL;
8389 *slot = entry;
8390 }
8391
8392 if (parent != NULL)
8393 {
8394 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8395 if (spec != NULL)
8396 parent = spec;
8397 if (!is_unit_die (parent))
8398 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8399 }
8400
8401 copy = clone_as_declaration (die);
8402 add_child_die (new_parent, copy);
8403
8404 if (decl_table)
8405 {
8406 /* Record the pointer to the copy. */
8407 entry->copy = copy;
8408 }
8409
8410 return copy;
8411 }
8412 /* Copy the declaration context to the new type unit DIE. This includes
8413 any surrounding namespace or type declarations. If the DIE has an
8414 AT_specification attribute, it also includes attributes and children
8415 attached to the specification, and returns a pointer to the original
8416 parent of the declaration DIE. Returns NULL otherwise. */
8417
8418 static dw_die_ref
8419 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8420 {
8421 dw_die_ref decl;
8422 dw_die_ref new_decl;
8423 dw_die_ref orig_parent = NULL;
8424
8425 decl = get_AT_ref (die, DW_AT_specification);
8426 if (decl == NULL)
8427 decl = die;
8428 else
8429 {
8430 unsigned ix;
8431 dw_die_ref c;
8432 dw_attr_node *a;
8433
8434 /* The original DIE will be changed to a declaration, and must
8435 be moved to be a child of the original declaration DIE. */
8436 orig_parent = decl->die_parent;
8437
8438 /* Copy the type node pointer from the new DIE to the original
8439 declaration DIE so we can forward references later. */
8440 decl->comdat_type_p = true;
8441 decl->die_id.die_type_node = die->die_id.die_type_node;
8442
8443 remove_AT (die, DW_AT_specification);
8444
8445 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8446 {
8447 if (a->dw_attr != DW_AT_name
8448 && a->dw_attr != DW_AT_declaration
8449 && a->dw_attr != DW_AT_external)
8450 add_dwarf_attr (die, a);
8451 }
8452
8453 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8454 }
8455
8456 if (decl->die_parent != NULL
8457 && !is_unit_die (decl->die_parent))
8458 {
8459 new_decl = copy_ancestor_tree (unit, decl, NULL);
8460 if (new_decl != NULL)
8461 {
8462 remove_AT (new_decl, DW_AT_signature);
8463 add_AT_specification (die, new_decl);
8464 }
8465 }
8466
8467 return orig_parent;
8468 }
8469
8470 /* Generate the skeleton ancestor tree for the given NODE, then clone
8471 the DIE and add the clone into the tree. */
8472
8473 static void
8474 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8475 {
8476 if (node->new_die != NULL)
8477 return;
8478
8479 node->new_die = clone_as_declaration (node->old_die);
8480
8481 if (node->parent != NULL)
8482 {
8483 generate_skeleton_ancestor_tree (node->parent);
8484 add_child_die (node->parent->new_die, node->new_die);
8485 }
8486 }
8487
8488 /* Generate a skeleton tree of DIEs containing any declarations that are
8489 found in the original tree. We traverse the tree looking for declaration
8490 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8491
8492 static void
8493 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8494 {
8495 skeleton_chain_node node;
8496 dw_die_ref c;
8497 dw_die_ref first;
8498 dw_die_ref prev = NULL;
8499 dw_die_ref next = NULL;
8500
8501 node.parent = parent;
8502
8503 first = c = parent->old_die->die_child;
8504 if (c)
8505 next = c->die_sib;
8506 if (c) do {
8507 if (prev == NULL || prev->die_sib == c)
8508 prev = c;
8509 c = next;
8510 next = (c == first ? NULL : c->die_sib);
8511 node.old_die = c;
8512 node.new_die = NULL;
8513 if (is_declaration_die (c))
8514 {
8515 if (is_template_instantiation (c))
8516 {
8517 /* Instantiated templates do not need to be cloned into the
8518 type unit. Just move the DIE and its children back to
8519 the skeleton tree (in the main CU). */
8520 remove_child_with_prev (c, prev);
8521 add_child_die (parent->new_die, c);
8522 c = prev;
8523 }
8524 else if (c->comdat_type_p)
8525 {
8526 /* This is the skeleton of earlier break_out_comdat_types
8527 type. Clone the existing DIE, but keep the children
8528 under the original (which is in the main CU). */
8529 dw_die_ref clone = clone_die (c);
8530
8531 replace_child (c, clone, prev);
8532 generate_skeleton_ancestor_tree (parent);
8533 add_child_die (parent->new_die, c);
8534 c = clone;
8535 continue;
8536 }
8537 else
8538 {
8539 /* Clone the existing DIE, move the original to the skeleton
8540 tree (which is in the main CU), and put the clone, with
8541 all the original's children, where the original came from
8542 (which is about to be moved to the type unit). */
8543 dw_die_ref clone = clone_die (c);
8544 move_all_children (c, clone);
8545
8546 /* If the original has a DW_AT_object_pointer attribute,
8547 it would now point to a child DIE just moved to the
8548 cloned tree, so we need to remove that attribute from
8549 the original. */
8550 remove_AT (c, DW_AT_object_pointer);
8551
8552 replace_child (c, clone, prev);
8553 generate_skeleton_ancestor_tree (parent);
8554 add_child_die (parent->new_die, c);
8555 node.old_die = clone;
8556 node.new_die = c;
8557 c = clone;
8558 }
8559 }
8560 generate_skeleton_bottom_up (&node);
8561 } while (next != NULL);
8562 }
8563
8564 /* Wrapper function for generate_skeleton_bottom_up. */
8565
8566 static dw_die_ref
8567 generate_skeleton (dw_die_ref die)
8568 {
8569 skeleton_chain_node node;
8570
8571 node.old_die = die;
8572 node.new_die = NULL;
8573 node.parent = NULL;
8574
8575 /* If this type definition is nested inside another type,
8576 and is not an instantiation of a template, always leave
8577 at least a declaration in its place. */
8578 if (die->die_parent != NULL
8579 && is_type_die (die->die_parent)
8580 && !is_template_instantiation (die))
8581 node.new_die = clone_as_declaration (die);
8582
8583 generate_skeleton_bottom_up (&node);
8584 return node.new_die;
8585 }
8586
8587 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8588 declaration. The original DIE is moved to a new compile unit so that
8589 existing references to it follow it to the new location. If any of the
8590 original DIE's descendants is a declaration, we need to replace the
8591 original DIE with a skeleton tree and move the declarations back into the
8592 skeleton tree. */
8593
8594 static dw_die_ref
8595 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8596 dw_die_ref prev)
8597 {
8598 dw_die_ref skeleton, orig_parent;
8599
8600 /* Copy the declaration context to the type unit DIE. If the returned
8601 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8602 that DIE. */
8603 orig_parent = copy_declaration_context (unit, child);
8604
8605 skeleton = generate_skeleton (child);
8606 if (skeleton == NULL)
8607 remove_child_with_prev (child, prev);
8608 else
8609 {
8610 skeleton->comdat_type_p = true;
8611 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8612
8613 /* If the original DIE was a specification, we need to put
8614 the skeleton under the parent DIE of the declaration.
8615 This leaves the original declaration in the tree, but
8616 it will be pruned later since there are no longer any
8617 references to it. */
8618 if (orig_parent != NULL)
8619 {
8620 remove_child_with_prev (child, prev);
8621 add_child_die (orig_parent, skeleton);
8622 }
8623 else
8624 replace_child (child, skeleton, prev);
8625 }
8626
8627 return skeleton;
8628 }
8629
8630 static void
8631 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8632 comdat_type_node *type_node,
8633 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8634
8635 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8636 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8637 DWARF procedure references in the DW_AT_location attribute. */
8638
8639 static dw_die_ref
8640 copy_dwarf_procedure (dw_die_ref die,
8641 comdat_type_node *type_node,
8642 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8643 {
8644 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8645
8646 /* DWARF procedures are not supposed to have children... */
8647 gcc_assert (die->die_child == NULL);
8648
8649 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8650 gcc_assert (vec_safe_length (die->die_attr) == 1
8651 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8652
8653 /* Do not copy more than once DWARF procedures. */
8654 bool existed;
8655 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8656 if (existed)
8657 return die_copy;
8658
8659 die_copy = clone_die (die);
8660 add_child_die (type_node->root_die, die_copy);
8661 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8662 return die_copy;
8663 }
8664
8665 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8666 procedures in DIE's attributes. */
8667
8668 static void
8669 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8670 comdat_type_node *type_node,
8671 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8672 {
8673 dw_attr_node *a;
8674 unsigned i;
8675
8676 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8677 {
8678 dw_loc_descr_ref loc;
8679
8680 if (a->dw_attr_val.val_class != dw_val_class_loc)
8681 continue;
8682
8683 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8684 {
8685 switch (loc->dw_loc_opc)
8686 {
8687 case DW_OP_call2:
8688 case DW_OP_call4:
8689 case DW_OP_call_ref:
8690 gcc_assert (loc->dw_loc_oprnd1.val_class
8691 == dw_val_class_die_ref);
8692 loc->dw_loc_oprnd1.v.val_die_ref.die
8693 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8694 type_node,
8695 copied_dwarf_procs);
8696
8697 default:
8698 break;
8699 }
8700 }
8701 }
8702 }
8703
8704 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8705 rewrite references to point to the copies.
8706
8707 References are looked for in DIE's attributes and recursively in all its
8708 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8709 mapping from old DWARF procedures to their copy. It is used not to copy
8710 twice the same DWARF procedure under TYPE_NODE. */
8711
8712 static void
8713 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8714 comdat_type_node *type_node,
8715 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8716 {
8717 dw_die_ref c;
8718
8719 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8720 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8721 type_node,
8722 copied_dwarf_procs));
8723 }
8724
8725 /* Traverse the DIE and set up additional .debug_types or .debug_info
8726 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8727 section. */
8728
8729 static void
8730 break_out_comdat_types (dw_die_ref die)
8731 {
8732 dw_die_ref c;
8733 dw_die_ref first;
8734 dw_die_ref prev = NULL;
8735 dw_die_ref next = NULL;
8736 dw_die_ref unit = NULL;
8737
8738 first = c = die->die_child;
8739 if (c)
8740 next = c->die_sib;
8741 if (c) do {
8742 if (prev == NULL || prev->die_sib == c)
8743 prev = c;
8744 c = next;
8745 next = (c == first ? NULL : c->die_sib);
8746 if (should_move_die_to_comdat (c))
8747 {
8748 dw_die_ref replacement;
8749 comdat_type_node *type_node;
8750
8751 /* Break out nested types into their own type units. */
8752 break_out_comdat_types (c);
8753
8754 /* Create a new type unit DIE as the root for the new tree. */
8755 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8756 add_AT_unsigned (unit, DW_AT_language,
8757 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8758 if (unsigned lname = get_AT_unsigned (comp_unit_die (),
8759 DW_AT_language_name))
8760 {
8761 add_AT_unsigned (unit, DW_AT_language_name, lname);
8762 add_AT_unsigned (unit, DW_AT_language_version,
8763 get_AT_unsigned (comp_unit_die (),
8764 DW_AT_language_version));
8765 }
8766
8767 /* Add the new unit's type DIE into the comdat type list. */
8768 type_node = ggc_cleared_alloc<comdat_type_node> ();
8769 type_node->root_die = unit;
8770 type_node->next = comdat_type_list;
8771 comdat_type_list = type_node;
8772
8773 /* Generate the type signature. */
8774 generate_type_signature (c, type_node);
8775
8776 /* Copy the declaration context, attributes, and children of the
8777 declaration into the new type unit DIE, then remove this DIE
8778 from the main CU (or replace it with a skeleton if necessary). */
8779 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8780 type_node->skeleton_die = replacement;
8781
8782 /* Add the DIE to the new compunit. */
8783 add_child_die (unit, c);
8784
8785 /* Types can reference DWARF procedures for type size or data location
8786 expressions. Calls in DWARF expressions cannot target procedures
8787 that are not in the same section. So we must copy DWARF procedures
8788 along with this type and then rewrite references to them. */
8789 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8790 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8791
8792 if (replacement != NULL)
8793 c = replacement;
8794 }
8795 else if (c->die_tag == DW_TAG_namespace
8796 || c->die_tag == DW_TAG_class_type
8797 || c->die_tag == DW_TAG_structure_type
8798 || c->die_tag == DW_TAG_union_type)
8799 {
8800 /* Look for nested types that can be broken out. */
8801 break_out_comdat_types (c);
8802 }
8803 } while (next != NULL);
8804 }
8805
8806 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8807 Enter all the cloned children into the hash table decl_table. */
8808
8809 static dw_die_ref
8810 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8811 {
8812 dw_die_ref c;
8813 dw_die_ref clone;
8814 struct decl_table_entry *entry;
8815 decl_table_entry **slot;
8816
8817 if (die->die_tag == DW_TAG_subprogram)
8818 clone = clone_as_declaration (die);
8819 else
8820 clone = clone_die (die);
8821
8822 slot = decl_table->find_slot_with_hash (die,
8823 htab_hash_pointer (die), INSERT);
8824
8825 /* Assert that DIE isn't in the hash table yet. If it would be there
8826 before, the ancestors would be necessarily there as well, therefore
8827 clone_tree_partial wouldn't be called. */
8828 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8829
8830 entry = XCNEW (struct decl_table_entry);
8831 entry->orig = die;
8832 entry->copy = clone;
8833 *slot = entry;
8834
8835 if (die->die_tag != DW_TAG_subprogram)
8836 FOR_EACH_CHILD (die, c,
8837 add_child_die (clone, clone_tree_partial (c, decl_table)));
8838
8839 return clone;
8840 }
8841
8842 /* Walk the DIE and its children, looking for references to incomplete
8843 or trivial types that are unmarked (i.e., that are not in the current
8844 type_unit). */
8845
8846 static void
8847 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8848 {
8849 dw_die_ref c;
8850 dw_attr_node *a;
8851 unsigned ix;
8852
8853 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8854 {
8855 if (AT_class (a) == dw_val_class_die_ref)
8856 {
8857 dw_die_ref targ = AT_ref (a);
8858 decl_table_entry **slot;
8859 struct decl_table_entry *entry;
8860
8861 if (targ->die_mark != 0 || targ->comdat_type_p)
8862 continue;
8863
8864 slot = decl_table->find_slot_with_hash (targ,
8865 htab_hash_pointer (targ),
8866 INSERT);
8867
8868 if (*slot != HTAB_EMPTY_ENTRY)
8869 {
8870 /* TARG has already been copied, so we just need to
8871 modify the reference to point to the copy. */
8872 entry = *slot;
8873 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8874 }
8875 else
8876 {
8877 dw_die_ref parent = unit;
8878 dw_die_ref copy = clone_die (targ);
8879
8880 /* Record in DECL_TABLE that TARG has been copied.
8881 Need to do this now, before the recursive call,
8882 because DECL_TABLE may be expanded and SLOT
8883 would no longer be a valid pointer. */
8884 entry = XCNEW (struct decl_table_entry);
8885 entry->orig = targ;
8886 entry->copy = copy;
8887 *slot = entry;
8888
8889 /* If TARG is not a declaration DIE, we need to copy its
8890 children. */
8891 if (!is_declaration_die (targ))
8892 {
8893 FOR_EACH_CHILD (
8894 targ, c,
8895 add_child_die (copy,
8896 clone_tree_partial (c, decl_table)));
8897 }
8898
8899 /* Make sure the cloned tree is marked as part of the
8900 type unit. */
8901 mark_dies (copy);
8902
8903 /* If TARG has surrounding context, copy its ancestor tree
8904 into the new type unit. */
8905 if (targ->die_parent != NULL
8906 && !is_unit_die (targ->die_parent))
8907 parent = copy_ancestor_tree (unit, targ->die_parent,
8908 decl_table);
8909
8910 add_child_die (parent, copy);
8911 a->dw_attr_val.v.val_die_ref.die = copy;
8912
8913 /* Make sure the newly-copied DIE is walked. If it was
8914 installed in a previously-added context, it won't
8915 get visited otherwise. */
8916 if (parent != unit)
8917 {
8918 /* Find the highest point of the newly-added tree,
8919 mark each node along the way, and walk from there. */
8920 parent->die_mark = 1;
8921 while (parent->die_parent
8922 && parent->die_parent->die_mark == 0)
8923 {
8924 parent = parent->die_parent;
8925 parent->die_mark = 1;
8926 }
8927 copy_decls_walk (unit, parent, decl_table);
8928 }
8929 }
8930 }
8931 }
8932
8933 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8934 }
8935
8936 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8937 and record them in DECL_TABLE. */
8938
8939 static void
8940 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8941 {
8942 dw_die_ref c;
8943
8944 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8945 {
8946 dw_die_ref targ = AT_ref (a);
8947 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8948 decl_table_entry **slot
8949 = decl_table->find_slot_with_hash (targ,
8950 htab_hash_pointer (targ),
8951 INSERT);
8952 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8953 /* Record in DECL_TABLE that TARG has been already copied
8954 by remove_child_or_replace_with_skeleton. */
8955 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8956 entry->orig = targ;
8957 entry->copy = die;
8958 *slot = entry;
8959 }
8960 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8961 }
8962
8963 /* Copy declarations for "unworthy" types into the new comdat section.
8964 Incomplete types, modified types, and certain other types aren't broken
8965 out into comdat sections of their own, so they don't have a signature,
8966 and we need to copy the declaration into the same section so that we
8967 don't have an external reference. */
8968
8969 static void
8970 copy_decls_for_unworthy_types (dw_die_ref unit)
8971 {
8972 mark_dies (unit);
8973 decl_hash_type decl_table (10);
8974 collect_skeleton_dies (unit, &decl_table);
8975 copy_decls_walk (unit, unit, &decl_table);
8976 unmark_dies (unit);
8977 }
8978
8979 /* Traverse the DIE and add a sibling attribute if it may have the
8980 effect of speeding up access to siblings. To save some space,
8981 avoid generating sibling attributes for DIE's without children. */
8982
8983 static void
8984 add_sibling_attributes (dw_die_ref die)
8985 {
8986 dw_die_ref c;
8987
8988 if (! die->die_child)
8989 return;
8990
8991 if (die->die_parent && die != die->die_parent->die_child)
8992 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8993
8994 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8995 }
8996
8997 /* Output all location lists for the DIE and its children. */
8998
8999 static void
9000 output_location_lists (dw_die_ref die)
9001 {
9002 dw_die_ref c;
9003 dw_attr_node *a;
9004 unsigned ix;
9005
9006 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9007 if (AT_class (a) == dw_val_class_loc_list)
9008 output_loc_list (AT_loc_list (a));
9009
9010 FOR_EACH_CHILD (die, c, output_location_lists (c));
9011 }
9012
9013 /* During assign_location_list_indexes and output_loclists_offset the
9014 current index, after it the number of assigned indexes (i.e. how
9015 large the .debug_loclists* offset table should be). */
9016 static unsigned int loc_list_idx;
9017
9018 /* Output all location list offsets for the DIE and its children. */
9019
9020 static void
9021 output_loclists_offsets (dw_die_ref die)
9022 {
9023 dw_die_ref c;
9024 dw_attr_node *a;
9025 unsigned ix;
9026
9027 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9028 if (AT_class (a) == dw_val_class_loc_list)
9029 {
9030 dw_loc_list_ref l = AT_loc_list (a);
9031 if (l->offset_emitted)
9032 continue;
9033 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9034 loc_section_label, NULL);
9035 gcc_assert (l->hash == loc_list_idx);
9036 loc_list_idx++;
9037 l->offset_emitted = true;
9038 }
9039
9040 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9041 }
9042
9043 /* Recursively set indexes of location lists. */
9044
9045 static void
9046 assign_location_list_indexes (dw_die_ref die)
9047 {
9048 dw_die_ref c;
9049 dw_attr_node *a;
9050 unsigned ix;
9051
9052 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9053 if (AT_class (a) == dw_val_class_loc_list)
9054 {
9055 dw_loc_list_ref list = AT_loc_list (a);
9056 if (!list->num_assigned)
9057 {
9058 list->num_assigned = true;
9059 list->hash = loc_list_idx++;
9060 }
9061 }
9062
9063 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9064 }
9065
9066 /* We want to limit the number of external references, because they are
9067 larger than local references: a relocation takes multiple words, and
9068 even a sig8 reference is always eight bytes, whereas a local reference
9069 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9070 So if we encounter multiple external references to the same type DIE, we
9071 make a local typedef stub for it and redirect all references there.
9072
9073 This is the element of the hash table for keeping track of these
9074 references. */
9075
9076 struct external_ref
9077 {
9078 dw_die_ref type;
9079 dw_die_ref stub;
9080 unsigned n_refs;
9081 };
9082
9083 /* Hashtable helpers. */
9084
9085 struct external_ref_hasher : free_ptr_hash <external_ref>
9086 {
9087 static inline hashval_t hash (const external_ref *);
9088 static inline bool equal (const external_ref *, const external_ref *);
9089 };
9090
9091 inline hashval_t
9092 external_ref_hasher::hash (const external_ref *r)
9093 {
9094 dw_die_ref die = r->type;
9095 hashval_t h = 0;
9096
9097 /* We can't use the address of the DIE for hashing, because
9098 that will make the order of the stub DIEs non-deterministic. */
9099 if (! die->comdat_type_p)
9100 /* We have a symbol; use it to compute a hash. */
9101 h = htab_hash_string (die->die_id.die_symbol);
9102 else
9103 {
9104 /* We have a type signature; use a subset of the bits as the hash.
9105 The 8-byte signature is at least as large as hashval_t. */
9106 comdat_type_node *type_node = die->die_id.die_type_node;
9107 memcpy (&h, type_node->signature, sizeof (h));
9108 }
9109 return h;
9110 }
9111
9112 inline bool
9113 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9114 {
9115 return r1->type == r2->type;
9116 }
9117
9118 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9119
9120 /* Return a pointer to the external_ref for references to DIE. */
9121
9122 static struct external_ref *
9123 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9124 {
9125 struct external_ref ref, *ref_p;
9126 external_ref **slot;
9127
9128 ref.type = die;
9129 slot = map->find_slot (&ref, INSERT);
9130 if (*slot != HTAB_EMPTY_ENTRY)
9131 return *slot;
9132
9133 ref_p = XCNEW (struct external_ref);
9134 ref_p->type = die;
9135 *slot = ref_p;
9136 return ref_p;
9137 }
9138
9139 /* Subroutine of optimize_external_refs, below.
9140
9141 If we see a type skeleton, record it as our stub. If we see external
9142 references, remember how many we've seen. */
9143
9144 static void
9145 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9146 {
9147 dw_die_ref c;
9148 dw_attr_node *a;
9149 unsigned ix;
9150 struct external_ref *ref_p;
9151
9152 if (is_type_die (die)
9153 && (c = get_AT_ref (die, DW_AT_signature)))
9154 {
9155 /* This is a local skeleton; use it for local references. */
9156 ref_p = lookup_external_ref (map, c);
9157 ref_p->stub = die;
9158 }
9159
9160 /* Scan the DIE references, and remember any that refer to DIEs from
9161 other CUs (i.e. those which are not marked). */
9162 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9163 if (AT_class (a) == dw_val_class_die_ref
9164 && (c = AT_ref (a))->die_mark == 0
9165 && is_type_die (c))
9166 {
9167 ref_p = lookup_external_ref (map, c);
9168 ref_p->n_refs++;
9169 }
9170
9171 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9172 }
9173
9174 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9175 points to an external_ref, DATA is the CU we're processing. If we don't
9176 already have a local stub, and we have multiple refs, build a stub. */
9177
9178 int
9179 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9180 {
9181 struct external_ref *ref_p = *slot;
9182
9183 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9184 {
9185 /* We have multiple references to this type, so build a small stub.
9186 Both of these forms are a bit dodgy from the perspective of the
9187 DWARF standard, since technically they should have names. */
9188 dw_die_ref cu = data;
9189 dw_die_ref type = ref_p->type;
9190 dw_die_ref stub = NULL;
9191
9192 if (type->comdat_type_p)
9193 {
9194 /* If we refer to this type via sig8, use AT_signature. */
9195 stub = new_die (type->die_tag, cu, NULL_TREE);
9196 add_AT_die_ref (stub, DW_AT_signature, type);
9197 }
9198 else
9199 {
9200 /* Otherwise, use a typedef with no name. */
9201 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9202 add_AT_die_ref (stub, DW_AT_type, type);
9203 }
9204
9205 stub->die_mark++;
9206 ref_p->stub = stub;
9207 }
9208 return 1;
9209 }
9210
9211 /* DIE is a unit; look through all the DIE references to see if there are
9212 any external references to types, and if so, create local stubs for
9213 them which will be applied in build_abbrev_table. This is useful because
9214 references to local DIEs are smaller. */
9215
9216 static external_ref_hash_type *
9217 optimize_external_refs (dw_die_ref die)
9218 {
9219 external_ref_hash_type *map = new external_ref_hash_type (10);
9220 optimize_external_refs_1 (die, map);
9221 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9222 return map;
9223 }
9224
9225 /* The following 3 variables are temporaries that are computed only during the
9226 build_abbrev_table call and used and released during the following
9227 optimize_abbrev_table call. */
9228
9229 /* First abbrev_id that can be optimized based on usage. */
9230 static unsigned int abbrev_opt_start;
9231
9232 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9233 abbrev_id smaller than this, because they must be already sized
9234 during build_abbrev_table). */
9235 static unsigned int abbrev_opt_base_type_end;
9236
9237 /* Vector of usage counts during build_abbrev_table. Indexed by
9238 abbrev_id - abbrev_opt_start. */
9239 static vec<unsigned int> abbrev_usage_count;
9240
9241 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9242 static vec<dw_die_ref> sorted_abbrev_dies;
9243
9244 /* The format of each DIE (and its attribute value pairs) is encoded in an
9245 abbreviation table. This routine builds the abbreviation table and assigns
9246 a unique abbreviation id for each abbreviation entry. The children of each
9247 die are visited recursively. */
9248
9249 static void
9250 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9251 {
9252 unsigned int abbrev_id = 0;
9253 dw_die_ref c;
9254 dw_attr_node *a;
9255 unsigned ix;
9256 dw_die_ref abbrev;
9257
9258 /* Scan the DIE references, and replace any that refer to
9259 DIEs from other CUs (i.e. those which are not marked) with
9260 the local stubs we built in optimize_external_refs. */
9261 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9262 if (AT_class (a) == dw_val_class_die_ref
9263 && (c = AT_ref (a))->die_mark == 0)
9264 {
9265 struct external_ref *ref_p;
9266 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9267
9268 if (is_type_die (c)
9269 && (ref_p = lookup_external_ref (extern_map, c))
9270 && ref_p->stub && ref_p->stub != die)
9271 {
9272 gcc_assert (a->dw_attr != DW_AT_signature);
9273 change_AT_die_ref (a, ref_p->stub);
9274 }
9275 else
9276 /* We aren't changing this reference, so mark it external. */
9277 set_AT_ref_external (a, 1);
9278 }
9279
9280 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9281 {
9282 dw_attr_node *die_a, *abbrev_a;
9283 unsigned ix;
9284 bool ok = true;
9285
9286 if (abbrev_id == 0)
9287 continue;
9288 if (abbrev->die_tag != die->die_tag)
9289 continue;
9290 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9291 continue;
9292
9293 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9294 continue;
9295
9296 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9297 {
9298 abbrev_a = &(*abbrev->die_attr)[ix];
9299 if ((abbrev_a->dw_attr != die_a->dw_attr)
9300 || (value_format (abbrev_a) != value_format (die_a)))
9301 {
9302 ok = false;
9303 break;
9304 }
9305 }
9306 if (ok)
9307 break;
9308 }
9309
9310 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9311 {
9312 vec_safe_push (abbrev_die_table, die);
9313 if (abbrev_opt_start)
9314 abbrev_usage_count.safe_push (0);
9315 }
9316 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9317 {
9318 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9319 sorted_abbrev_dies.safe_push (die);
9320 }
9321
9322 die->die_abbrev = abbrev_id;
9323 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9324 }
9325
9326 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9327 by die_abbrev's usage count, from the most commonly used
9328 abbreviation to the least. */
9329
9330 static int
9331 die_abbrev_cmp (const void *p1, const void *p2)
9332 {
9333 dw_die_ref die1 = *(const dw_die_ref *) p1;
9334 dw_die_ref die2 = *(const dw_die_ref *) p2;
9335
9336 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9337 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9338
9339 if (die1->die_abbrev >= abbrev_opt_base_type_end
9340 && die2->die_abbrev >= abbrev_opt_base_type_end)
9341 {
9342 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9343 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9344 return -1;
9345 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9346 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9347 return 1;
9348 }
9349
9350 /* Stabilize the sort. */
9351 if (die1->die_abbrev < die2->die_abbrev)
9352 return -1;
9353 if (die1->die_abbrev > die2->die_abbrev)
9354 return 1;
9355
9356 return 0;
9357 }
9358
9359 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9360 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9361 into dw_val_class_const_implicit or
9362 dw_val_class_unsigned_const_implicit. */
9363
9364 static void
9365 optimize_implicit_const (unsigned int first_id, unsigned int end,
9366 vec<bool> &implicit_consts)
9367 {
9368 /* It never makes sense if there is just one DIE using the abbreviation. */
9369 if (end < first_id + 2)
9370 return;
9371
9372 dw_attr_node *a;
9373 unsigned ix, i;
9374 dw_die_ref die = sorted_abbrev_dies[first_id];
9375 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9376 if (implicit_consts[ix])
9377 {
9378 enum dw_val_class new_class = dw_val_class_none;
9379 switch (AT_class (a))
9380 {
9381 case dw_val_class_unsigned_const:
9382 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9383 continue;
9384
9385 /* The .debug_abbrev section will grow by
9386 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9387 in all the DIEs using that abbreviation. */
9388 if (constant_size (AT_unsigned (a)) * (end - first_id)
9389 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9390 continue;
9391
9392 new_class = dw_val_class_unsigned_const_implicit;
9393 break;
9394
9395 case dw_val_class_const:
9396 new_class = dw_val_class_const_implicit;
9397 break;
9398
9399 case dw_val_class_file:
9400 new_class = dw_val_class_file_implicit;
9401 break;
9402
9403 default:
9404 continue;
9405 }
9406 for (i = first_id; i < end; i++)
9407 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9408 = new_class;
9409 }
9410 }
9411
9412 /* Attempt to optimize abbreviation table from abbrev_opt_start
9413 abbreviation above. */
9414
9415 static void
9416 optimize_abbrev_table (void)
9417 {
9418 if (abbrev_opt_start
9419 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9420 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9421 {
9422 auto_vec<bool, 32> implicit_consts;
9423 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9424
9425 unsigned int abbrev_id = abbrev_opt_start - 1;
9426 unsigned int first_id = ~0U;
9427 unsigned int last_abbrev_id = 0;
9428 unsigned int i;
9429 dw_die_ref die;
9430 if (abbrev_opt_base_type_end > abbrev_opt_start)
9431 abbrev_id = abbrev_opt_base_type_end - 1;
9432 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9433 most commonly used abbreviations come first. */
9434 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9435 {
9436 dw_attr_node *a;
9437 unsigned ix;
9438
9439 /* If calc_base_type_die_sizes has been called, the CU and
9440 base types after it can't be optimized, because we've already
9441 calculated their DIE offsets. We've sorted them first. */
9442 if (die->die_abbrev < abbrev_opt_base_type_end)
9443 continue;
9444 if (die->die_abbrev != last_abbrev_id)
9445 {
9446 last_abbrev_id = die->die_abbrev;
9447 if (dwarf_version >= 5 && first_id != ~0U)
9448 optimize_implicit_const (first_id, i, implicit_consts);
9449 abbrev_id++;
9450 (*abbrev_die_table)[abbrev_id] = die;
9451 if (dwarf_version >= 5)
9452 {
9453 first_id = i;
9454 implicit_consts.truncate (0);
9455
9456 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9457 switch (AT_class (a))
9458 {
9459 case dw_val_class_const:
9460 case dw_val_class_unsigned_const:
9461 case dw_val_class_file:
9462 implicit_consts.safe_push (true);
9463 break;
9464 default:
9465 implicit_consts.safe_push (false);
9466 break;
9467 }
9468 }
9469 }
9470 else if (dwarf_version >= 5)
9471 {
9472 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9473 if (!implicit_consts[ix])
9474 continue;
9475 else
9476 {
9477 dw_attr_node *other_a
9478 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9479 if (!dw_val_equal_p (&a->dw_attr_val,
9480 &other_a->dw_attr_val))
9481 implicit_consts[ix] = false;
9482 }
9483 }
9484 die->die_abbrev = abbrev_id;
9485 }
9486 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9487 if (dwarf_version >= 5 && first_id != ~0U)
9488 optimize_implicit_const (first_id, i, implicit_consts);
9489 }
9490
9491 abbrev_opt_start = 0;
9492 abbrev_opt_base_type_end = 0;
9493 abbrev_usage_count.release ();
9494 sorted_abbrev_dies.release ();
9495 }
9496
9497 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9498
9499 static int
9500 constant_size (unsigned HOST_WIDE_INT value)
9501 {
9502 int log;
9503
9504 if (value == 0)
9505 log = 0;
9506 else
9507 log = floor_log2 (value);
9508
9509 log = log / 8;
9510 log = 1 << (floor_log2 (log) + 1);
9511
9512 return log;
9513 }
9514
9515 /* Return the size of a DIE as it is represented in the
9516 .debug_info section. */
9517
9518 static unsigned long
9519 size_of_die (dw_die_ref die)
9520 {
9521 unsigned long size = 0;
9522 dw_attr_node *a;
9523 unsigned ix;
9524 enum dwarf_form form;
9525
9526 size += size_of_uleb128 (die->die_abbrev);
9527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9528 {
9529 switch (AT_class (a))
9530 {
9531 case dw_val_class_addr:
9532 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9533 {
9534 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9535 size += size_of_uleb128 (AT_index (a));
9536 }
9537 else
9538 size += DWARF2_ADDR_SIZE;
9539 break;
9540 case dw_val_class_offset:
9541 size += dwarf_offset_size;
9542 break;
9543 case dw_val_class_loc:
9544 {
9545 unsigned long lsize = size_of_locs (AT_loc (a));
9546
9547 /* Block length. */
9548 if (dwarf_version >= 4)
9549 size += size_of_uleb128 (lsize);
9550 else
9551 size += constant_size (lsize);
9552 size += lsize;
9553 }
9554 break;
9555 case dw_val_class_loc_list:
9556 if (dwarf_split_debug_info && dwarf_version >= 5)
9557 {
9558 gcc_assert (AT_loc_list (a)->num_assigned);
9559 size += size_of_uleb128 (AT_loc_list (a)->hash);
9560 }
9561 else
9562 size += dwarf_offset_size;
9563 break;
9564 case dw_val_class_view_list:
9565 size += dwarf_offset_size;
9566 break;
9567 case dw_val_class_range_list:
9568 if (value_format (a) == DW_FORM_rnglistx)
9569 {
9570 gcc_assert (rnglist_idx);
9571 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9572 size += size_of_uleb128 (r->idx);
9573 }
9574 else
9575 size += dwarf_offset_size;
9576 break;
9577 case dw_val_class_const:
9578 size += size_of_sleb128 (AT_int (a));
9579 break;
9580 case dw_val_class_unsigned_const:
9581 {
9582 int csize = constant_size (AT_unsigned (a));
9583 if (dwarf_version == 3
9584 && a->dw_attr == DW_AT_data_member_location
9585 && csize >= 4)
9586 size += size_of_uleb128 (AT_unsigned (a));
9587 else
9588 size += csize;
9589 }
9590 break;
9591 case dw_val_class_symview:
9592 if (symview_upper_bound <= 0xff)
9593 size += 1;
9594 else if (symview_upper_bound <= 0xffff)
9595 size += 2;
9596 else if (symview_upper_bound <= 0xffffffff)
9597 size += 4;
9598 else
9599 size += 8;
9600 break;
9601 case dw_val_class_const_implicit:
9602 case dw_val_class_unsigned_const_implicit:
9603 case dw_val_class_file_implicit:
9604 /* These occupy no size in the DIE, just an extra sleb128 in
9605 .debug_abbrev. */
9606 break;
9607 case dw_val_class_const_double:
9608 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9609 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9610 size++; /* block */
9611 break;
9612 case dw_val_class_wide_int:
9613 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9614 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9615 if (get_full_len (*a->dw_attr_val.v.val_wide)
9616 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9617 size++; /* block */
9618 break;
9619 case dw_val_class_vec:
9620 size += constant_size (a->dw_attr_val.v.val_vec.length
9621 * a->dw_attr_val.v.val_vec.elt_size)
9622 + a->dw_attr_val.v.val_vec.length
9623 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9624 break;
9625 case dw_val_class_flag:
9626 if (dwarf_version >= 4)
9627 /* Currently all add_AT_flag calls pass in 1 as last argument,
9628 so DW_FORM_flag_present can be used. If that ever changes,
9629 we'll need to use DW_FORM_flag and have some optimization
9630 in build_abbrev_table that will change those to
9631 DW_FORM_flag_present if it is set to 1 in all DIEs using
9632 the same abbrev entry. */
9633 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9634 else
9635 size += 1;
9636 break;
9637 case dw_val_class_die_ref:
9638 if (AT_ref_external (a))
9639 {
9640 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9641 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9642 is sized by target address length, whereas in DWARF3
9643 it's always sized as an offset. */
9644 if (AT_ref (a)->comdat_type_p)
9645 size += DWARF_TYPE_SIGNATURE_SIZE;
9646 else if (dwarf_version == 2)
9647 size += DWARF2_ADDR_SIZE;
9648 else
9649 size += dwarf_offset_size;
9650 }
9651 else
9652 size += dwarf_offset_size;
9653 break;
9654 case dw_val_class_fde_ref:
9655 size += dwarf_offset_size;
9656 break;
9657 case dw_val_class_lbl_id:
9658 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9659 {
9660 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9661 size += size_of_uleb128 (AT_index (a));
9662 }
9663 else
9664 size += DWARF2_ADDR_SIZE;
9665 break;
9666 case dw_val_class_lineptr:
9667 case dw_val_class_macptr:
9668 case dw_val_class_loclistsptr:
9669 size += dwarf_offset_size;
9670 break;
9671 case dw_val_class_str:
9672 form = AT_string_form (a);
9673 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9674 size += dwarf_offset_size;
9675 else if (form == dwarf_FORM (DW_FORM_strx))
9676 size += size_of_uleb128 (AT_index (a));
9677 else
9678 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9679 break;
9680 case dw_val_class_file:
9681 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9682 break;
9683 case dw_val_class_data8:
9684 size += 8;
9685 break;
9686 case dw_val_class_vms_delta:
9687 size += dwarf_offset_size;
9688 break;
9689 case dw_val_class_high_pc:
9690 size += DWARF2_ADDR_SIZE;
9691 break;
9692 case dw_val_class_discr_value:
9693 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9694 break;
9695 case dw_val_class_discr_list:
9696 {
9697 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9698
9699 /* This is a block, so we have the block length and then its
9700 data. */
9701 size += constant_size (block_size) + block_size;
9702 }
9703 break;
9704 default:
9705 gcc_unreachable ();
9706 }
9707 }
9708
9709 return size;
9710 }
9711
9712 /* Size the debugging information associated with a given DIE. Visits the
9713 DIE's children recursively. Updates the global variable next_die_offset, on
9714 each time through. Uses the current value of next_die_offset to update the
9715 die_offset field in each DIE. */
9716
9717 static void
9718 calc_die_sizes (dw_die_ref die)
9719 {
9720 dw_die_ref c;
9721
9722 gcc_assert (die->die_offset == 0
9723 || (unsigned long int) die->die_offset == next_die_offset);
9724 die->die_offset = next_die_offset;
9725 next_die_offset += size_of_die (die);
9726
9727 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9728
9729 if (die->die_child != NULL)
9730 /* Count the null byte used to terminate sibling lists. */
9731 next_die_offset += 1;
9732 }
9733
9734 /* Size just the base type children at the start of the CU.
9735 This is needed because build_abbrev needs to size locs
9736 and sizing of type based stack ops needs to know die_offset
9737 values for the base types. */
9738
9739 static void
9740 calc_base_type_die_sizes (void)
9741 {
9742 unsigned long die_offset = (dwarf_split_debug_info
9743 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9744 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9745 unsigned int i;
9746 dw_die_ref base_type;
9747 #if ENABLE_ASSERT_CHECKING
9748 dw_die_ref prev = comp_unit_die ()->die_child;
9749 #endif
9750
9751 die_offset += size_of_die (comp_unit_die ());
9752 for (i = 0; base_types.iterate (i, &base_type); i++)
9753 {
9754 #if ENABLE_ASSERT_CHECKING
9755 gcc_assert (base_type->die_offset == 0
9756 && prev->die_sib == base_type
9757 && base_type->die_child == NULL
9758 && base_type->die_abbrev);
9759 prev = base_type;
9760 #endif
9761 if (abbrev_opt_start
9762 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9763 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9764 base_type->die_offset = die_offset;
9765 die_offset += size_of_die (base_type);
9766 }
9767 }
9768
9769 /* Set the marks for a die and its children. We do this so
9770 that we know whether or not a reference needs to use FORM_ref_addr; only
9771 DIEs in the same CU will be marked. We used to clear out the offset
9772 and use that as the flag, but ran into ordering problems. */
9773
9774 static void
9775 mark_dies (dw_die_ref die)
9776 {
9777 dw_die_ref c;
9778
9779 gcc_assert (!die->die_mark);
9780
9781 die->die_mark = 1;
9782 FOR_EACH_CHILD (die, c, mark_dies (c));
9783 }
9784
9785 /* Clear the marks for a die and its children. */
9786
9787 static void
9788 unmark_dies (dw_die_ref die)
9789 {
9790 dw_die_ref c;
9791
9792 if (! use_debug_types)
9793 gcc_assert (die->die_mark);
9794
9795 die->die_mark = 0;
9796 FOR_EACH_CHILD (die, c, unmark_dies (c));
9797 }
9798
9799 /* Clear the marks for a die, its children and referred dies. */
9800
9801 static void
9802 unmark_all_dies (dw_die_ref die)
9803 {
9804 dw_die_ref c;
9805 dw_attr_node *a;
9806 unsigned ix;
9807
9808 if (!die->die_mark)
9809 return;
9810 die->die_mark = 0;
9811
9812 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9813
9814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9815 if (AT_class (a) == dw_val_class_die_ref)
9816 unmark_all_dies (AT_ref (a));
9817 }
9818
9819 /* Calculate if the entry should appear in the final output file. It may be
9820 from a pruned a type. */
9821
9822 static bool
9823 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9824 {
9825 /* By limiting gnu pubnames to definitions only, gold can generate a
9826 gdb index without entries for declarations, which don't include
9827 enough information to be useful. */
9828 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9829 return false;
9830
9831 if (table == pubname_table)
9832 {
9833 /* Enumerator names are part of the pubname table, but the
9834 parent DW_TAG_enumeration_type die may have been pruned.
9835 Don't output them if that is the case. */
9836 if (p->die->die_tag == DW_TAG_enumerator &&
9837 (p->die->die_parent == NULL
9838 || !p->die->die_parent->die_perennial_p))
9839 return false;
9840
9841 /* Everything else in the pubname table is included. */
9842 return true;
9843 }
9844
9845 /* The pubtypes table shouldn't include types that have been
9846 pruned. */
9847 return (p->die->die_offset != 0
9848 || !flag_eliminate_unused_debug_types);
9849 }
9850
9851 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9852 generated for the compilation unit. */
9853
9854 static unsigned long
9855 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9856 {
9857 unsigned long size;
9858 unsigned i;
9859 pubname_entry *p;
9860 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9861
9862 size = DWARF_PUBNAMES_HEADER_SIZE;
9863 FOR_EACH_VEC_ELT (*names, i, p)
9864 if (include_pubname_in_output (names, p))
9865 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9866
9867 size += dwarf_offset_size;
9868 return size;
9869 }
9870
9871 /* Return the size of the information in the .debug_aranges section. */
9872
9873 static unsigned long
9874 size_of_aranges (void)
9875 {
9876 unsigned long size;
9877
9878 size = DWARF_ARANGES_HEADER_SIZE;
9879
9880 /* Count the address/length pair for this compilation unit. */
9881 if (switch_text_ranges)
9882 size += 2 * DWARF2_ADDR_SIZE
9883 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9884 if (switch_cold_ranges)
9885 size += 2 * DWARF2_ADDR_SIZE
9886 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9887 if (have_multiple_function_sections)
9888 {
9889 unsigned fde_idx;
9890 dw_fde_ref fde;
9891
9892 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9893 {
9894 if (fde->ignored_debug)
9895 continue;
9896 if (!fde->in_std_section)
9897 size += 2 * DWARF2_ADDR_SIZE;
9898 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9899 size += 2 * DWARF2_ADDR_SIZE;
9900 }
9901 }
9902
9903 /* Count the two zero words used to terminated the address range table. */
9904 size += 2 * DWARF2_ADDR_SIZE;
9905 return size;
9906 }
9907
9908 /* Select the encoding of an attribute value. */
9909
9910 static enum dwarf_form
9911 value_format (dw_attr_node *a)
9912 {
9913 switch (AT_class (a))
9914 {
9915 case dw_val_class_addr:
9916 /* Only very few attributes allow DW_FORM_addr. */
9917 switch (a->dw_attr)
9918 {
9919 case DW_AT_low_pc:
9920 case DW_AT_high_pc:
9921 case DW_AT_entry_pc:
9922 case DW_AT_trampoline:
9923 return (AT_index (a) == NOT_INDEXED
9924 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9925 default:
9926 break;
9927 }
9928 switch (DWARF2_ADDR_SIZE)
9929 {
9930 case 1:
9931 return DW_FORM_data1;
9932 case 2:
9933 return DW_FORM_data2;
9934 case 4:
9935 return DW_FORM_data4;
9936 case 8:
9937 return DW_FORM_data8;
9938 default:
9939 gcc_unreachable ();
9940 }
9941 case dw_val_class_loc_list:
9942 if (dwarf_split_debug_info
9943 && dwarf_version >= 5
9944 && AT_loc_list (a)->num_assigned)
9945 return DW_FORM_loclistx;
9946 /* FALLTHRU */
9947 case dw_val_class_view_list:
9948 case dw_val_class_range_list:
9949 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9950 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9951 care about sizes of .debug* sections in shared libraries and
9952 executables and don't take into account relocations that affect just
9953 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9954 table in the .debug_rnglists section. */
9955 if (dwarf_split_debug_info
9956 && dwarf_version >= 5
9957 && AT_class (a) == dw_val_class_range_list
9958 && rnglist_idx
9959 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9960 return DW_FORM_rnglistx;
9961 if (dwarf_version >= 4)
9962 return DW_FORM_sec_offset;
9963 /* FALLTHRU */
9964 case dw_val_class_vms_delta:
9965 case dw_val_class_offset:
9966 switch (dwarf_offset_size)
9967 {
9968 case 4:
9969 return DW_FORM_data4;
9970 case 8:
9971 return DW_FORM_data8;
9972 default:
9973 gcc_unreachable ();
9974 }
9975 case dw_val_class_loc:
9976 if (dwarf_version >= 4)
9977 return DW_FORM_exprloc;
9978 switch (constant_size (size_of_locs (AT_loc (a))))
9979 {
9980 case 1:
9981 return DW_FORM_block1;
9982 case 2:
9983 return DW_FORM_block2;
9984 case 4:
9985 return DW_FORM_block4;
9986 default:
9987 gcc_unreachable ();
9988 }
9989 case dw_val_class_const:
9990 return DW_FORM_sdata;
9991 case dw_val_class_unsigned_const:
9992 switch (constant_size (AT_unsigned (a)))
9993 {
9994 case 1:
9995 return DW_FORM_data1;
9996 case 2:
9997 return DW_FORM_data2;
9998 case 4:
9999 /* In DWARF3 DW_AT_data_member_location with
10000 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
10001 constant, so we need to use DW_FORM_udata if we need
10002 a large constant. */
10003 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10004 return DW_FORM_udata;
10005 return DW_FORM_data4;
10006 case 8:
10007 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10008 return DW_FORM_udata;
10009 return DW_FORM_data8;
10010 default:
10011 gcc_unreachable ();
10012 }
10013 case dw_val_class_const_implicit:
10014 case dw_val_class_unsigned_const_implicit:
10015 case dw_val_class_file_implicit:
10016 return DW_FORM_implicit_const;
10017 case dw_val_class_const_double:
10018 switch (HOST_BITS_PER_WIDE_INT)
10019 {
10020 case 8:
10021 return DW_FORM_data2;
10022 case 16:
10023 return DW_FORM_data4;
10024 case 32:
10025 return DW_FORM_data8;
10026 case 64:
10027 if (dwarf_version >= 5)
10028 return DW_FORM_data16;
10029 /* FALLTHRU */
10030 default:
10031 return DW_FORM_block1;
10032 }
10033 case dw_val_class_wide_int:
10034 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10035 {
10036 case 8:
10037 return DW_FORM_data1;
10038 case 16:
10039 return DW_FORM_data2;
10040 case 32:
10041 return DW_FORM_data4;
10042 case 64:
10043 return DW_FORM_data8;
10044 case 128:
10045 if (dwarf_version >= 5)
10046 return DW_FORM_data16;
10047 /* FALLTHRU */
10048 default:
10049 return DW_FORM_block1;
10050 }
10051 case dw_val_class_symview:
10052 /* ??? We might use uleb128, but then we'd have to compute
10053 .debug_info offsets in the assembler. */
10054 if (symview_upper_bound <= 0xff)
10055 return DW_FORM_data1;
10056 else if (symview_upper_bound <= 0xffff)
10057 return DW_FORM_data2;
10058 else if (symview_upper_bound <= 0xffffffff)
10059 return DW_FORM_data4;
10060 else
10061 return DW_FORM_data8;
10062 case dw_val_class_vec:
10063 switch (constant_size (a->dw_attr_val.v.val_vec.length
10064 * a->dw_attr_val.v.val_vec.elt_size))
10065 {
10066 case 1:
10067 return DW_FORM_block1;
10068 case 2:
10069 return DW_FORM_block2;
10070 case 4:
10071 return DW_FORM_block4;
10072 default:
10073 gcc_unreachable ();
10074 }
10075 case dw_val_class_flag:
10076 if (dwarf_version >= 4)
10077 {
10078 /* Currently all add_AT_flag calls pass in 1 as last argument,
10079 so DW_FORM_flag_present can be used. If that ever changes,
10080 we'll need to use DW_FORM_flag and have some optimization
10081 in build_abbrev_table that will change those to
10082 DW_FORM_flag_present if it is set to 1 in all DIEs using
10083 the same abbrev entry. */
10084 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10085 return DW_FORM_flag_present;
10086 }
10087 return DW_FORM_flag;
10088 case dw_val_class_die_ref:
10089 if (AT_ref_external (a))
10090 {
10091 if (AT_ref (a)->comdat_type_p)
10092 return DW_FORM_ref_sig8;
10093 else
10094 return DW_FORM_ref_addr;
10095 }
10096 else
10097 return DW_FORM_ref;
10098 case dw_val_class_fde_ref:
10099 return DW_FORM_data;
10100 case dw_val_class_lbl_id:
10101 return (AT_index (a) == NOT_INDEXED
10102 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10103 case dw_val_class_lineptr:
10104 case dw_val_class_macptr:
10105 case dw_val_class_loclistsptr:
10106 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10107 case dw_val_class_str:
10108 return AT_string_form (a);
10109 case dw_val_class_file:
10110 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10111 {
10112 case 1:
10113 return DW_FORM_data1;
10114 case 2:
10115 return DW_FORM_data2;
10116 case 4:
10117 return DW_FORM_data4;
10118 default:
10119 gcc_unreachable ();
10120 }
10121
10122 case dw_val_class_data8:
10123 return DW_FORM_data8;
10124
10125 case dw_val_class_high_pc:
10126 switch (DWARF2_ADDR_SIZE)
10127 {
10128 case 1:
10129 return DW_FORM_data1;
10130 case 2:
10131 return DW_FORM_data2;
10132 case 4:
10133 return DW_FORM_data4;
10134 case 8:
10135 return DW_FORM_data8;
10136 default:
10137 gcc_unreachable ();
10138 }
10139
10140 case dw_val_class_discr_value:
10141 return (a->dw_attr_val.v.val_discr_value.pos
10142 ? DW_FORM_udata
10143 : DW_FORM_sdata);
10144 case dw_val_class_discr_list:
10145 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10146 {
10147 case 1:
10148 return DW_FORM_block1;
10149 case 2:
10150 return DW_FORM_block2;
10151 case 4:
10152 return DW_FORM_block4;
10153 default:
10154 gcc_unreachable ();
10155 }
10156
10157 default:
10158 gcc_unreachable ();
10159 }
10160 }
10161
10162 /* Output the encoding of an attribute value. */
10163
10164 static void
10165 output_value_format (dw_attr_node *a)
10166 {
10167 enum dwarf_form form = value_format (a);
10168
10169 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10170 }
10171
10172 /* Given a die and id, produce the appropriate abbreviations. */
10173
10174 static void
10175 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10176 {
10177 unsigned ix;
10178 dw_attr_node *a_attr;
10179
10180 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10181 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10182 dwarf_tag_name (abbrev->die_tag));
10183
10184 if (abbrev->die_child != NULL)
10185 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10186 else
10187 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10188
10189 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10190 {
10191 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10192 dwarf_attr_name (a_attr->dw_attr));
10193 output_value_format (a_attr);
10194 if (value_format (a_attr) == DW_FORM_implicit_const)
10195 {
10196 if (AT_class (a_attr) == dw_val_class_file_implicit)
10197 {
10198 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10199 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10200 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10201 }
10202 else
10203 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10204 }
10205 }
10206
10207 dw2_asm_output_data (1, 0, NULL);
10208 dw2_asm_output_data (1, 0, NULL);
10209 }
10210
10211
10212 /* Output the .debug_abbrev section which defines the DIE abbreviation
10213 table. */
10214
10215 static void
10216 output_abbrev_section (void)
10217 {
10218 unsigned int abbrev_id;
10219 dw_die_ref abbrev;
10220
10221 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10222 if (abbrev_id != 0)
10223 output_die_abbrevs (abbrev_id, abbrev);
10224
10225 /* Terminate the table. */
10226 dw2_asm_output_data (1, 0, NULL);
10227 }
10228
10229 /* Return a new location list, given the begin and end range, and the
10230 expression. */
10231
10232 static inline dw_loc_list_ref
10233 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10234 const char *end, var_loc_view vend,
10235 const char *section)
10236 {
10237 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10238
10239 retlist->begin = begin;
10240 retlist->begin_entry = NULL;
10241 retlist->end = end;
10242 retlist->end_entry = NULL;
10243 retlist->expr = expr;
10244 retlist->section = section;
10245 retlist->vbegin = vbegin;
10246 retlist->vend = vend;
10247
10248 return retlist;
10249 }
10250
10251 /* Return true iff there's any nonzero view number in the loc list.
10252
10253 ??? When views are not enabled, we'll often extend a single range
10254 to the entire function, so that we emit a single location
10255 expression rather than a location list. With views, even with a
10256 single range, we'll output a list if start or end have a nonzero
10257 view. If we change this, we may want to stop splitting a single
10258 range in dw_loc_list just because of a nonzero view, even if it
10259 straddles across hot/cold partitions. */
10260
10261 static bool
10262 loc_list_has_views (dw_loc_list_ref list)
10263 {
10264 if (!debug_variable_location_views)
10265 return false;
10266
10267 for (dw_loc_list_ref loc = list;
10268 loc != NULL; loc = loc->dw_loc_next)
10269 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10270 return true;
10271
10272 return false;
10273 }
10274
10275 /* Generate a new internal symbol for this location list node, if it
10276 hasn't got one yet. */
10277
10278 static inline void
10279 gen_llsym (dw_loc_list_ref list)
10280 {
10281 gcc_assert (!list->ll_symbol);
10282 list->ll_symbol = gen_internal_sym ("LLST");
10283
10284 if (!loc_list_has_views (list))
10285 return;
10286
10287 if (dwarf2out_locviews_in_attribute ())
10288 {
10289 /* Use the same label_num for the view list. */
10290 label_num--;
10291 list->vl_symbol = gen_internal_sym ("LVUS");
10292 }
10293 else
10294 list->vl_symbol = list->ll_symbol;
10295 }
10296
10297 /* Generate a symbol for the list, but only if we really want to emit
10298 it as a list. */
10299
10300 static inline void
10301 maybe_gen_llsym (dw_loc_list_ref list)
10302 {
10303 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10304 return;
10305
10306 gen_llsym (list);
10307 }
10308
10309 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10310 NULL, don't consider size of the location expression. If we're not
10311 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10312 representation in *SIZEP. */
10313
10314 static bool
10315 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10316 {
10317 /* Don't output an entry that starts and ends at the same address. */
10318 if (strcmp (curr->begin, curr->end) == 0
10319 && curr->vbegin == curr->vend && !curr->force)
10320 return true;
10321
10322 if (!sizep)
10323 return false;
10324
10325 unsigned long size = size_of_locs (curr->expr);
10326
10327 /* If the expression is too large, drop it on the floor. We could
10328 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10329 in the expression, but >= 64KB expressions for a single value
10330 in a single range are unlikely very useful. */
10331 if (dwarf_version < 5 && size > 0xffff)
10332 return true;
10333
10334 *sizep = size;
10335
10336 return false;
10337 }
10338
10339 /* Output a view pair loclist entry for CURR, if it requires one. */
10340
10341 static void
10342 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10343 {
10344 if (!dwarf2out_locviews_in_loclist ())
10345 return;
10346
10347 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10348 return;
10349
10350 #ifdef DW_LLE_view_pair
10351 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10352
10353 if (dwarf2out_as_locview_support)
10354 {
10355 if (ZERO_VIEW_P (curr->vbegin))
10356 dw2_asm_output_data_uleb128 (0, "Location view begin");
10357 else
10358 {
10359 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10360 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10361 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10362 }
10363
10364 if (ZERO_VIEW_P (curr->vend))
10365 dw2_asm_output_data_uleb128 (0, "Location view end");
10366 else
10367 {
10368 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10369 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10370 dw2_asm_output_symname_uleb128 (label, "Location view end");
10371 }
10372 }
10373 else
10374 {
10375 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vbegin)
10376 ? 0 : curr->vbegin, "Location view begin");
10377 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vend)
10378 ? 0 : curr->vend, "Location view end");
10379 }
10380 #endif /* DW_LLE_view_pair */
10381
10382 return;
10383 }
10384
10385 /* Output the location list given to us. */
10386
10387 static void
10388 output_loc_list (dw_loc_list_ref list_head)
10389 {
10390 int vcount = 0, lcount = 0;
10391
10392 if (list_head->emitted)
10393 return;
10394 list_head->emitted = true;
10395
10396 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10397 {
10398 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10399
10400 for (dw_loc_list_ref curr = list_head; curr != NULL;
10401 curr = curr->dw_loc_next)
10402 {
10403 unsigned long size;
10404
10405 if (skip_loc_list_entry (curr, &size))
10406 continue;
10407
10408 vcount++;
10409
10410 /* ?? dwarf_split_debug_info? */
10411 if (dwarf2out_as_locview_support)
10412 {
10413 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10414
10415 if (!ZERO_VIEW_P (curr->vbegin))
10416 {
10417 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10418 dw2_asm_output_symname_uleb128 (label,
10419 "View list begin (%s)",
10420 list_head->vl_symbol);
10421 }
10422 else
10423 dw2_asm_output_data_uleb128 (0,
10424 "View list begin (%s)",
10425 list_head->vl_symbol);
10426
10427 if (!ZERO_VIEW_P (curr->vend))
10428 {
10429 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10430 dw2_asm_output_symname_uleb128 (label,
10431 "View list end (%s)",
10432 list_head->vl_symbol);
10433 }
10434 else
10435 dw2_asm_output_data_uleb128 (0,
10436 "View list end (%s)",
10437 list_head->vl_symbol);
10438 }
10439 else
10440 {
10441 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vbegin)
10442 ? 0 : curr->vbegin,
10443 "View list begin (%s)",
10444 list_head->vl_symbol);
10445 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vend)
10446 ? 0 : curr->vend,
10447 "View list end (%s)",
10448 list_head->vl_symbol);
10449 }
10450 }
10451 }
10452
10453 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10454
10455 const char *last_section = NULL;
10456 const char *base_label = NULL;
10457
10458 /* Walk the location list, and output each range + expression. */
10459 for (dw_loc_list_ref curr = list_head; curr != NULL;
10460 curr = curr->dw_loc_next)
10461 {
10462 unsigned long size;
10463
10464 /* Skip this entry? If we skip it here, we must skip it in the
10465 view list above as well. */
10466 if (skip_loc_list_entry (curr, &size))
10467 continue;
10468
10469 lcount++;
10470
10471 if (dwarf_version >= 5)
10472 {
10473 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10474 {
10475 dwarf2out_maybe_output_loclist_view_pair (curr);
10476 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10477 uleb128 index into .debug_addr and uleb128 length. */
10478 dw2_asm_output_data (1, DW_LLE_startx_length,
10479 "DW_LLE_startx_length (%s)",
10480 list_head->ll_symbol);
10481 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10482 "Location list range start index "
10483 "(%s)", curr->begin);
10484 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10485 "Location list length (%s)",
10486 list_head->ll_symbol);
10487 }
10488 else if (dwarf_split_debug_info)
10489 {
10490 dwarf2out_maybe_output_loclist_view_pair (curr);
10491 /* For -gsplit-dwarf without usable .uleb128 support, emit
10492 DW_LLE_startx_endx, which has two uleb128 indexes into
10493 .debug_addr. */
10494 dw2_asm_output_data (1, DW_LLE_startx_endx,
10495 "DW_LLE_startx_endx (%s)",
10496 list_head->ll_symbol);
10497 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10498 "Location list range start index "
10499 "(%s)", curr->begin);
10500 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10501 "Location list range end index "
10502 "(%s)", curr->end);
10503 }
10504 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10505 {
10506 dwarf2out_maybe_output_loclist_view_pair (curr);
10507 /* If all code is in .text section, the base address is
10508 already provided by the CU attributes. Use
10509 DW_LLE_offset_pair where both addresses are uleb128 encoded
10510 offsets against that base. */
10511 dw2_asm_output_data (1, DW_LLE_offset_pair,
10512 "DW_LLE_offset_pair (%s)",
10513 list_head->ll_symbol);
10514 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10515 "Location list begin address (%s)",
10516 list_head->ll_symbol);
10517 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10518 "Location list end address (%s)",
10519 list_head->ll_symbol);
10520 }
10521 else if (HAVE_AS_LEB128)
10522 {
10523 /* Otherwise, find out how many consecutive entries could share
10524 the same base entry. If just one, emit DW_LLE_start_length,
10525 otherwise emit DW_LLE_base_address for the base address
10526 followed by a series of DW_LLE_offset_pair. */
10527 if (last_section == NULL || curr->section != last_section)
10528 {
10529 dw_loc_list_ref curr2;
10530 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10531 curr2 = curr2->dw_loc_next)
10532 {
10533 if (strcmp (curr2->begin, curr2->end) == 0
10534 && !curr2->force)
10535 continue;
10536 break;
10537 }
10538 if (curr2 == NULL || curr->section != curr2->section)
10539 last_section = NULL;
10540 else
10541 {
10542 last_section = curr->section;
10543 base_label = curr->begin;
10544 dw2_asm_output_data (1, DW_LLE_base_address,
10545 "DW_LLE_base_address (%s)",
10546 list_head->ll_symbol);
10547 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10548 "Base address (%s)",
10549 list_head->ll_symbol);
10550 }
10551 }
10552 /* Only one entry with the same base address. Use
10553 DW_LLE_start_length with absolute address and uleb128
10554 length. */
10555 if (last_section == NULL)
10556 {
10557 dwarf2out_maybe_output_loclist_view_pair (curr);
10558 dw2_asm_output_data (1, DW_LLE_start_length,
10559 "DW_LLE_start_length (%s)",
10560 list_head->ll_symbol);
10561 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10562 "Location list begin address (%s)",
10563 list_head->ll_symbol);
10564 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10565 "Location list length "
10566 "(%s)", list_head->ll_symbol);
10567 }
10568 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10569 DW_LLE_base_address. */
10570 else
10571 {
10572 dwarf2out_maybe_output_loclist_view_pair (curr);
10573 dw2_asm_output_data (1, DW_LLE_offset_pair,
10574 "DW_LLE_offset_pair (%s)",
10575 list_head->ll_symbol);
10576 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10577 "Location list begin address "
10578 "(%s)", list_head->ll_symbol);
10579 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10580 "Location list end address "
10581 "(%s)", list_head->ll_symbol);
10582 }
10583 }
10584 /* The assembler does not support .uleb128 directive. Emit
10585 DW_LLE_start_end with a pair of absolute addresses. */
10586 else
10587 {
10588 dwarf2out_maybe_output_loclist_view_pair (curr);
10589 dw2_asm_output_data (1, DW_LLE_start_end,
10590 "DW_LLE_start_end (%s)",
10591 list_head->ll_symbol);
10592 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10593 "Location list begin address (%s)",
10594 list_head->ll_symbol);
10595 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10596 "Location list end address (%s)",
10597 list_head->ll_symbol);
10598 }
10599 }
10600 else if (dwarf_split_debug_info)
10601 {
10602 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10603 and 4 byte length. */
10604 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10605 "Location list start/length entry (%s)",
10606 list_head->ll_symbol);
10607 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10608 "Location list range start index (%s)",
10609 curr->begin);
10610 /* The length field is 4 bytes. If we ever need to support
10611 an 8-byte length, we can add a new DW_LLE code or fall back
10612 to DW_LLE_GNU_start_end_entry. */
10613 dw2_asm_output_delta (4, curr->end, curr->begin,
10614 "Location list range length (%s)",
10615 list_head->ll_symbol);
10616 }
10617 else if (!have_multiple_function_sections)
10618 {
10619 /* Pair of relative addresses against start of text section. */
10620 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10621 "Location list begin address (%s)",
10622 list_head->ll_symbol);
10623 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10624 "Location list end address (%s)",
10625 list_head->ll_symbol);
10626 }
10627 else
10628 {
10629 /* Pair of absolute addresses. */
10630 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10631 "Location list begin address (%s)",
10632 list_head->ll_symbol);
10633 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10634 "Location list end address (%s)",
10635 list_head->ll_symbol);
10636 }
10637
10638 /* Output the block length for this list of location operations. */
10639 if (dwarf_version >= 5)
10640 dw2_asm_output_data_uleb128 (size, "Location expression size");
10641 else
10642 {
10643 gcc_assert (size <= 0xffff);
10644 dw2_asm_output_data (2, size, "Location expression size");
10645 }
10646
10647 output_loc_sequence (curr->expr, -1);
10648 }
10649
10650 /* And finally list termination. */
10651 if (dwarf_version >= 5)
10652 dw2_asm_output_data (1, DW_LLE_end_of_list,
10653 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10654 else if (dwarf_split_debug_info)
10655 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10656 "Location list terminator (%s)",
10657 list_head->ll_symbol);
10658 else
10659 {
10660 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10661 "Location list terminator begin (%s)",
10662 list_head->ll_symbol);
10663 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10664 "Location list terminator end (%s)",
10665 list_head->ll_symbol);
10666 }
10667
10668 gcc_assert (!list_head->vl_symbol
10669 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10670 }
10671
10672 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10673 section. Emit a relocated reference if val_entry is NULL, otherwise,
10674 emit an indirect reference. */
10675
10676 static void
10677 output_range_list_offset (dw_attr_node *a)
10678 {
10679 const char *name = dwarf_attr_name (a->dw_attr);
10680
10681 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10682 {
10683 if (dwarf_version >= 5)
10684 {
10685 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10686 dw2_asm_output_offset (dwarf_offset_size, r->label,
10687 debug_ranges_section, "%s", name);
10688 }
10689 else
10690 {
10691 char *p = strchr (ranges_section_label, '\0');
10692 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10693 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10694 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10695 debug_ranges_section, "%s", name);
10696 *p = '\0';
10697 }
10698 }
10699 else if (dwarf_version >= 5)
10700 {
10701 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10702 gcc_assert (rnglist_idx);
10703 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10704 }
10705 else
10706 dw2_asm_output_data (dwarf_offset_size,
10707 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10708 "%s (offset from %s)", name, ranges_section_label);
10709 }
10710
10711 /* Output the offset into the debug_loc section. */
10712
10713 static void
10714 output_loc_list_offset (dw_attr_node *a)
10715 {
10716 char *sym = AT_loc_list (a)->ll_symbol;
10717
10718 gcc_assert (sym);
10719 if (!dwarf_split_debug_info)
10720 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10721 "%s", dwarf_attr_name (a->dw_attr));
10722 else if (dwarf_version >= 5)
10723 {
10724 gcc_assert (AT_loc_list (a)->num_assigned);
10725 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10726 dwarf_attr_name (a->dw_attr),
10727 sym);
10728 }
10729 else
10730 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10731 "%s", dwarf_attr_name (a->dw_attr));
10732 }
10733
10734 /* Output the offset into the debug_loc section. */
10735
10736 static void
10737 output_view_list_offset (dw_attr_node *a)
10738 {
10739 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10740
10741 gcc_assert (sym);
10742 if (dwarf_split_debug_info)
10743 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10744 "%s", dwarf_attr_name (a->dw_attr));
10745 else
10746 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10747 "%s", dwarf_attr_name (a->dw_attr));
10748 }
10749
10750 /* Output an attribute's index or value appropriately. */
10751
10752 static void
10753 output_attr_index_or_value (dw_attr_node *a)
10754 {
10755 const char *name = dwarf_attr_name (a->dw_attr);
10756
10757 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10758 {
10759 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10760 return;
10761 }
10762 switch (AT_class (a))
10763 {
10764 case dw_val_class_addr:
10765 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10766 break;
10767 case dw_val_class_high_pc:
10768 case dw_val_class_lbl_id:
10769 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10770 break;
10771 default:
10772 gcc_unreachable ();
10773 }
10774 }
10775
10776 /* Output a type signature. */
10777
10778 static inline void
10779 output_signature (const char *sig, const char *name)
10780 {
10781 int i;
10782
10783 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10784 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10785 }
10786
10787 /* Output a discriminant value. */
10788
10789 static inline void
10790 output_discr_value (dw_discr_value *discr_value, const char *name)
10791 {
10792 if (discr_value->pos)
10793 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10794 else
10795 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10796 }
10797
10798 /* Output the DIE and its attributes. Called recursively to generate
10799 the definitions of each child DIE. */
10800
10801 static void
10802 output_die (dw_die_ref die)
10803 {
10804 dw_attr_node *a;
10805 dw_die_ref c;
10806 unsigned long size;
10807 unsigned ix;
10808
10809 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10810 (unsigned long)die->die_offset,
10811 dwarf_tag_name (die->die_tag));
10812
10813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10814 {
10815 const char *name = dwarf_attr_name (a->dw_attr);
10816
10817 switch (AT_class (a))
10818 {
10819 case dw_val_class_addr:
10820 output_attr_index_or_value (a);
10821 break;
10822
10823 case dw_val_class_offset:
10824 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10825 "%s", name);
10826 break;
10827
10828 case dw_val_class_range_list:
10829 output_range_list_offset (a);
10830 break;
10831
10832 case dw_val_class_loc:
10833 size = size_of_locs (AT_loc (a));
10834
10835 /* Output the block length for this list of location operations. */
10836 if (dwarf_version >= 4)
10837 dw2_asm_output_data_uleb128 (size, "%s", name);
10838 else
10839 dw2_asm_output_data (constant_size (size), size, "%s", name);
10840
10841 output_loc_sequence (AT_loc (a), -1);
10842 break;
10843
10844 case dw_val_class_const:
10845 /* ??? It would be slightly more efficient to use a scheme like is
10846 used for unsigned constants below, but gdb 4.x does not sign
10847 extend. Gdb 5.x does sign extend. */
10848 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10849 break;
10850
10851 case dw_val_class_unsigned_const:
10852 {
10853 int csize = constant_size (AT_unsigned (a));
10854 if (dwarf_version == 3
10855 && a->dw_attr == DW_AT_data_member_location
10856 && csize >= 4)
10857 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10858 else
10859 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10860 }
10861 break;
10862
10863 case dw_val_class_symview:
10864 {
10865 int vsize;
10866 if (symview_upper_bound <= 0xff)
10867 vsize = 1;
10868 else if (symview_upper_bound <= 0xffff)
10869 vsize = 2;
10870 else if (symview_upper_bound <= 0xffffffff)
10871 vsize = 4;
10872 else
10873 vsize = 8;
10874 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10875 "%s", name);
10876 }
10877 break;
10878
10879 case dw_val_class_const_implicit:
10880 if (flag_debug_asm)
10881 fprintf (asm_out_file, "\t\t\t%s %s ("
10882 HOST_WIDE_INT_PRINT_DEC ")\n",
10883 ASM_COMMENT_START, name, AT_int (a));
10884 break;
10885
10886 case dw_val_class_unsigned_const_implicit:
10887 if (flag_debug_asm)
10888 fprintf (asm_out_file, "\t\t\t%s %s ("
10889 HOST_WIDE_INT_PRINT_HEX ")\n",
10890 ASM_COMMENT_START, name, AT_unsigned (a));
10891 break;
10892
10893 case dw_val_class_const_double:
10894 {
10895 unsigned HOST_WIDE_INT first, second;
10896
10897 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10898 dw2_asm_output_data (1,
10899 HOST_BITS_PER_DOUBLE_INT
10900 / HOST_BITS_PER_CHAR,
10901 NULL);
10902
10903 if (WORDS_BIG_ENDIAN)
10904 {
10905 first = a->dw_attr_val.v.val_double.high;
10906 second = a->dw_attr_val.v.val_double.low;
10907 }
10908 else
10909 {
10910 first = a->dw_attr_val.v.val_double.low;
10911 second = a->dw_attr_val.v.val_double.high;
10912 }
10913
10914 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10915 first, "%s", name);
10916 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10917 second, NULL);
10918 }
10919 break;
10920
10921 case dw_val_class_wide_int:
10922 {
10923 int i;
10924 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10925 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10926 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10927 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10928 * l, NULL);
10929
10930 if (WORDS_BIG_ENDIAN)
10931 for (i = len - 1; i >= 0; --i)
10932 {
10933 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10934 "%s", name);
10935 name = "";
10936 }
10937 else
10938 for (i = 0; i < len; ++i)
10939 {
10940 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10941 "%s", name);
10942 name = "";
10943 }
10944 }
10945 break;
10946
10947 case dw_val_class_vec:
10948 {
10949 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10950 unsigned int len = a->dw_attr_val.v.val_vec.length;
10951 unsigned int i;
10952 unsigned char *p;
10953
10954 dw2_asm_output_data (constant_size (len * elt_size),
10955 len * elt_size, "%s", name);
10956 if (elt_size > sizeof (HOST_WIDE_INT))
10957 {
10958 elt_size /= 2;
10959 len *= 2;
10960 }
10961 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10962 i < len;
10963 i++, p += elt_size)
10964 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10965 "fp or vector constant word %u", i);
10966 break;
10967 }
10968
10969 case dw_val_class_flag:
10970 if (dwarf_version >= 4)
10971 {
10972 /* Currently all add_AT_flag calls pass in 1 as last argument,
10973 so DW_FORM_flag_present can be used. If that ever changes,
10974 we'll need to use DW_FORM_flag and have some optimization
10975 in build_abbrev_table that will change those to
10976 DW_FORM_flag_present if it is set to 1 in all DIEs using
10977 the same abbrev entry. */
10978 gcc_assert (AT_flag (a) == 1);
10979 if (flag_debug_asm)
10980 fprintf (asm_out_file, "\t\t\t%s %s\n",
10981 ASM_COMMENT_START, name);
10982 break;
10983 }
10984 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10985 break;
10986
10987 case dw_val_class_loc_list:
10988 output_loc_list_offset (a);
10989 break;
10990
10991 case dw_val_class_view_list:
10992 output_view_list_offset (a);
10993 break;
10994
10995 case dw_val_class_die_ref:
10996 if (AT_ref_external (a))
10997 {
10998 if (AT_ref (a)->comdat_type_p)
10999 {
11000 comdat_type_node *type_node
11001 = AT_ref (a)->die_id.die_type_node;
11002
11003 gcc_assert (type_node);
11004 output_signature (type_node->signature, name);
11005 }
11006 else
11007 {
11008 const char *sym = AT_ref (a)->die_id.die_symbol;
11009 int size;
11010
11011 gcc_assert (sym);
11012 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11013 length, whereas in DWARF3 it's always sized as an
11014 offset. */
11015 if (dwarf_version == 2)
11016 size = DWARF2_ADDR_SIZE;
11017 else
11018 size = dwarf_offset_size;
11019 /* ??? We cannot unconditionally output die_offset if
11020 non-zero - others might create references to those
11021 DIEs via symbols.
11022 And we do not clear its DIE offset after outputting it
11023 (and the label refers to the actual DIEs, not the
11024 DWARF CU unit header which is when using label + offset
11025 would be the correct thing to do).
11026 ??? This is the reason for the with_offset flag. */
11027 if (AT_ref (a)->with_offset)
11028 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
11029 debug_info_section, "%s", name);
11030 else
11031 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11032 name);
11033 }
11034 }
11035 else
11036 {
11037 gcc_assert (AT_ref (a)->die_offset);
11038 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11039 "%s", name);
11040 }
11041 break;
11042
11043 case dw_val_class_fde_ref:
11044 {
11045 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11046
11047 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11048 a->dw_attr_val.v.val_fde_index * 2);
11049 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11050 "%s", name);
11051 }
11052 break;
11053
11054 case dw_val_class_vms_delta:
11055 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11056 dw2_asm_output_vms_delta (dwarf_offset_size,
11057 AT_vms_delta2 (a), AT_vms_delta1 (a),
11058 "%s", name);
11059 #else
11060 dw2_asm_output_delta (dwarf_offset_size,
11061 AT_vms_delta2 (a), AT_vms_delta1 (a),
11062 "%s", name);
11063 #endif
11064 break;
11065
11066 case dw_val_class_lbl_id:
11067 output_attr_index_or_value (a);
11068 break;
11069
11070 case dw_val_class_lineptr:
11071 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11072 debug_line_section, "%s", name);
11073 break;
11074
11075 case dw_val_class_macptr:
11076 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11077 debug_macinfo_section, "%s", name);
11078 break;
11079
11080 case dw_val_class_loclistsptr:
11081 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11082 debug_loc_section, "%s", name);
11083 break;
11084
11085 case dw_val_class_str:
11086 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11087 dw2_asm_output_offset (dwarf_offset_size,
11088 a->dw_attr_val.v.val_str->label,
11089 debug_str_section,
11090 "%s: \"%s\"", name, AT_string (a));
11091 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11092 dw2_asm_output_offset (dwarf_offset_size,
11093 a->dw_attr_val.v.val_str->label,
11094 debug_line_str_section,
11095 "%s: \"%s\"", name, AT_string (a));
11096 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11097 dw2_asm_output_data_uleb128 (AT_index (a),
11098 "%s: \"%s\"", name, AT_string (a));
11099 else
11100 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11101 break;
11102
11103 case dw_val_class_file:
11104 {
11105 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11106
11107 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11108 a->dw_attr_val.v.val_file->filename);
11109 break;
11110 }
11111
11112 case dw_val_class_file_implicit:
11113 if (flag_debug_asm)
11114 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11115 ASM_COMMENT_START, name,
11116 maybe_emit_file (a->dw_attr_val.v.val_file),
11117 a->dw_attr_val.v.val_file->filename);
11118 break;
11119
11120 case dw_val_class_data8:
11121 {
11122 int i;
11123
11124 for (i = 0; i < 8; i++)
11125 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11126 i == 0 ? "%s" : NULL, name);
11127 break;
11128 }
11129
11130 case dw_val_class_high_pc:
11131 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11132 get_AT_low_pc (die), "DW_AT_high_pc");
11133 break;
11134
11135 case dw_val_class_discr_value:
11136 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11137 break;
11138
11139 case dw_val_class_discr_list:
11140 {
11141 dw_discr_list_ref list = AT_discr_list (a);
11142 const int size = size_of_discr_list (list);
11143
11144 /* This is a block, so output its length first. */
11145 dw2_asm_output_data (constant_size (size), size,
11146 "%s: block size", name);
11147
11148 for (; list != NULL; list = list->dw_discr_next)
11149 {
11150 /* One byte for the discriminant value descriptor, and then as
11151 many LEB128 numbers as required. */
11152 if (list->dw_discr_range)
11153 dw2_asm_output_data (1, DW_DSC_range,
11154 "%s: DW_DSC_range", name);
11155 else
11156 dw2_asm_output_data (1, DW_DSC_label,
11157 "%s: DW_DSC_label", name);
11158
11159 output_discr_value (&list->dw_discr_lower_bound, name);
11160 if (list->dw_discr_range)
11161 output_discr_value (&list->dw_discr_upper_bound, name);
11162 }
11163 break;
11164 }
11165
11166 default:
11167 gcc_unreachable ();
11168 }
11169 }
11170
11171 FOR_EACH_CHILD (die, c, output_die (c));
11172
11173 /* Add null byte to terminate sibling list. */
11174 if (die->die_child != NULL)
11175 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11176 (unsigned long) die->die_offset);
11177 }
11178
11179 /* Output the dwarf version number. */
11180
11181 static void
11182 output_dwarf_version ()
11183 {
11184 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11185 views in loclist. That will change eventually. */
11186 if (dwarf_version == 6)
11187 {
11188 static bool once;
11189 if (!once)
11190 {
11191 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11192 "incompatibilities");
11193 once = true;
11194 }
11195 dw2_asm_output_data (2, 5, "DWARF version number");
11196 }
11197 else
11198 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11199 }
11200
11201 /* Output the compilation unit that appears at the beginning of the
11202 .debug_info section, and precedes the DIE descriptions. */
11203
11204 static void
11205 output_compilation_unit_header (enum dwarf_unit_type ut)
11206 {
11207 if (!XCOFF_DEBUGGING_INFO)
11208 {
11209 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11210 dw2_asm_output_data (4, 0xffffffff,
11211 "Initial length escape value indicating 64-bit DWARF extension");
11212 dw2_asm_output_data (dwarf_offset_size,
11213 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11214 "Length of Compilation Unit Info");
11215 }
11216
11217 output_dwarf_version ();
11218 if (dwarf_version >= 5)
11219 {
11220 const char *name;
11221 switch (ut)
11222 {
11223 case DW_UT_compile: name = "DW_UT_compile"; break;
11224 case DW_UT_type: name = "DW_UT_type"; break;
11225 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11226 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11227 default: gcc_unreachable ();
11228 }
11229 dw2_asm_output_data (1, ut, "%s", name);
11230 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11231 }
11232 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11233 debug_abbrev_section,
11234 "Offset Into Abbrev. Section");
11235 if (dwarf_version < 5)
11236 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11237 }
11238
11239 /* Output the compilation unit DIE and its children. */
11240
11241 static void
11242 output_comp_unit (dw_die_ref die, int output_if_empty,
11243 const unsigned char *dwo_id)
11244 {
11245 const char *secname, *oldsym;
11246 char *tmp;
11247
11248 /* Unless we are outputting main CU, we may throw away empty ones. */
11249 if (!output_if_empty && die->die_child == NULL)
11250 return;
11251
11252 /* Even if there are no children of this DIE, we must output the information
11253 about the compilation unit. Otherwise, on an empty translation unit, we
11254 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11255 will then complain when examining the file. First mark all the DIEs in
11256 this CU so we know which get local refs. */
11257 mark_dies (die);
11258
11259 external_ref_hash_type *extern_map = optimize_external_refs (die);
11260
11261 /* For now, optimize only the main CU, in order to optimize the rest
11262 we'd need to see all of them earlier. Leave the rest for post-linking
11263 tools like DWZ. */
11264 if (die == comp_unit_die ())
11265 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11266
11267 build_abbrev_table (die, extern_map);
11268
11269 optimize_abbrev_table ();
11270
11271 delete extern_map;
11272
11273 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11274 next_die_offset = (dwo_id
11275 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11276 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11277 calc_die_sizes (die);
11278
11279 oldsym = die->die_id.die_symbol;
11280 if (oldsym && die->comdat_type_p)
11281 {
11282 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11283
11284 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11285 secname = tmp;
11286 die->die_id.die_symbol = NULL;
11287 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11288 }
11289 else
11290 {
11291 switch_to_section (debug_info_section);
11292 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11293 info_section_emitted = true;
11294 }
11295
11296 /* For LTO cross unit DIE refs we want a symbol on the start of the
11297 debuginfo section, not on the CU DIE. */
11298 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11299 {
11300 /* ??? No way to get visibility assembled without a decl. */
11301 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11302 get_identifier (oldsym), char_type_node);
11303 TREE_PUBLIC (decl) = true;
11304 TREE_STATIC (decl) = true;
11305 DECL_ARTIFICIAL (decl) = true;
11306 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11307 DECL_VISIBILITY_SPECIFIED (decl) = true;
11308 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11309 #ifdef ASM_WEAKEN_LABEL
11310 /* We prefer a .weak because that handles duplicates from duplicate
11311 archive members in a graceful way. */
11312 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11313 #else
11314 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11315 #endif
11316 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11317 }
11318
11319 /* Output debugging information. */
11320 output_compilation_unit_header (dwo_id
11321 ? DW_UT_split_compile : DW_UT_compile);
11322 if (dwarf_version >= 5)
11323 {
11324 if (dwo_id != NULL)
11325 for (int i = 0; i < 8; i++)
11326 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11327 }
11328 output_die (die);
11329
11330 /* Leave the marks on the main CU, so we can check them in
11331 output_pubnames. */
11332 if (oldsym)
11333 {
11334 unmark_dies (die);
11335 die->die_id.die_symbol = oldsym;
11336 }
11337 }
11338
11339 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11340 and .debug_pubtypes. This is configured per-target, but can be
11341 overridden by the -gpubnames or -gno-pubnames options. */
11342
11343 static inline bool
11344 want_pubnames (void)
11345 {
11346 if (debug_info_level <= DINFO_LEVEL_TERSE
11347 /* Names and types go to the early debug part only. */
11348 || in_lto_p)
11349 return false;
11350 if (debug_generate_pub_sections != -1)
11351 return debug_generate_pub_sections;
11352 return targetm.want_debug_pub_sections;
11353 }
11354
11355 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11356
11357 static void
11358 add_AT_pubnames (dw_die_ref die)
11359 {
11360 if (want_pubnames ())
11361 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11362 }
11363
11364 /* Add a string attribute value to a skeleton DIE. */
11365
11366 static inline void
11367 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11368 const char *str)
11369 {
11370 dw_attr_node attr;
11371 struct indirect_string_node *node;
11372
11373 if (! skeleton_debug_str_hash)
11374 skeleton_debug_str_hash
11375 = hash_table<indirect_string_hasher>::create_ggc (10);
11376
11377 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11378 find_string_form (node);
11379 if (node->form == dwarf_FORM (DW_FORM_strx))
11380 node->form = DW_FORM_strp;
11381
11382 attr.dw_attr = attr_kind;
11383 attr.dw_attr_val.val_class = dw_val_class_str;
11384 attr.dw_attr_val.val_entry = NULL;
11385 attr.dw_attr_val.v.val_str = node;
11386 add_dwarf_attr (die, &attr);
11387 }
11388
11389 /* Helper function to generate top-level dies for skeleton debug_info and
11390 debug_types. */
11391
11392 static void
11393 add_top_level_skeleton_die_attrs (dw_die_ref die)
11394 {
11395 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11396 const char *comp_dir = comp_dir_string ();
11397
11398 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11399 if (comp_dir != NULL)
11400 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11401 add_AT_pubnames (die);
11402 if (addr_index_table != NULL && addr_index_table->size () > 0)
11403 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11404 }
11405
11406 /* Output skeleton debug sections that point to the dwo file. */
11407
11408 static void
11409 output_skeleton_debug_sections (dw_die_ref comp_unit,
11410 const unsigned char *dwo_id)
11411 {
11412 /* These attributes will be found in the full debug_info section. */
11413 remove_AT (comp_unit, DW_AT_producer);
11414 remove_AT (comp_unit, DW_AT_language);
11415 remove_AT (comp_unit, DW_AT_language_name);
11416 remove_AT (comp_unit, DW_AT_language_version);
11417
11418 switch_to_section (debug_skeleton_info_section);
11419 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11420
11421 /* Produce the skeleton compilation-unit header. This one differs enough from
11422 a normal CU header that it's better not to call output_compilation_unit
11423 header. */
11424 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11425 dw2_asm_output_data (4, 0xffffffff,
11426 "Initial length escape value indicating 64-bit "
11427 "DWARF extension");
11428
11429 dw2_asm_output_data (dwarf_offset_size,
11430 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11431 - DWARF_INITIAL_LENGTH_SIZE
11432 + size_of_die (comp_unit),
11433 "Length of Compilation Unit Info");
11434 output_dwarf_version ();
11435 if (dwarf_version >= 5)
11436 {
11437 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11438 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11439 }
11440 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11441 debug_skeleton_abbrev_section,
11442 "Offset Into Abbrev. Section");
11443 if (dwarf_version < 5)
11444 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11445 else
11446 for (int i = 0; i < 8; i++)
11447 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11448
11449 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11450 output_die (comp_unit);
11451
11452 /* Build the skeleton debug_abbrev section. */
11453 switch_to_section (debug_skeleton_abbrev_section);
11454 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11455
11456 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11457
11458 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11459 }
11460
11461 /* Output a comdat type unit DIE and its children. */
11462
11463 static void
11464 output_comdat_type_unit (comdat_type_node *node,
11465 bool early_lto_debug ATTRIBUTE_UNUSED)
11466 {
11467 const char *secname;
11468 char *tmp;
11469 int i;
11470 #if defined (OBJECT_FORMAT_ELF)
11471 tree comdat_key;
11472 #endif
11473
11474 /* First mark all the DIEs in this CU so we know which get local refs. */
11475 mark_dies (node->root_die);
11476
11477 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11478
11479 build_abbrev_table (node->root_die, extern_map);
11480
11481 delete extern_map;
11482 extern_map = NULL;
11483
11484 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11485 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11486 calc_die_sizes (node->root_die);
11487
11488 #if defined (OBJECT_FORMAT_ELF)
11489 if (dwarf_version >= 5)
11490 {
11491 if (!dwarf_split_debug_info)
11492 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11493 else
11494 secname = (early_lto_debug
11495 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11496 }
11497 else if (!dwarf_split_debug_info)
11498 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11499 else
11500 secname = (early_lto_debug
11501 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11502
11503 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11504 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11505 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11506 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11507 comdat_key = get_identifier (tmp);
11508 targetm.asm_out.named_section (secname,
11509 SECTION_DEBUG | SECTION_LINKONCE,
11510 comdat_key);
11511 #else
11512 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11513 sprintf (tmp, (dwarf_version >= 5
11514 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11515 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11516 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11517 secname = tmp;
11518 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11519 #endif
11520
11521 /* Output debugging information. */
11522 output_compilation_unit_header (dwarf_split_debug_info
11523 ? DW_UT_split_type : DW_UT_type);
11524 output_signature (node->signature, "Type Signature");
11525 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11526 "Offset to Type DIE");
11527 output_die (node->root_die);
11528
11529 unmark_dies (node->root_die);
11530 }
11531
11532 /* Return the DWARF2/3 pubname associated with a decl. */
11533
11534 static const char *
11535 dwarf2_name (tree decl, int scope)
11536 {
11537 if (DECL_NAMELESS (decl))
11538 return NULL;
11539 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11540 }
11541
11542 /* Add a new entry to .debug_pubnames if appropriate. */
11543
11544 static void
11545 add_pubname_string (const char *str, dw_die_ref die)
11546 {
11547 pubname_entry e;
11548
11549 e.die = die;
11550 e.name = xstrdup (str);
11551 vec_safe_push (pubname_table, e);
11552 }
11553
11554 static void
11555 add_pubname (tree decl, dw_die_ref die)
11556 {
11557 if (!want_pubnames ())
11558 return;
11559
11560 /* Don't add items to the table when we expect that the consumer will have
11561 just read the enclosing die. For example, if the consumer is looking at a
11562 class_member, it will either be inside the class already, or will have just
11563 looked up the class to find the member. Either way, searching the class is
11564 faster than searching the index. */
11565 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11566 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11567 {
11568 const char *name = dwarf2_name (decl, 1);
11569
11570 if (name)
11571 add_pubname_string (name, die);
11572 }
11573 }
11574
11575 /* Add an enumerator to the pubnames section. */
11576
11577 static void
11578 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11579 {
11580 pubname_entry e;
11581
11582 gcc_assert (scope_name);
11583 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11584 e.die = die;
11585 vec_safe_push (pubname_table, e);
11586 }
11587
11588 /* Add a new entry to .debug_pubtypes if appropriate. */
11589
11590 static void
11591 add_pubtype (tree decl, dw_die_ref die)
11592 {
11593 pubname_entry e;
11594
11595 if (!want_pubnames ())
11596 return;
11597
11598 if ((TREE_PUBLIC (decl)
11599 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11600 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11601 {
11602 tree scope = NULL;
11603 const char *scope_name = "";
11604 const char *sep = is_cxx () ? "::" : ".";
11605 const char *name;
11606
11607 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11608 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11609 {
11610 scope_name = lang_hooks.dwarf_name (scope, 1);
11611 if (scope_name != NULL && scope_name[0] != '\0')
11612 scope_name = concat (scope_name, sep, NULL);
11613 else
11614 scope_name = "";
11615 }
11616
11617 if (TYPE_P (decl))
11618 name = type_tag (decl);
11619 else
11620 name = lang_hooks.dwarf_name (decl, 1);
11621
11622 /* If we don't have a name for the type, there's no point in adding
11623 it to the table. */
11624 if (name != NULL && name[0] != '\0')
11625 {
11626 e.die = die;
11627 e.name = concat (scope_name, name, NULL);
11628 vec_safe_push (pubtype_table, e);
11629 }
11630
11631 /* Although it might be more consistent to add the pubinfo for the
11632 enumerators as their dies are created, they should only be added if the
11633 enum type meets the criteria above. So rather than re-check the parent
11634 enum type whenever an enumerator die is created, just output them all
11635 here. This isn't protected by the name conditional because anonymous
11636 enums don't have names. */
11637 if (die->die_tag == DW_TAG_enumeration_type)
11638 {
11639 dw_die_ref c;
11640
11641 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11642 }
11643 }
11644 }
11645
11646 /* Output a single entry in the pubnames table. */
11647
11648 static void
11649 output_pubname (dw_offset die_offset, pubname_entry *entry)
11650 {
11651 dw_die_ref die = entry->die;
11652 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11653
11654 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11655
11656 if (debug_generate_pub_sections == 2)
11657 {
11658 /* This logic follows gdb's method for determining the value of the flag
11659 byte. */
11660 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11661 switch (die->die_tag)
11662 {
11663 case DW_TAG_typedef:
11664 case DW_TAG_base_type:
11665 case DW_TAG_subrange_type:
11666 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11667 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11668 break;
11669 case DW_TAG_enumerator:
11670 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11671 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11672 if (!is_cxx ())
11673 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11674 break;
11675 case DW_TAG_subprogram:
11676 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11677 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11678 if (!is_ada ())
11679 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11680 break;
11681 case DW_TAG_constant:
11682 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11683 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11684 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11685 break;
11686 case DW_TAG_variable:
11687 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11688 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11689 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11690 break;
11691 case DW_TAG_namespace:
11692 case DW_TAG_imported_declaration:
11693 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11694 break;
11695 case DW_TAG_class_type:
11696 case DW_TAG_interface_type:
11697 case DW_TAG_structure_type:
11698 case DW_TAG_union_type:
11699 case DW_TAG_enumeration_type:
11700 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11701 if (!is_cxx ())
11702 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11703 break;
11704 default:
11705 /* An unusual tag. Leave the flag-byte empty. */
11706 break;
11707 }
11708 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11709 "GDB-index flags");
11710 }
11711
11712 dw2_asm_output_nstring (entry->name, -1, "external name");
11713 }
11714
11715
11716 /* Output the public names table used to speed up access to externally
11717 visible names; or the public types table used to find type definitions. */
11718
11719 static void
11720 output_pubnames (vec<pubname_entry, va_gc> *names)
11721 {
11722 unsigned i;
11723 unsigned long pubnames_length = size_of_pubnames (names);
11724 pubname_entry *pub;
11725
11726 if (!XCOFF_DEBUGGING_INFO)
11727 {
11728 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11729 dw2_asm_output_data (4, 0xffffffff,
11730 "Initial length escape value indicating 64-bit DWARF extension");
11731 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11732 "Pub Info Length");
11733 }
11734
11735 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11736 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11737
11738 if (dwarf_split_debug_info)
11739 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11740 debug_skeleton_info_section,
11741 "Offset of Compilation Unit Info");
11742 else
11743 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11744 debug_info_section,
11745 "Offset of Compilation Unit Info");
11746 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11747 "Compilation Unit Length");
11748
11749 FOR_EACH_VEC_ELT (*names, i, pub)
11750 {
11751 if (include_pubname_in_output (names, pub))
11752 {
11753 dw_offset die_offset = pub->die->die_offset;
11754
11755 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11756 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11757 gcc_assert (pub->die->die_mark);
11758
11759 /* If we're putting types in their own .debug_types sections,
11760 the .debug_pubtypes table will still point to the compile
11761 unit (not the type unit), so we want to use the offset of
11762 the skeleton DIE (if there is one). */
11763 if (pub->die->comdat_type_p && names == pubtype_table)
11764 {
11765 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11766
11767 if (type_node != NULL)
11768 die_offset = (type_node->skeleton_die != NULL
11769 ? type_node->skeleton_die->die_offset
11770 : comp_unit_die ()->die_offset);
11771 }
11772
11773 output_pubname (die_offset, pub);
11774 }
11775 }
11776
11777 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11778 }
11779
11780 /* Output public names and types tables if necessary. */
11781
11782 static void
11783 output_pubtables (void)
11784 {
11785 if (!want_pubnames () || !info_section_emitted)
11786 return;
11787
11788 switch_to_section (debug_pubnames_section);
11789 output_pubnames (pubname_table);
11790 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11791 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11792 simply won't look for the section. */
11793 switch_to_section (debug_pubtypes_section);
11794 output_pubnames (pubtype_table);
11795 }
11796
11797
11798 /* Output the information that goes into the .debug_aranges table.
11799 Namely, define the beginning and ending address range of the
11800 text section generated for this compilation unit. */
11801
11802 static void
11803 output_aranges (void)
11804 {
11805 unsigned i;
11806 unsigned long aranges_length = size_of_aranges ();
11807
11808 if (!XCOFF_DEBUGGING_INFO)
11809 {
11810 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11811 dw2_asm_output_data (4, 0xffffffff,
11812 "Initial length escape value indicating 64-bit DWARF extension");
11813 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11814 "Length of Address Ranges Info");
11815 }
11816
11817 /* Version number for aranges is still 2, even up to DWARF5. */
11818 dw2_asm_output_data (2, 2, "DWARF aranges version");
11819 if (dwarf_split_debug_info)
11820 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11821 debug_skeleton_info_section,
11822 "Offset of Compilation Unit Info");
11823 else
11824 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11825 debug_info_section,
11826 "Offset of Compilation Unit Info");
11827 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11828 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11829
11830 /* We need to align to twice the pointer size here. */
11831 if (DWARF_ARANGES_PAD_SIZE)
11832 {
11833 /* Pad using a 2 byte words so that padding is correct for any
11834 pointer size. */
11835 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11836 2 * DWARF2_ADDR_SIZE);
11837 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11838 dw2_asm_output_data (2, 0, NULL);
11839 }
11840
11841 /* It is necessary not to output these entries if the sections were
11842 not used; if the sections were not used, the length will be 0 and
11843 the address may end up as 0 if the section is discarded by ld
11844 --gc-sections, leaving an invalid (0, 0) entry that can be
11845 confused with the terminator. */
11846 if (switch_text_ranges)
11847 {
11848 const char *prev_loc = text_section_label;
11849 const char *loc;
11850 unsigned idx;
11851
11852 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11853 if (prev_loc)
11854 {
11855 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11856 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11857 prev_loc = NULL;
11858 }
11859 else
11860 prev_loc = loc;
11861
11862 if (prev_loc)
11863 {
11864 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11865 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11866 prev_loc, "Length");
11867 }
11868 }
11869
11870 if (switch_cold_ranges)
11871 {
11872 const char *prev_loc = cold_text_section_label;
11873 const char *loc;
11874 unsigned idx;
11875
11876 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11877 if (prev_loc)
11878 {
11879 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11880 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11881 prev_loc = NULL;
11882 }
11883 else
11884 prev_loc = loc;
11885
11886 if (prev_loc)
11887 {
11888 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11889 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11890 prev_loc, "Length");
11891 }
11892 }
11893
11894 if (have_multiple_function_sections)
11895 {
11896 unsigned fde_idx;
11897 dw_fde_ref fde;
11898
11899 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11900 {
11901 if (fde->ignored_debug)
11902 continue;
11903 if (!fde->in_std_section)
11904 {
11905 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11906 "Address");
11907 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11908 fde->dw_fde_begin, "Length");
11909 }
11910 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11911 {
11912 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11913 "Address");
11914 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11915 fde->dw_fde_second_begin, "Length");
11916 }
11917 }
11918 }
11919
11920 /* Output the terminator words. */
11921 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11922 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11923 }
11924
11925 /* Add a new entry to .debug_ranges. Return its index into
11926 ranges_table vector. */
11927
11928 static unsigned int
11929 add_ranges_num (int num, bool maybe_new_sec)
11930 {
11931 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11932 vec_safe_push (ranges_table, r);
11933 return vec_safe_length (ranges_table) - 1;
11934 }
11935
11936 /* Add a new entry to .debug_ranges corresponding to a block, or a
11937 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11938 this entry might be in a different section from previous range. */
11939
11940 static unsigned int
11941 add_ranges (const_tree block, bool maybe_new_sec)
11942 {
11943 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11944 }
11945
11946 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11947 chain, or middle entry of a chain that will be directly referred to. */
11948
11949 static void
11950 note_rnglist_head (unsigned int offset)
11951 {
11952 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11953 return;
11954 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11955 }
11956
11957 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11958 When using dwarf_split_debug_info, address attributes in dies destined
11959 for the final executable should be direct references--setting the
11960 parameter force_direct ensures this behavior. */
11961
11962 static void
11963 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11964 bool *added, bool force_direct)
11965 {
11966 unsigned int in_use = vec_safe_length (ranges_by_label);
11967 unsigned int offset;
11968 dw_ranges_by_label rbl = { begin, end };
11969 vec_safe_push (ranges_by_label, rbl);
11970 offset = add_ranges_num (-(int)in_use - 1, true);
11971 if (!*added)
11972 {
11973 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11974 *added = true;
11975 note_rnglist_head (offset);
11976 if (dwarf_split_debug_info && force_direct)
11977 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11978 }
11979 }
11980
11981 /* Emit .debug_ranges section. */
11982
11983 static void
11984 output_ranges (void)
11985 {
11986 unsigned i;
11987 static const char *const start_fmt = "Offset %#x";
11988 const char *fmt = start_fmt;
11989 dw_ranges *r;
11990
11991 switch_to_section (debug_ranges_section);
11992 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11993 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11994 {
11995 int block_num = r->num;
11996
11997 if (block_num > 0)
11998 {
11999 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12000 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12001
12002 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12003 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12004
12005 /* If all code is in the text section, then the compilation
12006 unit base address defaults to DW_AT_low_pc, which is the
12007 base of the text section. */
12008 if (!have_multiple_function_sections)
12009 {
12010 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12011 text_section_label,
12012 fmt, i * 2 * DWARF2_ADDR_SIZE);
12013 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12014 text_section_label, NULL);
12015 }
12016
12017 /* Otherwise, the compilation unit base address is zero,
12018 which allows us to use absolute addresses, and not worry
12019 about whether the target supports cross-section
12020 arithmetic. */
12021 else
12022 {
12023 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12024 fmt, i * 2 * DWARF2_ADDR_SIZE);
12025 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12026 }
12027
12028 fmt = NULL;
12029 }
12030
12031 /* Negative block_num stands for an index into ranges_by_label. */
12032 else if (block_num < 0)
12033 {
12034 int lab_idx = - block_num - 1;
12035
12036 if (!have_multiple_function_sections)
12037 {
12038 gcc_unreachable ();
12039 #if 0
12040 /* If we ever use add_ranges_by_labels () for a single
12041 function section, all we have to do is to take out
12042 the #if 0 above. */
12043 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12044 (*ranges_by_label)[lab_idx].begin,
12045 text_section_label,
12046 fmt, i * 2 * DWARF2_ADDR_SIZE);
12047 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12048 (*ranges_by_label)[lab_idx].end,
12049 text_section_label, NULL);
12050 #endif
12051 }
12052 else
12053 {
12054 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12055 (*ranges_by_label)[lab_idx].begin,
12056 fmt, i * 2 * DWARF2_ADDR_SIZE);
12057 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12058 (*ranges_by_label)[lab_idx].end,
12059 NULL);
12060 }
12061 }
12062 else
12063 {
12064 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12065 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12066 fmt = start_fmt;
12067 }
12068 }
12069 }
12070
12071 /* Non-zero if .debug_line_str should be used for .debug_line section
12072 strings or strings that are likely shareable with those. */
12073 #define DWARF5_USE_DEBUG_LINE_STR \
12074 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12075 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12076 /* FIXME: there is no .debug_line_str.dwo section, \
12077 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12078 && !dwarf_split_debug_info)
12079
12080
12081 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12082 DWARF5 .debug_line tables using .debug_line_str or we generate
12083 it ourselves, except for split-dwarf which doesn't have a
12084 .debug_line_str. */
12085 static bool
12086 asm_outputs_debug_line_str (void)
12087 {
12088 if (dwarf_version >= 5
12089 && ! output_asm_line_debug_info ()
12090 && DWARF5_USE_DEBUG_LINE_STR)
12091 return true;
12092 else
12093 {
12094 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12095 return !dwarf_split_debug_info && dwarf_version >= 5;
12096 #else
12097 return false;
12098 #endif
12099 }
12100 }
12101
12102 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12103 I is index of the following range. */
12104
12105 static bool
12106 use_distinct_base_address_for_range (unsigned int i)
12107 {
12108 if (i >= vec_safe_length (ranges_table))
12109 return false;
12110
12111 dw_ranges *r2 = &(*ranges_table)[i];
12112 /* Use DW_RLE_base_address{,x} if there is a next range in the
12113 range list and is guaranteed to be in the same section. */
12114 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12115 }
12116
12117 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12118 section when needed. */
12119
12120 static void
12121 index_rnglists (void)
12122 {
12123 unsigned i;
12124 dw_ranges *r;
12125 bool base = false;
12126
12127 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12128 {
12129 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12130 r->idx = rnglist_idx++;
12131
12132 int block_num = r->num;
12133 if ((HAVE_AS_LEB128 || block_num < 0)
12134 && !have_multiple_function_sections)
12135 continue;
12136 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12137 base = false;
12138 if (block_num > 0)
12139 {
12140 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12141 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12142
12143 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12144 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12145
12146 if (HAVE_AS_LEB128)
12147 {
12148 if (!base && use_distinct_base_address_for_range (i + 1))
12149 {
12150 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12151 ate_kind_label);
12152 base = true;
12153 }
12154 if (base)
12155 /* If we have a base, no need for further
12156 begin_entry/end_entry, as DW_RLE_offset_pair will be
12157 used. */
12158 continue;
12159 r->begin_entry
12160 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12161 /* No need for end_entry, DW_RLE_start{,x}_length will use
12162 length as opposed to a pair of addresses. */
12163 }
12164 else
12165 {
12166 r->begin_entry
12167 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12168 r->end_entry
12169 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12170 }
12171 }
12172
12173 /* Negative block_num stands for an index into ranges_by_label. */
12174 else if (block_num < 0)
12175 {
12176 int lab_idx = - block_num - 1;
12177 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12178 const char *elabel = (*ranges_by_label)[lab_idx].end;
12179
12180 r->begin_entry
12181 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12182 if (!HAVE_AS_LEB128)
12183 r->end_entry
12184 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12185 }
12186 }
12187 }
12188
12189 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12190
12191 static bool
12192 output_rnglists (unsigned generation, bool dwo)
12193 {
12194 unsigned i;
12195 dw_ranges *r;
12196 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12197 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12198 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12199
12200 if (dwo)
12201 switch_to_section (debug_ranges_dwo_section);
12202 else
12203 {
12204 switch_to_section (debug_ranges_section);
12205 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12206 }
12207 /* There are up to 4 unique ranges labels per generation.
12208 See also init_sections_and_labels. */
12209 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12210 2 + 2 * dwo + generation * 6);
12211 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12212 3 + 2 * dwo + generation * 6);
12213 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12214 dw2_asm_output_data (4, 0xffffffff,
12215 "Initial length escape value indicating "
12216 "64-bit DWARF extension");
12217 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12218 "Length of Range Lists");
12219 ASM_OUTPUT_LABEL (asm_out_file, l1);
12220 output_dwarf_version ();
12221 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12222 dw2_asm_output_data (1, 0, "Segment Size");
12223 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12224 about relocation sizes and primarily care about the size of .debug*
12225 sections in linked shared libraries and executables, then
12226 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12227 into it are usually larger than just DW_FORM_sec_offset offsets
12228 into the .debug_rnglists section. */
12229 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12230 "Offset Entry Count");
12231 if (dwo)
12232 {
12233 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12234 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12235 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12236 dw2_asm_output_delta (dwarf_offset_size, r->label,
12237 ranges_base_label, NULL);
12238 }
12239
12240 const char *lab = "";
12241 const char *base = NULL;
12242 bool skipping = false;
12243 bool ret = false;
12244 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12245 {
12246 int block_num = r->num;
12247
12248 if (r->label)
12249 {
12250 if (dwarf_split_debug_info
12251 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12252 {
12253 ret = true;
12254 skipping = true;
12255 continue;
12256 }
12257 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12258 lab = r->label;
12259 }
12260 if (skipping)
12261 {
12262 if (block_num == 0)
12263 skipping = false;
12264 continue;
12265 }
12266 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12267 base = NULL;
12268 if (block_num > 0)
12269 {
12270 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12271 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12272
12273 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12274 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12275
12276 if (HAVE_AS_LEB128)
12277 {
12278 /* If all code is in the text section, then the compilation
12279 unit base address defaults to DW_AT_low_pc, which is the
12280 base of the text section. */
12281 if (!have_multiple_function_sections)
12282 {
12283 dw2_asm_output_data (1, DW_RLE_offset_pair,
12284 "DW_RLE_offset_pair (%s)", lab);
12285 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12286 "Range begin address (%s)", lab);
12287 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12288 "Range end address (%s)", lab);
12289 continue;
12290 }
12291 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12292 {
12293 if (dwarf_split_debug_info)
12294 {
12295 dw2_asm_output_data (1, DW_RLE_base_addressx,
12296 "DW_RLE_base_addressx (%s)", lab);
12297 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12298 "Base address index (%s)",
12299 blabel);
12300 }
12301 else
12302 {
12303 dw2_asm_output_data (1, DW_RLE_base_address,
12304 "DW_RLE_base_address (%s)", lab);
12305 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12306 "Base address (%s)", lab);
12307 }
12308 strcpy (basebuf, blabel);
12309 base = basebuf;
12310 }
12311 if (base)
12312 {
12313 dw2_asm_output_data (1, DW_RLE_offset_pair,
12314 "DW_RLE_offset_pair (%s)", lab);
12315 dw2_asm_output_delta_uleb128 (blabel, base,
12316 "Range begin address (%s)", lab);
12317 dw2_asm_output_delta_uleb128 (elabel, base,
12318 "Range end address (%s)", lab);
12319 continue;
12320 }
12321 if (dwarf_split_debug_info)
12322 {
12323 dw2_asm_output_data (1, DW_RLE_startx_length,
12324 "DW_RLE_startx_length (%s)", lab);
12325 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12326 "Range begin address index "
12327 "(%s)", blabel);
12328 }
12329 else
12330 {
12331 dw2_asm_output_data (1, DW_RLE_start_length,
12332 "DW_RLE_start_length (%s)", lab);
12333 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12334 "Range begin address (%s)", lab);
12335 }
12336 dw2_asm_output_delta_uleb128 (elabel, blabel,
12337 "Range length (%s)", lab);
12338 }
12339 else if (dwarf_split_debug_info)
12340 {
12341 dw2_asm_output_data (1, DW_RLE_startx_endx,
12342 "DW_RLE_startx_endx (%s)", lab);
12343 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12344 "Range begin address index "
12345 "(%s)", blabel);
12346 dw2_asm_output_data_uleb128 (r->end_entry->index,
12347 "Range end address index "
12348 "(%s)", elabel);
12349 }
12350 else
12351 {
12352 dw2_asm_output_data (1, DW_RLE_start_end,
12353 "DW_RLE_start_end (%s)", lab);
12354 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12355 "Range begin address (%s)", lab);
12356 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12357 "Range end address (%s)", lab);
12358 }
12359 }
12360
12361 /* Negative block_num stands for an index into ranges_by_label. */
12362 else if (block_num < 0)
12363 {
12364 int lab_idx = - block_num - 1;
12365 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12366 const char *elabel = (*ranges_by_label)[lab_idx].end;
12367
12368 if (!have_multiple_function_sections)
12369 gcc_unreachable ();
12370 if (HAVE_AS_LEB128)
12371 {
12372 if (dwarf_split_debug_info)
12373 {
12374 dw2_asm_output_data (1, DW_RLE_startx_length,
12375 "DW_RLE_startx_length (%s)", lab);
12376 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12377 "Range begin address index "
12378 "(%s)", blabel);
12379 }
12380 else
12381 {
12382 dw2_asm_output_data (1, DW_RLE_start_length,
12383 "DW_RLE_start_length (%s)", lab);
12384 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12385 "Range begin address (%s)", lab);
12386 }
12387 dw2_asm_output_delta_uleb128 (elabel, blabel,
12388 "Range length (%s)", lab);
12389 }
12390 else if (dwarf_split_debug_info)
12391 {
12392 dw2_asm_output_data (1, DW_RLE_startx_endx,
12393 "DW_RLE_startx_endx (%s)", lab);
12394 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12395 "Range begin address index "
12396 "(%s)", blabel);
12397 dw2_asm_output_data_uleb128 (r->end_entry->index,
12398 "Range end address index "
12399 "(%s)", elabel);
12400 }
12401 else
12402 {
12403 dw2_asm_output_data (1, DW_RLE_start_end,
12404 "DW_RLE_start_end (%s)", lab);
12405 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12406 "Range begin address (%s)", lab);
12407 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12408 "Range end address (%s)", lab);
12409 }
12410 }
12411 else
12412 dw2_asm_output_data (1, DW_RLE_end_of_list,
12413 "DW_RLE_end_of_list (%s)", lab);
12414 }
12415 ASM_OUTPUT_LABEL (asm_out_file, l2);
12416 return ret;
12417 }
12418
12419 /* Data structure containing information about input files. */
12420 struct file_info
12421 {
12422 const char *path; /* Complete file name. */
12423 const char *fname; /* File name part. */
12424 int length; /* Length of entire string. */
12425 struct dwarf_file_data * file_idx; /* Index in input file table. */
12426 int dir_idx; /* Index in directory table. */
12427 };
12428
12429 /* Data structure containing information about directories with source
12430 files. */
12431 struct dir_info
12432 {
12433 const char *path; /* Path including directory name. */
12434 int length; /* Path length. */
12435 int prefix; /* Index of directory entry which is a prefix. */
12436 int count; /* Number of files in this directory. */
12437 int dir_idx; /* Index of directory used as base. */
12438 };
12439
12440 /* Callback function for file_info comparison. We sort by looking at
12441 the directories in the path. */
12442
12443 static int
12444 file_info_cmp (const void *p1, const void *p2)
12445 {
12446 const struct file_info *const s1 = (const struct file_info *) p1;
12447 const struct file_info *const s2 = (const struct file_info *) p2;
12448 const unsigned char *cp1;
12449 const unsigned char *cp2;
12450
12451 /* Take care of file names without directories. We need to make sure that
12452 we return consistent values to qsort since some will get confused if
12453 we return the same value when identical operands are passed in opposite
12454 orders. So if neither has a directory, return 0 and otherwise return
12455 1 or -1 depending on which one has the directory. We want the one with
12456 the directory to sort after the one without, so all no directory files
12457 are at the start (normally only the compilation unit file). */
12458 if ((s1->path == s1->fname || s2->path == s2->fname))
12459 return (s2->path == s2->fname) - (s1->path == s1->fname);
12460
12461 cp1 = (const unsigned char *) s1->path;
12462 cp2 = (const unsigned char *) s2->path;
12463
12464 while (1)
12465 {
12466 ++cp1;
12467 ++cp2;
12468 /* Reached the end of the first path? If so, handle like above,
12469 but now we want longer directory prefixes before shorter ones. */
12470 if ((cp1 == (const unsigned char *) s1->fname)
12471 || (cp2 == (const unsigned char *) s2->fname))
12472 return ((cp1 == (const unsigned char *) s1->fname)
12473 - (cp2 == (const unsigned char *) s2->fname));
12474
12475 /* Character of current path component the same? */
12476 else if (*cp1 != *cp2)
12477 return *cp1 - *cp2;
12478 }
12479 }
12480
12481 struct file_name_acquire_data
12482 {
12483 struct file_info *files;
12484 int used_files;
12485 int max_files;
12486 };
12487
12488 /* Traversal function for the hash table. */
12489
12490 int
12491 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12492 {
12493 struct dwarf_file_data *d = *slot;
12494 struct file_info *fi;
12495 const char *f;
12496
12497 gcc_assert (fnad->max_files >= d->emitted_number);
12498
12499 if (! d->emitted_number)
12500 return 1;
12501
12502 gcc_assert (fnad->max_files != fnad->used_files);
12503
12504 fi = fnad->files + fnad->used_files++;
12505
12506 f = d->filename;
12507
12508 /* Skip all leading "./". */
12509 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12510 f += 2;
12511
12512 /* Create a new array entry. */
12513 fi->path = f;
12514 fi->length = strlen (f);
12515 fi->file_idx = d;
12516
12517 /* Search for the file name part. */
12518 f = strrchr (f, DIR_SEPARATOR);
12519 #if defined (DIR_SEPARATOR_2)
12520 {
12521 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12522
12523 if (g != NULL)
12524 {
12525 if (f == NULL || f < g)
12526 f = g;
12527 }
12528 }
12529 #endif
12530
12531 fi->fname = f == NULL ? fi->path : f + 1;
12532 return 1;
12533 }
12534
12535 /* Helper function for output_file_names. Emit a FORM encoded
12536 string STR, with assembly comment start ENTRY_KIND and
12537 index IDX */
12538
12539 static void
12540 output_line_string (enum dwarf_form form, const char *str,
12541 const char *entry_kind, unsigned int idx)
12542 {
12543 switch (form)
12544 {
12545 case DW_FORM_string:
12546 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12547 break;
12548 case DW_FORM_line_strp:
12549 if (!debug_line_str_hash)
12550 debug_line_str_hash
12551 = hash_table<indirect_string_hasher>::create_ggc (10);
12552
12553 struct indirect_string_node *node;
12554 node = find_AT_string_in_table (str, debug_line_str_hash);
12555 set_indirect_string (node);
12556 node->form = form;
12557 dw2_asm_output_offset (dwarf_offset_size, node->label,
12558 debug_line_str_section, "%s: %#x: \"%s\"",
12559 entry_kind, 0, node->str);
12560 break;
12561 default:
12562 gcc_unreachable ();
12563 }
12564 }
12565
12566 /* Output the directory table and the file name table. We try to minimize
12567 the total amount of memory needed. A heuristic is used to avoid large
12568 slowdowns with many input files. */
12569
12570 static void
12571 output_file_names (void)
12572 {
12573 struct file_name_acquire_data fnad;
12574 int numfiles;
12575 struct file_info *files;
12576 struct dir_info *dirs;
12577 int *saved;
12578 int *savehere;
12579 int *backmap;
12580 int ndirs;
12581 int idx_offset;
12582 int i;
12583
12584 if (!last_emitted_file)
12585 {
12586 if (dwarf_version >= 5)
12587 {
12588 const char *comp_dir = comp_dir_string ();
12589 if (comp_dir == NULL)
12590 comp_dir = "";
12591 dw2_asm_output_data (1, 1, "Directory entry format count");
12592 enum dwarf_form str_form = DW_FORM_string;
12593 if (DWARF5_USE_DEBUG_LINE_STR)
12594 str_form = DW_FORM_line_strp;
12595 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12596 dw2_asm_output_data_uleb128 (str_form, "%s",
12597 get_DW_FORM_name (str_form));
12598 dw2_asm_output_data_uleb128 (1, "Directories count");
12599 if (str_form == DW_FORM_string)
12600 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12601 else
12602 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12603 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12604 if (filename0 == NULL)
12605 filename0 = "";
12606 #ifdef VMS_DEBUGGING_INFO
12607 dw2_asm_output_data (1, 4, "File name entry format count");
12608 #else
12609 dw2_asm_output_data (1, 2, "File name entry format count");
12610 #endif
12611 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12612 dw2_asm_output_data_uleb128 (str_form, "%s",
12613 get_DW_FORM_name (str_form));
12614 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12615 "DW_LNCT_directory_index");
12616 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12617 get_DW_FORM_name (DW_FORM_data1));
12618 #ifdef VMS_DEBUGGING_INFO
12619 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12620 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12621 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12622 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12623 #endif
12624 dw2_asm_output_data_uleb128 (1, "File names count");
12625
12626 output_line_string (str_form, filename0, "File Entry", 0);
12627 dw2_asm_output_data (1, 0, NULL);
12628 #ifdef VMS_DEBUGGING_INFO
12629 dw2_asm_output_data_uleb128 (0, NULL);
12630 dw2_asm_output_data_uleb128 (0, NULL);
12631 #endif
12632 }
12633 else
12634 {
12635 dw2_asm_output_data (1, 0, "End directory table");
12636 dw2_asm_output_data (1, 0, "End file name table");
12637 }
12638 return;
12639 }
12640
12641 numfiles = last_emitted_file->emitted_number;
12642
12643 /* Allocate the various arrays we need. */
12644 files = XALLOCAVEC (struct file_info, numfiles);
12645 dirs = XALLOCAVEC (struct dir_info, numfiles);
12646
12647 fnad.files = files;
12648 fnad.used_files = 0;
12649 fnad.max_files = numfiles;
12650 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12651 gcc_assert (fnad.used_files == fnad.max_files);
12652
12653 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12654
12655 /* Find all the different directories used. */
12656 dirs[0].path = files[0].path;
12657 dirs[0].length = files[0].fname - files[0].path;
12658 dirs[0].prefix = -1;
12659 dirs[0].count = 1;
12660 dirs[0].dir_idx = 0;
12661 files[0].dir_idx = 0;
12662 ndirs = 1;
12663
12664 for (i = 1; i < numfiles; i++)
12665 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12666 && memcmp (dirs[ndirs - 1].path, files[i].path,
12667 dirs[ndirs - 1].length) == 0)
12668 {
12669 /* Same directory as last entry. */
12670 files[i].dir_idx = ndirs - 1;
12671 ++dirs[ndirs - 1].count;
12672 }
12673 else
12674 {
12675 int j;
12676
12677 /* This is a new directory. */
12678 dirs[ndirs].path = files[i].path;
12679 dirs[ndirs].length = files[i].fname - files[i].path;
12680 dirs[ndirs].count = 1;
12681 dirs[ndirs].dir_idx = ndirs;
12682 files[i].dir_idx = ndirs;
12683
12684 /* Search for a prefix. */
12685 dirs[ndirs].prefix = -1;
12686 for (j = 0; j < ndirs; j++)
12687 if (dirs[j].length < dirs[ndirs].length
12688 && dirs[j].length > 1
12689 && (dirs[ndirs].prefix == -1
12690 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12691 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12692 dirs[ndirs].prefix = j;
12693
12694 ++ndirs;
12695 }
12696
12697 /* Now to the actual work. We have to find a subset of the directories which
12698 allow expressing the file name using references to the directory table
12699 with the least amount of characters. We do not do an exhaustive search
12700 where we would have to check out every combination of every single
12701 possible prefix. Instead we use a heuristic which provides nearly optimal
12702 results in most cases and never is much off. */
12703 saved = XALLOCAVEC (int, ndirs);
12704 savehere = XALLOCAVEC (int, ndirs);
12705
12706 memset (saved, '\0', ndirs * sizeof (saved[0]));
12707 for (i = 0; i < ndirs; i++)
12708 {
12709 int j;
12710 int total;
12711
12712 /* We can always save some space for the current directory. But this
12713 does not mean it will be enough to justify adding the directory. */
12714 savehere[i] = dirs[i].length;
12715 total = (savehere[i] - saved[i]) * dirs[i].count;
12716
12717 for (j = i + 1; j < ndirs; j++)
12718 {
12719 savehere[j] = 0;
12720 if (saved[j] < dirs[i].length)
12721 {
12722 /* Determine whether the dirs[i] path is a prefix of the
12723 dirs[j] path. */
12724 int k;
12725
12726 k = dirs[j].prefix;
12727 while (k != -1 && k != (int) i)
12728 k = dirs[k].prefix;
12729
12730 if (k == (int) i)
12731 {
12732 /* Yes it is. We can possibly save some memory by
12733 writing the filenames in dirs[j] relative to
12734 dirs[i]. */
12735 savehere[j] = dirs[i].length;
12736 total += (savehere[j] - saved[j]) * dirs[j].count;
12737 }
12738 }
12739 }
12740
12741 /* Check whether we can save enough to justify adding the dirs[i]
12742 directory. */
12743 if (total > dirs[i].length + 1)
12744 {
12745 /* It's worthwhile adding. */
12746 for (j = i; j < ndirs; j++)
12747 if (savehere[j] > 0)
12748 {
12749 /* Remember how much we saved for this directory so far. */
12750 saved[j] = savehere[j];
12751
12752 /* Remember the prefix directory. */
12753 dirs[j].dir_idx = i;
12754 }
12755 }
12756 }
12757
12758 /* Emit the directory name table. */
12759 idx_offset = dirs[0].length > 0 ? 1 : 0;
12760 enum dwarf_form str_form = DW_FORM_string;
12761 enum dwarf_form idx_form = DW_FORM_udata;
12762 if (dwarf_version >= 5)
12763 {
12764 const char *comp_dir = comp_dir_string ();
12765 if (comp_dir == NULL)
12766 comp_dir = "";
12767 dw2_asm_output_data (1, 1, "Directory entry format count");
12768 if (DWARF5_USE_DEBUG_LINE_STR)
12769 str_form = DW_FORM_line_strp;
12770 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12771 dw2_asm_output_data_uleb128 (str_form, "%s",
12772 get_DW_FORM_name (str_form));
12773 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12774 if (str_form == DW_FORM_string)
12775 {
12776 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12777 for (i = 1 - idx_offset; i < ndirs; i++)
12778 dw2_asm_output_nstring (dirs[i].path,
12779 dirs[i].length
12780 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12781 "Directory Entry: %#x", i + idx_offset);
12782 }
12783 else
12784 {
12785 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12786 for (i = 1 - idx_offset; i < ndirs; i++)
12787 {
12788 const char *str
12789 = ggc_alloc_string (dirs[i].path,
12790 dirs[i].length
12791 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12792 output_line_string (str_form, str, "Directory Entry",
12793 (unsigned) i + idx_offset);
12794 }
12795 }
12796 }
12797 else
12798 {
12799 for (i = 1 - idx_offset; i < ndirs; i++)
12800 dw2_asm_output_nstring (dirs[i].path,
12801 dirs[i].length
12802 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12803 "Directory Entry: %#x", i + idx_offset);
12804
12805 dw2_asm_output_data (1, 0, "End directory table");
12806 }
12807
12808 /* We have to emit them in the order of emitted_number since that's
12809 used in the debug info generation. To do this efficiently we
12810 generate a back-mapping of the indices first. */
12811 backmap = XALLOCAVEC (int, numfiles);
12812 for (i = 0; i < numfiles; i++)
12813 backmap[files[i].file_idx->emitted_number - 1] = i;
12814
12815 if (dwarf_version >= 5)
12816 {
12817 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12818 if (filename0 == NULL)
12819 filename0 = "";
12820 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12821 DW_FORM_data2. Choose one based on the number of directories
12822 and how much space would they occupy in each encoding.
12823 If we have at most 256 directories, all indexes fit into
12824 a single byte, so DW_FORM_data1 is most compact (if there
12825 are at most 128 directories, DW_FORM_udata would be as
12826 compact as that, but not shorter and slower to decode). */
12827 if (ndirs + idx_offset <= 256)
12828 idx_form = DW_FORM_data1;
12829 /* If there are more than 65536 directories, we have to use
12830 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12831 Otherwise, compute what space would occupy if all the indexes
12832 used DW_FORM_udata - sum - and compare that to how large would
12833 be DW_FORM_data2 encoding, and pick the more efficient one. */
12834 else if (ndirs + idx_offset <= 65536)
12835 {
12836 unsigned HOST_WIDE_INT sum = 1;
12837 for (i = 0; i < numfiles; i++)
12838 {
12839 int file_idx = backmap[i];
12840 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12841 sum += size_of_uleb128 (dir_idx);
12842 }
12843 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12844 idx_form = DW_FORM_data2;
12845 }
12846 #ifdef VMS_DEBUGGING_INFO
12847 dw2_asm_output_data (1, 4, "File name entry format count");
12848 #else
12849 dw2_asm_output_data (1, 2, "File name entry format count");
12850 #endif
12851 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12852 dw2_asm_output_data_uleb128 (str_form, "%s",
12853 get_DW_FORM_name (str_form));
12854 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12855 "DW_LNCT_directory_index");
12856 dw2_asm_output_data_uleb128 (idx_form, "%s",
12857 get_DW_FORM_name (idx_form));
12858 #ifdef VMS_DEBUGGING_INFO
12859 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12860 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12861 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12862 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12863 #endif
12864 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12865
12866 output_line_string (str_form, filename0, "File Entry", 0);
12867
12868 /* Include directory index. */
12869 if (idx_form != DW_FORM_udata)
12870 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12871 0, NULL);
12872 else
12873 dw2_asm_output_data_uleb128 (0, NULL);
12874
12875 #ifdef VMS_DEBUGGING_INFO
12876 dw2_asm_output_data_uleb128 (0, NULL);
12877 dw2_asm_output_data_uleb128 (0, NULL);
12878 #endif
12879 }
12880
12881 /* Now write all the file names. */
12882 for (i = 0; i < numfiles; i++)
12883 {
12884 int file_idx = backmap[i];
12885 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12886
12887 #ifdef VMS_DEBUGGING_INFO
12888 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12889
12890 /* Setting these fields can lead to debugger miscomparisons,
12891 but VMS Debug requires them to be set correctly. */
12892
12893 int ver;
12894 long long cdt;
12895 long siz;
12896 int maxfilelen = (strlen (files[file_idx].path)
12897 + dirs[dir_idx].length
12898 + MAX_VMS_VERSION_LEN + 1);
12899 char *filebuf = XALLOCAVEC (char, maxfilelen);
12900
12901 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12902 snprintf (filebuf, maxfilelen, "%s;%d",
12903 files[file_idx].path + dirs[dir_idx].length, ver);
12904
12905 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12906
12907 /* Include directory index. */
12908 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12909 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12910 dir_idx + idx_offset, NULL);
12911 else
12912 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12913
12914 /* Modification time. */
12915 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12916 &cdt, 0, 0, 0) == 0)
12917 ? cdt : 0, NULL);
12918
12919 /* File length in bytes. */
12920 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12921 0, &siz, 0, 0) == 0)
12922 ? siz : 0, NULL);
12923 #else
12924 output_line_string (str_form,
12925 files[file_idx].path + dirs[dir_idx].length,
12926 "File Entry", (unsigned) i + 1);
12927
12928 /* Include directory index. */
12929 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12930 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12931 dir_idx + idx_offset, NULL);
12932 else
12933 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12934
12935 if (dwarf_version >= 5)
12936 continue;
12937
12938 /* Modification time. */
12939 dw2_asm_output_data_uleb128 (0, NULL);
12940
12941 /* File length in bytes. */
12942 dw2_asm_output_data_uleb128 (0, NULL);
12943 #endif /* VMS_DEBUGGING_INFO */
12944 }
12945
12946 if (dwarf_version < 5)
12947 dw2_asm_output_data (1, 0, "End file name table");
12948 }
12949
12950
12951 /* Output one line number table into the .debug_line section. */
12952
12953 static void
12954 output_one_line_info_table (dw_line_info_table *table)
12955 {
12956 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12957 unsigned int current_line = 1;
12958 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12959 dw_line_info_entry *ent, *prev_addr = NULL;
12960 size_t i;
12961 unsigned int view;
12962
12963 view = 0;
12964
12965 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12966 {
12967 switch (ent->opcode)
12968 {
12969 case LI_set_address:
12970 /* ??? Unfortunately, we have little choice here currently, and
12971 must always use the most general form. GCC does not know the
12972 address delta itself, so we can't use DW_LNS_advance_pc. Many
12973 ports do have length attributes which will give an upper bound
12974 on the address range. We could perhaps use length attributes
12975 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12976 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12977
12978 view = 0;
12979
12980 /* This can handle any delta. This takes
12981 4+DWARF2_ADDR_SIZE bytes. */
12982 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12983 debug_variable_location_views
12984 ? ", reset view to 0" : "");
12985 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12986 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12987 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12988
12989 prev_addr = ent;
12990 break;
12991
12992 case LI_adv_address:
12993 {
12994 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12995 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12996 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12997
12998 view++;
12999
13000 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
13001 dw2_asm_output_delta (2, line_label, prev_label,
13002 "from %s to %s", prev_label, line_label);
13003
13004 prev_addr = ent;
13005 break;
13006 }
13007
13008 case LI_set_line:
13009 if (ent->val == current_line)
13010 {
13011 /* We still need to start a new row, so output a copy insn. */
13012 dw2_asm_output_data (1, DW_LNS_copy,
13013 "copy line %u", current_line);
13014 }
13015 else
13016 {
13017 int line_offset = ent->val - current_line;
13018 int line_delta = line_offset - DWARF_LINE_BASE;
13019
13020 current_line = ent->val;
13021 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
13022 {
13023 /* This can handle deltas from -10 to 234, using the current
13024 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
13025 This takes 1 byte. */
13026 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
13027 "line %u", current_line);
13028 }
13029 else
13030 {
13031 /* This can handle any delta. This takes at least 4 bytes,
13032 depending on the value being encoded. */
13033 dw2_asm_output_data (1, DW_LNS_advance_line,
13034 "advance to line %u", current_line);
13035 dw2_asm_output_data_sleb128 (line_offset, NULL);
13036 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13037 }
13038 }
13039 break;
13040
13041 case LI_set_file:
13042 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13043 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13044 break;
13045
13046 case LI_set_column:
13047 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13048 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13049 break;
13050
13051 case LI_negate_stmt:
13052 current_is_stmt = !current_is_stmt;
13053 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13054 "is_stmt %d", current_is_stmt);
13055 break;
13056
13057 case LI_set_prologue_end:
13058 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13059 "set prologue end");
13060 break;
13061
13062 case LI_set_epilogue_begin:
13063 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13064 "set epilogue begin");
13065 break;
13066
13067 case LI_set_discriminator:
13068 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13069 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13070 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13071 dw2_asm_output_data_uleb128 (ent->val, NULL);
13072 break;
13073 }
13074 }
13075
13076 /* Emit debug info for the address of the end of the table. */
13077 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13078 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13079 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13080 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13081
13082 dw2_asm_output_data (1, 0, "end sequence");
13083 dw2_asm_output_data_uleb128 (1, NULL);
13084 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13085 }
13086
13087 static unsigned int output_line_info_generation;
13088
13089 /* Output the source line number correspondence information. This
13090 information goes into the .debug_line section. */
13091
13092 static void
13093 output_line_info (bool prologue_only)
13094 {
13095 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13096 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13097 bool saw_one = false;
13098 int opc;
13099
13100 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13101 output_line_info_generation);
13102 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13103 output_line_info_generation);
13104 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13105 output_line_info_generation);
13106 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13107 output_line_info_generation++);
13108
13109 if (!XCOFF_DEBUGGING_INFO)
13110 {
13111 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13112 dw2_asm_output_data (4, 0xffffffff,
13113 "Initial length escape value indicating 64-bit DWARF extension");
13114 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13115 "Length of Source Line Info");
13116 }
13117
13118 ASM_OUTPUT_LABEL (asm_out_file, l1);
13119
13120 output_dwarf_version ();
13121 if (dwarf_version >= 5)
13122 {
13123 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13124 dw2_asm_output_data (1, 0, "Segment Size");
13125 }
13126 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13127 ASM_OUTPUT_LABEL (asm_out_file, p1);
13128
13129 /* Define the architecture-dependent minimum instruction length (in bytes).
13130 In this implementation of DWARF, this field is used for information
13131 purposes only. Since GCC generates assembly language, we have no
13132 a priori knowledge of how many instruction bytes are generated for each
13133 source line, and therefore can use only the DW_LNE_set_address and
13134 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13135 this as '1', which is "correct enough" for all architectures,
13136 and don't let the target override. */
13137 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13138
13139 if (dwarf_version >= 4)
13140 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13141 "Maximum Operations Per Instruction");
13142 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13143 "Default is_stmt_start flag");
13144 dw2_asm_output_data (1, DWARF_LINE_BASE,
13145 "Line Base Value (Special Opcodes)");
13146 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13147 "Line Range Value (Special Opcodes)");
13148 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13149 "Special Opcode Base");
13150
13151 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13152 {
13153 int n_op_args;
13154 switch (opc)
13155 {
13156 case DW_LNS_advance_pc:
13157 case DW_LNS_advance_line:
13158 case DW_LNS_set_file:
13159 case DW_LNS_set_column:
13160 case DW_LNS_fixed_advance_pc:
13161 case DW_LNS_set_isa:
13162 n_op_args = 1;
13163 break;
13164 default:
13165 n_op_args = 0;
13166 break;
13167 }
13168
13169 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13170 opc, n_op_args);
13171 }
13172
13173 /* Write out the information about the files we use. */
13174 output_file_names ();
13175 ASM_OUTPUT_LABEL (asm_out_file, p2);
13176 if (prologue_only)
13177 {
13178 /* Output the marker for the end of the line number info. */
13179 ASM_OUTPUT_LABEL (asm_out_file, l2);
13180 return;
13181 }
13182
13183 if (separate_line_info)
13184 {
13185 dw_line_info_table *table;
13186 size_t i;
13187
13188 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13189 if (table->in_use)
13190 {
13191 output_one_line_info_table (table);
13192 saw_one = true;
13193 }
13194 }
13195 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13196 {
13197 output_one_line_info_table (cold_text_section_line_info);
13198 saw_one = true;
13199 }
13200
13201 /* ??? Some Darwin linkers crash on a .debug_line section with no
13202 sequences. Further, merely a DW_LNE_end_sequence entry is not
13203 sufficient -- the address column must also be initialized.
13204 Make sure to output at least one set_address/end_sequence pair,
13205 choosing .text since that section is always present. */
13206 if (text_section_line_info->in_use || !saw_one)
13207 output_one_line_info_table (text_section_line_info);
13208
13209 /* Output the marker for the end of the line number info. */
13210 ASM_OUTPUT_LABEL (asm_out_file, l2);
13211 }
13212
13213 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13214
13215 static inline bool
13216 need_endianity_attribute_p (bool reverse)
13217 {
13218 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13219 }
13220
13221 /* Given a pointer to a tree node for some base type, return a pointer to
13222 a DIE that describes the given type. REVERSE is true if the type is
13223 to be interpreted in the reverse storage order wrt the target order.
13224
13225 This routine must only be called for GCC type nodes that correspond to
13226 Dwarf base (fundamental) types. */
13227
13228 dw_die_ref
13229 base_type_die (tree type, bool reverse)
13230 {
13231 dw_die_ref base_type_result;
13232 enum dwarf_type encoding;
13233 bool fpt_used = false;
13234 struct fixed_point_type_info fpt_info;
13235 tree type_bias = NULL_TREE;
13236
13237 /* If this is a subtype that should not be emitted as a subrange type,
13238 use the base type. See subrange_type_for_debug_p. */
13239 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13240 type = TREE_TYPE (type);
13241
13242 switch (TREE_CODE (type))
13243 {
13244 case INTEGER_TYPE:
13245 if ((dwarf_version >= 4 || !dwarf_strict)
13246 && TYPE_NAME (type)
13247 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13248 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13249 && DECL_NAME (TYPE_NAME (type)))
13250 {
13251 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13252 if (strcmp (name, "char16_t") == 0
13253 || strcmp (name, "char8_t") == 0
13254 || strcmp (name, "char32_t") == 0)
13255 {
13256 encoding = DW_ATE_UTF;
13257 break;
13258 }
13259 }
13260 if ((dwarf_version >= 3 || !dwarf_strict)
13261 && lang_hooks.types.get_fixed_point_type_info)
13262 {
13263 memset (&fpt_info, 0, sizeof (fpt_info));
13264 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13265 {
13266 fpt_used = true;
13267 encoding = ((TYPE_UNSIGNED (type))
13268 ? DW_ATE_unsigned_fixed
13269 : DW_ATE_signed_fixed);
13270 break;
13271 }
13272 }
13273 if (TYPE_STRING_FLAG (type))
13274 {
13275 if ((dwarf_version >= 4 || !dwarf_strict)
13276 && is_rust ()
13277 && int_size_in_bytes (type) == 4)
13278 encoding = DW_ATE_UTF;
13279 else if (TYPE_UNSIGNED (type))
13280 encoding = DW_ATE_unsigned_char;
13281 else
13282 encoding = DW_ATE_signed_char;
13283 }
13284 else if (TYPE_UNSIGNED (type))
13285 encoding = DW_ATE_unsigned;
13286 else
13287 encoding = DW_ATE_signed;
13288
13289 if (!dwarf_strict
13290 && lang_hooks.types.get_type_bias)
13291 type_bias = lang_hooks.types.get_type_bias (type);
13292 break;
13293
13294 case REAL_TYPE:
13295 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13296 {
13297 if (dwarf_version >= 3 || !dwarf_strict)
13298 encoding = DW_ATE_decimal_float;
13299 else
13300 encoding = DW_ATE_lo_user;
13301 }
13302 else
13303 encoding = DW_ATE_float;
13304 break;
13305
13306 case FIXED_POINT_TYPE:
13307 if (!(dwarf_version >= 3 || !dwarf_strict))
13308 encoding = DW_ATE_lo_user;
13309 else if (TYPE_UNSIGNED (type))
13310 encoding = DW_ATE_unsigned_fixed;
13311 else
13312 encoding = DW_ATE_signed_fixed;
13313 break;
13314
13315 /* Dwarf2 doesn't know anything about complex ints, so use
13316 a user defined type for it. */
13317 case COMPLEX_TYPE:
13318 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (type)))
13319 encoding = DW_ATE_complex_float;
13320 else
13321 encoding = DW_ATE_lo_user;
13322 break;
13323
13324 case BOOLEAN_TYPE:
13325 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13326 encoding = DW_ATE_boolean;
13327 break;
13328
13329 case BITINT_TYPE:
13330 /* C23 _BitInt(N). */
13331 if (TYPE_UNSIGNED (type))
13332 encoding = DW_ATE_unsigned;
13333 else
13334 encoding = DW_ATE_signed;
13335 break;
13336
13337 default:
13338 /* No other TREE_CODEs are Dwarf fundamental types. */
13339 gcc_unreachable ();
13340 }
13341
13342 base_type_result = new_die_raw (DW_TAG_base_type);
13343
13344 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13345 int_size_in_bytes (type));
13346 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13347 if (TREE_CODE (type) == BITINT_TYPE)
13348 add_AT_unsigned (base_type_result, DW_AT_bit_size, TYPE_PRECISION (type));
13349
13350 if (need_endianity_attribute_p (reverse))
13351 add_AT_unsigned (base_type_result, DW_AT_endianity,
13352 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13353
13354 add_alignment_attribute (base_type_result, type);
13355
13356 if (fpt_used)
13357 {
13358 switch (fpt_info.scale_factor_kind)
13359 {
13360 case fixed_point_scale_factor_binary:
13361 add_AT_int (base_type_result, DW_AT_binary_scale,
13362 fpt_info.scale_factor.binary);
13363 break;
13364
13365 case fixed_point_scale_factor_decimal:
13366 add_AT_int (base_type_result, DW_AT_decimal_scale,
13367 fpt_info.scale_factor.decimal);
13368 break;
13369
13370 case fixed_point_scale_factor_arbitrary:
13371 /* Arbitrary scale factors cannot be described in standard DWARF. */
13372 if (!dwarf_strict)
13373 {
13374 /* Describe the scale factor as a rational constant. */
13375 const dw_die_ref scale_factor
13376 = new_die (DW_TAG_constant, comp_unit_die (), type);
13377
13378 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13379 fpt_info.scale_factor.arbitrary.numerator,
13380 dw_scalar_form_constant, NULL);
13381 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13382 fpt_info.scale_factor.arbitrary.denominator,
13383 dw_scalar_form_constant, NULL);
13384
13385 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13386 }
13387 break;
13388
13389 default:
13390 gcc_unreachable ();
13391 }
13392 }
13393
13394 if (type_bias)
13395 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13396 dw_scalar_form_constant
13397 | dw_scalar_form_exprloc
13398 | dw_scalar_form_reference,
13399 NULL);
13400
13401 return base_type_result;
13402 }
13403
13404 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13405 named 'auto' in its type: return true for it, false otherwise. */
13406
13407 static inline bool
13408 is_cxx_auto (tree type)
13409 {
13410 if (is_cxx ())
13411 {
13412 tree name = TYPE_IDENTIFIER (type);
13413 if (name == get_identifier ("auto")
13414 || name == get_identifier ("decltype(auto)"))
13415 return true;
13416 }
13417 return false;
13418 }
13419
13420 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if the
13421 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13422
13423 static inline bool
13424 is_base_type (tree type)
13425 {
13426 switch (TREE_CODE (type))
13427 {
13428 case INTEGER_TYPE:
13429 case REAL_TYPE:
13430 case FIXED_POINT_TYPE:
13431 case COMPLEX_TYPE:
13432 case BOOLEAN_TYPE:
13433 case BITINT_TYPE:
13434 return true;
13435
13436 case VOID_TYPE:
13437 case OPAQUE_TYPE:
13438 case ARRAY_TYPE:
13439 case RECORD_TYPE:
13440 case UNION_TYPE:
13441 case QUAL_UNION_TYPE:
13442 case ENUMERAL_TYPE:
13443 case FUNCTION_TYPE:
13444 case METHOD_TYPE:
13445 case POINTER_TYPE:
13446 case REFERENCE_TYPE:
13447 case NULLPTR_TYPE:
13448 case OFFSET_TYPE:
13449 case LANG_TYPE:
13450 case VECTOR_TYPE:
13451 return false;
13452
13453 default:
13454 if (is_cxx_auto (type))
13455 return false;
13456 gcc_unreachable ();
13457 }
13458 }
13459
13460 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13461 node, return the size in bits for the type if it is a constant, or else
13462 return the alignment for the type if the type's size is not constant, or
13463 else return BITS_PER_WORD if the type actually turns out to be an
13464 ERROR_MARK node. */
13465
13466 static inline unsigned HOST_WIDE_INT
13467 simple_type_size_in_bits (const_tree type)
13468 {
13469 if (TREE_CODE (type) == ERROR_MARK)
13470 return BITS_PER_WORD;
13471 else if (TYPE_SIZE (type) == NULL_TREE)
13472 return 0;
13473 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13474 return tree_to_uhwi (TYPE_SIZE (type));
13475 else
13476 return TYPE_ALIGN (type);
13477 }
13478
13479 /* Similarly, but return an offset_int instead of UHWI. */
13480
13481 static inline offset_int
13482 offset_int_type_size_in_bits (const_tree type)
13483 {
13484 if (TREE_CODE (type) == ERROR_MARK)
13485 return BITS_PER_WORD;
13486 else if (TYPE_SIZE (type) == NULL_TREE)
13487 return 0;
13488 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13489 return wi::to_offset (TYPE_SIZE (type));
13490 else
13491 return TYPE_ALIGN (type);
13492 }
13493
13494 /* Given a pointer to a tree node for a subrange type, return a pointer
13495 to a DIE that describes the given type. */
13496
13497 static dw_die_ref
13498 subrange_type_die (tree type, tree low, tree high, tree bias,
13499 dw_die_ref context_die)
13500 {
13501 dw_die_ref subrange_die;
13502 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13503
13504 if (context_die == NULL)
13505 context_die = comp_unit_die ();
13506
13507 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13508
13509 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13510 {
13511 /* The size of the subrange type and its base type do not match,
13512 so we need to generate a size attribute for the subrange type. */
13513 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13514 }
13515
13516 add_alignment_attribute (subrange_die, type);
13517
13518 if (low)
13519 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13520 if (high)
13521 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13522 if (bias && !dwarf_strict)
13523 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13524 dw_scalar_form_constant
13525 | dw_scalar_form_exprloc
13526 | dw_scalar_form_reference,
13527 NULL);
13528
13529 return subrange_die;
13530 }
13531
13532 /* Returns the (const and/or volatile) cv_qualifiers associated with
13533 the decl node. This will normally be augmented with the
13534 cv_qualifiers of the underlying type in add_type_attribute. */
13535
13536 static int
13537 decl_quals (const_tree decl)
13538 {
13539 return ((TREE_READONLY (decl)
13540 /* The C++ front-end correctly marks reference-typed
13541 variables as readonly, but from a language (and debug
13542 info) standpoint they are not const-qualified. */
13543 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13544 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13545 | (TREE_THIS_VOLATILE (decl)
13546 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13547 }
13548
13549 /* Determine the TYPE whose qualifiers match the largest strict subset
13550 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13551 qualifiers outside QUAL_MASK. */
13552
13553 static int
13554 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13555 {
13556 tree t;
13557 int best_rank = 0, best_qual = 0, max_rank;
13558
13559 type_quals &= qual_mask;
13560 max_rank = popcount_hwi (type_quals) - 1;
13561
13562 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13563 t = TYPE_NEXT_VARIANT (t))
13564 {
13565 int q = TYPE_QUALS (t) & qual_mask;
13566
13567 if ((q & type_quals) == q && q != type_quals
13568 && check_base_type (t, type))
13569 {
13570 int rank = popcount_hwi (q);
13571
13572 if (rank > best_rank)
13573 {
13574 best_rank = rank;
13575 best_qual = q;
13576 }
13577 }
13578 }
13579
13580 return best_qual;
13581 }
13582
13583 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13584 static const dwarf_qual_info_t dwarf_qual_info[] =
13585 {
13586 { TYPE_QUAL_CONST, DW_TAG_const_type },
13587 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13588 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13589 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13590 };
13591 static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13592
13593 /* If DIE is a qualified DIE of some base DIE with the same parent,
13594 return the base DIE, otherwise return NULL. Set MASK to the
13595 qualifiers added compared to the returned DIE. */
13596
13597 static dw_die_ref
13598 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13599 {
13600 unsigned int i;
13601 for (i = 0; i < dwarf_qual_info_size; i++)
13602 if (die->die_tag == dwarf_qual_info[i].t)
13603 break;
13604 if (i == dwarf_qual_info_size)
13605 return NULL;
13606 if (vec_safe_length (die->die_attr) != 1)
13607 return NULL;
13608 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13609 if (type == NULL || type->die_parent != die->die_parent)
13610 return NULL;
13611 *mask |= dwarf_qual_info[i].q;
13612 if (depth)
13613 {
13614 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13615 if (ret)
13616 return ret;
13617 }
13618 return type;
13619 }
13620
13621 /* If TYPE is long double or complex long double that
13622 should be emitted as artificial typedef to _Float128 or
13623 complex _Float128, return the type it should be emitted as.
13624 This is done in case the target already supports 16-byte
13625 composite floating point type (ibm_extended_format). */
13626
13627 static tree
13628 long_double_as_float128 (tree type)
13629 {
13630 if (type != long_double_type_node
13631 && type != complex_long_double_type_node)
13632 return NULL_TREE;
13633
13634 machine_mode mode, fmode;
13635 if (TREE_CODE (type) == COMPLEX_TYPE)
13636 mode = TYPE_MODE (TREE_TYPE (type));
13637 else
13638 mode = TYPE_MODE (type);
13639 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13640 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13641 if (known_eq (GET_MODE_SIZE (fmode), 16)
13642 && MODE_COMPOSITE_P (fmode))
13643 {
13644 if (type == long_double_type_node)
13645 {
13646 if (float128_type_node
13647 && (TYPE_MODE (float128_type_node)
13648 == TYPE_MODE (type)))
13649 return float128_type_node;
13650 return NULL_TREE;
13651 }
13652 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13653 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13654 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13655 == TYPE_MODE (type)))
13656 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13657 }
13658
13659 return NULL_TREE;
13660 }
13661
13662 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13663 entry that chains the modifiers specified by CV_QUALS in front of the
13664 given type. REVERSE is true if the type is to be interpreted in the
13665 reverse storage order wrt the target order. */
13666
13667 static dw_die_ref
13668 modified_type_die (tree type, int cv_quals, bool reverse,
13669 dw_die_ref context_die)
13670 {
13671 enum tree_code code = TREE_CODE (type);
13672 dw_die_ref mod_type_die;
13673 dw_die_ref sub_die = NULL;
13674 tree item_type = NULL;
13675 tree qualified_type;
13676 tree name, low, high;
13677 dw_die_ref mod_scope;
13678 struct array_descr_info info;
13679 /* Only these cv-qualifiers are currently handled. */
13680 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13681 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
13682 /* DW_AT_endianity is specified only for base types in the standard. */
13683 const bool reverse_type
13684 = need_endianity_attribute_p (reverse)
13685 && (is_base_type (type)
13686 || (TREE_CODE (type) == ENUMERAL_TYPE && !dwarf_strict));
13687
13688 if (code == ERROR_MARK)
13689 return NULL;
13690
13691 if (lang_hooks.types.get_debug_type)
13692 {
13693 tree debug_type = lang_hooks.types.get_debug_type (type);
13694
13695 if (debug_type != NULL_TREE && debug_type != type)
13696 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13697 }
13698
13699 cv_quals &= cv_qual_mask;
13700
13701 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13702 tag modifier (and not an attribute) old consumers won't be able
13703 to handle it. */
13704 if (dwarf_version < 3)
13705 cv_quals &= ~TYPE_QUAL_RESTRICT;
13706
13707 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13708 if (dwarf_version < 5)
13709 cv_quals &= ~TYPE_QUAL_ATOMIC;
13710
13711 /* See if we already have the appropriately qualified variant of
13712 this type. */
13713 qualified_type = get_qualified_type (type, cv_quals);
13714
13715 if (qualified_type == sizetype)
13716 {
13717 /* Try not to expose the internal sizetype type's name. */
13718 if (TYPE_NAME (qualified_type)
13719 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13720 {
13721 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13722
13723 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13724 && (TYPE_PRECISION (t)
13725 == TYPE_PRECISION (qualified_type))
13726 && (TYPE_UNSIGNED (t)
13727 == TYPE_UNSIGNED (qualified_type)));
13728 qualified_type = t;
13729 }
13730 else if (qualified_type == sizetype
13731 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13732 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13733 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13734 qualified_type = size_type_node;
13735 if (type == sizetype)
13736 type = qualified_type;
13737 }
13738
13739 /* If we do, then we can just use its DIE, if it exists. */
13740 if (qualified_type)
13741 {
13742 mod_type_die = lookup_type_die (qualified_type);
13743
13744 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13745 dealt with specially: the DIE with the attribute, if it exists, is
13746 placed immediately after the regular DIE for the same type. */
13747 if (mod_type_die
13748 && (!reverse_type
13749 || ((mod_type_die = mod_type_die->die_sib) != NULL
13750 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13751 return mod_type_die;
13752 }
13753
13754 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13755
13756 /* Handle C typedef types. */
13757 if (name
13758 && TREE_CODE (name) == TYPE_DECL
13759 && DECL_ORIGINAL_TYPE (name)
13760 && !DECL_ARTIFICIAL (name))
13761 {
13762 tree dtype = TREE_TYPE (name);
13763
13764 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13765 if (qualified_type == dtype && !reverse_type)
13766 {
13767 tree origin = decl_ultimate_origin (name);
13768
13769 /* Typedef variants that have an abstract origin don't get their own
13770 type DIE (see gen_typedef_die), so fall back on the ultimate
13771 abstract origin instead. */
13772 if (origin != NULL && origin != name)
13773 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13774 context_die);
13775
13776 /* For a named type, use the typedef. */
13777 gen_type_die (qualified_type, context_die);
13778 return lookup_type_die (qualified_type);
13779 }
13780 else
13781 {
13782 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13783 dquals &= cv_qual_mask;
13784 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13785 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13786 /* cv-unqualified version of named type. Just use
13787 the unnamed type to which it refers. */
13788 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13789 reverse, context_die);
13790 /* Else cv-qualified version of named type; fall through. */
13791 }
13792 }
13793
13794 mod_scope = scope_die_for (type, context_die);
13795
13796 if (cv_quals)
13797 {
13798 int sub_quals = 0, first_quals = 0;
13799 unsigned i;
13800 dw_die_ref first = NULL, last = NULL;
13801
13802 /* Determine a lesser qualified type that most closely matches
13803 this one. Then generate DW_TAG_* entries for the remaining
13804 qualifiers. */
13805 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13806 cv_qual_mask);
13807 if (sub_quals && use_debug_types)
13808 {
13809 bool needed = false;
13810 /* If emitting type units, make sure the order of qualifiers
13811 is canonical. Thus, start from unqualified type if
13812 an earlier qualifier is missing in sub_quals, but some later
13813 one is present there. */
13814 for (i = 0; i < dwarf_qual_info_size; i++)
13815 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13816 needed = true;
13817 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13818 {
13819 sub_quals = 0;
13820 break;
13821 }
13822 }
13823 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13824 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13825 {
13826 /* As not all intermediate qualified DIEs have corresponding
13827 tree types, ensure that qualified DIEs in the same scope
13828 as their DW_AT_type are emitted after their DW_AT_type,
13829 only with other qualified DIEs for the same type possibly
13830 in between them. Determine the range of such qualified
13831 DIEs now (first being the base type, last being corresponding
13832 last qualified DIE for it). */
13833 unsigned int count = 0;
13834 first = qualified_die_p (mod_type_die, &first_quals,
13835 dwarf_qual_info_size);
13836 if (first == NULL)
13837 first = mod_type_die;
13838 gcc_assert ((first_quals & ~sub_quals) == 0);
13839 for (count = 0, last = first;
13840 count < (1U << dwarf_qual_info_size);
13841 count++, last = last->die_sib)
13842 {
13843 int quals = 0;
13844 if (last == mod_scope->die_child)
13845 break;
13846 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13847 != first)
13848 break;
13849 }
13850 }
13851
13852 for (i = 0; i < dwarf_qual_info_size; i++)
13853 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13854 {
13855 dw_die_ref d;
13856 if (first && first != last)
13857 {
13858 for (d = first->die_sib; ; d = d->die_sib)
13859 {
13860 int quals = 0;
13861 qualified_die_p (d, &quals, dwarf_qual_info_size);
13862 if (quals == (first_quals | dwarf_qual_info[i].q))
13863 break;
13864 if (d == last)
13865 {
13866 d = NULL;
13867 break;
13868 }
13869 }
13870 if (d)
13871 {
13872 mod_type_die = d;
13873 continue;
13874 }
13875 }
13876 if (first)
13877 {
13878 d = new_die_raw (dwarf_qual_info[i].t);
13879 add_child_die_after (mod_scope, d, last);
13880 last = d;
13881 }
13882 else
13883 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13884 if (mod_type_die)
13885 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13886 mod_type_die = d;
13887 first_quals |= dwarf_qual_info[i].q;
13888 }
13889 }
13890 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13891 {
13892 dwarf_tag tag = DW_TAG_pointer_type;
13893 if (code == REFERENCE_TYPE)
13894 {
13895 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13896 tag = DW_TAG_rvalue_reference_type;
13897 else
13898 tag = DW_TAG_reference_type;
13899 }
13900 mod_type_die = new_die (tag, mod_scope, type);
13901
13902 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13903 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13904 add_alignment_attribute (mod_type_die, type);
13905 item_type = TREE_TYPE (type);
13906
13907 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13908 if (!ADDR_SPACE_GENERIC_P (as))
13909 {
13910 int action = targetm.addr_space.debug (as);
13911 if (action >= 0)
13912 {
13913 /* Positive values indicate an address_class. */
13914 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13915 }
13916 else
13917 {
13918 /* Negative values indicate an (inverted) segment base reg. */
13919 dw_loc_descr_ref d
13920 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13921 add_AT_loc (mod_type_die, DW_AT_segment, d);
13922 }
13923 }
13924 }
13925 else if (code == ARRAY_TYPE
13926 || (lang_hooks.types.get_array_descr_info
13927 && lang_hooks.types.get_array_descr_info (type, &info)))
13928 {
13929 gen_type_die (type, mod_scope);
13930 return lookup_type_die (type);
13931 }
13932 else if (code == INTEGER_TYPE
13933 && TREE_TYPE (type) != NULL_TREE
13934 && subrange_type_for_debug_p (type, &low, &high))
13935 {
13936 tree bias = NULL_TREE;
13937 if (lang_hooks.types.get_type_bias)
13938 bias = lang_hooks.types.get_type_bias (type);
13939 mod_type_die = subrange_type_die (type, low, high, bias, mod_scope);
13940 item_type = TREE_TYPE (type);
13941 }
13942 else if (is_base_type (type))
13943 {
13944 /* If a target supports long double as different floating point
13945 modes with the same 16-byte size, use normal DW_TAG_base_type
13946 only for the composite (ibm_extended_real_format) type and
13947 for the other for the time being emit instead a "_Float128"
13948 or "complex _Float128" DW_TAG_base_type and a "long double"
13949 or "complex long double" typedef to it. */
13950 if (tree other_type = long_double_as_float128 (type))
13951 {
13952 dw_die_ref other_die;
13953 if (TYPE_NAME (other_type))
13954 other_die
13955 = modified_type_die (other_type, TYPE_UNQUALIFIED, reverse,
13956 context_die);
13957 else
13958 {
13959 other_die = base_type_die (type, reverse);
13960 add_child_die (comp_unit_die (), other_die);
13961 add_name_attribute (other_die,
13962 TREE_CODE (type) == COMPLEX_TYPE
13963 ? "complex _Float128" : "_Float128");
13964 }
13965 mod_type_die = new_die_raw (DW_TAG_typedef);
13966 add_AT_die_ref (mod_type_die, DW_AT_type, other_die);
13967 }
13968 else
13969 mod_type_die = base_type_die (type, reverse);
13970
13971 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13972 if (reverse_type)
13973 {
13974 dw_die_ref after_die
13975 = modified_type_die (type, cv_quals, false, context_die);
13976 add_child_die_after (mod_scope, mod_type_die, after_die);
13977 }
13978 else
13979 add_child_die (mod_scope, mod_type_die);
13980
13981 add_pubtype (type, mod_type_die);
13982 }
13983 else
13984 {
13985 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13986 if (reverse_type)
13987 {
13988 dw_die_ref after_die
13989 = modified_type_die (type, cv_quals, false, context_die);
13990 gen_type_die (type, context_die, true);
13991 gcc_assert (after_die->die_sib
13992 && get_AT_unsigned (after_die->die_sib, DW_AT_endianity));
13993 return after_die->die_sib;
13994 }
13995
13996 gen_type_die (type, context_die);
13997
13998 /* We have to get the type_main_variant here (and pass that to the
13999 `lookup_type_die' routine) because the ..._TYPE node we have
14000 might simply be a *copy* of some original type node (where the
14001 copy was created to help us keep track of typedef names) and
14002 that copy might have a different TYPE_UID from the original
14003 ..._TYPE node. */
14004 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
14005 {
14006 /* For function/method types, can't just use type_main_variant here,
14007 because that can have different ref-qualifiers for C++,
14008 but try to canonicalize. */
14009 tree main = TYPE_MAIN_VARIANT (type);
14010 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
14011 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
14012 && check_base_type (t, main)
14013 && check_lang_type (t, type))
14014 return lookup_type_die (t);
14015 return lookup_type_die (type);
14016 }
14017 /* Vectors have the debugging information in the type,
14018 not the main variant. */
14019 else if (code == VECTOR_TYPE)
14020 return lookup_type_die (type);
14021 else
14022 return lookup_type_die (type_main_variant (type));
14023 }
14024
14025 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
14026 don't output a DW_TAG_typedef, since there isn't one in the
14027 user's program; just attach a DW_AT_name to the type.
14028 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
14029 if the base type already has the same name. */
14030 if (name
14031 && ((TREE_CODE (name) != TYPE_DECL
14032 && (qualified_type == TYPE_MAIN_VARIANT (type)
14033 || (cv_quals == TYPE_UNQUALIFIED)))
14034 || (TREE_CODE (name) == TYPE_DECL
14035 && DECL_NAME (name)
14036 && !DECL_NAMELESS (name)
14037 && (TREE_TYPE (name) == qualified_type
14038 || (lang_hooks.types.get_debug_type
14039 && (lang_hooks.types.get_debug_type (TREE_TYPE (name))
14040 == qualified_type))))))
14041 {
14042 if (TREE_CODE (name) == TYPE_DECL)
14043 /* Could just call add_name_and_src_coords_attributes here,
14044 but since this is a builtin type it doesn't have any
14045 useful source coordinates anyway. */
14046 name = DECL_NAME (name);
14047 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
14048 }
14049 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
14050 {
14051 if (TREE_CODE (type) == BITINT_TYPE)
14052 {
14053 char name_buf[sizeof ("unsigned _BitInt(2147483647)")];
14054 snprintf (name_buf, sizeof (name_buf),
14055 "%s_BitInt(%d)", TYPE_UNSIGNED (type) ? "unsigned " : "",
14056 TYPE_PRECISION (type));
14057 add_name_attribute (mod_type_die, name_buf);
14058 }
14059 else
14060 {
14061 /* This probably indicates a bug. */
14062 name = TYPE_IDENTIFIER (type);
14063 add_name_attribute (mod_type_die,
14064 name
14065 ? IDENTIFIER_POINTER (name) : "__unknown__");
14066 }
14067 }
14068
14069 if (qualified_type && !reverse_type)
14070 equate_type_number_to_die (qualified_type, mod_type_die);
14071
14072 if (item_type)
14073 /* We must do this after the equate_type_number_to_die call, in case
14074 this is a recursive type. This ensures that the modified_type_die
14075 recursion will terminate even if the type is recursive. Recursive
14076 types are possible in Ada. */
14077 sub_die = modified_type_die (item_type,
14078 TYPE_QUALS_NO_ADDR_SPACE (item_type),
14079 reverse,
14080 context_die);
14081
14082 if (sub_die != NULL)
14083 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
14084
14085 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14086 if (TYPE_ARTIFICIAL (type))
14087 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
14088
14089 return mod_type_die;
14090 }
14091
14092 /* Generate DIEs for the generic parameters of T.
14093 T must be either a generic type or a generic function.
14094 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14095
14096 static void
14097 gen_generic_params_dies (tree t)
14098 {
14099 tree parms, args;
14100 int parms_num, i;
14101 dw_die_ref die = NULL;
14102 int non_default;
14103
14104 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14105 return;
14106
14107 if (TYPE_P (t))
14108 die = lookup_type_die (t);
14109 else if (DECL_P (t))
14110 die = lookup_decl_die (t);
14111
14112 gcc_assert (die);
14113
14114 parms = lang_hooks.get_innermost_generic_parms (t);
14115 if (!parms)
14116 /* T has no generic parameter. It means T is neither a generic type
14117 or function. End of story. */
14118 return;
14119
14120 parms_num = TREE_VEC_LENGTH (parms);
14121 args = lang_hooks.get_innermost_generic_args (t);
14122 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14123 non_default = int_cst_value (TREE_CHAIN (args));
14124 else
14125 non_default = TREE_VEC_LENGTH (args);
14126 for (i = 0; i < parms_num; i++)
14127 {
14128 tree parm, arg, arg_pack_elems;
14129 dw_die_ref parm_die;
14130
14131 parm = TREE_VEC_ELT (parms, i);
14132 arg = TREE_VEC_ELT (args, i);
14133 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14134 gcc_assert (parm && TREE_VALUE (parm) && arg);
14135
14136 if (parm && TREE_VALUE (parm) && arg)
14137 {
14138 /* If PARM represents a template parameter pack,
14139 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14140 by DW_TAG_template_*_parameter DIEs for the argument
14141 pack elements of ARG. Note that ARG would then be
14142 an argument pack. */
14143 if (arg_pack_elems)
14144 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14145 arg_pack_elems,
14146 die);
14147 else
14148 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14149 true /* emit name */, die);
14150 if (i >= non_default)
14151 add_AT_flag (parm_die, DW_AT_default_value, 1);
14152 }
14153 }
14154 }
14155
14156 /* Create and return a DIE for PARM which should be
14157 the representation of a generic type parameter.
14158 For instance, in the C++ front end, PARM would be a template parameter.
14159 ARG is the argument to PARM.
14160 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14161 name of the PARM.
14162 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14163 as a child node. */
14164
14165 static dw_die_ref
14166 generic_parameter_die (tree parm, tree arg,
14167 bool emit_name_p,
14168 dw_die_ref parent_die)
14169 {
14170 dw_die_ref tmpl_die = NULL;
14171 const char *name = NULL;
14172
14173 /* C++20 accepts class literals as template parameters, and var
14174 decls with initializers represent them. The VAR_DECLs would be
14175 rejected, but we can take the DECL_INITIAL constructor and
14176 attempt to expand it. */
14177 if (arg && VAR_P (arg))
14178 arg = DECL_INITIAL (arg);
14179
14180 if (!parm || !DECL_NAME (parm) || !arg)
14181 return NULL;
14182
14183 /* We support non-type generic parameters and arguments,
14184 type generic parameters and arguments, as well as
14185 generic generic parameters (a.k.a. template template parameters in C++)
14186 and arguments. */
14187 if (TREE_CODE (parm) == PARM_DECL)
14188 /* PARM is a nontype generic parameter */
14189 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14190 else if (TREE_CODE (parm) == TYPE_DECL)
14191 /* PARM is a type generic parameter. */
14192 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14193 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14194 /* PARM is a generic generic parameter.
14195 Its DIE is a GNU extension. It shall have a
14196 DW_AT_name attribute to represent the name of the template template
14197 parameter, and a DW_AT_GNU_template_name attribute to represent the
14198 name of the template template argument. */
14199 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14200 parent_die, parm);
14201 else
14202 gcc_unreachable ();
14203
14204 if (tmpl_die)
14205 {
14206 tree tmpl_type;
14207
14208 /* If PARM is a generic parameter pack, it means we are
14209 emitting debug info for a template argument pack element.
14210 In other terms, ARG is a template argument pack element.
14211 In that case, we don't emit any DW_AT_name attribute for
14212 the die. */
14213 if (emit_name_p)
14214 {
14215 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14216 gcc_assert (name);
14217 add_AT_string (tmpl_die, DW_AT_name, name);
14218 }
14219
14220 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14221 {
14222 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14223 TMPL_DIE should have a child DW_AT_type attribute that is set
14224 to the type of the argument to PARM, which is ARG.
14225 If PARM is a type generic parameter, TMPL_DIE should have a
14226 child DW_AT_type that is set to ARG. */
14227 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14228 add_type_attribute (tmpl_die, tmpl_type,
14229 (TREE_THIS_VOLATILE (tmpl_type)
14230 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14231 false, parent_die);
14232 }
14233 else
14234 {
14235 /* So TMPL_DIE is a DIE representing a
14236 a generic generic template parameter, a.k.a template template
14237 parameter in C++ and arg is a template. */
14238
14239 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14240 to the name of the argument. */
14241 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14242 if (name)
14243 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14244 }
14245
14246 if (TREE_CODE (parm) == PARM_DECL)
14247 /* So PARM is a non-type generic parameter.
14248 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14249 attribute of TMPL_DIE which value represents the value
14250 of ARG.
14251 We must be careful here:
14252 The value of ARG might reference some function decls.
14253 We might currently be emitting debug info for a generic
14254 type and types are emitted before function decls, we don't
14255 know if the function decls referenced by ARG will actually be
14256 emitted after cgraph computations.
14257 So must defer the generation of the DW_AT_const_value to
14258 after cgraph is ready. */
14259 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14260 }
14261
14262 return tmpl_die;
14263 }
14264
14265 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14266 PARM_PACK must be a template parameter pack. The returned DIE
14267 will be child DIE of PARENT_DIE. */
14268
14269 static dw_die_ref
14270 template_parameter_pack_die (tree parm_pack,
14271 tree parm_pack_args,
14272 dw_die_ref parent_die)
14273 {
14274 dw_die_ref die;
14275 int j;
14276
14277 gcc_assert (parent_die && parm_pack);
14278
14279 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14280 add_name_and_src_coords_attributes (die, parm_pack);
14281 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14282 generic_parameter_die (parm_pack,
14283 TREE_VEC_ELT (parm_pack_args, j),
14284 false /* Don't emit DW_AT_name */,
14285 die);
14286 return die;
14287 }
14288
14289 /* Return the debugger register number described by a given RTL node. */
14290
14291 static unsigned int
14292 debugger_reg_number (const_rtx rtl)
14293 {
14294 unsigned regno = REGNO (rtl);
14295
14296 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14297
14298 #ifdef LEAF_REG_REMAP
14299 if (crtl->uses_only_leaf_regs)
14300 {
14301 int leaf_reg = LEAF_REG_REMAP (regno);
14302 if (leaf_reg != -1)
14303 regno = (unsigned) leaf_reg;
14304 }
14305 #endif
14306
14307 regno = DEBUGGER_REGNO (regno);
14308 gcc_assert (regno != INVALID_REGNUM);
14309 return regno;
14310 }
14311
14312 /* Optionally add a DW_OP_piece term to a location description expression.
14313 DW_OP_piece is only added if the location description expression already
14314 doesn't end with DW_OP_piece. */
14315
14316 static void
14317 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14318 {
14319 dw_loc_descr_ref loc;
14320
14321 if (*list_head != NULL)
14322 {
14323 /* Find the end of the chain. */
14324 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14325 ;
14326
14327 if (loc->dw_loc_opc != DW_OP_piece)
14328 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14329 }
14330 }
14331
14332 /* Return a location descriptor that designates a machine register or
14333 zero if there is none. */
14334
14335 static dw_loc_descr_ref
14336 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14337 {
14338 rtx regs;
14339
14340 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14341 return 0;
14342
14343 /* We only use "frame base" when we're sure we're talking about the
14344 post-prologue local stack frame. We do this by *not* running
14345 register elimination until this point, and recognizing the special
14346 argument pointer and soft frame pointer rtx's.
14347 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14348 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14349 && (ira_use_lra_p
14350 ? lra_eliminate_regs (rtl, VOIDmode, NULL_RTX)
14351 : eliminate_regs (rtl, VOIDmode, NULL_RTX)) != rtl)
14352 {
14353 dw_loc_descr_ref result = NULL;
14354
14355 if (dwarf_version >= 4 || !dwarf_strict)
14356 {
14357 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14358 initialized);
14359 if (result)
14360 add_loc_descr (&result,
14361 new_loc_descr (DW_OP_stack_value, 0, 0));
14362 }
14363 return result;
14364 }
14365
14366 regs = targetm.dwarf_register_span (rtl);
14367
14368 if (REG_NREGS (rtl) > 1 || regs)
14369 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14370 else
14371 {
14372 unsigned int debugger_regnum = debugger_reg_number (rtl);
14373 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14374 return 0;
14375 return one_reg_loc_descriptor (debugger_regnum, initialized);
14376 }
14377 }
14378
14379 /* Return a location descriptor that designates a machine register for
14380 a given hard register number. */
14381
14382 static dw_loc_descr_ref
14383 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14384 {
14385 dw_loc_descr_ref reg_loc_descr;
14386
14387 if (regno <= 31)
14388 reg_loc_descr
14389 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14390 else
14391 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14392
14393 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14394 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14395
14396 return reg_loc_descr;
14397 }
14398
14399 /* Given an RTL of a register, return a location descriptor that
14400 designates a value that spans more than one register. */
14401
14402 static dw_loc_descr_ref
14403 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14404 enum var_init_status initialized)
14405 {
14406 int size, i;
14407 dw_loc_descr_ref loc_result = NULL;
14408
14409 /* Simple, contiguous registers. */
14410 if (regs == NULL_RTX)
14411 {
14412 unsigned reg = REGNO (rtl);
14413 int nregs;
14414
14415 #ifdef LEAF_REG_REMAP
14416 if (crtl->uses_only_leaf_regs)
14417 {
14418 int leaf_reg = LEAF_REG_REMAP (reg);
14419 if (leaf_reg != -1)
14420 reg = (unsigned) leaf_reg;
14421 }
14422 #endif
14423
14424 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14425 nregs = REG_NREGS (rtl);
14426
14427 /* At present we only track constant-sized pieces. */
14428 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14429 return NULL;
14430 size /= nregs;
14431
14432 loc_result = NULL;
14433 while (nregs--)
14434 {
14435 dw_loc_descr_ref t;
14436
14437 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14438 VAR_INIT_STATUS_INITIALIZED);
14439 add_loc_descr (&loc_result, t);
14440 add_loc_descr_op_piece (&loc_result, size);
14441 ++reg;
14442 }
14443 return loc_result;
14444 }
14445
14446 /* Now onto stupid register sets in non contiguous locations. */
14447
14448 gcc_assert (GET_CODE (regs) == PARALLEL);
14449
14450 /* At present we only track constant-sized pieces. */
14451 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14452 return NULL;
14453 loc_result = NULL;
14454
14455 for (i = 0; i < XVECLEN (regs, 0); ++i)
14456 {
14457 dw_loc_descr_ref t;
14458
14459 t = one_reg_loc_descriptor (debugger_reg_number (XVECEXP (regs, 0, i)),
14460 VAR_INIT_STATUS_INITIALIZED);
14461 add_loc_descr (&loc_result, t);
14462 add_loc_descr_op_piece (&loc_result, size);
14463 }
14464
14465 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14466 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14467 return loc_result;
14468 }
14469
14470 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14471
14472 /* Return a location descriptor that designates a constant i,
14473 as a compound operation from constant (i >> shift), constant shift
14474 and DW_OP_shl. */
14475
14476 static dw_loc_descr_ref
14477 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14478 {
14479 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14480 add_loc_descr (&ret, int_loc_descriptor (shift));
14481 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14482 return ret;
14483 }
14484
14485 /* Return a location descriptor that designates constant POLY_I. */
14486
14487 static dw_loc_descr_ref
14488 int_loc_descriptor (poly_int64 poly_i)
14489 {
14490 enum dwarf_location_atom op;
14491
14492 HOST_WIDE_INT i;
14493 if (!poly_i.is_constant (&i))
14494 {
14495 /* Create location descriptions for the non-constant part and
14496 add any constant offset at the end. */
14497 dw_loc_descr_ref ret = NULL;
14498 HOST_WIDE_INT constant = poly_i.coeffs[0];
14499 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14500 {
14501 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14502 if (coeff != 0)
14503 {
14504 dw_loc_descr_ref start = ret;
14505 unsigned int factor;
14506 int bias;
14507 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14508 (j, &factor, &bias);
14509
14510 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14511 add COEFF * (REGNO / FACTOR) now and subtract
14512 COEFF * BIAS from the final constant part. */
14513 constant -= coeff * bias;
14514 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14515 if (coeff % factor == 0)
14516 coeff /= factor;
14517 else
14518 {
14519 int amount = exact_log2 (factor);
14520 gcc_assert (amount >= 0);
14521 add_loc_descr (&ret, int_loc_descriptor (amount));
14522 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14523 }
14524 if (coeff != 1)
14525 {
14526 add_loc_descr (&ret, int_loc_descriptor (coeff));
14527 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14528 }
14529 if (start)
14530 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14531 }
14532 }
14533 loc_descr_plus_const (&ret, constant);
14534 return ret;
14535 }
14536
14537 /* Pick the smallest representation of a constant, rather than just
14538 defaulting to the LEB encoding. */
14539 if (i >= 0)
14540 {
14541 int clz = clz_hwi (i);
14542 int ctz = ctz_hwi (i);
14543 if (i <= 31)
14544 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14545 else if (i <= 0xff)
14546 op = DW_OP_const1u;
14547 else if (i <= 0xffff)
14548 op = DW_OP_const2u;
14549 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14550 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14551 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14552 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14553 while DW_OP_const4u is 5 bytes. */
14554 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14555 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14556 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14557 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14558 while DW_OP_const4u is 5 bytes. */
14559 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14560
14561 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14562 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14563 <= 4)
14564 {
14565 /* As i >= 2**31, the double cast above will yield a negative number.
14566 Since wrapping is defined in DWARF expressions we can output big
14567 positive integers as small negative ones, regardless of the size
14568 of host wide ints.
14569
14570 Here, since the evaluator will handle 32-bit values and since i >=
14571 2**31, we know it's going to be interpreted as a negative literal:
14572 store it this way if we can do better than 5 bytes this way. */
14573 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14574 }
14575 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14576 op = DW_OP_const4u;
14577
14578 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14579 least 6 bytes: see if we can do better before falling back to it. */
14580 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14581 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14582 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14583 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14584 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14585 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14586 >= HOST_BITS_PER_WIDE_INT)
14587 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14588 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14589 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14590 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14591 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14592 && size_of_uleb128 (i) > 6)
14593 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14594 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14595 else
14596 op = DW_OP_constu;
14597 }
14598 else
14599 {
14600 if (i >= -0x80)
14601 op = DW_OP_const1s;
14602 else if (i >= -0x8000)
14603 op = DW_OP_const2s;
14604 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14605 {
14606 if (size_of_int_loc_descriptor (i) < 5)
14607 {
14608 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14609 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14610 return ret;
14611 }
14612 op = DW_OP_const4s;
14613 }
14614 else
14615 {
14616 if (size_of_int_loc_descriptor (i)
14617 < (unsigned long) 1 + size_of_sleb128 (i))
14618 {
14619 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14620 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14621 return ret;
14622 }
14623 op = DW_OP_consts;
14624 }
14625 }
14626
14627 return new_loc_descr (op, i, 0);
14628 }
14629
14630 /* Likewise, for unsigned constants. */
14631
14632 static dw_loc_descr_ref
14633 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14634 {
14635 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14636 const unsigned HOST_WIDE_INT max_uint
14637 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14638
14639 /* If possible, use the clever signed constants handling. */
14640 if (i <= max_int)
14641 return int_loc_descriptor ((HOST_WIDE_INT) i);
14642
14643 /* Here, we are left with positive numbers that cannot be represented as
14644 HOST_WIDE_INT, i.e.:
14645 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14646
14647 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14648 whereas may be better to output a negative integer: thanks to integer
14649 wrapping, we know that:
14650 x = x - 2 ** DWARF2_ADDR_SIZE
14651 = x - 2 * (max (HOST_WIDE_INT) + 1)
14652 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14653 small negative integers. Let's try that in cases it will clearly improve
14654 the encoding: there is no gain turning DW_OP_const4u into
14655 DW_OP_const4s. */
14656 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14657 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14658 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14659 {
14660 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14661
14662 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14663 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14664 const HOST_WIDE_INT second_shift
14665 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14666
14667 /* So we finally have:
14668 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14669 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14670 return int_loc_descriptor (second_shift);
14671 }
14672
14673 /* Last chance: fallback to a simple constant operation. */
14674 return new_loc_descr
14675 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14676 ? DW_OP_const4u
14677 : DW_OP_const8u,
14678 i, 0);
14679 }
14680
14681 /* Generate and return a location description that computes the unsigned
14682 comparison of the two stack top entries (a OP b where b is the top-most
14683 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14684 LE_EXPR, GT_EXPR or GE_EXPR. */
14685
14686 static dw_loc_descr_ref
14687 uint_comparison_loc_list (enum tree_code kind)
14688 {
14689 enum dwarf_location_atom op, flip_op;
14690 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14691
14692 switch (kind)
14693 {
14694 case LT_EXPR:
14695 op = DW_OP_lt;
14696 break;
14697 case LE_EXPR:
14698 op = DW_OP_le;
14699 break;
14700 case GT_EXPR:
14701 op = DW_OP_gt;
14702 break;
14703 case GE_EXPR:
14704 op = DW_OP_ge;
14705 break;
14706 default:
14707 gcc_unreachable ();
14708 }
14709
14710 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14711 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14712
14713 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14714 possible to perform unsigned comparisons: we just have to distinguish
14715 three cases:
14716
14717 1. when a and b have the same sign (as signed integers); then we should
14718 return: a OP(signed) b;
14719
14720 2. when a is a negative signed integer while b is a positive one, then a
14721 is a greater unsigned integer than b; likewise when a and b's roles
14722 are flipped.
14723
14724 So first, compare the sign of the two operands. */
14725 ret = new_loc_descr (DW_OP_over, 0, 0);
14726 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14727 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14728 /* If they have different signs (i.e. they have different sign bits), then
14729 the stack top value has now the sign bit set and thus it's smaller than
14730 zero. */
14731 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14732 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14733 add_loc_descr (&ret, bra_node);
14734
14735 /* We are in case 1. At this point, we know both operands have the same
14736 sign, to it's safe to use the built-in signed comparison. */
14737 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14738 add_loc_descr (&ret, jmp_node);
14739
14740 /* We are in case 2. Here, we know both operands do not have the same sign,
14741 so we have to flip the signed comparison. */
14742 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14743 tmp = new_loc_descr (flip_op, 0, 0);
14744 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14745 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14746 add_loc_descr (&ret, tmp);
14747
14748 /* This dummy operation is necessary to make the two branches join. */
14749 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14750 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14751 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14752 add_loc_descr (&ret, tmp);
14753
14754 return ret;
14755 }
14756
14757 /* Likewise, but takes the location description lists (might be destructive on
14758 them). Return NULL if either is NULL or if concatenation fails. */
14759
14760 static dw_loc_list_ref
14761 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14762 enum tree_code kind)
14763 {
14764 if (left == NULL || right == NULL)
14765 return NULL;
14766
14767 add_loc_list (&left, right);
14768 if (left == NULL)
14769 return NULL;
14770
14771 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14772 return left;
14773 }
14774
14775 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14776 without actually allocating it. */
14777
14778 static unsigned long
14779 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14780 {
14781 return size_of_int_loc_descriptor (i >> shift)
14782 + size_of_int_loc_descriptor (shift)
14783 + 1;
14784 }
14785
14786 /* Return size_of_locs (int_loc_descriptor (i)) without
14787 actually allocating it. */
14788
14789 static unsigned long
14790 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14791 {
14792 unsigned long s;
14793
14794 if (i >= 0)
14795 {
14796 int clz, ctz;
14797 if (i <= 31)
14798 return 1;
14799 else if (i <= 0xff)
14800 return 2;
14801 else if (i <= 0xffff)
14802 return 3;
14803 clz = clz_hwi (i);
14804 ctz = ctz_hwi (i);
14805 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14806 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14807 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14808 - clz - 5);
14809 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14810 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14811 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14812 - clz - 8);
14813 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14814 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14815 <= 4)
14816 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14817 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14818 return 5;
14819 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14820 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14821 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14822 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14823 - clz - 8);
14824 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14825 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14826 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14827 - clz - 16);
14828 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14829 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14830 && s > 6)
14831 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14832 - clz - 32);
14833 else
14834 return 1 + s;
14835 }
14836 else
14837 {
14838 if (i >= -0x80)
14839 return 2;
14840 else if (i >= -0x8000)
14841 return 3;
14842 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14843 {
14844 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14845 {
14846 s = size_of_int_loc_descriptor (-i) + 1;
14847 if (s < 5)
14848 return s;
14849 }
14850 return 5;
14851 }
14852 else
14853 {
14854 unsigned long r = 1 + size_of_sleb128 (i);
14855 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14856 {
14857 s = size_of_int_loc_descriptor (-i) + 1;
14858 if (s < r)
14859 return s;
14860 }
14861 return r;
14862 }
14863 }
14864 }
14865
14866 /* Return loc description representing "address" of integer value.
14867 This can appear only as toplevel expression. */
14868
14869 static dw_loc_descr_ref
14870 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14871 {
14872 int litsize;
14873 dw_loc_descr_ref loc_result = NULL;
14874
14875 if (!(dwarf_version >= 4 || !dwarf_strict))
14876 return NULL;
14877
14878 litsize = size_of_int_loc_descriptor (i);
14879 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14880 is more compact. For DW_OP_stack_value we need:
14881 litsize + 1 (DW_OP_stack_value)
14882 and for DW_OP_implicit_value:
14883 1 (DW_OP_implicit_value) + 1 (length) + size. */
14884 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14885 {
14886 loc_result = int_loc_descriptor (i);
14887 add_loc_descr (&loc_result,
14888 new_loc_descr (DW_OP_stack_value, 0, 0));
14889 return loc_result;
14890 }
14891
14892 loc_result = new_loc_descr (DW_OP_implicit_value,
14893 size, 0);
14894 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14895 loc_result->dw_loc_oprnd2.v.val_int = i;
14896 return loc_result;
14897 }
14898
14899 /* Return a location descriptor that designates a base+offset location. */
14900
14901 static dw_loc_descr_ref
14902 based_loc_descr (rtx reg, poly_int64 offset,
14903 enum var_init_status initialized)
14904 {
14905 unsigned int regno;
14906 dw_loc_descr_ref result;
14907 dw_fde_ref fde = cfun->fde;
14908
14909 /* We only use "frame base" when we're sure we're talking about the
14910 post-prologue local stack frame. We do this by *not* running
14911 register elimination until this point, and recognizing the special
14912 argument pointer and soft frame pointer rtx's. */
14913 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14914 {
14915 rtx elim = (ira_use_lra_p
14916 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14917 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14918
14919 if (elim != reg)
14920 {
14921 /* Allow hard frame pointer here even if frame pointer
14922 isn't used since hard frame pointer is encoded with
14923 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14924 not hard frame pointer directly. */
14925 elim = strip_offset_and_add (elim, &offset);
14926 gcc_assert (elim == hard_frame_pointer_rtx
14927 || elim == stack_pointer_rtx);
14928
14929 /* If drap register is used to align stack, use frame
14930 pointer + offset to access stack variables. If stack
14931 is aligned without drap, use stack pointer + offset to
14932 access stack variables. */
14933 if (crtl->stack_realign_tried
14934 && reg == frame_pointer_rtx)
14935 {
14936 int base_reg
14937 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14938 ? HARD_FRAME_POINTER_REGNUM
14939 : REGNO (elim));
14940 return new_reg_loc_descr (base_reg, offset);
14941 }
14942
14943 gcc_assert (frame_pointer_fb_offset_valid);
14944 offset += frame_pointer_fb_offset;
14945 HOST_WIDE_INT const_offset;
14946 if (offset.is_constant (&const_offset))
14947 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14948 else
14949 {
14950 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14951 loc_descr_plus_const (&ret, offset);
14952 return ret;
14953 }
14954 }
14955 }
14956
14957 regno = REGNO (reg);
14958 #ifdef LEAF_REG_REMAP
14959 if (crtl->uses_only_leaf_regs)
14960 {
14961 int leaf_reg = LEAF_REG_REMAP (regno);
14962 if (leaf_reg != -1)
14963 regno = (unsigned) leaf_reg;
14964 }
14965 #endif
14966 regno = DWARF_FRAME_REGNUM (regno);
14967
14968 HOST_WIDE_INT const_offset;
14969 if (!optimize && fde
14970 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14971 && offset.is_constant (&const_offset))
14972 {
14973 /* Use cfa+offset to represent the location of arguments passed
14974 on the stack when drap is used to align stack.
14975 Only do this when not optimizing, for optimized code var-tracking
14976 is supposed to track where the arguments live and the register
14977 used as vdrap or drap in some spot might be used for something
14978 else in other part of the routine. */
14979 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14980 }
14981
14982 result = new_reg_loc_descr (regno, offset);
14983
14984 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14985 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14986
14987 return result;
14988 }
14989
14990 /* Return true if this RTL expression describes a base+offset calculation. */
14991
14992 static inline bool
14993 is_based_loc (const_rtx rtl)
14994 {
14995 return (GET_CODE (rtl) == PLUS
14996 && ((REG_P (XEXP (rtl, 0))
14997 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14998 && CONST_INT_P (XEXP (rtl, 1)))));
14999 }
15000
15001 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
15002 failed. */
15003
15004 static dw_loc_descr_ref
15005 tls_mem_loc_descriptor (rtx mem)
15006 {
15007 tree base;
15008 dw_loc_descr_ref loc_result;
15009
15010 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
15011 return NULL;
15012
15013 base = get_base_address (MEM_EXPR (mem));
15014 if (base == NULL
15015 || !VAR_P (base)
15016 || !DECL_THREAD_LOCAL_P (base))
15017 return NULL;
15018
15019 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
15020 if (loc_result == NULL)
15021 return NULL;
15022
15023 if (maybe_ne (MEM_OFFSET (mem), 0))
15024 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
15025
15026 return loc_result;
15027 }
15028
15029 /* Output debug info about reason why we failed to expand expression as dwarf
15030 expression. */
15031
15032 static void
15033 expansion_failed (tree expr, rtx rtl, char const *reason)
15034 {
15035 if (dump_file && (dump_flags & TDF_DETAILS))
15036 {
15037 fprintf (dump_file, "Failed to expand as dwarf: ");
15038 if (expr)
15039 print_generic_expr (dump_file, expr, dump_flags);
15040 if (rtl)
15041 {
15042 fprintf (dump_file, "\n");
15043 print_rtl (dump_file, rtl);
15044 }
15045 fprintf (dump_file, "\nReason: %s\n", reason);
15046 }
15047 }
15048
15049 /* Helper function for const_ok_for_output. */
15050
15051 static bool
15052 const_ok_for_output_1 (rtx rtl)
15053 {
15054 if (targetm.const_not_ok_for_debug_p (rtl))
15055 {
15056 if (GET_CODE (rtl) != UNSPEC)
15057 {
15058 expansion_failed (NULL_TREE, rtl,
15059 "Expression rejected for debug by the backend.\n");
15060 return false;
15061 }
15062
15063 /* If delegitimize_address couldn't do anything with the UNSPEC, and
15064 the target hook doesn't explicitly allow it in debug info, assume
15065 we can't express it in the debug info. */
15066 /* Don't complain about TLS UNSPECs, those are just too hard to
15067 delegitimize. Note this could be a non-decl SYMBOL_REF such as
15068 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
15069 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
15070 if (flag_checking
15071 && (XVECLEN (rtl, 0) == 0
15072 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
15073 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
15074 inform (current_function_decl
15075 ? DECL_SOURCE_LOCATION (current_function_decl)
15076 : UNKNOWN_LOCATION,
15077 #if NUM_UNSPEC_VALUES > 0
15078 "non-delegitimized UNSPEC %s (%d) found in variable location",
15079 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
15080 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
15081 #else
15082 "non-delegitimized UNSPEC %d found in variable location",
15083 #endif
15084 XINT (rtl, 1));
15085 expansion_failed (NULL_TREE, rtl,
15086 "UNSPEC hasn't been delegitimized.\n");
15087 return false;
15088 }
15089
15090 if (CONST_POLY_INT_P (rtl))
15091 return false;
15092
15093 /* FIXME: Refer to PR60655. It is possible for simplification
15094 of rtl expressions in var tracking to produce such expressions.
15095 We should really identify / validate expressions
15096 enclosed in CONST that can be handled by assemblers on various
15097 targets and only handle legitimate cases here. */
15098 switch (GET_CODE (rtl))
15099 {
15100 case SYMBOL_REF:
15101 break;
15102 case NOT:
15103 case NEG:
15104 return false;
15105 case PLUS:
15106 {
15107 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15108 operands. */
15109 subrtx_var_iterator::array_type array;
15110 bool first = false;
15111 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15112 if (SYMBOL_REF_P (*iter)
15113 || LABEL_P (*iter)
15114 || GET_CODE (*iter) == UNSPEC)
15115 {
15116 first = true;
15117 break;
15118 }
15119 if (!first)
15120 return true;
15121 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15122 if (SYMBOL_REF_P (*iter)
15123 || LABEL_P (*iter)
15124 || GET_CODE (*iter) == UNSPEC)
15125 return false;
15126 return true;
15127 }
15128 case MINUS:
15129 {
15130 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15131 appear in the second operand of MINUS. */
15132 subrtx_var_iterator::array_type array;
15133 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15134 if (SYMBOL_REF_P (*iter)
15135 || LABEL_P (*iter)
15136 || GET_CODE (*iter) == UNSPEC)
15137 return false;
15138 return true;
15139 }
15140 default:
15141 return true;
15142 }
15143
15144 if (CONSTANT_POOL_ADDRESS_P (rtl))
15145 {
15146 bool marked;
15147 get_pool_constant_mark (rtl, &marked);
15148 /* If all references to this pool constant were optimized away,
15149 it was not output and thus we can't represent it. */
15150 if (!marked)
15151 {
15152 expansion_failed (NULL_TREE, rtl,
15153 "Constant was removed from constant pool.\n");
15154 return false;
15155 }
15156 }
15157
15158 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15159 return false;
15160
15161 /* Avoid references to external symbols in debug info, on several targets
15162 the linker might even refuse to link when linking a shared library,
15163 and in many other cases the relocations for .debug_info/.debug_loc are
15164 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15165 to be defined within the same shared library or executable are fine. */
15166 if (SYMBOL_REF_EXTERNAL_P (rtl))
15167 {
15168 tree decl = SYMBOL_REF_DECL (rtl);
15169
15170 if (decl == NULL || !targetm.binds_local_p (decl))
15171 {
15172 expansion_failed (NULL_TREE, rtl,
15173 "Symbol not defined in current TU.\n");
15174 return false;
15175 }
15176 }
15177
15178 return true;
15179 }
15180
15181 /* Return true if constant RTL can be emitted in DW_OP_addr or
15182 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15183 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15184
15185 static bool
15186 const_ok_for_output (rtx rtl)
15187 {
15188 if (GET_CODE (rtl) == SYMBOL_REF)
15189 return const_ok_for_output_1 (rtl);
15190
15191 if (GET_CODE (rtl) == CONST)
15192 {
15193 subrtx_var_iterator::array_type array;
15194 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15195 if (!const_ok_for_output_1 (*iter))
15196 return false;
15197 return true;
15198 }
15199
15200 return true;
15201 }
15202
15203 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15204 if possible, NULL otherwise. */
15205
15206 static dw_die_ref
15207 base_type_for_mode (machine_mode mode, bool unsignedp)
15208 {
15209 dw_die_ref type_die;
15210 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15211
15212 if (type == NULL)
15213 return NULL;
15214 switch (TREE_CODE (type))
15215 {
15216 case INTEGER_TYPE:
15217 case REAL_TYPE:
15218 break;
15219 default:
15220 return NULL;
15221 }
15222 type_die = lookup_type_die (type);
15223 if (!type_die)
15224 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15225 comp_unit_die ());
15226 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15227 return NULL;
15228 return type_die;
15229 }
15230
15231 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15232 type matching MODE, or, if MODE is narrower than or as wide as
15233 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15234 possible. */
15235
15236 static dw_loc_descr_ref
15237 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15238 {
15239 machine_mode outer_mode = mode;
15240 dw_die_ref type_die;
15241 dw_loc_descr_ref cvt;
15242
15243 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15244 {
15245 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15246 return op;
15247 }
15248 type_die = base_type_for_mode (outer_mode, 1);
15249 if (type_die == NULL)
15250 return NULL;
15251 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15252 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15253 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15254 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15255 add_loc_descr (&op, cvt);
15256 return op;
15257 }
15258
15259 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15260
15261 static dw_loc_descr_ref
15262 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15263 dw_loc_descr_ref op1)
15264 {
15265 dw_loc_descr_ref ret = op0;
15266 add_loc_descr (&ret, op1);
15267 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15268 if (STORE_FLAG_VALUE != 1)
15269 {
15270 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15271 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15272 }
15273 return ret;
15274 }
15275
15276 /* Subroutine of scompare_loc_descriptor for the case in which we're
15277 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15278 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15279
15280 static dw_loc_descr_ref
15281 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15282 scalar_int_mode op_mode,
15283 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15284 {
15285 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15286 dw_loc_descr_ref cvt;
15287
15288 if (type_die == NULL)
15289 return NULL;
15290 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15291 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15292 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15293 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15294 add_loc_descr (&op0, cvt);
15295 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15296 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15297 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15298 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15299 add_loc_descr (&op1, cvt);
15300 return compare_loc_descriptor (op, op0, op1);
15301 }
15302
15303 /* Subroutine of scompare_loc_descriptor for the case in which we're
15304 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15305 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15306
15307 static dw_loc_descr_ref
15308 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15309 scalar_int_mode op_mode,
15310 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15311 {
15312 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15313 /* For eq/ne, if the operands are known to be zero-extended,
15314 there is no need to do the fancy shifting up. */
15315 if (op == DW_OP_eq || op == DW_OP_ne)
15316 {
15317 dw_loc_descr_ref last0, last1;
15318 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15319 ;
15320 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15321 ;
15322 /* deref_size zero extends, and for constants we can check
15323 whether they are zero extended or not. */
15324 if (((last0->dw_loc_opc == DW_OP_deref_size
15325 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15326 || (CONST_INT_P (XEXP (rtl, 0))
15327 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15328 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15329 && ((last1->dw_loc_opc == DW_OP_deref_size
15330 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15331 || (CONST_INT_P (XEXP (rtl, 1))
15332 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15333 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15334 return compare_loc_descriptor (op, op0, op1);
15335
15336 /* EQ/NE comparison against constant in narrower type than
15337 DWARF2_ADDR_SIZE can be performed either as
15338 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15339 DW_OP_{eq,ne}
15340 or
15341 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15342 DW_OP_{eq,ne}. Pick whatever is shorter. */
15343 if (CONST_INT_P (XEXP (rtl, 1))
15344 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15345 && (size_of_int_loc_descriptor (shift) + 1
15346 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15347 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15348 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15349 & GET_MODE_MASK (op_mode))))
15350 {
15351 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15352 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15353 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15354 & GET_MODE_MASK (op_mode));
15355 return compare_loc_descriptor (op, op0, op1);
15356 }
15357 }
15358 add_loc_descr (&op0, int_loc_descriptor (shift));
15359 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15360 if (CONST_INT_P (XEXP (rtl, 1)))
15361 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15362 else
15363 {
15364 add_loc_descr (&op1, int_loc_descriptor (shift));
15365 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15366 }
15367 return compare_loc_descriptor (op, op0, op1);
15368 }
15369
15370 /* Return location descriptor for signed comparison OP RTL. */
15371
15372 static dw_loc_descr_ref
15373 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15374 machine_mode mem_mode)
15375 {
15376 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15377 dw_loc_descr_ref op0, op1;
15378
15379 if (op_mode == VOIDmode)
15380 op_mode = GET_MODE (XEXP (rtl, 1));
15381 if (op_mode == VOIDmode)
15382 return NULL;
15383
15384 scalar_int_mode int_op_mode;
15385 if (dwarf_strict
15386 && dwarf_version < 5
15387 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15388 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15389 return NULL;
15390
15391 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15392 VAR_INIT_STATUS_INITIALIZED);
15393 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15394 VAR_INIT_STATUS_INITIALIZED);
15395
15396 if (op0 == NULL || op1 == NULL)
15397 return NULL;
15398
15399 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15400 {
15401 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15402 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15403
15404 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15405 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15406 }
15407 return compare_loc_descriptor (op, op0, op1);
15408 }
15409
15410 /* Return location descriptor for unsigned comparison OP RTL. */
15411
15412 static dw_loc_descr_ref
15413 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15414 machine_mode mem_mode)
15415 {
15416 dw_loc_descr_ref op0, op1;
15417
15418 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15419 if (test_op_mode == VOIDmode)
15420 test_op_mode = GET_MODE (XEXP (rtl, 1));
15421
15422 scalar_int_mode op_mode;
15423 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15424 return NULL;
15425
15426 if (dwarf_strict
15427 && dwarf_version < 5
15428 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15429 return NULL;
15430
15431 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15432 VAR_INIT_STATUS_INITIALIZED);
15433 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15434 VAR_INIT_STATUS_INITIALIZED);
15435
15436 if (op0 == NULL || op1 == NULL)
15437 return NULL;
15438
15439 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15440 {
15441 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15442 dw_loc_descr_ref last0, last1;
15443 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15444 ;
15445 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15446 ;
15447 if (CONST_INT_P (XEXP (rtl, 0)))
15448 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15449 /* deref_size zero extends, so no need to mask it again. */
15450 else if (last0->dw_loc_opc != DW_OP_deref_size
15451 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15452 {
15453 add_loc_descr (&op0, int_loc_descriptor (mask));
15454 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15455 }
15456 if (CONST_INT_P (XEXP (rtl, 1)))
15457 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15458 /* deref_size zero extends, so no need to mask it again. */
15459 else if (last1->dw_loc_opc != DW_OP_deref_size
15460 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15461 {
15462 add_loc_descr (&op1, int_loc_descriptor (mask));
15463 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15464 }
15465 }
15466 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15467 {
15468 HOST_WIDE_INT bias = 1;
15469 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15470 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15471 if (CONST_INT_P (XEXP (rtl, 1)))
15472 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15473 + INTVAL (XEXP (rtl, 1)));
15474 else
15475 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15476 bias, 0));
15477 }
15478 return compare_loc_descriptor (op, op0, op1);
15479 }
15480
15481 /* Return location descriptor for {U,S}{MIN,MAX}. */
15482
15483 static dw_loc_descr_ref
15484 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15485 machine_mode mem_mode)
15486 {
15487 enum dwarf_location_atom op;
15488 dw_loc_descr_ref op0, op1, ret;
15489 dw_loc_descr_ref bra_node, drop_node;
15490
15491 scalar_int_mode int_mode;
15492 if (dwarf_strict
15493 && dwarf_version < 5
15494 && (!is_a <scalar_int_mode> (mode, &int_mode)
15495 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15496 return NULL;
15497
15498 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15499 VAR_INIT_STATUS_INITIALIZED);
15500 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15501 VAR_INIT_STATUS_INITIALIZED);
15502
15503 if (op0 == NULL || op1 == NULL)
15504 return NULL;
15505
15506 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15507 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15508 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15509 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15510 {
15511 /* Checked by the caller. */
15512 int_mode = as_a <scalar_int_mode> (mode);
15513 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15514 {
15515 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15516 add_loc_descr (&op0, int_loc_descriptor (mask));
15517 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15518 add_loc_descr (&op1, int_loc_descriptor (mask));
15519 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15520 }
15521 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15522 {
15523 HOST_WIDE_INT bias = 1;
15524 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15525 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15526 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15527 }
15528 }
15529 else if (is_a <scalar_int_mode> (mode, &int_mode)
15530 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15531 {
15532 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15533 add_loc_descr (&op0, int_loc_descriptor (shift));
15534 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15535 add_loc_descr (&op1, int_loc_descriptor (shift));
15536 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15537 }
15538 else if (is_a <scalar_int_mode> (mode, &int_mode)
15539 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15540 {
15541 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15542 dw_loc_descr_ref cvt;
15543 if (type_die == NULL)
15544 return NULL;
15545 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15546 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15547 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15548 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15549 add_loc_descr (&op0, cvt);
15550 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15551 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15552 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15553 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15554 add_loc_descr (&op1, cvt);
15555 }
15556
15557 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15558 op = DW_OP_lt;
15559 else
15560 op = DW_OP_gt;
15561 ret = op0;
15562 add_loc_descr (&ret, op1);
15563 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15564 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15565 add_loc_descr (&ret, bra_node);
15566 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15567 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15568 add_loc_descr (&ret, drop_node);
15569 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15570 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15571 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15572 && is_a <scalar_int_mode> (mode, &int_mode)
15573 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15574 ret = convert_descriptor_to_mode (int_mode, ret);
15575 return ret;
15576 }
15577
15578 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15579 but after converting arguments to type_die, afterwards
15580 convert back to unsigned. */
15581
15582 static dw_loc_descr_ref
15583 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15584 scalar_int_mode mode, machine_mode mem_mode)
15585 {
15586 dw_loc_descr_ref cvt, op0, op1;
15587
15588 if (type_die == NULL)
15589 return NULL;
15590 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15591 VAR_INIT_STATUS_INITIALIZED);
15592 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15593 VAR_INIT_STATUS_INITIALIZED);
15594 if (op0 == NULL || op1 == NULL)
15595 return NULL;
15596 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15597 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15598 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15599 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15600 add_loc_descr (&op0, cvt);
15601 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15602 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15603 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15604 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15605 add_loc_descr (&op1, cvt);
15606 add_loc_descr (&op0, op1);
15607 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15608 return convert_descriptor_to_mode (mode, op0);
15609 }
15610
15611 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15612 const0 is DW_OP_lit0 or corresponding typed constant,
15613 const1 is DW_OP_lit1 or corresponding typed constant
15614 and constMSB is constant with just the MSB bit set
15615 for the mode):
15616 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15617 L1: const0 DW_OP_swap
15618 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15619 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15620 L3: DW_OP_drop
15621 L4: DW_OP_nop
15622
15623 CTZ is similar:
15624 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15625 L1: const0 DW_OP_swap
15626 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15627 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15628 L3: DW_OP_drop
15629 L4: DW_OP_nop
15630
15631 FFS is similar:
15632 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15633 L1: const1 DW_OP_swap
15634 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15635 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15636 L3: DW_OP_drop
15637 L4: DW_OP_nop */
15638
15639 static dw_loc_descr_ref
15640 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15641 machine_mode mem_mode)
15642 {
15643 dw_loc_descr_ref op0, ret, tmp;
15644 HOST_WIDE_INT valv;
15645 dw_loc_descr_ref l1jump, l1label;
15646 dw_loc_descr_ref l2jump, l2label;
15647 dw_loc_descr_ref l3jump, l3label;
15648 dw_loc_descr_ref l4jump, l4label;
15649 rtx msb;
15650
15651 if (GET_MODE (XEXP (rtl, 0)) != mode)
15652 return NULL;
15653
15654 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15655 VAR_INIT_STATUS_INITIALIZED);
15656 if (op0 == NULL)
15657 return NULL;
15658 ret = op0;
15659 if (GET_CODE (rtl) == CLZ)
15660 {
15661 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15662 valv = GET_MODE_BITSIZE (mode);
15663 }
15664 else if (GET_CODE (rtl) == FFS)
15665 valv = 0;
15666 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15667 valv = GET_MODE_BITSIZE (mode);
15668 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15669 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15670 add_loc_descr (&ret, l1jump);
15671 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15672 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15673 VAR_INIT_STATUS_INITIALIZED);
15674 if (tmp == NULL)
15675 return NULL;
15676 add_loc_descr (&ret, tmp);
15677 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15678 add_loc_descr (&ret, l4jump);
15679 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15680 ? const1_rtx : const0_rtx,
15681 mode, mem_mode,
15682 VAR_INIT_STATUS_INITIALIZED);
15683 if (l1label == NULL)
15684 return NULL;
15685 add_loc_descr (&ret, l1label);
15686 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15687 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15688 add_loc_descr (&ret, l2label);
15689 if (GET_CODE (rtl) != CLZ)
15690 msb = const1_rtx;
15691 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15692 msb = GEN_INT (HOST_WIDE_INT_1U
15693 << (GET_MODE_BITSIZE (mode) - 1));
15694 else
15695 msb = immed_wide_int_const
15696 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15697 GET_MODE_PRECISION (mode)), mode);
15698 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15699 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15700 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15701 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15702 else
15703 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15704 VAR_INIT_STATUS_INITIALIZED);
15705 if (tmp == NULL)
15706 return NULL;
15707 add_loc_descr (&ret, tmp);
15708 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15709 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15710 add_loc_descr (&ret, l3jump);
15711 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15712 VAR_INIT_STATUS_INITIALIZED);
15713 if (tmp == NULL)
15714 return NULL;
15715 add_loc_descr (&ret, tmp);
15716 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15717 ? DW_OP_shl : DW_OP_shr, 0, 0));
15718 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15719 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15720 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15721 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15722 add_loc_descr (&ret, l2jump);
15723 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15724 add_loc_descr (&ret, l3label);
15725 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15726 add_loc_descr (&ret, l4label);
15727 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15728 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15729 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15730 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15731 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15732 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15733 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15734 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15735 return ret;
15736 }
15737
15738 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15739 const1 is DW_OP_lit1 or corresponding typed constant):
15740 const0 DW_OP_swap
15741 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15742 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15743 L2: DW_OP_drop
15744
15745 PARITY is similar:
15746 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15747 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15748 L2: DW_OP_drop */
15749
15750 static dw_loc_descr_ref
15751 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15752 machine_mode mem_mode)
15753 {
15754 dw_loc_descr_ref op0, ret, tmp;
15755 dw_loc_descr_ref l1jump, l1label;
15756 dw_loc_descr_ref l2jump, l2label;
15757
15758 if (GET_MODE (XEXP (rtl, 0)) != mode)
15759 return NULL;
15760
15761 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15762 VAR_INIT_STATUS_INITIALIZED);
15763 if (op0 == NULL)
15764 return NULL;
15765 ret = op0;
15766 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15767 VAR_INIT_STATUS_INITIALIZED);
15768 if (tmp == NULL)
15769 return NULL;
15770 add_loc_descr (&ret, tmp);
15771 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15772 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15773 add_loc_descr (&ret, l1label);
15774 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15775 add_loc_descr (&ret, l2jump);
15776 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15777 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15778 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15779 VAR_INIT_STATUS_INITIALIZED);
15780 if (tmp == NULL)
15781 return NULL;
15782 add_loc_descr (&ret, tmp);
15783 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15784 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15785 ? DW_OP_plus : DW_OP_xor, 0, 0));
15786 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15787 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15788 VAR_INIT_STATUS_INITIALIZED);
15789 add_loc_descr (&ret, tmp);
15790 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15791 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15792 add_loc_descr (&ret, l1jump);
15793 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15794 add_loc_descr (&ret, l2label);
15795 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15796 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15797 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15798 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15799 return ret;
15800 }
15801
15802 /* BSWAP (constS is initial shift count, either 56 or 24):
15803 constS const0
15804 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15805 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15806 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15807 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15808 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15809
15810 static dw_loc_descr_ref
15811 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15812 machine_mode mem_mode)
15813 {
15814 dw_loc_descr_ref op0, ret, tmp;
15815 dw_loc_descr_ref l1jump, l1label;
15816 dw_loc_descr_ref l2jump, l2label;
15817
15818 if (BITS_PER_UNIT != 8
15819 || (GET_MODE_BITSIZE (mode) != 32
15820 && GET_MODE_BITSIZE (mode) != 64))
15821 return NULL;
15822
15823 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15824 VAR_INIT_STATUS_INITIALIZED);
15825 if (op0 == NULL)
15826 return NULL;
15827
15828 ret = op0;
15829 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15830 mode, mem_mode,
15831 VAR_INIT_STATUS_INITIALIZED);
15832 if (tmp == NULL)
15833 return NULL;
15834 add_loc_descr (&ret, tmp);
15835 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15836 VAR_INIT_STATUS_INITIALIZED);
15837 if (tmp == NULL)
15838 return NULL;
15839 add_loc_descr (&ret, tmp);
15840 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15841 add_loc_descr (&ret, l1label);
15842 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15843 mode, mem_mode,
15844 VAR_INIT_STATUS_INITIALIZED);
15845 add_loc_descr (&ret, tmp);
15846 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15847 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15848 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15849 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15850 VAR_INIT_STATUS_INITIALIZED);
15851 if (tmp == NULL)
15852 return NULL;
15853 add_loc_descr (&ret, tmp);
15854 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15855 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15856 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15857 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15858 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15859 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15860 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15861 VAR_INIT_STATUS_INITIALIZED);
15862 add_loc_descr (&ret, tmp);
15863 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15864 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15865 add_loc_descr (&ret, l2jump);
15866 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15867 VAR_INIT_STATUS_INITIALIZED);
15868 add_loc_descr (&ret, tmp);
15869 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15870 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15871 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15872 add_loc_descr (&ret, l1jump);
15873 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15874 add_loc_descr (&ret, l2label);
15875 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15876 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15877 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15878 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15879 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15880 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15881 return ret;
15882 }
15883
15884 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15885 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15886 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15887 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15888
15889 ROTATERT is similar:
15890 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15891 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15892 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15893
15894 static dw_loc_descr_ref
15895 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15896 machine_mode mem_mode)
15897 {
15898 rtx rtlop1 = XEXP (rtl, 1);
15899 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15900 int i;
15901
15902 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15903 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15904 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15905 VAR_INIT_STATUS_INITIALIZED);
15906 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15907 VAR_INIT_STATUS_INITIALIZED);
15908 if (op0 == NULL || op1 == NULL)
15909 return NULL;
15910 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15911 for (i = 0; i < 2; i++)
15912 {
15913 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15914 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15915 mode, mem_mode,
15916 VAR_INIT_STATUS_INITIALIZED);
15917 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15918 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15919 ? DW_OP_const4u
15920 : HOST_BITS_PER_WIDE_INT == 64
15921 ? DW_OP_const8u : DW_OP_constu,
15922 GET_MODE_MASK (mode), 0);
15923 else
15924 mask[i] = NULL;
15925 if (mask[i] == NULL)
15926 return NULL;
15927 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15928 }
15929 ret = op0;
15930 add_loc_descr (&ret, op1);
15931 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15932 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15933 if (GET_CODE (rtl) == ROTATERT)
15934 {
15935 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15936 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15937 GET_MODE_BITSIZE (mode), 0));
15938 }
15939 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15940 if (mask[0] != NULL)
15941 add_loc_descr (&ret, mask[0]);
15942 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15943 if (mask[1] != NULL)
15944 {
15945 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15946 add_loc_descr (&ret, mask[1]);
15947 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15948 }
15949 if (GET_CODE (rtl) == ROTATE)
15950 {
15951 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15952 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15953 GET_MODE_BITSIZE (mode), 0));
15954 }
15955 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15956 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15957 return ret;
15958 }
15959
15960 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15961 for DEBUG_PARAMETER_REF RTL. */
15962
15963 static dw_loc_descr_ref
15964 parameter_ref_descriptor (rtx rtl)
15965 {
15966 dw_loc_descr_ref ret;
15967 dw_die_ref ref;
15968
15969 if (dwarf_strict)
15970 return NULL;
15971 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15972 /* With LTO during LTRANS we get the late DIE that refers to the early
15973 DIE, thus we add another indirection here. This seems to confuse
15974 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15975 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15976 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15977 if (ref)
15978 {
15979 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15980 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15981 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15982 }
15983 else
15984 {
15985 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15986 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15987 }
15988 return ret;
15989 }
15990
15991 /* The following routine converts the RTL for a variable or parameter
15992 (resident in memory) into an equivalent Dwarf representation of a
15993 mechanism for getting the address of that same variable onto the top of a
15994 hypothetical "address evaluation" stack.
15995
15996 When creating memory location descriptors, we are effectively transforming
15997 the RTL for a memory-resident object into its Dwarf postfix expression
15998 equivalent. This routine recursively descends an RTL tree, turning
15999 it into Dwarf postfix code as it goes.
16000
16001 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
16002
16003 MEM_MODE is the mode of the memory reference, needed to handle some
16004 autoincrement addressing modes.
16005
16006 Return 0 if we can't represent the location. */
16007
16008 dw_loc_descr_ref
16009 mem_loc_descriptor (rtx rtl, machine_mode mode,
16010 machine_mode mem_mode,
16011 enum var_init_status initialized)
16012 {
16013 dw_loc_descr_ref mem_loc_result = NULL;
16014 enum dwarf_location_atom op;
16015 dw_loc_descr_ref op0, op1;
16016 rtx inner = NULL_RTX;
16017
16018 if (mode == VOIDmode)
16019 mode = GET_MODE (rtl);
16020
16021 /* Note that for a dynamically sized array, the location we will generate a
16022 description of here will be the lowest numbered location which is
16023 actually within the array. That's *not* necessarily the same as the
16024 zeroth element of the array. */
16025
16026 rtl = targetm.delegitimize_address (rtl);
16027
16028 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
16029 return NULL;
16030
16031 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
16032 switch (GET_CODE (rtl))
16033 {
16034 case POST_INC:
16035 case POST_DEC:
16036 case POST_MODIFY:
16037 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
16038
16039 case SUBREG:
16040 /* The case of a subreg may arise when we have a local (register)
16041 variable or a formal (register) parameter which doesn't quite fill
16042 up an entire register. For now, just assume that it is
16043 legitimate to make the Dwarf info refer to the whole register which
16044 contains the given subreg. */
16045 if (!subreg_lowpart_p (rtl))
16046 break;
16047 inner = SUBREG_REG (rtl);
16048 /* FALLTHRU */
16049 case TRUNCATE:
16050 if (inner == NULL_RTX)
16051 inner = XEXP (rtl, 0);
16052 if (is_a <scalar_int_mode> (mode, &int_mode)
16053 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16054 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16055 #ifdef POINTERS_EXTEND_UNSIGNED
16056 || (int_mode == Pmode && mem_mode != VOIDmode)
16057 #endif
16058 )
16059 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
16060 {
16061 mem_loc_result = mem_loc_descriptor (inner,
16062 inner_mode,
16063 mem_mode, initialized);
16064 break;
16065 }
16066 if (dwarf_strict && dwarf_version < 5)
16067 break;
16068 if (is_a <scalar_int_mode> (mode, &int_mode)
16069 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16070 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
16071 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16072 {
16073 dw_die_ref type_die;
16074 dw_loc_descr_ref cvt;
16075
16076 mem_loc_result = mem_loc_descriptor (inner,
16077 GET_MODE (inner),
16078 mem_mode, initialized);
16079 if (mem_loc_result == NULL)
16080 break;
16081 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16082 if (type_die == NULL)
16083 {
16084 mem_loc_result = NULL;
16085 break;
16086 }
16087 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16088 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16089 else
16090 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
16091 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16092 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16093 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16094 add_loc_descr (&mem_loc_result, cvt);
16095 if (is_a <scalar_int_mode> (mode, &int_mode)
16096 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16097 {
16098 /* Convert it to untyped afterwards. */
16099 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16100 add_loc_descr (&mem_loc_result, cvt);
16101 }
16102 }
16103 break;
16104
16105 case REG:
16106 if (!is_a <scalar_int_mode> (mode, &int_mode)
16107 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16108 && rtl != arg_pointer_rtx
16109 && rtl != frame_pointer_rtx
16110 #ifdef POINTERS_EXTEND_UNSIGNED
16111 && (int_mode != Pmode || mem_mode == VOIDmode)
16112 #endif
16113 ))
16114 {
16115 dw_die_ref type_die;
16116 unsigned int debugger_regnum;
16117
16118 if (dwarf_strict && dwarf_version < 5)
16119 break;
16120 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16121 break;
16122 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16123 if (type_die == NULL)
16124 break;
16125
16126 debugger_regnum = debugger_reg_number (rtl);
16127 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16128 break;
16129 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
16130 debugger_regnum, 0);
16131 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16132 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16133 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16134 break;
16135 }
16136 /* Whenever a register number forms a part of the description of the
16137 method for calculating the (dynamic) address of a memory resident
16138 object, DWARF rules require the register number be referred to as
16139 a "base register". This distinction is not based in any way upon
16140 what category of register the hardware believes the given register
16141 belongs to. This is strictly DWARF terminology we're dealing with
16142 here. Note that in cases where the location of a memory-resident
16143 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16144 OP_CONST (0)) the actual DWARF location descriptor that we generate
16145 may just be OP_BASEREG (basereg). This may look deceptively like
16146 the object in question was allocated to a register (rather than in
16147 memory) so DWARF consumers need to be aware of the subtle
16148 distinction between OP_REG and OP_BASEREG. */
16149 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16150 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16151 else if (stack_realign_drap
16152 && crtl->drap_reg
16153 && crtl->args.internal_arg_pointer == rtl
16154 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16155 {
16156 /* If RTL is internal_arg_pointer, which has been optimized
16157 out, use DRAP instead. */
16158 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16159 VAR_INIT_STATUS_INITIALIZED);
16160 }
16161 break;
16162
16163 case SIGN_EXTEND:
16164 case ZERO_EXTEND:
16165 if (!is_a <scalar_int_mode> (mode, &int_mode)
16166 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16167 break;
16168 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16169 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16170 if (op0 == 0)
16171 break;
16172 else if (GET_CODE (rtl) == ZERO_EXTEND
16173 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16174 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16175 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16176 to expand zero extend as two shifts instead of
16177 masking. */
16178 && GET_MODE_SIZE (inner_mode) <= 4)
16179 {
16180 mem_loc_result = op0;
16181 add_loc_descr (&mem_loc_result,
16182 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16183 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16184 }
16185 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16186 {
16187 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16188 shift *= BITS_PER_UNIT;
16189 if (GET_CODE (rtl) == SIGN_EXTEND)
16190 op = DW_OP_shra;
16191 else
16192 op = DW_OP_shr;
16193 mem_loc_result = op0;
16194 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16195 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16196 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16197 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16198 }
16199 else if (!dwarf_strict || dwarf_version >= 5)
16200 {
16201 dw_die_ref type_die1, type_die2;
16202 dw_loc_descr_ref cvt;
16203
16204 type_die1 = base_type_for_mode (inner_mode,
16205 GET_CODE (rtl) == ZERO_EXTEND);
16206 if (type_die1 == NULL)
16207 break;
16208 type_die2 = base_type_for_mode (int_mode, 1);
16209 if (type_die2 == NULL)
16210 break;
16211 mem_loc_result = op0;
16212 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16213 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16214 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16215 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16216 add_loc_descr (&mem_loc_result, cvt);
16217 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16218 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16219 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16220 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16221 add_loc_descr (&mem_loc_result, cvt);
16222 }
16223 break;
16224
16225 case MEM:
16226 {
16227 rtx new_rtl = avoid_constant_pool_reference (rtl);
16228 if (new_rtl != rtl)
16229 {
16230 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16231 initialized);
16232 if (mem_loc_result != NULL)
16233 return mem_loc_result;
16234 }
16235 }
16236 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16237 get_address_mode (rtl), mode,
16238 VAR_INIT_STATUS_INITIALIZED);
16239 if (mem_loc_result == NULL)
16240 mem_loc_result = tls_mem_loc_descriptor (rtl);
16241 if (mem_loc_result != NULL)
16242 {
16243 if (!is_a <scalar_int_mode> (mode, &int_mode)
16244 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16245 {
16246 dw_die_ref type_die;
16247 dw_loc_descr_ref deref;
16248 HOST_WIDE_INT size;
16249
16250 if (dwarf_strict && dwarf_version < 5)
16251 return NULL;
16252 if (!GET_MODE_SIZE (mode).is_constant (&size))
16253 return NULL;
16254 type_die
16255 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16256 if (type_die == NULL)
16257 return NULL;
16258 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16259 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16260 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16261 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16262 add_loc_descr (&mem_loc_result, deref);
16263 }
16264 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16265 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16266 else
16267 add_loc_descr (&mem_loc_result,
16268 new_loc_descr (DW_OP_deref_size,
16269 GET_MODE_SIZE (int_mode), 0));
16270 }
16271 break;
16272
16273 case LO_SUM:
16274 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16275
16276 case LABEL_REF:
16277 /* Some ports can transform a symbol ref into a label ref, because
16278 the symbol ref is too far away and has to be dumped into a constant
16279 pool. */
16280 case CONST:
16281 case SYMBOL_REF:
16282 case UNSPEC:
16283 if (!is_a <scalar_int_mode> (mode, &int_mode)
16284 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16285 #ifdef POINTERS_EXTEND_UNSIGNED
16286 && (int_mode != Pmode || mem_mode == VOIDmode)
16287 #endif
16288 ))
16289 break;
16290
16291 if (GET_CODE (rtl) == UNSPEC)
16292 {
16293 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16294 can't express it in the debug info. This can happen e.g. with some
16295 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16296 approves. */
16297 bool not_ok = false;
16298 subrtx_var_iterator::array_type array;
16299 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16300 if (*iter != rtl && !CONSTANT_P (*iter))
16301 {
16302 not_ok = true;
16303 break;
16304 }
16305
16306 if (not_ok)
16307 break;
16308
16309 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16310 if (!const_ok_for_output_1 (*iter))
16311 {
16312 not_ok = true;
16313 break;
16314 }
16315
16316 if (not_ok)
16317 break;
16318
16319 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16320 goto symref;
16321 }
16322
16323 if (GET_CODE (rtl) == SYMBOL_REF
16324 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16325 {
16326 dw_loc_descr_ref temp;
16327
16328 /* If this is not defined, we have no way to emit the data. */
16329 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16330 break;
16331
16332 temp = new_addr_loc_descr (rtl, dtprel_true);
16333
16334 /* We check for DWARF 5 here because gdb did not implement
16335 DW_OP_form_tls_address until after 7.12. */
16336 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16337 ? DW_OP_form_tls_address
16338 : DW_OP_GNU_push_tls_address),
16339 0, 0);
16340 add_loc_descr (&mem_loc_result, temp);
16341
16342 break;
16343 }
16344
16345 if (!const_ok_for_output (rtl))
16346 {
16347 if (GET_CODE (rtl) == CONST)
16348 switch (GET_CODE (XEXP (rtl, 0)))
16349 {
16350 case NOT:
16351 op = DW_OP_not;
16352 goto try_const_unop;
16353 case NEG:
16354 op = DW_OP_neg;
16355 goto try_const_unop;
16356 try_const_unop:
16357 rtx arg;
16358 arg = XEXP (XEXP (rtl, 0), 0);
16359 if (!CONSTANT_P (arg))
16360 arg = gen_rtx_CONST (int_mode, arg);
16361 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16362 initialized);
16363 if (op0)
16364 {
16365 mem_loc_result = op0;
16366 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16367 }
16368 break;
16369 default:
16370 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16371 mem_mode, initialized);
16372 break;
16373 }
16374 break;
16375 }
16376
16377 symref:
16378 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16379 vec_safe_push (used_rtx_array, rtl);
16380 break;
16381
16382 case CONCAT:
16383 case CONCATN:
16384 case VAR_LOCATION:
16385 case DEBUG_IMPLICIT_PTR:
16386 expansion_failed (NULL_TREE, rtl,
16387 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16388 return 0;
16389
16390 case ENTRY_VALUE:
16391 if (dwarf_strict && dwarf_version < 5)
16392 return NULL;
16393 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16394 {
16395 if (!is_a <scalar_int_mode> (mode, &int_mode)
16396 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16397 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16398 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16399 else
16400 {
16401 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16402 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16403 return NULL;
16404 op0 = one_reg_loc_descriptor (debugger_regnum,
16405 VAR_INIT_STATUS_INITIALIZED);
16406 }
16407 }
16408 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16409 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16410 {
16411 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16412 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16413 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16414 return NULL;
16415 }
16416 else
16417 gcc_unreachable ();
16418 if (op0 == NULL)
16419 return NULL;
16420 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16421 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16422 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16423 break;
16424
16425 case DEBUG_PARAMETER_REF:
16426 mem_loc_result = parameter_ref_descriptor (rtl);
16427 break;
16428
16429 case PRE_MODIFY:
16430 /* Extract the PLUS expression nested inside and fall into
16431 PLUS code below. */
16432 rtl = XEXP (rtl, 1);
16433 goto plus;
16434
16435 case PRE_INC:
16436 case PRE_DEC:
16437 /* Turn these into a PLUS expression and fall into the PLUS code
16438 below. */
16439 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16440 gen_int_mode (GET_CODE (rtl) == PRE_INC
16441 ? GET_MODE_UNIT_SIZE (mem_mode)
16442 : -GET_MODE_UNIT_SIZE (mem_mode),
16443 mode));
16444
16445 /* fall through */
16446
16447 case PLUS:
16448 plus:
16449 if (is_based_loc (rtl)
16450 && is_a <scalar_int_mode> (mode, &int_mode)
16451 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16452 || XEXP (rtl, 0) == arg_pointer_rtx
16453 || XEXP (rtl, 0) == frame_pointer_rtx))
16454 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16455 INTVAL (XEXP (rtl, 1)),
16456 VAR_INIT_STATUS_INITIALIZED);
16457 else
16458 {
16459 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16460 VAR_INIT_STATUS_INITIALIZED);
16461 if (mem_loc_result == 0)
16462 break;
16463
16464 if (CONST_INT_P (XEXP (rtl, 1))
16465 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16466 <= DWARF2_ADDR_SIZE))
16467 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16468 else
16469 {
16470 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16471 VAR_INIT_STATUS_INITIALIZED);
16472 if (op1 == 0)
16473 return NULL;
16474 add_loc_descr (&mem_loc_result, op1);
16475 add_loc_descr (&mem_loc_result,
16476 new_loc_descr (DW_OP_plus, 0, 0));
16477 }
16478 }
16479 break;
16480
16481 /* If a pseudo-reg is optimized away, it is possible for it to
16482 be replaced with a MEM containing a multiply or shift. */
16483 case MINUS:
16484 op = DW_OP_minus;
16485 goto do_binop;
16486
16487 case MULT:
16488 op = DW_OP_mul;
16489 goto do_binop;
16490
16491 case DIV:
16492 if ((!dwarf_strict || dwarf_version >= 5)
16493 && is_a <scalar_int_mode> (mode, &int_mode)
16494 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16495 {
16496 mem_loc_result = typed_binop (DW_OP_div, rtl,
16497 base_type_for_mode (mode, 0),
16498 int_mode, mem_mode);
16499 break;
16500 }
16501 op = DW_OP_div;
16502 goto do_binop;
16503
16504 case UMOD:
16505 op = DW_OP_mod;
16506 goto do_binop;
16507
16508 case ASHIFT:
16509 op = DW_OP_shl;
16510 goto do_shift;
16511
16512 case ASHIFTRT:
16513 op = DW_OP_shra;
16514 goto do_shift;
16515
16516 case LSHIFTRT:
16517 op = DW_OP_shr;
16518 goto do_shift;
16519
16520 do_shift:
16521 if (!is_a <scalar_int_mode> (mode, &int_mode))
16522 break;
16523 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16524 VAR_INIT_STATUS_INITIALIZED);
16525 {
16526 rtx rtlop1 = XEXP (rtl, 1);
16527 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16528 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16529 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16530 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16531 VAR_INIT_STATUS_INITIALIZED);
16532 }
16533
16534 if (op0 == 0 || op1 == 0)
16535 break;
16536
16537 mem_loc_result = op0;
16538 add_loc_descr (&mem_loc_result, op1);
16539 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16540 break;
16541
16542 case AND:
16543 op = DW_OP_and;
16544 goto do_binop;
16545
16546 case IOR:
16547 op = DW_OP_or;
16548 goto do_binop;
16549
16550 case XOR:
16551 op = DW_OP_xor;
16552 goto do_binop;
16553
16554 do_binop:
16555 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16556 VAR_INIT_STATUS_INITIALIZED);
16557 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16558 {
16559 if (op0 == 0)
16560 break;
16561 mem_loc_result = op0;
16562 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16563 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16564 break;
16565 }
16566 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16567 VAR_INIT_STATUS_INITIALIZED);
16568
16569 if (op0 == 0 || op1 == 0)
16570 break;
16571
16572 mem_loc_result = op0;
16573 add_loc_descr (&mem_loc_result, op1);
16574 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16575 break;
16576
16577 case MOD:
16578 if ((!dwarf_strict || dwarf_version >= 5)
16579 && is_a <scalar_int_mode> (mode, &int_mode)
16580 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16581 {
16582 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16583 base_type_for_mode (mode, 0),
16584 int_mode, mem_mode);
16585 break;
16586 }
16587
16588 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16589 VAR_INIT_STATUS_INITIALIZED);
16590 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16591 VAR_INIT_STATUS_INITIALIZED);
16592
16593 if (op0 == 0 || op1 == 0)
16594 break;
16595
16596 mem_loc_result = op0;
16597 add_loc_descr (&mem_loc_result, op1);
16598 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16599 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16600 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16601 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16602 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16603 break;
16604
16605 case UDIV:
16606 if ((!dwarf_strict || dwarf_version >= 5)
16607 && is_a <scalar_int_mode> (mode, &int_mode))
16608 {
16609 /* We can use a signed divide if the sign bit is not set. */
16610 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16611 {
16612 op = DW_OP_div;
16613 goto do_binop;
16614 }
16615
16616 mem_loc_result = typed_binop (DW_OP_div, rtl,
16617 base_type_for_mode (int_mode, 1),
16618 int_mode, mem_mode);
16619 }
16620 break;
16621
16622 case NOT:
16623 op = DW_OP_not;
16624 goto do_unop;
16625
16626 case ABS:
16627 op = DW_OP_abs;
16628 goto do_unop;
16629
16630 case NEG:
16631 op = DW_OP_neg;
16632 goto do_unop;
16633
16634 do_unop:
16635 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16636 VAR_INIT_STATUS_INITIALIZED);
16637
16638 if (op0 == 0)
16639 break;
16640
16641 mem_loc_result = op0;
16642 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16643 break;
16644
16645 case CONST_INT:
16646 if (!is_a <scalar_int_mode> (mode, &int_mode)
16647 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16648 #ifdef POINTERS_EXTEND_UNSIGNED
16649 || (int_mode == Pmode
16650 && mem_mode != VOIDmode
16651 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16652 #endif
16653 )
16654 {
16655 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16656 break;
16657 }
16658 if ((!dwarf_strict || dwarf_version >= 5)
16659 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16660 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16661 {
16662 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16663 scalar_int_mode amode;
16664 if (type_die == NULL)
16665 return NULL;
16666 if (INTVAL (rtl) >= 0
16667 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16668 .exists (&amode))
16669 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16670 /* const DW_OP_convert <XXX> vs.
16671 DW_OP_const_type <XXX, 1, const>. */
16672 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16673 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16674 {
16675 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16676 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16677 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16678 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16679 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16680 add_loc_descr (&mem_loc_result, op0);
16681 return mem_loc_result;
16682 }
16683 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16684 INTVAL (rtl));
16685 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16686 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16687 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16688 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16689 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16690 else
16691 {
16692 mem_loc_result->dw_loc_oprnd2.val_class
16693 = dw_val_class_const_double;
16694 mem_loc_result->dw_loc_oprnd2.v.val_double
16695 = double_int::from_shwi (INTVAL (rtl));
16696 }
16697 }
16698 break;
16699
16700 case CONST_DOUBLE:
16701 if (!dwarf_strict || dwarf_version >= 5)
16702 {
16703 dw_die_ref type_die;
16704
16705 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16706 CONST_DOUBLE rtx could represent either a large integer
16707 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16708 the value is always a floating point constant.
16709
16710 When it is an integer, a CONST_DOUBLE is used whenever
16711 the constant requires 2 HWIs to be adequately represented.
16712 We output CONST_DOUBLEs as blocks. */
16713 if (mode == VOIDmode
16714 || (GET_MODE (rtl) == VOIDmode
16715 && maybe_ne (GET_MODE_BITSIZE (mode),
16716 HOST_BITS_PER_DOUBLE_INT)))
16717 break;
16718 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16719 if (type_die == NULL)
16720 return NULL;
16721 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16722 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16723 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16724 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16725 #if TARGET_SUPPORTS_WIDE_INT == 0
16726 if (!SCALAR_FLOAT_MODE_P (mode))
16727 {
16728 mem_loc_result->dw_loc_oprnd2.val_class
16729 = dw_val_class_const_double;
16730 mem_loc_result->dw_loc_oprnd2.v.val_double
16731 = rtx_to_double_int (rtl);
16732 }
16733 else
16734 #endif
16735 {
16736 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16737 unsigned int length = GET_MODE_SIZE (float_mode);
16738 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16739 unsigned int elt_size = insert_float (rtl, array);
16740
16741 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16742 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16743 = length / elt_size;
16744 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16745 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16746 }
16747 }
16748 break;
16749
16750 case CONST_WIDE_INT:
16751 if (!dwarf_strict || dwarf_version >= 5)
16752 {
16753 dw_die_ref type_die;
16754
16755 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16756 if (type_die == NULL)
16757 return NULL;
16758 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16759 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16760 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16761 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16762 mem_loc_result->dw_loc_oprnd2.val_class
16763 = dw_val_class_wide_int;
16764 mem_loc_result->dw_loc_oprnd2.v.val_wide
16765 = alloc_dw_wide_int (rtx_mode_t (rtl, mode));
16766 }
16767 break;
16768
16769 case CONST_POLY_INT:
16770 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16771 break;
16772
16773 case EQ:
16774 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16775 break;
16776
16777 case GE:
16778 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16779 break;
16780
16781 case GT:
16782 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16783 break;
16784
16785 case LE:
16786 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16787 break;
16788
16789 case LT:
16790 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16791 break;
16792
16793 case NE:
16794 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16795 break;
16796
16797 case GEU:
16798 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16799 break;
16800
16801 case GTU:
16802 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16803 break;
16804
16805 case LEU:
16806 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16807 break;
16808
16809 case LTU:
16810 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16811 break;
16812
16813 case UMIN:
16814 case UMAX:
16815 if (!SCALAR_INT_MODE_P (mode))
16816 break;
16817 /* FALLTHRU */
16818 case SMIN:
16819 case SMAX:
16820 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16821 break;
16822
16823 case ZERO_EXTRACT:
16824 case SIGN_EXTRACT:
16825 if (CONST_INT_P (XEXP (rtl, 1))
16826 && CONST_INT_P (XEXP (rtl, 2))
16827 && is_a <scalar_int_mode> (mode, &int_mode)
16828 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16829 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16830 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16831 && ((unsigned) INTVAL (XEXP (rtl, 1))
16832 + (unsigned) INTVAL (XEXP (rtl, 2))
16833 <= GET_MODE_BITSIZE (int_mode)))
16834 {
16835 int shift, size;
16836 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16837 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16838 if (op0 == 0)
16839 break;
16840 if (GET_CODE (rtl) == SIGN_EXTRACT)
16841 op = DW_OP_shra;
16842 else
16843 op = DW_OP_shr;
16844 mem_loc_result = op0;
16845 size = INTVAL (XEXP (rtl, 1));
16846 shift = INTVAL (XEXP (rtl, 2));
16847 if (BITS_BIG_ENDIAN)
16848 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16849 if (shift + size != (int) DWARF2_ADDR_SIZE)
16850 {
16851 add_loc_descr (&mem_loc_result,
16852 int_loc_descriptor (DWARF2_ADDR_SIZE
16853 - shift - size));
16854 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16855 }
16856 if (size != (int) DWARF2_ADDR_SIZE)
16857 {
16858 add_loc_descr (&mem_loc_result,
16859 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16860 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16861 }
16862 }
16863 break;
16864
16865 case IF_THEN_ELSE:
16866 {
16867 dw_loc_descr_ref op2, bra_node, drop_node;
16868 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16869 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16870 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16871 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16872 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16873 VAR_INIT_STATUS_INITIALIZED);
16874 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16875 VAR_INIT_STATUS_INITIALIZED);
16876 if (op0 == NULL || op1 == NULL || op2 == NULL)
16877 break;
16878
16879 mem_loc_result = op1;
16880 add_loc_descr (&mem_loc_result, op2);
16881 add_loc_descr (&mem_loc_result, op0);
16882 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16883 add_loc_descr (&mem_loc_result, bra_node);
16884 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16885 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16886 add_loc_descr (&mem_loc_result, drop_node);
16887 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16888 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16889 }
16890 break;
16891
16892 case FLOAT_EXTEND:
16893 case FLOAT_TRUNCATE:
16894 case FLOAT:
16895 case UNSIGNED_FLOAT:
16896 case FIX:
16897 case UNSIGNED_FIX:
16898 if (!dwarf_strict || dwarf_version >= 5)
16899 {
16900 dw_die_ref type_die;
16901 dw_loc_descr_ref cvt;
16902
16903 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16904 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16905 if (op0 == NULL)
16906 break;
16907 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16908 && (GET_CODE (rtl) == FLOAT
16909 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16910 {
16911 type_die = base_type_for_mode (int_mode,
16912 GET_CODE (rtl) == UNSIGNED_FLOAT);
16913 if (type_die == NULL)
16914 break;
16915 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16916 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16917 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16918 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16919 add_loc_descr (&op0, cvt);
16920 }
16921 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16922 if (type_die == NULL)
16923 break;
16924 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16925 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16926 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16927 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16928 add_loc_descr (&op0, cvt);
16929 if (is_a <scalar_int_mode> (mode, &int_mode)
16930 && (GET_CODE (rtl) == FIX
16931 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16932 {
16933 op0 = convert_descriptor_to_mode (int_mode, op0);
16934 if (op0 == NULL)
16935 break;
16936 }
16937 mem_loc_result = op0;
16938 }
16939 break;
16940
16941 case CLZ:
16942 case CTZ:
16943 case FFS:
16944 if (is_a <scalar_int_mode> (mode, &int_mode))
16945 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16946 break;
16947
16948 case POPCOUNT:
16949 case PARITY:
16950 if (is_a <scalar_int_mode> (mode, &int_mode))
16951 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16952 break;
16953
16954 case BSWAP:
16955 if (is_a <scalar_int_mode> (mode, &int_mode))
16956 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16957 break;
16958
16959 case ROTATE:
16960 case ROTATERT:
16961 if (is_a <scalar_int_mode> (mode, &int_mode))
16962 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16963 break;
16964
16965 case COMPARE:
16966 /* In theory, we could implement the above. */
16967 /* DWARF cannot represent the unsigned compare operations
16968 natively. */
16969 case SS_MULT:
16970 case US_MULT:
16971 case SS_DIV:
16972 case US_DIV:
16973 case SS_PLUS:
16974 case US_PLUS:
16975 case SS_MINUS:
16976 case US_MINUS:
16977 case SS_NEG:
16978 case US_NEG:
16979 case SS_ABS:
16980 case SS_ASHIFT:
16981 case US_ASHIFT:
16982 case SS_TRUNCATE:
16983 case US_TRUNCATE:
16984 case UNORDERED:
16985 case ORDERED:
16986 case UNEQ:
16987 case UNGE:
16988 case UNGT:
16989 case UNLE:
16990 case UNLT:
16991 case LTGT:
16992 case FRACT_CONVERT:
16993 case UNSIGNED_FRACT_CONVERT:
16994 case SAT_FRACT:
16995 case UNSIGNED_SAT_FRACT:
16996 case SQRT:
16997 case ASM_OPERANDS:
16998 case VEC_MERGE:
16999 case VEC_SELECT:
17000 case VEC_CONCAT:
17001 case VEC_DUPLICATE:
17002 case VEC_SERIES:
17003 case HIGH:
17004 case FMA:
17005 case STRICT_LOW_PART:
17006 case CONST_VECTOR:
17007 case CONST_FIXED:
17008 case CLRSB:
17009 case CLOBBER:
17010 case SMUL_HIGHPART:
17011 case UMUL_HIGHPART:
17012 case BITREVERSE:
17013 case COPYSIGN:
17014 break;
17015
17016 case CONST_STRING:
17017 resolve_one_addr (&rtl);
17018 goto symref;
17019
17020 /* RTL sequences inside PARALLEL record a series of DWARF operations for
17021 the expression. An UNSPEC rtx represents a raw DWARF operation,
17022 new_loc_descr is called for it to build the operation directly.
17023 Otherwise mem_loc_descriptor is called recursively. */
17024 case PARALLEL:
17025 {
17026 int index = 0;
17027 dw_loc_descr_ref exp_result = NULL;
17028
17029 for (; index < XVECLEN (rtl, 0); index++)
17030 {
17031 rtx elem = XVECEXP (rtl, 0, index);
17032 if (GET_CODE (elem) == UNSPEC)
17033 {
17034 /* Each DWARF operation UNSPEC contain two operands, if
17035 one operand is not used for the operation, const0_rtx is
17036 passed. */
17037 gcc_assert (XVECLEN (elem, 0) == 2);
17038
17039 HOST_WIDE_INT dw_op = XINT (elem, 1);
17040 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
17041 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
17042 exp_result
17043 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
17044 oprnd2);
17045 }
17046 else
17047 exp_result
17048 = mem_loc_descriptor (elem, mode, mem_mode,
17049 VAR_INIT_STATUS_INITIALIZED);
17050
17051 if (!mem_loc_result)
17052 mem_loc_result = exp_result;
17053 else
17054 add_loc_descr (&mem_loc_result, exp_result);
17055 }
17056
17057 break;
17058 }
17059
17060 default:
17061 if (flag_checking)
17062 {
17063 print_rtl (stderr, rtl);
17064 gcc_unreachable ();
17065 }
17066 break;
17067 }
17068
17069 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17070 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17071
17072 return mem_loc_result;
17073 }
17074
17075 /* Return a descriptor that describes the concatenation of two locations.
17076 This is typically a complex variable. */
17077
17078 static dw_loc_descr_ref
17079 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
17080 {
17081 /* At present we only track constant-sized pieces. */
17082 unsigned int size0, size1;
17083 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
17084 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
17085 return 0;
17086
17087 dw_loc_descr_ref cc_loc_result = NULL;
17088 dw_loc_descr_ref x0_ref
17089 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17090 dw_loc_descr_ref x1_ref
17091 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17092
17093 if (x0_ref == 0 || x1_ref == 0)
17094 return 0;
17095
17096 cc_loc_result = x0_ref;
17097 add_loc_descr_op_piece (&cc_loc_result, size0);
17098
17099 add_loc_descr (&cc_loc_result, x1_ref);
17100 add_loc_descr_op_piece (&cc_loc_result, size1);
17101
17102 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17103 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17104
17105 return cc_loc_result;
17106 }
17107
17108 /* Return a descriptor that describes the concatenation of N
17109 locations. */
17110
17111 static dw_loc_descr_ref
17112 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17113 {
17114 unsigned int i;
17115 dw_loc_descr_ref cc_loc_result = NULL;
17116 unsigned int n = XVECLEN (concatn, 0);
17117 unsigned int size;
17118
17119 for (i = 0; i < n; ++i)
17120 {
17121 dw_loc_descr_ref ref;
17122 rtx x = XVECEXP (concatn, 0, i);
17123
17124 /* At present we only track constant-sized pieces. */
17125 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
17126 return NULL;
17127
17128 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17129 if (ref == NULL)
17130 return NULL;
17131
17132 add_loc_descr (&cc_loc_result, ref);
17133 add_loc_descr_op_piece (&cc_loc_result, size);
17134 }
17135
17136 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17137 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17138
17139 return cc_loc_result;
17140 }
17141
17142 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17143 for DEBUG_IMPLICIT_PTR RTL. */
17144
17145 static dw_loc_descr_ref
17146 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17147 {
17148 dw_loc_descr_ref ret;
17149 dw_die_ref ref;
17150
17151 if (dwarf_strict && dwarf_version < 5)
17152 return NULL;
17153 gcc_assert (VAR_P (DEBUG_IMPLICIT_PTR_DECL (rtl))
17154 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17155 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17156 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17157 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17158 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17159 if (ref)
17160 {
17161 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17162 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17163 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17164 }
17165 else
17166 {
17167 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17168 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17169 }
17170 return ret;
17171 }
17172
17173 /* Output a proper Dwarf location descriptor for a variable or parameter
17174 which is either allocated in a register or in a memory location. For a
17175 register, we just generate an OP_REG and the register number. For a
17176 memory location we provide a Dwarf postfix expression describing how to
17177 generate the (dynamic) address of the object onto the address stack.
17178
17179 MODE is mode of the decl if this loc_descriptor is going to be used in
17180 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17181 allowed, VOIDmode otherwise.
17182
17183 If we don't know how to describe it, return 0. */
17184
17185 static dw_loc_descr_ref
17186 loc_descriptor (rtx rtl, machine_mode mode,
17187 enum var_init_status initialized)
17188 {
17189 dw_loc_descr_ref loc_result = NULL;
17190 scalar_int_mode int_mode;
17191
17192 switch (GET_CODE (rtl))
17193 {
17194 case SUBREG:
17195 /* The case of a subreg may arise when we have a local (register)
17196 variable or a formal (register) parameter which doesn't quite fill
17197 up an entire register. For now, just assume that it is
17198 legitimate to make the Dwarf info refer to the whole register which
17199 contains the given subreg. */
17200 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17201 loc_result = loc_descriptor (SUBREG_REG (rtl),
17202 GET_MODE (SUBREG_REG (rtl)), initialized);
17203 else
17204 goto do_default;
17205 break;
17206
17207 case REG:
17208 loc_result = reg_loc_descriptor (rtl, initialized);
17209 break;
17210
17211 case MEM:
17212 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17213 GET_MODE (rtl), initialized);
17214 if (loc_result == NULL)
17215 loc_result = tls_mem_loc_descriptor (rtl);
17216 if (loc_result == NULL)
17217 {
17218 rtx new_rtl = avoid_constant_pool_reference (rtl);
17219 if (new_rtl != rtl)
17220 loc_result = loc_descriptor (new_rtl, mode, initialized);
17221 }
17222 break;
17223
17224 case CONCAT:
17225 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17226 initialized);
17227 break;
17228
17229 case CONCATN:
17230 loc_result = concatn_loc_descriptor (rtl, initialized);
17231 break;
17232
17233 case VAR_LOCATION:
17234 /* Single part. */
17235 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17236 {
17237 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17238 if (GET_CODE (loc) == EXPR_LIST)
17239 loc = XEXP (loc, 0);
17240 loc_result = loc_descriptor (loc, mode, initialized);
17241 break;
17242 }
17243
17244 rtl = XEXP (rtl, 1);
17245 /* FALLTHRU */
17246
17247 case PARALLEL:
17248 {
17249 rtvec par_elems = XVEC (rtl, 0);
17250 int num_elem = GET_NUM_ELEM (par_elems);
17251 machine_mode mode;
17252 int i, size;
17253
17254 /* Create the first one, so we have something to add to. */
17255 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17256 VOIDmode, initialized);
17257 if (loc_result == NULL)
17258 return NULL;
17259 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17260 /* At present we only track constant-sized pieces. */
17261 if (!GET_MODE_SIZE (mode).is_constant (&size))
17262 return NULL;
17263 add_loc_descr_op_piece (&loc_result, size);
17264 for (i = 1; i < num_elem; i++)
17265 {
17266 dw_loc_descr_ref temp;
17267
17268 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17269 VOIDmode, initialized);
17270 if (temp == NULL)
17271 return NULL;
17272 add_loc_descr (&loc_result, temp);
17273 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17274 /* At present we only track constant-sized pieces. */
17275 if (!GET_MODE_SIZE (mode).is_constant (&size))
17276 return NULL;
17277 add_loc_descr_op_piece (&loc_result, size);
17278 }
17279 }
17280 break;
17281
17282 case CONST_INT:
17283 if (mode != VOIDmode && mode != BLKmode)
17284 {
17285 int_mode = as_a <scalar_int_mode> (mode);
17286 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17287 INTVAL (rtl));
17288 }
17289 break;
17290
17291 case CONST_DOUBLE:
17292 if (mode == VOIDmode)
17293 mode = GET_MODE (rtl);
17294
17295 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17296 {
17297 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17298
17299 /* Note that a CONST_DOUBLE rtx could represent either an integer
17300 or a floating-point constant. A CONST_DOUBLE is used whenever
17301 the constant requires more than one word in order to be
17302 adequately represented. We output CONST_DOUBLEs as blocks. */
17303 scalar_mode smode = as_a <scalar_mode> (mode);
17304 loc_result = new_loc_descr (DW_OP_implicit_value,
17305 GET_MODE_SIZE (smode), 0);
17306 #if TARGET_SUPPORTS_WIDE_INT == 0
17307 if (!SCALAR_FLOAT_MODE_P (smode))
17308 {
17309 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17310 loc_result->dw_loc_oprnd2.v.val_double
17311 = rtx_to_double_int (rtl);
17312 }
17313 else
17314 #endif
17315 {
17316 unsigned int length = GET_MODE_SIZE (smode);
17317 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17318 unsigned int elt_size = insert_float (rtl, array);
17319
17320 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17321 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17322 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17323 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17324 }
17325 }
17326 break;
17327
17328 case CONST_WIDE_INT:
17329 if (mode == VOIDmode)
17330 mode = GET_MODE (rtl);
17331
17332 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17333 {
17334 int_mode = as_a <scalar_int_mode> (mode);
17335 loc_result = new_loc_descr (DW_OP_implicit_value,
17336 GET_MODE_SIZE (int_mode), 0);
17337 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17338 loc_result->dw_loc_oprnd2.v.val_wide
17339 = alloc_dw_wide_int (rtx_mode_t (rtl, int_mode));
17340 }
17341 break;
17342
17343 case CONST_VECTOR:
17344 if (mode == VOIDmode)
17345 mode = GET_MODE (rtl);
17346
17347 if (mode != VOIDmode
17348 /* The combination of a length and byte elt_size doesn't extend
17349 naturally to boolean vectors, where several elements are packed
17350 into the same byte. */
17351 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17352 && (dwarf_version >= 4 || !dwarf_strict))
17353 {
17354 unsigned int length;
17355 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17356 return NULL;
17357
17358 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17359 unsigned char *array
17360 = ggc_vec_alloc<unsigned char> (length * elt_size);
17361 unsigned int i;
17362 unsigned char *p;
17363 machine_mode imode = GET_MODE_INNER (mode);
17364
17365 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17366 switch (GET_MODE_CLASS (mode))
17367 {
17368 case MODE_VECTOR_INT:
17369 for (i = 0, p = array; i < length; i++, p += elt_size)
17370 {
17371 rtx elt = CONST_VECTOR_ELT (rtl, i);
17372 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17373 }
17374 break;
17375
17376 case MODE_VECTOR_FLOAT:
17377 for (i = 0, p = array; i < length; i++, p += elt_size)
17378 {
17379 rtx elt = CONST_VECTOR_ELT (rtl, i);
17380 insert_float (elt, p);
17381 }
17382 break;
17383
17384 default:
17385 gcc_unreachable ();
17386 }
17387
17388 loc_result = new_loc_descr (DW_OP_implicit_value,
17389 length * elt_size, 0);
17390 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17391 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17392 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17393 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17394 }
17395 break;
17396
17397 case CONST:
17398 if (mode == VOIDmode
17399 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17400 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17401 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17402 {
17403 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17404 break;
17405 }
17406 /* FALLTHROUGH */
17407 case SYMBOL_REF:
17408 if (!const_ok_for_output (rtl))
17409 break;
17410 /* FALLTHROUGH */
17411 case LABEL_REF:
17412 if (is_a <scalar_int_mode> (mode, &int_mode)
17413 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17414 && (dwarf_version >= 4 || !dwarf_strict))
17415 {
17416 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17417 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17418 vec_safe_push (used_rtx_array, rtl);
17419 }
17420 break;
17421
17422 case DEBUG_IMPLICIT_PTR:
17423 loc_result = implicit_ptr_descriptor (rtl, 0);
17424 break;
17425
17426 case PLUS:
17427 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17428 && CONST_INT_P (XEXP (rtl, 1)))
17429 {
17430 loc_result
17431 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17432 break;
17433 }
17434 /* FALLTHRU */
17435 do_default:
17436 default:
17437 if ((is_a <scalar_int_mode> (mode, &int_mode)
17438 && GET_MODE (rtl) == int_mode
17439 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17440 && dwarf_version >= 4)
17441 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17442 {
17443 /* Value expression. */
17444 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17445 if (loc_result)
17446 add_loc_descr (&loc_result,
17447 new_loc_descr (DW_OP_stack_value, 0, 0));
17448 }
17449 break;
17450 }
17451
17452 return loc_result;
17453 }
17454
17455 /* We need to figure out what section we should use as the base for the
17456 address ranges where a given location is valid.
17457 1. If this particular DECL has a section associated with it, use that.
17458 2. If this function has a section associated with it, use that.
17459 3. Otherwise, use the text section.
17460 XXX: If you split a variable across multiple sections, we won't notice. */
17461
17462 static const char *
17463 secname_for_decl (const_tree decl)
17464 {
17465 const char *secname;
17466
17467 if (VAR_OR_FUNCTION_DECL_P (decl)
17468 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17469 && DECL_SECTION_NAME (decl))
17470 secname = DECL_SECTION_NAME (decl);
17471 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17472 {
17473 if (in_cold_section_p)
17474 {
17475 section *sec = current_function_section ();
17476 if (sec->common.flags & SECTION_NAMED)
17477 return sec->named.name;
17478 }
17479 secname = DECL_SECTION_NAME (current_function_decl);
17480 }
17481 else if (cfun && in_cold_section_p)
17482 secname = crtl->subsections.cold_section_label;
17483 else
17484 secname = text_section_label;
17485
17486 return secname;
17487 }
17488
17489 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17490
17491 static bool
17492 decl_by_reference_p (tree decl)
17493 {
17494 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17495 || VAR_P (decl))
17496 && DECL_BY_REFERENCE (decl));
17497 }
17498
17499 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17500 for VARLOC. */
17501
17502 static dw_loc_descr_ref
17503 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17504 enum var_init_status initialized)
17505 {
17506 int have_address = 0;
17507 dw_loc_descr_ref descr;
17508 machine_mode mode;
17509
17510 if (want_address != 2)
17511 {
17512 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17513 /* Single part. */
17514 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17515 {
17516 varloc = PAT_VAR_LOCATION_LOC (varloc);
17517 if (GET_CODE (varloc) == EXPR_LIST)
17518 varloc = XEXP (varloc, 0);
17519 mode = GET_MODE (varloc);
17520 if (MEM_P (varloc))
17521 {
17522 rtx addr = XEXP (varloc, 0);
17523 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17524 mode, initialized);
17525 if (descr)
17526 have_address = 1;
17527 else
17528 {
17529 rtx x = avoid_constant_pool_reference (varloc);
17530 if (x != varloc)
17531 descr = mem_loc_descriptor (x, mode, VOIDmode,
17532 initialized);
17533 }
17534 }
17535 else
17536 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17537 }
17538 else
17539 return 0;
17540 }
17541 else
17542 {
17543 if (GET_CODE (varloc) == VAR_LOCATION)
17544 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17545 else
17546 mode = DECL_MODE (loc);
17547 descr = loc_descriptor (varloc, mode, initialized);
17548 have_address = 1;
17549 }
17550
17551 if (!descr)
17552 return 0;
17553
17554 if (want_address == 2 && !have_address
17555 && (dwarf_version >= 4 || !dwarf_strict))
17556 {
17557 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17558 {
17559 expansion_failed (loc, NULL_RTX,
17560 "DWARF address size mismatch");
17561 return 0;
17562 }
17563 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17564 have_address = 1;
17565 }
17566 /* Show if we can't fill the request for an address. */
17567 if (want_address && !have_address)
17568 {
17569 expansion_failed (loc, NULL_RTX,
17570 "Want address and only have value");
17571 return 0;
17572 }
17573
17574 /* If we've got an address and don't want one, dereference. */
17575 if (!want_address && have_address)
17576 {
17577 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17578 enum dwarf_location_atom op;
17579
17580 if (size > DWARF2_ADDR_SIZE || size == -1)
17581 {
17582 expansion_failed (loc, NULL_RTX,
17583 "DWARF address size mismatch");
17584 return 0;
17585 }
17586 else if (size == DWARF2_ADDR_SIZE)
17587 op = DW_OP_deref;
17588 else
17589 op = DW_OP_deref_size;
17590
17591 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17592 }
17593
17594 return descr;
17595 }
17596
17597 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17598 if it is not possible. */
17599
17600 static dw_loc_descr_ref
17601 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17602 {
17603 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17604 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17605 else if (dwarf_version >= 3 || !dwarf_strict)
17606 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17607 else
17608 return NULL;
17609 }
17610
17611 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17612 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17613
17614 static dw_loc_descr_ref
17615 dw_sra_loc_expr (tree decl, rtx loc)
17616 {
17617 rtx p;
17618 unsigned HOST_WIDE_INT padsize = 0;
17619 dw_loc_descr_ref descr, *descr_tail;
17620 unsigned HOST_WIDE_INT decl_size;
17621 rtx varloc;
17622 enum var_init_status initialized;
17623
17624 if (DECL_SIZE (decl) == NULL
17625 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17626 return NULL;
17627
17628 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17629 descr = NULL;
17630 descr_tail = &descr;
17631
17632 for (p = loc; p; p = XEXP (p, 1))
17633 {
17634 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17635 rtx loc_note = *decl_piece_varloc_ptr (p);
17636 dw_loc_descr_ref cur_descr;
17637 dw_loc_descr_ref *tail, last = NULL;
17638 unsigned HOST_WIDE_INT opsize = 0;
17639
17640 if (loc_note == NULL_RTX
17641 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17642 {
17643 padsize += bitsize;
17644 continue;
17645 }
17646 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17647 varloc = NOTE_VAR_LOCATION (loc_note);
17648 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17649 if (cur_descr == NULL)
17650 {
17651 padsize += bitsize;
17652 continue;
17653 }
17654
17655 /* Check that cur_descr either doesn't use
17656 DW_OP_*piece operations, or their sum is equal
17657 to bitsize. Otherwise we can't embed it. */
17658 for (tail = &cur_descr; *tail != NULL;
17659 tail = &(*tail)->dw_loc_next)
17660 if ((*tail)->dw_loc_opc == DW_OP_piece)
17661 {
17662 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17663 * BITS_PER_UNIT;
17664 last = *tail;
17665 }
17666 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17667 {
17668 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17669 last = *tail;
17670 }
17671
17672 if (last != NULL && opsize != bitsize)
17673 {
17674 padsize += bitsize;
17675 /* Discard the current piece of the descriptor and release any
17676 addr_table entries it uses. */
17677 remove_loc_list_addr_table_entries (cur_descr);
17678 continue;
17679 }
17680
17681 /* If there is a hole, add DW_OP_*piece after empty DWARF
17682 expression, which means that those bits are optimized out. */
17683 if (padsize)
17684 {
17685 if (padsize > decl_size)
17686 {
17687 remove_loc_list_addr_table_entries (cur_descr);
17688 goto discard_descr;
17689 }
17690 decl_size -= padsize;
17691 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17692 if (*descr_tail == NULL)
17693 {
17694 remove_loc_list_addr_table_entries (cur_descr);
17695 goto discard_descr;
17696 }
17697 descr_tail = &(*descr_tail)->dw_loc_next;
17698 padsize = 0;
17699 }
17700 *descr_tail = cur_descr;
17701 descr_tail = tail;
17702 if (bitsize > decl_size)
17703 goto discard_descr;
17704 decl_size -= bitsize;
17705 if (last == NULL)
17706 {
17707 HOST_WIDE_INT offset = 0;
17708 if (GET_CODE (varloc) == VAR_LOCATION
17709 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17710 {
17711 varloc = PAT_VAR_LOCATION_LOC (varloc);
17712 if (GET_CODE (varloc) == EXPR_LIST)
17713 varloc = XEXP (varloc, 0);
17714 }
17715 do
17716 {
17717 if (GET_CODE (varloc) == CONST
17718 || GET_CODE (varloc) == SIGN_EXTEND
17719 || GET_CODE (varloc) == ZERO_EXTEND)
17720 varloc = XEXP (varloc, 0);
17721 else if (GET_CODE (varloc) == SUBREG)
17722 varloc = SUBREG_REG (varloc);
17723 else
17724 break;
17725 }
17726 while (1);
17727 /* DW_OP_bit_size offset should be zero for register
17728 or implicit location descriptions and empty location
17729 descriptions, but for memory addresses needs big endian
17730 adjustment. */
17731 if (MEM_P (varloc))
17732 {
17733 unsigned HOST_WIDE_INT memsize;
17734 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17735 goto discard_descr;
17736 memsize *= BITS_PER_UNIT;
17737 if (memsize != bitsize)
17738 {
17739 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17740 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17741 goto discard_descr;
17742 if (memsize < bitsize)
17743 goto discard_descr;
17744 if (BITS_BIG_ENDIAN)
17745 offset = memsize - bitsize;
17746 }
17747 }
17748
17749 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17750 if (*descr_tail == NULL)
17751 goto discard_descr;
17752 descr_tail = &(*descr_tail)->dw_loc_next;
17753 }
17754 }
17755
17756 /* If there were any non-empty expressions, add padding till the end of
17757 the decl. */
17758 if (descr != NULL && decl_size != 0)
17759 {
17760 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17761 if (*descr_tail == NULL)
17762 goto discard_descr;
17763 }
17764 return descr;
17765
17766 discard_descr:
17767 /* Discard the descriptor and release any addr_table entries it uses. */
17768 remove_loc_list_addr_table_entries (descr);
17769 return NULL;
17770 }
17771
17772 /* Return the dwarf representation of the location list LOC_LIST of
17773 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17774 function. */
17775
17776 static dw_loc_list_ref
17777 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17778 {
17779 const char *endname, *secname;
17780 var_loc_view endview;
17781 rtx varloc;
17782 enum var_init_status initialized;
17783 struct var_loc_node *node;
17784 dw_loc_descr_ref descr;
17785 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17786 dw_loc_list_ref list = NULL;
17787 dw_loc_list_ref *listp = &list;
17788
17789 /* Now that we know what section we are using for a base,
17790 actually construct the list of locations.
17791 The first location information is what is passed to the
17792 function that creates the location list, and the remaining
17793 locations just get added on to that list.
17794 Note that we only know the start address for a location
17795 (IE location changes), so to build the range, we use
17796 the range [current location start, next location start].
17797 This means we have to special case the last node, and generate
17798 a range of [last location start, end of function label]. */
17799
17800 if (cfun && crtl->has_bb_partition)
17801 {
17802 bool save_in_cold_section_p = in_cold_section_p;
17803 in_cold_section_p = first_function_block_is_cold;
17804 if (loc_list->last_before_switch == NULL)
17805 in_cold_section_p = !in_cold_section_p;
17806 secname = secname_for_decl (decl);
17807 in_cold_section_p = save_in_cold_section_p;
17808 }
17809 else
17810 secname = secname_for_decl (decl);
17811
17812 for (node = loc_list->first; node; node = node->next)
17813 {
17814 bool range_across_switch = false;
17815 if (GET_CODE (node->loc) == EXPR_LIST
17816 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17817 {
17818 if (GET_CODE (node->loc) == EXPR_LIST)
17819 {
17820 descr = NULL;
17821 /* This requires DW_OP_{,bit_}piece, which is not usable
17822 inside DWARF expressions. */
17823 if (want_address == 2)
17824 descr = dw_sra_loc_expr (decl, node->loc);
17825 }
17826 else
17827 {
17828 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17829 varloc = NOTE_VAR_LOCATION (node->loc);
17830 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17831 }
17832 if (descr)
17833 {
17834 /* If section switch happens in between node->label
17835 and node->next->label (or end of function) and
17836 we can't emit it as a single entry list,
17837 emit two ranges, first one ending at the end
17838 of first partition and second one starting at the
17839 beginning of second partition. */
17840 if (node == loc_list->last_before_switch
17841 && (node != loc_list->first || loc_list->first->next
17842 /* If we are to emit a view number, we will emit
17843 a loclist rather than a single location
17844 expression for the entire function (see
17845 loc_list_has_views), so we have to split the
17846 range that straddles across partitions. */
17847 || !ZERO_VIEW_P (node->view))
17848 && current_function_decl)
17849 {
17850 endname = cfun->fde->dw_fde_end;
17851 endview = 0;
17852 range_across_switch = true;
17853 }
17854 /* The variable has a location between NODE->LABEL and
17855 NODE->NEXT->LABEL. */
17856 else if (node->next)
17857 endname = node->next->label, endview = node->next->view;
17858 /* If the variable has a location at the last label
17859 it keeps its location until the end of function. */
17860 else if (!current_function_decl)
17861 endname = text_end_label, endview = 0;
17862 else
17863 {
17864 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17865 current_function_funcdef_no);
17866 endname = ggc_strdup (label_id);
17867 endview = 0;
17868 }
17869
17870 *listp = new_loc_list (descr, node->label, node->view,
17871 endname, endview, secname);
17872 if (TREE_CODE (decl) == PARM_DECL
17873 && node == loc_list->first
17874 && NOTE_P (node->loc)
17875 && strcmp (node->label, endname) == 0)
17876 (*listp)->force = true;
17877 listp = &(*listp)->dw_loc_next;
17878 }
17879 }
17880
17881 if (cfun
17882 && crtl->has_bb_partition
17883 && node == loc_list->last_before_switch)
17884 {
17885 bool save_in_cold_section_p = in_cold_section_p;
17886 in_cold_section_p = !first_function_block_is_cold;
17887 secname = secname_for_decl (decl);
17888 in_cold_section_p = save_in_cold_section_p;
17889 }
17890
17891 if (range_across_switch)
17892 {
17893 if (GET_CODE (node->loc) == EXPR_LIST)
17894 descr = dw_sra_loc_expr (decl, node->loc);
17895 else
17896 {
17897 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17898 varloc = NOTE_VAR_LOCATION (node->loc);
17899 descr = dw_loc_list_1 (decl, varloc, want_address,
17900 initialized);
17901 }
17902 gcc_assert (descr);
17903 /* The variable has a location between NODE->LABEL and
17904 NODE->NEXT->LABEL. */
17905 if (node->next)
17906 endname = node->next->label, endview = node->next->view;
17907 else
17908 endname = cfun->fde->dw_fde_second_end, endview = 0;
17909 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17910 endname, endview, secname);
17911 listp = &(*listp)->dw_loc_next;
17912 }
17913 }
17914
17915 /* Try to avoid the overhead of a location list emitting a location
17916 expression instead, but only if we didn't have more than one
17917 location entry in the first place. If some entries were not
17918 representable, we don't want to pretend a single entry that was
17919 applies to the entire scope in which the variable is
17920 available. */
17921 if (list && loc_list->first->next)
17922 gen_llsym (list);
17923 else
17924 maybe_gen_llsym (list);
17925
17926 return list;
17927 }
17928
17929 /* Return true if the loc_list has only single element and thus
17930 can be represented as location description. */
17931
17932 static bool
17933 single_element_loc_list_p (dw_loc_list_ref list)
17934 {
17935 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17936 return !list->ll_symbol;
17937 }
17938
17939 /* Duplicate a single element of location list. */
17940
17941 static inline dw_loc_descr_ref
17942 copy_loc_descr (dw_loc_descr_ref ref)
17943 {
17944 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17945 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17946 return copy;
17947 }
17948
17949 /* To each location in list LIST append loc descr REF. */
17950
17951 static void
17952 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17953 {
17954 dw_loc_descr_ref copy;
17955 add_loc_descr (&list->expr, ref);
17956 list = list->dw_loc_next;
17957 while (list)
17958 {
17959 copy = copy_loc_descr (ref);
17960 add_loc_descr (&list->expr, copy);
17961 while (copy->dw_loc_next)
17962 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17963 list = list->dw_loc_next;
17964 }
17965 }
17966
17967 /* To each location in list LIST prepend loc descr REF. */
17968
17969 static void
17970 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17971 {
17972 dw_loc_descr_ref copy;
17973 dw_loc_descr_ref ref_end = list->expr;
17974 add_loc_descr (&ref, list->expr);
17975 list->expr = ref;
17976 list = list->dw_loc_next;
17977 while (list)
17978 {
17979 dw_loc_descr_ref end = list->expr;
17980 list->expr = copy = copy_loc_descr (ref);
17981 while (copy->dw_loc_next != ref_end)
17982 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17983 copy->dw_loc_next = end;
17984 list = list->dw_loc_next;
17985 }
17986 }
17987
17988 /* Given two lists RET and LIST
17989 produce location list that is result of adding expression in LIST
17990 to expression in RET on each position in program.
17991 Might be destructive on both RET and LIST.
17992
17993 TODO: We handle only simple cases of RET or LIST having at most one
17994 element. General case would involve sorting the lists in program order
17995 and merging them that will need some additional work.
17996 Adding that will improve quality of debug info especially for SRA-ed
17997 structures. */
17998
17999 static void
18000 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
18001 {
18002 if (!list)
18003 return;
18004 if (!*ret)
18005 {
18006 *ret = list;
18007 return;
18008 }
18009 if (!list->dw_loc_next)
18010 {
18011 add_loc_descr_to_each (*ret, list->expr);
18012 return;
18013 }
18014 if (!(*ret)->dw_loc_next)
18015 {
18016 prepend_loc_descr_to_each (list, (*ret)->expr);
18017 *ret = list;
18018 return;
18019 }
18020 expansion_failed (NULL_TREE, NULL_RTX,
18021 "Don't know how to merge two non-trivial"
18022 " location lists.\n");
18023 *ret = NULL;
18024 return;
18025 }
18026
18027 /* LOC is constant expression. Try a luck, look it up in constant
18028 pool and return its loc_descr of its address. */
18029
18030 static dw_loc_descr_ref
18031 cst_pool_loc_descr (tree loc)
18032 {
18033 /* Get an RTL for this, if something has been emitted. */
18034 rtx rtl = lookup_constant_def (loc);
18035
18036 if (!rtl || !MEM_P (rtl))
18037 {
18038 gcc_assert (!rtl);
18039 return 0;
18040 }
18041 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
18042
18043 /* TODO: We might get more coverage if we was actually delaying expansion
18044 of all expressions till end of compilation when constant pools are fully
18045 populated. */
18046 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
18047 {
18048 expansion_failed (loc, NULL_RTX,
18049 "CST value in contant pool but not marked.");
18050 return 0;
18051 }
18052 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
18053 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
18054 }
18055
18056 /* Return dw_loc_list representing address of addr_expr LOC
18057 by looking for inner INDIRECT_REF expression and turning
18058 it into simple arithmetics.
18059
18060 See loc_list_from_tree for the meaning of CONTEXT. */
18061
18062 static dw_loc_list_ref
18063 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
18064 loc_descr_context *context)
18065 {
18066 tree obj, offset;
18067 poly_int64 bitsize, bitpos, bytepos;
18068 machine_mode mode;
18069 int unsignedp, reversep, volatilep = 0;
18070 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18071
18072 obj = get_inner_reference (TREE_OPERAND (loc, 0),
18073 &bitsize, &bitpos, &offset, &mode,
18074 &unsignedp, &reversep, &volatilep);
18075 STRIP_NOPS (obj);
18076 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
18077 {
18078 expansion_failed (loc, NULL_RTX, "bitfield access");
18079 return 0;
18080 }
18081 if (!INDIRECT_REF_P (obj))
18082 {
18083 expansion_failed (obj,
18084 NULL_RTX, "no indirect ref in inner refrence");
18085 return 0;
18086 }
18087 if (!offset && known_eq (bitpos, 0))
18088 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18089 context);
18090 else if (toplev
18091 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18092 && (dwarf_version >= 4 || !dwarf_strict))
18093 {
18094 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18095 if (!list_ret)
18096 return 0;
18097 if (offset)
18098 {
18099 /* Variable offset. */
18100 list_ret1 = loc_list_from_tree (offset, 0, context);
18101 if (list_ret1 == 0)
18102 return 0;
18103 add_loc_list (&list_ret, list_ret1);
18104 if (!list_ret)
18105 return 0;
18106 add_loc_descr_to_each (list_ret,
18107 new_loc_descr (DW_OP_plus, 0, 0));
18108 }
18109 HOST_WIDE_INT value;
18110 if (bytepos.is_constant (&value) && value > 0)
18111 add_loc_descr_to_each (list_ret,
18112 new_loc_descr (DW_OP_plus_uconst, value, 0));
18113 else if (maybe_ne (bytepos, 0))
18114 loc_list_plus_const (list_ret, bytepos);
18115 add_loc_descr_to_each (list_ret,
18116 new_loc_descr (DW_OP_stack_value, 0, 0));
18117 }
18118 return list_ret;
18119 }
18120
18121 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18122 all operations from LOC are nops, move to the last one. Insert in NOPS all
18123 operations that are skipped. */
18124
18125 static void
18126 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18127 hash_set<dw_loc_descr_ref> &nops)
18128 {
18129 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18130 {
18131 nops.add (loc);
18132 loc = loc->dw_loc_next;
18133 }
18134 }
18135
18136 /* Helper for loc_descr_without_nops: free the location description operation
18137 P. */
18138
18139 bool
18140 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18141 {
18142 ggc_free (loc);
18143 return true;
18144 }
18145
18146 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18147 finishes LOC. */
18148
18149 static void
18150 loc_descr_without_nops (dw_loc_descr_ref &loc)
18151 {
18152 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18153 return;
18154
18155 /* Set of all DW_OP_nop operations we remove. */
18156 hash_set<dw_loc_descr_ref> nops;
18157
18158 /* First, strip all prefix NOP operations in order to keep the head of the
18159 operations list. */
18160 loc_descr_to_next_no_nop (loc, nops);
18161
18162 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18163 {
18164 /* For control flow operations: strip "prefix" nops in destination
18165 labels. */
18166 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18167 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18168 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18169 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18170
18171 /* Do the same for the operations that follow, then move to the next
18172 iteration. */
18173 if (cur->dw_loc_next != NULL)
18174 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18175 cur = cur->dw_loc_next;
18176 }
18177
18178 nops.traverse<void *, free_loc_descr> (NULL);
18179 }
18180
18181
18182 struct dwarf_procedure_info;
18183
18184 /* Helper structure for location descriptions generation. */
18185 struct loc_descr_context
18186 {
18187 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18188 NULL_TREE if DW_OP_push_object_address in invalid for this location
18189 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18190 tree context_type;
18191 /* The ..._DECL node that should be translated as a
18192 DW_OP_push_object_address operation. */
18193 tree base_decl;
18194 /* Information about the DWARF procedure we are currently generating. NULL if
18195 we are not generating a DWARF procedure. */
18196 struct dwarf_procedure_info *dpi;
18197 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18198 by consumer. Used for DW_TAG_generic_subrange attributes. */
18199 bool placeholder_arg;
18200 /* True if PLACEHOLDER_EXPR has been seen. */
18201 bool placeholder_seen;
18202 /* True if strict preservation of signedness has been requested. */
18203 bool strict_signedness;
18204 };
18205
18206 /* DWARF procedures generation
18207
18208 DWARF expressions (aka. location descriptions) are used to encode variable
18209 things such as sizes or offsets. Such computations can have redundant parts
18210 that can be factorized in order to reduce the size of the output debug
18211 information. This is the whole point of DWARF procedures.
18212
18213 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18214 already factorized into functions ("size functions") in order to handle very
18215 big and complex types. Such functions are quite simple: they have integral
18216 arguments, they return an integral result and their body contains only a
18217 return statement with arithmetic expressions. This is the only kind of
18218 function we are interested in translating into DWARF procedures, here.
18219
18220 DWARF expressions and DWARF procedure are executed using a stack, so we have
18221 to define some calling convention for them to interact. Let's say that:
18222
18223 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18224 all arguments in reverse order (right-to-left) so that when the DWARF
18225 procedure execution starts, the first argument is the top of the stack.
18226
18227 - Then, when returning, the DWARF procedure must have consumed all arguments
18228 on the stack, must have pushed the result and touched nothing else.
18229
18230 - Each integral argument and the result are integral types can be hold in a
18231 single stack slot.
18232
18233 - We call "frame offset" the number of stack slots that are "under DWARF
18234 procedure control": it includes the arguments slots, the temporaries and
18235 the result slot. Thus, it is equal to the number of arguments when the
18236 procedure execution starts and must be equal to one (the result) when it
18237 returns. */
18238
18239 /* Helper structure used when generating operations for a DWARF procedure. */
18240 struct dwarf_procedure_info
18241 {
18242 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18243 currently translated. */
18244 tree fndecl;
18245 /* The number of arguments FNDECL takes. */
18246 unsigned args_count;
18247 };
18248
18249 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18250 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18251 equate it to this DIE. */
18252
18253 static dw_die_ref
18254 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18255 dw_die_ref parent_die)
18256 {
18257 dw_die_ref dwarf_proc_die;
18258
18259 if ((dwarf_version < 3 && dwarf_strict)
18260 || location == NULL)
18261 return NULL;
18262
18263 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18264 if (fndecl)
18265 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18266 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18267 return dwarf_proc_die;
18268 }
18269
18270 /* Return whether TYPE is a supported type as a DWARF procedure argument
18271 type or return type (we handle only scalar types and pointer types that
18272 aren't wider than the DWARF expression evaluation stack). */
18273
18274 static bool
18275 is_handled_procedure_type (tree type)
18276 {
18277 return ((INTEGRAL_TYPE_P (type)
18278 || TREE_CODE (type) == OFFSET_TYPE
18279 || TREE_CODE (type) == POINTER_TYPE)
18280 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18281 }
18282
18283 /* Helper for resolve_args_picking: do the same but stop when coming across
18284 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18285 offset *before* evaluating the corresponding operation. */
18286
18287 static bool
18288 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18289 struct dwarf_procedure_info *dpi,
18290 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18291 {
18292 /* The "frame_offset" identifier is already used to name a macro... */
18293 unsigned frame_offset_ = initial_frame_offset;
18294 dw_loc_descr_ref l;
18295
18296 for (l = loc; l != NULL;)
18297 {
18298 bool existed;
18299 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18300
18301 /* If we already met this node, there is nothing to compute anymore. */
18302 if (existed)
18303 {
18304 /* Make sure that the stack size is consistent wherever the execution
18305 flow comes from. */
18306 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18307 break;
18308 }
18309 l_frame_offset = frame_offset_;
18310
18311 /* If needed, relocate the picking offset with respect to the frame
18312 offset. */
18313 if (l->frame_offset_rel)
18314 {
18315 unsigned HOST_WIDE_INT off;
18316 switch (l->dw_loc_opc)
18317 {
18318 case DW_OP_pick:
18319 off = l->dw_loc_oprnd1.v.val_unsigned;
18320 break;
18321 case DW_OP_dup:
18322 off = 0;
18323 break;
18324 case DW_OP_over:
18325 off = 1;
18326 break;
18327 default:
18328 gcc_unreachable ();
18329 }
18330 /* frame_offset_ is the size of the current stack frame, including
18331 incoming arguments. Besides, the arguments are pushed
18332 right-to-left. Thus, in order to access the Nth argument from
18333 this operation node, the picking has to skip temporaries *plus*
18334 one stack slot per argument (0 for the first one, 1 for the second
18335 one, etc.).
18336
18337 The targetted argument number (N) is already set as the operand,
18338 and the number of temporaries can be computed with:
18339 frame_offsets_ - dpi->args_count */
18340 off += frame_offset_ - dpi->args_count;
18341
18342 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18343 if (off > 255)
18344 return false;
18345
18346 if (off == 0)
18347 {
18348 l->dw_loc_opc = DW_OP_dup;
18349 l->dw_loc_oprnd1.v.val_unsigned = 0;
18350 }
18351 else if (off == 1)
18352 {
18353 l->dw_loc_opc = DW_OP_over;
18354 l->dw_loc_oprnd1.v.val_unsigned = 0;
18355 }
18356 else
18357 {
18358 l->dw_loc_opc = DW_OP_pick;
18359 l->dw_loc_oprnd1.v.val_unsigned = off;
18360 }
18361 }
18362
18363 /* Update frame_offset according to the effect the current operation has
18364 on the stack. */
18365 switch (l->dw_loc_opc)
18366 {
18367 case DW_OP_deref:
18368 case DW_OP_swap:
18369 case DW_OP_rot:
18370 case DW_OP_abs:
18371 case DW_OP_neg:
18372 case DW_OP_not:
18373 case DW_OP_plus_uconst:
18374 case DW_OP_skip:
18375 case DW_OP_reg0:
18376 case DW_OP_reg1:
18377 case DW_OP_reg2:
18378 case DW_OP_reg3:
18379 case DW_OP_reg4:
18380 case DW_OP_reg5:
18381 case DW_OP_reg6:
18382 case DW_OP_reg7:
18383 case DW_OP_reg8:
18384 case DW_OP_reg9:
18385 case DW_OP_reg10:
18386 case DW_OP_reg11:
18387 case DW_OP_reg12:
18388 case DW_OP_reg13:
18389 case DW_OP_reg14:
18390 case DW_OP_reg15:
18391 case DW_OP_reg16:
18392 case DW_OP_reg17:
18393 case DW_OP_reg18:
18394 case DW_OP_reg19:
18395 case DW_OP_reg20:
18396 case DW_OP_reg21:
18397 case DW_OP_reg22:
18398 case DW_OP_reg23:
18399 case DW_OP_reg24:
18400 case DW_OP_reg25:
18401 case DW_OP_reg26:
18402 case DW_OP_reg27:
18403 case DW_OP_reg28:
18404 case DW_OP_reg29:
18405 case DW_OP_reg30:
18406 case DW_OP_reg31:
18407 case DW_OP_bregx:
18408 case DW_OP_piece:
18409 case DW_OP_deref_size:
18410 case DW_OP_nop:
18411 case DW_OP_bit_piece:
18412 case DW_OP_implicit_value:
18413 case DW_OP_stack_value:
18414 case DW_OP_deref_type:
18415 case DW_OP_convert:
18416 case DW_OP_reinterpret:
18417 case DW_OP_GNU_deref_type:
18418 case DW_OP_GNU_convert:
18419 case DW_OP_GNU_reinterpret:
18420 break;
18421
18422 case DW_OP_addr:
18423 case DW_OP_const1u:
18424 case DW_OP_const1s:
18425 case DW_OP_const2u:
18426 case DW_OP_const2s:
18427 case DW_OP_const4u:
18428 case DW_OP_const4s:
18429 case DW_OP_const8u:
18430 case DW_OP_const8s:
18431 case DW_OP_constu:
18432 case DW_OP_consts:
18433 case DW_OP_dup:
18434 case DW_OP_over:
18435 case DW_OP_pick:
18436 case DW_OP_lit0:
18437 case DW_OP_lit1:
18438 case DW_OP_lit2:
18439 case DW_OP_lit3:
18440 case DW_OP_lit4:
18441 case DW_OP_lit5:
18442 case DW_OP_lit6:
18443 case DW_OP_lit7:
18444 case DW_OP_lit8:
18445 case DW_OP_lit9:
18446 case DW_OP_lit10:
18447 case DW_OP_lit11:
18448 case DW_OP_lit12:
18449 case DW_OP_lit13:
18450 case DW_OP_lit14:
18451 case DW_OP_lit15:
18452 case DW_OP_lit16:
18453 case DW_OP_lit17:
18454 case DW_OP_lit18:
18455 case DW_OP_lit19:
18456 case DW_OP_lit20:
18457 case DW_OP_lit21:
18458 case DW_OP_lit22:
18459 case DW_OP_lit23:
18460 case DW_OP_lit24:
18461 case DW_OP_lit25:
18462 case DW_OP_lit26:
18463 case DW_OP_lit27:
18464 case DW_OP_lit28:
18465 case DW_OP_lit29:
18466 case DW_OP_lit30:
18467 case DW_OP_lit31:
18468 case DW_OP_breg0:
18469 case DW_OP_breg1:
18470 case DW_OP_breg2:
18471 case DW_OP_breg3:
18472 case DW_OP_breg4:
18473 case DW_OP_breg5:
18474 case DW_OP_breg6:
18475 case DW_OP_breg7:
18476 case DW_OP_breg8:
18477 case DW_OP_breg9:
18478 case DW_OP_breg10:
18479 case DW_OP_breg11:
18480 case DW_OP_breg12:
18481 case DW_OP_breg13:
18482 case DW_OP_breg14:
18483 case DW_OP_breg15:
18484 case DW_OP_breg16:
18485 case DW_OP_breg17:
18486 case DW_OP_breg18:
18487 case DW_OP_breg19:
18488 case DW_OP_breg20:
18489 case DW_OP_breg21:
18490 case DW_OP_breg22:
18491 case DW_OP_breg23:
18492 case DW_OP_breg24:
18493 case DW_OP_breg25:
18494 case DW_OP_breg26:
18495 case DW_OP_breg27:
18496 case DW_OP_breg28:
18497 case DW_OP_breg29:
18498 case DW_OP_breg30:
18499 case DW_OP_breg31:
18500 case DW_OP_fbreg:
18501 case DW_OP_push_object_address:
18502 case DW_OP_call_frame_cfa:
18503 case DW_OP_GNU_variable_value:
18504 case DW_OP_GNU_addr_index:
18505 case DW_OP_GNU_const_index:
18506 ++frame_offset_;
18507 break;
18508
18509 case DW_OP_drop:
18510 case DW_OP_xderef:
18511 case DW_OP_and:
18512 case DW_OP_div:
18513 case DW_OP_minus:
18514 case DW_OP_mod:
18515 case DW_OP_mul:
18516 case DW_OP_or:
18517 case DW_OP_plus:
18518 case DW_OP_shl:
18519 case DW_OP_shr:
18520 case DW_OP_shra:
18521 case DW_OP_xor:
18522 case DW_OP_bra:
18523 case DW_OP_eq:
18524 case DW_OP_ge:
18525 case DW_OP_gt:
18526 case DW_OP_le:
18527 case DW_OP_lt:
18528 case DW_OP_ne:
18529 case DW_OP_regx:
18530 case DW_OP_xderef_size:
18531 --frame_offset_;
18532 break;
18533
18534 case DW_OP_call2:
18535 case DW_OP_call4:
18536 case DW_OP_call_ref:
18537 {
18538 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18539 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18540
18541 if (stack_usage == NULL)
18542 return false;
18543 frame_offset_ += *stack_usage;
18544 break;
18545 }
18546
18547 case DW_OP_implicit_pointer:
18548 case DW_OP_entry_value:
18549 case DW_OP_const_type:
18550 case DW_OP_regval_type:
18551 case DW_OP_form_tls_address:
18552 case DW_OP_GNU_push_tls_address:
18553 case DW_OP_GNU_uninit:
18554 case DW_OP_GNU_encoded_addr:
18555 case DW_OP_GNU_implicit_pointer:
18556 case DW_OP_GNU_entry_value:
18557 case DW_OP_GNU_const_type:
18558 case DW_OP_GNU_regval_type:
18559 case DW_OP_GNU_parameter_ref:
18560 /* loc_list_from_tree will probably not output these operations for
18561 size functions, so assume they will not appear here. */
18562 /* Fall through... */
18563
18564 default:
18565 gcc_unreachable ();
18566 }
18567
18568 /* Now, follow the control flow (except subroutine calls). */
18569 switch (l->dw_loc_opc)
18570 {
18571 case DW_OP_bra:
18572 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18573 frame_offsets))
18574 return false;
18575 /* Fall through. */
18576
18577 case DW_OP_skip:
18578 l = l->dw_loc_oprnd1.v.val_loc;
18579 break;
18580
18581 case DW_OP_stack_value:
18582 return true;
18583
18584 default:
18585 l = l->dw_loc_next;
18586 break;
18587 }
18588 }
18589
18590 return true;
18591 }
18592
18593 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18594 operations) in order to resolve the operand of DW_OP_pick operations that
18595 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18596 offset *before* LOC is executed. Return if all relocations were
18597 successful. */
18598
18599 static bool
18600 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18601 struct dwarf_procedure_info *dpi)
18602 {
18603 /* Associate to all visited operations the frame offset *before* evaluating
18604 this operation. */
18605 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18606
18607 return
18608 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18609 }
18610
18611 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18612 Return NULL if it is not possible. */
18613
18614 static dw_die_ref
18615 function_to_dwarf_procedure (tree fndecl)
18616 {
18617 struct dwarf_procedure_info dpi;
18618 struct loc_descr_context ctx = {
18619 NULL_TREE, /* context_type */
18620 NULL_TREE, /* base_decl */
18621 &dpi, /* dpi */
18622 false, /* placeholder_arg */
18623 false, /* placeholder_seen */
18624 true /* strict_signedness */
18625 };
18626 dw_die_ref dwarf_proc_die;
18627 tree tree_body = DECL_SAVED_TREE (fndecl);
18628 dw_loc_descr_ref loc_body, epilogue;
18629
18630 tree cursor;
18631 unsigned i;
18632
18633 /* Do not generate multiple DWARF procedures for the same function
18634 declaration. */
18635 dwarf_proc_die = lookup_decl_die (fndecl);
18636 if (dwarf_proc_die != NULL)
18637 return dwarf_proc_die;
18638
18639 /* DWARF procedures are available starting with the DWARFv3 standard. */
18640 if (dwarf_version < 3 && dwarf_strict)
18641 return NULL;
18642
18643 /* We handle only functions for which we still have a body, that return a
18644 supported type and that takes arguments with supported types. Note that
18645 there is no point translating functions that return nothing. */
18646 if (tree_body == NULL_TREE
18647 || DECL_RESULT (fndecl) == NULL_TREE
18648 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18649 return NULL;
18650
18651 for (cursor = DECL_ARGUMENTS (fndecl);
18652 cursor != NULL_TREE;
18653 cursor = TREE_CHAIN (cursor))
18654 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18655 return NULL;
18656
18657 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18658 if (TREE_CODE (tree_body) != RETURN_EXPR)
18659 return NULL;
18660 tree_body = TREE_OPERAND (tree_body, 0);
18661 if (TREE_CODE (tree_body) != MODIFY_EXPR
18662 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18663 return NULL;
18664 tree_body = TREE_OPERAND (tree_body, 1);
18665
18666 /* Try to translate the body expression itself. Note that this will probably
18667 cause an infinite recursion if its call graph has a cycle. This is very
18668 unlikely for size functions, however, so don't bother with such things at
18669 the moment. */
18670 dpi.fndecl = fndecl;
18671 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18672 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18673 if (!loc_body)
18674 return NULL;
18675
18676 /* After evaluating all operands in "loc_body", we should still have on the
18677 stack all arguments plus the desired function result (top of the stack).
18678 Generate code in order to keep only the result in our stack frame. */
18679 epilogue = NULL;
18680 for (i = 0; i < dpi.args_count; ++i)
18681 {
18682 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18683 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18684 op_couple->dw_loc_next->dw_loc_next = epilogue;
18685 epilogue = op_couple;
18686 }
18687 add_loc_descr (&loc_body, epilogue);
18688 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18689 return NULL;
18690
18691 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18692 because they are considered useful. Now there is an epilogue, they are
18693 not anymore, so give it another try. */
18694 loc_descr_without_nops (loc_body);
18695
18696 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18697 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18698 though, given that size functions do not come from source, so they should
18699 not have a dedicated DW_TAG_subprogram DIE. */
18700 dwarf_proc_die
18701 = new_dwarf_proc_die (loc_body, fndecl,
18702 get_context_die (DECL_CONTEXT (fndecl)));
18703
18704 /* The called DWARF procedure consumes one stack slot per argument and
18705 returns one stack slot. */
18706 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18707
18708 return dwarf_proc_die;
18709 }
18710
18711 /* Helper function for loc_list_from_tree. Perform OP binary op,
18712 but after converting arguments to type_die, afterwards convert
18713 back to unsigned. */
18714
18715 static dw_loc_list_ref
18716 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18717 dw_die_ref type_die, scalar_int_mode mode,
18718 struct loc_descr_context *context)
18719 {
18720 dw_loc_list_ref op0, op1;
18721 dw_loc_descr_ref cvt, binop;
18722
18723 if (type_die == NULL)
18724 return NULL;
18725
18726 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18727 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18728 if (op0 == NULL || op1 == NULL)
18729 return NULL;
18730
18731 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18732 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18733 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18734 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18735 add_loc_descr_to_each (op0, cvt);
18736
18737 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18738 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18739 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18740 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18741 add_loc_descr_to_each (op1, cvt);
18742
18743 add_loc_list (&op0, op1);
18744 if (op0 == NULL)
18745 return NULL;
18746
18747 binop = new_loc_descr (op, 0, 0);
18748 convert_descriptor_to_mode (mode, binop);
18749 add_loc_descr_to_each (op0, binop);
18750
18751 return op0;
18752 }
18753
18754 /* Generate Dwarf location list representing LOC.
18755 If WANT_ADDRESS is false, expression computing LOC will be computed
18756 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18757 if WANT_ADDRESS is 2, expression computing address useable in location
18758 will be returned (i.e. DW_OP_reg can be used
18759 to refer to register values).
18760
18761 CONTEXT provides information to customize the location descriptions
18762 generation. Its context_type field specifies what type is implicitly
18763 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18764 will not be generated.
18765
18766 Its DPI field determines whether we are generating a DWARF expression for a
18767 DWARF procedure, so PARM_DECL references are processed specifically.
18768
18769 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18770 and dpi fields were null. */
18771
18772 static dw_loc_list_ref
18773 loc_list_from_tree_1 (tree loc, int want_address,
18774 struct loc_descr_context *context)
18775 {
18776 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18777 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18778 int have_address = 0;
18779 enum dwarf_location_atom op;
18780
18781 /* ??? Most of the time we do not take proper care for sign/zero
18782 extending the values properly. Hopefully this won't be a real
18783 problem... */
18784
18785 if (context != NULL
18786 && context->base_decl == loc
18787 && want_address == 0)
18788 {
18789 if (dwarf_version >= 3 || !dwarf_strict)
18790 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18791 NULL, 0, NULL, 0, NULL);
18792 else
18793 return NULL;
18794 }
18795
18796 switch (TREE_CODE (loc))
18797 {
18798 case ERROR_MARK:
18799 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18800 return 0;
18801
18802 case PLACEHOLDER_EXPR:
18803 /* This case involves extracting fields from an object to determine the
18804 position of other fields. It is supposed to appear only as the first
18805 operand of COMPONENT_REF nodes and to reference precisely the type
18806 that the context allows or its enclosing type. */
18807 if (context != NULL
18808 && (TREE_TYPE (loc) == context->context_type
18809 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18810 && want_address >= 1)
18811 {
18812 if (dwarf_version >= 3 || !dwarf_strict)
18813 {
18814 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18815 have_address = 1;
18816 break;
18817 }
18818 else
18819 return NULL;
18820 }
18821 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18822 the single argument passed by consumer. */
18823 else if (context != NULL
18824 && context->placeholder_arg
18825 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18826 && want_address == 0)
18827 {
18828 ret = new_loc_descr (DW_OP_pick, 0, 0);
18829 ret->frame_offset_rel = 1;
18830 context->placeholder_seen = true;
18831 break;
18832 }
18833 else
18834 expansion_failed (loc, NULL_RTX,
18835 "PLACEHOLDER_EXPR for an unexpected type");
18836 break;
18837
18838 case CALL_EXPR:
18839 {
18840 tree callee = get_callee_fndecl (loc);
18841 dw_die_ref dwarf_proc;
18842
18843 if (callee
18844 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18845 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18846 {
18847 /* DWARF procedures are used for size functions, which are built
18848 when size expressions contain conditional constructs, so we
18849 request strict preservation of signedness for comparisons. */
18850 bool old_strict_signedness;
18851 if (context)
18852 {
18853 old_strict_signedness = context->strict_signedness;
18854 context->strict_signedness = true;
18855 }
18856
18857 /* Evaluate arguments right-to-left so that the first argument
18858 will be the top-most one on the stack. */
18859 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18860 {
18861 tree arg = CALL_EXPR_ARG (loc, i);
18862 ret1 = loc_descriptor_from_tree (arg, 0, context);
18863 if (!ret1)
18864 {
18865 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18866 return NULL;
18867 }
18868 add_loc_descr (&ret, ret1);
18869 }
18870
18871 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18872 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18873 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18874 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18875 add_loc_descr (&ret, ret1);
18876 if (context)
18877 context->strict_signedness = old_strict_signedness;
18878 }
18879 else
18880 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18881 break;
18882 }
18883
18884 case PREINCREMENT_EXPR:
18885 case PREDECREMENT_EXPR:
18886 case POSTINCREMENT_EXPR:
18887 case POSTDECREMENT_EXPR:
18888 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18889 /* There are no opcodes for these operations. */
18890 return 0;
18891
18892 case ADDR_EXPR:
18893 /* If we already want an address, see if there is INDIRECT_REF inside
18894 e.g. for &this->field. */
18895 if (want_address)
18896 {
18897 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18898 (loc, want_address == 2, context);
18899 if (list_ret)
18900 have_address = 1;
18901 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18902 && (ret = cst_pool_loc_descr (loc)))
18903 have_address = 1;
18904 }
18905 /* Otherwise, process the argument and look for the address. */
18906 if (!list_ret && !ret)
18907 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18908 else
18909 {
18910 if (want_address)
18911 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18912 return NULL;
18913 }
18914 break;
18915
18916 case VAR_DECL:
18917 if (DECL_THREAD_LOCAL_P (loc))
18918 {
18919 rtx rtl;
18920 enum dwarf_location_atom tls_op;
18921 enum dtprel_bool dtprel = dtprel_false;
18922
18923 if (targetm.have_tls)
18924 {
18925 /* If this is not defined, we have no way to emit the
18926 data. */
18927 if (!targetm.asm_out.output_dwarf_dtprel)
18928 return 0;
18929
18930 /* The way DW_OP_GNU_push_tls_address is specified, we
18931 can only look up addresses of objects in the current
18932 module. We used DW_OP_addr as first op, but that's
18933 wrong, because DW_OP_addr is relocated by the debug
18934 info consumer, while DW_OP_GNU_push_tls_address
18935 operand shouldn't be. */
18936 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18937 return 0;
18938 dtprel = dtprel_true;
18939 /* We check for DWARF 5 here because gdb did not implement
18940 DW_OP_form_tls_address until after 7.12. */
18941 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18942 : DW_OP_GNU_push_tls_address);
18943 }
18944 else
18945 {
18946 if (!targetm.emutls.debug_form_tls_address
18947 || !(dwarf_version >= 3 || !dwarf_strict))
18948 return 0;
18949 /* We stuffed the control variable into the DECL_VALUE_EXPR
18950 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18951 no longer appear in gimple code. We used the control
18952 variable in specific so that we could pick it up here. */
18953 loc = DECL_VALUE_EXPR (loc);
18954 tls_op = DW_OP_form_tls_address;
18955 }
18956
18957 rtl = rtl_for_decl_location (loc);
18958 if (rtl == NULL_RTX)
18959 return 0;
18960
18961 if (!MEM_P (rtl))
18962 return 0;
18963 rtl = XEXP (rtl, 0);
18964 if (! CONSTANT_P (rtl))
18965 return 0;
18966
18967 ret = new_addr_loc_descr (rtl, dtprel);
18968 ret1 = new_loc_descr (tls_op, 0, 0);
18969 add_loc_descr (&ret, ret1);
18970
18971 have_address = 1;
18972 break;
18973 }
18974 /* FALLTHRU */
18975
18976 case PARM_DECL:
18977 if (context != NULL && context->dpi != NULL
18978 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18979 {
18980 /* We are generating code for a DWARF procedure and we want to access
18981 one of its arguments: find the appropriate argument offset and let
18982 the resolve_args_picking pass compute the offset that complies
18983 with the stack frame size. */
18984 unsigned i = 0;
18985 tree cursor;
18986
18987 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18988 cursor != NULL_TREE && cursor != loc;
18989 cursor = TREE_CHAIN (cursor), ++i)
18990 ;
18991 /* If we are translating a DWARF procedure, all referenced parameters
18992 must belong to the current function. */
18993 gcc_assert (cursor != NULL_TREE);
18994
18995 ret = new_loc_descr (DW_OP_pick, i, 0);
18996 ret->frame_offset_rel = 1;
18997 break;
18998 }
18999 /* FALLTHRU */
19000
19001 case RESULT_DECL:
19002 if (DECL_HAS_VALUE_EXPR_P (loc))
19003 {
19004 tree value_expr = DECL_VALUE_EXPR (loc);
19005
19006 /* Non-local frame structures are DECL_IGNORED_P variables so we need
19007 to wait until they get an RTX in order to reference them. */
19008 if (early_dwarf
19009 && TREE_CODE (value_expr) == COMPONENT_REF
19010 && VAR_P (TREE_OPERAND (value_expr, 0))
19011 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
19012 ;
19013 else
19014 return loc_list_from_tree_1 (value_expr, want_address, context);
19015 }
19016
19017 /* FALLTHRU */
19018
19019 case FUNCTION_DECL:
19020 {
19021 rtx rtl;
19022 var_loc_list *loc_list = lookup_decl_loc (loc);
19023
19024 if (loc_list && loc_list->first)
19025 {
19026 list_ret = dw_loc_list (loc_list, loc, want_address);
19027 have_address = want_address != 0;
19028 break;
19029 }
19030 rtl = rtl_for_decl_location (loc);
19031 if (rtl == NULL_RTX)
19032 {
19033 if (TREE_CODE (loc) != FUNCTION_DECL
19034 && early_dwarf
19035 && want_address != 1
19036 && ! DECL_IGNORED_P (loc)
19037 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
19038 || POINTER_TYPE_P (TREE_TYPE (loc)))
19039 && TYPE_MODE (TREE_TYPE (loc)) != BLKmode
19040 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
19041 <= DWARF2_ADDR_SIZE))
19042 {
19043 dw_die_ref ref = lookup_decl_die (loc);
19044 if (ref)
19045 {
19046 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19047 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19048 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
19049 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
19050 }
19051 else if (current_function_decl
19052 && DECL_CONTEXT (loc) == current_function_decl)
19053 {
19054 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19055 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19056 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
19057 }
19058 break;
19059 }
19060 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
19061 return 0;
19062 }
19063 else if (CONST_INT_P (rtl))
19064 {
19065 HOST_WIDE_INT val = INTVAL (rtl);
19066 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19067 val &= GET_MODE_MASK (DECL_MODE (loc));
19068 ret = int_loc_descriptor (val);
19069 }
19070 else if (GET_CODE (rtl) == CONST_STRING)
19071 {
19072 expansion_failed (loc, NULL_RTX, "CONST_STRING");
19073 return 0;
19074 }
19075 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
19076 ret = new_addr_loc_descr (rtl, dtprel_false);
19077 else
19078 {
19079 machine_mode mode, mem_mode;
19080
19081 /* Certain constructs can only be represented at top-level. */
19082 if (want_address == 2)
19083 {
19084 ret = loc_descriptor (rtl, VOIDmode,
19085 VAR_INIT_STATUS_INITIALIZED);
19086 have_address = 1;
19087 }
19088 else
19089 {
19090 mode = GET_MODE (rtl);
19091 mem_mode = VOIDmode;
19092 if (MEM_P (rtl))
19093 {
19094 mem_mode = mode;
19095 mode = get_address_mode (rtl);
19096 rtl = XEXP (rtl, 0);
19097 have_address = 1;
19098 }
19099 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19100 VAR_INIT_STATUS_INITIALIZED);
19101 }
19102 if (!ret)
19103 expansion_failed (loc, rtl,
19104 "failed to produce loc descriptor for rtl");
19105 }
19106 }
19107 break;
19108
19109 case MEM_REF:
19110 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19111 {
19112 have_address = 1;
19113 goto do_plus;
19114 }
19115 /* Fallthru. */
19116 case INDIRECT_REF:
19117 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19118 have_address = 1;
19119 break;
19120
19121 case TARGET_MEM_REF:
19122 case SSA_NAME:
19123 case DEBUG_EXPR_DECL:
19124 return NULL;
19125
19126 case COMPOUND_EXPR:
19127 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19128 context);
19129
19130 CASE_CONVERT:
19131 case VIEW_CONVERT_EXPR:
19132 case SAVE_EXPR:
19133 case MODIFY_EXPR:
19134 case NON_LVALUE_EXPR:
19135 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19136 context);
19137
19138 case COMPONENT_REF:
19139 case BIT_FIELD_REF:
19140 case ARRAY_REF:
19141 case ARRAY_RANGE_REF:
19142 case REALPART_EXPR:
19143 case IMAGPART_EXPR:
19144 {
19145 tree obj, offset;
19146 poly_int64 bitsize, bitpos, bytepos;
19147 machine_mode mode;
19148 int unsignedp, reversep, volatilep = 0;
19149
19150 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19151 &unsignedp, &reversep, &volatilep);
19152
19153 gcc_assert (obj != loc);
19154
19155 list_ret = loc_list_from_tree_1 (obj,
19156 want_address == 2
19157 && known_eq (bitpos, 0)
19158 && !offset ? 2 : 1,
19159 context);
19160 /* TODO: We can extract value of the small expression via shifting even
19161 for nonzero bitpos. */
19162 if (list_ret == 0)
19163 return 0;
19164 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19165 || !multiple_p (bitsize, BITS_PER_UNIT))
19166 {
19167 expansion_failed (loc, NULL_RTX,
19168 "bitfield access");
19169 return 0;
19170 }
19171
19172 if (offset != NULL_TREE)
19173 {
19174 /* Variable offset. */
19175 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19176 if (list_ret1 == 0)
19177 return 0;
19178 add_loc_list (&list_ret, list_ret1);
19179 if (!list_ret)
19180 return 0;
19181 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19182 }
19183
19184 HOST_WIDE_INT value;
19185 if (bytepos.is_constant (&value) && value > 0)
19186 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19187 value, 0));
19188 else if (maybe_ne (bytepos, 0))
19189 loc_list_plus_const (list_ret, bytepos);
19190
19191 have_address = 1;
19192 break;
19193 }
19194
19195 case INTEGER_CST:
19196 if ((want_address || !tree_fits_shwi_p (loc))
19197 && (ret = cst_pool_loc_descr (loc)))
19198 have_address = 1;
19199 else if (want_address == 2
19200 && tree_fits_shwi_p (loc)
19201 && (ret = address_of_int_loc_descriptor
19202 (int_size_in_bytes (TREE_TYPE (loc)),
19203 tree_to_shwi (loc))))
19204 have_address = 1;
19205 else if (tree_fits_shwi_p (loc))
19206 ret = int_loc_descriptor (tree_to_shwi (loc));
19207 else if (tree_fits_uhwi_p (loc))
19208 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19209 else
19210 {
19211 expansion_failed (loc, NULL_RTX,
19212 "Integer operand is not host integer");
19213 return 0;
19214 }
19215 break;
19216
19217 case POLY_INT_CST:
19218 {
19219 if (want_address)
19220 {
19221 expansion_failed (loc, NULL_RTX,
19222 "constant address with a runtime component");
19223 return 0;
19224 }
19225 poly_int64 value;
19226 if (!poly_int_tree_p (loc, &value))
19227 {
19228 expansion_failed (loc, NULL_RTX, "constant too big");
19229 return 0;
19230 }
19231 ret = int_loc_descriptor (value);
19232 }
19233 break;
19234
19235 case CONSTRUCTOR:
19236 case REAL_CST:
19237 case STRING_CST:
19238 case COMPLEX_CST:
19239 if ((ret = cst_pool_loc_descr (loc)))
19240 have_address = 1;
19241 else if (TREE_CODE (loc) == CONSTRUCTOR)
19242 {
19243 tree type = TREE_TYPE (loc);
19244 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19245 unsigned HOST_WIDE_INT offset = 0;
19246 unsigned HOST_WIDE_INT cnt;
19247 constructor_elt *ce;
19248
19249 if (TREE_CODE (type) == RECORD_TYPE)
19250 {
19251 /* This is very limited, but it's enough to output
19252 pointers to member functions, as long as the
19253 referenced function is defined in the current
19254 translation unit. */
19255 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19256 {
19257 tree val = ce->value;
19258
19259 tree field = ce->index;
19260
19261 if (val)
19262 STRIP_NOPS (val);
19263
19264 if (!field || DECL_BIT_FIELD (field))
19265 {
19266 expansion_failed (loc, NULL_RTX,
19267 "bitfield in record type constructor");
19268 size = offset = (unsigned HOST_WIDE_INT)-1;
19269 ret = NULL;
19270 break;
19271 }
19272
19273 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19274 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19275 gcc_assert (pos + fieldsize <= size);
19276 if (pos < offset)
19277 {
19278 expansion_failed (loc, NULL_RTX,
19279 "out-of-order fields in record constructor");
19280 size = offset = (unsigned HOST_WIDE_INT)-1;
19281 ret = NULL;
19282 break;
19283 }
19284 if (pos > offset)
19285 {
19286 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19287 add_loc_descr (&ret, ret1);
19288 offset = pos;
19289 }
19290 if (val && fieldsize != 0)
19291 {
19292 ret1 = loc_descriptor_from_tree (val, want_address, context);
19293 if (!ret1)
19294 {
19295 expansion_failed (loc, NULL_RTX,
19296 "unsupported expression in field");
19297 size = offset = (unsigned HOST_WIDE_INT)-1;
19298 ret = NULL;
19299 break;
19300 }
19301 add_loc_descr (&ret, ret1);
19302 }
19303 if (fieldsize)
19304 {
19305 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19306 add_loc_descr (&ret, ret1);
19307 offset = pos + fieldsize;
19308 }
19309 }
19310
19311 if (offset != size)
19312 {
19313 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19314 add_loc_descr (&ret, ret1);
19315 offset = size;
19316 }
19317
19318 have_address = !!want_address;
19319 }
19320 else
19321 expansion_failed (loc, NULL_RTX,
19322 "constructor of non-record type");
19323 }
19324 else
19325 /* We can construct small constants here using int_loc_descriptor. */
19326 expansion_failed (loc, NULL_RTX,
19327 "constructor or constant not in constant pool");
19328 break;
19329
19330 case TRUTH_AND_EXPR:
19331 case TRUTH_ANDIF_EXPR:
19332 case BIT_AND_EXPR:
19333 op = DW_OP_and;
19334 goto do_binop;
19335
19336 case TRUTH_XOR_EXPR:
19337 case BIT_XOR_EXPR:
19338 op = DW_OP_xor;
19339 goto do_binop;
19340
19341 case TRUTH_OR_EXPR:
19342 case TRUTH_ORIF_EXPR:
19343 case BIT_IOR_EXPR:
19344 op = DW_OP_or;
19345 goto do_binop;
19346
19347 case EXACT_DIV_EXPR:
19348 case FLOOR_DIV_EXPR:
19349 case TRUNC_DIV_EXPR:
19350 /* Turn a divide by a power of 2 into a shift when possible. */
19351 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19352 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19353 {
19354 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19355 if (log2 > 0)
19356 {
19357 list_ret
19358 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19359 if (list_ret == 0)
19360 return 0;
19361
19362 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19363 add_loc_descr_to_each (list_ret,
19364 new_loc_descr (DW_OP_shr, 0, 0));
19365 break;
19366 }
19367 }
19368
19369 /* fall through */
19370
19371 case CEIL_DIV_EXPR:
19372 case ROUND_DIV_EXPR:
19373 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19374 {
19375 const enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19376 scalar_int_mode int_mode;
19377
19378 if ((dwarf_strict && dwarf_version < 5)
19379 || !is_a <scalar_int_mode> (mode, &int_mode))
19380 return 0;
19381
19382 /* We can use a signed divide if the sign bit is not set. */
19383 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19384 {
19385 op = DW_OP_div;
19386 goto do_binop;
19387 }
19388
19389 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19390 base_type_for_mode (int_mode, 1),
19391 int_mode, context);
19392 break;
19393 }
19394 op = DW_OP_div;
19395 goto do_binop;
19396
19397 case MINUS_EXPR:
19398 op = DW_OP_minus;
19399 goto do_binop;
19400
19401 case FLOOR_MOD_EXPR:
19402 case CEIL_MOD_EXPR:
19403 case ROUND_MOD_EXPR:
19404 case TRUNC_MOD_EXPR:
19405 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19406 {
19407 op = DW_OP_mod;
19408 goto do_binop;
19409 }
19410 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19411 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19412 if (list_ret == 0 || list_ret1 == 0)
19413 return 0;
19414
19415 add_loc_list (&list_ret, list_ret1);
19416 if (list_ret == 0)
19417 return 0;
19418 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19419 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19420 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19421 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19422 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19423 break;
19424
19425 case MULT_EXPR:
19426 op = DW_OP_mul;
19427 goto do_binop;
19428
19429 case LSHIFT_EXPR:
19430 op = DW_OP_shl;
19431 goto do_binop;
19432
19433 case RSHIFT_EXPR:
19434 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19435 goto do_binop;
19436
19437 case POINTER_PLUS_EXPR:
19438 case PLUS_EXPR:
19439 do_plus:
19440 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19441 {
19442 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19443 smarter to encode their opposite. The DW_OP_plus_uconst operation
19444 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19445 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19446 bytes, Y being the size of the operation that pushes the opposite
19447 of the addend. So let's choose the smallest representation. */
19448 const tree tree_addend = TREE_OPERAND (loc, 1);
19449 offset_int wi_addend;
19450 HOST_WIDE_INT shwi_addend;
19451 dw_loc_descr_ref loc_naddend;
19452
19453 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19454 if (list_ret == 0)
19455 return 0;
19456
19457 /* Try to get the literal to push. It is the opposite of the addend,
19458 so as we rely on wrapping during DWARF evaluation, first decode
19459 the literal as a "DWARF-sized" signed number. */
19460 wi_addend = wi::to_offset (tree_addend);
19461 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19462 shwi_addend = wi_addend.to_shwi ();
19463 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19464 ? int_loc_descriptor (-shwi_addend)
19465 : NULL;
19466
19467 if (loc_naddend != NULL
19468 && ((unsigned) size_of_uleb128 (shwi_addend)
19469 > size_of_loc_descr (loc_naddend)))
19470 {
19471 add_loc_descr_to_each (list_ret, loc_naddend);
19472 add_loc_descr_to_each (list_ret,
19473 new_loc_descr (DW_OP_minus, 0, 0));
19474 }
19475 else
19476 {
19477 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19478 {
19479 loc_naddend = loc_cur;
19480 loc_cur = loc_cur->dw_loc_next;
19481 ggc_free (loc_naddend);
19482 }
19483 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19484 }
19485 break;
19486 }
19487
19488 op = DW_OP_plus;
19489 goto do_binop;
19490
19491 case LE_EXPR:
19492 op = DW_OP_le;
19493 goto do_comp_binop;
19494
19495 case GE_EXPR:
19496 op = DW_OP_ge;
19497 goto do_comp_binop;
19498
19499 case LT_EXPR:
19500 op = DW_OP_lt;
19501 goto do_comp_binop;
19502
19503 case GT_EXPR:
19504 op = DW_OP_gt;
19505 goto do_comp_binop;
19506
19507 do_comp_binop:
19508 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19509 {
19510 const enum machine_mode mode
19511 = TYPE_MODE (TREE_TYPE (TREE_OPERAND (loc, 0)));
19512 scalar_int_mode int_mode;
19513
19514 /* We can use a signed comparison if the sign bit is not set. */
19515 if (is_a <scalar_int_mode> (mode, &int_mode)
19516 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19517 goto do_binop;
19518
19519 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19520 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19521 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19522 TREE_CODE (loc));
19523 break;
19524 }
19525 else
19526 goto do_binop;
19527
19528 case EQ_EXPR:
19529 op = DW_OP_eq;
19530 goto do_binop;
19531
19532 case NE_EXPR:
19533 op = DW_OP_ne;
19534 goto do_binop;
19535
19536 do_binop:
19537 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19538 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19539 if (list_ret == 0 || list_ret1 == 0)
19540 return 0;
19541
19542 add_loc_list (&list_ret, list_ret1);
19543 if (list_ret == 0)
19544 return 0;
19545
19546 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19547 break;
19548
19549 case TRUTH_NOT_EXPR:
19550 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19551 if (list_ret == 0)
19552 return 0;
19553
19554 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_lit0, 0, 0));
19555 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_eq, 0, 0));
19556 break;
19557
19558 case BIT_NOT_EXPR:
19559 op = DW_OP_not;
19560 goto do_unop;
19561
19562 case ABS_EXPR:
19563 op = DW_OP_abs;
19564 goto do_unop;
19565
19566 case NEGATE_EXPR:
19567 op = DW_OP_neg;
19568 goto do_unop;
19569
19570 do_unop:
19571 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19572 if (list_ret == 0)
19573 return 0;
19574
19575 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19576 break;
19577
19578 case MIN_EXPR:
19579 case MAX_EXPR:
19580 {
19581 const enum tree_code code =
19582 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19583
19584 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19585 build2 (code, integer_type_node,
19586 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19587 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19588 }
19589
19590 /* fall through */
19591
19592 case COND_EXPR:
19593 {
19594 dw_loc_descr_ref lhs
19595 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19596 dw_loc_list_ref rhs
19597 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19598 dw_loc_descr_ref bra_node, jump_node, tmp;
19599
19600 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19601 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19602 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19603 list_ret
19604 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19605 0, context);
19606 /* Likewise, swap the operands for a logically negated condition. */
19607 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19608 {
19609 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19610 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19611 list_ret
19612 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19613 0, context);
19614 }
19615 else
19616 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19617 if (list_ret == 0 || lhs == 0 || rhs == 0)
19618 return 0;
19619
19620 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19621 add_loc_descr_to_each (list_ret, bra_node);
19622
19623 add_loc_list (&list_ret, rhs);
19624 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19625 add_loc_descr_to_each (list_ret, jump_node);
19626
19627 add_loc_descr_to_each (list_ret, lhs);
19628 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19629 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19630
19631 /* ??? Need a node to point the skip at. Use a nop. */
19632 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19633 add_loc_descr_to_each (list_ret, tmp);
19634 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19635 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19636 }
19637 break;
19638
19639 case FIX_TRUNC_EXPR:
19640 return 0;
19641
19642 case COMPOUND_LITERAL_EXPR:
19643 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19644 0, context);
19645
19646 default:
19647 /* Leave front-end specific codes as simply unknown. This comes
19648 up, for instance, with the C STMT_EXPR. */
19649 if ((unsigned int) TREE_CODE (loc)
19650 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19651 {
19652 expansion_failed (loc, NULL_RTX,
19653 "language specific tree node");
19654 return 0;
19655 }
19656
19657 /* Otherwise this is a generic code; we should just lists all of
19658 these explicitly. We forgot one. */
19659 if (flag_checking)
19660 gcc_unreachable ();
19661
19662 /* In a release build, we want to degrade gracefully: better to
19663 generate incomplete debugging information than to crash. */
19664 return NULL;
19665 }
19666
19667 if (!ret && !list_ret)
19668 return 0;
19669
19670 /* Implement wrap-around arithmetics for small integer types. */
19671 if ((TREE_CODE (loc) == PLUS_EXPR
19672 || TREE_CODE (loc) == MINUS_EXPR
19673 || TREE_CODE (loc) == MULT_EXPR
19674 || TREE_CODE (loc) == NEGATE_EXPR
19675 || TREE_CODE (loc) == LSHIFT_EXPR)
19676 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
19677 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (loc)))
19678 {
19679 const enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19680 scalar_int_mode int_mode;
19681
19682 if (is_a <scalar_int_mode> (mode, &int_mode)
19683 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19684 {
19685 const unsigned HOST_WIDE_INT mask
19686 = (HOST_WIDE_INT_1U << GET_MODE_BITSIZE (int_mode)) - 1;
19687 add_loc_descr_to_each (list_ret, uint_loc_descriptor (mask));
19688 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_and, 0, 0));
19689 }
19690 }
19691
19692 if (want_address == 2 && !have_address
19693 && (dwarf_version >= 4 || !dwarf_strict))
19694 {
19695 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19696 {
19697 expansion_failed (loc, NULL_RTX,
19698 "DWARF address size mismatch");
19699 return 0;
19700 }
19701 if (ret)
19702 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19703 else
19704 add_loc_descr_to_each (list_ret,
19705 new_loc_descr (DW_OP_stack_value, 0, 0));
19706 have_address = 1;
19707 }
19708 /* Show if we can't fill the request for an address. */
19709 if (want_address && !have_address)
19710 {
19711 expansion_failed (loc, NULL_RTX,
19712 "Want address and only have value");
19713 return 0;
19714 }
19715
19716 gcc_assert (!ret || !list_ret);
19717
19718 /* If we've got an address and don't want one, dereference. */
19719 if (!want_address && have_address)
19720 {
19721 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19722 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19723 scalar_int_mode int_mode;
19724 dw_die_ref type_die;
19725 dw_loc_descr_ref deref;
19726
19727 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19728 if (size > DWARF2_ADDR_SIZE || size == -1)
19729 {
19730 expansion_failed (loc, NULL_RTX,
19731 "DWARF address size mismatch");
19732 return 0;
19733 }
19734
19735 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19736 else if (size == DWARF2_ADDR_SIZE)
19737 deref = new_loc_descr (DW_OP_deref, size, 0);
19738
19739 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19740 extend the value, which is really OK for unsigned types only. */
19741 else if (!(context && context->strict_signedness)
19742 || TYPE_UNSIGNED (TREE_TYPE (loc))
19743 || (dwarf_strict && dwarf_version < 5)
19744 || !is_a <scalar_int_mode> (mode, &int_mode)
19745 || !(type_die = base_type_for_mode (mode, false)))
19746 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19747
19748 /* Use DW_OP_deref_type for signed integral types if possible, but
19749 convert back to the generic type to avoid type mismatches later. */
19750 else
19751 {
19752 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19753 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19754 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19755 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19756 add_loc_descr (&deref,
19757 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19758 }
19759
19760 if (ret)
19761 add_loc_descr (&ret, deref);
19762 else
19763 add_loc_descr_to_each (list_ret, deref);
19764 }
19765
19766 if (ret)
19767 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19768
19769 return list_ret;
19770 }
19771
19772 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19773 expressions. */
19774
19775 static dw_loc_list_ref
19776 loc_list_from_tree (tree loc, int want_address,
19777 struct loc_descr_context *context)
19778 {
19779 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19780
19781 for (dw_loc_list_ref loc_cur = result;
19782 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19783 loc_descr_without_nops (loc_cur->expr);
19784 return result;
19785 }
19786
19787 /* Same as above but return only single location expression. */
19788 static dw_loc_descr_ref
19789 loc_descriptor_from_tree (tree loc, int want_address,
19790 struct loc_descr_context *context)
19791 {
19792 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19793 if (!ret)
19794 return NULL;
19795 if (ret->dw_loc_next)
19796 {
19797 expansion_failed (loc, NULL_RTX,
19798 "Location list where only loc descriptor needed");
19799 return NULL;
19800 }
19801 return ret->expr;
19802 }
19803
19804 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19805 pointer to the declared type for the relevant field variable, or return
19806 `integer_type_node' if the given node turns out to be an
19807 ERROR_MARK node. */
19808
19809 static inline tree
19810 field_type (const_tree decl)
19811 {
19812 tree type;
19813
19814 if (TREE_CODE (decl) == ERROR_MARK)
19815 return integer_type_node;
19816
19817 type = DECL_BIT_FIELD_TYPE (decl);
19818 if (type == NULL_TREE)
19819 type = TREE_TYPE (decl);
19820
19821 return type;
19822 }
19823
19824 /* Given a pointer to a tree node, return the alignment in bits for
19825 it, or else return BITS_PER_WORD if the node actually turns out to
19826 be an ERROR_MARK node. */
19827
19828 static inline unsigned
19829 simple_type_align_in_bits (const_tree type)
19830 {
19831 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19832 }
19833
19834 static inline unsigned
19835 simple_decl_align_in_bits (const_tree decl)
19836 {
19837 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19838 }
19839
19840 /* Return the result of rounding T up to ALIGN. */
19841
19842 static inline offset_int
19843 round_up_to_align (const offset_int &t, unsigned int align)
19844 {
19845 return wi::udiv_trunc (t + align - 1, align) * align;
19846 }
19847
19848 /* Helper structure for RECORD_TYPE processing. */
19849 struct vlr_context
19850 {
19851 /* Root RECORD_TYPE. It is needed to generate data member location
19852 descriptions in variable-length records (VLR), but also to cope with
19853 variants, which are composed of nested structures multiplexed with
19854 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19855 function processing a FIELD_DECL, it is required to be non null. */
19856 tree struct_type;
19857
19858 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19859 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19860 this variant part as part of the root record (in storage units). For
19861 regular records, it must be NULL_TREE. */
19862 tree variant_part_offset;
19863 };
19864
19865 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19866 addressed byte of the "containing object" for the given FIELD_DECL. If
19867 possible, return a native constant through CST_OFFSET (in which case NULL is
19868 returned); otherwise return a DWARF expression that computes the offset.
19869
19870 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19871 that offset is, either because the argument turns out to be a pointer to an
19872 ERROR_MARK node, or because the offset expression is too complex for us.
19873
19874 CTX is required: see the comment for VLR_CONTEXT. */
19875
19876 static dw_loc_descr_ref
19877 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19878 HOST_WIDE_INT *cst_offset)
19879 {
19880 tree tree_result;
19881 dw_loc_list_ref loc_result;
19882
19883 *cst_offset = 0;
19884
19885 if (TREE_CODE (decl) == ERROR_MARK)
19886 return NULL;
19887 else
19888 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19889
19890 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19891 case. */
19892 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19893 return NULL;
19894
19895 /* We used to handle only constant offsets in all cases. Now, we handle
19896 properly dynamic byte offsets only when PCC bitfield type doesn't
19897 matter. */
19898 if (PCC_BITFIELD_TYPE_MATTERS
19899 && DECL_BIT_FIELD_TYPE (decl)
19900 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19901 {
19902 offset_int object_offset_in_bits;
19903 offset_int object_offset_in_bytes;
19904 offset_int bitpos_int;
19905 tree type;
19906 tree field_size_tree;
19907 offset_int deepest_bitpos;
19908 offset_int field_size_in_bits;
19909 unsigned int type_align_in_bits;
19910 unsigned int decl_align_in_bits;
19911 offset_int type_size_in_bits;
19912
19913 bitpos_int = wi::to_offset (bit_position (decl));
19914 type = field_type (decl);
19915 type_size_in_bits = offset_int_type_size_in_bits (type);
19916 type_align_in_bits = simple_type_align_in_bits (type);
19917
19918 field_size_tree = DECL_SIZE (decl);
19919
19920 /* The size could be unspecified if there was an error, or for
19921 a flexible array member. */
19922 if (!field_size_tree)
19923 field_size_tree = bitsize_zero_node;
19924
19925 /* If the size of the field is not constant, use the type size. */
19926 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19927 field_size_in_bits = wi::to_offset (field_size_tree);
19928 else
19929 field_size_in_bits = type_size_in_bits;
19930
19931 decl_align_in_bits = simple_decl_align_in_bits (decl);
19932
19933 /* The GCC front-end doesn't make any attempt to keep track of the
19934 starting bit offset (relative to the start of the containing
19935 structure type) of the hypothetical "containing object" for a
19936 bit-field. Thus, when computing the byte offset value for the
19937 start of the "containing object" of a bit-field, we must deduce
19938 this information on our own. This can be rather tricky to do in
19939 some cases. For example, handling the following structure type
19940 definition when compiling for an i386/i486 target (which only
19941 aligns long long's to 32-bit boundaries) can be very tricky:
19942
19943 struct S { int field1; long long field2:31; };
19944
19945 Fortunately, there is a simple rule-of-thumb which can be used
19946 in such cases. When compiling for an i386/i486, GCC will
19947 allocate 8 bytes for the structure shown above. It decides to
19948 do this based upon one simple rule for bit-field allocation.
19949 GCC allocates each "containing object" for each bit-field at
19950 the first (i.e. lowest addressed) legitimate alignment boundary
19951 (based upon the required minimum alignment for the declared
19952 type of the field) which it can possibly use, subject to the
19953 condition that there is still enough available space remaining
19954 in the containing object (when allocated at the selected point)
19955 to fully accommodate all of the bits of the bit-field itself.
19956
19957 This simple rule makes it obvious why GCC allocates 8 bytes for
19958 each object of the structure type shown above. When looking
19959 for a place to allocate the "containing object" for `field2',
19960 the compiler simply tries to allocate a 64-bit "containing
19961 object" at each successive 32-bit boundary (starting at zero)
19962 until it finds a place to allocate that 64- bit field such that
19963 at least 31 contiguous (and previously unallocated) bits remain
19964 within that selected 64 bit field. (As it turns out, for the
19965 example above, the compiler finds it is OK to allocate the
19966 "containing object" 64-bit field at bit-offset zero within the
19967 structure type.)
19968
19969 Here we attempt to work backwards from the limited set of facts
19970 we're given, and we try to deduce from those facts, where GCC
19971 must have believed that the containing object started (within
19972 the structure type). The value we deduce is then used (by the
19973 callers of this routine) to generate DW_AT_location and
19974 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19975 the case of DW_AT_location, regular fields as well). */
19976
19977 /* Figure out the bit-distance from the start of the structure to
19978 the "deepest" bit of the bit-field. */
19979 deepest_bitpos = bitpos_int + field_size_in_bits;
19980
19981 /* This is the tricky part. Use some fancy footwork to deduce
19982 where the lowest addressed bit of the containing object must
19983 be. */
19984 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19985
19986 /* Round up to type_align by default. This works best for
19987 bitfields. */
19988 object_offset_in_bits
19989 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19990
19991 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19992 {
19993 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19994
19995 /* Round up to decl_align instead. */
19996 object_offset_in_bits
19997 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19998 }
19999
20000 object_offset_in_bytes
20001 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
20002 if (ctx->variant_part_offset == NULL_TREE)
20003 {
20004 *cst_offset = object_offset_in_bytes.to_shwi ();
20005 return NULL;
20006 }
20007 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
20008 }
20009 else
20010 tree_result = byte_position (decl);
20011
20012 if (ctx->variant_part_offset != NULL_TREE)
20013 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
20014 ctx->variant_part_offset, tree_result);
20015
20016 /* If the byte offset is a constant, it's simplier to handle a native
20017 constant rather than a DWARF expression. */
20018 if (TREE_CODE (tree_result) == INTEGER_CST)
20019 {
20020 *cst_offset = wi::to_offset (tree_result).to_shwi ();
20021 return NULL;
20022 }
20023
20024 struct loc_descr_context loc_ctx = {
20025 ctx->struct_type, /* context_type */
20026 NULL_TREE, /* base_decl */
20027 NULL, /* dpi */
20028 false, /* placeholder_arg */
20029 false, /* placeholder_seen */
20030 false /* strict_signedness */
20031 };
20032 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
20033
20034 /* We want a DWARF expression: abort if we only have a location list with
20035 multiple elements. */
20036 if (!loc_result || !single_element_loc_list_p (loc_result))
20037 return NULL;
20038 else
20039 return loc_result->expr;
20040 }
20041
20042 /* The following routines define various Dwarf attributes and any data
20043 associated with them. */
20044
20045 /* Add a location description attribute value to a DIE.
20046
20047 This emits location attributes suitable for whole variables and
20048 whole parameters. Note that the location attributes for struct fields are
20049 generated by the routine `data_member_location_attribute' below. */
20050
20051 static inline void
20052 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
20053 dw_loc_list_ref descr)
20054 {
20055 bool check_no_locviews = true;
20056 if (descr == 0)
20057 return;
20058 if (single_element_loc_list_p (descr))
20059 add_AT_loc (die, attr_kind, descr->expr);
20060 else
20061 {
20062 add_AT_loc_list (die, attr_kind, descr);
20063 gcc_assert (descr->ll_symbol);
20064 if (attr_kind == DW_AT_location && descr->vl_symbol
20065 && dwarf2out_locviews_in_attribute ())
20066 {
20067 add_AT_view_list (die, DW_AT_GNU_locviews);
20068 check_no_locviews = false;
20069 }
20070 }
20071
20072 if (check_no_locviews)
20073 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
20074 }
20075
20076 /* Add DW_AT_accessibility attribute to DIE if needed. */
20077
20078 static void
20079 add_accessibility_attribute (dw_die_ref die, tree decl)
20080 {
20081 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20082 children, otherwise the default is DW_ACCESS_public. In DWARF2
20083 the default has always been DW_ACCESS_public. */
20084 if (TREE_PROTECTED (decl))
20085 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20086 else if (TREE_PRIVATE (decl))
20087 {
20088 if (dwarf_version == 2
20089 || die->die_parent == NULL
20090 || die->die_parent->die_tag != DW_TAG_class_type)
20091 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20092 }
20093 else if (dwarf_version > 2
20094 && die->die_parent
20095 && die->die_parent->die_tag == DW_TAG_class_type)
20096 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20097 }
20098
20099 /* Attach the specialized form of location attribute used for data members of
20100 struct and union types. In the special case of a FIELD_DECL node which
20101 represents a bit-field, the "offset" part of this special location
20102 descriptor must indicate the distance in bytes from the lowest-addressed
20103 byte of the containing struct or union type to the lowest-addressed byte of
20104 the "containing object" for the bit-field. (See the `field_byte_offset'
20105 function above).
20106
20107 For any given bit-field, the "containing object" is a hypothetical object
20108 (of some integral or enum type) within which the given bit-field lives. The
20109 type of this hypothetical "containing object" is always the same as the
20110 declared type of the individual bit-field itself (for GCC anyway... the
20111 DWARF spec doesn't actually mandate this). Note that it is the size (in
20112 bytes) of the hypothetical "containing object" which will be given in the
20113 DW_AT_byte_size attribute for this bit-field. (See the
20114 `byte_size_attribute' function below.) It is also used when calculating the
20115 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
20116 function below.)
20117
20118 CTX is required: see the comment for VLR_CONTEXT. */
20119
20120 static void
20121 add_data_member_location_attribute (dw_die_ref die,
20122 tree decl,
20123 struct vlr_context *ctx)
20124 {
20125 HOST_WIDE_INT offset;
20126 dw_loc_descr_ref loc_descr = 0;
20127
20128 if (TREE_CODE (decl) == TREE_BINFO)
20129 {
20130 /* We're working on the TAG_inheritance for a base class. */
20131 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20132 {
20133 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20134 aren't at a fixed offset from all (sub)objects of the same
20135 type. We need to extract the appropriate offset from our
20136 vtable. The following dwarf expression means
20137
20138 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20139
20140 This is specific to the V3 ABI, of course. */
20141
20142 dw_loc_descr_ref tmp;
20143
20144 /* Make a copy of the object address. */
20145 tmp = new_loc_descr (DW_OP_dup, 0, 0);
20146 add_loc_descr (&loc_descr, tmp);
20147
20148 /* Extract the vtable address. */
20149 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20150 add_loc_descr (&loc_descr, tmp);
20151
20152 /* Calculate the address of the offset. */
20153 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20154 gcc_assert (offset < 0);
20155
20156 tmp = int_loc_descriptor (-offset);
20157 add_loc_descr (&loc_descr, tmp);
20158 tmp = new_loc_descr (DW_OP_minus, 0, 0);
20159 add_loc_descr (&loc_descr, tmp);
20160
20161 /* Extract the offset. */
20162 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20163 add_loc_descr (&loc_descr, tmp);
20164
20165 /* Add it to the object address. */
20166 tmp = new_loc_descr (DW_OP_plus, 0, 0);
20167 add_loc_descr (&loc_descr, tmp);
20168 }
20169 else
20170 offset = tree_to_shwi (BINFO_OFFSET (decl));
20171 }
20172 else
20173 {
20174 loc_descr = field_byte_offset (decl, ctx, &offset);
20175
20176 if (!loc_descr)
20177 ;
20178
20179 /* If loc_descr is available, then we know the offset is dynamic. */
20180 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20181 {
20182 loc_descr = NULL;
20183 offset = 0;
20184 }
20185
20186 /* Data member location evaluation starts with the base address on the
20187 stack. Compute the field offset and add it to this base address. */
20188 else
20189 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
20190 }
20191
20192 if (!loc_descr)
20193 {
20194 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20195 e.g. GDB only added support to it in November 2016. For DWARF5
20196 we need newer debug info consumers anyway. We might change this
20197 to dwarf_version >= 4 once most consumers catched up. */
20198 if (dwarf_version >= 5
20199 && TREE_CODE (decl) == FIELD_DECL
20200 && DECL_BIT_FIELD_TYPE (decl)
20201 && (ctx->variant_part_offset == NULL_TREE
20202 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20203 {
20204 tree off = bit_position (decl);
20205 if (ctx->variant_part_offset)
20206 off = bit_from_pos (ctx->variant_part_offset, off);
20207 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20208 {
20209 remove_AT (die, DW_AT_byte_size);
20210 remove_AT (die, DW_AT_bit_offset);
20211 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20212 return;
20213 }
20214 }
20215 if (dwarf_version > 2)
20216 {
20217 /* Don't need to output a location expression, just the constant. */
20218 if (offset < 0)
20219 add_AT_int (die, DW_AT_data_member_location, offset);
20220 else
20221 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20222 return;
20223 }
20224 else
20225 {
20226 enum dwarf_location_atom op;
20227
20228 /* The DWARF2 standard says that we should assume that the structure
20229 address is already on the stack, so we can specify a structure
20230 field address by using DW_OP_plus_uconst. */
20231 op = DW_OP_plus_uconst;
20232 loc_descr = new_loc_descr (op, offset, 0);
20233 }
20234 }
20235
20236 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20237 }
20238
20239 /* Writes integer values to dw_vec_const array. */
20240
20241 static void
20242 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20243 {
20244 while (size != 0)
20245 {
20246 *dest++ = val & 0xff;
20247 val >>= 8;
20248 --size;
20249 }
20250 }
20251
20252 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20253
20254 static HOST_WIDE_INT
20255 extract_int (const unsigned char *src, unsigned int size)
20256 {
20257 HOST_WIDE_INT val = 0;
20258
20259 src += size;
20260 while (size != 0)
20261 {
20262 val <<= 8;
20263 val |= *--src & 0xff;
20264 --size;
20265 }
20266 return val;
20267 }
20268
20269 /* Writes wide_int values to dw_vec_const array. */
20270
20271 static void
20272 insert_wide_int (const wide_int_ref &val, unsigned char *dest, int elt_size)
20273 {
20274 int i;
20275
20276 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20277 {
20278 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20279 return;
20280 }
20281
20282 /* We'd have to extend this code to support odd sizes. */
20283 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20284
20285 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20286
20287 if (WORDS_BIG_ENDIAN)
20288 for (i = n - 1; i >= 0; i--)
20289 {
20290 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20291 dest += sizeof (HOST_WIDE_INT);
20292 }
20293 else
20294 for (i = 0; i < n; i++)
20295 {
20296 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20297 dest += sizeof (HOST_WIDE_INT);
20298 }
20299 }
20300
20301 /* Writes floating point values to dw_vec_const array. */
20302
20303 static unsigned
20304 insert_float (const_rtx rtl, unsigned char *array)
20305 {
20306 long val[4];
20307 int i;
20308 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20309
20310 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20311
20312 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20313 if (GET_MODE_SIZE (mode) < 4)
20314 {
20315 gcc_assert (GET_MODE_SIZE (mode) == 2);
20316 insert_int (val[0], 2, array);
20317 return 2;
20318 }
20319
20320 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20321 {
20322 insert_int (val[i], 4, array);
20323 array += 4;
20324 }
20325 return 4;
20326 }
20327
20328 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20329 does not have a "location" either in memory or in a register. These
20330 things can arise in GNU C when a constant is passed as an actual parameter
20331 to an inlined function. They can also arise in C++ where declared
20332 constants do not necessarily get memory "homes". */
20333
20334 static bool
20335 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20336 {
20337 scalar_mode int_mode;
20338
20339 switch (GET_CODE (rtl))
20340 {
20341 case CONST_INT:
20342 {
20343 HOST_WIDE_INT val = INTVAL (rtl);
20344
20345 if (val < 0)
20346 add_AT_int (die, DW_AT_const_value, val);
20347 else
20348 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20349 }
20350 return true;
20351
20352 case CONST_WIDE_INT:
20353 if (is_int_mode (mode, &int_mode)
20354 && (GET_MODE_PRECISION (int_mode)
20355 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20356 {
20357 add_AT_wide (die, DW_AT_const_value, rtx_mode_t (rtl, int_mode));
20358 return true;
20359 }
20360 return false;
20361
20362 case CONST_DOUBLE:
20363 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20364 floating-point constant. A CONST_DOUBLE is used whenever the
20365 constant requires more than one word in order to be adequately
20366 represented. */
20367 if (TARGET_SUPPORTS_WIDE_INT == 0
20368 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20369 add_AT_double (die, DW_AT_const_value,
20370 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20371 else
20372 {
20373 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20374 unsigned int length = GET_MODE_SIZE (mode);
20375 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20376 unsigned int elt_size = insert_float (rtl, array);
20377
20378 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20379 array);
20380 }
20381 return true;
20382
20383 case CONST_VECTOR:
20384 {
20385 unsigned int length;
20386 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20387 return false;
20388
20389 machine_mode mode = GET_MODE (rtl);
20390 /* The combination of a length and byte elt_size doesn't extend
20391 naturally to boolean vectors, where several elements are packed
20392 into the same byte. */
20393 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20394 return false;
20395
20396 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20397 unsigned char *array
20398 = ggc_vec_alloc<unsigned char> (length * elt_size);
20399 unsigned int i;
20400 unsigned char *p;
20401 machine_mode imode = GET_MODE_INNER (mode);
20402
20403 switch (GET_MODE_CLASS (mode))
20404 {
20405 case MODE_VECTOR_INT:
20406 for (i = 0, p = array; i < length; i++, p += elt_size)
20407 {
20408 rtx elt = CONST_VECTOR_ELT (rtl, i);
20409 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20410 }
20411 break;
20412
20413 case MODE_VECTOR_FLOAT:
20414 for (i = 0, p = array; i < length; i++, p += elt_size)
20415 {
20416 rtx elt = CONST_VECTOR_ELT (rtl, i);
20417 insert_float (elt, p);
20418 }
20419 break;
20420
20421 default:
20422 gcc_unreachable ();
20423 }
20424
20425 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20426 }
20427 return true;
20428
20429 case CONST_STRING:
20430 if (dwarf_version >= 4 || !dwarf_strict)
20431 {
20432 dw_loc_descr_ref loc_result;
20433 resolve_one_addr (&rtl);
20434 rtl_addr:
20435 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20436 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20437 add_AT_loc (die, DW_AT_location, loc_result);
20438 vec_safe_push (used_rtx_array, rtl);
20439 return true;
20440 }
20441 return false;
20442
20443 case CONST:
20444 if (CONSTANT_P (XEXP (rtl, 0)))
20445 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20446 /* FALLTHROUGH */
20447 case SYMBOL_REF:
20448 if (!const_ok_for_output (rtl))
20449 return false;
20450 /* FALLTHROUGH */
20451 case LABEL_REF:
20452 if (dwarf_version >= 4 || !dwarf_strict)
20453 goto rtl_addr;
20454 return false;
20455
20456 case PLUS:
20457 /* In cases where an inlined instance of an inline function is passed
20458 the address of an `auto' variable (which is local to the caller) we
20459 can get a situation where the DECL_RTL of the artificial local
20460 variable (for the inlining) which acts as a stand-in for the
20461 corresponding formal parameter (of the inline function) will look
20462 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20463 exactly a compile-time constant expression, but it isn't the address
20464 of the (artificial) local variable either. Rather, it represents the
20465 *value* which the artificial local variable always has during its
20466 lifetime. We currently have no way to represent such quasi-constant
20467 values in Dwarf, so for now we just punt and generate nothing. */
20468 return false;
20469
20470 case HIGH:
20471 case CONST_FIXED:
20472 case MINUS:
20473 case SIGN_EXTEND:
20474 case ZERO_EXTEND:
20475 case CONST_POLY_INT:
20476 return false;
20477
20478 case MEM:
20479 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20480 && MEM_READONLY_P (rtl)
20481 && GET_MODE (rtl) == BLKmode)
20482 {
20483 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20484 return true;
20485 }
20486 return false;
20487
20488 default:
20489 /* No other kinds of rtx should be possible here. */
20490 gcc_unreachable ();
20491 }
20492 }
20493
20494 /* Determine whether the evaluation of EXPR references any variables
20495 or functions which aren't otherwise used (and therefore may not be
20496 output). */
20497 static tree
20498 reference_to_unused (tree * tp, int * walk_subtrees,
20499 void * data ATTRIBUTE_UNUSED)
20500 {
20501 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20502 *walk_subtrees = 0;
20503
20504 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20505 && ! TREE_ASM_WRITTEN (*tp))
20506 return *tp;
20507 /* ??? The C++ FE emits debug information for using decls, so
20508 putting gcc_unreachable here falls over. See PR31899. For now
20509 be conservative. */
20510 else if (!symtab->global_info_ready && VAR_P (*tp))
20511 return *tp;
20512 else if (VAR_P (*tp))
20513 {
20514 varpool_node *node = varpool_node::get (*tp);
20515 if (!node || !node->definition)
20516 return *tp;
20517 }
20518 else if (TREE_CODE (*tp) == FUNCTION_DECL
20519 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20520 {
20521 /* The call graph machinery must have finished analyzing,
20522 optimizing and gimplifying the CU by now.
20523 So if *TP has no call graph node associated
20524 to it, it means *TP will not be emitted. */
20525 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20526 return *tp;
20527 }
20528 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20529 return *tp;
20530
20531 return NULL_TREE;
20532 }
20533
20534 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20535 for use in a later add_const_value_attribute call. */
20536
20537 static rtx
20538 rtl_for_decl_init (tree init, tree type)
20539 {
20540 rtx rtl = NULL_RTX;
20541
20542 STRIP_NOPS (init);
20543
20544 /* If a variable is initialized with a string constant without embedded
20545 zeros, build CONST_STRING. */
20546 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20547 {
20548 tree enttype = TREE_TYPE (type);
20549 tree domain = TYPE_DOMAIN (type);
20550 scalar_int_mode mode;
20551
20552 if (is_int_mode (TYPE_MODE (enttype), &mode)
20553 && GET_MODE_SIZE (mode) == 1
20554 && domain
20555 && TYPE_MAX_VALUE (domain)
20556 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20557 && integer_zerop (TYPE_MIN_VALUE (domain))
20558 && compare_tree_int (TYPE_MAX_VALUE (domain),
20559 TREE_STRING_LENGTH (init) - 1) == 0
20560 && ((size_t) TREE_STRING_LENGTH (init)
20561 == strlen (TREE_STRING_POINTER (init)) + 1))
20562 {
20563 rtl = gen_rtx_CONST_STRING (VOIDmode,
20564 ggc_strdup (TREE_STRING_POINTER (init)));
20565 rtl = gen_rtx_MEM (BLKmode, rtl);
20566 MEM_READONLY_P (rtl) = 1;
20567 }
20568 }
20569 /* Other aggregates, and complex values, could be represented using
20570 CONCAT: FIXME!
20571 If this changes, please adjust tree_add_const_value_attribute
20572 so that for early_dwarf it will for such initializers mangle referenced
20573 decls. */
20574 else if (AGGREGATE_TYPE_P (type)
20575 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20576 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20577 || TREE_CODE (type) == COMPLEX_TYPE)
20578 ;
20579 /* Vectors only work if their mode is supported by the target.
20580 FIXME: generic vectors ought to work too. */
20581 else if (TREE_CODE (type) == VECTOR_TYPE
20582 && !VECTOR_MODE_P (TYPE_MODE (type)))
20583 ;
20584 /* If the initializer is something that we know will expand into an
20585 immediate RTL constant, expand it now. We must be careful not to
20586 reference variables which won't be output. */
20587 else if (initializer_constant_valid_p (init, type)
20588 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20589 {
20590 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20591 possible. */
20592 if (TREE_CODE (type) == VECTOR_TYPE)
20593 switch (TREE_CODE (init))
20594 {
20595 case VECTOR_CST:
20596 break;
20597 case CONSTRUCTOR:
20598 if (TREE_CONSTANT (init))
20599 {
20600 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20601 bool constant_p = true;
20602 tree value;
20603 unsigned HOST_WIDE_INT ix;
20604
20605 /* Even when ctor is constant, it might contain non-*_CST
20606 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20607 belong into VECTOR_CST nodes. */
20608 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20609 if (!CONSTANT_CLASS_P (value))
20610 {
20611 constant_p = false;
20612 break;
20613 }
20614
20615 if (constant_p)
20616 {
20617 init = build_vector_from_ctor (type, elts);
20618 break;
20619 }
20620 }
20621 /* FALLTHRU */
20622
20623 default:
20624 return NULL;
20625 }
20626
20627 /* Large _BitInt BLKmode INTEGER_CSTs would yield a MEM. */
20628 if (TREE_CODE (init) == INTEGER_CST
20629 && TREE_CODE (TREE_TYPE (init)) == BITINT_TYPE
20630 && TYPE_MODE (TREE_TYPE (init)) == BLKmode)
20631 {
20632 if (tree_fits_shwi_p (init))
20633 return GEN_INT (tree_to_shwi (init));
20634 else
20635 return NULL;
20636 }
20637
20638 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20639
20640 /* If expand_expr returns a MEM, it wasn't immediate. */
20641 gcc_assert (!rtl || !MEM_P (rtl));
20642 }
20643
20644 return rtl;
20645 }
20646
20647 /* Generate RTL for the variable DECL to represent its location. */
20648
20649 static rtx
20650 rtl_for_decl_location (tree decl)
20651 {
20652 rtx rtl;
20653
20654 /* Here we have to decide where we are going to say the parameter "lives"
20655 (as far as the debugger is concerned). We only have a couple of
20656 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20657
20658 DECL_RTL normally indicates where the parameter lives during most of the
20659 activation of the function. If optimization is enabled however, this
20660 could be either NULL or else a pseudo-reg. Both of those cases indicate
20661 that the parameter doesn't really live anywhere (as far as the code
20662 generation parts of GCC are concerned) during most of the function's
20663 activation. That will happen (for example) if the parameter is never
20664 referenced within the function.
20665
20666 We could just generate a location descriptor here for all non-NULL
20667 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20668 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20669 where DECL_RTL is NULL or is a pseudo-reg.
20670
20671 Note however that we can only get away with using DECL_INCOMING_RTL as
20672 a backup substitute for DECL_RTL in certain limited cases. In cases
20673 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20674 we can be sure that the parameter was passed using the same type as it is
20675 declared to have within the function, and that its DECL_INCOMING_RTL
20676 points us to a place where a value of that type is passed.
20677
20678 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20679 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20680 because in these cases DECL_INCOMING_RTL points us to a value of some
20681 type which is *different* from the type of the parameter itself. Thus,
20682 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20683 such cases, the debugger would end up (for example) trying to fetch a
20684 `float' from a place which actually contains the first part of a
20685 `double'. That would lead to really incorrect and confusing
20686 output at debug-time.
20687
20688 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20689 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20690 are a couple of exceptions however. On little-endian machines we can
20691 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20692 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20693 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20694 when (on a little-endian machine) a non-prototyped function has a
20695 parameter declared to be of type `short' or `char'. In such cases,
20696 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20697 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20698 passed `int' value. If the debugger then uses that address to fetch
20699 a `short' or a `char' (on a little-endian machine) the result will be
20700 the correct data, so we allow for such exceptional cases below.
20701
20702 Note that our goal here is to describe the place where the given formal
20703 parameter lives during most of the function's activation (i.e. between the
20704 end of the prologue and the start of the epilogue). We'll do that as best
20705 as we can. Note however that if the given formal parameter is modified
20706 sometime during the execution of the function, then a stack backtrace (at
20707 debug-time) will show the function as having been called with the *new*
20708 value rather than the value which was originally passed in. This happens
20709 rarely enough that it is not a major problem, but it *is* a problem, and
20710 I'd like to fix it.
20711
20712 A future version of dwarf2out.cc may generate two additional attributes for
20713 any given DW_TAG_formal_parameter DIE which will describe the "passed
20714 type" and the "passed location" for the given formal parameter in addition
20715 to the attributes we now generate to indicate the "declared type" and the
20716 "active location" for each parameter. This additional set of attributes
20717 could be used by debuggers for stack backtraces. Separately, note that
20718 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20719 This happens (for example) for inlined-instances of inline function formal
20720 parameters which are never referenced. This really shouldn't be
20721 happening. All PARM_DECL nodes should get valid non-NULL
20722 DECL_INCOMING_RTL values. FIXME. */
20723
20724 /* Use DECL_RTL as the "location" unless we find something better. */
20725 rtl = DECL_RTL_IF_SET (decl);
20726
20727 /* When generating abstract instances, ignore everything except
20728 constants, symbols living in memory, and symbols living in
20729 fixed registers. */
20730 if (! reload_completed)
20731 {
20732 if (rtl
20733 && (CONSTANT_P (rtl)
20734 || (MEM_P (rtl)
20735 && CONSTANT_P (XEXP (rtl, 0)))
20736 || (REG_P (rtl)
20737 && VAR_P (decl)
20738 && TREE_STATIC (decl))))
20739 {
20740 rtl = targetm.delegitimize_address (rtl);
20741 return rtl;
20742 }
20743 rtl = NULL_RTX;
20744 }
20745 else if (TREE_CODE (decl) == PARM_DECL)
20746 {
20747 if (rtl == NULL_RTX
20748 || is_pseudo_reg (rtl)
20749 || (MEM_P (rtl)
20750 && is_pseudo_reg (XEXP (rtl, 0))
20751 && DECL_INCOMING_RTL (decl)
20752 && MEM_P (DECL_INCOMING_RTL (decl))
20753 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20754 {
20755 tree declared_type = TREE_TYPE (decl);
20756 tree passed_type = DECL_ARG_TYPE (decl);
20757 machine_mode dmode = TYPE_MODE (declared_type);
20758 machine_mode pmode = TYPE_MODE (passed_type);
20759
20760 /* This decl represents a formal parameter which was optimized out.
20761 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20762 all cases where (rtl == NULL_RTX) just below. */
20763 if (dmode == pmode)
20764 rtl = DECL_INCOMING_RTL (decl);
20765 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20766 && SCALAR_INT_MODE_P (dmode)
20767 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20768 && DECL_INCOMING_RTL (decl))
20769 {
20770 rtx inc = DECL_INCOMING_RTL (decl);
20771 if (REG_P (inc))
20772 rtl = inc;
20773 else if (MEM_P (inc))
20774 {
20775 if (BYTES_BIG_ENDIAN)
20776 rtl = adjust_address_nv (inc, dmode,
20777 GET_MODE_SIZE (pmode)
20778 - GET_MODE_SIZE (dmode));
20779 else
20780 rtl = inc;
20781 }
20782 }
20783 }
20784
20785 /* If the parm was passed in registers, but lives on the stack, then
20786 make a big endian correction if the mode of the type of the
20787 parameter is not the same as the mode of the rtl. */
20788 /* ??? This is the same series of checks that are made in dbxout.cc before
20789 we reach the big endian correction code there. It isn't clear if all
20790 of these checks are necessary here, but keeping them all is the safe
20791 thing to do. */
20792 else if (MEM_P (rtl)
20793 && XEXP (rtl, 0) != const0_rtx
20794 && ! CONSTANT_P (XEXP (rtl, 0))
20795 /* Not passed in memory. */
20796 && !MEM_P (DECL_INCOMING_RTL (decl))
20797 /* Not passed by invisible reference. */
20798 && (!REG_P (XEXP (rtl, 0))
20799 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20800 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20801 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20802 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20803 #endif
20804 )
20805 /* Big endian correction check. */
20806 && BYTES_BIG_ENDIAN
20807 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20808 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20809 UNITS_PER_WORD))
20810 {
20811 machine_mode addr_mode = get_address_mode (rtl);
20812 poly_int64 offset = (UNITS_PER_WORD
20813 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20814
20815 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20816 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20817 }
20818 }
20819 else if (VAR_P (decl)
20820 && rtl
20821 && MEM_P (rtl)
20822 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20823 {
20824 machine_mode addr_mode = get_address_mode (rtl);
20825 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20826 GET_MODE (rtl));
20827
20828 /* If a variable is declared "register" yet is smaller than
20829 a register, then if we store the variable to memory, it
20830 looks like we're storing a register-sized value, when in
20831 fact we are not. We need to adjust the offset of the
20832 storage location to reflect the actual value's bytes,
20833 else gdb will not be able to display it. */
20834 if (maybe_ne (offset, 0))
20835 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20836 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20837 }
20838
20839 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20840 and will have been substituted directly into all expressions that use it.
20841 C does not have such a concept, but C++ and other languages do. */
20842 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20843 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20844
20845 if (rtl)
20846 rtl = targetm.delegitimize_address (rtl);
20847
20848 /* If we don't look past the constant pool, we risk emitting a
20849 reference to a constant pool entry that isn't referenced from
20850 code, and thus is not emitted. */
20851 if (rtl)
20852 rtl = avoid_constant_pool_reference (rtl);
20853
20854 /* Try harder to get a rtl. If this symbol ends up not being emitted
20855 in the current CU, resolve_addr will remove the expression referencing
20856 it. */
20857 if (rtl == NULL_RTX
20858 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20859 && VAR_P (decl)
20860 && !DECL_EXTERNAL (decl)
20861 && TREE_STATIC (decl)
20862 && DECL_NAME (decl)
20863 && !DECL_HARD_REGISTER (decl)
20864 && DECL_MODE (decl) != VOIDmode)
20865 {
20866 rtl = make_decl_rtl_for_debug (decl);
20867 if (!MEM_P (rtl)
20868 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20869 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20870 rtl = NULL_RTX;
20871 }
20872
20873 return rtl;
20874 }
20875
20876 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20877 returned. If so, the decl for the COMMON block is returned, and the
20878 value is the offset into the common block for the symbol. */
20879
20880 static tree
20881 fortran_common (tree decl, HOST_WIDE_INT *value)
20882 {
20883 tree val_expr, cvar;
20884 machine_mode mode;
20885 poly_int64 bitsize, bitpos;
20886 tree offset;
20887 HOST_WIDE_INT cbitpos;
20888 int unsignedp, reversep, volatilep = 0;
20889
20890 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20891 it does not have a value (the offset into the common area), or if it
20892 is thread local (as opposed to global) then it isn't common, and shouldn't
20893 be handled as such. */
20894 if (!VAR_P (decl)
20895 || !TREE_STATIC (decl)
20896 || !DECL_HAS_VALUE_EXPR_P (decl)
20897 || !is_fortran ())
20898 return NULL_TREE;
20899
20900 val_expr = DECL_VALUE_EXPR (decl);
20901 if (TREE_CODE (val_expr) != COMPONENT_REF)
20902 return NULL_TREE;
20903
20904 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20905 &unsignedp, &reversep, &volatilep);
20906
20907 if (cvar == NULL_TREE
20908 || !VAR_P (cvar)
20909 || DECL_ARTIFICIAL (cvar)
20910 || !TREE_PUBLIC (cvar)
20911 /* We don't expect to have to cope with variable offsets,
20912 since at present all static data must have a constant size. */
20913 || !bitpos.is_constant (&cbitpos))
20914 return NULL_TREE;
20915
20916 *value = 0;
20917 if (offset != NULL)
20918 {
20919 if (!tree_fits_shwi_p (offset))
20920 return NULL_TREE;
20921 *value = tree_to_shwi (offset);
20922 }
20923 if (cbitpos != 0)
20924 *value += cbitpos / BITS_PER_UNIT;
20925
20926 return cvar;
20927 }
20928
20929 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20930 data attribute for a variable or a parameter. We generate the
20931 DW_AT_const_value attribute only in those cases where the given variable
20932 or parameter does not have a true "location" either in memory or in a
20933 register. This can happen (for example) when a constant is passed as an
20934 actual argument in a call to an inline function. (It's possible that
20935 these things can crop up in other ways also.) Note that one type of
20936 constant value which can be passed into an inlined function is a constant
20937 pointer. This can happen for example if an actual argument in an inlined
20938 function call evaluates to a compile-time constant address.
20939
20940 CACHE_P is true if it is worth caching the location list for DECL,
20941 so that future calls can reuse it rather than regenerate it from scratch.
20942 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20943 since we will need to refer to them each time the function is inlined. */
20944
20945 static bool
20946 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20947 {
20948 rtx rtl;
20949 dw_loc_list_ref list;
20950 var_loc_list *loc_list;
20951 cached_dw_loc_list *cache;
20952
20953 if (early_dwarf)
20954 return false;
20955
20956 if (TREE_CODE (decl) == ERROR_MARK)
20957 return false;
20958
20959 if (get_AT (die, DW_AT_location)
20960 || get_AT (die, DW_AT_const_value))
20961 return true;
20962
20963 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20964 || TREE_CODE (decl) == RESULT_DECL);
20965
20966 /* Try to get some constant RTL for this decl, and use that as the value of
20967 the location. */
20968
20969 rtl = rtl_for_decl_location (decl);
20970 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20971 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20972 return true;
20973
20974 /* See if we have single element location list that is equivalent to
20975 a constant value. That way we are better to use add_const_value_attribute
20976 rather than expanding constant value equivalent. */
20977 loc_list = lookup_decl_loc (decl);
20978 if (loc_list
20979 && loc_list->first
20980 && loc_list->first->next == NULL
20981 && NOTE_P (loc_list->first->loc)
20982 && NOTE_VAR_LOCATION (loc_list->first->loc)
20983 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20984 {
20985 struct var_loc_node *node;
20986
20987 node = loc_list->first;
20988 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20989 if (GET_CODE (rtl) == EXPR_LIST)
20990 rtl = XEXP (rtl, 0);
20991 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20992 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20993 return true;
20994 }
20995 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20996 list several times. See if we've already cached the contents. */
20997 list = NULL;
20998 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20999 cache_p = false;
21000 if (cache_p)
21001 {
21002 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
21003 if (cache)
21004 list = cache->loc_list;
21005 }
21006 if (list == NULL)
21007 {
21008 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
21009 NULL);
21010 /* It is usually worth caching this result if the decl is from
21011 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
21012 if (cache_p && list && list->dw_loc_next)
21013 {
21014 cached_dw_loc_list **slot
21015 = cached_dw_loc_list_table->find_slot_with_hash (decl,
21016 DECL_UID (decl),
21017 INSERT);
21018 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
21019 cache->decl_id = DECL_UID (decl);
21020 cache->loc_list = list;
21021 *slot = cache;
21022 }
21023 }
21024 if (list)
21025 {
21026 add_AT_location_description (die, DW_AT_location, list);
21027 return true;
21028 }
21029 /* None of that worked, so it must not really have a location;
21030 try adding a constant value attribute from the DECL_INITIAL. */
21031 return tree_add_const_value_attribute_for_decl (die, decl);
21032 }
21033
21034 /* Mangle referenced decls. */
21035 static tree
21036 mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
21037 {
21038 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
21039 *walk_subtrees = 0;
21040
21041 if (VAR_OR_FUNCTION_DECL_P (*tp))
21042 assign_assembler_name_if_needed (*tp);
21043
21044 return NULL_TREE;
21045 }
21046
21047 /* Attach a DW_AT_const_value attribute to DIE. The value of the
21048 attribute is the const value T. */
21049
21050 static bool
21051 tree_add_const_value_attribute (dw_die_ref die, tree t)
21052 {
21053 tree init;
21054 tree type = TREE_TYPE (t);
21055
21056 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
21057 return false;
21058
21059 init = t;
21060 gcc_assert (!DECL_P (init));
21061
21062 if (TREE_CODE (init) == INTEGER_CST)
21063 {
21064 if (tree_fits_uhwi_p (init))
21065 {
21066 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
21067 return true;
21068 }
21069 if (tree_fits_shwi_p (init))
21070 {
21071 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
21072 return true;
21073 }
21074 }
21075 if (!early_dwarf)
21076 {
21077 rtx rtl = rtl_for_decl_init (init, type);
21078 if (rtl)
21079 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
21080 }
21081 else
21082 {
21083 /* For early_dwarf force mangling of all referenced symbols. */
21084 tree initializer = init;
21085 STRIP_NOPS (initializer);
21086 /* rtl_for_decl_init punts on other aggregates, and complex values. */
21087 if (AGGREGATE_TYPE_P (type)
21088 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
21089 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
21090 || TREE_CODE (type) == COMPLEX_TYPE)
21091 ;
21092 else if (initializer_constant_valid_p (initializer, type))
21093 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
21094 }
21095 /* If the host and target are sane, try harder. */
21096 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
21097 && initializer_constant_valid_p (init, type))
21098 {
21099 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
21100 if (size > 0 && (int) size == size)
21101 {
21102 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
21103
21104 if (native_encode_initializer (init, array, size) == size)
21105 {
21106 add_AT_vec (die, DW_AT_const_value, size, 1, array);
21107 return true;
21108 }
21109 ggc_free (array);
21110 }
21111 }
21112 return false;
21113 }
21114
21115 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
21116 attribute is the const value of T, where T is an integral constant
21117 variable with static storage duration
21118 (so it can't be a PARM_DECL or a RESULT_DECL). */
21119
21120 static bool
21121 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
21122 {
21123
21124 if (!decl
21125 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
21126 || (VAR_P (decl) && !TREE_STATIC (decl)))
21127 return false;
21128
21129 if (TREE_READONLY (decl)
21130 && ! TREE_THIS_VOLATILE (decl)
21131 && DECL_INITIAL (decl))
21132 /* OK */;
21133 else
21134 return false;
21135
21136 /* Don't add DW_AT_const_value if abstract origin already has one. */
21137 if (get_AT (var_die, DW_AT_const_value))
21138 return false;
21139
21140 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
21141 }
21142
21143 /* Convert the CFI instructions for the current function into a
21144 location list. This is used for DW_AT_frame_base when we targeting
21145 a dwarf2 consumer that does not support the dwarf3
21146 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21147 expressions. */
21148
21149 static dw_loc_list_ref
21150 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21151 {
21152 int ix;
21153 dw_fde_ref fde;
21154 dw_loc_list_ref list, *list_tail;
21155 dw_cfi_ref cfi;
21156 dw_cfa_location last_cfa, next_cfa;
21157 const char *start_label, *last_label, *section;
21158 dw_cfa_location remember;
21159
21160 fde = cfun->fde;
21161 gcc_assert (fde != NULL);
21162
21163 section = secname_for_decl (current_function_decl);
21164 list_tail = &list;
21165 list = NULL;
21166
21167 memset (&next_cfa, 0, sizeof (next_cfa));
21168
21169 #ifdef CODEVIEW_DEBUGGING_INFO
21170 /* We can write simplified frame base information for CodeView, as we're
21171 not using it for rewinding. */
21172 if (codeview_debuginfo_p ())
21173 {
21174 int dwreg = DEBUGGER_REGNO (cfun->machine->fs.cfa_reg->u.reg.regno);
21175
21176 next_cfa.reg.set_by_dwreg (dwreg);
21177 next_cfa.offset = cfun->machine->fs.fp_valid
21178 ? cfun->machine->fs.fp_offset : cfun->machine->fs.sp_offset;
21179
21180 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21181 fde->dw_fde_begin, 0,
21182 fde->dw_fde_second_begin
21183 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21184 section);
21185 maybe_gen_llsym (list);
21186
21187 return list;
21188 }
21189 #endif
21190
21191 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21192 remember = next_cfa;
21193
21194 start_label = fde->dw_fde_begin;
21195
21196 /* ??? Bald assumption that the CIE opcode list does not contain
21197 advance opcodes. */
21198 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21199 lookup_cfa_1 (cfi, &next_cfa, &remember);
21200
21201 last_cfa = next_cfa;
21202 last_label = start_label;
21203
21204 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21205 {
21206 /* If the first partition contained no CFI adjustments, the
21207 CIE opcodes apply to the whole first partition. */
21208 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21209 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
21210 list_tail =&(*list_tail)->dw_loc_next;
21211 start_label = last_label = fde->dw_fde_second_begin;
21212 }
21213
21214 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21215 {
21216 switch (cfi->dw_cfi_opc)
21217 {
21218 case DW_CFA_set_loc:
21219 case DW_CFA_advance_loc1:
21220 case DW_CFA_advance_loc2:
21221 case DW_CFA_advance_loc4:
21222 if (!cfa_equal_p (&last_cfa, &next_cfa))
21223 {
21224 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21225 start_label, 0, last_label, 0, section);
21226
21227 list_tail = &(*list_tail)->dw_loc_next;
21228 last_cfa = next_cfa;
21229 start_label = last_label;
21230 }
21231 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21232 break;
21233
21234 case DW_CFA_advance_loc:
21235 /* The encoding is complex enough that we should never emit this. */
21236 gcc_unreachable ();
21237
21238 default:
21239 lookup_cfa_1 (cfi, &next_cfa, &remember);
21240 break;
21241 }
21242 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21243 {
21244 if (!cfa_equal_p (&last_cfa, &next_cfa))
21245 {
21246 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21247 start_label, 0, last_label, 0, section);
21248
21249 list_tail = &(*list_tail)->dw_loc_next;
21250 last_cfa = next_cfa;
21251 start_label = last_label;
21252 }
21253 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21254 start_label, 0, fde->dw_fde_end, 0, section);
21255 list_tail = &(*list_tail)->dw_loc_next;
21256 start_label = last_label = fde->dw_fde_second_begin;
21257 }
21258 }
21259
21260 if (!cfa_equal_p (&last_cfa, &next_cfa))
21261 {
21262 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21263 start_label, 0, last_label, 0, section);
21264 list_tail = &(*list_tail)->dw_loc_next;
21265 start_label = last_label;
21266 }
21267
21268 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21269 start_label, 0,
21270 fde->dw_fde_second_begin
21271 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21272 section);
21273
21274 maybe_gen_llsym (list);
21275
21276 return list;
21277 }
21278
21279 /* Compute a displacement from the "steady-state frame pointer" to the
21280 frame base (often the same as the CFA), and store it in
21281 frame_pointer_fb_offset. OFFSET is added to the displacement
21282 before the latter is negated. */
21283
21284 static void
21285 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21286 {
21287 rtx reg, elim;
21288
21289 #ifdef FRAME_POINTER_CFA_OFFSET
21290 reg = frame_pointer_rtx;
21291 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21292 #else
21293 reg = arg_pointer_rtx;
21294 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21295 #endif
21296
21297 elim = (ira_use_lra_p
21298 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21299 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21300 elim = strip_offset_and_add (elim, &offset);
21301
21302 frame_pointer_fb_offset = -offset;
21303
21304 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21305 in which to eliminate. This is because it's stack pointer isn't
21306 directly accessible as a register within the ISA. To work around
21307 this, assume that while we cannot provide a proper value for
21308 frame_pointer_fb_offset, we won't need one either. We can use
21309 hard frame pointer in debug info even if frame pointer isn't used
21310 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21311 which uses the DW_AT_frame_base attribute, not hard frame pointer
21312 directly. */
21313 frame_pointer_fb_offset_valid
21314 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21315 }
21316
21317 /* Generate a DW_AT_name attribute given some string value to be included as
21318 the value of the attribute. */
21319
21320 void
21321 add_name_attribute (dw_die_ref die, const char *name_string)
21322 {
21323 if (name_string != NULL && *name_string != 0)
21324 {
21325 if (demangle_name_func)
21326 name_string = (*demangle_name_func) (name_string);
21327
21328 add_AT_string (die, DW_AT_name, name_string);
21329 }
21330 }
21331
21332 /* Generate a DW_AT_name attribute given some string value representing a
21333 file or filepath to be included as value of the attribute. */
21334 static void
21335 add_filename_attribute (dw_die_ref die, const char *name_string)
21336 {
21337 if (name_string != NULL && *name_string != 0)
21338 add_filepath_AT_string (die, DW_AT_name, name_string);
21339 }
21340
21341 /* Generate a DW_AT_description attribute given some string value to be included
21342 as the value of the attribute. */
21343
21344 static void
21345 add_desc_attribute (dw_die_ref die, const char *name_string)
21346 {
21347 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21348 return;
21349
21350 if (name_string == NULL || *name_string == 0)
21351 return;
21352
21353 if (demangle_name_func)
21354 name_string = (*demangle_name_func) (name_string);
21355
21356 add_AT_string (die, DW_AT_description, name_string);
21357 }
21358
21359 /* Generate a DW_AT_description attribute given some decl to be included
21360 as the value of the attribute. */
21361
21362 static void
21363 add_desc_attribute (dw_die_ref die, tree decl)
21364 {
21365 tree decl_name;
21366
21367 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21368 return;
21369
21370 if (decl == NULL_TREE || !DECL_P (decl))
21371 return;
21372 decl_name = DECL_NAME (decl);
21373
21374 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21375 {
21376 const char *name = dwarf2_name (decl, 0);
21377 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21378 }
21379 else
21380 {
21381 char *desc = print_generic_expr_to_str (decl);
21382 add_desc_attribute (die, desc);
21383 free (desc);
21384 }
21385 }
21386
21387 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21388 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21389 of TYPE accordingly.
21390
21391 ??? This is a temporary measure until after we're able to generate
21392 regular DWARF for the complex Ada type system. */
21393
21394 static void
21395 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21396 dw_die_ref context_die)
21397 {
21398 tree dtype;
21399 dw_die_ref dtype_die;
21400
21401 if (!lang_hooks.types.descriptive_type)
21402 return;
21403
21404 dtype = lang_hooks.types.descriptive_type (type);
21405 if (!dtype)
21406 return;
21407
21408 dtype_die = lookup_type_die (dtype);
21409 if (!dtype_die)
21410 {
21411 gen_type_die (dtype, context_die);
21412 dtype_die = lookup_type_die (dtype);
21413 gcc_assert (dtype_die);
21414 }
21415
21416 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21417 }
21418
21419 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21420
21421 static const char *
21422 comp_dir_string (void)
21423 {
21424 const char *wd;
21425 char *wd_plus_sep = NULL;
21426 static const char *cached_wd = NULL;
21427
21428 if (cached_wd != NULL)
21429 return cached_wd;
21430
21431 wd = get_src_pwd ();
21432 if (wd == NULL)
21433 return NULL;
21434
21435 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21436 {
21437 size_t wdlen = strlen (wd);
21438 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21439 strcpy (wd_plus_sep, wd);
21440 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21441 wd_plus_sep [wdlen + 1] = 0;
21442 wd = wd_plus_sep;
21443 }
21444
21445 cached_wd = remap_debug_filename (wd);
21446
21447 /* remap_debug_filename can just pass through wd or return a new gc string.
21448 These two types can't be both stored in a GTY(())-tagged string, but since
21449 the cached value lives forever just copy it if needed. */
21450 if (cached_wd != wd)
21451 {
21452 cached_wd = xstrdup (cached_wd);
21453 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21454 free (wd_plus_sep);
21455 }
21456
21457 return cached_wd;
21458 }
21459
21460 /* Generate a DW_AT_comp_dir attribute for DIE. */
21461
21462 static void
21463 add_comp_dir_attribute (dw_die_ref die)
21464 {
21465 const char * wd = comp_dir_string ();
21466 if (wd != NULL)
21467 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21468 }
21469
21470 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21471 pointer computation, ...), output a representation for that bound according
21472 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21473 loc_list_from_tree for the meaning of CONTEXT. */
21474
21475 static void
21476 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21477 int forms, struct loc_descr_context *context)
21478 {
21479 dw_die_ref context_die, decl_die = NULL;
21480 dw_loc_list_ref list;
21481 bool strip_conversions = true;
21482 bool placeholder_seen = false;
21483
21484 while (strip_conversions)
21485 switch (TREE_CODE (value))
21486 {
21487 case ERROR_MARK:
21488 case SAVE_EXPR:
21489 return;
21490
21491 CASE_CONVERT:
21492 case VIEW_CONVERT_EXPR:
21493 value = TREE_OPERAND (value, 0);
21494 break;
21495
21496 default:
21497 strip_conversions = false;
21498 break;
21499 }
21500
21501 /* If possible and permitted, output the attribute as a constant. */
21502 if ((forms & dw_scalar_form_constant) != 0
21503 && TREE_CODE (value) == INTEGER_CST)
21504 {
21505 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21506
21507 /* If HOST_WIDE_INT is big enough then represent the bound as
21508 a constant value. We need to choose a form based on
21509 whether the type is signed or unsigned. We cannot just
21510 call add_AT_unsigned if the value itself is positive
21511 (add_AT_unsigned might add the unsigned value encoded as
21512 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21513 bounds type and then sign extend any unsigned values found
21514 for signed types. This is needed only for
21515 DW_AT_{lower,upper}_bound, since for most other attributes,
21516 consumers will treat DW_FORM_data[1248] as unsigned values,
21517 regardless of the underlying type. */
21518 if (prec <= HOST_BITS_PER_WIDE_INT
21519 || tree_fits_uhwi_p (value))
21520 {
21521 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21522 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21523 else
21524 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21525 }
21526 else if (dwarf_version >= 5
21527 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21528 /* Otherwise represent the bound as an unsigned value with
21529 the precision of its type. The precision and signedness
21530 of the type will be necessary to re-interpret it
21531 unambiguously. */
21532 add_AT_wide (die, attr, wi::to_wide (value));
21533 else
21534 {
21535 rtx v = immed_wide_int_const (wi::to_wide (value),
21536 TYPE_MODE (TREE_TYPE (value)));
21537 dw_loc_descr_ref loc
21538 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21539 VAR_INIT_STATUS_INITIALIZED);
21540 if (loc)
21541 add_AT_loc (die, attr, loc);
21542 }
21543 return;
21544 }
21545
21546 /* Otherwise, if it's possible and permitted too, output a reference to
21547 another DIE. */
21548 if ((forms & dw_scalar_form_reference) != 0)
21549 {
21550 tree decl = NULL_TREE;
21551
21552 /* Some type attributes reference an outer type. For instance, the upper
21553 bound of an array may reference an embedding record (this happens in
21554 Ada). */
21555 if (TREE_CODE (value) == COMPONENT_REF
21556 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21557 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21558 decl = TREE_OPERAND (value, 1);
21559
21560 else if (VAR_P (value)
21561 || TREE_CODE (value) == PARM_DECL
21562 || TREE_CODE (value) == RESULT_DECL)
21563 decl = value;
21564
21565 if (decl != NULL_TREE)
21566 {
21567 decl_die = lookup_decl_die (decl);
21568
21569 /* ??? Can this happen, or should the variable have been bound
21570 first? Probably it can, since I imagine that we try to create
21571 the types of parameters in the order in which they exist in
21572 the list, and won't have created a forward reference to a
21573 later parameter. */
21574 if (decl_die != NULL)
21575 {
21576 if (get_AT (decl_die, DW_AT_location)
21577 || get_AT (decl_die, DW_AT_data_member_location)
21578 || get_AT (decl_die, DW_AT_data_bit_offset)
21579 || get_AT (decl_die, DW_AT_const_value))
21580 {
21581 add_AT_die_ref (die, attr, decl_die);
21582 return;
21583 }
21584 }
21585 }
21586 }
21587
21588 /* Last chance: try to create a stack operation procedure to evaluate the
21589 value. Do nothing if even that is not possible or permitted. */
21590 if ((forms & dw_scalar_form_exprloc) == 0)
21591 return;
21592
21593 list = loc_list_from_tree (value, 2, context);
21594 if (context && context->placeholder_arg)
21595 {
21596 placeholder_seen = context->placeholder_seen;
21597 context->placeholder_seen = false;
21598 }
21599 if (list == NULL || single_element_loc_list_p (list))
21600 {
21601 /* If this attribute is not a reference nor constant, it is
21602 a DWARF expression rather than location description. For that
21603 loc_list_from_tree (value, 0, &context) is needed. */
21604 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21605 if (list2 && single_element_loc_list_p (list2))
21606 {
21607 if (placeholder_seen)
21608 {
21609 struct dwarf_procedure_info dpi;
21610 dpi.fndecl = NULL_TREE;
21611 dpi.args_count = 1;
21612 if (!resolve_args_picking (list2->expr, 1, &dpi))
21613 return;
21614 }
21615 add_AT_loc (die, attr, list2->expr);
21616 return;
21617 }
21618 }
21619
21620 /* If that failed to give a single element location list, fall back to
21621 outputting this as a reference... still if permitted. */
21622 if (list == NULL
21623 || (forms & dw_scalar_form_reference) == 0
21624 || placeholder_seen)
21625 return;
21626
21627 if (!decl_die)
21628 {
21629 if (current_function_decl == 0)
21630 context_die = comp_unit_die ();
21631 else
21632 context_die = lookup_decl_die (current_function_decl);
21633
21634 decl_die = new_die (DW_TAG_variable, context_die, value);
21635 add_AT_flag (decl_die, DW_AT_artificial, 1);
21636 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21637 context_die);
21638 }
21639
21640 add_AT_location_description (decl_die, DW_AT_location, list);
21641 add_AT_die_ref (die, attr, decl_die);
21642 }
21643
21644 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21645 default. */
21646
21647 static int
21648 lower_bound_default (void)
21649 {
21650 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21651 {
21652 case DW_LANG_C:
21653 case DW_LANG_C89:
21654 case DW_LANG_C99:
21655 case DW_LANG_C11:
21656 case DW_LANG_C_plus_plus:
21657 case DW_LANG_C_plus_plus_11:
21658 case DW_LANG_C_plus_plus_14:
21659 case DW_LANG_ObjC:
21660 case DW_LANG_ObjC_plus_plus:
21661 return 0;
21662 case DW_LANG_Fortran77:
21663 case DW_LANG_Fortran90:
21664 case DW_LANG_Fortran95:
21665 case DW_LANG_Fortran03:
21666 case DW_LANG_Fortran08:
21667 return 1;
21668 case DW_LANG_UPC:
21669 case DW_LANG_D:
21670 case DW_LANG_Python:
21671 return dwarf_version >= 4 ? 0 : -1;
21672 case DW_LANG_Ada95:
21673 case DW_LANG_Ada83:
21674 case DW_LANG_Cobol74:
21675 case DW_LANG_Cobol85:
21676 case DW_LANG_Modula2:
21677 case DW_LANG_PLI:
21678 return dwarf_version >= 4 ? 1 : -1;
21679 default:
21680 return -1;
21681 }
21682 }
21683
21684 /* Given a tree node describing an array bound (either lower or upper) output
21685 a representation for that bound. */
21686
21687 static void
21688 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21689 tree bound, struct loc_descr_context *context)
21690 {
21691 int dflt;
21692
21693 while (1)
21694 switch (TREE_CODE (bound))
21695 {
21696 /* Strip all conversions. */
21697 CASE_CONVERT:
21698 case VIEW_CONVERT_EXPR:
21699 bound = TREE_OPERAND (bound, 0);
21700 break;
21701
21702 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21703 are even omitted when they are the default. */
21704 case INTEGER_CST:
21705 /* If the value for this bound is the default one, we can even omit the
21706 attribute. */
21707 if (bound_attr == DW_AT_lower_bound
21708 && tree_fits_shwi_p (bound)
21709 && (dflt = lower_bound_default ()) != -1
21710 && tree_to_shwi (bound) == dflt)
21711 return;
21712
21713 /* FALLTHRU */
21714
21715 default:
21716 /* Let GNAT encodings do the magic for self-referential bounds. */
21717 if (is_ada ()
21718 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21719 && contains_placeholder_p (bound))
21720 return;
21721
21722 add_scalar_info (subrange_die, bound_attr, bound,
21723 dw_scalar_form_constant
21724 | dw_scalar_form_exprloc
21725 | dw_scalar_form_reference,
21726 context);
21727 return;
21728 }
21729 }
21730
21731 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21732 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21733
21734 This function reuses previously set type and bound information if
21735 available. */
21736
21737 static void
21738 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21739 {
21740 dw_die_ref child = type_die->die_child;
21741 struct array_descr_info info;
21742 int dimension_number;
21743
21744 if (lang_hooks.types.get_array_descr_info)
21745 {
21746 memset (&info, 0, sizeof (info));
21747 if (lang_hooks.types.get_array_descr_info (type, &info))
21748 /* Fortran sometimes emits array types with no dimension. */
21749 gcc_assert (info.ndimensions >= 0
21750 && info.ndimensions
21751 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21752 }
21753 else
21754 info.ndimensions = 0;
21755
21756 for (dimension_number = 0;
21757 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21758 type = TREE_TYPE (type), dimension_number++)
21759 {
21760 tree domain = TYPE_DOMAIN (type);
21761
21762 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21763 break;
21764
21765 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21766 and (in GNU C only) variable bounds. Handle all three forms
21767 here. */
21768
21769 /* Find and reuse a previously generated DW_TAG_subrange_type if
21770 available.
21771
21772 For multi-dimensional arrays, as we iterate through the
21773 various dimensions in the enclosing for loop above, we also
21774 iterate through the DIE children and pick at each
21775 DW_TAG_subrange_type previously generated (if available).
21776 Each child DW_TAG_subrange_type DIE describes the range of
21777 the current dimension. At this point we should have as many
21778 DW_TAG_subrange_type's as we have dimensions in the
21779 array. */
21780 dw_die_ref subrange_die = NULL;
21781 if (child)
21782 while (1)
21783 {
21784 child = child->die_sib;
21785 if (child->die_tag == DW_TAG_subrange_type)
21786 subrange_die = child;
21787 if (child == type_die->die_child)
21788 {
21789 /* If we wrapped around, stop looking next time. */
21790 child = NULL;
21791 break;
21792 }
21793 if (child->die_tag == DW_TAG_subrange_type)
21794 break;
21795 }
21796 if (!subrange_die)
21797 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21798
21799 if (domain)
21800 {
21801 /* We have an array type with specified bounds. */
21802 tree lower = TYPE_MIN_VALUE (domain);
21803 tree upper = TYPE_MAX_VALUE (domain);
21804 tree index_type = TREE_TYPE (domain);
21805
21806 if (dimension_number <= info.ndimensions - 1)
21807 {
21808 lower = info.dimen[dimension_number].lower_bound;
21809 upper = info.dimen[dimension_number].upper_bound;
21810 index_type = info.dimen[dimension_number].bounds_type;
21811 }
21812
21813 /* Define the index type. */
21814 if (index_type && !get_AT (subrange_die, DW_AT_type))
21815 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21816 false, type_die);
21817
21818 /* ??? If upper is NULL, the array has unspecified length,
21819 but it does have a lower bound. This happens with Fortran
21820 dimension arr(N:*)
21821 Since the debugger is definitely going to need to know N
21822 to produce useful results, go ahead and output the lower
21823 bound solo, and hope the debugger can cope. */
21824
21825 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21826 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21827
21828 if (!get_AT (subrange_die, DW_AT_upper_bound)
21829 && !get_AT (subrange_die, DW_AT_count))
21830 {
21831 if (upper)
21832 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21833 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21834 /* Zero-length array. */
21835 add_bound_info (subrange_die, DW_AT_count,
21836 build_int_cst (TREE_TYPE (lower), 0), NULL);
21837 }
21838 }
21839
21840 /* Otherwise we have an array type with an unspecified length. The
21841 DWARF-2 spec does not say how to handle this; let's just leave out the
21842 bounds. */
21843 }
21844 }
21845
21846 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21847
21848 static void
21849 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21850 {
21851 dw_die_ref decl_die;
21852 HOST_WIDE_INT size;
21853
21854 switch (TREE_CODE (tree_node))
21855 {
21856 case ERROR_MARK:
21857 size = 0;
21858 break;
21859 case ENUMERAL_TYPE:
21860 case RECORD_TYPE:
21861 case UNION_TYPE:
21862 case QUAL_UNION_TYPE:
21863 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21864 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21865 {
21866 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21867 return;
21868 }
21869 size = int_size_in_bytes (tree_node);
21870 break;
21871 case FIELD_DECL:
21872 /* For a data member of a struct or union, the DW_AT_byte_size is
21873 generally given as the number of bytes normally allocated for an
21874 object of the *declared* type of the member itself. This is true
21875 even for bit-fields. */
21876 size = int_size_in_bytes (field_type (tree_node));
21877 break;
21878 default:
21879 gcc_unreachable ();
21880 }
21881
21882 /* Note that `size' might be -1 when we get to this point. If it is, that
21883 indicates that the byte size of the entity in question is variable. */
21884 if (size >= 0)
21885 add_AT_unsigned (die, DW_AT_byte_size, size);
21886
21887 /* Support for dynamically-sized objects was introduced in DWARF3. */
21888 else if (TYPE_P (tree_node)
21889 && (dwarf_version >= 3 || !dwarf_strict)
21890 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21891 {
21892 struct loc_descr_context ctx = {
21893 const_cast<tree> (tree_node), /* context_type */
21894 NULL_TREE, /* base_decl */
21895 NULL, /* dpi */
21896 false, /* placeholder_arg */
21897 false, /* placeholder_seen */
21898 false /* strict_signedness */
21899 };
21900
21901 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21902 add_scalar_info (die, DW_AT_byte_size, tree_size,
21903 dw_scalar_form_constant
21904 | dw_scalar_form_exprloc
21905 | dw_scalar_form_reference,
21906 &ctx);
21907 }
21908 }
21909
21910 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21911 alignment. */
21912
21913 static void
21914 add_alignment_attribute (dw_die_ref die, tree tree_node)
21915 {
21916 if (dwarf_version < 5 && dwarf_strict)
21917 return;
21918
21919 unsigned align;
21920
21921 if (DECL_P (tree_node))
21922 {
21923 if (!DECL_USER_ALIGN (tree_node))
21924 return;
21925
21926 align = DECL_ALIGN_UNIT (tree_node);
21927 }
21928 else if (TYPE_P (tree_node))
21929 {
21930 if (!TYPE_USER_ALIGN (tree_node))
21931 return;
21932
21933 align = TYPE_ALIGN_UNIT (tree_node);
21934 }
21935 else
21936 gcc_unreachable ();
21937
21938 add_AT_unsigned (die, DW_AT_alignment, align);
21939 }
21940
21941 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21942 which specifies the distance in bits from the highest order bit of the
21943 "containing object" for the bit-field to the highest order bit of the
21944 bit-field itself.
21945
21946 For any given bit-field, the "containing object" is a hypothetical object
21947 (of some integral or enum type) within which the given bit-field lives. The
21948 type of this hypothetical "containing object" is always the same as the
21949 declared type of the individual bit-field itself. The determination of the
21950 exact location of the "containing object" for a bit-field is rather
21951 complicated. It's handled by the `field_byte_offset' function (above).
21952
21953 Note that it is the size (in bytes) of the hypothetical "containing object"
21954 which will be given in the DW_AT_byte_size attribute for this bit-field.
21955 (See `byte_size_attribute' above). */
21956
21957 static inline void
21958 add_bit_offset_attribute (dw_die_ref die, tree decl)
21959 {
21960 HOST_WIDE_INT object_offset_in_bytes;
21961 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21962 HOST_WIDE_INT bitpos_int;
21963 HOST_WIDE_INT highest_order_object_bit_offset;
21964 HOST_WIDE_INT highest_order_field_bit_offset;
21965 HOST_WIDE_INT bit_offset;
21966
21967 /* The containing object is within the DECL_CONTEXT. */
21968 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21969
21970 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21971
21972 /* Must be a field and a bit field. */
21973 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21974
21975 /* We can't yet handle bit-fields whose offsets are variable, so if we
21976 encounter such things, just return without generating any attribute
21977 whatsoever. Likewise for variable or too large size. */
21978 if (! tree_fits_shwi_p (bit_position (decl))
21979 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21980 return;
21981
21982 bitpos_int = int_bit_position (decl);
21983
21984 /* Note that the bit offset is always the distance (in bits) from the
21985 highest-order bit of the "containing object" to the highest-order bit of
21986 the bit-field itself. Since the "high-order end" of any object or field
21987 is different on big-endian and little-endian machines, the computation
21988 below must take account of these differences. */
21989 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21990 highest_order_field_bit_offset = bitpos_int;
21991
21992 if (! BYTES_BIG_ENDIAN)
21993 {
21994 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21995 highest_order_object_bit_offset +=
21996 simple_type_size_in_bits (original_type);
21997 }
21998
21999 bit_offset
22000 = (! BYTES_BIG_ENDIAN
22001 ? highest_order_object_bit_offset - highest_order_field_bit_offset
22002 : highest_order_field_bit_offset - highest_order_object_bit_offset);
22003
22004 if (bit_offset < 0)
22005 add_AT_int (die, DW_AT_bit_offset, bit_offset);
22006 else
22007 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
22008 }
22009
22010 /* For a FIELD_DECL node which represents a bit field, output an attribute
22011 which specifies the length in bits of the given field. */
22012
22013 static inline void
22014 add_bit_size_attribute (dw_die_ref die, tree decl)
22015 {
22016 /* Must be a field and a bit field. */
22017 gcc_assert (TREE_CODE (decl) == FIELD_DECL
22018 && DECL_BIT_FIELD_TYPE (decl));
22019
22020 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
22021 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
22022 }
22023
22024 /* If the compiled language is ANSI C, then add a 'prototyped'
22025 attribute, if arg types are given for the parameters of a function. */
22026
22027 static inline void
22028 add_prototyped_attribute (dw_die_ref die, tree func_type)
22029 {
22030 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
22031 {
22032 case DW_LANG_C:
22033 case DW_LANG_C89:
22034 case DW_LANG_C99:
22035 case DW_LANG_C11:
22036 case DW_LANG_ObjC:
22037 if (prototype_p (func_type))
22038 add_AT_flag (die, DW_AT_prototyped, 1);
22039 break;
22040 default:
22041 break;
22042 }
22043 }
22044
22045 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
22046 by looking in the type declaration, the object declaration equate table or
22047 the block mapping. */
22048
22049 static inline void
22050 add_abstract_origin_attribute (dw_die_ref die, tree origin)
22051 {
22052 dw_die_ref origin_die = NULL;
22053
22054 /* For late LTO debug output we want to refer directly to the abstract
22055 DIE in the early debug rather to the possibly existing concrete
22056 instance and avoid creating that just for this purpose. */
22057 sym_off_pair *desc;
22058 if (in_lto_p
22059 && external_die_map
22060 && (desc = external_die_map->get (origin)))
22061 {
22062 add_AT_external_die_ref (die, DW_AT_abstract_origin,
22063 desc->sym, desc->off);
22064 return;
22065 }
22066
22067 if (DECL_P (origin))
22068 origin_die = lookup_decl_die (origin);
22069 else if (TYPE_P (origin))
22070 origin_die = lookup_type_die (origin);
22071 else if (TREE_CODE (origin) == BLOCK)
22072 origin_die = lookup_block_die (origin);
22073
22074 /* XXX: Functions that are never lowered don't always have correct block
22075 trees (in the case of java, they simply have no block tree, in some other
22076 languages). For these functions, there is nothing we can really do to
22077 output correct debug info for inlined functions in all cases. Rather
22078 than die, we'll just produce deficient debug info now, in that we will
22079 have variables without a proper abstract origin. In the future, when all
22080 functions are lowered, we should re-add a gcc_assert (origin_die)
22081 here. */
22082
22083 if (origin_die)
22084 {
22085 dw_attr_node *a;
22086 /* Like above, if we already created a concrete instance DIE
22087 do not use that for the abstract origin but the early DIE
22088 if present. */
22089 if (in_lto_p
22090 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
22091 origin_die = AT_ref (a);
22092 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
22093 }
22094 }
22095
22096 /* We do not currently support the pure_virtual attribute. */
22097
22098 static inline void
22099 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
22100 {
22101 if (DECL_VINDEX (func_decl))
22102 {
22103 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22104
22105 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
22106 add_AT_loc (die, DW_AT_vtable_elem_location,
22107 new_loc_descr (DW_OP_constu,
22108 tree_to_shwi (DECL_VINDEX (func_decl)),
22109 0));
22110
22111 /* GNU extension: Record what type this method came from originally. */
22112 if (debug_info_level > DINFO_LEVEL_TERSE
22113 && DECL_CONTEXT (func_decl))
22114 add_AT_die_ref (die, DW_AT_containing_type,
22115 lookup_type_die (DECL_CONTEXT (func_decl)));
22116 }
22117 }
22118
22119 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
22120 given decl. This used to be a vendor extension until after DWARF 4
22121 standardized it. */
22122
22123 static void
22124 add_linkage_attr (dw_die_ref die, tree decl)
22125 {
22126 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
22127
22128 /* Mimic what assemble_name_raw does with a leading '*'. */
22129 if (name[0] == '*')
22130 name = &name[1];
22131
22132 if (dwarf_version >= 4)
22133 add_AT_string (die, DW_AT_linkage_name, name);
22134 else
22135 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
22136 }
22137
22138 /* Add source coordinate attributes for the given decl. */
22139
22140 static void
22141 add_src_coords_attributes (dw_die_ref die, tree decl)
22142 {
22143 expanded_location s;
22144
22145 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
22146 return;
22147 s = expand_location (DECL_SOURCE_LOCATION (decl));
22148 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
22149 add_AT_unsigned (die, DW_AT_decl_line, s.line);
22150 if (debug_column_info && s.column)
22151 add_AT_unsigned (die, DW_AT_decl_column, s.column);
22152 }
22153
22154 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22155
22156 static void
22157 add_linkage_name_raw (dw_die_ref die, tree decl)
22158 {
22159 /* Defer until we have an assembler name set. */
22160 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22161 {
22162 limbo_die_node *asm_name;
22163
22164 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22165 asm_name->die = die;
22166 asm_name->created_for = decl;
22167 asm_name->next = deferred_asm_name;
22168 deferred_asm_name = asm_name;
22169 }
22170 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22171 add_linkage_attr (die, decl);
22172 }
22173
22174 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22175
22176 static void
22177 add_linkage_name (dw_die_ref die, tree decl)
22178 {
22179 if (debug_info_level > DINFO_LEVEL_NONE
22180 && VAR_OR_FUNCTION_DECL_P (decl)
22181 && TREE_PUBLIC (decl)
22182 && !(VAR_P (decl) && DECL_REGISTER (decl))
22183 && die->die_tag != DW_TAG_member)
22184 add_linkage_name_raw (die, decl);
22185 }
22186
22187 /* Add a DW_AT_name attribute and source coordinate attribute for the
22188 given decl, but only if it actually has a name. */
22189
22190 static void
22191 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22192 bool no_linkage_name)
22193 {
22194 tree decl_name;
22195
22196 decl_name = DECL_NAME (decl);
22197 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22198 {
22199 const char *name = dwarf2_name (decl, 0);
22200 if (name)
22201 add_name_attribute (die, name);
22202 else
22203 add_desc_attribute (die, decl);
22204
22205 if (! DECL_ARTIFICIAL (decl))
22206 add_src_coords_attributes (die, decl);
22207
22208 if (!no_linkage_name)
22209 add_linkage_name (die, decl);
22210 }
22211 else
22212 add_desc_attribute (die, decl);
22213
22214 #ifdef VMS_DEBUGGING_INFO
22215 /* Get the function's name, as described by its RTL. This may be different
22216 from the DECL_NAME name used in the source file. */
22217 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22218 {
22219 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22220 XEXP (DECL_RTL (decl), 0), false);
22221 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22222 }
22223 #endif /* VMS_DEBUGGING_INFO */
22224 }
22225
22226 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22227
22228 static void
22229 add_discr_value (dw_die_ref die, dw_discr_value *value)
22230 {
22231 dw_attr_node attr;
22232
22233 attr.dw_attr = DW_AT_discr_value;
22234 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22235 attr.dw_attr_val.val_entry = NULL;
22236 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22237 if (value->pos)
22238 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22239 else
22240 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22241 add_dwarf_attr (die, &attr);
22242 }
22243
22244 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22245
22246 static void
22247 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22248 {
22249 dw_attr_node attr;
22250
22251 attr.dw_attr = DW_AT_discr_list;
22252 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22253 attr.dw_attr_val.val_entry = NULL;
22254 attr.dw_attr_val.v.val_discr_list = discr_list;
22255 add_dwarf_attr (die, &attr);
22256 }
22257
22258 static inline dw_discr_list_ref
22259 AT_discr_list (dw_attr_node *attr)
22260 {
22261 return attr->dw_attr_val.v.val_discr_list;
22262 }
22263
22264 #ifdef VMS_DEBUGGING_INFO
22265 /* Output the debug main pointer die for VMS */
22266
22267 void
22268 dwarf2out_vms_debug_main_pointer (void)
22269 {
22270 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22271 dw_die_ref die;
22272
22273 /* Allocate the VMS debug main subprogram die. */
22274 die = new_die_raw (DW_TAG_subprogram);
22275 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22276 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22277 current_function_funcdef_no);
22278 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22279
22280 /* Make it the first child of comp_unit_die (). */
22281 die->die_parent = comp_unit_die ();
22282 if (comp_unit_die ()->die_child)
22283 {
22284 die->die_sib = comp_unit_die ()->die_child->die_sib;
22285 comp_unit_die ()->die_child->die_sib = die;
22286 }
22287 else
22288 {
22289 die->die_sib = die;
22290 comp_unit_die ()->die_child = die;
22291 }
22292 }
22293 #endif /* VMS_DEBUGGING_INFO */
22294
22295 /* walk_tree helper function for uses_local_type, below. */
22296
22297 static tree
22298 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22299 {
22300 if (!TYPE_P (*tp))
22301 *walk_subtrees = 0;
22302 else
22303 {
22304 tree name = TYPE_NAME (*tp);
22305 if (name && DECL_P (name) && decl_function_context (name))
22306 return *tp;
22307 }
22308 return NULL_TREE;
22309 }
22310
22311 /* If TYPE involves a function-local type (including a local typedef to a
22312 non-local type), returns that type; otherwise returns NULL_TREE. */
22313
22314 static tree
22315 uses_local_type (tree type)
22316 {
22317 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22318 return used;
22319 }
22320
22321 /* Return the DIE for the scope that immediately contains this type.
22322 Non-named types that do not involve a function-local type get global
22323 scope. Named types nested in namespaces or other types get their
22324 containing scope. All other types (i.e. function-local named types) get
22325 the current active scope. */
22326
22327 static dw_die_ref
22328 scope_die_for (tree t, dw_die_ref context_die)
22329 {
22330 dw_die_ref scope_die = NULL;
22331 tree containing_scope;
22332
22333 /* Non-types always go in the current scope. */
22334 gcc_assert (TYPE_P (t));
22335
22336 /* Use the scope of the typedef, rather than the scope of the type
22337 it refers to. */
22338 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22339 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22340 else
22341 containing_scope = TYPE_CONTEXT (t);
22342
22343 /* Use the containing namespace if there is one. */
22344 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22345 {
22346 if (context_die == lookup_decl_die (containing_scope))
22347 /* OK */;
22348 else if (debug_info_level > DINFO_LEVEL_TERSE)
22349 context_die = get_context_die (containing_scope);
22350 else
22351 containing_scope = NULL_TREE;
22352 }
22353
22354 /* Ignore function type "scopes" from the C frontend. They mean that
22355 a tagged type is local to a parmlist of a function declarator, but
22356 that isn't useful to DWARF. */
22357 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22358 containing_scope = NULL_TREE;
22359
22360 if (SCOPE_FILE_SCOPE_P (containing_scope))
22361 {
22362 /* If T uses a local type keep it local as well, to avoid references
22363 to function-local DIEs from outside the function. */
22364 if (current_function_decl && uses_local_type (t))
22365 scope_die = context_die;
22366 else
22367 scope_die = comp_unit_die ();
22368 }
22369 else if (TYPE_P (containing_scope))
22370 {
22371 /* For types, we can just look up the appropriate DIE. */
22372 if (debug_info_level > DINFO_LEVEL_TERSE)
22373 scope_die = get_context_die (containing_scope);
22374 else
22375 {
22376 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22377 if (scope_die == NULL)
22378 scope_die = comp_unit_die ();
22379 }
22380 }
22381 else
22382 scope_die = context_die;
22383
22384 return scope_die;
22385 }
22386
22387 /* Returns true if CONTEXT_DIE is internal to a function. */
22388
22389 static inline bool
22390 local_scope_p (dw_die_ref context_die)
22391 {
22392 for (; context_die; context_die = context_die->die_parent)
22393 if (context_die->die_tag == DW_TAG_inlined_subroutine
22394 || context_die->die_tag == DW_TAG_subprogram)
22395 return true;
22396
22397 return false;
22398 }
22399
22400 /* Returns true if CONTEXT_DIE is a class. */
22401
22402 static inline bool
22403 class_scope_p (dw_die_ref context_die)
22404 {
22405 return (context_die
22406 && (context_die->die_tag == DW_TAG_structure_type
22407 || context_die->die_tag == DW_TAG_class_type
22408 || context_die->die_tag == DW_TAG_interface_type
22409 || context_die->die_tag == DW_TAG_union_type));
22410 }
22411
22412 /* Returns true if CONTEXT_DIE is a class or namespace, for deciding
22413 whether or not to treat a DIE in this context as a declaration. */
22414
22415 static inline bool
22416 class_or_namespace_scope_p (dw_die_ref context_die)
22417 {
22418 return (class_scope_p (context_die)
22419 || (context_die && context_die->die_tag == DW_TAG_namespace));
22420 }
22421
22422 /* Many forms of DIEs require a "type description" attribute. This
22423 routine locates the proper "type descriptor" die for the type given
22424 by 'type' plus any additional qualifiers given by 'cv_quals', and
22425 adds a DW_AT_type attribute below the given die. */
22426
22427 static void
22428 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22429 bool reverse, dw_die_ref context_die)
22430 {
22431 enum tree_code code = TREE_CODE (type);
22432 dw_die_ref type_die = NULL;
22433
22434 if (debug_info_level <= DINFO_LEVEL_TERSE)
22435 return;
22436
22437 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22438 or fixed-point type, use the inner type. This is because we have no
22439 support for unnamed types in base_type_die. This can happen if this is
22440 an Ada subrange type. Correct solution is emit a subrange type die. */
22441 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22442 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22443 type = TREE_TYPE (type), code = TREE_CODE (type);
22444
22445 if (code == ERROR_MARK
22446 /* Handle a special case. For functions whose return type is void, we
22447 generate *no* type attribute. (Note that no object may have type
22448 `void', so this only applies to function return types). */
22449 || code == VOID_TYPE)
22450 return;
22451
22452 type_die = modified_type_die (type,
22453 cv_quals | TYPE_QUALS (type),
22454 reverse,
22455 context_die);
22456
22457 if (type_die != NULL)
22458 add_AT_die_ref (object_die, DW_AT_type, type_die);
22459 }
22460
22461 /* Given an object die, add the calling convention attribute for the
22462 function call type. */
22463 static void
22464 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22465 {
22466 enum dwarf_calling_convention value = DW_CC_normal;
22467
22468 value = ((enum dwarf_calling_convention)
22469 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22470
22471 if (is_fortran ()
22472 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22473 {
22474 /* DWARF 2 doesn't provide a way to identify a program's source-level
22475 entry point. DW_AT_calling_convention attributes are only meant
22476 to describe functions' calling conventions. However, lacking a
22477 better way to signal the Fortran main program, we used this for
22478 a long time, following existing custom. Now, DWARF 4 has
22479 DW_AT_main_subprogram, which we add below, but some tools still
22480 rely on the old way, which we thus keep. */
22481 value = DW_CC_program;
22482
22483 if (dwarf_version >= 4 || !dwarf_strict)
22484 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22485 }
22486
22487 /* Only add the attribute if the backend requests it, and
22488 is not DW_CC_normal. */
22489 if (value && (value != DW_CC_normal))
22490 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22491 }
22492
22493 /* Given a tree pointer to a struct, class, union, or enum type node, return
22494 a pointer to the (string) tag name for the given type, or zero if the type
22495 was declared without a tag. */
22496
22497 static const char *
22498 type_tag (const_tree type)
22499 {
22500 const char *name = 0;
22501
22502 if (TYPE_NAME (type) != 0)
22503 {
22504 tree t = 0;
22505
22506 /* Find the IDENTIFIER_NODE for the type name. */
22507 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22508 && !TYPE_NAMELESS (type))
22509 t = TYPE_NAME (type);
22510
22511 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22512 a TYPE_DECL node, regardless of whether or not a `typedef' was
22513 involved. */
22514 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22515 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22516 {
22517 /* We want to be extra verbose. Don't call dwarf_name if
22518 DECL_NAME isn't set. The default hook for decl_printable_name
22519 doesn't like that, and in this context it's correct to return
22520 0, instead of "<anonymous>" or the like. */
22521 if (DECL_NAME (TYPE_NAME (type))
22522 && !DECL_NAMELESS (TYPE_NAME (type)))
22523 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22524 }
22525
22526 /* Now get the name as a string, or invent one. */
22527 if (!name && t != 0)
22528 name = IDENTIFIER_POINTER (t);
22529 }
22530
22531 return (name == 0 || *name == '\0') ? 0 : name;
22532 }
22533
22534 /* Return the type associated with a data member, make a special check
22535 for bit field types. */
22536
22537 static inline tree
22538 member_declared_type (const_tree member)
22539 {
22540 return (DECL_BIT_FIELD_TYPE (member)
22541 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22542 }
22543
22544 /* Get the decl's label, as described by its RTL. This may be different
22545 from the DECL_NAME name used in the source file. */
22546
22547 #if 0
22548 static const char *
22549 decl_start_label (tree decl)
22550 {
22551 rtx x;
22552 const char *fnname;
22553
22554 x = DECL_RTL (decl);
22555 gcc_assert (MEM_P (x));
22556
22557 x = XEXP (x, 0);
22558 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22559
22560 fnname = XSTR (x, 0);
22561 return fnname;
22562 }
22563 #endif
22564
22565 /* For variable-length arrays that have been previously generated, but
22566 may be incomplete due to missing subscript info, fill the subscript
22567 info. Return TRUE if this is one of those cases. */
22568
22569 static bool
22570 fill_variable_array_bounds (tree type)
22571 {
22572 if (TREE_ASM_WRITTEN (type)
22573 && TREE_CODE (type) == ARRAY_TYPE
22574 && variably_modified_type_p (type, NULL))
22575 {
22576 dw_die_ref array_die = lookup_type_die (type);
22577 if (!array_die)
22578 return false;
22579 add_subscript_info (array_die, type, !is_ada ());
22580 return true;
22581 }
22582 return false;
22583 }
22584
22585 /* These routines generate the internal representation of the DIE's for
22586 the compilation unit. Debugging information is collected by walking
22587 the declaration trees passed in from dwarf2out_decl(). */
22588
22589 static void
22590 gen_array_type_die (tree type, dw_die_ref context_die)
22591 {
22592 dw_die_ref array_die;
22593
22594 /* GNU compilers represent multidimensional array types as sequences of one
22595 dimensional array types whose element types are themselves array types.
22596 We sometimes squish that down to a single array_type DIE with multiple
22597 subscripts in the Dwarf debugging info. The draft Dwarf specification
22598 say that we are allowed to do this kind of compression in C, because
22599 there is no difference between an array of arrays and a multidimensional
22600 array. We don't do this for Ada to remain as close as possible to the
22601 actual representation, which is especially important against the language
22602 flexibilty wrt arrays of variable size. */
22603
22604 bool collapse_nested_arrays = !is_ada ();
22605
22606 if (fill_variable_array_bounds (type))
22607 return;
22608
22609 dw_die_ref scope_die = scope_die_for (type, context_die);
22610 tree element_type;
22611
22612 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22613 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22614 if (TREE_CODE (type) == ARRAY_TYPE
22615 && TYPE_STRING_FLAG (type)
22616 && is_fortran ()
22617 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22618 {
22619 HOST_WIDE_INT size;
22620
22621 array_die = new_die (DW_TAG_string_type, scope_die, type);
22622 add_name_attribute (array_die, type_tag (type));
22623 equate_type_number_to_die (type, array_die);
22624 size = int_size_in_bytes (type);
22625 if (size >= 0)
22626 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22627 /* ??? We can't annotate types late, but for LTO we may not
22628 generate a location early either (gfortran.dg/save_6.f90). */
22629 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22630 && TYPE_DOMAIN (type) != NULL_TREE
22631 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22632 {
22633 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22634 tree rszdecl = szdecl;
22635
22636 size = int_size_in_bytes (TREE_TYPE (szdecl));
22637 if (!DECL_P (szdecl))
22638 {
22639 if (INDIRECT_REF_P (szdecl)
22640 && DECL_P (TREE_OPERAND (szdecl, 0)))
22641 {
22642 rszdecl = TREE_OPERAND (szdecl, 0);
22643 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22644 != DWARF2_ADDR_SIZE)
22645 size = 0;
22646 }
22647 else
22648 size = 0;
22649 }
22650 if (size > 0)
22651 {
22652 dw_loc_list_ref loc
22653 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22654 NULL);
22655 if (loc)
22656 {
22657 add_AT_location_description (array_die, DW_AT_string_length,
22658 loc);
22659 if (size != DWARF2_ADDR_SIZE)
22660 add_AT_unsigned (array_die, dwarf_version >= 5
22661 ? DW_AT_string_length_byte_size
22662 : DW_AT_byte_size, size);
22663 }
22664 }
22665 }
22666 return;
22667 }
22668
22669 array_die = new_die (DW_TAG_array_type, scope_die, type);
22670 add_name_attribute (array_die, type_tag (type));
22671 equate_type_number_to_die (type, array_die);
22672
22673 if (VECTOR_TYPE_P (type))
22674 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22675
22676 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22677 if (is_fortran ()
22678 && TREE_CODE (type) == ARRAY_TYPE
22679 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22680 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22681 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22682
22683 #if 0
22684 /* We default the array ordering. Debuggers will probably do the right
22685 things even if DW_AT_ordering is not present. It's not even an issue
22686 until we start to get into multidimensional arrays anyway. If a debugger
22687 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22688 then we'll have to put the DW_AT_ordering attribute back in. (But if
22689 and when we find out that we need to put these in, we will only do so
22690 for multidimensional arrays. */
22691 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22692 #endif
22693
22694 if (VECTOR_TYPE_P (type))
22695 {
22696 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22697 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22698 int lb = lower_bound_default ();
22699 if (lb == -1)
22700 lb = 0;
22701 add_bound_info (subrange_die, DW_AT_lower_bound, size_int (lb), NULL);
22702 add_bound_info (subrange_die, DW_AT_upper_bound,
22703 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22704 }
22705 else
22706 add_subscript_info (array_die, type, collapse_nested_arrays);
22707
22708 /* Add representation of the type of the elements of this array type and
22709 emit the corresponding DIE if we haven't done it already. */
22710 element_type = TREE_TYPE (type);
22711 if (collapse_nested_arrays)
22712 while (TREE_CODE (element_type) == ARRAY_TYPE)
22713 {
22714 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22715 break;
22716 element_type = TREE_TYPE (element_type);
22717 }
22718
22719 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22720 TREE_CODE (type) == ARRAY_TYPE
22721 && TYPE_REVERSE_STORAGE_ORDER (type),
22722 context_die);
22723
22724 /* Add bit stride information to boolean vectors of single bits so that
22725 elements can be correctly read and displayed by a debugger. */
22726 if (VECTOR_BOOLEAN_TYPE_P (type))
22727 {
22728 enum machine_mode tmode = TYPE_MODE_RAW (type);
22729 if (GET_MODE_CLASS (tmode) == MODE_VECTOR_BOOL)
22730 {
22731 /* Calculate bit-size of element based on mnode. */
22732 poly_uint16 bit_size = exact_div (GET_MODE_BITSIZE (tmode),
22733 GET_MODE_NUNITS (tmode));
22734 /* Set bit stride in the array type DIE. */
22735 add_AT_unsigned (array_die, DW_AT_bit_stride, bit_size.coeffs[0]);
22736 /* Find DIE corresponding to the element type so that we could
22737 add DW_AT_bit_size to it. */
22738 dw_die_ref elem_die = get_AT_ref (array_die, DW_AT_type);
22739 /* Avoid adding DW_AT_bit_size twice. */
22740 if (get_AT (elem_die, DW_AT_bit_size) == NULL)
22741 add_AT_unsigned (elem_die, DW_AT_bit_size,
22742 TYPE_PRECISION (element_type));
22743 }
22744 }
22745
22746 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22747 if (TYPE_ARTIFICIAL (type))
22748 add_AT_flag (array_die, DW_AT_artificial, 1);
22749
22750 if (get_AT (array_die, DW_AT_name))
22751 add_pubtype (type, array_die);
22752
22753 add_alignment_attribute (array_die, type);
22754 }
22755
22756 /* This routine generates DIE for array with hidden descriptor, details
22757 are filled into *info by a langhook. */
22758
22759 static void
22760 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22761 dw_die_ref context_die)
22762 {
22763 const dw_die_ref scope_die = scope_die_for (type, context_die);
22764 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22765 struct loc_descr_context context = {
22766 type, /* context_type */
22767 info->base_decl, /* base_decl */
22768 NULL, /* dpi */
22769 false, /* placeholder_arg */
22770 false, /* placeholder_seen */
22771 false /* strict_signedness */
22772 };
22773 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22774 int dim;
22775
22776 add_name_attribute (array_die, type_tag (type));
22777 equate_type_number_to_die (type, array_die);
22778
22779 if (info->ndimensions > 1)
22780 switch (info->ordering)
22781 {
22782 case array_descr_ordering_row_major:
22783 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22784 break;
22785 case array_descr_ordering_column_major:
22786 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22787 break;
22788 default:
22789 break;
22790 }
22791
22792 if (dwarf_version >= 3 || !dwarf_strict)
22793 {
22794 if (info->data_location)
22795 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22796 dw_scalar_form_exprloc, &context);
22797 if (info->associated)
22798 add_scalar_info (array_die, DW_AT_associated, info->associated,
22799 dw_scalar_form_constant
22800 | dw_scalar_form_exprloc
22801 | dw_scalar_form_reference, &context);
22802 if (info->allocated)
22803 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22804 dw_scalar_form_constant
22805 | dw_scalar_form_exprloc
22806 | dw_scalar_form_reference, &context);
22807 if (info->stride)
22808 {
22809 const enum dwarf_attribute attr
22810 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22811 const int forms
22812 = (info->stride_in_bits)
22813 ? dw_scalar_form_constant
22814 : (dw_scalar_form_constant
22815 | dw_scalar_form_exprloc
22816 | dw_scalar_form_reference);
22817
22818 add_scalar_info (array_die, attr, info->stride, forms, &context);
22819 }
22820 }
22821 if (dwarf_version >= 5)
22822 {
22823 if (info->rank)
22824 {
22825 add_scalar_info (array_die, DW_AT_rank, info->rank,
22826 dw_scalar_form_constant
22827 | dw_scalar_form_exprloc, &context);
22828 subrange_tag = DW_TAG_generic_subrange;
22829 context.placeholder_arg = true;
22830 }
22831 }
22832
22833 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22834
22835 for (dim = 0; dim < info->ndimensions; dim++)
22836 {
22837 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22838
22839 if (info->dimen[dim].bounds_type)
22840 add_type_attribute (subrange_die,
22841 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22842 false, context_die);
22843 if (info->dimen[dim].lower_bound)
22844 add_bound_info (subrange_die, DW_AT_lower_bound,
22845 info->dimen[dim].lower_bound, &context);
22846 if (info->dimen[dim].upper_bound)
22847 add_bound_info (subrange_die, DW_AT_upper_bound,
22848 info->dimen[dim].upper_bound, &context);
22849 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22850 add_scalar_info (subrange_die, DW_AT_byte_stride,
22851 info->dimen[dim].stride,
22852 dw_scalar_form_constant
22853 | dw_scalar_form_exprloc
22854 | dw_scalar_form_reference,
22855 &context);
22856 }
22857
22858 gen_type_die (info->element_type, context_die);
22859 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22860 TREE_CODE (type) == ARRAY_TYPE
22861 && TYPE_REVERSE_STORAGE_ORDER (type),
22862 context_die);
22863
22864 if (get_AT (array_die, DW_AT_name))
22865 add_pubtype (type, array_die);
22866
22867 add_alignment_attribute (array_die, type);
22868 }
22869
22870 #if 0
22871 static void
22872 gen_entry_point_die (tree decl, dw_die_ref context_die)
22873 {
22874 tree origin = decl_ultimate_origin (decl);
22875 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22876
22877 if (origin != NULL)
22878 add_abstract_origin_attribute (decl_die, origin);
22879 else
22880 {
22881 add_name_and_src_coords_attributes (decl_die, decl);
22882 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22883 TYPE_UNQUALIFIED, false, context_die);
22884 }
22885
22886 if (DECL_ABSTRACT_P (decl))
22887 equate_decl_number_to_die (decl, decl_die);
22888 else
22889 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22890 }
22891 #endif
22892
22893 /* Walk through the list of incomplete types again, trying once more to
22894 emit full debugging info for them. */
22895
22896 static void
22897 retry_incomplete_types (void)
22898 {
22899 set_early_dwarf s;
22900 int i;
22901
22902 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22903 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22904 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22905 vec_safe_truncate (incomplete_types, 0);
22906 }
22907
22908 /* Determine what tag to use for a record type. */
22909
22910 static enum dwarf_tag
22911 record_type_tag (tree type)
22912 {
22913 if (! lang_hooks.types.classify_record)
22914 return DW_TAG_structure_type;
22915
22916 switch (lang_hooks.types.classify_record (type))
22917 {
22918 case RECORD_IS_STRUCT:
22919 return DW_TAG_structure_type;
22920
22921 case RECORD_IS_CLASS:
22922 return DW_TAG_class_type;
22923
22924 case RECORD_IS_INTERFACE:
22925 if (dwarf_version >= 3 || !dwarf_strict)
22926 return DW_TAG_interface_type;
22927 return DW_TAG_structure_type;
22928
22929 default:
22930 gcc_unreachable ();
22931 }
22932 }
22933
22934 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22935 include all of the information about the enumeration values also. Each
22936 enumerated type name/value is listed as a child of the enumerated type
22937 DIE. REVERSE is true if the type is to be interpreted in the reverse
22938 storage order wrt the target order. */
22939
22940 static dw_die_ref
22941 gen_enumeration_type_die (tree type, dw_die_ref context_die, bool reverse)
22942 {
22943 dw_die_ref type_die = lookup_type_die (type);
22944 dw_die_ref orig_type_die = type_die;
22945
22946 if (type_die == NULL || reverse)
22947 {
22948 dw_die_ref scope_die = scope_die_for (type, context_die);
22949
22950 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
22951 if (reverse)
22952 {
22953 gcc_assert (type_die);
22954 dw_die_ref after_die = type_die;
22955 type_die = new_die_raw (DW_TAG_enumeration_type);
22956 add_child_die_after (scope_die, type_die, after_die);
22957 }
22958 else
22959 {
22960 type_die = new_die (DW_TAG_enumeration_type, scope_die, type);
22961 equate_type_number_to_die (type, type_die);
22962 }
22963 add_name_attribute (type_die, type_tag (type));
22964 if ((dwarf_version >= 4 || !dwarf_strict)
22965 && ENUM_IS_SCOPED (type))
22966 add_AT_flag (type_die, DW_AT_enum_class, 1);
22967 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22968 add_AT_flag (type_die, DW_AT_declaration, 1);
22969 if (!dwarf_strict)
22970 add_AT_unsigned (type_die, DW_AT_encoding,
22971 TYPE_UNSIGNED (type)
22972 ? DW_ATE_unsigned
22973 : DW_ATE_signed);
22974 if (reverse)
22975 add_AT_unsigned (type_die, DW_AT_endianity,
22976 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
22977 }
22978 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22979 return type_die;
22980 else
22981 remove_AT (type_die, DW_AT_declaration);
22982
22983 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22984 given enum type is incomplete, do not generate the DW_AT_byte_size
22985 attribute or the DW_AT_element_list attribute. */
22986 if (TYPE_SIZE (type))
22987 {
22988 tree link;
22989
22990 if (!ENUM_IS_OPAQUE (type))
22991 TREE_ASM_WRITTEN (type) = 1;
22992 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22993 add_byte_size_attribute (type_die, type);
22994 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22995 add_alignment_attribute (type_die, type);
22996 if ((dwarf_version >= 3 || !dwarf_strict)
22997 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22998 {
22999 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
23000 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
23001 context_die);
23002 }
23003 if (TYPE_STUB_DECL (type) != NULL_TREE)
23004 {
23005 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
23006 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
23007 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
23008 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
23009 }
23010
23011 /* If the first reference to this type was as the return type of an
23012 inline function, then it may not have a parent. Fix this now. */
23013 if (type_die->die_parent == NULL)
23014 add_child_die (scope_die_for (type, context_die), type_die);
23015
23016 for (link = TYPE_VALUES (type);
23017 link != NULL; link = TREE_CHAIN (link))
23018 {
23019 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
23020 tree value = TREE_VALUE (link);
23021
23022 if (DECL_P (value))
23023 equate_decl_number_to_die (value, enum_die);
23024
23025 gcc_assert (!ENUM_IS_OPAQUE (type));
23026 add_name_attribute (enum_die,
23027 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
23028
23029 if (TREE_CODE (value) == CONST_DECL)
23030 value = DECL_INITIAL (value);
23031
23032 if (simple_type_size_in_bits (TREE_TYPE (value))
23033 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
23034 {
23035 /* For constant forms created by add_AT_unsigned DWARF
23036 consumers (GDB, elfutils, etc.) always zero extend
23037 the value. Only when the actual value is negative
23038 do we need to use add_AT_int to generate a constant
23039 form that can represent negative values. */
23040 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
23041 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
23042 add_AT_unsigned (enum_die, DW_AT_const_value,
23043 (unsigned HOST_WIDE_INT) val);
23044 else
23045 add_AT_int (enum_die, DW_AT_const_value, val);
23046 }
23047 else
23048 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
23049 that here. TODO: This should be re-worked to use correct
23050 signed/unsigned double tags for all cases. */
23051 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
23052 }
23053
23054 add_gnat_descriptive_type_attribute (type_die, type, context_die);
23055 if (TYPE_ARTIFICIAL (type)
23056 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
23057 add_AT_flag (type_die, DW_AT_artificial, 1);
23058 }
23059 else
23060 add_AT_flag (type_die, DW_AT_declaration, 1);
23061
23062 add_pubtype (type, type_die);
23063
23064 return type_die;
23065 }
23066
23067 /* Generate a DIE to represent either a real live formal parameter decl or to
23068 represent just the type of some formal parameter position in some function
23069 type.
23070
23071 Note that this routine is a bit unusual because its argument may be a
23072 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
23073 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
23074 node. If it's the former then this function is being called to output a
23075 DIE to represent a formal parameter object (or some inlining thereof). If
23076 it's the latter, then this function is only being called to output a
23077 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
23078 argument type of some subprogram type.
23079 If EMIT_NAME_P is true, name and source coordinate attributes
23080 are emitted. */
23081
23082 static dw_die_ref
23083 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
23084 dw_die_ref context_die)
23085 {
23086 tree node_or_origin = node ? node : origin;
23087 tree ultimate_origin;
23088 dw_die_ref parm_die = NULL;
23089
23090 if (DECL_P (node_or_origin))
23091 {
23092 parm_die = lookup_decl_die (node);
23093
23094 /* If the contexts differ, we may not be talking about the same
23095 thing.
23096 ??? When in LTO the DIE parent is the "abstract" copy and the
23097 context_die is the specification "copy". */
23098 if (parm_die
23099 && parm_die->die_parent != context_die
23100 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
23101 || parm_die->die_parent->die_parent != context_die)
23102 && !in_lto_p)
23103 {
23104 gcc_assert (!DECL_ABSTRACT_P (node));
23105 /* This can happen when creating a concrete instance, in
23106 which case we need to create a new DIE that will get
23107 annotated with DW_AT_abstract_origin. */
23108 parm_die = NULL;
23109 }
23110
23111 if (parm_die && parm_die->die_parent == NULL)
23112 {
23113 /* Check that parm_die already has the right attributes that
23114 we would have added below. If any attributes are
23115 missing, fall through to add them. */
23116 if (! DECL_ABSTRACT_P (node_or_origin)
23117 && !get_AT (parm_die, DW_AT_location)
23118 && !get_AT (parm_die, DW_AT_const_value))
23119 /* We are missing location info, and are about to add it. */
23120 ;
23121 else
23122 {
23123 add_child_die (context_die, parm_die);
23124 return parm_die;
23125 }
23126 }
23127 }
23128
23129 /* If we have a previously generated DIE, use it, unless this is an
23130 concrete instance (origin != NULL), in which case we need a new
23131 DIE with a corresponding DW_AT_abstract_origin. */
23132 bool reusing_die;
23133 if (parm_die && origin == NULL)
23134 reusing_die = true;
23135 else
23136 {
23137 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
23138 reusing_die = false;
23139 }
23140
23141 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
23142 {
23143 case tcc_declaration:
23144 ultimate_origin = decl_ultimate_origin (node_or_origin);
23145 if (node || ultimate_origin)
23146 origin = ultimate_origin;
23147
23148 if (reusing_die)
23149 goto add_location;
23150
23151 if (origin != NULL)
23152 add_abstract_origin_attribute (parm_die, origin);
23153 else if (emit_name_p)
23154 add_name_and_src_coords_attributes (parm_die, node);
23155 if (origin == NULL
23156 || (! DECL_ABSTRACT_P (node_or_origin)
23157 && variably_modified_type_p (TREE_TYPE (node_or_origin),
23158 decl_function_context
23159 (node_or_origin))))
23160 {
23161 tree type = TREE_TYPE (node_or_origin);
23162 if (decl_by_reference_p (node_or_origin))
23163 add_type_attribute (parm_die, TREE_TYPE (type),
23164 TYPE_UNQUALIFIED,
23165 false, context_die);
23166 else
23167 add_type_attribute (parm_die, type,
23168 decl_quals (node_or_origin),
23169 false, context_die);
23170 }
23171 if (origin == NULL && DECL_ARTIFICIAL (node))
23172 add_AT_flag (parm_die, DW_AT_artificial, 1);
23173 add_location:
23174 if (node && node != origin)
23175 equate_decl_number_to_die (node, parm_die);
23176 if (! DECL_ABSTRACT_P (node_or_origin))
23177 add_location_or_const_value_attribute (parm_die, node_or_origin,
23178 node == NULL);
23179
23180 break;
23181
23182 case tcc_type:
23183 /* We were called with some kind of a ..._TYPE node. */
23184 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
23185 context_die);
23186 break;
23187
23188 default:
23189 gcc_unreachable ();
23190 }
23191
23192 return parm_die;
23193 }
23194
23195 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23196 children DW_TAG_formal_parameter DIEs representing the arguments of the
23197 parameter pack.
23198
23199 PARM_PACK must be a function parameter pack.
23200 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23201 must point to the subsequent arguments of the function PACK_ARG belongs to.
23202 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23203 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23204 following the last one for which a DIE was generated. */
23205
23206 static dw_die_ref
23207 gen_formal_parameter_pack_die (tree parm_pack,
23208 tree pack_arg,
23209 dw_die_ref subr_die,
23210 tree *next_arg)
23211 {
23212 tree arg;
23213 dw_die_ref parm_pack_die;
23214
23215 gcc_assert (parm_pack
23216 && lang_hooks.function_parameter_pack_p (parm_pack)
23217 && subr_die);
23218
23219 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
23220 add_name_and_src_coords_attributes (parm_pack_die, parm_pack);
23221
23222 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23223 {
23224 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23225 parm_pack))
23226 break;
23227 gen_formal_parameter_die (arg, NULL,
23228 false /* Don't emit name attribute. */,
23229 parm_pack_die);
23230 }
23231 if (next_arg)
23232 *next_arg = arg;
23233 return parm_pack_die;
23234 }
23235
23236 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23237 at the end of an (ANSI prototyped) formal parameters list. */
23238
23239 static void
23240 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23241 {
23242 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
23243 }
23244
23245 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23246 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23247 parameters as specified in some function type specification (except for
23248 those which appear as part of a function *definition*). */
23249
23250 static void
23251 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23252 {
23253 tree link;
23254 tree formal_type = NULL;
23255 tree first_parm_type;
23256 tree arg;
23257
23258 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23259 {
23260 arg = DECL_ARGUMENTS (function_or_method_type);
23261 function_or_method_type = TREE_TYPE (function_or_method_type);
23262 }
23263 else
23264 arg = NULL_TREE;
23265
23266 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23267
23268 /* Make our first pass over the list of formal parameter types and output a
23269 DW_TAG_formal_parameter DIE for each one. */
23270 for (link = first_parm_type; link; )
23271 {
23272 dw_die_ref parm_die;
23273
23274 formal_type = TREE_VALUE (link);
23275 if (formal_type == void_type_node)
23276 break;
23277
23278 /* Output a (nameless) DIE to represent the formal parameter itself. */
23279 parm_die = gen_formal_parameter_die (formal_type, NULL,
23280 true /* Emit name attribute. */,
23281 context_die);
23282 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23283 && link == first_parm_type)
23284 {
23285 add_AT_flag (parm_die, DW_AT_artificial, 1);
23286 if (dwarf_version >= 3 || !dwarf_strict)
23287 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
23288 }
23289 else if (arg && DECL_ARTIFICIAL (arg))
23290 add_AT_flag (parm_die, DW_AT_artificial, 1);
23291
23292 link = TREE_CHAIN (link);
23293 if (arg)
23294 arg = DECL_CHAIN (arg);
23295 }
23296
23297 /* If this function type has an ellipsis, add a
23298 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23299 if (formal_type != void_type_node)
23300 gen_unspecified_parameters_die (function_or_method_type, context_die);
23301
23302 /* Make our second (and final) pass over the list of formal parameter types
23303 and output DIEs to represent those types (as necessary). */
23304 for (link = TYPE_ARG_TYPES (function_or_method_type);
23305 link && TREE_VALUE (link);
23306 link = TREE_CHAIN (link))
23307 gen_type_die (TREE_VALUE (link), context_die);
23308 }
23309
23310 /* We want to generate the DIE for TYPE so that we can generate the
23311 die for MEMBER, which has been defined; we will need to refer back
23312 to the member declaration nested within TYPE. If we're trying to
23313 generate minimal debug info for TYPE, processing TYPE won't do the
23314 trick; we need to attach the member declaration by hand. */
23315
23316 static void
23317 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23318 {
23319 gen_type_die (type, context_die);
23320
23321 /* If we're trying to avoid duplicate debug info, we may not have
23322 emitted the member decl for this function. Emit it now. */
23323 if (TYPE_STUB_DECL (type)
23324 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23325 && ! lookup_decl_die (member))
23326 {
23327 dw_die_ref type_die;
23328 gcc_assert (!decl_ultimate_origin (member));
23329
23330 type_die = lookup_type_die_strip_naming_typedef (type);
23331 if (TREE_CODE (member) == FUNCTION_DECL)
23332 gen_subprogram_die (member, type_die);
23333 else if (TREE_CODE (member) == FIELD_DECL)
23334 {
23335 /* Ignore the nameless fields that are used to skip bits but handle
23336 C++ anonymous unions and structs. */
23337 if (DECL_NAME (member) != NULL_TREE
23338 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23339 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23340 {
23341 struct vlr_context vlr_ctx = {
23342 DECL_CONTEXT (member), /* struct_type */
23343 NULL_TREE /* variant_part_offset */
23344 };
23345 gen_type_die (member_declared_type (member), type_die);
23346 gen_field_die (member, &vlr_ctx, type_die);
23347 }
23348 }
23349 else
23350 gen_variable_die (member, NULL_TREE, type_die);
23351 }
23352 }
23353
23354 /* Forward declare these functions, because they are mutually recursive
23355 with their set_block_* pairing functions. */
23356 static void set_decl_origin_self (tree);
23357
23358 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23359 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23360 that it points to the node itself, thus indicating that the node is its
23361 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23362 the given node is NULL, recursively descend the decl/block tree which
23363 it is the root of, and for each other ..._DECL or BLOCK node contained
23364 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23365 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23366 values to point to themselves. */
23367
23368 static void
23369 set_block_origin_self (tree stmt)
23370 {
23371 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23372 {
23373 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23374
23375 {
23376 tree local_decl;
23377
23378 for (local_decl = BLOCK_VARS (stmt);
23379 local_decl != NULL_TREE;
23380 local_decl = DECL_CHAIN (local_decl))
23381 /* Do not recurse on nested functions since the inlining status
23382 of parent and child can be different as per the DWARF spec. */
23383 if (TREE_CODE (local_decl) != FUNCTION_DECL
23384 && !DECL_EXTERNAL (local_decl))
23385 set_decl_origin_self (local_decl);
23386 }
23387
23388 {
23389 tree subblock;
23390
23391 for (subblock = BLOCK_SUBBLOCKS (stmt);
23392 subblock != NULL_TREE;
23393 subblock = BLOCK_CHAIN (subblock))
23394 set_block_origin_self (subblock); /* Recurse. */
23395 }
23396 }
23397 }
23398
23399 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23400 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23401 node to so that it points to the node itself, thus indicating that the
23402 node represents its own (abstract) origin. Additionally, if the
23403 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23404 the decl/block tree of which the given node is the root of, and for
23405 each other ..._DECL or BLOCK node contained therein whose
23406 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23407 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23408 point to themselves. */
23409
23410 static void
23411 set_decl_origin_self (tree decl)
23412 {
23413 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23414 {
23415 DECL_ABSTRACT_ORIGIN (decl) = decl;
23416 if (TREE_CODE (decl) == FUNCTION_DECL)
23417 {
23418 tree arg;
23419
23420 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23421 DECL_ABSTRACT_ORIGIN (arg) = arg;
23422 if (DECL_INITIAL (decl) != NULL_TREE
23423 && DECL_INITIAL (decl) != error_mark_node)
23424 set_block_origin_self (DECL_INITIAL (decl));
23425 }
23426 }
23427 }
23428
23429 /* Mark the early DIE for DECL as the abstract instance. */
23430
23431 static void
23432 dwarf2out_abstract_function (tree decl)
23433 {
23434 dw_die_ref old_die;
23435
23436 /* Make sure we have the actual abstract inline, not a clone. */
23437 decl = DECL_ORIGIN (decl);
23438
23439 if (DECL_IGNORED_P (decl))
23440 return;
23441
23442 #ifdef CODEVIEW_DEBUGGING_INFO
23443 if (codeview_debuginfo_p ())
23444 codeview_abstract_function (decl);
23445 #endif
23446
23447 /* In LTO we're all set. We already created abstract instances
23448 early and we want to avoid creating a concrete instance of that
23449 if we don't output it. */
23450 if (in_lto_p)
23451 return;
23452
23453 old_die = lookup_decl_die (decl);
23454 gcc_assert (old_die != NULL);
23455 if (get_AT (old_die, DW_AT_inline))
23456 /* We've already generated the abstract instance. */
23457 return;
23458
23459 /* Go ahead and put DW_AT_inline on the DIE. */
23460 if (DECL_DECLARED_INLINE_P (decl))
23461 {
23462 if (cgraph_function_possibly_inlined_p (decl))
23463 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23464 else
23465 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23466 }
23467 else
23468 {
23469 if (cgraph_function_possibly_inlined_p (decl))
23470 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23471 else
23472 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23473 }
23474
23475 if (DECL_DECLARED_INLINE_P (decl)
23476 && !DECL_ARTIFICIAL (decl)
23477 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23478 add_AT_flag (old_die, DW_AT_artificial, 1);
23479
23480 set_decl_origin_self (decl);
23481 }
23482
23483 /* Helper function of premark_used_types() which gets called through
23484 htab_traverse.
23485
23486 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23487 marked as unused by prune_unused_types. */
23488
23489 bool
23490 premark_used_types_helper (tree const &type, void *)
23491 {
23492 dw_die_ref die;
23493
23494 die = lookup_type_die (type);
23495 if (die != NULL)
23496 die->die_perennial_p = 1;
23497 return true;
23498 }
23499
23500 /* Helper function of premark_types_used_by_global_vars which gets called
23501 through htab_traverse.
23502
23503 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23504 marked as unused by prune_unused_types. The DIE of the type is marked
23505 only if the global variable using the type will actually be emitted. */
23506
23507 int
23508 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23509 void *)
23510 {
23511 struct types_used_by_vars_entry *entry;
23512 dw_die_ref die;
23513
23514 entry = (struct types_used_by_vars_entry *) *slot;
23515 gcc_assert (entry->type != NULL
23516 && entry->var_decl != NULL);
23517 die = lookup_type_die (entry->type);
23518 if (die)
23519 {
23520 /* Ask cgraph if the global variable really is to be emitted.
23521 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23522 varpool_node *node = varpool_node::get (entry->var_decl);
23523 if (node && node->definition)
23524 {
23525 die->die_perennial_p = 1;
23526 /* Keep the parent DIEs as well. */
23527 while ((die = die->die_parent) && die->die_perennial_p == 0)
23528 die->die_perennial_p = 1;
23529 }
23530 }
23531 return 1;
23532 }
23533
23534 /* Mark all members of used_types_hash as perennial. */
23535
23536 static void
23537 premark_used_types (struct function *fun)
23538 {
23539 if (fun && fun->used_types_hash)
23540 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23541 }
23542
23543 /* Mark all members of types_used_by_vars_entry as perennial. */
23544
23545 static void
23546 premark_types_used_by_global_vars (void)
23547 {
23548 if (types_used_by_vars_hash)
23549 types_used_by_vars_hash
23550 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23551 }
23552
23553 /* Mark all variables used by the symtab as perennial. */
23554
23555 static void
23556 premark_used_variables (void)
23557 {
23558 /* Mark DIEs in the symtab as used. */
23559 varpool_node *var;
23560 FOR_EACH_VARIABLE (var)
23561 {
23562 dw_die_ref die = lookup_decl_die (var->decl);
23563 if (die)
23564 die->die_perennial_p = 1;
23565 }
23566 }
23567
23568 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23569 for CA_LOC call arg loc node. */
23570
23571 static dw_die_ref
23572 gen_call_site_die (tree decl, dw_die_ref subr_die,
23573 struct call_arg_loc_node *ca_loc)
23574 {
23575 dw_die_ref stmt_die = NULL, die;
23576 tree block = ca_loc->block;
23577
23578 while (block
23579 && block != DECL_INITIAL (decl)
23580 && TREE_CODE (block) == BLOCK)
23581 {
23582 stmt_die = lookup_block_die (block);
23583 if (stmt_die)
23584 break;
23585 block = BLOCK_SUPERCONTEXT (block);
23586 }
23587 if (stmt_die == NULL)
23588 stmt_die = subr_die;
23589 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23590 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc),
23591 ca_loc->label,
23592 targetm.calls.call_offset_return_label (ca_loc->call_insn));
23593 if (ca_loc->tail_call_p)
23594 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23595 if (ca_loc->symbol_ref)
23596 {
23597 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23598 if (tdie)
23599 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23600 else
23601 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23602 false);
23603 }
23604 return die;
23605 }
23606
23607 /* Generate a DIE to represent a declared function (either file-scope or
23608 block-local). */
23609
23610 static void
23611 gen_subprogram_die (tree decl, dw_die_ref context_die)
23612 {
23613 tree origin = decl_ultimate_origin (decl);
23614 dw_die_ref subr_die;
23615 dw_die_ref old_die = lookup_decl_die (decl);
23616 bool old_die_had_no_children = false;
23617
23618 /* This function gets called multiple times for different stages of
23619 the debug process. For example, for func() in this code:
23620
23621 namespace S
23622 {
23623 void func() { ... }
23624 }
23625
23626 ...we get called 4 times. Twice in early debug and twice in
23627 late debug:
23628
23629 Early debug
23630 -----------
23631
23632 1. Once while generating func() within the namespace. This is
23633 the declaration. The declaration bit below is set, as the
23634 context is the namespace.
23635
23636 A new DIE will be generated with DW_AT_declaration set.
23637
23638 2. Once for func() itself. This is the specification. The
23639 declaration bit below is clear as the context is the CU.
23640
23641 We will use the cached DIE from (1) to create a new DIE with
23642 DW_AT_specification pointing to the declaration in (1).
23643
23644 Late debug via rest_of_handle_final()
23645 -------------------------------------
23646
23647 3. Once generating func() within the namespace. This is also the
23648 declaration, as in (1), but this time we will early exit below
23649 as we have a cached DIE and a declaration needs no additional
23650 annotations (no locations), as the source declaration line
23651 info is enough.
23652
23653 4. Once for func() itself. As in (2), this is the specification,
23654 but this time we will re-use the cached DIE, and just annotate
23655 it with the location information that should now be available.
23656
23657 For something without namespaces, but with abstract instances, we
23658 are also called a multiple times:
23659
23660 class Base
23661 {
23662 public:
23663 Base (); // constructor declaration (1)
23664 };
23665
23666 Base::Base () { } // constructor specification (2)
23667
23668 Early debug
23669 -----------
23670
23671 1. Once for the Base() constructor by virtue of it being a
23672 member of the Base class. This is done via
23673 rest_of_type_compilation.
23674
23675 This is a declaration, so a new DIE will be created with
23676 DW_AT_declaration.
23677
23678 2. Once for the Base() constructor definition, but this time
23679 while generating the abstract instance of the base
23680 constructor (__base_ctor) which is being generated via early
23681 debug of reachable functions.
23682
23683 Even though we have a cached version of the declaration (1),
23684 we will create a DW_AT_specification of the declaration DIE
23685 in (1).
23686
23687 3. Once for the __base_ctor itself, but this time, we generate
23688 an DW_AT_abstract_origin version of the DW_AT_specification in
23689 (2).
23690
23691 Late debug via rest_of_handle_final
23692 -----------------------------------
23693
23694 4. One final time for the __base_ctor (which will have a cached
23695 DIE with DW_AT_abstract_origin created in (3). This time,
23696 we will just annotate the location information now
23697 available.
23698 */
23699 int declaration = (current_function_decl != decl
23700 || (!DECL_INITIAL (decl) && !origin)
23701 || class_or_namespace_scope_p (context_die));
23702
23703 /* A declaration that has been previously dumped needs no
23704 additional information. */
23705 if (old_die && declaration)
23706 return;
23707
23708 if (in_lto_p && old_die && old_die->die_child == NULL)
23709 old_die_had_no_children = true;
23710
23711 /* Now that the C++ front end lazily declares artificial member fns, we
23712 might need to retrofit the declaration into its class. */
23713 if (!declaration && !origin && !old_die
23714 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23715 && !class_or_namespace_scope_p (context_die)
23716 && debug_info_level > DINFO_LEVEL_TERSE)
23717 old_die = force_decl_die (decl);
23718
23719 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23720 if (origin != NULL)
23721 {
23722 gcc_assert (!declaration || local_scope_p (context_die));
23723
23724 /* Fixup die_parent for the abstract instance of a nested
23725 inline function. */
23726 if (old_die && old_die->die_parent == NULL)
23727 add_child_die (context_die, old_die);
23728
23729 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23730 {
23731 /* If we have a DW_AT_abstract_origin we have a working
23732 cached version. */
23733 subr_die = old_die;
23734 }
23735 else
23736 {
23737 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23738 add_abstract_origin_attribute (subr_die, origin);
23739 /* This is where the actual code for a cloned function is.
23740 Let's emit linkage name attribute for it. This helps
23741 debuggers to e.g, set breakpoints into
23742 constructors/destructors when the user asks "break
23743 K::K". */
23744 add_linkage_name (subr_die, decl);
23745 }
23746 }
23747 /* A cached copy, possibly from early dwarf generation. Reuse as
23748 much as possible. */
23749 else if (old_die)
23750 {
23751 if (!get_AT_flag (old_die, DW_AT_declaration)
23752 /* We can have a normal definition following an inline one in the
23753 case of redefinition of GNU C extern inlines.
23754 It seems reasonable to use AT_specification in this case. */
23755 && !get_AT (old_die, DW_AT_inline))
23756 {
23757 /* Detect and ignore this case, where we are trying to output
23758 something we have already output. */
23759 if (get_AT (old_die, DW_AT_low_pc)
23760 || get_AT (old_die, DW_AT_ranges))
23761 return;
23762
23763 /* If we have no location information, this must be a
23764 partially generated DIE from early dwarf generation.
23765 Fall through and generate it. */
23766 }
23767
23768 /* If the definition comes from the same place as the declaration,
23769 maybe use the old DIE. We always want the DIE for this function
23770 that has the *_pc attributes to be under comp_unit_die so the
23771 debugger can find it. We also need to do this for abstract
23772 instances of inlines, since the spec requires the out-of-line copy
23773 to have the same parent. For local class methods, this doesn't
23774 apply; we just use the old DIE. */
23775 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23776 struct dwarf_file_data * file_index = lookup_filename (s.file);
23777 if (((is_unit_die (old_die->die_parent)
23778 /* This condition fixes the inconsistency/ICE with the
23779 following Fortran test (or some derivative thereof) while
23780 building libgfortran:
23781
23782 module some_m
23783 contains
23784 logical function funky (FLAG)
23785 funky = .true.
23786 end function
23787 end module
23788 */
23789 || (old_die->die_parent
23790 && old_die->die_parent->die_tag == DW_TAG_module)
23791 || local_scope_p (old_die->die_parent)
23792 || context_die == NULL)
23793 && (DECL_ARTIFICIAL (decl)
23794 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23795 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23796 == (unsigned) s.line)
23797 && (!debug_column_info
23798 || s.column == 0
23799 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23800 == (unsigned) s.column)))))
23801 /* With LTO if there's an abstract instance for
23802 the old DIE, this is a concrete instance and
23803 thus re-use the DIE. */
23804 || get_AT (old_die, DW_AT_abstract_origin))
23805 {
23806 subr_die = old_die;
23807
23808 /* Clear out the declaration attribute, but leave the
23809 parameters so they can be augmented with location
23810 information later. Unless this was a declaration, in
23811 which case, wipe out the nameless parameters and recreate
23812 them further down. */
23813 if (remove_AT (subr_die, DW_AT_declaration))
23814 {
23815
23816 remove_AT (subr_die, DW_AT_object_pointer);
23817 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23818 }
23819 }
23820 /* Make a specification pointing to the previously built
23821 declaration. */
23822 else
23823 {
23824 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23825 add_AT_specification (subr_die, old_die);
23826 add_pubname (decl, subr_die);
23827 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23828 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23829 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23830 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23831 if (debug_column_info
23832 && s.column
23833 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23834 != (unsigned) s.column))
23835 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23836
23837 /* If the prototype had an 'auto' or 'decltype(auto)' in
23838 the return type, emit the real type on the definition die. */
23839 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23840 {
23841 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23842 while (die
23843 && (die->die_tag == DW_TAG_reference_type
23844 || die->die_tag == DW_TAG_rvalue_reference_type
23845 || die->die_tag == DW_TAG_pointer_type
23846 || die->die_tag == DW_TAG_const_type
23847 || die->die_tag == DW_TAG_volatile_type
23848 || die->die_tag == DW_TAG_restrict_type
23849 || die->die_tag == DW_TAG_array_type
23850 || die->die_tag == DW_TAG_ptr_to_member_type
23851 || die->die_tag == DW_TAG_subroutine_type))
23852 die = get_AT_ref (die, DW_AT_type);
23853 if (die == auto_die || die == decltype_auto_die)
23854 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23855 TYPE_UNQUALIFIED, false, context_die);
23856 }
23857
23858 /* When we process the method declaration, we haven't seen
23859 the out-of-class defaulted definition yet, so we have to
23860 recheck now. */
23861 if ((dwarf_version >= 5 || ! dwarf_strict)
23862 && !get_AT (subr_die, DW_AT_defaulted))
23863 {
23864 int defaulted
23865 = lang_hooks.decls.decl_dwarf_attribute (decl,
23866 DW_AT_defaulted);
23867 if (defaulted != -1)
23868 {
23869 /* Other values must have been handled before. */
23870 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23871 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23872 }
23873 }
23874 }
23875 }
23876 /* Create a fresh DIE for anything else. */
23877 else
23878 {
23879 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23880
23881 if (TREE_PUBLIC (decl))
23882 add_AT_flag (subr_die, DW_AT_external, 1);
23883
23884 add_name_and_src_coords_attributes (subr_die, decl);
23885 add_pubname (decl, subr_die);
23886 if (debug_info_level > DINFO_LEVEL_TERSE)
23887 {
23888 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23889 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23890 TYPE_UNQUALIFIED, false, context_die);
23891 }
23892
23893 add_pure_or_virtual_attribute (subr_die, decl);
23894 if (DECL_ARTIFICIAL (decl))
23895 add_AT_flag (subr_die, DW_AT_artificial, 1);
23896
23897 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23898 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23899
23900 add_alignment_attribute (subr_die, decl);
23901
23902 add_accessibility_attribute (subr_die, decl);
23903 }
23904
23905 /* Unless we have an existing non-declaration DIE, equate the new
23906 DIE. */
23907 if (!old_die || is_declaration_die (old_die))
23908 equate_decl_number_to_die (decl, subr_die);
23909
23910 if (declaration)
23911 {
23912 if (!old_die || !get_AT (old_die, DW_AT_inline))
23913 {
23914 add_AT_flag (subr_die, DW_AT_declaration, 1);
23915
23916 /* If this is an explicit function declaration then generate
23917 a DW_AT_explicit attribute. */
23918 if ((dwarf_version >= 3 || !dwarf_strict)
23919 && lang_hooks.decls.decl_dwarf_attribute (decl,
23920 DW_AT_explicit) == 1)
23921 add_AT_flag (subr_die, DW_AT_explicit, 1);
23922
23923 /* If this is a C++11 deleted special function member then generate
23924 a DW_AT_deleted attribute. */
23925 if ((dwarf_version >= 5 || !dwarf_strict)
23926 && lang_hooks.decls.decl_dwarf_attribute (decl,
23927 DW_AT_deleted) == 1)
23928 add_AT_flag (subr_die, DW_AT_deleted, 1);
23929
23930 /* If this is a C++11 defaulted special function member then
23931 generate a DW_AT_defaulted attribute. */
23932 if (dwarf_version >= 5 || !dwarf_strict)
23933 {
23934 int defaulted
23935 = lang_hooks.decls.decl_dwarf_attribute (decl,
23936 DW_AT_defaulted);
23937 if (defaulted != -1)
23938 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23939 }
23940
23941 /* If this is a C++11 non-static member function with & ref-qualifier
23942 then generate a DW_AT_reference attribute. */
23943 if ((dwarf_version >= 5 || !dwarf_strict)
23944 && lang_hooks.decls.decl_dwarf_attribute (decl,
23945 DW_AT_reference) == 1)
23946 add_AT_flag (subr_die, DW_AT_reference, 1);
23947
23948 /* If this is a C++11 non-static member function with &&
23949 ref-qualifier then generate a DW_AT_reference attribute. */
23950 if ((dwarf_version >= 5 || !dwarf_strict)
23951 && lang_hooks.decls.decl_dwarf_attribute (decl,
23952 DW_AT_rvalue_reference)
23953 == 1)
23954 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23955 }
23956 }
23957 /* For non DECL_EXTERNALs, if range information is available, fill
23958 the DIE with it. */
23959 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23960 {
23961 HOST_WIDE_INT cfa_fb_offset;
23962
23963 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23964
23965 if (!crtl->has_bb_partition)
23966 {
23967 dw_fde_ref fde = fun->fde;
23968 if (fde->dw_fde_begin)
23969 {
23970 /* We have already generated the labels. */
23971 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23972 fde->dw_fde_end, false);
23973 }
23974 else
23975 {
23976 /* Create start/end labels and add the range. */
23977 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23978 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23979 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23980 current_function_funcdef_no);
23981 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23982 current_function_funcdef_no);
23983 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23984 false);
23985 }
23986
23987 #if VMS_DEBUGGING_INFO
23988 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23989 Section 2.3 Prologue and Epilogue Attributes:
23990 When a breakpoint is set on entry to a function, it is generally
23991 desirable for execution to be suspended, not on the very first
23992 instruction of the function, but rather at a point after the
23993 function's frame has been set up, after any language defined local
23994 declaration processing has been completed, and before execution of
23995 the first statement of the function begins. Debuggers generally
23996 cannot properly determine where this point is. Similarly for a
23997 breakpoint set on exit from a function. The prologue and epilogue
23998 attributes allow a compiler to communicate the location(s) to use. */
23999
24000 {
24001 if (fde->dw_fde_vms_end_prologue)
24002 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
24003 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
24004
24005 if (fde->dw_fde_vms_begin_epilogue)
24006 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
24007 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
24008 }
24009 #endif
24010
24011 }
24012 else
24013 {
24014 /* Generate pubnames entries for the split function code ranges. */
24015 dw_fde_ref fde = fun->fde;
24016
24017 if (fde->dw_fde_second_begin)
24018 {
24019 if (dwarf_version >= 3 || !dwarf_strict)
24020 {
24021 /* We should use ranges for non-contiguous code section
24022 addresses. Use the actual code range for the initial
24023 section, since the HOT/COLD labels might precede an
24024 alignment offset. */
24025 bool range_list_added = false;
24026 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
24027 fde->dw_fde_end, &range_list_added,
24028 false);
24029 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
24030 fde->dw_fde_second_end,
24031 &range_list_added, false);
24032 if (range_list_added)
24033 add_ranges (NULL);
24034 }
24035 else
24036 {
24037 /* There is no real support in DW2 for this .. so we make
24038 a work-around. First, emit the pub name for the segment
24039 containing the function label. Then make and emit a
24040 simplified subprogram DIE for the second segment with the
24041 name pre-fixed by __hot/cold_sect_of_. We use the same
24042 linkage name for the second die so that gdb will find both
24043 sections when given "b foo". */
24044 const char *name = NULL;
24045 tree decl_name = DECL_NAME (decl);
24046 dw_die_ref seg_die;
24047
24048 /* Do the 'primary' section. */
24049 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
24050 fde->dw_fde_end, false);
24051
24052 /* Build a minimal DIE for the secondary section. */
24053 seg_die = new_die (DW_TAG_subprogram,
24054 subr_die->die_parent, decl);
24055
24056 if (TREE_PUBLIC (decl))
24057 add_AT_flag (seg_die, DW_AT_external, 1);
24058
24059 if (decl_name != NULL
24060 && IDENTIFIER_POINTER (decl_name) != NULL)
24061 {
24062 name = dwarf2_name (decl, 1);
24063 if (! DECL_ARTIFICIAL (decl))
24064 add_src_coords_attributes (seg_die, decl);
24065
24066 add_linkage_name (seg_die, decl);
24067 }
24068 gcc_assert (name != NULL);
24069 add_pure_or_virtual_attribute (seg_die, decl);
24070 if (DECL_ARTIFICIAL (decl))
24071 add_AT_flag (seg_die, DW_AT_artificial, 1);
24072
24073 name = concat ("__second_sect_of_", name, NULL);
24074 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
24075 fde->dw_fde_second_end, false);
24076 add_name_attribute (seg_die, name);
24077 if (want_pubnames ())
24078 add_pubname_string (name, seg_die);
24079 }
24080 }
24081 else
24082 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
24083 false);
24084 }
24085
24086 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
24087
24088 /* We define the "frame base" as the function's CFA. This is more
24089 convenient for several reasons: (1) It's stable across the prologue
24090 and epilogue, which makes it better than just a frame pointer,
24091 (2) With dwarf3, there exists a one-byte encoding that allows us
24092 to reference the .debug_frame data by proxy, but failing that,
24093 (3) We can at least reuse the code inspection and interpretation
24094 code that determines the CFA position at various points in the
24095 function. */
24096 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
24097 {
24098 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
24099 add_AT_loc (subr_die, DW_AT_frame_base, op);
24100 }
24101 else
24102 {
24103 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
24104 if (list->dw_loc_next)
24105 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
24106 else
24107 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
24108 }
24109
24110 /* Compute a displacement from the "steady-state frame pointer" to
24111 the CFA. The former is what all stack slots and argument slots
24112 will reference in the rtl; the latter is what we've told the
24113 debugger about. We'll need to adjust all frame_base references
24114 by this displacement. */
24115 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
24116
24117 if (fun->static_chain_decl)
24118 {
24119 /* DWARF requires here a location expression that computes the
24120 address of the enclosing subprogram's frame base. The machinery
24121 in tree-nested.cc is supposed to store this specific address in the
24122 last field of the FRAME record. */
24123 const tree frame_type
24124 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
24125 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
24126
24127 tree fb_expr
24128 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
24129 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
24130 fb_expr, fb_decl, NULL_TREE);
24131
24132 add_AT_location_description (subr_die, DW_AT_static_link,
24133 loc_list_from_tree (fb_expr, 0, NULL));
24134 }
24135
24136 resolve_variable_values ();
24137 }
24138
24139 /* Generate child dies for template parameters. */
24140 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
24141 gen_generic_params_dies (decl);
24142
24143 /* Now output descriptions of the arguments for this function. This gets
24144 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
24145 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
24146 `...' at the end of the formal parameter list. In order to find out if
24147 there was a trailing ellipsis or not, we must instead look at the type
24148 associated with the FUNCTION_DECL. This will be a node of type
24149 FUNCTION_TYPE. If the chain of type nodes hanging off of this
24150 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
24151 an ellipsis at the end. */
24152
24153 /* In the case where we are describing a mere function declaration, all we
24154 need to do here (and all we *can* do here) is to describe the *types* of
24155 its formal parameters. */
24156 if (debug_info_level <= DINFO_LEVEL_TERSE)
24157 ;
24158 else if (declaration)
24159 gen_formal_types_die (decl, subr_die);
24160 else
24161 {
24162 /* Generate DIEs to represent all known formal parameters. */
24163 tree parm = DECL_ARGUMENTS (decl);
24164 tree generic_decl = early_dwarf
24165 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
24166 tree generic_decl_parm = generic_decl
24167 ? DECL_ARGUMENTS (generic_decl)
24168 : NULL;
24169
24170 /* Now we want to walk the list of parameters of the function and
24171 emit their relevant DIEs.
24172
24173 We consider the case of DECL being an instance of a generic function
24174 as well as it being a normal function.
24175
24176 If DECL is an instance of a generic function we walk the
24177 parameters of the generic function declaration _and_ the parameters of
24178 DECL itself. This is useful because we want to emit specific DIEs for
24179 function parameter packs and those are declared as part of the
24180 generic function declaration. In that particular case,
24181 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
24182 That DIE has children DIEs representing the set of arguments
24183 of the pack. Note that the set of pack arguments can be empty.
24184 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
24185 children DIE.
24186
24187 Otherwise, we just consider the parameters of DECL. */
24188 while (generic_decl_parm || parm)
24189 {
24190 if (generic_decl_parm
24191 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
24192 gen_formal_parameter_pack_die (generic_decl_parm,
24193 parm, subr_die,
24194 &parm);
24195 else if (parm)
24196 {
24197 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24198
24199 if (early_dwarf
24200 && parm == DECL_ARGUMENTS (decl)
24201 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24202 && parm_die
24203 && (dwarf_version >= 3 || !dwarf_strict))
24204 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
24205
24206 parm = DECL_CHAIN (parm);
24207 }
24208
24209 if (generic_decl_parm)
24210 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24211 }
24212
24213 /* Decide whether we need an unspecified_parameters DIE at the end.
24214 There are 2 more cases to do this for: 1) the ansi ... declaration -
24215 this is detectable when the end of the arg list is not a
24216 void_type_node 2) an unprototyped function declaration (not a
24217 definition). This just means that we have no info about the
24218 parameters at all. */
24219 if (early_dwarf)
24220 {
24221 if (prototype_p (TREE_TYPE (decl)))
24222 {
24223 /* This is the prototyped case, check for.... */
24224 if (stdarg_p (TREE_TYPE (decl)))
24225 gen_unspecified_parameters_die (decl, subr_die);
24226 }
24227 else if (DECL_INITIAL (decl) == NULL_TREE)
24228 gen_unspecified_parameters_die (decl, subr_die);
24229 }
24230 else if ((subr_die != old_die || old_die_had_no_children)
24231 && prototype_p (TREE_TYPE (decl))
24232 && stdarg_p (TREE_TYPE (decl)))
24233 gen_unspecified_parameters_die (decl, subr_die);
24234 }
24235
24236 if (subr_die != old_die)
24237 /* Add the calling convention attribute if requested. */
24238 add_calling_convention_attribute (subr_die, decl);
24239
24240 /* Output Dwarf info for all of the stuff within the body of the function
24241 (if it has one - it may be just a declaration).
24242
24243 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24244 a function. This BLOCK actually represents the outermost binding contour
24245 for the function, i.e. the contour in which the function's formal
24246 parameters and labels get declared. Curiously, it appears that the front
24247 end doesn't actually put the PARM_DECL nodes for the current function onto
24248 the BLOCK_VARS list for this outer scope, but are strung off of the
24249 DECL_ARGUMENTS list for the function instead.
24250
24251 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24252 the LABEL_DECL nodes for the function however, and we output DWARF info
24253 for those in decls_for_scope. Just within the `outer_scope' there will be
24254 a BLOCK node representing the function's outermost pair of curly braces,
24255 and any blocks used for the base and member initializers of a C++
24256 constructor function. */
24257 tree outer_scope = DECL_INITIAL (decl);
24258 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24259 {
24260 int call_site_note_count = 0;
24261 int tail_call_site_note_count = 0;
24262
24263 /* Emit a DW_TAG_variable DIE for a named return value. */
24264 if (DECL_NAME (DECL_RESULT (decl)))
24265 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24266
24267 /* The first time through decls_for_scope we will generate the
24268 DIEs for the locals. The second time, we fill in the
24269 location info. */
24270 decls_for_scope (outer_scope, subr_die);
24271
24272 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24273 {
24274 struct call_arg_loc_node *ca_loc;
24275 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24276 {
24277 dw_die_ref die = NULL;
24278 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24279 rtx call_arg_loc_note
24280 = find_reg_note (ca_loc->call_insn,
24281 REG_CALL_ARG_LOCATION, NULL_RTX);
24282 rtx arg, next_arg;
24283 tree arg_decl = NULL_TREE;
24284
24285 for (arg = (call_arg_loc_note != NULL_RTX
24286 ? XEXP (call_arg_loc_note, 0)
24287 : NULL_RTX);
24288 arg; arg = next_arg)
24289 {
24290 dw_loc_descr_ref reg, val;
24291 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24292 dw_die_ref cdie, tdie = NULL;
24293
24294 next_arg = XEXP (arg, 1);
24295 if (REG_P (XEXP (XEXP (arg, 0), 0))
24296 && next_arg
24297 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24298 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24299 && REGNO (XEXP (XEXP (arg, 0), 0))
24300 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24301 next_arg = XEXP (next_arg, 1);
24302 if (mode == VOIDmode)
24303 {
24304 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24305 if (mode == VOIDmode)
24306 mode = GET_MODE (XEXP (arg, 0));
24307 }
24308 if (mode == VOIDmode || mode == BLKmode)
24309 continue;
24310 /* Get dynamic information about call target only if we
24311 have no static information: we cannot generate both
24312 DW_AT_call_origin and DW_AT_call_target
24313 attributes. */
24314 if (ca_loc->symbol_ref == NULL_RTX)
24315 {
24316 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24317 {
24318 tloc = XEXP (XEXP (arg, 0), 1);
24319 continue;
24320 }
24321 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24322 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24323 {
24324 tlocc = XEXP (XEXP (arg, 0), 1);
24325 continue;
24326 }
24327 }
24328 reg = NULL;
24329 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24330 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24331 VAR_INIT_STATUS_INITIALIZED);
24332 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24333 {
24334 rtx mem = XEXP (XEXP (arg, 0), 0);
24335 reg = mem_loc_descriptor (XEXP (mem, 0),
24336 get_address_mode (mem),
24337 GET_MODE (mem),
24338 VAR_INIT_STATUS_INITIALIZED);
24339 }
24340 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24341 == DEBUG_PARAMETER_REF)
24342 {
24343 tree tdecl
24344 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24345 tdie = lookup_decl_die (tdecl);
24346 if (tdie == NULL)
24347 continue;
24348 arg_decl = tdecl;
24349 }
24350 else
24351 continue;
24352 if (reg == NULL
24353 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24354 != DEBUG_PARAMETER_REF)
24355 continue;
24356 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24357 VOIDmode,
24358 VAR_INIT_STATUS_INITIALIZED);
24359 if (val == NULL)
24360 continue;
24361 if (die == NULL)
24362 die = gen_call_site_die (decl, subr_die, ca_loc);
24363 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24364 NULL_TREE);
24365 add_desc_attribute (cdie, arg_decl);
24366 if (reg != NULL)
24367 add_AT_loc (cdie, DW_AT_location, reg);
24368 else if (tdie != NULL)
24369 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24370 tdie);
24371 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24372 if (next_arg != XEXP (arg, 1))
24373 {
24374 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24375 if (mode == VOIDmode)
24376 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24377 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24378 0), 1),
24379 mode, VOIDmode,
24380 VAR_INIT_STATUS_INITIALIZED);
24381 if (val != NULL)
24382 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24383 val);
24384 }
24385 }
24386 if (die == NULL
24387 && (ca_loc->symbol_ref || tloc))
24388 die = gen_call_site_die (decl, subr_die, ca_loc);
24389 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24390 {
24391 dw_loc_descr_ref tval = NULL;
24392
24393 if (tloc != NULL_RTX)
24394 tval = mem_loc_descriptor (tloc,
24395 GET_MODE (tloc) == VOIDmode
24396 ? Pmode : GET_MODE (tloc),
24397 VOIDmode,
24398 VAR_INIT_STATUS_INITIALIZED);
24399 if (tval)
24400 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24401 else if (tlocc != NULL_RTX)
24402 {
24403 tval = mem_loc_descriptor (tlocc,
24404 GET_MODE (tlocc) == VOIDmode
24405 ? Pmode : GET_MODE (tlocc),
24406 VOIDmode,
24407 VAR_INIT_STATUS_INITIALIZED);
24408 if (tval)
24409 add_AT_loc (die,
24410 dwarf_AT (DW_AT_call_target_clobbered),
24411 tval);
24412 }
24413 }
24414 if (die != NULL)
24415 {
24416 call_site_note_count++;
24417 if (ca_loc->tail_call_p)
24418 tail_call_site_note_count++;
24419 }
24420 }
24421 }
24422 call_arg_locations = NULL;
24423 call_arg_loc_last = NULL;
24424 if (tail_call_site_count >= 0
24425 && tail_call_site_count == tail_call_site_note_count
24426 && (!dwarf_strict || dwarf_version >= 5))
24427 {
24428 if (call_site_count >= 0
24429 && call_site_count == call_site_note_count)
24430 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24431 else
24432 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24433 }
24434 call_site_count = -1;
24435 tail_call_site_count = -1;
24436 }
24437
24438 /* Mark used types after we have created DIEs for the functions scopes. */
24439 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24440 }
24441
24442 /* Returns a hash value for X (which really is a die_struct). */
24443
24444 hashval_t
24445 block_die_hasher::hash (die_struct *d)
24446 {
24447 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24448 }
24449
24450 /* Return true if decl_id and die_parent of die_struct X is the same
24451 as decl_id and die_parent of die_struct Y. */
24452
24453 bool
24454 block_die_hasher::equal (die_struct *x, die_struct *y)
24455 {
24456 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24457 }
24458
24459 /* Hold information about markers for inlined entry points. */
24460 struct GTY ((for_user)) inline_entry_data
24461 {
24462 /* The block that's the inlined_function_outer_scope for an inlined
24463 function. */
24464 tree block;
24465
24466 /* The label at the inlined entry point. */
24467 const char *label_pfx;
24468 unsigned int label_num;
24469
24470 /* The view number to be used as the inlined entry point. */
24471 var_loc_view view;
24472 };
24473
24474 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24475 {
24476 typedef tree compare_type;
24477 static inline hashval_t hash (const inline_entry_data *);
24478 static inline bool equal (const inline_entry_data *, const_tree);
24479 };
24480
24481 /* Hash table routines for inline_entry_data. */
24482
24483 inline hashval_t
24484 inline_entry_data_hasher::hash (const inline_entry_data *data)
24485 {
24486 return htab_hash_pointer (data->block);
24487 }
24488
24489 inline bool
24490 inline_entry_data_hasher::equal (const inline_entry_data *data,
24491 const_tree block)
24492 {
24493 return data->block == block;
24494 }
24495
24496 /* Inlined entry points pending DIE creation in this compilation unit. */
24497
24498 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24499
24500
24501 /* Return TRUE if DECL, which may have been previously generated as
24502 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24503 true if decl (or its origin) is either an extern declaration or a
24504 class/namespace scoped declaration.
24505
24506 The declare_in_namespace support causes us to get two DIEs for one
24507 variable, both of which are declarations. We want to avoid
24508 considering one to be a specification, so we must test for
24509 DECLARATION and DW_AT_declaration. */
24510 static inline bool
24511 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24512 {
24513 return (old_die && TREE_STATIC (decl) && !declaration
24514 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24515 }
24516
24517 /* Return true if DECL is a local static. */
24518
24519 static inline bool
24520 local_function_static (tree decl)
24521 {
24522 gcc_assert (VAR_P (decl));
24523 return TREE_STATIC (decl)
24524 && DECL_CONTEXT (decl)
24525 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24526 }
24527
24528 /* Return true iff DECL overrides (presumably completes) the type of
24529 OLD_DIE within CONTEXT_DIE. */
24530
24531 static bool
24532 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24533 dw_die_ref context_die)
24534 {
24535 tree type = TREE_TYPE (decl);
24536 int cv_quals;
24537
24538 if (decl_by_reference_p (decl))
24539 {
24540 type = TREE_TYPE (type);
24541 cv_quals = TYPE_UNQUALIFIED;
24542 }
24543 else
24544 cv_quals = decl_quals (decl);
24545
24546 dw_die_ref type_die = modified_type_die (type,
24547 cv_quals | TYPE_QUALS (type),
24548 false,
24549 context_die);
24550
24551 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24552
24553 return type_die != old_type_die;
24554 }
24555
24556 /* Generate a DIE to represent a declared data object.
24557 Either DECL or ORIGIN must be non-null. */
24558
24559 static void
24560 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24561 {
24562 HOST_WIDE_INT off = 0;
24563 tree com_decl;
24564 tree decl_or_origin = decl ? decl : origin;
24565 tree ultimate_origin;
24566 dw_die_ref var_die;
24567 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24568 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24569 || class_or_namespace_scope_p (context_die));
24570 bool specialization_p = false;
24571 bool no_linkage_name = false;
24572
24573 /* While C++ inline static data members have definitions inside of the
24574 class, force the first DIE to be a declaration, then let gen_member_die
24575 reparent it to the class context and call gen_variable_die again
24576 to create the outside of the class DIE for the definition. */
24577 if (!declaration
24578 && old_die == NULL
24579 && decl
24580 && DECL_CONTEXT (decl)
24581 && TYPE_P (DECL_CONTEXT (decl))
24582 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24583 {
24584 declaration = true;
24585 if (dwarf_version < 5)
24586 no_linkage_name = true;
24587 }
24588
24589 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24590 if (decl || ultimate_origin)
24591 origin = ultimate_origin;
24592 com_decl = fortran_common (decl_or_origin, &off);
24593
24594 /* Symbol in common gets emitted as a child of the common block, in the form
24595 of a data member. */
24596 if (com_decl)
24597 {
24598 dw_die_ref com_die;
24599 dw_loc_list_ref loc = NULL;
24600 die_node com_die_arg;
24601
24602 var_die = lookup_decl_die (decl_or_origin);
24603 if (var_die)
24604 {
24605 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24606 {
24607 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24608 if (loc)
24609 {
24610 if (off)
24611 {
24612 /* Optimize the common case. */
24613 if (single_element_loc_list_p (loc)
24614 && loc->expr->dw_loc_opc == DW_OP_addr
24615 && loc->expr->dw_loc_next == NULL
24616 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24617 == SYMBOL_REF)
24618 {
24619 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24620 loc->expr->dw_loc_oprnd1.v.val_addr
24621 = plus_constant (GET_MODE (x), x , off);
24622 }
24623 else
24624 loc_list_plus_const (loc, off);
24625 }
24626 add_AT_location_description (var_die, DW_AT_location, loc);
24627 remove_AT (var_die, DW_AT_declaration);
24628 }
24629 }
24630 return;
24631 }
24632
24633 if (common_block_die_table == NULL)
24634 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24635
24636 com_die_arg.decl_id = DECL_UID (com_decl);
24637 com_die_arg.die_parent = context_die;
24638 com_die = common_block_die_table->find (&com_die_arg);
24639 if (! early_dwarf)
24640 loc = loc_list_from_tree (com_decl, 2, NULL);
24641 if (com_die == NULL)
24642 {
24643 const char *cnam
24644 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24645 die_node **slot;
24646
24647 com_die = new_die (DW_TAG_common_block, context_die, decl);
24648 add_name_and_src_coords_attributes (com_die, com_decl);
24649 if (loc)
24650 {
24651 add_AT_location_description (com_die, DW_AT_location, loc);
24652 /* Avoid sharing the same loc descriptor between
24653 DW_TAG_common_block and DW_TAG_variable. */
24654 loc = loc_list_from_tree (com_decl, 2, NULL);
24655 }
24656 else if (DECL_EXTERNAL (decl_or_origin))
24657 add_AT_flag (com_die, DW_AT_declaration, 1);
24658 if (want_pubnames ())
24659 add_pubname_string (cnam, com_die); /* ??? needed? */
24660 com_die->decl_id = DECL_UID (com_decl);
24661 slot = common_block_die_table->find_slot (com_die, INSERT);
24662 *slot = com_die;
24663 }
24664 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24665 {
24666 add_AT_location_description (com_die, DW_AT_location, loc);
24667 loc = loc_list_from_tree (com_decl, 2, NULL);
24668 remove_AT (com_die, DW_AT_declaration);
24669 }
24670 var_die = new_die (DW_TAG_variable, com_die, decl);
24671 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24672 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24673 decl_quals (decl_or_origin), false,
24674 context_die);
24675 add_alignment_attribute (var_die, decl);
24676 add_AT_flag (var_die, DW_AT_external, 1);
24677 if (loc)
24678 {
24679 if (off)
24680 {
24681 /* Optimize the common case. */
24682 if (single_element_loc_list_p (loc)
24683 && loc->expr->dw_loc_opc == DW_OP_addr
24684 && loc->expr->dw_loc_next == NULL
24685 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24686 {
24687 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24688 loc->expr->dw_loc_oprnd1.v.val_addr
24689 = plus_constant (GET_MODE (x), x, off);
24690 }
24691 else
24692 loc_list_plus_const (loc, off);
24693 }
24694 add_AT_location_description (var_die, DW_AT_location, loc);
24695 }
24696 else if (DECL_EXTERNAL (decl_or_origin))
24697 add_AT_flag (var_die, DW_AT_declaration, 1);
24698 if (decl)
24699 equate_decl_number_to_die (decl, var_die);
24700 return;
24701 }
24702
24703 if (old_die)
24704 {
24705 if (declaration)
24706 {
24707 /* A declaration that has been previously dumped, needs no
24708 further annotations, since it doesn't need location on
24709 the second pass. */
24710 return;
24711 }
24712 else if (decl_will_get_specification_p (old_die, decl, declaration)
24713 && !get_AT (old_die, DW_AT_specification))
24714 {
24715 /* Fall-thru so we can make a new variable die along with a
24716 DW_AT_specification. */
24717 }
24718 else if (origin && old_die->die_parent != context_die)
24719 {
24720 /* If we will be creating an inlined instance, we need a
24721 new DIE that will get annotated with
24722 DW_AT_abstract_origin. */
24723 gcc_assert (!DECL_ABSTRACT_P (decl));
24724 }
24725 else
24726 {
24727 /* If a DIE was dumped early, it still needs location info.
24728 Skip to where we fill the location bits. */
24729 var_die = old_die;
24730
24731 /* ??? In LTRANS we cannot annotate early created variably
24732 modified type DIEs without copying them and adjusting all
24733 references to them. Thus we dumped them again. Also add a
24734 reference to them but beware of -g0 compile and -g link
24735 in which case the reference will be already present. */
24736 tree type = TREE_TYPE (decl_or_origin);
24737 if (in_lto_p
24738 && ! get_AT (var_die, DW_AT_type)
24739 && variably_modified_type_p
24740 (type, decl_function_context (decl_or_origin)))
24741 {
24742 if (decl_by_reference_p (decl_or_origin))
24743 add_type_attribute (var_die, TREE_TYPE (type),
24744 TYPE_UNQUALIFIED, false, context_die);
24745 else
24746 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24747 false, context_die);
24748 }
24749
24750 goto gen_variable_die_location;
24751 }
24752 }
24753
24754 /* For static data members, the declaration in the class is supposed
24755 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24756 also in DWARF2; the specification should still be DW_TAG_variable
24757 referencing the DW_TAG_member DIE. */
24758 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24759 var_die = new_die (DW_TAG_member, context_die, decl);
24760 else
24761 var_die = new_die (DW_TAG_variable, context_die, decl);
24762
24763 if (origin != NULL)
24764 add_abstract_origin_attribute (var_die, origin);
24765
24766 /* Loop unrolling can create multiple blocks that refer to the same
24767 static variable, so we must test for the DW_AT_declaration flag.
24768
24769 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24770 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24771 sharing them.
24772
24773 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24774 else if (decl_will_get_specification_p (old_die, decl, declaration))
24775 {
24776 /* This is a definition of a C++ class level static. */
24777 add_AT_specification (var_die, old_die);
24778 specialization_p = true;
24779 if (DECL_NAME (decl))
24780 {
24781 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24782 struct dwarf_file_data * file_index = lookup_filename (s.file);
24783
24784 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24785 add_AT_file (var_die, DW_AT_decl_file, file_index);
24786
24787 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24788 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24789
24790 if (debug_column_info
24791 && s.column
24792 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24793 != (unsigned) s.column))
24794 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24795
24796 if (old_die->die_tag == DW_TAG_member)
24797 add_linkage_name (var_die, decl);
24798 }
24799 }
24800 else
24801 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24802
24803 if ((origin == NULL && !specialization_p)
24804 || (origin != NULL
24805 && !DECL_ABSTRACT_P (decl_or_origin)
24806 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24807 decl_function_context
24808 (decl_or_origin)))
24809 || (old_die && specialization_p
24810 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24811 {
24812 tree type = TREE_TYPE (decl_or_origin);
24813
24814 if (decl_by_reference_p (decl_or_origin))
24815 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24816 context_die);
24817 else
24818 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24819 context_die);
24820 }
24821
24822 if (origin == NULL && !specialization_p)
24823 {
24824 if (TREE_PUBLIC (decl))
24825 add_AT_flag (var_die, DW_AT_external, 1);
24826
24827 if (DECL_ARTIFICIAL (decl))
24828 add_AT_flag (var_die, DW_AT_artificial, 1);
24829
24830 add_alignment_attribute (var_die, decl);
24831
24832 add_accessibility_attribute (var_die, decl);
24833 }
24834
24835 if (declaration)
24836 add_AT_flag (var_die, DW_AT_declaration, 1);
24837
24838 if (decl && (DECL_ABSTRACT_P (decl)
24839 || !old_die || is_declaration_die (old_die)))
24840 equate_decl_number_to_die (decl, var_die);
24841
24842 gen_variable_die_location:
24843 if (! declaration
24844 && (! DECL_ABSTRACT_P (decl_or_origin)
24845 /* Local static vars are shared between all clones/inlines,
24846 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24847 already set. */
24848 || (VAR_P (decl_or_origin)
24849 && TREE_STATIC (decl_or_origin)
24850 && DECL_RTL_SET_P (decl_or_origin))))
24851 {
24852 if (early_dwarf)
24853 {
24854 add_pubname (decl_or_origin, var_die);
24855 /* For global register variables, emit DW_AT_location if possible
24856 already during early_dwarf, as late_global_decl won't be usually
24857 called. */
24858 if (DECL_HARD_REGISTER (decl_or_origin)
24859 && TREE_STATIC (decl_or_origin)
24860 && !decl_by_reference_p (decl_or_origin)
24861 && !get_AT (var_die, DW_AT_location)
24862 && !get_AT (var_die, DW_AT_const_value)
24863 && DECL_RTL_SET_P (decl_or_origin)
24864 && REG_P (DECL_RTL (decl_or_origin)))
24865 {
24866 dw_loc_descr_ref descr
24867 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24868 VAR_INIT_STATUS_INITIALIZED);
24869 if (descr)
24870 add_AT_loc (var_die, DW_AT_location, descr);
24871 }
24872 }
24873 else
24874 add_location_or_const_value_attribute (var_die, decl_or_origin,
24875 decl == NULL);
24876 }
24877 else
24878 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24879
24880 if ((dwarf_version >= 4 || !dwarf_strict)
24881 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24882 DW_AT_const_expr) == 1
24883 && !get_AT (var_die, DW_AT_const_expr)
24884 && !specialization_p)
24885 add_AT_flag (var_die, DW_AT_const_expr, 1);
24886
24887 if (!dwarf_strict)
24888 {
24889 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24890 DW_AT_inline);
24891 if (inl != -1
24892 && !get_AT (var_die, DW_AT_inline)
24893 && !specialization_p)
24894 add_AT_unsigned (var_die, DW_AT_inline, inl);
24895 }
24896 }
24897
24898 /* Generate a DIE to represent a named constant. */
24899
24900 static void
24901 gen_const_die (tree decl, dw_die_ref context_die)
24902 {
24903 dw_die_ref const_die;
24904 tree type = TREE_TYPE (decl);
24905
24906 const_die = lookup_decl_die (decl);
24907 if (const_die)
24908 return;
24909
24910 const_die = new_die (DW_TAG_constant, context_die, decl);
24911 equate_decl_number_to_die (decl, const_die);
24912 add_name_and_src_coords_attributes (const_die, decl);
24913 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24914 if (TREE_PUBLIC (decl))
24915 add_AT_flag (const_die, DW_AT_external, 1);
24916 if (DECL_ARTIFICIAL (decl))
24917 add_AT_flag (const_die, DW_AT_artificial, 1);
24918 tree_add_const_value_attribute_for_decl (const_die, decl);
24919 }
24920
24921 /* Generate a DIE to represent a label identifier. */
24922
24923 static void
24924 gen_label_die (tree decl, dw_die_ref context_die)
24925 {
24926 tree origin = decl_ultimate_origin (decl);
24927 dw_die_ref lbl_die = lookup_decl_die (decl);
24928 rtx insn;
24929 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24930
24931 if (!lbl_die)
24932 {
24933 lbl_die = new_die (DW_TAG_label, context_die, decl);
24934 equate_decl_number_to_die (decl, lbl_die);
24935
24936 if (origin != NULL)
24937 add_abstract_origin_attribute (lbl_die, origin);
24938 else
24939 add_name_and_src_coords_attributes (lbl_die, decl);
24940 }
24941
24942 if (DECL_ABSTRACT_P (decl))
24943 equate_decl_number_to_die (decl, lbl_die);
24944 else if (! early_dwarf)
24945 {
24946 insn = DECL_RTL_IF_SET (decl);
24947
24948 /* Deleted labels are programmer specified labels which have been
24949 eliminated because of various optimizations. We still emit them
24950 here so that it is possible to put breakpoints on them. */
24951 if (insn
24952 && (LABEL_P (insn)
24953 || ((NOTE_P (insn)
24954 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24955 {
24956 /* When optimization is enabled (via -O) some parts of the compiler
24957 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24958 represent source-level labels which were explicitly declared by
24959 the user. This really shouldn't be happening though, so catch
24960 it if it ever does happen. */
24961 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24962
24963 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24964 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24965 }
24966 else if (insn
24967 && NOTE_P (insn)
24968 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24969 && CODE_LABEL_NUMBER (insn) != -1)
24970 {
24971 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24972 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24973 }
24974 }
24975 }
24976
24977 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24978 attributes to the DIE for a block STMT, to describe where the inlined
24979 function was called from. This is similar to add_src_coords_attributes. */
24980
24981 static inline void
24982 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24983 {
24984 /* We can end up with BUILTINS_LOCATION here. */
24985 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24986 return;
24987
24988 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
24989 expanded_location s = expand_location (locus);
24990
24991 if (dwarf_version >= 3 || !dwarf_strict)
24992 {
24993 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24994 add_AT_unsigned (die, DW_AT_call_line, s.line);
24995 if (debug_column_info && s.column)
24996 add_AT_unsigned (die, DW_AT_call_column, s.column);
24997 unsigned discr = get_discriminator_from_loc (locus);
24998 if (discr != 0)
24999 add_AT_unsigned (die, DW_AT_GNU_discriminator, discr);
25000 }
25001 }
25002
25003
25004 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
25005 Add low_pc and high_pc attributes to the DIE for a block STMT. */
25006
25007 static inline void
25008 add_high_low_attributes (tree stmt, dw_die_ref die)
25009 {
25010 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25011
25012 if (inline_entry_data **iedp
25013 = !inline_entry_data_table ? NULL
25014 : inline_entry_data_table->find_slot_with_hash (stmt,
25015 htab_hash_pointer (stmt),
25016 NO_INSERT))
25017 {
25018 inline_entry_data *ied = *iedp;
25019 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
25020 gcc_assert (debug_inline_points);
25021 gcc_assert (inlined_function_outer_scope_p (stmt));
25022
25023 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
25024 add_AT_lbl_id (die, DW_AT_entry_pc, label);
25025
25026 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
25027 && !dwarf_strict)
25028 {
25029 if (!output_asm_line_debug_info ())
25030 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
25031 else
25032 {
25033 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
25034 /* FIXME: this will resolve to a small number. Could we
25035 possibly emit smaller data? Ideally we'd emit a
25036 uleb128, but that would make the size of DIEs
25037 impossible for the compiler to compute, since it's
25038 the assembler that computes the value of the view
25039 label in this case. Ideally, we'd have a single form
25040 encompassing both the address and the view, and
25041 indirecting them through a table might make things
25042 easier, but even that would be more wasteful,
25043 space-wise, than what we have now. */
25044 add_AT_symview (die, DW_AT_GNU_entry_view, label);
25045 }
25046 }
25047
25048 inline_entry_data_table->clear_slot (iedp);
25049 }
25050
25051 if (BLOCK_FRAGMENT_CHAIN (stmt)
25052 && (dwarf_version >= 3 || !dwarf_strict))
25053 {
25054 tree chain, superblock = NULL_TREE;
25055 dw_die_ref pdie;
25056 dw_attr_node *attr = NULL;
25057
25058 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
25059 {
25060 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25061 BLOCK_NUMBER (stmt));
25062 add_AT_lbl_id (die, DW_AT_entry_pc, label);
25063 }
25064
25065 /* Optimize duplicate .debug_ranges lists or even tails of
25066 lists. If this BLOCK has same ranges as its supercontext,
25067 lookup DW_AT_ranges attribute in the supercontext (and
25068 recursively so), verify that the ranges_table contains the
25069 right values and use it instead of adding a new .debug_range. */
25070 for (chain = stmt, pdie = die;
25071 BLOCK_SAME_RANGE (chain);
25072 chain = BLOCK_SUPERCONTEXT (chain))
25073 {
25074 dw_attr_node *new_attr;
25075
25076 pdie = pdie->die_parent;
25077 if (pdie == NULL)
25078 break;
25079 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
25080 break;
25081 new_attr = get_AT (pdie, DW_AT_ranges);
25082 if (new_attr == NULL
25083 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
25084 break;
25085 attr = new_attr;
25086 superblock = BLOCK_SUPERCONTEXT (chain);
25087 }
25088 if (attr != NULL
25089 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
25090 == (int)BLOCK_NUMBER (superblock))
25091 && BLOCK_FRAGMENT_CHAIN (superblock))
25092 {
25093 unsigned long off = attr->dw_attr_val.v.val_offset;
25094 unsigned long supercnt = 0, thiscnt = 0;
25095 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
25096 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25097 {
25098 ++supercnt;
25099 gcc_checking_assert ((*ranges_table)[off + supercnt].num
25100 == (int)BLOCK_NUMBER (chain));
25101 }
25102 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
25103 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
25104 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25105 ++thiscnt;
25106 gcc_assert (supercnt >= thiscnt);
25107 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
25108 false);
25109 note_rnglist_head (off + supercnt - thiscnt);
25110 return;
25111 }
25112
25113 unsigned int offset = add_ranges (stmt, true);
25114 add_AT_range_list (die, DW_AT_ranges, offset, false);
25115 note_rnglist_head (offset);
25116
25117 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
25118 chain = BLOCK_FRAGMENT_CHAIN (stmt);
25119 do
25120 {
25121 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
25122 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
25123 chain = BLOCK_FRAGMENT_CHAIN (chain);
25124 }
25125 while (chain);
25126 add_ranges (NULL);
25127 }
25128 else
25129 {
25130 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
25131 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25132 BLOCK_NUMBER (stmt));
25133 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
25134 BLOCK_NUMBER (stmt));
25135 add_AT_low_high_pc (die, label, label_high, false);
25136 }
25137 }
25138
25139 /* Generate a DIE for a lexical block. */
25140
25141 static void
25142 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
25143 {
25144 dw_die_ref old_die = lookup_block_die (stmt);
25145 dw_die_ref stmt_die = NULL;
25146 if (!old_die)
25147 {
25148 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25149 equate_block_to_die (stmt, stmt_die);
25150 }
25151
25152 if (BLOCK_ABSTRACT_ORIGIN (stmt))
25153 {
25154 /* If this is an inlined or conrecte instance, create a new lexical
25155 die for anything below to attach DW_AT_abstract_origin to. */
25156 if (old_die)
25157 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25158
25159 tree origin = block_ultimate_origin (stmt);
25160 if (origin != NULL_TREE && (origin != stmt || old_die))
25161 add_abstract_origin_attribute (stmt_die, origin);
25162
25163 old_die = NULL;
25164 }
25165
25166 if (old_die)
25167 stmt_die = old_die;
25168
25169 /* A non abstract block whose blocks have already been reordered
25170 should have the instruction range for this block. If so, set the
25171 high/low attributes. */
25172 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
25173 {
25174 gcc_assert (stmt_die);
25175 add_high_low_attributes (stmt, stmt_die);
25176 }
25177
25178 decls_for_scope (stmt, stmt_die);
25179 }
25180
25181 /* Generate a DIE for an inlined subprogram. */
25182
25183 static void
25184 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
25185 {
25186 tree decl = block_ultimate_origin (stmt);
25187
25188 /* Make sure any inlined functions are known to be inlineable. */
25189 gcc_checking_assert (DECL_ABSTRACT_P (decl)
25190 || cgraph_function_possibly_inlined_p (decl));
25191
25192 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
25193
25194 if (call_arg_locations || debug_inline_points)
25195 equate_block_to_die (stmt, subr_die);
25196 add_abstract_origin_attribute (subr_die, decl);
25197 if (TREE_ASM_WRITTEN (stmt))
25198 add_high_low_attributes (stmt, subr_die);
25199 add_call_src_coords_attributes (stmt, subr_die);
25200
25201 /* The inliner creates an extra BLOCK for the parameter setup,
25202 we want to merge that with the actual outermost BLOCK of the
25203 inlined function to avoid duplicate locals in consumers.
25204 Do that by doing the recursion to subblocks on the single subblock
25205 of STMT. */
25206 bool unwrap_one = false;
25207 tree sub = BLOCK_SUBBLOCKS (stmt);
25208 if (sub)
25209 {
25210 tree origin = block_ultimate_origin (sub);
25211 if (origin
25212 && TREE_CODE (origin) == BLOCK
25213 && BLOCK_SUPERCONTEXT (origin) == decl)
25214 unwrap_one = true;
25215 for (tree next = BLOCK_CHAIN (sub); unwrap_one && next;
25216 next = BLOCK_CHAIN (next))
25217 if (BLOCK_FRAGMENT_ORIGIN (next) != sub)
25218 unwrap_one = false;
25219 }
25220 decls_for_scope (stmt, subr_die, !unwrap_one);
25221 if (unwrap_one)
25222 {
25223 decls_for_scope (sub, subr_die);
25224 for (sub = BLOCK_CHAIN (sub); sub; sub = BLOCK_CHAIN (sub))
25225 gen_block_die (sub, subr_die);
25226 }
25227 }
25228
25229 /* Generate a DIE for a field in a record, or structure. CTX is required: see
25230 the comment for VLR_CONTEXT. */
25231
25232 static void
25233 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25234 {
25235 dw_die_ref decl_die;
25236
25237 if (TREE_TYPE (decl) == error_mark_node)
25238 return;
25239
25240 decl_die = new_die (DW_TAG_member, context_die, decl);
25241 add_name_and_src_coords_attributes (decl_die, decl);
25242 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
25243 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25244 context_die);
25245
25246 if (DECL_BIT_FIELD_TYPE (decl))
25247 {
25248 add_byte_size_attribute (decl_die, decl);
25249 add_bit_size_attribute (decl_die, decl);
25250 add_bit_offset_attribute (decl_die, decl);
25251 }
25252
25253 add_alignment_attribute (decl_die, decl);
25254
25255 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25256 add_data_member_location_attribute (decl_die, decl, ctx);
25257
25258 if (DECL_ARTIFICIAL (decl))
25259 add_AT_flag (decl_die, DW_AT_artificial, 1);
25260
25261 add_accessibility_attribute (decl_die, decl);
25262
25263 /* Add DW_AT_export_symbols to anonymous unions or structs. */
25264 if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
25265 if (tree type = member_declared_type (decl))
25266 if (lang_hooks.types.type_dwarf_attribute (TYPE_MAIN_VARIANT (type),
25267 DW_AT_export_symbols) != -1)
25268 {
25269 dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
25270 if (type_die && get_AT (type_die, DW_AT_export_symbols) == NULL)
25271 add_AT_flag (type_die, DW_AT_export_symbols, 1);
25272 }
25273
25274 /* Equate decl number to die, so that we can look up this decl later on. */
25275 equate_decl_number_to_die (decl, decl_die);
25276 }
25277
25278 /* Generate a DIE for a pointer to a member type. TYPE can be an
25279 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25280 pointer to member function. */
25281
25282 static void
25283 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25284 {
25285 if (lookup_type_die (type))
25286 return;
25287
25288 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
25289 scope_die_for (type, context_die), type);
25290
25291 equate_type_number_to_die (type, ptr_die);
25292 add_AT_die_ref (ptr_die, DW_AT_containing_type,
25293 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25294 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
25295 context_die);
25296 add_alignment_attribute (ptr_die, type);
25297
25298 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25299 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25300 {
25301 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
25302 add_AT_loc (ptr_die, DW_AT_use_location, op);
25303 }
25304 }
25305
25306 static char *producer_string;
25307
25308 /* Given a C and/or C++ language/version string return the "highest".
25309 C++ is assumed to be "higher" than C in this case. Used for merging
25310 LTO translation unit languages. */
25311 static const char *
25312 highest_c_language (const char *lang1, const char *lang2)
25313 {
25314 if (strcmp ("GNU C++26", lang1) == 0 || strcmp ("GNU C++26", lang2) == 0)
25315 return "GNU C++26";
25316 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
25317 return "GNU C++23";
25318 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
25319 return "GNU C++20";
25320 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
25321 return "GNU C++17";
25322 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
25323 return "GNU C++14";
25324 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
25325 return "GNU C++11";
25326 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
25327 return "GNU C++98";
25328
25329 if (strcmp ("GNU C2Y", lang1) == 0 || strcmp ("GNU C2Y", lang2) == 0)
25330 return "GNU C2Y";
25331 if (strcmp ("GNU C23", lang1) == 0 || strcmp ("GNU C23", lang2) == 0)
25332 return "GNU C23";
25333 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
25334 return "GNU C17";
25335 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
25336 return "GNU C11";
25337 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
25338 return "GNU C99";
25339 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
25340 return "GNU C89";
25341
25342 gcc_unreachable ();
25343 }
25344
25345
25346 /* Generate the DIE for the compilation unit. */
25347
25348 static dw_die_ref
25349 gen_compile_unit_die (const char *filename)
25350 {
25351 dw_die_ref die;
25352 const char *language_string = lang_hooks.name;
25353 int language, lname, lversion;
25354
25355 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25356
25357 if (filename)
25358 {
25359 add_filename_attribute (die, filename);
25360 /* Don't add cwd for <built-in>. */
25361 if (filename[0] != '<')
25362 add_comp_dir_attribute (die);
25363 }
25364
25365 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25366
25367 /* If our producer is LTO try to figure out a common language to use
25368 from the global list of translation units. */
25369 if (strcmp (language_string, "GNU GIMPLE") == 0)
25370 {
25371 unsigned i;
25372 tree t;
25373 const char *common_lang = NULL;
25374
25375 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25376 {
25377 if (!TRANSLATION_UNIT_LANGUAGE (t))
25378 continue;
25379 if (!common_lang)
25380 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25381 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25382 ;
25383 else if (startswith (common_lang, "GNU C")
25384 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25385 /* Mixing C and C++ is ok, use C++ in that case. */
25386 common_lang = highest_c_language (common_lang,
25387 TRANSLATION_UNIT_LANGUAGE (t));
25388 else
25389 {
25390 /* Fall back to C. */
25391 common_lang = NULL;
25392 break;
25393 }
25394 }
25395
25396 if (common_lang)
25397 language_string = common_lang;
25398 }
25399
25400 language = DW_LANG_C;
25401 lname = 0;
25402 lversion = 0;
25403 if (startswith (language_string, "GNU C")
25404 && ISDIGIT (language_string[5]))
25405 {
25406 language = DW_LANG_C89;
25407 if (dwarf_version >= 3 || !dwarf_strict)
25408 {
25409 if (strcmp (language_string, "GNU C89") != 0)
25410 language = DW_LANG_C99;
25411
25412 if (dwarf_version >= 5 /* || !dwarf_strict */)
25413 {
25414 if (strcmp (language_string, "GNU C11") == 0)
25415 language = DW_LANG_C11;
25416 else if (strcmp (language_string, "GNU C17") == 0)
25417 {
25418 language = DW_LANG_C11;
25419 lname = DW_LNAME_C;
25420 lversion = 201710;
25421 }
25422 else if (strcmp (language_string, "GNU C23") == 0)
25423 {
25424 language = DW_LANG_C11;
25425 lname = DW_LNAME_C;
25426 lversion = 202311;
25427 }
25428 else if (strcmp (language_string, "GNU C2Y") == 0)
25429 {
25430 language = DW_LANG_C11;
25431 lname = DW_LNAME_C;
25432 lversion = 202500;
25433 }
25434 }
25435 }
25436 }
25437 else if (startswith (language_string, "GNU C++"))
25438 {
25439 language = DW_LANG_C_plus_plus;
25440 if (dwarf_version >= 5 /* || !dwarf_strict */)
25441 {
25442 if (strcmp (language_string, "GNU C++11") == 0)
25443 language = DW_LANG_C_plus_plus_11;
25444 else if (strcmp (language_string, "GNU C++14") == 0)
25445 language = DW_LANG_C_plus_plus_14;
25446 else if (strcmp (language_string, "GNU C++17") == 0)
25447 {
25448 language = DW_LANG_C_plus_plus_14;
25449 lname = DW_LNAME_C_plus_plus;
25450 lversion = 201703;
25451 }
25452 else if (strcmp (language_string, "GNU C++20") == 0)
25453 {
25454 language = DW_LANG_C_plus_plus_14;
25455 lname = DW_LNAME_C_plus_plus;
25456 lversion = 202002;
25457 }
25458 else if (strcmp (language_string, "GNU C++23") == 0)
25459 {
25460 language = DW_LANG_C_plus_plus_14;
25461 lname = DW_LNAME_C_plus_plus;
25462 lversion = 202302;
25463 }
25464 else if (strcmp (language_string, "GNU C++26") == 0)
25465 {
25466 language = DW_LANG_C_plus_plus_14;
25467 lname = DW_LNAME_C_plus_plus;
25468 lversion = 202400;
25469 }
25470 }
25471 }
25472 else if (strcmp (language_string, "GNU F77") == 0)
25473 language = DW_LANG_Fortran77;
25474 else if (strcmp (language_string, "GCC COBOL") == 0)
25475 language = DW_LANG_Cobol85;
25476 else if (strcmp (language_string, "GNU Modula-2") == 0)
25477 language = DW_LANG_Modula2;
25478 else if (dwarf_version >= 3 || !dwarf_strict)
25479 {
25480 if (strcmp (language_string, "GNU Ada") == 0)
25481 language = DW_LANG_Ada95;
25482 else if (startswith (language_string, "GNU Fortran"))
25483 {
25484 language = DW_LANG_Fortran95;
25485 if (dwarf_version >= 5 /* || !dwarf_strict */)
25486 {
25487 if (strcmp (language_string, "GNU Fortran2003") == 0)
25488 language = DW_LANG_Fortran03;
25489 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25490 language = DW_LANG_Fortran08;
25491 }
25492 }
25493 else if (strcmp (language_string, "GNU Objective-C") == 0)
25494 language = DW_LANG_ObjC;
25495 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25496 language = DW_LANG_ObjC_plus_plus;
25497 else if (strcmp (language_string, "GNU D") == 0)
25498 language = DW_LANG_D;
25499 else if (dwarf_version >= 5 || !dwarf_strict)
25500 {
25501 if (strcmp (language_string, "GNU Go") == 0)
25502 language = DW_LANG_Go;
25503 else if (strcmp (language_string, "GNU Rust") == 0)
25504 language = DW_LANG_Rust;
25505 }
25506 }
25507 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25508 else if (startswith (language_string, "GNU Fortran"))
25509 language = DW_LANG_Fortran90;
25510 /* Likewise for Ada. */
25511 else if (strcmp (language_string, "GNU Ada") == 0)
25512 language = DW_LANG_Ada83;
25513
25514 add_AT_unsigned (die, DW_AT_language, language);
25515 if (lname && dwarf_version >= 5 && !dwarf_strict)
25516 {
25517 add_AT_unsigned (die, DW_AT_language_name, lname);
25518 add_AT_unsigned (die, DW_AT_language_version, lversion);
25519 }
25520
25521 switch (language)
25522 {
25523 case DW_LANG_Fortran77:
25524 case DW_LANG_Fortran90:
25525 case DW_LANG_Fortran95:
25526 case DW_LANG_Fortran03:
25527 case DW_LANG_Fortran08:
25528 /* Fortran has case insensitive identifiers and the front-end
25529 lowercases everything. */
25530 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25531 break;
25532 case DW_LANG_Cobol85:
25533 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_case_insensitive);
25534 break;
25535 default:
25536 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25537 break;
25538 }
25539 return die;
25540 }
25541
25542 /* Generate the DIE for a base class. */
25543
25544 static void
25545 gen_inheritance_die (tree binfo, tree access, tree type,
25546 dw_die_ref context_die)
25547 {
25548 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25549 struct vlr_context ctx = { type, NULL };
25550
25551 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25552 context_die);
25553 add_data_member_location_attribute (die, binfo, &ctx);
25554
25555 if (BINFO_VIRTUAL_P (binfo))
25556 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25557
25558 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25559 children, otherwise the default is DW_ACCESS_public. In DWARF2
25560 the default has always been DW_ACCESS_private. */
25561 if (access == access_public_node)
25562 {
25563 if (dwarf_version == 2
25564 || context_die->die_tag == DW_TAG_class_type)
25565 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25566 }
25567 else if (access == access_protected_node)
25568 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25569 else if (dwarf_version > 2
25570 && context_die->die_tag != DW_TAG_class_type)
25571 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25572 }
25573
25574 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25575 structure. */
25576
25577 static bool
25578 is_variant_part (tree decl)
25579 {
25580 return (TREE_CODE (decl) == FIELD_DECL
25581 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25582 }
25583
25584 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25585 return the FIELD_DECL. Return NULL_TREE otherwise. */
25586
25587 static tree
25588 analyze_discr_in_predicate (tree operand, tree struct_type)
25589 {
25590 while (CONVERT_EXPR_P (operand))
25591 operand = TREE_OPERAND (operand, 0);
25592
25593 /* Match field access to members of struct_type only. */
25594 if (TREE_CODE (operand) == COMPONENT_REF
25595 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25596 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25597 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25598 return TREE_OPERAND (operand, 1);
25599 else
25600 return NULL_TREE;
25601 }
25602
25603 /* Check that SRC is a constant integer that can be represented as a native
25604 integer constant (either signed or unsigned). If so, store it into DEST and
25605 return true. Return false otherwise. */
25606
25607 static bool
25608 get_discr_value (tree src, dw_discr_value *dest)
25609 {
25610 tree discr_type = TREE_TYPE (src);
25611
25612 if (lang_hooks.types.get_debug_type)
25613 {
25614 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25615 if (debug_type != NULL)
25616 discr_type = debug_type;
25617 }
25618
25619 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25620 return false;
25621
25622 /* Signedness can vary between the original type and the debug type. This
25623 can happen for character types in Ada for instance: the character type
25624 used for code generation can be signed, to be compatible with the C one,
25625 but from a debugger point of view, it must be unsigned. */
25626 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25627 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25628
25629 if (is_orig_unsigned != is_debug_unsigned)
25630 src = fold_convert (discr_type, src);
25631
25632 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25633 return false;
25634
25635 dest->pos = is_debug_unsigned;
25636 if (is_debug_unsigned)
25637 dest->v.uval = tree_to_uhwi (src);
25638 else
25639 dest->v.sval = tree_to_shwi (src);
25640
25641 return true;
25642 }
25643
25644 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25645 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25646 store NULL_TREE in DISCR_DECL. Otherwise:
25647
25648 - store the discriminant field in STRUCT_TYPE that controls the variant
25649 part to *DISCR_DECL
25650
25651 - put in *DISCR_LISTS_P an array where for each variant, the item
25652 represents the corresponding matching list of discriminant values.
25653
25654 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25655 the above array.
25656
25657 Note that when the array is allocated (i.e. when the analysis is
25658 successful), it is up to the caller to free the array. */
25659
25660 static void
25661 analyze_variants_discr (tree variant_part_decl,
25662 tree struct_type,
25663 tree *discr_decl,
25664 dw_discr_list_ref **discr_lists_p,
25665 unsigned *discr_lists_length)
25666 {
25667 tree variant_part_type = TREE_TYPE (variant_part_decl);
25668 tree variant;
25669 dw_discr_list_ref *discr_lists;
25670 unsigned i;
25671
25672 /* Compute how many variants there are in this variant part. */
25673 *discr_lists_length = 0;
25674 for (variant = TYPE_FIELDS (variant_part_type);
25675 variant != NULL_TREE;
25676 variant = DECL_CHAIN (variant))
25677 ++*discr_lists_length;
25678
25679 *discr_decl = NULL_TREE;
25680 *discr_lists_p
25681 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25682 sizeof (**discr_lists_p));
25683 discr_lists = *discr_lists_p;
25684
25685 /* And then analyze all variants to extract discriminant information for all
25686 of them. This analysis is conservative: as soon as we detect something we
25687 do not support, abort everything and pretend we found nothing. */
25688 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25689 variant != NULL_TREE;
25690 variant = DECL_CHAIN (variant), ++i)
25691 {
25692 tree match_expr = DECL_QUALIFIER (variant);
25693
25694 /* Now, try to analyze the predicate and deduce a discriminant for
25695 it. */
25696 if (match_expr == boolean_true_node)
25697 /* Typically happens for the default variant: it matches all cases that
25698 previous variants rejected. Don't output any matching value for
25699 this one. */
25700 continue;
25701
25702 /* The following loop tries to iterate over each discriminant
25703 possibility: single values or ranges. */
25704 while (match_expr != NULL_TREE)
25705 {
25706 tree next_round_match_expr;
25707 tree candidate_discr = NULL_TREE;
25708 dw_discr_list_ref new_node = NULL;
25709
25710 /* Possibilities are matched one after the other by nested
25711 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25712 continue with the rest at next iteration. */
25713 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25714 {
25715 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25716 match_expr = TREE_OPERAND (match_expr, 1);
25717 }
25718 else
25719 next_round_match_expr = NULL_TREE;
25720
25721 if (match_expr == boolean_false_node)
25722 /* This sub-expression matches nothing: just wait for the next
25723 one. */
25724 ;
25725
25726 else if (TREE_CODE (match_expr) == EQ_EXPR)
25727 {
25728 /* We are matching: <discr_field> == <integer_cst>
25729 This sub-expression matches a single value. */
25730 tree integer_cst = TREE_OPERAND (match_expr, 1);
25731
25732 candidate_discr
25733 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25734 struct_type);
25735
25736 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25737 if (!get_discr_value (integer_cst,
25738 &new_node->dw_discr_lower_bound))
25739 goto abort;
25740 new_node->dw_discr_range = false;
25741 }
25742
25743 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25744 {
25745 /* We are matching:
25746 <discr_field> > <integer_cst>
25747 && <discr_field> < <integer_cst>.
25748 This sub-expression matches the range of values between the
25749 two matched integer constants. Note that comparisons can be
25750 inclusive or exclusive. */
25751 tree candidate_discr_1, candidate_discr_2;
25752 tree lower_cst, upper_cst;
25753 bool lower_cst_included, upper_cst_included;
25754 tree lower_op = TREE_OPERAND (match_expr, 0);
25755 tree upper_op = TREE_OPERAND (match_expr, 1);
25756
25757 /* When the comparison is exclusive, the integer constant is not
25758 the discriminant range bound we are looking for: we will have
25759 to increment or decrement it. */
25760 if (TREE_CODE (lower_op) == GE_EXPR)
25761 lower_cst_included = true;
25762 else if (TREE_CODE (lower_op) == GT_EXPR)
25763 lower_cst_included = false;
25764 else
25765 goto abort;
25766
25767 if (TREE_CODE (upper_op) == LE_EXPR)
25768 upper_cst_included = true;
25769 else if (TREE_CODE (upper_op) == LT_EXPR)
25770 upper_cst_included = false;
25771 else
25772 goto abort;
25773
25774 /* Extract the discriminant from the first operand and check it
25775 is consistant with the same analysis in the second
25776 operand. */
25777 candidate_discr_1
25778 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25779 struct_type);
25780 candidate_discr_2
25781 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25782 struct_type);
25783 if (candidate_discr_1 == candidate_discr_2)
25784 candidate_discr = candidate_discr_1;
25785 else
25786 goto abort;
25787
25788 /* Extract bounds from both. */
25789 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25790 lower_cst = TREE_OPERAND (lower_op, 1);
25791 upper_cst = TREE_OPERAND (upper_op, 1);
25792
25793 if (!lower_cst_included)
25794 lower_cst
25795 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25796 build_int_cst (TREE_TYPE (lower_cst), 1));
25797 if (!upper_cst_included)
25798 upper_cst
25799 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25800 build_int_cst (TREE_TYPE (upper_cst), 1));
25801
25802 if (!get_discr_value (lower_cst,
25803 &new_node->dw_discr_lower_bound)
25804 || !get_discr_value (upper_cst,
25805 &new_node->dw_discr_upper_bound))
25806 goto abort;
25807
25808 new_node->dw_discr_range = true;
25809 }
25810
25811 else if ((candidate_discr
25812 = analyze_discr_in_predicate (match_expr, struct_type))
25813 && (TREE_TYPE (candidate_discr) == boolean_type_node
25814 || TREE_TYPE (TREE_TYPE (candidate_discr))
25815 == boolean_type_node))
25816 {
25817 /* We are matching: <discr_field> for a boolean discriminant.
25818 This sub-expression matches boolean_true_node. */
25819 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25820 if (!get_discr_value (boolean_true_node,
25821 &new_node->dw_discr_lower_bound))
25822 goto abort;
25823 new_node->dw_discr_range = false;
25824 }
25825
25826 else
25827 /* Unsupported sub-expression: we cannot determine the set of
25828 matching discriminant values. Abort everything. */
25829 goto abort;
25830
25831 /* If the discriminant info is not consistant with what we saw so
25832 far, consider the analysis failed and abort everything. */
25833 if (candidate_discr == NULL_TREE
25834 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25835 goto abort;
25836 else
25837 *discr_decl = candidate_discr;
25838
25839 if (new_node != NULL)
25840 {
25841 new_node->dw_discr_next = discr_lists[i];
25842 discr_lists[i] = new_node;
25843 }
25844 match_expr = next_round_match_expr;
25845 }
25846 }
25847
25848 /* If we reach this point, we could match everything we were interested
25849 in. */
25850 return;
25851
25852 abort:
25853 /* Clean all data structure and return no result. */
25854 free (*discr_lists_p);
25855 *discr_lists_p = NULL;
25856 *discr_decl = NULL_TREE;
25857 }
25858
25859 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25860 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25861 under CONTEXT_DIE.
25862
25863 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25864 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25865 this type, which are record types, represent the available variants and each
25866 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25867 values are inferred from these attributes.
25868
25869 In trees, the offsets for the fields inside these sub-records are relative
25870 to the variant part itself, whereas the corresponding DIEs should have
25871 offset attributes that are relative to the embedding record base address.
25872 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25873 must be an expression that computes the offset of the variant part to
25874 describe in DWARF. */
25875
25876 static void
25877 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25878 dw_die_ref context_die)
25879 {
25880 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25881 tree variant_part_offset = vlr_ctx->variant_part_offset;
25882
25883 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25884 NULL_TREE if there is no such field. */
25885 tree discr_decl = NULL_TREE;
25886 dw_discr_list_ref *discr_lists;
25887 unsigned discr_lists_length = 0;
25888 unsigned i;
25889
25890 dw_die_ref dwarf_proc_die = NULL;
25891 dw_die_ref variant_part_die
25892 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25893
25894 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25895
25896 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25897 &discr_decl, &discr_lists, &discr_lists_length);
25898
25899 if (discr_decl != NULL_TREE)
25900 {
25901 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25902
25903 if (discr_die)
25904 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25905 else
25906 /* We have no DIE for the discriminant, so just discard all
25907 discrimimant information in the output. */
25908 discr_decl = NULL_TREE;
25909 }
25910
25911 /* If the offset for this variant part is more complex than a constant,
25912 create a DWARF procedure for it so that we will not have to generate
25913 DWARF expressions for it for each member. */
25914 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25915 && (dwarf_version >= 3 || !dwarf_strict))
25916 {
25917 struct loc_descr_context ctx = {
25918 vlr_ctx->struct_type, /* context_type */
25919 NULL_TREE, /* base_decl */
25920 NULL, /* dpi */
25921 false, /* placeholder_arg */
25922 false, /* placeholder_seen */
25923 false /* strict_signedness */
25924 };
25925 const tree dwarf_proc_fndecl
25926 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25927 build_function_type (TREE_TYPE (variant_part_offset),
25928 NULL_TREE));
25929 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25930 const dw_loc_descr_ref dwarf_proc_body
25931 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25932
25933 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25934 dwarf_proc_fndecl, context_die);
25935 if (dwarf_proc_die != NULL)
25936 variant_part_offset = dwarf_proc_call;
25937 }
25938
25939 /* Output DIEs for all variants. */
25940 i = 0;
25941 for (tree variant = TYPE_FIELDS (variant_part_type);
25942 variant != NULL_TREE;
25943 variant = DECL_CHAIN (variant), ++i)
25944 {
25945 tree variant_type = TREE_TYPE (variant);
25946 dw_die_ref variant_die;
25947
25948 /* All variants (i.e. members of a variant part) are supposed to be
25949 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25950 under these records. */
25951 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25952
25953 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25954 equate_decl_number_to_die (variant, variant_die);
25955
25956 /* Output discriminant values this variant matches, if any. */
25957 if (discr_decl == NULL || discr_lists[i] == NULL)
25958 /* In the case we have discriminant information at all, this is
25959 probably the default variant: as the standard says, don't
25960 output any discriminant value/list attribute. */
25961 ;
25962 else if (discr_lists[i]->dw_discr_next == NULL
25963 && !discr_lists[i]->dw_discr_range)
25964 /* If there is only one accepted value, don't bother outputting a
25965 list. */
25966 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25967 else
25968 add_discr_list (variant_die, discr_lists[i]);
25969
25970 for (tree member = TYPE_FIELDS (variant_type);
25971 member != NULL_TREE;
25972 member = DECL_CHAIN (member))
25973 {
25974 struct vlr_context vlr_sub_ctx = {
25975 vlr_ctx->struct_type, /* struct_type */
25976 NULL /* variant_part_offset */
25977 };
25978 if (is_variant_part (member))
25979 {
25980 /* All offsets for fields inside variant parts are relative to
25981 the top-level embedding RECORD_TYPE's base address. On the
25982 other hand, offsets in GCC's types are relative to the
25983 nested-most variant part. So we have to sum offsets each time
25984 we recurse. */
25985
25986 vlr_sub_ctx.variant_part_offset
25987 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25988 variant_part_offset, byte_position (member));
25989 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25990 }
25991 else
25992 {
25993 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25994 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25995 }
25996 }
25997 }
25998
25999 free (discr_lists);
26000 }
26001
26002 /* Generate a DIE for a class member. */
26003
26004 static void
26005 gen_member_die (tree type, dw_die_ref context_die)
26006 {
26007 tree member;
26008 tree binfo = TYPE_BINFO (type);
26009
26010 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
26011
26012 /* If this is not an incomplete type, output descriptions of each of its
26013 members. Note that as we output the DIEs necessary to represent the
26014 members of this record or union type, we will also be trying to output
26015 DIEs to represent the *types* of those members. However the `type'
26016 function (above) will specifically avoid generating type DIEs for member
26017 types *within* the list of member DIEs for this (containing) type except
26018 for those types (of members) which are explicitly marked as also being
26019 members of this (containing) type themselves. The g++ front- end can
26020 force any given type to be treated as a member of some other (containing)
26021 type by setting the TYPE_CONTEXT of the given (member) type to point to
26022 the TREE node representing the appropriate (containing) type. */
26023
26024 /* First output info about the base classes. */
26025 if (binfo && early_dwarf)
26026 {
26027 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
26028 int i;
26029 tree base;
26030
26031 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
26032 gen_inheritance_die (base,
26033 (accesses ? (*accesses)[i] : access_public_node),
26034 type,
26035 context_die);
26036 }
26037
26038 /* Now output info about the members. */
26039 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
26040 {
26041 /* Ignore clones. */
26042 if (DECL_ABSTRACT_ORIGIN (member))
26043 continue;
26044
26045 struct vlr_context vlr_ctx = { type, NULL_TREE };
26046 bool static_inline_p
26047 = (VAR_P (member)
26048 && TREE_STATIC (member)
26049 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
26050 != -1));
26051
26052 /* If we thought we were generating minimal debug info for TYPE
26053 and then changed our minds, some of the member declarations
26054 may have already been defined. Don't define them again, but
26055 do put them in the right order. */
26056
26057 if (dw_die_ref child = lookup_decl_die (member))
26058 {
26059 /* Handle inline static data members, which only have in-class
26060 declarations. */
26061 bool splice = true;
26062
26063 dw_die_ref ref = NULL;
26064 if (child->die_tag == DW_TAG_variable
26065 && child->die_parent == comp_unit_die ())
26066 {
26067 ref = get_AT_ref (child, DW_AT_specification);
26068
26069 /* For C++17 inline static data members followed by redundant
26070 out of class redeclaration, we might get here with
26071 child being the DIE created for the out of class
26072 redeclaration and with its DW_AT_specification being
26073 the DIE created for in-class definition. We want to
26074 reparent the latter, and don't want to create another
26075 DIE with DW_AT_specification in that case, because
26076 we already have one. */
26077 if (ref
26078 && static_inline_p
26079 && ref->die_tag == DW_TAG_variable
26080 && ref->die_parent == comp_unit_die ()
26081 && get_AT (ref, DW_AT_specification) == NULL)
26082 {
26083 child = ref;
26084 ref = NULL;
26085 static_inline_p = false;
26086 }
26087
26088 if (!ref)
26089 {
26090 reparent_child (child, context_die);
26091 if (dwarf_version < 5)
26092 child->die_tag = DW_TAG_member;
26093 splice = false;
26094 }
26095 }
26096 else if (child->die_tag == DW_TAG_enumerator)
26097 /* Enumerators remain under their enumeration even if
26098 their names are introduced in the enclosing scope. */
26099 splice = false;
26100
26101 if (splice)
26102 splice_child_die (context_die, child);
26103 }
26104
26105 /* Do not generate DWARF for variant parts if we are generating the
26106 corresponding GNAT encodings: DIEs generated for the two schemes
26107 would conflict in our mappings. */
26108 else if (is_variant_part (member)
26109 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
26110 {
26111 vlr_ctx.variant_part_offset = byte_position (member);
26112 gen_variant_part (member, &vlr_ctx, context_die);
26113 }
26114 else
26115 {
26116 vlr_ctx.variant_part_offset = NULL_TREE;
26117 gen_decl_die (member, NULL, &vlr_ctx, context_die);
26118 }
26119
26120 /* For C++ inline static data members emit immediately a DW_TAG_variable
26121 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
26122 DW_AT_specification. */
26123 if (static_inline_p)
26124 {
26125 int old_extern = DECL_EXTERNAL (member);
26126 DECL_EXTERNAL (member) = 0;
26127 gen_decl_die (member, NULL, NULL, comp_unit_die ());
26128 DECL_EXTERNAL (member) = old_extern;
26129 }
26130 }
26131 }
26132
26133 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
26134 is set, we pretend that the type was never defined, so we only get the
26135 member DIEs needed by later specification DIEs. */
26136
26137 static void
26138 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
26139 enum debug_info_usage usage)
26140 {
26141 if (TREE_ASM_WRITTEN (type))
26142 {
26143 /* Fill in the bound of variable-length fields in late dwarf if
26144 still incomplete. */
26145 if (!early_dwarf && variably_modified_type_p (type, NULL))
26146 for (tree member = TYPE_FIELDS (type);
26147 member;
26148 member = DECL_CHAIN (member))
26149 fill_variable_array_bounds (TREE_TYPE (member));
26150 return;
26151 }
26152
26153 dw_die_ref type_die = lookup_type_die (type);
26154 dw_die_ref scope_die = 0;
26155 bool nested = false;
26156 bool complete = (TYPE_SIZE (type)
26157 && (! TYPE_STUB_DECL (type)
26158 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
26159 bool ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
26160 complete = complete && should_emit_struct_debug (type, usage);
26161
26162 if (type_die && ! complete)
26163 return;
26164
26165 if (TYPE_CONTEXT (type) != NULL_TREE
26166 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26167 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
26168 nested = true;
26169
26170 scope_die = scope_die_for (type, context_die);
26171
26172 /* Generate child dies for template parameters. */
26173 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
26174 schedule_generic_params_dies_gen (type);
26175
26176 if (! type_die || (nested && is_cu_die (scope_die)))
26177 /* First occurrence of type or toplevel definition of nested class. */
26178 {
26179 dw_die_ref old_die = type_die;
26180
26181 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
26182 ? record_type_tag (type) : DW_TAG_union_type,
26183 scope_die, type);
26184 equate_type_number_to_die (type, type_die);
26185 if (old_die)
26186 add_AT_specification (type_die, old_die);
26187 else
26188 add_name_attribute (type_die, type_tag (type));
26189 }
26190 else
26191 remove_AT (type_die, DW_AT_declaration);
26192
26193 /* If this type has been completed, then give it a byte_size attribute and
26194 then give a list of members. */
26195 if (complete && !ns_decl)
26196 {
26197 /* Prevent infinite recursion in cases where the type of some member of
26198 this type is expressed in terms of this type itself. */
26199 TREE_ASM_WRITTEN (type) = 1;
26200 add_byte_size_attribute (type_die, type);
26201 add_alignment_attribute (type_die, type);
26202 if (TYPE_STUB_DECL (type) != NULL_TREE)
26203 {
26204 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
26205 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
26206 }
26207
26208 /* If the first reference to this type was as the return type of an
26209 inline function, then it may not have a parent. Fix this now. */
26210 if (type_die->die_parent == NULL)
26211 add_child_die (scope_die, type_die);
26212
26213 gen_member_die (type, type_die);
26214
26215 add_gnat_descriptive_type_attribute (type_die, type, context_die);
26216 if (TYPE_ARTIFICIAL (type))
26217 add_AT_flag (type_die, DW_AT_artificial, 1);
26218
26219 /* GNU extension: Record what type our vtable lives in. */
26220 if (TYPE_VFIELD (type))
26221 {
26222 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
26223
26224 gen_type_die (vtype, context_die);
26225 add_AT_die_ref (type_die, DW_AT_containing_type,
26226 lookup_type_die (vtype));
26227 }
26228 }
26229 else
26230 {
26231 add_AT_flag (type_die, DW_AT_declaration, 1);
26232
26233 /* We don't need to do this for function-local types. */
26234 if (TYPE_STUB_DECL (type)
26235 && ! decl_function_context (TYPE_STUB_DECL (type)))
26236 vec_safe_push (incomplete_types, type);
26237 }
26238
26239 if (get_AT (type_die, DW_AT_name))
26240 add_pubtype (type, type_die);
26241 }
26242
26243 /* Generate a DIE for a subroutine _type_. */
26244
26245 static void
26246 gen_subroutine_type_die (tree type, dw_die_ref context_die)
26247 {
26248 tree return_type = TREE_TYPE (type);
26249 dw_die_ref subr_die
26250 = new_die (DW_TAG_subroutine_type,
26251 scope_die_for (type, context_die), type);
26252
26253 equate_type_number_to_die (type, subr_die);
26254 add_prototyped_attribute (subr_die, type);
26255 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
26256 context_die);
26257 add_alignment_attribute (subr_die, type);
26258 gen_formal_types_die (type, subr_die);
26259
26260 if (get_AT (subr_die, DW_AT_name))
26261 add_pubtype (type, subr_die);
26262 if ((dwarf_version >= 5 || !dwarf_strict)
26263 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
26264 add_AT_flag (subr_die, DW_AT_reference, 1);
26265 if ((dwarf_version >= 5 || !dwarf_strict)
26266 && lang_hooks.types.type_dwarf_attribute (type,
26267 DW_AT_rvalue_reference) != -1)
26268 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
26269 }
26270
26271 /* Generate a DIE for a type definition. */
26272
26273 static void
26274 gen_typedef_die (tree decl, dw_die_ref context_die)
26275 {
26276 dw_die_ref type_die;
26277 tree type;
26278
26279 if (TREE_ASM_WRITTEN (decl))
26280 {
26281 if (DECL_ORIGINAL_TYPE (decl))
26282 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26283 return;
26284 }
26285
26286 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26287 checks in process_scope_var and modified_type_die), this should be called
26288 only for original types. */
26289 gcc_assert (decl_ultimate_origin (decl) == NULL
26290 || decl_ultimate_origin (decl) == decl);
26291
26292 TREE_ASM_WRITTEN (decl) = 1;
26293 type_die = new_die (DW_TAG_typedef, context_die, decl);
26294
26295 add_name_and_src_coords_attributes (type_die, decl);
26296 if (DECL_ORIGINAL_TYPE (decl))
26297 {
26298 type = DECL_ORIGINAL_TYPE (decl);
26299 if (type == error_mark_node)
26300 return;
26301
26302 gcc_assert (type != TREE_TYPE (decl));
26303 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26304 }
26305 else
26306 {
26307 type = TREE_TYPE (decl);
26308 if (type == error_mark_node)
26309 return;
26310
26311 if (is_naming_typedef_decl (TYPE_NAME (type)))
26312 {
26313 /* Here, we are in the case of decl being a typedef naming
26314 an anonymous type, e.g:
26315 typedef struct {...} foo;
26316 In that case TREE_TYPE (decl) is not a typedef variant
26317 type and TYPE_NAME of the anonymous type is set to the
26318 TYPE_DECL of the typedef. This construct is emitted by
26319 the C++ FE.
26320
26321 TYPE is the anonymous struct named by the typedef
26322 DECL. As we need the DW_AT_type attribute of the
26323 DW_TAG_typedef to point to the DIE of TYPE, let's
26324 generate that DIE right away. add_type_attribute
26325 called below will then pick (via lookup_type_die) that
26326 anonymous struct DIE. */
26327 if (!TREE_ASM_WRITTEN (type))
26328 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26329
26330 /* This is a GNU Extension. We are adding a
26331 DW_AT_linkage_name attribute to the DIE of the
26332 anonymous struct TYPE. The value of that attribute
26333 is the name of the typedef decl naming the anonymous
26334 struct. This greatly eases the work of consumers of
26335 this debug info. */
26336 add_linkage_name_raw (lookup_type_die (type), decl);
26337 }
26338 }
26339
26340 add_type_attribute (type_die, type, decl_quals (decl), false,
26341 context_die);
26342
26343 if (is_naming_typedef_decl (decl))
26344 /* We want that all subsequent calls to lookup_type_die with
26345 TYPE in argument yield the DW_TAG_typedef we have just
26346 created. */
26347 equate_type_number_to_die (type, type_die);
26348
26349 add_alignment_attribute (type_die, TREE_TYPE (decl));
26350
26351 add_accessibility_attribute (type_die, decl);
26352
26353 if (DECL_ABSTRACT_P (decl))
26354 equate_decl_number_to_die (decl, type_die);
26355
26356 if (get_AT (type_die, DW_AT_name))
26357 add_pubtype (decl, type_die);
26358 }
26359
26360 /* Generate a DIE for a struct, class, enum or union type. */
26361
26362 static void
26363 gen_tagged_type_die (tree type,
26364 dw_die_ref context_die,
26365 enum debug_info_usage usage,
26366 bool reverse)
26367 {
26368 if (type == NULL_TREE
26369 || !is_tagged_type (type))
26370 return;
26371
26372 if (TREE_ASM_WRITTEN (type))
26373 ;
26374 /* If this is a nested type whose containing class hasn't been written
26375 out yet, writing it out will cover this one, too. This does not apply
26376 to instantiations of member class templates; they need to be added to
26377 the containing class as they are generated. FIXME: This hurts the
26378 idea of combining type decls from multiple TUs, since we can't predict
26379 what set of template instantiations we'll get. */
26380 else if (TYPE_CONTEXT (type)
26381 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26382 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26383 {
26384 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26385
26386 if (TREE_ASM_WRITTEN (type))
26387 return;
26388
26389 /* If that failed, attach ourselves to the stub. */
26390 context_die = lookup_type_die (TYPE_CONTEXT (type));
26391 }
26392 else if (TYPE_CONTEXT (type) != NULL_TREE
26393 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26394 {
26395 /* If this type is local to a function that hasn't been written
26396 out yet, use a NULL context for now; it will be fixed up in
26397 decls_for_scope. */
26398 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26399 /* A declaration DIE doesn't count; nested types need to go in the
26400 specification. */
26401 if (context_die && is_declaration_die (context_die))
26402 context_die = NULL;
26403 }
26404 else
26405 context_die = declare_in_namespace (type, context_die);
26406
26407 if (TREE_CODE (type) == ENUMERAL_TYPE)
26408 {
26409 /* This might have been written out by the call to
26410 declare_in_namespace. */
26411 if (!TREE_ASM_WRITTEN (type) || reverse)
26412 gen_enumeration_type_die (type, context_die, reverse);
26413 }
26414 else
26415 gen_struct_or_union_type_die (type, context_die, usage);
26416
26417 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26418 it up if it is ever completed. gen_*_type_die will set it for us
26419 when appropriate. */
26420 }
26421
26422 /* Generate a type description DIE. */
26423
26424 static void
26425 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26426 enum debug_info_usage usage, bool reverse)
26427 {
26428 struct array_descr_info info;
26429
26430 if (type == NULL_TREE || type == error_mark_node)
26431 return;
26432
26433 if (flag_checking && type)
26434 verify_type (type);
26435
26436 if (TYPE_NAME (type) != NULL_TREE
26437 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26438 && is_redundant_typedef (TYPE_NAME (type))
26439 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26440 /* The DECL of this type is a typedef we don't want to emit debug
26441 info for but we want debug info for its underlying typedef.
26442 This can happen for e.g, the injected-class-name of a C++
26443 type. */
26444 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26445
26446 /* If TYPE is a typedef type variant, let's generate debug info
26447 for the parent typedef which TYPE is a type of. */
26448 if (typedef_variant_p (type))
26449 {
26450 tree name = TYPE_NAME (type);
26451 if (TREE_ASM_WRITTEN (name))
26452 return;
26453
26454 tree origin = decl_ultimate_origin (name);
26455 if (origin != NULL && origin != name)
26456 {
26457 gen_decl_die (origin, NULL, NULL, context_die);
26458 return;
26459 }
26460
26461 /* Prevent broken recursion; we can't hand off to the same type. */
26462 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26463
26464 /* Give typedefs the right scope. */
26465 context_die = scope_die_for (type, context_die);
26466
26467 gen_decl_die (name, NULL, NULL, context_die);
26468 return;
26469 }
26470
26471 /* If type is an anonymous tagged type named by a typedef, let's
26472 generate debug info for the typedef. */
26473 if (is_naming_typedef_decl (TYPE_NAME (type)))
26474 {
26475 /* Give typedefs the right scope. */
26476 context_die = scope_die_for (type, context_die);
26477
26478 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26479 return;
26480 }
26481
26482 if (lang_hooks.types.get_debug_type)
26483 {
26484 tree debug_type = lang_hooks.types.get_debug_type (type);
26485
26486 if (debug_type != NULL_TREE && debug_type != type)
26487 {
26488 gen_type_die_with_usage (debug_type, context_die, usage, reverse);
26489 return;
26490 }
26491 }
26492
26493 /* We are going to output a DIE to represent the unqualified version
26494 of this type (i.e. without any const or volatile qualifiers) so
26495 get the main variant (i.e. the unqualified version) of this type
26496 now. (Vectors and arrays are special because the debugging info is in the
26497 cloned type itself. Similarly function/method types can contain extra
26498 ref-qualification). */
26499 if (FUNC_OR_METHOD_TYPE_P (type))
26500 {
26501 /* For function/method types, can't use type_main_variant here,
26502 because that can have different ref-qualifiers for C++,
26503 but try to canonicalize. */
26504 tree main = TYPE_MAIN_VARIANT (type);
26505 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26506 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26507 && check_base_type (t, main)
26508 && check_lang_type (t, type))
26509 {
26510 type = t;
26511 break;
26512 }
26513 }
26514 else if (TREE_CODE (type) != VECTOR_TYPE
26515 && TREE_CODE (type) != ARRAY_TYPE)
26516 type = type_main_variant (type);
26517
26518 /* If this is an array type with hidden descriptor, handle it first. */
26519 if (!TREE_ASM_WRITTEN (type)
26520 && lang_hooks.types.get_array_descr_info)
26521 {
26522 memset (&info, 0, sizeof (info));
26523 if (lang_hooks.types.get_array_descr_info (type, &info))
26524 {
26525 /* Fortran sometimes emits array types with no dimension. */
26526 gcc_assert (info.ndimensions >= 0
26527 && (info.ndimensions
26528 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26529 gen_descr_array_type_die (type, &info, context_die);
26530 TREE_ASM_WRITTEN (type) = 1;
26531 return;
26532 }
26533 }
26534
26535 if (TREE_ASM_WRITTEN (type) && !reverse)
26536 {
26537 /* Variable-length types may be incomplete even if
26538 TREE_ASM_WRITTEN. For such types, fall through to
26539 gen_array_type_die() and possibly fill in
26540 DW_AT_{upper,lower}_bound attributes. */
26541 if ((TREE_CODE (type) != ARRAY_TYPE
26542 && TREE_CODE (type) != RECORD_TYPE
26543 && TREE_CODE (type) != UNION_TYPE
26544 && TREE_CODE (type) != QUAL_UNION_TYPE)
26545 || !variably_modified_type_p (type, NULL))
26546 return;
26547 }
26548
26549 switch (TREE_CODE (type))
26550 {
26551 case ERROR_MARK:
26552 break;
26553
26554 case POINTER_TYPE:
26555 case REFERENCE_TYPE:
26556 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26557 ensures that the gen_type_die recursion will terminate even if the
26558 type is recursive. Recursive types are possible in Ada. */
26559 /* ??? We could perhaps do this for all types before the switch
26560 statement. */
26561 TREE_ASM_WRITTEN (type) = 1;
26562
26563 /* For these types, all that is required is that we output a DIE (or a
26564 set of DIEs) to represent the "basis" type. */
26565 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26566 DINFO_USAGE_IND_USE);
26567 break;
26568
26569 case OFFSET_TYPE:
26570 /* This code is used for C++ pointer-to-data-member types.
26571 Output a description of the relevant class type. */
26572 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26573 DINFO_USAGE_IND_USE);
26574
26575 /* Output a description of the type of the object pointed to. */
26576 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26577 DINFO_USAGE_IND_USE);
26578
26579 /* Now output a DIE to represent this pointer-to-data-member type
26580 itself. */
26581 gen_ptr_to_mbr_type_die (type, context_die);
26582 break;
26583
26584 case FUNCTION_TYPE:
26585 /* Force out return type (in case it wasn't forced out already). */
26586 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26587 DINFO_USAGE_DIR_USE);
26588 gen_subroutine_type_die (type, context_die);
26589 break;
26590
26591 case METHOD_TYPE:
26592 /* Force out return type (in case it wasn't forced out already). */
26593 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26594 DINFO_USAGE_DIR_USE);
26595 gen_subroutine_type_die (type, context_die);
26596 break;
26597
26598 case ARRAY_TYPE:
26599 case VECTOR_TYPE:
26600 gen_array_type_die (type, context_die);
26601 break;
26602
26603 case ENUMERAL_TYPE:
26604 case RECORD_TYPE:
26605 case UNION_TYPE:
26606 case QUAL_UNION_TYPE:
26607 gen_tagged_type_die (type, context_die, usage, reverse);
26608 return;
26609
26610 case VOID_TYPE:
26611 case OPAQUE_TYPE:
26612 case INTEGER_TYPE:
26613 case REAL_TYPE:
26614 case FIXED_POINT_TYPE:
26615 case COMPLEX_TYPE:
26616 case BOOLEAN_TYPE:
26617 case BITINT_TYPE:
26618 /* No DIEs needed for fundamental types. */
26619 break;
26620
26621 case NULLPTR_TYPE:
26622 case LANG_TYPE:
26623 /* Just use DW_TAG_unspecified_type. */
26624 {
26625 dw_die_ref type_die = lookup_type_die (type);
26626 if (type_die == NULL)
26627 {
26628 tree name = TYPE_IDENTIFIER (type);
26629 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26630 type);
26631 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26632 equate_type_number_to_die (type, type_die);
26633 }
26634 }
26635 break;
26636
26637 default:
26638 if (is_cxx_auto (type))
26639 {
26640 tree name = TYPE_IDENTIFIER (type);
26641 dw_die_ref *die = (name == get_identifier ("auto")
26642 ? &auto_die : &decltype_auto_die);
26643 if (!*die)
26644 {
26645 *die = new_die (DW_TAG_unspecified_type,
26646 comp_unit_die (), NULL_TREE);
26647 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26648 }
26649 equate_type_number_to_die (type, *die);
26650 break;
26651 }
26652 gcc_unreachable ();
26653 }
26654
26655 TREE_ASM_WRITTEN (type) = 1;
26656 }
26657
26658 static void
26659 gen_type_die (tree type, dw_die_ref context_die, bool reverse)
26660 {
26661 if (type != error_mark_node)
26662 {
26663 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE, reverse);
26664 if (flag_checking)
26665 {
26666 dw_die_ref die = lookup_type_die (type);
26667 if (die)
26668 check_die (die);
26669 }
26670 }
26671 }
26672
26673 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26674 things which are local to the given block. */
26675
26676 static void
26677 gen_block_die (tree stmt, dw_die_ref context_die)
26678 {
26679 int must_output_die = 0;
26680 bool inlined_func;
26681
26682 /* Ignore blocks that are NULL. */
26683 if (stmt == NULL_TREE)
26684 return;
26685
26686 inlined_func = inlined_function_outer_scope_p (stmt);
26687
26688 /* If the block is one fragment of a non-contiguous block, do not
26689 process the variables, since they will have been done by the
26690 origin block. Do process subblocks. */
26691 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26692 {
26693 tree sub;
26694
26695 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26696 gen_block_die (sub, context_die);
26697
26698 return;
26699 }
26700
26701 /* Determine if we need to output any Dwarf DIEs at all to represent this
26702 block. */
26703 if (inlined_func)
26704 /* The outer scopes for inlinings *must* always be represented. We
26705 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26706 must_output_die = 1;
26707 else if (lookup_block_die (stmt))
26708 /* If we already have a DIE then it was filled early. Meanwhile
26709 we might have pruned all BLOCK_VARS as optimized out but we
26710 still want to generate high/low PC attributes so output it. */
26711 must_output_die = 1;
26712 else if (TREE_USED (stmt)
26713 || TREE_ASM_WRITTEN (stmt))
26714 {
26715 /* Determine if this block directly contains any "significant"
26716 local declarations which we will need to output DIEs for. */
26717 if (debug_info_level > DINFO_LEVEL_TERSE)
26718 {
26719 /* We are not in terse mode so any local declaration that
26720 is not ignored for debug purposes counts as being a
26721 "significant" one. */
26722 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26723 must_output_die = 1;
26724 else
26725 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26726 if (!DECL_IGNORED_P (var))
26727 {
26728 must_output_die = 1;
26729 break;
26730 }
26731 }
26732 else if (!dwarf2out_ignore_block (stmt))
26733 must_output_die = 1;
26734 }
26735
26736 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26737 DIE for any block which contains no significant local declarations at
26738 all. Rather, in such cases we just call `decls_for_scope' so that any
26739 needed Dwarf info for any sub-blocks will get properly generated. Note
26740 that in terse mode, our definition of what constitutes a "significant"
26741 local declaration gets restricted to include only inlined function
26742 instances and local (nested) function definitions. */
26743 if (must_output_die)
26744 {
26745 if (inlined_func)
26746 gen_inlined_subroutine_die (stmt, context_die);
26747 else
26748 gen_lexical_block_die (stmt, context_die);
26749 }
26750 else
26751 decls_for_scope (stmt, context_die);
26752 }
26753
26754 /* Process variable DECL (or variable with origin ORIGIN) within
26755 block STMT and add it to CONTEXT_DIE. */
26756 static void
26757 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26758 {
26759 dw_die_ref die;
26760 tree decl_or_origin = decl ? decl : origin;
26761
26762 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26763 die = lookup_decl_die (decl_or_origin);
26764 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26765 {
26766 if (TYPE_DECL_IS_STUB (decl_or_origin))
26767 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26768 else
26769 die = lookup_decl_die (decl_or_origin);
26770 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26771 if (! die && ! early_dwarf)
26772 return;
26773 }
26774 else
26775 die = NULL;
26776
26777 /* Avoid creating DIEs for local typedefs and concrete static variables that
26778 will only be pruned later. */
26779 if ((origin || decl_ultimate_origin (decl))
26780 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26781 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26782 {
26783 origin = decl_ultimate_origin (decl_or_origin);
26784 if (decl && VAR_P (decl) && die != NULL)
26785 {
26786 die = lookup_decl_die (origin);
26787 if (die != NULL)
26788 equate_decl_number_to_die (decl, die);
26789 }
26790 return;
26791 }
26792
26793 if (die != NULL && die->die_parent == NULL)
26794 add_child_die (context_die, die);
26795
26796 if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26797 {
26798 if (early_dwarf)
26799 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26800 stmt, context_die);
26801 }
26802 else
26803 {
26804 if (decl && DECL_P (decl))
26805 {
26806 die = lookup_decl_die (decl);
26807
26808 /* Early created DIEs do not have a parent as the decls refer
26809 to the function as DECL_CONTEXT rather than the BLOCK. */
26810 if (die && die->die_parent == NULL)
26811 {
26812 gcc_assert (in_lto_p);
26813 add_child_die (context_die, die);
26814 }
26815 }
26816
26817 gen_decl_die (decl, origin, NULL, context_die);
26818 }
26819 }
26820
26821 /* Generate all of the decls declared within a given scope and (recursively)
26822 all of its sub-blocks. */
26823
26824 static void
26825 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26826 {
26827 tree decl;
26828 unsigned int i;
26829 tree subblocks;
26830
26831 /* Ignore NULL blocks. */
26832 if (stmt == NULL_TREE)
26833 return;
26834
26835 /* Output the DIEs to represent all of the data objects and typedefs
26836 declared directly within this block but not within any nested
26837 sub-blocks. Also, nested function and tag DIEs have been
26838 generated with a parent of NULL; fix that up now. We don't
26839 have to do this if we're at -g1. */
26840 if (debug_info_level > DINFO_LEVEL_TERSE)
26841 {
26842 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26843 process_scope_var (stmt, decl, NULL_TREE, context_die);
26844 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26845 origin - avoid doing this twice as we have no good way to see
26846 if we've done it once already. */
26847 if (! early_dwarf)
26848 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26849 {
26850 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26851 if (decl == current_function_decl)
26852 /* Ignore declarations of the current function, while they
26853 are declarations, gen_subprogram_die would treat them
26854 as definitions again, because they are equal to
26855 current_function_decl and endlessly recurse. */;
26856 else if (TREE_CODE (decl) == FUNCTION_DECL)
26857 process_scope_var (stmt, decl, NULL_TREE, context_die);
26858 else
26859 process_scope_var (stmt, NULL_TREE, decl, context_die);
26860 }
26861 }
26862
26863 /* Even if we're at -g1, we need to process the subblocks in order to get
26864 inlined call information. */
26865
26866 /* Output the DIEs to represent all sub-blocks (and the items declared
26867 therein) of this block. */
26868 if (recurse)
26869 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26870 subblocks != NULL;
26871 subblocks = BLOCK_CHAIN (subblocks))
26872 gen_block_die (subblocks, context_die);
26873 }
26874
26875 /* Is this a typedef we can avoid emitting? */
26876
26877 static bool
26878 is_redundant_typedef (const_tree decl)
26879 {
26880 if (TYPE_DECL_IS_STUB (decl))
26881 return true;
26882
26883 if (DECL_ARTIFICIAL (decl)
26884 && DECL_CONTEXT (decl)
26885 && is_tagged_type (DECL_CONTEXT (decl))
26886 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26887 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26888 /* Also ignore the artificial member typedef for the class name. */
26889 return true;
26890
26891 return false;
26892 }
26893
26894 /* Return TRUE if TYPE is a typedef that names a type for linkage
26895 purposes. This kind of typedefs is produced by the C++ FE for
26896 constructs like:
26897
26898 typedef struct {...} foo;
26899
26900 In that case, there is no typedef variant type produced for foo.
26901 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26902 struct type. */
26903
26904 static bool
26905 is_naming_typedef_decl (const_tree decl)
26906 {
26907 if (decl == NULL_TREE
26908 || TREE_CODE (decl) != TYPE_DECL
26909 || DECL_NAMELESS (decl)
26910 || !is_tagged_type (TREE_TYPE (decl))
26911 || DECL_IS_UNDECLARED_BUILTIN (decl)
26912 || is_redundant_typedef (decl)
26913 /* It looks like Ada produces TYPE_DECLs that are very similar
26914 to C++ naming typedefs but that have different
26915 semantics. Let's be specific to c++ for now. */
26916 || !is_cxx (decl))
26917 return false;
26918
26919 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26920 && TYPE_NAME (TREE_TYPE (decl)) == decl
26921 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26922 != TYPE_NAME (TREE_TYPE (decl))));
26923 }
26924
26925 /* Looks up the DIE for a context. */
26926
26927 static inline dw_die_ref
26928 lookup_context_die (tree context)
26929 {
26930 if (context)
26931 {
26932 /* Find die that represents this context. */
26933 if (TYPE_P (context))
26934 {
26935 context = TYPE_MAIN_VARIANT (context);
26936 dw_die_ref ctx = lookup_type_die (context);
26937 if (!ctx)
26938 return NULL;
26939 return strip_naming_typedef (context, ctx);
26940 }
26941 else
26942 return lookup_decl_die (context);
26943 }
26944 return comp_unit_die ();
26945 }
26946
26947 /* Returns the DIE for a context. */
26948
26949 static inline dw_die_ref
26950 get_context_die (tree context)
26951 {
26952 if (context)
26953 {
26954 /* Find die that represents this context. */
26955 if (TYPE_P (context))
26956 {
26957 context = TYPE_MAIN_VARIANT (context);
26958 return strip_naming_typedef (context, force_type_die (context));
26959 }
26960 else
26961 return force_decl_die (context);
26962 }
26963 return comp_unit_die ();
26964 }
26965
26966 /* Returns the DIE for decl. A DIE will always be returned. */
26967
26968 static dw_die_ref
26969 force_decl_die (tree decl)
26970 {
26971 dw_die_ref decl_die;
26972 unsigned saved_external_flag;
26973 tree save_fn = NULL_TREE;
26974 decl_die = lookup_decl_die (decl);
26975 if (!decl_die)
26976 {
26977 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26978
26979 decl_die = lookup_decl_die (decl);
26980 if (decl_die)
26981 return decl_die;
26982
26983 switch (TREE_CODE (decl))
26984 {
26985 case FUNCTION_DECL:
26986 /* Clear current_function_decl, so that gen_subprogram_die thinks
26987 that this is a declaration. At this point, we just want to force
26988 declaration die. */
26989 save_fn = current_function_decl;
26990 current_function_decl = NULL_TREE;
26991 gen_subprogram_die (decl, context_die);
26992 current_function_decl = save_fn;
26993 break;
26994
26995 case VAR_DECL:
26996 /* Set external flag to force declaration die. Restore it after
26997 gen_decl_die() call. */
26998 saved_external_flag = DECL_EXTERNAL (decl);
26999 DECL_EXTERNAL (decl) = 1;
27000 gen_decl_die (decl, NULL, NULL, context_die);
27001 DECL_EXTERNAL (decl) = saved_external_flag;
27002 break;
27003
27004 case NAMESPACE_DECL:
27005 if (dwarf_version >= 3 || !dwarf_strict)
27006 dwarf2out_decl (decl);
27007 else
27008 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
27009 decl_die = comp_unit_die ();
27010 break;
27011
27012 case CONST_DECL:
27013 /* Enumerators shouldn't need force_decl_die. */
27014 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
27015 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
27016 gen_decl_die (decl, NULL, NULL, context_die);
27017 break;
27018
27019 case TRANSLATION_UNIT_DECL:
27020 decl_die = comp_unit_die ();
27021 break;
27022
27023 default:
27024 gcc_unreachable ();
27025 }
27026
27027 /* We should be able to find the DIE now. */
27028 if (!decl_die)
27029 decl_die = lookup_decl_die (decl);
27030 gcc_assert (decl_die);
27031 }
27032
27033 return decl_die;
27034 }
27035
27036 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
27037 always returned. */
27038
27039 static dw_die_ref
27040 force_type_die (tree type)
27041 {
27042 dw_die_ref type_die;
27043
27044 type_die = lookup_type_die (type);
27045 if (!type_die)
27046 {
27047 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
27048
27049 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
27050 false, context_die);
27051 gcc_assert (type_die);
27052 }
27053 return type_die;
27054 }
27055
27056 /* Force out any required namespaces to be able to output DECL,
27057 and return the new context_die for it, if it's changed. */
27058
27059 static dw_die_ref
27060 setup_namespace_context (tree thing, dw_die_ref context_die)
27061 {
27062 tree context = (DECL_P (thing)
27063 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
27064 if (context && TREE_CODE (context) == NAMESPACE_DECL)
27065 /* Force out the namespace. */
27066 context_die = force_decl_die (context);
27067
27068 return context_die;
27069 }
27070
27071 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
27072 type) within its namespace, if appropriate.
27073
27074 For compatibility with older debuggers, namespace DIEs only contain
27075 declarations; all definitions are emitted at CU scope, with
27076 DW_AT_specification pointing to the declaration (like with class
27077 members). */
27078
27079 static dw_die_ref
27080 declare_in_namespace (tree thing, dw_die_ref context_die)
27081 {
27082 dw_die_ref ns_context;
27083
27084 if (debug_info_level <= DINFO_LEVEL_TERSE)
27085 return context_die;
27086
27087 /* External declarations in the local scope only need to be emitted
27088 once, not once in the namespace and once in the scope.
27089
27090 This avoids declaring the `extern' below in the
27091 namespace DIE as well as in the innermost scope:
27092
27093 namespace S
27094 {
27095 int i=5;
27096 int foo()
27097 {
27098 int i=8;
27099 extern int i;
27100 return i;
27101 }
27102 }
27103 */
27104 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
27105 return context_die;
27106
27107 /* If this decl is from an inlined function, then don't try to emit it in its
27108 namespace, as we will get confused. It would have already been emitted
27109 when the abstract instance of the inline function was emitted anyways. */
27110 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
27111 return context_die;
27112
27113 ns_context = setup_namespace_context (thing, context_die);
27114
27115 if (ns_context != context_die)
27116 {
27117 if (is_fortran () || is_dlang ())
27118 return ns_context;
27119 if (DECL_P (thing))
27120 gen_decl_die (thing, NULL, NULL, ns_context);
27121 else
27122 gen_type_die (thing, ns_context);
27123 }
27124 return context_die;
27125 }
27126
27127 /* Generate a DIE for a namespace or namespace alias. */
27128
27129 static void
27130 gen_namespace_die (tree decl, dw_die_ref context_die)
27131 {
27132 dw_die_ref namespace_die;
27133
27134 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
27135 they are an alias of. */
27136 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
27137 {
27138 /* Output a real namespace or module. */
27139 context_die = setup_namespace_context (decl, comp_unit_die ());
27140 namespace_die = new_die (is_fortran () || is_dlang () || is_ada ()
27141 ? DW_TAG_module : DW_TAG_namespace,
27142 context_die, decl);
27143 /* For Fortran modules defined in different CU don't add src coords. */
27144 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
27145 {
27146 const char *name = dwarf2_name (decl, 0);
27147 if (name)
27148 add_name_attribute (namespace_die, name);
27149 }
27150 else
27151 add_name_and_src_coords_attributes (namespace_die, decl);
27152 if (DECL_EXTERNAL (decl))
27153 add_AT_flag (namespace_die, DW_AT_declaration, 1);
27154 equate_decl_number_to_die (decl, namespace_die);
27155 }
27156 else
27157 {
27158 /* Output a namespace alias. */
27159
27160 /* Force out the namespace we are an alias of, if necessary. */
27161 dw_die_ref origin_die
27162 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
27163
27164 if (DECL_FILE_SCOPE_P (decl)
27165 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
27166 context_die = setup_namespace_context (decl, comp_unit_die ());
27167 /* Now create the namespace alias DIE. */
27168 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
27169 add_name_and_src_coords_attributes (namespace_die, decl);
27170 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
27171 equate_decl_number_to_die (decl, namespace_die);
27172 }
27173 if ((dwarf_version >= 5 || !dwarf_strict)
27174 && lang_hooks.decls.decl_dwarf_attribute (decl,
27175 DW_AT_export_symbols) == 1)
27176 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
27177
27178 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
27179 if (want_pubnames ())
27180 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
27181 }
27182
27183 /* Generate Dwarf debug information for a decl described by DECL.
27184 The return value is currently only meaningful for PARM_DECLs,
27185 for all other decls it returns NULL.
27186
27187 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
27188 It can be NULL otherwise. */
27189
27190 static dw_die_ref
27191 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
27192 dw_die_ref context_die)
27193 {
27194 tree decl_or_origin = decl ? decl : origin;
27195 tree class_origin = NULL, ultimate_origin;
27196
27197 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
27198 return NULL;
27199
27200 switch (TREE_CODE (decl_or_origin))
27201 {
27202 case ERROR_MARK:
27203 break;
27204
27205 case CONST_DECL:
27206 if (!is_fortran () && !is_ada () && !is_dlang ())
27207 {
27208 /* The individual enumerators of an enum type get output when we output
27209 the Dwarf representation of the relevant enum type itself. */
27210 break;
27211 }
27212
27213 /* Emit its type. */
27214 gen_type_die (TREE_TYPE (decl), context_die);
27215
27216 /* And its containing namespace. */
27217 context_die = declare_in_namespace (decl, context_die);
27218
27219 gen_const_die (decl, context_die);
27220 break;
27221
27222 case FUNCTION_DECL:
27223 #if 0
27224 /* FIXME */
27225 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
27226 on local redeclarations of global functions. That seems broken. */
27227 if (current_function_decl != decl)
27228 /* This is only a declaration. */;
27229 #endif
27230
27231 /* We should have abstract copies already and should not generate
27232 stray type DIEs in late LTO dumping. */
27233 if (! early_dwarf)
27234 ;
27235
27236 /* If we're emitting a clone, emit info for the abstract instance. */
27237 else if (origin || DECL_ORIGIN (decl) != decl)
27238 dwarf2out_abstract_function (origin
27239 ? DECL_ORIGIN (origin)
27240 : DECL_ABSTRACT_ORIGIN (decl));
27241
27242 /* If we're emitting a possibly inlined function emit it as
27243 abstract instance. */
27244 else if (cgraph_function_possibly_inlined_p (decl)
27245 && ! DECL_ABSTRACT_P (decl)
27246 && ! class_or_namespace_scope_p (context_die)
27247 /* dwarf2out_abstract_function won't emit a die if this is just
27248 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
27249 that case, because that works only if we have a die. */
27250 && DECL_INITIAL (decl) != NULL_TREE)
27251 dwarf2out_abstract_function (decl);
27252
27253 /* Otherwise we're emitting the primary DIE for this decl. */
27254 else if (debug_info_level > DINFO_LEVEL_TERSE)
27255 {
27256 /* Before we describe the FUNCTION_DECL itself, make sure that we
27257 have its containing type. */
27258 if (!origin)
27259 origin = decl_class_context (decl);
27260 if (origin != NULL_TREE)
27261 gen_type_die (origin, context_die);
27262
27263 /* And its return type. */
27264 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
27265
27266 /* And its virtual context. */
27267 if (DECL_VINDEX (decl) != NULL_TREE)
27268 gen_type_die (DECL_CONTEXT (decl), context_die);
27269
27270 /* Make sure we have a member DIE for decl. */
27271 if (origin != NULL_TREE)
27272 gen_type_die_for_member (origin, decl, context_die);
27273
27274 /* And its containing namespace. */
27275 context_die = declare_in_namespace (decl, context_die);
27276 }
27277
27278 /* Now output a DIE to represent the function itself. */
27279 if (decl)
27280 gen_subprogram_die (decl, context_die);
27281 break;
27282
27283 case TYPE_DECL:
27284 /* If we are in terse mode, don't generate any DIEs to represent any
27285 actual typedefs. */
27286 if (debug_info_level <= DINFO_LEVEL_TERSE)
27287 break;
27288
27289 /* In the special case of a TYPE_DECL node representing the declaration
27290 of some type tag, if the given TYPE_DECL is marked as having been
27291 instantiated from some other (original) TYPE_DECL node (e.g. one which
27292 was generated within the original definition of an inline function) we
27293 used to generate a special (abbreviated) DW_TAG_structure_type,
27294 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27295 should be actually referencing those DIEs, as variable DIEs with that
27296 type would be emitted already in the abstract origin, so it was always
27297 removed during unused type prunning. Don't add anything in this
27298 case. */
27299 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27300 break;
27301
27302 if (is_redundant_typedef (decl))
27303 gen_type_die (TREE_TYPE (decl), context_die);
27304 else
27305 /* Output a DIE to represent the typedef itself. */
27306 gen_typedef_die (decl, context_die);
27307 break;
27308
27309 case LABEL_DECL:
27310 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27311 gen_label_die (decl, context_die);
27312 break;
27313
27314 case VAR_DECL:
27315 case RESULT_DECL:
27316 /* If we are in terse mode, don't generate any DIEs to represent any
27317 variable declarations or definitions unless it is external. */
27318 if (debug_info_level < DINFO_LEVEL_TERSE
27319 || (debug_info_level == DINFO_LEVEL_TERSE
27320 && !TREE_PUBLIC (decl_or_origin)))
27321 break;
27322
27323 if (debug_info_level > DINFO_LEVEL_TERSE)
27324 {
27325 /* Avoid generating stray type DIEs during late dwarf dumping.
27326 All types have been dumped early. */
27327 if (early_dwarf
27328 /* ??? But in LTRANS we cannot annotate early created variably
27329 modified type DIEs without copying them and adjusting all
27330 references to them. Dump them again as happens for inlining
27331 which copies both the decl and the types. */
27332 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27333 in VLA bound information for example. */
27334 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27335 current_function_decl)))
27336 {
27337 /* Output any DIEs that are needed to specify the type of this data
27338 object. */
27339 if (decl_by_reference_p (decl_or_origin))
27340 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27341 else
27342 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27343 }
27344
27345 if (early_dwarf)
27346 {
27347 /* And its containing type. */
27348 class_origin = decl_class_context (decl_or_origin);
27349 if (class_origin != NULL_TREE)
27350 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
27351
27352 /* And its containing namespace. */
27353 context_die = declare_in_namespace (decl_or_origin, context_die);
27354 }
27355 }
27356
27357 /* Now output the DIE to represent the data object itself. This gets
27358 complicated because of the possibility that the VAR_DECL really
27359 represents an inlined instance of a formal parameter for an inline
27360 function. */
27361 ultimate_origin = decl_ultimate_origin (decl_or_origin);
27362 if (ultimate_origin != NULL_TREE
27363 && TREE_CODE (ultimate_origin) == PARM_DECL)
27364 gen_formal_parameter_die (decl, origin,
27365 true /* Emit name attribute. */,
27366 context_die);
27367 else
27368 gen_variable_die (decl, origin, context_die);
27369 break;
27370
27371 case FIELD_DECL:
27372 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27373 /* Ignore the nameless fields that are used to skip bits but handle C++
27374 anonymous unions and structs. */
27375 if (DECL_NAME (decl) != NULL_TREE
27376 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27377 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27378 {
27379 gen_type_die (member_declared_type (decl), context_die);
27380 gen_field_die (decl, ctx, context_die);
27381 }
27382 break;
27383
27384 case PARM_DECL:
27385 /* Avoid generating stray type DIEs during late dwarf dumping.
27386 All types have been dumped early. */
27387 if (early_dwarf
27388 /* ??? But in LTRANS we cannot annotate early created variably
27389 modified type DIEs without copying them and adjusting all
27390 references to them. Dump them again as happens for inlining
27391 which copies both the decl and the types. */
27392 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27393 in VLA bound information for example. */
27394 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27395 current_function_decl)))
27396 {
27397 if (DECL_BY_REFERENCE (decl_or_origin))
27398 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27399 else
27400 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27401 }
27402 return gen_formal_parameter_die (decl, origin,
27403 true /* Emit name attribute. */,
27404 context_die);
27405
27406 case NAMESPACE_DECL:
27407 if (dwarf_version >= 3 || !dwarf_strict)
27408 gen_namespace_die (decl, context_die);
27409 break;
27410
27411 case IMPORTED_DECL:
27412 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27413 DECL_CONTEXT (decl), context_die);
27414 break;
27415
27416 case NAMELIST_DECL:
27417 gen_namelist_decl (DECL_NAME (decl), context_die,
27418 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27419 break;
27420
27421 default:
27422 /* Probably some frontend-internal decl. Assume we don't care. */
27423 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27424 break;
27425 }
27426
27427 return NULL;
27428 }
27429
27430 /* Output initial debug information for global DECL. Called at the
27431 end of the parsing process.
27432
27433 This is the initial debug generation process. As such, the DIEs
27434 generated may be incomplete. A later debug generation pass
27435 (dwarf2out_late_global_decl) will augment the information generated
27436 in this pass (e.g., with complete location info). */
27437
27438 static void
27439 dwarf2out_early_global_decl (tree decl)
27440 {
27441 set_early_dwarf s;
27442
27443 /* gen_decl_die() will set DECL_ABSTRACT because
27444 cgraph_function_possibly_inlined_p() returns true. This is in
27445 turn will cause DW_AT_inline attributes to be set.
27446
27447 This happens because at early dwarf generation, there is no
27448 cgraph information, causing cgraph_function_possibly_inlined_p()
27449 to return true. Trick cgraph_function_possibly_inlined_p()
27450 while we generate dwarf early. */
27451 bool save = symtab->global_info_ready;
27452 symtab->global_info_ready = true;
27453
27454 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27455 other DECLs and they can point to template types or other things
27456 that dwarf2out can't handle when done via dwarf2out_decl. */
27457 if (TREE_CODE (decl) != TYPE_DECL
27458 && TREE_CODE (decl) != PARM_DECL)
27459 {
27460 if (TREE_CODE (decl) == FUNCTION_DECL)
27461 {
27462 tree save_fndecl = current_function_decl;
27463
27464 /* For nested functions, make sure we have DIEs for the parents first
27465 so that all nested DIEs are generated at the proper scope in the
27466 first shot. */
27467 tree context = decl_function_context (decl);
27468 if (context != NULL)
27469 {
27470 dw_die_ref context_die = lookup_decl_die (context);
27471 current_function_decl = context;
27472
27473 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27474 enough so that it lands in its own context. This avoids type
27475 pruning issues later on. */
27476 if (context_die == NULL || is_declaration_die (context_die))
27477 dwarf2out_early_global_decl (context);
27478 }
27479
27480 /* Emit an abstract origin of a function first. This happens
27481 with C++ constructor clones for example and makes
27482 dwarf2out_abstract_function happy which requires the early
27483 DIE of the abstract instance to be present. */
27484 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27485 dw_die_ref origin_die;
27486 if (origin != NULL
27487 /* Do not emit the DIE multiple times but make sure to
27488 process it fully here in case we just saw a declaration. */
27489 && ((origin_die = lookup_decl_die (origin)) == NULL
27490 || is_declaration_die (origin_die)))
27491 {
27492 current_function_decl = origin;
27493 dwarf2out_decl (origin);
27494 }
27495
27496 /* Emit the DIE for decl but avoid doing that multiple times. */
27497 dw_die_ref old_die;
27498 if ((old_die = lookup_decl_die (decl)) == NULL
27499 || is_declaration_die (old_die))
27500 {
27501 current_function_decl = decl;
27502 dwarf2out_decl (decl);
27503 }
27504
27505 current_function_decl = save_fndecl;
27506 }
27507 else
27508 dwarf2out_decl (decl);
27509 }
27510 symtab->global_info_ready = save;
27511 }
27512
27513 /* Return whether EXPR is an expression with the following pattern:
27514 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27515
27516 static bool
27517 is_trivial_indirect_ref (tree expr)
27518 {
27519 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27520 return false;
27521
27522 tree nop = TREE_OPERAND (expr, 0);
27523 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27524 return false;
27525
27526 tree int_cst = TREE_OPERAND (nop, 0);
27527 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27528 }
27529
27530 /* Output debug information for global decl DECL. Called from
27531 toplev.cc after compilation proper has finished. */
27532
27533 static void
27534 dwarf2out_late_global_decl (tree decl)
27535 {
27536 /* Fill-in any location information we were unable to determine
27537 on the first pass. */
27538 if (VAR_P (decl))
27539 {
27540 dw_die_ref die = lookup_decl_die (decl);
27541
27542 /* We may have to generate full debug late for LTO in case debug
27543 was not enabled at compile-time or the target doesn't support
27544 the LTO early debug scheme. */
27545 if (! die && in_lto_p
27546 /* Function scope variables are emitted when emitting the
27547 DIE for the function. */
27548 && ! local_function_static (decl))
27549 dwarf2out_decl (decl);
27550 else if (die)
27551 {
27552 /* We get called via the symtab code invoking late_global_decl
27553 for symbols that are optimized out.
27554
27555 Do not add locations for those, except if they have a
27556 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27557 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27558 INDIRECT_REF expression, as this could generate relocations to
27559 text symbols in LTO object files, which is invalid. */
27560 varpool_node *node = varpool_node::get (decl);
27561 if ((! node || ! node->definition)
27562 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27563 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27564 tree_add_const_value_attribute_for_decl (die, decl);
27565 else
27566 add_location_or_const_value_attribute (die, decl, false);
27567 }
27568 }
27569 }
27570
27571 /* Output debug information for type decl DECL. Called from toplev.cc
27572 and from language front ends (to record built-in types). */
27573 static void
27574 dwarf2out_type_decl (tree decl, int local)
27575 {
27576 if (!local)
27577 {
27578 set_early_dwarf s;
27579 dwarf2out_decl (decl);
27580 }
27581 }
27582
27583 /* Output debug information for imported module or decl DECL.
27584 NAME is non-NULL name in the lexical block if the decl has been renamed.
27585 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27586 that DECL belongs to.
27587 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27588 static void
27589 dwarf2out_imported_module_or_decl_1 (tree decl,
27590 tree name,
27591 tree lexical_block,
27592 dw_die_ref lexical_block_die)
27593 {
27594 expanded_location xloc;
27595 dw_die_ref imported_die = NULL;
27596 dw_die_ref at_import_die;
27597
27598 if (TREE_CODE (decl) == IMPORTED_DECL)
27599 {
27600 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27601 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27602 gcc_assert (decl);
27603 }
27604 else
27605 xloc = expand_location (input_location);
27606
27607 if (TREE_CODE (decl) == TYPE_DECL)
27608 {
27609 at_import_die = force_type_die (TREE_TYPE (decl));
27610 /* For namespace N { typedef void T; } using N::T; base_type_die
27611 returns NULL, but DW_TAG_imported_declaration requires
27612 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27613 if (!at_import_die)
27614 {
27615 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27616 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27617 at_import_die = lookup_type_die (TREE_TYPE (decl));
27618 gcc_assert (at_import_die);
27619 }
27620 }
27621 else
27622 {
27623 at_import_die = lookup_decl_die (decl);
27624 if (!at_import_die)
27625 {
27626 /* If we're trying to avoid duplicate debug info, we may not have
27627 emitted the member decl for this field. Emit it now. */
27628 if (TREE_CODE (decl) == FIELD_DECL)
27629 {
27630 tree type = DECL_CONTEXT (decl);
27631
27632 if (TYPE_CONTEXT (type)
27633 && TYPE_P (TYPE_CONTEXT (type))
27634 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27635 DINFO_USAGE_DIR_USE))
27636 return;
27637 gen_type_die_for_member (type, decl,
27638 get_context_die (TYPE_CONTEXT (type)));
27639 }
27640 if (TREE_CODE (decl) == CONST_DECL)
27641 {
27642 /* Individual enumerators of an enum type do not get output here
27643 (see gen_decl_die), so we cannot call force_decl_die. */
27644 if (!is_fortran () && !is_ada () && !is_dlang ())
27645 return;
27646 }
27647 if (TREE_CODE (decl) == NAMELIST_DECL)
27648 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27649 get_context_die (DECL_CONTEXT (decl)),
27650 NULL_TREE);
27651 else
27652 at_import_die = force_decl_die (decl);
27653 }
27654 }
27655
27656 if (TREE_CODE (decl) == NAMESPACE_DECL)
27657 {
27658 if (dwarf_version >= 3 || !dwarf_strict)
27659 imported_die = new_die (DW_TAG_imported_module,
27660 lexical_block_die,
27661 lexical_block);
27662 else
27663 return;
27664 }
27665 else
27666 imported_die = new_die (DW_TAG_imported_declaration,
27667 lexical_block_die,
27668 lexical_block);
27669
27670 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27671 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27672 if (debug_column_info && xloc.column)
27673 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27674 if (name)
27675 add_AT_string (imported_die, DW_AT_name,
27676 IDENTIFIER_POINTER (name));
27677 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27678 }
27679
27680 /* Output debug information for imported module or decl DECL.
27681 NAME is non-NULL name in context if the decl has been renamed.
27682 CHILD is true if decl is one of the renamed decls as part of
27683 importing whole module.
27684 IMPLICIT is set if this hook is called for an implicit import
27685 such as inline namespace. */
27686
27687 static void
27688 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27689 bool child, bool implicit)
27690 {
27691 /* dw_die_ref at_import_die; */
27692 dw_die_ref scope_die;
27693
27694 if (debug_info_level <= DINFO_LEVEL_TERSE)
27695 return;
27696
27697 gcc_assert (decl);
27698
27699 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27700 should be enough, for DWARF4 and older even if we emit as extension
27701 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27702 for the benefit of consumers unaware of DW_AT_export_symbols. */
27703 if (implicit
27704 && dwarf_version >= 5
27705 && lang_hooks.decls.decl_dwarf_attribute (decl,
27706 DW_AT_export_symbols) == 1)
27707 return;
27708
27709 set_early_dwarf s;
27710
27711 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27712 We need decl DIE for reference and scope die. First, get DIE for the decl
27713 itself. */
27714
27715 /* Get the scope die for decl context. Use comp_unit_die for global module
27716 or decl. If die is not found for non globals, force new die. */
27717 if (context
27718 && TYPE_P (context)
27719 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27720 return;
27721
27722 scope_die = get_context_die (context);
27723
27724 if (child)
27725 {
27726 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27727 there is nothing we can do, here. */
27728 if (dwarf_version < 3 && dwarf_strict)
27729 return;
27730
27731 gcc_assert (scope_die->die_child);
27732 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27733 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27734 scope_die = scope_die->die_child;
27735 }
27736
27737 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27738 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27739 }
27740
27741 /* Output debug information for namelists. */
27742
27743 static dw_die_ref
27744 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27745 {
27746 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27747 tree value;
27748 unsigned i;
27749
27750 if (debug_info_level <= DINFO_LEVEL_TERSE)
27751 return NULL;
27752
27753 gcc_assert (scope_die != NULL);
27754 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27755 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27756
27757 /* If there are no item_decls, we have a nondefining namelist, e.g.
27758 with USE association; hence, set DW_AT_declaration. */
27759 if (item_decls == NULL_TREE)
27760 {
27761 add_AT_flag (nml_die, DW_AT_declaration, 1);
27762 return nml_die;
27763 }
27764
27765 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27766 {
27767 nml_item_ref_die = lookup_decl_die (value);
27768 if (!nml_item_ref_die)
27769 nml_item_ref_die = force_decl_die (value);
27770
27771 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27772 add_AT_die_ref (nml_item_die, DW_AT_namelist_item, nml_item_ref_die);
27773 }
27774 return nml_die;
27775 }
27776
27777
27778 /* Write the debugging output for DECL and return the DIE. */
27779
27780 static void
27781 dwarf2out_decl (tree decl)
27782 {
27783 dw_die_ref context_die = comp_unit_die ();
27784
27785 switch (TREE_CODE (decl))
27786 {
27787 case ERROR_MARK:
27788 return;
27789
27790 case FUNCTION_DECL:
27791 /* If we're a nested function, initially use a parent of NULL; if we're
27792 a plain function, this will be fixed up in decls_for_scope. If
27793 we're a method, it will be ignored, since we already have a DIE.
27794 Avoid doing this late though since clones of class methods may
27795 otherwise end up in limbo and create type DIEs late. */
27796 if (early_dwarf
27797 && decl_function_context (decl)
27798 /* But if we're in terse mode, we don't care about scope. */
27799 && debug_info_level > DINFO_LEVEL_TERSE)
27800 context_die = NULL;
27801 break;
27802
27803 case VAR_DECL:
27804 /* For local statics lookup proper context die. */
27805 if (local_function_static (decl))
27806 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27807
27808 /* If we are in terse mode, don't generate any DIEs to represent any
27809 variable declarations or definitions unless it is external. */
27810 if (debug_info_level < DINFO_LEVEL_TERSE
27811 || (debug_info_level == DINFO_LEVEL_TERSE
27812 && !TREE_PUBLIC (decl)))
27813 return;
27814 break;
27815
27816 case CONST_DECL:
27817 if (debug_info_level <= DINFO_LEVEL_TERSE)
27818 return;
27819 if (!is_fortran () && !is_ada () && !is_dlang ())
27820 return;
27821 if (TREE_STATIC (decl) && decl_function_context (decl))
27822 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27823 break;
27824
27825 case NAMESPACE_DECL:
27826 case IMPORTED_DECL:
27827 if (debug_info_level <= DINFO_LEVEL_TERSE)
27828 return;
27829 if (lookup_decl_die (decl) != NULL)
27830 return;
27831 break;
27832
27833 case TYPE_DECL:
27834 /* Don't emit stubs for types unless they are needed by other DIEs. */
27835 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27836 return;
27837
27838 /* Don't bother trying to generate any DIEs to represent any of the
27839 normal built-in types for the language we are compiling. */
27840 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27841 return;
27842
27843 /* If we are in terse mode, don't generate any DIEs for types. */
27844 if (debug_info_level <= DINFO_LEVEL_TERSE)
27845 return;
27846
27847 /* If we're a function-scope tag, initially use a parent of NULL;
27848 this will be fixed up in decls_for_scope. */
27849 if (decl_function_context (decl))
27850 context_die = NULL;
27851
27852 break;
27853
27854 case NAMELIST_DECL:
27855 break;
27856
27857 default:
27858 return;
27859 }
27860
27861 gen_decl_die (decl, NULL, NULL, context_die);
27862
27863 if (flag_checking)
27864 {
27865 dw_die_ref die = lookup_decl_die (decl);
27866 if (die)
27867 check_die (die);
27868 }
27869 }
27870
27871 /* Write the debugging output for DECL. */
27872
27873 static void
27874 dwarf2out_function_decl (tree decl)
27875 {
27876 dwarf2out_decl (decl);
27877 call_arg_locations = NULL;
27878 call_arg_loc_last = NULL;
27879 call_site_count = -1;
27880 tail_call_site_count = -1;
27881 decl_loc_table->empty ();
27882 cached_dw_loc_list_table->empty ();
27883 }
27884
27885 /* Output a marker (i.e. a label) for the beginning of the generated code for
27886 a lexical block. */
27887
27888 static void
27889 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27890 unsigned int blocknum,
27891 tree block ATTRIBUTE_UNUSED)
27892 {
27893 #ifdef CODEVIEW_DEBUGGING_INFO
27894 if (codeview_debuginfo_p ())
27895 codeview_begin_block (line, blocknum, block);
27896 #endif
27897
27898 switch_to_section (current_function_section ());
27899 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27900 }
27901
27902 /* Output a marker (i.e. a label) for the end of the generated code for a
27903 lexical block. */
27904
27905 static void
27906 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27907 {
27908 #ifdef CODEVIEW_DEBUGGING_INFO
27909 if (codeview_debuginfo_p ())
27910 codeview_end_block (line, blocknum);
27911 #endif
27912
27913 switch_to_section (current_function_section ());
27914 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27915 }
27916
27917 /* Returns true if it is appropriate not to emit any debugging
27918 information for BLOCK, because it doesn't contain any instructions.
27919
27920 Don't allow this for blocks with nested functions or local classes
27921 as we would end up with orphans, and in the presence of scheduling
27922 we may end up calling them anyway. */
27923
27924 static bool
27925 dwarf2out_ignore_block (const_tree block)
27926 {
27927 tree decl;
27928 unsigned int i;
27929
27930 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27931 if (TREE_CODE (decl) == FUNCTION_DECL
27932 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27933 return false;
27934 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27935 {
27936 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27937 if (TREE_CODE (decl) == FUNCTION_DECL
27938 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27939 return false;
27940 }
27941
27942 return true;
27943 }
27944
27945 /* Hash table routines for file_hash. */
27946
27947 bool
27948 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27949 {
27950 return filename_cmp (p1->key, p2) == 0;
27951 }
27952
27953 hashval_t
27954 dwarf_file_hasher::hash (dwarf_file_data *p)
27955 {
27956 return htab_hash_string (p->key);
27957 }
27958
27959 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27960 dwarf2out.cc) and return its "index". The index of each (known) filename is
27961 just a unique number which is associated with only that one filename. We
27962 need such numbers for the sake of generating labels (in the .debug_sfnames
27963 section) and references to those files numbers (in the .debug_srcinfo
27964 and .debug_macinfo sections). If the filename given as an argument is not
27965 found in our current list, add it to the list and assign it the next
27966 available unique index number. */
27967
27968 static struct dwarf_file_data *
27969 lookup_filename (const char *file_name)
27970 {
27971 struct dwarf_file_data * created;
27972
27973 if (!file_name)
27974 return NULL;
27975
27976 if (!file_name[0])
27977 file_name = "<stdin>";
27978
27979 dwarf_file_data **slot
27980 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27981 INSERT);
27982 if (*slot)
27983 return *slot;
27984
27985 created = ggc_alloc<dwarf_file_data> ();
27986 created->key = file_name;
27987 created->filename = remap_debug_filename (file_name);
27988 created->emitted_number = 0;
27989 *slot = created;
27990 return created;
27991 }
27992
27993 /* If the assembler will construct the file table, then translate the compiler
27994 internal file table number into the assembler file table number, and emit
27995 a .file directive if we haven't already emitted one yet. The file table
27996 numbers are different because we prune debug info for unused variables and
27997 types, which may include filenames. */
27998
27999 static int
28000 maybe_emit_file (struct dwarf_file_data * fd)
28001 {
28002 if (! fd->emitted_number)
28003 {
28004 if (last_emitted_file)
28005 fd->emitted_number = last_emitted_file->emitted_number + 1;
28006 else
28007 fd->emitted_number = 1;
28008 last_emitted_file = fd;
28009
28010 if (output_asm_line_debug_info ())
28011 {
28012 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
28013 output_quoted_string (asm_out_file, fd->filename);
28014 fputc ('\n', asm_out_file);
28015 }
28016 }
28017
28018 return fd->emitted_number;
28019 }
28020
28021 /* Schedule generation of a DW_AT_const_value attribute to DIE.
28022 That generation should happen after function debug info has been
28023 generated. The value of the attribute is the constant value of ARG. */
28024
28025 static void
28026 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
28027 {
28028 die_arg_entry entry;
28029
28030 if (!die || !arg)
28031 return;
28032
28033 gcc_assert (early_dwarf);
28034
28035 if (!tmpl_value_parm_die_table)
28036 vec_alloc (tmpl_value_parm_die_table, 32);
28037
28038 entry.die = die;
28039 entry.arg = arg;
28040 vec_safe_push (tmpl_value_parm_die_table, entry);
28041 }
28042
28043 /* Return TRUE if T is an instance of generic type, FALSE
28044 otherwise. */
28045
28046 static bool
28047 generic_type_p (tree t)
28048 {
28049 if (t == NULL_TREE || !TYPE_P (t))
28050 return false;
28051 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
28052 }
28053
28054 /* Schedule the generation of the generic parameter dies for the
28055 instance of generic type T. The proper generation itself is later
28056 done by gen_scheduled_generic_parms_dies. */
28057
28058 static void
28059 schedule_generic_params_dies_gen (tree t)
28060 {
28061 if (!generic_type_p (t))
28062 return;
28063
28064 gcc_assert (early_dwarf);
28065
28066 if (!generic_type_instances)
28067 vec_alloc (generic_type_instances, 256);
28068
28069 vec_safe_push (generic_type_instances, t);
28070 }
28071
28072 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
28073 by append_entry_to_tmpl_value_parm_die_table. This function must
28074 be called after function DIEs have been generated. */
28075
28076 static void
28077 gen_remaining_tmpl_value_param_die_attribute (void)
28078 {
28079 if (tmpl_value_parm_die_table)
28080 {
28081 unsigned i, j;
28082 die_arg_entry *e;
28083
28084 /* We do this in two phases - first get the cases we can
28085 handle during early-finish, preserving those we cannot
28086 (containing symbolic constants where we don't yet know
28087 whether we are going to output the referenced symbols).
28088 For those we try again at late-finish. */
28089 j = 0;
28090 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
28091 {
28092 if (!e->die->removed
28093 && !tree_add_const_value_attribute (e->die, e->arg))
28094 {
28095 dw_loc_descr_ref loc = NULL;
28096 if (! early_dwarf
28097 && (dwarf_version >= 5 || !dwarf_strict))
28098 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
28099 if (loc)
28100 add_AT_loc (e->die, DW_AT_location, loc);
28101 else
28102 (*tmpl_value_parm_die_table)[j++] = *e;
28103 }
28104 }
28105 tmpl_value_parm_die_table->truncate (j);
28106 }
28107 }
28108
28109 /* Generate generic parameters DIEs for instances of generic types
28110 that have been previously scheduled by
28111 schedule_generic_params_dies_gen. This function must be called
28112 after all the types of the CU have been laid out. */
28113
28114 static void
28115 gen_scheduled_generic_parms_dies (void)
28116 {
28117 unsigned i;
28118 tree t;
28119
28120 if (!generic_type_instances)
28121 return;
28122
28123 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
28124 if (COMPLETE_TYPE_P (t))
28125 gen_generic_params_dies (t);
28126
28127 generic_type_instances = NULL;
28128 }
28129
28130
28131 /* Replace DW_AT_name for the decl with name. */
28132
28133 static void
28134 dwarf2out_set_name (tree decl, tree name)
28135 {
28136 dw_die_ref die;
28137 dw_attr_node *attr;
28138 const char *dname;
28139
28140 die = TYPE_SYMTAB_DIE (decl);
28141 if (!die)
28142 return;
28143
28144 dname = dwarf2_name (name, 0);
28145 if (!dname)
28146 return;
28147
28148 attr = get_AT (die, DW_AT_name);
28149 if (attr)
28150 {
28151 struct indirect_string_node *node;
28152
28153 node = find_AT_string (dname);
28154 /* replace the string. */
28155 attr->dw_attr_val.v.val_str = node;
28156 }
28157
28158 else
28159 add_name_attribute (die, dname);
28160 }
28161
28162 /* True if before or during processing of the first function being emitted. */
28163 static bool in_first_function_p = true;
28164 /* True if loc_note during dwarf2out_var_location call might still be
28165 before first real instruction at address equal to .Ltext0. */
28166 static bool maybe_at_text_label_p = true;
28167 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
28168 static unsigned int first_loclabel_num_not_at_text_label;
28169
28170 /* Look ahead for a real insn. */
28171
28172 static rtx_insn *
28173 dwarf2out_next_real_insn (rtx_insn *loc_note)
28174 {
28175 rtx_insn *next_real = NEXT_INSN (loc_note);
28176
28177 while (next_real)
28178 if (INSN_P (next_real))
28179 break;
28180 else
28181 next_real = NEXT_INSN (next_real);
28182
28183 return next_real;
28184 }
28185
28186 /* Called by the final INSN scan whenever we see a var location. We
28187 use it to drop labels in the right places, and throw the location in
28188 our lookup table. */
28189
28190 static void
28191 dwarf2out_var_location (rtx_insn *loc_note)
28192 {
28193 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
28194 struct var_loc_node *newloc;
28195 rtx_insn *next_real;
28196 rtx_insn *call_insn = NULL;
28197 static const char *last_label;
28198 static const char *last_postcall_label;
28199 static bool last_in_cold_section_p;
28200 static rtx_insn *expected_next_loc_note;
28201 tree decl;
28202 bool var_loc_p;
28203 var_loc_view view = 0;
28204
28205 if (!NOTE_P (loc_note))
28206 {
28207 if (CALL_P (loc_note))
28208 {
28209 maybe_reset_location_view (loc_note, cur_line_info_table);
28210 call_site_count++;
28211 if (SIBLING_CALL_P (loc_note))
28212 tail_call_site_count++;
28213 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
28214 {
28215 call_insn = loc_note;
28216 loc_note = NULL;
28217 var_loc_p = false;
28218
28219 next_real = dwarf2out_next_real_insn (call_insn);
28220 cached_next_real_insn = NULL;
28221 goto create_label;
28222 }
28223 if (optimize == 0 && !flag_var_tracking)
28224 {
28225 /* When the var-tracking pass is not running, there is no note
28226 for indirect calls whose target is compile-time known. In this
28227 case, process such calls specifically so that we generate call
28228 sites for them anyway. */
28229 rtx x = PATTERN (loc_note);
28230 if (GET_CODE (x) == PARALLEL)
28231 x = XVECEXP (x, 0, 0);
28232 if (GET_CODE (x) == SET)
28233 x = SET_SRC (x);
28234 if (GET_CODE (x) == CALL)
28235 x = XEXP (x, 0);
28236 if (!MEM_P (x)
28237 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
28238 || !SYMBOL_REF_DECL (XEXP (x, 0))
28239 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
28240 != FUNCTION_DECL))
28241 {
28242 call_insn = loc_note;
28243 loc_note = NULL;
28244 var_loc_p = false;
28245
28246 next_real = dwarf2out_next_real_insn (call_insn);
28247 cached_next_real_insn = NULL;
28248 goto create_label;
28249 }
28250 }
28251 }
28252 else if (!debug_variable_location_views)
28253 gcc_unreachable ();
28254 else
28255 maybe_reset_location_view (loc_note, cur_line_info_table);
28256
28257 return;
28258 }
28259
28260 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
28261 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
28262 return;
28263
28264 /* Optimize processing a large consecutive sequence of location
28265 notes so we don't spend too much time in next_real_insn. If the
28266 next insn is another location note, remember the next_real_insn
28267 calculation for next time. */
28268 next_real = cached_next_real_insn;
28269 if (next_real)
28270 {
28271 if (expected_next_loc_note != loc_note)
28272 next_real = NULL;
28273 }
28274
28275 if (! next_real)
28276 next_real = dwarf2out_next_real_insn (loc_note);
28277
28278 if (next_real)
28279 {
28280 rtx_insn *next_note = NEXT_INSN (loc_note);
28281 while (next_note != next_real)
28282 {
28283 if (! next_note->deleted ()
28284 && NOTE_P (next_note)
28285 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
28286 break;
28287 next_note = NEXT_INSN (next_note);
28288 }
28289
28290 if (next_note == next_real)
28291 cached_next_real_insn = NULL;
28292 else
28293 {
28294 expected_next_loc_note = next_note;
28295 cached_next_real_insn = next_real;
28296 }
28297 }
28298 else
28299 cached_next_real_insn = NULL;
28300
28301 /* If there are no instructions which would be affected by this note,
28302 don't do anything. */
28303 if (var_loc_p
28304 && next_real == NULL_RTX
28305 && !NOTE_DURING_CALL_P (loc_note))
28306 return;
28307
28308 create_label:
28309
28310 if (next_real == NULL_RTX)
28311 next_real = get_last_insn ();
28312
28313 /* If there were any real insns between note we processed last time
28314 and this note (or if it is the first note), clear
28315 last_{,postcall_}label so that they are not reused this time. */
28316 if (last_var_location_insn == NULL_RTX
28317 || last_var_location_insn != next_real
28318 || last_in_cold_section_p != in_cold_section_p)
28319 {
28320 last_label = NULL;
28321 last_postcall_label = NULL;
28322 }
28323
28324 if (var_loc_p)
28325 {
28326 const char *label
28327 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28328 view = cur_line_info_table->view;
28329 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28330 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28331 if (newloc == NULL)
28332 return;
28333 }
28334 else
28335 {
28336 decl = NULL_TREE;
28337 newloc = NULL;
28338 }
28339
28340 /* If there were no real insns between note we processed last time
28341 and this note, use the label we emitted last time. Otherwise
28342 create a new label and emit it. */
28343 if (last_label == NULL)
28344 {
28345 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28346 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28347 loclabel_num++;
28348 last_label = ggc_strdup (loclabel);
28349 /* See if loclabel might be equal to .Ltext0. If yes,
28350 bump first_loclabel_num_not_at_text_label. */
28351 if (!have_multiple_function_sections
28352 && in_first_function_p
28353 && maybe_at_text_label_p)
28354 {
28355 static rtx_insn *last_start;
28356 rtx_insn *insn;
28357 for (insn = loc_note; insn; insn = previous_insn (insn))
28358 if (insn == last_start)
28359 break;
28360 else if (!NONDEBUG_INSN_P (insn))
28361 continue;
28362 else
28363 {
28364 rtx body = PATTERN (insn);
28365 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28366 continue;
28367 /* Inline asm could occupy zero bytes. */
28368 else if (GET_CODE (body) == ASM_INPUT
28369 || asm_noperands (body) >= 0)
28370 continue;
28371 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28372 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28373 continue;
28374 #endif
28375 else
28376 {
28377 /* Assume insn has non-zero length. */
28378 maybe_at_text_label_p = false;
28379 break;
28380 }
28381 }
28382 if (maybe_at_text_label_p)
28383 {
28384 last_start = loc_note;
28385 first_loclabel_num_not_at_text_label = loclabel_num;
28386 }
28387 }
28388 }
28389
28390 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28391 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28392
28393 if (!var_loc_p)
28394 {
28395 struct call_arg_loc_node *ca_loc
28396 = ggc_cleared_alloc<call_arg_loc_node> ();
28397 rtx_insn *prev = call_insn;
28398
28399 ca_loc->call_insn = call_insn;
28400 ca_loc->next = NULL;
28401 ca_loc->label = last_label;
28402 gcc_assert (prev
28403 && (CALL_P (prev)
28404 || (NONJUMP_INSN_P (prev)
28405 && GET_CODE (PATTERN (prev)) == SEQUENCE
28406 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28407 if (!CALL_P (prev))
28408 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28409 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28410
28411 /* Look for a SYMBOL_REF in the "prev" instruction. */
28412 rtx x = get_call_rtx_from (prev);
28413 if (x)
28414 {
28415 /* Try to get the call symbol, if any. */
28416 if (MEM_P (XEXP (x, 0)))
28417 x = XEXP (x, 0);
28418 /* First, look for a memory access to a symbol_ref. */
28419 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28420 && SYMBOL_REF_DECL (XEXP (x, 0))
28421 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28422 ca_loc->symbol_ref = XEXP (x, 0);
28423 /* Otherwise, look at a compile-time known user-level function
28424 declaration. */
28425 else if (MEM_P (x)
28426 && MEM_EXPR (x)
28427 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28428 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28429 }
28430
28431 ca_loc->block = insn_scope (prev);
28432 if (call_arg_locations)
28433 call_arg_loc_last->next = ca_loc;
28434 else
28435 call_arg_locations = ca_loc;
28436 call_arg_loc_last = ca_loc;
28437 }
28438 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28439 {
28440 newloc->label = last_label;
28441 newloc->view = view;
28442 }
28443 else
28444 {
28445 if (!last_postcall_label)
28446 {
28447 sprintf (loclabel, "%s-1", last_label);
28448 last_postcall_label = ggc_strdup (loclabel);
28449 }
28450 newloc->label = last_postcall_label;
28451 /* ??? This view is at last_label, not last_label-1, but we
28452 could only assume view at last_label-1 is zero if we could
28453 assume calls always have length greater than one. This is
28454 probably true in general, though there might be a rare
28455 exception to this rule, e.g. if a call insn is optimized out
28456 by target magic. Then, even the -1 in the label will be
28457 wrong, which might invalidate the range. Anyway, using view,
28458 though technically possibly incorrect, will work as far as
28459 ranges go: since L-1 is in the middle of the call insn,
28460 (L-1).0 and (L-1).V shouldn't make any difference, and having
28461 the loclist entry refer to the .loc entry might be useful, so
28462 leave it like this. */
28463 newloc->view = view;
28464 }
28465
28466 if (var_loc_p && flag_debug_asm)
28467 {
28468 const char *name, *sep, *patstr;
28469 if (decl && DECL_NAME (decl))
28470 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28471 else
28472 name = "";
28473 if (NOTE_VAR_LOCATION_LOC (loc_note))
28474 {
28475 sep = " => ";
28476 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28477 }
28478 else
28479 {
28480 sep = " ";
28481 patstr = "RESET";
28482 }
28483 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28484 name, sep, patstr);
28485 }
28486
28487 last_var_location_insn = next_real;
28488 last_in_cold_section_p = in_cold_section_p;
28489 }
28490
28491 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28492 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28493 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28494 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28495 BLOCK_FRAGMENT_ORIGIN links. */
28496 static bool
28497 block_within_block_p (tree block, tree outer, bool bothways)
28498 {
28499 if (block == outer)
28500 return true;
28501
28502 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28503 for (tree context = BLOCK_SUPERCONTEXT (block);
28504 context != outer;
28505 context = BLOCK_SUPERCONTEXT (context))
28506 if (!context || TREE_CODE (context) != BLOCK)
28507 return false;
28508
28509 if (!bothways)
28510 return true;
28511
28512 /* Now check that each block is actually referenced by its
28513 parent. */
28514 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28515 context = BLOCK_SUPERCONTEXT (context))
28516 {
28517 if (BLOCK_FRAGMENT_ORIGIN (context))
28518 {
28519 gcc_assert (!BLOCK_SUBBLOCKS (context));
28520 context = BLOCK_FRAGMENT_ORIGIN (context);
28521 }
28522 for (tree sub = BLOCK_SUBBLOCKS (context);
28523 sub != block;
28524 sub = BLOCK_CHAIN (sub))
28525 if (!sub)
28526 return false;
28527 if (context == outer)
28528 return true;
28529 else
28530 block = context;
28531 }
28532 }
28533
28534 /* Called during final while assembling the marker of the entry point
28535 for an inlined function. */
28536
28537 static void
28538 dwarf2out_inline_entry (tree block)
28539 {
28540 gcc_assert (debug_inline_points);
28541
28542 /* If we can't represent it, don't bother. */
28543 if (!(dwarf_version >= 3 || !dwarf_strict))
28544 return;
28545
28546 gcc_assert (DECL_P (block_ultimate_origin (block)));
28547
28548 /* Sanity check the block tree. This would catch a case in which
28549 BLOCK got removed from the tree reachable from the outermost
28550 lexical block, but got retained in markers. It would still link
28551 back to its parents, but some ancestor would be missing a link
28552 down the path to the sub BLOCK. If the block got removed, its
28553 BLOCK_NUMBER will not be a usable value. */
28554 if (flag_checking)
28555 gcc_assert (block_within_block_p (block,
28556 DECL_INITIAL (current_function_decl),
28557 true));
28558
28559 gcc_assert (inlined_function_outer_scope_p (block));
28560 gcc_assert (!lookup_block_die (block));
28561
28562 if (BLOCK_FRAGMENT_ORIGIN (block))
28563 block = BLOCK_FRAGMENT_ORIGIN (block);
28564 /* Can the entry point ever not be at the beginning of an
28565 unfragmented lexical block? */
28566 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28567 || (cur_line_info_table
28568 && !ZERO_VIEW_P (cur_line_info_table->view))))
28569 return;
28570
28571 if (!inline_entry_data_table)
28572 inline_entry_data_table
28573 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28574
28575
28576 inline_entry_data **iedp
28577 = inline_entry_data_table->find_slot_with_hash (block,
28578 htab_hash_pointer (block),
28579 INSERT);
28580 if (*iedp)
28581 /* ??? Ideally, we'd record all entry points for the same inlined
28582 function (some may have been duplicated by e.g. unrolling), but
28583 we have no way to represent that ATM. */
28584 return;
28585
28586 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28587 ied->block = block;
28588 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28589 ied->label_num = BLOCK_NUMBER (block);
28590 if (cur_line_info_table)
28591 ied->view = cur_line_info_table->view;
28592
28593 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28594 BLOCK_NUMBER (block));
28595 }
28596
28597 /* Called from finalize_size_functions for size functions so that their body
28598 can be encoded in the debug info to describe the layout of variable-length
28599 structures. */
28600
28601 static void
28602 dwarf2out_size_function (tree decl)
28603 {
28604 set_early_dwarf s;
28605 function_to_dwarf_procedure (decl);
28606 }
28607
28608 /* Note in one location list that text section has changed. */
28609
28610 int
28611 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28612 {
28613 var_loc_list *list = *slot;
28614 if (list->first)
28615 list->last_before_switch
28616 = list->last->next ? list->last->next : list->last;
28617 return 1;
28618 }
28619
28620 /* Note in all location lists that text section has changed. */
28621
28622 static void
28623 var_location_switch_text_section (void)
28624 {
28625 if (decl_loc_table == NULL)
28626 return;
28627
28628 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28629 }
28630
28631 /* Create a new line number table. */
28632
28633 static dw_line_info_table *
28634 new_line_info_table (void)
28635 {
28636 dw_line_info_table *table;
28637
28638 table = ggc_cleared_alloc<dw_line_info_table> ();
28639 table->file_num = 1;
28640 table->line_num = 1;
28641 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28642 FORCE_RESET_NEXT_VIEW (table->view);
28643 table->symviews_since_reset = 0;
28644
28645 return table;
28646 }
28647
28648 /* Lookup the "current" table into which we emit line info, so
28649 that we don't have to do it for every source line. */
28650
28651 static void
28652 set_cur_line_info_table (section *sec)
28653 {
28654 dw_line_info_table *table;
28655
28656 if (sec == text_section)
28657 table = text_section_line_info;
28658 else if (sec == cold_text_section)
28659 {
28660 table = cold_text_section_line_info;
28661 if (!table)
28662 {
28663 cold_text_section_line_info = table = new_line_info_table ();
28664 table->end_label = cold_end_label;
28665 }
28666 }
28667 else
28668 {
28669 const char *end_label;
28670
28671 if (crtl->has_bb_partition)
28672 {
28673 if (in_cold_section_p)
28674 end_label = crtl->subsections.cold_section_end_label;
28675 else
28676 end_label = crtl->subsections.hot_section_end_label;
28677 }
28678 else
28679 {
28680 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28681 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28682 current_function_funcdef_no);
28683 end_label = ggc_strdup (label);
28684 }
28685
28686 table = new_line_info_table ();
28687 table->end_label = end_label;
28688
28689 vec_safe_push (separate_line_info, table);
28690 }
28691
28692 if (output_asm_line_debug_info ())
28693 table->is_stmt = (cur_line_info_table
28694 ? cur_line_info_table->is_stmt
28695 : DWARF_LINE_DEFAULT_IS_STMT_START);
28696 cur_line_info_table = table;
28697 }
28698
28699
28700 /* We need to reset the locations at the beginning of each
28701 function. We can't do this in the end_function hook, because the
28702 declarations that use the locations won't have been output when
28703 that hook is called. Also compute have_multiple_function_sections here. */
28704
28705 static void
28706 dwarf2out_begin_function (tree fun)
28707 {
28708 section *sec = function_section (fun);
28709
28710 if (sec != text_section)
28711 have_multiple_function_sections = true;
28712
28713 if (crtl->has_bb_partition && !cold_text_section)
28714 {
28715 gcc_assert (current_function_decl == fun);
28716 cold_text_section = unlikely_text_section ();
28717 switch_to_section (cold_text_section);
28718 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28719 switch_to_section (sec);
28720 }
28721
28722 call_site_count = 0;
28723 tail_call_site_count = 0;
28724
28725 set_cur_line_info_table (sec);
28726 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28727 }
28728
28729 /* Helper function of dwarf2out_end_function, called only after emitting
28730 the very first function into assembly. Check if some .debug_loc range
28731 might end with a .LVL* label that could be equal to .Ltext0.
28732 In that case we must force using absolute addresses in .debug_loc ranges,
28733 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28734 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28735 list terminator.
28736 Set have_multiple_function_sections to true in that case and
28737 terminate htab traversal. */
28738
28739 int
28740 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28741 {
28742 var_loc_list *entry = *slot;
28743 struct var_loc_node *node;
28744
28745 node = entry->first;
28746 if (node && node->next && node->next->label)
28747 {
28748 unsigned int i;
28749 const char *label = node->next->label;
28750 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28751
28752 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28753 {
28754 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28755 if (strcmp (label, loclabel) == 0)
28756 {
28757 have_multiple_function_sections = true;
28758 return 0;
28759 }
28760 }
28761 }
28762 return 1;
28763 }
28764
28765 /* Hook called after emitting a function into assembly.
28766 This does something only for the very first function emitted. */
28767
28768 static void
28769 dwarf2out_end_function (unsigned int)
28770 {
28771 if (in_first_function_p
28772 && !have_multiple_function_sections
28773 && first_loclabel_num_not_at_text_label
28774 && decl_loc_table)
28775 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28776 in_first_function_p = false;
28777 maybe_at_text_label_p = false;
28778 }
28779
28780 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28781 front-ends register a translation unit even before dwarf2out_init is
28782 called. */
28783 static tree main_translation_unit = NULL_TREE;
28784
28785 /* Hook called by front-ends after they built their main translation unit.
28786 Associate comp_unit_die to UNIT. */
28787
28788 static void
28789 dwarf2out_register_main_translation_unit (tree unit)
28790 {
28791 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28792 && main_translation_unit == NULL_TREE);
28793 main_translation_unit = unit;
28794 /* If dwarf2out_init has not been called yet, it will perform the association
28795 itself looking at main_translation_unit. */
28796 if (decl_die_table != NULL)
28797 equate_decl_number_to_die (unit, comp_unit_die ());
28798 }
28799
28800 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28801
28802 static void
28803 push_dw_line_info_entry (dw_line_info_table *table,
28804 enum dw_line_info_opcode opcode, unsigned int val)
28805 {
28806 dw_line_info_entry e;
28807 e.opcode = opcode;
28808 e.val = val;
28809 vec_safe_push (table->entries, e);
28810 }
28811
28812 /* Output a label to mark the beginning of a source code line entry
28813 and record information relating to this source line, in
28814 'line_info_table' for later output of the .debug_line section. */
28815 /* ??? The discriminator parameter ought to be unsigned. */
28816
28817 static void
28818 dwarf2out_source_line (unsigned int line, unsigned int column,
28819 const char *filename,
28820 int discriminator, bool is_stmt)
28821 {
28822 unsigned int file_num;
28823 dw_line_info_table *table;
28824 static var_loc_view lvugid;
28825
28826 #ifdef CODEVIEW_DEBUGGING_INFO
28827 if (codeview_debuginfo_p ())
28828 codeview_source_line (line, filename);
28829 #endif
28830
28831 /* 'line_info_table' information gathering is not needed when the debug
28832 info level is set to the lowest value. Also, the current DWARF-based
28833 debug formats do not use this info. */
28834 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28835 return;
28836
28837 table = cur_line_info_table;
28838
28839 if (line == 0)
28840 {
28841 if (debug_variable_location_views
28842 && output_asm_line_debug_info ()
28843 && table && !RESETTING_VIEW_P (table->view))
28844 {
28845 /* If we're using the assembler to compute view numbers, we
28846 can't issue a .loc directive for line zero, so we can't
28847 get a view number at this point. We might attempt to
28848 compute it from the previous view, or equate it to a
28849 subsequent view (though it might not be there!), but
28850 since we're omitting the line number entry, we might as
28851 well omit the view number as well. That means pretending
28852 it's a view number zero, which might very well turn out
28853 to be correct. ??? Extend the assembler so that the
28854 compiler could emit e.g. ".locview .LVU#", to output a
28855 view without changing line number information. We'd then
28856 have to count it in symviews_since_reset; when it's omitted,
28857 it doesn't count. */
28858 if (!zero_view_p)
28859 zero_view_p = BITMAP_GGC_ALLOC ();
28860 bitmap_set_bit (zero_view_p, table->view);
28861 if (flag_debug_asm)
28862 {
28863 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28864 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28865 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28866 ASM_COMMENT_START);
28867 assemble_name (asm_out_file, label);
28868 putc ('\n', asm_out_file);
28869 }
28870 table->view = ++lvugid;
28871 }
28872 return;
28873 }
28874
28875 /* The discriminator column was added in dwarf4. Simplify the below
28876 by simply removing it if we're not supposed to output it. */
28877 if (dwarf_version < 4 && dwarf_strict)
28878 discriminator = 0;
28879
28880 if (!debug_column_info)
28881 column = 0;
28882
28883 file_num = maybe_emit_file (lookup_filename (filename));
28884
28885 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28886 the debugger has used the second (possibly duplicate) line number
28887 at the beginning of the function to mark the end of the prologue.
28888 We could eliminate any other duplicates within the function. For
28889 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28890 that second line number entry. */
28891 /* Recall that this end-of-prologue indication is *not* the same thing
28892 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28893 to which the hook corresponds, follows the last insn that was
28894 emitted by gen_prologue. What we need is to precede the first insn
28895 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28896 insn that corresponds to something the user wrote. These may be
28897 very different locations once scheduling is enabled. */
28898
28899 if (0 && file_num == table->file_num
28900 && line == table->line_num
28901 && column == table->column_num
28902 && discriminator == table->discrim_num
28903 && is_stmt == table->is_stmt)
28904 return;
28905
28906 switch_to_section (current_function_section ());
28907
28908 /* If requested, emit something human-readable. */
28909 if (flag_debug_asm)
28910 {
28911 if (debug_column_info)
28912 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28913 filename, line, column);
28914 else
28915 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28916 filename, line);
28917 }
28918
28919 if (output_asm_line_debug_info ())
28920 {
28921 /* Emit the .loc directive understood by GNU as. */
28922 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28923 file_num, line, is_stmt, discriminator */
28924 fputs ("\t.loc ", asm_out_file);
28925 fprint_ul (asm_out_file, file_num);
28926 putc (' ', asm_out_file);
28927 fprint_ul (asm_out_file, line);
28928 putc (' ', asm_out_file);
28929 fprint_ul (asm_out_file, column);
28930
28931 if (is_stmt != table->is_stmt)
28932 {
28933 #if HAVE_GAS_LOC_STMT
28934 fputs (" is_stmt ", asm_out_file);
28935 putc (is_stmt ? '1' : '0', asm_out_file);
28936 #endif
28937 }
28938 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28939 {
28940 gcc_assert (discriminator > 0);
28941 fputs (" discriminator ", asm_out_file);
28942 fprint_ul (asm_out_file, (unsigned long) discriminator);
28943 }
28944 if (debug_variable_location_views)
28945 {
28946 if (!RESETTING_VIEW_P (table->view))
28947 {
28948 table->symviews_since_reset++;
28949 if (table->symviews_since_reset > symview_upper_bound)
28950 symview_upper_bound = table->symviews_since_reset;
28951 /* When we're using the assembler to compute view
28952 numbers, we output symbolic labels after "view" in
28953 .loc directives, and the assembler will set them for
28954 us, so that we can refer to the view numbers in
28955 location lists. The only exceptions are when we know
28956 a view will be zero: "-0" is a forced reset, used
28957 e.g. in the beginning of functions, whereas "0" tells
28958 the assembler to check that there was a PC change
28959 since the previous view, in a way that implicitly
28960 resets the next view. */
28961 fputs (" view ", asm_out_file);
28962 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28963 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28964 assemble_name (asm_out_file, label);
28965 table->view = ++lvugid;
28966 }
28967 else
28968 {
28969 table->symviews_since_reset = 0;
28970 if (FORCE_RESETTING_VIEW_P (table->view))
28971 fputs (" view -0", asm_out_file);
28972 else
28973 fputs (" view 0", asm_out_file);
28974 /* Mark the present view as a zero view. Earlier debug
28975 binds may have already added its id to loclists to be
28976 emitted later, so we can't reuse the id for something
28977 else. However, it's good to know whether a view is
28978 known to be zero, because then we may be able to
28979 optimize out locviews that are all zeros, so take
28980 note of it in zero_view_p. */
28981 if (!zero_view_p)
28982 zero_view_p = BITMAP_GGC_ALLOC ();
28983 bitmap_set_bit (zero_view_p, lvugid);
28984 table->view = ++lvugid;
28985 }
28986 }
28987 putc ('\n', asm_out_file);
28988 }
28989 else
28990 {
28991 unsigned int label_num = ++line_info_label_num;
28992
28993 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28994
28995 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28996 push_dw_line_info_entry (table, LI_adv_address, label_num);
28997 else
28998 push_dw_line_info_entry (table, LI_set_address, label_num);
28999 if (debug_variable_location_views)
29000 {
29001 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
29002 if (resetting)
29003 table->view = 0;
29004
29005 if (flag_debug_asm)
29006 fprintf (asm_out_file, "\t%s view %s%d\n",
29007 ASM_COMMENT_START,
29008 resetting ? "-" : "",
29009 table->view);
29010
29011 table->view++;
29012 }
29013 if (file_num != table->file_num)
29014 push_dw_line_info_entry (table, LI_set_file, file_num);
29015 if (discriminator != table->discrim_num)
29016 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
29017 if (is_stmt != table->is_stmt)
29018 push_dw_line_info_entry (table, LI_negate_stmt, 0);
29019 push_dw_line_info_entry (table, LI_set_line, line);
29020 if (debug_column_info)
29021 push_dw_line_info_entry (table, LI_set_column, column);
29022 }
29023
29024 table->file_num = file_num;
29025 table->line_num = line;
29026 table->column_num = column;
29027 table->discrim_num = discriminator;
29028 table->is_stmt = is_stmt;
29029 table->in_use = true;
29030 }
29031
29032 /* Record a source file location for a DECL_IGNORED_P function. */
29033
29034 static void
29035 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
29036 const char *filename)
29037 {
29038 dw_fde_ref fde = cfun->fde;
29039
29040 fde->ignored_debug = false;
29041 set_cur_line_info_table (current_function_section ());
29042
29043 dwarf2out_source_line (line, column, filename, 0, true);
29044 }
29045
29046 /* Record the beginning of a new source file. */
29047
29048 static void
29049 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
29050 {
29051 #ifdef CODEVIEW_DEBUGGING_INFO
29052 if (codeview_debuginfo_p ())
29053 codeview_start_source_file (filename);
29054 #endif
29055
29056 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29057 {
29058 macinfo_entry e;
29059 e.code = DW_MACINFO_start_file;
29060 e.lineno = lineno;
29061 e.info = ggc_strdup (filename);
29062 vec_safe_push (macinfo_table, e);
29063 }
29064 }
29065
29066 /* Record the end of a source file. */
29067
29068 static void
29069 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
29070 {
29071 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29072 {
29073 macinfo_entry e;
29074 e.code = DW_MACINFO_end_file;
29075 e.lineno = lineno;
29076 e.info = NULL;
29077 vec_safe_push (macinfo_table, e);
29078 }
29079 }
29080
29081 /* Called from debug_define in toplev.cc. The `buffer' parameter contains
29082 the tail part of the directive line, i.e. the part which is past the
29083 initial whitespace, #, whitespace, directive-name, whitespace part. */
29084
29085 static void
29086 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
29087 const char *buffer ATTRIBUTE_UNUSED)
29088 {
29089 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29090 {
29091 macinfo_entry e;
29092 /* Insert a dummy first entry to be able to optimize the whole
29093 predefined macro block using DW_MACRO_import. */
29094 if (macinfo_table->is_empty () && lineno <= 1)
29095 {
29096 e.code = 0;
29097 e.lineno = 0;
29098 e.info = NULL;
29099 vec_safe_push (macinfo_table, e);
29100 }
29101 e.code = DW_MACINFO_define;
29102 e.lineno = lineno;
29103 e.info = ggc_strdup (buffer);
29104 vec_safe_push (macinfo_table, e);
29105 }
29106 }
29107
29108 /* Called from debug_undef in toplev.cc. The `buffer' parameter contains
29109 the tail part of the directive line, i.e. the part which is past the
29110 initial whitespace, #, whitespace, directive-name, whitespace part. */
29111
29112 static void
29113 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
29114 const char *buffer ATTRIBUTE_UNUSED)
29115 {
29116 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29117 {
29118 macinfo_entry e;
29119 /* Insert a dummy first entry to be able to optimize the whole
29120 predefined macro block using DW_MACRO_import. */
29121 if (macinfo_table->is_empty () && lineno <= 1)
29122 {
29123 e.code = 0;
29124 e.lineno = 0;
29125 e.info = NULL;
29126 vec_safe_push (macinfo_table, e);
29127 }
29128 e.code = DW_MACINFO_undef;
29129 e.lineno = lineno;
29130 e.info = ggc_strdup (buffer);
29131 vec_safe_push (macinfo_table, e);
29132 }
29133 }
29134
29135 /* Helpers to manipulate hash table of CUs. */
29136
29137 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
29138 {
29139 static inline hashval_t hash (const macinfo_entry *);
29140 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
29141 };
29142
29143 inline hashval_t
29144 macinfo_entry_hasher::hash (const macinfo_entry *entry)
29145 {
29146 return htab_hash_string (entry->info);
29147 }
29148
29149 inline bool
29150 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
29151 const macinfo_entry *entry2)
29152 {
29153 return !strcmp (entry1->info, entry2->info);
29154 }
29155
29156 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
29157
29158 /* Output a single .debug_macinfo entry. */
29159
29160 static void
29161 output_macinfo_op (macinfo_entry *ref)
29162 {
29163 int file_num;
29164 size_t len;
29165 struct indirect_string_node *node;
29166 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29167 struct dwarf_file_data *fd;
29168
29169 switch (ref->code)
29170 {
29171 case DW_MACINFO_start_file:
29172 fd = lookup_filename (ref->info);
29173 file_num = maybe_emit_file (fd);
29174 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
29175 dw2_asm_output_data_uleb128 (ref->lineno,
29176 "Included from line number "
29177 HOST_WIDE_INT_PRINT_UNSIGNED,
29178 ref->lineno);
29179 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
29180 break;
29181 case DW_MACINFO_end_file:
29182 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
29183 break;
29184 case DW_MACINFO_define:
29185 case DW_MACINFO_undef:
29186 len = strlen (ref->info) + 1;
29187 if ((!dwarf_strict || dwarf_version >= 5)
29188 && len > (size_t) dwarf_offset_size
29189 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29190 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29191 {
29192 if (dwarf_split_debug_info)
29193 ref->code = ref->code == DW_MACINFO_define
29194 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
29195 else
29196 ref->code = ref->code == DW_MACINFO_define
29197 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
29198 output_macinfo_op (ref);
29199 return;
29200 }
29201 dw2_asm_output_data (1, ref->code,
29202 ref->code == DW_MACINFO_define
29203 ? "Define macro" : "Undefine macro");
29204 dw2_asm_output_data_uleb128 (ref->lineno,
29205 "At line number "
29206 HOST_WIDE_INT_PRINT_UNSIGNED,
29207 ref->lineno);
29208 dw2_asm_output_nstring (ref->info, -1, "The macro");
29209 break;
29210 case DW_MACRO_define_strp:
29211 dw2_asm_output_data (1, ref->code, "Define macro strp");
29212 goto do_DW_MACRO_define_strpx;
29213 case DW_MACRO_undef_strp:
29214 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
29215 goto do_DW_MACRO_define_strpx;
29216 case DW_MACRO_define_strx:
29217 dw2_asm_output_data (1, ref->code, "Define macro strx");
29218 goto do_DW_MACRO_define_strpx;
29219 case DW_MACRO_undef_strx:
29220 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
29221 /* FALLTHRU */
29222 do_DW_MACRO_define_strpx:
29223 /* NB: dwarf2out_finish performs:
29224 1. save_macinfo_strings
29225 2. hash table traverse of index_string
29226 3. output_macinfo -> output_macinfo_op
29227 4. output_indirect_strings
29228 -> hash table traverse of output_index_string
29229
29230 When output_macinfo_op is called, all index strings have been
29231 added to hash table by save_macinfo_strings and we can't pass
29232 INSERT to find_slot_with_hash which may expand hash table, even
29233 if no insertion is needed, and change hash table traverse order
29234 between index_string and output_index_string. */
29235 node = find_AT_string (ref->info, NO_INSERT);
29236 gcc_assert (node
29237 && (node->form == DW_FORM_strp
29238 || node->form == dwarf_FORM (DW_FORM_strx)));
29239 dw2_asm_output_data_uleb128 (ref->lineno,
29240 "At line number "
29241 HOST_WIDE_INT_PRINT_UNSIGNED,
29242 ref->lineno);
29243 if (node->form == DW_FORM_strp)
29244 {
29245 gcc_assert (ref->code == DW_MACRO_define_strp
29246 || ref->code == DW_MACRO_undef_strp);
29247 dw2_asm_output_offset (dwarf_offset_size, node->label,
29248 debug_str_section, "The macro: \"%s\"",
29249 ref->info);
29250 }
29251 else
29252 {
29253 gcc_assert (ref->code == DW_MACRO_define_strx
29254 || ref->code == DW_MACRO_undef_strx);
29255 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
29256 ref->info);
29257 }
29258 break;
29259 case DW_MACRO_import:
29260 dw2_asm_output_data (1, ref->code, "Import");
29261 ASM_GENERATE_INTERNAL_LABEL (label,
29262 DEBUG_MACRO_SECTION_LABEL,
29263 ref->lineno + macinfo_label_base);
29264 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
29265 break;
29266 default:
29267 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
29268 ASM_COMMENT_START, (unsigned long) ref->code);
29269 break;
29270 }
29271 }
29272
29273 /* Attempt to make a sequence of define/undef macinfo ops shareable with
29274 other compilation unit .debug_macinfo sections. IDX is the first
29275 index of a define/undef, return the number of ops that should be
29276 emitted in a comdat .debug_macinfo section and emit
29277 a DW_MACRO_import entry referencing it.
29278 If the define/undef entry should be emitted normally, return 0. */
29279
29280 static unsigned
29281 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
29282 macinfo_hash_type **macinfo_htab)
29283 {
29284 macinfo_entry *first, *second, *cur, *inc;
29285 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
29286 unsigned char checksum[16];
29287 struct md5_ctx ctx;
29288 char *grp_name, *tail;
29289 const char *base;
29290 unsigned int i, count, encoded_filename_len, linebuf_len;
29291 macinfo_entry **slot;
29292
29293 first = &(*macinfo_table)[idx];
29294 second = &(*macinfo_table)[idx + 1];
29295
29296 /* Optimize only if there are at least two consecutive define/undef ops,
29297 and either all of them are before first DW_MACINFO_start_file
29298 with lineno {0,1} (i.e. predefined macro block), or all of them are
29299 in some included header file. */
29300 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
29301 return 0;
29302 if (vec_safe_is_empty (files))
29303 {
29304 if (first->lineno > 1 || second->lineno > 1)
29305 return 0;
29306 }
29307 else if (first->lineno == 0)
29308 return 0;
29309
29310 /* Find the last define/undef entry that can be grouped together
29311 with first and at the same time compute md5 checksum of their
29312 codes, linenumbers and strings. */
29313 md5_init_ctx (&ctx);
29314 for (i = idx; macinfo_table->iterate (i, &cur); i++)
29315 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29316 break;
29317 else if (vec_safe_is_empty (files) && cur->lineno > 1)
29318 break;
29319 else
29320 {
29321 unsigned char code = cur->code;
29322 md5_process_bytes (&code, 1, &ctx);
29323 checksum_uleb128 (cur->lineno, &ctx);
29324 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
29325 }
29326 md5_finish_ctx (&ctx, checksum);
29327 count = i - idx;
29328
29329 /* From the containing include filename (if any) pick up just
29330 usable characters from its basename. */
29331 if (vec_safe_is_empty (files))
29332 base = "";
29333 else
29334 base = lbasename (files->last ().info);
29335 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29336 if (ISIDNUM (base[i]) || base[i] == '.')
29337 encoded_filename_len++;
29338 /* Count . at the end. */
29339 if (encoded_filename_len)
29340 encoded_filename_len++;
29341
29342 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29343 linebuf_len = strlen (linebuf);
29344
29345 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29346 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29347 + 16 * 2 + 1);
29348 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
29349 tail = grp_name + 4;
29350 if (encoded_filename_len)
29351 {
29352 for (i = 0; base[i]; i++)
29353 if (ISIDNUM (base[i]) || base[i] == '.')
29354 *tail++ = base[i];
29355 *tail++ = '.';
29356 }
29357 memcpy (tail, linebuf, linebuf_len);
29358 tail += linebuf_len;
29359 *tail++ = '.';
29360 for (i = 0; i < 16; i++)
29361 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
29362
29363 /* Construct a macinfo_entry for DW_MACRO_import
29364 in the empty vector entry before the first define/undef. */
29365 inc = &(*macinfo_table)[idx - 1];
29366 inc->code = DW_MACRO_import;
29367 inc->lineno = 0;
29368 inc->info = ggc_strdup (grp_name);
29369 if (!*macinfo_htab)
29370 *macinfo_htab = new macinfo_hash_type (10);
29371 /* Avoid emitting duplicates. */
29372 slot = (*macinfo_htab)->find_slot (inc, INSERT);
29373 if (*slot != NULL)
29374 {
29375 inc->code = 0;
29376 inc->info = NULL;
29377 /* If such an entry has been used before, just emit
29378 a DW_MACRO_import op. */
29379 inc = *slot;
29380 output_macinfo_op (inc);
29381 /* And clear all macinfo_entry in the range to avoid emitting them
29382 in the second pass. */
29383 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
29384 {
29385 cur->code = 0;
29386 cur->info = NULL;
29387 }
29388 }
29389 else
29390 {
29391 *slot = inc;
29392 inc->lineno = (*macinfo_htab)->elements ();
29393 output_macinfo_op (inc);
29394 }
29395 return count;
29396 }
29397
29398 /* Save any strings needed by the macinfo table in the debug str
29399 table. All strings must be collected into the table by the time
29400 index_string is called. */
29401
29402 static void
29403 save_macinfo_strings (void)
29404 {
29405 unsigned len;
29406 unsigned i;
29407 macinfo_entry *ref;
29408
29409 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
29410 {
29411 switch (ref->code)
29412 {
29413 /* Match the logic in output_macinfo_op to decide on
29414 indirect strings. */
29415 case DW_MACINFO_define:
29416 case DW_MACINFO_undef:
29417 len = strlen (ref->info) + 1;
29418 if ((!dwarf_strict || dwarf_version >= 5)
29419 && len > (unsigned) dwarf_offset_size
29420 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29421 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29422 set_indirect_string (find_AT_string (ref->info));
29423 break;
29424 case DW_MACINFO_start_file:
29425 /* -gsplit-dwarf -g3 will also output filename as indirect
29426 string. */
29427 if (!dwarf_split_debug_info)
29428 break;
29429 /* Fall through. */
29430 case DW_MACRO_define_strp:
29431 case DW_MACRO_undef_strp:
29432 case DW_MACRO_define_strx:
29433 case DW_MACRO_undef_strx:
29434 set_indirect_string (find_AT_string (ref->info));
29435 break;
29436 default:
29437 break;
29438 }
29439 }
29440 }
29441
29442 /* Output macinfo section(s). */
29443
29444 static void
29445 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29446 {
29447 unsigned i;
29448 unsigned long length = vec_safe_length (macinfo_table);
29449 macinfo_entry *ref;
29450 vec<macinfo_entry, va_gc> *files = NULL;
29451 macinfo_hash_type *macinfo_htab = NULL;
29452 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29453
29454 if (! length)
29455 return;
29456
29457 /* output_macinfo* uses these interchangeably. */
29458 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29459 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29460 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29461 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29462
29463 /* AIX Assembler inserts the length, so adjust the reference to match the
29464 offset expected by debuggers. */
29465 strcpy (dl_section_ref, debug_line_label);
29466 if (XCOFF_DEBUGGING_INFO)
29467 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29468
29469 /* For .debug_macro emit the section header. */
29470 if (!dwarf_strict || dwarf_version >= 5)
29471 {
29472 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29473 "DWARF macro version number");
29474 if (dwarf_offset_size == 8)
29475 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29476 else
29477 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29478 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29479 debug_line_section, NULL);
29480 }
29481
29482 /* In the first loop, it emits the primary .debug_macinfo section
29483 and after each emitted op the macinfo_entry is cleared.
29484 If a longer range of define/undef ops can be optimized using
29485 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29486 the vector before the first define/undef in the range and the
29487 whole range of define/undef ops is not emitted and kept. */
29488 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29489 {
29490 switch (ref->code)
29491 {
29492 case DW_MACINFO_start_file:
29493 vec_safe_push (files, *ref);
29494 break;
29495 case DW_MACINFO_end_file:
29496 if (!vec_safe_is_empty (files))
29497 files->pop ();
29498 break;
29499 case DW_MACINFO_define:
29500 case DW_MACINFO_undef:
29501 if ((!dwarf_strict || dwarf_version >= 5)
29502 && !dwarf_split_debug_info
29503 && HAVE_COMDAT_GROUP
29504 && vec_safe_length (files) != 1
29505 && i > 0
29506 && i + 1 < length
29507 && (*macinfo_table)[i - 1].code == 0)
29508 {
29509 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29510 if (count)
29511 {
29512 i += count - 1;
29513 continue;
29514 }
29515 }
29516 break;
29517 case 0:
29518 /* A dummy entry may be inserted at the beginning to be able
29519 to optimize the whole block of predefined macros. */
29520 if (i == 0)
29521 continue;
29522 default:
29523 break;
29524 }
29525 output_macinfo_op (ref);
29526 ref->info = NULL;
29527 ref->code = 0;
29528 }
29529
29530 if (!macinfo_htab)
29531 return;
29532
29533 /* Save the number of transparent includes so we can adjust the
29534 label number for the fat LTO object DWARF. */
29535 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29536
29537 delete macinfo_htab;
29538 macinfo_htab = NULL;
29539
29540 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29541 terminate the current chain and switch to a new comdat .debug_macinfo
29542 section and emit the define/undef entries within it. */
29543 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29544 switch (ref->code)
29545 {
29546 case 0:
29547 continue;
29548 case DW_MACRO_import:
29549 {
29550 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29551 tree comdat_key = get_identifier (ref->info);
29552 /* Terminate the previous .debug_macinfo section. */
29553 dw2_asm_output_data (1, 0, "End compilation unit");
29554 targetm.asm_out.named_section (debug_macinfo_section_name,
29555 SECTION_DEBUG
29556 | SECTION_LINKONCE
29557 | (early_lto_debug
29558 ? SECTION_EXCLUDE : 0),
29559 comdat_key);
29560 ASM_GENERATE_INTERNAL_LABEL (label,
29561 DEBUG_MACRO_SECTION_LABEL,
29562 ref->lineno + macinfo_label_base);
29563 ASM_OUTPUT_LABEL (asm_out_file, label);
29564 ref->code = 0;
29565 ref->info = NULL;
29566 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29567 "DWARF macro version number");
29568 if (dwarf_offset_size == 8)
29569 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29570 else
29571 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29572 }
29573 break;
29574 case DW_MACINFO_define:
29575 case DW_MACINFO_undef:
29576 output_macinfo_op (ref);
29577 ref->code = 0;
29578 ref->info = NULL;
29579 break;
29580 default:
29581 gcc_unreachable ();
29582 }
29583
29584 macinfo_label_base += macinfo_label_base_adj;
29585 }
29586
29587 /* As init_sections_and_labels may get called multiple times, have a
29588 generation count for labels. */
29589 static unsigned init_sections_and_labels_generation;
29590
29591 /* Initialize the various sections and labels for dwarf output and prefix
29592 them with PREFIX if non-NULL. Returns the generation (zero based
29593 number of times function was called). */
29594
29595 static unsigned
29596 init_sections_and_labels (bool early_lto_debug)
29597 {
29598 if (early_lto_debug)
29599 {
29600 if (!dwarf_split_debug_info)
29601 {
29602 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29603 SECTION_DEBUG | SECTION_EXCLUDE,
29604 NULL);
29605 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29606 SECTION_DEBUG | SECTION_EXCLUDE,
29607 NULL);
29608 debug_macinfo_section_name
29609 = ((dwarf_strict && dwarf_version < 5)
29610 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29611 debug_macinfo_section = get_section (debug_macinfo_section_name,
29612 SECTION_DEBUG
29613 | SECTION_EXCLUDE, NULL);
29614 }
29615 else
29616 {
29617 /* ??? Which of the following do we need early? */
29618 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29619 SECTION_DEBUG | SECTION_EXCLUDE,
29620 NULL);
29621 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29622 SECTION_DEBUG | SECTION_EXCLUDE,
29623 NULL);
29624 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29625 SECTION_DEBUG
29626 | SECTION_EXCLUDE, NULL);
29627 debug_skeleton_abbrev_section
29628 = get_section (DEBUG_LTO_ABBREV_SECTION,
29629 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29630 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29631 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29632 init_sections_and_labels_generation);
29633
29634 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29635 stay in the main .o, but the skeleton_line goes into the split
29636 off dwo. */
29637 debug_skeleton_line_section
29638 = get_section (DEBUG_LTO_LINE_SECTION,
29639 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29640 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29641 DEBUG_SKELETON_LINE_SECTION_LABEL,
29642 init_sections_and_labels_generation);
29643 debug_str_offsets_section
29644 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29645 SECTION_DEBUG | SECTION_EXCLUDE,
29646 NULL);
29647 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29648 DEBUG_SKELETON_INFO_SECTION_LABEL,
29649 init_sections_and_labels_generation);
29650 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29651 DEBUG_STR_DWO_SECTION_FLAGS,
29652 NULL);
29653 debug_macinfo_section_name
29654 = ((dwarf_strict && dwarf_version < 5)
29655 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29656 debug_macinfo_section = get_section (debug_macinfo_section_name,
29657 SECTION_DEBUG | SECTION_EXCLUDE,
29658 NULL);
29659 }
29660 /* For macro info and the file table we have to refer to a
29661 debug_line section. */
29662 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29663 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29664 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29665 DEBUG_LINE_SECTION_LABEL,
29666 init_sections_and_labels_generation);
29667
29668 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29669 DEBUG_STR_SECTION_FLAGS
29670 | SECTION_EXCLUDE, NULL);
29671 if (!dwarf_split_debug_info)
29672 debug_line_str_section
29673 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29674 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29675 }
29676 else
29677 {
29678 if (!dwarf_split_debug_info)
29679 {
29680 debug_info_section = get_section (DEBUG_INFO_SECTION,
29681 SECTION_DEBUG, NULL);
29682 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29683 SECTION_DEBUG, NULL);
29684 debug_loc_section = get_section (dwarf_version >= 5
29685 ? DEBUG_LOCLISTS_SECTION
29686 : DEBUG_LOC_SECTION,
29687 SECTION_DEBUG, NULL);
29688 debug_macinfo_section_name
29689 = ((dwarf_strict && dwarf_version < 5)
29690 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29691 debug_macinfo_section = get_section (debug_macinfo_section_name,
29692 SECTION_DEBUG, NULL);
29693 }
29694 else
29695 {
29696 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29697 SECTION_DEBUG | SECTION_EXCLUDE,
29698 NULL);
29699 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29700 SECTION_DEBUG | SECTION_EXCLUDE,
29701 NULL);
29702 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29703 SECTION_DEBUG, NULL);
29704 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29705 SECTION_DEBUG, NULL);
29706 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29707 SECTION_DEBUG, NULL);
29708 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29709 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29710 init_sections_and_labels_generation);
29711
29712 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29713 stay in the main .o, but the skeleton_line goes into the
29714 split off dwo. */
29715 debug_skeleton_line_section
29716 = get_section (DEBUG_DWO_LINE_SECTION,
29717 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29718 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29719 DEBUG_SKELETON_LINE_SECTION_LABEL,
29720 init_sections_and_labels_generation);
29721 debug_str_offsets_section
29722 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29723 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29724 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29725 DEBUG_SKELETON_INFO_SECTION_LABEL,
29726 init_sections_and_labels_generation);
29727 debug_loc_section = get_section (dwarf_version >= 5
29728 ? DEBUG_DWO_LOCLISTS_SECTION
29729 : DEBUG_DWO_LOC_SECTION,
29730 SECTION_DEBUG | SECTION_EXCLUDE,
29731 NULL);
29732 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29733 DEBUG_STR_DWO_SECTION_FLAGS,
29734 NULL);
29735 debug_macinfo_section_name
29736 = ((dwarf_strict && dwarf_version < 5)
29737 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29738 debug_macinfo_section = get_section (debug_macinfo_section_name,
29739 SECTION_DEBUG | SECTION_EXCLUDE,
29740 NULL);
29741 if (dwarf_version >= 5)
29742 debug_ranges_dwo_section
29743 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29744 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29745 }
29746 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29747 SECTION_DEBUG, NULL);
29748 debug_line_section = get_section (DEBUG_LINE_SECTION,
29749 SECTION_DEBUG, NULL);
29750 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29751 SECTION_DEBUG, NULL);
29752 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29753 SECTION_DEBUG, NULL);
29754 debug_str_section = get_section (DEBUG_STR_SECTION,
29755 DEBUG_STR_SECTION_FLAGS, NULL);
29756 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29757 || asm_outputs_debug_line_str ())
29758 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29759 DEBUG_STR_SECTION_FLAGS, NULL);
29760
29761 debug_ranges_section = get_section (dwarf_version >= 5
29762 ? DEBUG_RNGLISTS_SECTION
29763 : DEBUG_RANGES_SECTION,
29764 SECTION_DEBUG, NULL);
29765 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29766 SECTION_DEBUG, NULL);
29767 }
29768
29769 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29770 DEBUG_ABBREV_SECTION_LABEL,
29771 init_sections_and_labels_generation);
29772 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29773 DEBUG_INFO_SECTION_LABEL,
29774 init_sections_and_labels_generation);
29775 info_section_emitted = false;
29776 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29777 DEBUG_LINE_SECTION_LABEL,
29778 init_sections_and_labels_generation);
29779 /* There are up to 6 unique ranges labels per generation.
29780 See also output_rnglists. */
29781 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29782 DEBUG_RANGES_SECTION_LABEL,
29783 init_sections_and_labels_generation * 6);
29784 if (dwarf_version >= 5 && dwarf_split_debug_info)
29785 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29786 DEBUG_RANGES_SECTION_LABEL,
29787 1 + init_sections_and_labels_generation * 6);
29788 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29789 DEBUG_ADDR_SECTION_LABEL,
29790 init_sections_and_labels_generation);
29791 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29792 (dwarf_strict && dwarf_version < 5)
29793 ? DEBUG_MACINFO_SECTION_LABEL
29794 : DEBUG_MACRO_SECTION_LABEL,
29795 init_sections_and_labels_generation);
29796 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29797 init_sections_and_labels_generation);
29798
29799 ++init_sections_and_labels_generation;
29800 return init_sections_and_labels_generation - 1;
29801 }
29802
29803 /* Set up for Dwarf output at the start of compilation. */
29804
29805 static void
29806 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29807 {
29808 /* Allocate the file_table. */
29809 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29810
29811 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29812 /* Allocate the decl_die_table. */
29813 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29814
29815 /* Allocate the decl_loc_table. */
29816 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29817
29818 /* Allocate the cached_dw_loc_list_table. */
29819 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29820
29821 /* Allocate the initial hunk of the abbrev_die_table. */
29822 vec_alloc (abbrev_die_table, 256);
29823 /* Zero-th entry is allocated, but unused. */
29824 abbrev_die_table->quick_push (NULL);
29825
29826 /* Allocate the dwarf_proc_stack_usage_map. */
29827 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29828
29829 /* Allocate the pubtypes and pubnames vectors. */
29830 vec_alloc (pubname_table, 32);
29831 vec_alloc (pubtype_table, 32);
29832
29833 vec_alloc (incomplete_types, 64);
29834
29835 vec_alloc (used_rtx_array, 32);
29836
29837 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29838 vec_alloc (macinfo_table, 64);
29839 #endif
29840
29841 /* If front-ends already registered a main translation unit but we were not
29842 ready to perform the association, do this now. */
29843 if (main_translation_unit != NULL_TREE)
29844 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29845 }
29846
29847 /* Called before compile () starts outputtting functions, variables
29848 and toplevel asms into assembly. */
29849
29850 static void
29851 dwarf2out_assembly_start (void)
29852 {
29853 if (text_section_line_info)
29854 return;
29855
29856 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29857 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29858 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29859 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29860 COLD_TEXT_SECTION_LABEL, 0);
29861 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29862
29863 switch_to_section (text_section);
29864 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29865 #endif
29866
29867 /* Make sure the line number table for .text always exists. */
29868 text_section_line_info = new_line_info_table ();
29869 text_section_line_info->end_label = text_end_label;
29870
29871 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29872 cur_line_info_table = text_section_line_info;
29873 #endif
29874
29875 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29876 && dwarf2out_do_cfi_asm ()
29877 && !dwarf2out_do_eh_frame ())
29878 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29879
29880 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29881 if (output_asm_line_debug_info () && dwarf_version >= 5)
29882 {
29883 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29884 tell it the comp_dir and main file name for the zero entry
29885 line table. */
29886 const char *comp_dir, *filename0;
29887
29888 comp_dir = comp_dir_string ();
29889 if (comp_dir == NULL)
29890 comp_dir = "";
29891
29892 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29893 if (filename0 == NULL)
29894 filename0 = "";
29895
29896 fprintf (asm_out_file, "\t.file 0 ");
29897 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29898 fputc (' ', asm_out_file);
29899 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29900 fputc ('\n', asm_out_file);
29901 }
29902 else
29903 #endif
29904 /* Work around for PR101575: output a dummy .file directive. */
29905 if (!last_emitted_file && dwarf_debuginfo_p ()
29906 && debug_info_level >= DINFO_LEVEL_TERSE)
29907 {
29908 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29909
29910 if (filename0 == NULL)
29911 filename0 = "<dummy>";
29912 maybe_emit_file (lookup_filename (filename0));
29913 }
29914 }
29915
29916 /* A helper function for dwarf2out_finish called through
29917 htab_traverse. Assign a string its index. All strings must be
29918 collected into the table by the time index_string is called,
29919 because the indexing code relies on htab_traverse to traverse nodes
29920 in the same order for each run. */
29921
29922 int
29923 index_string (indirect_string_node **h, unsigned int *index)
29924 {
29925 indirect_string_node *node = *h;
29926
29927 find_string_form (node);
29928 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29929 {
29930 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29931 node->index = *index;
29932 *index += 1;
29933 }
29934 return 1;
29935 }
29936
29937 /* A helper function for output_indirect_strings called through
29938 htab_traverse. Output the offset to a string and update the
29939 current offset. */
29940
29941 int
29942 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29943 {
29944 indirect_string_node *node = *h;
29945
29946 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29947 {
29948 /* Assert that this node has been assigned an index. */
29949 gcc_assert (node->index != NO_INDEX_ASSIGNED
29950 && node->index != NOT_INDEXED);
29951 dw2_asm_output_data (dwarf_offset_size, *offset,
29952 "indexed string 0x%x: %s", node->index, node->str);
29953 *offset += strlen (node->str) + 1;
29954 }
29955 return 1;
29956 }
29957
29958 /* A helper function for dwarf2out_finish called through
29959 htab_traverse. Output the indexed string. */
29960
29961 int
29962 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29963 {
29964 struct indirect_string_node *node = *h;
29965
29966 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29967 {
29968 /* Assert that the strings are output in the same order as their
29969 indexes were assigned. */
29970 gcc_assert (*cur_idx == node->index);
29971 assemble_string (node->str, strlen (node->str) + 1);
29972 *cur_idx += 1;
29973 }
29974 return 1;
29975 }
29976
29977 /* A helper function for output_indirect_strings. Counts the number
29978 of index strings offsets. Must match the logic of the functions
29979 output_index_string[_offsets] above. */
29980 int
29981 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29982 {
29983 struct indirect_string_node *node = *h;
29984
29985 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29986 *last_idx += 1;
29987 return 1;
29988 }
29989
29990 /* A helper function for dwarf2out_finish called through
29991 htab_traverse. Emit one queued .debug_str string. */
29992
29993 int
29994 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29995 {
29996 struct indirect_string_node *node = *h;
29997
29998 node->form = find_string_form (node);
29999 if (node->form == form && node->refcount > 0)
30000 {
30001 ASM_OUTPUT_LABEL (asm_out_file, node->label);
30002 assemble_string (node->str, strlen (node->str) + 1);
30003 }
30004
30005 return 1;
30006 }
30007
30008 /* Output the indexed string table. */
30009
30010 static void
30011 output_indirect_strings (void)
30012 {
30013 switch_to_section (debug_str_section);
30014 if (!dwarf_split_debug_info)
30015 debug_str_hash->traverse<enum dwarf_form,
30016 output_indirect_string> (DW_FORM_strp);
30017 else
30018 {
30019 unsigned int offset = 0;
30020 unsigned int cur_idx = 0;
30021
30022 if (skeleton_debug_str_hash)
30023 skeleton_debug_str_hash->traverse<enum dwarf_form,
30024 output_indirect_string> (DW_FORM_strp);
30025
30026 switch_to_section (debug_str_offsets_section);
30027 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
30028 header. Note that we don't need to generate a label to the
30029 actual index table following the header here, because this is
30030 for the split dwarf case only. In an .dwo file there is only
30031 one string offsets table (and one debug info section). But
30032 if we would start using string offset tables for the main (or
30033 skeleton) unit, then we have to add a DW_AT_str_offsets_base
30034 pointing to the actual index after the header. Split dwarf
30035 units will never have a string offsets base attribute. When
30036 a split unit is moved into a .dwp file the string offsets can
30037 be found through the .debug_cu_index section table. */
30038 if (dwarf_version >= 5)
30039 {
30040 unsigned int last_idx = 0;
30041 unsigned long str_offsets_length;
30042
30043 debug_str_hash->traverse_noresize
30044 <unsigned int *, count_index_strings> (&last_idx);
30045 str_offsets_length = last_idx * dwarf_offset_size + 4;
30046 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
30047 dw2_asm_output_data (4, 0xffffffff,
30048 "Escape value for 64-bit DWARF extension");
30049 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
30050 "Length of string offsets unit");
30051 dw2_asm_output_data (2, 5, "DWARF string offsets version");
30052 dw2_asm_output_data (2, 0, "Header zero padding");
30053 }
30054 debug_str_hash->traverse_noresize
30055 <unsigned int *, output_index_string_offset> (&offset);
30056 switch_to_section (debug_str_dwo_section);
30057 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
30058 (&cur_idx);
30059 }
30060 }
30061
30062 /* Callback for htab_traverse to assign an index to an entry in the
30063 table, and to write that entry to the .debug_addr section. */
30064
30065 int
30066 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
30067 {
30068 addr_table_entry *entry = *slot;
30069
30070 if (entry->refcount == 0)
30071 {
30072 gcc_assert (entry->index == NO_INDEX_ASSIGNED
30073 || entry->index == NOT_INDEXED);
30074 return 1;
30075 }
30076
30077 gcc_assert (entry->index == *cur_index);
30078 (*cur_index)++;
30079
30080 switch (entry->kind)
30081 {
30082 case ate_kind_rtx:
30083 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
30084 "0x%x", entry->index);
30085 break;
30086 case ate_kind_rtx_dtprel:
30087 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
30088 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
30089 DWARF2_ADDR_SIZE,
30090 entry->addr.rtl);
30091 fputc ('\n', asm_out_file);
30092 break;
30093 case ate_kind_label:
30094 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
30095 "0x%x", entry->index);
30096 break;
30097 default:
30098 gcc_unreachable ();
30099 }
30100 return 1;
30101 }
30102
30103 /* A helper function for dwarf2out_finish. Counts the number
30104 of indexed addresses. Must match the logic of the functions
30105 output_addr_table_entry above. */
30106 int
30107 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
30108 {
30109 addr_table_entry *entry = *slot;
30110
30111 if (entry->refcount > 0)
30112 *last_idx += 1;
30113 return 1;
30114 }
30115
30116 /* Produce the .debug_addr section. */
30117
30118 static void
30119 output_addr_table (void)
30120 {
30121 unsigned int index = 0;
30122 if (addr_index_table == NULL || addr_index_table->size () == 0)
30123 return;
30124
30125 switch_to_section (debug_addr_section);
30126 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
30127 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
30128 before DWARF5, didn't have a header for .debug_addr units.
30129 DWARF5 specifies a small header when address tables are used. */
30130 if (dwarf_version >= 5)
30131 {
30132 unsigned int last_idx = 0;
30133 unsigned long addrs_length;
30134
30135 addr_index_table->traverse_noresize
30136 <unsigned int *, count_index_addrs> (&last_idx);
30137 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
30138
30139 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
30140 dw2_asm_output_data (4, 0xffffffff,
30141 "Escape value for 64-bit DWARF extension");
30142 dw2_asm_output_data (dwarf_offset_size, addrs_length,
30143 "Length of Address Unit");
30144 dw2_asm_output_data (2, 5, "DWARF addr version");
30145 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
30146 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
30147 }
30148 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30149
30150 addr_index_table
30151 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
30152 }
30153
30154 #if ENABLE_ASSERT_CHECKING
30155 /* Verify that all marks are clear. */
30156
30157 static void
30158 verify_marks_clear (dw_die_ref die)
30159 {
30160 dw_die_ref c;
30161
30162 gcc_assert (! die->die_mark);
30163 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
30164 }
30165 #endif /* ENABLE_ASSERT_CHECKING */
30166
30167 /* Clear the marks for a die and its children.
30168 Be cool if the mark isn't set. */
30169
30170 static void
30171 prune_unmark_dies (dw_die_ref die)
30172 {
30173 dw_die_ref c;
30174
30175 if (die->die_mark)
30176 die->die_mark = 0;
30177 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
30178 }
30179
30180 /* Given LOC that is referenced by a DIE we're marking as used, find all
30181 referenced DWARF procedures it references and mark them as used. */
30182
30183 static void
30184 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
30185 {
30186 for (; loc != NULL; loc = loc->dw_loc_next)
30187 switch (loc->dw_loc_opc)
30188 {
30189 case DW_OP_implicit_pointer:
30190 case DW_OP_convert:
30191 case DW_OP_reinterpret:
30192 case DW_OP_GNU_implicit_pointer:
30193 case DW_OP_GNU_convert:
30194 case DW_OP_GNU_reinterpret:
30195 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
30196 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30197 break;
30198 case DW_OP_GNU_variable_value:
30199 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30200 {
30201 dw_die_ref ref
30202 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30203 if (ref == NULL)
30204 break;
30205 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30206 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30207 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30208 }
30209 /* FALLTHRU */
30210 case DW_OP_call2:
30211 case DW_OP_call4:
30212 case DW_OP_call_ref:
30213 case DW_OP_const_type:
30214 case DW_OP_GNU_const_type:
30215 case DW_OP_GNU_parameter_ref:
30216 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
30217 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30218 break;
30219 case DW_OP_regval_type:
30220 case DW_OP_deref_type:
30221 case DW_OP_GNU_regval_type:
30222 case DW_OP_GNU_deref_type:
30223 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
30224 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
30225 break;
30226 case DW_OP_entry_value:
30227 case DW_OP_GNU_entry_value:
30228 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
30229 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
30230 break;
30231 default:
30232 break;
30233 }
30234 }
30235
30236 /* Given DIE that we're marking as used, find any other dies
30237 it references as attributes and mark them as used. */
30238
30239 static void
30240 prune_unused_types_walk_attribs (dw_die_ref die)
30241 {
30242 dw_attr_node *a;
30243 unsigned ix;
30244
30245 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30246 {
30247 switch (AT_class (a))
30248 {
30249 /* Make sure DWARF procedures referenced by location descriptions will
30250 get emitted. */
30251 case dw_val_class_loc:
30252 prune_unused_types_walk_loc_descr (AT_loc (a));
30253 break;
30254 case dw_val_class_loc_list:
30255 for (dw_loc_list_ref list = AT_loc_list (a);
30256 list != NULL;
30257 list = list->dw_loc_next)
30258 prune_unused_types_walk_loc_descr (list->expr);
30259 break;
30260
30261 case dw_val_class_view_list:
30262 /* This points to a loc_list in another attribute, so it's
30263 already covered. */
30264 break;
30265
30266 case dw_val_class_die_ref:
30267 /* A reference to another DIE.
30268 Make sure that it will get emitted.
30269 If it was broken out into a comdat group, don't follow it. */
30270 if (! AT_ref (a)->comdat_type_p
30271 || a->dw_attr == DW_AT_specification)
30272 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
30273 break;
30274
30275 case dw_val_class_str:
30276 /* Set the string's refcount to 0 so that prune_unused_types_mark
30277 accounts properly for it. */
30278 a->dw_attr_val.v.val_str->refcount = 0;
30279 break;
30280
30281 default:
30282 break;
30283 }
30284 }
30285 }
30286
30287 /* Mark the generic parameters and arguments children DIEs of DIE. */
30288
30289 static void
30290 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
30291 {
30292 dw_die_ref c;
30293
30294 if (die == NULL || die->die_child == NULL)
30295 return;
30296 c = die->die_child;
30297 do
30298 {
30299 if (is_template_parameter (c))
30300 prune_unused_types_mark (c, 1);
30301 c = c->die_sib;
30302 } while (c && c != die->die_child);
30303 }
30304
30305 /* Mark DIE as being used. If DOKIDS is true, then walk down
30306 to DIE's children. */
30307
30308 static void
30309 prune_unused_types_mark (dw_die_ref die, int dokids)
30310 {
30311 dw_die_ref c;
30312
30313 if (die->die_mark == 0)
30314 {
30315 /* We haven't done this node yet. Mark it as used. */
30316 die->die_mark = 1;
30317 /* If this is the DIE of a generic type instantiation,
30318 mark the children DIEs that describe its generic parms and
30319 args. */
30320 prune_unused_types_mark_generic_parms_dies (die);
30321
30322 /* We also have to mark its parents as used.
30323 (But we don't want to mark our parent's kids due to this,
30324 unless it is a class.) */
30325 if (die->die_parent)
30326 prune_unused_types_mark (die->die_parent,
30327 class_scope_p (die->die_parent));
30328
30329 /* Mark any referenced nodes. */
30330 prune_unused_types_walk_attribs (die);
30331
30332 /* If this node is a specification,
30333 also mark the definition, if it exists. */
30334 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
30335 prune_unused_types_mark (die->die_definition, 1);
30336 }
30337
30338 if (dokids && die->die_mark != 2)
30339 {
30340 /* We need to walk the children, but haven't done so yet.
30341 Remember that we've walked the kids. */
30342 die->die_mark = 2;
30343
30344 /* If this is an array type, we need to make sure our
30345 kids get marked, even if they're types. If we're
30346 breaking out types into comdat sections, do this
30347 for all type definitions. */
30348 if (die->die_tag == DW_TAG_array_type
30349 || (use_debug_types
30350 && is_type_die (die) && ! is_declaration_die (die)))
30351 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30352 else
30353 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30354 }
30355 }
30356
30357 /* For local classes, look if any static member functions were emitted
30358 and if so, mark them. */
30359
30360 static void
30361 prune_unused_types_walk_local_classes (dw_die_ref die)
30362 {
30363 dw_die_ref c;
30364
30365 if (die->die_mark == 2)
30366 return;
30367
30368 switch (die->die_tag)
30369 {
30370 case DW_TAG_structure_type:
30371 case DW_TAG_union_type:
30372 case DW_TAG_class_type:
30373 case DW_TAG_interface_type:
30374 break;
30375
30376 case DW_TAG_subprogram:
30377 if (!get_AT_flag (die, DW_AT_declaration)
30378 || die->die_definition != NULL)
30379 prune_unused_types_mark (die, 1);
30380 return;
30381
30382 default:
30383 return;
30384 }
30385
30386 /* Mark children. */
30387 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30388 }
30389
30390 /* Walk the tree DIE and mark types that we actually use. */
30391
30392 static void
30393 prune_unused_types_walk (dw_die_ref die)
30394 {
30395 dw_die_ref c;
30396
30397 /* Don't do anything if this node is already marked and
30398 children have been marked as well. */
30399 if (die->die_mark == 2)
30400 return;
30401
30402 switch (die->die_tag)
30403 {
30404 case DW_TAG_structure_type:
30405 case DW_TAG_union_type:
30406 case DW_TAG_class_type:
30407 case DW_TAG_interface_type:
30408 if (die->die_perennial_p)
30409 break;
30410
30411 for (c = die->die_parent; c; c = c->die_parent)
30412 if (c->die_tag == DW_TAG_subprogram)
30413 break;
30414
30415 /* Finding used static member functions inside of classes
30416 is needed just for local classes, because for other classes
30417 static member function DIEs with DW_AT_specification
30418 are emitted outside of the DW_TAG_*_type. If we ever change
30419 it, we'd need to call this even for non-local classes. */
30420 if (c)
30421 prune_unused_types_walk_local_classes (die);
30422
30423 /* It's a type node --- don't mark it. */
30424 return;
30425
30426 case DW_TAG_const_type:
30427 case DW_TAG_packed_type:
30428 case DW_TAG_pointer_type:
30429 case DW_TAG_reference_type:
30430 case DW_TAG_rvalue_reference_type:
30431 case DW_TAG_volatile_type:
30432 case DW_TAG_restrict_type:
30433 case DW_TAG_shared_type:
30434 case DW_TAG_atomic_type:
30435 case DW_TAG_immutable_type:
30436 case DW_TAG_typedef:
30437 case DW_TAG_array_type:
30438 case DW_TAG_coarray_type:
30439 case DW_TAG_friend:
30440 case DW_TAG_enumeration_type:
30441 case DW_TAG_subroutine_type:
30442 case DW_TAG_string_type:
30443 case DW_TAG_set_type:
30444 case DW_TAG_subrange_type:
30445 case DW_TAG_ptr_to_member_type:
30446 case DW_TAG_file_type:
30447 case DW_TAG_unspecified_type:
30448 case DW_TAG_dynamic_type:
30449 /* Type nodes are useful only when other DIEs reference them --- don't
30450 mark them. */
30451 /* FALLTHROUGH */
30452
30453 case DW_TAG_dwarf_procedure:
30454 /* Likewise for DWARF procedures. */
30455
30456 if (die->die_perennial_p)
30457 break;
30458
30459 return;
30460
30461 case DW_TAG_variable:
30462 if (flag_debug_only_used_symbols)
30463 {
30464 if (die->die_perennial_p)
30465 break;
30466
30467 /* For static data members, the declaration in the class is supposed
30468 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30469 DWARF5. DW_TAG_member will be marked, so mark even such
30470 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30471 attribute. */
30472 if (dwarf_version >= 5
30473 && class_scope_p (die->die_parent)
30474 && get_AT (die, DW_AT_const_value))
30475 break;
30476
30477 /* premark_used_variables marks external variables --- don't mark
30478 them here. But function-local externals are always considered
30479 used. */
30480 if (get_AT (die, DW_AT_external))
30481 {
30482 for (c = die->die_parent; c; c = c->die_parent)
30483 if (c->die_tag == DW_TAG_subprogram)
30484 break;
30485 if (!c)
30486 return;
30487 }
30488 }
30489 /* FALLTHROUGH */
30490
30491 default:
30492 /* Mark everything else. */
30493 break;
30494 }
30495
30496 if (die->die_mark == 0)
30497 {
30498 die->die_mark = 1;
30499
30500 /* Now, mark any dies referenced from here. */
30501 prune_unused_types_walk_attribs (die);
30502 }
30503
30504 die->die_mark = 2;
30505
30506 /* Mark children. */
30507 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30508 }
30509
30510 /* Increment the string counts on strings referred to from DIE's
30511 attributes. */
30512
30513 static void
30514 prune_unused_types_update_strings (dw_die_ref die)
30515 {
30516 dw_attr_node *a;
30517 unsigned ix;
30518
30519 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30520 if (AT_class (a) == dw_val_class_str)
30521 {
30522 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30523 s->refcount++;
30524 /* Avoid unnecessarily putting strings that are used less than
30525 twice in the hash table. */
30526 if (s->form != DW_FORM_line_strp
30527 && (s->refcount
30528 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30529 {
30530 indirect_string_node **slot
30531 = debug_str_hash->find_slot_with_hash (s->str,
30532 htab_hash_string (s->str),
30533 INSERT);
30534 gcc_assert (*slot == NULL);
30535 *slot = s;
30536 }
30537 }
30538 }
30539
30540 /* Mark DIE and its children as removed. */
30541
30542 static void
30543 mark_removed (dw_die_ref die)
30544 {
30545 dw_die_ref c;
30546 die->removed = true;
30547 FOR_EACH_CHILD (die, c, mark_removed (c));
30548 }
30549
30550 /* Remove from the tree DIE any dies that aren't marked. */
30551
30552 static void
30553 prune_unused_types_prune (dw_die_ref die)
30554 {
30555 dw_die_ref c;
30556
30557 gcc_assert (die->die_mark);
30558 prune_unused_types_update_strings (die);
30559
30560 if (! die->die_child)
30561 return;
30562
30563 c = die->die_child;
30564 do {
30565 dw_die_ref prev = c, next;
30566 for (c = c->die_sib; ! c->die_mark; c = next)
30567 if (c == die->die_child)
30568 {
30569 /* No marked children between 'prev' and the end of the list. */
30570 if (prev == c)
30571 /* No marked children at all. */
30572 die->die_child = NULL;
30573 else
30574 {
30575 prev->die_sib = c->die_sib;
30576 die->die_child = prev;
30577 }
30578 c->die_sib = NULL;
30579 mark_removed (c);
30580 return;
30581 }
30582 else
30583 {
30584 next = c->die_sib;
30585 c->die_sib = NULL;
30586 mark_removed (c);
30587 }
30588
30589 if (c != prev->die_sib)
30590 prev->die_sib = c;
30591 prune_unused_types_prune (c);
30592 } while (c != die->die_child);
30593 }
30594
30595 /* Remove dies representing declarations that we never use. */
30596
30597 static void
30598 prune_unused_types (void)
30599 {
30600 unsigned int i;
30601 limbo_die_node *node;
30602 comdat_type_node *ctnode;
30603 pubname_entry *pub;
30604 dw_die_ref base_type;
30605
30606 #if ENABLE_ASSERT_CHECKING
30607 /* All the marks should already be clear. */
30608 verify_marks_clear (comp_unit_die ());
30609 for (node = limbo_die_list; node; node = node->next)
30610 verify_marks_clear (node->die);
30611 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30612 verify_marks_clear (ctnode->root_die);
30613 #endif /* ENABLE_ASSERT_CHECKING */
30614
30615 /* Mark types that are used in global variables. */
30616 premark_types_used_by_global_vars ();
30617
30618 /* Mark variables used in the symtab. */
30619 if (flag_debug_only_used_symbols)
30620 premark_used_variables ();
30621
30622 /* Set the mark on nodes that are actually used. */
30623 prune_unused_types_walk (comp_unit_die ());
30624 for (node = limbo_die_list; node; node = node->next)
30625 prune_unused_types_walk (node->die);
30626 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30627 {
30628 prune_unused_types_walk (ctnode->root_die);
30629 prune_unused_types_mark (ctnode->type_die, 1);
30630 }
30631
30632 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30633 are unusual in that they are pubnames that are the children of pubtypes.
30634 They should only be marked via their parent DW_TAG_enumeration_type die,
30635 not as roots in themselves. */
30636 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30637 if (pub->die->die_tag != DW_TAG_enumerator)
30638 prune_unused_types_mark (pub->die, 1);
30639 for (i = 0; base_types.iterate (i, &base_type); i++)
30640 prune_unused_types_mark (base_type, 1);
30641
30642 /* Also set the mark on nodes that could be referenced by
30643 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30644 by DW_TAG_inlined_subroutine origins. */
30645 cgraph_node *cnode;
30646 FOR_EACH_FUNCTION (cnode)
30647 if (cnode->referred_to_p (false))
30648 {
30649 dw_die_ref die = lookup_decl_die (cnode->decl);
30650 if (die == NULL || die->die_mark)
30651 continue;
30652 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30653 if (e->caller != cnode)
30654 {
30655 prune_unused_types_mark (die, 1);
30656 break;
30657 }
30658 }
30659
30660 if (debug_str_hash)
30661 debug_str_hash->empty ();
30662 if (skeleton_debug_str_hash)
30663 skeleton_debug_str_hash->empty ();
30664 prune_unused_types_prune (comp_unit_die ());
30665 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30666 {
30667 node = *pnode;
30668 if (!node->die->die_mark)
30669 *pnode = node->next;
30670 else
30671 {
30672 prune_unused_types_prune (node->die);
30673 pnode = &node->next;
30674 }
30675 }
30676 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30677 prune_unused_types_prune (ctnode->root_die);
30678
30679 /* Leave the marks clear. */
30680 prune_unmark_dies (comp_unit_die ());
30681 for (node = limbo_die_list; node; node = node->next)
30682 prune_unmark_dies (node->die);
30683 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30684 prune_unmark_dies (ctnode->root_die);
30685 }
30686
30687 /* Helpers to manipulate hash table of comdat type units. */
30688
30689 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30690 {
30691 static inline hashval_t hash (const comdat_type_node *);
30692 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30693 };
30694
30695 inline hashval_t
30696 comdat_type_hasher::hash (const comdat_type_node *type_node)
30697 {
30698 hashval_t h;
30699 memcpy (&h, type_node->signature, sizeof (h));
30700 return h;
30701 }
30702
30703 inline bool
30704 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30705 const comdat_type_node *type_node_2)
30706 {
30707 return (! memcmp (type_node_1->signature, type_node_2->signature,
30708 DWARF_TYPE_SIGNATURE_SIZE));
30709 }
30710
30711 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30712 to the location it would have been added, should we know its
30713 DECL_ASSEMBLER_NAME when we added other attributes. This will
30714 probably improve compactness of debug info, removing equivalent
30715 abbrevs, and hide any differences caused by deferring the
30716 computation of the assembler name, triggered by e.g. PCH. */
30717
30718 static inline void
30719 move_linkage_attr (dw_die_ref die)
30720 {
30721 unsigned ix = vec_safe_length (die->die_attr);
30722 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30723
30724 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30725 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30726
30727 while (--ix > 0)
30728 {
30729 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30730
30731 if (prev->dw_attr == DW_AT_decl_line
30732 || prev->dw_attr == DW_AT_decl_column
30733 || prev->dw_attr == DW_AT_name)
30734 break;
30735 }
30736
30737 if (ix != vec_safe_length (die->die_attr) - 1)
30738 {
30739 die->die_attr->pop ();
30740 die->die_attr->quick_insert (ix, linkage);
30741 }
30742 }
30743
30744 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30745 referenced from typed stack ops and count how often they are used. */
30746
30747 static void
30748 mark_base_types (dw_loc_descr_ref loc)
30749 {
30750 dw_die_ref base_type = NULL;
30751
30752 for (; loc; loc = loc->dw_loc_next)
30753 {
30754 switch (loc->dw_loc_opc)
30755 {
30756 case DW_OP_regval_type:
30757 case DW_OP_deref_type:
30758 case DW_OP_GNU_regval_type:
30759 case DW_OP_GNU_deref_type:
30760 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30761 break;
30762 case DW_OP_convert:
30763 case DW_OP_reinterpret:
30764 case DW_OP_GNU_convert:
30765 case DW_OP_GNU_reinterpret:
30766 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30767 continue;
30768 /* FALLTHRU */
30769 case DW_OP_const_type:
30770 case DW_OP_GNU_const_type:
30771 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30772 break;
30773 case DW_OP_entry_value:
30774 case DW_OP_GNU_entry_value:
30775 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30776 continue;
30777 default:
30778 continue;
30779 }
30780 gcc_assert (base_type->die_parent == comp_unit_die ());
30781 if (base_type->die_mark)
30782 base_type->die_mark++;
30783 else
30784 {
30785 base_types.safe_push (base_type);
30786 base_type->die_mark = 1;
30787 }
30788 }
30789 }
30790
30791 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30792 referenced from typed stack ops and count how often they are used. */
30793
30794 static void
30795 mark_base_types (dw_die_ref die)
30796 {
30797 dw_die_ref c;
30798 dw_attr_node *a;
30799 dw_loc_list_ref *curr;
30800 unsigned ix;
30801
30802 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30803 switch (AT_class (a))
30804 {
30805 case dw_val_class_loc_list:
30806 curr = AT_loc_list_ptr (a);
30807 while (*curr)
30808 {
30809 mark_base_types ((*curr)->expr);
30810 curr = &(*curr)->dw_loc_next;
30811 }
30812 break;
30813
30814 case dw_val_class_loc:
30815 mark_base_types (AT_loc (a));
30816 break;
30817
30818 default:
30819 break;
30820 }
30821
30822 FOR_EACH_CHILD (die, c, mark_base_types (c));
30823 }
30824
30825 /* Comparison function for sorting marked base types. */
30826
30827 static int
30828 base_type_cmp (const void *x, const void *y)
30829 {
30830 dw_die_ref dx = *(const dw_die_ref *) x;
30831 dw_die_ref dy = *(const dw_die_ref *) y;
30832 unsigned int byte_size1, byte_size2;
30833 unsigned int encoding1, encoding2;
30834 unsigned int align1, align2;
30835 if (dx->die_mark > dy->die_mark)
30836 return -1;
30837 if (dx->die_mark < dy->die_mark)
30838 return 1;
30839 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30840 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30841 if (byte_size1 < byte_size2)
30842 return 1;
30843 if (byte_size1 > byte_size2)
30844 return -1;
30845 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30846 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30847 if (encoding1 < encoding2)
30848 return 1;
30849 if (encoding1 > encoding2)
30850 return -1;
30851 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30852 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30853 if (align1 < align2)
30854 return 1;
30855 if (align1 > align2)
30856 return -1;
30857 return 0;
30858 }
30859
30860 /* Move base types marked by mark_base_types as early as possible
30861 in the CU, sorted by decreasing usage count both to make the
30862 uleb128 references as small as possible and to make sure they
30863 will have die_offset already computed by calc_die_sizes when
30864 sizes of typed stack loc ops is computed. */
30865
30866 static void
30867 move_marked_base_types (void)
30868 {
30869 unsigned int i;
30870 dw_die_ref base_type, die, c;
30871
30872 if (base_types.is_empty ())
30873 return;
30874
30875 /* Sort by decreasing usage count, they will be added again in that
30876 order later on. */
30877 base_types.qsort (base_type_cmp);
30878 die = comp_unit_die ();
30879 c = die->die_child;
30880 do
30881 {
30882 dw_die_ref prev = c;
30883 c = c->die_sib;
30884 while (c->die_mark)
30885 {
30886 remove_child_with_prev (c, prev);
30887 /* As base types got marked, there must be at least
30888 one node other than DW_TAG_base_type. */
30889 gcc_assert (die->die_child != NULL);
30890 c = prev->die_sib;
30891 }
30892 }
30893 while (c != die->die_child);
30894 gcc_assert (die->die_child);
30895 c = die->die_child;
30896 for (i = 0; base_types.iterate (i, &base_type); i++)
30897 {
30898 base_type->die_mark = 0;
30899 base_type->die_sib = c->die_sib;
30900 c->die_sib = base_type;
30901 c = base_type;
30902 }
30903 }
30904
30905 /* Helper function for resolve_addr, attempt to resolve
30906 one CONST_STRING, return true if successful. Similarly verify that
30907 SYMBOL_REFs refer to variables emitted in the current CU. */
30908
30909 static bool
30910 resolve_one_addr (rtx *addr)
30911 {
30912 rtx rtl = *addr;
30913
30914 if (GET_CODE (rtl) == CONST_STRING)
30915 {
30916 size_t len = strlen (XSTR (rtl, 0)) + 1;
30917 tree t = build_string (len, XSTR (rtl, 0));
30918 tree tlen = size_int (len - 1);
30919 TREE_TYPE (t)
30920 = build_array_type (char_type_node, build_index_type (tlen));
30921 rtl = lookup_constant_def (t);
30922 if (!rtl || !MEM_P (rtl))
30923 return false;
30924 rtl = XEXP (rtl, 0);
30925 if (GET_CODE (rtl) == SYMBOL_REF
30926 && SYMBOL_REF_DECL (rtl)
30927 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30928 return false;
30929 vec_safe_push (used_rtx_array, rtl);
30930 *addr = rtl;
30931 return true;
30932 }
30933
30934 if (GET_CODE (rtl) == SYMBOL_REF
30935 && SYMBOL_REF_DECL (rtl))
30936 {
30937 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30938 {
30939 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30940 return false;
30941 }
30942 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30943 return false;
30944 }
30945
30946 if (GET_CODE (rtl) == CONST)
30947 {
30948 subrtx_ptr_iterator::array_type array;
30949 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30950 if (!resolve_one_addr (*iter))
30951 return false;
30952 }
30953
30954 return true;
30955 }
30956
30957 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30958 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30959 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30960
30961 static rtx
30962 string_cst_pool_decl (tree t)
30963 {
30964 rtx rtl = output_constant_def (t, 1);
30965 unsigned char *array;
30966 dw_loc_descr_ref l;
30967 tree decl;
30968 size_t len;
30969 dw_die_ref ref;
30970
30971 if (!rtl || !MEM_P (rtl))
30972 return NULL_RTX;
30973 rtl = XEXP (rtl, 0);
30974 if (GET_CODE (rtl) != SYMBOL_REF
30975 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30976 return NULL_RTX;
30977
30978 decl = SYMBOL_REF_DECL (rtl);
30979 if (!lookup_decl_die (decl))
30980 {
30981 len = TREE_STRING_LENGTH (t);
30982 vec_safe_push (used_rtx_array, rtl);
30983 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30984 array = ggc_vec_alloc<unsigned char> (len);
30985 memcpy (array, TREE_STRING_POINTER (t), len);
30986 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30987 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30988 l->dw_loc_oprnd2.v.val_vec.length = len;
30989 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30990 l->dw_loc_oprnd2.v.val_vec.array = array;
30991 add_AT_loc (ref, DW_AT_location, l);
30992 equate_decl_number_to_die (decl, ref);
30993 }
30994 return rtl;
30995 }
30996
30997 /* Helper function of resolve_addr_in_expr. LOC is
30998 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30999 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
31000 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
31001 with DW_OP_implicit_pointer if possible
31002 and return true, if unsuccessful, return false. */
31003
31004 static bool
31005 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
31006 {
31007 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
31008 HOST_WIDE_INT offset = 0;
31009 dw_die_ref ref = NULL;
31010 tree decl;
31011
31012 if (GET_CODE (rtl) == CONST
31013 && GET_CODE (XEXP (rtl, 0)) == PLUS
31014 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
31015 {
31016 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
31017 rtl = XEXP (XEXP (rtl, 0), 0);
31018 }
31019 if (GET_CODE (rtl) == CONST_STRING)
31020 {
31021 size_t len = strlen (XSTR (rtl, 0)) + 1;
31022 tree t = build_string (len, XSTR (rtl, 0));
31023 tree tlen = size_int (len - 1);
31024
31025 TREE_TYPE (t)
31026 = build_array_type (char_type_node, build_index_type (tlen));
31027 rtl = string_cst_pool_decl (t);
31028 if (!rtl)
31029 return false;
31030 }
31031 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
31032 {
31033 decl = SYMBOL_REF_DECL (rtl);
31034 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
31035 {
31036 ref = lookup_decl_die (decl);
31037 if (ref && (get_AT (ref, DW_AT_location)
31038 || get_AT (ref, DW_AT_const_value)))
31039 {
31040 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
31041 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31042 loc->dw_loc_oprnd1.val_entry = NULL;
31043 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31044 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31045 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31046 loc->dw_loc_oprnd2.v.val_int = offset;
31047 return true;
31048 }
31049 }
31050 }
31051 return false;
31052 }
31053
31054 /* Helper function for resolve_addr, handle one location
31055 expression, return false if at least one CONST_STRING or SYMBOL_REF in
31056 the location list couldn't be resolved. */
31057
31058 static bool
31059 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31060 {
31061 dw_loc_descr_ref keep = NULL;
31062 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
31063 switch (loc->dw_loc_opc)
31064 {
31065 case DW_OP_addr:
31066 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
31067 {
31068 if ((prev == NULL
31069 || prev->dw_loc_opc == DW_OP_piece
31070 || prev->dw_loc_opc == DW_OP_bit_piece)
31071 && loc->dw_loc_next
31072 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
31073 && (!dwarf_strict || dwarf_version >= 5)
31074 && optimize_one_addr_into_implicit_ptr (loc))
31075 break;
31076 return false;
31077 }
31078 break;
31079 case DW_OP_GNU_addr_index:
31080 case DW_OP_addrx:
31081 case DW_OP_GNU_const_index:
31082 case DW_OP_constx:
31083 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
31084 || loc->dw_loc_opc == DW_OP_addrx)
31085 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
31086 || loc->dw_loc_opc == DW_OP_constx)
31087 && loc->dtprel))
31088 {
31089 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
31090 if (!resolve_one_addr (&rtl))
31091 return false;
31092 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
31093 loc->dw_loc_oprnd1.val_entry
31094 = add_addr_table_entry (rtl, loc->dtprel
31095 ? ate_kind_rtx_dtprel : ate_kind_rtx);
31096 }
31097 break;
31098 case DW_OP_const4u:
31099 case DW_OP_const8u:
31100 if (loc->dtprel
31101 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
31102 return false;
31103 break;
31104 case DW_OP_plus_uconst:
31105 if (size_of_loc_descr (loc)
31106 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
31107 + 1
31108 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
31109 {
31110 dw_loc_descr_ref repl
31111 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
31112 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
31113 add_loc_descr (&repl, loc->dw_loc_next);
31114 *loc = *repl;
31115 }
31116 break;
31117 case DW_OP_implicit_value:
31118 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
31119 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
31120 return false;
31121 break;
31122 case DW_OP_implicit_pointer:
31123 case DW_OP_GNU_implicit_pointer:
31124 case DW_OP_GNU_parameter_ref:
31125 case DW_OP_GNU_variable_value:
31126 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31127 {
31128 dw_die_ref ref
31129 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
31130 if (ref == NULL)
31131 return false;
31132 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31133 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31134 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31135 }
31136 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
31137 {
31138 if (prev == NULL
31139 && loc->dw_loc_next == NULL
31140 && AT_class (a) == dw_val_class_loc)
31141 switch (a->dw_attr)
31142 {
31143 /* Following attributes allow both exprloc and reference,
31144 so if the whole expression is DW_OP_GNU_variable_value
31145 alone we could transform it into reference. */
31146 case DW_AT_byte_size:
31147 case DW_AT_bit_size:
31148 case DW_AT_lower_bound:
31149 case DW_AT_upper_bound:
31150 case DW_AT_bit_stride:
31151 case DW_AT_count:
31152 case DW_AT_allocated:
31153 case DW_AT_associated:
31154 case DW_AT_byte_stride:
31155 a->dw_attr_val.val_class = dw_val_class_die_ref;
31156 a->dw_attr_val.val_entry = NULL;
31157 a->dw_attr_val.v.val_die_ref.die
31158 = loc->dw_loc_oprnd1.v.val_die_ref.die;
31159 a->dw_attr_val.v.val_die_ref.external = 0;
31160 return true;
31161 default:
31162 break;
31163 }
31164 if (dwarf_strict)
31165 return false;
31166 }
31167 break;
31168 case DW_OP_const_type:
31169 case DW_OP_regval_type:
31170 case DW_OP_deref_type:
31171 case DW_OP_convert:
31172 case DW_OP_reinterpret:
31173 case DW_OP_GNU_const_type:
31174 case DW_OP_GNU_regval_type:
31175 case DW_OP_GNU_deref_type:
31176 case DW_OP_GNU_convert:
31177 case DW_OP_GNU_reinterpret:
31178 while (loc->dw_loc_next
31179 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
31180 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
31181 {
31182 dw_die_ref base1, base2;
31183 unsigned enc1, enc2, size1, size2;
31184 if (loc->dw_loc_opc == DW_OP_regval_type
31185 || loc->dw_loc_opc == DW_OP_deref_type
31186 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31187 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31188 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
31189 else if (loc->dw_loc_oprnd1.val_class
31190 == dw_val_class_unsigned_const)
31191 break;
31192 else
31193 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
31194 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
31195 == dw_val_class_unsigned_const)
31196 break;
31197 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
31198 gcc_assert (base1->die_tag == DW_TAG_base_type
31199 && base2->die_tag == DW_TAG_base_type);
31200 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
31201 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
31202 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
31203 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
31204 if (size1 == size2
31205 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
31206 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
31207 && loc != keep)
31208 || enc1 == enc2))
31209 {
31210 /* Optimize away next DW_OP_convert after
31211 adjusting LOC's base type die reference. */
31212 if (loc->dw_loc_opc == DW_OP_regval_type
31213 || loc->dw_loc_opc == DW_OP_deref_type
31214 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31215 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31216 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
31217 else
31218 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
31219 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31220 continue;
31221 }
31222 /* Don't change integer DW_OP_convert after e.g. floating
31223 point typed stack entry. */
31224 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
31225 keep = loc->dw_loc_next;
31226 break;
31227 }
31228 break;
31229 default:
31230 break;
31231 }
31232 return true;
31233 }
31234
31235 /* Helper function of resolve_addr. DIE had DW_AT_location of
31236 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
31237 and DW_OP_addr couldn't be resolved. resolve_addr has already
31238 removed the DW_AT_location attribute. This function attempts to
31239 add a new DW_AT_location attribute with DW_OP_implicit_pointer
31240 to it or DW_AT_const_value attribute, if possible. */
31241
31242 static void
31243 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
31244 {
31245 if (!VAR_P (decl)
31246 || lookup_decl_die (decl) != die
31247 || DECL_EXTERNAL (decl)
31248 || !TREE_STATIC (decl)
31249 || DECL_INITIAL (decl) == NULL_TREE
31250 || DECL_P (DECL_INITIAL (decl))
31251 || get_AT (die, DW_AT_const_value))
31252 return;
31253
31254 tree init = DECL_INITIAL (decl);
31255 HOST_WIDE_INT offset = 0;
31256 /* For variables that have been optimized away and thus
31257 don't have a memory location, see if we can emit
31258 DW_AT_const_value instead. */
31259 if (tree_add_const_value_attribute (die, init))
31260 return;
31261 if (dwarf_strict && dwarf_version < 5)
31262 return;
31263 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
31264 and ADDR_EXPR refers to a decl that has DW_AT_location or
31265 DW_AT_const_value (but isn't addressable, otherwise
31266 resolving the original DW_OP_addr wouldn't fail), see if
31267 we can add DW_OP_implicit_pointer. */
31268 STRIP_NOPS (init);
31269 if (TREE_CODE (init) == POINTER_PLUS_EXPR
31270 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
31271 {
31272 offset = tree_to_shwi (TREE_OPERAND (init, 1));
31273 init = TREE_OPERAND (init, 0);
31274 STRIP_NOPS (init);
31275 }
31276 if (TREE_CODE (init) != ADDR_EXPR)
31277 return;
31278 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
31279 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
31280 || (VAR_P (TREE_OPERAND (init, 0))
31281 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
31282 && TREE_OPERAND (init, 0) != decl))
31283 {
31284 dw_die_ref ref;
31285 dw_loc_descr_ref l;
31286
31287 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
31288 {
31289 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
31290 if (!rtl)
31291 return;
31292 decl = SYMBOL_REF_DECL (rtl);
31293 }
31294 else
31295 decl = TREE_OPERAND (init, 0);
31296 ref = lookup_decl_die (decl);
31297 if (ref == NULL
31298 || (!get_AT (ref, DW_AT_location)
31299 && !get_AT (ref, DW_AT_const_value)))
31300 return;
31301 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
31302 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31303 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
31304 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31305 add_AT_loc (die, DW_AT_location, l);
31306 }
31307 }
31308
31309 /* Return NULL if l is a DWARF expression, or first op that is not
31310 valid DWARF expression. */
31311
31312 static dw_loc_descr_ref
31313 non_dwarf_expression (dw_loc_descr_ref l)
31314 {
31315 while (l)
31316 {
31317 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31318 return l;
31319 switch (l->dw_loc_opc)
31320 {
31321 case DW_OP_regx:
31322 case DW_OP_implicit_value:
31323 case DW_OP_stack_value:
31324 case DW_OP_implicit_pointer:
31325 case DW_OP_GNU_implicit_pointer:
31326 case DW_OP_GNU_parameter_ref:
31327 case DW_OP_piece:
31328 case DW_OP_bit_piece:
31329 return l;
31330 default:
31331 break;
31332 }
31333 l = l->dw_loc_next;
31334 }
31335 return NULL;
31336 }
31337
31338 /* Return adjusted copy of EXPR:
31339 If it is empty DWARF expression, return it.
31340 If it is valid non-empty DWARF expression,
31341 return copy of EXPR with DW_OP_deref appended to it.
31342 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31343 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31344 If it is DWARF expression followed by DW_OP_stack_value, return
31345 copy of the DWARF expression without anything appended.
31346 Otherwise, return NULL. */
31347
31348 static dw_loc_descr_ref
31349 copy_deref_exprloc (dw_loc_descr_ref expr)
31350 {
31351 dw_loc_descr_ref tail = NULL;
31352
31353 if (expr == NULL)
31354 return NULL;
31355
31356 dw_loc_descr_ref l = non_dwarf_expression (expr);
31357 if (l && l->dw_loc_next)
31358 return NULL;
31359
31360 if (l)
31361 {
31362 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31363 tail = new_loc_descr ((enum dwarf_location_atom)
31364 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31365 0, 0);
31366 else
31367 switch (l->dw_loc_opc)
31368 {
31369 case DW_OP_regx:
31370 tail = new_loc_descr (DW_OP_bregx,
31371 l->dw_loc_oprnd1.v.val_unsigned, 0);
31372 break;
31373 case DW_OP_stack_value:
31374 break;
31375 default:
31376 return NULL;
31377 }
31378 }
31379 else
31380 tail = new_loc_descr (DW_OP_deref, 0, 0);
31381
31382 dw_loc_descr_ref ret = NULL, *p = &ret;
31383 while (expr != l)
31384 {
31385 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
31386 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
31387 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
31388 p = &(*p)->dw_loc_next;
31389 expr = expr->dw_loc_next;
31390 }
31391 *p = tail;
31392 return ret;
31393 }
31394
31395 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31396 reference to a variable or argument, adjust it if needed and return:
31397 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31398 attribute if present should be removed
31399 0 keep the attribute perhaps with minor modifications, no need to rescan
31400 1 if the attribute has been successfully adjusted. */
31401
31402 static int
31403 optimize_string_length (dw_attr_node *a)
31404 {
31405 dw_loc_descr_ref l = AT_loc (a), lv;
31406 dw_die_ref die;
31407 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31408 {
31409 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31410 die = lookup_decl_die (decl);
31411 if (die)
31412 {
31413 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31414 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31415 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31416 }
31417 else
31418 return -1;
31419 }
31420 else
31421 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31422
31423 /* DWARF5 allows reference class, so we can then reference the DIE.
31424 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31425 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31426 {
31427 a->dw_attr_val.val_class = dw_val_class_die_ref;
31428 a->dw_attr_val.val_entry = NULL;
31429 a->dw_attr_val.v.val_die_ref.die = die;
31430 a->dw_attr_val.v.val_die_ref.external = 0;
31431 return 0;
31432 }
31433
31434 dw_attr_node *av = get_AT (die, DW_AT_location);
31435 dw_loc_list_ref d;
31436 bool non_dwarf_expr = false;
31437
31438 if (av == NULL)
31439 return dwarf_strict ? -1 : 0;
31440 switch (AT_class (av))
31441 {
31442 case dw_val_class_loc_list:
31443 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31444 if (d->expr && non_dwarf_expression (d->expr))
31445 non_dwarf_expr = true;
31446 break;
31447 case dw_val_class_view_list:
31448 gcc_unreachable ();
31449 case dw_val_class_loc:
31450 lv = AT_loc (av);
31451 if (lv == NULL)
31452 return dwarf_strict ? -1 : 0;
31453 if (non_dwarf_expression (lv))
31454 non_dwarf_expr = true;
31455 break;
31456 default:
31457 return dwarf_strict ? -1 : 0;
31458 }
31459
31460 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31461 into DW_OP_call4 or DW_OP_GNU_variable_value into
31462 DW_OP_call4 DW_OP_deref, do so. */
31463 if (!non_dwarf_expr
31464 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31465 {
31466 l->dw_loc_opc = DW_OP_call4;
31467 if (l->dw_loc_next)
31468 l->dw_loc_next = NULL;
31469 else
31470 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31471 return 0;
31472 }
31473
31474 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31475 copy over the DW_AT_location attribute from die to a. */
31476 if (l->dw_loc_next != NULL)
31477 {
31478 a->dw_attr_val = av->dw_attr_val;
31479 return 1;
31480 }
31481
31482 dw_loc_list_ref list, *p;
31483 switch (AT_class (av))
31484 {
31485 case dw_val_class_loc_list:
31486 p = &list;
31487 list = NULL;
31488 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31489 {
31490 lv = copy_deref_exprloc (d->expr);
31491 if (lv)
31492 {
31493 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31494 p = &(*p)->dw_loc_next;
31495 }
31496 else if (!dwarf_strict && d->expr)
31497 return 0;
31498 }
31499 if (list == NULL)
31500 return dwarf_strict ? -1 : 0;
31501 a->dw_attr_val.val_class = dw_val_class_loc_list;
31502 gen_llsym (list);
31503 *AT_loc_list_ptr (a) = list;
31504 return 1;
31505 case dw_val_class_loc:
31506 lv = copy_deref_exprloc (AT_loc (av));
31507 if (lv == NULL)
31508 return dwarf_strict ? -1 : 0;
31509 a->dw_attr_val.v.val_loc = lv;
31510 return 1;
31511 default:
31512 gcc_unreachable ();
31513 }
31514 }
31515
31516 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31517 an address in .rodata section if the string literal is emitted there,
31518 or remove the containing location list or replace DW_AT_const_value
31519 with DW_AT_location and empty location expression, if it isn't found
31520 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31521 to something that has been emitted in the current CU. */
31522
31523 static void
31524 resolve_addr (dw_die_ref die)
31525 {
31526 dw_die_ref c;
31527 dw_attr_node *a;
31528 dw_loc_list_ref *curr, *start, loc;
31529 unsigned ix;
31530 bool remove_AT_byte_size = false;
31531
31532 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31533 switch (AT_class (a))
31534 {
31535 case dw_val_class_loc_list:
31536 start = curr = AT_loc_list_ptr (a);
31537 loc = *curr;
31538 gcc_assert (loc);
31539 /* The same list can be referenced more than once. See if we have
31540 already recorded the result from a previous pass. */
31541 if (loc->replaced)
31542 *curr = loc->dw_loc_next;
31543 else if (!loc->resolved_addr)
31544 {
31545 /* As things stand, we do not expect or allow one die to
31546 reference a suffix of another die's location list chain.
31547 References must be identical or completely separate.
31548 There is therefore no need to cache the result of this
31549 pass on any list other than the first; doing so
31550 would lead to unnecessary writes. */
31551 while (*curr)
31552 {
31553 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31554 if (!resolve_addr_in_expr (a, (*curr)->expr))
31555 {
31556 dw_loc_list_ref next = (*curr)->dw_loc_next;
31557 dw_loc_descr_ref l = (*curr)->expr;
31558
31559 if (next && (*curr)->ll_symbol)
31560 {
31561 gcc_assert (!next->ll_symbol);
31562 next->ll_symbol = (*curr)->ll_symbol;
31563 next->vl_symbol = (*curr)->vl_symbol;
31564 }
31565 if (dwarf_split_debug_info)
31566 remove_loc_list_addr_table_entries (l);
31567 *curr = next;
31568 }
31569 else
31570 {
31571 mark_base_types ((*curr)->expr);
31572 curr = &(*curr)->dw_loc_next;
31573 }
31574 }
31575 if (loc == *start)
31576 loc->resolved_addr = 1;
31577 else
31578 {
31579 loc->replaced = 1;
31580 loc->dw_loc_next = *start;
31581 }
31582 }
31583 if (!*start)
31584 {
31585 remove_AT (die, a->dw_attr);
31586 ix--;
31587 }
31588 break;
31589 case dw_val_class_view_list:
31590 {
31591 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31592 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31593 dw_val_node *llnode
31594 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31595 /* If we no longer have a loclist, or it no longer needs
31596 views, drop this attribute. */
31597 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31598 {
31599 remove_AT (die, a->dw_attr);
31600 ix--;
31601 }
31602 break;
31603 }
31604 case dw_val_class_loc:
31605 {
31606 dw_loc_descr_ref l = AT_loc (a);
31607 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31608 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31609 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31610 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31611 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31612 with DW_FORM_ref referencing the same DIE as
31613 DW_OP_GNU_variable_value used to reference. */
31614 if (a->dw_attr == DW_AT_string_length
31615 && l
31616 && l->dw_loc_opc == DW_OP_GNU_variable_value
31617 && (l->dw_loc_next == NULL
31618 || (l->dw_loc_next->dw_loc_next == NULL
31619 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31620 {
31621 switch (optimize_string_length (a))
31622 {
31623 case -1:
31624 remove_AT (die, a->dw_attr);
31625 ix--;
31626 /* If we drop DW_AT_string_length, we need to drop also
31627 DW_AT_{string_length_,}byte_size. */
31628 remove_AT_byte_size = true;
31629 continue;
31630 default:
31631 break;
31632 case 1:
31633 /* Even if we keep the optimized DW_AT_string_length,
31634 it might have changed AT_class, so process it again. */
31635 ix--;
31636 continue;
31637 }
31638 }
31639 /* For -gdwarf-2 don't attempt to optimize
31640 DW_AT_data_member_location containing
31641 DW_OP_plus_uconst - older consumers might
31642 rely on it being that op instead of a more complex,
31643 but shorter, location description. */
31644 if ((dwarf_version > 2
31645 || a->dw_attr != DW_AT_data_member_location
31646 || l == NULL
31647 || l->dw_loc_opc != DW_OP_plus_uconst
31648 || l->dw_loc_next != NULL)
31649 && !resolve_addr_in_expr (a, l))
31650 {
31651 if (dwarf_split_debug_info)
31652 remove_loc_list_addr_table_entries (l);
31653 if (l != NULL
31654 && l->dw_loc_next == NULL
31655 && l->dw_loc_opc == DW_OP_addr
31656 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31657 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31658 && a->dw_attr == DW_AT_location)
31659 {
31660 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31661 remove_AT (die, a->dw_attr);
31662 ix--;
31663 optimize_location_into_implicit_ptr (die, decl);
31664 break;
31665 }
31666 if (a->dw_attr == DW_AT_string_length)
31667 /* If we drop DW_AT_string_length, we need to drop also
31668 DW_AT_{string_length_,}byte_size. */
31669 remove_AT_byte_size = true;
31670 remove_AT (die, a->dw_attr);
31671 ix--;
31672 }
31673 else
31674 mark_base_types (l);
31675 }
31676 break;
31677 case dw_val_class_addr:
31678 if (a->dw_attr == DW_AT_const_value
31679 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31680 {
31681 if (AT_index (a) != NOT_INDEXED)
31682 remove_addr_table_entry (a->dw_attr_val.val_entry);
31683 remove_AT (die, a->dw_attr);
31684 ix--;
31685 }
31686 if ((die->die_tag == DW_TAG_call_site
31687 && a->dw_attr == DW_AT_call_origin)
31688 || (die->die_tag == DW_TAG_GNU_call_site
31689 && a->dw_attr == DW_AT_abstract_origin))
31690 {
31691 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31692 dw_die_ref tdie = lookup_decl_die (tdecl);
31693 dw_die_ref cdie;
31694 if (tdie == NULL
31695 && DECL_EXTERNAL (tdecl)
31696 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31697 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31698 {
31699 dw_die_ref pdie = cdie;
31700 /* Make sure we don't add these DIEs into type units.
31701 We could emit skeleton DIEs for context (namespaces,
31702 outer structs/classes) and a skeleton DIE for the
31703 innermost context with DW_AT_signature pointing to the
31704 type unit. See PR78835. */
31705 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31706 pdie = pdie->die_parent;
31707 if (pdie == NULL)
31708 {
31709 /* Creating a full DIE for tdecl is overly expensive and
31710 at this point even wrong when in the LTO phase
31711 as it can end up generating new type DIEs we didn't
31712 output and thus optimize_external_refs will crash. */
31713 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31714 add_AT_flag (tdie, DW_AT_external, 1);
31715 add_AT_flag (tdie, DW_AT_declaration, 1);
31716 add_linkage_attr (tdie, tdecl);
31717 add_name_and_src_coords_attributes (tdie, tdecl, true);
31718 equate_decl_number_to_die (tdecl, tdie);
31719 }
31720 }
31721 if (tdie)
31722 {
31723 a->dw_attr_val.val_class = dw_val_class_die_ref;
31724 a->dw_attr_val.v.val_die_ref.die = tdie;
31725 a->dw_attr_val.v.val_die_ref.external = 0;
31726 }
31727 else
31728 {
31729 if (AT_index (a) != NOT_INDEXED)
31730 remove_addr_table_entry (a->dw_attr_val.val_entry);
31731 remove_AT (die, a->dw_attr);
31732 ix--;
31733 }
31734 }
31735 break;
31736 default:
31737 break;
31738 }
31739
31740 if (remove_AT_byte_size)
31741 remove_AT (die, dwarf_version >= 5
31742 ? DW_AT_string_length_byte_size
31743 : DW_AT_byte_size);
31744
31745 FOR_EACH_CHILD (die, c, resolve_addr (c));
31746 }
31747
31748 /* Helper routines for optimize_location_lists.
31749 This pass tries to share identical local lists in .debug_loc
31750 section. */
31751
31752 /* Iteratively hash operands of LOC opcode into HSTATE. */
31753
31754 static void
31755 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31756 {
31757 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31758 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31759
31760 switch (loc->dw_loc_opc)
31761 {
31762 case DW_OP_const4u:
31763 case DW_OP_const8u:
31764 if (loc->dtprel)
31765 goto hash_addr;
31766 /* FALLTHRU */
31767 case DW_OP_const1u:
31768 case DW_OP_const1s:
31769 case DW_OP_const2u:
31770 case DW_OP_const2s:
31771 case DW_OP_const4s:
31772 case DW_OP_const8s:
31773 case DW_OP_constu:
31774 case DW_OP_consts:
31775 case DW_OP_pick:
31776 case DW_OP_plus_uconst:
31777 case DW_OP_breg0:
31778 case DW_OP_breg1:
31779 case DW_OP_breg2:
31780 case DW_OP_breg3:
31781 case DW_OP_breg4:
31782 case DW_OP_breg5:
31783 case DW_OP_breg6:
31784 case DW_OP_breg7:
31785 case DW_OP_breg8:
31786 case DW_OP_breg9:
31787 case DW_OP_breg10:
31788 case DW_OP_breg11:
31789 case DW_OP_breg12:
31790 case DW_OP_breg13:
31791 case DW_OP_breg14:
31792 case DW_OP_breg15:
31793 case DW_OP_breg16:
31794 case DW_OP_breg17:
31795 case DW_OP_breg18:
31796 case DW_OP_breg19:
31797 case DW_OP_breg20:
31798 case DW_OP_breg21:
31799 case DW_OP_breg22:
31800 case DW_OP_breg23:
31801 case DW_OP_breg24:
31802 case DW_OP_breg25:
31803 case DW_OP_breg26:
31804 case DW_OP_breg27:
31805 case DW_OP_breg28:
31806 case DW_OP_breg29:
31807 case DW_OP_breg30:
31808 case DW_OP_breg31:
31809 case DW_OP_regx:
31810 case DW_OP_fbreg:
31811 case DW_OP_piece:
31812 case DW_OP_deref_size:
31813 case DW_OP_xderef_size:
31814 hstate.add_object (val1->v.val_int);
31815 break;
31816 case DW_OP_skip:
31817 case DW_OP_bra:
31818 {
31819 int offset;
31820
31821 gcc_assert (val1->val_class == dw_val_class_loc);
31822 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31823 hstate.add_object (offset);
31824 }
31825 break;
31826 case DW_OP_implicit_value:
31827 hstate.add_object (val1->v.val_unsigned);
31828 switch (val2->val_class)
31829 {
31830 case dw_val_class_const:
31831 hstate.add_object (val2->v.val_int);
31832 break;
31833 case dw_val_class_vec:
31834 {
31835 unsigned int elt_size = val2->v.val_vec.elt_size;
31836 unsigned int len = val2->v.val_vec.length;
31837
31838 hstate.add_int (elt_size);
31839 hstate.add_int (len);
31840 hstate.add (val2->v.val_vec.array, len * elt_size);
31841 }
31842 break;
31843 case dw_val_class_const_double:
31844 hstate.add_object (val2->v.val_double.low);
31845 hstate.add_object (val2->v.val_double.high);
31846 break;
31847 case dw_val_class_wide_int:
31848 hstate.add (val2->v.val_wide->get_val (),
31849 get_full_len (*val2->v.val_wide)
31850 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31851 break;
31852 case dw_val_class_addr:
31853 inchash::add_rtx (val2->v.val_addr, hstate);
31854 break;
31855 default:
31856 gcc_unreachable ();
31857 }
31858 break;
31859 case DW_OP_bregx:
31860 case DW_OP_bit_piece:
31861 hstate.add_object (val1->v.val_int);
31862 hstate.add_object (val2->v.val_int);
31863 break;
31864 case DW_OP_addr:
31865 hash_addr:
31866 if (loc->dtprel)
31867 {
31868 unsigned char dtprel = 0xd1;
31869 hstate.add_object (dtprel);
31870 }
31871 inchash::add_rtx (val1->v.val_addr, hstate);
31872 break;
31873 case DW_OP_GNU_addr_index:
31874 case DW_OP_addrx:
31875 case DW_OP_GNU_const_index:
31876 case DW_OP_constx:
31877 {
31878 if (loc->dtprel)
31879 {
31880 unsigned char dtprel = 0xd1;
31881 hstate.add_object (dtprel);
31882 }
31883 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31884 }
31885 break;
31886 case DW_OP_implicit_pointer:
31887 case DW_OP_GNU_implicit_pointer:
31888 hstate.add_int (val2->v.val_int);
31889 break;
31890 case DW_OP_entry_value:
31891 case DW_OP_GNU_entry_value:
31892 hstate.add_object (val1->v.val_loc);
31893 break;
31894 case DW_OP_regval_type:
31895 case DW_OP_deref_type:
31896 case DW_OP_GNU_regval_type:
31897 case DW_OP_GNU_deref_type:
31898 {
31899 unsigned int byte_size
31900 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31901 unsigned int encoding
31902 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31903 hstate.add_object (val1->v.val_int);
31904 hstate.add_object (byte_size);
31905 hstate.add_object (encoding);
31906 }
31907 break;
31908 case DW_OP_convert:
31909 case DW_OP_reinterpret:
31910 case DW_OP_GNU_convert:
31911 case DW_OP_GNU_reinterpret:
31912 if (val1->val_class == dw_val_class_unsigned_const)
31913 {
31914 hstate.add_object (val1->v.val_unsigned);
31915 break;
31916 }
31917 /* FALLTHRU */
31918 case DW_OP_const_type:
31919 case DW_OP_GNU_const_type:
31920 {
31921 unsigned int byte_size
31922 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31923 unsigned int encoding
31924 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31925 hstate.add_object (byte_size);
31926 hstate.add_object (encoding);
31927 if (loc->dw_loc_opc != DW_OP_const_type
31928 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31929 break;
31930 hstate.add_object (val2->val_class);
31931 switch (val2->val_class)
31932 {
31933 case dw_val_class_const:
31934 hstate.add_object (val2->v.val_int);
31935 break;
31936 case dw_val_class_vec:
31937 {
31938 unsigned int elt_size = val2->v.val_vec.elt_size;
31939 unsigned int len = val2->v.val_vec.length;
31940
31941 hstate.add_object (elt_size);
31942 hstate.add_object (len);
31943 hstate.add (val2->v.val_vec.array, len * elt_size);
31944 }
31945 break;
31946 case dw_val_class_const_double:
31947 hstate.add_object (val2->v.val_double.low);
31948 hstate.add_object (val2->v.val_double.high);
31949 break;
31950 case dw_val_class_wide_int:
31951 hstate.add (val2->v.val_wide->get_val (),
31952 get_full_len (*val2->v.val_wide)
31953 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31954 break;
31955 default:
31956 gcc_unreachable ();
31957 }
31958 }
31959 break;
31960
31961 default:
31962 /* Other codes have no operands. */
31963 break;
31964 }
31965 }
31966
31967 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31968
31969 static inline void
31970 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31971 {
31972 dw_loc_descr_ref l;
31973 bool sizes_computed = false;
31974 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31975 size_of_locs (loc);
31976
31977 for (l = loc; l != NULL; l = l->dw_loc_next)
31978 {
31979 enum dwarf_location_atom opc = l->dw_loc_opc;
31980 hstate.add_object (opc);
31981 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31982 {
31983 size_of_locs (loc);
31984 sizes_computed = true;
31985 }
31986 hash_loc_operands (l, hstate);
31987 }
31988 }
31989
31990 /* Compute hash of the whole location list LIST_HEAD. */
31991
31992 static inline void
31993 hash_loc_list (dw_loc_list_ref list_head)
31994 {
31995 dw_loc_list_ref curr = list_head;
31996 inchash::hash hstate;
31997
31998 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31999 {
32000 hstate.add (curr->begin, strlen (curr->begin) + 1);
32001 hstate.add (curr->end, strlen (curr->end) + 1);
32002 hstate.add_object (curr->vbegin);
32003 hstate.add_object (curr->vend);
32004 if (curr->section)
32005 hstate.add (curr->section, strlen (curr->section) + 1);
32006 hash_locs (curr->expr, hstate);
32007 }
32008 list_head->hash = hstate.end ();
32009 }
32010
32011 /* Return true if X and Y opcodes have the same operands. */
32012
32013 static inline bool
32014 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
32015 {
32016 dw_val_ref valx1 = &x->dw_loc_oprnd1;
32017 dw_val_ref valx2 = &x->dw_loc_oprnd2;
32018 dw_val_ref valy1 = &y->dw_loc_oprnd1;
32019 dw_val_ref valy2 = &y->dw_loc_oprnd2;
32020
32021 switch (x->dw_loc_opc)
32022 {
32023 case DW_OP_const4u:
32024 case DW_OP_const8u:
32025 if (x->dtprel)
32026 goto hash_addr;
32027 /* FALLTHRU */
32028 case DW_OP_const1u:
32029 case DW_OP_const1s:
32030 case DW_OP_const2u:
32031 case DW_OP_const2s:
32032 case DW_OP_const4s:
32033 case DW_OP_const8s:
32034 case DW_OP_constu:
32035 case DW_OP_consts:
32036 case DW_OP_pick:
32037 case DW_OP_plus_uconst:
32038 case DW_OP_breg0:
32039 case DW_OP_breg1:
32040 case DW_OP_breg2:
32041 case DW_OP_breg3:
32042 case DW_OP_breg4:
32043 case DW_OP_breg5:
32044 case DW_OP_breg6:
32045 case DW_OP_breg7:
32046 case DW_OP_breg8:
32047 case DW_OP_breg9:
32048 case DW_OP_breg10:
32049 case DW_OP_breg11:
32050 case DW_OP_breg12:
32051 case DW_OP_breg13:
32052 case DW_OP_breg14:
32053 case DW_OP_breg15:
32054 case DW_OP_breg16:
32055 case DW_OP_breg17:
32056 case DW_OP_breg18:
32057 case DW_OP_breg19:
32058 case DW_OP_breg20:
32059 case DW_OP_breg21:
32060 case DW_OP_breg22:
32061 case DW_OP_breg23:
32062 case DW_OP_breg24:
32063 case DW_OP_breg25:
32064 case DW_OP_breg26:
32065 case DW_OP_breg27:
32066 case DW_OP_breg28:
32067 case DW_OP_breg29:
32068 case DW_OP_breg30:
32069 case DW_OP_breg31:
32070 case DW_OP_regx:
32071 case DW_OP_fbreg:
32072 case DW_OP_piece:
32073 case DW_OP_deref_size:
32074 case DW_OP_xderef_size:
32075 return valx1->v.val_int == valy1->v.val_int;
32076 case DW_OP_skip:
32077 case DW_OP_bra:
32078 /* If splitting debug info, the use of DW_OP_GNU_addr_index
32079 can cause irrelevant differences in dw_loc_addr. */
32080 gcc_assert (valx1->val_class == dw_val_class_loc
32081 && valy1->val_class == dw_val_class_loc
32082 && (dwarf_split_debug_info
32083 || x->dw_loc_addr == y->dw_loc_addr));
32084 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
32085 case DW_OP_implicit_value:
32086 if (valx1->v.val_unsigned != valy1->v.val_unsigned
32087 || valx2->val_class != valy2->val_class)
32088 return false;
32089 switch (valx2->val_class)
32090 {
32091 case dw_val_class_const:
32092 return valx2->v.val_int == valy2->v.val_int;
32093 case dw_val_class_vec:
32094 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
32095 && valx2->v.val_vec.length == valy2->v.val_vec.length
32096 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
32097 valx2->v.val_vec.elt_size
32098 * valx2->v.val_vec.length) == 0;
32099 case dw_val_class_const_double:
32100 return valx2->v.val_double.low == valy2->v.val_double.low
32101 && valx2->v.val_double.high == valy2->v.val_double.high;
32102 case dw_val_class_wide_int:
32103 return *valx2->v.val_wide == *valy2->v.val_wide;
32104 case dw_val_class_addr:
32105 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
32106 default:
32107 gcc_unreachable ();
32108 }
32109 case DW_OP_bregx:
32110 case DW_OP_bit_piece:
32111 return valx1->v.val_int == valy1->v.val_int
32112 && valx2->v.val_int == valy2->v.val_int;
32113 case DW_OP_addr:
32114 hash_addr:
32115 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
32116 case DW_OP_GNU_addr_index:
32117 case DW_OP_addrx:
32118 case DW_OP_GNU_const_index:
32119 case DW_OP_constx:
32120 {
32121 rtx ax1 = valx1->val_entry->addr.rtl;
32122 rtx ay1 = valy1->val_entry->addr.rtl;
32123 return rtx_equal_p (ax1, ay1);
32124 }
32125 case DW_OP_implicit_pointer:
32126 case DW_OP_GNU_implicit_pointer:
32127 return valx1->val_class == dw_val_class_die_ref
32128 && valx1->val_class == valy1->val_class
32129 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
32130 && valx2->v.val_int == valy2->v.val_int;
32131 case DW_OP_entry_value:
32132 case DW_OP_GNU_entry_value:
32133 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
32134 case DW_OP_const_type:
32135 case DW_OP_GNU_const_type:
32136 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
32137 || valx2->val_class != valy2->val_class)
32138 return false;
32139 switch (valx2->val_class)
32140 {
32141 case dw_val_class_const:
32142 return valx2->v.val_int == valy2->v.val_int;
32143 case dw_val_class_vec:
32144 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
32145 && valx2->v.val_vec.length == valy2->v.val_vec.length
32146 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
32147 valx2->v.val_vec.elt_size
32148 * valx2->v.val_vec.length) == 0;
32149 case dw_val_class_const_double:
32150 return valx2->v.val_double.low == valy2->v.val_double.low
32151 && valx2->v.val_double.high == valy2->v.val_double.high;
32152 case dw_val_class_wide_int:
32153 return *valx2->v.val_wide == *valy2->v.val_wide;
32154 default:
32155 gcc_unreachable ();
32156 }
32157 case DW_OP_regval_type:
32158 case DW_OP_deref_type:
32159 case DW_OP_GNU_regval_type:
32160 case DW_OP_GNU_deref_type:
32161 return valx1->v.val_int == valy1->v.val_int
32162 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
32163 case DW_OP_convert:
32164 case DW_OP_reinterpret:
32165 case DW_OP_GNU_convert:
32166 case DW_OP_GNU_reinterpret:
32167 if (valx1->val_class != valy1->val_class)
32168 return false;
32169 if (valx1->val_class == dw_val_class_unsigned_const)
32170 return valx1->v.val_unsigned == valy1->v.val_unsigned;
32171 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
32172 case DW_OP_GNU_parameter_ref:
32173 return valx1->val_class == dw_val_class_die_ref
32174 && valx1->val_class == valy1->val_class
32175 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
32176 default:
32177 /* Other codes have no operands. */
32178 return true;
32179 }
32180 }
32181
32182 /* Return true if DWARF location expressions X and Y are the same. */
32183
32184 static inline bool
32185 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
32186 {
32187 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
32188 if (x->dw_loc_opc != y->dw_loc_opc
32189 || x->dtprel != y->dtprel
32190 || !compare_loc_operands (x, y))
32191 break;
32192 return x == NULL && y == NULL;
32193 }
32194
32195 /* Hashtable helpers. */
32196
32197 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
32198 {
32199 static inline hashval_t hash (const dw_loc_list_struct *);
32200 static inline bool equal (const dw_loc_list_struct *,
32201 const dw_loc_list_struct *);
32202 };
32203
32204 /* Return precomputed hash of location list X. */
32205
32206 inline hashval_t
32207 loc_list_hasher::hash (const dw_loc_list_struct *x)
32208 {
32209 return x->hash;
32210 }
32211
32212 /* Return true if location lists A and B are the same. */
32213
32214 inline bool
32215 loc_list_hasher::equal (const dw_loc_list_struct *a,
32216 const dw_loc_list_struct *b)
32217 {
32218 if (a == b)
32219 return true;
32220 if (a->hash != b->hash)
32221 return false;
32222 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
32223 if (strcmp (a->begin, b->begin) != 0
32224 || strcmp (a->end, b->end) != 0
32225 || (a->section == NULL) != (b->section == NULL)
32226 || (a->section && strcmp (a->section, b->section) != 0)
32227 || a->vbegin != b->vbegin || a->vend != b->vend
32228 || !compare_locs (a->expr, b->expr))
32229 break;
32230 return a == NULL && b == NULL;
32231 }
32232
32233 typedef hash_table<loc_list_hasher> loc_list_hash_type;
32234
32235
32236 /* Recursively optimize location lists referenced from DIE
32237 children and share them whenever possible. */
32238
32239 static void
32240 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
32241 {
32242 dw_die_ref c;
32243 dw_attr_node *a;
32244 unsigned ix;
32245 dw_loc_list_struct **slot;
32246 bool drop_locviews = false;
32247 bool has_locviews = false;
32248
32249 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32250 if (AT_class (a) == dw_val_class_loc_list)
32251 {
32252 dw_loc_list_ref list = AT_loc_list (a);
32253 /* TODO: perform some optimizations here, before hashing
32254 it and storing into the hash table. */
32255 hash_loc_list (list);
32256 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
32257 if (*slot == NULL)
32258 {
32259 *slot = list;
32260 if (loc_list_has_views (list))
32261 gcc_assert (list->vl_symbol);
32262 else if (list->vl_symbol)
32263 {
32264 drop_locviews = true;
32265 list->vl_symbol = NULL;
32266 }
32267 }
32268 else
32269 {
32270 if (list->vl_symbol && !(*slot)->vl_symbol)
32271 drop_locviews = true;
32272 a->dw_attr_val.v.val_loc_list = *slot;
32273 }
32274 }
32275 else if (AT_class (a) == dw_val_class_view_list)
32276 {
32277 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
32278 has_locviews = true;
32279 }
32280
32281
32282 if (drop_locviews && has_locviews)
32283 remove_AT (die, DW_AT_GNU_locviews);
32284
32285 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
32286 }
32287
32288
32289 /* Recursively assign each location list a unique index into the debug_addr
32290 section. */
32291
32292 static void
32293 index_location_lists (dw_die_ref die)
32294 {
32295 dw_die_ref c;
32296 dw_attr_node *a;
32297 unsigned ix;
32298
32299 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32300 if (AT_class (a) == dw_val_class_loc_list)
32301 {
32302 dw_loc_list_ref list = AT_loc_list (a);
32303 dw_loc_list_ref curr;
32304 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
32305 {
32306 /* Don't index an entry that has already been indexed
32307 or won't be output. Make sure skip_loc_list_entry doesn't
32308 call size_of_locs, because that might cause circular dependency,
32309 index_location_lists requiring address table indexes to be
32310 computed, but adding new indexes through add_addr_table_entry
32311 and address table index computation requiring no new additions
32312 to the hash table. In the rare case of DWARF[234] >= 64KB
32313 location expression, we'll just waste unused address table entry
32314 for it. */
32315 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
32316 continue;
32317
32318 curr->begin_entry
32319 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
32320 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
32321 curr->end_entry
32322 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
32323 }
32324 }
32325
32326 FOR_EACH_CHILD (die, c, index_location_lists (c));
32327 }
32328
32329 /* Optimize location lists referenced from DIE
32330 children and share them whenever possible. */
32331
32332 static void
32333 optimize_location_lists (dw_die_ref die)
32334 {
32335 loc_list_hash_type htab (500);
32336 optimize_location_lists_1 (die, &htab);
32337 }
32338
32339 /* Traverse the limbo die list, and add parent/child links. The only
32340 dies without parents that should be here are concrete instances of
32341 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32342 For concrete instances, we can get the parent die from the abstract
32343 instance. */
32344
32345 static void
32346 flush_limbo_die_list (void)
32347 {
32348 limbo_die_node *node;
32349
32350 /* get_context_die calls force_decl_die, which can put new DIEs on the
32351 limbo list in LTO mode when nested functions are put in a different
32352 partition than that of their parent function. */
32353 while ((node = limbo_die_list))
32354 {
32355 dw_die_ref die = node->die;
32356 limbo_die_list = node->next;
32357
32358 if (die->die_parent == NULL)
32359 {
32360 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
32361
32362 if (origin && origin->die_parent)
32363 add_child_die (origin->die_parent, die);
32364 else if (is_cu_die (die))
32365 ;
32366 else if (seen_error ())
32367 /* It's OK to be confused by errors in the input. */
32368 add_child_die (comp_unit_die (), die);
32369 else
32370 {
32371 /* In certain situations, the lexical block containing a
32372 nested function can be optimized away, which results
32373 in the nested function die being orphaned. Likewise
32374 with the return type of that nested function. Force
32375 this to be a child of the containing function.
32376
32377 It may happen that even the containing function got fully
32378 inlined and optimized out. In that case we are lost and
32379 assign the empty child. This should not be big issue as
32380 the function is likely unreachable too. */
32381 gcc_assert (node->created_for);
32382
32383 if (DECL_P (node->created_for))
32384 origin = get_context_die (DECL_CONTEXT (node->created_for));
32385 else if (TYPE_P (node->created_for))
32386 origin = scope_die_for (node->created_for, comp_unit_die ());
32387 else
32388 origin = comp_unit_die ();
32389
32390 add_child_die (origin, die);
32391 }
32392 }
32393 }
32394 }
32395
32396 /* Reset DIEs so we can output them again. */
32397
32398 static void
32399 reset_dies (dw_die_ref die)
32400 {
32401 dw_die_ref c;
32402
32403 /* Remove stuff we re-generate. */
32404 die->die_mark = 0;
32405 die->die_offset = 0;
32406 die->die_abbrev = 0;
32407 remove_AT (die, DW_AT_sibling);
32408
32409 FOR_EACH_CHILD (die, c, reset_dies (c));
32410 }
32411
32412 /* reset_indirect_string removed the references coming from DW_AT_name
32413 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32414 .debug_line_str strings again. */
32415
32416 static void
32417 adjust_name_comp_dir (dw_die_ref die)
32418 {
32419 for (int i = 0; i < 2; i++)
32420 {
32421 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32422 dw_attr_node *a = get_AT (die, attr_kind);
32423 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32424 continue;
32425
32426 if (!debug_line_str_hash)
32427 debug_line_str_hash
32428 = hash_table<indirect_string_hasher>::create_ggc (10);
32429
32430 struct indirect_string_node *node
32431 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
32432 debug_line_str_hash);
32433 set_indirect_string (node);
32434 node->form = DW_FORM_line_strp;
32435 a->dw_attr_val.v.val_str = node;
32436 }
32437 }
32438
32439 /* Output stuff that dwarf requires at the end of every file,
32440 and generate the DWARF-2 debugging info. */
32441
32442 static void
32443 dwarf2out_finish (const char *filename)
32444 {
32445 comdat_type_node *ctnode;
32446 dw_die_ref main_comp_unit_die;
32447 unsigned char checksum[16];
32448 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32449
32450 /* Generate CTF/BTF debug info. */
32451 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32452 || btf_debuginfo_p ()) && lang_GNU_C ())
32453 ctf_debug_finish ();
32454
32455 #ifdef CODEVIEW_DEBUGGING_INFO
32456 if (codeview_debuginfo_p ())
32457 codeview_debug_finish ();
32458 #endif
32459
32460 /* Skip emitting DWARF if not required. */
32461 if (!dwarf_debuginfo_p ())
32462 return;
32463
32464 /* Flush out any latecomers to the limbo party. */
32465 flush_limbo_die_list ();
32466
32467 if (inline_entry_data_table)
32468 gcc_assert (inline_entry_data_table->is_empty ());
32469
32470 if (flag_checking)
32471 {
32472 verify_die (comp_unit_die ());
32473 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32474 verify_die (node->die);
32475 }
32476
32477 /* We shouldn't have any symbols with delayed asm names for
32478 DIEs generated after early finish. */
32479 gcc_assert (deferred_asm_name == NULL);
32480
32481 gen_remaining_tmpl_value_param_die_attribute ();
32482
32483 if (flag_generate_lto || flag_generate_offload)
32484 {
32485 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32486
32487 /* Prune stuff so that dwarf2out_finish runs successfully
32488 for the fat part of the object. */
32489 reset_dies (comp_unit_die ());
32490 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32491 reset_dies (node->die);
32492 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32493 {
32494 /* Remove the pointer to the line table. */
32495 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32496 if (debug_info_level >= DINFO_LEVEL_TERSE)
32497 reset_dies (ctnode->root_die);
32498 }
32499
32500 /* Reset die CU symbol so we don't output it twice. */
32501 comp_unit_die ()->die_id.die_symbol = NULL;
32502
32503 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32504 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32505 if (have_macinfo)
32506 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32507
32508 /* Remove indirect string decisions. */
32509 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32510 if (debug_line_str_hash)
32511 {
32512 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32513 debug_line_str_hash = NULL;
32514 if (asm_outputs_debug_line_str ())
32515 {
32516 adjust_name_comp_dir (comp_unit_die ());
32517 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32518 adjust_name_comp_dir (node->die);
32519 }
32520 }
32521 }
32522
32523 #if ENABLE_ASSERT_CHECKING
32524 {
32525 dw_die_ref die = comp_unit_die (), c;
32526 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32527 }
32528 #endif
32529 base_types.truncate (0);
32530 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32531 resolve_addr (ctnode->root_die);
32532 resolve_addr (comp_unit_die ());
32533 move_marked_base_types ();
32534
32535 if (dump_file)
32536 {
32537 fprintf (dump_file, "DWARF for %s\n", filename);
32538 print_die (comp_unit_die (), dump_file);
32539 }
32540
32541 /* Initialize sections and labels used for actual assembler output. */
32542 unsigned generation = init_sections_and_labels (false);
32543
32544 /* Traverse the DIE's and add sibling attributes to those DIE's that
32545 have children. */
32546 add_sibling_attributes (comp_unit_die ());
32547 limbo_die_node *node;
32548 for (node = cu_die_list; node; node = node->next)
32549 add_sibling_attributes (node->die);
32550 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32551 add_sibling_attributes (ctnode->root_die);
32552
32553 /* When splitting DWARF info, we put some attributes in the
32554 skeleton compile_unit DIE that remains in the .o, while
32555 most attributes go in the DWO compile_unit_die. */
32556 if (dwarf_split_debug_info)
32557 {
32558 limbo_die_node *cu;
32559 main_comp_unit_die = gen_compile_unit_die (NULL);
32560 if (dwarf_version >= 5)
32561 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32562 cu = limbo_die_list;
32563 gcc_assert (cu->die == main_comp_unit_die);
32564 limbo_die_list = limbo_die_list->next;
32565 cu->next = cu_die_list;
32566 cu_die_list = cu;
32567 }
32568 else
32569 main_comp_unit_die = comp_unit_die ();
32570
32571 /* Output a terminator label for the .text section. */
32572 switch_to_section (text_section);
32573 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32574 if (cold_text_section)
32575 {
32576 switch_to_section (cold_text_section);
32577 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32578 }
32579
32580 /* We can only use the low/high_pc attributes if all of the code was
32581 in .text. */
32582 if ((!have_multiple_function_sections
32583 && vec_safe_length (switch_text_ranges) < 2)
32584 || (dwarf_version < 3 && dwarf_strict))
32585 {
32586 const char *end_label = text_end_label;
32587 if (vec_safe_length (switch_text_ranges) == 1)
32588 end_label = (*switch_text_ranges)[0];
32589 /* Don't add if the CU has no associated code. */
32590 if (switch_text_ranges)
32591 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32592 end_label, true);
32593 }
32594 else
32595 {
32596 unsigned fde_idx;
32597 dw_fde_ref fde;
32598 bool range_list_added = false;
32599 if (switch_text_ranges)
32600 {
32601 const char *prev_loc = text_section_label;
32602 const char *loc;
32603 unsigned idx;
32604
32605 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32606 if (prev_loc)
32607 {
32608 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32609 loc, &range_list_added, true);
32610 prev_loc = NULL;
32611 }
32612 else
32613 prev_loc = loc;
32614
32615 if (prev_loc)
32616 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32617 text_end_label, &range_list_added, true);
32618 }
32619
32620 if (switch_cold_ranges)
32621 {
32622 const char *prev_loc = cold_text_section_label;
32623 const char *loc;
32624 unsigned idx;
32625
32626 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32627 if (prev_loc)
32628 {
32629 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32630 loc, &range_list_added, true);
32631 prev_loc = NULL;
32632 }
32633 else
32634 prev_loc = loc;
32635
32636 if (prev_loc)
32637 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32638 cold_end_label, &range_list_added, true);
32639 }
32640
32641 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32642 {
32643 if (fde->ignored_debug)
32644 continue;
32645 if (!fde->in_std_section)
32646 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32647 fde->dw_fde_end, &range_list_added,
32648 true);
32649 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32650 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32651 fde->dw_fde_second_end, &range_list_added,
32652 true);
32653 }
32654
32655 if (range_list_added)
32656 {
32657 /* We need to give .debug_loc and .debug_ranges an appropriate
32658 "base address". Use zero so that these addresses become
32659 absolute. Historically, we've emitted the unexpected
32660 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32661 Emit both to give time for other tools to adapt. */
32662 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32663 if (! dwarf_strict && dwarf_version < 4)
32664 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32665
32666 add_ranges (NULL);
32667 have_multiple_function_sections = true;
32668 }
32669 }
32670
32671 /* AIX Assembler inserts the length, so adjust the reference to match the
32672 offset expected by debuggers. */
32673 strcpy (dl_section_ref, debug_line_section_label);
32674 if (XCOFF_DEBUGGING_INFO)
32675 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32676
32677 if (debug_info_level >= DINFO_LEVEL_TERSE)
32678 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32679 dl_section_ref);
32680
32681 if (have_macinfo)
32682 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32683 macinfo_section_label);
32684
32685 if (dwarf_split_debug_info)
32686 {
32687 if (have_location_lists)
32688 {
32689 /* Since we generate the loclists in the split DWARF .dwo
32690 file itself, we don't need to generate a loclists_base
32691 attribute for the split compile unit DIE. That attribute
32692 (and using relocatable sec_offset FORMs) isn't allowed
32693 for a split compile unit. Only if the .debug_loclists
32694 section was in the main file, would we need to generate a
32695 loclists_base attribute here (for the full or skeleton
32696 unit DIE). */
32697
32698 /* optimize_location_lists calculates the size of the lists,
32699 so index them first, and assign indices to the entries.
32700 Although optimize_location_lists will remove entries from
32701 the table, it only does so for duplicates, and therefore
32702 only reduces ref_counts to 1. */
32703 index_location_lists (comp_unit_die ());
32704 }
32705
32706 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32707 index_rnglists ();
32708
32709 if (addr_index_table != NULL)
32710 {
32711 unsigned int index = 0;
32712 addr_index_table
32713 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32714 (&index);
32715 }
32716 }
32717
32718 loc_list_idx = 0;
32719 if (have_location_lists)
32720 {
32721 optimize_location_lists (comp_unit_die ());
32722 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32723 if (dwarf_version >= 5 && dwarf_split_debug_info)
32724 assign_location_list_indexes (comp_unit_die ());
32725 }
32726
32727 save_macinfo_strings ();
32728
32729 if (dwarf_split_debug_info)
32730 {
32731 unsigned int index = 0;
32732
32733 /* Add attributes common to skeleton compile_units and
32734 type_units. Because these attributes include strings, it
32735 must be done before freezing the string table. Top-level
32736 skeleton die attrs are added when the skeleton type unit is
32737 created, so ensure it is created by this point. */
32738 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32739 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32740 }
32741
32742 /* Output all of the compilation units. We put the main one last so that
32743 the offsets are available to output_pubnames. */
32744 for (node = cu_die_list; node; node = node->next)
32745 output_comp_unit (node->die, 0, NULL);
32746
32747 hash_table<comdat_type_hasher> comdat_type_table (100);
32748 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32749 {
32750 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32751
32752 /* Don't output duplicate types. */
32753 if (*slot != HTAB_EMPTY_ENTRY)
32754 continue;
32755
32756 /* Add a pointer to the line table for the main compilation unit
32757 so that the debugger can make sense of DW_AT_decl_file
32758 attributes. */
32759 if (debug_info_level >= DINFO_LEVEL_TERSE)
32760 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32761 (!dwarf_split_debug_info
32762 ? dl_section_ref
32763 : debug_skeleton_line_section_label));
32764
32765 output_comdat_type_unit (ctnode, false);
32766 *slot = ctnode;
32767 }
32768
32769 if (dwarf_split_debug_info)
32770 {
32771 int mark;
32772 struct md5_ctx ctx;
32773
32774 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32775 md5_init_ctx (&ctx);
32776 mark = 0;
32777 die_checksum (comp_unit_die (), &ctx, &mark);
32778 unmark_all_dies (comp_unit_die ());
32779 md5_finish_ctx (&ctx, checksum);
32780
32781 if (dwarf_version < 5)
32782 {
32783 /* Use the first 8 bytes of the checksum as the dwo_id,
32784 and add it to both comp-unit DIEs. */
32785 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32786 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32787 }
32788
32789 /* Add the base offset of the ranges table to the skeleton
32790 comp-unit DIE. */
32791 if (!vec_safe_is_empty (ranges_table))
32792 {
32793 if (dwarf_version < 5)
32794 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32795 ranges_section_label);
32796 }
32797
32798 output_addr_table ();
32799 }
32800
32801 /* Output the main compilation unit if non-empty or if .debug_macinfo
32802 or .debug_macro will be emitted. */
32803 output_comp_unit (comp_unit_die (), have_macinfo,
32804 dwarf_split_debug_info ? checksum : NULL);
32805
32806 if (dwarf_split_debug_info && info_section_emitted)
32807 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32808
32809 /* Output the abbreviation table. */
32810 if (vec_safe_length (abbrev_die_table) != 1)
32811 {
32812 switch_to_section (debug_abbrev_section);
32813 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32814 output_abbrev_section ();
32815 }
32816
32817 /* Output location list section if necessary. */
32818 if (have_location_lists)
32819 {
32820 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32821 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32822 /* Output the location lists info. */
32823 switch_to_section (debug_loc_section);
32824 if (dwarf_version >= 5)
32825 {
32826 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32827 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32828 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32829 dw2_asm_output_data (4, 0xffffffff,
32830 "Initial length escape value indicating "
32831 "64-bit DWARF extension");
32832 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32833 "Length of Location Lists");
32834 ASM_OUTPUT_LABEL (asm_out_file, l1);
32835 output_dwarf_version ();
32836 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32837 dw2_asm_output_data (1, 0, "Segment Size");
32838 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32839 "Offset Entry Count");
32840 }
32841 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32842 if (dwarf_version >= 5 && dwarf_split_debug_info)
32843 {
32844 unsigned int save_loc_list_idx = loc_list_idx;
32845 loc_list_idx = 0;
32846 output_loclists_offsets (comp_unit_die ());
32847 gcc_assert (save_loc_list_idx == loc_list_idx);
32848 }
32849 output_location_lists (comp_unit_die ());
32850 if (dwarf_version >= 5)
32851 ASM_OUTPUT_LABEL (asm_out_file, l2);
32852 }
32853
32854 output_pubtables ();
32855
32856 /* Output the address range information if a CU (.debug_info section)
32857 was emitted. We output an empty table even if we had no functions
32858 to put in it. This because the consumer has no way to tell the
32859 difference between an empty table that we omitted and failure to
32860 generate a table that would have contained data. */
32861 if (info_section_emitted)
32862 {
32863 switch_to_section (debug_aranges_section);
32864 output_aranges ();
32865 }
32866
32867 /* Output ranges section if necessary. */
32868 if (!vec_safe_is_empty (ranges_table))
32869 {
32870 if (dwarf_version >= 5)
32871 {
32872 if (dwarf_split_debug_info)
32873 {
32874 /* We don't know right now whether there are any
32875 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32876 Depending on into which of those two belongs the first
32877 ranges_table entry, emit that section first and that
32878 output_rnglists call will return true if the other kind of
32879 ranges needs to be emitted as well. */
32880 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32881 if (output_rnglists (generation, dwo))
32882 output_rnglists (generation, !dwo);
32883 }
32884 else
32885 output_rnglists (generation, false);
32886 }
32887 else
32888 output_ranges ();
32889 }
32890
32891 /* Have to end the macro section. */
32892 if (have_macinfo)
32893 {
32894 switch_to_section (debug_macinfo_section);
32895 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32896 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32897 : debug_skeleton_line_section_label, false);
32898 dw2_asm_output_data (1, 0, "End compilation unit");
32899 }
32900
32901 /* Output the source line correspondence table. We must do this
32902 even if there is no line information. Otherwise, on an empty
32903 translation unit, we will generate a present, but empty,
32904 .debug_info section. IRIX 6.5 `nm' will then complain when
32905 examining the file. This is done late so that any filenames
32906 used by the debug_info section are marked as 'used'. */
32907 switch_to_section (debug_line_section);
32908 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32909 if (! output_asm_line_debug_info ())
32910 output_line_info (false);
32911
32912 if (dwarf_split_debug_info && info_section_emitted)
32913 {
32914 switch_to_section (debug_skeleton_line_section);
32915 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32916 output_line_info (true);
32917 }
32918
32919 /* If we emitted any indirect strings, output the string table too. */
32920 if (debug_str_hash || skeleton_debug_str_hash)
32921 output_indirect_strings ();
32922 if (debug_line_str_hash)
32923 {
32924 switch_to_section (debug_line_str_section);
32925 const enum dwarf_form form = DW_FORM_line_strp;
32926 debug_line_str_hash->traverse<enum dwarf_form,
32927 output_indirect_string> (form);
32928 }
32929
32930 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32931 symview_upper_bound = 0;
32932 if (zero_view_p)
32933 bitmap_clear (zero_view_p);
32934 }
32935
32936 /* Returns a hash value for X (which really is a variable_value_struct). */
32937
32938 inline hashval_t
32939 variable_value_hasher::hash (variable_value_struct *x)
32940 {
32941 return (hashval_t) x->decl_id;
32942 }
32943
32944 /* Return true if decl_id of variable_value_struct X is the same as
32945 UID of decl Y. */
32946
32947 inline bool
32948 variable_value_hasher::equal (variable_value_struct *x, tree y)
32949 {
32950 return x->decl_id == DECL_UID (y);
32951 }
32952
32953 /* Helper function for resolve_variable_value, handle
32954 DW_OP_GNU_variable_value in one location expression.
32955 Return true if exprloc has been changed into loclist. */
32956
32957 static bool
32958 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32959 {
32960 dw_loc_descr_ref next;
32961 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32962 {
32963 next = loc->dw_loc_next;
32964 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32965 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32966 continue;
32967
32968 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32969 if (DECL_CONTEXT (decl) != current_function_decl)
32970 continue;
32971
32972 dw_die_ref ref = lookup_decl_die (decl);
32973 if (ref)
32974 {
32975 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32976 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32977 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32978 continue;
32979 }
32980 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32981 if (l == NULL)
32982 continue;
32983 if (l->dw_loc_next)
32984 {
32985 if (AT_class (a) != dw_val_class_loc)
32986 continue;
32987 switch (a->dw_attr)
32988 {
32989 /* Following attributes allow both exprloc and loclist
32990 classes, so we can change them into a loclist. */
32991 case DW_AT_location:
32992 case DW_AT_string_length:
32993 case DW_AT_return_addr:
32994 case DW_AT_data_member_location:
32995 case DW_AT_frame_base:
32996 case DW_AT_segment:
32997 case DW_AT_static_link:
32998 case DW_AT_use_location:
32999 case DW_AT_vtable_elem_location:
33000 if (prev)
33001 {
33002 prev->dw_loc_next = NULL;
33003 prepend_loc_descr_to_each (l, AT_loc (a));
33004 }
33005 if (next)
33006 add_loc_descr_to_each (l, next);
33007 a->dw_attr_val.val_class = dw_val_class_loc_list;
33008 a->dw_attr_val.val_entry = NULL;
33009 a->dw_attr_val.v.val_loc_list = l;
33010 have_location_lists = true;
33011 return true;
33012 /* Following attributes allow both exprloc and reference,
33013 so if the whole expression is DW_OP_GNU_variable_value alone
33014 we could transform it into reference. */
33015 case DW_AT_byte_size:
33016 case DW_AT_bit_size:
33017 case DW_AT_lower_bound:
33018 case DW_AT_upper_bound:
33019 case DW_AT_bit_stride:
33020 case DW_AT_count:
33021 case DW_AT_allocated:
33022 case DW_AT_associated:
33023 case DW_AT_byte_stride:
33024 if (prev == NULL && next == NULL)
33025 break;
33026 /* FALLTHRU */
33027 default:
33028 if (dwarf_strict)
33029 continue;
33030 break;
33031 }
33032 /* Create DW_TAG_variable that we can refer to. */
33033 gen_decl_die (decl, NULL_TREE, NULL,
33034 lookup_decl_die (current_function_decl));
33035 ref = lookup_decl_die (decl);
33036 if (ref)
33037 {
33038 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
33039 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
33040 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
33041 }
33042 continue;
33043 }
33044 if (prev)
33045 {
33046 prev->dw_loc_next = l->expr;
33047 add_loc_descr (&prev->dw_loc_next, next);
33048 free_loc_descr (loc, NULL);
33049 next = prev->dw_loc_next;
33050 }
33051 else
33052 {
33053 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
33054 add_loc_descr (&loc, next);
33055 next = loc;
33056 }
33057 loc = prev;
33058 }
33059 return false;
33060 }
33061
33062 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
33063
33064 static void
33065 resolve_variable_value (dw_die_ref die)
33066 {
33067 dw_attr_node *a;
33068 dw_loc_list_ref loc;
33069 unsigned ix;
33070
33071 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
33072 switch (AT_class (a))
33073 {
33074 case dw_val_class_loc:
33075 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
33076 break;
33077 /* FALLTHRU */
33078 case dw_val_class_loc_list:
33079 loc = AT_loc_list (a);
33080 gcc_assert (loc);
33081 for (; loc; loc = loc->dw_loc_next)
33082 resolve_variable_value_in_expr (a, loc->expr);
33083 break;
33084 default:
33085 break;
33086 }
33087 }
33088
33089 /* Attempt to optimize DW_OP_GNU_variable_value refering to
33090 temporaries in the current function. */
33091
33092 static void
33093 resolve_variable_values (void)
33094 {
33095 if (!variable_value_hash || !current_function_decl)
33096 return;
33097
33098 struct variable_value_struct *node
33099 = variable_value_hash->find_with_hash (current_function_decl,
33100 DECL_UID (current_function_decl));
33101
33102 if (node == NULL)
33103 return;
33104
33105 unsigned int i;
33106 dw_die_ref die;
33107 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
33108 resolve_variable_value (die);
33109 }
33110
33111 /* Helper function for note_variable_value, handle one location
33112 expression. */
33113
33114 static void
33115 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
33116 {
33117 for (; loc; loc = loc->dw_loc_next)
33118 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
33119 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
33120 {
33121 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
33122 dw_die_ref ref = lookup_decl_die (decl);
33123 if (! ref && (flag_generate_lto || flag_generate_offload))
33124 {
33125 /* ??? This is somewhat a hack because we do not create DIEs
33126 for variables not in BLOCK trees early but when generating
33127 early LTO output we need the dw_val_class_decl_ref to be
33128 fully resolved. For fat LTO objects we'd also like to
33129 undo this after LTO dwarf output. */
33130 gcc_assert (DECL_CONTEXT (decl));
33131 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
33132 gcc_assert (ctx != NULL);
33133 gen_decl_die (decl, NULL_TREE, NULL, ctx);
33134 ref = lookup_decl_die (decl);
33135 gcc_assert (ref != NULL);
33136 }
33137 if (ref)
33138 {
33139 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
33140 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
33141 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
33142 continue;
33143 }
33144 if (VAR_P (decl)
33145 && DECL_CONTEXT (decl)
33146 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
33147 && lookup_decl_die (DECL_CONTEXT (decl)))
33148 {
33149 if (!variable_value_hash)
33150 variable_value_hash
33151 = hash_table<variable_value_hasher>::create_ggc (10);
33152
33153 tree fndecl = DECL_CONTEXT (decl);
33154 struct variable_value_struct *node;
33155 struct variable_value_struct **slot
33156 = variable_value_hash->find_slot_with_hash (fndecl,
33157 DECL_UID (fndecl),
33158 INSERT);
33159 if (*slot == NULL)
33160 {
33161 node = ggc_cleared_alloc<variable_value_struct> ();
33162 node->decl_id = DECL_UID (fndecl);
33163 *slot = node;
33164 }
33165 else
33166 node = *slot;
33167
33168 vec_safe_push (node->dies, die);
33169 }
33170 }
33171 }
33172
33173 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
33174 with dw_val_class_decl_ref operand. */
33175
33176 static void
33177 note_variable_value (dw_die_ref die)
33178 {
33179 dw_die_ref c;
33180 dw_attr_node *a;
33181 dw_loc_list_ref loc;
33182 unsigned ix;
33183
33184 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
33185 switch (AT_class (a))
33186 {
33187 case dw_val_class_loc_list:
33188 loc = AT_loc_list (a);
33189 gcc_assert (loc);
33190 if (!loc->noted_variable_value)
33191 {
33192 loc->noted_variable_value = 1;
33193 for (; loc; loc = loc->dw_loc_next)
33194 note_variable_value_in_expr (die, loc->expr);
33195 }
33196 break;
33197 case dw_val_class_loc:
33198 note_variable_value_in_expr (die, AT_loc (a));
33199 break;
33200 default:
33201 break;
33202 }
33203
33204 /* Mark children. */
33205 FOR_EACH_CHILD (die, c, note_variable_value (c));
33206 }
33207
33208 /* Process DWARF dies for CTF generation. */
33209
33210 static void
33211 ctf_debug_do_cu (dw_die_ref die)
33212 {
33213 dw_die_ref c;
33214
33215 if (!ctf_do_die (die))
33216 return;
33217
33218 FOR_EACH_CHILD (die, c, ctf_do_die (c));
33219 }
33220
33221 /* Perform any cleanups needed after the early debug generation pass
33222 has run. */
33223
33224 static void
33225 dwarf2out_early_finish (const char *filename)
33226 {
33227 comdat_type_node *ctnode;
33228 set_early_dwarf s;
33229 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
33230
33231 /* PCH might result in DW_AT_producer string being restored from the
33232 header compilation, so always fill it with empty string initially
33233 and overwrite only here. */
33234 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
33235
33236 if (dwarf_record_gcc_switches)
33237 producer_string = gen_producer_string (lang_hooks.name,
33238 save_decoded_options,
33239 save_decoded_options_count);
33240 else
33241 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
33242
33243 producer->dw_attr_val.v.val_str->refcount--;
33244 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
33245
33246 /* Add the name for the main input file now. We delayed this from
33247 dwarf2out_init to avoid complications with PCH. */
33248 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
33249 add_comp_dir_attribute (comp_unit_die ());
33250
33251 /* With LTO early dwarf was really finished at compile-time, so make
33252 sure to adjust the phase after annotating the LTRANS CU DIE. */
33253 if (in_lto_p)
33254 {
33255 early_dwarf_finished = true;
33256 if (dump_file)
33257 {
33258 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
33259 print_die (comp_unit_die (), dump_file);
33260 }
33261 return;
33262 }
33263
33264 /* Walk through the list of incomplete types again, trying once more to
33265 emit full debugging info for them. */
33266 retry_incomplete_types ();
33267
33268 gen_scheduled_generic_parms_dies ();
33269 gen_remaining_tmpl_value_param_die_attribute ();
33270
33271 /* The point here is to flush out the limbo list so that it is empty
33272 and we don't need to stream it for LTO. */
33273 flush_limbo_die_list ();
33274
33275 /* Add DW_AT_linkage_name for all deferred DIEs. */
33276 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
33277 {
33278 tree decl = node->created_for;
33279 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
33280 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
33281 ended up in deferred_asm_name before we knew it was
33282 constant and never written to disk. */
33283 && DECL_ASSEMBLER_NAME (decl))
33284 {
33285 add_linkage_attr (node->die, decl);
33286 move_linkage_attr (node->die);
33287 }
33288 }
33289 deferred_asm_name = NULL;
33290
33291 if (flag_eliminate_unused_debug_types)
33292 prune_unused_types ();
33293
33294 /* Generate separate COMDAT sections for type DIEs. */
33295 if (use_debug_types)
33296 {
33297 break_out_comdat_types (comp_unit_die ());
33298
33299 /* Each new type_unit DIE was added to the limbo die list when created.
33300 Since these have all been added to comdat_type_list, clear the
33301 limbo die list. */
33302 limbo_die_list = NULL;
33303
33304 /* For each new comdat type unit, copy declarations for incomplete
33305 types to make the new unit self-contained (i.e., no direct
33306 references to the main compile unit). */
33307 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33308 copy_decls_for_unworthy_types (ctnode->root_die);
33309 copy_decls_for_unworthy_types (comp_unit_die ());
33310
33311 /* In the process of copying declarations from one unit to another,
33312 we may have left some declarations behind that are no longer
33313 referenced. Prune them. */
33314 prune_unused_types ();
33315 }
33316
33317 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33318 with dw_val_class_decl_ref operand. */
33319 note_variable_value (comp_unit_die ());
33320 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33321 note_variable_value (node->die);
33322 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33323 note_variable_value (ctnode->root_die);
33324 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33325 note_variable_value (node->die);
33326
33327 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33328 both the main_cu and all skeleton TUs. Making this call unconditional
33329 would end up either adding a second copy of the AT_pubnames attribute, or
33330 requiring a special case in add_top_level_skeleton_die_attrs. */
33331 if (!dwarf_split_debug_info)
33332 add_AT_pubnames (comp_unit_die ());
33333
33334 /* The early debug phase is now finished. */
33335 early_dwarf_finished = true;
33336 if (dump_file)
33337 {
33338 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
33339 print_die (comp_unit_die (), dump_file);
33340 }
33341
33342 /* Generate CTF/BTF debug info. */
33343 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33344 || btf_debuginfo_p ()) && lang_GNU_C ())
33345 {
33346 ctf_debug_init ();
33347 ctf_debug_do_cu (comp_unit_die ());
33348 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33349 ctf_debug_do_cu (node->die);
33350
33351 ctf_debug_early_finish (filename);
33352 }
33353
33354 #ifdef CODEVIEW_DEBUGGING_INFO
33355 if (codeview_debuginfo_p ())
33356 codeview_debug_early_finish (comp_unit_die ());
33357 #endif
33358
33359 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33360 if ((!flag_generate_lto && !flag_generate_offload)
33361 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33362 copy_lto_debug_sections operation of the simple object support in
33363 libiberty is not implemented for them yet. */
33364 || TARGET_PECOFF || TARGET_COFF)
33365 return;
33366
33367 /* Now as we are going to output for LTO initialize sections and labels
33368 to the LTO variants. We don't need a random-seed postfix as other
33369 LTO sections as linking the LTO debug sections into one in a partial
33370 link is fine. */
33371 init_sections_and_labels (true);
33372
33373 /* The output below is modeled after dwarf2out_finish with all
33374 location related output removed and some LTO specific changes.
33375 Some refactoring might make both smaller and easier to match up. */
33376
33377 base_types.truncate (0);
33378 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33379 mark_base_types (ctnode->root_die);
33380 mark_base_types (comp_unit_die ());
33381 move_marked_base_types ();
33382
33383 /* Traverse the DIE's and add sibling attributes to those DIE's
33384 that have children. */
33385 add_sibling_attributes (comp_unit_die ());
33386 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33387 add_sibling_attributes (node->die);
33388 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33389 add_sibling_attributes (ctnode->root_die);
33390
33391 /* AIX Assembler inserts the length, so adjust the reference to match the
33392 offset expected by debuggers. */
33393 strcpy (dl_section_ref, debug_line_section_label);
33394 if (XCOFF_DEBUGGING_INFO)
33395 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33396
33397 if (debug_info_level >= DINFO_LEVEL_TERSE)
33398 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
33399
33400 if (have_macinfo)
33401 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33402 macinfo_section_label);
33403
33404 save_macinfo_strings ();
33405
33406 if (dwarf_split_debug_info)
33407 {
33408 unsigned int index = 0;
33409 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
33410 }
33411
33412 /* Output all of the compilation units. We put the main one last so that
33413 the offsets are available to output_pubnames. */
33414 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33415 output_comp_unit (node->die, 0, NULL);
33416
33417 hash_table<comdat_type_hasher> comdat_type_table (100);
33418 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33419 {
33420 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
33421
33422 /* Don't output duplicate types. */
33423 if (*slot != HTAB_EMPTY_ENTRY)
33424 continue;
33425
33426 /* Add a pointer to the line table for the main compilation unit
33427 so that the debugger can make sense of DW_AT_decl_file
33428 attributes. */
33429 if (debug_info_level >= DINFO_LEVEL_TERSE)
33430 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
33431 (!dwarf_split_debug_info
33432 ? debug_line_section_label
33433 : debug_skeleton_line_section_label));
33434
33435 output_comdat_type_unit (ctnode, true);
33436 *slot = ctnode;
33437 }
33438
33439 /* Stick a unique symbol to the main debuginfo section. */
33440 compute_comp_unit_symbol (comp_unit_die ());
33441
33442 /* Output the main compilation unit. We always need it if only for
33443 the CU symbol. */
33444 output_comp_unit (comp_unit_die (), true, NULL);
33445
33446 /* Output the abbreviation table. */
33447 if (vec_safe_length (abbrev_die_table) != 1)
33448 {
33449 switch_to_section (debug_abbrev_section);
33450 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33451 output_abbrev_section ();
33452 }
33453
33454 /* Have to end the macro section. */
33455 if (have_macinfo)
33456 {
33457 /* We have to save macinfo state if we need to output it again
33458 for the FAT part of the object. */
33459 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33460 if (flag_fat_lto_objects)
33461 macinfo_table = macinfo_table->copy ();
33462
33463 switch_to_section (debug_macinfo_section);
33464 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33465 output_macinfo (debug_line_section_label, true);
33466 dw2_asm_output_data (1, 0, "End compilation unit");
33467
33468 if (flag_fat_lto_objects)
33469 {
33470 vec_free (macinfo_table);
33471 macinfo_table = saved_macinfo_table;
33472 }
33473 }
33474
33475 /* Emit a skeleton debug_line section. */
33476 switch_to_section (debug_line_section);
33477 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33478 output_line_info (true);
33479
33480 /* If we emitted any indirect strings, output the string table too. */
33481 if (debug_str_hash || skeleton_debug_str_hash)
33482 output_indirect_strings ();
33483 if (debug_line_str_hash)
33484 {
33485 switch_to_section (debug_line_str_section);
33486 const enum dwarf_form form = DW_FORM_line_strp;
33487 debug_line_str_hash->traverse<enum dwarf_form,
33488 output_indirect_string> (form);
33489 }
33490
33491 /* Switch back to the text section. */
33492 switch_to_section (text_section);
33493 }
33494
33495 /* Reset all state within dwarf2out.cc so that we can rerun the compiler
33496 within the same process. For use by toplev::finalize. */
33497
33498 void
33499 dwarf2out_cc_finalize (void)
33500 {
33501 last_var_location_insn = NULL;
33502 cached_next_real_insn = NULL;
33503 used_rtx_array = NULL;
33504 incomplete_types = NULL;
33505 debug_info_section = NULL;
33506 debug_skeleton_info_section = NULL;
33507 debug_abbrev_section = NULL;
33508 debug_skeleton_abbrev_section = NULL;
33509 debug_aranges_section = NULL;
33510 debug_addr_section = NULL;
33511 debug_macinfo_section = NULL;
33512 debug_line_section = NULL;
33513 debug_skeleton_line_section = NULL;
33514 debug_loc_section = NULL;
33515 debug_pubnames_section = NULL;
33516 debug_pubtypes_section = NULL;
33517 debug_str_section = NULL;
33518 debug_line_str_section = NULL;
33519 debug_str_dwo_section = NULL;
33520 debug_str_offsets_section = NULL;
33521 debug_ranges_section = NULL;
33522 debug_ranges_dwo_section = NULL;
33523 debug_frame_section = NULL;
33524 fde_vec = NULL;
33525 debug_str_hash = NULL;
33526 debug_line_str_hash = NULL;
33527 skeleton_debug_str_hash = NULL;
33528 dw2_string_counter = 0;
33529 have_multiple_function_sections = false;
33530 in_text_section_p = false;
33531 cold_text_section = NULL;
33532 last_text_label = NULL;
33533 last_cold_label = NULL;
33534 switch_text_ranges = NULL;
33535 switch_cold_ranges = NULL;
33536 current_unit_personality = NULL;
33537
33538 early_dwarf = false;
33539 early_dwarf_finished = false;
33540
33541 next_die_offset = 0;
33542 single_comp_unit_die = NULL;
33543 comdat_type_list = NULL;
33544 limbo_die_list = NULL;
33545 file_table = NULL;
33546 decl_die_table = NULL;
33547 common_block_die_table = NULL;
33548 decl_loc_table = NULL;
33549 call_arg_locations = NULL;
33550 call_arg_loc_last = NULL;
33551 call_site_count = -1;
33552 tail_call_site_count = -1;
33553 cached_dw_loc_list_table = NULL;
33554 abbrev_die_table = NULL;
33555 delete dwarf_proc_stack_usage_map;
33556 dwarf_proc_stack_usage_map = NULL;
33557 line_info_label_num = 0;
33558 cur_line_info_table = NULL;
33559 text_section_line_info = NULL;
33560 cold_text_section_line_info = NULL;
33561 separate_line_info = NULL;
33562 info_section_emitted = false;
33563 pubname_table = NULL;
33564 pubtype_table = NULL;
33565 macinfo_table = NULL;
33566 ranges_table = NULL;
33567 ranges_by_label = NULL;
33568 rnglist_idx = 0;
33569 have_location_lists = false;
33570 loclabel_num = 0;
33571 poc_label_num = 0;
33572 last_emitted_file = NULL;
33573 label_num = 0;
33574 tmpl_value_parm_die_table = NULL;
33575 generic_type_instances = NULL;
33576 frame_pointer_fb_offset = 0;
33577 frame_pointer_fb_offset_valid = false;
33578 base_types.release ();
33579 XDELETEVEC (producer_string);
33580 producer_string = NULL;
33581 output_line_info_generation = 0;
33582 init_sections_and_labels_generation = 0;
33583 }
33584
33585 #include "gt-dwarf2out.h"